Skip to content

Commit

Permalink
feat(form_valodator): multiple validation level
Browse files Browse the repository at this point in the history
Signed-off-by: Thierry Bugier <tbugier@teclib.com>
  • Loading branch information
btry committed Apr 1, 2021
1 parent bd9b865 commit 75106b6
Show file tree
Hide file tree
Showing 7 changed files with 595 additions and 245 deletions.
52 changes: 52 additions & 0 deletions front/form_validator.form.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
/**
* ---------------------------------------------------------------------
* Formcreator is a plugin which allows creation of custom forms of
* easy access.
* ---------------------------------------------------------------------
* LICENSE
*
* This file is part of Formcreator.
*
* Formcreator 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.
*
* Formcreator 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 Formcreator. If not, see <http://www.gnu.org/licenses/>.
* ---------------------------------------------------------------------
* @copyright Copyright © 2011 - 2021 Teclib'
* @license http://www.gnu.org/licenses/gpl.txt GPLv3+
* @link https://github.com/pluginsGLPI/formcreator/
* @link https://pluginsglpi.github.io/formcreator/
* @link http://plugins.glpi-project.org/#/plugin/formcreator
* ---------------------------------------------------------------------
*/

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

Session::checkRight('entity', UPDATE);

// Check if plugin is activated...
if (!(new Plugin())->isActivated('formcreator')) {
Html::displayNotFoundError();
}

if (!isset($_POST['plugin_formcreator_forms_id'])) {
// should not happen
}
$formId = (int) $_POST['plugin_formcreator_forms_id'];
$formValidator = new PluginFormcreatorForm_Validator();
$form = new PluginFormcreatorForm();
if (isset($_POST['add'])) {
// Add a new Form
Session::checkRight('entity', UPDATE);
$formValidator->addMultipleItems($_POST);
Html::redirect($form->getFormURLWithID($formId));
}
199 changes: 2 additions & 197 deletions inc/form.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,6 @@ public function canPurgeItem() {
return Session::haveRight('entity', UPDATE);
}

