PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/controllers/admin/AdminSearchConfController.php

https://github.com/netplayer/PrestaShop
PHP | 310 lines | 260 code | 20 blank | 30 comment | 12 complexity | 11ea7f9210c0c0f151019af7f346ad20 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 AdminSearchConfControllerCore extends AdminController
  27. {
  28. protected $toolbar_scroll = false;
  29. public function __construct()
  30. {
  31. $this->bootstrap = true;
  32. $this->table = 'alias';
  33. $this->className = 'Alias';
  34. $this->lang = false;
  35. parent::__construct();
  36. // Alias fields
  37. $this->addRowAction('edit');
  38. $this->addRowAction('delete');
  39. if (!Tools::getValue('realedit'))
  40. $this->deleted = false;
  41. $this->bulk_actions = array(
  42. 'delete' => array(
  43. 'text' => $this->l('Delete selected'),
  44. 'confirm' => $this->l('Delete selected items?'),
  45. 'icon' => 'icon-trash'
  46. )
  47. );
  48. $this->fields_list = array(
  49. 'alias' => array('title' => $this->l('Aliases')),
  50. 'search' => array('title' => $this->l('Search')),
  51. 'active' => array('title' => $this->l('Status'), 'class' => 'fixed-width-sm', 'align' => 'center', 'active' => 'status', 'type' => 'bool', 'orderby' => false)
  52. );
  53. // Search options
  54. $current_file_name = array_reverse(explode('/', $_SERVER['SCRIPT_NAME']));
  55. $cron_url = Tools::getHttpHost(true, true).__PS_BASE_URI__.basename(_PS_ADMIN_DIR_).
  56. '/searchcron.php?full=1&token='.substr(_COOKIE_KEY_, 34, 8);
  57. list($total, $indexed) = Db::getInstance()->getRow('SELECT COUNT(*) as "0", SUM(product_shop.indexed) as "1" FROM '._DB_PREFIX_.'product p '.Shop::addSqlAssociation('product', 'p').' WHERE product_shop.`visibility` IN ("both", "search") AND product_shop.`active` = 1');
  58. $this->fields_options = array(
  59. 'indexation' => array(
  60. 'title' => $this->l('Indexing'),
  61. 'icon' => 'icon-cogs',
  62. 'info' =>
  63. $this->l('The "indexed" products have been analyzed by PrestaShop and will appear in the results of a Front Office search.').'<br />
  64. '.$this->l('Indexed products').' <strong>'.(int)$indexed.' / '.(int)$total.'</strong>.
  65. </p>
  66. <p>'.$this->l('Building the product index may take a few minutes.')
  67. .$this->l('If your server stops before the process ends, you can resume the indexing by clicking "Add missing products."').'</p>
  68. <a href="searchcron.php?token='.substr(_COOKIE_KEY_, 34, 8).'&redirect=1" class="btn-link"><i class="icon-external-link-sign"></i> '.
  69. $this->l('Add missing products to the index.').'</a><br />
  70. <a href="searchcron.php?full=1&token='.substr(_COOKIE_KEY_, 34, 8).'&redirect=1" class="btn-link"><i class="icon-external-link-sign"></i> '.
  71. $this->l('Re-build the entire index.').'</a><br /><br />
  72. '.$this->l('You can set a cron job that will rebuild your index using the following URL:').' <a href="'.$cron_url.'"><i class="icon-external-link-sign"></i> '.$cron_url.'</a>',
  73. 'fields' => array(
  74. 'PS_SEARCH_INDEXATION' => array(
  75. 'title' => $this->l('Indexing'),
  76. 'validation' => 'isBool',
  77. 'type' => 'bool',
  78. 'cast' => 'intval',
  79. 'desc' => $this->l('Enable the automatic indexing of products. If you enable this feature, the products will be indexed in the search automatically when they are saved. If the feature is disabled, you will have to index products manually by using the links provided in the field set.')
  80. )
  81. ),
  82. 'submit' => array('title' => $this->l('Save'))
  83. ),
  84. 'search' => array(
  85. 'title' => $this->l('Search'),
  86. 'icon' => 'icon-search',
  87. 'fields' => array(
  88. 'PS_SEARCH_AJAX' => array(
  89. 'title' => $this->l('Ajax search'),
  90. 'validation' => 'isBool',
  91. 'type' => 'bool',
  92. 'cast' => 'intval',
  93. 'hint' => array(
  94. $this->l('Enable ajax search for your visitors.'),
  95. $this->l('With ajax search, the first 10 products matching the user query will appear in real time below the input field.')
  96. )
  97. ),
  98. 'PS_INSTANT_SEARCH' => array(
  99. 'title' => $this->l('Instant search'),
  100. 'validation' => 'isBool',
  101. 'cast' => 'intval',
  102. 'type' => 'bool',
  103. 'hint' => array(
  104. $this->l('Enable instant search for your visitors?'),
  105. $this->l('With instant search, the results will appear immediately as the user writes a query.')
  106. )
  107. ),
  108. 'PS_SEARCH_MINWORDLEN' => array(
  109. 'title' => $this->l('Minimum word length (in characters)'),
  110. 'hint' => $this->l('Only words this size or larger will be indexed.'),
  111. 'validation' => 'isUnsignedInt',
  112. 'type' => 'text',
  113. 'cast' => 'intval'
  114. ),
  115. 'PS_SEARCH_BLACKLIST' => array(
  116. 'title' => $this->l('Blacklisted words'),
  117. 'validation' => 'isGenericName',
  118. 'hint' => $this->l('Please enter the index words separated by a "|".'),
  119. 'type' => 'textLang'
  120. )
  121. ),
  122. 'submit' => array('title' => $this->l('Save'))
  123. ),
  124. 'relevance' => array(
  125. 'title' => $this->l('Weight'),
  126. 'icon' => 'icon-cogs',
  127. 'info' =>
  128. $this->l('The "weight" represents its importance and relevance for the ranking of the products when completing a new search.').'<br />
  129. '.$this->l('A word with a weight of eight will have four times more value than a word with a weight of two.').'<br /><br />
  130. '.$this->l('We advise you to set a greater weight for words which appear in the name or reference of a product. This will allow the search results to be as precise and relevant as possible.').'<br /><br />
  131. '.$this->l('Setting a weight to 0 will exclude that field from search index. Re-build of the entire index is required when changing to or from 0'),
  132. 'fields' => array(
  133. 'PS_SEARCH_WEIGHT_PNAME' => array(
  134. 'title' => $this->l('Product name weight'),
  135. 'validation' => 'isUnsignedInt',
  136. 'type' => 'text',
  137. 'cast' => 'intval'
  138. ),
  139. 'PS_SEARCH_WEIGHT_REF' => array(
  140. 'title' => $this->l('Reference weight'),
  141. 'validation' => 'isUnsignedInt',
  142. 'type' => 'text',
  143. 'cast' => 'intval'
  144. ),
  145. 'PS_SEARCH_WEIGHT_SHORTDESC' => array(
  146. 'title' => $this->l('Short description weight'),
  147. 'validation' => 'isUnsignedInt',
  148. 'type' => 'text',
  149. 'cast' => 'intval'
  150. ),
  151. 'PS_SEARCH_WEIGHT_DESC' => array(
  152. 'title' => $this->l('Description weight'),
  153. 'validation' => 'isUnsignedInt',
  154. 'type' => 'text',
  155. 'cast' => 'intval'
  156. ),
  157. 'PS_SEARCH_WEIGHT_CNAME' => array(
  158. 'title' => $this->l('Category weight'),
  159. 'validation' => 'isUnsignedInt',
  160. 'type' => 'text',
  161. 'cast' => 'intval'
  162. ),
  163. 'PS_SEARCH_WEIGHT_MNAME' => array(
  164. 'title' => $this->l('Manufacturer weight'),
  165. 'validation' => 'isUnsignedInt',
  166. 'type' => 'text',
  167. 'cast' => 'intval'
  168. ),
  169. 'PS_SEARCH_WEIGHT_TAG' => array(
  170. 'title' => $this->l('Tags weight'),
  171. 'validation' => 'isUnsignedInt',
  172. 'type' => 'text',
  173. 'cast' => 'intval'
  174. ),
  175. 'PS_SEARCH_WEIGHT_ATTRIBUTE' => array(
  176. 'title' => $this->l('Attributes weight'),
  177. 'validation' => 'isUnsignedInt',
  178. 'type' => 'text',
  179. 'cast' => 'intval'
  180. ),
  181. 'PS_SEARCH_WEIGHT_FEATURE' => array(
  182. 'title' => $this->l('Features weight'),
  183. 'validation' => 'isUnsignedInt',
  184. 'type' => 'text',
  185. 'cast' => 'intval'
  186. )
  187. ),
  188. 'submit' => array('title' => $this->l('Save'))
  189. ),
  190. );
  191. }
  192. public function initPageHeaderToolbar()
  193. {
  194. if (empty($this->display))
  195. $this->page_header_toolbar_btn['new_alias'] = array(
  196. 'href' => self::$currentIndex.'&addalias&token='.$this->token,
  197. 'desc' => $this->l('Add new alias', null, null, false),
  198. 'icon' => 'process-icon-new'
  199. );
  200. $this->identifier_name = 'alias';
  201. parent::initPageHeaderToolbar();
  202. $this->toolbar_btn['import'] = array(
  203. 'href' => $this->context->link->getAdminLink('AdminImport', true).'&import_type=alias',
  204. 'desc' => $this->l('Import', null, null, false)
  205. );
  206. }
  207. public function initProcess()
  208. {
  209. parent::initProcess();
  210. // This is a composite page, we don't want the "options" display mode
  211. if ($this->display == 'options')
  212. $this->display = '';
  213. }
  214. /**
  215. * Function used to render the options for this controller
  216. */
  217. public function renderOptions()
  218. {
  219. if ($this->fields_options && is_array($this->fields_options))
  220. {
  221. $helper = new HelperOptions($this);
  222. $this->setHelperDisplay($helper);
  223. $helper->toolbar_scroll = true;
  224. $helper->toolbar_btn = array('save' => array(
  225. 'href' => '#',
  226. 'desc' => $this->l('Save')
  227. ));
  228. $helper->id = $this->id;
  229. $helper->tpl_vars = $this->tpl_option_vars;
  230. $options = $helper->generateOptions($this->fields_options);
  231. return $options;
  232. }
  233. }
  234. public function renderForm()
  235. {
  236. $this->fields_form = array(
  237. 'legend' => array(
  238. 'title' => $this->l('Aliases'),
  239. 'icon' => 'icon-search'
  240. ),
  241. 'input' => array(
  242. array(
  243. 'type' => 'text',
  244. 'label' => $this->l('Alias'),
  245. 'name' => 'alias',
  246. 'required' => true,
  247. 'hint' => array(
  248. $this->l('Enter each alias separated by a comma (e.g. \'prestshop,preztashop,prestasohp\').'),
  249. $this->l('Forbidden characters: &lt;&gt;;=#{}')
  250. )
  251. ),
  252. array(
  253. 'type' => 'text',
  254. 'label' => $this->l('Result'),
  255. 'name' => 'search',
  256. 'required' => true,
  257. 'hint' => $this->l('Search this word instead.')
  258. )
  259. ),
  260. 'submit' => array(
  261. 'title' => $this->l('Save'),
  262. )
  263. );
  264. $this->fields_value = array('alias' => $this->object->getAliases());
  265. return parent::renderForm();
  266. }
  267. public function processSave()
  268. {
  269. $search = strval(Tools::getValue('search'));
  270. $string = strval(Tools::getValue('alias'));
  271. $aliases = explode(',', $string);
  272. if (empty($search) || empty($string))
  273. $this->errors[] = $this->l('Aliases and results are both required.');
  274. if (!Validate::isValidSearch($search))
  275. $this->errors[] = $search.' '.$this->l('Is not a valid result');
  276. foreach ($aliases as $alias)
  277. if (!Validate::isValidSearch($alias))
  278. $this->errors[] = $alias.' '.$this->l('Is not a valid alias');
  279. if (!count($this->errors))
  280. {
  281. foreach ($aliases as $alias)
  282. {
  283. $obj = new Alias(null, trim($alias), trim($search));
  284. $obj->save();
  285. }
  286. }
  287. if (empty($this->errors))
  288. $this->confirmations[] = $this->l('Creation successful');
  289. }
  290. }