.
*
* ------------------------------------------------------------------------
*
* This file is used to manage the actions in package for deploy system.
*
* ------------------------------------------------------------------------
*
* @package FusionInventory
* @author Alexandre Delaunay
* @author David Durieux
* @copyright Copyright (c) 2010-2016 FusionInventory team
* @license AGPL License 3.0 or (at your option) any later version
* http://www.gnu.org/licenses/agpl-3.0-standalone.html
* @link http://www.fusioninventory.org/
* @link https://github.com/fusioninventory/fusioninventory-for-glpi
*
*/
if (!defined('GLPI_ROOT')) {
die("Sorry. You can't access directly to this file");
}
/**
* Manage the actions in package for deploy system.
*/
class PluginFusioninventoryDeployAction extends PluginFusioninventoryDeployPackageItem {
public $shortname = 'actions';
public $json_name = 'actions';
/**
* Get list of return actions available
*
* @return array
*/
function getReturnActionNames() {
return [
0 => Dropdown::EMPTY_VALUE,
'okCode' => __("Return code is equal to", 'fusioninventory'),
'errorCode' => __("Return code is not equal to", 'fusioninventory'),
'okPattern' => __("Command output contains", 'fusioninventory'),
'errorPattern' => __("Command output does not contains", 'fusioninventory')
];
}
/**
* Get types of actions with name => description
*
* @return array
*/
function getTypes() {
return [
'cmd' => __('Command', 'fusioninventory'),
'move' => __('Move', 'fusioninventory'),
'copy' => __('Copy', 'fusioninventory'),
'delete' => __('Delete directory', 'fusioninventory'),
'mkdir' => __('Create directory', 'fusioninventory')
];
}
/**
* Get description of the type name
*
* @param string $type name of the type
* @return string mapped with the type
*/
function getLabelForAType($type) {
$a_types = $this->getTypes();
if (isset($a_types[$type])) {
return $a_types[$type];
}
return $type;
}
/**
* Display form
*
* @param object $package PluginFusioninventoryDeployPackage instance
* @param array $request_data
* @param string $rand unique element id used to identify/update an element
* @param string $mode possible values: init|edit|create
*/
function displayForm(PluginFusioninventoryDeployPackage $package, $request_data, $rand, $mode) {
/*
* Get element config in 'edit' mode
*/
$config = null;
if ($mode === self::EDIT && isset($request_data['index'])) {
/*
* Add an hidden input about element's index to be updated
*/
echo "";
$element = $package->getSubElement($this->shortname, $request_data['index']);
if (is_array($element) && count($element) == 1) {
reset($element);
$type = key($element);
$config = ['type' => $type, 'data' => $element[$type]];
}
}
/*
* Display start of div form
*/
if (in_array($mode, [self::INIT], true)) {
echo "
";
}
/*
* Display element's dropdownType in 'create' or 'edit' mode
*/
if (in_array($mode, [self::CREATE, self::EDIT], true)) {
$this->displayDropdownType($package, $config, $rand, $mode);
}
/*
* Display element's values in 'edit' mode only.
* In 'create' mode, those values are refreshed with dropdownType 'change'
* javascript event.
*/
if (in_array($mode, [self::CREATE, self::EDIT], true)) {
echo "";
if ($mode === self::EDIT) {
$this->displayAjaxValues($config, $request_data, $rand, $mode);
}
echo "";
}
/*
* Close form div
*/
if (in_array($mode, [self::INIT], true)) {
echo "
";
}
}
/**
* Display list of actions
*
* @global array $CFG_GLPI
* @param object $package PluginFusioninventoryDeployPackage instance
* @param array $data array converted of 'json' field in DB where stored actions
* @param string $rand unique element id used to identify/update an element
*/
function displayList(PluginFusioninventoryDeployPackage $package, $data, $rand) {
global $CFG_GLPI;
$canedit = $package->canUpdateContent();
$package_id = $package->getID();
echo "
";
$i=0;
foreach ($data['jobs'][$this->json_name] as $action) {
echo Search::showNewLine(Search::HTML_OUTPUT, ($i%2));
if ($canedit) {
echo "
";
}
$this->addOrSaveButton($pfDeployPackage, $mode);
echo "";
}
/**
* Add a new item in actions of the package
*
* @param array $params list of fields with value of the action
*/
function add_item($params) {
//prepare new action entry to insert in json
$fields = ['list', 'from', 'to', 'exec', 'name', 'logLineLimit'];
foreach ($fields as $field) {
if (isset($params[$field])) {
$tmp[$field] = $params[$field];
}
}
//process ret checks
if (isset($params['retchecks_type'])
&& !empty($params['retchecks_type'])) {
foreach ($params['retchecks_type'] as $index => $type) {
if ($type !== '0') {
$tmp['retChecks'][] = [
'type' => $type,
'values' => [$params['retchecks_value'][$index]]
];
}
}
}
//append prepared data to new entry
$new_entry[$params['actionstype']] = $tmp;
//get current order json
$data = json_decode($this->getJson($params['id']), true);
//add new entry
$data['jobs'][$this->json_name][] = $new_entry;
//update order
$this->updateOrderJson($params['id'], $data);
}
/**
* Save the item in actions
*
* @param array $params list of fields with value of the action
*/
function save_item($params) {
$tmp = [];
$fields = ['list', 'from', 'to', 'exec', 'name', 'logLineLimit'];
foreach ($fields as $field) {
if (isset($params[$field])) {
$tmp[$field] = $params[$field];
}
}
//process ret checks
if (isset($params['retchecks_type']) && !empty($params['retchecks_type'])) {
foreach ($params['retchecks_type'] as $index => $type) {
//if type == '0', this means nothing is selected
if ($type !== '0') {
$tmp['retChecks'][] = [
'type' => $type,
'values' => [$params['retchecks_value'][$index]]
];
}
}
}
//append prepared data to new entry
$entry[$params['actionstype']] = $tmp;
//update order
$this->updateOrderJson($params['id'],
$this->prepareDataToSave($params, $entry));
}
}