static function showForContract(Contract $contract)
{
global $DB, $CFG_GLPI;
$instID = $contract->fields['id'];
if (!$contract->can($instID, READ)) {
return false;
}
$canedit = $contract->can($instID, UPDATE);
$rand = mt_rand();
$query = "SELECT DISTINCT `itemtype`\n FROM `glpi_contracts_items`\n WHERE `glpi_contracts_items`.`contracts_id` = '{$instID}'\n ORDER BY `itemtype`";
$result = $DB->query($query);
$number = $DB->numrows($result);
$data = array();
$totalnb = 0;
$used = array();
for ($i = 0; $i < $number; $i++) {
$itemtype = $DB->result($result, $i, "itemtype");
if (!($item = getItemForItemtype($itemtype))) {
continue;
}
if ($item->canView()) {
$itemtable = getTableForItemType($itemtype);
$query = "SELECT `{$itemtable}`.*,\n `glpi_contracts_items`.`id` AS IDD,\n `glpi_entities`.`id` AS entity\n FROM `glpi_contracts_items`,\n `{$itemtable}`";
if ($itemtype != 'Entity') {
$query .= " LEFT JOIN `glpi_entities`\n ON (`{$itemtable}`.`entities_id`=`glpi_entities`.`id`) ";
}
$query .= " WHERE `{$itemtable}`.`id` = `glpi_contracts_items`.`items_id`\n AND `glpi_contracts_items`.`itemtype` = '{$itemtype}'\n AND `glpi_contracts_items`.`contracts_id` = '{$instID}'";
if ($item->maybeTemplate()) {
$query .= " AND `{$itemtable}`.`is_template` = '0'";
}
$query .= getEntitiesRestrictRequest(" AND", $itemtable, '', '', $item->maybeRecursive()) . "\n ORDER BY `glpi_entities`.`completename`, `{$itemtable}`.`name`";
$result_linked = $DB->query($query);
$nb = $DB->numrows($result_linked);
if ($nb > $_SESSION['glpilist_limit']) {
$opt = array('order' => 'ASC', 'is_deleted' => 0, 'reset' => 'reset', 'start' => 0, 'sort' => 80, 'criteria' => array(0 => array('value' => '$$$$' . $instID, 'searchtype' => 'contains', 'field' => 29)));
$url = $item::getSearchURL();
$url .= strpos($url, '?') ? '&' : '?';
$url .= Toolbox::append_params($opt);
$link = "<a href='{$url}'>" . __('Device list') . "</a>";
$data[$itemtype] = array('longlist' => true, 'name' => sprintf(__('%1$s: %2$s'), $item->getTypeName($nb), $nb), 'link' => $link);
} else {
if ($nb > 0) {
$data[$itemtype] = array();
while ($objdata = $DB->fetch_assoc($result_linked)) {
$data[$itemtype][$objdata['id']] = $objdata;
$used[$itemtype][$objdata['id']] = $objdata['id'];
}
}
}
$totalnb += $nb;
}
}
if ($canedit && ($contract->fields['max_links_allowed'] == 0 || $contract->fields['max_links_allowed'] > $totalnb)) {
echo "<div class='firstbloc'>";
echo "<form name='contract_form{$rand}' id='contract_form{$rand}' method='post'\n action='" . Toolbox::getItemTypeFormURL(__CLASS__) . "'>";
echo "<table class='tab_cadre_fixe'>";
echo "<tr class='tab_bg_2'><th colspan='2'>" . __('Add an item') . "</th></tr>";
echo "<tr class='tab_bg_1'><td class='right'>";
Dropdown::showSelectItemFromItemtypes(array('itemtypes' => $CFG_GLPI["contract_types"], 'entity_restrict' => $contract->fields['is_recursive'] ? getSonsOf('glpi_entities', $contract->fields['entities_id']) : $contract->fields['entities_id'], 'checkright' => true, 'used' => $used));
echo "</td><td class='center'>";
echo "<input type='submit' name='add' value=\"" . _sx('button', 'Add') . "\" class='submit'>";
echo "<input type='hidden' name='contracts_id' value='{$instID}'>";
echo "</td></tr>";
echo "</table>";
Html::closeForm();
echo "</div>";
}
echo "<div class='spaced'>";
if ($canedit && $totalnb) {
Html::openMassiveActionsForm('mass' . __CLASS__ . $rand);
$massiveactionparams = array('container' => 'mass' . __CLASS__ . $rand);
Html::showMassiveActions($massiveactionparams);
}
echo "<table class='tab_cadre_fixehov'>";
$header_begin = "<tr>";
$header_top = '';
$header_bottom = '';
$header_end = '';
if ($canedit && $totalnb) {
$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>" . __('Type') . "</th>";
$header_end .= "<th>" . __('Entity') . "</th>";
$header_end .= "<th>" . __('Name') . "</th>";
$header_end .= "<th>" . __('Serial number') . "</th>";
$header_end .= "<th>" . __('Inventory number') . "</th>";
$header_end .= "<th>" . __('Status') . "</th>";
$header_end .= "</tr>";
echo $header_begin . $header_top . $header_end;
$totalnb = 0;
foreach ($data as $itemtype => $datas) {
if (isset($datas['longlist'])) {
echo "<tr class='tab_bg_1'>";
if ($canedit) {
echo "<td> </td>";
}
echo "<td class='center'>" . $datas['name'] . "</td>";
echo "<td class='center' colspan='2'>" . $datas['link'] . "</td>";
echo "<td class='center'>-</td><td class='center'>-</td></tr>";
} else {
$prem = true;
$nb = count($datas);
foreach ($datas as $id => $objdata) {
$name = $objdata["name"];
if ($_SESSION["glpiis_ids_visible"] || empty($data["name"])) {
$name = sprintf(__('%1$s (%2$s)'), $name, $objdata["id"]);
}
$link = $itemtype::getFormURLWithID($objdata["id"]);
$name = "<a href=\"" . $link . "\">" . $name . "</a>";
echo "<tr class='tab_bg_1'>";
if ($canedit) {
echo "<td width='10'>";
Html::showMassiveActionCheckBox(__CLASS__, $objdata["IDD"]);
echo "</td>";
}
if ($prem) {
$item = new $itemtype();
$typename = $item->getTypeName($nb);
echo "<td class='center top' rowspan='{$nb}'>" . ($nb > 1 ? sprintf(__('%1$s: %2$s'), $typename, $nb) : $typename) . "</td>";
$prem = false;
}
echo "<td class='center'>";
echo Dropdown::getDropdownName("glpi_entities", $objdata['entity']) . "</td>";
echo "<td class='center" . (isset($objdata['is_deleted']) && $objdata['is_deleted'] ? " tab_bg_2_2'" : "'");
echo ">" . $name . "</td>";
echo "<td class='center'>" . (isset($objdata["serial"]) ? "" . $objdata["serial"] . "" : "-") . "</td>";
echo "<td class='center'>" . (isset($objdata["otherserial"]) ? "" . $objdata["otherserial"] . "" : "-") . "</td>";
echo "<td class='center'>";
if (isset($objdata["states_id"])) {
echo Dropdown::getDropdownName("glpi_states", $objdata['states_id']);
} else {
echo ' ';
}
echo "</td></tr>";
}
}
}
if ($number) {
echo $header_begin . $header_bottom . $header_end;
}
echo "</table>";
if ($canedit && $number) {
$massiveactionparams['ontop'] = false;
Html::showMassiveActions($massiveactionparams);
Html::closeForm();
}
echo "</div>";
}