PageRenderTime 28ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/appagebuilder/classes/shortcodes/ApProductList.php

https://gitlab.com/ptisky/API_prestashop
PHP | 428 lines | 397 code | 10 blank | 21 comment | 10 complexity | 39e6ccdb5a04572ff75d2d0d206f066b MD5 | raw file
  1. <?php
  2. /**
  3. * 2007-2015 Apollotheme
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * ApPageBuilder is module help you can build content for your shop
  8. *
  9. * DISCLAIMER
  10. *
  11. * @Module Name: AP Page Builder
  12. * @author Apollotheme <apollotheme@gmail.com>
  13. * @copyright 2007-2015 Apollotheme
  14. * @license http://apollotheme.com - prestashop template provider
  15. */
  16. require_once( _PS_MODULE_DIR_.'appagebuilder/classes/ApPageBuilderProductsModel.php');
  17. class ApProductList extends ApShortCodeBase
  18. {
  19. public $name = 'ApProductList';
  20. public $for_module = 'manage';
  21. public function getInfo()
  22. {
  23. return array('label' => $this->l('Product List'),
  24. 'position' => 7,
  25. 'desc' => $this->l('Create Product List'),
  26. 'icon_class' => 'icon icon-th',
  27. 'tag' => 'content');
  28. }
  29. public function getAdditionConfig()
  30. {
  31. return array(
  32. array(
  33. 'type' => '',
  34. 'name' => 'select_by_categories',
  35. 'default' => '0'
  36. ),
  37. array(
  38. 'type' => '',
  39. 'name' => 'select_by_product_type',
  40. 'default' => '0'
  41. ),
  42. array(
  43. 'type' => '',
  44. 'name' => 'select_by_manufacture',
  45. 'default' => '0'
  46. ),
  47. array(
  48. 'type' => '',
  49. 'name' => 'select_by_supplier',
  50. 'default' => '0'
  51. ),
  52. array(
  53. 'type' => '',
  54. 'name' => 'select_by_product_id',
  55. 'default' => '0'
  56. ),
  57. array(
  58. 'type' => '',
  59. 'name' => 'select_by_tags',
  60. 'default' => '0'
  61. )
  62. );
  63. }
  64. public function getConfigList()
  65. {
  66. $selected_categories = array();
  67. if (Tools::getIsset('categorybox'))
  68. {
  69. $category_box = Tools::getValue('categorybox');
  70. $selected_categories = explode(',', $category_box);
  71. }
  72. //get all manufacture
  73. $manufacturers = Manufacturer::getManufacturers(false, 0, true, false, false, false, true);
  74. $suppliers = Supplier::getSuppliers();
  75. // $product_active = ApPageBuilderProductsModel::getActive();
  76. // $product_class = $product_active['class'];
  77. $profile = new ApPageBuilderProductsModel();
  78. $profile_list = $profile->getAllProductProfileByShop();
  79. //echo '<pre>';print_r($profile_list);die;
  80. array_unshift($profile_list, array('plist_key' => 'default', 'name' => $this->l('Use Default')));
  81. $id_root_category = Context::getContext()->shop->getCategory();
  82. $input = array(
  83. array(
  84. 'type' => 'text',
  85. 'name' => 'title',
  86. 'label' => $this->l('Title'),
  87. 'desc' => $this->l('Auto hide if leave it blank'), 'lang' => 'true',
  88. 'default' => ''
  89. ),
  90. array(
  91. 'type' => 'text',
  92. 'name' => 'class',
  93. 'label' => $this->l('Class'),
  94. 'desc' => $this->l('css class'),
  95. 'default' => ''
  96. ),
  97. array(
  98. 'type' => 'html',
  99. 'name' => 'default_html',
  100. 'html_content' => '<div class="alert alert-info">'.$this->l('Step 1: Product Filter').'</div>',
  101. ),
  102. array(
  103. 'type' => 'checkbox',
  104. 'name' => 'select_by',
  105. 'label' => $this->l('Select By'),
  106. 'class' => 'checkbox-group',
  107. 'desc' => $this->l('Select Product Condition'),
  108. 'values' => array(
  109. 'query' => array(
  110. array(
  111. 'id' => 'categories',
  112. 'name' => $this->l('Categories'),
  113. 'val' => '1'
  114. ),
  115. array(
  116. 'id' => 'product_type',
  117. 'name' => $this->l('Product Type'),
  118. 'val' => '1'
  119. ),
  120. array(
  121. 'id' => 'manufacture',
  122. 'name' => $this->l('Manufacture'),
  123. 'val' => '1'
  124. ),
  125. array(
  126. 'id' => 'supplier',
  127. 'name' => $this->l('Supplier'),
  128. 'val' => '1'
  129. ),
  130. array(
  131. 'id' => 'product_id',
  132. 'name' => $this->l('Product Ids'),
  133. 'val' => '1'
  134. ),
  135. ),
  136. 'id' => 'id',
  137. 'name' => 'name'
  138. )
  139. ),
  140. array(
  141. 'type' => 'categories',
  142. 'label' => $this->l('Select Category'),
  143. 'name' => 'categorybox',
  144. 'desc' => $this->l('You can select one or more, if not select we will not search by category'),
  145. 'tree' => array(
  146. 'root_category' => $id_root_category,
  147. 'use_search' => false,
  148. 'id' => 'categorybox',
  149. 'use_checkbox' => true,
  150. 'selected_categories' => $selected_categories,
  151. ),
  152. 'form_group_class' => 'select_by_categories',
  153. ),
  154. array(
  155. 'type' => 'select',
  156. 'label' => $this->l('Product of Category'),
  157. 'name' => 'category_type',
  158. 'options' => array(
  159. 'query' => array(
  160. array('id' => 'all', 'name' => $this->l('Get All Product of Category')),
  161. array('id' => 'default', 'name' => $this->l('Get Product if category is default category of product'))),
  162. 'id' => 'id',
  163. 'name' => 'name'
  164. ),
  165. 'form_group_class' => 'select_by_categories',
  166. 'default' => 'all'
  167. ),
  168. array(
  169. 'type' => 'html',
  170. 'name' => 'default_html',
  171. 'html_content' => '<div class="sperator"></div>',
  172. 'form_group_class' => 'select_by_categories',
  173. ),
  174. array(
  175. 'type' => 'select',
  176. 'label' => $this->l('Product Type'),
  177. 'name' => 'product_type',
  178. 'options' => array(
  179. 'query' => array(
  180. array(
  181. 'id' => 'all',
  182. 'name' => $this->l('All Product'),
  183. ),
  184. array(
  185. 'id' => 'new_product',
  186. 'name' => $this->l('New Product'),
  187. ),
  188. array(
  189. 'id' => 'best_sellers',
  190. 'name' => $this->l('Best sellers'),
  191. ),
  192. array(
  193. 'id' => 'price_drop',
  194. 'name' => $this->l('Special'),
  195. ),
  196. array(
  197. 'id' => 'home_featured',
  198. 'name' => $this->l('Home Featured'),
  199. )
  200. ),
  201. 'id' => 'id',
  202. 'name' => 'name'
  203. ),
  204. 'form_group_class' => 'select_by_product_type',
  205. 'default' => 'all',
  206. ),
  207. array(
  208. 'type' => 'html',
  209. 'name' => 'default_html',
  210. 'html_content' => '<div class="sperator"></div>',
  211. 'form_group_class' => 'select_by_product_type',
  212. ),
  213. array(
  214. 'type' => 'select',
  215. 'label' => $this->l('Manufacture'),
  216. 'name' => 'manufacture[]',
  217. 'multiple' => true,
  218. 'options' => array(
  219. 'query' => $manufacturers,
  220. 'id' => 'id_manufacturer',
  221. 'name' => 'name'
  222. ),
  223. 'form_group_class' => 'aprow_exceptions',
  224. 'default' => 'all',
  225. 'form_group_class' => 'select_by_manufacture',
  226. ),
  227. array(
  228. 'type' => 'html',
  229. 'name' => 'default_html',
  230. 'html_content' => '<div class="sperator"></div>',
  231. 'form_group_class' => 'select_by_manufacture',
  232. ),
  233. array(
  234. 'type' => 'select',
  235. 'label' => $this->l('Supplier'),
  236. 'name' => 'supplier[]',
  237. 'multiple' => true,
  238. 'options' => array(
  239. 'query' => $suppliers,
  240. 'id' => 'id_supplier',
  241. 'name' => 'name'
  242. ),
  243. 'form_group_class' => 'select_by_supplier',
  244. ),
  245. array(
  246. 'type' => 'html',
  247. 'name' => 'default_html',
  248. 'html_content' => '<div class="sperator"></div>',
  249. 'form_group_class' => 'select_by_supplier',
  250. ),
  251. array(
  252. 'type' => 'text',
  253. 'name' => 'product_id',
  254. 'label' => $this->l('Product Ids'),
  255. 'desc' => $this->l('Show product follow product id. Ex 1 or 1,2,3,4 '),
  256. 'default' => '',
  257. 'form_group_class' => 'select_by_product_id',
  258. ),
  259. array(
  260. 'type' => 'html',
  261. 'name' => 'default_html',
  262. 'html_content' => '<div class="sperator"></div>',
  263. 'form_group_class' => 'select_by_product_id',
  264. ),
  265. array(
  266. 'type' => 'html',
  267. 'name' => 'default_html',
  268. 'html_content' => '<div class="alert alert-info">'.$this->l('Step 2: Product Order And Limit').'</div>',
  269. ),
  270. array(
  271. 'type' => 'select',
  272. 'label' => $this->l('Order Way'),
  273. 'class' => 'form-action',
  274. 'name' => 'order_way',
  275. 'options' => array(
  276. 'query' => array(
  277. array('id' => 'asc', 'name' => $this->l('Asc')),
  278. array('id' => 'desc', 'name' => $this->l('Desc')),
  279. array('id' => 'random', 'name' => $this->l('Random'))),
  280. 'id' => 'id',
  281. 'name' => 'name'
  282. ),
  283. 'default' => 'all'
  284. ),
  285. array(
  286. 'type' => 'select',
  287. 'label' => $this->l('Order By'),
  288. 'name' => 'order_by',
  289. 'options' => array(
  290. 'query' => ApPageSetting::getOrderBy(),
  291. 'id' => 'id',
  292. 'name' => 'name'
  293. ),
  294. 'form_group_class' => 'order_type_sub order_type-asc order_type-desc',
  295. 'default' => 'all'
  296. ),
  297. array(
  298. 'type' => 'select',
  299. 'label' => $this->l('Columns'),
  300. 'name' => 'columns',
  301. 'options' => array('query' => array(
  302. array('id' => '1', 'name' => $this->l('1 Column')),
  303. array('id' => '2', 'name' => $this->l('2 Columns')),
  304. array('id' => '3', 'name' => $this->l('3 Columns')),
  305. array('id' => '4', 'name' => $this->l('4 Columns')),
  306. array('id' => '5', 'name' => $this->l('5 Columns')),
  307. ),
  308. 'id' => 'id',
  309. 'name' => 'name'
  310. ),
  311. 'default' => '4',
  312. ),
  313. array(
  314. 'type' => 'text',
  315. 'name' => 'nb_products',
  316. 'label' => $this->l('Limit'),
  317. 'default' => '10',
  318. ),
  319. //boostrap carousel end
  320. array(
  321. 'type' => 'html',
  322. 'name' => 'default_html',
  323. 'html_content' => '<div class="alert alert-info">'.$this->l('Step 3: Product Template').'</div>',
  324. ),
  325. array(
  326. 'type' => 'select',
  327. 'label' => $this->l('Product Template'),
  328. 'name' => 'profile',
  329. 'options' => array(
  330. 'query' => $profile_list,
  331. 'id' => 'plist_key',
  332. 'name' => 'name'
  333. ),
  334. 'default' => 'all'
  335. ),
  336. array(
  337. 'type' => 'switch',
  338. 'label' => $this->l('Use Show More button'),
  339. 'name' => 'use_showmore',
  340. 'desc' => $this->l('Show button to load more product or hidden this function'),
  341. 'values' => ApPageSetting::returnYesNo(),
  342. 'default' => '1'
  343. )
  344. );
  345. return $input;
  346. }
  347. public function prepareFontContent($assign, $module = null)
  348. {
  349. // validate module
  350. unset($module);
  351. $form_atts = $assign['formAtts'];
  352. $n = (int)isset($form_atts['nb_products']) ? $form_atts['nb_products'] : '10';
  353. $p = (int)Tools::getIsset('p') ? Tools::getValue('p') : '1';
  354. $form_atts['page_number'] = $p;
  355. $form_atts['get_total'] = true;
  356. $module_temp = new APPageBuilder();
  357. $total = $module_temp->getProductsFont($form_atts);
  358. $total = (is_array($total) && count($total) > 0) ? count($total) : 0;
  359. $total_page = (int)$total / $n + ($total % $n == 0 ? 0 : 1);
  360. $is_more = $p < $total_page ? 'more' : '';
  361. $products = array();
  362. if ($p <= $total_page)
  363. {
  364. $form_atts['get_total'] = false;
  365. $products = $module_temp->getProductsFont($form_atts);
  366. }
  367. $assign['profile'] = (isset($form_atts['profile']) && $form_atts['profile'] != 'default') ? $form_atts['profile'] : '';
  368. $assign['scolumn'] = $form_atts['columns'];
  369. $assign['products'] = $products;
  370. $assign['is_more'] = $is_more;
  371. $assign['p'] = $p + 1;
  372. $assign['apPConfig'] = Tools::jsonEncode($form_atts);
  373. $assign['productClassWidget'] = $this->getProductClassByPListKey($assign['profile']);
  374. //auto copy when install
  375. $assign['product_item_path'] = $this->getDirOfFile('sub/product-item/', 'product-item.tpl', 'views/templates/front/product-item/');
  376. return $assign;
  377. }
  378. public function ajaxProcessRender($module)
  379. {
  380. $hooks_tpl_path = _PS_MODULE_DIR_.'appagebuilder/views/templates/hook/';
  381. $assign = array();
  382. $params = array();
  383. $input = Tools::jsonDecode(Tools::getValue('config'));
  384. $n = (int)isset($input->nb_products) ? $input->nb_products : '10';
  385. $p = (int)Tools::getIsset('p') ? Tools::getValue('p') : '1';
  386. $params['select_by_categories'] = isset($input->select_by_categories) ? $input->select_by_categories : '0';
  387. $params['select_by_product_type'] = isset($input->select_by_product_type) ? $input->select_by_product_type : '0';
  388. $params['select_by_manufacture'] = isset($input->select_by_manufacture) ? $input->select_by_manufacture : '0';
  389. $params['select_by_supplier'] = isset($input->select_by_supplier) ? $input->select_by_supplier : '0';
  390. $params['categorybox'] = isset($input->categorybox) ? $input->categorybox : '';
  391. $params['category_type'] = isset($input->category_type) ? $input->category_type : '';
  392. $params['product_type'] = isset($input->product_type) ? $input->product_type : '';
  393. $params['manufacture'] = isset($input->manufacture) ? $input->manufacture : '';
  394. $params['supplier'] = isset($input->supplier) ? $input->supplier : '';
  395. $params['nb_products'] = $n;
  396. $params['page_number'] = $p;
  397. $params['order_by'] = isset($input->order_by) ? $input->order_by : '';
  398. $params['order_way'] = isset($input->order_way) ? $input->order_way : '';
  399. $params['get_total'] = true;
  400. $total = $module->getProductsFont($params);
  401. $total = (is_array($total) && count($total) > 0) ? count($total) : 0;
  402. $total_page = ceil(($total / $n));
  403. $is_more = ($p < $total_page) ? 'more' : '';
  404. $products = array();
  405. if ($p <= $total_page)
  406. {
  407. $params['get_total'] = false;
  408. $products = $module->getProductsFont($params);
  409. }
  410. $assign['profile'] = (isset($input->profile) && $input->profile != 'default') ? $input->profile : '';
  411. $assign['products'] = $products;
  412. $assign['apAjax'] = 1;
  413. $assign['scolumn'] = $input->columns;
  414. $assign['product_item_path'] = $this->getDirOfFile('sub/product-item/', 'product-item.tpl', 'views/templates/front/product-item/');
  415. $html = $module->ajaxProccessFetch($assign, $hooks_tpl_path.'ApProductList.tpl');
  416. return array('html' => $html, 'is_more' => $is_more);
  417. }
  418. }