From 96844f2d92f941908b43ac7901d1c8a88008867b Mon Sep 17 00:00:00 2001 From: lLefevre Date: Mon, 17 Oct 2011 16:37:21 +0000 Subject: [PATCH] [*] BO #PSFV-94 : update of the displayform method by helperForm in AdminStoresController git-svn-id: http://dev.prestashop.com/svn/v1/branches/1.5.x@9401 b9a71923-0436-4b27-9f14-aed3839534dd --- admin-dev/tabs/AdminStores.php | 324 --------------- admin-dev/themes/template/stores/form.tpl | 294 ++++++++++++++ admin-dev/themes/template/stores/options.tpl | 162 ++++++++ classes/AdminController.php | 8 +- classes/HelperOptions.php | 5 +- controllers/admin/AdminStoresController.php | 395 +++++++++++++++++++ 6 files changed, 859 insertions(+), 329 deletions(-) delete mode 100644 admin-dev/tabs/AdminStores.php create mode 100644 admin-dev/themes/template/stores/form.tpl create mode 100644 admin-dev/themes/template/stores/options.tpl create mode 100644 controllers/admin/AdminStoresController.php diff --git a/admin-dev/tabs/AdminStores.php b/admin-dev/tabs/AdminStores.php deleted file mode 100644 index a3a3a0de1..000000000 --- a/admin-dev/tabs/AdminStores.php +++ /dev/null @@ -1,324 +0,0 @@ - -* @copyright 2007-2011 PrestaShop SA -* @version Release: $Revision: 7310 $ -* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) -* International Registered Trademark & Property of PrestaShop SA -*/ - -include_once(dirname(__FILE__).'/../../classes/AdminTab.php'); - -class AdminStores extends AdminTab -{ - /** @var array countries list */ - private $countriesArray = array(); - - public function __construct() - { - $this->context = Context::getContext(); - $this->table = 'store'; - $this->className = 'Store'; - $this->lang = false; - $this->edit = true; - $this->delete = true; - - $this->fieldImageSettings = array('name' => 'image', 'dir' => 'st'); - - $this->_select = 'cl.`name` country, st.`name` state'; - $this->_join = ' - LEFT JOIN `'._DB_PREFIX_.'country_lang` cl ON (cl.`id_country` = a.`id_country` AND cl.`id_lang` = '.(int)$this->context->language->id.') - LEFT JOIN `'._DB_PREFIX_.'state` st ON (st.`id_state` = a.`id_state`)'; - - $countries = Country::getCountries($this->context->language->id); - foreach ($countries AS $country) - $this->countriesArray[$country['id_country']] = $country['name']; - - $this->fieldsDisplay = array( - 'id_store' => array('title' => $this->l('ID'), 'align' => 'center', 'width' => 25), - 'country' => array('title' => $this->l('Country'), 'width' => 100, 'filter_key' => 'cl!name'), - 'state' => array('title' => $this->l('State'), 'width' => 100, 'filter_key' => 'st!name'), - 'city' => array('title' => $this->l('City'), 'width' => 100), - 'postcode' => array('title' => $this->l('Zip code'), 'width' => 50), - 'name' => array('title' => $this->l('Name'), 'width' => 120, 'filter_key' => 'a!name'), - 'phone' => array('title' => $this->l('Phone'), 'width' => 70), - 'fax' => array('title' => $this->l('Fax'), 'width' => 70), - 'active' => array('title' => $this->l('Enabled'), 'align' => 'center', 'active' => 'status', 'type' => 'bool', 'orderby' => false) - ); - - $this->optionsList = array( - 'general' => array( - 'title' => $this->l('Parameters'), - 'fields' => array( - 'PS_STORES_DISPLAY_FOOTER' => array('title' => $this->l('Display in the footer:'), 'desc' => $this->l('Display a link to the store locator in the footer'), 'cast' => 'intval', 'type' => 'bool'), - 'PS_STORES_DISPLAY_SITEMAP' => array('title' => $this->l('Display in the sitemap page:'), 'desc' => $this->l('Display a link to the store locator in the sitemap page'), 'cast' => 'intval', 'type' => 'bool'), - 'PS_STORES_SIMPLIFIED' => array('title' => $this->l('Show a simplified store locator:'), 'desc' => $this->l('No map, no search, only a store directory'), 'cast' => 'intval', 'type' => 'bool'), - 'PS_STORES_CENTER_LAT' => array('title' => $this->l('Latitude by default:'), 'desc' => $this->l('Used for the position by default of the map'), 'cast' => 'floatval', 'type' => 'text', 'size' => '10'), - 'PS_STORES_CENTER_LONG' => array('title' => $this->l('Longitude by default:'), 'desc' => $this->l('Used for the position by default of the map'), 'cast' => 'floatval', 'type' => 'text', 'size' => '10') - ), - ), - ); - parent::__construct(); - } - - protected function postImage($id) - { - $ret = parent::postImage($id); - if (($id_store = (int)(Tools::getValue('id_store'))) AND isset($_FILES) AND sizeof($_FILES) AND file_exists(_PS_STORE_IMG_DIR_.$id_store.'.jpg')) - { - $imagesTypes = ImageType::getImagesTypes('stores'); - foreach ($imagesTypes AS $k => $imageType) - imageResize(_PS_STORE_IMG_DIR_.$id_store.'.jpg', _PS_STORE_IMG_DIR_.$id_store.'-'.stripslashes($imageType['name']).'.jpg', (int)($imageType['width']), (int)($imageType['height'])); - } - return $ret; - } - - public function displayOptionsList() - { - parent::displayOptionsList(); - - echo '

