Skip to content

Commit 45e43e3

Browse files
authored
feat(form): show all forms or only default forms on virtual root category (#2644)
1 parent 2c5e647 commit 45e43e3

File tree

6 files changed

+88
-25
lines changed

6 files changed

+88
-25
lines changed

ajax/homepage_wizard.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ function plugin_formcreator_showWizardCategories() {
5858
}
5959

6060
function plugin_formcreator_showWizardForms($rootCategory = 0, $keywords = '', $helpdeskHome = false) {
61-
$form = PluginFormcreatorCommon::getForm();
62-
$formList = $form->showFormList($rootCategory, $keywords, $helpdeskHome);
61+
$formList = PluginFormcreatorForm::getFormList($rootCategory, $keywords, $helpdeskHome);
6362
echo json_encode($formList, JSON_UNESCAPED_SLASHES);
6463
}

inc/entityconfig.class.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ class PluginFormcreatorEntityconfig extends CommonDBTM {
4545
const CONFIG_SIMPLIFIED_SERVICE_CATALOG = 1;
4646
const CONFIG_EXTENDED_SERVICE_CATALOG = 2;
4747

48+
const CONFIG_DEFAULT_FORM_LIST_ALL = 0;
49+
const CONFIG_DEFAULT_FORM_LIST_DEFAULT = 1;
50+
4851
const CONFIG_SORT_POPULARITY = 0;
4952
const CONFIG_SORT_ALPHABETICAL = 1;
5053

@@ -84,6 +87,14 @@ public static function getEnumHelpdeskMode() : array {
8487
];
8588
}
8689

90+
public static function getEnumDefaultFormList(): array {
91+
return [
92+
self::CONFIG_PARENT => __('Inheritance of the parent entity'),
93+
self::CONFIG_DEFAULT_FORM_LIST_ALL => __('All available forms', 'formcreator'),
94+
self::CONFIG_DEFAULT_FORM_LIST_DEFAULT => __('Only default forms', 'formcreator'),
95+
];
96+
}
97+
8798
public static function getEnumSort() : array {
8899
return [
89100
self::CONFIG_PARENT => __('Inheritance of the parent entity'),
@@ -210,6 +221,20 @@ public function showFormForEntity(Entity $entity) {
210221
}
211222
echo '</td></tr>';
212223

224+
echo "<tr class='tab_bg_1'>";
225+
echo "<td>".__('Default Form list mode', 'formcreator')."</td>";
226+
echo "<td>";
227+
$elements = self::getEnumDefaultFormList();
228+
$options = ['value' => $this->fields['default_form_list_mode']];
229+
$options['no_parent'] = ($ID == 0);
230+
self::dropdownDefaultFormList('default_form_list_mode', $options);
231+
if (!$options['no_parent'] && $this->fields['default_form_list_mode'] == self::CONFIG_PARENT) {
232+
$tid = self::getUsedConfig('default_form_list_mode', $ID);
233+
echo '<br>';
234+
Entity::inheritedValue($elements[$tid], true);
235+
}
236+
echo '</td></tr>';
237+
213238
$elements = self::getEnumSort();
214239
if ($ID == 0) {
215240
unset($elements[self::CONFIG_PARENT]);
@@ -427,4 +452,19 @@ static function getUsedConfig($fieldref, $entities_id, $fieldval = '', $default_
427452

428453
return $default_value;
429454
}
455+
456+
/**
457+
* Show a dropdown to select default form list mode
458+
*
459+
* @param string $name name if the input field
460+
* @param array $options options
461+
* @return void
462+
*/
463+
public static function dropdownDefaultFormList(string $name, array $options = []): void {
464+
$items = self::getEnumDefaultFormList();
465+
if (isset($options['no_parent']) && $options['no_parent']) {
466+
unset($items[self::CONFIG_PARENT]);
467+
}
468+
Dropdown::showFromArray($name, $items, $options);
469+
}
430470
}

inc/form.class.php

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -655,25 +655,28 @@ public function showWizard() : void {
655655
$this->showSearchBar();
656656
echo '</div>';
657657
}
658-
$sortSettings = PluginFormcreatorEntityConfig::getEnumSort();
658+
$sort_settings = PluginFormcreatorEntityConfig::getEnumSort();
659659
echo '<div class="plugin_formcreator_sort">';
660660
echo '<span class="radios">';
661-
$sortOrder = PluginFormcreatorEntityconfig::getUsedConfig('sort_order', Session::getActiveEntity());
662-
$selected = $sortOrder == PluginFormcreatorEntityconfig::CONFIG_SORT_POPULARITY ? 'checked="checked"' : '';
661+
$sort_order = PluginFormcreatorEntityconfig::getUsedConfig('sort_order', Session::getActiveEntity());
662+
$selected = $sort_order == PluginFormcreatorEntityconfig::CONFIG_SORT_POPULARITY ? 'checked="checked"' : '';
663663
echo '<input type="radio" class="-check-input" id="plugin_formcreator_mostPopular" name="sort" value="mostPopularSort" '.$selected.' onclick="showTiles(tiles)"/>';
664-
echo '<label for="plugin_formcreator_mostPopular">&nbsp;'.$sortSettings[PluginFormcreatorEntityConfig::CONFIG_SORT_POPULARITY] .'</label>';
664+
echo '<label for="plugin_formcreator_mostPopular">&nbsp;'.$sort_settings[PluginFormcreatorEntityConfig::CONFIG_SORT_POPULARITY] .'</label>';
665665
echo '</span>';
666666
echo '&nbsp;';
667667
echo '<span class="radios">';
668-
$selected = $sortOrder == PluginFormcreatorEntityconfig::CONFIG_SORT_ALPHABETICAL ? 'checked="checked"' : '';
668+
$selected = $sort_order == PluginFormcreatorEntityconfig::CONFIG_SORT_ALPHABETICAL ? 'checked="checked"' : '';
669669
echo '<input type="radio" class="-check-input" id="plugin_formcreator_alphabetic" name="sort" value="alphabeticSort" '.$selected.' onclick="showTiles(tiles)"/>';
670-
echo '<label for="plugin_formcreator_alphabetic">&nbsp;'.$sortSettings[PluginFormcreatorEntityConfig::CONFIG_SORT_ALPHABETICAL].'</label>';
670+
echo '<label for="plugin_formcreator_alphabetic">&nbsp;'.$sort_settings[PluginFormcreatorEntityConfig::CONFIG_SORT_ALPHABETICAL].'</label>';
671671
echo '</span>';
672672
echo '</div>';
673673
echo '<div id="plugin_formcreator_wizard_forms">';
674674
echo '</div>';
675675
echo '</div>';
676676
echo '</div>';
677+
echo Html::scriptblock("$(function() {
678+
plugin_formcreator.updateWizardFormsView();
679+
});");
677680
}
678681

679682
/**
@@ -683,7 +686,7 @@ public function showWizard() : void {
683686
* @param bool $helpdeskHome show items for helpdesk only
684687
* @return array
685688
*/
686-
public function showFormList(int $rootCategory = 0, string $keywords = '', bool $helpdeskHome = false) : array {
689+
public static function getFormList(int $rootCategory = 0, string $keywords = '', bool $helpdeskHome = false): array {
687690
global $DB, $TRANSLATE;
688691

689692
$table_cat = getTableForItemType(PluginFormcreatorCategory::class);
@@ -723,8 +726,15 @@ public function showFormList(int $rootCategory = 0, string $keywords = '', bool
723726

724727
$selectedCategories = [];
725728
if ($rootCategory != 0) {
729+
// The user choosed a category
726730
$selectedCategories = getSonsOf($table_cat, $rootCategory);
727731
$where_form['WHERE']['AND']["$table_form.$categoryFk"] = $selectedCategories;
732+
} else {
733+
// Searching for all forms without category restriction
734+
$onlyDefault = PluginFormcreatorEntityconfig::getUsedConfig('default_form_list_mode', Session::getActiveEntity());
735+
if ($onlyDefault == PluginFormcreatorEntityconfig::CONFIG_DEFAULT_FORM_LIST_DEFAULT) {
736+
$where_form['WHERE']['is_default'] = 1;
737+
}
728738
}
729739

730740
$where_form['GROUPBY'] = [

install/mysql/plugin_formcreator_empty.sql

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,16 @@ CREATE TABLE IF NOT EXISTS `glpi_plugin_formcreator_categories` (
2828
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
2929

3030
CREATE TABLE IF NOT EXISTS `glpi_plugin_formcreator_entityconfigs` (
31-
`id` int unsigned NOT NULL AUTO_INCREMENT,
32-
`entities_id` int unsigned NOT NULL DEFAULT '0',
33-
`replace_helpdesk` int(11) NOT NULL DEFAULT '-2',
34-
`sort_order` int(11) NOT NULL DEFAULT '-2',
35-
`is_kb_separated` int(11) NOT NULL DEFAULT '-2',
36-
`is_search_visible` int(11) NOT NULL DEFAULT '-2',
37-
`is_dashboard_visible` int(11) NOT NULL DEFAULT '-2',
38-
`is_header_visible` int(11) NOT NULL DEFAULT '-2',
39-
`header` text,
31+
`id` int unsigned NOT NULL AUTO_INCREMENT,
32+
`entities_id` int unsigned NOT NULL DEFAULT '0',
33+
`replace_helpdesk` int(11) NOT NULL DEFAULT '-2',
34+
`default_form_list_mode` int(11) NOT NULL DEFAULT '-2',
35+
`sort_order` int(11) NOT NULL DEFAULT '-2',
36+
`is_kb_separated` int(11) NOT NULL DEFAULT '-2',
37+
`is_search_visible` int(11) NOT NULL DEFAULT '-2',
38+
`is_dashboard_visible` int(11) NOT NULL DEFAULT '-2',
39+
`is_header_visible` int(11) NOT NULL DEFAULT '-2',
40+
`header` text,
4041
PRIMARY KEY (`id`),
4142
UNIQUE KEY `unicity` (`entities_id`)
4243
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

install/upgrade_to_2.13.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class PluginFormcreatorUpgradeTo2_13 {
3838
public function upgrade(Migration $migration) {
3939
$this->migration = $migration;
4040
$this->migrateEntityConfig();
41+
$this->addDefaultFormListMode();
4142
$this->fixRootEntityConfig();
4243
$this->migrateFkToUnsignedInt();
4344
$this->addFormAnswerTitle();
@@ -46,7 +47,7 @@ public function upgrade(Migration $migration) {
4647
$this->addTargetValidationSetting();
4748
$this->addFormVisibility();
4849
$this->addDashboardVisibility();
49-
$this->addRequestSourceSeting();
50+
$this->addRequestSourceSetting();
5051
}
5152

5253
public function addFormAnswerTitle() {
@@ -159,12 +160,13 @@ protected function migrateEntityConfig() {
159160
*
160161
* @return void
161162
*/
162-
private function fixRootEntityConfig() {
163+
private function fixRootEntityConfig(): void {
163164
global $DB;
164165

165166
$table = 'glpi_plugin_formcreator_entityconfigs';
166167
$DB->update($table, [
167168
'replace_helpdesk' => new QueryExpression("IF(`replace_helpdesk` = -2, 0, `replace_helpdesk`)"),
169+
'default_form_list_mode' => new QueryExpression("IF(`default_form_list_mode` = -2, 0, `default_form_list_mode`)"),
168170
'sort_order' => new QueryExpression("IF(`sort_order` = -2, 0, `sort_order`)"),
169171
'is_kb_separated' => new QueryExpression("IF(`is_kb_separated` = -2, 0, `is_kb_separated`)"),
170172
'is_search_visible' => new QueryExpression("IF(`is_search_visible` = -2, 1, `is_search_visible`)"),
@@ -276,7 +278,7 @@ protected function migrateFkToUnsignedInt() {
276278
$this->migration->changeField($table, 'id', 'id', 'int ' . DBConnection::getDefaultPrimaryKeySignOption() . ' not null auto_increment');
277279
}
278280

279-
public function addRequestSourceSeting(): void {
281+
public function addRequestSourceSetting(): void {
280282
global $DB;
281283

282284
$table = 'glpi_plugin_formcreator_targettickets';
@@ -289,4 +291,13 @@ public function addRequestSourceSeting(): void {
289291
$DB->queryOrDie("UPDATE `$table` SET `source_rule` = '1', `source_question` = '$formcreatorSourceId'");
290292
}
291293
}
294+
295+
public function addDefaultFormListMode() {
296+
$table = 'glpi_plugin_formcreator_entityconfigs';
297+
298+
$this->migration->addField($table, 'default_form_list_mode', 'int not null default -2', ['after' => 'replace_helpdesk']);
299+
$this->migration->migrationOneTable($table);
300+
301+
$this->migration->addPostQuery("UPDATE `glpi_plugin_formcreator_entityconfigs` SET `default_form_list_mode`=0 WHERE `id`=0");
302+
}
292303
}

js/scripts.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,8 @@ $(function() {
117117
if (searchInput.val().length > 0) {
118118
searchInput.val('');
119119
plugin_formcreator.updateWizardFormsView(null);
120-
$.when(getFormAndFaqItems(0)).then(
120+
$.when(getFormAndFaqItems(0))
121+
.then(
121122
function (response) {
122123
tiles = response;
123124
showTiles(tiles.forms);
@@ -263,7 +264,7 @@ function sortFormAndFaqItems(items, byName) {
263264
return -1;
264265
}
265266
if (a.name > b.name) {
266-
return 1
267+
return 1;
267268
}
268269
return 0;
269270
});
@@ -274,7 +275,7 @@ function sortFormAndFaqItems(items, byName) {
274275
return -1;
275276
}
276277
if (a.usage_count < b.usage_count) {
277-
return 1
278+
return 1;
278279
}
279280
return 0;
280281
});
@@ -1195,7 +1196,8 @@ var plugin_formcreator = new function() {
11951196
if (item) {
11961197
this.activeCategory = item.getAttribute('data-category-id');
11971198
}
1198-
$.when(getFormAndFaqItems(this.activeCategory)).done(
1199+
$.when(getFormAndFaqItems(this.activeCategory))
1200+
.done(
11991201
function (response) {
12001202
tiles = response.forms;
12011203
showTiles(tiles, response.default);

0 commit comments

Comments
 (0)