Ejemplo n.º 1
0
 static function pdfForItem(PluginPdfSimplePDF $pdf, CommonDBTM $item)
 {
     global $DB, $CFG_GLPIG;
     if (!Session::haveRight("contract", "r")) {
         return false;
     }
     $type = $item->getType();
     $ID = $item->getField('id');
     $con = new Contract();
     $query = "SELECT *\n                FROM `glpi_contracts_items`\n                WHERE `glpi_contracts_items`.`items_id` = '" . $ID . "'\n                      AND `glpi_contracts_items`.`itemtype` = '" . $type . "'";
     $result = $DB->query($query);
     $number = $DB->numrows($result);
     $i = $j = 0;
     $pdf->setColumnsSize(100);
     if ($number > 0) {
         $pdf->displayTitle('<b>' . _N('Associated contract', 'Associated contracts', 2) . '</b>');
         $pdf->setColumnsSize(19, 19, 19, 16, 11, 16);
         $pdf->displayTitle(__('Name'), _x('phone', 'Number'), __('Contract type'), __('Supplier'), __('Start date'), __('Initial contract period'));
         $i++;
         while ($j < $number) {
             $cID = $DB->result($result, $j, "contracts_id");
             $assocID = $DB->result($result, $j, "id");
             if ($con->getFromDB($cID)) {
                 $pdf->displayLine(empty($con->fields["name"]) ? "(" . $con->fields["id"] . ")" : $con->fields["name"], $con->fields["num"], Html::clean(Dropdown::getDropdownName("glpi_contracttypes", $con->fields["contracttypes_id"])), str_replace("<br>", " ", $con->getSuppliersNames()), Html::convDate($con->fields["begin_date"]), sprintf(_n('%d month', '%d months', $con->fields["duration"]), $con->fields["duration"]));
             }
             $j++;
         }
     } else {
         $pdf->displayTitle("<b>" . __('No item found') . "</b>");
     }
     $pdf->displaySpace();
 }
 /**
  * Check right on an contract - overloaded to check max_links_allowed
  *
  * @param $ID ID of the item (-1 if new item)
  * @param $right Right to check : r / w / recursive
  * @param $input array of input data (used for adding item)
  *
  * @return boolean
  **/
 function can($ID, $right, &$input = NULL)
 {
     if ($ID < 0) {
         // Ajout
         $contract = new Contract();
         if (!$contract->getFromDB($input['contracts_id'])) {
             return false;
         }
         if ($contract->fields['max_links_allowed'] > 0 && countElementsInTable($this->getTable(), "`contracts_id`='" . $input['contracts_id'] . "'") >= $contract->fields['max_links_allowed']) {
             return false;
         }
     }
     return parent::can($ID, $right, $input);
 }
