From 9569eced26483f809d31cce5724cbf2bfd18050d Mon Sep 17 00:00:00 2001 From: lLefevre Date: Tue, 11 Oct 2011 16:18:14 +0000 Subject: [PATCH] [*] BO #PSFV-94 : added class HelperForm --- admin-dev/themes/template/form.tpl | 109 +++++++++++++++++++++ admin-dev/themes/template/layout.tpl | 138 ++++++++++++++------------- classes/AdminController.php | 44 ++++++++- classes/HelperForm.php | 100 +++++++++++++++++++ 4 files changed, 322 insertions(+), 69 deletions(-) create mode 100644 admin-dev/themes/template/form.tpl create mode 100644 classes/HelperForm.php diff --git a/admin-dev/themes/template/form.tpl b/admin-dev/themes/template/form.tpl new file mode 100644 index 000000000..ece525581 --- /dev/null +++ b/admin-dev/themes/template/form.tpl @@ -0,0 +1,109 @@ +{* +* 2007-2011 PrestaShop +* +* NOTICE OF LICENSE +* +* This source file is subject to the Academic Free License (AFL 3.0) +* that is bundled with this package in the file LICENSE.txt. +* It is also available through the world-wide-web at this URL: +* http://opensource.org/licenses/afl-3.0.php +* If you did not receive a copy of the license and are unable to +* obtain it through the world-wide-web, please send an email +* to license@prestashop.com so we can send you a copy immediately. +* +* DISCLAIMER +* +* Do not edit or add to this file if you wish to upgrade PrestaShop to newer +* versions in the future. If you wish to customize PrestaShop for your +* needs please refer to http://www.prestashop.com for more information. +* +* @author PrestaShop SA +* @copyright 2007-2011 PrestaShop SA +* @version Release: $Revision: 8971 $ +* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0) +* International Registered Trademark & Property of PrestaShop SA +*} + +{if $firstCall} + +{/if} + +
+ {if $form_id} + + {/if} +
+ {foreach $fields as $key => $field} + {if $key == 'legend'} + + {if isset($field.image)}{$field.title}{/if} + {$field.title} + + {elseif $key == 'input'} + {foreach $field as $input} + +
+ {if $input.type == 'text'} + + {elseif $input.type == 'select'} + + {elseif $input.type == 'radio'} + {foreach $input.values as $value} + + + {/foreach} + {elseif $input.type == 'textarea'} + + {/if} + {if isset($input.required) && $input.required} *{/if} + {if isset($input.p)} +

+ {if is_array($input.p)} + {foreach $input.p as $p} + {$p}
+ {/foreach} + {else} + {$input.p} + {/if} +

+ {/if} +
+ {/foreach} + {elseif $key == 'submit'} +
+ +
+ {/if} + {/foreach} + {if $requiredFields} +
* {l s ='Required field'}
+ {/if} +
+
+ +

+{if $firstCall} + {if $back} + {l s='Back'} + {else} + {l s='Back to list'} + {/if} +
+{/if} \ No newline at end of file diff --git a/admin-dev/themes/template/layout.tpl b/admin-dev/themes/template/layout.tpl index 173ca2d01..ca72e3702 100644 --- a/admin-dev/themes/template/layout.tpl +++ b/admin-dev/themes/template/layout.tpl @@ -33,77 +33,85 @@ {/if} {if count($errors)} {* @todo what is ??? AND $this->_includeContainer *} - -
- X - {if count($errors) == 1} - {$errors[0]} - {else} - {$errors|count} {l s='errors'} -
-
    - {foreach from=$errors item=$error} -
  1. {$error}
  2. - {/foreach} -
- {/if} -
+ +
+ X + {if count($errors) == 1} + {$errors[0]} + {else} + {$errors|count} {l s='errors'} +
+
    + {foreach from=$errors item=$error} +
  1. {$error}
  2. + {/foreach} +
+ {/if} +
+{/if} + +{if count($informations) && $informations} +
+ {foreach $informations as $info} + {$info} + {/foreach} +

