function getSpecificMassiveActions($checkitem = NULL)
 {
     $actions = array();
     if (isset($_GET['id'])) {
         $id = $_GET['id'];
     } else {
         $id = $_POST['id'];
     }
     $group = new PluginFusioninventoryDeployGroup();
     $group->getFromDB($id);
     //There's no massive action associated with a dynamic group !
     if ($group->isDynamicGroup() || !$group->canEdit($group->getID())) {
         return array();
     }
     if (!isset($_POST['custom_action'])) {
         $actions['PluginFusioninventoryComputer' . MassiveAction::CLASS_ACTION_SEPARATOR . 'add'] = _x('button', 'Add');
         $actions['PluginFusioninventoryComputer' . MassiveAction::CLASS_ACTION_SEPARATOR . 'deleteitem'] = _x('button', 'Delete permanently');
     } else {
         if ($_POST['custom_action'] == 'add_to_group') {
             $actions['PluginFusioninventoryComputer' . MassiveAction::CLASS_ACTION_SEPARATOR . 'add'] = _x('button', 'Add');
         } elseif ($_POST['custom_action'] == 'delete_from_group') {
             $actions['PluginFusioninventoryComputer' . MassiveAction::CLASS_ACTION_SEPARATOR . 'deleteitem'] = _x('button', 'Delete permanently');
         }
     }
     return $actions;
 }
 /**
  * @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);
 }
 /**
  * @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);
 }
  @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();
$group = new PluginFusioninventoryDeployGroup();
if (isset($_POST['save'])) {
    $group_item = new PluginFusioninventoryDeployGroup_Dynamicdata();
    if (!countElementsInTable($group_item->getTable(), "plugin_fusioninventory_deploygroups_id='" . $_POST['id'] . "'")) {
        $criteria = array('criteria' => $_POST['criteria'], 'metacriteria' => $_POST['metacriteria']);
        $values['fields_array'] = serialize($criteria);
        $values['plugin_fusioninventory_deploygroups_id'] = $_POST['id'];
        $group_item->add($values);
    } else {
        $item = getAllDatasFromTable($group_item->getTable(), "plugin_fusioninventory_deploygroups_id='" . $_POST['id'] . "'");
        $values = array_pop($item);
        $criteria = array('criteria' => $_POST['criteria'], 'metacriteria' => $_POST['metacriteria']);
        $values['fields_array'] = serialize($criteria);
        $group_item->update($values);
    }
    Html::redirect(Toolbox::getItemTypeFormURL("PluginFusioninventoryDeployGroup") . "?id=" . $_GET['id']);
 static function getActions($params)
 {
     global $DB;
     $res = '';
     if (!isset($params['get'])) {
         exit;
     }
     switch ($params['get']) {
         case "type":
             $res = array('action_types' => self::getActionTypes());
             $res = json_encode($res);
             break;
         case "selection":
             switch ($params['type']) {
                 case 'Computer':
                     $query = "SELECT id, name FROM glpi_computers";
                     if (isset($params['query'])) {
                         $like = $DB->escape($params['query']);
                         $query .= " WHERE name LIKE '%{$like}'";
                     }
                     $query .= " ORDER BY name ASC";
                     $query_res = $DB->query($query);
                     $i = 0;
                     while ($row = $DB->fetch_array($query_res)) {
                         $res['action_selections'][$i]['id'] = $row['id'];
                         $res['action_selections'][$i]['name'] = $row['name'];
                         $i++;
                     }
                     $res = json_encode($res);
                     break;
                 case 'Group':
                     $like = "1";
                     if (isset($params['query'])) {
                         $like = "name LIKE '%" . $DB->escape($params['query']) . "'";
                     }
                     $group = new Group();
                     $group_datas = $group->find($like);
                     $i = 0;
                     foreach ($group_datas as $group_data) {
                         $res['action_selections'][$i]['id'] = $group_data['id'];
                         $res['action_selections'][$i]['name'] = $group_data['name'];
                         $i++;
                     }
                     $res = json_encode($res);
                     break;
                 case 'PluginFusioninventoryDeployGroup':
                     $res = PluginFusioninventoryDeployGroup::getAllDatas('action_selections');
                     break;
             }
             break;
         case "oneSelection":
             break;
         default:
             $res = '';
     }
     return $res;
 }
 static function showCriteriaAndSearch(PluginFusioninventoryDeployGroup $item)
 {
     $search_params = PluginFusioninventoryDeployGroup::getSearchParamsAsAnArray($item, true);
     //If metacriteria array is empty, remove it as it displays the metacriteria form,
     //and it's is not we want !
     if (isset($search_params['metacriteria']) && empty($search_params['metacriteria'])) {
         unset($search_params['metacriteria']);
     }
     PluginFusioninventoryDeployGroup::showCriteria($item, true, $search_params);
     unset($_SESSION['glpisearch']['PluginFusioninventoryComputer']);
     if (isset($_GET['preview'])) {
         //Add extra parameters for massive action display : only the Add action should be displayed
         $search_params['massiveactionparams']['extraparams']['id'] = $item->getID();
         $search_params['massiveactionparams']['extraparams']['custom_action'] = 'add_to_group';
         $search_params['massiveactionparams']['extraparams']['massive_action_fields'] = array('action', 'id');
         Search::showList('PluginFusioninventoryComputer', $search_params);
     }
 }
 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']);
     }
 }
  (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();
header("Content-Type: text/html; charset=UTF-8");
Html::header_nocache();
$_SESSION['groupSearchResults'] = $_REQUEST;
PluginFusioninventoryDeployGroup::showSearchResults($_REQUEST);
 /**
  * Get computers belonging to a dynamic group
  * @since 0.85+1.0
  * @param group the group object
  * @return an array of computer ids
  */
 static function getTargetsByGroup(PluginFusioninventoryDeployGroup $group)
 {
     $search_params = PluginFusioninventoryDeployGroup::getSearchParamsAsAnArray($group, false, true);
     if (isset($search_params['metacriteria']) && empty($search_params['metacriteria'])) {
         unset($search_params['metacriteria']);
     }
     $search_params['sort'] = '';
     //Only retrieve computers IDs
     $results = Search::getDatas('PluginFusioninventoryComputer', $search_params, array('2'));
     $ids = array();
     foreach ($results['data']['rows'] as $id => $row) {
         $ids[$row['id']] = $row['id'];
     }
     return $ids;
 }
 function prepareRun($taskjobs_id)
 {
     global $DB;
     $task = new PluginFusioninventoryTask();
     $job = new PluginFusioninventoryTaskjob();
     $joblog = new PluginFusioninventoryTaskjoblog();
     $jobstate = new PluginFusioninventoryTaskjobstate();
     $agent = new PluginFusioninventoryAgent();
     $uniqid = uniqid();
     $job->getFromDB($taskjobs_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':
                 $computers[] = $items_id;
                 break;
             case 'Group':
                 $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']}'");
                     foreach ($computers as $computer) {
                         $computers_a_1[] = $computer['id'];
                     }
                 }
                 //find computers directly associated with this group
                 $computers = $computer_object->find("groups_id = '{$items_id}'");
                 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':
                         $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':
                         $query = "SELECT fields_array\n                     FROM glpi_plugin_fusioninventory_deploygroups_dynamicdatas\n                     WHERE groups_id = '{$items_id}'\n                     LIMIT 1";
                         $res = $DB->query($query);
                         $row = $DB->fetch_assoc($res);
                         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::manageGetValues('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;
         }
     }
     $c_input = array();
     $c_input['plugin_fusioninventory_taskjobs_id'] = $taskjobs_id;
     $c_input['state'] = 0;
     $c_input['plugin_fusioninventory_agents_id'] = 0;
     $c_input['execution_id'] = $task->fields['execution_id'];
     $pfCollect = new PluginFusioninventoryCollect();
     foreach ($computers as $computer_id) {
         //get agent if 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 {
             foreach ($definitions as $definition) {
                 $pfCollect->getFromDB($definition['PluginFusioninventoryCollect']);
                 switch ($pfCollect->fields['type']) {
                     case 'registry':
                         // get all registry
                         $pfCollect_Registry = new PluginFusioninventoryCollect_Registry();
                         $a_registries = $pfCollect_Registry->find("`plugin_fusioninventory_collects_id`='" . $pfCollect->fields['id'] . "'");
                         foreach ($a_registries as $data_r) {
                             $uniqid = uniqid();
                             $c_input['state'] = 0;
                             $c_input['itemtype'] = 'PluginFusioninventoryCollect_Registry';
                             $c_input['items_id'] = $data_r['id'];
                             $c_input['date'] = date("Y-m-d H:i:s");
                             $c_input['uniqid'] = $uniqid;
                             $c_input['plugin_fusioninventory_agents_id'] = $agents_id;
                             # 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);
                         }
                         break;
                     case 'wmi':
                         // get all wmi
                         $pfCollect_Wmi = new PluginFusioninventoryCollect_Wmi();
                         $a_wmies = $pfCollect_Wmi->find("`plugin_fusioninventory_collects_id`='" . $pfCollect->fields['id'] . "'");
                         foreach ($a_wmies as $data_r) {
                             $uniqid = uniqid();
                             $c_input['state'] = 0;
                             $c_input['itemtype'] = 'PluginFusioninventoryCollect_Wmi';
                             $c_input['items_id'] = $data_r['id'];
                             $c_input['date'] = date("Y-m-d H:i:s");
                             $c_input['uniqid'] = $uniqid;
                             $c_input['plugin_fusioninventory_agents_id'] = $agents_id;
                             # 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);
                         }
                         break;
                     case 'file':
                         // find files
                         $pfCollect_File = new PluginFusioninventoryCollect_File();
                         $a_files = $pfCollect_File->find("`plugin_fusioninventory_collects_id`='" . $pfCollect->fields['id'] . "'");
                         foreach ($a_files as $data_r) {
                             $uniqid = uniqid();
                             $c_input['state'] = 0;
                             $c_input['itemtype'] = 'PluginFusioninventoryCollect_File';
                             $c_input['items_id'] = $data_r['id'];
                             $c_input['date'] = date("Y-m-d H:i:s");
                             $c_input['uniqid'] = $uniqid;
                             $c_input['plugin_fusioninventory_agents_id'] = $agents_id;
                             # 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);
                         }
                         break;
                 }
             }
         }
     }
     if ($taskvalid > 0) {
         $job->fields['status'] = 1;
         $job->update($job->fields);
     } else {
         $job->reinitializeTaskjobs($job->fields['plugin_fusioninventory_tasks_id']);
     }
 }
 /**
  * @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);
 }
 /**
  *
  */
 static function getSearchParamsAsAnArray(PluginFusioninventoryDeployGroup $group, $check_post_values = FALSE, $getAll = FALSE)
 {
     global $DB;
     $computers_params = array();
     unset($_SESSION['glpisearch']['PluginFusioninventoryComputer']);
     //Check criteria from DB
     if (!$check_post_values) {
         if ($group->fields['type'] == PluginFusioninventoryDeployGroup::DYNAMIC_GROUP) {
             $query = "SELECT `fields_array`\n                     FROM `glpi_plugin_fusioninventory_deploygroups_dynamicdatas`\n                     WHERE `plugin_fusioninventory_deploygroups_id`='" . $group->getID() . "'";
             $result = $DB->query($query);
             if ($DB->numrows($result) > 0) {
                 $fields_array = $DB->result($result, 0, 'fields_array');
                 $computers_params = unserialize($fields_array);
             }
         }
     } else {
         $computers_params = $_GET;
     }
     if ($getAll) {
         $computers_params['export_all'] = true;
     }
     return Search::manageParams('PluginFusioninventoryComputer', $computers_params);
 }
 function prepareRun($taskjobs_id)
 {
     global $DB;
     $pfTask = new PluginFusioninventoryTask();
     $pfTaskjob = new PluginFusioninventoryTaskjob();
     $pfTaskjobstate = new PluginFusioninventoryTaskjobstate();
     $pfTaskjoblog = new PluginFusioninventoryTaskjoblog();
     $pfAgent = new PluginFusioninventoryAgent();
     $uniqid = uniqid();
     $pfTaskjob->getFromDB($taskjobs_id);
     $pfTask->getFromDB($pfTaskjob->fields['plugin_fusioninventory_tasks_id']);
     $communication = $pfTask->fields['communication'];
     $a_definitions = importArrayFromDB($pfTaskjob->fields['definition']);
     $a_computers_to_wake = array();
     foreach ($a_definitions as $definition) {
         $itemtype = key($definition);
         $items_id = current($definition);
         switch ($itemtype) {
             case 'Computer':
                 $a_computers_to_wake[] = $items_id;
                 break;
             case 'PluginFusioninventoryDeployGroup':
                 $group = new PluginFusioninventoryDeployGroup();
                 $group->getFromDB($items_id);
                 switch ($group->getField('type')) {
                     case 'STATIC':
                         $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)) {
                             $a_computers_to_wake[] = $row['items_id'];
                         }
                         break;
                     case 'DYNAMIC':
                         $query = "SELECT fields_array\n                     FROM glpi_plugin_fusioninventory_deploygroups_dynamicdatas\n                     WHERE groups_id = '{$items_id}'\n                     LIMIT 1";
                         $res = $DB->query($query);
                         $row = $DB->fetch_assoc($res);
                         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::manageGetValues('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)) {
                             $a_computers_to_wake[] = $data['id'];
                         }
                         if (count($get_tmp) > 0) {
                             $_GET = $get_tmp;
                         }
                         break;
                 }
         }
     }
     $a_actions = importArrayFromDB($pfTaskjob->fields['action']);
     $a_agentList = array();
     if (!strstr($pfTaskjob->fields['action'], '".1"') and !strstr($pfTaskjob->fields['action'], '".2"')) {
         foreach ($a_actions as $a_action) {
             if (!in_array('.1', $a_action) && !in_array('.2', $a_action)) {
                 $agent_id = current($a_action);
                 if ($pfAgent->getFromDB($agent_id)) {
                     if ($communication == 'pull') {
                         $a_agentList[] = $agent_id;
                     } else {
                         if ($pfTaskjob->isAgentAlive('1', $agent_id)) {
                             $a_agentList[] = $agent_id;
                         }
                     }
                 }
             }
         }
     } else {
         if (strstr($pfTaskjob->fields['action'], '".1"')) {
             $a_agentList = $this->getAgentsSubnet(count($a_computers_to_wake), $communication);
         } else {
             if (in_array('.2', $a_actions)) {
                 $subnet = '';
                 foreach ($a_computers_to_wake as $items_id) {
                     $sql = "SELECT * FROM `glpi_networkports`\n               WHERE `items_id`='" . $items_id . "'\n                  AND `itemtype`='Computer'\n                  AND `mac`!='' ";
                     $result = $DB->query($sql);
                     if ($result) {
                         while ($data = $DB->fetch_array($result)) {
                             $subnet = $data['subnet'];
                         }
                     }
                 }
                 if ($subnet != '') {
                     $a_agentList = $this->getAgentsSubnet(count($a_computers_to_wake), $communication, $subnet);
                 }
             }
         }
     }
     if (count($a_agentList) == '0') {
         $a_input = array();
         $a_input['plugin_fusioninventory_taskjobs_id'] = $taskjobs_id;
         $a_input['state'] = 1;
         $a_input['plugin_fusioninventory_agents_id'] = 0;
         $a_input['itemtype'] = 'Computer';
         $a_input['items_id'] = 0;
         $a_input['uniqid'] = $uniqid;
         $Taskjobstates_id = $pfTaskjobstate->add($a_input);
         //Add log of taskjob
         $a_input['plugin_fusioninventory_taskjobstates_id'] = $Taskjobstates_id;
         $a_input['state'] = 7;
         $a_input['date'] = date("Y-m-d H:i:s");
         $pfTaskjoblog->add($a_input);
         $pfTaskjobstate->changeStatusFinish($Taskjobstates_id, 0, 'Computer', 1, "Unable to find agent to run this job");
     } else {
         $nb_computers = ceil(count($a_computers_to_wake) / count($a_agentList));
         $a_input = array();
         $a_input['plugin_fusioninventory_taskjobs_id'] = $taskjobs_id;
         $a_input['state'] = 0;
         $a_input['itemtype'] = 'Computer';
         $a_input['uniqid'] = $uniqid;
         while (count($a_computers_to_wake) != 0) {
             $agent_id = array_pop($a_agentList);
             $a_input['plugin_fusioninventory_agents_id'] = $agent_id;
             for ($i = 0; $i < $nb_computers; $i++) {
                 //Add jobstate and put status
                 $a_input['items_id'] = array_pop($a_computers_to_wake);
                 $Taskjobstates_id = $pfTaskjobstate->add($a_input);
                 //Add log of taskjob
                 $a_input['plugin_fusioninventory_taskjobstates_id'] = $Taskjobstates_id;
                 $a_input['state'] = 7;
                 $a_input['date'] = date("Y-m-d H:i:s");
                 $pfTaskjoblog->add($a_input);
                 unset($a_input['state']);
                 if ($communication == "push") {
                     $_SESSION['glpi_plugin_fusioninventory']['agents'][$agent_id] = 1;
                 }
             }
         }
     }
     $pfTaskjob->fields['status'] = 1;
     $pfTaskjob->update($pfTaskjob->fields);
     return $uniqid;
 }