Ejemplo n.º 3
0
 /**
  * Transfer contracts
  *
  * @param $itemtype original type of transfered item
  * @param $ID original ID of the contract
  * @param $newID new ID of the contract
  **/
 function transferContracts($itemtype, $ID, $newID)
 {
     global $DB;
     $need_clean_process = false;
     // if keep
     if ($this->options['keep_contract']) {
         $contract = new Contract();
         // Get contracts for the item
         $query = "SELECT *\n                   FROM `glpi_contracts_items`\n                   WHERE `items_id` = '{$ID}'\n                         AND `itemtype` = '{$itemtype}'\n                         AND `contracts_id` NOT IN " . $this->item_recurs['Contract'];
         if ($result = $DB->query($query)) {
             if ($DB->numrows($result) > 0) {
                 // Foreach get item
                 while ($data = $DB->fetch_array($result)) {
                     $need_clean_process = false;
                     $item_ID = $data['contracts_id'];
                     $newcontractID = -1;
                     // is already transfer ?
                     if (isset($this->already_transfer['Contract'][$item_ID])) {
                         $newcontractID = $this->already_transfer['Contract'][$item_ID];
                         if ($newcontractID != $item_ID) {
                             $need_clean_process = true;
                         }
                     } else {
                         // No
                         // Can be transfer without copy ? = all linked items need to be transfer (so not copy)
                         $canbetransfer = true;
                         $query = "SELECT DISTINCT `itemtype`\n                               FROM `glpi_contracts_items`\n                               WHERE `contracts_id` = '{$item_ID}'";
                         if ($result_type = $DB->query($query)) {
                             if ($DB->numrows($result_type) > 0) {
                                 while (($data_type = $DB->fetch_array($result_type)) && $canbetransfer) {
                                     $dtype = $data_type['itemtype'];
                                     if (isset($this->item_search[$dtype])) {
                                         // No items to transfer -> exists links
                                         $query_search = "SELECT count(*) AS CPT\n                                                  FROM `glpi_contracts_items`\n                                                  WHERE `contracts_id` = '{$item_ID}'\n                                                        AND `itemtype` = '{$dtype}'\n                                                        AND `items_id`\n                                                             NOT IN " . $this->item_search[$dtype];
                                         $result_search = $DB->query($query_search);
                                         if ($DB->result($result_search, 0, 'CPT') > 0) {
                                             $canbetransfer = false;
                                         }
                                     } else {
                                         $canbetransfer = false;
                                     }
                                 }
                             }
                         }
                         // Yes : transfer
                         if ($canbetransfer) {
                             $this->transferItem('Contract', $item_ID, $item_ID);
                             $newcontractID = $item_ID;
                         } else {
                             $need_clean_process = true;
                             $contract->getFromDB($item_ID);
                             // No : search contract
                             $query = "SELECT *\n                                  FROM `glpi_contracts`\n                                  WHERE `entities_id` = '" . $this->to . "'\n                                        AND `name` = '" . addslashes($contract->fields['name']) . "'";
                             if ($result_search = $DB->query($query)) {
                                 if ($DB->numrows($result_search) > 0) {
                                     $newcontractID = $DB->result($result_search, 0, 'id');
                                     $this->addToAlreadyTransfer('Contract', $item_ID, $newcontractID);
                                 }
                             }
                             // found : use it
                             // not found : copy contract
                             if ($newcontractID < 0) {
                                 // 1 - create new item
                                 unset($contract->fields['id']);
                                 $input = $contract->fields;
                                 $input['entities_id'] = $this->to;
                                 unset($contract->fields);
                                 $newcontractID = $contract->add($input);
                                 // 2 - transfer as copy
                                 $this->transferItem('Contract', $item_ID, $newcontractID);
                             }
                         }
                     }
                     // Update links
                     if ($ID == $newID) {
                         if ($item_ID != $newcontractID) {
                             $query = "UPDATE `glpi_contracts_items`\n                                  SET `contracts_id` = '{$newcontractID}'\n                                  WHERE `id` = '" . $data['id'] . "'";
                             $DB->query($query);
                         }
                         // Same Item -> update links
                     } else {
                         // Copy Item -> copy links
                         if ($item_ID != $newcontractID) {
                             $query = "INSERT\n                                  INTO `glpi_contracts_items`\n                                  (`contracts_id`, `items_id`, `itemtype`)\n                                  VALUES ('{$newcontractID}', '{$newID}', '{$itemtype}')";
                             $DB->query($query);
                         } else {
                             // same contract for new item update link
                             $query = "UPDATE `glpi_contracts_items`\n                                  SET `items_id` = '{$newID}'\n                                  WHERE `id` = '" . $data['id'] . "'";
                             $DB->query($query);
                         }
                     }
                     // If clean and unused ->
                     if ($need_clean_process && $this->options['clean_contract']) {
                         $query = "SELECT COUNT(*) AS CPT\n                               FROM `glpi_contracts_items`\n                               WHERE `contracts_id` = '{$item_ID}'";
                         if ($result_remaining = $DB->query($query)) {
                             if ($DB->result($result_remaining, 0, 'CPT') == 0) {
                                 if ($this->options['clean_contract'] == 1) {
                                     $contract->delete(array('id' => $item_ID));
                                 }
                                 if ($this->options['clean_contract'] == 2) {
                                     // purge
                                     $contract->delete(array('id' => $item_ID), 1);
                                 }
                             }
                         }
                     }
                 }
             }
         }
     } else {
         // else unlink
         $query = "DELETE\n                   FROM `glpi_contracts_items`\n                   WHERE `items_id` = '{$ID}'\n                         AND `itemtype` = '{$itemtype}'";
         $DB->query($query);
     }
 }
Ejemplo n.º 4
0
 /**
  * Print the contract costs
  *
  * @param $contract               Contract object
  * @param $withtemplate  boolean  Template or basic item (default '')
  *
  * @return Nothing (call to classes members)
  **/
 static function showForContract(Contract $contract, $withtemplate = '')
 {
     global $DB, $CFG_GLPI;
     $ID = $contract->fields['id'];
     if (!$contract->getFromDB($ID) || !$contract->can($ID, READ)) {
         return false;
     }
     $canedit = $contract->can($ID, UPDATE);
     echo "<div class='center'>";
     $query = "SELECT *\n                FROM `glpi_contractcosts`\n                WHERE `contracts_id` = '{$ID}'\n                ORDER BY `begin_date`";
     $rand = mt_rand();
     if ($canedit) {
         echo "<div id='viewcost" . $ID . "_{$rand}'></div>\n";
         echo "<script type='text/javascript' >\n";
         echo "function viewAddCost" . $ID . "_{$rand}() {\n";
         $params = array('type' => __CLASS__, 'parenttype' => 'Contract', 'contracts_id' => $ID, 'id' => -1);
         Ajax::updateItemJsCode("viewcost" . $ID . "_{$rand}", $CFG_GLPI["root_doc"] . "/ajax/viewsubitem.php", $params);
         echo "};";
         echo "</script>\n";
         echo "<div class='center firstbloc'>" . "<a class='vsubmit' href='javascript:viewAddCost" . $ID . "_{$rand}();'>";
         echo __('Add a new cost') . "</a></div>\n";
     }
     if ($result = $DB->query($query)) {
         echo "<table class='tab_cadre_fixehov'>";
         echo "<tr><th colspan='5'>" . self::getTypeName($DB->numrows($result)) . "</th></tr>";
         if ($DB->numrows($result)) {
             echo "<tr><th>" . __('Name') . "</th>";
             echo "<th>" . __('Begin date') . "</th>";
             echo "<th>" . __('End date') . "</th>";
             echo "<th>" . __('Budget') . "</th>";
             echo "<th>" . __('Cost') . "</th>";
             echo "</tr>";
             Session::initNavigateListItems(__CLASS__, sprintf(__('%1$s = %2$s'), Contract::getTypeName(1), $contract->getName()));
             $total = 0;
             while ($data = $DB->fetch_assoc($result)) {
                 echo "<tr class='tab_bg_2' " . ($canedit ? "style='cursor:pointer' onClick=\"viewEditCost" . $data['contracts_id'] . "_" . $data['id'] . "_{$rand}();\"" : '') . ">";
                 $name = empty($data['name']) ? sprintf(__('%1$s (%2$s)'), $data['name'], $data['id']) : $data['name'];
                 echo "<td>";
                 printf(__('%1$s %2$s'), $name, Html::showToolTip($data['comment'], array('display' => false)));
                 if ($canedit) {
                     echo "\n<script type='text/javascript' >\n";
                     echo "function viewEditCost" . $data['contracts_id'] . "_" . $data["id"] . "_{$rand}() {\n";
                     $params = array('type' => __CLASS__, 'parenttype' => 'Contract', 'contracts_id' => $data["contracts_id"], 'id' => $data["id"]);
                     Ajax::updateItemJsCode("viewcost" . $ID . "_{$rand}", $CFG_GLPI["root_doc"] . "/ajax/viewsubitem.php", $params);
                     echo "};";
                     echo "</script>\n";
                 }
                 echo "</td>";
                 echo "<td>" . Html::convDate($data['begin_date']) . "</td>";
                 echo "<td>" . Html::convDate($data['end_date']) . "</td>";
                 echo "<td>" . Dropdown::getDropdownName('glpi_budgets', $data['budgets_id']) . "</td>";
                 echo "<td class='numeric'>" . Html::formatNumber($data['cost']) . "</td>";
                 $total += $data['cost'];
                 echo "</tr>";
                 Session::addToNavigateListItems(__CLASS__, $data['id']);
             }
             echo "<tr class='b noHover'><td colspan='3'>&nbsp;</td>";
             echo "<td class='right'>" . __('Total cost') . '</td>';
             echo "<td class='numeric'>" . Html::formatNumber($total) . '</td></tr>';
         } else {
             echo "<tr><th colspan='5'>" . __('No item found') . "</th></tr>";
         }
         echo "</table>";
     }
     echo "</div><br>";
 }
