PageRenderTime 38ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/classes/helper/Helper.php

https://github.com/netplayer/PrestaShop
PHP | 406 lines | 266 code | 41 blank | 99 comment | 60 complexity | e3b35f32035b291805c774b842236db8 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-2.1, LGPL-3.0
  1. <?php
  2. /*
  3. * 2007-2014 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-2014 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. class HelperCore
  27. {
  28. public $currentIndex;
  29. public $table = 'configuration';
  30. public $identifier;
  31. public $token;
  32. public $toolbar_btn;
  33. public $ps_help_context;
  34. public $title;
  35. public $show_toolbar = true;
  36. public $context;
  37. public $toolbar_scroll = false;
  38. public $bootstrap = false;
  39. /**
  40. * @var Module
  41. */
  42. public $module;
  43. /** @var string Helper tpl folder */
  44. public $base_folder;
  45. /** @var string Controller tpl folder */
  46. public $override_folder;
  47. /**
  48. * @var smartyTemplate base template object
  49. */
  50. protected $tpl;
  51. /**
  52. * @var string base template name
  53. */
  54. public $base_tpl = 'content.tpl';
  55. public $tpl_vars = array();
  56. public function __construct()
  57. {
  58. $this->context = Context::getContext();
  59. }
  60. public function setTpl($tpl)
  61. {
  62. $this->tpl = $this->createTemplate($tpl);
  63. }
  64. /**
  65. * Create a template from the override file, else from the base file.
  66. *
  67. * @param string $tpl_name filename
  68. * @return Template
  69. */
  70. public function createTemplate($tpl_name)
  71. {
  72. if ($this->override_folder)
  73. {
  74. if ($this->context->controller instanceof ModuleAdminController)
  75. $override_tpl_path = $this->context->controller->getTemplatePath().$this->override_folder.$this->base_folder.$tpl_name;
  76. else if ($this->module)
  77. $override_tpl_path = _PS_MODULE_DIR_.$this->module->name.'/views/templates/admin/_configure/'.$this->override_folder.$this->base_folder.$tpl_name;
  78. else
  79. {
  80. if (file_exists($this->context->smarty->getTemplateDir(1).$this->override_folder.$this->base_folder.$tpl_name))
  81. $override_tpl_path = $this->context->smarty->getTemplateDir(1).$this->override_folder.$this->base_folder.$tpl_name;
  82. else if (file_exists($this->context->smarty->getTemplateDir(0).'controllers'.DIRECTORY_SEPARATOR.$this->override_folder.$this->base_folder.$tpl_name))
  83. $override_tpl_path = $this->context->smarty->getTemplateDir(0).'controllers'.DIRECTORY_SEPARATOR.$this->override_folder.$this->base_folder.$tpl_name;
  84. }
  85. }
  86. else if ($this->module)
  87. $override_tpl_path = _PS_MODULE_DIR_.$this->module->name.'/views/templates/admin/_configure/'.$this->base_folder.$tpl_name;
  88. if (isset($override_tpl_path) && file_exists($override_tpl_path))
  89. return $this->context->smarty->createTemplate($override_tpl_path, $this->context->smarty);
  90. else
  91. return $this->context->smarty->createTemplate($this->base_folder.$tpl_name, $this->context->smarty);
  92. }
  93. /**
  94. * default behaviour for helper is to return a tpl fetched
  95. *
  96. * @return string
  97. */
  98. public function generate()
  99. {
  100. $this->tpl->assign($this->tpl_vars);
  101. return $this->tpl->fetch();
  102. }
  103. /**
  104. * @deprecated 1.5.0
  105. */
  106. public static function renderAdminCategorieTree($translations,
  107. $selected_cat = array(),
  108. $input_name = 'categoryBox',
  109. $use_radio = false,
  110. $use_search = false,
  111. $disabled_categories = array(),
  112. $use_in_popup = false)
  113. {
  114. Tools::displayAsDeprecated();
  115. $helper = new Helper();
  116. if (isset($translations['Root']))
  117. $root = $translations['Root'];
  118. else if (isset($translations['Home']))
  119. $root = array('name' => $translations['Home'], 'id_category' => 1);
  120. else
  121. throw new PrestaShopException('Missing root category parameter.');
  122. return $helper->renderCategoryTree($root, $selected_cat, $input_name, $use_radio, $use_search, $disabled_categories, $use_in_popup);
  123. }
  124. /**
  125. *
  126. * @param array $root array with the name and ID of the tree root category, if null the Shop's root category will be used
  127. * @param type $selected_cat array of selected categories
  128. * Format
  129. * Array
  130. * (
  131. * [0] => 1
  132. * [1] => 2
  133. * )
  134. * OR
  135. * Array
  136. * (
  137. * [1] => Array
  138. * (
  139. * [id_category] => 1
  140. * [name] => Home page
  141. * )
  142. * )
  143. * @param string $input_name name of input
  144. * @param bool $use_radio use radio tree or checkbox tree
  145. * @param bool $use_search display a find category search box
  146. * @param array $disabled_categories
  147. * @param bool $use_in_popup
  148. * @param bool $use_shop_context
  149. * @return string
  150. */
  151. public function renderCategoryTree($root = null,
  152. $selected_cat = array(),
  153. $input_name = 'categoryBox',
  154. $use_radio = false,
  155. $use_search = false,
  156. $disabled_categories = array(),
  157. $use_in_popup = false,
  158. $use_shop_context = false)
  159. {
  160. $translations = array(
  161. 'selected' => $this->l('Selected'),
  162. 'Collapse All' => $this->l('Collapse All'),
  163. 'Expand All' => $this->l('Expand All'),
  164. 'Check All' => $this->l('Check All'),
  165. 'Uncheck All' => $this->l('Uncheck All'),
  166. 'search' => $this->l('Find a category')
  167. );
  168. $top_category = Category::getTopCategory();
  169. if (Tools::isSubmit('id_shop'))
  170. $id_shop = Tools::getValue('id_shop');
  171. else
  172. if (Context::getContext()->shop->id)
  173. $id_shop = Context::getContext()->shop->id;
  174. else
  175. if (!Shop::isFeatureActive())
  176. $id_shop = Configuration::get('PS_SHOP_DEFAULT');
  177. else
  178. $id_shop = 0;
  179. $shop = new Shop($id_shop);
  180. $root_category = Category::getRootCategory(null, $shop);
  181. $disabled_categories[] = $top_category->id;
  182. if (!$root)
  183. $root = array('name' => $root_category->name, 'id_category' => $root_category->id);
  184. if (!$use_radio)
  185. $input_name = $input_name.'[]';
  186. if ($use_search)
  187. $this->context->controller->addJs(_PS_JS_DIR_.'jquery/plugins/autocomplete/jquery.autocomplete.js');
  188. $html = '
  189. <script type="text/javascript">
  190. var inputName = \''.addcslashes($input_name, '\'').'\';'."\n";
  191. if (count($selected_cat) > 0)
  192. {
  193. if (isset($selected_cat[0]))
  194. $html .= ' var selectedCat = '.(int)implode(',', $selected_cat).';'."\n";
  195. else
  196. $html .= ' var selectedCat = '.(int)implode(',', array_keys($selected_cat)).';'."\n";
  197. }
  198. else
  199. $html .= ' var selectedCat = \'\';'."\n";
  200. $html .= ' var selectedLabel = \''.$translations['selected'].'\';
  201. var home = \''.addcslashes($root['name'], '\'').'\';
  202. var use_radio = '.(int)$use_radio.';';
  203. $html .= '</script>';
  204. $html .= '
  205. <div class="category-filter">
  206. <a class="btn btn-link" href="#" id="collapse_all"><i class="icon-collapse"></i> '.$translations['Collapse All'].'</a>
  207. <a class="btn btn-link" href="#" id="expand_all"><i class="icon-expand"></i> '.$translations['Expand All'].'</a>
  208. '.(!$use_radio ? '
  209. <a class="btn btn-link" href="#" id="check_all"><i class="icon-check"></i> '.$translations['Check All'].'</a>
  210. <a class="btn btn-link" href="#" id="uncheck_all"><i class="icon-check-empty"></i> '.$translations['Uncheck All'].'</a>' : '')
  211. .($use_search ? '
  212. <div class="row">
  213. <label class="control-label col-lg-6" for="search_cat">'.$translations['search'].' :</label>
  214. <div class="col-lg-6">
  215. <input type="text" name="search_cat" id="search_cat"/>
  216. </div>
  217. </div>' : '')
  218. .'</div>';
  219. $home_is_selected = false;
  220. foreach ($selected_cat as $cat)
  221. {
  222. if (is_array($cat))
  223. {
  224. $disabled = in_array($cat['id_category'], $disabled_categories);
  225. if ($cat['id_category'] != $root['id_category'])
  226. $html .= '<input '.($disabled?'disabled="disabled"':'').' type="hidden" name="'.$input_name.'" value="'.$cat['id_category'].'" >';
  227. else
  228. $home_is_selected = true;
  229. }
  230. else
  231. {
  232. $disabled = in_array($cat, $disabled_categories);
  233. if ($cat != $root['id_category'])
  234. $html .= '<input '.($disabled?'disabled="disabled"':'').' type="hidden" name="'.$input_name.'" value="'.$cat.'" >';
  235. else
  236. $home_is_selected = true;
  237. }
  238. }
  239. $root_input = '';
  240. if ($root['id_category'] != $top_category->id || (Tools::isSubmit('ajax') && Tools::getValue('action') == 'getCategoriesFromRootCategory'))
  241. $root_input = '
  242. <p class="checkbox"><i class="icon-folder-open"></i><label>
  243. <input type="'.(!$use_radio ? 'checkbox' : 'radio').'" name="'
  244. .$input_name.'" value="'.$root['id_category'].'" '
  245. .($home_is_selected ? 'checked' : '').' onclick="clickOnCategoryBox($(this));" />'
  246. .$root['name'].
  247. '</label></p>';
  248. $html .= '
  249. <div class="container">
  250. <div class="well">
  251. <ul id="categories-treeview">
  252. <li id="'.$root['id_category'].'" class="hasChildren">
  253. <span class="folder">'.$root_input.' </span>
  254. <ul>
  255. <li><span class="placeholder">&nbsp;</span></li>
  256. </ul>
  257. </li>
  258. </ul>
  259. </div>
  260. </div>';
  261. if ($use_search)
  262. $html .= '<script type="text/javascript">searchCategory();</script>';
  263. return $html;
  264. }
  265. /**
  266. * use translations files to replace english expression.
  267. *
  268. * @param mixed $string term or expression in english
  269. * @param string $class
  270. * @param boolan $addslashes if set to true, the return value will pass through addslashes(). Otherwise, stripslashes().
  271. * @param boolean $htmlentities if set to true(default), the return value will pass through htmlentities($string, ENT_QUOTES, 'utf-8')
  272. * @return string the translation if available, or the english default text.
  273. */
  274. protected function l($string, $class = 'AdminTab', $addslashes = false, $htmlentities = true)
  275. {
  276. // if the class is extended by a module, use modules/[module_name]/xx.php lang file
  277. $currentClass = get_class($this);
  278. if (Module::getModuleNameFromClass($currentClass))
  279. return Translate::getModuleTranslation(Module::$classInModule[$currentClass], $string, $currentClass);
  280. return Translate::getAdminTranslation($string, get_class($this), $addslashes, $htmlentities);
  281. }
  282. /**
  283. * Render a form with potentials required fields
  284. *
  285. * @param string $class_name
  286. * @param string $identifier
  287. * @param array $table_fields
  288. * @return string
  289. */
  290. public function renderRequiredFields($class_name, $identifier, $table_fields)
  291. {
  292. $rules = call_user_func_array(array($class_name, 'getValidationRules'), array($class_name));
  293. $required_class_fields = array($identifier);
  294. foreach ($rules['required'] as $required)
  295. $required_class_fields[] = $required;
  296. $object = new $class_name();
  297. $res = $object->getFieldsRequiredDatabase();
  298. $required_fields = array();
  299. foreach ($res as $row)
  300. $required_fields[(int)$row['id_required_field']] = $row['field_name'];
  301. $this->tpl_vars = array(
  302. 'table_fields' => $table_fields,
  303. 'irow' => 0,
  304. 'required_class_fields' => $required_class_fields,
  305. 'required_fields' => $required_fields,
  306. 'current' => $this->currentIndex,
  307. 'token' => $this->token
  308. );
  309. $tpl = $this->createTemplate('helpers/required_fields.tpl');
  310. $tpl->assign($this->tpl_vars);
  311. return $tpl->fetch();
  312. }
  313. public function renderModulesList($modules_list)
  314. {
  315. $this->tpl_vars = array(
  316. 'modules_list' => $modules_list,
  317. 'modules_uri' => __PS_BASE_URI__.basename(_PS_MODULE_DIR_)
  318. );
  319. // The translations for this are defined by AdminModules, so override the context for the translations
  320. $override_controller_name_for_translations = Context::getContext()->override_controller_name_for_translations;
  321. Context::getContext()->override_controller_name_for_translations = 'AdminModules';
  322. $tpl = $this->createTemplate('helpers/modules_list/list.tpl');
  323. $tpl->assign($this->tpl_vars);
  324. $html = $tpl->fetch();
  325. // Restore the previous context
  326. Context::getContext()->override_controller_name_for_translations = $override_controller_name_for_translations;
  327. return $html;
  328. }
  329. public static function renderShopList()
  330. {
  331. if (!Shop::isFeatureActive() || Shop::getTotalShops(false, null) < 2)
  332. return null;
  333. $tree = Shop::getTree();
  334. $context = Context::getContext();
  335. // Get default value
  336. $shop_context = Shop::getContext();
  337. if ($shop_context == Shop::CONTEXT_ALL || ($context->controller->multishop_context_group == false && $shop_context == Shop::CONTEXT_GROUP))
  338. $value = '';
  339. else if ($shop_context == Shop::CONTEXT_GROUP)
  340. $value = 'g-'.Shop::getContextShopGroupID();
  341. else
  342. $value = 's-'.Shop::getContextShopID();
  343. // Generate HTML
  344. $url = $_SERVER['REQUEST_URI'].(($_SERVER['QUERY_STRING']) ? '&' : '?').'setShopContext=';
  345. $shop = new Shop(Shop::getContextShopID());
  346. // $html = '<a href="#"><i class="icon-home"></i> '.$shop->name.'</a>';
  347. $html = '<select class="shopList" onchange="location.href = \''.$url.'\'+$(this).val();">';
  348. $html .= '<option value="" class="first">'.Translate::getAdminTranslation('All shops').'</option>';
  349. foreach ($tree as $gID => $group_data)
  350. {
  351. if ((!isset($context->controller->multishop_context) || $context->controller->multishop_context & Shop::CONTEXT_GROUP))
  352. $html .= '<option class="group" value="g-'.$gID.'"'.(((empty($value) && $shop_context == Shop::CONTEXT_GROUP) || $value == 'g-'.$gID) ? ' selected="selected"' : '').($context->controller->multishop_context_group == false ? ' disabled="disabled"' : '').'>'.Translate::getAdminTranslation('Group:').' '.htmlspecialchars($group_data['name']).'</option>';
  353. else
  354. $html .= '<optgroup class="group" label="'.Translate::getAdminTranslation('Group:').' '.htmlspecialchars($group_data['name']).'"'.($context->controller->multishop_context_group == false ? ' disabled="disabled"' : '').'>';
  355. if (!isset($context->controller->multishop_context) || $context->controller->multishop_context & Shop::CONTEXT_SHOP)
  356. foreach ($group_data['shops'] as $sID => $shopData)
  357. if ($shopData['active'])
  358. $html .= '<option value="s-'.$sID.'" class="shop"'.(($value == 's-'.$sID) ? ' selected="selected"' : '').'>'.($context->controller->multishop_context_group == false ? htmlspecialchars($group_data['name']).' - ' : '').$shopData['name'].'</option>';
  359. if (!(!isset($context->controller->multishop_context) || $context->controller->multishop_context & Shop::CONTEXT_GROUP))
  360. $html .= '</optgroup>';
  361. }
  362. $html .= '</select>';
  363. return $html;
  364. }
  365. }