From 5f2a28d8240afe87627606e9c9b4bfba5711e34b Mon Sep 17 00:00:00 2001 From: tomolimo Date: Wed, 19 Jun 2019 16:21:15 +0200 Subject: [PATCH] Added itemtype creation re-organized installation and updates Added Export/Import of pages fixes #27 --- README.md | 36 +- ajax/existingFile.php | 9 + ajax/getFormValidations.php | 173 +++++----- ajax/getLocales.php | 126 +++---- ajax/setUnsetField.php | 237 ++++++------- css/formvalidation.css | 4 +- front/field.form.php | 174 +++++----- front/form.form.php | 146 ++++---- front/formvalidation.backup.php | 48 +++ front/itemtype.form.php | 5 + front/itemtype.php | 6 + front/page.form.php | 150 ++++---- front/page.php | 2 +- front/user.form.php | 88 ++--- hook.php | 274 +++------------ inc/config.class.php | 96 +++--- inc/field.class.php | 15 + inc/form.class.php | 15 + inc/hook.class.php | 422 +++++++++++------------ inc/itemtype.class.php | 63 +++- inc/menu.class.php | 98 +++--- inc/page.class.php | 451 ++++++++++++++++++++++++- inc/user.class.php | 174 +++++----- install/install.php | 104 ++++++ install/mysql/formvalidation-empty.sql | 75 ++++ install/update.php | 240 +++++++++++++ js/disable_required_fields.js | 21 -- js/formvalidation.js | 20 +- locales/cs_CZ.php | 90 ++--- locales/cs_CZ.po | 420 +++++++++++------------ locales/en_GB.php | 90 ++--- locales/en_GB.po | 420 +++++++++++------------ locales/fr_FR.php | 84 ++--- locales/fr_FR.po | 420 +++++++++++------------ setup.php | 4 +- 35 files changed, 2791 insertions(+), 2009 deletions(-) create mode 100644 ajax/existingFile.php create mode 100644 front/formvalidation.backup.php create mode 100644 front/itemtype.form.php create mode 100644 front/itemtype.php create mode 100644 install/install.php create mode 100644 install/mysql/formvalidation-empty.sql create mode 100644 install/update.php delete mode 100644 js/disable_required_fields.js diff --git a/README.md b/README.md index 333274c..c49f826 100644 --- a/README.md +++ b/README.md @@ -1,18 +1,18 @@ -# Form Validation -GLPI plugin to provide form validations -This plugin gives the possibility to graphically set / unset mandatory fields on any GLPI form. -See : https://github.com/tomolimo/formvalidation/wiki - - - -## Installation -- download the archive file -- copy content of archive into your `glpi/plugins/` folder, beware that the plugin folder must be `formvalidation`. So you should have a directory like: `glpi/plugins/formvalidation/`. -- go to glpi plugins page to install and enable the newly installed plugin. - -## Configuration -See wiki pages https://github.com/tomolimo/formvalidation/wiki - -## GLPI compatibility: -Tested with 0.85, 0.90, 9.1, 9.2 and 9.3 - +# Form Validation +GLPI plugin to provide form validations +This plugin gives the possibility to graphically set / unset mandatory fields on any GLPI form. +See : https://github.com/tomolimo/formvalidation/wiki + + + +## Installation +- download the archive file +- copy content of archive into your `glpi/plugins/` folder, beware that the plugin folder must be `formvalidation`. So you should have a directory like: `glpi/plugins/formvalidation/`. +- go to glpi plugins page to install and enable the newly installed plugin. + +## Configuration +See wiki pages https://github.com/tomolimo/formvalidation/wiki + +## GLPI compatibility: +Tested with 0.85, 0.90, 9.1, 9.2 and 9.3 + diff --git a/ajax/existingFile.php b/ajax/existingFile.php new file mode 100644 index 0000000..fdc6f6c --- /dev/null +++ b/ajax/existingFile.php @@ -0,0 +1,9 @@ +. --------------------------------------------------------------------------- - */ - -$AJAX_INCLUDE = 1; -include ('../../../inc/includes.php'); - -// Send UTF8 Headers -header("Content-Type: text/html; charset=UTF-8"); -Html::header_nocache(); - -Session::checkLoginUser(); - -$config = PluginFormvalidationConfig::getInstance(); - -$validations = [ 'config' => $config->fields, 'pages_id' => 0, 'forms' => [ ] ]; // by default - -// from user session -$validations['config']['editmode']=$_SESSION['glpiformvalidationeditmode']; - -$is_createitem = 0; // by default - -$obj = getItemForItemtype( $_GET['itemtype'] ); -if ($obj) { - if ($_GET['id'] > 0) { - $obj->getFromDB( $_GET['id'] ); - $entity_restrict = 0; // by default if $obj doesn't have entities_id in table - if (isset($obj->fields['entities_id'])) { - $entity_restrict = $obj->fields['entities_id']; - } - } else { - $is_createitem = 1; - $entity_restrict = $_SESSION['glpiactive_entity']; - } - - $query = "SELECT glpi_plugin_formvalidation_pages.*, glpi_plugin_formvalidation_itemtypes.itemtype FROM glpi_plugin_formvalidation_pages - JOIN glpi_plugin_formvalidation_itemtypes on glpi_plugin_formvalidation_itemtypes.id=glpi_plugin_formvalidation_pages.itemtypes_id - WHERE glpi_plugin_formvalidation_itemtypes.itemtype = '".$obj->getType()."' AND is_active=1 ". - getEntitiesRestrictRequest( 'AND', 'glpi_plugin_formvalidation_pages', '', $entity_restrict, true); - - foreach ($DB->request( $query ) as $page) { - $validations['pages_id']=$page['id']; // normaly there is only one page - $validations['itemtype']=$page['itemtype']; // normaly there is only one page - - $query = "SELECT * FROM glpi_plugin_formvalidation_forms WHERE is_createitem=$is_createitem AND pages_id = ".$page['id']; - - foreach ($DB->request( $query ) as $form) { - $validations['forms'][$form['id']]= Toolbox::stripslashes_deep( $form ); - $validations['forms'][$form['id']]['fields'] = []; // needed in case this form has no fields - - $query = "SELECT * FROM glpi_plugin_formvalidation_fields WHERE forms_id = ".$form['id']; - foreach ($DB->request( $query ) as $field) { - $validations['forms'][$form['id']]['fields'][$field['id']] = Toolbox::stripslashes_deep( $field ); - } - } - } -} -echo json_encode( $validations, JSON_HEX_APOS | JSON_HEX_QUOT ); - +. +-------------------------------------------------------------------------- + */ + +$AJAX_INCLUDE = 1; +include ('../../../inc/includes.php'); + +// Send UTF8 Headers +header("Content-Type: text/html; charset=UTF-8"); +Html::header_nocache(); + +Session::checkLoginUser(); + +$config = PluginFormvalidationConfig::getInstance(); + +$validations = [ 'config' => $config->fields, 'pages_id' => 0, 'forms' => [ ] ]; // by default + +// from user session +$validations['config']['editmode']=$_SESSION['glpiformvalidationeditmode']; + +$is_createitem = 0; // by default + +$obj = getItemForItemtype( $_GET['name'] ); +if ($obj) { + if ($_GET['id'] > 0) { + $obj->getFromDB( $_GET['id'] ); + $entity_restrict = 0; // by default if $obj doesn't have entities_id in table + if (isset($obj->fields['entities_id'])) { + $entity_restrict = $obj->fields['entities_id']; + } + } else { + $is_createitem = 1; + $entity_restrict = $_SESSION['glpiactive_entity']; + } + + $query = $DB->request([ + 'SELECT' => ['glpi_plugin_formvalidation_pages.*','glpi_plugin_formvalidation_itemtypes.name'], + 'FROM' => 'glpi_plugin_formvalidation_pages', + 'INNER JOIN' => ['glpi_plugin_formvalidation_itemtypes' => + ['FKEY' => + [ + 'glpi_plugin_formvalidation_pages' => 'itemtypes_id', + 'glpi_plugin_formvalidation_itemtypes' => 'id' + ] + ] + ], + ['AND' => ['glpi_plugin_formvalidation_itemtypes.name'=>$obj->getType(), 'is_active' => 1, getEntitiesRestrictCriteria('glpi_plugin_formvalidation_pages', '', $entity_restrict, true)]] + + ] + ); + foreach ($query as $page) { + $validations['pages_id']=$page['id']; // normaly there is only one page + //$validations['itemtype']=$page['itemtype']; // normaly there is only one page + $validations['itemtype']=$page['name']; // normaly there is only one page + + foreach ($DB->request('glpi_plugin_formvalidation_forms', ['AND'=>['is_createitem' => $is_createitem, 'pages_id' => $page['id']]]) as $form) { + $validations['forms'][$form['id']]= Toolbox::stripslashes_deep( $form ); + $validations['forms'][$form['id']]['fields'] = []; // needed in case this form has no fields + foreach ($DB->request('glpi_plugin_formvalidation_fields', ['forms_id' => $form['id']]) as $field) { + $validations['forms'][$form['id']]['fields'][$field['id']] = Toolbox::stripslashes_deep( $field ); + } + } + } +} +echo json_encode( $validations, JSON_HEX_APOS | JSON_HEX_QUOT ); + diff --git a/ajax/getLocales.php b/ajax/getLocales.php index 2ccdc24..b170adb 100644 --- a/ajax/getLocales.php +++ b/ajax/getLocales.php @@ -1,63 +1,63 @@ -. --------------------------------------------------------------------------- -*/ - -$AJAX_INCLUDE = 1; -include ('../../../inc/includes.php'); - -// Send UTF8 Headers -header("Content-Type: text/html; charset=UTF-8"); -Html::header_nocache(); - -Session::checkLoginUser(); - -if (!isset($LANG['plugin_formvalidation'])) { - $dir = GLPI_ROOT . "/plugins/formvalidation/locales/"; - // try to load en_GB default language - // to be sure that it will default to english if user's language - // can't be found - if (file_exists($dir . "en_GB.php")) { - include ($dir . "en_GB.php"); - } - // and then try to load user's own language - if (file_exists($dir.$_SESSION["glpilanguage"].'.php')) { - include ($dir.$_SESSION["glpilanguage"].'.php'); - } -} - -// JSON encode all strings that are needed in formvalidation.js -$localization = [ -// 'job' => array( 44 => "LANG['job'][44]" ), -// 'common' => array( 17 => "LANG['common'][17]", -// 36 => "LANG['common'][36]") - ]; - -// add plugin own language strings to $localization -$localization['plugin_formvalidation'] = $LANG['plugin_formvalidation']; - -echo json_encode( $localization, JSON_HEX_APOS | JSON_HEX_QUOT ); - +. +-------------------------------------------------------------------------- +*/ + +$AJAX_INCLUDE = 1; +include ('../../../inc/includes.php'); + +// Send UTF8 Headers +header("Content-Type: text/html; charset=UTF-8"); +Html::header_nocache(); + +Session::checkLoginUser(); + +if (!isset($LANG['plugin_formvalidation'])) { + $dir = GLPI_ROOT . "/plugins/formvalidation/locales/"; + // try to load en_GB default language + // to be sure that it will default to english if user's language + // can't be found + if (file_exists($dir . "en_GB.php")) { + include ($dir . "en_GB.php"); + } + // and then try to load user's own language + if (file_exists($dir.$_SESSION["glpilanguage"].'.php')) { + include ($dir.$_SESSION["glpilanguage"].'.php'); + } +} + +// JSON encode all strings that are needed in formvalidation.js +$localization = [ +// 'job' => array( 44 => "LANG['job'][44]" ), +// 'common' => array( 17 => "LANG['common'][17]", +// 36 => "LANG['common'][36]") + ]; + +// add plugin own language strings to $localization +$localization['plugin_formvalidation'] = $LANG['plugin_formvalidation']; + +echo json_encode( $localization, JSON_HEX_APOS | JSON_HEX_QUOT ); + diff --git a/ajax/setUnsetField.php b/ajax/setUnsetField.php index 80acfc0..c38ad2d 100644 --- a/ajax/setUnsetField.php +++ b/ajax/setUnsetField.php @@ -1,115 +1,122 @@ -. --------------------------------------------------------------------------- -*/ - -$AJAX_INCLUDE = 1; -include ('../../../inc/includes.php'); - -// Send UTF8 Headers -header("Content-Type: text/html; charset=UTF-8"); -Html::header_nocache(); - -Session::checkLoginUser(); - -$ret = false; -if (isset($_REQUEST['action'])) { - switch ($_REQUEST['action']) { - case 'set' : - if ($_REQUEST['fieldindex'] > 0) { - $query = "UPDATE glpi_plugin_formvalidation_fields - SET is_active=1, - show_mandatory=1, - css_selector_errorsign='".$DB->escape(html_entity_decode( $_REQUEST['css_selector_errorsign']))."', - css_selector_mandatorysign='".$DB->escape(html_entity_decode( $_REQUEST['css_selector_mandatorysign']))."' - WHERE id=".$_REQUEST['fieldindex']; - if ($DB->query( $query ) && $DB->affected_rows() > 0) { - $ret = true; - } - } else { - // we may need to add a form - if ($_REQUEST['formindex'] == 0) { - // add form - // extract default form name from form_css_selector - $name = ''; - $action = ''; - $matches = []; - $regex = "/form(\\[name=\\\"(?'name'\\w*)\\\"])?\\[action=\\\"(?'action'[\\w\\/\\.]*)\\\"]/"; - if (preg_match( $regex, str_replace("\\", "", html_entity_decode($_REQUEST['form_css_selector'])), $matches )) { - if (isset($matches['name'] )) { - $name = $matches['name']; - } - $action = $matches['action']; - } - $query = "INSERT INTO `glpi_plugin_formvalidation_forms` (`name`, `pages_id`, `css_selector`, `is_active`, `is_createitem`) - VALUES ('".$DB->escape("$name($action)")."', ".$_REQUEST['pages_id'].", '".$DB->escape(html_entity_decode($_REQUEST['form_css_selector']))."', 1, ".$_REQUEST['is_createitem'].")"; - if ($DB->query( $query )) { - $_REQUEST['formindex'] = $DB->insert_id(); - } else { - $ret = false; - } - } - $query = "INSERT INTO `glpi_plugin_formvalidation_fields` (`name`, `forms_id`, `css_selector_value`, `css_selector_errorsign`, `css_selector_mandatorysign`, `is_active`) VALUES ("; - $query .= "'".$_REQUEST['name']."', "; - $query .= $_REQUEST['formindex'].", "; - $query .= "'".$DB->escape(html_entity_decode( $_REQUEST['css_selector_value']))."', "; - $query .= "'".$DB->escape(html_entity_decode( $_REQUEST['css_selector_errorsign']))."', "; - $query .= "'".$DB->escape(html_entity_decode( $_REQUEST['css_selector_mandatorysign']))."', 1)"; - - if ($DB->query( $query )) { - $_REQUEST['fieldindex'] = $DB->insert_id(); - $ret = [ 'forms' => [ ] ]; // by default - $query = "SELECT * FROM glpi_plugin_formvalidation_forms WHERE id = ".$_REQUEST['formindex']; - foreach ($DB->request( $query ) as $form) { - $ret['forms_id'] = $form['id']; - $ret['forms'][$form['id']]=Toolbox::stripslashes_deep( $form ); - $ret['forms'][$form['id']]['fields'] = []; // needed in case this form has no fields - $query = "SELECT * FROM glpi_plugin_formvalidation_fields WHERE id = ".$_REQUEST['fieldindex']; - foreach ($DB->request( $query ) as $field) { - $ret['fields_id']=$field['id']; - $ret['forms'][$form['id']]['fields'][$field['id']] = Toolbox::stripslashes_deep( $field ); - } - } - } - } - break; - case 'unset' : - $query = "UPDATE glpi_plugin_formvalidation_fields SET is_active=0 WHERE id=".$_REQUEST['fieldindex']; - if ($DB->query( $query ) && $DB->affected_rows() > 0) { - $ret = true; - } - break; - case 'hidemandatorysign': - $query = "UPDATE glpi_plugin_formvalidation_fields SET show_mandatory=0 WHERE id=".$_REQUEST['fieldindex']; - if ($DB->query( $query ) && $DB->affected_rows() > 0) { - $ret = true; - } - break; - } -} - -echo json_encode( $ret ); - +. +-------------------------------------------------------------------------- +*/ + +$AJAX_INCLUDE = 1; +include ('../../../inc/includes.php'); + +// Send UTF8 Headers +header("Content-Type: text/html; charset=UTF-8"); +Html::header_nocache(); + +Session::checkLoginUser(); + +$ret = false; +if (isset($_REQUEST['action'])) { + + $fields = new PluginFormvalidationField(); + switch ($_REQUEST['action']) { + case 'set' : + if ($_REQUEST['fieldindex'] > 0) { + if ($fields->update([ + 'id' => $_REQUEST['fieldindex'], + 'is_active' => 1, + 'show_mandatory' => 1, + 'css_selector_errorsign' => $DB->escape(html_entity_decode( $_REQUEST['css_selector_errorsign'])), + 'css_selector_mandatorysign' => $DB->escape(html_entity_decode( $_REQUEST['css_selector_mandatorysign'])) + ])) { + $ret = true; + } + } else { + // we may need to add a form + if ($_REQUEST['formindex'] == 0) { + // add form + $form = new PluginFormvalidationForm(); + // extract default form name from form_css_selector + $name = ''; + $action = ''; + $matches = []; + $regex = "/form(\\[name=\\\"(?'name'\\w*)\\\"])?\\[action=\\\"(?'action'[\\w\\/\\.]*)\\\"]/"; + if (preg_match( $regex, str_replace("\\", "", html_entity_decode($_REQUEST['form_css_selector'])), $matches )) { + if (isset($matches['name'] )) { + $name = $matches['name']; + } + $action = $matches['action']; + } + if ($form->add( + [ + 'name' => $DB->escape("$name($action)"), + 'pages_id' => $_REQUEST['pages_id'], + 'css_selector' => $DB->escape(html_entity_decode($_REQUEST['form_css_selector'])), + 'is_active' => 1, + 'is_createitem' => $_REQUEST['is_createitem'] + ] + )) { + $_REQUEST['formindex'] = $form->fields['id']; + } else { + $ret = false; + } + } + if ($fields->add( + [ + 'name' => $_REQUEST['name'], + 'forms_id' => $_REQUEST['formindex'], + 'css_selector_value' => $DB->escape(html_entity_decode( $_REQUEST['css_selector_value'])), + 'css_selector_errorsign' => $DB->escape(html_entity_decode( $_REQUEST['css_selector_errorsign'])), + 'css_selector_mandatorysign' => $DB->escape(html_entity_decode( $_REQUEST['css_selector_mandatorysign'])), + 'is_active' => 1 + ] + )) { + $_REQUEST['fieldindex'] = $fields->fields['id']; + $ret = [ 'forms' => [ ] ]; // by default + foreach ($DB->request('glpi_plugin_formvalidation_forms', ['id' => $_REQUEST['formindex']]) as $form) { + $ret['forms_id'] = $form['id']; + $ret['forms'][$form['id']]=Toolbox::stripslashes_deep( $form ); + $ret['forms'][$form['id']]['fields'] = []; // needed in case this form has no fields + foreach ($DB->request('glpi_plugin_formvalidation_fields', ['id' => $_REQUEST['fieldindex']]) as $field) { + $ret['fields_id']=$field['id']; + $ret['forms'][$form['id']]['fields'][$field['id']] = Toolbox::stripslashes_deep( $field ); + } + } + } + } + break; + case 'unset' : + if ($fields->update(['id'=>$_REQUEST['fieldindex'], 'is_active' => 0])) { + $ret = true; + } + break; + case 'hidemandatorysign': + if ($fields->update(['id'=>$_REQUEST['fieldindex'], 'show_mandatory' => 0])) { + $ret = true; + } + break; + } +} + +echo json_encode( $ret ); + diff --git a/css/formvalidation.css b/css/formvalidation.css index ac72aca..d602446 100644 --- a/css/formvalidation.css +++ b/css/formvalidation.css @@ -1,2 +1,2 @@ -.fv-alert { z-index: 2000 !important ;} -.fv-editmodeinfo { z-index: 10000 !important ;} +.fv-alert { z-index: 2000 !important ;} +.fv-editmodeinfo { z-index: 10000 !important ;} diff --git a/front/field.form.php b/front/field.form.php index ac28d64..978663b 100644 --- a/front/field.form.php +++ b/front/field.form.php @@ -1,87 +1,87 @@ -. --------------------------------------------------------------------------- - */ - -/** @file - * @brief - */ - -include ('../../../inc/includes.php'); - -if (empty($_GET["id"])) { - $_GET["id"] = ''; -} - -Session::checkLoginUser(); - -$field = new PluginFormvalidationField(); -if (isset($_POST["purge"])) { - $field->check($_POST["id"], PURGE); - $field->delete($_POST, 1); - - $field->redirectToList(); - -} else if (isset($_POST["update"])) { - - if (!isset( $_POST['id'] )) { - // then we have an array of input to update - foreach ($_POST as $key => $val) { - $match = []; - if (preg_match( "/^formula_(\\d+)$/", $key, $match )) { - $ID = $match[1]; - $field->check($ID, UPDATE); - - $formula = Html::entity_decode_deep( $val ); - $formula = ($formula===''?'NULL':$formula); - - $_POST["show_mandatory_if_$ID"] = Html::entity_decode_deep( $_POST["show_mandatory_if_$ID"] ); - $post = [ 'id' => $ID, 'formula' => $formula, 'is_active' => $_POST["is_active_$ID"], 'show_mandatory' => $_POST["show_mandatory_$ID"], 'show_mandatory_if' => $_POST["show_mandatory_if_$ID"] ]; - $field->update($post); - } - } - - } else { - // then we have only one field - $field->check($_POST["id"], UPDATE); - - $_POST["formula"] = Html::entity_decode_deep( $_POST["formula"] ); - $_POST["show_mandatory_if"] = Html::entity_decode_deep( $_POST["show_mandatory_if"] ); - $field->update($_POST); - } - - Html::back(); - -} else { - Html::header(__('Form Validation - Field', 'formvalidation'), $_SERVER['PHP_SELF'], "config", "PluginFormvalidationMenu", "formvalidationfield"); - $field->display($_GET); - Html::footer(); -} +. +-------------------------------------------------------------------------- + */ + +/** @file + * @brief + */ + +include ('../../../inc/includes.php'); + +if (empty($_GET["id"])) { + $_GET["id"] = ''; +} + +Session::checkLoginUser(); + +$field = new PluginFormvalidationField(); +if (isset($_POST["purge"])) { + $field->check($_POST["id"], PURGE); + $field->delete($_POST, 1); + + $field->redirectToList(); + +} else if (isset($_POST["update"])) { + + if (!isset( $_POST['id'] )) { + // then we have an array of input to update + foreach ($_POST as $key => $val) { + $match = []; + if (preg_match( "/^formula_(\\d+)$/", $key, $match )) { + $ID = $match[1]; + $field->check($ID, UPDATE); + + $formula = Html::entity_decode_deep( $val ); + $formula = ($formula===''?'NULL':$formula); + + $_POST["show_mandatory_if_$ID"] = Html::entity_decode_deep( $_POST["show_mandatory_if_$ID"] ); + $post = [ 'id' => $ID, 'formula' => $formula, 'is_active' => $_POST["is_active_$ID"], 'show_mandatory' => $_POST["show_mandatory_$ID"], 'show_mandatory_if' => $_POST["show_mandatory_if_$ID"] ]; + $field->update($post); + } + } + + } else { + // then we have only one field + $field->check($_POST["id"], UPDATE); + + $_POST["formula"] = Html::entity_decode_deep( $_POST["formula"] ); + $_POST["show_mandatory_if"] = Html::entity_decode_deep( $_POST["show_mandatory_if"] ); + $field->update($_POST); + } + + Html::back(); + +} else { + Html::header(__('Form Validation - Field', 'formvalidation'), $_SERVER['PHP_SELF'], "config", "PluginFormvalidationMenu", "formvalidationfield"); + $field->display($_GET); + Html::footer(); +} diff --git a/front/form.form.php b/front/form.form.php index fbf3cd6..71e6bda 100644 --- a/front/form.form.php +++ b/front/form.form.php @@ -1,73 +1,73 @@ -. --------------------------------------------------------------------------- - */ - -/** @file - * @brief - */ - -include ('../../../inc/includes.php'); - -if (empty($_GET["id"])) { - $_GET["id"] = ''; -} - -Session::checkLoginUser(); - -$form = new PluginFormvalidationForm(); -if (isset($_POST["purge"])) { - $form->check($_POST["id"], PURGE); - $form->delete($_POST, 1); - - //Event::log($_POST["id"], "change", 4, "maintain", - // //TRANS: %s is the user login - // sprintf(__('%s purges an item'), $_SESSION["glpiname"])); - $form->redirectToList(); - -} else if (isset($_POST["update"])) { - $form->check($_POST["id"], UPDATE); - - $_POST["formula"] = Html::entity_decode_deep( $_POST["formula"] ); - - $form->update($_POST); - //Event::log($_POST["id"], "change", 4, "maintain", - // //TRANS: %s is the user login - // sprintf(__('%s updates an item'), $_SESSION["glpiname"])); - - Html::back(); - -} else { - // Html::header(Change::getTypeName(Session::getPluralNumber()), $_SERVER['PHP_SELF'], "helpdesk", "change"); - Html::header(__('Form Validation - Form', 'formvalidation'), $_SERVER['PHP_SELF'], "config", "PluginFormvalidationMenu", "formvalidationform"); - $form->display($_GET); - Html::footer(); -} +. +-------------------------------------------------------------------------- + */ + +/** @file + * @brief + */ + +include ('../../../inc/includes.php'); + +if (empty($_GET["id"])) { + $_GET["id"] = ''; +} + +Session::checkLoginUser(); + +$form = new PluginFormvalidationForm(); +if (isset($_POST["purge"])) { + $form->check($_POST["id"], PURGE); + $form->delete($_POST, 1); + + //Event::log($_POST["id"], "change", 4, "maintain", + // //TRANS: %s is the user login + // sprintf(__('%s purges an item'), $_SESSION["glpiname"])); + $form->redirectToList(); + +} else if (isset($_POST["update"])) { + $form->check($_POST["id"], UPDATE); + + $_POST["formula"] = Html::entity_decode_deep( $_POST["formula"] ); + + $form->update($_POST); + //Event::log($_POST["id"], "change", 4, "maintain", + // //TRANS: %s is the user login + // sprintf(__('%s updates an item'), $_SESSION["glpiname"])); + + Html::back(); + +} else { + // Html::header(Change::getTypeName(Session::getPluralNumber()), $_SERVER['PHP_SELF'], "helpdesk", "change"); + Html::header(__('Form Validation - Form', 'formvalidation'), $_SERVER['PHP_SELF'], "config", "PluginFormvalidationMenu", "formvalidationform"); + $form->display($_GET); + Html::footer(); +} diff --git a/front/formvalidation.backup.php b/front/formvalidation.backup.php new file mode 100644 index 0000000..66eb02d --- /dev/null +++ b/front/formvalidation.backup.php @@ -0,0 +1,48 @@ +"; + $itemtype = $_REQUEST['itemtype']; + echo "".__('Back').""; + echo ""; + Html::redirect("formvalidation.backup.php?action=export&filename=".$_GET['filename']); + break; + case "import": + PluginFormvalidationPage::displayImportFormvalidationForm(); + break; + case "process_import": + PluginFormvalidationPage::processImportPage(); + Html::back(); + break; +} + +if ($action != "export") { + Html::footer(); +} diff --git a/front/itemtype.form.php b/front/itemtype.form.php new file mode 100644 index 0000000..9a6678a --- /dev/null +++ b/front/itemtype.form.php @@ -0,0 +1,5 @@ +. - -------------------------------------------------------------------------- - */ - -/** @file -* @brief -*/ - -include ('../../../inc/includes.php'); - -if (empty($_GET["id"])) { - $_GET["id"] = ''; -} - -Session::checkLoginUser(); - -$page = new PluginFormvalidationPage(); -if (isset($_POST["add"])) { - $page->check(-1, CREATE, $_POST); - - $newID = $page->add($_POST); - if ($_SESSION['glpibackcreated']) { - Html::redirect($page->getFormURL()."?id=".$newID); - } else { - Html::back(); - } - -} else if (isset($_POST["purge"])) { - $page->check($_POST["id"], PURGE); - $page->delete($_POST, 1); - - $page->redirectToList(); - -} else if (isset($_POST["update"])) { - $page->check($_POST["id"], UPDATE); - - $page->update($_POST); - - Html::back(); - -} else { - // Html::header(Change::getTypeName(Session::getPluralNumber()), $_SERVER['PHP_SELF'], "helpdesk", "change"); - Html::header(__('Form Validation - Page', 'formvalidation'), $_SERVER['PHP_SELF'], "config", "PluginFormvalidationMenu", "formvalidationpage"); - $page->display($_GET); - Html::footer(); -} +. + -------------------------------------------------------------------------- + */ + +/** @file +* @brief +*/ + +include ('../../../inc/includes.php'); + +if (empty($_GET["id"])) { + $_GET["id"] = ''; +} + +Session::checkLoginUser(); + +$page = new PluginFormvalidationPage(); +if (isset($_POST["add"])) { + $page->check(-1, CREATE, $_POST); + + $newID = $page->add($_POST); + if ($_SESSION['glpibackcreated']) { + Html::redirect($page->getFormURL()."?id=".$newID); + } else { + Html::back(); + } + +} else if (isset($_POST["purge"])) { + $page->check($_POST["id"], PURGE); + $page->delete($_POST, 1); + + $page->redirectToList(); + +} else if (isset($_POST["update"])) { + $page->check($_POST["id"], UPDATE); + + $page->update($_POST); + + Html::back(); + +} else { + // Html::header(Change::getTypeName(Session::getPluralNumber()), $_SERVER['PHP_SELF'], "helpdesk", "change"); + Html::header(__('Form Validation - Page', 'formvalidation'), $_SERVER['PHP_SELF'], "config", "PluginFormvalidationMenu", "formvalidationpage"); + $page->display($_GET); + Html::footer(); +} diff --git a/front/page.php b/front/page.php index 1cf4c88..df559de 100644 --- a/front/page.php +++ b/front/page.php @@ -6,7 +6,7 @@ if (Session::haveRight('config', READ) || Session::haveRight("config", UPDATE)) { //$process=new PluginFormvalidationPage(); - + PluginFormvalidationPage::titleBackup(); Search::show('PluginFormvalidationPage'); } else { diff --git a/front/user.form.php b/front/user.form.php index c98956f..ef1730e 100644 --- a/front/user.form.php +++ b/front/user.form.php @@ -1,44 +1,44 @@ -. --------------------------------------------------------------------------- -*/ - -define('GLPI_ROOT', '../../..'); -include (GLPI_ROOT . "/inc/includes.php"); - - -$pref = new PluginFormvalidationUser(); -if (isset($_POST["update"])) { - - if (isset($_POST['editmode']) && $_POST['users_id'] == Session::getLoginUserID()) { - $_SESSION['glpiformvalidationeditmode'] = $_POST['editmode']; - } - - Html::back(); -} - -Html::redirect($CFG_GLPI["root_doc"]."/front/preference.php?forcetab=". - urlencode('PluginFormvalidationUser$1')); +. +-------------------------------------------------------------------------- +*/ + +define('GLPI_ROOT', '../../..'); +include (GLPI_ROOT . "/inc/includes.php"); + + +$pref = new PluginFormvalidationUser(); +if (isset($_POST["update"])) { + + if (isset($_POST['editmode']) && $_POST['users_id'] == Session::getLoginUserID()) { + $_SESSION['glpiformvalidationeditmode'] = $_POST['editmode']; + } + + Html::back(); +} + +Html::redirect($CFG_GLPI["root_doc"]."/front/preference.php?forcetab=". + urlencode('PluginFormvalidationUser$1')); diff --git a/hook.php b/hook.php index 2f332a2..5f066ee 100644 --- a/hook.php +++ b/hook.php @@ -31,245 +31,17 @@ * @return boolean */ function plugin_formvalidation_install() { - global $DB; + global $DB, $CFG_GLPI; $dbu = new DbUtils(); - if (!$DB->tableExists("glpi_plugin_formvalidation_configs")) { - $query = "CREATE TABLE `glpi_plugin_formvalidation_configs` ( - `id` INT(11) NOT NULL AUTO_INCREMENT, - `css_mandatory` VARCHAR(200) NOT NULL DEFAULT '{\"background-color\":\"lightgrey\", \"font-weight\":\"bold\"}', - `css_error` VARCHAR(200) NOT NULL DEFAULT '{\"background-color\": \"red\"}', - PRIMARY KEY (`id`) - ) - ENGINE=InnoDB - ; - "; - - $DB->query($query) or die("Error creating glpi_plugin_formvalidation_configs " . $DB->error()); - $DB->insertOrDie( - 'glpi_plugin_formvalidation_configs', [ - 'id' => '1', - ], - "Error inserting default config into glpi_plugin_formvalidation_configs " - ); - // $query = "INSERT INTO `glpi_plugin_formvalidation_configs` (`id`) VALUES (1);"; - //$DB->query($query) or die("Error inserting default config into glpi_plugin_formvalidation_configs " . $DB->error()); - } - - if (!$DB->tableExists("glpi_plugin_formvalidation_itemtypes")) { - $query = "CREATE TABLE `glpi_plugin_formvalidation_itemtypes` ( - `id` INT(11) NOT NULL AUTO_INCREMENT, - `name` VARCHAR(255) NOT NULL, - `itemtype` VARCHAR(100) NOT NULL, - `URL_path_part` VARCHAR(255) NOT NULL, - PRIMARY KEY (`id`), - UNIQUE INDEX `itemtype` (`itemtype`), - UNIQUE INDEX `name` (`name`) - ) - COLLATE='utf8_general_ci' - ENGINE=InnoDB - ; - "; - - $DB->query($query) or die("Error creating glpi_plugin_formvalidation_itemtypes " . $DB->error()); - - // init data - $query = "INSERT INTO `glpi_plugin_formvalidation_itemtypes` (`id`, `name`, `itemtype`, `URL_path_part`) - VALUES (1, 'Computer', 'Computer', '/front/computer.form.php'), - (2, 'Monitor', 'Monitor', '/front/monitor.form.php'), - (3, 'Software', 'Software', '/front/software.form.php'), - (4, 'NetworkEquipment', 'NetworkEquipment', '/front/networkequipment.form.php'), - (5, 'Peripheral', 'Peripheral', '/front/peripheral.form.php'), - (6, 'Printer', 'Printer', '/front/printer.form.php'), - (7, 'CartridgeItem', 'CartridgeItem', '/front/cartridgeitem.form.php'), - (8, 'ConsumableItem', 'ConsumableItem', '/front/consumableitem.form.php'), - (9, 'Phone', 'Phone', '/front/phone.form.php'), - (10, 'Ticket', 'Ticket', '/front/ticket.form.php,'), - (11, 'Problem', 'Problem', '/front/problem.form.php'), - (12, 'TicketRecurrent', 'TicketRecurrent', '/front/ticketrecurrent.form.php'), - (13, 'Budget', 'Budget', '/front/budget.form.php'), - (14, 'Supplier', 'Supplier', '/front/supplier.form.php'), - (15, 'Contact', 'Contact', '/front/contact.form.php'), - (16, 'Contract', 'Contract', '/front/contract.form.php'), - (17, 'Document', 'Document', '/front/document.form.php'), - (18, 'Notes', 'Notes', '/front/notes.form.php'), - (19, 'RSSFeed', 'RSSFeed', '/front/rssfeed.form.php'), - (20, 'User', 'User', '/front/user.form.php'), - (21, 'Group', 'Group', '/front/group.form.php'), - (22, 'Entity', 'Entity', '/front/entity.form.php'), - (23, 'Profile', 'Profile', '/front/profile.form.php'), - (24, 'PluginFormcreatorForm', 'PluginFormcreatorForm', '/plugins/formcreator/formdisplay.php'), - (25, 'PluginFormvalidationPage', 'PluginFormvalidationPage', '/plugins/formvalidation/front/page.form.php'), - (26, 'PluginFormvalidationForm', 'PluginFormvalidationForm', '/plugins/formvalidation/front/form.form.php'), - (27, 'PluginFormvalidationField', 'PluginFormvalidationField', '/plugins/formvalidation/front/field.form.php'), - (28, 'PluginRayusermanagementticketRayusermanagementticket', 'PluginRayusermanagementticketRayusermanagementticket', '/plugins/rayusermanagementticket/front/rayusermanagementticket.helpdesk.public.php');"; - $DB->query($query) or die("Error inserting default item types into glpi_plugin_formvalidation_itemtypes " . $DB->error()); - - } - - if (!$DB->tableExists("glpi_plugin_formvalidation_pages")) { - $query = "CREATE TABLE `glpi_plugin_formvalidation_pages` ( - `id` INT(11) NOT NULL AUTO_INCREMENT, - `name` VARCHAR(200) NULL DEFAULT NULL, - `entities_id` INT(11) NOT NULL DEFAULT '0', - `itemtypes_id` INT(11) NOT NULL DEFAULT '0', - `is_recursive` TINYINT(1) NOT NULL DEFAULT '0', - `is_active` TINYINT(1) NOT NULL DEFAULT '1', - `comment` TEXT NULL, - `date_mod` TIMESTAMP NULL DEFAULT NULL, - PRIMARY KEY (`id`), - INDEX `itemtypes_id` (`itemtypes_id`), - INDEX `entities_id` (`entities_id`) - ) - COLLATE='utf8_general_ci' - ENGINE=InnoDB - ; - "; - - $DB->query($query) or die("Error creating glpi_plugin_formvalidation_pages " . $DB->error()); - - // init data - $query = "INSERT INTO `glpi_plugin_formvalidation_pages` (`id`, `name`, `entities_id`, `itemtypes_id`, `is_recursive`, `is_active`, `comment`, `date_mod`) - VALUES (1, 'Form Validation Page', 0, 25, 1, 1, NULL, NULL), - (2, 'Form Validation Form', 0, 26, 1, 1, NULL, NULL), - (3, 'Form Validation Field', 0, 27, 1, 1, NULL, NULL), - (4, 'Ticket Validations', 0, 10, 1, 1, NULL, NULL);"; - $DB->query($query) or die("Error inserting default pages into glpi_plugin_formvalidation_pages " . $DB->error()); + // new installation + include_once(GLPI_ROOT."/plugins/formvalidation/install/install.php"); + formvalidation_install(); } else { - if (!$DB->fieldExists('glpi_plugin_formvalidation_pages', 'itemtypes_id')) { - $query = "ALTER TABLE `glpi_plugin_formvalidation_pages` - ADD COLUMN `itemtypes_id` INT(11) NOT NULL DEFAULT '0' AFTER `itemtype`, - ADD INDEX `itemtypes_id` (`itemtypes_id`); - "; - $DB->query($query) or die("Error inserting itemtypes_id field into glpi_plugin_formvalidation_pages " . $DB->error()); - } - - // check if migration is neccessary - $pages = $dbu->getAllDataFromTable('glpi_plugin_formvalidation_pages', ['itemtypes_id' => '0']); - if (count($pages)) { - // migration of itemtype into itemtypes_id - $query = "UPDATE glpi_plugin_formvalidation_pages AS gpfp, glpi_plugin_formvalidation_itemtypes AS gpfi - SET gpfp.itemtypes_id = gpfi.id - WHERE gpfi.itemtype = gpfp.itemtype;"; - $DB->query($query) or die("Error migrating itemtype into itemtypes_id field in glpi_plugin_formvalidation_pages " . $DB->error()); - - // check if all pages have been migrated - $pages = $dbu->getAllDataFromTable('glpi_plugin_formvalidation_pages', ['itemtypes_id' => '0']); - if (count($pages)) { - die("Error some itemtype can't be migrated into itemtypes_id field from glpi_plugin_formvalidation_pages,
- please check the list of itemtype in glpi_plugin_formvalidation_pages and in glpi_plugin_formvalidation_itemtypes,
- fix the issue and restart install of the plugin."); - } - } - - if ($DB->fieldExists('glpi_plugin_formvalidation_pages', 'itemtype') && !count($pages)) { - // delete itemtype field after migration is done - $query = "ALTER TABLE `glpi_plugin_formvalidation_pages` - DROP COLUMN `itemtype`, - DROP INDEX `itemtype`;"; - $DB->query($query) or die("Error deleting itemtypes field and index from glpi_plugin_formvalidation_pages " . $DB->error()); - - // delete the itemtype field from glpi_displaypreferences - $query = "UPDATE `glpi_displaypreferences` - SET num = 803 - WHERE itemtype = 'PluginFormvalidationPage' AND num = 3;"; - $DB->query($query) or die("Error updating num in glpi_displaypreferences " . $DB->error()); - - } - } - - if (!$DB->tableExists("glpi_plugin_formvalidation_forms")) { - $query = "CREATE TABLE `glpi_plugin_formvalidation_forms` ( - `id` INT(11) NOT NULL AUTO_INCREMENT, - `name` VARCHAR(200) NULL DEFAULT NULL, - `pages_id` INT(11) NOT NULL, - `css_selector` VARCHAR(255) NOT NULL, - `is_createitem` TINYINT(1) NOT NULL DEFAULT '0', - `is_active` TINYINT(1) NOT NULL DEFAULT '1', - `use_for_massiveaction` TINYINT(1) NOT NULL DEFAULT '0', - `formula` TEXT NULL, - `comment` TEXT NULL, - `date_mod` TIMESTAMP NULL DEFAULT NULL, - PRIMARY KEY (`id`), - INDEX `pages_id` (`pages_id`) - ) - COLLATE='utf8_general_ci' - ENGINE=InnoDB - ; - "; - - $DB->query($query) or die("Error creating glpi_plugin_formvalidation_forms" . $DB->error()); - - $query = "INSERT INTO `glpi_plugin_formvalidation_forms` (`id`, `name`, `pages_id`, `css_selector`, `is_createitem`, `is_active`, `use_for_massiveaction`, `formula`, `comment`, `date_mod`) - VALUES (1, 'form(/plugins/formvalidation/front/page.form.php)', 1, 'form[name=\\\"form\\\"][action=\\\"/plugins/formvalidation/front/page.form.php\\\"]', 0, 1, 0, NULL, NULL, NULL), - (2, 'form(/plugins/formvalidation/front/form.form.php)', 2, 'form[name=\\\"form\\\"][action=\\\"/plugins/formvalidation/front/form.form.php\\\"]', 0, 1, 0, NULL, NULL, NULL), - (3, 'form(/plugins/formvalidation/front/field.form.php)', 3, 'form[name=\\\"form\\\"][action=\\\"/plugins/formvalidation/front/field.form.php\\\"]', 0, 1, 0, NULL, NULL, NULL), - (4, 'Simplified interface Creation', 4, 'form[name=\\\"helpdeskform\\\"][action=\\\"/front/tracking.injector.php\\\"]', 1, 1, 0, NULL, NULL, NULL), - (5, 'Followup Validations', 4, 'form[name=\\\"form\\\"][action=\\\"/front/ticketfollowup.form.php\\\"]', 0, 1, 0, NULL, NULL, NULL), - (6, 'Central Interface Edit', 4, 'form[name=\\\"form_ticket\\\"][action=\\\"/front/ticket.form.php\\\"]', 0, 1, 1, NULL, NULL,NULL), - (7, 'Central Interface Creation', 4, 'form[name=\\\"form_ticket\\\"][action=\\\"/front/ticket.form.php\\\"]', 1, 1, 0, NULL, NULL, NULL), - (8, 'form(/plugins/formvalidation/front/page.form.php)', 1, 'form[name=\\\"form\\\"][action=\\\"/plugins/formvalidation/front/page.form.php\\\"]', 1, 1, 0, NULL, NULL, NULL);"; - - $DB->query($query) or die("Error inserting default data into glpi_plugin_formvalidation_forms " . $DB->error()); - - } else { - if (!$DB->fieldExists('glpi_plugin_formvalidation_forms', 'use_for_massiveaction')) { - $query = "ALTER TABLE `glpi_plugin_formvalidation_forms` - ADD COLUMN `use_for_massiveaction` TINYINT(1) NOT NULL DEFAULT '0' AFTER `is_active`; - "; - $DB->query($query) or die("Error inserting use_for_massiveaction field into glpi_plugin_formvalidation_forms " . $DB->error()); - } - } - - if (!$DB->tableExists("glpi_plugin_formvalidation_fields")) { - $query = "CREATE TABLE `glpi_plugin_formvalidation_fields` ( - `id` INT(11) NOT NULL AUTO_INCREMENT, - `name` VARCHAR(200) NULL DEFAULT NULL, - `forms_id` INT(11) NOT NULL, - `css_selector_value` VARCHAR(255) NULL DEFAULT NULL, - `css_selector_altvalue` VARCHAR(255) NULL DEFAULT NULL, - `css_selector_errorsign` VARCHAR(255) NULL DEFAULT NULL, - `css_selector_mandatorysign` VARCHAR(255) NULL DEFAULT NULL, - `is_active` TINYINT(1) NOT NULL DEFAULT '1', - `show_mandatory` TINYINT(1) NOT NULL DEFAULT '1', - `show_mandatory_if` TEXT NULL, - `formula` TEXT NULL, - `comment` TEXT NULL, - `date_mod` TIMESTAMP NULL DEFAULT NULL, - PRIMARY KEY (`id`), - UNIQUE INDEX `forms_id_css_selector_value` (`forms_id`, `css_selector_value`), - INDEX `forms_id` (`forms_id`) - ) - COLLATE='utf8_general_ci' - ENGINE=InnoDB - ; - "; - - $DB->query($query) or die("Error creating glpi_plugin_formvalidation_fields " . $DB->error()); - - $query = "INSERT INTO `glpi_plugin_formvalidation_fields` (`id`, `name`, `forms_id`, `css_selector_value`, `css_selector_altvalue`, `css_selector_errorsign`, `css_selector_mandatorysign`, `is_active`, `show_mandatory`, `show_mandatory_if`, `formula`, `comment`, `date_mod`) - VALUES (1, 'Name', 1, 'div>table>tbody>tr:eq(1)>td:eq(1) input[name=\\\"name\\\"]', NULL, 'div>table>tbody>tr:eq(1)>td:eq(1)', 'div>table>tbody>tr:eq(1)>td:eq(0)', 1, 1, NULL, NULL, NULL, NULL), - (2, 'Name', 2, 'div>table>tbody>tr:eq(1)>td:eq(1) input[name=\\\"name\\\"]', NULL, 'div>table>tbody>tr:eq(1)>td:eq(1)', 'div>table>tbody>tr:eq(1)>td:eq(0)', 1, 1, NULL, NULL, NULL, NULL), - (3, 'CSS Selector', 2, 'div>table>tbody>tr:eq(4)>td:eq(1) input[name=\\\"css_selector\\\"]', NULL, 'div>table>tbody>tr:eq(4)>td:eq(1)', 'div>table>tbody>tr:eq(4)>td:eq(0)', 1, 1, NULL, NULL, NULL, NULL), - (4, 'Name', 3, 'div>table>tbody>tr:eq(1)>td:eq(1) input[name=\\\"name\\\"]', NULL, 'div>table>tbody>tr:eq(1)>td:eq(1)', 'div>table>tbody>tr:eq(1)>td:eq(0)', 1, 1, NULL, NULL, NULL, NULL), - (5, 'Value CSS selector', 3, 'div>table>tbody>tr:eq(3)>td:eq(1) input[name=\\\"css_selector_value\\\"]', NULL, 'div>table>tbody>tr:eq(3)>td:eq(1)', 'div>table>tbody>tr:eq(3)>td:eq(0)', 1, 1, NULL, NULL, NULL, NULL), - (6, 'Force mandatory sign', 3, 'div>table>tbody>tr:eq(6)>td:eq(1)>span input[name=\\\"show_mandatory\\\"]', NULL, 'div>table>tbody>tr:eq(6)>td:eq(1)', 'div>table>tbody>tr:eq(6)>td:eq(0)', 1, 0, NULL, NULL, NULL, NULL), - (7, 'Mandatory sign formula', 3, 'div>table>tbody>tr:eq(7)>td:eq(1) input[name=\\\"show_mandatory_if\\\"]', NULL, 'div>table>tbody>tr:eq(7)>td:eq(1)', 'div>table>tbody>tr:eq(7)>td:eq(0)', 0, 0, NULL, NULL, NULL, NULL), - (8, 'Mandatory sign CSS selector', 3, 'div>table>tbody>tr:eq(8)>td:eq(1) input[name=\\\"css_selector_mandatorysign\\\"]', NULL, 'div>table>tbody>tr:eq(8)>td:eq(1)', 'div>table>tbody>tr:eq(8)>td:eq(0)', 1, 0, '#6 || #7!=\'\'', NULL, NULL, NULL), - (9, 'Title', 4, 'div>table>tbody>tr:eq(8)>td:eq(1) input[name=\\\"name\\\"]', NULL, 'div>table>tbody>tr:eq(8)>td:eq(1)', 'div>table>tbody>tr:eq(8)>td:eq(0)', 1, 1, NULL, NULL, NULL, NULL), - (10, 'Description', 4, 'div>table>tbody>tr:eq(9)>td:eq(1)>div textarea[name=\\\"content\\\"]', NULL, 'div>table>tbody>tr:eq(9)>td:eq(1)', 'div>table>tbody>tr:eq(9)>td:eq(0)', 1, 0, '#11 == 2','(#11 == 1) || (#11 == 2 && #.length > 10 && countWords(#) > 5)', NULL, NULL), - (11, 'Type', 4, 'div>table>tbody>tr:eq(1)>td:eq(1) select[name=\\\"type\\\"]', NULL, 'div>table>tbody>tr:eq(1)>td:eq(1)', 'div>table>tbody>tr:eq(1)>td:eq(0)', 1, 0, NULL, NULL, NULL, NULL), - (12, 'Description', 5, 'div>table>tbody>tr:eq(1)>td:eq(1) textarea[name=\\\"content\\\"]', NULL, 'div>table>tbody>tr:eq(1)>td:eq(1)', 'div>table>tbody>tr:eq(1)>td:eq(0)', 1, 1, NULL, '#.length > 10 && countWords(#) > 3', NULL, NULL), - (13, 'Title', 6, 'div>table:eq(2)>tbody>tr:eq(0)>td input[name=\\\"name\\\"]', NULL, 'div>table:eq(2)>tbody>tr:eq(0)>td', 'div>table:eq(2)>tbody>tr:eq(0)>th', 1, 1, NULL, NULL, NULL, NULL), - (14, 'Description', 6, 'div>table:eq(2)>tbody>tr:eq(1)>td>div textarea[name=\\\"content\\\"]', NULL, 'div>table:eq(2)>tbody>tr:eq(1)>td', 'div>table:eq(2)>tbody>tr:eq(1)>th', 1, 1, NULL, NULL, NULL, NULL), - (15, 'Type', 6, 'div>table:eq(1)>tbody>tr:eq(0)>td:eq(0) select[name=\\\"type\\\"]', NULL, 'div>table:eq(1)>tbody>tr:eq(0)>td:eq(0)', 'div>table:eq(1)>tbody>tr:eq(0)>th:eq(0)', 1, 0, NULL, NULL, NULL, NULL), - (16, 'Title', 7, 'div>table:eq(2)>tbody>tr:eq(0)>td input[name=\\\"name\\\"]', NULL, 'div>table:eq(2)>tbody>tr:eq(0)>td', 'div>table:eq(2)>tbody>tr:eq(0)>th', 1, 1, NULL, NULL, NULL, NULL), - (17, 'Description', 7, 'div>table:eq(2)>tbody>tr:eq(1)>td>div textarea[name=\\\"content\\\"]', NULL, 'div>table:eq(2)>tbody>tr:eq(1)>td', 'div>table:eq(2)>tbody>tr:eq(1)>th', 1, 1, NULL, NULL, NULL, NULL), - (18, 'Name', 8, 'div>table>tbody>tr:eq(1)>td:eq(1) input[name=\\\"name\\\"]', NULL, 'div>table>tbody>tr:eq(1)>td:eq(1)', 'div>table>tbody>tr:eq(1)>td:eq(0)', 1, 1, NULL, NULL, NULL, NULL); "; - - $DB->query($query) or die("Error inserting default data into glpi_plugin_formvalidation_fields " . $DB->error()); - + // upgrade installation + include_once(GLPI_ROOT."/plugins/formvalidation/install/update.php"); + formvalidation_update(); } return true; @@ -286,4 +58,36 @@ function plugin_formvalidation_uninstall() { return true; } +/** + * Define Dropdown tables to be manage in GLPI : + **/ +function plugin_formvalidation_getDropdown() { + + return ['PluginFormvalidationItemtype' => PluginFormvalidationItemtype::getTypeName(2)]; +} + + +function plugin_formvalidation_MassiveActions($type) { + $actions = []; + switch ($type) { + case 'PluginFormvalidationPage' : + $myclass = 'PluginFormvalidationPage'; + $action_key = 'exportPage'; + $action_label = __("Export Page", 'Export'); + $actions[$myclass.MassiveAction::CLASS_ACTION_SEPARATOR.$action_key] + = $action_label; + + break; + case 'PluginFormvalidationForm': + $myclass = 'PluginFormvalidationForm'; + $action_key = 'exportForm'; + $action_label = __("Export Form", 'Export'); + $actions[$myclass.MassiveAction::CLASS_ACTION_SEPARATOR.$action_key] + = $action_label; + + break; + } + return $actions; +} + diff --git a/inc/config.class.php b/inc/config.class.php index 2ab4b77..9d65461 100644 --- a/inc/config.class.php +++ b/inc/config.class.php @@ -1,48 +1,48 @@ -. --------------------------------------------------------------------------- -*/ - -class PluginFormvalidationConfig extends CommonDBTM { - - static private $_instance = null; - - /** - * Singleton for the unique config record - * @return PluginFormvalidationConfig singleton - */ - static function getInstance() { - - if (!isset(self::$_instance)) { - self::$_instance = new self(); - if (!self::$_instance->getFromDB(1)) { - self::$_instance->getEmpty(); - } - } - return self::$_instance; - } -} - +. +-------------------------------------------------------------------------- +*/ + +class PluginFormvalidationConfig extends CommonDBTM { + + static private $_instance = null; + + /** + * Singleton for the unique config record + * @return PluginFormvalidationConfig singleton + */ + static function getInstance() { + + if (!isset(self::$_instance)) { + self::$_instance = new self(); + if (!self::$_instance->getFromDB(1)) { + self::$_instance->getEmpty(); + } + } + return self::$_instance; + } +} + diff --git a/inc/field.class.php b/inc/field.class.php index 0181eb1..ee4d4ca 100644 --- a/inc/field.class.php +++ b/inc/field.class.php @@ -455,6 +455,21 @@ function prepareInputForUpdate($input) { return $input; } + function post_addItem() { + global $DB,$CFG_GLPI; + $id = $this->fields['id']; + $guid = $CFG_GLPI['url_base']."/plugins/formvalidation/ajax/fields/".time()."/".rand()."/".$id; + $DB->updateOrDie( + 'glpi_plugin_formvalidation_fields', + [ + 'guid' => md5($guid) + ], + [ + 'id' => $id + ] + ); + } + } diff --git a/inc/form.class.php b/inc/form.class.php index 1eae796..deff7db 100644 --- a/inc/form.class.php +++ b/inc/form.class.php @@ -465,6 +465,21 @@ function post_purgeItem() { } } + function post_addItem() { + global $DB,$CFG_GLPI; + $id = $this->fields['id']; + $guid = $CFG_GLPI['url_base']."/plugins/formvalidation/ajax/form/".time()."/".rand()."/".$id; + $DB->updateOrDie( + 'glpi_plugin_formvalidation_forms', + [ + 'guid' => md5($guid) + ], + [ + 'id' => $id + ] + ); + } + } diff --git a/inc/hook.class.php b/inc/hook.class.php index ad91a44..ce8e2f9 100644 --- a/inc/hook.class.php +++ b/inc/hook.class.php @@ -1,211 +1,211 @@ -getType() == 'TicketSatisfaction') { - echo ""; - echo ""; - } - } - - const HELPER_FUNCTIONS = <<<'EOT' - //------------------------------------------ - // helper function to verify if a string - // is really a date - // uses the datepicker JQuery plugin - //------------------------------------------ - function isValidDate(string) { - try { - if (string.length == 0) { - return false; - } - $.datepicker.parseDate($('.hasDatepicker').datepicker('option', 'dateFormat'), string); - return true; - } catch (e) { - return false; - } - } - - //------------------------------------------ - // helper function to verify a if a string - // is really a time from 00:00[:00] to 23:59[:59] - //------------------------------------------ - function isValidTime(str) { - return /^(?:[0-1]\d|2[0-3]):[0-5]\d(?::[0-5]\d)?$/.test(str); - } - - //------------------------------------------ - // helper function to verify a if a string - // is really an integer - //------------------------------------------ - function isValidInteger(str) { - return /^\d+$/.test(str); - } - - //------------------------------------------ - // helper function to count words in a given string - // returns quantity of words - //------------------------------------------ - function countWords(str) { - return str.split(/\W+/).length; - } - - //------------------------------------------ - // helper function to verify a if a string - // is really an IPV4 address - // uses the datapicker JQuery plugin - //------------------------------------------ - function isValidIPv4(ipaddress) { - return /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(ipaddress) ; - } - - - //------------------------------------------ - // helper function to verify a if a string - // is really an IPV6 address - // uses the datapicker JQuery plugin - //------------------------------------------ - function isValidIPv6(ipaddress) { - return /^((?:[0-9A-Fa-f]{1,4}))((?::[0-9A-Fa-f]{1,4}))*::((?:[0-9A-Fa-f]{1,4}))((?::[0-9A-Fa-f]{1,4}))*|((?:[0-9A-Fa-f]{1,4}))((?::[0-9A-Fa-f]{1,4})){7}$/.test(ipaddress); - } - - //------------------------------------------ - // helper function to verify a if a string - // is really an email address - // will use the input type=email if it exists (HTML5) - // otherwise will use a basic verification. - //------------------------------------------ - function isValidEmail(value) { - var input = document.createElement("input"); - - input.type = "email"; - input.value = value; - - return typeof input.checkValidity == "function" ? input.checkValidity() : /^\S+@\S+\.\S+$/.test(value); - } - - //------------------------------------------ - // helper function to verify a if a string - // is really a MAC address - //------------------------------------------ - function isValidMacAddress(str) { - return /^[\da-f]{2}([:-])(?:[\da-f]{2}\1){4}[\da-f]{2}$/i.test(str); - } - -EOT; - - /** - * Summary of plugin_pre_item_update_formvalidation - * @param mixed $parm the object that is going to be updated - * @return mixed - */ - static public function plugin_pre_item_update_formvalidation($parm) { - global $DB; - - // to be executed only for massive actions - if (strstr($_SERVER['PHP_SELF'], "/front/massiveaction.php")) { - $ret=[]; - - //return; - //clean input values - $input = $parm->input; - unset( $input['id'] ); - foreach ($input as $key => $val) { - if (preg_match("/^_/", $key )) { - unset( $input[$key] ); - } - } - $itemvalues = array_merge( $parm->fields, $input ); - $formulas = []; - $fieldnames = []; - $fieldtitles = []; - - $query = "SELECT DISTINCT glpi_plugin_formvalidation_forms.* FROM glpi_plugin_formvalidation_forms - LEFT JOIN glpi_plugin_formvalidation_fields ON glpi_plugin_formvalidation_forms.id=glpi_plugin_formvalidation_fields.forms_id - LEFT JOIN glpi_plugin_formvalidation_pages ON glpi_plugin_formvalidation_pages.id=glpi_plugin_formvalidation_forms.pages_id - LEFT JOIN glpi_plugin_formvalidation_itemtypes ON glpi_plugin_formvalidation_itemtypes.id=glpi_plugin_formvalidation_pages.itemtypes_id - WHERE glpi_plugin_formvalidation_itemtypes.itemtype ='".$parm->getType()."' - AND glpi_plugin_formvalidation_forms.use_for_massiveaction=1 - "; // css_selector like '%\"".$_SERVER['PHP_SELF']."%' - $where = ""; - foreach (array_keys($input) as $val) { - if ($where != "") { - $where .= "\nOR "; - } - $where .= "glpi_plugin_formvalidation_fields.css_selector_value like '%\"$val%'"; - } - - foreach ($DB->request( $query." AND ( $where )" ) as $form) { - foreach ($DB->request("SELECT * from glpi_plugin_formvalidation_fields WHERE forms_id = ".$form['id']) as $field) { - $matches = []; - if (preg_match('/\[(name|id\^)=\\\\{0,1}"(?[a-z_\-0-9]+)\\\\{0,1}"\]/i', $field['css_selector_value'], $matches)) { - $fieldnames[$field['id']]=$matches['name']; - $formulas[$field['id']]=($field['formula'] ? $field['formula'] : '#>0 || #!=""'); - $fieldtitles[$field['id']]=$field['name']; - } - } - - $values=[]; - foreach ($formulas as $fieldnum => $formula) { - $values[$fieldnum] = ($itemvalues[$fieldnames[$fieldnum]] ? $itemvalues[$fieldnames[$fieldnum]] : "" ); - } - $formulaJS=[]; - foreach ($formulas as $fieldnum => $formula) { - $formulaJS[$fieldnum] = $formula; - foreach ($values as $valnum => $val) { - if ($fieldnum == $valnum) { - $regex = '/#\B/i'; - } else { - $regex = '/#'.$valnum.'\b/i'; - } - //$formulaJS[$fieldnum] = preg_replace( $regex, '"'.Toolbox::addslashes_deep( $val ).'"', $formulaJS[$fieldnum] ) ; - $formulaJS[$fieldnum] = preg_replace( $regex, "PHP.val[$valnum]", $formulaJS[$fieldnum] ); - } - } - - $v8 = new V8Js(); - $v8->val = $values; - $ret=[]; - foreach ($formulaJS as $index => $formula) { - try { - if (!$v8->executeString(self::HELPER_FUNCTIONS." - exec = $formula; - " ) ) { - Session::addMessageAfterRedirect( __('Mandatory fields or wrong value: ').__($fieldtitles[$index]), true, ERROR ); - $ret[]=$fieldnames[$index]; - } - } catch (Exception $ex) { - Session::addMessageAfterRedirect( __('Error: ').__($ex->message), false, ERROR ); - $ret[]=$fieldnames[$index]; - } - } - } - - if (count($ret) > 0) { - $parm->input = []; //to prevent update of unconsistant data - } - - } - } - - static public function plugin_pre_show_tab_formvalidation($parm) { - echo ''; - } - -} +getType() == 'TicketSatisfaction') { + echo ""; + echo ""; + } + } + + const HELPER_FUNCTIONS = <<<'EOT' + //------------------------------------------ + // helper function to verify if a string + // is really a date + // uses the datepicker JQuery plugin + //------------------------------------------ + function isValidDate(string) { + try { + if (string.length == 0) { + return false; + } + $.datepicker.parseDate($('.hasDatepicker').datepicker('option', 'dateFormat'), string); + return true; + } catch (e) { + return false; + } + } + + //------------------------------------------ + // helper function to verify a if a string + // is really a time from 00:00[:00] to 23:59[:59] + //------------------------------------------ + function isValidTime(str) { + return /^(?:[0-1]\d|2[0-3]):[0-5]\d(?::[0-5]\d)?$/.test(str); + } + + //------------------------------------------ + // helper function to verify a if a string + // is really an integer + //------------------------------------------ + function isValidInteger(str) { + return /^\d+$/.test(str); + } + + //------------------------------------------ + // helper function to count words in a given string + // returns quantity of words + //------------------------------------------ + function countWords(str) { + return str.split(/\W+/).length; + } + + //------------------------------------------ + // helper function to verify a if a string + // is really an IPV4 address + // uses the datapicker JQuery plugin + //------------------------------------------ + function isValidIPv4(ipaddress) { + return /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(ipaddress) ; + } + + + //------------------------------------------ + // helper function to verify a if a string + // is really an IPV6 address + // uses the datapicker JQuery plugin + //------------------------------------------ + function isValidIPv6(ipaddress) { + return /^((?:[0-9A-Fa-f]{1,4}))((?::[0-9A-Fa-f]{1,4}))*::((?:[0-9A-Fa-f]{1,4}))((?::[0-9A-Fa-f]{1,4}))*|((?:[0-9A-Fa-f]{1,4}))((?::[0-9A-Fa-f]{1,4})){7}$/.test(ipaddress); + } + + //------------------------------------------ + // helper function to verify a if a string + // is really an email address + // will use the input type=email if it exists (HTML5) + // otherwise will use a basic verification. + //------------------------------------------ + function isValidEmail(value) { + var input = document.createElement("input"); + + input.type = "email"; + input.value = value; + + return typeof input.checkValidity == "function" ? input.checkValidity() : /^\S+@\S+\.\S+$/.test(value); + } + + //------------------------------------------ + // helper function to verify a if a string + // is really a MAC address + //------------------------------------------ + function isValidMacAddress(str) { + return /^[\da-f]{2}([:-])(?:[\da-f]{2}\1){4}[\da-f]{2}$/i.test(str); + } + +EOT; + + /** + * Summary of plugin_pre_item_update_formvalidation + * @param mixed $parm the object that is going to be updated + * @return mixed + */ + static public function plugin_pre_item_update_formvalidation($parm) { + global $DB; + + // to be executed only for massive actions + if (strstr($_SERVER['PHP_SELF'], "/front/massiveaction.php")) { + $ret=[]; + + //return; + //clean input values + $input = $parm->input; + unset( $input['id'] ); + foreach ($input as $key => $val) { + if (preg_match("/^_/", $key )) { + unset( $input[$key] ); + } + } + $itemvalues = array_merge( $parm->fields, $input ); + $formulas = []; + $fieldnames = []; + $fieldtitles = []; + + $query = "SELECT DISTINCT glpi_plugin_formvalidation_forms.* FROM glpi_plugin_formvalidation_forms + LEFT JOIN glpi_plugin_formvalidation_fields ON glpi_plugin_formvalidation_forms.id=glpi_plugin_formvalidation_fields.forms_id + LEFT JOIN glpi_plugin_formvalidation_pages ON glpi_plugin_formvalidation_pages.id=glpi_plugin_formvalidation_forms.pages_id + LEFT JOIN glpi_plugin_formvalidation_itemtypes ON glpi_plugin_formvalidation_itemtypes.id=glpi_plugin_formvalidation_pages.itemtypes_id + WHERE glpi_plugin_formvalidation_itemtypes.itemtype ='".$parm->getType()."' + AND glpi_plugin_formvalidation_forms.use_for_massiveaction=1 + "; // css_selector like '%\"".$_SERVER['PHP_SELF']."%' + $where = ""; + foreach (array_keys($input) as $val) { + if ($where != "") { + $where .= "\nOR "; + } + $where .= "glpi_plugin_formvalidation_fields.css_selector_value like '%\"$val%'"; + } + + foreach ($DB->request( $query." AND ( $where )" ) as $form) { + foreach ($DB->request("SELECT * from glpi_plugin_formvalidation_fields WHERE forms_id = ".$form['id']) as $field) { + $matches = []; + if (preg_match('/\[(name|id\^)=\\\\{0,1}"(?[a-z_\-0-9]+)\\\\{0,1}"\]/i', $field['css_selector_value'], $matches)) { + $fieldnames[$field['id']]=$matches['name']; + $formulas[$field['id']]=($field['formula'] ? $field['formula'] : '#>0 || #!=""'); + $fieldtitles[$field['id']]=$field['name']; + } + } + + $values=[]; + foreach ($formulas as $fieldnum => $formula) { + $values[$fieldnum] = ($itemvalues[$fieldnames[$fieldnum]] ? $itemvalues[$fieldnames[$fieldnum]] : "" ); + } + $formulaJS=[]; + foreach ($formulas as $fieldnum => $formula) { + $formulaJS[$fieldnum] = $formula; + foreach ($values as $valnum => $val) { + if ($fieldnum == $valnum) { + $regex = '/#\B/i'; + } else { + $regex = '/#'.$valnum.'\b/i'; + } + //$formulaJS[$fieldnum] = preg_replace( $regex, '"'.Toolbox::addslashes_deep( $val ).'"', $formulaJS[$fieldnum] ) ; + $formulaJS[$fieldnum] = preg_replace( $regex, "PHP.val[$valnum]", $formulaJS[$fieldnum] ); + } + } + + $v8 = new V8Js(); + $v8->val = $values; + $ret=[]; + foreach ($formulaJS as $index => $formula) { + try { + if (!$v8->executeString(self::HELPER_FUNCTIONS." + exec = $formula; + " ) ) { + Session::addMessageAfterRedirect( __('Mandatory fields or wrong value: ').__($fieldtitles[$index]), true, ERROR ); + $ret[]=$fieldnames[$index]; + } + } catch (Exception $ex) { + Session::addMessageAfterRedirect( __('Error: ').__($ex->message), false, ERROR ); + $ret[]=$fieldnames[$index]; + } + } + } + + if (count($ret) > 0) { + $parm->input = []; //to prevent update of unconsistant data + } + + } + } + + static public function plugin_pre_show_tab_formvalidation($parm) { + echo ''; + } + +} diff --git a/inc/itemtype.class.php b/inc/itemtype.class.php index f1667ce..ab1af37 100644 --- a/inc/itemtype.class.php +++ b/inc/itemtype.class.php @@ -1,13 +1,50 @@ -false]) { + global $DB; + if (!$id) { + $id = -1; + } + $this->initForm($id); + $this->showFormHeader(['formtitle'=>'Item type','colspan' => 4]); + + echo ""; + echo ""; + echo ""; + echo ""; + echo ""; + echo ""; + echo ""; + echo ""; + echo ""; + echo ""; + echo ""; + echo ""; + + $this->showFormButtons(); + //echo ''; + + } +} diff --git a/inc/menu.class.php b/inc/menu.class.php index 7499586..4604c84 100644 --- a/inc/menu.class.php +++ b/inc/menu.class.php @@ -1,49 +1,49 @@ - 'formvalidationpage', - 'PluginFormvalidationForm' => 'formvalidationform', - 'PluginFormvalidationField' => 'formvalidationfield']; - - foreach ($itemtypes as $itemtype => $option) { - $menu['options'][$option]['title'] = $itemtype::getTypeName(Session::getPluralNumber()); - switch ($itemtype) { - case 'PluginFormvalidationPage': - $menu['options'][$option]['page'] = $itemtype::getSearchURL(false); - $menu['options'][$option]['links']['search'] = $itemtype::getSearchURL(false); - if ($itemtype::canCreate()) { - $menu['options'][$option]['links']['add'] = $itemtype::getFormURL(false); - } - break; - case 'PluginFormvalidationForm': - case 'PluginFormvalidationField': - $menu['options'][$option]['page'] = $itemtype::getSearchURL(false); - $menu['options'][$option]['links']['search'] = $itemtype::getSearchURL(false); - break; - default : - $menu['options'][$option]['page'] = PluginFormvalidationPage::getSearchURL(false); - break; - } - - } - return $menu; - } - - -} + 'formvalidationpage', + 'PluginFormvalidationForm' => 'formvalidationform', + 'PluginFormvalidationField' => 'formvalidationfield']; + + foreach ($itemtypes as $itemtype => $option) { + $menu['options'][$option]['title'] = $itemtype::getTypeName(Session::getPluralNumber()); + switch ($itemtype) { + case 'PluginFormvalidationPage': + $menu['options'][$option]['page'] = $itemtype::getSearchURL(false); + $menu['options'][$option]['links']['search'] = $itemtype::getSearchURL(false); + if ($itemtype::canCreate()) { + $menu['options'][$option]['links']['add'] = $itemtype::getFormURL(false); + } + break; + case 'PluginFormvalidationForm': + case 'PluginFormvalidationField': + $menu['options'][$option]['page'] = $itemtype::getSearchURL(false); + $menu['options'][$option]['links']['search'] = $itemtype::getSearchURL(false); + break; + default : + $menu['options'][$option]['page'] = PluginFormvalidationPage::getSearchURL(false); + break; + } + + } + return $menu; + } + + +} diff --git a/inc/page.class.php b/inc/page.class.php index f3be0eb..eb3d1c7 100644 --- a/inc/page.class.php +++ b/inc/page.class.php @@ -1,7 +1,7 @@ false]) { echo "".__("Name")." :"; //Html::autocompletionTextField($this, "name"); echo "fields["name"], ENT_QUOTES)."\">"; + " value=\"".htmlentities($this->fields["name"], ENT_QUOTES)."\">"; echo ""; echo "".__("Comments")." :"; echo "