Ejemplo n.º 5
0
}
if (!isset($_GET["contracts_id"])) {
    $_GET["contracts_id"] = "";
}
$cost = new ContractCost();
if (isset($_POST["add"])) {
    $cost->check(-1, CREATE, $_POST);
    if ($newID = $cost->add($_POST)) {
        Event::log($_POST['contracts_id'], "contracts", 4, "financial", sprintf(__('%s adds a cost'), $_SESSION["glpiname"]));
    }
    Html::back();
} else {
    if (isset($_POST["purge"])) {
        $cost->check($_POST["id"], PURGE);
        if ($cost->delete($_POST, 1)) {
            Event::log($cost->fields['contracts_id'], "contracts", 4, "financial", sprintf(__('%s purges a cost'), $_SESSION["glpiname"]));
        }
        $contract = new Contract();
        $contract->getFromDB($cost->fields['contracts_id']);
        Html::redirect(Toolbox::getItemTypeFormURL('Contract') . '?id=' . $cost->fields['contracts_id'] . ($contract->fields['is_template'] ? "&withtemplate=1" : ""));
    } else {
        if (isset($_POST["update"])) {
            $cost->check($_POST["id"], UPDATE);
            if ($cost->update($_POST)) {
                Event::log($cost->fields['contracts_id'], "contracts", 4, "financial", sprintf(__('%s updates a cost'), $_SESSION["glpiname"]));
            }
            Html::back();
        }
    }
}
Html::displayErrorAndDie('Lost');
Ejemplo n.º 6
0
 /**
  * Print an HTML array of contract associated to an object
  *
  * @since version 0.84
  *
  * @param $item            CommonDBTM object wanted
  * @param $withtemplate    not used (to be deleted) (default '')
  *
  * @return Nothing (display)
  **/
 static function showForItem(CommonDBTM $item, $withtemplate = '')
 {
     global $DB, $CFG_GLPI;
     $itemtype = $item->getType();
     $ID = $item->fields['id'];
     if (!Contract::canView() || !$item->can($ID, READ)) {
         return false;
     }
     $canedit = $item->can($ID, UPDATE);
     $rand = mt_rand();
     $query = "SELECT `glpi_contracts_items`.*\n                FROM `glpi_contracts_items`,\n                     `glpi_contracts`\n                LEFT JOIN `glpi_entities` ON (`glpi_contracts`.`entities_id`=`glpi_entities`.`id`)\n                WHERE `glpi_contracts`.`id`=`glpi_contracts_items`.`contracts_id`\n                      AND `glpi_contracts_items`.`items_id` = '{$ID}'\n                      AND `glpi_contracts_items`.`itemtype` = '{$itemtype}'" . getEntitiesRestrictRequest(" AND", "glpi_contracts", '', '', true) . "\n                ORDER BY `glpi_contracts`.`name`";
     $result = $DB->query($query);
     $contracts = array();
     $used = array();
     if ($number = $DB->numrows($result)) {
         while ($data = $DB->fetch_assoc($result)) {
             $contracts[$data['id']] = $data;
             $used[$data['contracts_id']] = $data['contracts_id'];
         }
     }
     if ($canedit && $withtemplate != 2) {
         echo "<div class='firstbloc'>";
         echo "<form name='contractitem_form{$rand}' id='contractitem_form{$rand}' method='post'\n                action='" . Toolbox::getItemTypeFormURL(__CLASS__) . "'>";
         echo "<input type='hidden' name='items_id' value='{$ID}'>";
         echo "<input type='hidden' name='itemtype' value='{$itemtype}'>";
         echo "<table class='tab_cadre_fixe'>";
         echo "<tr class='tab_bg_2'><th colspan='2'>" . __('Add a contract') . "</th></tr>";
         echo "<tr class='tab_bg_1'><td>";
         Contract::dropdown(array('entity' => $item->getEntityID(), 'used' => $used));
         echo "</td><td class='center'>";
         echo "<input type='submit' name='add' value=\"" . _sx('button', 'Add') . "\" class='submit'>";
         echo "</td></tr>";
         echo "</table>";
         Html::closeForm();
         echo "</div>";
     }
     echo "<div class='spaced'>";
     if ($withtemplate != 2) {
         if ($canedit && $number) {
             Html::openMassiveActionsForm('mass' . __CLASS__ . $rand);
             $massiveactionparams = array('num_displayed' => $number, 'container' => 'mass' . __CLASS__ . $rand);
             Html::showMassiveActions($massiveactionparams);
         }
     }
     echo "<table class='tab_cadre_fixehov'>";
     $header_begin = "<tr>";
     $header_top = '';
     $header_bottom = '';
     $header_end = '';
     if ($canedit && $number && $withtemplate != 2) {
         $header_top .= "<th width='10'>" . Html::getCheckAllAsCheckbox('mass' . __CLASS__ . $rand);
         $header_top .= "</th>";
         $header_bottom .= "<th width='10'>" . Html::getCheckAllAsCheckbox('mass' . __CLASS__ . $rand);
         $header_bottom .= "</th>";
     }
     $header_end .= "<th>" . __('Name') . "</th>";
     $header_end .= "<th>" . __('Entity') . "</th>";
     $header_end .= "<th>" . _x('phone', 'Number') . "</th>";
     $header_end .= "<th>" . __('Contract type') . "</th>";
     $header_end .= "<th>" . __('Supplier') . "</th>";
     $header_end .= "<th>" . __('Start date') . "</th>";
     $header_end .= "<th>" . __('Initial contract period') . "</th>";
     $header_end .= "</tr>";
     echo $header_begin . $header_top . $header_end;
     if ($number > 0) {
         Session::initNavigateListItems(__CLASS__, sprintf(__('%1$s = %2$s'), $item->getTypeName(1), $item->getName()));
         foreach ($contracts as $data) {
             $cID = $data["contracts_id"];
             Session::addToNavigateListItems(__CLASS__, $cID);
             $contracts[] = $cID;
             $assocID = $data["id"];
             $con = new Contract();
             $con->getFromDB($cID);
             echo "<tr class='tab_bg_1" . ($con->fields["is_deleted"] ? "_2" : "") . "'>";
             if ($canedit && $withtemplate != 2) {
                 echo "<td width='10'>";
                 Html::showMassiveActionCheckBox(__CLASS__, $data["id"]);
                 echo "</td>";
             }
             echo "<td class='center b'>";
             $name = $con->fields["name"];
             if ($_SESSION["glpiis_ids_visible"] || empty($con->fields["name"])) {
                 $name = sprintf(__('%1$s (%2$s)'), $name, $con->fields["id"]);
             }
             echo "<a href='" . $CFG_GLPI["root_doc"] . "/front/contract.form.php?id={$cID}'>" . $name;
             echo "</a></td>";
             echo "<td class='center'>";
             echo Dropdown::getDropdownName("glpi_entities", $con->fields["entities_id"]) . "</td>";
             echo "<td class='center'>" . $con->fields["num"] . "</td>";
             echo "<td class='center'>";
             echo Dropdown::getDropdownName("glpi_contracttypes", $con->fields["contracttypes_id"]) . "</td>";
             echo "<td class='center'>" . $con->getSuppliersNames() . "</td>";
             echo "<td class='center'>" . Html::convDate($con->fields["begin_date"]) . "</td>";
             echo "<td class='center'>" . sprintf(__('%1$s %2$s'), $con->fields["duration"], _n('month', 'months', $con->fields["duration"]));
             if ($con->fields["begin_date"] != '' && !empty($con->fields["begin_date"])) {
                 echo " -> " . Infocom::getWarrantyExpir($con->fields["begin_date"], $con->fields["duration"], 0, true);
             }
             echo "</td>";
             echo "</tr>";
         }
         echo $header_begin . $header_bottom . $header_end;
     }
     echo "</table>";
     if ($canedit && $number && $withtemplate != 2) {
         $massiveactionparams['ontop'] = false;
         Html::showMassiveActions($massiveactionparams);
         Html::closeForm();
     }
     echo "</div>";
 }
 /**
  * Return Infocom for an object
  *
  * @param $protocol        the commonication protocol used
  * @param $params    array
  *
  * @return a hasdtable, fields of glpi_infocoms
  **/
 static function methodgetItemContracts($params, $protocol)
 {
     global $DB;
     if (isset($params['help'])) {
         $params = array('itemtype' => 'string, mandatory', 'id' => 'integer, mandatory', 'id2name' => 'bool,optional', 'help' => 'bool,optional');
     }
     $check = self::checkStandardParameters($params, $protocol);
     if (!$check == 1) {
         exit;
     }
     $item = new $params['itemtype']();
     if (!$item->getFromDB($params['id']) || !Session::haveRight('contract', READ) || !$item->can($params['id'], READ)) {
         return self::Error($protocol, WEBSERVICES_ERROR_NOTFOUND);
     }
     $contract = new Contract();
     $query = "SELECT `glpi_contracts`.*\n                FROM `glpi_contracts_items`, `glpi_contracts`\n                LEFT JOIN `glpi_entities` ON (`glpi_contracts`.`entities_id` = `glpi_entities`.`id`)\n                WHERE `glpi_contracts`.`id` = `glpi_contracts_items`.`contracts_id`\n                      AND `glpi_contracts_items`.`items_id` = '" . $params['id'] . "'\n                      AND `glpi_contracts_items`.`itemtype` = '" . $params['itemtype'] . "'" . getEntitiesRestrictRequest(" AND", "glpi_contracts", '', '', true) . "\n                ORDER BY `glpi_contracts`.`name`";
     $result = $DB->query($query);
     $resp = array();
     while ($datas = $DB->fetch_array($result)) {
         $contract->getFromDB($datas['id']);
         $resp[$datas['id']] = $contract->fields;
         if ($id2name) {
             $resp[$datas['id']]['contracttypes_name'] = Html::clean(Dropdown::getDropdownName('glpi_contracttypes', $contract->fields['contracttypes_id']));
         }
     }
     return $resp;
 }
