Skip to content

Commit

Permalink
Add Item_Device* searchable lists (#4045)
Browse files Browse the repository at this point in the history
* Rework search options to add only one items link column
* fix label and class targeting
  • Loading branch information
cedric-anne authored and trasher committed May 24, 2018
1 parent 1f511e5 commit 2156ea4
Show file tree
Hide file tree
Showing 6 changed files with 203 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -16,6 +16,7 @@ The present file will list all changes made to the project; according to the
- `CommonITILObject::showMassiveSolutionForm()` now takes a `CommonITILObject` as argument
- `Profileuser::getUserProfiles` filter parameter is now an array
- Select2 javascript component has been upgraded to 4.0 version, see [Migrating from Select2 3.5](https://select2.org/upgrading/migrating-from-35)
- `CommonDevice::getItem_DeviceType` has a new optional `$devicetype` parameter

#### Deprecated

Expand Down
2 changes: 1 addition & 1 deletion front/item_device.common.form.php
Expand Up @@ -73,7 +73,7 @@
Html::back();

} else {
Html::header($item_device->getTypeName(Session::getPluralNumber()), '', "config", "commondevice", get_class($item_device));
Html::header($item_device->getTypeName(Session::getPluralNumber()), '', "config", "commondevice", $item_device->getDeviceType());

if (!isset($options)) {
$options = [];
Expand Down
52 changes: 52 additions & 0 deletions front/item_device.php
@@ -0,0 +1,52 @@
<?php
/**
* ---------------------------------------------------------------------
* GLPI - Gestionnaire Libre de Parc Informatique
* Copyright (C) 2015-2018 Teclib' and contributors.
*
* http://glpi-project.org
*
* based on GLPI - Gestionnaire Libre de Parc Informatique
* Copyright (C) 2003-2014 by the INDEPNET Development Team.
*
* ---------------------------------------------------------------------
*
* LICENSE
*
* This file is part of GLPI.
*
* GLPI is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* GLPI is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GLPI. If not, see <http://www.gnu.org/licenses/>.
* ---------------------------------------------------------------------
*/

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

if (!isset($_GET['itemtype']) || !class_exists($_GET['itemtype'])) {
throw new \RuntimeException(
'Missing or incorrect item device type called!'
);
}

$itemDevice = new $_GET['itemtype']();
if (!$itemDevice->canView()) {
Session::redirectIfNotLoggedIn();
Html::displayRightError();
}

Html::header($itemDevice->getTypeName(Session::getPluralNumber()), '', "config", "commondevice", $itemDevice->getDeviceType());

Search::show($_GET['itemtype']);

Html::footer();

16 changes: 14 additions & 2 deletions inc/commondevice.class.php
Expand Up @@ -74,12 +74,17 @@ static function getDeviceTypes() {
* This method can be override, for instance by the plugin
*
* @since 0.85
* @since 9.3 added the $devicetype parameter
*
* @param string $devicetype class name of device type, defaults to called class name
*
* @return array of the types of CommonDevice available
**/
static function getItem_DeviceType() {
static function getItem_DeviceType($devicetype = null) {

$devicetype = get_called_class();
if (null === $devicetype) {
$devicetype = get_called_class();
}
if ($plug = isPluginItemType($devicetype)) {
return 'Plugin'.$plug['plugin'].'Item_'.$plug['class'];
}
Expand Down Expand Up @@ -110,6 +115,13 @@ static function getMenuContent() {
if ($tmp->canCreate()) {
$menu['options'][$key]['links']['add'] = $tmp->getFormURL(false);
}
if ($itemClass = getItemForItemtype(self::getItem_DeviceType($key))) {
$itemTypeName = sprintf(__('%1$s items'), $key::getTypeName(1));

$listLabel = '<i class="fa fa-list pointer" title="' . $itemTypeName . '"></i>'
. '<span class="sr-only">' . $itemTypeName . '</span>';
$menu['options'][$key]['links'][$listLabel] = $itemClass->getSearchURL(false);
}
}
}
}
Expand Down
115 changes: 113 additions & 2 deletions inc/item_devices.class.php
Expand Up @@ -105,9 +105,62 @@ function getForbiddenStandardMassiveAction() {
return $forbidden;
}


function rawSearchOptions() {
$tab = parent::rawSearchOptions();
$tab = [];

$tab[] = [
'id' => 'common',
'name' => __('Characteristics')
];

$tab[] = [
'id' => '1',
'table' => $this->getTable(),
'field' => 'id',
'name' => __('ID'),
'datatype' => 'itemlink',
'massiveaction' => false,
];

$deviceType = $this->getDeviceType();
$tab[] = [
'id' => '4',
'table' => getTableForItemType($deviceType),
'field' => 'designation',
'name' => $deviceType::getTypeName(1),
'datatype' => 'itemlink',
'forcegroupby' => true,
'massiveaction' => false,
'joinparams' => [
'beforejoin' => [
'table' => $this->getTable(),
'joinparams' => [
'jointype' => 'child'
]
]
]
];

$tab[] = [
'id' => '5',
'table' => $this->getTable(),
'field' => 'items_id',
'name' => _n('Associated element', 'Associated elements', 2),
'datatype' => 'specific',
'comments' => true,
'nosort' => true,
'additionalfields' => ['itemtype']
];

$tab[] = [
'id' => '6',
'table' => $this->getTable(),
'field' => 'itemtype',
'name' => _n('Associated item type', 'Associated item types', 2),
'datatype' => 'itemtypename',
'itemtype_list' => 'itemdevices_types',
'nosort' => true
];

foreach (static::getSpecificities() as $field => $attributs) {
switch ($field) {
Expand Down Expand Up @@ -152,6 +205,55 @@ function rawSearchOptions() {
return $tab;
}

static function getSpecificValueToDisplay($field, $values, array $options = []) {

if (!is_array($values)) {
$values = [$field => $values];
}
switch ($field) {
case 'items_id':
if (isset($values['itemtype'])) {
if (isset($options['comments']) && $options['comments']) {
$valueData = Dropdown::getDropdownName(
getTableForItemtype($values['itemtype']),
$values[$field],
1
);
return sprintf(
__('%1$s %2$s'),
$valueData['name'],
Html::showToolTip($valueData['comment'], ['display' => false])
);

}
return Dropdown::getDropdownName(
getTableForItemtype($values['itemtype']),
$values[$field]
);
}
break;
}
return parent::getSpecificValueToDisplay($field, $values, $options);
}

static function getSpecificValueToSelect($field, $name = '', $values = '', array $options = []) {

if (!is_array($values)) {
$values = [$field => $values];
}
$options['display'] = false;
switch ($field) {
case 'items_id' :
if (isset($values['itemtype']) && !empty($values['itemtype'])) {
$options['name'] = $name;
$options['value'] = $values[$field];
return Dropdown::show($values['itemtype'], $options);
}
break;
}
return parent::getSpecificValueToSelect($field, $name, $values, $options);
}


/**
* Get the specificities of the given device. For instance, the
Expand Down Expand Up @@ -1266,4 +1368,13 @@ static public function unsetUndisclosedFields(&$fields) {
}
}

static function getSearchURL($full = true) {
global $CFG_GLPI;

$dir = ($full ? $CFG_GLPI['root_doc'] : '');
$itemtype = get_called_class();
$link = "$dir/front/item_device.php?itemtype=$itemtype";

return $link;
}
}
22 changes: 22 additions & 0 deletions install/update_92_93.php
Expand Up @@ -736,6 +736,28 @@ function update92to93() {
);
/** /Migrate computerdisks to items_disks */

/** Add Item_Device* display preferences */
$itemDeviceTypes = Item_Devices::getDeviceTypes();
foreach ($itemDeviceTypes as $itemDeviceType) {
$optToAdd = [];

// Serial number
$itemDeviceSpecificities = $itemDeviceType::getSpecificities();
if (array_key_exists('serial', $itemDeviceSpecificities)) {
$optToAdd[] = $itemDeviceSpecificities['serial']['id'];
}

// Parent device.
$optToAdd[] = 4;
// Associated item.
$optToAdd[] = 5;
// Associated itemtype.
$optToAdd[] = 6;

$ADDTODISPLAYPREF[$itemDeviceType] = $optToAdd;
}
/** /Add Item_Device* display preferences */

foreach ($ADDTODISPLAYPREF as $type => $tab) {
$rank = 1;
foreach ($tab as $newval) {
Expand Down

0 comments on commit 2156ea4

Please sign in to comment.