choiceai - Version 1.0.10

Version Notes

Initial Code

Download this release

Release Info

Developer MineWhat Inc.
Extension choiceai
Version 1.0.10
Comparing to
See all releases


Code changes from version 1.0.9 to 1.0.10

Files changed (62) hide show
  1. app/code/community/ChoiceAI/Personalisation/Block/Event/Analytics/Listpage.php +26 -0
  2. app/code/community/ChoiceAI/Personalisation/Block/Event/Analytics/Search.php +26 -0
  3. app/code/community/ChoiceAI/Personalisation/Block/Searchpage/Addparam.php +20 -0
  4. app/code/community/ChoiceAI/Personalisation/Model/Observer.php +0 -2
  5. app/code/community/ChoiceAI/Personalisation/controllers/ApiController.php +503 -43
  6. app/code/local/ChoiceAI/Search/Block/Catalog/Layer/View.php +5 -16
  7. app/code/local/ChoiceAI/Search/Block/Catalog/Product/List/Toolbar.php +3 -3
  8. app/code/local/ChoiceAI/Search/Block/Catalogsearch/Layer.php +2 -2
  9. app/code/local/ChoiceAI/Search/Block/Catalogsearch/Result.php +1 -1
  10. app/code/local/ChoiceAI/Search/Helper/Catalogsearch.php +0 -19
  11. app/code/local/ChoiceAI/Search/Helper/Choiceaisearch.php +0 -1
  12. app/code/local/ChoiceAI/Search/Helper/Data.php +72 -317
  13. app/code/local/ChoiceAI/Search/Helper/Productimg.php +43 -0
  14. app/code/local/ChoiceAI/Search/Model/Catalog/Category.php +1 -2
  15. app/code/local/ChoiceAI/Search/Model/Catalog/Config.php +13 -12
  16. app/code/local/ChoiceAI/Search/Model/Catalog/Product/Url.php +73 -0
  17. app/code/local/ChoiceAI/Search/Model/Resource/Catalog/Product/Collection.php +16 -11
  18. app/code/local/ChoiceAI/Search/Model/Resource/Engine/Abstract.php +5 -294
  19. app/code/local/ChoiceAI/Search/Model/Resource/Engine/Choiceaisearch.php +94 -64
  20. app/code/local/ChoiceAI/Search/Model/Resource/Engine/Choiceaisearch/Client.php +1 -3
  21. app/code/local/ChoiceAI/Search/etc/config.xml +5 -47
  22. app/code/local/ChoiceAI/Searchcore/Helper/Confighelper.php +0 -472
  23. app/code/local/ChoiceAI/Searchcore/Helper/Constants.php +1 -1
  24. app/code/local/ChoiceAI/Searchcore/Helper/Data.php +16 -162
  25. app/code/local/ChoiceAI/Searchcore/Helper/Feedhelper.php +0 -288
  26. app/code/local/ChoiceAI/Searchcore/Model/Api/Request.php +0 -212
  27. app/code/local/ChoiceAI/Searchcore/Model/Api/Response.php +0 -165
  28. app/code/local/ChoiceAI/Searchcore/Model/Api/Task.php +0 -84
  29. app/code/local/ChoiceAI/Searchcore/Model/Api/Task/Analyticsimpression.php +0 -49
  30. app/code/local/ChoiceAI/Searchcore/Model/Api/Task/Autosuggestindex.php +0 -52
  31. app/code/local/ChoiceAI/Searchcore/Model/Api/Task/Feeddetails.php +0 -71
  32. app/code/local/ChoiceAI/Searchcore/Model/Api/Task/Searchimpression.php +0 -57
  33. app/code/local/ChoiceAI/Searchcore/Model/Api/Task/Searchsetup.php +0 -52
  34. app/code/local/ChoiceAI/Searchcore/Model/Api/Task/Supportmail.php +0 -48
  35. app/code/local/ChoiceAI/Searchcore/Model/Api/Task/Trackcart.php +0 -46
  36. app/code/local/ChoiceAI/Searchcore/Model/Api/Task/Trackorder.php +0 -46
  37. app/code/local/ChoiceAI/Searchcore/Model/Api/Task/Triggerfeedupload.php +0 -42
  38. app/code/local/ChoiceAI/Searchcore/Model/Api/Task/Updatefeaturefields.php +0 -83
  39. app/code/local/ChoiceAI/Searchcore/Model/Api/Task/Validatekeys.php +0 -73
  40. app/code/local/ChoiceAI/Searchcore/Model/Config.php +0 -47
  41. app/code/local/ChoiceAI/Searchcore/Model/Field.php +0 -342
  42. app/code/local/ChoiceAI/Searchcore/Model/Observer.php +0 -201
  43. app/code/local/ChoiceAI/Searchcore/Model/Resource/Attribute.php +0 -27
  44. app/code/local/ChoiceAI/Searchcore/Model/Resource/Config.php +0 -213
  45. app/code/local/ChoiceAI/Searchcore/Model/Resource/Config/Collection.php +0 -21
  46. app/code/local/ChoiceAI/Searchcore/Model/Resource/Field.php +0 -146
  47. app/code/local/ChoiceAI/Searchcore/Model/Resource/Field/Collection.php +0 -66
  48. app/code/local/ChoiceAI/Searchcore/Model/Resource/Product/Collection.php +0 -110
  49. app/code/local/ChoiceAI/Searchcore/etc/config.xml +0 -114
  50. app/code/local/ChoiceAI/Searchcore/sql/choiceai_searchcore_setup/mysql4-install-1.0.0.php +0 -91
  51. app/code/local/ChoiceAI/Searchcore/sql/choiceai_searchcore_setup/upgrade-1.0.21-1.0.22.php +0 -46
  52. app/design/frontend/base/default/layout/choiceai_personalisation.xml +21 -0
  53. app/design/frontend/base/default/template/choiceai/personalisation/analytics/listpage.phtml +25 -0
  54. app/design/frontend/base/default/template/choiceai/personalisation/analytics/search.phtml +25 -0
  55. app/design/frontend/base/default/template/choiceai/personalisation/base/addparam.phtml +34 -0
  56. lib/ChoiceAI/Client.php +82 -20
  57. lib/ChoiceAI/Response.php +0 -45
  58. lib/ChoiceAI/Result.php +4 -4
  59. lib/ChoiceAI/ResultSet.php +30 -26
  60. lib/ChoiceAI/Service.php +139 -115
  61. lib/ChoiceAI/test.php +0 -53
  62. package.xml +4 -4
app/code/community/ChoiceAI/Personalisation/Block/Event/Analytics/Listpage.php ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Created by PhpStorm.
4
+ * User: harkirat
5
+ * Date: 1/7/17
6
+ * Time: 3:27 PM
7
+ */
8
+
9
+ /**
10
+ * @category ChoiceAI
11
+ * @package ChoiceAI_Personalisation
12
+ * @copyright Copyright (c) MineWhat
13
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
14
+ */
15
+ class ChoiceAI_Personalisation_Block_Event_Analytics_Listpage extends Mage_Core_Block_Template {
16
+
17
+ protected function _construct() {
18
+ parent::_construct();
19
+ $this->setTemplate('choiceai/personalisation/analytics/listpage.phtml');
20
+ }
21
+
22
+ public function engineStatus(){
23
+ return Mage::helper('choiceai_search')->isActiveEngine();
24
+ }
25
+
26
+ }
app/code/community/ChoiceAI/Personalisation/Block/Event/Analytics/Search.php ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Created by PhpStorm.
4
+ * User: harkirat
5
+ * Date: 1/7/17
6
+ * Time: 3:37 PM
7
+ */
8
+
9
+ /**
10
+ * @category ChoiceAI
11
+ * @package ChoiceAI_Personalisation
12
+ * @copyright Copyright (c) MineWhat
13
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
14
+ */
15
+ class ChoiceAI_Personalisation_Block_Event_Analytics_Search extends Mage_Core_Block_Template {
16
+
17
+ protected function _construct() {
18
+ parent::_construct();
19
+ $this->setTemplate('choiceai/personalisation/analytics/search.phtml');
20
+ }
21
+
22
+ public function engineStatus(){
23
+ return Mage::helper('choiceai_search')->isActiveEngine();
24
+ }
25
+
26
+ }
app/code/community/ChoiceAI/Personalisation/Block/Searchpage/Addparam.php ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ /**
4
+ * @category ChoiceAI
5
+ * @package ChoiceAI_Personalisation
6
+ * @copyright Copyright (c) MineWhat
7
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
8
+ */
9
+ class ChoiceAI_Personalisation_Block_Searchpage_Addparam extends Mage_Core_Block_Template {
10
+
11
+ protected function _construct() {
12
+ parent::_construct();
13
+ $this->setTemplate('choiceai/personalisation/base/addparam.phtml');
14
+ }
15
+
16
+ public function engineStatus(){
17
+ return Mage::helper('choiceai_search')->isActiveEngine();
18
+ }
19
+
20
+ }
app/code/community/ChoiceAI/Personalisation/Model/Observer.php CHANGED
@@ -67,9 +67,7 @@ class ChoiceAI_Personalisation_Model_Observer {
67
  ->saveRelations();
68
 
69
  } catch (Exception $e) {
70
-
71
  $error_message = $e->getMessage();
72
-
73
  }
74
 
75
  file_get_contents("https://app.choice.ai/stats/magentoinstall?enabled=".$enabled."&api_key=".$api_key."&store_url=".$store_url."&magentoversion=".$magento_version."&error=".$error_message);
67
  ->saveRelations();
68
 
69
  } catch (Exception $e) {
 
70
  $error_message = $e->getMessage();
 
71
  }
72
 
73
  file_get_contents("https://app.choice.ai/stats/magentoinstall?enabled=".$enabled."&api_key=".$api_key."&store_url=".$store_url."&magentoversion=".$magento_version."&error=".$error_message);