'.$this->l('You can also replace the icon representing your store in Google Maps. Go to the Preferences tab, and then the Appearance subtab.').'

'; - } - - public function postProcess() - { - if (isset($_POST['submitAdd'.$this->table])) - { - /* Cleaning fields */ - foreach ($_POST as $kp => $vp) - $_POST[$kp] = trim($vp); - - /* If the selected country does not contain states */ - $id_state = (int)Tools::getValue('id_state'); - if ($id_country = Tools::getValue('id_country') AND $country = new Country((int)($id_country)) AND !(int)($country->contains_states) AND $id_state) - $this->_errors[] = Tools::displayError('You have selected a state for a country that does not contain states.'); - - /* If the selected country contains states, then a state have to be selected */ - if ((int)($country->contains_states) AND !$id_state) - $this->_errors[] = Tools::displayError('An address located in a country containing states must have a state selected.'); - - $latitude = (float)Tools::getValue('latitude'); - $longitude = (float)Tools::getValue('longitude'); - - if(empty($latitude) OR empty($longitude)) - $this->_errors[] = Tools::displayError('Latitude and longitude are required.'); - - /* Check zip code */ - if ($country->need_zip_code) - { - $zip_code_format = $country->zip_code_format; - if (($postcode = Tools::getValue('postcode')) AND $zip_code_format) - { - $zip_regexp = '/^'.$zip_code_format.'$/ui'; - $zip_regexp = str_replace(' ', '( |)', $zip_regexp); - $zip_regexp = str_replace('-', '(-|)', $zip_regexp); - $zip_regexp = str_replace('N', '[0-9]', $zip_regexp); - $zip_regexp = str_replace('L', '[a-zA-Z]', $zip_regexp); - $zip_regexp = str_replace('C', $country->iso_code, $zip_regexp); - if (!preg_match($zip_regexp, $postcode)) - $this->_errors[] = Tools::displayError('Your zip/postal code is incorrect.').'
'.Tools::displayError('Must be typed as follows:').' '.str_replace('C', $country->iso_code, str_replace('N', '0', str_replace('L', 'A', $zip_code_format))); - } - elseif ($zip_code_format) - $this->_errors[] = Tools::displayError('Postcode required.'); - elseif ($postcode AND !preg_match('/^[0-9a-zA-Z -]{4,9}$/ui', $postcode)) - $this->_errors[] = Tools::displayError('Your zip/postal code is incorrect.'); - } - - /* Store hours */ - $_POST['hours'] = array(); - for ($i = 1; $i < 8; $i++) - $_POST['hours'][] .= Tools::getValue('hours_'.(int)($i)); - $_POST['hours'] = serialize($_POST['hours']); - } - - if (!sizeof($this->_errors)) - parent::postProcess(); - } - - public function displayForm($isMainTab = true) - { - parent::displayForm(); - - if (!($obj = $this->loadObject(true))) - return; - echo ' - -
- '.($obj->id ? '' : '').' -
- '.$this->l('Stores').' -
- -
- - '.$this->l('Allowed characters: letters, spaces and').' (-)  -

