From fd72f9a960a82fdb097aed7d12288ec63be5a085 Mon Sep 17 00:00:00 2001 From: drTr0jan Date: Wed, 19 Feb 2020 00:13:30 +1000 Subject: [PATCH 1/7] Add genericobject inter-links --- ajax/dropdownByItemtype.php | 26 ++++++ front/object_item.form.php | 132 +++++++++++++++++++++++++++ front/type.form.php | 2 + inc/object.class.php | 12 ++- inc/object_item.class.php | 174 +++++++++++++++++++++++++++++++++++- inc/type.class.php | 40 +++++++-- locales/genericobject.pot | 5 ++ 7 files changed, 380 insertions(+), 11 deletions(-) create mode 100644 ajax/dropdownByItemtype.php create mode 100644 front/object_item.form.php diff --git a/ajax/dropdownByItemtype.php b/ajax/dropdownByItemtype.php new file mode 100644 index 00000000..1d0c406d --- /dev/null +++ b/ajax/dropdownByItemtype.php @@ -0,0 +1,26 @@ +table.'_id'); + +if (! isset($_POST[$coluimn1]) || empty($_POST[$coluimn1])) { + Session::addMessageAfterRedirect(__('Error'), true, ERROR, true); +} else { + + $IdToAdd = $_POST[$coluimn1]; + + $mainObject = new $nameMainObject; + $mainObject->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(array( + 'items_id' => $idObjectToAdd, + $coluimn => $idMainObject, + 'itemtype' => $_POST['objectToAdd'])); + if ($res) { + Session::addMessageAfterRedirect(__('This Object is already link','genericobject'), + true, ERROR, true); + } else { + $values = array(); + $values['items_id'] = $idObjectToAdd; + $values[$coluimn] = $idMainObject; + $values['itemtype'] = $_POST['objectToAdd']; + + $mainObjectItem->fields = $values; + $mainObjectItem->addToDB(); + + $values = array(); + $values['items_id'] = $idMainObject; + $values[$coluimn1] = $idObjectToAdd; + $values['itemtype'] = $_POST['mainobject']; + + $mainObjectToAddItem->fields = $values; + $mainObjectToAddItem->addToDB($values); + } +} + +Html::back(); +table.'_id'); + +if (! isset($_POST[$coluimn1]) || empty($_POST[$coluimn1])) { + Session::addMessageAfterRedirect(__('Error'), true, ERROR, true); +} else { + + $IdToAdd = $_POST[$coluimn1]; + + $mainObject = new $nameMainObject; + $mainObject->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(array( + 'items_id' => $idObjectToAdd, + $coluimn => $idMainObject, + 'itemtype' => $_POST['objectToAdd'])); + if ($res) { + Session::addMessageAfterRedirect(__('This Object is already link','genericobject'), + true, ERROR, true); + } else { + $values = array(); + $values['items_id'] = $idObjectToAdd; + $values[$coluimn] = $idMainObject; + $values['itemtype'] = $_POST['objectToAdd']; + + $mainObjectItem->fields = $values; + $mainObjectItem->addToDB(); + + $values = array(); + $values['items_id'] = $idMainObject; + $values[$coluimn1] = $idObjectToAdd; + $values['itemtype'] = $_POST['mainobject']; + + $mainObjectToAddItem->fields = $values; + $mainObjectToAddItem->addToDB($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..1ea5e3da 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; @@ -229,9 +233,9 @@ static function registerType() { array_push($GO_LINKED_TYPES, $class); } $items_class = $class."_Item"; - //if (class_exists($items_class)) { + if (class_exists($items_class)) { $items_class::registerType(); - //} + } } if ($item->canUseProjects()) { @@ -372,6 +376,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..fef95ee6 100644 --- a/inc/object_item.class.php +++ b/inc/object_item.class.php @@ -62,6 +62,85 @@ 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(array( + '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 = array()) { + if (!is_array($values)) { + $values = array($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 = array(); + foreach ($mainObjectItem->find() as $record) { + if ($record[$column] == $id) { + $listeId[] = $record['items_id']; + } + } + $object->dropdown(array('used' => $listeId)); + } + + static function getItemListForObject($itemtype, $obj_item, $idItemType) { + $nameMainObject = $itemtype.'_item'; + $objectItem = new $nameMainObject(); + $mainObject = new $itemtype(); + $column = str_replace('glpi_','',$mainObject->table.'_id'); + $resultat = $objectItem->find("`itemtype` = '".$obj_item."' and `".$column."` = $idItemType"); + foreach ($resultat as $item) { + $obj = new $item['itemtype'](); + $obj->getFromDB($item['items_id']); + echo ""; + //if ($canedit) { + echo ""; + Html::showMassiveActionCheckBox($objectItem->getType(), $item["id"]); + echo ""; + //} + echo "".$obj->getTypeName().""; + echo "".$item['items_id'].""; + echo "".$obj->getLink().""; + echo ""; + } + } + /** * * Enter description here ... @@ -82,13 +161,91 @@ static function showItemsForTarget(CommonDBTM $item) { } + static function showItems(CommonDBTM $item) { + global $DB, $CFG_GLPI; + $instID = $item->fields['id']; + if (!$item->can($instID, READ)) { + return false; + } + if ($item->canEdit($instID)) { + echo "
"; + echo "
"; + echo "
"; + echo ""; + echo ""; + echo ""; + echo ""; + echo "
".__("Select an object to link", 'genericobject')."  "; + echo ""; + //echo ""; + echo ""; + $elements = array('' => Dropdown::EMPTY_VALUE); + foreach ($item->getLinkedItemTypesAsArray() as $itemL) { + $object = new $itemL(); + $elements[$itemL] = $object->getTypeName(); + } + $rand = Dropdown::showFromArray('objectToAdd', $elements); + $paramsselsoft = array('objectToAdd' => '__VALUE__', + 'idMainobject' => $item->getID(), + '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 "
"; + Html::closeForm(); + echo "
"; + } + echo "
"; + //if ($canedit && $number) { + Html::openMassiveActionsForm('mass'.$item->getType().'_Item'.$rand); + $massiveactionparams = array('container' => 'mass'.$item->getType().'_Item'.$rand); + //Note : useless ? + $massiveactionparams['check_itemtype'] = $item->getType(); + Html::showMassiveActions($massiveactionparams); + //} + echo ""; + $header_begin = ""; + $header_top = ''; + $header_bottom = ''; + $header_end = ''; + //if ($canedit && $number) { + $header_top .= ""; + $header_bottom .= ""; + //} + $header_end .= ""; + $header_end .= ""; + $header_end .= ""; + echo $header_begin.$header_top.$header_end; + foreach ($item->getLinkedItemTypesAsArray() as $itemL) { + $object = new $itemL(); + self::getItemListForObject($item->accesObjectType()->fields['itemtype'], + $object->accesObjectType()->fields['itemtype'], $item->fields['id']); + } + //if ($number) { + 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')."
"; + //if ($canedit && $number) { + $massiveactionparams['ontop'] = false; + Html::showMassiveActions($massiveactionparams); + Html::closeForm(); + //} + return true; + } + /** * * Enter description here ... * @since 2.2.0 */ static function registerType() { - Plugin::registerClass(get_called_class(), ['addtabon' => self::getLinkedItemTypes()]); +// Plugin::registerClass(get_called_class(), ['addtabon' => self::getLinkedItemTypes()]); } static function getLinkedItemTypes() { @@ -104,21 +261,30 @@ 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'), + array("$coluimn1" => $item->getID())); + $str = _n("Linked object", "Linked objects", $nb == 0 ? 1 : $nb, "genericobject"); + return array(1 => self::createTabEntry($str, $nb)); } } return ''; } static function displayTabContentForItem(CommonGLPI $item, $tabnum = 1, $withtemplate = 0) { - $itemtypes = self::getLinkedItemTypes(); + /*$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..f279af6c 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,9 +740,9 @@ function showLinkedTypesForm() { echo ""; echo "".__("Link to other objects", "genericobject").""; echo ""; - echo ""._n("Type", "Types", 2).""; + echo ""; echo ""; - echo ""; $selected = []; if (!empty($this->fields['linked_itemtypes'])) { $selected = json_decode($this->fields['linked_itemtypes'], false); @@ -742,7 +757,22 @@ function showLinkedTypesForm() { } echo ">".$itemtype::getTypeName().""; } - echo ""; + echo "";*/ + $selected = $this->getLinkedItemTypesAsArray(); + $values = array(); + $elements = array(); + 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, array('multiple' => true, + 'values' => $values)); echo ""; echo ""; $this->showFormButtons(['candel' => false, 'canadd' => false]); @@ -917,7 +947,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 From e3bd4770a75b1cf30308af0050fb38813765d7b4 Mon Sep 17 00:00:00 2001 From: drTr0jan Date: Sat, 22 Feb 2020 12:03:32 +1000 Subject: [PATCH 2/7] Fixed Travis-founded errors --- front/object_item.form.php | 200 ++++++++++++++++++------------------- inc/object_item.class.php | 41 ++++---- inc/type.class.php | 15 +-- 3 files changed, 129 insertions(+), 127 deletions(-) diff --git a/front/object_item.form.php b/front/object_item.form.php index 0bd7c0ab..4179068c 100644 --- a/front/object_item.form.php +++ b/front/object_item.form.php @@ -3,64 +3,64 @@ include('../../../inc/includes.php'); if (! isset($_POST['objectToAdd']) || empty($_POST['objectToAdd'])) { - Session::addMessageAfterRedirect(__('Error'), true, ERROR, true); + Session::addMessageAfterRedirect(__('Error'), true, ERROR, true); Html::back(); - exit(); + exit(); } -$idMainObject = $_POST['items_id']; -$nameMainObject = $_POST['mainobject']; -$nameObjectToAdd = $_POST['objectToAdd']; -//$IdToAdd = $_POST['items_id']; +$idMainObject = $_POST['items_id']; +$nameMainObject = $_POST['mainobject']; +$nameObjectToAdd = $_POST['objectToAdd']; +//$IdToAdd = $_POST['items_id']; -$objectToAdd = new $nameObjectToAdd(); -$coluimn1 = str_replace('glpi_','',$objectToAdd->table.'_id'); +$objectToAdd = new $nameObjectToAdd(); +$coluimn1 = str_replace('glpi_', '', $objectToAdd->table.'_id'); if (! isset($_POST[$coluimn1]) || empty($_POST[$coluimn1])) { - Session::addMessageAfterRedirect(__('Error'), true, ERROR, true); + Session::addMessageAfterRedirect(__('Error'), true, ERROR, true); } else { - $IdToAdd = $_POST[$coluimn1]; - - $mainObject = new $nameMainObject; - $mainObject->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(array( - 'items_id' => $idObjectToAdd, - $coluimn => $idMainObject, - 'itemtype' => $_POST['objectToAdd'])); - if ($res) { - Session::addMessageAfterRedirect(__('This Object is already link','genericobject'), - true, ERROR, true); - } else { - $values = array(); - $values['items_id'] = $idObjectToAdd; - $values[$coluimn] = $idMainObject; - $values['itemtype'] = $_POST['objectToAdd']; - - $mainObjectItem->fields = $values; - $mainObjectItem->addToDB(); - - $values = array(); - $values['items_id'] = $idMainObject; - $values[$coluimn1] = $idObjectToAdd; - $values['itemtype'] = $_POST['mainobject']; - - $mainObjectToAddItem->fields = $values; - $mainObjectToAddItem->addToDB($values); - } + $IdToAdd = $_POST[$coluimn1]; + + $mainObject = new $nameMainObject; + $mainObject->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->fields = $values; + $mainObjectItem->addToDB(); + + $values = []; + $values['items_id'] = $idMainObject; + $values[$coluimn1] = $idObjectToAdd; + $values['itemtype'] = $_POST['mainobject']; + + $mainObjectToAddItem->fields = $values; + $mainObjectToAddItem->addToDB($values); + } } Html::back(); @@ -69,64 +69,64 @@ include('../../../inc/includes.php'); if (! isset($_POST['objectToAdd']) || empty($_POST['objectToAdd'])) { - Session::addMessageAfterRedirect(__('Error'), true, ERROR, true); + Session::addMessageAfterRedirect(__('Error'), true, ERROR, true); Html::back(); - exit(); + exit(); } -$idMainObject = $_POST['items_id']; -$nameMainObject = $_POST['mainobject']; -$nameObjectToAdd = $_POST['objectToAdd']; -//$IdToAdd = $_POST['items_id']; +$idMainObject = $_POST['items_id']; +$nameMainObject = $_POST['mainobject']; +$nameObjectToAdd = $_POST['objectToAdd']; +//$IdToAdd = $_POST['items_id']; -$objectToAdd = new $nameObjectToAdd(); -$coluimn1 = str_replace('glpi_','',$objectToAdd->table.'_id'); +$objectToAdd = new $nameObjectToAdd(); +$coluimn1 = str_replace('glpi_', '', $objectToAdd->table.'_id'); if (! isset($_POST[$coluimn1]) || empty($_POST[$coluimn1])) { - Session::addMessageAfterRedirect(__('Error'), true, ERROR, true); + Session::addMessageAfterRedirect(__('Error'), true, ERROR, true); } else { - $IdToAdd = $_POST[$coluimn1]; - - $mainObject = new $nameMainObject; - $mainObject->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(array( - 'items_id' => $idObjectToAdd, - $coluimn => $idMainObject, - 'itemtype' => $_POST['objectToAdd'])); - if ($res) { - Session::addMessageAfterRedirect(__('This Object is already link','genericobject'), - true, ERROR, true); - } else { - $values = array(); - $values['items_id'] = $idObjectToAdd; - $values[$coluimn] = $idMainObject; - $values['itemtype'] = $_POST['objectToAdd']; - - $mainObjectItem->fields = $values; - $mainObjectItem->addToDB(); - - $values = array(); - $values['items_id'] = $idMainObject; - $values[$coluimn1] = $idObjectToAdd; - $values['itemtype'] = $_POST['mainobject']; - - $mainObjectToAddItem->fields = $values; - $mainObjectToAddItem->addToDB($values); - } + $IdToAdd = $_POST[$coluimn1]; + + $mainObject = new $nameMainObject; + $mainObject->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->fields = $values; + $mainObjectItem->addToDB(); + + $values = array[]; + $values['items_id'] = $idMainObject; + $values[$coluimn1] = $idObjectToAdd; + $values['itemtype'] = $_POST['mainobject']; + + $mainObjectToAddItem->fields = $values; + $mainObjectToAddItem->addToDB($values); + } } Html::back(); diff --git a/inc/object_item.class.php b/inc/object_item.class.php index fef95ee6..8df71ac9 100644 --- a/inc/object_item.class.php +++ b/inc/object_item.class.php @@ -67,7 +67,8 @@ static function canPurge() { return true; } - static function canDelete() { //useless + static function canDelete() { + //useless //Note : can be add a right return true; } @@ -79,18 +80,18 @@ function post_purgeItem() { $obj_item = new $obj_itemtype(); $itemtype = $this->fields['itemtype']; $obj = new $itemtype(); - $column = str_replace('glpi_','',$obj->table.'_id'); - $obj_item->deleteByCriteria(array( + $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) + 'itemtype' => static::$itemtype_1] ); parent::post_purgeItem(); } - static function getSpecificValueToDisplay($field, $values, array $options = array()) { + static function getSpecificValueToDisplay($field, $values, array $options = []) { if (!is_array($values)) { - $values = array($field => $values); + $values = [$field => $values]; } switch ($field) { // Column "Linked objects" : Display name of the object and type of object @@ -109,21 +110,21 @@ public static function getDropdownItemLinked($object, $itemType, $id) { $obj = new $itemType(); $nameMainObjectItem = $itemType."_Item"; $mainObjectItem = new $nameMainObjectItem(); - $column = str_replace('glpi_','',$obj->table."_id"); - $listeId = array(); + $column = str_replace('glpi_', '', $obj->table."_id"); + $listeId = []; foreach ($mainObjectItem->find() as $record) { if ($record[$column] == $id) { $listeId[] = $record['items_id']; } } - $object->dropdown(array('used' => $listeId)); + $object->dropdown(['used' => $listeId]); } static function getItemListForObject($itemtype, $obj_item, $idItemType) { $nameMainObject = $itemtype.'_item'; $objectItem = new $nameMainObject(); $mainObject = new $itemtype(); - $column = str_replace('glpi_','',$mainObject->table.'_id'); + $column = str_replace('glpi_', '', $mainObject->table.'_id'); $resultat = $objectItem->find("`itemtype` = '".$obj_item."' and `".$column."` = $idItemType"); foreach ($resultat as $item) { $obj = new $item['itemtype'](); @@ -177,15 +178,15 @@ static function showItems(CommonDBTM $item) { echo ""; //echo ""; echo ""; - $elements = array('' => Dropdown::EMPTY_VALUE); + $elements = ['' => Dropdown::EMPTY_VALUE]; foreach ($item->getLinkedItemTypesAsArray() as $itemL) { $object = new $itemL(); $elements[$itemL] = $object->getTypeName(); } $rand = Dropdown::showFromArray('objectToAdd', $elements); - $paramsselsoft = array('objectToAdd' => '__VALUE__', + $paramsselsoft = ['objectToAdd' => '__VALUE__', 'idMainobject' => $item->getID(), - 'mainobject' => $item->getType()); + 'mainobject' => $item->getType()]; Ajax::updateItemOnSelectEvent("dropdown_objectToAdd$rand", "show_".$rand, $CFG_GLPI["root_doc"]."/plugins/genericobject/ajax/dropdownByItemtype.php", $paramsselsoft); @@ -202,7 +203,7 @@ static function showItems(CommonDBTM $item) { echo "
"; //if ($canedit && $number) { Html::openMassiveActionsForm('mass'.$item->getType().'_Item'.$rand); - $massiveactionparams = array('container' => 'mass'.$item->getType().'_Item'.$rand); + $massiveactionparams = ['container' => 'mass'.$item->getType().'_Item'.$rand]; //Note : useless ? $massiveactionparams['check_itemtype'] = $item->getType(); Html::showMassiveActions($massiveactionparams); @@ -224,7 +225,7 @@ static function showItems(CommonDBTM $item) { echo $header_begin.$header_top.$header_end; foreach ($item->getLinkedItemTypesAsArray() as $itemL) { $object = new $itemL(); - self::getItemListForObject($item->accesObjectType()->fields['itemtype'], + self::getItemListForObject($item->accesObjectType()->fields['itemtype'], $object->accesObjectType()->fields['itemtype'], $item->fields['id']); } //if ($number) { @@ -245,7 +246,7 @@ static function showItems(CommonDBTM $item) { * @since 2.2.0 */ static function registerType() { -// Plugin::registerClass(get_called_class(), ['addtabon' => self::getLinkedItemTypes()]); + //Plugin::registerClass(get_called_class(), ['addtabon' => self::getLinkedItemTypes()]); } static function getLinkedItemTypes() { @@ -263,12 +264,12 @@ function getTabNameForItem(CommonGLPI $item, $withtemplate = 0) { if (!$withtemplate) { $itemtypes = self::getLinkedItemTypes(get_class($item)); if (in_array(get_class($item), $itemtypes) || get_class($item) == self::getItemType1()) { -// return [1 => __("Objects management", "genericobject")]; - $coluimn1 = str_replace('glpi_','',$item->table.'_id'); + //return [1 => __("Objects management", "genericobject")]; + $coluimn1 = str_replace('glpi_', '', $item->table.'_id'); $nb = countElementsInTable(getTableForItemType($item->getType().'_Item'), - array("$coluimn1" => $item->getID())); + ["$coluimn1" => $item->getID()]); $str = _n("Linked object", "Linked objects", $nb == 0 ? 1 : $nb, "genericobject"); - return array(1 => self::createTabEntry($str, $nb)); + return [1 => self::createTabEntry($str, $nb)]; } } return ''; diff --git a/inc/type.class.php b/inc/type.class.php index f279af6c..15aff21a 100644 --- a/inc/type.class.php +++ b/inc/type.class.php @@ -141,8 +141,8 @@ static function displayTabContentForItem(CommonGLPI $item, $tabnum = 1, $withtem case 6: PluginGenericobjectProfile::showForItemtype($item); break; - case 7: - if($item->canUseDirectConnections()) { + case 7: + if ($item->canUseDirectConnections()) { $item->showLinkedTypesForm(); } break; @@ -742,7 +742,8 @@ function showLinkedTypesForm() { echo ""; echo ""; echo ""; -/* echo ""; $selected = []; if (!empty($this->fields['linked_itemtypes'])) { $selected = json_decode($this->fields['linked_itemtypes'], false); @@ -759,8 +760,8 @@ function showLinkedTypesForm() { } echo "";*/ $selected = $this->getLinkedItemTypesAsArray(); - $values = array(); - $elements = array(); + $values = []; + $elements = []; foreach ($GO_LINKED_TYPES as $itemtype) { if ($itemtype == $this->fields['itemtype']) { continue; @@ -771,8 +772,8 @@ function showLinkedTypesForm() { $elements[$itemtype] = $itemtype::getTypeName(); } echo ""; - Dropdown::showFromArray('itemtypes', $elements, array('multiple' => true, - 'values' => $values)); + Dropdown::showFromArray('itemtypes', $elements, ['multiple' => true, + 'values' => $values]); echo ""; echo ""; $this->showFormButtons(['candel' => false, 'canadd' => false]); From ebaa59737177a95290e21ac5f4263b18eec4a3b6 Mon Sep 17 00:00:00 2001 From: drTr0jan Date: Sat, 22 Feb 2020 12:06:23 +1000 Subject: [PATCH 3/7] Fixed Travis-founded errors #2 --- front/object_item.form.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/front/object_item.form.php b/front/object_item.form.php index 4179068c..a7515ee5 100644 --- a/front/object_item.form.php +++ b/front/object_item.form.php @@ -24,7 +24,7 @@ $mainObject = new $nameMainObject; $mainObject->getFromDB(1); //useless ? - $coluimn = str_replace('glpi_','',$mainObject->table.'_id'); + $coluimn = str_replace('glpi_', '', $mainObject->table.'_id'); $nameMainObject = $nameMainObject.'_item'; $nameObjectToAdd = $nameObjectToAdd.'_item'; From 4dfc94e5600d99166161dd49818e290a452a0093 Mon Sep 17 00:00:00 2001 From: drTr0jan Date: Sat, 25 Apr 2020 11:05:53 +1000 Subject: [PATCH 4/7] Fixed computers link and some typos --- ajax/dropdownByItemtype.php | 13 ---- front/object_item.form.php | 68 +---------------- inc/object.class.php | 4 - inc/object_item.class.php | 148 +++++++++++++++++++++++------------- 4 files changed, 96 insertions(+), 137 deletions(-) diff --git a/ajax/dropdownByItemtype.php b/ajax/dropdownByItemtype.php index 1d0c406d..690f6639 100644 --- a/ajax/dropdownByItemtype.php +++ b/ajax/dropdownByItemtype.php @@ -11,16 +11,3 @@ $object = new $itemtype(); PluginGenericobjectObject_Item::getDropdownItemLinked($object, $_REQUEST['mainobject'], $_REQUEST['idMainobject']); -table.'_id'); +$coluimn1 = str_replace('glpi_', '', $nameObjectToAdd::getTable().'_id'); if (! isset($_POST[$coluimn1]) || empty($_POST[$coluimn1])) { Session::addMessageAfterRedirect(__('Error'), true, ERROR, true); @@ -64,69 +64,3 @@ } Html::back(); -table.'_id'); - -if (! isset($_POST[$coluimn1]) || empty($_POST[$coluimn1])) { - Session::addMessageAfterRedirect(__('Error'), true, ERROR, true); -} else { - - $IdToAdd = $_POST[$coluimn1]; - - $mainObject = new $nameMainObject; - $mainObject->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->fields = $values; - $mainObjectItem->addToDB(); - - $values = array[]; - $values['items_id'] = $idMainObject; - $values[$coluimn1] = $idObjectToAdd; - $values['itemtype'] = $_POST['mainobject']; - - $mainObjectToAddItem->fields = $values; - $mainObjectToAddItem->addToDB($values); - } -} - -Html::back(); diff --git a/inc/object.class.php b/inc/object.class.php index 1ea5e3da..4512d2dc 100644 --- a/inc/object.class.php +++ b/inc/object.class.php @@ -232,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()) { diff --git a/inc/object_item.class.php b/inc/object_item.class.php index 8df71ac9..e329df2d 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; @@ -164,19 +164,38 @@ static function showItemsForTarget(CommonDBTM $item) { static function showItems(CommonDBTM $item) { global $DB, $CFG_GLPI; - $instID = $item->fields['id']; - if (!$item->can($instID, READ)) { + + $ID = $item->getID(); + if (!$item->can($ID, READ)) { return false; } - if ($item->canEdit($instID)) { + $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 ""; echo "
".__("Select an object to link", 'genericobject')."  "; - echo ""; - //echo ""; + echo ""; echo ""; $elements = ['' => Dropdown::EMPTY_VALUE]; foreach ($item->getLinkedItemTypesAsArray() as $itemL) { @@ -185,14 +204,14 @@ static function showItems(CommonDBTM $item) { } $rand = Dropdown::showFromArray('objectToAdd', $elements); $paramsselsoft = ['objectToAdd' => '__VALUE__', - 'idMainobject' => $item->getID(), + '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 "
"; @@ -200,43 +219,73 @@ static function showItems(CommonDBTM $item) { Html::closeForm(); echo "
"; } - echo "
"; - //if ($canedit && $number) { - Html::openMassiveActionsForm('mass'.$item->getType().'_Item'.$rand); - $massiveactionparams = ['container' => 'mass'.$item->getType().'_Item'.$rand]; - //Note : useless ? - $massiveactionparams['check_itemtype'] = $item->getType(); - Html::showMassiveActions($massiveactionparams); - //} - echo ""; - $header_begin = ""; - $header_top = ''; - $header_bottom = ''; - $header_end = ''; - //if ($canedit && $number) { - $header_top .= ""; - $header_bottom .= ""; - //} - $header_end .= ""; - $header_end .= ""; - $header_end .= ""; - echo $header_begin.$header_top.$header_end; - foreach ($item->getLinkedItemTypesAsArray() as $itemL) { - $object = new $itemL(); - self::getItemListForObject($item->accesObjectType()->fields['itemtype'], - $object->accesObjectType()->fields['itemtype'], $item->fields['id']); + + 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 "
".Html::getCheckAllAsCheckbox('mass'.$item->getType().'_Item'.$rand); - $header_top .= "".Html::getCheckAllAsCheckbox('mass'.$item->getType().'_Item'.$rand); - $header_bottom .= "".__('Type')."".__('ID')."".__('Name')."
"; + $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 "
"; } - //if ($number) { - echo $header_begin.$header_bottom.$header_end; - //} - echo ""; - //if ($canedit && $number) { - $massiveactionparams['ontop'] = false; - Html::showMassiveActions($massiveactionparams); - Html::closeForm(); - //} return true; } @@ -246,7 +295,7 @@ static function showItems(CommonDBTM $item) { * @since 2.2.0 */ static function registerType() { - //Plugin::registerClass(get_called_class(), ['addtabon' => self::getLinkedItemTypes()]); + Plugin::registerClass(get_called_class(), ['addtabon' => self::getLinkedItemTypes()]); } static function getLinkedItemTypes() { @@ -276,13 +325,6 @@ function getTabNameForItem(CommonGLPI $item, $withtemplate = 0) { } 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); } From e35c64d3f6de3f86efa3f87476d31296558b4b16 Mon Sep 17 00:00:00 2001 From: drTr0jan Date: Sat, 25 Apr 2020 11:15:09 +1000 Subject: [PATCH 5/7] Fixed Travis-founded errors #3 --- inc/object_item.class.php | 120 +++++++++++++++++++------------------- 1 file changed, 60 insertions(+), 60 deletions(-) diff --git a/inc/object_item.class.php b/inc/object_item.class.php index e329df2d..b469897f 100644 --- a/inc/object_item.class.php +++ b/inc/object_item.class.php @@ -180,9 +180,9 @@ static function showItems(CommonDBTM $item) { $iterator = self::getTypeItems($ID, $itemtype); while ($data = $iterator->next()) { - $data['assoc_itemtype'] = $itemtype; - $datas[] = $data; - $used[$itemtype][] = $data['id']; + $data['assoc_itemtype'] = $itemtype; + $datas[] = $data; + $used[$itemtype][] = $data['id']; } } } @@ -221,10 +221,10 @@ static function showItems(CommonDBTM $item) { } if ($number) { - echo "
"; - if ($canedit) { - Html::openMassiveActionsForm('mass'.$item->getType().'_Item'.$rand); - $massiveactionparams + echo "
"; + if ($canedit) { + Html::openMassiveActionsForm('mass'.$item->getType().'_Item'.$rand); + $massiveactionparams = ['num_displayed' => min($_SESSION['glpilist_limit'], $number), 'specific_actions' @@ -232,59 +232,59 @@ static function showItems(CommonDBTM $item) { '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 "
"; + 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; } From a935e0909e977f070091fc731199335358a3f961 Mon Sep 17 00:00:00 2001 From: drTr0jan Date: Sat, 25 Apr 2020 13:50:40 +1000 Subject: [PATCH 6/7] Rewrited usage Dropdown::showFromArray() to Dropdown::showItemType() like a Computer_Item --- inc/object_item.class.php | 51 ++++++++++++--------------------------- 1 file changed, 16 insertions(+), 35 deletions(-) diff --git a/inc/object_item.class.php b/inc/object_item.class.php index b469897f..24e96112 100644 --- a/inc/object_item.class.php +++ b/inc/object_item.class.php @@ -120,28 +120,6 @@ public static function getDropdownItemLinked($object, $itemType, $id) { $object->dropdown(['used' => $listeId]); } - static function getItemListForObject($itemtype, $obj_item, $idItemType) { - $nameMainObject = $itemtype.'_item'; - $objectItem = new $nameMainObject(); - $mainObject = new $itemtype(); - $column = str_replace('glpi_', '', $mainObject->table.'_id'); - $resultat = $objectItem->find("`itemtype` = '".$obj_item."' and `".$column."` = $idItemType"); - foreach ($resultat as $item) { - $obj = new $item['itemtype'](); - $obj->getFromDB($item['items_id']); - echo ""; - //if ($canedit) { - echo ""; - Html::showMassiveActionCheckBox($objectItem->getType(), $item["id"]); - echo ""; - //} - echo "".$obj->getTypeName().""; - echo "".$item['items_id'].""; - echo "".$obj->getLink().""; - echo ""; - } - } - /** * * Enter description here ... @@ -195,21 +173,24 @@ static function showItems(CommonDBTM $item) { echo ""; echo ""; echo ""; From a8abb582e091d2f6d4e097f4a3804d479c59401c Mon Sep 17 00:00:00 2001 From: drTr0jan Date: Mon, 11 May 2020 15:05:40 +1000 Subject: [PATCH 7/7] Fixed Historical behaviour --- front/object_item.form.php | 6 ++---- inc/object_item.class.php | 1 + 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/front/object_item.form.php b/front/object_item.form.php index ab6fc691..5074b740 100644 --- a/front/object_item.form.php +++ b/front/object_item.form.php @@ -50,16 +50,14 @@ $values[$coluimn] = $idMainObject; $values['itemtype'] = $_POST['objectToAdd']; - $mainObjectItem->fields = $values; - $mainObjectItem->addToDB(); + $mainObjectItem->add($values); $values = []; $values['items_id'] = $idMainObject; $values[$coluimn1] = $idObjectToAdd; $values['itemtype'] = $_POST['mainobject']; - $mainObjectToAddItem->fields = $values; - $mainObjectToAddItem->addToDB($values); + $mainObjectToAddItem->add($values); } } diff --git a/inc/object_item.class.php b/inc/object_item.class.php index 24e96112..79e878cd 100644 --- a/inc/object_item.class.php +++ b/inc/object_item.class.php @@ -35,6 +35,7 @@ class PluginGenericobjectObject_Item extends CommonDBRelation { 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) {
".__("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 ""; - $elements = ['' => Dropdown::EMPTY_VALUE]; - foreach ($item->getLinkedItemTypesAsArray() as $itemL) { - $object = new $itemL(); - $elements[$itemL] = $object->getTypeName(); - } - $rand = Dropdown::showFromArray('objectToAdd', $elements); - $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 "