diff --git a/ajax/dropdownByItemtype.php b/ajax/dropdownByItemtype.php new file mode 100644 index 00000000..690f6639 --- /dev/null +++ b/ajax/dropdownByItemtype.php @@ -0,0 +1,13 @@ +getFromDB(1); //useless ? + $coluimn = str_replace('glpi_', '', $mainObject->table.'_id'); + + $nameMainObject = $nameMainObject.'_item'; + $nameObjectToAdd = $nameObjectToAdd.'_item'; + + $mainObjectItem = new $nameMainObject(); + $mainObjectToAddItem = new $nameObjectToAdd(); + + //id de l'objet rajouté + $objectToAdd->getFromDB($IdToAdd); + $idObjectToAdd = $objectToAdd->fields['id']; + + // Probably SQL injection + $res = $mainObjectItem->getFromDBByCrit([ + 'items_id' => $idObjectToAdd, + $coluimn => $idMainObject, + 'itemtype' => $_POST['objectToAdd']]); + if ($res) { + Session::addMessageAfterRedirect(__('This Object is already link', 'genericobject'), + true, ERROR, true); + } else { + $values = []; + $values['items_id'] = $idObjectToAdd; + $values[$coluimn] = $idMainObject; + $values['itemtype'] = $_POST['objectToAdd']; + + $mainObjectItem->add($values); + + $values = []; + $values['items_id'] = $idMainObject; + $values[$coluimn1] = $idObjectToAdd; + $values['itemtype'] = $_POST['mainobject']; + + $mainObjectToAddItem->add($values); + } +} + +Html::back(); diff --git a/front/type.form.php b/front/type.form.php index bfd208c6..e84bcffd 100644 --- a/front/type.form.php +++ b/front/type.form.php @@ -52,6 +52,8 @@ } else if (isset ($_POST["update"])) { //Update an existing itemtype if (isset($_POST['itemtypes']) && is_array($_POST['itemtypes'])) { + // Remove [""] if no values ([""] => []) + $_POST['itemtypes'] = array_filter($_POST['itemtypes']); $_POST['linked_itemtypes'] = json_encode($_POST['itemtypes']); } $type->update($_POST); diff --git a/inc/object.class.php b/inc/object.class.php index e1f088d9..4512d2dc 100644 --- a/inc/object.class.php +++ b/inc/object.class.php @@ -32,6 +32,10 @@ class PluginGenericobjectObject extends CommonDBTM { //Internal field counter private $cpt = 0; + function accesObjectType() { + return $this->objecttype; + } + //Get itemtype name static function getTypeName($nb = 0) { global $LANG; @@ -228,10 +232,6 @@ static function registerType() { if (!in_array($class, $GO_LINKED_TYPES)) { array_push($GO_LINKED_TYPES, $class); } - $items_class = $class."_Item"; - //if (class_exists($items_class)) { - $items_class::registerType(); - //} } if ($item->canUseProjects()) { @@ -372,6 +372,10 @@ function defineTabs($options = []) { $this->addStandardTab('Reservation', $tabs, $options); } + if ($this->canUseDirectConnections()) { + $this->addStandardTab($this->getType()."_Item", $tabs, $options); + } + if ($this->canUseHistory()) { $this->addStandardTab('Log', $tabs, $options); } diff --git a/inc/object_item.class.php b/inc/object_item.class.php index 204676bf..79e878cd 100644 --- a/inc/object_item.class.php +++ b/inc/object_item.class.php @@ -25,7 +25,7 @@ @since 2009 ---------------------------------------------------------------------- */ -class PluginGenericobjectObject_Item extends CommonDBChild { +class PluginGenericobjectObject_Item extends CommonDBRelation { public $dohistory = true; @@ -35,6 +35,7 @@ class PluginGenericobjectObject_Item extends CommonDBChild { static public $itemtype_2 = 'itemtype'; static public $items_id_2 = 'items_id'; + static public $logs_for_item_2 = false; //Get itemtype name static function getTypeName($nb = 0) { @@ -62,6 +63,64 @@ static function canCreate() { return Session::haveRight(self::$itemtype_1, CREATE); } + static function canPurge() { + //Note : can be add a right + return true; + } + + static function canDelete() { + //useless + //Note : can be add a right + return true; + } + + function post_purgeItem() { + global $DB; + // Delete the other genericobject link + $obj_itemtype = $this->fields['itemtype'].'_Item'; + $obj_item = new $obj_itemtype(); + $itemtype = $this->fields['itemtype']; + $obj = new $itemtype(); + $column = str_replace('glpi_', '', $obj->table.'_id'); + $obj_item->deleteByCriteria([ + 'items_id' => $this->fields[static::$items_id_1], + $column => $this->fields['items_id'], + 'itemtype' => static::$itemtype_1] + ); + parent::post_purgeItem(); + } + + static function getSpecificValueToDisplay($field, $values, array $options = []) { + if (!is_array($values)) { + $values = [$field => $values]; + } + switch ($field) { + // Column "Linked objects" : Display name of the object and type of object + case 'id' : + $itemtype = get_called_class(); + $objectItem = new $itemtype(); + $objectItem->getFromDB($values['id']); + $namelinkedObject = $objectItem->fields['itemtype']; + $oobjectLinked = new $namelinkedObject(); + $oobjectLinked->getFromDB($objectItem->fields['items_id']); + return $oobjectLinked->getLink()." - (".$oobjectLinked->getTypeName().")"; + } + } + + public static function getDropdownItemLinked($object, $itemType, $id) { + $obj = new $itemType(); + $nameMainObjectItem = $itemType."_Item"; + $mainObjectItem = new $nameMainObjectItem(); + $column = str_replace('glpi_', '', $obj->table."_id"); + $listeId = []; + foreach ($mainObjectItem->find() as $record) { + if ($record[$column] == $id) { + $listeId[] = $record['items_id']; + } + } + $object->dropdown(['used' => $listeId]); + } + /** * * Enter description here ... @@ -82,6 +141,136 @@ static function showItemsForTarget(CommonDBTM $item) { } + static function showItems(CommonDBTM $item) { + global $DB, $CFG_GLPI; + + $ID = $item->getID(); + if (!$item->can($ID, READ)) { + return false; + } + $canedit = $item->canEdit($ID); + $rand = mt_rand(); + + $datas = []; + $used = []; + foreach ($item->getLinkedItemTypesAsArray() as $itemtype) { + $object = new $itemtype(); + if ($object->canView()) { + $iterator = self::getTypeItems($ID, $itemtype); + + while ($data = $iterator->next()) { + $data['assoc_itemtype'] = $itemtype; + $datas[] = $data; + $used[$itemtype][] = $data['id']; + } + } + } + $number = count($datas); + + if ($canedit) { + echo "
"; + echo "
"; + echo "
"; + echo ""; + echo ""; + echo ""; + echo ""; + echo "
".__("Select an object to link", 'genericobject')."  "; + + $options = []; + $options['checkright'] = true; + $options['name'] = 'objectToAdd'; + + $rand = Dropdown::showItemType($item->getLinkedItemTypesAsArray(), $options); + if ($rand) { + $paramsselsoft = ['objectToAdd' => '__VALUE__', + 'idMainobject' => $ID, + 'mainobject' => $item->getType()]; + Ajax::updateItemOnSelectEvent("dropdown_objectToAdd$rand", "show_".$rand, + $CFG_GLPI["root_doc"]."/plugins/genericobject/ajax/dropdownByItemtype.php", + $paramsselsoft); + echo " "; + } + + echo ""; + echo ""; + echo ""; + echo ""; + echo "
"; + echo "
"; + Html::closeForm(); + echo "
"; + } + + if ($number) { + echo "
"; + if ($canedit) { + Html::openMassiveActionsForm('mass'.$item->getType().'_Item'.$rand); + $massiveactionparams + = ['num_displayed' + => min($_SESSION['glpilist_limit'], $number), + 'specific_actions' + => ['purge' => _x('button', 'Disconnect')], + 'container' + => 'mass'.$item->getType().'_Item'.$rand]; + + Html::showMassiveActions($massiveactionparams); + } + echo ""; + $header_begin = ""; + $header_top = ''; + $header_bottom = ''; + $header_end = ''; + + if ($canedit) { + $header_top .= ""; + $header_bottom .= ""; + } + + $header_end .= ""; + $header_end .= ""; + $header_end .= ""; + echo $header_begin.$header_top.$header_end; + + foreach ($datas as $data) { + $linkname = $data["name"]; + $itemtype = $data['assoc_itemtype']; + if ($_SESSION["glpiis_ids_visible"] || empty($data["name"])) { + $linkname = sprintf(__('%1$s (%2$s)'), $linkname, $data["id"]); + } + $link = $itemtype::getFormURLWithID($data["id"]); + $name = "".$linkname.""; + + echo ""; + + if ($canedit) { + echo ""; + } + + echo ""; + echo ""; + echo ""; + echo ""; + } + + echo $header_begin.$header_bottom.$header_end; + echo "
".Html::getCheckAllAsCheckbox('mass'.$item->getType().'_Item'.$rand); + $header_top .= "".Html::getCheckAllAsCheckbox('mass'.$item->getType().'_Item'.$rand); + $header_bottom .= "".__('Type')."".__('ID')."".__('Name')."
"; + Html::showMassiveActionCheckBox($item->getType().'_Item', $data["linkid"]); + echo "".$itemtype::getTypeName(1)."".$data["id"]."$name
"; + if ($canedit) { + $massiveactionparams['ontop'] = false; + Html::showMassiveActions($massiveactionparams); + Html::closeForm(); + } + echo "
"; + } + return true; + } + /** * * Enter description here ... @@ -104,20 +293,22 @@ static function getItemType1() { function getTabNameForItem(CommonGLPI $item, $withtemplate = 0) { if (!$withtemplate) { - $itemtypes = self::getLinkedItemTypes(); + $itemtypes = self::getLinkedItemTypes(get_class($item)); if (in_array(get_class($item), $itemtypes) || get_class($item) == self::getItemType1()) { - return [1 => __("Objects management", "genericobject")]; + //return [1 => __("Objects management", "genericobject")]; + $coluimn1 = str_replace('glpi_', '', $item->table.'_id'); + $nb = countElementsInTable(getTableForItemType($item->getType().'_Item'), + ["$coluimn1" => $item->getID()]); + $str = _n("Linked object", "Linked objects", $nb == 0 ? 1 : $nb, "genericobject"); + return [1 => self::createTabEntry($str, $nb)]; } } return ''; } static function displayTabContentForItem(CommonGLPI $item, $tabnum = 1, $withtemplate = 0) { - $itemtypes = self::getLinkedItemTypes(); - if (get_class($item) == self::getItemType1()) { - self::showItemsForSource($item); - } else if (in_array(get_class($item), $itemtypes)) { - self::showItemsForTarget($item); + if ($tabnum == 1) { + self::showItems($item); } return true; } diff --git a/inc/type.class.php b/inc/type.class.php index b940fe70..15aff21a 100644 --- a/inc/type.class.php +++ b/inc/type.class.php @@ -113,7 +113,8 @@ function getTabNameForItem(CommonGLPI $item, $withtemplate = 0) { 5 => __("Preview") ]; if ($item->canUseDirectConnections()) { - $tabs[7] = __("Associated element"); + $nb = count($item->getLinkedItemTypesAsArray()); + $tabs[7] = self::createTabEntry(_n("Associated element", "Associated elements", Session::getPluralNumber()), $nb); } return $tabs; } @@ -140,6 +141,11 @@ static function displayTabContentForItem(CommonGLPI $item, $tabnum = 1, $withtem case 6: PluginGenericobjectProfile::showForItemtype($item); break; + case 7: + if ($item->canUseDirectConnections()) { + $item->showLinkedTypesForm(); + } + break; } } return true; @@ -395,6 +401,14 @@ function rawSearchOptions() { 'massiveaction' => false, ]; + $sopt[] = [ + 'id' => 22, + 'table' => $this->getTable(), + 'field' => 'use_direct_connections', + 'name' => _sx('button', 'Use') . ' ' . __('Link to other objects', 'genericobject'), + 'datatype' => 'bool', + ]; + $sopt[] = [ 'id' => 121, 'table' => $this->getTable(), @@ -538,6 +552,7 @@ function showBehaviorForm($ID, $options = []) { "use_global_search" => __("Global search"), "use_projects" => _n("Project", "Projects", 2), "use_network_ports" => __("Network connections", "genericobject"), + "use_direct_connections" => __("Link to other objects", "genericobject"), ]; $plugins = [ @@ -725,8 +740,9 @@ function showLinkedTypesForm() { echo ""; echo "".__("Link to other objects", "genericobject").""; echo ""; - echo ""._n("Type", "Types", 2).""; + echo ""; echo ""; + /* echo ""; + echo "";*/ + $selected = $this->getLinkedItemTypesAsArray(); + $values = []; + $elements = []; + foreach ($GO_LINKED_TYPES as $itemtype) { + if ($itemtype == $this->fields['itemtype']) { + continue; + } + if (in_array($itemtype, $selected)) { + $values[] = $itemtype; + } + $elements[$itemtype] = $itemtype::getTypeName(); + } + echo ""; + Dropdown::showFromArray('itemtypes', $elements, ['multiple' => true, + 'values' => $values]); echo ""; echo ""; $this->showFormButtons(['candel' => false, 'canadd' => false]); @@ -917,7 +948,7 @@ function checkNecessaryFieldsUpdate() { if ($this->canUseDirectConnections()) { self::addItemsTable($itemtype); - //self::addItemClassFile($this->fields['name'], $itemtype); + self::addItemClassFile($this->fields['name'], $itemtype); } else { self::deleteItemsTable($itemtype); self::deleteClassFile($this->fields['name']."_item"); diff --git a/locales/genericobject.pot b/locales/genericobject.pot index bde74fa9..6c62b202 100644 --- a/locales/genericobject.pot +++ b/locales/genericobject.pot @@ -146,6 +146,11 @@ msgstr "" msgid "Link to other objects" msgstr "" +msgid "Linked object" +msgstr "Linked objects" +msgstr[0] "" +msgstr[1] "" + #. TRANS: %1$s is itemtype name #: inc/type.class.php:2039 #, php-format