Skip to content

Commit

Permalink
add some clonable dropdowns
Browse files Browse the repository at this point in the history
  • Loading branch information
cconard96 committed Dec 19, 2023
1 parent be4c36b commit 946c22b
Show file tree
Hide file tree
Showing 12 changed files with 147 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/AbstractITILChildTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

use Glpi\ContentTemplates\ParametersPreset;
use Glpi\ContentTemplates\TemplateManager;
use Glpi\Features\Clonable;

/**
* Base template class
Expand All @@ -43,6 +44,8 @@
*/
abstract class AbstractITILChildTemplate extends CommonDropdown
{
use Clonable;

public function showForm($ID, array $options = [])
{
if (!parent::showForm($ID, $options)) {
Expand Down Expand Up @@ -149,4 +152,9 @@ public function getRenderedContent(CommonITILObject $itil_item): string

return $html;
}

public function getCloneRelations(): array
{
return [];
}
}
8 changes: 8 additions & 0 deletions src/Blacklist.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,16 @@
* ---------------------------------------------------------------------
*/

use Glpi\Features\Clonable;

/**
* Blacklist Class
*
* @since 0.84
**/
class Blacklist extends CommonDropdown
{
use Clonable;
// From CommonDBTM
public $dohistory = true;

Expand Down Expand Up @@ -561,4 +564,9 @@ public static function getIcon()
{
return "fas fa-ban";
}

public function getCloneRelations(): array
{
return [];
}
}
8 changes: 8 additions & 0 deletions src/BlacklistedMailContent.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,16 @@
* ---------------------------------------------------------------------
*/

use Glpi\Features\Clonable;

/**
* BlacklistedMailContent Class
*
* @since 0.85
**/
class BlacklistedMailContent extends CommonDropdown
{
use Clonable;
// From CommonDBTM
public $dohistory = false;

Expand Down Expand Up @@ -100,4 +103,9 @@ public static function getIcon()
{
return "fas fa-envelope-square";
}

public function getCloneRelations(): array
{
return [];
}
}
7 changes: 7 additions & 0 deletions src/CommonDCModelDropdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,13 @@

use Glpi\DBAL\QuerySubQuery;
use Glpi\Features\AssetImage;
use Glpi\Features\Clonable;

/// CommonDCModelDropdown class - dropdown for datacenter items models
abstract class CommonDCModelDropdown extends CommonDropdown
{
use AssetImage;
use Clonable;

public $additional_fields_for_dictionnary = ['manufacturer'];

Expand Down Expand Up @@ -479,4 +481,9 @@ public static function getIcon()
$device_class = str_replace('Model', '', $model_class);
return $device_class::getIcon();
}

public function getCloneRelations(): array
{
return [];
}
}
9 changes: 9 additions & 0 deletions src/CommonDeviceModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,13 @@
* ---------------------------------------------------------------------
*/

use Glpi\Features\Clonable;

/// Class DeviceBatteryModel
abstract class CommonDeviceModel extends CommonDropdown
{
use Clonable;

public static function getTypeName($nb = 0)
{
return _n('Device model', 'Device models', $nb);
Expand Down Expand Up @@ -71,4 +75,9 @@ public static function getIcon()
$device_class = str_replace('Model', '', $model_class);
return $device_class::getIcon();
}

public function getCloneRelations(): array
{
return [];
}
}
9 changes: 9 additions & 0 deletions src/CommonType.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,12 @@
* ---------------------------------------------------------------------
*/

use Glpi\Features\Clonable;

abstract class CommonType extends CommonDropdown
{
use Clonable;

public static function getFieldLabel()
{
return _n('Type', 'Types', 1);
Expand All @@ -46,4 +50,9 @@ public static function getIcon()
$device_class = str_replace('Type', '', $type_class);
return $device_class::getIcon();
}

public function getCloneRelations(): array
{
return [];
}
}
9 changes: 9 additions & 0 deletions src/ITILCategory.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,15 @@
* ---------------------------------------------------------------------
*/

use Glpi\Features\Clonable;

/**
* ITILCategory class
**/
class ITILCategory extends CommonTreeDropdown
{
use Clonable;

// From CommonDBTM
public $dohistory = true;
public $can_be_translated = true;
Expand Down Expand Up @@ -527,4 +531,9 @@ public static function showForITILTemplate(ITILTemplate $tt, $withtemplate = 0)

echo "</table></div>";
}