Ejemplo n.º 8
0
 /**
  * Do the standard massive actions
  *
  * @since version 0.84
  *
  * This must not be overloaded in Class
  * @param $input array of input datas
  *
  * @return an array of results (ok, ko, noright counts, may include REDIRECT field to set REDIRECT page)
  **/
 function doMassiveActions($input = array())
 {
     global $CFG_GLPI;
     if (!isset($input["item"]) || count($input["item"]) == 0) {
         return false;
     }
     $res = array('ok' => 0, 'ko' => 0, 'noright' => 0);
     switch ($input['action']) {
         case 'add_document':
         case 'remove_document':
             $doc = new Document();
             return $doc->doSpecificMassiveActions($input);
         case "add_transfer_list":
             if (!isset($_SESSION['glpitransfer_list'])) {
                 $_SESSION['glpitransfer_list'] = array();
             }
             if (!isset($_SESSION['glpitransfer_list'][$input["itemtype"]])) {
                 $_SESSION['glpitransfer_list'][$input["itemtype"]] = array();
             }
             foreach ($input["item"] as $key => $val) {
                 if ($val == 1) {
                     $_SESSION['glpitransfer_list'][$input["itemtype"]][$key] = $key;
                     $res['ok']++;
                 }
             }
             $res['REDIRECT'] = $CFG_GLPI['root_doc'] . '/front/transfer.action.php';
             break;
         case "delete":
             foreach ($input["item"] as $key => $val) {
                 if ($val == 1) {
                     if ($this->can($key, 'd')) {
                         if ($this->delete(array("id" => $key))) {
                             $res['ok']++;
                         } else {
                             $res['ko']++;
                         }
                     } else {
                         $res['noright']++;
                     }
                 }
             }
             break;
         case "purge":
             foreach ($input["item"] as $key => $val) {
                 if ($val == 1) {
                     if ($this->can($key, 'd')) {
                         $force = 1;
                         // Only mark deletion for
                         if ($this->maybeDeleted() && $this->useDeletedToLockIfDynamic() && $this->isDynamic()) {
                             $force = 0;
                         }
                         if ($this->delete(array("id" => $key), $force)) {
                             $res['ok']++;
                         } else {
                             $res['ko']++;
                         }
                     } else {
                         $res['noright']++;
                     }
                 }
             }
             break;
         case "restore":
             foreach ($input["item"] as $key => $val) {
                 if ($val == 1) {
                     if ($this->can($key, 'd')) {
                         if ($this->restore(array("id" => $key))) {
                             $res['ok']++;
                         } else {
                             $res['ko']++;
                         }
                     } else {
                         $res['noright']++;
                     }
                 }
             }
             break;
         case "update":
             $searchopt = Search::getCleanedOptions($input["itemtype"], 'w');
             if (isset($searchopt[$input["id_field"]])) {
                 /// Infocoms case
                 if (!isPluginItemType($input["itemtype"]) && Search::isInfocomOption($input["itemtype"], $input["id_field"])) {
                     $ic = new Infocom();
                     $link_entity_type = -1;
                     /// Specific entity item
                     if ($searchopt[$input["id_field"]]["table"] == "glpi_suppliers") {
                         $ent = new Supplier();
                         if ($ent->getFromDB($input[$input["field"]])) {
                             $link_entity_type = $ent->fields["entities_id"];
                         }
                     }
                     foreach ($input["item"] as $key => $val) {
                         if ($val == 1) {
                             if ($this->getFromDB($key)) {
                                 if ($link_entity_type < 0 || $link_entity_type == $this->getEntityID() || $ent->fields["is_recursive"] && in_array($link_entity_type, getAncestorsOf("glpi_entities", $this->getEntityID()))) {
                                     $input2["items_id"] = $key;
                                     $input2["itemtype"] = $input["itemtype"];
                                     if ($ic->can(-1, 'w', $input2)) {
                                         // Add infocom if not exists
                                         if (!$ic->getFromDBforDevice($input["itemtype"], $key)) {
                                             $input2["items_id"] = $key;
                                             $input2["itemtype"] = $input["itemtype"];
                                             unset($ic->fields);
                                             $ic->add($input2);
                                             $ic->getFromDBforDevice($input["itemtype"], $key);
                                         }
                                         $id = $ic->fields["id"];
                                         unset($ic->fields);
                                         if ($ic->update(array('id' => $id, $input["field"] => $input[$input["field"]]))) {
                                             $res['ok']++;
                                         } else {
                                             $res['ko']++;
                                         }
                                     } else {
                                         $res['noright']++;
                                     }
                                 } else {
                                     $res['ko']++;
                                 }
                             } else {
                                 $res['ko']++;
                             }
                         }
                     }
                 } else {
                     /// Not infocoms
                     $link_entity_type = array();
                     /// Specific entity item
                     $itemtable = getTableForItemType($input["itemtype"]);
                     $itemtype2 = getItemTypeForTable($searchopt[$input["id_field"]]["table"]);
                     if ($item2 = getItemForItemtype($itemtype2)) {
                         if ($input["id_field"] != 80 && $searchopt[$input["id_field"]]["table"] != $itemtable && $item2->isEntityAssign() && $this->isEntityAssign()) {
                             if ($item2->getFromDB($input[$input["field"]])) {
                                 if (isset($item2->fields["entities_id"]) && $item2->fields["entities_id"] >= 0) {
                                     if (isset($item2->fields["is_recursive"]) && $item2->fields["is_recursive"]) {
                                         $link_entity_type = getSonsOf("glpi_entities", $item2->fields["entities_id"]);
                                     } else {
                                         $link_entity_type[] = $item2->fields["entities_id"];
                                     }
                                 }
                             }
                         }
                     }
                     foreach ($input["item"] as $key => $val) {
                         if ($val == 1) {
                             if ($this->can($key, 'w') && $this->canMassiveAction($input['action'], $input['field'], $input[$input["field"]])) {
                                 if (count($link_entity_type) == 0 || in_array($this->fields["entities_id"], $link_entity_type)) {
                                     if ($this->update(array('id' => $key, $input["field"] => $input[$input["field"]]))) {
                                         $res['ok']++;
                                     } else {
                                         $res['ko']++;
                                     }
                                 } else {
                                     $res['ko']++;
                                 }
                             } else {
                                 $res['noright']++;
                             }
                         }
                     }
                 }
             }
             break;
         case "activate_infocoms":
             $ic = new Infocom();
             if ($ic->canCreate()) {
                 foreach ($input["item"] as $key => $val) {
                     $input = array('itemtype' => $input['itemtype'], 'items_id' => $key);
                     if (!$ic->getFromDBforDevice($input['itemtype'], $key)) {
                         if ($ic->can(-1, 'w', $input)) {
                             if ($ic->add($input)) {
                                 $res['ok']++;
                             } else {
                                 $res['ko']++;
                             }
                         } else {
                             $res['noright']++;
                         }
                     } else {
                         $res['ko']++;
                     }
                 }
             }
             break;
         case "add_contract_item":
             $contractitem = new Contract_Item();
             foreach ($input["item"] as $key => $val) {
                 if (isset($input['items_id'])) {
                     // Add items to contracts
                     $input = array('itemtype' => $input["item_itemtype"], 'items_id' => $input["items_id"], 'contracts_id' => $key);
                 }
                 if (isset($input['contracts_id'])) {
                     // Add contract to item
                     $input = array('itemtype' => $input["itemtype"], 'items_id' => $key, 'contracts_id' => $input['contracts_id']);
                 } else {
                     return false;
                 }
                 if ($contractitem->can(-1, 'w', $input)) {
                     if ($contractitem->add($input)) {
                         $res['ok']++;
                     } else {
                         $res['ko']++;
                     }
                 } else {
                     $res['noright']++;
                 }
             }
             break;
         case "remove_contract_item":
             foreach ($input["item"] as $key => $val) {
                 if (isset($input['items_id'])) {
                     // Remove item to contracts
                     $input = array('itemtype' => $input["item_itemtype"], 'items_id' => $input["items_id"], 'contracts_id' => $key);
                 } else {
                     // Remove contract to items
                     $input = array('itemtype' => $input["itemtype"], 'items_id' => $key, 'contracts_id' => $input['contracts_id']);
                 }
                 $contractitem = new Contract_Item();
                 if ($contractitem->can(-1, 'w', $input)) {
                     if ($item = getItemForItemtype($input["itemtype"])) {
                         if ($item->getFromDB($input['items_id'])) {
                             $contract = new Contract();
                             if ($contract->getFromDB($input['contracts_id'])) {
                                 if ($contractitem->getFromDBForItems($contract, $item)) {
                                     if ($contractitem->delete(array('id' => $contractitem->getID()))) {
                                         $res['ok']++;
                                     } else {
                                         $res['ko']++;
                                     }
                                 } else {
                                     $res['ko']++;
                                 }
                             } else {
                                 $res['ko']++;
                             }
                         } else {
                             $res['ko']++;
                         }
                     } else {
                         $res['ko']++;
                     }
                 } else {
                     $res['noright']++;
                 }
             }
             break;
             //Lock management
         //Lock management
         case 'unlock_Printer':
         case 'unlock_Monitor':
         case 'unlock_NetworkPort':
         case 'unlock_NetworkName':
         case 'unlock_IPAddress':
         case 'unlock_ComputerDisk':
         case 'unlock_ComputerVirtualMachine':
         case 'unlock_Peripheral':
         case 'unlock_SoftwareVersion':
             $itemtype = Lock::getItemTypeForMassiveAction($input["action"]);
             if ($itemtype) {
                 $res = Lock::unlockItems($itemtype, $this->getType(), $input["item"]);
             }
             break;
         case 'unlock_Device':
             foreach (Item_Devices::getDeviceTypes() as $itemtype) {
                 $res = Lock::unlockItems($itemtype, $this->getType(), $input["item"]);
             }
             break;
         default:
             // Plugin specific actions
             $split = explode('_', $input["action"]);
             $res = '';
             if ($split[0] == 'plugin' && isset($split[1])) {
                 // Normalized name plugin_name_action
                 // Allow hook from any plugin on any (core or plugin) type
                 $res = Plugin::doOneHook($split[1], 'MassiveActionsProcess', $input);
                 //            } else if ($plug=isPluginItemType($input["itemtype"])) {
                 // non-normalized name
                 // hook from the plugin defining the type
                 //               $res = Plugin::doOneHook($plug['plugin'], 'MassiveActionsProcess', $input);
             } else {
                 $res = $this->doSpecificMassiveActions($input);
             }
             break;
     }
     return $res;
 }