/**
* Returns the type name with consideration of plural
*
* @param number $nb Number of item(s)
* @return string Itemtype name
*/
public static function getTypeName($nb = 0) {
return _n('Form', 'Forms', $nb, 'formcreator');
}
Expand Down Expand Up @@ -403,8 +397,6 @@ public static function getSpecificValueToDisplay($field, $values, array $options
* @return NULL Nothing, just display the form
*/
public function showForm($ID, $options = []) {
global $DB;

$this->initForm($ID, $options);
$this->showFormHeader($options);

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

echo '<tr class="tab_bg_2">';
echo '<td>' . __('Need to be validate?', 'formcreator') . '</td>';
echo '<td class="validators_bloc">';

Dropdown::showFromArray('validation_required', [
self::VALIDATION_NONE => Dropdown::EMPTY_VALUE,
self::VALIDATION_USER => User::getTypeName(1),
self::VALIDATION_GROUP => Group::getTypeName(1),
], [
'value' => $this->fields['validation_required'],
'on_change' => 'plugin_formcreator_changeValidators(this.value)'
]);
echo '</td>';
echo '<td colspan="2">';
// Select all users with ticket validation right and the groups
$userTable = User::getTable();
$userFk = User::getForeignKeyField();
$groupTable = Group::getTable();
$groupFk = Group::getForeignKeyField();
$profileUserTable = Profile_User::getTable();
$profileTable = Profile::getTable();
$profileFk = Profile::getForeignKeyField();
$profileRightTable = ProfileRight::getTable();
$groupUserTable = Group_User::getTable();
$subQuery = [
'SELECT' => "$profileUserTable.$userFk",
'FROM' => $profileUserTable,
'INNER JOIN' => [
$profileTable => [
'FKEY' => [
$profileTable => 'id',
$profileUserTable => $profileFk,
]
],
$profileRightTable =>[
'FKEY' => [
$profileTable => 'id',
$profileRightTable => $profileFk,
]
],
],
'WHERE' => [
"$profileRightTable.name" => "ticketvalidation",
[
'OR' => [
"$profileRightTable.rights" => ['&', TicketValidation::VALIDATEREQUEST],
"$profileRightTable.rights" => ['&', TicketValidation::VALIDATEINCIDENT],
],
],
"$userTable.is_active" => '1',
],
];
$usersCondition = [
"$userTable.id" => new QuerySubquery($subQuery)
];
$formValidator = new PluginFormcreatorForm_Validator();
$selectedValidatorUsers = [];
foreach ($formValidator->getValidatorsForForm($this, User::class) as $user) {
$selectedValidatorUsers[$user->getID()] = $user->getID();
}
$users = $DB->request([
'SELECT' => ['id', 'name'],
'FROM' => User::getTable(),
'WHERE' => $usersCondition,
]);
$validatorUsers = [];
foreach ($users as $user) {
$validatorUsers[$user['id']] = $user['name'];
}
echo '<div id="validators_users">';
echo User::getTypeName() . '&nbsp';
Dropdown::showFromArray(
'_validator_users',
$validatorUsers, [
'multiple' => true,
'values' => $selectedValidatorUsers
]
);
echo '</div>';

// Validators groups
$subQuery = [
'SELECT' => "$groupUserTable.$groupFk",
'FROM' => $groupUserTable,
'INNER JOIN' => [
$userTable => [
'FKEY' => [
$groupUserTable => $userFk,
$userTable => 'id',
]
],
$profileUserTable => [
'FKEY' => [
$profileUserTable => $userFk,
$userTable => 'id',
],
],
$profileTable => [
'FKEY' => [
$profileTable => 'id',
$profileUserTable => $profileFk,
]
],
$profileRightTable =>[
'FKEY' => [
$profileTable => 'id',
$profileRightTable => $profileFk,
]
],
],
'WHERE' => [
"$groupUserTable.$userFk" => new QueryExpression("`$userTable`.`id`"),
"$profileRightTable.name" => "ticketvalidation",
[
'OR' => [
"$profileRightTable.rights" => ['&', TicketValidation::VALIDATEREQUEST],
"$profileRightTable.rights" => ['&', TicketValidation::VALIDATEINCIDENT],
],
],
"$userTable.is_active" => '1',
],
];
$groupsCondition = [
"$groupTable.id" => new QuerySubquery($subQuery),
];
$groups = $DB->request([
'SELECT' => ['id' ,'name'],
'FROM' => Group::getTable(),
'WHERE' => $groupsCondition,
]);
$formValidator = new PluginFormcreatorForm_Validator();
$selectecValidatorGroups = [];
foreach ($formValidator->getValidatorsForForm($this, Group::class) as $group) {
$selectecValidatorGroups[$group->getID()] = $group->getID();
}
$validatorGroups = [];
foreach ($groups as $group) {
$validatorGroups[$group['id']] = $group['name'];
}
echo '<div id="validators_groups" style="width: 100%">';
echo Group::getTypeName() . '&nbsp';
Dropdown::showFromArray(
'_validator_groups',
$validatorGroups,
[
'multiple' => true,
'values' => $selectecValidatorGroups
]
);
echo '</div>';

$script = '$(document).ready(function() {plugin_formcreator_changeValidators(' . $this->fields["validation_required"] . ');});';
echo Html::scriptBlock($script);

echo '</td>';
echo '</tr>';

echo '<tr>';
echo '<td>'.__('Default form in service catalog', 'formcreator').'</td>';
echo '<td>';
Expand Down Expand Up @@ -762,6 +597,7 @@ public static function displayTabContentForItem(CommonGLPI $item, $tabnum = 1, $
public function defineTabs($options = []) {
$ong = [];
$this->addDefaultFormTab($ong);
$this->addStandardTab(PluginFormcreatorForm_Validator::class, $ong, $options);
$this->addStandardTab(PluginFormcreatorQuestion::class, $ong, $options);
$this->addStandardTab(PluginFormcreatorForm_Profile::class, $ong, $options);
$this->addStandardTab(__CLASS__, $ong, $options);
Expand Down Expand Up @@ -1336,38 +1172,7 @@ public function displayUserForm() : void {

// Show validator selector
if ($this->fields['validation_required'] != PluginFormcreatorForm_Validator::VALIDATION_NONE) {
$validators = [];
$formValidator = new PluginFormcreatorForm_Validator();
switch ($this->fields['validation_required']) {
case PluginFormcreatorForm_Validator::VALIDATION_GROUP:
$validatorType = Group::class;
$result = $formValidator->getValidatorsForForm($this, $validatorType);
foreach ($result as $validator) {
$validators[$validator->getID()] = $validator->fields['completename'];
}
break;
case PluginFormcreatorForm_Validator::VALIDATION_USER:
$validatorType = User::class;
$result = $formValidator->getValidatorsForForm($this, $validatorType);
foreach ($result as $validator) {
$validators[$validator->getID()] = formatUserName($validator->getID(), $validator->fields['name'], $validator->fields['realname'], $validator->fields['firstname']);
}
break;
}

$resultCount = count($result);
if ($resultCount == 1) {
reset($validators);
$validatorId = key($validators);
echo Html::hidden('formcreator_validator', ['value' => $validatorId]);
} else if ($resultCount > 1) {
$validators = [0 => Dropdown::EMPTY_VALUE] + $validators;
echo '<h2>' . __('Validation', 'formcreator') . '</h2>';
echo '<div class="form-group required liste" id="form-validator">';
echo '<label>' . __('Choose a validator', 'formcreator') . ' <span class="red">*</span></label>';
Dropdown::showFromArray('formcreator_validator', $validators);
echo '</div>';
}
echo PluginFormcreatorForm_Validator::dropdownValidator($this);
}

echo Html::scriptBlock('$(function() {
Expand Down
Loading

0 comments on commit 75106b6

Please sign in to comment.