app/code/community/ChoiceAI/Personalisation/controllers/ApiController.php CHANGED
@@ -58,12 +58,10 @@ class ChoiceAI_Personalisation_ApiController extends Mage_Core_Controller_Front_
58
  }
59
 
60
  public function configAction() {
61
-
62
  try {
63
-
64
- if(!$this->_authorise()) {
65
- return $this;
66
- }
67
 
68
  $responseObj = array();
69
 
@@ -103,14 +101,11 @@ class ChoiceAI_Personalisation_ApiController extends Mage_Core_Controller_Front_
103
  ->setHeader('Content-type', 'application/json', true);
104
  }
105
 
106
- return this;
107
-
108
  }
109
 
110
  public function ordersAction() {
111
-
112
  try {
113
-
114
  if(!$this->_authorise()) {
115
  return $this;
116
  }
@@ -196,9 +191,7 @@ class ChoiceAI_Personalisation_ApiController extends Mage_Core_Controller_Front_
196
  ->setBody(json_encode(array('orders' => $orders, 'fromDate' => $fromDate, 'toDate' => $toDate, 'version' => self::API_VERSION)))
197
  ->setHttpResponseCode(200)
198
  ->setHeader('Content-type', 'application/json', true);
199
-
200
  }
201
-
202
  } catch(Exception $e) {
203
  $this->getResponse()
204
  ->setBody(json_encode(array('status' => 'error', 'message' => 'Internal server error', 'version' => self::API_VERSION)))
@@ -206,14 +199,11 @@ class ChoiceAI_Personalisation_ApiController extends Mage_Core_Controller_Front_
206
  ->setHeader('Content-type', 'application/json', true);
207
  }
208
 
209
- return this;
210
-
211
  }
212
 
213
  public function productattributesAction() {
214
-
215
  try {
216
-
217
  if(!$this->_authorise()) {
218
  return $this;
219
  }
@@ -221,9 +211,8 @@ class ChoiceAI_Personalisation_ApiController extends Mage_Core_Controller_Front_
221
  $sections = explode('/', trim($this->getRequest()->getPathInfo(), '/'));
222
 
223
  if(!isset($sections[3])) {
224
-
225
  throw new Exception();
226
-
227
  }
228
 
229
  $productId = $sections[3];
@@ -289,31 +278,24 @@ class ChoiceAI_Personalisation_ApiController extends Mage_Core_Controller_Front_
289
  }
290
 
291
  $variants[] = $variant;
292
-
293
  }
294
-
295
  }
296
 
297
  $this->getResponse()
298
  ->setBody(json_encode(array('product' => $product_info, 'variants' => $variants, 'options' => $options, 'version' => self::API_VERSION)))
299
  ->setHttpResponseCode(200)
300
  ->setHeader('Content-type', 'application/json', true);
301
-
302
  } catch(Exception $e) {
303
  $this->getResponse()
304
  ->setBody(json_encode(array('status' => 'error', 'message' => 'Internal server error', 'version' => self::API_VERSION)))
305
  ->setHttpResponseCode(500)
306
  ->setHeader('Content-type', 'application/json', true);
307
  }
308
-
309
  return $this;
310
-
311
  }
312
 
313
  public function productsAction() {
314
-
315
  try {
316
-
317
  if(!$this->_authorise()) {
318
  return $this;
319
  }
@@ -352,6 +334,8 @@ class ChoiceAI_Personalisation_ApiController extends Mage_Core_Controller_Front_
352
  $product = $this->getFormattedProduct($product, $extras, $debug);
353
  if($product !== null) {
354
  $products[] = $product;
 
 
355
  }
356
 
357
  } else {
@@ -360,8 +344,11 @@ class ChoiceAI_Personalisation_ApiController extends Mage_Core_Controller_Front_
360
  $offset = $this->getRequest()->getParam('offset', 1);
361
 
362
  $productsCollection = Mage::getModel('catalog/product')->getCollection();
 
 
363
  $productsCollection
364
  ->addAttributeToSelect($attributes)
 
365
  ->getSelect()->limit($limit, $offset) //we can specify how many products we want to show on this page
366
  ;
367
 
@@ -369,6 +356,8 @@ class ChoiceAI_Personalisation_ApiController extends Mage_Core_Controller_Front_
369
  $product = $this->getFormattedProduct($product, $extras, $debug);
370
  if($product !== null) {
371
  $products[] = $product;
 
 
372
  }
373
  }
374
 
@@ -390,14 +379,11 @@ class ChoiceAI_Personalisation_ApiController extends Mage_Core_Controller_Front_
390
  }
391
 
392
  return $this;
393
-
394
  }
395
 
396
 
397
  public function categoriesAction() {
398
-
399
  try {
400
-
401
  if(!$this->_authorise()) {
402
  return $this;
403
  }
@@ -408,6 +394,8 @@ class ChoiceAI_Personalisation_ApiController extends Mage_Core_Controller_Front_
408
  'image',
409
  'url',
410
  'level',
 
 
411
  'is_active',
412
  'created_at',
413
  'updated_at'
@@ -418,6 +406,7 @@ class ChoiceAI_Personalisation_ApiController extends Mage_Core_Controller_Front_
418
 
419
  $level = $this->getRequest()->getParam('level');
420
  $active = $this->getRequest()->getParam('active', 'false') === 'true';
 
421
 
422
  if($level && strlen($level)) {
423
  $level = intval($level);
@@ -432,8 +421,10 @@ class ChoiceAI_Personalisation_ApiController extends Mage_Core_Controller_Front_
432
  $category = Mage::getModel('catalog/category')->load($categoryId);
433
 
434
  $category = $this->getFormattedCategory($category);
435
- if($category !== null) {
436
  $categories[] = $category;
 
 
437
  }
438
 
439
  } else {
@@ -455,6 +446,12 @@ class ChoiceAI_Personalisation_ApiController extends Mage_Core_Controller_Front_
455
  ;
456
  }
457
 
 
 
 
 
 
 
458
  $categoriesCollection
459
  ->addAttributeToSelect($attributes)
460
  ->getSelect()->limit($limit, $offset) //we can specify how many categories we want to show on this page
@@ -462,7 +459,7 @@ class ChoiceAI_Personalisation_ApiController extends Mage_Core_Controller_Front_
462
 
463
  foreach($categoriesCollection as $category) {
464
  $category = $this->getFormattedCategory($category);
465
- if($category !== null) {
466
  $categories[] = $category;
467
  }
468
  }
@@ -474,7 +471,109 @@ class ChoiceAI_Personalisation_ApiController extends Mage_Core_Controller_Front_
474
  ->setHttpResponseCode(200)
475
  ->setHeader('Content-type', 'application/json', true);
476
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
477
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
478
  } catch(Exception $e) {
479
  $this->getResponse()
480
  ->setBody(json_encode(array('status' => 'error', 'message' => 'Internal server error', 'version' => self::API_VERSION)))
@@ -483,9 +582,183 @@ class ChoiceAI_Personalisation_ApiController extends Mage_Core_Controller_Front_
483
  }
484
 
485
  return $this;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
486
 
 
487
  }
488
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
489
  public function usersAction() {
490
 
491
  try {
@@ -664,11 +937,19 @@ class ChoiceAI_Personalisation_ApiController extends Mage_Core_Controller_Front_
664
  if($debug) {
665
  $attributes = $product->getAttributes();
666
  foreach($attributes as $key => $value) {
667
- $formattedProduct['extras'][$key] = $product->getAttributeText($key);
 
 
 
 
668
  }
669
  } else {
670
  foreach($extras as $key) {
671
- $formattedProduct['extras'][$key] = $product->getAttributeText($key);
 
 
 
 
672
  }
673
  }
674
 
@@ -686,10 +967,10 @@ class ChoiceAI_Personalisation_ApiController extends Mage_Core_Controller_Front_
686
  $formattedProduct['cat'][$formattedCategory['id']] = $formattedCategory;
687
  }
688
 
689
- } catch(Exception $e) {}
690
-
691
- return $formattedProduct;
692
-
693
  }
694
 
695
  private function getFormattedCategory($category) {
@@ -697,31 +978,30 @@ class ChoiceAI_Personalisation_ApiController extends Mage_Core_Controller_Front_
697
  $formattedCategory = null;
698
 
699
  try {
700
-
701
  $formattedCategory = array(
702
  'id' => $category->getId(),
703
  'name' => $category->getName(),
704
  'image' => $category->getImageUrl(),
705
  'url' => $category->getUrl(),
706
  'level' => $category->getLevel(),
 
 
707
  'is_active' => $category->getIsActive(),
708
- 'created_at' => $category->getCreatedAt(),
709
- 'updated_at' => $category->getUpdatedAt()
710
  );
711
 
712
  } catch(Exception $e) {}
713
 
714
  return $formattedCategory;
715
-
716
  }
717
 
718
  public function sortbyAction() {
719
 
720
  try {
721
-
722
- if(!$this->_authorise()) {
723
- return $this;
724
- }
725
 
726
  $sortByOptions = array();
727
  // $attributesData = Mage::getResourceModel('catalog/config')->getAttributesUsedForSortBy();
@@ -767,4 +1047,184 @@ class ChoiceAI_Personalisation_ApiController extends Mage_Core_Controller_Front_
767
  return $this;
768
  }
769
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
770
  }
58
  }
59
 
60
  public function configAction() {
 
61
  try {
62
+ if(!$this->_authorise()) {
63
+ return $this;
64
+ }
 
65
 
66
  $responseObj = array();
67
 
101
  ->setHeader('Content-type', 'application/json', true);
102
  }
103
 
104
+ return $this;
 
105
  }
106
 