'.$this->l('Store name, e.g. Citycentre Mall Store').'

-
- -
- * -
- -
- -
- -
- -
- -
- * -
- -
- * -
-
- -
- * -
-
- -
- / - * -

'.$this->l('Store coords, eg. 45.265469 / -47.226478').'

-
- -
- -
- -
- -
- -
- -
- -
- -
- -
- id OR Tools::getValue('active', $obj->active)) ? 'checked="checked" ' : '').'/> - - active) AND $obj->id) ? 'checked="checked" ' : '').'/> - -

'.$this->l('Display or not this store').'

-
-
-
- -
- -

'.$this->l('Store window picture').'

'; - - echo $this->displayImage($obj->id, _PS_STORE_IMG_DIR_.'/'.$obj->id.'.jpg', 350, NULL, Tools::getAdminToken('AdminStores'.(int)(Tab::getIdFromClassName('AdminStores')).(int)$this->context->employee->id), true); - - echo '
- - - - - - - - '; - - $days = array(); - $days[1] = $this->l('Monday'); - $days[2] = $this->l('Tuesday'); - $days[3] = $this->l('Wednesday'); - $days[4] = $this->l('Thursday'); - $days[5] = $this->l('Friday'); - $days[6] = $this->l('Saturday'); - $days[7] = $this->l('Sunday'); - - $hours = $this->getFieldValue($obj, 'hours'); - if (!empty($hours)) - $hoursUnserialized = unserialize($hours); - - for ($i = 1; $i < 8; $i++) - echo ' - - - - '; - echo ' -
'.$this->l('Hours:').'
 '.$this->l('Sample: 10:00AM - 9:30PM').'
'.$days[(int)($i)].'
-
-
'; - if (Shop::isMultiShopActivated()) - { - echo '
'; - $this->displayAssoShop(); - echo '
'; - } - echo ' -
- -
-
* '.$this->l('Required field').'
-
-
'; - } - -} - - diff --git a/admin-dev/themes/template/stores/form.tpl b/admin-dev/themes/template/stores/form.tpl new file mode 100644 index 000000000..e2ef4d996 --- /dev/null +++ b/admin-dev/themes/template/stores/form.tpl @@ -0,0 +1,294 @@ +{* +* 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.name == 'id_state'} +
+ {/if} + +
+ {if $input.type == 'text'} + {if $input.name == 'latitude'} + / + + {else} + {if isset($input.lang) && isset($input.attributeLang)} + {foreach $languages as $language} +
+ + {if isset($input.hint)}{$input.hint} {/if} +
+ {/foreach} + {if count($languages) > 1} +
+ +
+
+ {l s='Choose language:'}

+ {foreach $languages as $language} + {$language.name} + {/foreach} +
+ {/if} + {else} + + {if isset($input.hint)}{$input.hint} {/if} + {/if} + {/if} + {elseif $input.type == 'select'} + + {elseif $input.type == 'radio'} + {foreach $input.values as $value} + + + {/foreach} + {elseif $input.type == 'textarea'} + {if isset($input.lang) && isset($input.attributeLang)} + {foreach $languages as $language} +
+ +
+ {/foreach} + {if count($languages) > 1} +
+ +
+
+ {l s='Choose language:'}

