Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Form transition : legacy (full page) or modal #2837

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 103 additions & 0 deletions ajax/formanswer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?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');

if (!isset($_REQUEST['id']) || !isset($_REQUEST['action'])) {
http_response_code(400);
die();
}

if ($_POST['action'] == 'submit_formcreator') {
unset($_SESSION['MESSAGE_AFTER_REDIRECT']);
// Save form
$formAnswer = PluginFormcreatorCommon::getFormAnswer();
$form = PluginFormcreatorCommon::getForm();
$form->getFromDB($_POST['id']);
$_POST['plugin_formcreator_forms_id'] = (int) $_POST['id'];
unset($_POST['id']);
if ($formAnswer->add($_POST) === false) {
http_response_code(400);
die();
}
$form->increaseUsageCount();

if ($_SESSION['glpiname'] == 'formcreator_temp_user') {
// Form was saved by an annymous user
unset($_SESSION['glpiname']);
// don't show notifications
unset($_SESSION['MESSAGE_AFTER_REDIRECT']);
echo json_encode([
'redirect' => 'formdisplay.php?answer_saved',
], JSON_OBJECT_AS_ARRAY);
http_response_code(200);
die();
}

// redirect to created item
if ($_SESSION['glpibackcreated']) {
if (count($formAnswer->targetList) == 1) {
$target = current($formAnswer->targetList);
echo json_encode([
'redirect' => $target->getFormURLWithID($target->getID()),
], JSON_OBJECT_AS_ARRAY);
http_response_code(200);
die();
}
echo json_encode([
'redirect' => PluginFormcreatorFormAnswer::getFormURLWithID($formAnswer->getID()),
], JSON_OBJECT_AS_ARRAY);
http_response_code(200);
die();
}

if (plugin_formcreator_replaceHelpdesk()) {
// Form was saved from the service catalog
echo json_encode([
'redirect' => PluginFormcreatorIssue::getSearchUrl(),
], JSON_OBJECT_AS_ARRAY);
http_response_code(200);
die();
}
if (strpos($_SERVER['HTTP_REFERER'], 'formdisplay.php') !== false) {
// Form was saved from helpdesk (assistance > forms)
http_response_code(200);
echo json_encode([
'redirect' => $_SERVER['HTTP_REFERER'],
], JSON_OBJECT_AS_ARRAY);
die();
}
// Form was saved from preview tab, go back to the preview
http_response_code(200);
echo json_encode([
'redirect' => $_SERVER['HTTP_REFERER'],
], JSON_OBJECT_AS_ARRAY);
}
4 changes: 2 additions & 2 deletions ajax/showfields.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@
}