107
  public function ordersAction() {
 
108
  try {
 
109
  if(!$this->_authorise()) {
110
  return $this;
111
  }
191
  ->setBody(json_encode(array('orders' => $orders, 'fromDate' => $fromDate, 'toDate' => $toDate, 'version' => self::API_VERSION)))
192
  ->setHttpResponseCode(200)
193
  ->setHeader('Content-type', 'application/json', true);
 
194
  }
 
195
  } catch(Exception $e) {
196
  $this->getResponse()
197
  ->setBody(json_encode(array('status' => 'error', 'message' => 'Internal server error', 'version' => self::API_VERSION)))
199
  ->setHeader('Content-type', 'application/json', true);
200
  }
201
 
202
+ return $this;
 
203
  }
204
 
205
  public function productattributesAction() {
 
206
  try {
 
207
  if(!$this->_authorise()) {
208
  return $this;
209
  }
211
  $sections = explode('/', trim($this->getRequest()->getPathInfo(), '/'));
212
 
213
  if(!isset($sections[3])) {
214
+ // product id
215
  throw new Exception();
 
216
  }
217
 
218
  $productId = $sections[3];
278
  }
279
 
280
  $variants[] = $variant;
 
281
  }
 
282
  }
283
 
284
  $this->getResponse()
285
  ->setBody(json_encode(array('product' => $product_info, 'variants' => $variants, 'options' => $options, 'version' => self::API_VERSION)))
286
  ->setHttpResponseCode(200)
287
  ->setHeader('Content-type', 'application/json', true);
 
288
  } catch(Exception $e) {
289
  $this->getResponse()
290
  ->setBody(json_encode(array('status' => 'error', 'message' => 'Internal server error', 'version' => self::API_VERSION)))
291
  ->setHttpResponseCode(500)
292
  ->setHeader('Content-type', 'application/json', true);
293
  }
 
294
  return $this;
 
295
  }
296
 
297
  public function productsAction() {
 
298
  try {
 
299
  if(!$this->_authorise()) {
300
  return $this;
301
  }
334
  $product = $this->getFormattedProduct($product, $extras, $debug);
335
  if($product !== null) {
336
  $products[] = $product;
337
+ }else{
338
+ throw new Exception("Failed to fetch the product");
339
  }
340
 
341
  } else {
344
  $offset = $this->getRequest()->getParam('offset', 1);
345
 
346
  $productsCollection = Mage::getModel('catalog/product')->getCollection();
347
+
348
+ // Get only enabled products
349
  $productsCollection
350
  ->addAttributeToSelect($attributes)
351
+ ->addAttributeToFilter('status', array('eq' => Mage_Catalog_Model_Product_Status::STATUS_ENABLED))
352
  ->getSelect()->limit($limit, $offset) //we can specify how many products we want to show on this page
353
  ;
354
 
356
  $product = $this->getFormattedProduct($product, $extras, $debug);
357
  if($product !== null) {
358
  $products[] = $product;
359
+ }else{
360
+ throw new Exception("Failed to fetch the product");
361
  }
362
  }
363
 
379
  }
380
 
381
  return $this;
 
382
  }
383
 
384
 
385
  public function categoriesAction() {
 
386
  try {
 
387
  if(!$this->_authorise()) {
388
  return $this;
389
  }
394
  'image',
395
  'url',
396
  'level',
397
+ 'path',
398
+ 'is_anchor',
399
  'is_active',
400
  'created_at',
401
  'updated_at'
406
 
407
  $level = $this->getRequest()->getParam('level');
408
  $active = $this->getRequest()->getParam('active', 'false') === 'true';
409
+ $isAnchor = $this->getRequest()->getParam('is_anchor', 'false') === 'true';
410
 
411
  if($level && strlen($level)) {
412
  $level = intval($level);
421
  $category = Mage::getModel('catalog/category')->load($categoryId);
422
 
423
  $category = $this->getFormattedCategory($category);
424
+ if($category !== null && $category->id != null && is_array($category)) {
425
  $categories[] = $category;
426
+ } else{
427
+ throw new Exception("Category ".$categoryId." not found");
428
  }
429
 
430
  } else {
446
  ;
447
  }
448
 
449
+ if($isAnchor != null) {
450
+ $categoriesCollection
451
+ ->addAttributeToFilter('is_anchor', 1) // If you want categories which are shown in search facets
452
+ ;
453
+ }
454
+
455
  $categoriesCollection
456
  ->addAttributeToSelect($attributes)
457
  ->getSelect()->limit($limit, $offset) //we can specify how many categories we want to show on this page
459
 
460
  foreach($categoriesCollection as $category) {
461
  $category = $this->getFormattedCategory($category);
462
+ if($category !== null && is_array($category)) {
463
  $categories[] = $category;
464
  }
465
  }
471
  ->setHttpResponseCode(200)
472
  ->setHeader('Content-type', 'application/json', true);
473
 
474
+ } catch(\Exception $e) {
475
+ if($e->getMessage() != null)
476
+ $message = $e->getMessage();
477
+ else
478
+ $message = 'Internal server error';
479
+
480
+ $this->getResponse()
481
+ ->setBody(json_encode(array('status' => 'error', 'message' => $message, 'version' => self::API_VERSION)))
482
+ ->setHttpResponseCode(500)
483
+ ->setHeader('Content-type', 'application/json', true);
484
+ }
485
+
486
+ return $this;
487
+ }
488
+
489
+
490
+ public function facetattributesAction() {
491
+ try {
492
+ if(!$this->_authorise()) {
493
+ return $this;
494
+ }
495
+
496
+ $limit = $this->getRequest()->getParam('limit', 100);
497
+ $page = $this->getRequest()->getParam('page', 1);
498
+ $fields = $this->getRequest()->getParam('fields', false);
499
+ $isFilterable = $this->getRequest()->getParam('is_filterable', false);
500
+
501
+ $collection = Mage::getResourceModel('catalog/product_attribute_collection');
502
+
503
+ if($isFilterable && is_numeric($isFilterable)){
504
+ $collection->addFieldToFilter('is_filterable', (int) $isFilterable);
505
+ } else{
506
+ // Give only is filterable with results
507
+ $collection->addFieldToFilter('is_filterable', 1);
508
+ }
509
 
510
+ // $fieldsToSelect = array(
511
+ // "attribute_id",
512
+ // "is_filterable",
513
+ // "attribute_code",
514
+ // "frontend_label",
515
+ // "is_visible",
516
+ // "is_visible_on_front",
517
+ // "is_user_defined",
518
+ // "is_required",
519
+ // "is_searchable",
520
+ // "is_filterable_in_search",
521
+ // "position",
522
+ // "is_used_for_promo_rules",
523
+ // "is_used_for_price_rules",
524
+ // "used_in_product_listing"
525
+ // );
526
+ // //@TODO: Mysql error coming up
527
+ // $collection->addFieldToSelect($fieldsToSelect);
528
+
529
+ $collection->setOrder('position', 'ASC');
530
+ $collection->setPageSize((int) $limit);
531
+ $collection->setCurPage((int) $page);
532
+ $collection->load();
533
+
534
+ if($limit * ($page-1) < $collection->getSize()) {
535
+ $allAttrs = array();
536
+
537
+ foreach ($collection as $attr) {
538
+ $newAttr = array();
539
+ if($fields=="all") {
540
+ $newAttr['attribute_id'] = $attr->getAttributeId();
541
+ $newAttr['is_filterable'] = $attr->getIsFilterable();
542
+ $newAttr['attribute_code'] = $attr->getAttributeCode();
543
+ $newAttr['frontend_label'] = $attr->getFrontendLabel();
544
+ $newAttr['is_visible'] = $attr->getIsVisible();
545
+ $newAttr['is_visible_on_front'] = $attr->getIsVisibleOnFront();
546
+ $newAttr['is_user_defined'] = $attr->getIsUserDefined();
547
+ $newAttr['is_required'] = $attr->getIsRequired();
548
+ $newAttr['is_searchable'] = $attr->getIsSearchable();
549
+ $newAttr['is_filterable_in_search'] = $attr->getIsFilterableInSearch();
550
+ $newAttr['position'] = $attr->getPosition();
551
+ $newAttr['is_used_for_promo_rules'] = $attr->getIsUsedForPromoRules();
552
+ $newAttr['is_used_for_price_rules'] = $attr->getIsUsedForPriceRules();
553
+ $newAttr['used_in_product_listing'] = $attr->getUsedInProductListing();
554
+ } else{
555
+ $newAttr['attribute_id'] = $attr->getAttributeId();
556
+ $newAttr['is_filterable'] = $attr->getIsFilterable();
557
+ $newAttr['attribute_code'] = $attr->getAttributeCode();
558
+ $newAttr['frontend_label'] = $attr->getFrontendLabel();
559
+ $newAttr['is_visible'] = $attr->getIsVisible();
560
+ $newAttr['is_visible_on_front'] = $attr->getIsVisibleOnFront();
561
+ $newAttr['is_searchable'] = $attr->getIsSearchable();
562
+ $newAttr['is_filterable_in_search'] = $attr->getIsFilterableInSearch();
563
+ $newAttr['position'] = $attr->getPosition();
564
+ $newAttr['used_in_product_listing'] = $attr->getUsedInProductListing();
565
+ }
566
+
567
+ $allAttrs[] = $newAttr;
568
+ }
569
+ } else{
570
+ $allAttrs = array();
571
+ }
572
+
573
+ $this->getResponse()
574
+ ->setBody(json_encode(array('attributes' => $allAttrs, 'version' => self::API_VERSION)))
575
+ ->setHttpResponseCode(200)
576
+ ->setHeader('Content-type', 'application/json', true);
577
  } catch(Exception $e) {
578
  $this->getResponse()
579
  ->setBody(json_encode(array('status' => 'error', 'message' => 'Internal server error', 'version' => self::API_VERSION)))
582
  }
583
 
584
  return $this;
585
+ }
586
+
587
+
588
+ public function modifyfacetattributeAction() {
589
+ try {
590
+ if(!$this->_authorise()) {
591
+ return $this;
592
+ }
593
+
594
+ $facetData = $this->getRequest()->getParam('newData', false);
595
+ $newFacetData = json_decode($facetData);
596
+
597
+ if(!$newFacetData){
598
+ if ($facetData == "") {
599
+ $input = file_get_contents('php://input');
600
+ $input = utf8_encode($input);
601
+ $newFacetData = json_decode($input)->newData;
602
+ }
603
+ }
604
+
605
+ if(!$newFacetData && !$newFacetData->attribute_id)
606
+ throw new Exception("Insufficient data");
607
+
608
+ // updates the facet
609
+ $resourceModel = Mage::getResourceModel('catalog/product_attribute_collection');
610
+ $attributeObj = $resourceModel->getItemById($newFacetData->attribute_id);
611
+
612
+ // Not required anymore
613
+ unset($newFacetData->attribute_id);
614
+
615
+ foreach ($newFacetData as $key => $value) {
616
+ $attributeObj->setData($key, $value);
617
+ }
618
+ $updateStatus = $attributeObj->save();
619
+
620
+ if(!$updateStatus)
621
+ throw new Exception("Couldn't update");
622
+
623
+ $this->getResponse()
624
+ ->setBody(json_encode(array("status"=>"ok", 'version' => self::API_VERSION)))
625
+ ->setHttpResponseCode(200)
626
+ ->setHeader('Content-type', 'application/json', true);
627
+ } catch(Exception $e) {
628
+ if($e->getMessage())
629
+ $errorMsg = $e->getMessage();
630
+ else
631
+ $errorMsg = "Internal server error";
632
+
633
+ $this->getResponse()
634
+ ->setBody(json_encode(array('status' => 'error', 'message' => $errorMsg, 'version' => self::API_VERSION)))
635
+ ->setHttpResponseCode(500)
636
+ ->setHeader('Content-type', 'application/json', true);
637
+ }
638
 
639
+ return $this;
640
  }
