. * --------------------------------------------------------------------- **/ class Item_Cluster extends CommonDBRelation { static public $itemtype_1 = 'Cluster'; static public $items_id_1 = 'clusters_id'; static public $itemtype_2 = 'itemtype'; static public $items_id_2 = 'items_id'; static public $checkItem_1_Rights = self::DONT_CHECK_ITEM_RIGHTS; static public $mustBeAttached_1 = false; static public $mustBeAttached_2 = false; static function getTypeName($nb = 0) { return _n('Item', 'Item', $nb); } function getTabNameForItem(CommonGLPI $item, $withtemplate = 0) { $nb = 0; if ($_SESSION['glpishow_count_on_tabs']) { $nb = self::countForMainItem($item); } return self::createTabEntry(self::getTypeName(Session::getPluralNumber()), $nb); } static function displayTabContentForItem(CommonGLPI $item, $tabnum = 1, $withtemplate = 0) { self::showItems($item, $withtemplate); } function getForbiddenStandardMassiveAction() { $forbidden = parent::getForbiddenStandardMassiveAction(); $forbidden[] = 'MassiveAction:update'; $forbidden[] = 'CommonDBConnexity:affect'; $forbidden[] = 'CommonDBConnexity:unaffect'; return $forbidden; } /** * Print enclosure items * * @return void **/ static function showItems(Cluster $cluster) { global $DB; $ID = $cluster->fields['id']; $rand = mt_rand(); if (!$cluster->getFromDB($ID) || !$cluster->can($ID, READ)) { return false; } $canedit = $cluster->canEdit($ID); $items = $DB->request([ 'FROM' => self::getTable(), 'WHERE' => [ 'clusters_id' => $ID ] ]); Session::initNavigateListItems( self::getType(), //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'), $cluster->getTypeName(1), $cluster->getName() ) ); if ($cluster->canAddItem('itemtype')) { echo "
"; Html::showSimpleForm( self::getFormURL(), '_add_fromitem', __('Add new item to this cluster...'), [ 'cluster' => $ID, 'position' => 1 ] ); echo "
"; } $items = iterator_to_array($items); if (!count($items)) { echo ""; echo "
".__('No item found')."
"; } else { if ($canedit) { Html::openMassiveActionsForm('mass'.__CLASS__.$rand); $massiveactionparams = [ 'num_displayed' => min($_SESSION['glpilist_limit'], count($items)), 'container' => 'mass'.__CLASS__.$rand ]; Html::showMassiveActions($massiveactionparams); } echo ""; $header = ""; if ($canedit) { $header .= ""; } $header .= ""; $header .= ""; echo $header; foreach ($items as $row) { $item = new $row['itemtype']; $item->getFromDB($row['items_id']); echo ""; if ($canedit) { echo ""; } echo ""; echo ""; } echo $header; echo "
"; $header .= Html::getCheckAllAsCheckbox('mass'.__CLASS__.$rand); $header .= ""._n('Item', 'Items', 1)."
"; Html::showMassiveActionCheckBox(__CLASS__, $row["id"]); echo "" . $item->getLink() . "
"; if ($canedit && count($items)) { $massiveactionparams['ontop'] = false; Html::showMassiveActions($massiveactionparams); } if ($canedit) { Html::closeForm(); } } } function showForm($ID, $options = []) { global $DB, $CFG_GLPI; echo "
"; $this->initForm($ID, $options); $this->showFormHeader(); $cluster = new Cluster(); $cluster->getFromDB($this->fields['clusters_id']); $rand = mt_rand(); echo ""; echo ""; echo ""; $types = $CFG_GLPI['cluster_types']; $translated_types = []; foreach ($types as $type) { $translated_types[$type] = $type::getTypeName(1); } Dropdown::showFromArray( 'itemtype', $translated_types, [ 'display_emptychoice' => true, 'value' => $this->fields["itemtype"], 'rand' => $rand ] ); //get all used items $used = []; $iterator = $DB->request([ 'FROM' => $this->getTable() ]); while ($row = $iterator->next()) { $used [$row['itemtype']][] = $row['items_id']; } Ajax::updateItemOnSelectEvent( "dropdown_itemtype$rand", "items_id", $CFG_GLPI["root_doc"]."/ajax/dropdownAllItems.php", [ 'idtable' => '__VALUE__', 'name' => 'items_id', 'value' => $this->fields['items_id'], 'rand' => $rand, 'used' => $used ] ); echo ""; echo ""; echo ""; if (isset($this->fields['itemtype']) && !empty($this->fields['itemtype'])) { $itemtype = $this->fields['itemtype']; $itemtype = new $itemtype(); $itemtype::dropdown([ 'name' => "items_id", 'value' => $this->fields['items_id'], 'rand' => $rand ]); } else { Dropdown::showFromArray( 'items_id', [], [ 'display_emptychoice' => true, 'rand' => $rand ] ); } echo ""; echo ""; echo ""; echo ""; echo ""; Cluster::dropdown(['value' => $this->fields["clusters_id"], 'rand' => $rand]); echo ""; echo ""; $this->showFormButtons($options); } function prepareInputForAdd($input) { return $this->prepareInput($input); } function prepareInputForUpdate($input) { return $this->prepareInput($input); } /** * Prepares input (for update and add) * * @param array $input Input data * * @return array */ private function prepareInput($input) { $error_detected = []; //check for requirements if (($this->isNewItem() && (!isset($input['itemtype']) || empty($input['itemtype']))) || (isset($input['itemtype']) && empty($input['itemtype']))) { $error_detected[] = __('An item type is required'); } if (($this->isNewItem() && (!isset($input['items_id']) || empty($input['items_id']))) || (isset($input['items_id']) && empty($input['items_id']))) { $error_detected[] = __('An item is required'); } if (($this->isNewItem() && (!isset($input['clusters_id']) || empty($input['clusters_id']))) || (isset($input['clusters_id']) && empty($input['clusters_id']))) { $error_detected[] = __('A cluster is required'); } if (count($error_detected)) { foreach ($error_detected as $error) { Session::addMessageAfterRedirect( $error, true, ERROR ); } return false; } return $input; } }