Ejemplo n.º 9
0
 function dropdownTaskItems($ID, $name, $used = array())
 {
     global $DB, $CFG_GLPI;
     $restrict = "`plugin_projet_projets_id` = '{$ID}'";
     $items = getAllDatasFromTable("glpi_plugin_projet_projets_items", $restrict);
     $restrictdoc = "`items_id` = '{$ID}' AND `itemtype` = 'PluginProjetProjet'";
     $docs = getAllDatasFromTable("glpi_documents_items", $restrictdoc);
     $restrictcontract = "`items_id` = '{$ID}' AND `itemtype` = 'PluginProjetProjet'";
     $contracts = getAllDatasFromTable("glpi_contracts_items", $restrictcontract);
     echo "<select name='{$name}'>";
     echo "<option value='0' selected>" . Dropdown::EMPTY_VALUE . "</option>";
     if (!empty($items)) {
         foreach ($items as $item) {
             $table = getTableForItemType($item["itemtype"]);
             $query = "SELECT `" . $table . "`.*\n                     FROM `glpi_plugin_projet_projets_items`\n                     INNER JOIN `" . $table . "` ON (`" . $table . "`.`id` = `glpi_plugin_projet_projets_items`.`items_id`)\n                     WHERE `glpi_plugin_projet_projets_items`.`itemtype` = '" . $item["itemtype"] . "'\n                     AND `glpi_plugin_projet_projets_items`.`items_id` = '" . $item["items_id"] . "' ";
             if (count($used)) {
                 $query .= " AND `" . $table . "`.`id` NOT IN (0";
                 foreach ($used as $ID) {
                     $query .= ",{$ID}";
                 }
                 $query .= ")";
             }
             $query .= " GROUP BY `" . $table . "`.`name`";
             $query .= " ORDER BY `" . $table . "`.`name`";
             $result_linked = $DB->query($query);
             if ($DB->numrows($result_linked)) {
                 while ($data = $DB->fetch_assoc($result_linked)) {
                     $name = $data["name"];
                     $itemclass = new $item["itemtype"]();
                     if ($item["itemtype"] == 'User') {
                         $name = getUserName($data["id"]);
                     }
                     echo "<option value='" . $data["id"] . "," . $item["itemtype"] . "'>" . $itemclass->getTypeName() . " - " . $name;
                     if (empty($data["name"]) || $_SESSION["glpiis_ids_visible"] == 1) {
                         echo " (";
                         echo $data["id"] . ")";
                     }
                     echo "</option>";
                 }
             }
         }
     }
     if (!empty($docs)) {
         foreach ($docs as $doc) {
             $docclass = new Document();
             if ($docclass->getFromDB($doc["documents_id"])) {
                 $name = $docclass->fields["name"];
                 echo "<option value='" . $doc["documents_id"] . ",Document'>" . $docclass->getTypeName() . " - " . $name;
                 if (empty($name) || $_SESSION["glpiis_ids_visible"] == 1) {
                     echo " (";
                     echo $doc["documents_id"] . ")";
                 }
                 echo "</option>";
             }
         }
     }
     if (!empty($contracts)) {
         foreach ($contracts as $contract) {
             $contractclass = new Contract();
             if ($contractclass->getFromDB($contract["contracts_id"])) {
                 $name = $contractclass->fields["name"];
                 echo "<option value='" . $contract["contracts_id"] . ",Contract'>" . $contractclass->getTypeName() . " - " . $name;
                 if (empty($name) || $_SESSION["glpiis_ids_visible"] == 1) {
                     echo " (";
                     echo $contract["contracts_id"] . ")";
                 }
                 echo "</option>";
             }
         }
     }
     echo "</select>";
 }