641
 
642
+ // Formats the attribute for save
643
+ private function _prepareAttributeForSave($data) {
644
+ /** @var $helperCatalog Mage_Catalog_Helper_Data */
645
+ $helperCatalog = Mage::helper('catalog');
646
+
647
+ if ($data['scope'] == 'global') {
648
+ $data['is_global'] = Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL;
649
+ } else if ($data['scope'] == 'website') {
650
+ $data['is_global'] = Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_WEBSITE;
651
+ } else {
652
+ $data['is_global'] = Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE;
653
+ }
654
+
655
+ if (!isset($data['is_configurable'])) {
656
+ $data['is_configurable'] = 0;
657
+ }
658
+ if (!isset($data['is_filterable'])) {
659
+ $data['is_filterable'] = 1;
660
+ }
661
+ if (!isset($data['is_filterable_in_search'])) {
662
+ $data['is_filterable_in_search'] = 1;
663
+ }
664
+ if (!isset($data['apply_to'])) {
665
+ $data['apply_to'] = array();
666
+ }
667
+ // set frontend labels array with store_id as keys
668
+ if (isset($data['frontend_label']) && is_array($data['frontend_label'])) {
669
+ $labels = array();
670
+ foreach ($data['frontend_label'] as $label) {
671
+ $storeId = $label['store_id'];
672
+ $labelText = $helperCatalog->stripTags($label['label']);
673
+ $labels[$storeId] = $labelText;
674
+ }
675
+ $data['frontend_label'] = $labels;
676
+ }
677
+ // set additional fields
678
+ if (isset($data['additional_fields']) && is_array($data['additional_fields'])) {
679
+ $data = array_merge($data, $data['additional_fields']);
680
+ unset($data['additional_fields']);
681
+ }
682
+ //default value
683
+ if (!empty($data['default_value'])) {
684
+ $data['default_value'] = $helperCatalog->stripTags($data['default_value']);
685
+ }
686
+
687
+ return $data;
688
+ }
689
+
690
+
691
+ public function addfacetattributeAction() {
692
+ try {
693
+ if(!$this->_authorise()) {
694
+ return $this;
695
+ }
696
+
697
+ $facetData = $this->getRequest()->getParam('newData', false);
698
+ $newFacetData = json_decode($facetData);
699
+
700
+ if(!$newFacetData){
701
+ if ($facetData == "") {
702
+ $input = file_get_contents('php://input');
703
+ $input = utf8_encode($input);
704
+ $newFacetData = json_decode($input)->newData;
705
+ }
706
+ }
707
+ $newFacetData = (array)$newFacetData;
708
+
709
+ if (!$newFacetData || empty($newFacetData['attribute_code']) || !isset($newFacetData['frontend_label'])) {
710
+ throw new Exception("Insufficient data");
711
+ }
712
+
713
+ //validate attribute_code
714
+ if (!preg_match('/^[a-z][a-z_0-9]{0,254}$/', $newFacetData['attribute_code'])) {
715
+ throw new Exception("Invalid attribute_code");
716
+ }
717
+
718
+ /** @var $model Mage_Catalog_Model_Resource_Eav_Attribute */
719
+ $model = Mage::getModel('catalog/resource_eav_attribute');
720
+ /** @var $helper Mage_Catalog_Helper_Product */
721
+ $helper = Mage::helper('catalog/product');
722
+
723
+
724
+ $newFacetData['source_model'] = $helper->getAttributeSourceModelByInputType('multiselect');
725
+ $newFacetData['backend_model'] = $helper->getAttributeBackendModelByInputType('multiselect');
726
+ if (is_null($model->getIsUserDefined()) || $model->getIsUserDefined() != 0) {
727
+ $newFacetData['backend_type'] = $model->getBackendTypeByInput('multiselect');
728
+ }
729
+
730
+ $newFacetData = $this->_prepareAttributeForSave($newFacetData);
731
+
732
+ $entityTypeId = Mage::getModel('eav/entity')->setType('catalog_product')->getTypeId();
733
+
734
+ $model->addData($newFacetData);
735
+ $model->setEntityTypeId($entityTypeId);
736
+ $model->setIsUserDefined(1);
737
+
738
+ $model->save();
739
+
740
+ Mage::app()->cleanCache(array(Mage_Core_Model_Translate::CACHE_TAG));
741
+
742
+ $this->getResponse()
743
+ ->setBody(json_encode(array("status"=>"ok", 'version' => self::API_VERSION)))
744
+ ->setHttpResponseCode(200)
745
+ ->setHeader('Content-type', 'application/json', true);
746
+ } catch(Exception $e) {
747
+ if($e->getMessage())
748
+ $errorMsg = $e->getMessage();
749
+ else
750
+ $errorMsg = "Internal server error";
751
+
752
+ $this->getResponse()
753
+ ->setBody(json_encode(array('status' => 'error', 'message' => $errorMsg, 'version' => self::API_VERSION)))
754
+ ->setHttpResponseCode(500)
755
+ ->setHeader('Content-type', 'application/json', true);
756
+ }
757
+
758
+ return $this;
759
+ }
760
+
761
+
762
  public function usersAction() {
763
 
764
  try {
937
  if($debug) {
938
  $attributes = $product->getAttributes();
939
  foreach($attributes as $key => $value) {
940
+ // Sanity check, or else crashes without proper error handling
941
+ $existenceCheck = $product->getResource()->getAttribute($key);
942
+
943
+ if($existenceCheck)
944
+ $formattedProduct['extras'][$key] = $product->getAttributeText($key);
945
  }
946
  } else {
947
  foreach($extras as $key) {
948
+ // Sanity check, or else crashes without proper error handling
949
+ $existenceCheck = $product->getResource()->getAttribute($key);
950
+
951
+ if($existenceCheck)
952
+ $formattedProduct['extras'][$key] = $product->getAttributeText($key);
953
  }
954
  }
955
 
967
  $formattedProduct['cat'][$formattedCategory['id']] = $formattedCategory;
968
  }
969
 
970
+ return $formattedProduct;
971
+ } catch(Exception $e) {
972
+ return NULL;
973
+ }
974
  }
975
 
976
  private function getFormattedCategory($category) {
978
  $formattedCategory = null;
979
 
980
  try {
 
981
  $formattedCategory = array(
982
  'id' => $category->getId(),
983
  'name' => $category->getName(),
984
  'image' => $category->getImageUrl(),
985
  'url' => $category->getUrl(),
986
  'level' => $category->getLevel(),
987
+ 'path' => $category->getPath(),
988
+ 'is_anchor' => $category->getIsAnchor(),
989
  'is_active' => $category->getIsActive(),
990
+ 'created_at' => $category->getCreatedAt(),
991
+ 'updated_at' => $category->getUpdatedAt()
992
  );
993
 
994
  } catch(Exception $e) {}
995
 
996
  return $formattedCategory;
 
997
  }
998
 
