@author    David Durieux
  @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     2010

  ------------------------------------------------------------------------
*/
include "../../../inc/includes.php";
header("Content-Type: text/html; charset=UTF-8");
Html::header_nocache();
Session::checkCentralAccess();
if (!isset($_POST['rand']) && !isset($_POST['subtype'])) {
    exit;
}
$rand = $_POST['rand'];
$mode = $_POST['mode'];
switch ($_POST['type']) {
    case 'check':
        PluginFusioninventoryDeployCheck::displayAjaxValues(NULL, $_POST, $rand, $mode);
        break;
    case 'file':
        PluginFusioninventoryDeployFile::displayAjaxValues(NULL, $_POST, $rand, $mode);
        break;
    case 'action':
        PluginFusioninventoryDeployAction::displayAjaxValues(NULL, $_POST, $rand, $mode);
        break;
}
}
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;
}
 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;
 }
 static function displayList(PluginFusioninventoryDeployOrder $order, $datas, $rand)
 {
     global $CFG_GLPI;
     $pfDeployPackage = new PluginFusioninventoryDeployPackage();
     $pfDeployPackage->getFromDB($order->fields['plugin_fusioninventory_deploypackages_id']);
     echo "<table class='tab_cadrehov package_item_list' id='table_action_{$rand}'>";
     $i = 0;
     foreach ($datas['jobs']['actions'] as $action) {
         echo Search::showNewLine(Search::HTML_OUTPUT, $i % 2);
         if ($pfDeployPackage->can($pfDeployPackage->getID(), UPDATE)) {
             echo "<td class='control'>";
             Html::showCheckbox(array('name' => 'action_entries[]'));
             echo "</td>";
         }
         $keys = array_keys($action);
         $action_type = array_shift($keys);
         echo "<td>";
         echo "<a class='edit' " . "onclick=\"edit_subtype('action', {$order->fields['id']}, {$rand}, this)\">";
         echo PluginFusioninventoryDeployAction::getType($action_type);
         echo "</a><br />";
         foreach ($action[$action_type] as $key => $value) {
             if (is_array($value)) {
                 if ($key === "list") {
                     foreach ($value as $list) {
                         echo $list;
                         echo " ";
                     }
                 }
             } else {
                 echo "<b>";
                 if ($key == 'exec') {
                     echo __('Command to execute', 'fusioninventory');
                 } else {
                     echo $key;
                 }
                 echo "</b>";
                 if ($key === "exec") {
                     echo "<pre style='border-left:solid lightgrey 3px;margin-left: 5px;" . "padding-left:2px'>{$value}</pre>";
                 } else {
                     echo " {$value} ";
                 }
             }
         }
         if (isset($action[$action_type]['retChecks'])) {
             echo "<br><b>" . __("return codes saved for this command", 'fusioninventory') . "</b> : <ul class='retChecks'>";
             foreach ($action[$action_type]['retChecks'] as $retCheck) {
                 echo "<li>";
                 $retchecks_entries = self::retchecks_entries();
                 echo $retchecks_entries[$retCheck['type']] . " " . array_shift($retCheck['values']);
                 echo "</li>";
             }
             echo "</ul>";
         }
         echo "</td>";
         echo "</td>";
         if ($pfDeployPackage->can($pfDeployPackage->getID(), UPDATE)) {
             echo "<td class='rowhandler control' title='" . __('drag', 'fusioninventory') . "'><div class='drag row'></div></td>";
         }
         echo "</tr>";
         $i++;
     }
     if ($pfDeployPackage->can($pfDeployPackage->getID(), UPDATE)) {
         echo "<tr><th>";
         Html::checkAllAsCheckbox("actionsList{$rand}", mt_rand());
         echo "</th><th colspan='3' class='mark'></th></tr>";
     }
     echo "</table>";
     if ($pfDeployPackage->can($pfDeployPackage->getID(), UPDATE)) {
         echo "&nbsp;&nbsp;<img src='" . $CFG_GLPI["root_doc"] . "/pics/arrow-left.png' alt=''>";
         echo "<input type='submit' name='delete' value=\"" . __('Delete', 'fusioninventory') . "\" class='submit'>";
     }
 }