. * --------------------------------------------------------------------- */ if (!defined('GLPI_ROOT')) { die("Sorry. You can't access this file directly"); } class Domain_Item extends CommonDBRelation { // From CommonDBRelation static public $itemtype_1 = "Domain"; static public $items_id_1 = 'domains_id'; static public $itemtype_2 = 'itemtype'; static public $items_id_2 = 'items_id'; static $rightname = 'domain'; static function cleanForItem(CommonDBTM $item) { $temp = new self(); $temp->deleteByCriteria( ['itemtype' => $item->getType(), 'items_id' => $item->getField('id')] ); } function getTabNameForItem(CommonGLPI $item, $withtemplate = 0) { if (!$withtemplate) { if ($item->getType() == 'Domain' && count(Domain::getTypes(false)) ) { if ($_SESSION['glpishow_count_on_tabs']) { return self::createTabEntry(_n('Associated item', 'Associated items', Session::getPluralNumber()), self::countForDomain($item)); } return _n('Associated item', 'Associated items', Session::getPluralNumber()); } else if ($item->getType()== 'DomainRelation' || in_array($item->getType(), Domain::getTypes(true)) && Session::haveRight('domain', READ) ) { if ($_SESSION['glpishow_count_on_tabs']) { return self::createTabEntry(Domain::getTypeName(Session::getPluralNumber()), self::countForItem($item)); } return Domain::getTypeName(2); } } return ''; } static function displayTabContentForItem(CommonGLPI $item, $tabnum = 1, $withtemplate = 0) { if ($item->getType() == 'Domain') { self::showForDomain($item); } else if (in_array($item->getType(), Domain::getTypes(true)) || $item->getType() == DomainRelation::getType() ) { self::showForItem($item); } return true; } static function countForDomain(Domain $item) { $types = $item->getTypes(); if (count($types) == 0) { return 0; } return countElementsInTable( 'glpi_domains_items', [ "domains_id" => $item->getID(), "itemtype" => $types ] ); } static function countForItem(CommonDBTM $item) { $criteria = []; if ($item instanceof DomainRelation) { $criteria = ['domainrelations_id' => $item->fields['id']]; } else { $criteria = [ 'itemtype' => $item->getType(), 'items_id' => $item->fields['id'] ]; } return countElementsInTable( self::getTable(), $criteria ); } function getFromDBbyDomainsAndItem($domains_id, $items_id, $itemtype) { $criteria = ['domains_id' => $domains_id]; $item = new $itemtype; if ($item instanceof DomainRelation) { $criteria += ['domainrelations_id' => $items_id]; } else { $criteria += [ 'itemtype' => $itemtype, 'items_id' => $items_id ]; } return $this->getFromDBByCrit($criteria); } function addItem($values) { $this->add([ 'domains_id' => $values['domains_id'], 'items_id' => $values['items_id'], 'itemtype' => $values['itemtype'], 'domainrelations_id' => $values['domainrelations_id'] ]); } function deleteItemByDomainsAndItem($domains_id, $items_id, $itemtype) { if ($this->getFromDBbyDomainsAndItem($domains_id, $items_id, $itemtype)) { $this->delete(['id' => $this->fields["id"]]); } } /** * Show items linked to a domain * * @param Domain $domain Domain object * * @return void **/ public static function showForDomain(Domain $domain) { global $DB; $instID = $domain->fields['id']; if (!$domain->can($instID, READ)) { return false; } $canedit = $domain->can($instID, UPDATE); $rand = mt_rand(); $iterator = $DB->request([ 'SELECT' => 'itemtype', 'DISTINCT' => true, 'FROM' => self::getTable(), 'WHERE' => ['domains_id' => $instID], 'ORDER' => 'itemtype', 'LIMIT' => count(Domain::getTypes(true)) ]); $number = count($iterator); if (Session::isMultiEntitiesMode()) { $colsup = 1; } else { $colsup = 0; } if ($canedit) { echo "
"; echo "
"; echo ""; echo ""; echo ""; echo "
" . __('Add an item') . "
"; Dropdown::showSelectItemFromItemtypes(['items_id_name' => 'items_id', 'itemtypes' => Domain::getTypes(true), 'entity_restrict' => ($domain->fields['is_recursive'] ? getSonsOf('glpi_entities', $domain->fields['entities_id']) : $domain->fields['entities_id']), 'checkright' => true, ]); Dropdown::show( 'DomainRelation', [ 'name' => "domainrelations_id", 'value' => DomainRelation::BELONGS ] ); echo ""; echo ""; echo ""; echo "
"; Html::closeForm(); echo "
"; } echo "
"; if ($canedit && $number) { Html::openMassiveActionsForm('mass' . __CLASS__ . $rand); $massiveactionparams = []; Html::showMassiveActions($massiveactionparams); } echo ""; echo ""; if ($canedit && $number) { echo ""; } echo ""; echo ""; if (Session::isMultiEntitiesMode()) { echo ""; } echo ""; echo ""; echo ""; echo ""; while ($data = $iterator->next()) { $itemtype = $data['itemtype']; if (!($item = getItemForItemtype($itemtype))) { continue; } if ($item->canView()) { $itemTable = getTableForItemType($itemtype); $linked_criteria = [ 'SELECT' => [ "$itemTable.*", 'glpi_domains_items.id AS items_id', 'glpi_domains_items.domainrelations_id', 'glpi_entities.id AS entity' ], 'FROM' => self::getTable(), 'INNER JOIN' => [ $itemTable => [ 'ON' => [ $itemTable => 'id', self::getTable() => 'items_id' ] ] ], 'LEFT JOIN' => [ 'glpi_entities' => [ 'ON' => [ 'glpi_entities' => 'id', $itemTable => 'entities_id' ] ] ], 'WHERE' => [ self::getTable() . '.itemtype' => $itemtype, self::getTable() . '.domains_id' => $instID ] + getEntitiesRestrictCriteria($itemTable, '', '', $item->maybeRecursive()) ]; if ($item->maybeTemplate()) { $linked_criteria['WHERE']["$itemTable.is_template"] = 0; } $linked_iterator = $DB->request($linked_criteria); if (count($linked_iterator)) { Session::initNavigateListItems($itemtype, Domain::getTypeName(2) . " = " . $domain->fields['name']); while ($data = $linked_iterator->next()) { $item->getFromDB($data["id"]); Session::addToNavigateListItems($itemtype, $data["id"]); $ID = ""; if ($_SESSION["glpiis_ids_visible"] || empty($data["name"])) { $ID = " (" . $data["id"] . ")"; } $link = Toolbox::getItemTypeFormURL($itemtype); $name = "" . $data["name"] . "$ID"; echo ""; if ($canedit) { echo ""; } echo ""; echo ""; if (Session::isMultiEntitiesMode()) { echo ""; } echo ""; echo ""; echo ""; echo ""; } } } } echo "
" . Html::getCheckAllAsCheckbox('mass' . __CLASS__ . $rand) . "" . _n('Type', 'Types', 1) . "" . __('Name') . "" . Entity::getTypeName(1) . "" . DomainRelation::getTypeName(1) . "" . __('Serial number') . "" . __('Inventory number') . "
"; Html::showMassiveActionCheckBox(__CLASS__, $data["items_id"]); echo "" . $item->getTypeName(1) . "" . $name . "" . Dropdown::getDropdownName("glpi_entities", $data['entity']) . "" . Dropdown::getDropdownName("glpi_domainrelations", $data['domainrelations_id']) . "" . (isset($data["serial"]) ? "" . $data["serial"] . "" : "-") . "" . (isset($data["otherserial"]) ? "" . $data["otherserial"] . "" : "-") . "
"; if ($canedit && $number) { $paramsma['ontop'] = false; Html::showMassiveActions($paramsma); Html::closeForm(); } echo "
"; } /** * Show domains associated to an item * * @param $item CommonDBTM object for which associated domains must be displayed * @param $withtemplate (default '') * * @return bool */ static function showForItem(CommonDBTM $item, $withtemplate = '') { global $DB; $ID = $item->getField('id'); if ($item->isNewID($ID)) { return false; } if (!Session::haveRight('domain', READ)) { return false; } if (!$item->can($item->fields['id'], READ)) { return false; } if (empty($withtemplate)) { $withtemplate = 0; } $canedit = $item->canAddItem('Domain'); $rand = mt_rand(); $is_recursive = $item->isRecursive(); $criteria = [ 'SELECT' => [ 'glpi_domains_items.id AS assocID', 'glpi_domains_items.domainrelations_id', 'glpi_entities.id AS entity', 'glpi_domains.name AS assocName', 'glpi_domains.*' ], 'FROM' => self::getTable(), 'LEFT JOIN' => [ Domain::getTable() => [ 'ON' => [ Domain::getTable() => 'id', self::getTable() => 'domains_id' ] ], Entity::getTable() => [ 'ON' => [ Domain::getTable() => 'entities_id', Entity::getTable() => 'id' ] ] ], 'WHERE' => [],//to be filled 'ORDER' => 'assocName' ]; if ($item instanceof DomainRelation) { $criteria['WHERE'] = ['glpi_domains_items.domainrelations_id' => $ID]; } else { $criteria['WHERE'] = [ 'glpi_domains_items.itemtype' => $item->getType(), 'glpi_domains_items.items_id' => $ID ]; } $criteria['WHERE'] += getEntitiesRestrictCriteria(Domain::getTable(), '', '', true); $iterator = $DB->request($criteria); $number = count($iterator); $i = 0; $domains = []; $domain = new Domain(); $used = []; while ($data = $iterator->next()) { $domains[$data['assocID']] = $data; $used[$data['id']] = $data['id']; } if (!($item instanceof DomainRelation) && $canedit && $withtemplate < 2) { // Restrict entity for knowbase $entities = ""; $entity = $_SESSION["glpiactive_entity"]; if ($item->isEntityAssign()) { /// Case of personal items : entity = -1 : create on active entity (Reminder case)) if ($item->getEntityID() >= 0) { $entity = $item->getEntityID(); } if ($item->isRecursive()) { $entities = getSonsOf('glpi_entities', $entity); } else { $entities = $entity; } } $domain_iterator = $DB->request([ 'COUNT' => 'cpt', 'FROM' => Domain::getTable(), 'WHERE' => ['is_deleted' => 0] + getEntitiesRestrictCriteria(Domain::getTable(), '', $entities, true) ]); $result = $domain_iterator->next(); $nb = $result['cpt']; echo "
"; if (Session::haveRight('domain', READ) && ($nb > count($used)) ) { echo ""; echo ""; echo ""; echo ""; echo ""; echo "
"; echo ""; echo ""; echo ""; echo ""; if ($item->getType() == 'Ticket') { echo ""; } Dropdown::show( 'DomainRelation', [ 'name' => "domainrelations_id", 'value' => DomainRelation::BELONGS, 'display_emptychoice' => false ] ); Domain::dropdownDomains([ 'entity' => $entities, 'used' => $used ]); echo ""; echo ""; echo "
"; Html::closeForm(); } echo "
"; } echo "
"; if ($canedit && $number && ($withtemplate < 2)) { Html::openMassiveActionsForm('mass' . __CLASS__ . $rand); $massiveactionparams = ['num_displayed' => $number]; Html::showMassiveActions($massiveactionparams); } echo ""; echo ""; if ($canedit && $number && ($withtemplate < 2)) { echo ""; } echo ""; if (Session::isMultiEntitiesMode()) { echo ""; } echo ""; echo ""; echo ""; if (!$item instanceof DomainRelation) { echo ""; } echo ""; echo ""; echo ""; $used = []; if ($number) { Session::initNavigateListItems('Domain', //TRANS : %1$s is the itemtype name, // %2$s is the name of the item (used for headings of a list) sprintf(__('%1$s = %2$s'), $item->getTypeName(1), $item->getName())); foreach ($domains as $data) { $domainID = $data["id"]; $link = NOT_AVAILABLE; if ($domain->getFromDB($domainID)) { $link = $domain->getLink(); } Session::addToNavigateListItems('Domain', $domainID); $used[$domainID] = $domainID; echo ""; if ($canedit && ($withtemplate < 2)) { echo ""; } echo ""; if (Session::isMultiEntitiesMode()) { echo ""; } echo ""; echo ""; echo ""; if (!$item instanceof DomainRelation) { echo ""; } echo ""; if ($data["date_expiration"] <= date('Y-m-d') && !empty($data["date_expiration"]) ) { echo ""; } else if (empty($data["date_expiration"])) { echo ""; } else { echo ""; } echo ""; $i++; } } echo "
" . Html::getCheckAllAsCheckbox('mass' . __CLASS__ . $rand) . "" . __('Name') . "" . Entity::getTypeName(1) . "" . __('Group in charge') . "" . __('Technician in charge') . "" . _n('Type', 'Types', 1) . "" . DomainRelation::getTypeName(1) . "" . __('Creation date') . "" . __('Expiration date') . "
"; Html::showMassiveActionCheckBox(__CLASS__, $data["assocID"]); echo "$link" . Dropdown::getDropdownName("glpi_entities", $data['entities_id']) . "" . Dropdown::getDropdownName("glpi_groups", $data["groups_id_tech"]) . "" . getUserName($data["users_id_tech"]) . "" . Dropdown::getDropdownName("glpi_domaintypes", $data["domaintypes_id"]) . "" . Dropdown::getDropdownName("glpi_domainrelations", $data["domainrelations_id"]) . "" . Html::convDate($data["date_creation"]) . "
" . Html::convDate($data["date_expiration"]) . "
" . __('Does not expire') . "" . Html::convDate($data["date_expiration"]) . "
"; if ($canedit && $number && ($withtemplate < 2)) { $massiveactionparams['ontop'] = false; Html::showMassiveActions($massiveactionparams); Html::closeForm(); } echo "
"; } function rawSearchOptions() { $tab = []; $tab[] = [ 'id' => '2', 'table' => DomainRelation::getTable(), 'field' => 'name', 'name' => DomainRelation::getTypeName(), 'datatype' => 'itemlink', 'itemlink_type' => $this->getType(), ]; return $tab; } }