999
  public function sortbyAction() {
1000
 
1001
  try {
1002
+ if(!$this->_authorise()) {
1003
+ return $this;
1004
+ }
 
1005
 
1006
  $sortByOptions = array();
1007
  // $attributesData = Mage::getResourceModel('catalog/config')->getAttributesUsedForSortBy();
1047
  return $this;
1048
  }
1049
 
1050
+ public function choicepageAction() {
1051
+ try{
1052
+ if(!$this->_authorise()) {
1053
+ return $this;
1054
+ }
1055
+
1056
+ $responseObj = array();
1057
+
1058
+ if ($_SERVER["REQUEST_METHOD"] == "POST") {
1059
+ // Create Choice page
1060
+ // Expects title, url_path and content
1061
+
1062
+ $pageData = $this->getRequest()->getParam('data');
1063
+ if ($pageData == "") {
1064
+ $input = file_get_contents('php://input');
1065
+ $input = utf8_encode($input);
1066
+ $pageData = json_encode(json_decode($input)->data);
1067
+ }
1068
+ $pageData = json_decode($pageData);
1069
+ $urlPath = $pageData->url_path;
1070
+
1071
+ if(!$urlPath){
1072
+ // What can this poor guy do without the path?
1073
+ throw new Exception("No urlPath");
1074
+ }
1075
+
1076
+ // Ensure no other existing page contains the same URL path or say "identifier"
1077
+ $collection = Mage::getModel('cms/page')
1078
+ ->getCollection()
1079
+ ->addFieldToFilter('identifier', $urlPath);
1080
+ // Not useful anymore
1081
+ // $existingPages = Mage::getModel('cms/page')->load($collection->getFirstItem()->getId());
1082
+
1083
+ // There can be multiple pages with same url path, but different store scope. Let's disable all
1084
+ foreach ($collection as $existingPage){
1085
+ // Page already exists && is_active
1086
+ if($existingPage->getId()) {
1087
+ // Delete all other pages having same URL
1088
+ // and then continue adding our new page
1089
+ // $this->updateChoiceUrl(false, $existingPage->getId(), array("is_active" => 0, "identifier" => $urlPath));
1090
+ $existingPage->delete();
1091
+ }
1092
+ }
1093
+
1094
+ // Setting default content if not provided
1095
+ $pageContent = (!isset($pageData->content)) ? "<script>window._caichoicePage = true;</script><div id='caichoicePage'></div>" : $pageData->content;
1096
+
1097
+ // Setting default Title if not available
1098
+ $pageTitle = (!isset($pageData->title)) ? "Choice.AI" : $pageData->title;
1099
+
1100
+
1101
+ $choicePageData = array(
1102
+ 'title' => $pageTitle,
1103
+ 'root_template' => 'one_column',
1104
+ //'meta_keywords' => 'meta,keywords',
1105
+ //'meta_description' => 'meta description',
1106
+ 'identifier' => $urlPath,
1107
+ //'content_heading' => 'content heading',
1108
+ 'stores' => array(0),//available for all store views
1109
+ 'content' => $pageContent
1110
+ );
1111
+
1112
+ // Create new page
1113
+ $choicePage = Mage::getModel('cms/page')->setData($choicePageData)->save();
1114
+
1115
+ $responseObj["status"] = "ok";
1116
+ $responseObj["id"] = $choicePage->getId();
1117
+ } else if ($_SERVER["REQUEST_METHOD"] == "PUT") {
1118
+ // Update Choice page's URL path
1119
+ $newPageData = $this->getRequest()->getParam('data');
1120
+
1121
+ if ($newPageData == "") {
1122
+ $input = file_get_contents('php://input');
1123
+ $input = utf8_encode($input);
1124
+ $newPageData = json_encode(json_decode($input)->data);
1125
+ }
1126
+ $newPageData = json_decode($newPageData);
1127
+
1128
+ if ($newPageData == NULL || !isset($newPageData->page_id)) {
1129
+ throw new Exception();
1130
+ }
1131
+
1132
+ $pageId = $newPageData->page_id;
1133
+
1134
+ $this->updateChoiceUrl($newPageData, $pageId);
1135
+ $responseObj["status"] = "ok";
1136
+ } else if ($_SERVER["REQUEST_METHOD"] == "DELETE") {
1137
+ $newPageData = $this->getRequest()->getParam('data');
1138
+
1139
+ if ($newPageData == "") {
1140
+ $input = file_get_contents('php://input');
1141
+ $input = utf8_encode($input);
1142
+ $newPageData = json_encode(json_decode($input)->data);
1143
+ }
1144
+ $newPageData = json_decode($newPageData);
1145
+
1146
+ if ($newPageData == NULL || !isset($newPageData->page_id)) {
1147
+ throw new Exception();
1148
+ }
1149
+
1150
+ $pageId = $newPageData->page_id;
1151
+
1152
+ $pagesCollection = Mage::getModel('cms/page')
1153
+ ->getCollection()
1154
+ ->addFieldToFilter('page_id', array("eq"=> $pageId));
1155
+ $pageData = Mage::getModel('cms/page')->load($pagesCollection->getFirstItem()->getId());
1156
+ $pageData->delete();
1157
+ // Mage::app()->getStore()->resetConfig();
1158
+ $responseObj["status"] = "ok";
1159
+ } else {
1160
+ // Invalid, return error
1161
+ $responseObj['status'] = 'error';
1162
+ $responseObj['message'] = 'Invalid request';
1163
+ }
1164
+
1165
+ $responseObj['version'] = self::API_VERSION;
1166
+ $this->getResponse()
1167
+ ->setBody(json_encode($responseObj))
1168
+ ->setHttpResponseCode(200)
1169
+ ->setHeader('Content-type', 'application/json', true);
1170
+ } catch(Exception $e) {
1171
+ $this->getResponse()
1172
+ ->setBody(json_encode(array('emsg'=> $e->getMessage(), 'status' => 'error', 'message' => 'Internal server error', 'version' => self::API_VERSION)))
1173
+ ->setHttpResponseCode(500)
1174
+ ->setHeader('Content-type', 'application/json', true);
1175
+ }
1176
+
1177
+ return this;
1178
+ }
1179
+
1180
+ private function updateChoiceUrl($newPageData, $pageId, $dataToUpdate=false){
1181
+ // Doesn't allow update without "identifier" field :/
1182
+ if(!$dataToUpdate) {
1183
+ // Update case
1184
+ $dataToUpdate = array();
1185
+
1186
+ if (isset($newPageData->url_path)) {
1187
+ $urlPath = $newPageData->url_path;
1188
+
1189
+ // Ensure no other existing page contains the same URL path / "identifier"
1190
+ $collection = Mage::getModel('cms/page')
1191
+ ->getCollection()
1192
+ ->addFieldToFilter('identifier', $urlPath)
1193
+ ->addFieldToFilter('page_id', array("neq"=> $pageId));
1194
+ $page = Mage::getModel('cms/page')->load($collection->getFirstItem()->getId());
1195
+
1196
+ // URL path already being used?
1197
+ if ($page->getId())
1198
+ throw new Exception();
1199
+ else
1200
+ $dataToUpdate['identifier'] = $urlPath;
1201
+ }
1202
+
1203
+ if (isset($newPageData->content)) {
1204
+ $dataToUpdate['content'] = $newPageData->content;
1205
+ }
1206
+
1207
+ if (isset($newPageData->title)) {
1208
+ $dataToUpdate['title'] = $newPageData->title;
1209
+ }
1210
+ }
1211
+
1212
+ $dataToUpdate['page_id'] = $pageId;
1213
+
1214
+ // Ensuring proper scope per update
1215
+ $dataToUpdate['stores'] = array(0);
1216
+
1217
+ if(!isset($dataToUpdate['identifier'])){
1218
+ // Get existing "identifier"
1219
+ $pagesCollection = Mage::getModel('cms/page')
1220
+ ->getCollection()
1221
+ ->addFieldToFilter('page_id', array("eq"=> $pageId));
1222
+ $pageData = Mage::getModel('cms/page')->load($pagesCollection->getFirstItem()->getId());
1223
+
1224
+ $dataToUpdate['identifier'] = $pageData->getIdentifier();
1225
+ }
1226
+
1227
+ return Mage::getModel('cms/page')->setData($dataToUpdate)->save();
1228
+ }
1229
+
1230
  }
app/code/local/ChoiceAI/Search/Block/Catalog/Layer/View.php CHANGED
@@ -85,35 +85,24 @@ class ChoiceAI_Search_Block_Catalog_Layer_View extends Mage_Catalog_Block_Layer_
85
  }
86
 
87
  $this->getLayer()->apply();
 
 
88
  $this->getLayer()->getProductCollection()->load();
89
  } else {
90
- parent::_prepareLayout();
91
-
92
  }
93
 
94
  return $this;
95
  }
96
 
97
-
98
- public function getRequest(){
99
- $controller = Mage::app()->getFrontController();
100
- if ($controller) {
101
- $this->_request = $controller->getRequest();
102
- } else {
103
- throw new Exception(Mage::helper('core')->__("Can't retrieve request object"));
104
- }
105
- return $this->_request;
106
- }
107
-
108
  /**
109
  * Returns current catalog layer.
110
  *
111
  * @return ChoiceAI_Search_Model_Catalog_Layer|Mage_Catalog_Model_Layer
112
  */
113
- public function getLayer()
114
- {
115
  if (Mage::helper('choiceai_search')->isActiveEngine()) {
116
- return Mage::getSingleton('choiceai_search/catalog_layer');
117
  }
118
  return parent::getLayer();
119
  }
85
  }
86
 
87
  $this->getLayer()->apply();
88
+
89
+ // Causing a request to the data server, without sort by param
90
  $this->getLayer()->getProductCollection()->load();
91
  } else {
92
+ parent::_prepareLayout();
 
93
  }
94
 
95
  return $this;
96
  }
97
 
 
 
 
 
 
 
 
 
 
 
 
98
  /**
99
  * Returns current catalog layer.
100
  *
101
  * @return ChoiceAI_Search_Model_Catalog_Layer|Mage_Catalog_Model_Layer
102
  */
103
+ public function getLayer(){
 
104
  if (Mage::helper('choiceai_search')->isActiveEngine()) {
105
+ return Mage::getSingleton('choiceai_search/catalog_layer');
106
  }
107
  return parent::getLayer();
108
  }
app/code/local/ChoiceAI/Search/Block/Catalog/Product/List/Toolbar.php CHANGED
@@ -19,10 +19,10 @@ class ChoiceAI_Search_Block_Catalog_Product_List_Toolbar extends Mage_Catalog_Bl
19
  public function getCurrentOrder()
