Skip to content

Commit

Permalink
feat(entityconfig): setting to define sort order
Browse files Browse the repository at this point in the history
Signed-off-by: Thierry Bugier <tbugier@teclib.com>
  • Loading branch information
btry committed Jul 16, 2020
1 parent 26e228d commit 180f6c2
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 16 deletions.
77 changes: 68 additions & 9 deletions inc/entityconfig.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,14 @@
class PluginFormcreatorEntityconfig extends CommonDBTM {

const CONFIG_PARENT = -2;

const CONFIG_GLPI_HELPDSK = 0;
const CONFIG_SIMPLIFIED_SERVICE_CATALOG = 1;
const CONFIG_EXTENDED_SERVICE_CATALOG = 2;

const CONFIG_SORT_POPULARITY = 0;
const CONFIG_SORT_ALPHABETICAL = 1;

/**
* @var bool $dohistory maintain history
*/
Expand All @@ -58,6 +63,23 @@ public function getTabNameForItem(CommonGLPI $item, $withtemplate = 0) {
return $tabNames;
}

public static function getEnumHelpdeskMode() {
return [
self::CONFIG_PARENT => __('Inheritance of the parent entity'),
self::CONFIG_GLPI_HELPDSK => __('GLPi\'s helpdesk', 'formcreator'),
self::CONFIG_SIMPLIFIED_SERVICE_CATALOG => __('Service catalog simplified', 'formcreator'),
self::CONFIG_EXTENDED_SERVICE_CATALOG => __('Service catalog extended', 'formcreator'),
];
}

public static function getEnumSort() {
return [
self::CONFIG_PARENT => __('Inheritance of the parent entity'),
self::CONFIG_SORT_POPULARITY => __('Popularity sort', 'formcreator'),
self::CONFIG_SORT_ALPHABETICAL => __('Alphabetic sort', 'formcreator'),
];
}

public static function displayTabContentForItem(CommonGLPI $item, $tabnum = 1, $withtemplate = 0) {
if ($item->getType() == 'Entity') {
$config = new self();
Expand Down Expand Up @@ -88,16 +110,10 @@ public function showFormForEntity(Entity $entity) {
echo "<table class='tab_cadre_fixe'>";
echo "<tr><th colspan='2'>".__('Helpdesk', 'formcreator')."</th></tr>";

if ($ID != 0) {
$elements = [
self::CONFIG_PARENT => __('Inheritance of the parent entity')
];
} else {
$elements = [];
$elements = self::getEnumHelpdeskMode();
if ($ID == 0) {
unset($elements[self::CONFIG_PARENT]);
}
$elements[0] = __('GLPi\'s helpdesk', 'formcreator');
$elements[1] = __('Service catalog simplified', 'formcreator');
$elements[2] = __('Service catalog extended', 'formcreator');

echo "<tr class='tab_bg_1'>";
echo "<td>".__('Helpdesk mode', 'formcreator')."</td>";
Expand All @@ -111,6 +127,23 @@ public function showFormForEntity(Entity $entity) {
}
echo '</td></tr>';

$elements = self::getEnumSort();
if ($ID == 0) {
unset($elements[self::CONFIG_PARENT]);
}

echo "<tr class='tab_bg_1'>";
echo "<td>".__('Sort order', 'formcreator')."</td>";
echo "<td>";
Dropdown::showFromArray('sort_order', $elements, ['value' => $this->fields['sort_order']]);
if ($this->fields['replace_helpdesk'] == self::CONFIG_PARENT) {
$tid = self::getUsedConfig('sort_order', $ID);
echo '<div class="green">';
echo $elements[$tid];
echo '</div>';
}
echo '</td></tr>';

if ($canedit) {
echo "<tr>";
echo "<td class='tab_bg_2 center' colspan='4'>";
Expand All @@ -125,6 +158,32 @@ public function showFormForEntity(Entity $entity) {
}

echo "</div>";

}
public function rawSearchOptions() {
$tab = [];

$tab[] = [
'id' => '3',
'table' => self::getTable(),
'name' => __('Helpdesk mode', 'formcreator'),
'field' => 'replace_helpdesk',
'datatype' => 'integer',
'nosearch' => true,
'massiveaction' => false,
];

$tab[] = [
'id' => '4',
'table' => self::getTable(),
'name' => __('Sort order', 'formcreator'),
'field' => 'sort_order',
'datatype' => 'integer',
'nosearch' => true,
'massiveaction' => false,
];

return $tab;
}

/**
Expand Down
12 changes: 8 additions & 4 deletions inc/form.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -795,14 +795,18 @@ public function showWizard($service_catalog = false) {
echo '<div id="plugin_formcreator_searchBar">';
$this->showSearchBar();
echo '</div>';
$sortSettings = PluginFormcreatorEntityConfig::getEnumSort();
echo '<div class="plugin_formcreator_sort">';
echo '<span class="radios">';
echo '<input type="radio" class="form-control" id="plugin_formcreator_mostPopular" name="sort" value="mostPopularSort" />';
echo '<label for="plugin_formcreator_mostPopular">'.__('Popularity sort', 'formcreator').'</label>';
$sortOrder = PluginFormcreatorEntityconfig::getUsedConfig('sort_order', $_SESSION['glpiactive_entity']);
$selected = $sortOrder == PluginFormcreatorEntityconfig::CONFIG_SORT_POPULARITY ? 'checked="checked"' : '';
echo '<input type="radio" class="form-control" id="plugin_formcreator_mostPopular" name="sort" value="mostPopularSort" '.$selected.'/>';
echo '<label for="plugin_formcreator_mostPopular">'.$sortSettings[PluginFormcreatorEntityConfig::CONFIG_SORT_POPULARITY] .'</label>';
echo '</span>';
echo '<span class="radios">';
echo '<input type="radio" class="form-control" id="plugin_formcreator_alphabetic" name="sort" value="alphabeticSort" />';
echo '<label for="plugin_formcreator_alphabetic">'.__('Alphabetic sort', 'formcreator').'</label>';
$selected = $sortOrder == PluginFormcreatorEntityconfig::CONFIG_SORT_ALPHABETICAL ? 'checked="checked"' : '';
echo '<input type="radio" class="form-control" id="plugin_formcreator_alphabetic" name="sort" value="alphabeticSort" '.$selected.'/>';
echo '<label for="plugin_formcreator_alphabetic">'.$sortSettings[PluginFormcreatorEntityConfig::CONFIG_SORT_ALPHABETICAL].'</label>';
echo '</span>';
echo '</div>';
echo '<div id="plugin_formcreator_wizard_forms">';
Expand Down
1 change: 1 addition & 0 deletions install/mysql/plugin_formcreator_empty.sql
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ CREATE TABLE IF NOT EXISTS `glpi_plugin_formcreator_categories` (
CREATE TABLE IF NOT EXISTS `glpi_plugin_formcreator_entityconfigs` (
`id` int(11) NOT NULL,
`replace_helpdesk` int(11) NOT NULL DEFAULT '0',
`sort_order` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

Expand Down
12 changes: 12 additions & 0 deletions install/upgrade_to_2.11.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,18 @@ public function upgrade(Migration $migration) {
$table = 'glpi_plugin_formcreator_targettickets';
$migration->addPostQuery("ALTER TABLE `$table` MODIFY `uuid` varchar(255) DEFAULT NULL AFTER `ola_question_ttr`");
$migration->migrationOneTable($table);

// sort setting in entityes
$table = 'glpi_plugin_formcreator_entityconfigs';
if (!$DB->fieldExists($table, 'sort_order')) {
// Write default settigns only if the columns must be created
$migration->addPostQuery("UPDATE `$table`
INNER JOIN `glpi_entities` ON (`$table`.`id` = `glpi_entities`.`id`)
SET `sort_order` = '-2'
WHERE `level` > '1'"
);
}
$migration->addField($table, 'sort_order', 'integer', ['after' => 'replace_helpdesk']);
}

/**
Expand Down
3 changes: 0 additions & 3 deletions js/scripts.js.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,6 @@ function (response) {
);
}
});

// Initialize sort controls
$('.plugin_formcreator_sort [value=mostPopularSort]')[0].checked = true;
}

// === Add better multi-select on form configuration validators ===
Expand Down
10 changes: 10 additions & 0 deletions tests/suite-unit/PluginFormcreatorEntityConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,14 @@ public function testGetUsedConfig() {
$output = $instance::getUsedConfig('replace_helpdesk', $entityId2);
$this->integer((int) $output)->isEqualTo(\PluginFormcreatorEntityconfig::CONFIG_SIMPLIFIED_SERVICE_CATALOG);
}

public function testgetEnumHelpdeskMode() {
$output = \PluginFormcreatorEntityconfig::getEnumHelpdeskMode();
$this->array($output)->isEqualTo([
\PluginFormcreatorEntityconfig::CONFIG_PARENT => __('Inheritance of the parent entity'),
\PluginFormcreatorEntityconfig::CONFIG_GLPI_HELPDSK => __('GLPi\'s helpdesk', 'formcreator'),
\PluginFormcreatorEntityconfig::CONFIG_SIMPLIFIED_SERVICE_CATALOG => __('Service catalog simplified', 'formcreator'),
\PluginFormcreatorEntityconfig::CONFIG_EXTENDED_SERVICE_CATALOG => __('Service catalog extended', 'formcreator'),
]);
}
}

0 comments on commit 180f6c2

Please sign in to comment.