Skip to content

Commit

Permalink
feat(form): show form in regular page or in modal
Browse files Browse the repository at this point in the history
  • Loading branch information
btry committed Jun 23, 2022
1 parent b6d1d1c commit 0983f95
Show file tree
Hide file tree
Showing 13 changed files with 270 additions and 88 deletions.
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();
}
39 changes: 37 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 @@ -507,6 +534,16 @@ public function rawSearchOptions() {
'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 @@ -541,7 +578,6 @@ static function getUsedConfig($fieldref, $entities_id, $fieldval = '', $default_
if (!is_numeric($default_value)) {
return $entityConfig->fields[$fieldval];
}

}
}

Expand All @@ -552,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
28 changes: 18 additions & 10 deletions inc/form.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ public static function displayTabContentForItem(CommonGLPI $item, $tabnum = 1, $

case 2:
echo '<div style="text-align: left">';
$item->displayUserForm();
$item->displayUserForm(true);
echo '</div>';
break;

Expand Down Expand Up @@ -937,10 +937,11 @@ protected function showMyLastForms() : void {

/**
* Display the Form end-user form to be filled
* @param bool $forcePage Force display as page
*
* @return void
*/
public function displayUserForm() : void {
public function displayUserForm(bool $forcePage = false) : void {
global $TRANSLATE;

// Print css media
Expand All @@ -958,16 +959,23 @@ public function displayUserForm() : void {
}
$formanswer = new PluginFormcreatorFormAnswer();
$formanswer->loadAnswersFromSession();
$options = [
'columns' => PluginFormcreatorSection::COLUMNS,
'domain' => $domain, // For translation
'public' => isset($_SESSION['formcreator_public']),
'formanswer' => $formanswer,
'use_captcha' => ($this->fields['access_rights'] == PluginFormcreatorForm::ACCESS_PUBLIC
&& $this->fields['is_captcha_enabled'] != '0'),
];
$options['target'] = "javascript:;";
$options['formoptions'] = sprintf('onsubmit="plugin_formcreator.submitForm(this)"');
// if (!$forcePage && PluginFormcreatorEntityConfig::getUsedConfig('form_transition', Session::getActiveEntity()) == PluginFormcreatorEntityConfig::CONFIG_FORM_TRANSITION_MODAL) {
// $options['target'] = "javascript:;";
// $options['formoptions'] = sprintf('onsubmit="plugin_formcreator.submitForm(this)"');
// }
TemplateRenderer::getInstance()->display('@formcreator/pages/userform.html.twig', [
'item' => $this,
'options' => [
'columns' => PluginFormcreatorSection::COLUMNS,
'domain' => $domain, // For translation
'public' => isset($_SESSION['formcreator_public']),
'formanswer' => $formanswer,
'use_captcha' => ($this->fields['access_rights'] == PluginFormcreatorForm::ACCESS_PUBLIC
&& $this->fields['is_captcha_enabled'] != '0'),
]
'options' => $options
]);
// Delete saved answers if any
unset($_SESSION['formcreator']['data']);
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 @@ -41,6 +41,7 @@ CREATE TABLE IF NOT EXISTS `glpi_plugin_formcreator_entityconfigs` (
`is_search_issue_visible` int(11) NOT NULL DEFAULT '-2',
`tile_design` int(11) NOT NULL DEFAULT '-2',
`header` text,
`form_transition` int(11) NOT NULL DEFAULT '-2',
PRIMARY KEY (`id`),
UNIQUE KEY `unicity` (`entities_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci ROW_FORMAT=DYNAMIC;
Expand Down

0 comments on commit 0983f95

Please sign in to comment.