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 "
".Html::getCheckAllAsCheckbox('mass'.$item->getType().'_Item'.$rand); + $header_top .= " | "; + $header_bottom .= "".Html::getCheckAllAsCheckbox('mass'.$item->getType().'_Item'.$rand); + $header_bottom .= " | "; + } + + $header_end .= "".__('Type')." | "; + $header_end .= "".__('ID')." | "; + $header_end .= "".__('Name')." | "; + 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 "
---|---|---|---|---|
"; + Html::showMassiveActionCheckBox($item->getType().'_Item', $data["linkid"]); + echo " | "; + } + + echo "".$itemtype::getTypeName(1)." | "; + echo "".$data["id"]." | "; + echo "$name | "; + echo "