/**
  * @test
  */
 public function prepareDB()
 {
     global $DB;
     $DB->connect();
     $pfDeployPackage = new PluginFusioninventoryDeployPackage();
     $pfDeployGroup = new PluginFusioninventoryDeployGroup();
     $pfTask = new PluginFusioninventoryTask();
     $pfTaskjob = new PluginFusioninventoryTaskjob();
     $pfTaskjobState = new PluginFusioninventoryTaskjobstate();
     $pfTaskjoblog = new PluginFusioninventoryTaskjoblog();
     $pfDeployGroup_Dynamicdata = new PluginFusioninventoryDeployGroup_Dynamicdata();
     // Create package
     $input = array('entities_id' => 0, 'name' => 'package');
     $packages_id = $pfDeployPackage->add($input);
     // Create fusioninventory dynamic group
     $input = array('name' => 'all computers have name computer', 'type' => 'DYNAMIC');
     $groups_id = $pfDeployGroup->add($input);
     $input = array('plugin_fusioninventory_deploygroups_id' => $groups_id, 'fields_array' => 'a:2:{s:8:"criteria";a:1:{i:0;a:3:{s:5:"field";s:1:"1";s:10:"searchtype";s:8:"contains";s:5:"value";s:8:"computer";}}s:12:"metacriteria";s:0:"";}');
     $pfDeployGroup_Dynamicdata->add($input);
     // create task
     $input = array('entities_id' => 0, 'name' => 'deploy', 'is_active' => 1);
     self::$tasks_id = $pfTask->add($input);
     // create taskjob
     $input = array('plugin_fusioninventory_tasks_id' => self::$tasks_id, 'entities_id' => 0, 'name' => 'deploy', 'method' => 'deployinstall', 'targets' => '[{"PluginFusioninventoryDeployPackage":"' . $packages_id . '"}]', 'actors' => '[{"PluginFusioninventoryDeployGroup":"' . self::$tasks_id . '"}]');
     self::$taskjobs_id = $pfTaskjob->add($input);
     //create taskjobstate
     $input = array('plugin_fusioninventory_taskjobs_id' => self::$taskjobs_id, 'items_id' => 0, 'itemtype' => 'Computer', 'state' => PluginFusioninventoryTaskjobstate::FINISHED, 'plugin_fusioninventory_agents_id' => 0, 'specificity' => 0, 'uniqid' => 0);
     self::$taskjobstates_id = $pfTaskjobState->add($input);
     //crfeate taskjoblogR
     $input = array('plugin_fusioninventory_taskjobstates_id' => self::$taskjobstates_id, 'date ' => date('Y-m-d H:i:s'), 'items_id' => 0, 'itemtype' => 'Computer', 'state' => PluginFusioninventoryTaskjoblog::TASK_RUNNING, 'comment' => "1 ==devicesfound==");
     self::$taskjoblogs_id = $pfTaskjoblog->add($input);
 }
 static function displayTabContentForItem(CommonGLPI $item, $tabnum = 1, $withtemplate = 0)
 {
     switch (get_class($item)) {
         case 'PluginFusioninventoryDeployPackage':
             PluginFusioninventoryDeployPackage::displayOrderTypeForm(PluginFusioninventoryDeployOrder::INSTALLATION_ORDER, $item->getID(), $item);
             break;
     }
 }
 /**
  * @test
  */
 public function prepareDB()
 {
     global $DB;
     $DB->connect();
     $computer = new Computer();
     $pfAgent = new PluginFusioninventoryAgent();
     $pfDeployPackage = new PluginFusioninventoryDeployPackage();
     $pfDeployGroup = new PluginFusioninventoryDeployGroup();
     $pfTask = new PluginFusioninventoryTask();
     $pfTaskjob = new PluginFusioninventoryTaskjob();
     $pfDeployGroup_Dynamicdata = new PluginFusioninventoryDeployGroup_Dynamicdata();
     // Create package
     $input = array('entities_id' => 0, 'name' => 'package');
     $packages_id = $pfDeployPackage->add($input);
     // Create fusioninventory dynamic group
     $input = array('name' => 'all computers have name computer', 'type' => 'DYNAMIC');
     $groups_id = $pfDeployGroup->add($input);
     $input = array('plugin_fusioninventory_deploygroups_id' => $groups_id, 'fields_array' => 'a:2:{s:8:"criteria";a:1:{i:0;a:3:{s:5:"field";s:1:"1";s:10:"searchtype";s:8:"contains";s:5:"value";s:8:"computer";}}s:12:"metacriteria";s:0:"";}');
     $pfDeployGroup_Dynamicdata->add($input);
     // create task
     $input = array('entities_id' => 0, 'name' => 'deploy', 'is_active' => 1);
     $tasks_id = $pfTask->add($input);
     // create takjob
     $input = array('plugin_fusioninventory_tasks_id' => $tasks_id, 'entities_id' => 0, 'name' => 'deploy', 'method' => 'deployinstall', 'targets' => '[{"PluginFusioninventoryDeployPackage":"' . $packages_id . '"}]', 'actors' => '[{"PluginFusioninventoryDeployGroup":"' . $tasks_id . '"}]');
     $pfTaskjob->add($input);
     // Create computers + agents
     $input = array('entities_id' => 0, 'name' => 'computer1');
     $computers_id = $computer->add($input);
     $input = array('entities_id' => 0, 'name' => 'portdavid', 'version' => '{"INVENTORY":"v2.3.11"}', 'device_id' => 'portdavid', 'useragent' => 'FusionInventory-Agent_v2.3.11', 'computers_id' => $computers_id);
     $pfAgent->add($input);
     $input = array('entities_id' => 0, 'name' => 'computer2');
     $computers_id = $computer->add($input);
     $input = array('entities_id' => 0, 'name' => 'computer2', 'version' => '{"INVENTORY":"v2.3.11"}', 'device_id' => 'computer2', 'useragent' => 'FusionInventory-Agent_v2.3.11', 'computers_id' => $computers_id);
     $pfAgent->add($input);
     $input = array('entities_id' => 0, 'name' => 'computer3');
     $computers_id = $computer->add($input);
     $input = array('entities_id' => 0, 'name' => 'computer3', 'version' => '{"INVENTORY":"v2.3.11"}', 'device_id' => 'computer3', 'useragent' => 'FusionInventory-Agent_v2.3.11', 'computers_id' => $computers_id);
     $pfAgent->add($input);
 }
 static function displayAjaxValues($config, $request_data, $rand, $mode)
 {
     global $CFG_GLPI;
     $pfDeployPackage = new PluginFusioninventoryDeployPackage();
     $pfDeployOrder = new PluginFusioninventoryDeployOrder();
     if (isset($request_data['orders_id'])) {
         $pfDeployOrder->getFromDB($request_data['orders_id']);
         $pfDeployPackage->getFromDB($pfDeployOrder->fields['plugin_fusioninventory_deploypackages_id']);
     } else {
         $pfDeployPackage->getEmpty();
     }
     $p2p = 0;
     $p2p_retention_duration = 0;
     $uncompress = 0;
     if ($mode === 'create') {
         $source = $request_data['value'];
         /**
          * No need to continue if there is no selected source
          */
         if ($source === '0') {
             return;
         }
     } else {
         $p2p = $config['data']['p2p'];
         $p2p_retention_duration = $config['data']['p2p-retention-duration'];
         $uncompress = $config['data']['uncompress'];
     }
     echo "<table class='package_item'>";
     /*
      * Display file upload input only in 'create' mode
      */
     echo "<tr>";
     echo "<th>" . __("File", 'fusioninventory') . "</th>";
     echo "<td>";
     if ($mode === 'create') {
         switch ($source) {
             case "Computer":
                 echo "<input type='file' name='file' value='" . __("filename", 'fusioninventory') . "' />";
                 echo "<i>" . self::getMaxUploadSize() . "</i>";
                 break;
             case "Server":
                 echo "<input type='text' name='filename' id='server_filename{$rand}'" . " style='width:120px;float:left' />";
                 echo "<input type='button' class='submit' value='" . __("Choose", 'fusioninventory') . "' onclick='fileModal{$rand}.dialog(\"open\");' />";
                 Ajax::createModalWindow("fileModal{$rand}", $CFG_GLPI['root_doc'] . "/plugins/fusioninventory/ajax/deployfilemodal.php", array('title' => __('Select the file on server', 'fusioninventory'), 'extraparams' => array('rand' => $rand)));
                 break;
         }
     } else {
         /*
          * Display only name in 'edit' mode
          */
         echo $config['data']['name'];
     }
     echo "</td>";
     echo "</tr>";
     echo "<tr>";
     echo "<th>" . __("Uncompress", 'fusioninventory') . "<img style='float:right' " . "src='" . $CFG_GLPI["root_doc"] . "/plugins/fusioninventory//pics/uncompress.png' /></th>";
     echo "<td>";
     Html::showCheckbox(array('name' => 'uncompress', 'checked' => $uncompress));
     echo "</td>";
     echo "</tr><tr>";
     echo "<th>" . __("P2P", 'fusioninventory') . "<img style='float:right' src='" . $CFG_GLPI["root_doc"] . "/plugins/fusioninventory//pics/p2p.png' /></th>";
     echo "<td>";
     Html::showCheckbox(array('name' => 'p2p', 'checked' => $p2p));
     echo "</td>";
     echo "</tr><tr>";
     echo "<th>" . __("retention days", 'fusioninventory') . "</th>";
     echo "<td>";
     /*
      * TODO: use task periodicity input to propose days, months and years
      */
     Dropdown::showNumber('p2p-retention-duration', array('value' => $p2p_retention_duration, 'min' => 0, 'max' => 400));
     echo "</td>";
     echo "</tr><tr>";
     echo "<td>";
     echo "</td><td>";
     if ($pfDeployPackage->can($pfDeployPackage->getID(), UPDATE)) {
         if ($mode === 'edit') {
             echo "<input type='submit' name='save_item' value=\"" . _sx('button', 'Save') . "\" class='submit' >";
         } else {
             echo "<input type='submit' name='add_item' value=\"" . _sx('button', 'Add') . "\" class='submit' >";
         }
     }
     echo "</td>";
     echo "</tr></table>";
 }
 public function package_clone($new_name = '')
 {
     if ($this->getField('id') < 0) {
         return FALSE;
     }
     $_SESSION['tmp_clone_package'] = TRUE;
     //duplicate package
     $package_oldId = $this->getField('id');
     if ($new_name == "") {
         $new_name = $this->getField('name');
     }
     $params = $this->fields;
     unset($params['id']);
     $params['name'] = $new_name;
     $new_package = new PluginFusioninventoryDeployPackage();
     $package_newId = $new_package->add($params);
     //duplicate orders
     $order_obj = new PluginFusioninventoryDeployOrder();
     $orders = $order_obj->find("plugin_fusioninventory_deploypackages_id = '" . $package_oldId . "'");
     foreach ($orders as $order_oldId => $order) {
         //create new order for this new package
         $order_param = array('type' => $order['type'], 'create_date' => date("Y-m-d H:i:s"), 'plugin_fusioninventory_deploypackages_id' => $package_newId);
         $order_newId = $order_obj->add($order_param);
         unset($order_param);
         //duplicate checks
         $check_obj = new PluginFusioninventoryDeployCheck();
         $checks = $check_obj->find("plugin_fusioninventory_deployorders_id = '" . $order_oldId . "'");
         foreach ($checks as $check) {
             //create new check for this new order
             unset($check['id']);
             $check['plugin_fusioninventory_deployorders_id'] = $order_newId;
             $check_obj->add($check);
         }
         //duplicate files
         $file_obj = new PluginFusioninventoryDeployFile();
         $files = $file_obj->find("plugin_fusioninventory_deployorders_id = '" . $order_oldId . "'");
         foreach ($files as $file) {
             //create new file for this new order
             unset($file['id']);
             $file['plugin_fusioninventory_deployorders_id'] = $order_newId;
             $file_newId = $file_obj->add($file);
             //duplicate fileparts
             $filepart_obj = new PluginFusioninventoryDeployFilepart();
             $fileparts = $filepart_obj->find("plugin_fusioninventory_deployfiles_id = '" . $order_oldId . "'");
             foreach ($fileparts as $filepart) {
                 //create new filepart for this new file
                 unset($filepart['id']);
                 $filepart['plugin_fusioninventory_deployorders_id'] = $order_newId;
                 $filepart['plugin_fusioninventory_deployfiles_id'] = $file_newId;
                 $filepart_obj->add($filepart);
             }
         }
         //duplicate actions
         $action_obj = new PluginFusioninventoryDeployAction();
         $actions = $action_obj->find("plugin_fusioninventory_deployorders_id = '" . $order_oldId . "'");
         foreach ($actions as $action) {
             //duplicate actions subitem
             $action_subitem_obj = new $action['itemtype']();
             $action_subitem_oldId = $action['items_id'];
             $action_subitem_obj->getFromDB($action_subitem_oldId);
             $params_subitem = $action_subitem_obj->fields;
             unset($params_subitem['id']);
             $action_subitem_newId = $action_subitem_obj->add($params_subitem);
             //special case for command, we need to duplicate commandstatus and commandenvvariables
             if ($action['itemtype'] == 'PluginFusioninventoryDeployAction_Command') {
                 $command_oldId = $action_subitem_oldId;
                 $command_newId = $action_subitem_newId;
                 //duplicate commandstatus
                 $commandstatus_obj = new PluginFusioninventoryDeployAction_Commandstatus();
                 $commandstatus = $commandstatus_obj->find("plugin_fusioninventory_deploycommands_id = '" . $command_oldId . "'");
                 foreach ($commandstatus as $commandstate) {
                     //create new commandstatus for this command
                     unset($commandstate['id']);
                     $commandstate['plugin_fusioninventory_deploycommands_id'] = $command_newId;
                     $commandstatus_obj->add($commandstate);
                 }
                 //duplicate commandenvvariables
                 $commandenvvariables_obj = new PluginFusioninventoryDeployAction_Commandenvvariable();
                 $commandenvvariables = $commandenvvariables_obj->find("plugin_fusioninventory_deploycommands_id = '" . $command_oldId . "'");
                 foreach ($commandenvvariables as $commandenvvariable) {
                     //create new commandenvvariable for this command
                     unset($commandenvvariable['id']);
                     $commandenvvariable['plugin_fusioninventory_deploycommands_id'] = $command_newId;
                     $commandenvvariables_obj->add($commandenvvariable);
                 }
             }
             //create new action for this new order
             unset($action['id']);
             $action['plugin_fusioninventory_deployorders_id'] = $order_newId;
             $action['items_id'] = $action_subitem_newId;
             $action_obj->add($action);
         }
     }
     if ($new_package->getName() == NOT_AVAILABLE) {
         $new_package->fields['name'] = $new_package->getTypeName() . " : " . __('ID') . " " . $new_package->fields['id'];
     }
     $display = isset($this->input['_no_message_link']) ? $new_package->getNameID() : $new_package->getLink();
     // Do not display quotes
     Session::addMessageAfterRedirect(__('Item successfully added', 'fusioninventory') . "&nbsp;: " . stripslashes($display));
     unset($_SESSION['tmp_clone_package']);
     //exit;
 }
}
if (!isset($_REQUEST['orders_id']) && !isset($_REQUEST['rand']) && !isset($_REQUEST['subtype'])) {
    exit;
}
if (!is_numeric($_REQUEST['orders_id'])) {
    Toolbox::logDebug("Error: orders_id in request is not an integer");
    Toolbox::logDebug(var_dump($_REQUEST['orders_id']));
    exit;
}
$order = new PluginFusioninventoryDeployOrder();
$order->getFromDB($_REQUEST['orders_id']);
//TODO: In the displayForm function, $_REQUEST is somewhat too much for the '$datas' parameter
// I think we could use only $order -- Kevin 'kiniou' Roy
switch ($_REQUEST['subtype']) {
    case 'check':
        PluginFusioninventoryDeployCheck::displayForm($order, $_REQUEST, $_REQUEST['rand'], $_POST['mode']);
        break;
    case 'file':
        PluginFusioninventoryDeployFile::displayForm($order, $_REQUEST, $_REQUEST['rand'], $_POST['mode']);
        break;
    case 'action':
        PluginFusioninventoryDeployAction::displayForm($order, $_REQUEST, $_REQUEST['rand'], $_POST['mode']);
        break;
    case 'package_json_debug':
        if (isset($order->fields['json'])) {
            PluginFusioninventoryDeployPackage::display_json_debug($order);
        } else {
            echo "{}";
        }
        break;
}
 function prepareRun($taskjob_id, $definitions_filter = NULL)
 {
     global $DB;
     $task = new PluginFusioninventoryTask();
     $job = new PluginFusioninventoryTaskjob();
     $joblog = new PluginFusioninventoryTaskjoblog();
     $jobstate = new PluginFusioninventoryTaskjobstate();
     $agent = new PluginFusioninventoryAgent();
     $agentmodule = new PluginFusioninventoryAgentmodule();
     $job->getFromDB($taskjob_id);
     $task->getFromDB($job->fields['plugin_fusioninventory_tasks_id']);
     $communication = $task->fields['communication'];
     $actions = importArrayFromDB($job->fields['action']);
     $definitions = importArrayFromDB($job->fields['definition']);
     $taskvalid = 0;
     $computers = array();
     foreach ($actions as $action) {
         $itemtype = key($action);
         $items_id = current($action);
         switch ($itemtype) {
             case 'Computer':
                 if ($this->definitionFiltered("Computer", $definitions_filter)) {
                     break;
                 }
                 $computers[] = $items_id;
                 break;
             case 'Group':
                 if ($this->definitionFiltered("Group", $definitions_filter)) {
                     break;
                 }
                 $computer_object = new Computer();
                 //find computers by user associated with this group
                 $group_users = new Group_User();
                 $group = new Group();
                 $group->getFromDB($items_id);
                 $members = array();
                 $computers_a_1 = array();
                 $computers_a_2 = array();
                 //array_keys($group_users->find("groups_id = '$items_id'"));
                 $members = $group_users->getGroupUsers($items_id);
                 foreach ($members as $member) {
                     $computers = $computer_object->find("users_id = '{$member['id']}' " . " AND `is_deleted`='0' AND `is_template`='0'");
                     foreach ($computers as $computer) {
                         $computers_a_1[] = $computer['id'];
                     }
                 }
                 //find computers directly associated with this group
                 $computers = $computer_object->find("groups_id = '{$items_id}' " . " AND `is_deleted`='0' AND `is_template`='0'");
                 foreach ($computers as $computer) {
                     $computers_a_2[] = $computer['id'];
                 }
                 //merge two previous array and deduplicate entries
                 $computers = array_unique(array_merge($computers_a_1, $computers_a_2));
                 break;
             case 'PluginFusioninventoryDeployGroup':
                 $group = new PluginFusioninventoryDeployGroup();
                 $group->getFromDB($items_id);
                 switch ($group->getField('type')) {
                     case 'STATIC':
                         if ($this->definitionFiltered("PluginFusioninventoryDeployGroupStatic", $definitions_filter)) {
                             break;
                         }
                         $query = "SELECT items_id\n                     FROM glpi_plugin_fusioninventory_deploygroups_staticdatas\n                     WHERE groups_id = '{$items_id}'\n                     AND itemtype = 'Computer'";
                         $res = $DB->query($query);
                         while ($row = $DB->fetch_assoc($res)) {
                             $computers[] = $row['items_id'];
                         }
                         break;
                     case 'DYNAMIC':
                         if ($this->definitionFiltered("PluginFusioninventoryDeployGroupDynamic", $definitions_filter)) {
                             break;
                         }
                         //$definitions_filter is NULL = update by crontask !
                         if ($definitions_filter != NULL) {
                             $where = " AND `can_update_group`='1'";
                         } else {
                             $where = "";
                         }
                         $query = "SELECT fields_array\n                     FROM glpi_plugin_fusioninventory_deploygroups_dynamicdatas\n                     WHERE groups_id = '{$items_id}' {$where}\n                     LIMIT 1";
                         $res = $DB->query($query);
                         $row = $DB->fetch_assoc($res);
                         //No dynamic groups have been found : break
                         if ($DB->numrows($res) == 0) {
                             break;
                         }
                         if (isset($_GET)) {
                             $get_tmp = $_GET;
                         }
                         if (isset($_SESSION["glpisearchcount"]['Computer'])) {
                             unset($_SESSION["glpisearchcount"]['Computer']);
                         }
                         if (isset($_SESSION["glpisearchcount2"]['Computer'])) {
                             unset($_SESSION["glpisearchcount2"]['Computer']);
                         }
                         $_GET = importArrayFromDB($row['fields_array']);
                         $_GET["glpisearchcount"] = count($_GET['field']);
                         if (isset($_GET['field2'])) {
                             $_GET["glpisearchcount2"] = count($_GET['field2']);
                         }
                         $pfSearch = new PluginFusioninventorySearch();
                         Search::manageParams('Computer');
                         $glpilist_limit = $_SESSION['glpilist_limit'];
                         $_SESSION['glpilist_limit'] = 999999999;
                         $result = $pfSearch->constructSQL('Computer', $_GET);
                         $_SESSION['glpilist_limit'] = $glpilist_limit;
                         while ($data = $DB->fetch_array($result)) {
                             $computers[] = $data['id'];
                         }
                         if (count($get_tmp) > 0) {
                             $_GET = $get_tmp;
                         }
                         break;
                 }
                 break;
         }
     }
     //Remove duplicatas from array
     //We are using isset for faster processing than array_unique because we might have many
     //entries in this list.
     $tmp_computers = array();
     foreach ($computers as $computer) {
         if (!isset($tmp_computers[$computer])) {
             $tmp_computers[$computer] = 1;
         }
     }
     $computers = array_keys($tmp_computers);
     $c_input = array();
     $c_input['plugin_fusioninventory_taskjobs_id'] = $job->fields['id'];
     $c_input['state'] = 0;
     $c_input['plugin_fusioninventory_agents_id'] = 0;
     $c_input['execution_id'] = $task->fields['execution_id'];
     $package = new PluginFusioninventoryDeployPackage();
     foreach ($computers as $computer_id) {
         //Unique Id match taskjobstatuses for an agent(computer)
         foreach ($definitions as $definition) {
             $uniqid = uniqid();
             $package->getFromDB($definition['PluginFusioninventoryDeployPackage']);
             $c_input['state'] = 0;
             $c_input['itemtype'] = 'PluginFusioninventoryDeployPackage';
             $c_input['items_id'] = $package->fields['id'];
             $c_input['date'] = date("Y-m-d H:i:s");
             $c_input['uniqid'] = $uniqid;
             //get agent for this computer
             $agents_id = $agent->getAgentWithComputerid($computer_id);
             if ($agents_id === FALSE) {
                 $jobstates_id = $jobstate->add($c_input);
                 $jobstate->changeStatusFinish($jobstates_id, 0, '', 1, "No agent found for [[Computer::" . $computer_id . "]]", 0, 0);
             } else {
                 if ($agentmodule->isAgentCanDo('DEPLOY', $agents_id)) {
                     $c_input['plugin_fusioninventory_agents_id'] = $agents_id;
                     $jobstates_running = $jobstate->find(implode(" ", array("    `itemtype` = 'PluginFusioninventoryDeployPackage'", "AND `items_id` = " . $package->fields['id'], "AND `state` <> " . PluginFusioninventoryTaskjobstate::FINISHED, "AND `plugin_fusioninventory_agents_id` = " . $agents_id)));
                     if (count($jobstates_running) == 0) {
                         # Push the agent, in the stack of agent to awake
                         if ($communication == "push") {
                             $_SESSION['glpi_plugin_fusioninventory']['agents'][$agents_id] = 1;
                         }
                         $jobstates_id = $jobstate->add($c_input);
                         //Add log of taskjob
                         $c_input['plugin_fusioninventory_taskjobstates_id'] = $jobstates_id;
                         $c_input['state'] = PluginFusioninventoryTaskjoblog::TASK_PREPARED;
                         $taskvalid++;
                         $joblog->add($c_input);
                         unset($c_input['state']);
                         unset($c_input['plugin_fusioninventory_agents_id']);
                     }
                 }
             }
         }
     }
     if ($taskvalid > 0) {
         $job->fields['status'] = 1;
         $job->update($job->fields);
     } else {
         $job->reinitializeTaskjobs($job->fields['plugin_fusioninventory_tasks_id']);
     }
 }
 static function displayAjaxValues($config, $request_data, $rand, $mode)
 {
     global $CFG_GLPI;
     $pfDeployPackage = new PluginFusioninventoryDeployPackage();
     $pfDeployOrder = new PluginFusioninventoryDeployOrder();
     if (isset($request_data['orders_id'])) {
         $pfDeployOrder->getFromDB($request_data['orders_id']);
         $pfDeployPackage->getFromDB($pfDeployOrder->fields['plugin_fusioninventory_deploypackages_id']);
     } else {
         $pfDeployPackage->getEmpty();
     }
     /*
      * Get type from request params
      */
     $type = NULL;
     if ($mode === 'create') {
         $type = $request_data['values'];
     } else {
         $type = $config['type'];
         $config_data = $config['data'];
     }
     /*
      * Set default values
      */
     $value_type_1 = "input";
     $value_1 = "";
     $value_2 = "";
     $retChecks = NULL;
     /*
      * 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 === '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 === '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 === '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 "<table class='package_item'>";
     echo "<tr>";
     echo "<th>{$value_label_1}</th>";
     echo "<td>";
     switch ($value_type_1) {
         case "input":
             echo "<input type='text' name='{$name_label_1}' value='{$value_1}' />";
             break;
         case "textarea":
             echo "<textarea name='{$name_label_1}' rows='3'>{$value_1}</textarea>";
             break;
     }
     echo "</td>";
     echo "</tr>";
     if ($value_label_2 !== FALSE) {
         echo "<tr>";
         echo "<th>{$value_label_2}</th>";
         echo "<td><input type='text' name='{$name_label_2}' value='{$value_2}'/></td>";
         echo "</tr>";
     }
     //specific case for cmd : add retcheck form
     if ($type == "cmd") {
         echo "<tr>";
         echo "<th>" . __("Execution checks", 'fusioninventory');
         PluginFusioninventoryDeployPackage::plusButton("retchecks", ".table_retchecks.template");
         echo "</th>";
         echo "<td>";
         $display = "style='display:none'";
         if ($retChecks) {
             $display = "style='display:block'";
         }
         echo "<span id='retchecks' style='display:block'>";
         if (is_array($retChecks) && count($retChecks)) {
             foreach ($retChecks as $retcheck) {
                 echo "<table class='table_retchecks'>";
                 echo "<tr>";
                 echo "<td>";
                 Dropdown::showFromArray('retchecks_type[]', self::retchecks_entries(), array('value' => $retcheck['type']));
                 echo "</td>";
                 echo "<td>";
                 echo "<input type='text' name='retchecks_value[]' value='" . $retcheck['values'][0] . "' />";
                 echo "</td>";
                 echo "<td><a class='edit' onclick='removeLine(this)'><img src='" . $CFG_GLPI["root_doc"] . "/pics/delete.png' /></a></td>";
                 echo "</tr>";
                 echo "</table>";
             }
         }
         echo "<table class='table_retchecks template' style='display:none'>";
         echo "<tr>";
         echo "<td>";
         //Toolbox::logDebug(self::retchecks_entries());
         Dropdown::showFromArray('retchecks_type[]', self::retchecks_entries(), array());
         echo "</td>";
         echo "<td><input type='text' name='retchecks_value[]' /></td>";
         echo "<td><a class='edit' onclick='removeLine(this)'><img src='" . $CFG_GLPI["root_doc"] . "/pics/delete.png' /></a></td>";
         echo "</tr>";
         echo "</table>";
         echo "</span>";
         echo "</td>";
         echo "</tr>";
     }
     echo "<tr>";
     echo "<td></td><td>";
     if ($pfDeployPackage->can($pfDeployPackage->getID(), UPDATE)) {
         if ($mode === 'edit') {
             echo "<input type='submit' name='save_item' value=\"" . _sx('button', 'Save') . "\" class='submit' >";
         } else {
             echo "<input type='submit' name='add_item' value=\"" . _sx('button', 'Add') . "\" class='submit' >";
         }
     }
     echo "</td>";
     echo "</tr></table>";
     echo "<script type='text/javascript'>\n         function removeLine(item) {\n            var tag_table = item.parentNode.parentNode.parentNode.parentNode;\n            var parent = tag_table.parentNode;\n               parent.removeChild(tag_table);\n         }\n      </script>";
 }
} elseif (isset($_POST['add_item'])) {
    $data = array_map(array('Toolbox', 'stripslashes_deep'), $_POST);
    PluginFusioninventoryDeployPackage::alter_json('add_item', $data);
    Html::back();
} elseif (isset($_POST['save_item'])) {
    $data = array_map(array('Toolbox', 'stripslashes_deep'), $_POST);
    PluginFusioninventoryDeployPackage::alter_json('save_item', $data);
    Html::back();
} elseif (isset($_POST['remove_item'])) {
    $data = array_map(array('Toolbox', 'stripslashes_deep'), $_POST);
    PluginFusioninventoryDeployPackage::alter_json('remove_item', $data);
    Html::back();
}
//$data = Toolbox::stripslashes_deep($_POST);
$data = $_POST;
$package = new PluginFusioninventoryDeployPackage();
//general form
if (isset($data["add"])) {
    Session::checkRight('plugin_fusioninventory_package', CREATE);
    $newID = $package->add($data);
    html::redirect(Toolbox::getItemTypeFormURL('PluginFusioninventoryDeployPackage') . "?id=" . $newID);
} else {
    if (isset($data["update"])) {
        Session::checkRight('plugin_fusioninventory_package', UPDATE);
        $package->update($data);
        Html::back();
    } else {
        if (isset($data["delete"])) {
            Session::checkRight('plugin_fusioninventory_package', PURGE);
            $package->delete($data);
            $package->redirectToList();
  GNU Affero General Public License for more details.

  You should have received a copy of the GNU Affero General Public License
  along with FusionInventory. If not, see <http://www.gnu.org/licenses/>.

  ------------------------------------------------------------------------

  @package   FusionInventory
  @author    Alexandre Delaunay
  @co-author
  @copyright Copyright (c) 2010-2014 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      http://forge.fusioninventory.org/projects/fusioninventory-for-glpi/
  @since     2014

  ------------------------------------------------------------------------
*/
include "../../../inc/includes.php";
Session::checkLoginUser();
Session::checkRight('plugin_fusioninventory_package', CREATE);
Html::header(__('FusionInventory DEPLOY'), $_SERVER["PHP_SELF"], "plugins", "pluginfusioninventorymenu", "deploypackage");
$package = new PluginFusioninventoryDeployPackage();
if (isset($_POST['importfile'])) {
    $package->importPackage($_POST['importfile']);
    Html::back();
}
PluginFusioninventoryMenu::displayMenu("mini");
$package->listPackagesToImport();
Html::footer();
 static function displayAjaxValues($config, $request_data, $rand, $mode)
 {
     $pfDeployPackage = new PluginFusioninventoryDeployPackage();
     $pfDeployOrder = new PluginFusioninventoryDeployOrder();
     if (isset($request_data['orders_id'])) {
         $pfDeployOrder->getFromDB($request_data['orders_id']);
         $pfDeployPackage->getFromDB($pfDeployOrder->fields['plugin_fusioninventory_deploypackages_id']);
     } else {
         $pfDeployPackage->getEmpty();
     }
     /*
      * Get type from request params
      */
     $type = NULL;
     if ($mode === 'create') {
         $type = $request_data['value'];
         $config_data = NULL;
     } else {
         $type = $config['type'];
         $config_data = $config['data'];
     }
     $values = self::getValues($type, $config_data, $mode);
     if ($values === FALSE) {
         return FALSE;
     }
     echo "<table class='package_item'>";
     echo "<tr>";
     echo "<th>{$values['path_label']}</th>";
     echo "<td><input type='text' name='path' id='check_path{$rand}' value='{$values['path_value']}' /></td>";
     echo "</tr>";
     if ($values['value_label'] !== FALSE) {
         echo "<tr>";
         echo "<th>{$values['value_label']}</th>";
         switch ($values['value_type']) {
             case "textarea":
                 echo "<td><textarea name='value' id='check_value{$rand}' rows='5'>" . $values['value'] . "</textarea></td>";
                 break;
             case "input":
                 echo "<td><input type='text' name='value' id='check_value{$rand}' value='" . $values['value'] . "' /></td>";
                 break;
             case "input+unit":
                 $value = $values['value'];
                 // freespaceGreater check is saved as MiB
                 if ($type == 'freespaceGreater') {
                     $value = $value * 1024 * 1024;
                 }
                 $options['value'] = 'KB';
                 if ($mode === 'edit') {
                     if ($value >= self::getUnitSize('GB')) {
                         $value = $value / self::getUnitSize('GB');
                         $options['value'] = 'GB';
                     } elseif ($value >= self::getUnitSize('MB')) {
                         $value = $value / self::getUnitSize('MB');
                         $options['value'] = 'MB';
                     } elseif ($value >= self::getUnitSize('KB')) {
                         $value = $value / self::getUnitSize('KB');
                         $options['value'] = 'KB';
                     } else {
                         $options['value'] = 'B';
                     }
                 }
                 echo "<td>";
                 echo "<input " . "type='text' " . "name='value' " . "id='check_value{$rand}' " . "value='{$value}' " . "/>";
                 echo "</td>";
                 echo "</tr><tr>";
                 echo "<th>" . __("Unit", 'fusioninventory') . "</th>";
                 echo "<td>";
                 $unit_labels = self::getUnitLabel();
                 /*
                  * The freespaceGreater check does not need to propose KiB or B
                  * because its value is based on MiB according to REST API.
                  *                               -- Kevin 'kiniou' Roy
                  */
                 if ($type == 'freespaceGreater') {
                     unset($unit_labels['KB']);
                     unset($unit_labels['B']);
                 }
                 Dropdown::showFromArray('unit', $unit_labels, $options);
                 echo "</td>";
                 break;
         }
         echo "</tr>";
     }
     echo "<tr>";
     echo "<th>" . __("In case of error", 'fusioninventory') . "</th>";
     echo "<td>";
     Dropdown::showFromArray('return', array("error" => __('Error', 'fusioninventory'), "ignore" => __("Ignore", 'fusioninventory')), array('value' => $values['return']));
     echo "</td>";
     echo "</tr>";
     echo "<tr><td></td><td>";
     if ($pfDeployPackage->can($pfDeployPackage->getID(), UPDATE)) {
         if ($mode === 'edit') {
             echo "<input type='submit' name='save_item' value=\"" . _sx('button', 'Save') . "\" class='submit' >";
         } else {
             echo "<input type='submit' name='add_item' value=\"" . _sx('button', 'Add') . "\" class='submit' >";
         }
     }
     echo "</td></tr>";
     echo "</table>";
 }
 /**
  * @test
  */
 public function TaskWithPC()
 {
     $this->mark_incomplete();
     global $DB;
     $_SESSION['glpiactiveentities_string'] = 0;
     $pfDeployGroup = new PluginFusioninventoryDeployGroup();
     $pfDeployGroup_Dynamicdata = new PluginFusioninventoryDeployGroup_Dynamicdata();
     $pfDeployPackage = new PluginFusioninventoryDeployPackage();
     $pfTask = new PluginFusioninventoryTask();
     $pfTaskJob = new PluginFusioninventoryTaskjob();
     $input = array('name' => 'test', 'type' => 'DYNAMIC');
     $pfDeployGroup->add($input);
     $input = array('groups_id' => 1, 'fields_array' => '{"entities_id":"0","name":"","field":["1"],"searchtype":["contains"],"contains":["^pc"],"plugin_fusioninventory_deploygroup_dynamicdatas_id":"1","id":"1","updaterule":"Update this rule","itemtype":"Computer"}');
     $pfDeployGroup_Dynamicdata->add($input);
     $input = array('name' => 'ls', 'entities_id' => 0);
     $pfDeployPackage->add($input);
     $input = array('name' => 'deploy', 'is_active' => 1, 'communication' => 'pull');
     $pfTask->add($input);
     $a_plugins = current(getAllDatasFromTable('glpi_plugins', '`directory`="fusioninventory"'));
     $input = array('plugin_fusioninventory_tasks_id' => 1, 'name' => 'deploy', 'plugins_id' => $a_plugins['id'], 'method' => 'deployinstall', 'action' => '[{"PluginFusioninventoryDeployGroup":"1"}]');
     $pfTaskJob->add($input);
     $DB->query('UPDATE `glpi_plugin_fusioninventory_taskjobs`
      SET `definition`=\'[{"PluginFusioninventoryDeployPackage":"1"}]\'
      WHERE `id`="1"');
     // Force task prepation
     $pfTaskJob->forceRunningTask(1);
     $a_jobstates = getAllDatasFromTable("glpi_plugin_fusioninventory_taskjobstates");
     foreach ($a_jobstates as $num => $data) {
         unset($data['uniqid']);
         $a_jobstates[$num] = $data;
     }
     $a_reference = array(1 => array('id' => 1, 'plugin_fusioninventory_taskjobs_id' => 1, 'items_id' => 1, 'itemtype' => "PluginFusioninventoryDeployPackage", 'state' => 0, 'plugin_fusioninventory_agents_id' => 1, 'specificity' => '', 'execution_id' => '0'), 2 => array('id' => 2, 'plugin_fusioninventory_taskjobs_id' => 1, 'items_id' => 1, 'itemtype' => "PluginFusioninventoryDeployPackage", 'state' => 0, 'plugin_fusioninventory_agents_id' => 2, 'specificity' => '', 'execution_id' => '0'), 3 => array('id' => 3, 'plugin_fusioninventory_taskjobs_id' => 1, 'items_id' => 1, 'itemtype' => "PluginFusioninventoryDeployPackage", 'state' => 0, 'plugin_fusioninventory_agents_id' => 3, 'specificity' => '', 'execution_id' => '0'), 4 => array('id' => 4, 'plugin_fusioninventory_taskjobs_id' => 1, 'items_id' => 1, 'itemtype' => "PluginFusioninventoryDeployPackage", 'state' => 0, 'plugin_fusioninventory_agents_id' => 4, 'specificity' => '', 'execution_id' => '0'), 5 => array('id' => 5, 'plugin_fusioninventory_taskjobs_id' => 1, 'items_id' => 1, 'itemtype' => "PluginFusioninventoryDeployPackage", 'state' => 0, 'plugin_fusioninventory_agents_id' => 5, 'specificity' => '', 'execution_id' => '0'), 6 => array('id' => 6, 'plugin_fusioninventory_taskjobs_id' => 1, 'items_id' => 1, 'itemtype' => "PluginFusioninventoryDeployPackage", 'state' => 0, 'plugin_fusioninventory_agents_id' => 6, 'specificity' => '', 'execution_id' => '0'), 7 => array('id' => 7, 'plugin_fusioninventory_taskjobs_id' => 1, 'items_id' => 1, 'itemtype' => "PluginFusioninventoryDeployPackage", 'state' => 0, 'plugin_fusioninventory_agents_id' => 7, 'specificity' => '', 'execution_id' => '0'), 8 => array('id' => 8, 'plugin_fusioninventory_taskjobs_id' => 1, 'items_id' => 1, 'itemtype' => "PluginFusioninventoryDeployPackage", 'state' => 0, 'plugin_fusioninventory_agents_id' => 8, 'specificity' => '', 'execution_id' => '0'), 9 => array('id' => 9, 'plugin_fusioninventory_taskjobs_id' => 1, 'items_id' => 1, 'itemtype' => "PluginFusioninventoryDeployPackage", 'state' => 0, 'plugin_fusioninventory_agents_id' => 9, 'specificity' => '', 'execution_id' => '0'), 10 => array('id' => 10, 'plugin_fusioninventory_taskjobs_id' => 1, 'items_id' => 1, 'itemtype' => "PluginFusioninventoryDeployPackage", 'state' => 0, 'plugin_fusioninventory_agents_id' => 10, 'specificity' => '', 'execution_id' => '0'), 11 => array('id' => 11, 'plugin_fusioninventory_taskjobs_id' => 1, 'items_id' => 1, 'itemtype' => "PluginFusioninventoryDeployPackage", 'state' => 0, 'plugin_fusioninventory_agents_id' => 11, 'specificity' => '', 'execution_id' => '0'), 12 => array('id' => 12, 'plugin_fusioninventory_taskjobs_id' => 1, 'items_id' => 1, 'itemtype' => "PluginFusioninventoryDeployPackage", 'state' => 0, 'plugin_fusioninventory_agents_id' => 12, 'specificity' => '', 'execution_id' => '0'), 13 => array('id' => 13, 'plugin_fusioninventory_taskjobs_id' => 1, 'items_id' => 1, 'itemtype' => "PluginFusioninventoryDeployPackage", 'state' => 0, 'plugin_fusioninventory_agents_id' => 13, 'specificity' => '', 'execution_id' => '0'));
     $this->assertEquals($a_reference, $a_jobstates);
 }
  (at your option) any later version.

  FusionInventory is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  GNU Affero General Public License for more details.

  You should have received a copy of the GNU Affero General Public License
  along with FusionInventory. If not, see <http://www.gnu.org/licenses/>.

  ------------------------------------------------------------------------

  @package   FusionInventory
  @author    Alexandre Delaunay
  @co-author
  @copyright Copyright (c) 2010-2015 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      http://forge.fusioninventory.org/projects/fusioninventory-for-glpi/
  @since     2010

  ------------------------------------------------------------------------
*/
include "../../../inc/includes.php";
Session::checkLoginUser();
Html::header(__('FusionInventory'), $_SERVER["PHP_SELF"], "plugins", "pluginfusioninventorymenu", "deploypackage");
PluginFusioninventoryMenu::displayMenu("mini");
$package = new PluginFusioninventoryDeployPackage();
$package->showList();
Html::footer();