+ {foreach $languages as $language} + {$language.name} + {/foreach} +
+ {/if} + {else} + + {/if} + {elseif $input.type == 'checkbox'} + + {elseif $input.type == 'file'} + + + {/if} + {if isset($input.required) && $input.required} *{/if} + {if isset($input.p)} +

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

+ {if $input.name == 'active'} +
+ {/if} + {/if} + {if isset($languages)}
{/if} +
+ {if $input.name == 'id_state'} +
+ {/if} + {/foreach} + {elseif $key == 'rightCols'} +
+ {foreach $field as $input} + {if $input.type == 'file'} + +
+ +

{$input.p}

+ {if isset($fields_value.image) && $fields_value.image} +
+ {$fields_value.image} +

{l s='File size'} {$fields_value.size}kb

+ + {l s='Delete'} {l s='Delete'} + +
+ {/if} +
+ {/if} + + + + + + + + + + {foreach $fields_value.days as $k => $value} + + + + + {/foreach} +
{l s='Hours:'}
 {l s='Sample: 10:00AM - 9:30PM'}
{$value}
+
+ {/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/stores/options.tpl b/admin-dev/themes/template/stores/options.tpl new file mode 100644 index 000000000..bac3959d7 --- /dev/null +++ b/admin-dev/themes/template/stores/options.tpl @@ -0,0 +1,162 @@ +{* +* 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: 9369 $ +* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0) +* International Registered Trademark & Property of PrestaShop SA +*} +
+ + +
+{foreach $optionsList AS $category => $categoryData} + {if isset($categoryData['top'])}{$categoryData['top']}{/if} +
+ {* Options category title *} + + + {if isset($categoryData['title'])}{$categoryData['title']}{else}{l s='Options'}{/if} + + + {* Category description *} + {if (isset($categoryData['description']) && $categoryData['description'])} +

{$categoryData['description']}