public function getCloneRelations(): array
{
return [];
}
}
7 changes: 7 additions & 0 deletions src/Location.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@

use Glpi\DBAL\QueryExpression;
use Glpi\DBAL\QueryUnion;
use Glpi\Features\Clonable;
use Glpi\Socket;

/// Location class
class Location extends CommonTreeDropdown
{
use MapGeolocation;
use Clonable;

// From CommonDBTM
public $dohistory = true;
Expand Down Expand Up @@ -609,4 +611,9 @@ public function prepareInputForAdd($input)
}
return $input;
}

public function getCloneRelations(): array
{
return [];
}
}
8 changes: 8 additions & 0 deletions src/Manufacturer.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,13 @@
* ---------------------------------------------------------------------
*/

use Glpi\Features\Clonable;

/// Class Manufacturer
/// @todo study if we should integrate getHTMLTableHeader and getHTMLTableCellsForItem ...
class Manufacturer extends CommonDropdown
{
use Clonable;
public $can_be_translated = false;


Expand Down Expand Up @@ -225,4 +228,9 @@ public static function getHTMLTableCellsForItem(
);
}
}

public function getCloneRelations(): array
{
return [];
}
}
7 changes: 7 additions & 0 deletions src/State.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@

use Glpi\DBAL\QueryExpression;
use Glpi\DBAL\QuerySubQuery;
use Glpi\Features\Clonable;

/**
* State Class
**/
class State extends CommonTreeDropdown
{
use Clonable;
public $can_be_translated = true;

public static $rightname = 'state';
Expand Down Expand Up @@ -649,4 +651,9 @@ public static function getDisplayConditionForAssistance(): array
]
];
}

public function getCloneRelations(): array
{
return [];
}
}
9 changes: 9 additions & 0 deletions src/TaskCategory.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,15 @@
* ---------------------------------------------------------------------
*/

use Glpi\Features\Clonable;

/**
* TaskCategory class
**/
class TaskCategory extends CommonTreeDropdown
{
use Clonable;

// From CommonDBTM
public $dohistory = true;
public $can_be_translated = true;
Expand Down Expand Up @@ -89,4 +93,9 @@ public static function getIcon()
{
return "fas fa-tags";
}

public function getCloneRelations(): array
{
return [];
}
}
58 changes: 58 additions & 0 deletions tests/functional/Dropdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -1866,4 +1866,62 @@ public function testDropdownNumber(array $params, array $expected): void
$this->variable($dropdown_entry['id'])->isEqualTo($expected[$key]);
}
}

protected function cloneProvider()
{
$this->login();
$dropdowns = \Dropdown::getStandardDropdownItemTypes();
foreach ($dropdowns as $items) {
foreach ($items as $item => $n) {
if (is_subclass_of($item, \CommonDropdown::class) && \Toolbox::hasTrait($item, \Glpi\Features\Clonable::class)) {
yield [$item];
}
}
}
}

/**
* @dataProvider cloneProvider
*/
public function testClone($dropdown_class)
{
$this->login();

/** @var \CommonDropdown $item */
$item = new $dropdown_class();

$extra_fields = $item->getAdditionalFields();
$input = [
'name' => __FUNCTION__
];
$parent_id = null;
foreach ($extra_fields as $field) {
if (!isset($field['type'])) {
continue;
}
if ($field['type'] === 'parent' && $parent_id === null) {
$this->integer($parent_id = $item->add([
'name' => __FUNCTION__ . '_parent'
]))->isGreaterThan(0);
}
$value = match ($field['type']) {
'text' => $field['name'],
'bool' => 1,
'tinymce' => '<p>' . $field['name'] . '</p>',
'parent' => $parent_id,
default => null
};
if ($value !== null && isset($field['name']) && is_string($field['name'])) {
$input[$field['name']] = $value;
}
}
if ($dropdown_class === \NetworkName::class) {
$input['itemtype'] = 'Computer';
$input['items_id'] = 1;
}
$this->integer($item->add($input))->isGreaterThan(0);
foreach ($input as $field => $value) {
$this->variable($item->fields[$field])->isEqualTo($value);
}
}
}

0 comments on commit 946c22b

Please sign in to comment.