From f2072dcfd203d5942143d1ed95d6a0c6d1ca1174 Mon Sep 17 00:00:00 2001 From: Benjamin Renard Date: Wed, 17 Nov 2010 19:00:09 +0100 Subject: [PATCH] LSform : Added data entry form in create mode --- .../LSobjects/config.LSobjects.LSpeople.php | 22 ++- public_html/create.php | 9 ++ public_html/css/default/LSform.css | 8 ++ public_html/includes/class/class.LSform.php | 136 ++++++++++++++++-- public_html/includes/js/LSform.js | 13 ++ public_html/templates/default/create.tpl | 8 ++ 6 files changed, 182 insertions(+), 14 deletions(-) diff --git a/public_html/conf/LSobjects/config.LSobjects.LSpeople.php b/public_html/conf/LSobjects/config.LSobjects.LSpeople.php index 5a27e3b8..bd314e3b 100644 --- a/public_html/conf/LSobjects/config.LSobjects.LSpeople.php +++ b/public_html/conf/LSobjects/config.LSobjects.LSpeople.php @@ -106,7 +106,27 @@ $GLOBALS['LSobjects']['LSpeople'] = array ( 'sambaNTPassword' ) ) - ) // fin Layout + ), // fin Layout + 'dataEntryForm' => array ( + 'simple' => array ( + 'label' => 'Simple', + 'disabledLayout' => true, + 'displayedElements' => array ( + 'uid', + 'personalTitle', + 'givenName', + 'sn', + 'cn', + 'mail', + 'userPassword' + ), + 'defaultValues' => array ( + 'description' => 'Create with the simple data entry form', + 'loginShell' => 'no', + 'gidNumber' => '102001' + ) + ) + ) // fin dataEntryForm ), // fin LSform 'LSsearch' => array ( diff --git a/public_html/create.php b/public_html/create.php index cf94bda3..9fbbef52 100644 --- a/public_html/create.php +++ b/public_html/create.php @@ -43,6 +43,15 @@ if(LSsession :: startLSsession()) { else { $form = $object -> getForm('create'); } + + if (isset($_REQUEST['LSform_dataEntryForm'])) { + $form -> applyDataEntryForm((string)$_REQUEST['LSform_dataEntryForm']); + $GLOBALS['Smarty'] -> assign('LSform_dataEntryForm',(string)$_REQUEST['LSform_dataEntryForm']); + } + + $GLOBALS['Smarty'] -> assign('listAvailableDataEntryForm',array_merge(array(''=>''),LSform :: listAvailableDataEntryForm($LSobject))); + $GLOBALS['Smarty'] -> assign('DataEntryFormLabel',_('Data entry form')); + if ($form->validate()) { // Data update for LDAP object if ($object -> updateData('create')) { diff --git a/public_html/css/default/LSform.css b/public_html/css/default/LSform.css index cbe4c8e9..bdab7b72 100644 --- a/public_html/css/default/LSform.css +++ b/public_html/css/default/LSform.css @@ -147,3 +147,11 @@ div.LSformWarnBox { border: 1px solid #f00!important; color: #fff!important; } + +/* -- LSform_listAvailableDataEntryForm -- */ +p.LSform_listAvailableDataEntryForm { + margin: 2px; + margin-right: 1em; + color: #0072b8; + float: right; +} diff --git a/public_html/includes/class/class.LSform.php b/public_html/includes/class/class.LSform.php index 00811e3b..4e46920c 100644 --- a/public_html/includes/class/class.LSform.php +++ b/public_html/includes/class/class.LSform.php @@ -45,6 +45,9 @@ class LSform { var $maxFileSize = NULL; + var $dataEntryForm = NULL; + var $dataEntryFormConfig = NULL; + var $warnings = array(); /** @@ -111,19 +114,48 @@ class LSform { $GLOBALS['Smarty'] -> assign('LSform_object',$LSform_object); $layout_config=LSconfig :: get("LSobjects.".$LSform_object['type'].".LSform.layout"); - if (is_array($layout_config)) { - $GLOBALS['Smarty'] -> assign('LSform_layout',$layout_config); - $GLOBALS['Smarty'] -> assign('LSform_layout_nofield_label',_('No field.')); + + if (!isset($this -> dataEntryFormConfig['disabledLayout']) || $this -> dataEntryFormConfig['disabledLayout']==false) { + if (is_array($layout_config)) { + $GLOBALS['Smarty'] -> assign('LSform_layout',$layout_config); + $GLOBALS['Smarty'] -> assign('LSform_layout_nofield_label',_('No field.')); + } } $fields = array(); - foreach($this -> elements as $element) { - $field = array(); - $field = $element -> getDisplay(); - if (isset($this -> _elementsErrors[$element -> name])) { - $field['errors']= $this -> _elementsErrors[$element -> name]; + if (!isset($this -> dataEntryFormConfig['displayedElements']) && !is_array($this -> dataEntryFormConfig['displayedElements'])) { + foreach($this -> elements as $element) { + $field = array(); + $field = $element -> getDisplay(); + if (isset($this -> _elementsErrors[$element -> name])) { + $field['errors']= $this -> _elementsErrors[$element -> name]; + } + $fields[$element -> name] = $field; } - $fields[$element -> name] = $field; + } + else { + foreach($this -> dataEntryFormConfig['displayedElements'] as $elementName) { + if (!isset($this -> elements[$elementName])) { + LSerror :: addErrorCode('LSform_09',$elementName); + continue; + } + $element = $this -> elements[$elementName]; + $field = array(); + $field = $element -> getDisplay(); + if (isset($this -> _elementsErrors[$element -> name])) { + $field['errors']= $this -> _elementsErrors[$element -> name]; + } + $fields[$element -> name] = $field; + } + // Add warning for other elements errors + foreach(array_keys($this -> elements) as $name) { + if (isset($this -> _elementsErrors[$name]) && !isset($fields[$name])) { + foreach ($this -> _elementsErrors[$name] as $error) { + $this -> addWarning("$name : $error"); + } + } + } + $LSform_header .= "\t\n"; } if ($this -> maxFileSize) { @@ -379,10 +411,31 @@ class LSform { * @retval boolean true si les valeurs ont bien été récupérées, false sinon. */ function getPostData() { - foreach($this -> elements as $element_name => $element) { - if( !($element -> getPostData($this -> _postData)) ) { - LSerror :: addErrorCode('LSform_02',$element_name); - return; + if (is_null($this -> dataEntryForm)) { + foreach($this -> elements as $element_name => $element) { + if( !($element -> getPostData($this -> _postData)) ) { + LSerror :: addErrorCode('LSform_02',$element_name); + return; + } + } + } + else { + $elementsList = $this -> dataEntryFormConfig['displayedElements']; + if (isset($this -> dataEntryFormConfig['defaultValues']) && is_array($this -> dataEntryFormConfig['defaultValues'])) { + $this -> setPostData($this -> dataEntryFormConfig['defaultValues']); + $elementsList = array_merge($elementsList,array_keys($this -> dataEntryFormConfig['defaultValues'])); + } + + foreach($elementsList as $elementName) { + if (!isset($this -> elements[$elementName])) { + LSerror :: addErrorCode('LSform_09',$elementName); + continue; + } + $element = $this -> elements[$elementName]; + if( !($element -> getPostData($this -> _postData)) ) { + LSerror :: addErrorCode('LSform_02',$element_name); + return; + } } } return true; @@ -555,6 +608,54 @@ class LSform { $this -> maxFileSize = $size; } + /** + * Applique un masque de saisie au formulaire + * + * @param[in] $dataEntryForm string Le nom du masque de saisie + * + * @retval boolean True si le masque de saisie a été appliqué, False sinon + **/ + function applyDataEntryForm($dataEntryForm) { + $dataEntryForm=(string)$dataEntryForm; + $objType = $this -> ldapObject -> getType(); + $config=LSconfig :: get("LSobjects.".$objType.".LSform.dataEntryForm.".$dataEntryForm); + if (is_array($config)) { + if (!is_array($config['displayedElements'])) { + LSerror :: addErrorCode('LSform_08',$dataEntryForm); + } + $this -> dataEntryForm = $dataEntryForm; + $this -> dataEntryFormConfig = $config; + return true; + } + LSerror :: addErrorCode('LSform_07',$dataEntryForm); + return; + } + + /** + * Liste les dataEntryForm disponible pour un type d'LSldapObject + * + * @param[in] $type string Le type d'LSldapObject + * + * @retval array Tableau contenant la liste de dataEntryForm disponible pour ce type d'LSldapObject (nom => label) + **/ + public static function listAvailableDataEntryForm($type) { + $retval=array(); + if (LSsession ::loadLSobject($type)) { + $config=LSconfig :: get("LSobjects.".$type.".LSform.dataEntryForm"); + if (is_array($config)) { + foreach($config as $name => $conf) { + if (isset($conf['label'])) { + $retval[$name]=__($conf['label']); + } + else { + $retval[$name]=__($name); + } + } + } + } + return $retval; + } + /** * Ajoute un avertissement au sujet du formulaire * @@ -614,4 +715,13 @@ _("LSfom : Field type unknow (%{type}).") LSerror :: defineError('LSform_06', _("LSform : Error during the creation of the element '%{element}'.") ); +LSerror :: defineError('LSform_07', +_("LSform : The data entry form %{name} doesn't exist.") +); +LSerror :: defineError('LSform_08', +_("LSform : The data entry form %{name} is not correctly configured.") +); +LSerror :: defineError('LSform_09', +_("LSform : The element %{name}, listed as displayed in data entry form configuration, doesn't exist.") +); ?> diff --git a/public_html/includes/js/LSform.js b/public_html/includes/js/LSform.js index a25edfe8..f4adfff5 100644 --- a/public_html/includes/js/LSform.js +++ b/public_html/includes/js/LSform.js @@ -44,6 +44,11 @@ var LSform = new Class({ this.warnBox.display(this.warnTxt); } LSdebug(this.params); + + this.listAvailableDataEntryForm=$('LSform_listAvailableDataEntryForm'); + if ($type(this.listAvailableDataEntryForm)) { + this.listAvailableDataEntryForm.addEvent('change',this.onListAvailableDataEntryFormChange.bind(this)); + } } LSforms = $$('form.LSform'); @@ -282,6 +287,14 @@ var LSform = new Class({ this.tmp +=""; this.warnBox.display(this.tmp); } + }, + + onListAvailableDataEntryFormChange: function() { + var url=window.location.pathname+"?LSobject="+this.objecttype + if (this.listAvailableDataEntryForm.value!="") { + url+="&LSform_dataEntryForm="+this.listAvailableDataEntryForm.value; + } + document.location=url; } }); window.addEvent(window.ie ? 'load' : 'domready', function() { diff --git a/public_html/templates/default/create.tpl b/public_html/templates/default/create.tpl index 34bbfa12..ee2912e1 100644 --- a/public_html/templates/default/create.tpl +++ b/public_html/templates/default/create.tpl @@ -1,5 +1,13 @@ {include file='top.tpl'} {if $pagetitle != ''}

{$pagetitle}

{/if} + + {if !empty($listAvailableDataEntryForm)} +

+ {/if} {include file='LSform.tpl'} {include file='bottom.tpl'}