static function showForDocument(Document $doc)
{
global $DB, $CFG_GLPI;
$instID = $doc->fields['id'];
if (!$doc->can($instID, READ)) {
return false;
}
$canedit = $doc->can($instID, UPDATE);
// for a document,
// don't show here others documents associated to this one,
// it's done for both directions in self::showAssociated
$query = "SELECT DISTINCT `itemtype`\n FROM `glpi_documents_items`\n WHERE `glpi_documents_items`.`documents_id` = '{$instID}'\n AND `glpi_documents_items`.`itemtype` != 'Document'\n ORDER BY `itemtype`";
$result = $DB->query($query);
$number = $DB->numrows($result);
$rand = mt_rand();
if ($canedit) {
echo "<div class='firstbloc'>";
echo "<form name='documentitem_form{$rand}' id='documentitem_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' => Document::getItemtypesThatCanHave(), 'entity_restrict' => $doc->fields['is_recursive'] ? getSonsOf('glpi_entities', $doc->fields['entities_id']) : $doc->fields['entities_id'], 'checkright' => true));
echo "</td><td class='center'>";
echo "<input type='submit' name='add' value=\"" . _sx('button', 'Add') . "\" class='submit'>";
echo "<input type='hidden' name='documents_id' value='{$instID}'>";
echo "</td></tr>";
echo "</table>";
Html::closeForm();
echo "</div>";
}
echo "<div class='spaced'>";
if ($canedit && $number) {
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 && $number) {
$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>" . __('Name') . "</th>";
$header_end .= "<th>" . __('Entity') . "</th>";
$header_end .= "<th>" . __('Serial number') . "</th>";
$header_end .= "<th>" . __('Inventory number') . "</th>";
$header_end .= "</tr>";
echo $header_begin . $header_top . $header_end;
for ($i = 0; $i < $number; $i++) {
$itemtype = $DB->result($result, $i, "itemtype");
if (!($item = getItemForItemtype($itemtype))) {
continue;
}
if ($item->canView()) {
if ($item instanceof CommonDevice) {
$column = "designation";
} else {
if ($item instanceof Item_Devices) {
$column = "itemtype";
} else {
$column = "name";
}
}
if ($itemtype == 'Ticket') {
$column = "id";
}
$itemtable = getTableForItemType($itemtype);
$query = "SELECT `{$itemtable}`.*,\n `glpi_documents_items`.`id` AS IDD, ";
if ($itemtype == 'KnowbaseItem') {
$query .= "-1 AS entity\n FROM `glpi_documents_items`, `{$itemtable}`\n " . KnowbaseItem::addVisibilityJoins() . "\n WHERE `{$itemtable}`.`id` = `glpi_documents_items`.`items_id`\n AND ";
} else {
$query .= "`glpi_entities`.`id` AS entity\n FROM `glpi_documents_items`, `{$itemtable}`";
if ($itemtype != 'Entity') {
$query .= " LEFT JOIN `glpi_entities`\n ON (`glpi_entities`.`id` = `{$itemtable}`.`entities_id`)";
}
$query .= " WHERE `{$itemtable}`.`id` = `glpi_documents_items`.`items_id`\n AND ";
}
$query .= "`glpi_documents_items`.`itemtype` = '{$itemtype}'\n AND `glpi_documents_items`.`documents_id` = '{$instID}' ";
if ($itemtype == 'KnowbaseItem') {
if (Session::getLoginUserID()) {
$where = "AND " . KnowbaseItem::addVisibilityRestrict();
} else {
// Anonymous access
if (Session::isMultiEntitiesMode()) {
$where = " AND (`glpi_entities_knowbaseitems`.`entities_id` = '0'\n AND `glpi_entities_knowbaseitems`.`is_recursive` = '1')";
}
}
} else {
$query .= getEntitiesRestrictRequest(" AND ", $itemtable, '', '', $item->maybeRecursive());
}
if ($item->maybeTemplate()) {
$query .= " AND `{$itemtable}`.`is_template` = '0'";
}
if ($itemtype == 'KnowbaseItem') {
$query .= " ORDER BY `{$itemtable}`.`{$column}`";
} else {
$query .= " ORDER BY `glpi_entities`.`completename`, `{$itemtable}`.`{$column}`";
}
if ($itemtype == 'SoftwareLicense') {
$soft = new Software();
}
if ($result_linked = $DB->query($query)) {
if ($DB->numrows($result_linked)) {
while ($data = $DB->fetch_assoc($result_linked)) {
if ($itemtype == 'Ticket') {
$data["name"] = sprintf(__('%1$s: %2$s'), __('Ticket'), $data["id"]);
}
if ($itemtype == 'SoftwareLicense') {
$soft->getFromDB($data['softwares_id']);
$data["name"] = sprintf(__('%1$s - %2$s'), $data["name"], $soft->fields['name']);
}
if ($item instanceof CommonDevice) {
$linkname = $data["designation"];
} else {
if ($item instanceof Item_Devices) {
$linkname = $data["itemtype"];
} else {
$linkname = $data["name"];
}
}
if ($_SESSION["glpiis_ids_visible"] || empty($data["name"])) {
$linkname = sprintf(__('%1$s (%2$s)'), $linkname, $data["id"]);
}
if ($item instanceof Item_Devices) {
$tmpitem = new $item::$itemtype_2();
if ($tmpitem->getFromDB($data[$item::$items_id_2])) {
$linkname = $tmpitem->getLink();
}
}
$link = $itemtype::getFormURLWithID($data['id']);
$name = "<a href=\"" . $link . "\">" . $linkname . "</a>";
echo "<tr class='tab_bg_1'>";
if ($canedit) {
echo "<td width='10'>";
Html::showMassiveActionCheckBox(__CLASS__, $data["IDD"]);
echo "</td>";
}
echo "<td class='center'>" . $item->getTypeName(1) . "</td>";
echo "<td " . (isset($data['is_deleted']) && $data['is_deleted'] ? "class='tab_bg_2_2'" : "") . ">" . $name . "</td>";
echo "<td class='center'>" . Dropdown::getDropdownName("glpi_entities", $data['entity']);
echo "</td>";
echo "<td class='center'>" . (isset($data["serial"]) ? "" . $data["serial"] . "" : "-") . "</td>";
echo "<td class='center'>" . (isset($data["otherserial"]) ? "" . $data["otherserial"] . "" : "-") . "</td>";
echo "</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>";
}