20
  {
21
  // To set currently selected sort by option to extended/overridden one, in case of search/productlist
22
- if(Mage::getStoreConfig(self::IS_ACTIVE)=='1') {
23
- if (is_null($_REQUEST['order']) && isset($_SESSION['plist_sort_by'])) {
24
  return $_SESSION['plist_sort_by'];
25
- } else if ($_REQUEST['order'] == $_SESSION['plist_sort_by']) {
26
  return $_SESSION['plist_sort_by'];
27
  } else{
28
  return parent::getCurrentOrder();
19
  public function getCurrentOrder()
20
  {
21
  // To set currently selected sort by option to extended/overridden one, in case of search/productlist
22
+ if(Mage::helper('choiceai_search')->isActiveEngine()) {
23
+ if ((!isset($_REQUEST['order']) ||is_null($_REQUEST['order'])) && isset($_SESSION['plist_sort_by'])) {
24
  return $_SESSION['plist_sort_by'];
25
+ } else if (isset($_SESSION['plist_sort_by']) && isset($_REQUEST['order']) && $_REQUEST['order'] == $_SESSION['plist_sort_by']) {
26
  return $_SESSION['plist_sort_by'];
27
  } else{
28
  return parent::getCurrentOrder();
app/code/local/ChoiceAI/Search/Block/Catalogsearch/Layer.php CHANGED
@@ -43,7 +43,7 @@ class ChoiceAI_Search_Block_Catalogsearch_Layer extends Mage_CatalogSearch_Block
43
  {
44
  /** @var $helper ChoiceAI_Search_Helper_Data */
45
  $helper = Mage::helper('choiceai_search');
46
- if ($helper->isActiveEngine()) {
47
 
48
  $stateBlock = $this->getLayout()->createBlock($this->_stateBlockName)
49
  ->setLayer($this->getLayer());
@@ -79,7 +79,7 @@ class ChoiceAI_Search_Block_Catalogsearch_Layer extends Mage_CatalogSearch_Block
79
  }
80
 
81
  $this->getLayer()->apply();
82
- $this->getLayer()->getProductCollection()->load();
83
  }else{
84
  parent::_prepareLayout();
85
  }
43
  {
44
  /** @var $helper ChoiceAI_Search_Helper_Data */
45
  $helper = Mage::helper('choiceai_search');
46
+ if ($helper->isActiveEngine()) {
47
 
48
  $stateBlock = $this->getLayout()->createBlock($this->_stateBlockName)
49
  ->setLayer($this->getLayer());
79
  }
80
 
81
  $this->getLayer()->apply();
82
+ $this->getLayer()->getProductCollection()->load();
83
  }else{
84
  parent::_prepareLayout();
85
  }
app/code/local/ChoiceAI/Search/Block/Catalogsearch/Result.php CHANGED
@@ -15,7 +15,7 @@ class ChoiceAI_Search_Block_Catalogsearch_Result extends Mage_CatalogSearch_Bloc
15
  */
16
  public function setListOrders()
17
  {
18
- if(Mage::getStoreConfig(self::IS_ACTIVE)=='1') {
19
  $category = Mage::getSingleton('catalog/layer')
20
  ->getCurrentCategory();
21
  /* @var $category Mage_Catalog_Model_Category */
15
  */
16
  public function setListOrders()
17
  {
18
+ if(Mage::helper('choiceai_search')->isActiveEngine()) {
19
  $category = Mage::getSingleton('catalog/layer')
20
  ->getCurrentCategory();
21
  /* @var $category Mage_Catalog_Model_Category */
app/code/local/ChoiceAI/Search/Helper/Catalogsearch.php DELETED
@@ -1,19 +0,0 @@
1
- <?php
2
-
3
- /**
4
- * @category ChoiceAI
5
- * @package ChoiceAI_Recommendation
6
- * @copyright Copyright (c) MineWhat
7
- * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
8
- */
9
- class ChoiceAI_Search_Helper_Catalogsearch extends Mage_CatalogSearch_Helper_Data {
10
-
11
- public function getResultUrl($query = null) {
12
- if(Mage::helper('choiceai_search')->isHostedSearchActive()) {
13
- $redirectUrl = Mage::helper('choiceai_search')->getHostedRedirectUrl();
14
- return $redirectUrl . ((!is_null($query) && $query != "")?($this->getQueryParamName()."=".$query):"");
15
- }
16
- return parent::getResultUrl($query);
17
- }
18
-
19
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app/code/local/ChoiceAI/Search/Helper/Choiceaisearch.php CHANGED
@@ -11,6 +11,5 @@ class ChoiceAI_Search_Helper_ChoiceAIsearch extends ChoiceAI_Search_Helper_Data
11
  public function getEngineConfigData($prefix = '', $website = null)
12
  {
13
  return Mage::helper('choiceai_searchcore')->getEngineConfigData($prefix, $website);
14
-
15
  }
16
  }
11
  public function getEngineConfigData($prefix = '', $website = null)
12
  {
13
  return Mage::helper('choiceai_searchcore')->getEngineConfigData($prefix, $website);
 
14
  }
15
  }
app/code/local/ChoiceAI/Search/Helper/Data.php CHANGED
@@ -1,4 +1,5 @@
1
  <?php
 
2
  /**
3
  *
4
  * @package ChoiceAI_Search
@@ -50,10 +51,10 @@ class ChoiceAI_Search_Helper_Data extends Mage_Core_Helper_Abstract
50
  );
51
 
52
  /**
53
- * Boolean field which stores choiceai active or not
54
- *
55
- * @var boolean
56
- */
57
  public $is_active = NULL;
58
 
59
 
@@ -91,16 +92,6 @@ class ChoiceAI_Search_Helper_Data extends Mage_Core_Helper_Abstract
91
  return $attributeCode;
92
  }
93
 
94
- /**
95
- * Returns cache lifetime in seconds.
96
- *
97
- * @return int
98
- */
99
- public function getCacheLifetime()
100
- {
101
- return Mage::getStoreConfig('core/cache/lifetime');
102
- }
103
-
104
  /**
105
  * Returns search engine config data.
106
  *
@@ -131,7 +122,7 @@ class ChoiceAI_Search_Helper_Data extends Mage_Core_Helper_Abstract
131
  */
132
  public function getLanguageCodeByLocaleCode($localeCode)
133
  {
134
- $localeCode = (string) $localeCode;
135
  if (!$localeCode) {
136
  return false;
137
  }
@@ -153,17 +144,6 @@ class ChoiceAI_Search_Helper_Data extends Mage_Core_Helper_Abstract
153
  return $this->_languageCodes[$localeCode];
154
  }
155
 
156
- /**
157
- * Returns store language code.
158
- *
159
- * @param mixed $store
160
- * @return bool
161
- */
162
- public function getLanguageCodeByStore($store = null)
163
- {
164
- return $this->getLanguageCodeByLocaleCode($this->getLocaleCode($store));
165
- }
166
-
167
  /**
168
  * Returns store locale code.
169
  *
@@ -175,65 +155,6 @@ class ChoiceAI_Search_Helper_Data extends Mage_Core_Helper_Abstract
175
  return Mage::getStoreConfig(Mage_Core_Model_Locale::XML_PATH_DEFAULT_LOCALE, $store);
176
  }
177
 
178
- /**
179
- * Retrieves all searchable product attributes.
180
- * Possibility to filter attributes by backend type.
181
- *
182
- * @param array $backendType
183
- * @return array
184
- */
185
- public function getSearchableAttributes($backendType = null)
186
- {
187
- if (null === $this->_searchableAttributes) {
188
- $this->_searchableAttributes = array();
189
- $entityType = $this->getEavConfig()->getEntityType('catalog_product');
190
- $entity = $entityType->getEntity();
191
-
192
- /* @var $productAttributeCollection Mage_Catalog_Model_Resource_Product_Attribute_Collection */
193
- $productAttributeCollection = Mage::getResourceModel('catalog/product_attribute_collection')
194
- ->setEntityTypeFilter($entityType->getEntityTypeId())
195
- ->addVisibleFilter()
196
- ->addToIndexFilter(true);
197
-
198
- $attributes = $productAttributeCollection->getItems();
199
- foreach ($attributes as $attribute) {
200
- /** @var $attribute Mage_Catalog_Model_Resource_Eav_Attribute */
201
- $attribute->setEntity($entity);
202
- $this->_searchableAttributes[$attribute->getAttributeCode()] = $attribute;
203
- }
204
- }
205
-
206
- if (null !== $backendType) {
207
- $backendType = (array) $backendType;
208
- $attributes = array();
209
- foreach ($this->_searchableAttributes as $attribute) {
210
- /** @var $attribute Mage_Catalog_Model_Resource_Eav_Attribute */
211
- if (in_array($attribute->getBackendType(), $backendType)) {
212
- $attributes[$attribute->getAttributeCode()] = $attribute;
213
- }
214
- }
215
-
216
- return $attributes;
217
- }
218
-
219
- return $this->_searchableAttributes;
220
- }
221
-
222
- /**
223
- * Returns seach config data.
224
- *
225
- * @param string $field
226
- * @param mixed $store
227
- * @return array
228
- */
229
- public function getSearchConfigData($field, $store = null)
230
- {
231
- $path = 'catalog/search/' . $field;
232
-
233
- return Mage::getStoreConfig($path, $store);
234
- }
235
-
236
-
237
  /**
238
  * Returns searched parameter as array.
239
  *
@@ -245,7 +166,8 @@ class ChoiceAI_Search_Helper_Data extends Mage_Core_Helper_Abstract
245
  {
246
  if (empty($value) ||
247
  (isset($value['from']) && empty($value['from']) &&
248
- isset($value['to']) && empty($value['to']))) {
 
249
  return false;
250
  }
251
 
@@ -276,54 +198,6 @@ class ChoiceAI_Search_Helper_Data extends Mage_Core_Helper_Abstract
276
  return array($field => $value);
277
  }
278
 
279
- /**
280
- * Returns sortable attribute field name (localized if needed).
281
- *
282
- * @param Mage_Catalog_Model_Resource_Eav_Attribute $attribute
283
- * @param string $locale
284
- * @return string
285
- */
286
- public function getSortableAttributeFieldName($attribute, $locale = null)
287
- {
288
- if (is_string($attribute)) {
289
- $this->getSortableAttributes(); // populate sortable attributes if not already set
290
- if (!isset($this->_sortableAttributes[$attribute])) {
291
- return $attribute;
292
- }
293
- $attribute = $this->_sortableAttributes[$attribute];
294
- }
295
-
296
- $attributeCode = $attribute->getAttributeCode();
297
-
298
- if ($attributeCode != 'score' && !in_array($attribute->getBackendType(), $this->_unlocalizedFieldTypes)) {
299
- if (null === $locale) {
300
- $locale = $this->getLocaleCode();
301
- }
302
- $languageCode = $this->getLanguageCodeByLocaleCode($locale);
303
- $languageSuffix = $languageCode ? '_' . $languageCode : '';
304
- $attributeCode .= $languageSuffix;
305
- }
306
-
307
- return 'sort_by_' . $attributeCode;
308
- }
309
-
310
- /**
311
- * Retrieves all sortable product attributes.
312
- *
313
- * @return array
314
- */
315
- public function getSortableAttributes()
316
- {
317
- if (null === $this->_sortableAttributes) {
318
- $this->_sortableAttributes = Mage::getSingleton('catalog/config')->getAttributesUsedForSortBy();
319
- if (array_key_exists('price', $this->_sortableAttributes)) {
320
- unset($this->_sortableAttributes['price']); // Price sorting is handled with searchable attribute.
321
- }
322
- }
323
-
324
- return $this->_sortableAttributes;
325
- }
326
-
327
  /**
328
  * Defines supported languages for snowball filter.
329
  *
@@ -346,9 +220,9 @@ class ChoiceAI_Search_Helper_Data extends Mage_Core_Helper_Abstract
346
  // French
347
  'fr' => array('fr_CA', 'fr_FR'),
348
  // German
349
- 'de' => array('de_DE','de_DE','de_AT'),
350
  // Italian
351
- 'it' => array('it_IT','it_CH'),
352
  // Norwegian
353
  'nb' => array('nb_NO', 'nn_NO'),
354
  // Portuguese
@@ -389,9 +263,9 @@ class ChoiceAI_Search_Helper_Data extends Mage_Core_Helper_Abstract
389
  *
390
  * @return bool
391
  */
392
- public function isActiveEngine() {
393
-
394
- if(is_null($this->is_active)) {
395
  $storeConfig = json_decode(Mage::getStoreConfig(self::CONFIG_KEY));
396
  $sortbyObjs = $storeConfig->sortby;
397
 
@@ -418,6 +292,7 @@ class ChoiceAI_Search_Helper_Data extends Mage_Core_Helper_Abstract
418
  $currentReqPath = str_replace("/index.php/", "/", $currentReqPath);
419
 
420
  $isPluginActive = Mage::getStoreConfig(self::IS_ACTIVE) == '1';
 
421
 
422
  if ($isPluginActive) {
423
  foreach ($sortbyObjs as $sortbyObj) {
@@ -425,7 +300,9 @@ class ChoiceAI_Search_Helper_Data extends Mage_Core_Helper_Abstract
425
  if (in_array($currentReqPath, $sortbyObj->rule->paths)) {
426
  $choiceOptions = $this->_getOptionsAddedByChoice($sortbyObj);
427
 
428
- if (is_null($_REQUEST["order"]))
 
 
429
  $this->_setDefaultSortOption($sortbyObj, $storeConfig);
430
  break;
431
  }
@@ -435,16 +312,27 @@ class ChoiceAI_Search_Helper_Data extends Mage_Core_Helper_Abstract
435
  if (!empty(array_intersect($currentReqParams, $sortbyObj->rule->params))) {
436
  $choiceOptions = $this->_getOptionsAddedByChoice($sortbyObj);
437
 
438
- //
439
- if (is_null($_REQUEST["order"]))
 
440
  $this->_setDefaultSortOption($sortbyObj, $storeConfig);
441
  break;
442
  }
443
  }
444
  }
445
 
446
- if (count($choiceOptions) && in_array($_REQUEST["order"], $choiceOptions)) {
447
- $this->is_active = true;
 
 
 
 
 
 
 
 
 
 
448
  }
449
  }
450
  }
@@ -452,96 +340,64 @@ class ChoiceAI_Search_Helper_Data extends Mage_Core_Helper_Abstract
452
  return $this->is_active;
453
  }
454
 
 
 
 
 
 
 
 
 
 
 
 
 
455
  // If sort order is default, $_REQUEST["order"] will not exist
456
  // This adds a default value in it
457
- private function _setDefaultSortOption($sortbyObj, $STORE_CONFIG){
458
- if(isset($sortbyObj->extend)){
459
- foreach ($sortbyObj->extend as $optionKey => $optionValue) {
460
- $_REQUEST['order'] = $optionKey;
461
- break;
 
 
 
 
 
 
462
  }
463
- } else if(isset($sortbyObj->override)) {
464
- $_REQUEST["order"] = $STORE_CONFIG->default_search_sort;
465
- } else if(isset($_SESSION['catalog']) && isset($_SESSION['catalog']['sort_order'])){
466
- // If prod list page, use the session value
467
- // This case although seems useless
468
- $_REQUEST["order"] = $_SESSION['catalog']['sort_order'];
469
  }
 
 
 
 
 
470
  }
471
 
472
  /**
473
  * @param $sortbyObj
474
  * @return array
475
  */
476
- private function _getOptionsAddedByChoice($sortbyObj){
 
477
  $options = array();
478
 
479
- if(isset($sortbyObj->override)){
480
  // Search case, override all system options with choice
481
- $caiOptions = $sortbyObj->override;
482
-
483
- foreach ($caiOptions as $key => $caiOption)
484
  $options[] = $key;
485
- } elseif(isset($sortbyObj->extend)) {
486
- foreach ($sortbyObj->extend as $key=>$option)
487
  $options[] = $key;
488
  }
489
 
490
  return $options;
491
  }
492
 
493
- /**
494
- * Checks whether request is a navigation
495
- *
496
- * @param Mage_Catalog_Model_Resource_Eav_Attribute $attribute
497
- * @return bool
498
- */
499
- public function isNavigation() {
500
- if($this->_getRequest()->getControllerName() == "category") {
501
- return true;
502
- }
503
- return false;
504
- }
505
-
506
- /**
507
- * Checks whether request is a resultPage
508
- *
509
- * @param Mage_Catalog_Model_Resource_Eav_Attribute $attribute
510
- * @return bool
511
- */
512
- public function isSearch() {
513
- if($this->_getRequest()->getControllerName() == "result") {
514
- return true;
515
- }
516
- return false;
517
- }
518
-
519
- /**
520
- * Checks if specified attribute is indexable by search engine.
521
- *
522
- * @param Mage_Catalog_Model_Resource_Eav_Attribute $attribute
523
- * @return bool
524
- */
525
- public function isAttributeIndexable($attribute)
526
- {
527
- if ($attribute->getBackendType() == 'varchar' && !$attribute->getBackendModel()) {
528
- return true;
529
- }
530
-
531
- if ($attribute->getBackendType() == 'int'
532
- && $attribute->getSourceModel() != 'eav/entity_attribute_source_boolean'
533
- && ($attribute->getIsSearchable() || $attribute->getIsFilterable() || $attribute->getIsFilterableInSearch())
534
- ) {
535
- return true;
536
- }
537
-
538
- if ($attribute->getIsSearchable() || $attribute->getIsFilterable() || $attribute->getIsFilterableInSearch()) {
539
- return true;
540
- }
541
-
542
- return false;
543
- }
544
-
545
  /**
546
  * Checks if debug mode is enabled.
547
  *
@@ -554,20 +410,9 @@ class ChoiceAI_Search_Helper_Data extends Mage_Core_Helper_Abstract
554
  return array_key_exists('enable_debug_mode', $config) && $config['enable_debug_mode'];
555
  }
556
 
557
- /**
558
- * Method that can be overriden for customing product data indexation.
559
- *
560
- * @param array $index
561
- * @param string $separator
562
- * @return array
563
- */
564
- public function prepareIndexData($index, $separator = null)
565
- {
566
- return $index;
567
- }
568
-
569
  /**
570
  * Forces error display.
 
571
  *
572
  * @param string $error
573
  */
@@ -577,94 +422,4 @@ class ChoiceAI_Search_Helper_Data extends Mage_Core_Helper_Abstract
577
  ->addError($error)->getGroupedHtml();
578
  }
579
 
580
- /*
581
- * returns the choiceai site name
582
- *
583
- * @return String
584
- */
585
- public function getSiteName(){
586
- $siteKeyLabel = ChoiceAI_Searchcore_Helper_Constants::SITE_KEY;
587
- $config = $this->getEngineConfigData($siteKeyLabel);
588
- if(array_key_exists($siteKeyLabel, $config) && $config[$siteKeyLabel] != "")
589
- return $config[$siteKeyLabel];
590
- else {
591
- $siteKey = Mage::getResourceModel('choiceai_searchcore/config')
592
- ->getValue(Mage::app()->getWebsite()->getWebsiteId(),
593
- $siteKeyLabel);
594
- if(isset($siteKey) && $siteKey != ''){
595
- Mage::helper('choiceai_searchcore')->saveConfig(Mage::app()->getWebsite(),
596
- array($siteKeyLabel => $siteKey));
597
- return $siteKey;
598
- } else {
599
- if(Mage::getIsDeveloperMode()) {
600
- Mage::throwException("ChoiceAI site key is empty");
601
- } else {
602
- Mage::helper('choiceai_searchcore')->log(Zend_Log::ERR, 'ChoiceAI site key is not set');
603
- return null;
604
- }
605
- }
606
- }
607
- }
608
-
609
- /*
610
- * returns the choiceai api Key
611
- *
612
- * @return String
613
- */
614
- public function getApiKey(){
615
- $apiKeyLabel = ChoiceAI_Searchcore_Helper_Constants::API_KEY;
616
- $config = $this->getEngineConfigData($apiKeyLabel);
617
- if(array_key_exists($apiKeyLabel, $config) && $config[$apiKeyLabel] != "")
618
- return $config[$apiKeyLabel];
619
- else {
620
- $apiKey = Mage::getResourceModel('choiceai_searchcore/config')
621
- ->getValue(Mage::app()->getWebsite()->getWebsiteId(), $apiKeyLabel);
622
- if(isset($apiKey) && $apiKey != ''){
623
- Mage::helper('choiceai_searchcore')->saveConfig(Mage::app()->getWebsite(),
624
- array($apiKeyLabel => $apiKey));
625
- return $apiKey;
626
- } else {
627
- if(Mage::getIsDeveloperMode()) {
628
- Mage::throwException("ChoiceAI api key is empty");
629
- } else {
630
- Mage::helper('choiceai_searchcore')->log(Zend_Log::ERR, 'ChoiceAI Api key is not set');
631
- return null;
632
- }
633
- }
634
- }
635
- }
636
-
637
- /**
638
- * This method checks whether hosted search is active or not
639
- * @return boolean
640
- */
641
- public function isHostedSearchActive()
642
- {
643
- $siteKey = null;
644
- $apiKey = null;
645
- try {
646
- $siteKey = $this->getSiteName();
647
- $apiKey = $this->getApiKey();
648
- } catch(Exception $e) {
649
- //ignoring the exception
650
- }
651
- $searchConf = $this->getEngineConfigData('search');
652
- $hostedSearchConf = Mage::helper('choiceai_searchcore')->isConfigTrue(Mage::app()->getWebsite(),ChoiceAI_Searchcore_Helper_Constants::SEARCH_HOSTED_STATUS);
653
- $searchModConf = Mage::helper('choiceai_searchcore')->isConfigTrue(Mage::app()->getWebsite(), ChoiceAI_Searchcore_Helper_Constants::SEARCH_MOD_STATUS);
654
- $hostedIntStatus = $searchConf[ChoiceAI_Searchcore_Helper_Constants::SEARCH_HOSTED_INT_STATUS];
655
- $hostedSearchRedirectUrl = $searchConf[ChoiceAI_Searchcore_Helper_Constants::SEARCH_HOSTED_REDIRECT_URL];
656
- return !is_null($siteKey) && !is_null($apiKey) &&
657
- $hostedSearchConf && !$searchModConf &&
658
- $hostedIntStatus == ChoiceAI_Searchcore_Helper_Constants::SEARCH_HOSTED_INT_COMPLETE &&
659
- sizeof($hostedSearchRedirectUrl) > 0;
660
- }
661
-
662
- /**
663
- * Method to get the hosted redirection url
664
- * @return mixed
665
- */
666
- public function getHostedRedirectUrl() {
667
- $config = $this->getEngineConfigData(ChoiceAI_Searchcore_Helper_Constants::SEARCH_HOSTED_REDIRECT_URL);
668
- return $config[ChoiceAI_Searchcore_Helper_Constants::SEARCH_HOSTED_REDIRECT_URL];
669
- }
670
- }
1
  <?php
2
+
3
  /**
4
  *
5
  * @package ChoiceAI_Search
51
  );
52
 
53
  /**
54
+ * Boolean field which stores choiceai active or not
55
+ *
56
+ * @var boolean
57
+ */
58
  public $is_active = NULL;
59
 
60
 
92
  return $attributeCode;
93
  }
94
 
 
 
 
 
 
 
 
 
 
 
95
  /**
96
  * Returns search engine config data.
97
  *
122
  */
123
  public function getLanguageCodeByLocaleCode($localeCode)
124
  {
125
+ $localeCode = (string)$localeCode;
126
  if (!$localeCode) {
127
  return false;
128
  }
144
  return $this->_languageCodes[$localeCode];
145
  }
146
 
 
 
 
 
 
 
 
 
 
 
 
147
  /**
148
  * Returns store locale code.
149
  *
155
  return Mage::getStoreConfig(Mage_Core_Model_Locale::XML_PATH_DEFAULT_LOCALE, $store);
156
  }
157
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
158
  /**
159
  * Returns searched parameter as array.
160
  *
166
  {
167
  if (empty($value) ||
168
  (isset($value['from']) && empty($value['from']) &&
169
+ isset($value['to']) && empty($value['to']))
170
+ ) {
171
  return false;
172
  }
173
 
198
  return array($field => $value);
199
  }
200
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
201
  /**
202
  * Defines supported languages for snowball filter.
203
  *
220
  // French
221
  'fr' => array('fr_CA', 'fr_FR'),
222
  // German
223
+ 'de' => array('de_DE', 'de_DE', 'de_AT'),
224
  // Italian
225
+ 'it' => array('it_IT', 'it_CH'),
226
  // Norwegian
227
  'nb' => array('nb_NO', 'nn_NO'),
228
  // Portuguese
263
  *
264
  * @return bool
265
  */
266
+ public function isActiveEngine()
267
+ {
268
+ if (is_null($this->is_active)) {
269
  $storeConfig = json_decode(Mage::getStoreConfig(self::CONFIG_KEY));
270
  $sortbyObjs = $storeConfig->sortby;
271
 
292
  $currentReqPath = str_replace("/index.php/", "/", $currentReqPath);
293
 
294
  $isPluginActive = Mage::getStoreConfig(self::IS_ACTIVE) == '1';
295
+ $choiceOptions = array();
296
 
297
  if ($isPluginActive) {
298
  foreach ($sortbyObjs as $sortbyObj) {
300
  if (in_array($currentReqPath, $sortbyObj->rule->paths)) {
301
  $choiceOptions = $this->_getOptionsAddedByChoice($sortbyObj);
302
 
303
+ if (count($choiceOptions) < 1)
304
+ $this->is_active = true;
305
+ else if (!isset($_REQUEST["order"]) && is_null($_REQUEST["order"]))
306
  $this->_setDefaultSortOption($sortbyObj, $storeConfig);
307
  break;
308
  }
312
  if (!empty(array_intersect($currentReqParams, $sortbyObj->rule->params))) {
313
  $choiceOptions = $this->_getOptionsAddedByChoice($sortbyObj);
314
 
315
+ if (count($choiceOptions) < 1)
316
+ $this->is_active = true;
317
+ else if (!isset($_REQUEST["order"]) || is_null($_REQUEST["order"]))
318
  $this->_setDefaultSortOption($sortbyObj, $storeConfig);
319
  break;
320
  }
321
  }
322
  }
323
 
324
+ Mage::register('expId', $sortbyObj->expId, true);
325
+ $device = $this->getDevice($_SERVER['HTTP_USER_AGENT']);
326
+
327
+ if (count($choiceOptions) && isset($_REQUEST["order"]) && in_array($_REQUEST["order"], $choiceOptions)) {
328
+ if(isset($sortbyObj) && isset($sortbyObj->device)) {
329
+ if (in_array("all", $sortbyObj->device) || in_array($device, $sortbyObj->device))
330
+ $this->is_active = true;
331
+ else
332
+ $this->is_active = false;
333
+ }
334
+ else
335
+ $this->is_active = false;
336
  }
337
  }
338
  }
340
  return $this->is_active;
341
  }
342
 
343
+ private function getDevice($userAgent)
344
+ {
345
+ if (stripos($userAgent, "ipad") !== false) {
346
+ return "desktoptablet";
347
+ // return "tablet";
348
+ } else if (stripos($userAgent, "android") !== false || stripos($userAgent, "mobile") !== false || stripos($userAgent, "iphone") !== false){
349
+ return "desktoptablet";
350
+ // return "mobile";
351
+ } else
352
+ return 'desktop';
353
+ }
354
+
355
  // If sort order is default, $_REQUEST["order"] will not exist
356
  // This adds a default value in it
357
+ // author: Harkirat
358
+ private function _setDefaultSortOption($sortbyObj, $storeConfig)
359
+ {
360
+ if (isset($sortbyObj->extend)) {
361
+ if (count($sortbyObj->extend)) {
362
+ foreach ($sortbyObj->extend as $optionKey => $optionValue) {
363
+ $_REQUEST['order'] = $optionKey;
364
+ break;
365
+ }
366
+ } else {
367
+ $this->is_active = true;
368
  }
369
+ } else if (isset($sortbyObj->override) && $storeConfig->default_search_sort) {
370
+ $_REQUEST["order"] = $storeConfig->default_search_sort;
371
+ } else if (!isset($sortbyObj->override) && !isset($sortbyObj->extend)) {
372
+ $_REQUEST["order"] = "takeover_mode";
 
 
373
  }
374
+ // } else if(isset($_SESSION['catalog']) && isset($_SESSION['catalog']['sort_order'])){
375
+ // // If prod list page, use the session value
376
+ // // This case although seems useless
377
+ // $_REQUEST["order"] = $_SESSION['catalog']['sort_order'];
378
+ // }
379
  }
380
 
381
  /**
382
  * @param $sortbyObj
383
  * @return array
384
  */
385
+ private function _getOptionsAddedByChoice($sortbyObj)
386
+ {
387
  $options = array();
388
 
389
+ if (isset($sortbyObj->override)) {
390
  // Search case, override all system options with choice
391
+ foreach ($sortbyObj->override as $key => $caiOption)
 
 
392
  $options[] = $key;
393
+ } elseif (isset($sortbyObj->extend)) {
394
+ foreach ($sortbyObj->extend as $key => $option)
395
  $options[] = $key;
396
  }
397
 
398
  return $options;
399
  }
400
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
401
  /**
402
  * Checks if debug mode is enabled.
403
  *
410
  return array_key_exists('enable_debug_mode', $config) && $config['enable_debug_mode'];
411
  }
412
 
 
 
 
 
 
 
 
 
 
 
 
 
413
  /**
414
  * Forces error display.
415
+ * Used in Abstract.php model's search function
416
  *
417
  * @param string $error
418
  */
422
  ->addError($error)->getGroupedHtml();
423
  }
424
 
425
+ }