$formFk = PluginFormcreatorForm::getForeignKeyField();
if (!isset($_POST[$formFk])) {
if (!isset($_POST['id'])) {
http_response_code(403);
exit();
}

$form = PluginFormcreatorCommon::getForm();
$form->getFromDB((int) $_POST['plugin_formcreator_forms_id']);
$form->getFromDB((int) $_POST['id']);
if (!Session::haveRight(PluginFormcreatorForm::$rightname, UPDATE) && ($form->isDeleted() || $form->fields['is_active'] == '0')) {
http_response_code(403);
exit();
Expand Down
46 changes: 0 additions & 46 deletions front/form.form.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,52 +126,6 @@
$form->importJson($_REQUEST);
Html::back();

} else if (isset($_POST['submit_formcreator'])) {
// Save form to target
if (!$form->getFromDB($_POST['plugin_formcreator_forms_id'])) {
Html::back();
}

// If user is not authenticated, create temporary user
if (!isset($_SESSION['glpiname'])) {
$_SESSION['glpiname'] = 'formcreator_temp_user';
}

// Save form
$formAnswer = PluginFormcreatorCommon::getFormAnswer();
if ($formAnswer->add($_POST) === false) {
Html::back();
}
$form->increaseUsageCount();

if ($_SESSION['glpiname'] == 'formcreator_temp_user') {
// Form was saved by an annymous user
unset($_SESSION['glpiname']);
// don't show notifications
unset($_SESSION['MESSAGE_AFTER_REDIRECT']);
Html::redirect('formdisplay.php?answer_saved');
}

// redirect to created item
if ($_SESSION['glpibackcreated']) {
if (count($formAnswer->targetList) == 1) {
$target = current($formAnswer->targetList);
Html::redirect($target->getFormURLWithID($target->getID()));
}
Html::redirect(PluginFormcreatorFormAnswer::getFormURLWithID($formAnswer->getID()));
}

if (plugin_formcreator_replaceHelpdesk()) {
// Form was saved from the service catalog
Html::redirect('issue.php');
}
if (strpos($_SERVER['HTTP_REFERER'], 'formdisplay.php') !== false) {
// Form was saved from helpdesk (assistance > forms)
Html::redirect('formlist.php');
}
// Form was saved from preview tab, go back to the preview
Html::back();

} else {
// Show forms form
Session::checkRight(PluginFormcreatorForm::$rightname, READ);
Expand Down
21 changes: 17 additions & 4 deletions front/formdisplay.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@
Html::displayNotFoundError();
}

PluginFormcreatorCommon::header();
if (!isset($_REQUEST['modal'])) {
PluginFormcreatorCommon::header();
}

if (isset($_REQUEST['id'])
&& is_numeric($_REQUEST['id'])) {
Expand All @@ -54,12 +56,20 @@

// If the form has restriced access and user is not logged in, send to login form
if ($form->fields['access_rights'] != PluginFormcreatorForm::ACCESS_PUBLIC && Session::getLoginUserID() === false) {
Session::redirectIfNotLoggedIn();
if (!(new Plugin())->isActivated('formcreator')) {
Session::redirectIfNotLoggedIn();
} else {
http_response_code(403);
}
exit();
}

if (!$form->canViewForRequest()) {
Html::displayRightError();
if (!(new Plugin())->isActivated('formcreator')) {
Html::displayRightError();
} else {
http_response_code(403);
}
exit();
}
if (($form->fields['access_rights'] == PluginFormcreatorForm::ACCESS_PUBLIC) && (!isset($_SESSION['glpiID']))) {
Expand All @@ -75,6 +85,7 @@
? "'" . implode("', '", $subentities) . "'"
: "'" . $form->fields['entities_id'] . "'";
$_SESSION['glpilanguage'] = $form->getBestLanguage();
$CFG_GLPI['plugin_formcreator']['form_transition'] = PluginFormcreatorEntityConfig::getUsedConfig('form_transition', $form->fields['entities_id'], 'form_transition', );
}
}

Expand All @@ -91,4 +102,6 @@
Html::displayTitle($CFG_GLPI['root_doc']."/pics/ok.png", $message, $message);
}