{/if} {if count($warnings)} - -
- - X - - - {if count($warnings) > 1} - {l s='There are'} {count($warnings)} {l s='warnings'} - {else} - {l s='There is'} {count($warnings)} {l s='warning'} - {/if} - - {l s='Click here to see more'} - - - -
+ +
+ + X + + + {if count($warnings) > 1} + {l s='There are'} {count($warnings)} {l s='warnings'} + {else} + {l s='There is'} {count($warnings)} {l s='warning'} + {/if} + + {l s='Click here to see more'} + + + +
{/if} {$page} diff --git a/classes/AdminController.php b/classes/AdminController.php index faa015dfa..22909b13d 100644 --- a/classes/AdminController.php +++ b/classes/AdminController.php @@ -31,7 +31,8 @@ class AdminControllerCore extends Controller public static $currentIndex; public $content; - public $warnings; + public $warnings = array(); + public $informations = array(); public $content_only = false; public $layout = 'layout.tpl'; @@ -715,6 +716,7 @@ class AdminControllerCore extends Controller $this->context->smarty->assign('errors', $this->_errors); $this->context->smarty->assign('warnings', $this->warnings); + $this->context->smarty->assign('informations', $this->informations); } $this->context->smarty->assign('page', $page); @@ -731,6 +733,16 @@ class AdminControllerCore extends Controller $this->warnings[] = $msg; } + /** + * add a info message to display at the top of the page + * + * @param string $msg + */ + protected function displayInformation($msg) + { + $this->informations[] = $msg; + } + /** * Assign smarty variables for the header */ @@ -889,7 +901,25 @@ class AdminControllerCore extends Controller { if ($this->display == 'edit') { - $this->content .= $this->displayForm(); + + if (!($obj = $this->loadObject(true))) + return; + + if (isset($this->fields_form)) + { + $helper = new HelperForm(); + $helper::$currentIndex = self::$currentIndex; + $helper->token = $this->token; + $helper->table = $this->table; + $helper->table = $this->table; + $helper->id = $obj->id; + $helper->fields_value = $this->getFieldsValue($obj); + $this->content .= $helper->generateForm($this->fields_form); + } + else + //@TODO à supprimer + $this->content .= $this->displayForm(); + if ($this->tabAccess['view']) { if (Tools::getValue('back')) @@ -897,8 +927,6 @@ class AdminControllerCore extends Controller else $this->context->smarty->assign('back', Tools::safeOutput(Tools::getValue(self::$currentIndex.'&token='.$this->token))); } - // move to form.tpl - $this->content .= '

'.((Tools::getValue('back')) ? $this->l('Back') : $this->l('Back to list')).'
'; } else if ($this->display == 'list') { @@ -1288,6 +1316,14 @@ class AdminControllerCore extends Controller $this->_listTotal = Db::getInstance()->getValue('SELECT FOUND_ROWS() AS `'._DB_PREFIX_.$this->table.'`'); } + public function getFieldsValue($obj) + { + foreach ($this->fields_form['input'] as $input) + if (empty($this->fields_value[$input['name']])) + $this->fields_value[$input['name']] = $this->getFieldValue($obj, $input['name']); + return $this->fields_value; + } + /** * Return field value if possible (both classical and multilingual fields) * diff --git a/classes/HelperForm.php b/classes/HelperForm.php new file mode 100644 index 000000000..86ff2c904 --- /dev/null +++ b/classes/HelperForm.php @@ -0,0 +1,100 @@ + +* @copyright 2007-2011 PrestaShop SA +* @version Release: $Revision: 9194 $ +* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) +* International Registered Trademark & Property of PrestaShop SA +*/ + +class HelperFormCore extends Helper +{ + + public $id; + + public $first_call = true; + + /** + * @var array of forms fields + * Usage : + * + */ + protected $fields_form = array(); + + public $fields_value = array(); + + public $table; + + public $token; + + public static $currentIndex; + + public function generateForm($fields_form) + { + $this->fields_form = $fields_form; + + return $this->displayForm(); + } + + public function displayForm() + { + $allowEmployeeFormLang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') ? Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') : 0; + if ($allowEmployeeFormLang && !$this->context->cookie->employee_form_lang) + $this->context->cookie->employee_form_lang = (int)(Configuration::get('PS_LANG_DEFAULT')); + $useLangFromCookie = false; + $languages = Language::getLanguages(false); + if ($allowEmployeeFormLang) + foreach ($languages as $lang) + if ($this->context->cookie->employee_form_lang == $lang['id_lang']) + $useLangFromCookie = true; + if (!$useLangFromCookie) + $defaultFormLanguage = (int)(Configuration::get('PS_LANG_DEFAULT')); + else + $defaultFormLanguage = (int)($this->context->cookie->employee_form_lang); + + info($this->fields_value); + $this->context->smarty->assign(array( + 'firstCall' => $this->first_call, + 'current' => self::$currentIndex, + 'token' => $this->token, + 'table' => $this->table, + 'defaultFormLanguage' => $defaultFormLanguage, + 'languages' => $languages, + 'allowEmployeeFormLang' => $allowEmployeeFormLang, + 'form_id' => $this->id, + 'back' => Tools::getValue('back'), + 'fields' => $this->fields_form, + 'fields_value' => $this->fields_value, + 'requiredFields' => $this->getFieldsRequired() + )); + + return $this->context->smarty->fetch(_PS_ADMIN_DIR_.'/themes/template/form.tpl'); + } + + public function getFieldsRequired() + { + foreach ($this->fields_form['input'] as $input) + if (array_key_exists('required', $input) && $input['required']) + return true; + + return false; + } +} \ No newline at end of file