Skip to content

Commit 75106b6

Browse files
committed
feat(form_valodator): multiple validation level
Signed-off-by: Thierry Bugier <tbugier@teclib.com>
1 parent bd9b865 commit 75106b6

File tree

7 files changed

+595
-245
lines changed

7 files changed

+595
-245
lines changed

front/form_validator.form.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
/**
3+
* ---------------------------------------------------------------------
4+
* Formcreator is a plugin which allows creation of custom forms of
5+
* easy access.
6+
* ---------------------------------------------------------------------
7+
* LICENSE
8+
*
9+
* This file is part of Formcreator.
10+
*
11+
* Formcreator is free software; you can redistribute it and/or modify
12+
* it under the terms of the GNU General Public License as published by
13+
* the Free Software Foundation; either version 2 of the License, or
14+
* (at your option) any later version.
15+
*
16+
* Formcreator is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
* GNU General Public License for more details.
20+
*
21+
* You should have received a copy of the GNU General Public License
22+
* along with Formcreator. If not, see <http://www.gnu.org/licenses/>.
23+
* ---------------------------------------------------------------------
24+
* @copyright Copyright © 2011 - 2021 Teclib'
25+
* @license http://www.gnu.org/licenses/gpl.txt GPLv3+
26+
* @link https://github.com/pluginsGLPI/formcreator/
27+
* @link https://pluginsglpi.github.io/formcreator/
28+
* @link http://plugins.glpi-project.org/#/plugin/formcreator
29+
* ---------------------------------------------------------------------
30+
*/
31+
32+
include ('../../../inc/includes.php');
33+
34+
Session::checkRight('entity', UPDATE);
35+
36+
// Check if plugin is activated...
37+
if (!(new Plugin())->isActivated('formcreator')) {
38+
Html::displayNotFoundError();
39+
}
40+
41+
if (!isset($_POST['plugin_formcreator_forms_id'])) {
42+
// should not happen
43+
}
44+
$formId = (int) $_POST['plugin_formcreator_forms_id'];
45+
$formValidator = new PluginFormcreatorForm_Validator();
46+
$form = new PluginFormcreatorForm();
47+
if (isset($_POST['add'])) {
48+
// Add a new Form
49+
Session::checkRight('entity', UPDATE);
50+
$formValidator->addMultipleItems($_POST);
51+
Html::redirect($form->getFormURLWithID($formId));
52+
}

inc/form.class.php

Lines changed: 2 additions & 197 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,6 @@ public function canPurgeItem() {
9494
return Session::haveRight('entity', UPDATE);
9595
}
9696

