Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add genericobject link #266

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions ajax/dropdownByItemtype.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

include ("../../../inc/includes.php");
header("Content-Type: text/html; charset=UTF-8");
Html::header_nocache();
Session::checkLoginUser();

//Session::checkRight("software", UPDATE);

$itemtype = $_REQUEST['objectToAdd'];

$object = new $itemtype();
PluginGenericobjectObject_Item::getDropdownItemLinked($object, $_REQUEST['mainobject'], $_REQUEST['idMainobject']);
<?php

include ("../../../inc/includes.php");
header("Content-Type: text/html; charset=UTF-8");
Html::header_nocache();
Session::checkLoginUser();

//Session::checkRight("software", UPDATE);

$itemtype = $_REQUEST['objectToAdd'];

$object = new $itemtype();
PluginGenericobjectObject_Item::getDropdownItemLinked($object, $_REQUEST['mainobject'], $_REQUEST['idMainobject']);
drTr0jan marked this conversation as resolved.
Show resolved Hide resolved
132 changes: 132 additions & 0 deletions front/object_item.form.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<?php

include('../../../inc/includes.php');

if (! isset($_POST['objectToAdd']) || empty($_POST['objectToAdd'])) {
Session::addMessageAfterRedirect(__('Error'), true, ERROR, true);
Html::back();
exit();
}

$idMainObject = $_POST['items_id'];
$nameMainObject = $_POST['mainobject'];
$nameObjectToAdd = $_POST['objectToAdd'];
//$IdToAdd = $_POST['items_id'];

$objectToAdd = new $nameObjectToAdd();
$coluimn1 = str_replace('glpi_', '', $objectToAdd->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 = [];
$values['items_id'] = $idMainObject;
$values[$coluimn1] = $idObjectToAdd;
$values['itemtype'] = $_POST['mainobject'];

$mainObjectToAddItem->fields = $values;
$mainObjectToAddItem->addToDB($values);
}
}

Html::back();
<?php
drTr0jan marked this conversation as resolved.
Show resolved Hide resolved

include('../../../inc/includes.php');

if (! isset($_POST['objectToAdd']) || empty($_POST['objectToAdd'])) {
Session::addMessageAfterRedirect(__('Error'), true, ERROR, true);
Html::back();
exit();
}

$idMainObject = $_POST['items_id'];
$nameMainObject = $_POST['mainobject'];
$nameObjectToAdd = $_POST['objectToAdd'];
//$IdToAdd = $_POST['items_id'];

$objectToAdd = new $nameObjectToAdd();
$coluimn1 = str_replace('glpi_', '', $objectToAdd->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();
2 changes: 2 additions & 0 deletions front/type.form.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 10 additions & 2 deletions inc/object.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -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);
}
Expand Down
Loading