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

/catalog/model/catalog/filter.php

https://bitbucket.org/monobasic/shop.volero.ch
PHP | 1170 lines | 955 code | 177 blank | 38 comment | 112 complexity | eae50de13f044c204e8854fc46dd9b89 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /*
  3. * simonfilters - 2.4.0 Build 0015
  4. */
  5. class ModelCatalogFilter extends Model {
  6. public static $sent = 0;
  7. public static $data = null;
  8. public static $simonsql = '';
  9. public static $currentfilters=array();
  10. public static $debugmode=false;
  11. public static $language_id=0;
  12. public static $store_id=0;
  13. public static $all_a = array();
  14. public static $all_o = array();
  15. public static $all_m = array();
  16. public function myutf8($value2convert) {
  17. if(function_exists('utf8_strtolower')) {
  18. return utf8_strtolower($value2convert);
  19. }else {
  20. return mb_strtolower($value2convert, 'UTF-8');
  21. }
  22. }
  23. public function getCurrentIDS() {
  24. if (isset($this->request->get['filter_name'])) {
  25. $filter_name = $this->request->get['filter_name'];
  26. } else {
  27. $filter_name = '';
  28. }
  29. if (isset($this->request->get['filter_tag'])) {
  30. $filter_tag = $this->request->get['filter_tag'];
  31. } elseif (isset($this->request->get['filter_name'])) {
  32. $filter_tag = $this->request->get['filter_name'];
  33. } else {
  34. $filter_tag = '';
  35. }
  36. if (isset($this->request->get['filter_description'])) {
  37. $filter_description = $this->request->get['filter_description'];
  38. } else {
  39. $filter_description = '';
  40. }
  41. if (isset($this->request->get['filter_sub_category'])) {
  42. $filter_sub_category = $this->request->get['filter_sub_category'];
  43. } else {
  44. $filter_sub_category = '';
  45. }
  46. if (isset($this->request->get['manufacturer_id'])) {
  47. $manufacturer_id = $this->request->get['manufacturer_id'];
  48. } else {
  49. $manufacturer_id = "";
  50. }
  51. $data = array(
  52. 'filter_name' => $filter_name,
  53. 'filter_tag' => $filter_tag,
  54. 'filter_description' => $filter_description,
  55. 'filter_sub_category' => $filter_sub_category,
  56. 'filter_manufacturer_id' => $manufacturer_id,
  57. );
  58. switch($this->getRoute()) {
  59. case "product/search":
  60. $data['categories'] = array();
  61. $categories_1 = $this->model_catalog_category->getCategories(0);
  62. foreach ($categories_1 as $category_1) {
  63. $level_2_data = array();
  64. $categories_2 = $this->model_catalog_category->getCategories($category_1['category_id']);
  65. foreach ($categories_2 as $category_2) {
  66. $level_3_data = array();
  67. $categories_3 = $this->model_catalog_category->getCategories($category_2['category_id']);
  68. foreach ($categories_3 as $category_3) {
  69. $level_3_data[] = array(
  70. 'category_id' => $category_3['category_id'],
  71. 'name' => $category_3['name'],
  72. );
  73. }
  74. $level_2_data[] = array(
  75. 'category_id' => $category_2['category_id'],
  76. 'name' => $category_2['name'],
  77. 'children' => $level_3_data
  78. );
  79. }
  80. $data['categories'][] = array(
  81. 'category_id' => $category_1['category_id'],
  82. 'name' => $category_1['name'],
  83. 'children' => $level_2_data
  84. );
  85. }
  86. if (isset($this->request->get['filter_category_id'])) {
  87. $filter_category_id = $this->request->get['filter_category_id'];
  88. } else {
  89. $filter_category_id = 0;
  90. }
  91. if (isset($this->request->get['filter_sub_category'])) {
  92. $filter_sub_category = $this->request->get['filter_sub_category'];
  93. } else {
  94. $filter_sub_category = '';
  95. }
  96. break;
  97. default:
  98. if (isset($this->request->get['filter_category_id'])) {
  99. $filter_category_id = $this->request->get['filter_category_id'];
  100. } else {
  101. $filter_category_id = 0;
  102. }
  103. if($filter_category_id==0)$filter_category_id=$this->getCategoryID();
  104. break;
  105. }
  106. $data['filter_category_id'] = $filter_category_id;
  107. foreach($data as $k=>$v) {
  108. if($v=='')unset($data[$k]);
  109. }
  110. if($this->config->get("simonfilters_dynamic")=='1') {
  111. $data['simonfilters']=$this->getSimonAttributesCacheData();
  112. }
  113. $cache = md5(http_build_query($data));
  114. $product_data = $this->cache->get('simonfilters.productids.' . (int)$this->config->get('config_language_id') . '.' . (int)$this->config->get('config_store_id') . $cache);
  115. if (!$product_data) {
  116. $sql = "SELECT p.product_id FROM " . DB_PREFIX . "product p LEFT JOIN " . DB_PREFIX . "product_description pd ON (p.product_id = pd.product_id) LEFT JOIN " . DB_PREFIX . "product_to_store p2s ON (p.product_id = p2s.product_id)";
  117. if (!empty($data['filter_tag'])) {
  118. $sql .= " LEFT JOIN " . DB_PREFIX . "product_tag pt ON (p.product_id = pt.product_id)";
  119. }
  120. if (!empty($data['filter_category_id'])) {
  121. $sql .= " LEFT JOIN " . DB_PREFIX . "product_to_category p2c ON (p.product_id = p2c.product_id)";
  122. }
  123. $sql .= " WHERE pd.language_id = '" . (int)$this->config->get('config_language_id') . "' AND p.status = '1' AND p.date_available <= NOW() AND p2s.store_id = '" . (int)$this->config->get('config_store_id') . "'";
  124. if (!empty($data['filter_name']) || !empty($data['filter_tag'])) {
  125. $sql .= " AND (";
  126. if (!empty($data['filter_name'])) {
  127. $implode = array();
  128. $words = explode(' ', $data['filter_name']);
  129. foreach ($words as $word) {
  130. if (!empty($data['filter_description'])) {
  131. $implode[] = "LCASE(pd.name) LIKE '%" . $this->db->escape($this->myutf8($word)) . "%' OR LCASE(pd.description) LIKE '%" . $this->db->escape($this->myutf8($word)) . "%'";
  132. } else {
  133. $implode[] = "LCASE(pd.name) LIKE '%" . $this->db->escape($this->myutf8($word)) . "%'";
  134. }
  135. }
  136. if ($implode) {
  137. $sql .= " " . implode(" OR ", $implode) . "";
  138. }
  139. }
  140. if (!empty($data['filter_name']) && !empty($data['filter_tag'])) {
  141. $sql .= " OR ";
  142. }
  143. if (!empty($data['filter_tag'])) {
  144. $implode = array();
  145. $words = explode(' ', $data['filter_tag']);
  146. foreach ($words as $word) {
  147. $implode[] = "LCASE(pt.tag) LIKE '%" . $this->db->escape($this->myutf8($data['filter_tag'])) . "%' AND pt.language_id = '" . (int)$this->config->get('config_language_id') . "'";
  148. }
  149. if ($implode) {
  150. $sql .= " " . implode(" OR ", $implode) . "";
  151. }
  152. }
  153. $sql .= ")";
  154. }
  155. if (!empty($data['filter_category_id'])) {
  156. if (!empty($data['filter_sub_category'])) {
  157. $implode_data = array();
  158. $implode_data[] = "p2c.category_id = '" . (int)$data['filter_category_id'] . "'";
  159. $this->load->model('catalog/category');
  160. $categories = $this->model_catalog_category->getCategoriesByParentId($data['filter_category_id']);
  161. foreach ($categories as $category_id) {
  162. $implode_data[] = "p2c.category_id = '" . (int)$category_id . "'";
  163. }
  164. $sql .= " AND (" . implode(' OR ', $implode_data) . ")";
  165. } else {
  166. $sql .= " AND p2c.category_id = '" . (int)$data['filter_category_id'] . "'";
  167. }
  168. }
  169. if (!empty($data['filter_manufacturer_id'])) {
  170. $sql .= " AND p.manufacturer_id = '" . (int)$data['filter_manufacturer_id'] . "'";
  171. }
  172. if($this->config->get("simonfilters_dynamic")=='1') {
  173. $sql .= $this->getSimonAttributes();
  174. }
  175. $sql .= " GROUP BY p.product_id";
  176. $product_data = array();
  177. $query = $this->db->query($sql);
  178. foreach ($query->rows as $result) {
  179. $product_data[] = $result['product_id'];
  180. }
  181. $this->debug(__FILE__.';'.__LINE__,'currentIDS:sql:',$sql);
  182. $this->cache->set('simonfilters.productids.' . (int)$this->config->get('config_language_id') . '.' . (int)$this->config->get('config_store_id') . $cache, $product_data);
  183. }
  184. return $product_data;
  185. }
  186. /*
  187. * constructor
  188. */
  189. public function __construct($registry) {
  190. parent::__construct($registry);
  191. $this->load->model('setting/setting');
  192. $this->setting = $this->model_setting_setting->getSetting('filter');
  193. $this->load->model('localisation/currency');
  194. if($this->sent==0 && $this->isSupportedRoute()) {
  195. $this->language_id = (int)$this->config->get('config_language_id');
  196. $this->store_id = (int)$this->config->get('config_store_id');
  197. $this->all_a = $this->getAllProductAttributes();
  198. $this->all_m = $this->getAllProductManufacturers();
  199. $this->all_o = $this->getAllProductOptions();
  200. $this->all_t = $this->getAllTags();
  201. $this->all_c = $this->getAllCategoriesToFilter();
  202. $index = $this->getSimonIndex();
  203. if(isset($_GET['forcefiltersupdate'])) {
  204. if(isset($_GET['checkedfilters'])) {
  205. $this->currentfilters = $_GET['checkedfilters'];
  206. }else {
  207. $this->currentfilters = array();
  208. }
  209. $_SESSION['simonfilters']['filters'][$index]=$this->currentfilters;
  210. }elseif(isset($_SESSION['simonfilters']['filters'][$index])) {
  211. if($this->config->get('simonfilters_persist')=='1' || isset($_GET['limit']) || isset($_GET['page'])) {
  212. $this->currentfilters = $_SESSION['simonfilters']['filters'][$index];
  213. }else {
  214. $this->currentfilters = array();
  215. }
  216. }else {
  217. $this->currentfilters = array();
  218. }
  219. /*
  220. * Debug stuff
  221. */
  222. if(defined("SIMON_DEBUG") && SIMON_DEBUG=='1')$this->debugmode=true;
  223. if(isset($_GET['debug'])) {
  224. // Comment next line if you don't want me to debug your site!
  225. $_SESSION['simonisdebugging']= ($_GET['debug']=='itsmesimon'?1:0);
  226. }
  227. if(isset($_SESSION['simonisdebugging']) && $_SESSION['simonisdebugging']==1) {
  228. $this->debugmode=true;
  229. }
  230. /*
  231. * Debug stuff
  232. */
  233. $this->data = $this->getFiltersByIds();
  234. }
  235. }
  236. public function manageCache($sql,$name) {
  237. $cache = md5($sql);
  238. $query = $this->cache->get($name. '.' . $cache);
  239. if (!$query) {
  240. $query = $this->db->query($sql);
  241. $this->cache->set($name . $cache, $query);
  242. }
  243. return $query;
  244. }
  245. public function getFiltersByIds() {
  246. $store_id = (int)$this->config->get('config_store_id');
  247. $language_id = $this->config->get('config_language_id');
  248. if($this->config->get("simonfilters_force_all")=='1') {
  249. $sfsids = "";
  250. }else {
  251. $currentIDS = $this->getCurrentIDS();
  252. if(count($currentIDS)==0)$currentIDS[]=0;
  253. $sfsids = join(',', $currentIDS);
  254. }
  255. $data = array();
  256. $activefilter_simonfilters_attribute = $this->config->get("activefilter_simonfilters_attribute");
  257. $activefilter_simonfilters_options = $this->config->get("activefilter_simonfilters_options");
  258. $activefilter_simonfilters_tags = $this->config->get("activefilter_simonfilters_tags");
  259. $activefilter_simonfilters_manufacturer = $this->config->get("activefilter_simonfilters_manufacturer");
  260. $activefilter_simonfilters_stock = $this->config->get("activefilter_simonfilters_stock");
  261. $activefilter_simonfilters_categories = $this->config->get("activefilter_simonfilters_categories");
  262. $price_config_data = $this->config->get("activefilter_simonfilters_price");
  263. $activefilter_simonfilters_price = isset($price_config_data[0]['a']);
  264. $this->debug(__FILE__.';'.__LINE__,'getFiltersByCategory:\$activefilter_simonfilters_attribute',$activefilter_simonfilters_attribute);
  265. $this->debug(__FILE__.';'.__LINE__,'getFiltersByCategory:\$activefilter_simonfilters_options',$activefilter_simonfilters_options);
  266. $this->debug(__FILE__.';'.__LINE__,'getFiltersByCategory:\$activefilter_simonfilters_tags',$activefilter_simonfilters_tags);
  267. $this->debug(__FILE__.';'.__LINE__,'getFiltersByCategory:\$activefilter_simonfilters_manufacturer',$activefilter_simonfilters_manufacturer);
  268. $this->debug(__FILE__.';'.__LINE__,'getFiltersByCategory:\$activefilter_simonfilters_stock',$activefilter_simonfilters_stock);
  269. $this->debug(__FILE__.';'.__LINE__,'getFiltersByCategory:\$activefilter_simonfilters_price',$activefilter_simonfilters_price);
  270. $this->debug(__FILE__.';'.__LINE__,'getFiltersByCategory:\$activefilter_simonfilters_categories',$activefilter_simonfilters_categories);
  271. /*Categories*/
  272. if (count($activefilter_simonfilters_categories)>0) {
  273. $attribute_ids = implode(',',array_keys($activefilter_simonfilters_categories));
  274. if($this->config->get("simonfilters_force_all")=="1") {
  275. $sql_id_products = "";
  276. }else {
  277. $sql_id_products = "AND pdc.product_id IN ($sfsids)";
  278. $sql_id_products = "";
  279. #$sql_id_products = "AND pdc.product_id IN ($sfsids)";
  280. }
  281. $sql = "
  282. SELECT c.category_id, cd.name AS filter_name, 'Categories' AS filter_group_name, 1 AS group_id, '' AS total
  283. FROM
  284. ". DB_PREFIX ."category c
  285. JOIN ". DB_PREFIX ."product_to_category pdc ON c.category_id = pdc.category_id
  286. JOIN ". DB_PREFIX ."category_description cd ON c.category_id = cd.category_id AND cd.language_id=1
  287. WHERE
  288. c.status=1 AND c.parent_id=0 {$sql_id_products}
  289. GROUP BY
  290. c.category_id
  291. ORDER by c.sort_order
  292. ";
  293. $this->debug(__FILE__.';'.__LINE__,'getFiltersByIds:categories sql',$sql);
  294. $query = $this->manageCache($sql,'simonfilters.getCategories'. $language_id . '.' . $store_id);
  295. foreach($query->rows as $row) {
  296. $data['c'][$row['group_id']]['filter_group_name']=$row['filter_group_name'];
  297. $data['c'][$row['group_id']][$row['category_id']]['filter_name'][md5($row['category_id'])]=$row['filter_name'];
  298. }
  299. }
  300. /*Attributes*/
  301. if (count($activefilter_simonfilters_attribute)>0) {
  302. $attribute_ids = implode(',',array_keys($activefilter_simonfilters_attribute));
  303. if($this->config->get("simonfilters_force_all")=="1") {
  304. $sql_id_products = "";
  305. }else {
  306. $sql_id_products = "AND pa.product_id IN ($sfsids)";
  307. }
  308. $sql = "
  309. SELECT
  310. ad.attribute_id, ad.name AS filter_name, agd.name AS filter_group_name, ad.attribute_id as group_id, pa.text, count(pa.product_id) as total
  311. FROM ". DB_PREFIX ."attribute a
  312. JOIN ". DB_PREFIX ."attribute_description ad ON a.attribute_id = ad.attribute_id
  313. JOIN ". DB_PREFIX ."attribute_group_description agd ON agd.attribute_group_id = a.attribute_group_id
  314. JOIN ". DB_PREFIX ."product_attribute pa ON a.attribute_id = pa.attribute_id
  315. WHERE
  316. pa.text<>'' AND a.attribute_id IN($attribute_ids) AND ad.language_id='$language_id' AND agd.language_id = ad.language_id AND pa.language_id = ad.language_id
  317. {$sql_id_products}
  318. GROUP BY a.attribute_id, ad.name, agd.name, agd.attribute_group_id, pa.text
  319. ORDER BY a.sort_order
  320. ";
  321. $this->debug(__FILE__.';'.__LINE__,'getFiltersByIds:attribute sql',$sql);
  322. $query = $this->manageCache($sql,'simonfilters.getAttributesByIds'. $language_id . '.' . $store_id);
  323. $simonfilters_enable_attribute_separator = $this->config->get("simonfilters_enable_attribute_separator");
  324. foreach($query->rows as $row) {
  325. $data['a'][$row['group_id']]['filter_group_name']=$row['filter_name'];
  326. switch($simonfilters_enable_attribute_separator) {
  327. case '1':
  328. $simonfilters_attribute_separator_char = $this->config->get("simonfilters_attribute_separator_char");
  329. if($simonfilters_attribute_separator_char!='') {
  330. foreach(explode($simonfilters_attribute_separator_char,$row['text']) as $a) {
  331. $data['a'][$row['group_id']][$row['attribute_id']]['filter_name'][md5($a)]=$a;
  332. $data['a'][$row['group_id']][$row['attribute_id']]['filter_totals'][md5($a)]=$row['total'];
  333. }
  334. }else {
  335. $data['a'][$row['group_id']][$row['attribute_id']]['filter_name'][md5($row['text'])]=$row['text'];
  336. $data['a'][$row['group_id']][$row['attribute_id']]['filter_totals'][md5($row['text'])]=$row['total'];
  337. }
  338. break;
  339. default:
  340. $data['a'][$row['group_id']][$row['attribute_id']]['filter_name'][md5($row['text'])]=$row['text'];
  341. $data['a'][$row['group_id']][$row['attribute_id']]['filter_totals'][md5($row['text'])]=$row['total'];
  342. break;
  343. }
  344. $data['a'][$row['group_id']]['b']=$activefilter_simonfilters_attribute[$row['attribute_id']]['b'];
  345. }
  346. }
  347. /*Options*/
  348. if (count($activefilter_simonfilters_options)>0) {
  349. $attribute_ids = implode(',',array_keys($activefilter_simonfilters_options));
  350. $filter_group_name = $this->get_filter_group_name($language_id);
  351. #$simonfilters_option_zero_quantity
  352. if($this->config->get("simonfilters_option_zero_quantity")=='1') {
  353. $sql_option_zero_quantity = "AND pov.quantity>0";
  354. }else {
  355. $sql_option_zero_quantity = "";
  356. }
  357. if($this->config->get("simonfilters_force_all")=="1") {
  358. $sql_id_products = "";
  359. }else {
  360. $sql_id_products = "AND pov.product_id IN ($sfsids)";
  361. }
  362. $sql = "
  363. SELECT DISTINCT
  364. pov.option_id as group_id ,pov.option_value_id
  365. FROM
  366. ". DB_PREFIX ."product_option_value pov join `". DB_PREFIX ."option` o ON pov.option_id = o.option_id
  367. WHERE
  368. pov.option_id IN({$attribute_ids}) {$sql_id_products} {$sql_option_zero_quantity}
  369. ORDER BY o.sort_order
  370. ";
  371. $this->debug(__FILE__.';'.__LINE__,'getFiltersByCategory:option sql',$sql);
  372. $query = $this->manageCache($sql,'simonfilters.getOptionsByIds.'. $language_id . '.' . $store_id);
  373. $option_value_id=array();
  374. foreach($query->rows as $row) {
  375. $option_value_id[]=$row['option_value_id'];
  376. }
  377. $totals = $this->get_options_totals($option_value_id);
  378. foreach($query->rows as $row) {
  379. if(isset($row['group_id'])) {
  380. $this_total = isset($totals[$row['option_value_id']])?$totals[$row['option_value_id']]:'';
  381. if(isset($filter_group_name[$row['group_id']][$row['option_value_id']]['filter_name'])) {
  382. $data['o'][$row['group_id']]['filter_group_name']=$filter_group_name[$row['group_id']][$row['option_value_id']]['filter_group_name'];
  383. $data['o'][$row['group_id']][$row['option_value_id']]['filter_name'][md5($row['option_value_id'])]=$filter_group_name[$row['group_id']][$row['option_value_id']]['filter_name'];
  384. $data['o'][$row['group_id']]['b']=$activefilter_simonfilters_options[$row['group_id']]['b'];
  385. }
  386. }
  387. }
  388. if(!function_exists('cmpo')) {
  389. function cmpo($a,$b) {
  390. if($a['filter_group_name']==$b['filter_group_name']) {
  391. return 0;
  392. }
  393. return ($a['filter_group_name'] < $b['filter_group_name']) ? -1 : 1;
  394. }
  395. }
  396. if(isset($data['o']))usort($data['o'],"cmpo");
  397. }
  398. /*Tags*/
  399. if (count($activefilter_simonfilters_tags)>0) {
  400. $attribute_ids = array_keys($activefilter_simonfilters_tags);
  401. foreach($attribute_ids as $ak=>$av)$attribute_ids[$ak]="'". $av ."'";
  402. $attribute_ids = implode(',',$attribute_ids);
  403. $sql = "SELECT * FROM ". DB_PREFIX ."product_tag pt WHERE pt.product_tag_id in($attribute_ids) and pt.language_id={$language_id}";
  404. $query = $this->db->query($sql);
  405. $tags = array();
  406. foreach($query->rows as $row) {
  407. $tags[]= $row['product_tag_id'] ;
  408. }
  409. if($this->config->get("simonfilters_force_all")=="1") {
  410. $sql_id_products = "";
  411. }else {
  412. $sql_id_products = "AND p.product_id IN ($sfsids)";
  413. }
  414. if(count($tags)>0) {
  415. $tagsflat = implode(',',$tags);
  416. $sql = "
  417. SELECT pt.product_tag_id, pt.tag AS filter_name, 'Tags' AS filter_group_name, 1 AS group_id, '' AS total
  418. FROM ". DB_PREFIX ."product_tag pt
  419. JOIN ". DB_PREFIX ."product p ON pt.product_id = p.product_id
  420. JOIN ". DB_PREFIX ."product_to_category pc ON p.product_id = pc.product_id
  421. WHERE p.status=1 AND pt.product_tag_id IN({$tagsflat}) $sql_id_products AND pt.language_id={$language_id}
  422. GROUP BY pt.tag
  423. ";
  424. $this->debug(__FILE__.';'.__LINE__,'getFiltersByIds:tags sql',$sql);
  425. $query = $this->manageCache($sql,'simonfilters.getTagsByIds.'. $language_id . '.' . $store_id);
  426. foreach($query->rows as $row) {
  427. $data['t'][$row['group_id']]['filter_group_name']=$row['filter_group_name'];
  428. $data['t'][$row['group_id']][$row['product_tag_id']]['filter_name'][md5($row['product_tag_id'])]=$row['filter_name'];
  429. }
  430. }
  431. }
  432. /*manufacturers*/
  433. $this->load->language('module/filter');
  434. $manufacturerText = $this->language->get('manufacturer');
  435. if (count($activefilter_simonfilters_manufacturer)>0) {
  436. $attribute_ids = implode(',',array_keys($activefilter_simonfilters_manufacturer));
  437. if($this->config->get("simonfilters_force_all")=="1") {
  438. $sql_id_products = "";
  439. }else {
  440. $sql_id_products = "AND p.product_id IN($sfsids)";
  441. }
  442. $sql = "
  443. select
  444. m.name as filter_name, p.manufacturer_id, '{$manufacturerText}' as filter_group_name, 1 as group_id, '' as total
  445. from
  446. ". DB_PREFIX ."product p
  447. JOIN ". DB_PREFIX ."manufacturer m ON p.manufacturer_id = m.manufacturer_id
  448. WHERE
  449. p.status=1 AND p.manufacturer_id IN($attribute_ids) {$sql_id_products}
  450. GROUP BY manufacturer_id
  451. ORDER BY m.sort_order
  452. ";
  453. $this->debug(__FILE__.';'.__LINE__,'getFiltersByCategory:manufacturers sql',$sql);
  454. $query = $this->manageCache($sql,'simonfilters.getManufacturersByIds.'. $language_id . '.' . $store_id);
  455. foreach($query->rows as $row) {
  456. $data['m'][$row['group_id']]['filter_group_name']=$row['filter_group_name'];
  457. $data['m'][$row['group_id']][$row['manufacturer_id']]['filter_name'][]=$row['filter_name'];
  458. }
  459. }
  460. /*stock*/
  461. $stockText = $this->language->get('stock');
  462. if (count($activefilter_simonfilters_stock)>0) {
  463. $attribute_ids = implode(',',array_keys($activefilter_simonfilters_stock));
  464. if($this->config->get("simonfilters_force_all")=="1") {
  465. $sql_id_products = "";
  466. }else {
  467. $sql_id_products = "AND p.product_id IN({$sfsids})";
  468. }
  469. $sql = "
  470. SELECT ss.name AS filter_name, '{$stockText}' as filter_group_name, ss.stock_status_id, COUNT(p.product_id) AS total, 1 as group_id
  471. FROM ". DB_PREFIX ."product p
  472. JOIN ". DB_PREFIX ."stock_status ss ON p.stock_status_id = ss.stock_status_id
  473. WHERE
  474. p.status=1
  475. AND ss.stock_status_id IN($attribute_ids) {$sql_id_products}
  476. AND ss.language_id='{$language_id}'
  477. GROUP BY ss.name
  478. " ;
  479. $this->debug(__FILE__.';'.__LINE__,'getFiltersByCategory:stocks sql',$sql);
  480. $query = $this->manageCache($sql,'simonfilters.getStockByIds.'. $language_id . '.' . $store_id);
  481. foreach($query->rows as $row) {
  482. $data['s'][$row['group_id']]['filter_group_name']=$row['filter_group_name'];
  483. $data['s'][$row['group_id']][$row['stock_status_id']]['filter_name'][]=$row['filter_name'];
  484. }
  485. }
  486. /*price*/
  487. $priceText = $this->language->get('price');
  488. if ($activefilter_simonfilters_price) {
  489. //Needs more work. p.tax_class_id usage is a bit silly
  490. if($this->config->get("simonfilters_force_all")=="1") {
  491. $sql_id_products = "";
  492. }else {
  493. $sql_id_products = " WHERE p.product_id IN ($sfsids) ";
  494. }
  495. $route = $this->getRoute();
  496. switch($route) {
  497. case "product/category":
  498. $price_filter = "WHERE p.product_id in (select pc.product_id FROM ". DB_PREFIX ."product_to_category pc WHERE pc.category_id='". $this->getCategoryID() ."')";
  499. break;
  500. case "product/manufacturer/product":
  501. $price_filter = "WHERE p.product_id in (select p.product_id FROM ". DB_PREFIX ."product p WHERE p.manufacturer_id='". $this->getManufacturerID() ."')";
  502. break;
  503. case "product/manufacturer":
  504. $price_filter = "WHERE p.product_id in (select p.product_id FROM ". DB_PREFIX ."product p WHERE p.manufacturer_id='". $this->getManufacturerID() ."')";
  505. break;
  506. case "product/search":
  507. $price_filter = "";
  508. break;
  509. default:
  510. $price_filter = "";
  511. break;
  512. }
  513. $sql = "
  514. (
  515. SELECT 'min' AS title, p.price, p.tax_class_id
  516. FROM ". DB_PREFIX ."product p
  517. $price_filter
  518. ORDER BY p.price ASC
  519. LIMIT 1
  520. )UNION(
  521. SELECT 'min_special' AS title, p.price, 0 as tax_class_id
  522. FROM ". DB_PREFIX ."product_special p
  523. $price_filter
  524. ORDER BY p.price,p.priority
  525. LIMIT 1
  526. )UNION(
  527. SELECT 'max' AS title, p.price, p.tax_class_id
  528. FROM ". DB_PREFIX ."product p
  529. $price_filter
  530. ORDER BY p.price DESC
  531. LIMIT 1
  532. )UNION(
  533. SELECT 'max_special' AS title, p.price, 0 as tax_class_id
  534. FROM ". DB_PREFIX ."product_special p
  535. $price_filter
  536. ORDER BY p.price DESC,p.priority
  537. LIMIT 1
  538. )
  539. ";
  540. $this->debug(__FILE__.';'.__LINE__,'getFiltersByCategory:price sql',$sql);
  541. $query = $this->manageCache($sql,'simonfilters.getPriceByIds.'. $language_id . '.' . $store_id);
  542. $rows = $query->rows;
  543. if (count($rows)>1) {
  544. $prices = array();
  545. if(count($rows)==4) {
  546. $prices['minprice']=intval( $this->currency->convert( $rows[0]['price'], $this->config->get('config_currency'), $this->currency->getCode()) );
  547. $prices['minprice_special']=intval( $this->currency->convert( $rows[1]['price'], $this->config->get('config_currency'), $this->currency->getCode()) );
  548. $prices['maxprice']=intval( $this->currency->convert( $rows[2]['price'], $this->config->get('config_currency'), $this->currency->getCode()) );
  549. $prices['maxprice_special']=intval( $this->currency->convert( $rows[3]['price'], $this->config->get('config_currency'), $this->currency->getCode()) );
  550. if(isset($activefilter_simonfilters_price['special'])) {
  551. if($prices['minprice_special']<$prices['minprice'])$prices['minprice']=$prices['minprice_special'];
  552. if($prices['maxprice_special']>$prices['maxprice'])$prices['maxprice']=$prices['maxprice_special'];
  553. }
  554. $tax_min = $rows[0]['tax_class_id'];
  555. $tax_max = $rows[2]['tax_class_id'];
  556. }else {
  557. $prices['minprice']=intval( $this->currency->convert( $rows[0]['price'], $this->config->get('config_currency'), $this->currency->getCode()) );
  558. $prices['maxprice']=intval( $this->currency->convert( $rows[1]['price'], $this->config->get('config_currency'), $this->currency->getCode()) );
  559. $tax_min = $rows[0]['tax_class_id'];
  560. $tax_max = $rows[1]['tax_class_id'];
  561. }
  562. #if($prices['minprice']!=$prices['maxprice']) {
  563. if(isset($activefilter_simonfilters_price['tax'])) {
  564. $prices['minprice'] = floor($this->tax->calculate($prices['minprice'], $tax_min,true));
  565. $prices['maxprice'] = ceil($this->tax->calculate($prices['maxprice'], $tax_max,true));
  566. }
  567. $data['p'][1]['filter_group_name']=$priceText;
  568. $data['p'][1][1]['filter_name'][]='price';
  569. $data['p'][1]['prices']=$prices;
  570. #}
  571. }
  572. }
  573. $this->debug(__FILE__.';'.__LINE__,'getFiltersByCategory:data',$data);
  574. return $data;
  575. }
  576. public function getAllCategories() {
  577. $sql = "SELECT * FROM " . DB_PREFIX . "category c LEFT JOIN " . DB_PREFIX . "category_description cd ON (c.category_id = cd.category_id) LEFT JOIN " . DB_PREFIX . "category_to_store c2s ON (c.category_id = c2s.category_id) WHERE cd.language_id = '" . (int)$this->config->get('config_language_id') . "' AND c2s.store_id = '" . (int)$this->config->get('config_store_id') . "' AND c.status = '1' ORDER BY c.sort_order, LCASE(cd.name)";
  578. $query = $this->db->query($sql);
  579. return $query->rows;
  580. }
  581. public function isThereAnyFilterEnabled() {
  582. return
  583. (count($this->config->get("activefilter_simonfilters_attribute"))>0)
  584. ||(count($this->config->get("activefilter_simonfilters_options"))>0)
  585. ||(count($this->config->get("activefilter_simonfilters_tags"))>0)
  586. ||(count($this->config->get("activefilter_simonfilters_manufacturer"))>0)
  587. ||(count($this->config->get("activefilter_simonfilters_stock"))>0)
  588. ||(count($this->config->get("activefilter_simonfilters_price"))>0);
  589. }
  590. public function debug($data) {
  591. if($this->debugmode) {
  592. echo "<pre class='simondebug'>";
  593. foreach(func_get_args() as $arg) {
  594. echo ( is_array($arg)?print_r($arg,1):"<p>{$arg}</p>" );
  595. }
  596. echo "</pre>";
  597. }
  598. }
  599. public function getHeader($header) {
  600. $temp = 'HTTP_' . strtoupper(str_replace('-', '_', $header));
  601. if (isset($_SERVER[$temp]) && !empty($_SERVER[$temp])) {
  602. return $_SERVER[$temp];
  603. }
  604. if (function_exists('apache_request_headers')) {
  605. $headers = apache_request_headers();
  606. if (!empty($headers[$header])) {
  607. return $headers[$header];
  608. }
  609. }
  610. return false;
  611. }
  612. private function get_options_totals($option_value_id) {
  613. $totals = array();
  614. if(count($option_value_id)>0) {
  615. $store_id = (int)$this->config->get('config_store_id');
  616. $option_value_id_flat = implode(',',$option_value_id) ;
  617. $sql = "
  618. SELECT
  619. pov.option_value_id,
  620. count(pov.product_id) as total
  621. FROM
  622. ". DB_PREFIX ."product_option_value pov
  623. join ". DB_PREFIX ."product p on pov.product_id = p.product_id
  624. where
  625. pov.option_value_id in ({$option_value_id_flat})
  626. AND p.status=1
  627. group by pov.option_value_id
  628. ";
  629. $this->debug(__FILE__.';'.__LINE__,'get_options_totals: sql',$sql);
  630. $cache = md5($sql);
  631. $option_value_id_md5 = str_replace(',','_', $option_value_id);
  632. $query = $this->cache->get('simonfilters.get_options_totals.'. $store_id . '.' . $cache);
  633. if (!$query) {
  634. $query = $this->db->query($sql);
  635. $this->cache->set('simonfilters.get_options_totals.'. $store_id . '.' . $cache, $query);
  636. }
  637. foreach($query->rows as $row) {
  638. $totals[$row['option_value_id']] = $row['total'];
  639. }
  640. }
  641. return $totals;
  642. }
  643. private function get_filter_group_name($language_id) {
  644. $store_id = (int)$this->config->get('config_store_id');
  645. $sql = "
  646. select
  647. od.option_id
  648. ,ovd.option_value_id
  649. ,od.name as filter_group_name
  650. ,ovd.name as filter_name
  651. from
  652. ". DB_PREFIX ."option_description od
  653. join ". DB_PREFIX ."option_value_description ovd on od.option_id = ovd.option_id
  654. where
  655. od.language_id={$language_id}
  656. ";
  657. $this->debug(__FILE__.';'.__LINE__,'get_filter_group_name: sql',$sql);
  658. $cache = md5($sql);
  659. $query = $this->cache->get('simonfilters.get_filter_group_name.'. $language_id . '.' . $store_id . '.' . $cache);
  660. if (!$query) {
  661. $query = $this->db->query($sql);
  662. $this->cache->set('simonfilters.get_filter_group_name.'. $language_id . '.' . $store_id . '.' . $cache, $query);
  663. }
  664. $filter_group_name = array();
  665. foreach($query->rows as $row) {
  666. $filter_group_name[$row['option_id']][$row['option_value_id']]['filter_group_name']=$row['filter_group_name'];
  667. $filter_group_name[$row['option_id']][$row['option_value_id']]['filter_name']=$row['filter_name'];
  668. }
  669. return $filter_group_name;
  670. }
  671. private function getCurrencies() {
  672. $currencies = array();
  673. $results = $this->model_localisation_currency->getCurrencies();
  674. foreach ($results as $result) {
  675. if ($result['status']) {
  676. $currencies[] = array(
  677. 'code' => $result['code']
  678. );
  679. }
  680. }
  681. return $currencies;
  682. }
  683. public function getCategoryID() {
  684. if (isset($this->request->get['path'])) {
  685. $parts = explode('_', (string)$this->request->get['path']);
  686. } else {
  687. $parts = array();
  688. }
  689. if (count($parts) > 0) {
  690. return $parts[count($parts)-1];
  691. } else {
  692. return 0;
  693. }
  694. }
  695. public function getManufacturerID() {
  696. return (isset($this->request->get['manufacturer_id'])?$this->request->get['manufacturer_id']:0);
  697. }
  698. public function getRoute() {
  699. $route = isset($this->request->get['route'])?$this->request->get['route']:'homepage';
  700. switch($route) {
  701. case 'common/home':
  702. $route="homepage";
  703. break;
  704. }
  705. return $route;
  706. }
  707. public function isSupportedRoute() {
  708. return in_array($this->getRoute(),array("homepage","product/category", "product/manufacturer/product", "product/search"));
  709. #return true;
  710. }
  711. public function getSimonIndex() {
  712. $route = $this->getRoute();
  713. switch($route) {
  714. case "product/category":
  715. $index = 'cat_'. $this->getCategoryID();
  716. break;
  717. case "product/manufacturer/product":
  718. $index = 'man_'. $this->getManufacturerID();
  719. break;
  720. case "product/manufacturer":
  721. $index = 'man_index';
  722. break;
  723. default:
  724. $index = 'hp';
  725. break;
  726. }
  727. return $index;
  728. }
  729. /*
  730. *
  731. *
  732. * stuff for catalog\model\catalog\product.php
  733. *
  734. *
  735. *
  736. */
  737. public function getAllProductAttributes() {
  738. $sql = "select attribute_id, text from ". DB_PREFIX."product_attribute where language_id='{$this->language_id}' order by attribute_id";
  739. $query = $this->manageCache($sql,'simonfilters.getAllProductAttributes.'. $this->language_id . '.' . $this->store_id);
  740. $a = array();
  741. foreach ($query->rows as $row) {
  742. $simonfilters_enable_attribute_separator = $this->config->get("simonfilters_enable_attribute_separator");
  743. switch($simonfilters_enable_attribute_separator) {
  744. case '1':
  745. $simonfilters_attribute_separator_char = $this->config->get("simonfilters_attribute_separator_char");
  746. if($simonfilters_attribute_separator_char!='') {
  747. foreach(explode($simonfilters_attribute_separator_char,$row['text']) as $attr_item) {
  748. #if($attr_item!='')$a[$row['attribute_id']][md5($attr_item)]=$attr_item;
  749. $attribute_id = $row['attribute_id'];
  750. $md5_attr_item = md5($attr_item);
  751. if($attr_item!='')$a["a.{$attribute_id}.{$md5_attr_item}"]=$attr_item;
  752. }
  753. }else {
  754. $attribute_id = $row['attribute_id'];
  755. $md5_text = md5($row['text']);
  756. if($row['text']!='')$a["a.{$attribute_id}.{$md5_text}"]=$row['text'];
  757. }
  758. break;
  759. default:
  760. $attribute_id = $row['attribute_id'];
  761. $md5_text = md5($row['text']);
  762. if($row['text']!='')$a["a.{$attribute_id}.{$md5_text}"]=$row['text'];
  763. break;
  764. }
  765. }
  766. return $a;
  767. }
  768. public function getAllProductManufacturers() {
  769. $sql = "select distinct m.manufacturer_id, m.name from ". DB_PREFIX."product p join ". DB_PREFIX."manufacturer m on p.manufacturer_id = p.manufacturer_id";
  770. $query = $this->manageCache($sql,'simonfilters.getAllProductManufacturers.'. $this->language_id . '.' . $this->store_id);
  771. $m = array();
  772. foreach ($query->rows as $row) {
  773. $manufacturer_id = $row['manufacturer_id'];
  774. $md5_name = md5($row['name']);
  775. $m["m.{$manufacturer_id}.{$md5_name}"]=$row['name'];
  776. }
  777. return $m;
  778. }
  779. public function getAllProductOptions() {
  780. $sql = "
  781. SELECT DISTINCT pov.option_value_id, ovd.name
  782. FROM
  783. ". DB_PREFIX."product_option_value pov
  784. JOIN `". DB_PREFIX."option` o ON pov.option_id = o.option_id
  785. JOIN ". DB_PREFIX."option_value_description ovd ON ovd.option_id = pov.option_id
  786. WHERE ovd.language_id='{$this->language_id}'
  787. ";
  788. $query = $this->manageCache($sql,'simonfilters.getAllProductOptions.'. $this->language_id . '.' . $this->store_id);
  789. $o = array();
  790. foreach ($query->rows as $row) {
  791. $option_value_id = $row['option_value_id'];
  792. $md5_name = md5($row['name']);
  793. $o["o.{$option_value_id}.{$md5_name}"]=$row['name'];
  794. }
  795. return $o;
  796. }
  797. public function getAllTags() {
  798. $sql = "select product_tag_id,tag,product_id from ". DB_PREFIX."product_tag where language_id='{$this->language_id}'";
  799. $query = $this->manageCache($sql,'simonfilters.getAllTags.'. $this->language_id . '.' . $this->store_id);
  800. $t = array();
  801. foreach ($query->rows as $row) {
  802. $product_tag_id = $row['product_tag_id'];
  803. $md5_tag = md5($row['tag']);
  804. $t["t.{$md5_tag}"][]=$product_tag_id;
  805. }
  806. return $t;
  807. }
  808. public function getAllCategoriesToFilter() {
  809. $sql = "
  810. select ptc.category_id,ptc.product_id, cd.name
  811. from ". DB_PREFIX."product_to_category ptc
  812. join ". DB_PREFIX."category_description cd ON ptc.category_id = cd.category_id
  813. where cd.language_id='{$this->language_id}'
  814. group by ptc.category_id,ptc.product_id,cd.name
  815. ";
  816. $query = $this->manageCache($sql,'simonfilters.getAllCategories.'. $this->language_id . '.' . $this->store_id);
  817. $c = array();
  818. foreach ($query->rows as $row) {
  819. $category_id = $row['category_id'];
  820. $md5_name = md5($row['name']);
  821. $c["c.{$category_id}.{$md5_name}"]=$row['name'];
  822. }
  823. return $c;
  824. }
  825. public function getSimonAttributesCacheData() {
  826. return (sizeof($this->currentfilters)>0?implode(',',$this->currentfilters):'');
  827. }
  828. public function getSimonAttributes() {
  829. $sqlfilters = "";
  830. $attributeCount = 0;
  831. $manufacturerCount = 0;
  832. $route = $this->getRoute();
  833. $index = $this->getSimonIndex();
  834. $language_id = (int)$this->config->get('config_language_id');
  835. if($this->isSupportedRoute()) {
  836. if($this->sent == 0) {
  837. $this->simonsql = '';
  838. $ari = Array(
  839. 'a' => Array(),
  840. 'o' => Array(),
  841. 'm' => Array(),
  842. 's' => Array(),
  843. 'p' => Array()
  844. );
  845. foreach($this->currentfilters as $checkedfilter) {
  846. $checkedfilterari = preg_split('/\./',$checkedfilter);
  847. switch(count($checkedfilterari)) {
  848. case 3:
  849. list($sfsfilter_type_name, $sfsfilter_id, $sfskey)=$checkedfilterari;
  850. break;
  851. case 4:
  852. #list($sfsfilter_type_name, $sfsfilter_id, $sfskey, $optional_value)=$checkedfilterari;
  853. break;
  854. }
  855. switch($sfsfilter_type_name) {
  856. case 'a':
  857. $a = $this->all_a;
  858. if(array_key_exists("a.{$sfsfilter_id}.{$sfskey}", $a)) {
  859. $text = $a["a.{$sfsfilter_id}.{$sfskey}"];
  860. $ari[$sfsfilter_type_name][$sfsfilter_id][]=" (text LIKE '%{$text}%' AND text REGEXP '[[:<:]]{$text}[[:>:]]')";
  861. }else {
  862. $this->debug(__FILE__.';'.__LINE__,"Error",$sfsfilter_type_name.'.'.$sfsfilter_id.'.'.$sfskey);
  863. }
  864. break;
  865. case 'o':
  866. $o = $this->all_o;
  867. if(array_key_exists("o.{$sfsfilter_id}.{$sfskey}", $o)) {
  868. #$text = $o["o.{$sfsfilter_id}.{$sfskey}"];
  869. $ari[$sfsfilter_type_name][$sfsfilter_id]=1;
  870. }else {
  871. $this->debug(__FILE__.';'.__LINE__,"Error",$sfsfilter_type_name.'.'.$sfsfilter_id.'.'.$sfskey);
  872. }
  873. break;
  874. case 't':
  875. $t = $this->all_t;
  876. if(array_key_exists("t.{$sfskey}", $t)) {
  877. $ari[$sfsfilter_type_name][]=join(',',$t["t.{$sfskey}"]);
  878. }else {
  879. $this->debug(__FILE__.';'.__LINE__,"Error",$sfsfilter_type_name.'.'.$sfsfilter_id.'.'.$sfskey);
  880. }
  881. break;
  882. case 'm':
  883. $m = $this->all_m;
  884. if(array_key_exists("m.{$sfsfilter_id}.{$sfskey}", $m)) {
  885. $text = $m["m.{$sfsfilter_id}.{$sfskey}"];
  886. $ari[$sfsfilter_type_name][]=$sfsfilter_id;
  887. }else {
  888. $this->debug(__FILE__.';'.__LINE__,"Error",$sfsfilter_type_name.'.'.$sfsfilter_id.'.'.$sfskey);
  889. }
  890. break;
  891. case 's1':
  892. if(isset($this->data[$sfsfilter_type_name][$sfsfilter_id]['filter_name'][$sfskey])) {
  893. $text = $this->data[$sfsfilter_type_name][$sfsgroup_id][$sfsfilter_id]['filter_name'][$sfskey];
  894. $ari[$sfsfilter_type_name][]=$sfsfilter_id;
  895. }else {
  896. $this->debug(__FILE__.';'.__LINE__,"Error",$sfsfilter_type_name.'.'.$sfsfilter_id.'.'.$sfskey);
  897. }
  898. break;
  899. case 'p':
  900. $sfskeyAri = preg_split('/,/',$sfskey);
  901. $ari[$sfsfilter_type_name]=$sfskeyAri;
  902. break;
  903. case 'c':
  904. $c = $this->all_c;
  905. if(array_key_exists("c.{$sfsfilter_id}.{$sfskey}", $c)) {
  906. $text = $c["c.{$sfsfilter_id}.{$sfskey}"];
  907. $ari[$sfsfilter_type_name][]=$sfsfilter_id;
  908. }else {
  909. $this->debug(__FILE__.';'.__LINE__,"Error",$sfsfilter_type_name.'.'.$sfsfilter_id.'.'.$sfskey);
  910. }
  911. break;
  912. #var_dump($c);
  913. break;
  914. }
  915. }
  916. foreach($ari as $k => $attrAri) {
  917. switch($k) {
  918. case 'p':
  919. if(count($attrAri)>0) {
  920. $defaultcurrency = $this->config->get('config_currency');
  921. $currentcurrency = $this->currency->getCode();
  922. $minprice_slider = $this->currency->convert( $attrAri[0], $currentcurrency, $defaultcurrency);
  923. $maxprice_slider = $this->currency->convert( $attrAri[1], $currentcurrency, $defaultcurrency);
  924. $activefilter_simonfilters_price = $this->config->get("activefilter_simonfilters_price");
  925. if(isset($activefilter_simonfilters_price['tax']) && isset($this->data['p'])) {
  926. $minpriceclean = $this->data['p']['1']['prices']['minprice'];
  927. $maxpriceclean = $this->data['p']['1']['prices']['maxprice'];
  928. $minpricecleantax = $this->tax->calculate($minpriceclean, 9,true);
  929. $maxpricecleantax = $this->tax->calculate($maxpriceclean, 9,true);
  930. $this->debug(__FILE__.';'.__LINE__,"minprice_slider",$minprice_slider, "maxprice_slider", $maxprice_slider);
  931. $minprice_slider = floor($minprice_slider * $minpriceclean / $minpricecleantax);
  932. $maxprice_slider = ceil( $maxprice_slider * $maxpriceclean / $maxpriceclean

Large files files are truncated, but you can click here to view the full file