97-
/**
98-
* Returns the type name with consideration of plural
99-
*
100-
* @param number $nb Number of item(s)
101-
* @return string Itemtype name
102-
*/
10397
public static function getTypeName($nb = 0) {
10498
return _n('Form', 'Forms', $nb, 'formcreator');
10599
}
@@ -403,8 +397,6 @@ public static function getSpecificValueToDisplay($field, $values, array $options
403397
* @return NULL Nothing, just display the form
404398
*/
405399
public function showForm($ID, $options = []) {
406-
global $DB;
407-
408400
$this->initForm($ID, $options);
409401
$this->showFormHeader($options);
410402

@@ -472,163 +464,6 @@ public function showForm($ID, $options = []) {
472464
echo '</td>';
473465
echo '</tr>';
474466

475-
echo '<tr class="tab_bg_2">';
476-
echo '<td>' . __('Need to be validate?', 'formcreator') . '</td>';
477-
echo '<td class="validators_bloc">';
478-
479-
Dropdown::showFromArray('validation_required', [
480-
self::VALIDATION_NONE => Dropdown::EMPTY_VALUE,
481-
self::VALIDATION_USER => User::getTypeName(1),
482-
self::VALIDATION_GROUP => Group::getTypeName(1),
483-
], [
484-
'value' => $this->fields['validation_required'],
485-
'on_change' => 'plugin_formcreator_changeValidators(this.value)'
486-
]);
487-
echo '</td>';
488-
echo '<td colspan="2">';
489-
// Select all users with ticket validation right and the groups
490-
$userTable = User::getTable();
491-
$userFk = User::getForeignKeyField();
492-
$groupTable = Group::getTable();
493-
$groupFk = Group::getForeignKeyField();
494-
$profileUserTable = Profile_User::getTable();
495-
$profileTable = Profile::getTable();
496-
$profileFk = Profile::getForeignKeyField();
497-
$profileRightTable = ProfileRight::getTable();
498-
$groupUserTable = Group_User::getTable();
499-
$subQuery = [
500-
'SELECT' => "$profileUserTable.$userFk",
501-
'FROM' => $profileUserTable,
502-
'INNER JOIN' => [
503-
$profileTable => [
504-
'FKEY' => [
505-
$profileTable => 'id',
506-
$profileUserTable => $profileFk,
507-
]
508-
],
509-
$profileRightTable =>[
510-
'FKEY' => [
511-
$profileTable => 'id',
512-
$profileRightTable => $profileFk,
513-
]
514-
],
515-
],
516-
'WHERE' => [
517-
"$profileRightTable.name" => "ticketvalidation",
518-
[
519-
'OR' => [
520-
"$profileRightTable.rights" => ['&', TicketValidation::VALIDATEREQUEST],
521-
"$profileRightTable.rights" => ['&', TicketValidation::VALIDATEINCIDENT],
522-
],
523-
],
524-
"$userTable.is_active" => '1',
525-
],
526-
];
527-
$usersCondition = [
528-
"$userTable.id" => new QuerySubquery($subQuery)
529-
];
530-
$formValidator = new PluginFormcreatorForm_Validator();
531-
$selectedValidatorUsers = [];
532-
foreach ($formValidator->getValidatorsForForm($this, User::class) as $user) {
533-
$selectedValidatorUsers[$user->getID()] = $user->getID();
534-
}
535-
$users = $DB->request([
536-
'SELECT' => ['id', 'name'],
537-
'FROM' => User::getTable(),
538-
'WHERE' => $usersCondition,
539-
]);
540-
$validatorUsers = [];
541-
foreach ($users as $user) {
542-
$validatorUsers[$user['id']] = $user['name'];
543-
}
544-
echo '<div id="validators_users">';
545-
echo User::getTypeName() . '&nbsp';
546-
Dropdown::showFromArray(
547-
'_validator_users',
548-
$validatorUsers, [
549-
'multiple' => true,
550-
'values' => $selectedValidatorUsers
551-
]
552-
);
553-
echo '</div>';
554-
555-
// Validators groups
556-
$subQuery = [
557-
'SELECT' => "$groupUserTable.$groupFk",
558-
'FROM' => $groupUserTable,
559-
'INNER JOIN' => [
560-
$userTable => [
561-
'FKEY' => [
562-
$groupUserTable => $userFk,
563-
$userTable => 'id',
564-
]
565-
],
566-
$profileUserTable => [
567-
'FKEY' => [
568-
$profileUserTable => $userFk,
569-
$userTable => 'id',
570-
],
571-
],
572-
$profileTable => [
573-
'FKEY' => [
574-
$profileTable => 'id',
575-
$profileUserTable => $profileFk,
576-
]
577-
],
578-
$profileRightTable =>[
579-
'FKEY' => [
580-
$profileTable => 'id',
581-
$profileRightTable => $profileFk,
582-
]
583-
],
584-
],
585-
'WHERE' => [
586-
"$groupUserTable.$userFk" => new QueryExpression("`$userTable`.`id`"),
587-
"$profileRightTable.name" => "ticketvalidation",
588-
[
589-
'OR' => [
590-
"$profileRightTable.rights" => ['&', TicketValidation::VALIDATEREQUEST],
591-
"$profileRightTable.rights" => ['&', TicketValidation::VALIDATEINCIDENT],
592-
],
593-
],
594-
"$userTable.is_active" => '1',
595-
],
596-
];
597-
$groupsCondition = [
598-
"$groupTable.id" => new QuerySubquery($subQuery),
599-
];
600-
$groups = $DB->request([
601-
'SELECT' => ['id' ,'name'],
602-
'FROM' => Group::getTable(),
603-
'WHERE' => $groupsCondition,
604-
]);
605-
$formValidator = new PluginFormcreatorForm_Validator();
606-
$selectecValidatorGroups = [];
607-
foreach ($formValidator->getValidatorsForForm($this, Group::class) as $group) {
608-
$selectecValidatorGroups[$group->getID()] = $group->getID();
609-
}
610-
$validatorGroups = [];
611-
foreach ($groups as $group) {
612-
$validatorGroups[$group['id']] = $group['name'];
613-
}
614-
echo '<div id="validators_groups" style="width: 100%">';
615-
echo Group::getTypeName() . '&nbsp';
616-
Dropdown::showFromArray(
617-
'_validator_groups',
618-
$validatorGroups,
619-
[
620-
'multiple' => true,
621-
'values' => $selectecValidatorGroups
622-
]
623-
);
624-
echo '</div>';
625-
626-
$script = '$(document).ready(function() {plugin_formcreator_changeValidators(' . $this->fields["validation_required"] . ');});';
627-
echo Html::scriptBlock($script);
628-
629-
echo '</td>';
630-
echo '</tr>';
631-
632467
echo '<tr>';
633468
echo '<td>'.__('Default form in service catalog', 'formcreator').'</td>';
634469
echo '<td>';
@@ -762,6 +597,7 @@ public static function displayTabContentForItem(CommonGLPI $item, $tabnum = 1, $
762597
public function defineTabs($options = []) {
763598
$ong = [];
764599
$this->addDefaultFormTab($ong);
600+
$this->addStandardTab(PluginFormcreatorForm_Validator::class, $ong, $options);
765601
$this->addStandardTab(PluginFormcreatorQuestion::class, $ong, $options);
766602
$this->addStandardTab(PluginFormcreatorForm_Profile::class, $ong, $options);
767603
$this->addStandardTab(__CLASS__, $ong, $options);
@@ -1336,38 +1172,7 @@ public function displayUserForm() : void {
13361172

13371173
// Show validator selector
13381174
if ($this->fields['validation_required'] != PluginFormcreatorForm_Validator::VALIDATION_NONE) {
1339-
$validators = [];
1340-
$formValidator = new PluginFormcreatorForm_Validator();
1341-
switch ($this->fields['validation_required']) {
1342-
case PluginFormcreatorForm_Validator::VALIDATION_GROUP:
1343-
$validatorType = Group::class;
1344-
$result = $formValidator->getValidatorsForForm($this, $validatorType);
1345-
foreach ($result as $validator) {
1346-
$validators[$validator->getID()] = $validator->fields['completename'];
1347-
}
1348-
break;
1349-
case PluginFormcreatorForm_Validator::VALIDATION_USER:
1350-
$validatorType = User::class;
1351-
$result = $formValidator->getValidatorsForForm($this, $validatorType);
1352-
foreach ($result as $validator) {
1353-
$validators[$validator->getID()] = formatUserName($validator->getID(), $validator->fields['name'], $validator->fields['realname'], $validator->fields['firstname']);
1354-
}
1355-
break;
1356-
}
1357-
1358-
$resultCount = count($result);
1359-
if ($resultCount == 1) {
1360-
reset($validators);
1361-
$validatorId = key($validators);
1362-
echo Html::hidden('formcreator_validator', ['value' => $validatorId]);
1363-
} else if ($resultCount > 1) {
1364-
$validators = [0 => Dropdown::EMPTY_VALUE] + $validators;
1365-
echo '<h2>' . __('Validation', 'formcreator') . '</h2>';
1366-
echo '<div class="form-group required liste" id="form-validator">';
1367-
echo '<label>' . __('Choose a validator', 'formcreator') . ' <span class="red">*</span></label>';
1368-
Dropdown::showFromArray('formcreator_validator', $validators);
1369-
echo '</div>';
1370-
}
1175+
echo PluginFormcreatorForm_Validator::dropdownValidator($this);
13711176
}
13721177

13731178
echo Html::scriptBlock('$(function() {

0 commit comments

Comments
 (0)