.
* ---------------------------------------------------------------------
*/
if (!defined('GLPI_ROOT')) {
die("Sorry. You can't access this file directly");
}
/**
* Item_Problem Class
*
* Relation between Problems and Items
**/
class Item_Problem extends CommonDBRelation{
// From CommonDBRelation
static public $itemtype_1 = 'Problem';
static public $items_id_1 = 'problems_id';
static public $itemtype_2 = 'itemtype';
static public $items_id_2 = 'items_id';
static public $checkItem_2_Rights = self::HAVE_VIEW_RIGHT_ON_ITEM;
function getForbiddenStandardMassiveAction() {
$forbidden = parent::getForbiddenStandardMassiveAction();
$forbidden[] = 'update';
return $forbidden;
}
function prepareInputForAdd($input) {
// Avoid duplicate entry
if (countElementsInTable($this->getTable(), ['problems_id' => $input['problems_id'],
'itemtype' => $input['itemtype'],
'items_id' => $input['items_id']])>0) {
return false;
}
return parent::prepareInputForAdd($input);
}
/**
* Print the HTML array for Items linked to a problem
*
* @param $problem Problem object
*
* @return void
**/
static function showForProblem(Problem $problem) {
$instID = $problem->fields['id'];
if (!$problem->can($instID, READ)) {
return false;
}
$canedit = $problem->canEdit($instID);
$rand = mt_rand();
$types_iterator= self::getDistinctTypes($instID);
$number = count($types_iterator);
if ($canedit) {
echo "
";
}
echo "";
if ($canedit && $number) {
Html::openMassiveActionsForm('mass'.__CLASS__.$rand);
$massiveactionparams = ['container' => 'mass'.__CLASS__.$rand];
Html::showMassiveActions($massiveactionparams);
}
echo "
";
$header_begin = "";
$header_top = '';
$header_bottom = '';
$header_end = '';
if ($canedit && $number) {
$header_top .= "| ".Html::getCheckAllAsCheckbox('mass'.__CLASS__.$rand);
$header_top .= " | ";
$header_bottom .= "".Html::getCheckAllAsCheckbox('mass'.__CLASS__.$rand);
$header_bottom .= " | ";
}
$header_end .= ""._n('Type', 'Types', 1)." | ";
$header_end .= "".Entity::getTypeName(1)." | ";
$header_end .= "".__('Name')." | ";
$header_end .= "".__('Serial number')." | ";
$header_end .= "".__('Inventory number')." |
";
echo $header_begin.$header_top.$header_end;
$totalnb = 0;
while ($row = $types_iterator->next()) {
$itemtype = $row['itemtype'];
if (!($item = getItemForItemtype($itemtype))) {
continue;
}
if ($item->canView()) {
$iterator = self::getTypeItems($instID, $itemtype);
$nb = count($iterator);
$prem = true;
while ($data = $iterator->next()) {
$name = $data["name"];
if ($_SESSION["glpiis_ids_visible"]
|| empty($data["name"])) {
$name = sprintf(__('%1$s (%2$s)'), $name, $data["id"]);
}
$link = $itemtype::getFormURLWithID($data['id']);
$namelink = "".$name."";
echo "";
if ($canedit) {
echo "| ";
Html::showMassiveActionCheckBox(__CLASS__, $data["linkid"]);
echo " | ";
}
if ($prem) {
$typename = $item->getTypeName($nb);
echo "".
(($nb > 1) ? sprintf(__('%1$s: %2$s'), $typename, $nb) : $typename)." | ";
$prem = false;
}
echo "";
echo Dropdown::getDropdownName("glpi_entities", $data['entity'])." | ";
echo "".$namelink." | ";
echo "".(isset($data["serial"])? "".$data["serial"]."" :"-").
" | ";
echo "".
(isset($data["otherserial"])? "".$data["otherserial"]."" :"-")." | ";
echo "
";
}
$totalnb += $nb;
}
}
if ($number) {
echo $header_begin.$header_bottom.$header_end;
}
echo "
";
if ($canedit && $number) {
$massiveactionparams['ontop'] = false;
Html::showMassiveActions($massiveactionparams);
Html::closeForm();
}
echo "
";
}
function getTabNameForItem(CommonGLPI $item, $withtemplate = 0) {
global $DB;
if (!$withtemplate) {
$nb = 0;
switch ($item->getType()) {
case 'Problem' :
if ($_SESSION['glpishow_count_on_tabs']) {
$nb = self::countForMainItem($item);
}
return self::createTabEntry(_n('Item', 'Items', Session::getPluralNumber()), $nb);
case 'User' :
case 'Group' :
case 'Supplier' :
if ($_SESSION['glpishow_count_on_tabs']) {
$from = $item->getType() == 'Group' ? 'glpi_groups_problems' : 'glpi_problems_' . strtolower($item->getType() . 's');
$result = $DB->request([
'COUNT' => 'cpt',
'FROM' => $from,
'WHERE' => [
$item->getForeignKeyField() => $item->fields['id']
]
])->next();
$nb = $result['cpt'];
}
return self::createTabEntry(Problem::getTypeName(Session::getPluralNumber()), $nb);
default :
if (Session::haveRight("problem", Problem::READALL)) {
if ($_SESSION['glpishow_count_on_tabs']) {
// Direct one
$nb = self::countForItem($item);
// Linked items
$linkeditems = $item->getLinkedItems();
if (count($linkeditems)) {
foreach ($linkeditems as $type => $tab) {
$typeitem = new $type;
foreach ($tab as $ID) {
$typeitem->getFromDB($ID);
$nb += self::countForItem($typeitem);
}
}
}
}
return self::createTabEntry(Problem::getTypeName(Session::getPluralNumber()), $nb);
}
}
}
return '';
}
static function displayTabContentForItem(CommonGLPI $item, $tabnum = 1, $withtemplate = 0) {
switch ($item->getType()) {
case 'Problem' :
self::showForProblem($item);
break;
default :
Problem::showListForItem($item, $withtemplate);
}
return true;
}
}