PluginFormcreatorCommon::footer();
if (!isset($_REQUEST['modal'])) {
PluginFormcreatorCommon::footer();
}
89 changes: 87 additions & 2 deletions inc/entityconfig.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ class PluginFormcreatorEntityconfig extends CommonDBTM {
const CONFIG_UI_FORM_MASONRY = 0;
const CONFIG_UI_FORM_UNIFORM_HEIGHT = 1;

const CONFIG_FORM_TRANSITION_PAGE = 0;
const CONFIG_FORM_TRANSITION_MODAL = 1;

/**
* @var bool $dohistory maintain history
Expand Down Expand Up @@ -158,6 +160,14 @@ public static function getEnumUIForm() : array {
];
}

public static function getEnumFormTransition(): array {
return [
self::CONFIG_PARENT => __('Inheritance of the parent entity'),
self::CONFIG_FORM_TRANSITION_PAGE => __('Full page', 'formcreator'),
self::CONFIG_FORM_TRANSITION_MODAL => __('Modal', 'formcreator'),
];
}

public static function displayTabContentForItem(CommonGLPI $item, $tabnum = 1, $withtemplate = 0) {
if ($item->getType() == 'Entity') {
$config = new self();
Expand Down Expand Up @@ -208,6 +218,7 @@ protected static function createDefaultsForEntity($entityId): self {
'is_search_visible' => self::CONFIG_PARENT,
'is_dashboard_visible' => self::CONFIG_PARENT,
'is_header_visible' => self::CONFIG_PARENT,
'form_transition' => self::CONFIG_PARENT,
]);

return $entityConfig;
Expand Down Expand Up @@ -382,6 +393,22 @@ public function showFormForEntity(Entity $entity) {
]);
echo '</td></tr>';

// Form transition
$elements = self::getEnumFormTransition();
if ($entityId == 0) {
unset($elements[self::CONFIG_PARENT]);
}
echo "<tr class='tab_bg_1'>";
echo "<td>".__('Form transition', 'formcreator')."</td>";
echo "<td>";
Dropdown::showFromArray('form_transition', $elements, ['value' => $this->fields['form_transition']]);
if ($this->fields['form_transition'] == self::CONFIG_PARENT) {
$tid = self::getUsedConfig('form_transition', $entityId);
echo '<br>';
Entity::inheritedValue($elements[$tid], true);
}
echo '</td></tr>';

if ($canedit) {
echo "<tr>";
echo "<td class='tab_bg_2 center' colspan='4'>";
Expand Down Expand Up @@ -457,6 +484,66 @@ public function rawSearchOptions() {
'massiveaction' => true,
];

$tab[] = [
'id' => '9',
'table' => self::getTable(),
'name' => __('Default Form list mode', 'formcreator'),
'field' => 'default_form_list_mode',
'datatype' => 'integer',
'nosearch' => true,
'massiveaction' => true,
];

$tab[] = [
'id' => '10',
'table' => self::getTable(),
'name' => __('Search', 'formcreator'),
'field' => 'is_search_visible',
'datatype' => 'integer',
'nosearch' => true,
'massiveaction' => true,
];

$tab[] = [
'id' => '11',
'table' => self::getTable(),
'name' => __('Counters dashboard', 'formcreator'),
'field' => 'is_dashboard_visible',
'datatype' => 'integer',
'nosearch' => true,
'massiveaction' => true,
];

$tab[] = [
'id' => '12',
'table' => self::getTable(),
'name' => __('Search issue', 'formcreator'),
'field' => 'is_search_issue_visible',
'datatype' => 'integer',
'nosearch' => true,
'massiveaction' => true,
];

$tab[] = [
'id' => '13',
'table' => self::getTable(),
'name' => __('Tile design', 'formcreator'),
'field' => 'tile_design',
'datatype' => 'integer',
'nosearch' => false,
'massiveaction' => true,
];

$tab[] = [
'id' => '14',
'table' => self::getTable(),
'name' => __('Form transition', 'formcreator'),
'field' => 'form_transition',
'datatype' => 'integer',
'nosearch' => false,
'massiveaction' => true,
];

return $tab;
}

Expand Down Expand Up @@ -491,7 +578,6 @@ static function getUsedConfig($fieldref, $entities_id, $fieldval = '', $default_
if (!is_numeric($default_value)) {
return $entityConfig->fields[$fieldval];
}

}
}

Expand All @@ -502,7 +588,6 @@ static function getUsedConfig($fieldref, $entities_id, $fieldval = '', $default_
$ret = self::getUsedConfig($fieldref, $entity->fields['entities_id'], $fieldval,
$default_value);
return $ret;

}
}

Expand Down
2 changes: 1 addition & 1 deletion inc/fields.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ public static function isVisible(PluginFormcreatorConditionnableInterface $item,
*/
public static function updateVisibility($input) {
$form = PluginFormcreatorCommon::getForm();
$form->getFromDB((int) $input['plugin_formcreator_forms_id']);
$form->getFromDB((int) $input['id']);
$fields = $form->getFields();
foreach ($fields as $id => $field) {
$fields[$id]->parseAnswerValues($input, true);
Expand Down
Loading