. * * ------------------------------------------------------------------------ * * 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 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 ""; } $keys = array_keys($action); $action_type = array_shift($keys); echo ""; echo ""; if ($canedit) { echo ""; } echo ""; $i++; } if ($canedit) { echo ""; } echo "
"; Html::showCheckbox(['name' => 'actions_entries['.$i.']']); echo ""; if ($canedit) { echo ""; } echo $this->getLabelForAType($action_type); if ($canedit) { echo ""; } echo "
"; foreach ($action[$action_type] as $key => $value) { if (is_array($value)) { if ($key === "list") { foreach ($value as $list) { echo $list; echo " "; } } } else { echo ""; if ($key == 'exec') { echo __('Command to execute', 'fusioninventory'); } else { echo $key; } echo ""; if ($key ==="exec") { echo "
$value
"; } else { echo " $value "; } } } if (isset($action[$action_type]['retChecks'])) { echo "
".__("return codes saved for this command", 'fusioninventory'). " :
    "; foreach ($action[$action_type]['retChecks'] as $retCheck) { echo "
  • "; $getReturnActionNames = $this->getReturnActionNames(); echo $getReturnActionNames[$retCheck['type']]." ".array_shift($retCheck['values']); echo "
  • "; } echo "
"; } echo "
"; echo Html::getCheckAllAsCheckbox("actionsList$rand", mt_rand()); echo "
"; if ($canedit) { echo "  "; echo ""; } } /** * Display different fields relative the action selected (cmd, move...) * * @param array $config * @param array $request_data * @param string $mode mode in use (create, edit...) * @return boolean */ function displayAjaxValues($config, $request_data, $rand, $mode) { global $CFG_GLPI; $mandatory_mark = $this->getMandatoryMark(); $pfDeployPackage = new PluginFusioninventoryDeployPackage(); if (isset($request_data['packages_id'])) { $pfDeployPackage->getFromDB($request_data['packages_id']); } else { $pfDeployPackage->getEmpty(); } /* * Get type from request params */ $type = null; if ($mode === self::CREATE) { $type = $request_data['value']; } else { $type = $config['type']; $config_data = $config['data']; } /* * Set default values */ $value_type_1 = "input"; $value_1 = ""; $value_2 = ""; $retChecks = null; $name_label = __('Action label', 'fusioninventory'); $name_value = (isset($config_data['name']))?$config_data['name']:""; $name_type = "input"; $logLineLimit = (isset($config_data['logLineLimit']))?$config_data['logLineLimit']:100; /* * set values from element's config in 'edit' mode */ switch ($type) { case 'move': case 'copy': $value_label_1 = __("From", 'fusioninventory'); $name_label_1 = "from"; $value_label_2 = __("To", 'fusioninventory'); $name_label_2 = "to"; if ($mode === self::EDIT) { $value_1 = $config_data['from']; $value_2 = $config_data['to']; } break; case 'cmd': $value_label_1 = __("exec", 'fusioninventory'); $name_label_1 = "exec"; $value_label_2 = false; $value_type_1 = "textarea"; if ($mode === self::EDIT) { $value_1 = $config_data['exec']; if (isset($config_data['retChecks'])) { $retChecks = $config_data['retChecks']; } } break; case 'delete': case 'mkdir': $value_label_1 = __("path", 'fusioninventory'); $name_label_1 = "list[]"; $value_label_2 = false; if ($mode === self::EDIT) { /* * TODO : Add list input like `retChecks` on `mkdir` and `delete` * because those methods are defined as list in specification */ $value_1 = array_shift($config_data['list']); } break; default: return false; } echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; if ($value_label_2 !== false) { echo ""; echo ""; echo ""; echo ""; } //specific case for cmd : add retcheck form if ($type == "cmd") { echo ""; echo ""; echo ""; echo ""; echo ""; 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)); } }
".__('Action label', 'fusioninventory')."
$value_label_1 ".$mandatory_mark.""; switch ($value_type_1) { case "input": echo ""; break; case "textarea": echo ""; break; } echo "
".$value_label_2." ".$mandatory_mark."
".__("Execution checks", 'fusioninventory'); PluginFusioninventoryDeployPackage::plusButton("retchecks", ".table_retchecks.template"); echo ""; echo ""; if (is_array($retChecks) && count($retChecks)) { foreach ($retChecks as $retcheck) { echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo "
"; Dropdown::showFromArray('retchecks_type[]', self::getReturnActionNames(), [ 'value' => $retcheck['type'], 'width' => '200px' ] ); echo ""; echo ""; echo "
"; } } echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; } if ($type == 'cmd') { echo "
".__('Number of output lines to retrieve', 'fusioninventory').""; $options = ['min' => 0, 'max' => 5000, 'step' => 10, 'toadd' => [0 => __('None'), -1 => __('All')], 'value' => (isset($config_data['logLineLimit']))?$config_data['logLineLimit']:10 ]; Dropdown::showNumber('logLineLimit', $options); echo " "; echo __('Fusioninventory-Agent 2.3.20 or higher mandatory'); echo "