Ejemplo n.º 10
0
 /**
  * Print an HTML array of contract associated to an object
  *
  *
  *@param $item CommonDBTM : object wanted
  *@param $withtemplate='' not used (to be deleted)
  *
  *@return Nothing (display)
  *
  **/
 static function showAssociated(CommonDBTM $item, $withtemplate = '')
 {
     global $DB, $CFG_GLPI, $LANG;
     $itemtype = $item->getType();
     $ID = $item->fields['id'];
     if (!haveRight("contract", "r") || !$item->can($ID, "r")) {
         return false;
     }
     $canedit = $item->can($ID, "w");
     $query = "SELECT `glpi_contracts_items`.*\n                FROM `glpi_contracts_items`,\n                     `glpi_contracts`\n                LEFT JOIN `glpi_entities` ON (`glpi_contracts`.`entities_id`=`glpi_entities`.`id`)\n                WHERE `glpi_contracts`.`id`=`glpi_contracts_items`.`contracts_id`\n                      AND `glpi_contracts_items`.`items_id` = '{$ID}'\n                      AND `glpi_contracts_items`.`itemtype` = '{$itemtype}'" . getEntitiesRestrictRequest(" AND", "glpi_contracts", '', '', true) . "\n                ORDER BY `glpi_contracts`.`name`";
     $result = $DB->query($query);
     $number = $DB->numrows($result);
     $i = 0;
     echo "<div class='spaced'>";
     if ($withtemplate != 2) {
         echo "<form method='post' action=\"" . $CFG_GLPI["root_doc"] . "/front/contract.form.php\">";
     }
     echo "<table class='tab_cadre_fixe'>";
     echo "<tr><th colspan='8'>";
     if ($number == 0) {
         echo $LANG['financial'][58];
     } else {
         if ($number == 1) {
             echo $LANG['financial'][63];
         } else {
             echo $LANG['financial'][66];
         }
     }
     echo "</th></tr>";
     echo "<tr><th>" . $LANG['common'][16] . "</th>";
     echo "<th>" . $LANG['entity'][0] . "</th>";
     echo "<th>" . $LANG['financial'][4] . "</th>";
     echo "<th>" . $LANG['financial'][6] . "</th>";
     echo "<th>" . $LANG['financial'][26] . "</th>";
     echo "<th>" . $LANG['search'][8] . "</th>";
     echo "<th>" . $LANG['financial'][8] . "</th>";
     if ($withtemplate != 2) {
         echo "<th>&nbsp;</th>";
     }
     echo "</tr>";
     if ($number > 0) {
         initNavigateListItems('Contract', $item->getTypeName() . " = " . $item->getName());
     }
     $contracts = array();
     while ($i < $number) {
         $cID = $DB->result($result, $i, "contracts_id");
         addToNavigateListItems('Contract', $cID);
         $contracts[] = $cID;
         $assocID = $DB->result($result, $i, "id");
         $con = new Contract();
         $con->getFromDB($cID);
         echo "<tr class='tab_bg_1" . ($con->fields["is_deleted"] ? "_2" : "") . "'>";
         echo "<td class='center'>";
         echo "<a href='" . $CFG_GLPI["root_doc"] . "/front/contract.form.php?id={$cID}'>";
         echo "<strong>" . $con->fields["name"];
         if ($_SESSION["glpiis_ids_visible"] || empty($con->fields["name"])) {
             echo " (" . $con->fields["id"] . ")";
         }
         echo "</strong></a></td>";
         echo "<td class='center'>";
         echo Dropdown::getDropdownName("glpi_entities", $con->fields["entities_id"]) . "</td>";
         echo "<td class='center'>" . $con->fields["num"] . "</td>";
         echo "<td class='center'>";
         echo Dropdown::getDropdownName("glpi_contracttypes", $con->fields["contracttypes_id"]) . "</td>";
         echo "<td class='center'>" . $con->getSuppliersNames() . "</td>";
         echo "<td class='center'>" . convDate($con->fields["begin_date"]) . "</td>";
         echo "<td class='center'>" . $con->fields["duration"] . " " . $LANG['financial'][57];
         if ($con->fields["begin_date"] != '' && !empty($con->fields["begin_date"])) {
             echo " -> " . getWarrantyExpir($con->fields["begin_date"], $con->fields["duration"]);
         }
         echo "</td>";
         if ($withtemplate != 2) {
             echo "<td class='tab_bg_2 center'>";
             if ($canedit) {
                 echo "<a href='" . $CFG_GLPI["root_doc"] . "/front/contract.form.php?deleteitem=deleteitem&amp;id={$assocID}&amp;contracts_id={$cID}'>";
                 echo "<img src='" . $CFG_GLPI["root_doc"] . "/pics/delete2.png' alt='" . $LANG['buttons'][6] . "'></a>";
             } else {
                 echo "&nbsp;";
             }
             echo "</td>";
         }
         echo "</tr>";
         $i++;
     }
     $q = "SELECT *\n            FROM `glpi_contracts`\n            WHERE `is_deleted` = '0' " . getEntitiesRestrictRequest("AND", "glpi_contracts", "entities_id", $item->getEntityID(), true);
     $result = $DB->query($q);
     $nb = $DB->numrows($result);
     if ($canedit) {
         if ($withtemplate != 2 && $nb > count($contracts)) {
             echo "<tr class='tab_bg_1'><td class='right' colspan='3'>";
             echo "<input type='hidden' name='items_id' value='{$ID}'>";
             echo "<input type='hidden' name='itemtype' value='{$itemtype}'>";
             self::dropdown(array('entity' => $item->getEntityID(), 'used' => $contracts));
             echo "</td><td class='center'>";
             echo "<input type='submit' name='additem' value=\"" . $LANG['buttons'][8] . "\"\n                   class='submit'>";
             echo "</td>";
             echo "<td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>";
         }
     }
     echo "</table>";
     if ($withtemplate != 2) {
         echo "</form>";
     }
     echo "</div>";
 }