. * --------------------------------------------------------------------- */ if (!defined('GLPI_ROOT')) { die("Sorry. You can't access this file directly"); } /** * Class to link a certificate to an item */ class Certificate_Item extends CommonDBRelation { // From CommonDBRelation static public $itemtype_1 = "Certificate"; static public $items_id_1 = 'certificates_id'; static public $take_entity_1 = false; static public $itemtype_2 = 'itemtype'; static public $items_id_2 = 'items_id'; static public $take_entity_2 = true; /** * @since 9.2 * **/ function getForbiddenStandardMassiveAction() { $forbidden = parent::getForbiddenStandardMassiveAction(); $forbidden[] = 'update'; return $forbidden; } /** * @param CommonDBTM $item */ static function cleanForItem(CommonDBTM $item) { $temp = new self(); $temp->deleteByCriteria(['itemtype' => $item->getType(), 'items_id' => $item->getField('id')]); } /** * @param CommonGLPI $item * @param int $withtemplate * @return string */ function getTabNameForItem(CommonGLPI $item, $withtemplate = 0) { if (!$withtemplate) { if ($item->getType() == 'Certificate' && count(Certificate::getTypes(false))) { if ($_SESSION['glpishow_count_on_tabs']) { return self::createTabEntry(_n('Associated item', 'Associated items', Session::getPluralNumber()), self::countForMainItem($item)); } return _n('Associated item', 'Associated items', Session::getPluralNumber()); } else if (in_array($item->getType(), Certificate::getTypes(true)) && Certificate::canView()) { if ($_SESSION['glpishow_count_on_tabs']) { return self::createTabEntry(Certificate::getTypeName(2), self::countForItem($item)); } return Certificate::getTypeName(2); } } return ''; } /** * @param CommonGLPI $item * @param int $tabnum * @param int $withtemplate * @return bool */ static function displayTabContentForItem(CommonGLPI $item, $tabnum = 1, $withtemplate = 0) { if ($item->getType() == 'Certificate') { self::showForCertificate($item); } else if (in_array($item->getType(), Certificate::getTypes(true))) { self::showForItem($item); } return true; } /** * @param $certificates_id * @param $items_id * @param $itemtype * @return bool */ function getFromDBbyCertificatesAndItem($certificates_id, $items_id, $itemtype) { $certificate = new self(); $certificates = $certificate->find([ 'certificates_id' => $certificates_id, 'itemtype' => $itemtype, 'items_id' => $items_id ]); if (count($certificates) != 1) { return false; } $cert = current($certificates); $this->fields = $cert; return true; } /** * Link a certificate to an item * * @since 9.2 * @param $values */ function addItem($values) { $this->add(['certificates_id' => $values["certificates_id"], 'items_id' => $values["items_id"], 'itemtype' => $values["itemtype"]]); } /** * Delete a certificate link to an item * * @since 9.2 * * @param integer $certificates_id the certificate ID * @param integer $items_id the item's id * @param string $itemtype the itemtype */ function deleteItemByCertificatesAndItem($certificates_id, $items_id, $itemtype) { if ($this->getFromDBbyCertificatesAndItem($certificates_id, $items_id, $itemtype)) { $this->delete(['id' => $this->fields["id"]]); } } /** * Show items linked to a certificate * * @since 9.2 * * @param $certificate Certificate object * * @return void (HTML display) **/ public static function showForCertificate(Certificate $certificate) { $instID = $certificate->fields['id']; if (!$certificate->can($instID, READ)) { return false; } $canedit = $certificate->can($instID, UPDATE); $rand = mt_rand(); $types_iterator = self::getDistinctTypes($instID, ['itemtype' => Certificate::getTypes(true)]); $number = count($types_iterator); if (Session::isMultiEntitiesMode()) { $colsup = 1; } else { $colsup = 0; } if ($canedit) { echo "
"; echo "
"; echo ""; echo ""; echo ""; echo ""; echo "
" . __('Add an item') . "
"; Dropdown::showSelectItemFromItemtypes( ['items_id_name' => 'items_id', 'itemtypes' => Certificate::getTypes(true), 'entity_restrict' => ($certificate->fields['is_recursive'] ? getSonsOf('glpi_entities', $certificate->fields['entities_id']) : $certificate->fields['entities_id']), 'checkright' => true, ]); echo ""; echo Html::hidden('certificates_id', ['value' => $instID]); echo Html::submit(_x('button', 'Add'), ['name' => 'add']); 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 ""; while ($type_row = $types_iterator->next()) { $itemtype = $type_row['itemtype']; if (!($item = getItemForItemtype($itemtype))) { continue; } if ($item->canView()) { $iterator = self::getTypeItems($instID, $itemtype); if (count($iterator)) { Session::initNavigateListItems($itemtype, Certificate::getTypeName(2) . " = " . $certificate->fields['name']); while ($data = $iterator->next()) { $item->getFromDB($data["id"]); Session::addToNavigateListItems($itemtype, $data["id"]); $ID = ""; if ($_SESSION["glpiis_ids_visible"] || empty($data["name"])) { $ID = " (" . $data["id"] . ")"; } $link = $itemtype::getFormURLWithID($data["id"]); $name = "" . $data["name"] . "$ID"; echo ""; if ($canedit) { echo ""; } echo ""; echo ""; if (Session::isMultiEntitiesMode()) { $entity = ($item->isEntityAssign() ? Dropdown::getDropdownName("glpi_entities", $data['entity']) : '-'); echo ""; } echo ""; echo ""; echo ""; } } } } echo "
" . Html::getCheckAllAsCheckbox('mass' . __CLASS__ . $rand) . "" . _n('Type', 'Types', 1) . "" . __('Name') . "" . Entity::getTypeName(1) . "" . __('Serial number') . "" . __('Inventory number') . "
"; Html::showMassiveActionCheckBox(__CLASS__, $data["linkid"]); echo "" . $item->getTypeName(1) . "" . $name . "" . $entity . "" . (isset($data["serial"]) ? "" . $data["serial"] . "" : "-") . "" . (isset($data["otherserial"]) ? "" . $data["otherserial"] . "" : "-") . "
"; if ($canedit && $number) { $paramsma = [ 'ontop' => false, ]; Html::showMassiveActions($paramsma); Html::closeForm(); } echo "
"; } /** * Show certificates associated to an item * * @since 9.2 * * @param $item CommonDBTM object for which associated certificates must be displayed * @param $withtemplate (default 0) * * @return bool */ static function showForItem(CommonDBTM $item, $withtemplate = 0) { $ID = $item->getField('id'); if ($item->isNewID($ID) || !Certificate::canView() || !$item->can($item->fields['id'], READ)) { return false; } $certificate = new Certificate(); if (empty($withtemplate)) { $withtemplate = 0; } $canedit = $item->canAddItem('Certificate'); $rand = mt_rand(); $is_recursive = $item->isRecursive(); $iterator = self::getListForItem($item); $number = $iterator->numrows(); $i = 0; $certificates = []; $used = []; foreach ($iterator as $data) { $certificates[$data['linkid']] = $data; $used[$data['id']] = $data['id']; } if ($canedit && $withtemplate < 2) { if ($item->maybeRecursive()) { $is_recursive = $item->fields['is_recursive']; } else { $is_recursive = false; } $entity_restrict = getEntitiesRestrictCriteria("glpi_certificates", 'entities_id', $item->fields['entities_id'], $is_recursive); $nb = countElementsInTable('glpi_certificates', [ 'is_deleted' => 0 ] + $entity_restrict); echo "
"; if (Certificate::canView() && (!$nb || ($nb > count($used)))) { echo ""; echo ""; echo ""; echo ""; echo ""; echo "
"; echo Html::hidden('entities_id', ['value' => $item->fields['entities_id']]); echo Html::hidden('is_recursive', ['value' => $is_recursive]); echo Html::hidden('itemtype', ['value' => $item->getType()]); echo Html::hidden('items_id', ['value' => $ID]); if ($item->getType() == 'Ticket') { echo Html::hidden('tickets_id', ['value' => $ID]); } Dropdown::show('Certificate', ['entity' => $item->fields['entities_id'], 'is_recursive' => $is_recursive, 'used' => $used ]); echo ""; echo Html::submit(_sx('button', 'Associate'), ['name' => 'add']); echo "
"; Html::closeForm(); } echo "
"; } echo "
"; if ($canedit && $number && ($withtemplate < 2)) { $massiveactionparams = ['num_displayed' => $number]; Html::openMassiveActionsForm('mass' . __CLASS__ . $rand); Html::showMassiveActions($massiveactionparams); } echo ""; echo ""; if ($canedit && $number && ($withtemplate < 2)) { echo ""; } echo ""; if (Session::isMultiEntitiesMode()) { echo ""; } echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; $used = []; if ($number) { Session::initNavigateListItems('Certificate', sprintf(__('%1$s = %2$s'), $item->getTypeName(1), $item->getName())); foreach ($certificates as $data) { $certificateID = $data["id"]; $link = NOT_AVAILABLE; if ($certificate->getFromDB($certificateID)) { $link = $certificate->getLink(); } Session::addToNavigateListItems('Certificate', $certificateID); $used[$certificateID] = $certificateID; echo ""; if ($canedit && ($withtemplate < 2)) { echo ""; } echo ""; if (Session::isMultiEntitiesMode()) { echo ""; } echo ""; echo ""; 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 ""; echo ""; $i++; } } echo "
"; echo Html::getCheckAllAsCheckbox('mass' . __CLASS__ . $rand); echo "" . __('Name') . "" . Entity::getTypeName(1) . "" . _n('Type', 'Types', 1) . "" . __('DNS name') . "" . __('DNS suffix') . "" . __('Creation date') . "" . __('Expiration date') . "" . __('Status') . "
"; Html::showMassiveActionCheckBox(__CLASS__, $data["linkid"]); echo "$link" . Dropdown::getDropdownName("glpi_entities", $data['entities_id']) . ""; echo Dropdown::getDropdownName("glpi_certificatetypes", $data["certificatetypes_id"]); echo "" . $data["dns_name"] . "" . $data["dns_suffix"] . "" . Html::convDate($data["date_creation"]) . ""; echo "
" . Html::convDate($data["date_expiration"]) . "
"; echo "
" . __('Does not expire') . "" . Html::convDate($data["date_expiration"]) . ""; echo Dropdown::getDropdownName("glpi_states", $data["states_id"]); echo "
"; if ($canedit && $number && ($withtemplate < 2)) { $massiveactionparams['ontop'] = false; Html::showMassiveActions($massiveactionparams); Html::closeForm(); } echo "
"; } }