From 7d0942bcc2eeaee94507ce4edad5e06aeeaa2a2d Mon Sep 17 00:00:00 2001 From: vChabot Date: Thu, 15 Dec 2011 16:37:08 +0000 Subject: [PATCH] [*] BO : Required fields improved (with a white list) git-svn-id: http://dev.prestashop.com/svn/v1/branches/1.5.x@11276 b9a71923-0436-4b27-9f14-aed3839534dd --- .../template/helper/required_fields.tpl | 59 +++++++++++++++++++ classes/AdminController.php | 25 +++++++- classes/helper/Helper.php | 42 ++++++++++++- .../admin/AdminAddressesController.php | 2 + .../admin/AdminCustomersController.php | 4 +- 5 files changed, 127 insertions(+), 5 deletions(-) create mode 100644 admin-dev/themes/template/helper/required_fields.tpl diff --git a/admin-dev/themes/template/helper/required_fields.tpl b/admin-dev/themes/template/helper/required_fields.tpl new file mode 100644 index 000000000..6c730bc7f --- /dev/null +++ b/admin-dev/themes/template/helper/required_fields.tpl @@ -0,0 +1,59 @@ +{* +* 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: 11256 $ +* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0) +* International Registered Trademark & Property of PrestaShop SA +*} + +
+

+ {l s='Set required fields for this section'} +

+ diff --git a/classes/AdminController.php b/classes/AdminController.php index 04b24359c..d4412edc5 100644 --- a/classes/AdminController.php +++ b/classes/AdminController.php @@ -64,6 +64,8 @@ class AdminControllerCore extends Controller /** @var array noTabLink array of admintab names witch have no content */ public $noTabLink = array('AdminCatalog', 'AdminTools', 'AdminStock', 'AdminAccounting'); + public $required_database = false; + /** @var string Security token */ public $token; @@ -78,6 +80,7 @@ class AdminControllerCore extends Controller public $tpl_delete_link_vars = array(); public $tpl_option_vars = array(); public $tpl_view_vars = array(); + public $tpl_required_fields_vars = array(); public $base_tpl_view = null; public $base_tpl_form = null; @@ -158,6 +161,9 @@ class AdminControllerCore extends Controller /** @var array $cache_lang cache for traduction */ public static $cache_lang = array(); + /** @var array required_fields to display in the Required Fields form */ + public $required_fields = array(); + /** * @var array actions to execute on multiple selections * Usage: @@ -1361,6 +1367,9 @@ class AdminControllerCore extends Controller $this->content .= $this->renderList(); $this->content .= $this->renderOptions(); } + // if we have to display the required fields form + if ($this->required_database) + $this->content .= $this->displayRequiredFields(); $this->context->smarty->assign(array( 'content' => $this->content, @@ -1853,7 +1862,7 @@ class AdminControllerCore extends Controller /* Submit options list */ else if (Tools::getValue('submitOptions'.$this->table) || Tools::getValue('submitOptions')) $this->action = 'update_options'; - else if (Tools::isSubmit('submitFields') && $this->requiredDatabase && $this->tabAccess['add'] === '1' && $this->tabAccess['delete'] === '1') + else if (Tools::isSubmit('submitFields') && $this->required_database && $this->tabAccess['add'] === '1' && $this->tabAccess['delete'] === '1') $this->action = 'update_fields'; else if (is_array($this->bulk_actions)) foreach ($this->bulk_actions as $bulk_action => $params) @@ -2512,5 +2521,19 @@ class AdminControllerCore extends Controller { return true; } + + /** + * prepare the view to display the required fields form + */ + public function displayRequiredFields() + { + if (!$this->tabAccess['add'] || !$this->tabAccess['delete'] === '1' || !$this->required_database) + return; + + $helper = new Helper(); + $helper->currentIndex = self::$currentIndex; + $helper->token = $this->token; + return $helper->renderRequiredFields($this->className, $this->identifier, $this->required_fields); + } } diff --git a/classes/helper/Helper.php b/classes/helper/Helper.php index 9f5a1b6ff..df2c41fea 100755 --- a/classes/helper/Helper.php +++ b/classes/helper/Helper.php @@ -321,17 +321,17 @@ class HelperCore $type = 'shop'; $assos = array(); - + if ((int)$this->id) { $sql = 'SELECT id_'.$type.', `'.pSQL($this->identifier).'` FROM `'._DB_PREFIX_.pSQL($this->table).'_'.$type.'` WHERE `'.pSQL($this->identifier).'` = '.(int)$this->id; - + foreach (Db::getInstance()->executeS($sql) as $row) $assos[$row['id_'.$type]] = $row['id_'.$type]; } - + $tpl = $this->createTemplate('helper/assoshop.tpl'); $tpl->assign(array( 'input' => array( @@ -347,5 +347,41 @@ class HelperCore return $tpl->fetch(); } + /** + * Render a form with potentials required fields + * + * @param string $class_name + * @param string $identifier + * @param array $table_fields + * @return string + */ + public function renderRequiredFields($class_name, $identifier, $table_fields) + { + $rules = call_user_func_array(array($class_name, 'getValidationRules'), array($class_name)); + $required_class_fields = array($identifier); + foreach ($rules['required'] as $required) + $required_class_fields[] = $required; + + $object = new $class_name(); + $res = $object->getFieldsRequiredDatabase(); + + $required_fields = array(); + foreach ($res as $row) + $required_fields[(int)$row['id_required_field']] = $row['field_name']; + + $this->tpl_vars = array( + 'table_fields' => $table_fields, + 'irow' => 0, + 'required_class_fields' => $required_class_fields, + 'required_fields' => $required_fields, + 'current' => $this->currentIndex, + 'token' => $this->token + ); + + $tpl = $this->createTemplate('helper/required_fields.tpl'); + $tpl->assign($this->tpl_vars); + + return $tpl->fetch(); + } } diff --git a/controllers/admin/AdminAddressesController.php b/controllers/admin/AdminAddressesController.php index c0ee2c546..183aa42ef 100644 --- a/controllers/admin/AdminAddressesController.php +++ b/controllers/admin/AdminAddressesController.php @@ -32,6 +32,8 @@ class AdminAddressesControllerCore extends AdminController public function __construct() { + $this->required_database = true; + $this->required_fields = array('company','address2', 'postcode', 'other', 'phone', 'phone_mobile', 'vat_number', 'dni'); $this->table = 'address'; $this->className = 'Address'; $this->lang = false; diff --git a/controllers/admin/AdminCustomersController.php b/controllers/admin/AdminCustomersController.php index 1ae0598d8..2b377746b 100644 --- a/controllers/admin/AdminCustomersController.php +++ b/controllers/admin/AdminCustomersController.php @@ -31,6 +31,8 @@ class AdminCustomersControllerCore extends AdminController public function __construct() { + $this->required_database = true; + $this->required_fields = array('newsletter','optin'); $this->table = 'customer'; $this->className = 'Customer'; $this->lang = false; @@ -157,6 +159,7 @@ class AdminCustomersControllerCore extends AdminController 'url_delete' => htmlentities($_SERVER['REQUEST_URI']), 'boxes' => $this->boxes, )); + parent::initContent(); } @@ -736,7 +739,6 @@ class AdminCustomersControllerCore extends AdminController '.($customer->optin ? '' : ''). ''; } - }