. * * ------------------------------------------------------------------------ * * This file is used to manage the actions in package for deploy system. * * ------------------------------------------------------------------------ * * @package FusionInventory * @author Walid Nouh * @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 user interactions templates. * @since 9.2 */ class PluginFusioninventoryDeployUserinteractionTemplate extends CommonDropdown { /** * The right name for this class * * @var string */ static $rightname = 'plugin_fusioninventory_userinteractiontemplate'; const ALERT_WTS = 'win32'; //Alerts for win32 platform (WTS API) //Behaviors (to sent to the agent) : //in two parts : //- left part is the instruction for the agent //- right part is the code that the agent returns to the server //Continue a software deployment const BEHAVIOR_CONTINUE_DEPLOY = 'continue:continue'; //Cancel a software deployment const BEHAVIOR_STOP_DEPLOY = 'stop:stop'; //Postpone a software deployment const BEHAVIOR_POSTPONE_DEPLOY = 'stop:postpone'; //Available buttons for Windows WTS API const WTS_BUTTON_OK_SYNC = 'ok'; const WTS_BUTTON_OK_ASYNC = 'ok_async'; const WTS_BUTTON_OK_CANCEL = 'okcancel'; const WTS_BUTTON_YES_NO = 'yesno'; const WTS_BUTTON_ABORT_RETRY_IGNORE = 'abortretryignore'; const WTS_BUTTON_RETRY_CANCEL = 'retrycancel'; const WTS_BUTTON_YES_NO_CANCEL = 'yesnocancel'; const WTS_BUTTON_CANCEL_TRY_CONTINUE = 'canceltrycontinue'; //Icons to be displayed const WTS_ICON_NONE = 'none'; const WTS_ICON_WARNING = 'warn'; const WTS_ICON_QUESTION = 'question'; const WTS_ICON_INFO = 'info'; const WTS_ICON_ERROR = 'error'; /** * @see CommonGLPI::defineTabs() **/ function defineTabs($options = []) { $ong = []; $this->addStandardTab(__CLASS__, $ong, $options) ->addStandardTab('Log', $ong, $options); return $ong; } /** * @see CommonGLPI::getTabNameForItem() **/ function getTabNameForItem(CommonGLPI $item, $withtemplate = 0) { $tabs[1] = __('General'); $tabs[2] = _n('Behavior', 'Behaviors', 2, 'fusioninventory'); return $tabs; } /** * @param $item CommonGLPI object * @param $tabnum (default 1) * @param $withtemplate (default 0) **/ static function displayTabContentForItem(CommonGLPI $item, $tabnum = 1, $withtemplate = 0) { global $CFG_GLPI; if ($item->getType() == __CLASS__) { switch ($tabnum) { case 1 : $item->showForm($item->fields['id']); break; case 2 : $item->showBehaviors($item->fields['id']); break; } } return true; } /** * Get name of this type by language of the user connected * * @param integer $nb number of elements * @return string name of this type */ static function getTypeName($nb = 0) { return _n('User interaction template', 'User interaction templates', $nb, 'fusioninventory'); } /** * Get list of supported interaction methods * * @since 9.2 * @return array */ static function getTypes() { return [self::ALERT_WTS => __("Windows system alert (WTS)", 'fusioninventory')]; } /** * Display a dropdown with the list of alert types * * @since 9.2 * @param type the type of alert (if one already selected) * @return rand */ function dropdownTypes($type = self::ALERT_WTS) { return Dropdown::showFromArray('platform', self::getTypes(), ['value' => $type]); } /** * Get available buttons for alerts, by interaction type * * @since 9.2 * @param interaction_type the type of interaction * @return array */ static function getButtons($interaction_type = '') { $interactions = [ self::ALERT_WTS => [ self::WTS_BUTTON_OK_SYNC => __('OK', 'fusioninventory'), self::WTS_BUTTON_OK_ASYNC => __('OK (asynchronous)', 'fusioninventory'), self::WTS_BUTTON_OK_CANCEL => __('OK - Cancel', 'fusioninventory'), self::WTS_BUTTON_YES_NO => __('Yes - No', 'fusioninventory'), self::WTS_BUTTON_ABORT_RETRY_IGNORE => __('OK - Abort - Retry', 'fusioninventory'), self::WTS_BUTTON_RETRY_CANCEL => __('Retry - Cancel', 'fusioninventory'), self::WTS_BUTTON_ABORT_RETRY_IGNORE => __('Abort - Retry - Ignore', 'fusioninventory'), self::WTS_BUTTON_CANCEL_TRY_CONTINUE => __('Cancel - Try - Continue', 'fusioninventory'), self::WTS_BUTTON_YES_NO_CANCEL => __('Yes - No - Cancel', 'fusioninventory') ] ]; if (isset($interactions[$interaction_type])) { return $interactions[$interaction_type]; } else { return false; } } /** * Display a dropdown with the list of buttons available * * @since 9.2 * @param type the type of button (if one already selected) * @return rand */ public function dropdownButtons($button = self::WTS_BUTTON_OK_SYNC) { return Dropdown::showFromArray('buttons', self::getButtons(self::ALERT_WTS), ['value' => $button]); } /** * Get available icons for alerts, by interaction type * * @since 9.2 * @param interaction_type the type of interaction * @return array */ static function getIcons($interaction_type = self::ALERT_WTS) { $icons = [ self::ALERT_WTS => [ self::WTS_ICON_NONE => __('None'), self::WTS_ICON_WARNING => __('Warning'), self::WTS_ICON_INFO => _n('Information', 'Informations', 1), self::WTS_ICON_ERROR => __('Error'), self::WTS_ICON_QUESTION => __('Question', 'fusioninventory') ] ]; if (isset($icons[$interaction_type])) { return $icons[$interaction_type]; } else { return false; } } /** * Display a dropdown with the list of buttons available * * @since 9.2 * @param type the type of button (if one already selected) * @return rand */ function dropdownIcons($icon = self::WTS_ICON_NONE) { return Dropdown::showFromArray('icon', self::getIcons(), ['value' => $icon]); } /** * Get available behaviors in case of user interactions * * @since 9.2 * @return array */ static function getBehaviors() { return [self::BEHAVIOR_CONTINUE_DEPLOY => __('Continue job with no user interaction', 'fusioninventory'), self::BEHAVIOR_POSTPONE_DEPLOY => __('Retry job later', 'fusioninventory'), self::BEHAVIOR_STOP_DEPLOY => __('Cancel job', 'fusioninventory') ]; } /** * Display a dropdown with the list of available behaviors * * @since 9.2 * @param type the type of bahaviors (if one already selected) * @return rand */ function dropdownBehaviors($name, $behavior = self::BEHAVIOR_CONTINUE_DEPLOY) { return Dropdown::showFromArray($name, self::getBehaviors(), ['value' => $behavior]); } /** * Get the fields to be encoded in json * @since 9.2 * @return an array of field names */ function getJsonFields() { return array_merge($this->getMainFormFields(), $this->getBehaviorsFields()); } /** * Get the fields to be encoded in json * @since 9.2 * @return an array of field names */ function getMainFormFields() { return ['platform', 'timeout', 'buttons', 'icon', 'retry_after', 'nb_max_retry']; } /** * Get the fields to be encoded in json * @since 9.2 * @return an array of field names */ function getBehaviorsFields() { return ['on_timeout', 'on_nouser', 'on_multiusers', 'on_ok', 'on_no', 'on_yes', 'on_cancel', 'on_abort', 'on_retry', 'on_tryagain', 'on_ignore', 'on_continue', 'on_async']; } /** * Initialize json fields * @since 9.2 * * @return an array of field names */ function initializeJsonFields($json_fields) { foreach ($this->getJsonFields() as $field) { if (!isset($json_fields[$field])) { $json_fields[$field] = $this->getDefaultBehaviorForAButton($field); } } return $json_fields; } /** * Save form data as a json encoded array * @since 9.2 * @param params form parameters * @return json encoded array */ function saveToJson($params = []) { $result = []; foreach ($this->getJsonFields() as $field) { if (isset($params[$field])) { $result[$field] = $params[$field]; } } return json_encode($result); } /** * Add the json template fields to package * * @since 9.2 * @param the input array * @param array now containing input data + data from the template */ function addJsonFieldsToArray($params = []) { $fields = json_decode($this->fields['json'], true); foreach ($this->getJsonFields() as $field) { if (isset($fields[$field])) { $params[$field] = $fields[$field]; } } //If we deal with an asynchronous OK, then wait must be set to 0 if ($params['buttons'] == self::WTS_BUTTON_OK_ASYNC) { $params['buttons'] = self::WTS_BUTTON_OK_SYNC; $params['wait'] = 'no'; } else { //Otherwise wait is 1 $params['wait'] = 'yes'; } return $params; } /** * Display an interaction template form * @since 9.2 * @param $id id of a template to edit * @param options POST form options */ function showForm($ID, $options = []) { $this->initForm($ID); $this->showFormHeader(); $json_data = json_decode($this->fields['json'], true); $json_data = $this->initializeJsonFields($json_data); echo "