PageRenderTime 24ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/classes/helper/HelperForm.php

https://gitlab.com/staging06/myproject
PHP | 337 lines | 247 code | 42 blank | 48 comment | 45 complexity | c51179448c99159f18714c467722d9e3 MD5 | raw file
  1. <?php
  2. /*
  3. * 2007-2015 PrestaShop
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@prestashop.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
  18. * versions in the future. If you wish to customize PrestaShop for your
  19. * needs please refer to http://www.prestashop.com for more information.
  20. *
  21. * @author PrestaShop SA <contact@prestashop.com>
  22. * @copyright 2007-2015 PrestaShop SA
  23. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  24. * International Registered Trademark & Property of PrestaShop SA
  25. */
  26. /**
  27. * @since 1.5.0
  28. */
  29. class HelperFormCore extends Helper
  30. {
  31. public $id;
  32. public $first_call = true;
  33. /** @var array of forms fields */
  34. protected $fields_form = array();
  35. /** @var array values of form fields */
  36. public $fields_value = array();
  37. public $name_controller = '';
  38. /** @var string if not null, a title will be added on that list */
  39. public $title = null;
  40. /** @var string Used to override default 'submitAdd' parameter in form action attribute */
  41. public $submit_action;
  42. public $token;
  43. public $languages = null;
  44. public $default_form_language = null;
  45. public $allow_employee_form_lang = null;
  46. public $show_cancel_button = false;
  47. public $back_url = '#';
  48. public function __construct()
  49. {
  50. $this->base_folder = 'helpers/form/';
  51. $this->base_tpl = 'form.tpl';
  52. parent::__construct();
  53. }
  54. public function generateForm($fields_form)
  55. {
  56. $this->fields_form = $fields_form;
  57. return $this->generate();
  58. }
  59. public function generate()
  60. {
  61. $this->tpl = $this->createTemplate($this->base_tpl);
  62. if (is_null($this->submit_action)) {
  63. $this->submit_action = 'submitAdd'.$this->table;
  64. }
  65. $categories = true;
  66. $color = true;
  67. $date = true;
  68. $tinymce = true;
  69. $textarea_autosize = true;
  70. $file = true;
  71. foreach ($this->fields_form as $fieldset_key => &$fieldset) {
  72. if (isset($fieldset['form']['tabs'])) {
  73. $tabs[] = $fieldset['form']['tabs'];
  74. }
  75. if (isset($fieldset['form']['input'])) {
  76. foreach ($fieldset['form']['input'] as $key => &$params) {
  77. // If the condition is not met, the field will not be displayed
  78. if (isset($params['condition']) && !$params['condition']) {
  79. unset($this->fields_form[$fieldset_key]['form']['input'][$key]);
  80. }
  81. switch ($params['type']) {
  82. case 'select':
  83. $field_name = (string)$params['name'];
  84. // If multiple select check that 'name' field is suffixed with '[]'
  85. if (isset($params['multiple']) && $params['multiple'] && stripos($field_name, '[]') === false) {
  86. $params['name'] .= '[]';
  87. }
  88. break;
  89. case 'categories':
  90. if ($categories) {
  91. if (!isset($params['tree']['id'])) {
  92. throw new PrestaShopException('Id must be filled for categories tree');
  93. }
  94. $tree = new HelperTreeCategories($params['tree']['id'], isset($params['tree']['title']) ? $params['tree']['title'] : null);
  95. if (isset($params['name'])) {
  96. $tree->setInputName($params['name']);
  97. }
  98. if (isset($params['tree']['selected_categories'])) {
  99. $tree->setSelectedCategories($params['tree']['selected_categories']);
  100. }
  101. if (isset($params['tree']['disabled_categories'])) {
  102. $tree->setDisabledCategories($params['tree']['disabled_categories']);
  103. }
  104. if (isset($params['tree']['root_category'])) {
  105. $tree->setRootCategory($params['tree']['root_category']);
  106. }
  107. if (isset($params['tree']['use_search'])) {
  108. $tree->setUseSearch($params['tree']['use_search']);
  109. }
  110. if (isset($params['tree']['use_checkbox'])) {
  111. $tree->setUseCheckBox($params['tree']['use_checkbox']);
  112. }
  113. if (isset($params['tree']['set_data'])) {
  114. $tree->setData($params['tree']['set_data']);
  115. }
  116. $this->context->smarty->assign('categories_tree', $tree->render());
  117. $categories = false;
  118. }
  119. break;
  120. case 'file':
  121. $uploader = new HelperUploader();
  122. $uploader->setId(isset($params['id'])?$params['id']:null);
  123. $uploader->setName($params['name']);
  124. $uploader->setUrl(isset($params['url'])?$params['url']:null);
  125. $uploader->setMultiple(isset($params['multiple'])?$params['multiple']:false);
  126. $uploader->setUseAjax(isset($params['ajax'])?$params['ajax']:false);
  127. $uploader->setMaxFiles(isset($params['max_files'])?$params['max_files']:null);
  128. if (isset($params['files']) && $params['files']) {
  129. $uploader->setFiles($params['files']);
  130. } elseif (isset($params['image']) && $params['image']) { // Use for retrocompatibility
  131. $uploader->setFiles(array(
  132. 0 => array(
  133. 'type' => HelperUploader::TYPE_IMAGE,
  134. 'image' => isset($params['image'])?$params['image']:null,
  135. 'size' => isset($params['size'])?$params['size']:null,
  136. 'delete_url' => isset($params['delete_url'])?$params['delete_url']:null
  137. )));
  138. }
  139. if (isset($params['file']) && $params['file']) { // Use for retrocompatibility
  140. $uploader->setFiles(array(
  141. 0 => array(
  142. 'type' => HelperUploader::TYPE_FILE,
  143. 'size' => isset($params['size'])?$params['size']:null,
  144. 'delete_url' => isset($params['delete_url'])?$params['delete_url']:null,
  145. 'download_url' => isset($params['file'])?$params['file']:null
  146. )));
  147. }
  148. if (isset($params['thumb']) && $params['thumb']) { // Use for retrocompatibility
  149. $uploader->setFiles(array(
  150. 0 => array(
  151. 'type' => HelperUploader::TYPE_IMAGE,
  152. 'image' => isset($params['thumb'])?'<img src="'.$params['thumb'].'" alt="'.(isset($params['title']) ? $params['title'] : '').'" title="'.(isset($params['title']) ? $params['title'] : '').'" />':null,
  153. )));
  154. }
  155. $uploader->setTitle(isset($params['title'])?$params['title']:null);
  156. $params['file'] = $uploader->render();
  157. break;
  158. case 'color':
  159. if ($color) {
  160. // Added JS file
  161. $this->context->controller->addJqueryPlugin('colorpicker');
  162. $color = false;
  163. }
  164. break;
  165. case 'date':
  166. if ($date) {
  167. $this->context->controller->addJqueryUI('ui.datepicker');
  168. $date = false;
  169. }
  170. break;
  171. case 'textarea':
  172. if ($tinymce) {
  173. $iso = $this->context->language->iso_code;
  174. $this->tpl_vars['iso'] = file_exists(_PS_CORE_DIR_.'/js/tiny_mce/langs/'.$iso.'.js') ? $iso : 'en';
  175. $this->tpl_vars['path_css'] = _THEME_CSS_DIR_;
  176. $this->tpl_vars['ad'] = __PS_BASE_URI__.basename(_PS_ADMIN_DIR_);
  177. $this->tpl_vars['tinymce'] = true;
  178. $this->context->controller->addJS(_PS_JS_DIR_.'tiny_mce/tiny_mce.js');
  179. $this->context->controller->addJS(_PS_JS_DIR_.'admin/tinymce.inc.js');
  180. $tinymce = false;
  181. }
  182. if ($textarea_autosize) {
  183. $this->context->controller->addJqueryPlugin('autosize');
  184. $textarea_autosize = false;
  185. }
  186. break;
  187. case 'shop' :
  188. $disable_shops = isset($params['disable_shared']) ? $params['disable_shared'] : false;
  189. $params['html'] = $this->renderAssoShop($disable_shops);
  190. if (Shop::getTotalShops(false) == 1) {
  191. if ((isset($this->fields_form[$fieldset_key]['form']['force']) && !$this->fields_form[$fieldset_key]['form']['force']) || !isset($this->fields_form[$fieldset_key]['form']['force'])) {
  192. unset($this->fields_form[$fieldset_key]['form']['input'][$key]);
  193. }
  194. }
  195. break;
  196. }
  197. }
  198. }
  199. }
  200. $this->tpl->assign(array(
  201. 'title' => $this->title,
  202. 'toolbar_btn' => $this->toolbar_btn,
  203. 'show_toolbar' => $this->show_toolbar,
  204. 'toolbar_scroll' => $this->toolbar_scroll,
  205. 'submit_action' => $this->submit_action,
  206. 'firstCall' => $this->first_call,
  207. 'current' => $this->currentIndex,
  208. 'token' => $this->token,
  209. 'table' => $this->table,
  210. 'identifier' => $this->identifier,
  211. 'name_controller' => $this->name_controller,
  212. 'languages' => $this->languages,
  213. 'current_id_lang' => $this->context->language->id,
  214. 'defaultFormLanguage' => $this->default_form_language,
  215. 'allowEmployeeFormLang' => $this->allow_employee_form_lang,
  216. 'form_id' => $this->id,
  217. 'tabs' => (isset($tabs)) ? $tabs : null,
  218. 'fields' => $this->fields_form,
  219. 'fields_value' => $this->fields_value,
  220. 'required_fields' => $this->getFieldsRequired(),
  221. 'vat_number' => Module::isInstalled('vatnumber') && file_exists(_PS_MODULE_DIR_.'vatnumber/ajax.php'),
  222. 'module_dir' => _MODULE_DIR_,
  223. 'base_url' => $this->context->shop->getBaseURL(),
  224. 'contains_states' => (isset($this->fields_value['id_country']) && isset($this->fields_value['id_state'])) ? Country::containsStates($this->fields_value['id_country']) : null,
  225. 'show_cancel_button' => $this->show_cancel_button,
  226. 'back_url' => $this->back_url
  227. ));
  228. return parent::generate();
  229. }
  230. /**
  231. * Return true if there are required fields
  232. */
  233. public function getFieldsRequired()
  234. {
  235. foreach ($this->fields_form as $fieldset) {
  236. if (isset($fieldset['form']['input'])) {
  237. foreach ($fieldset['form']['input'] as $input) {
  238. if (!empty($input['required']) && $input['type'] != 'radio') {
  239. return true;
  240. }
  241. }
  242. }
  243. }
  244. return false;
  245. }
  246. /**
  247. * Render an area to determinate shop association
  248. *
  249. * @return string
  250. */
  251. public function renderAssoShop($disable_shared = false, $template_directory = null)
  252. {
  253. if (!Shop::isFeatureActive()) {
  254. return;
  255. }
  256. $assos = array();
  257. if ((int)$this->id) {
  258. $sql = 'SELECT `id_shop`, `'.bqSQL($this->identifier).'`
  259. FROM `'._DB_PREFIX_.bqSQL($this->table).'_shop`
  260. WHERE `'.bqSQL($this->identifier).'` = '.(int)$this->id;
  261. foreach (Db::getInstance()->executeS($sql) as $row) {
  262. $assos[$row['id_shop']] = $row['id_shop'];
  263. }
  264. } else {
  265. switch (Shop::getContext()) {
  266. case Shop::CONTEXT_SHOP :
  267. $assos[Shop::getContextShopID()] = Shop::getContextShopID();
  268. break;
  269. case Shop::CONTEXT_GROUP :
  270. foreach (Shop::getShops(false, Shop::getContextShopGroupID(), true) as $id_shop) {
  271. $assos[$id_shop] = $id_shop;
  272. }
  273. break;
  274. default :
  275. foreach (Shop::getShops(false, null, true) as $id_shop) {
  276. $assos[$id_shop] = $id_shop;
  277. }
  278. break;
  279. }
  280. }
  281. /*$nb_shop = 0;
  282. foreach ($tree as &$value)
  283. {
  284. $value['disable_shops'] = (isset($value[$disable_shared]) && $value[$disable_shared]);
  285. $nb_shop += count($value['shops']);
  286. }*/
  287. $tree = new HelperTreeShops('shop-tree', 'Shops');
  288. if (isset($template_directory)) {
  289. $tree->setTemplateDirectory($template_directory);
  290. }
  291. $tree->setSelectedShops($assos);
  292. $tree->setAttribute('table', $this->table);
  293. return $tree->render();
  294. }
  295. }