+ {/if} + + {foreach $categoryData['fields'] AS $key => $field} +
+ {if $field['title']} + + {/if} +
+ + {if $field['type'] == 'select'} + + {elseif $field['type'] == 'bool'} + + + + + + + {elseif $field['type'] == 'radio'} + {foreach $field['choices'] AS $k => $v} + +
+ {/foreach} +
+ {*{elseif $field['type'] == 'checkbox'} + {foreach $field['choices'] AS $k => $v} + +
+ {/foreach} +
+ *} + {elseif $field['type'] == 'text'} + + {if isset($field['next'])} {$field['next']|strval}{/if} + {elseif $field['type'] == 'password'} + + {if isset($field['next'])} {$field['next']|strval}{/if} + {elseif $field['type'] == 'textarea'} + + {elseif $field['type'] == 'file'} + {if isset($field['thumb']) && $field['thumb'] && $field['thumb']['pos'] == 'before'} + {$field['title']}
+ {/if} + + {* {elseif $field['type'] == 'image'} + + + $i = 0; + foreach ($field['list'] as $theme) + { + '; + if (isset($field['max']) && ($i +1 ) % $field['max'] == 0) + echo ''; + $i++; + } + echo ''; + echo '
+ '; + echo ''; + echo '
'; + echo ''; + echo '
'; + *} + {elseif $field['type'] == 'textLang'} + {foreach $field['languages'] AS $id_lang => $value} +
+ +
+ {/foreach} + {$field['flags']} + {elseif $field['type'] == 'textareaLang'} + {foreach $field['languages'] AS $id_lang => $value} +
'; + +
+ {/foreach} + {$field['flags']} +
+ + + {/if} + {if isset($field['method'])}$field['method']{/if} + + {if ($field['multishop_default'])} +
+ +
+ {/if} + {if isset($field['desc'])}

{$field['desc']}

{/if} + {if $field['is_invisible']}

{l s='You can\'t change the value of this configuration field in this shop context'}

{/if} +
+ {/foreach} + +
+ +
+ {if $field['required']} +
* {l s='Required field'}
+ {/if} +

+ {if isset($categoryData['bottom'])}{$categoryData['bottom']}{/if} +{/foreach} +

+

+ + {l s='You can also replace the icon representing your store in Google Maps. Go to the Preferences tab, and then the Appearance subtab.'} +

diff --git a/classes/AdminController.php b/classes/AdminController.php index 7e8caa5a2..a96d26394 100644 --- a/classes/AdminController.php +++ b/classes/AdminController.php @@ -971,6 +971,7 @@ class AdminControllerCore extends Controller else if ($this->display == 'list') { $this->getList($this->context->language->id); + $helper = new HelperList(); // Check if list templates have been overriden if (file_exists($this->context->smarty->template_dir.'/'.$this->tpl_folder.'list_header.tpl')) @@ -1004,9 +1005,9 @@ class AdminControllerCore extends Controller $helper->identifier = $this->identifier; $helper->token = $this->token; $helper->imageType = $this->imageType; + $helper->no_add = isset($this->no_add) ? $this->no_add : false; $helper->_listSkipDelete = $this->_listSkipDelete; $helper->colorOnBackground = $this->colorOnBackground; - $this->content .= $helper->generateList($this->_list, $this->fieldsDisplay); } else if ($this->display == 'options') @@ -1194,6 +1195,7 @@ class AdminControllerCore extends Controller if (isset($_GET['deleteImage'])) { $this->action = 'delete_image'; + $token = Tools::getValue('token') ? Tools::getValue('token') : $this->token; if (Validate::isLoadedObject($object = $this->loadObject())) if (($object->deleteImage())) Tools::redirectAdmin(self::$currentIndex.'&add'.$this->table.'&'.$this->identifier.'='.Tools::getValue($this->identifier).'&conf=7&token='.$token); @@ -1277,7 +1279,7 @@ class AdminControllerCore extends Controller public function displayErrors() { // @TODO includesubtab - $this->includeSubTab('displayErrors'); + $content = $this->includeSubTab('displayErrors'); return $content; } @@ -1337,7 +1339,7 @@ class AdminControllerCore extends Controller $selectShop = ', shop.name as shop_name '; $joinShop = ' LEFT JOIN '._DB_PREFIX_.$this->shopLinkType.' shop ON a.id_'.$this->shopLinkType.' = shop.id_'.$this->shopLinkType; - $whereShop = $this->context->shop->addSqlRestriction($this->shopShareDatas, 'a', $this->shopLinkType); + $whereShop = $this->context->shop->sqlRestriction($this->shopShareDatas, 'a', $this->shopLinkType); } $assos = Shop::getAssoTables(); if (isset($assos[$this->table]) && $assos[$this->table]['type'] == 'shop') diff --git a/classes/HelperOptions.php b/classes/HelperOptions.php index 8a87e2b35..4d25f749f 100644 --- a/classes/HelperOptions.php +++ b/classes/HelperOptions.php @@ -31,6 +31,7 @@ class HelperOptionsCore extends Helper { public $first_call = true; + public $required = false; /** * @var array of forms fields @@ -51,10 +52,8 @@ class HelperOptionsCore extends Helper public function generateOptions($option_list) { $tab = Tab::getTab($this->context->language->id, $this->id); - foreach ($option_list as $category => $category_data) { - $required = false; foreach ($category_data['fields'] as $key => $field) { // Field value @@ -75,6 +74,8 @@ class HelperOptionsCore extends Helper $option_list[$category]['fields'][$key]['is_disabled'] = $isDisabled; $option_list[$category]['fields'][$key]['is_invisible'] = $isInvisible; + $option_list[$category]['fields'][$key]['required'] = isset($field['required']) ? $field['required'] : $this->required; + // Cast options values if specified if ($field['type'] == 'select' && isset($field['cast'])) foreach ($field['list'] as $option_key => $option) diff --git a/controllers/admin/AdminStoresController.php b/controllers/admin/AdminStoresController.php new file mode 100644 index 000000000..0a1e445cf --- /dev/null +++ b/controllers/admin/AdminStoresController.php @@ -0,0 +1,395 @@ + +* @copyright 2007-2011 PrestaShop SA +* @version Release: $Revision: 8971 $ +* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) +* International Registered Trademark & Property of PrestaShop SA +*/ + +class AdminStoresControllerCore extends AdminController +{ + public function __construct() + { + $this->table = 'store'; + $this->className = 'Store'; + $this->lang = false; + $this->addRowAction('edit'); + $this->addRowAction('delete'); + $this->requiredDatabase = true; + + $this->context = Context::getContext(); + + if (!Tools::getValue('realedit')) + $this->deleted = false; + + $this->bulk_actions = array('delete' => array('text' => $this->l('Delete selected'), 'confirm' => $this->l('Delete selected items?'))); + + $this->fieldImageSettings = array( + 'name' => 'image', + 'dir' => 'st' + ); + + $this->_select = 'cl.`name` country, st.`name` state'; + $this->_join = ' + LEFT JOIN `'._DB_PREFIX_.'country_lang` cl + ON (cl.`id_country` = a.`id_country` + AND cl.`id_lang` = '.(int)$this->context->language->id.') + LEFT JOIN `'._DB_PREFIX_.'state` st + ON (st.`id_state` = a.`id_state`)'; + + $this->fieldsDisplay = array( + 'id_store' => array( + 'title' => $this->l('ID'), + 'align' => 'center', + 'width' => 25 + ), + 'country' => array( + 'title' => $this->l('Country'), + 'width' => 100, + 'filter_key' => 'cl!name' + ), + 'state' => array( + 'title' => $this->l('State'), + 'width' => 100, + 'filter_key' => 'st!name' + ), + 'city' => array('title' => $this->l('City'), 'width' => 100), + 'postcode' => array('title' => $this->l('Zip code'), 'width' => 50), + 'name' => array('title' => $this->l('Name'), 'width' => 120, 'filter_key' => 'a!name'), + 'phone' => array('title' => $this->l('Phone'), 'width' => 70), + 'fax' => array('title' => $this->l('Fax'), 'width' => 70), + 'active' => array('title' => $this->l('Enabled'), 'align' => 'center', 'active' => 'status', 'type' => 'bool', 'orderby' => false) + ); + + $this->fields_form = array( + 'legend' => array( + 'title' => $this->l('Stores'), + 'image' => '../img/admin/home.gif' + ), + 'input' => array( + array( + 'type' => 'text', + 'label' => $this->l('Name:'), + 'name' => 'name', + 'size' => 33, + 'required' => false, + 'hint' => $this->l('Allowed characters: letters, spaces and').' (-)', + 'p' => $this->l('Store name, e.g. Citycentre Mall Store') + ), + array( + 'type' => 'text', + 'label' => $this->l('Address:'), + 'name' => 'address1', + 'size' => 33, + 'required' => true + ), + array( + 'type' => 'text', + 'label' => $this->l('Address:'), + 'name' => 'address2', + 'size' => 33 + ), + array( + 'type' => 'text', + 'label' => $this->l('Postcode/ Zip Code:'), + 'name' => 'postcode', + 'size' => 6 + ), + array( + 'type' => 'text', + 'label' => $this->l('City:'), + 'name' => 'city', + 'size' => 33, + 'class' => 'uppercase', + 'required' => true + ), + array( + 'type' => 'select', + 'label' => $this->l('Country:'), + 'name' => 'id_country', + 'required' => true, + 'options' => array( + 'query' => Country::getCountries($this->context->language->id), + 'id' => 'id_country', + 'name' => 'name' + ) + ), + array( + 'type' => 'select', + 'label' => $this->l('State:'), + 'name' => 'id_state', + 'required' => true, + 'options' => array( + 'id' => 'id_state', + 'name' => 'name' + ) + ), + array( + 'type' => 'text', + 'label' => $this->l('Latitude / Longitude:'), + 'name' => 'latitude', + 'required' => true, + 'size' => 11, + 'maxlength' => 12, + 'p' => $this->l('Store coords, eg. 45.265469 / -47.226478') + ), + array( + 'type' => 'text', + 'label' => $this->l('Phone:'), + 'name' => 'phone', + 'size' => 33 + ), + array( + 'type' => 'text', + 'label' => $this->l('Fax:'), + 'name' => 'fax', + 'size' => 33 + ), + array( + 'type' => 'text', + 'label' => $this->l('E-mail address:'), + 'name' => 'email', + 'size' => 33 + ), + array( + 'type' => 'textarea', + 'label' => $this->l('Note:'), + 'name' => 'note', + 'cols' => 42, + 'rows' => 4 + ), + array( + 'type' => 'radio', + 'label' => $this->l('Status:'), + 'name' => 'active', + 'required' => false, + 'class' => 't', + 'is_bool' => true, + 'values' => array( + array( + 'id' => 'active_on', + 'value' => 1, + 'label' => $this->l('Enabled') + ), + array( + 'id' => 'active_off', + 'value' => 0, + 'label' => $this->l('Disabled') + ) + ), + 'p' => $this->l('Display or not this store') + ) + ), + 'rightCols' => array ( + 'input' => array( + 'type' => 'file', + 'label' => $this->l('Picture:'), + 'name' => 'image', + 'p' => $this->l('Store window picture') + ) + ), + 'submit' => array( + 'title' => $this->l(' Save '), + 'class' => 'button' + ) + ); + + $this->form_list = array( + 'option_store_pref' => array( + 'general' => array( + 'title' => $this->l('Parameters'), + 'fields' => array( + 'PS_STORES_DISPLAY_FOOTER' => array( + 'title' => $this->l('Display in the footer:'), + 'desc' => $this->l('Display a link to the store locator in the footer'), + 'cast' => 'intval', + 'type' => 'bool' + ), + 'PS_STORES_DISPLAY_SITEMAP' => array( + 'title' => $this->l('Display in the sitemap page:'), + 'desc' => $this->l('Display a link to the store locator in the sitemap page'), + 'cast' => 'intval', + 'type' => 'bool' + ), + 'PS_STORES_SIMPLIFIED' => array( + 'title' => $this->l('Show a simplified store locator:'), + 'desc' => $this->l('No map, no search, only a store directory'), + 'cast' => 'intval', + 'type' => 'bool' + ), + 'PS_STORES_CENTER_LAT' => array( + 'title' => $this->l('Latitude by default:'), + 'desc' => $this->l('Used for the position by default of the map'), + 'cast' => 'floatval', + 'type' => 'text', + 'size' => '10' + ), + 'PS_STORES_CENTER_LONG' => array( + 'title' => $this->l('Longitude by default:'), + 'desc' => $this->l('Used for the position by default of the map'), + 'cast' => 'floatval', + 'type' => 'text', + 'size' => '10' + ) + ) + ) + ) + ); + + parent::__construct(); + } + + public function postProcess() + { + if (isset($_POST['submitAdd'.$this->table])) + { + /* Cleaning fields */ + foreach ($_POST as $kp => $vp) + $_POST[$kp] = trim($vp); + + /* If the selected country does not contain states */ + $id_state = (int)Tools::getValue('id_state'); + $id_country = (int)Tools::getValue('id_country'); + $country = new Country((int)$id_country); + + if ($id_country && $country && !(int)$country->contains_states && $id_state) + $this->_errors[] = Tools::displayError('You have selected a state for a country that does not contain states.'); + + /* If the selected country contains states, then a state have to be selected */ + if ((int)$country->contains_states && !$id_state) + $this->_errors[] = Tools::displayError('An address located in a country containing states must have a state selected.'); + + $latitude = (float)Tools::getValue('latitude'); + $longitude = (float)Tools::getValue('longitude'); + + if (empty($latitude) || empty($longitude)) + $this->_errors[] = Tools::displayError('Latitude and longitude are required.'); + + /* Check zip code */ + if ($country->need_zip_code) + { + $zip_code_format = $country->zip_code_format; + if (($postcode = Tools::getValue('postcode')) && $zip_code_format) + { + $zip_regexp = '/^'.$zip_code_format.'$/ui'; + $zip_regexp = str_replace(' ', '( |)', $zip_regexp); + $zip_regexp = str_replace('-', '(-|)', $zip_regexp); + $zip_regexp = str_replace('N', '[0-9]', $zip_regexp); + $zip_regexp = str_replace('L', '[a-zA-Z]', $zip_regexp); + $zip_regexp = str_replace('C', $country->iso_code, $zip_regexp); + if (!preg_match($zip_regexp, $postcode)) + $this->_errors[] = Tools::displayError('Your zip/postal code is incorrect.').'
'.Tools::displayError('Must be typed as follows:').' '. + str_replace( + 'C', + $country->iso_code, + str_replace( + 'N', + '0', + str_replace( + 'L', + 'A', + $zip_code_format + ) + ) + ); + } + else if ($zip_code_format) + $this->_errors[] = Tools::displayError('Postcode required.'); + else if ($postcode && !preg_match('/^[0-9a-zA-Z -]{4,9}$/ui', $postcode)) + $this->_errors[] = Tools::displayError('Your zip/postal code is incorrect.'); + } + + /* Store hours */ + $_POST['hours'] = array(); + for ($i = 1; $i < 8; $i++) + $_POST['hours'][] .= Tools::getValue('hours_'.(int)$i); + $_POST['hours'] = serialize($_POST['hours']); + } + + if (!count($this->_errors)) + parent::postProcess(); + } + + public function initContent() + { + if (!($obj = $this->loadObject(true))) + return; + + $image = cacheImage(_PS_STORE_IMG_DIR_.'/'.$obj->id.'.jpg', $this->table.'_'.(int)$obj->id.'.'.$this->imageType, 350, $this->imageType, true); + + $days = array(); + $days[1] = $this->l('Monday'); + $days[2] = $this->l('Tuesday'); + $days[3] = $this->l('Wednesday'); + $days[4] = $this->l('Thursday'); + $days[5] = $this->l('Friday'); + $days[6] = $this->l('Saturday'); + $days[7] = $this->l('Sunday'); + + $hours = $this->getFieldValue($obj, 'hours'); + if (!empty($hours)) + $hours_unserialized = unserialize($hours); + + $this->fields_value = array( + 'latitude' => $this->getFieldValue($obj, 'latitude') ? $this->getFieldValue($obj, 'latitude') : Configuration::get('PS_STORES_CENTER_LAT'), + 'longitude' => $this->getFieldValue($obj, 'longitude') ? $this->getFieldValue($obj, 'longitude') : Configuration::get('PS_STORES_CENTER_LONG'), + 'image' => $image ? $image : false, + 'size' => $image ? filesize(_PS_STORE_IMG_DIR_.'/'.$obj->id.'.jpg') / 1000 : false, + 'days' => $days, + 'hours' => isset($hours_unserialized) ? $hours_unserialized : false + ); + + if ($this->display != 'edit' && $this->display != 'add') + $this->display = 'list'; + + parent::initContent(); + if ($this->display == 'list') + { + $helper = new HelperOptions(); + + if (file_exists($this->context->smarty->template_dir.'/'.$this->tpl_folder.'options.tpl')) + $helper->tpl = $this->tpl_folder.'options.tpl'; + + $helper->id = $this->id; + $helper->currentIndex = self::$currentIndex; + $this->content .= $helper->generateOptions($this->form_list['option_store_pref']); + } + } + + protected function postImage($id) + { + $ret = parent::postImage($id); + if (($id_store = (int)Tools::getValue('id_store')) && isset($_FILES) && count($_FILES) && file_exists(_PS_STORE_IMG_DIR_.$id_store.'.jpg')) + { + $images_types = ImageType::getImagesTypes('stores'); + foreach ($images_types as $k => $image_type) + imageResize(_PS_STORE_IMG_DIR_.$id_store.'.jpg', + _PS_STORE_IMG_DIR_.$id_store.'-'.stripslashes($image_type['name']).'.jpg', + (int)$image_type['width'], (int)$image_type['height'] + ); + } + return $ret; + } + +} + +