.
* ---------------------------------------------------------------------
**/
class Appliance_Item extends CommonDBRelation {
use Glpi\Features\Clonable;
static public $itemtype_1 = 'Appliance';
static public $items_id_1 = 'appliances_id';
static public $take_entity_1 = false;
static public $itemtype_2 = 'itemtype';
static public $items_id_2 = 'items_id';
static public $take_entity_2 = true;
public function getCloneRelations() :array {
return [
Appliance_Item_Relation::class
];
}
static function getTypeName($nb = 0) {
return _n('Item', 'Items', $nb);
}
function getTabNameForItem(CommonGLPI $item, $withtemplate = 0) {
if (!Appliance::canView()) {
return '';
}
$nb = 0;
if ($item->getType() == Appliance::class) {
if ($_SESSION['glpishow_count_on_tabs']) {
if (!$item->isNewItem()) {
$nb = self::countForMainItem($item);
}
}
return self::createTabEntry(self::getTypeName(Session::getPluralNumber()), $nb);
} else if (in_array($item->getType(), Appliance::getTypes(true))) {
if ($_SESSION['glpishow_count_on_tabs']) {
$nb = self::countForItem($item);
}
return self::createTabEntry(Appliance::getTypeName(Session::getPluralNumber()), $nb);
}
}
static function displayTabContentForItem(CommonGLPI $item, $tabnum = 1, $withtemplate = 0) {
switch ($item->getType()) {
case Appliance::class:
self::showItems($item);
break;
default :
if (in_array($item->getType(), Appliance::getTypes())) {
self::showForItem($item, $withtemplate);
}
}
return true;
}
/**
* Print enclosure items
*
* @param CommonDBTM $item CommonDBTM object wanted
*
* @return void
**/
static function showItems(Appliance $appliance) {
global $DB;
$ID = $appliance->fields['id'];
$rand = mt_rand();
if (!$appliance->getFromDB($ID)
|| !$appliance->can($ID, READ)) {
return false;
}
$canedit = $appliance->canEdit($ID);
$items = $DB->request([
'FROM' => self::getTable(),
'WHERE' => [
self::$items_id_1 => $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'),
$appliance->getTypeName(1),
$appliance->getName()
)
);
if ($appliance->canAddItem('itemtype')) {
echo "
";
}
$items = iterator_to_array($items);
if (!count($items)) {
echo "| ".__('No item found')." |
";
echo "
";
} 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 .= Html::getCheckAllAsCheckbox('mass'.__CLASS__.$rand);
$header .= " | ";
}
$header .= "".__('Itemtype')." | ";
$header .= ""._n('Item', 'Items', 1)." | ";
$header .= "".__("Serial")." | ";
$header .= "".__("Inventory number")." | ";
$header .= "".Appliance_Item_Relation::getTypeName(Session::getPluralNumber())." | ";
$header .= "
";
echo $header;
foreach ($items as $row) {
$item = new $row['itemtype'];
$item->getFromDB($row['items_id']);
echo "";
if ($canedit) {
echo "| ";
Html::showMassiveActionCheckBox(__CLASS__, $row["id"]);
echo " | ";
}
echo "" . $item->getTypeName(1) . " | ";
echo "" . $item->getLink() . " | ";
echo "" . $item->fields['serial'] ?? "" . " | ";
echo "" . $item->fields['otherserial'] ?? "" . " | ";
echo "";
echo Appliance_Item_Relation::showListForApplianceItem($row["id"], $canedit);
echo " | ";
echo "
";
}
echo $header;
echo "
";
if ($canedit && count($items)) {
$massiveactionparams['ontop'] = false;
Html::showMassiveActions($massiveactionparams);
}
if ($canedit) {
Html::closeForm();
}
echo Appliance_Item_Relation::getListJSForApplianceItem($appliance, $canedit);
}
}
/**
* Print an HTML array of appliances associated to an object
*
* @since 9.5.2
*
* @param CommonDBTM $item CommonDBTM object wanted
* @param boolean $withtemplate not used (to be deleted)
*
* @return void
**/
static function showForItem(CommonDBTM $item, $withtemplate = 0) {
$itemtype = $item->getType();
$ID = $item->fields['id'];
if (!Appliance::canView()
|| !$item->can($ID, READ)) {
return;
}
$canedit = $item->can($ID, UPDATE);
$rand = mt_rand();
$iterator = self::getListForItem($item);
$number = count($iterator);
$appliances = [];
$used = [];
while ($data = $iterator->next()) {
$appliances[$data['id']] = $data;
$used[$data['id']] = $data['id'];
}
if ($canedit && ($withtemplate != 2)) {
echo "";
}
echo "";
if ($withtemplate != 2) {
if ($canedit && $number) {
Html::openMassiveActionsForm('mass'.__CLASS__.$rand);
$massiveactionparams = ['num_displayed' => min($_SESSION['glpilist_limit'], $number),
'container' => 'mass'.__CLASS__.$rand];
Html::showMassiveActions($massiveactionparams);
}
}
echo "
";
$header = "";
if ($canedit && $number && ($withtemplate != 2)) {
$header .= "| ".Html::getCheckAllAsCheckbox('mass'.__CLASS__.$rand);
$header .= " | ";
}
$header .= "".__('Name')." | ";
$header .= "".Appliance_Item_Relation::getTypeName(Session::getPluralNumber())." | ";
$header .= "
";
if ($number > 0) {
echo $header;
Session::initNavigateListItems(__CLASS__,
//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'),
$item->getTypeName(1), $item->getName()));
foreach ($appliances as $data) {
$cID = $data["id"];
Session::addToNavigateListItems(__CLASS__, $cID);
$assocID = $data["linkid"];
$app = new Appliance();
$app->getFromResultSet($data);
echo "";
if ($canedit && ($withtemplate != 2)) {
echo "| ";
Html::showMassiveActionCheckBox(__CLASS__, $assocID);
echo " | ";
}
echo "";
$name = $app->fields["name"];
if ($_SESSION["glpiis_ids_visible"]
|| empty($app->fields["name"])) {
$name = sprintf(__('%1$s (%2$s)'), $name, $app->fields["id"]);
}
echo "".$name."";
echo " | ";
echo "";
echo Appliance_Item_Relation::showListForApplianceItem($assocID, $canedit);
echo " | ";
echo "
";
}
echo $header;
echo "
";
} else {
echo "
";
echo "| ".__('No item found')." |
|---|
";
}
echo "";
if ($canedit && $number && ($withtemplate != 2)) {
$massiveactionparams['ontop'] = false;
Html::showMassiveActions($massiveactionparams);
Html::closeForm();
}
echo "
";
echo Appliance_Item_Relation::getListJSForApplianceItem($item, $canedit);
}
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[self::$items_id_1]) || empty($input[self::$items_id_1])))
|| (isset($input[self::$items_id_1]) && empty($input[self::$items_id_1]))) {
$error_detected[] = __('An appliance is required');
}
if (count($error_detected)) {
foreach ($error_detected as $error) {
Session::addMessageAfterRedirect(
$error,
true,
ERROR
);
}
return false;
}
return $input;
}
public static function countForMainItem(CommonDBTM $item, $extra_types_where = []) {
$types = Appliance::getTypes();
$clause = [];
if (count($types)) {
$clause = ['itemtype' => $types];
} else {
$clause = [new \QueryExpression('true = false')];
}
$extra_types_where = array_merge(
$extra_types_where,
$clause
);
return parent::countForMainItem($item, $extra_types_where);
}
function getForbiddenStandardMassiveAction() {
$forbidden = parent::getForbiddenStandardMassiveAction();
$forbidden[] = 'update';
$forbidden[] = 'CommonDBConnexity:unaffect';
$forbidden[] = 'CommonDBConnexity:affect';
return $forbidden;
}
static function getRelationMassiveActionsSpecificities() {
global $CFG_GLPI;
$specificities = parent::getRelationMassiveActionsSpecificities();
$specificities['itemtypes'] = Appliance::getTypes();
return $specificities;
}
function cleanDBonPurge() {
$this->deleteChildrenAndRelationsFromDb(
[
Appliance_Item_Relation::class,
]
);
}
}