choiceai - Version 1.1.1

Version Notes

Choice AI plugin

Download this release

Release Info

Developer BrainSINS
Extension choiceai
Version 1.1.1
Comparing to
See all releases


Code changes from version 1.0.12 to 1.1.1

Files changed (55) hide show
  1. app/code/community/ChoiceAI/Personalisation/Block/Base/Script.php +10 -4
  2. app/code/community/ChoiceAI/Personalisation/Block/Event/Analytics/Listpage.php +6 -3
  3. app/code/community/ChoiceAI/Personalisation/Block/Event/Analytics/Search.php +6 -3
  4. app/code/community/ChoiceAI/Personalisation/Block/Event/Catalog/Product/View.php +45 -39
  5. app/code/community/ChoiceAI/Personalisation/Block/Event/Checkout/Cart/Index.php +37 -32
  6. app/code/community/ChoiceAI/Personalisation/Block/Event/Checkout/Onepage/Success.php +119 -118
  7. app/code/community/ChoiceAI/Personalisation/Block/Searchpage/Addparam.php +6 -3
  8. app/code/community/ChoiceAI/Personalisation/Helper/Data.php +23 -10
  9. app/code/community/ChoiceAI/Personalisation/Model/Observer.php +109 -110
  10. app/code/community/ChoiceAI/Personalisation/controllers/ApiController.php +1030 -305
  11. app/code/community/ChoiceAI/Personalisation/etc/config.xml +1 -1
  12. app/code/community/ChoiceAI/Personalisation/sql/personalisation_setup/install-1.0.12.php +0 -31
  13. app/code/community/ChoiceAI/Personalisation/sql/personalisation_setup/install-1.1.1.php +37 -0
  14. app/code/local/ChoiceAI/Search/Block/Catalog/Layer/Filter/Attribute.php +46 -45
  15. app/code/local/ChoiceAI/Search/Block/Catalog/Layer/Filter/Boolean.php +21 -20
  16. app/code/local/ChoiceAI/Search/Block/Catalog/Layer/Filter/Category.php +34 -33
  17. app/code/local/ChoiceAI/Search/Block/Catalog/Layer/Filter/Decimal.php +46 -45
  18. app/code/local/ChoiceAI/Search/Block/Catalog/Layer/Filter/Price.php +50 -50
  19. app/code/local/ChoiceAI/Search/Block/Catalog/Layer/View.php +112 -109
  20. app/code/local/ChoiceAI/Search/Block/Catalog/Product/List.php +1 -0
  21. app/code/local/ChoiceAI/Search/Block/Catalog/Product/List/Toolbar.php +10 -7
  22. app/code/local/ChoiceAI/Search/Block/Catalogsearch/Enterprise/Layer.php +4 -3
  23. app/code/local/ChoiceAI/Search/Block/Catalogsearch/Layer.php +116 -115
  24. app/code/local/ChoiceAI/Search/Block/Catalogsearch/Layer/Filter/Attribute.php +47 -46
  25. app/code/local/ChoiceAI/Search/Block/Catalogsearch/Result.php +5 -3
  26. app/code/local/ChoiceAI/Search/Helper/Choiceaisearch.php +16 -15
  27. app/code/local/ChoiceAI/Search/Helper/Data.php +450 -425
  28. app/code/local/ChoiceAI/Search/Helper/Productimg.php +5 -5
  29. app/code/local/ChoiceAI/Search/Model/Catalog/Category.php +8 -8
  30. app/code/local/ChoiceAI/Search/Model/Catalog/Config.php +125 -121
  31. app/code/local/ChoiceAI/Search/Model/Catalog/Layer.php +38 -37
  32. app/code/local/ChoiceAI/Search/Model/Catalog/Layer/Filter/Attribute.php +173 -168
  33. app/code/local/ChoiceAI/Search/Model/Catalog/Layer/Filter/Boolean.php +43 -41
  34. app/code/local/ChoiceAI/Search/Model/Catalog/Layer/Filter/Category.php +138 -132
  35. app/code/local/ChoiceAI/Search/Model/Catalog/Layer/Filter/Decimal.php +191 -190
  36. app/code/local/ChoiceAI/Search/Model/Catalog/Layer/Filter/Price.php +162 -154
  37. app/code/local/ChoiceAI/Search/Model/Catalog/Product/Url.php +4 -4
  38. app/code/local/ChoiceAI/Search/Model/Catalogsearch/Layer.php +31 -31
  39. app/code/local/ChoiceAI/Search/Model/Catalogsearch/Layer/Filter/Attribute.php +17 -16
  40. app/code/local/ChoiceAI/Search/Model/Resource/Catalog/Product/Collection.php +383 -373
  41. app/code/local/ChoiceAI/Search/Model/Resource/Engine/Abstract.php +13 -11
  42. app/code/local/ChoiceAI/Search/Model/Resource/Engine/Choiceaisearch.php +288 -276
  43. app/code/local/ChoiceAI/Search/Model/Resource/Engine/Choiceaisearch/Client.php +5 -4
  44. app/code/local/ChoiceAI/Search/etc/config.xml +2 -2
  45. app/code/local/ChoiceAI/Searchcore/Helper/Constants.php +6 -5
  46. app/code/local/ChoiceAI/Searchcore/Helper/Data.php +37 -22
  47. app/code/local/ChoiceAI/Searchcore/etc/config.xml +1 -1
  48. app/design/frontend/base/default/template/choiceai/personalisation/analytics/listpage.phtml +20 -23
  49. app/design/frontend/base/default/template/choiceai/personalisation/analytics/search.phtml +20 -23
  50. app/design/frontend/base/default/template/choiceai/personalisation/base/addparam.phtml +3 -24
  51. app/design/frontend/base/default/template/choiceai/personalisation/base/script.phtml +39 -39
  52. app/design/frontend/default/default/template/choiceai/personalisation/base/script.phtml +38 -38
  53. lib/ChoiceAI/Client.php +312 -291
  54. lib/ChoiceAI/Service.php +174 -181
  55. package.xml +5 -5
app/code/community/ChoiceAI/Personalisation/Block/Base/Script.php CHANGED
@@ -6,25 +6,31 @@
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_Base_Script extends Mage_Core_Block_Template {
 
10
 
11
- protected function _construct() {
 
12
  parent::_construct();
13
  $this->setTemplate('choiceai/personalisation/base/script.phtml');
14
  }
15
 
16
- protected function _toHtml() {
 
17
  if (!$this->helper('choiceai_personalisation')->isModuleOutputEnabled()) {
18
  return '';
19
  }
 
20
  return parent::_toHtml();
21
  }
22
 
23
- public function getUser() {
 
24
  if (Mage::getSingleton('customer/session')->isLoggedIn()) {
25
  $user = Mage::getSingleton('customer/session')->getCustomer();
26
  return $user;
27
  }
 
28
  return null;
29
  }
30
 
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_Base_Script extends Mage_Core_Block_Template
10
+ {
11
 
12
+ protected function _construct()
13
+ {
14
  parent::_construct();
15
  $this->setTemplate('choiceai/personalisation/base/script.phtml');
16
  }
17
 
18
+ protected function _toHtml()
19
+ {
20
  if (!$this->helper('choiceai_personalisation')->isModuleOutputEnabled()) {
21
  return '';
22
  }
23
+
24
  return parent::_toHtml();
25
  }
26
 
27
+ public function getUser()
28
+ {
29
  if (Mage::getSingleton('customer/session')->isLoggedIn()) {
30
  $user = Mage::getSingleton('customer/session')->getCustomer();
31
  return $user;
32
  }
33
+
34
  return null;
35
  }
36
 
app/code/community/ChoiceAI/Personalisation/Block/Event/Analytics/Listpage.php CHANGED
@@ -12,14 +12,17 @@
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
 
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
 
18
+ protected function _construct()
19
+ {
20
  parent::_construct();
21
  $this->setTemplate('choiceai/personalisation/analytics/listpage.phtml');
22
  }
23
 
24
+ public function engineStatus()
25
+ {
26
  return Mage::helper('choiceai_search')->isActiveEngine();
27
  }
28
 
app/code/community/ChoiceAI/Personalisation/Block/Event/Analytics/Search.php CHANGED
@@ -12,14 +12,17 @@
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
 
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
 
18
+ protected function _construct()
19
+ {
20
  parent::_construct();
21
  $this->setTemplate('choiceai/personalisation/analytics/search.phtml');
22
  }
23
 
24
+ public function engineStatus()
25
+ {
26
  return Mage::helper('choiceai_search')->isActiveEngine();
27
  }
28
 
app/code/community/ChoiceAI/Personalisation/Block/Event/Catalog/Product/View.php CHANGED
@@ -1,39 +1,45 @@
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_Event_Catalog_Product_View extends Mage_Core_Block_Template {
10
-
11
- protected function _construct() {
12
- parent::_construct();
13
- $this->setTemplate('choiceai/personalisation/event/catalog/product/view.phtml');
14
- }
15
-
16
- public function getCurrentProduct() {
17
- return Mage::registry('current_product');
18
- }
19
-
20
- public function getAssociatedProducts($_product) {
21
-
22
- if($_product->getTypeId() == "configurable") {
23
- $conf = Mage::getModel('catalog/product_type_configurable')->setProduct($_product);
24
- $simple_collection = $conf->getUsedProductCollection()->addAttributeToSelect(array('id', 'sku', 'price'))->addFilterByRequiredOptions();
25
- return $simple_collection;
26
- } else {
27
- return array();
28
- }
29
-
30
- }
31
-
32
- protected function _toHtml() {
33
- if (!$this->helper('choiceai_personalisation')->isModuleOutputEnabled()) {
34
- return '';
35
- }
36
- return parent::_toHtml();
37
- }
38
-
39
- }
 
 
 
 
 
 
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_Event_Catalog_Product_View extends Mage_Core_Block_Template
10
+ {
11
+
12
+ protected function _construct()
13
+ {
14
+ parent::_construct();
15
+ $this->setTemplate('choiceai/personalisation/event/catalog/product/view.phtml');
16
+ }
17
+
18
+ public function getCurrentProduct()
19
+ {
20
+ return Mage::registry('current_product');
21
+ }
22
+
23
+ public function getAssociatedProducts($_product)
24
+ {
25
+
26
+ if ($_product->getTypeId() == "configurable") {
27
+ $conf = Mage::getModel('catalog/product_type_configurable')->setProduct($_product);
28
+ return $conf->getUsedProductCollection()->addAttributeToSelect(array('id', 'sku', 'price'))
29
+ ->addFilterByRequiredOptions();
30
+ } else {
31
+ return array();
32
+ }
33
+
34
+ }
35
+
36
+ protected function _toHtml()
37
+ {
38
+ if (!$this->helper('choiceai_personalisation')->isModuleOutputEnabled()) {
39
+ return '';
40
+ }
41
+
42
+ return parent::_toHtml();
43
+ }
44
+
45
+ }
app/code/community/ChoiceAI/Personalisation/Block/Event/Checkout/Cart/Index.php CHANGED
@@ -1,32 +1,37 @@
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_Event_Checkout_Cart_Index extends Mage_Core_Block_Template {
10
-
11
- protected function _construct() {
12
- parent::_construct();
13
- $this->setTemplate('choiceai/personalisation/event/checkout/cart/index.phtml');
14
- }
15
-
16
- public function getProductToShoppingCart() {
17
- if (($product = Mage::getModel('core/session')->getProductToShoppingCart())) {
18
- Mage::getModel('core/session')->unsProductToShoppingCart();
19
- return $product;
20
- }
21
-
22
- return null;
23
- }
24
-
25
- protected function _toHtml() {
26
- if (!$this->helper('choiceai_personalisation')->isModuleOutputEnabled()) {
27
- return '';
28
- }
29
- return parent::_toHtml();
30
- }
31
-
32
- }
 
 
 
 
 
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_Event_Checkout_Cart_Index extends Mage_Core_Block_Template
10
+ {
11
+
12
+ protected function _construct()
13
+ {
14
+ parent::_construct();
15
+ $this->setTemplate('choiceai/personalisation/event/checkout/cart/index.phtml');
16
+ }
17
+
18
+ public function getProductToShoppingCart()
19
+ {
20
+ if (($product = Mage::getModel('core/session')->getProductToShoppingCart())) {
21
+ Mage::getModel('core/session')->unsProductToShoppingCart();
22
+ return $product;
23
+ }
24
+
25
+ return null;
26
+ }
27
+
28
+ protected function _toHtml()
29
+ {
30
+ if (!$this->helper('choiceai_personalisation')->isModuleOutputEnabled()) {
31
+ return '';
32
+ }
33
+
34
+ return parent::_toHtml();
35
+ }
36
+
37
+ }
app/code/community/ChoiceAI/Personalisation/Block/Event/Checkout/Onepage/Success.php CHANGED
@@ -1,118 +1,119 @@
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_Event_Checkout_Onepage_Success extends Mage_Core_Block_Template {
10
-
11
- protected function _construct() {
12
- parent::_construct();
13
- $this->setTemplate('choiceai/personalisation/event/checkout/onepage/success.phtml');
14
- }
15
-
16
- public function getOrderInfo() {
17
- try {
18
- $lastOrderId = Mage::getSingleton('checkout/session')->getLastOrderId();
19
-
20
- $order = Mage::getSingleton('sales/order');
21
- $order->load($lastOrderId);
22
-
23
-
24
- if ($order && $order->getId()) {
25
-
26
- $orderInfo['items'] = array();
27
-
28
- $orderedItems = $order->getAllItems();
29
-
30
- foreach($orderedItems as $item) {
31
-
32
- if($item->getProductType() == "bundle") {
33
-
34
- $orderInfo['items'][$item->getItemId()] = array(
35
- 'id' => $item->getProductId(),
36
- 'parentId' => '',
37
- 'sku' => $item->getSku(),
38
- 'qty' => $item->getQtyOrdered(),
39
- 'price' => $item->getPrice(),
40
- 'bundle' => array()
41
- );
42
-
43
- } else if($item->getProductType() != "configurable") {
44
-
45
- if($orderInfo['items'][$item->getParentItemId()] != null) {
46
- $bundleItems = $orderInfo['items'][$item->getParentItemId()]['bundle'];
47
- $bundleItem = array(
48
- 'pid' => $item->getProductId(),
49
- 'sku' => $item->getSku(),
50
- 'qty' => $item->getQtyOrdered(),
51
- 'price' => $item->getPrice()
52
- );
53
- $bundleItems[] = $bundleItem;
54
- $orderInfo['items'][$item->getParentItemId()]['bundle'] = $bundleItems;
55
-
56
- } else {
57
-
58
- $parentId = '';
59
- $parentIds = Mage::getModel('catalog/product_type_configurable')->getParentIdsByChild($item->getProductId());
60
- if($parentIds != null && count($parentIds) > 0) {
61
- $parentId = $parentIds[0];
62
- }
63
- $orderInfo['items'][] = array(
64
- 'id' => $item->getProductId(),
65
- 'parentId' => $parentId,
66
- 'sku' => $item->getSku(),
67
- 'qty' => $item->getQtyOrdered(),
68
- 'price' => $item->getPrice(),
69
- 'bundle' => array()
70
- );
71
-
72
-
73
- }
74
-
75
- }
76
-
77
- }
78
-
79
- $orderInfo['orderId'] = $order->getIncrementId();
80
- $orderInfo['email'] = $order->getCustomerEmail();
81
- $orderInfo['createdAt'] = $order->getCreatedAt();
82
-
83
- $currency = $order->getOrderCurrency();
84
- if (is_object($currency)) {
85
- $orderInfo['currency'] = $currency->getCurrencyCode();
86
- }
87
- $paymentMethod = $order->getPayment()->getMethodInstance()->getTitle();
88
- $orderInfo['paymentMethod'] = $paymentMethod;
89
-
90
- return $orderInfo;
91
- }
92
- } catch (Exception $e) {}
93
-
94
-
95
- return null;
96
- }
97
-
98
- protected function _toHtml() {
99
- if (!$this->helper('choiceai_personalisation')->isModuleOutputEnabled() || !$this->isOrderConfirmation()) {
100
- return '';
101
- }
102
- return parent::_toHtml();
103
- }
104
-
105
- protected function isOrderConfirmation() {
106
- return strpos($this->_getRouteName(), 'checkout') !== false
107
- && $this->_getActionName() == 'success';
108
- }
109
-
110
- protected function _getRouteName() {
111
- return $this->getRequest()->getRequestedRouteName();
112
- }
113
-
114
- protected function _getActionName() {
115
- return $this->getRequest()->getRequestedActionName();
116
- }
117
-
118
- }
 
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_Event_Checkout_Onepage_Success extends Mage_Core_Block_Template
10
+ {
11
+
12
+ protected function _construct()
13
+ {
14
+ parent::_construct();
15
+ $this->setTemplate('choiceai/personalisation/event/checkout/onepage/success.phtml');
16
+ }
17
+
18
+ public function getOrderInfo()
19
+ {
20
+ try {
21
+ $lastOrderId = Mage::getSingleton('checkout/session')->getLastOrderId();
22
+
23
+ $order = Mage::getSingleton('sales/order');
24
+ $order->load($lastOrderId);
25
+
26
+
27
+ if ($order && $order->getId()) {
28
+ $orderInfo['items'] = array();
29
+
30
+ $orderedItems = $order->getAllItems();
31
+
32
+ foreach ($orderedItems as $item) {
33
+ if ($item->getProductType() == "bundle") {
34
+ $orderInfo['items'][$item->getItemId()] = array(
35
+ 'id' => $item->getProductId(),
36
+ 'parentId' => '',
37
+ 'sku' => $item->getSku(),
38
+ 'qty' => $item->getQtyOrdered(),
39
+ 'price' => $item->getPrice(),
40
+ 'bundle' => array()
41
+ );
42
+ } else if ($item->getProductType() != "configurable") {
43
+ if ($orderInfo['items'][$item->getParentItemId()] != null) {
44
+ $bundleItems = $orderInfo['items'][$item->getParentItemId()]['bundle'];
45
+ $bundleItem = array(
46
+ 'pid' => $item->getProductId(),
47
+ 'sku' => $item->getSku(),
48
+ 'qty' => $item->getQtyOrdered(),
49
+ 'price' => $item->getPrice()
50
+ );
51
+ $bundleItems[] = $bundleItem;
52
+ $orderInfo['items'][$item->getParentItemId()]['bundle'] = $bundleItems;
53
+ } else {
54
+ $parentId = '';
55
+ $parentIds = Mage::getModel('catalog/product_type_configurable')->
56
+ getParentIdsByChild($item->getProductId());
57
+ if ($parentIds != null && !empty($parentIds)) {
58
+ $parentId = $parentIds[0];
59
+ }
60
+
61
+ $orderInfo['items'][] = array(
62
+ 'id' => $item->getProductId(),
63
+ 'parentId' => $parentId,
64
+ 'sku' => $item->getSku(),
65
+ 'qty' => $item->getQtyOrdered(),
66
+ 'price' => $item->getPrice(),
67
+ 'bundle' => array()
68
+ );
69
+ }
70
+ }
71
+ }
72
+
73
+ $orderInfo['orderId'] = $order->getIncrementId();
74
+ $orderInfo['email'] = $order->getCustomerEmail();
75
+ $orderInfo['createdAt'] = $order->getCreatedAt();
76
+
77
+ $currency = $order->getOrderCurrency();
78
+ if (is_object($currency)) {
79
+ $orderInfo['currency'] = $currency->getCurrencyCode();
80
+ }
81
+
82
+ $paymentMethod = $order->getPayment()->getMethodInstance()->getTitle();
83
+ $orderInfo['paymentMethod'] = $paymentMethod;
84
+
85
+ return $orderInfo;
86
+ }
87
+ } catch (Exception $e) {
88
+ return null;
89
+ }
90
+
91
+ return null;
92
+ }
93
+
94
+ protected function _toHtml()
95
+ {
96
+ if (!$this->helper('choiceai_personalisation')->isModuleOutputEnabled() || !$this->isOrderConfirmation()) {
97
+ return '';
98
+ }
99
+
100
+ return parent::_toHtml();
101
+ }
102
+
103
+ protected function isOrderConfirmation()
104
+ {
105
+ return strpos($this->_getRouteName(), 'checkout') !== false
106
+ && $this->_getActionName() == 'success';
107
+ }
108
+
109
+ protected function _getRouteName()
110
+ {
111
+ return $this->getRequest()->getRequestedRouteName();
112
+ }
113
+
114
+ protected function _getActionName()
115
+ {
116
+ return $this->getRequest()->getRequestedActionName();
117
+ }
118
+
119
+ }
app/code/community/ChoiceAI/Personalisation/Block/Searchpage/Addparam.php CHANGED
@@ -6,14 +6,17 @@
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
 
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
 
12
+ protected function _construct()
13
+ {
14
  parent::_construct();
15
  $this->setTemplate('choiceai/personalisation/base/addparam.phtml');
16
  }
17
 
18
+ public function engineStatus()
19
+ {
20
  return Mage::helper('choiceai_search')->isActiveEngine();
21
  }
22
 
app/code/community/ChoiceAI/Personalisation/Helper/Data.php CHANGED
@@ -6,12 +6,14 @@
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_Helper_Data extends Mage_Core_Helper_Data {
 
10
 
11
  const CONFIG_ACTIVE = 'choiceai_personalisation/settings/active';
12
  const CONFIG_API_KEY = 'choiceai_personalisation/settings/api_key';
13
 
14
- public function isModuleEnabled($moduleName = null) {
 
15
  if (Mage::getStoreConfig(self::CONFIG_ACTIVE) == '0') {
16
  return false;
17
  }
@@ -19,20 +21,31 @@ class ChoiceAI_Personalisation_Helper_Data extends Mage_Core_Helper_Data {
19
  return parent::isModuleEnabled($moduleName = null);
20
  }
21
 
22
- public function getBaseScript($store = null) {
 
23
 
24
- $base_script = "";
25
 
26
  try {
27
-
28
- $base_script = "\n<!-- ChoiceAI Script begins -->\n<script type='text/javascript'>!function(){function t(){if(!window.CAISDK){var t=document.createElement('script'),n='beaconhttp.choice.ai';t.type='text/javascript',t.async=!0,'https:'==location.protocol&&(n='d3caf2da6t944y.cloudfront.net'),t.src='//'+n+'/site/ethno/ORG_HANDLE/choice.js';var e=document.getElementsByTagName('script')[0];e.parentNode.insertBefore(t,e)}}window.CAISDK&&window.CAISDK.reinit&&window.CAISDK.reinit(),!window.CAISDK && t()}();</script>\n<!-- ChoiceAI Script ends -->\n";
29
- $base_script = str_replace("ORG_HANDLE", split("_", Mage::getStoreConfig(self::CONFIG_API_KEY, $store))[0], $base_script);
30
-
 
 
 
 
 
 
 
 
 
 
31
  } catch (Exception $e) {
32
- $base_script = "";
33
  }
34
 
35
- return $base_script;
36
 
37
  }
38
 
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_Helper_Data extends Mage_Core_Helper_Data
10
+ {
11
 
12
  const CONFIG_ACTIVE = 'choiceai_personalisation/settings/active';
13
  const CONFIG_API_KEY = 'choiceai_personalisation/settings/api_key';
14
 
15
+ public function isModuleEnabled($moduleName = null)
16
+ {
17
  if (Mage::getStoreConfig(self::CONFIG_ACTIVE) == '0') {
18
  return false;
19
  }
21
  return parent::isModuleEnabled($moduleName = null);
22
  }
23
 
24
+ public function getBaseScript($store = null)
25
+ {
26
 
27
+ // $baseScript = "";
28
 
29
  try {
30
+ $baseScript = "\n<!-- ChoiceAI Script begins -->\n<script type='text/javascript'>!function(){function t(){
31
+ if(!window.CAIBASE){var t=document.createElement('script'),n='beaconhttp.choice.ai';
32
+ t.type='text/javascript',t.async=!0,
33
+ 'https:'==location.protocol&&(n='d3caf2da6t944y.cloudfront.net'),t.src='//'+n+'/site/ethno/ORG_HANDLE/choice.js';
34
+ var e=document.getElementsByTagName('script')[0];e.parentNode.insertBefore(t,e)}}
35
+ window.CAIBASE&&window.CAIBASE.reinit&&window.CAIBASE.reinit(),!window.CAIBASE && t()}();</script>
36
+ \n<!-- ChoiceAI Script ends -->\n";
37
+ $baseScript = str_replace(
38
+ "ORG_HANDLE", explode(
39
+ "_", Mage::getStoreConfig(
40
+ self::CONFIG_API_KEY, $store
41
+ )
42
+ )[0], $baseScript
43
+ );
44
  } catch (Exception $e) {
45
+ $baseScript = "";
46
  }
47
 
48
+ return $baseScript;
49
 
50
  }
51
 
app/code/community/ChoiceAI/Personalisation/Model/Observer.php CHANGED
@@ -1,124 +1,123 @@
1
  <?php
2
 
3
- class ChoiceAI_Personalisation_Model_Observer {
4
-
5
- const CONFIG_ACTIVE = 'choiceai_personalisation/settings/active';
6
- const CONFIG_API_KEY = 'choiceai_personalisation/settings/api_key';
7
-
8
- public function onSaveSettings($observer) {
9
-
10
- $enabled = Mage::getStoreConfig(self::CONFIG_ACTIVE);
11
- $api_key = Mage::getStoreConfig(self::CONFIG_API_KEY);
12
- $store_url = Mage::getBaseUrl();
13
- $magento_version = Mage::getVersion();
14
-
15
- $error_message = "";
16
-
17
- try {
18
-
19
- $roles = Mage::getModel('api/roles')->getCollection()->addFieldToFilter('role_name', 'ChoiceAI');
20
-
21
- if ($roles && sizeof($roles) > 0) {
22
-
23
- $api_role = $roles->getFirstItem();
24
-
25
- } else {
26
-
27
- $api_role = Mage::getModel('api/roles')
28
- ->setName('ChoiceAI')
29
- ->setPid("ChoiceAI")
30
- ->setRoleType('G')
31
- ->save();
32
- }
33
-
34
- Mage::getModel('api/rules')
35
- ->setRoleId($api_role->getId())
36
- ->setResources(array('all'))
37
- ->saveRel();
38
-
39
- $users = Mage::getModel('api/user')->getCollection()->addFieldToFilter('email', 'magento@choice.ai');
40
- if ($users && sizeof($users) > 0) {
41
-
42
- $api_user = $users->getFirstItem();
43
-
44
- } else {
45
-
46
- $api_user = Mage::getModel('api/user');
47
- $api_user->setData(array(
48
- 'username' => 'choiceai',
49
- 'firstname' => 'ChoiceAI',
50
- 'lastname' => 'Personalisation',
51
- 'email' => 'magento@choice.ai',
52
- 'api_key' => $api_key,
53
- 'api_key_confirmation' => $api_key,
54
- 'is_active' => 1,
55
- 'user_roles' => '',
56
- 'assigned_user_role' => '',
57
- 'role_name' => '',
58
- 'roles' => array($api_role->getId())
59
- ));
60
-
61
- $api_user->save()->load($api_user->getId());
62
-
63
- }
 
 
 
64
 
65
- $api_user->setRoleIds(array($api_role->getId()))
66
- ->setRoleUserId($api_user->getUserId())
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);
 
74
 
75
- }
76
-
77
- public function logCartAdd($observer) {
78
 
79
- if (!$observer->getQuoteItem()->getProduct()->getId()) {
80
- return;
81
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82
 
83
- $product = $observer->getProduct();
84
- $id = $observer->getQuoteItem()->getProduct()->getId();
85
- $bundle = array();
86
-
87
- if($product->getTypeId() == 'bundle') {
88
-
89
- $id = $product->getId();
90
- $optionCollection = $product->getTypeInstance()->getOptionsCollection();
91
- $selectionCollection = $product->getTypeInstance()->getSelectionsCollection($product->getTypeInstance()->getOptionsIds());
92
- $options = $optionCollection->appendSelections($selectionCollection);
93
- foreach( $options as $option )
94
- {
95
- $_selections = $option->getSelections();
96
- foreach( $_selections as $selection )
97
- {
98
- $bundleItem = array();
99
- $bundleItem['pid'] = $selection->getId();
100
- $bundleItem['sku'] = $selection->getSku();
101
- $bundleItem['price'] = $selection->getPrice();
102
- $bundle[] = $bundleItem;
103
  }
104
- }
105
 
106
- }
 
 
 
 
 
 
 
 
107
 
108
- $parentId = '';
109
- $parentIds = Mage::getModel('catalog/product_type_configurable')->getParentIdsByChild($id);
110
- if($parentIds != null && count($parentIds) > 0) {
111
- $parentId = $parentIds[0];
112
  }
113
- Mage::getModel('core/session')->setProductToShoppingCart(
114
- array(
115
- 'id' => $id,
116
- 'sku' => $product->getSku(),
117
- 'parentId' => $parentId,
118
- 'qty' => $product->getQty(),
119
- 'bundle' => $bundle
120
- )
121
- );
122
-
123
- }
124
  }
1
  <?php
2
 
3
+ class ChoiceAI_Personalisation_Model_Observer
4
+ {
5
+
6
+ const CONFIG_ACTIVE = 'choiceai_personalisation/settings/active';
7
+ const CONFIG_API_KEY = 'choiceai_personalisation/settings/api_key';
8
+
9
+ public function onSaveSettings($observer)
10
+ {
11
+
12
+ $enabled = Mage::getStoreConfig(self::CONFIG_ACTIVE);
13
+ $apiKey = Mage::getStoreConfig(self::CONFIG_API_KEY);
14
+ $errorMessage = "";
15
+
16
+ try {
17
+ $roles = Mage::getModel('api/roles')->getCollection()->addFieldToFilter('role_name', 'ChoiceAI');
18
+
19
+ if ($roles && !empty($roles)) {
20
+ $apiRole = $roles->getFirstItem();
21
+ } else {
22
+ $apiRole = Mage::getModel('api/roles')
23
+ ->setName('ChoiceAI')
24
+ ->setPid("ChoiceAI")
25
+ ->setRoleType('G')
26
+ ->save();
27
+ }
28
+
29
+ Mage::getModel('api/rules')
30
+ ->setRoleId($apiRole->getId())
31
+ ->setResources(array('all'))
32
+ ->saveRel();
33
+
34
+ $users = Mage::getModel('api/user')->getCollection()->addFieldToFilter(
35
+ 'email',
36
+ 'magento@choice.ai'
37
+ );
38
+ if ($users && !empty($users)) {
39
+ $apiUser = $users->getFirstItem();
40
+ } else {
41
+ $apiUser = Mage::getModel('api/user');
42
+ $apiUser->setData(
43
+ array(
44
+ 'username' => 'choiceai',
45
+ 'firstname' => 'ChoiceAI',
46
+ 'lastname' => 'Personalisation',
47
+ 'email' => 'magento@choice.ai',
48
+ 'api_key' => $apiKey,
49
+ 'api_key_confirmation' => $apiKey,
50
+ 'is_active' => 1,
51
+ 'user_roles' => '',
52
+ 'assigned_user_role' => '',
53
+ 'role_name' => '',
54
+ 'roles' => array($apiRole->getId())
55
+ )
56
+ );
57
+
58
+ $apiUser->save()->load($apiUser->getId());
59
+ }
60
+
61
+ $apiUser->setRoleIds(array($apiRole->getId()))
62
+ ->setRoleUserId($apiUser->getUserId())
63
+ ->saveRelations();
64
+ } catch (Exception $e) {
65
+ $errorMessage = $e->getMessage();
66
+ }
67
 
68
+ file_get_contents(
69
+ "https://app.choice.ai/stats/magentoinstall?enabled=" . $enabled . "&api_key="
70
+ . $apiKey . "&error=" . $errorMessage
71
+ );
72
 
 
 
73
  }
74
 
75
+ public function logCartAdd($observer)
76
+ {
77
 
78
+ if (!$observer->getQuoteItem()->getProduct()->getId()) {
79
+ return;
80
+ }
81
 
82
+ $product = $observer->getProduct();
83
+ $id = $observer->getQuoteItem()->getProduct()->getId();
84
+ $bundle = array();
85
+
86
+ if ($product->getTypeId() == 'bundle') {
87
+ $id = $product->getId();
88
+ $optionCollection = $product->getTypeInstance()->getOptionsCollection();
89
+ $selectionCollection = $product->getTypeInstance()->getSelectionsCollection(
90
+ $product->getTypeInstance()
91
+ ->getOptionsIds()
92
+ );
93
+ $options = $optionCollection->appendSelections($selectionCollection);
94
+ foreach ($options as $option) {
95
+ $_selections = $option->getSelections();
96
+ foreach ($_selections as $selection) {
97
+ $bundleItem = array();
98
+ $bundleItem['pid'] = $selection->getId();
99
+ $bundleItem['sku'] = $selection->getSku();
100
+ $bundleItem['price'] = $selection->getPrice();
101
+ $bundle[] = $bundleItem;
102
+ }
103
+ }
104
+ }
105
 
106
+ $parentId = '';
107
+ $parentIds = Mage::getModel('catalog/product_type_configurable')->getParentIdsByChild($id);
108
+ if ($parentIds != null && !empty($parentIds)) {
109
+ $parentId = $parentIds[0];
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
110
  }
 
111
 
112
+ Mage::getModel('core/session')->setProductToShoppingCart(
113
+ array(
114
+ 'id' => $id,
115
+ 'sku' => $product->getSku(),
116
+ 'parentId' => $parentId,
117
+ 'qty' => $product->getQty(),
118
+ 'bundle' => $bundle
119
+ )
120
+ );
121
 
 
 
 
 
122
  }
 
 
 
 
 
 
 
 
 
 
 
123
  }
app/code/community/ChoiceAI/Personalisation/controllers/ApiController.php CHANGED
@@ -7,21 +7,28 @@
7
  * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
8
  */
9
 
10
- class ChoiceAI_Personalisation_ApiController extends Mage_Core_Controller_Front_Action {
 
11
 
12
  const CONFIG_API_KEY = 'choiceai_personalisation/settings/api_key';
13
  const CONFIG_KEY = 'choiceai_personalisation/settings/config';
14
- const API_VERSION = 3;
15
 
16
- public function _authorise() {
 
17
 
18
- $API_KEY = Mage::getStoreConfig(self::CONFIG_API_KEY);
19
 
20
  // Check for api access
21
- if(!$API_KEY && strlen($API_KEY) === 0) {
22
  // Api access disabled
23
  $this->getResponse()
24
- ->setBody(json_encode(array('status' => 'error', 'message' => 'API access disabled', 'version' => self::API_VERSION)))
 
 
 
 
 
25
  ->setHttpResponseCode(403)
26
  ->setHeader('Content-type', 'application/json', true);
27
  return false;
@@ -38,16 +45,26 @@ class ChoiceAI_Personalisation_ApiController extends Mage_Core_Controller_Front_
38
  Mage::log('Unable to extract authorization header from request', null, 'choiceai.log');
39
  // Internal server error
40
  $this->getResponse()
41
- ->setBody(json_encode(array('status' => 'error', 'message' => 'Internal server error, Authorization header not found', 'version' => self::API_VERSION)))
 
 
 
 
 
42
  ->setHttpResponseCode(500)
43
  ->setHeader('Content-type', 'application/json', true);
44
  return false;
45
  }
46
 
47
- if(trim($authHeader) !== trim($API_KEY)) {
48
  // Api access denied
49
  $this->getResponse()
50
- ->setBody(json_encode(array('status' => 'error', 'message' => 'Api access denied', 'version' => self::API_VERSION)))
 
 
 
 
 
51
  ->setHttpResponseCode(401)
52
  ->setHeader('Content-type', 'application/json', true);
53
  return false;
@@ -57,30 +74,134 @@ class ChoiceAI_Personalisation_ApiController extends Mage_Core_Controller_Front_
57
 
58
  }
59
 
60
- public function configAction() {
 
61
  try {
62
- if(!$this->_authorise()) {
63
  return $this;
64
  }
65
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
  $responseObj = array();
67
 
68
- if ($_SERVER["REQUEST_METHOD"] == "GET") {
69
- $STORE_CONFIG = Mage::getStoreConfig(self::CONFIG_KEY);
70
  $responseObj["status"] = "ok";
71
- $responseObj["config"] = json_decode($STORE_CONFIG);
72
- } else if ($_SERVER["REQUEST_METHOD"] == "PUT") {
73
- $store_config = $this->getRequest()->getParam('config');
74
- if($store_config ==""){
75
  $input = file_get_contents('php://input');
76
  $input = utf8_encode($input);
77
- $store_config = json_encode(json_decode($input)->config);
78
  }
79
- $store_config_json = json_decode($store_config);
80
- if ($store_config_json == NULL) {
81
- throw new Exception();
 
82
  }
83
- Mage::getModel('core/config')->saveConfig(self::CONFIG_KEY, $store_config);
 
84
  Mage::app()->getStore()->resetConfig();
85
  $responseObj["status"] = "ok";
86
  } else {
@@ -88,24 +209,79 @@ class ChoiceAI_Personalisation_ApiController extends Mage_Core_Controller_Front_
88
  $responseObj['message'] = 'Invalid request';
89
  }
90
 
91
- if(isset($_SERVER['REQUEST_SCHEME']) && isset($_SERVER['SERVER_NAME'])){
92
- $storeUrl = $_SERVER['REQUEST_SCHEME'] ."://". $_SERVER['SERVER_NAME'];
93
- } else{
94
  $storeUrl = Mage::getBaseUrl();
95
  }
 
96
  $responseObj['store_url'] = $storeUrl;
97
- $responseObj['installed'] = '1';
98
  $responseObj['api_version'] = self::API_VERSION;
99
- $responseObj['plugin_version'] = (string)Mage::getConfig()->getNode('modules/ChoiceAI_Personalisation/version');
 
100
  $responseObj['magento_version'] = Mage::getVersion();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
101
  $this->getResponse()
102
  ->setBody(json_encode($responseObj))
103
  ->setHttpResponseCode(200)
104
  ->setHeader('Content-type', 'application/json', true);
105
-
106
- } catch(Exception $e) {
107
  $this->getResponse()
108
- ->setBody(json_encode(array('status' => 'error', 'message' => 'Internal server error', 'version' => self::API_VERSION)))
 
 
 
 
 
109
  ->setHttpResponseCode(500)
110
  ->setHeader('Content-type', 'application/json', true);
111
  }
@@ -113,15 +289,16 @@ class ChoiceAI_Personalisation_ApiController extends Mage_Core_Controller_Front_
113
  return $this;
114
  }
115
 
116
- public function ordersAction() {
 
117
  try {
118
- if(!$this->_authorise()) {
119
  return $this;
120
  }
121
 
122
  $sections = explode('/', trim($this->getRequest()->getPathInfo(), '/'));
123
 
124
- if(isset($sections[3])) {
125
  // Looking for a specific order
126
  $orderId = $sections[3];
127
 
@@ -129,9 +306,10 @@ class ChoiceAI_Personalisation_ApiController extends Mage_Core_Controller_Front_
129
 
130
  $extras = $this->getRequest()->getParam('extras');
131
  $debug = $this->getRequest()->getParam('mwdebug', 'false') === 'true';
132
- if($extras && strlen($extras)) {
133
  $extras = explode(',', $extras);
134
- for($i = 0;$i < sizeof($extras);$i++) {
 
135
  $extras[$i] = trim($extras[$i]);
136
  }
137
  }
@@ -140,13 +318,13 @@ class ChoiceAI_Personalisation_ApiController extends Mage_Core_Controller_Front_
140
 
141
  $orderItems = $order->getItemsCollection()->load();
142
 
143
- foreach($orderItems as $key => $orderItem) {
144
  $items[] = array(
145
- 'name' => $orderItem->getName(),
146
- 'pid' => $orderItem->getProductId(),
147
- 'sku' => $orderItem->getSku(),
148
- 'qty' => $orderItem->getQtyOrdered(),
149
- 'price' => $orderItem->getPrice()
150
  );
151
  }
152
 
@@ -157,10 +335,10 @@ class ChoiceAI_Personalisation_ApiController extends Mage_Core_Controller_Front_
157
  );
158
 
159
  $attributes = $order->debug();
160
- if($debug) {
161
  $responseObj['extras'] = $attributes;
162
  } else {
163
- foreach($extras as $key) {
164
  $responseObj['extras'][$key] = $attributes[$key];
165
  }
166
  }
@@ -173,7 +351,8 @@ class ChoiceAI_Personalisation_ApiController extends Mage_Core_Controller_Front_
173
  } else {
174
  // Looking for a list of orders
175
  $currentTime = time();
176
- $fromDate = $this->getRequest()->getParam('fromDate', date('Y-m-d', ($currentTime - 86400)));
 
177
  $toDate = $this->getRequest()->getParam('toDate', date('Y-m-d', $currentTime));
178
 
179
  $orders = array();
@@ -184,26 +363,35 @@ class ChoiceAI_Personalisation_ApiController extends Mage_Core_Controller_Front_
184
  ->addAttributeToSelect('created_at')
185
  ->addAttributeToSelect('increment_id')
186
  ->addAttributeToSelect('status')
187
- ->addAttributeToFilter('created_at', array('from' => $fromDate, 'to' => $toDate))
188
- ;
189
 
190
  foreach ($ordersCollection as $order) {
191
  $orders[] = array(
192
- 'order_id' => $order->getIncrementId(),
193
- 'status' => $order->getStatus(),
194
- 'email' => $order->getCustomerEmail(),
195
- 'created_at' => $order->getCreatedAt()
196
  );
197
  }
198
 
199
  $this->getResponse()
200
- ->setBody(json_encode(array('orders' => $orders, 'fromDate' => $fromDate, 'toDate' => $toDate, 'version' => self::API_VERSION)))
 
 
 
 
 
201
  ->setHttpResponseCode(200)
202
  ->setHeader('Content-type', 'application/json', true);
203
  }
204
- } catch(Exception $e) {
205
  $this->getResponse()
206
- ->setBody(json_encode(array('status' => 'error', 'message' => 'Internal server error', 'version' => self::API_VERSION)))
 
 
 
 
 
207
  ->setHttpResponseCode(500)
208
  ->setHeader('Content-type', 'application/json', true);
209
  }
@@ -211,44 +399,43 @@ class ChoiceAI_Personalisation_ApiController extends Mage_Core_Controller_Front_
211
  return $this;
212
  }
213
 
214
- public function productattributesAction() {
 
215
  try {
216
- if(!$this->_authorise()) {
217
  return $this;
218
  }
219
 
220
  $sections = explode('/', trim($this->getRequest()->getPathInfo(), '/'));
221
 
222
- if(!isset($sections[3])) {
223
  // product id
224
- throw new Exception();
225
  }
226
 
227
  $productId = $sections[3];
228
 
229
  $product = Mage::getModel('catalog/product')->load($productId);
230
 
231
- $product_info = array();
232
 
233
- $product_info["is_available"] = $product->isAvailable();
234
- $product_info["name"] = $product->getName();
235
- $product_info["id"] = $product->getId();
236
- $product_info["sku"] = $product->getSku();
237
- $product_info["price"] = $product->getPrice();
238
- $product_info["final_price"] = $product->getFinalPrice();
239
- $product_info["special_price"] = $product->getSpecialPrice();
240
- $product_info["type"] = $product->getTypeId();
241
 
242
  $variants = array();
243
  $options = array();
244
 
245
  if ($product->getTypeId() == "configurable") {
246
-
247
  $productAttributeOptions = $product->getTypeInstance(true)->getConfigurableAttributesAsArray($product);
248
  $attributeTypes = array();
249
 
250
  foreach ($productAttributeOptions as $productAttribute) {
251
-
252
  $attributes = array();
253
  foreach ($productAttribute['values'] as $attribute) {
254
  $attributes[] = array(
@@ -267,12 +454,10 @@ class ChoiceAI_Personalisation_ApiController extends Mage_Core_Controller_Front_
267
  );
268
 
269
  $attributeTypes[] = $attributeType;
270
-
271
  }
272
 
273
  $associatedProducts = $product->getTypeInstance()->getUsedProducts();
274
  foreach ($associatedProducts as $associatedProduct) {
275
-
276
  $variant = array();
277
  $variant["is_available"] = $associatedProduct->isAvailable();
278
  $variant["id"] = $associatedProduct->getId();
@@ -291,21 +476,130 @@ class ChoiceAI_Personalisation_ApiController extends Mage_Core_Controller_Front_
291
  }
292
 
293
  $this->getResponse()
294
- ->setBody(json_encode(array('product' => $product_info, 'variants' => $variants, 'options' => $options, 'version' => self::API_VERSION)))
 
 
 
 
 
295
  ->setHttpResponseCode(200)
296
  ->setHeader('Content-type', 'application/json', true);
297
- } catch(Exception $e) {
298
  $this->getResponse()
299
- ->setBody(json_encode(array('status' => 'error', 'message' => 'Internal server error', 'version' => self::API_VERSION)))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
300
  ->setHttpResponseCode(500)
301
  ->setHeader('Content-type', 'application/json', true);
302
  }
 
303
  return $this;
 
304
  }
305
 
306
- public function productsAction() {
 
307
  try {
308
- if(!$this->_authorise()) {
309
  return $this;
310
  }
311
 
@@ -317,6 +611,9 @@ class ChoiceAI_Personalisation_ApiController extends Mage_Core_Controller_Front_
317
  'sku',
318
  'image',
319
  'manufacturer',
 
 
 
320
  'price',
321
  'final_price',
322
  'special_price',
@@ -324,65 +621,117 @@ class ChoiceAI_Personalisation_ApiController extends Mage_Core_Controller_Front_
324
  );
325
 
326
  $extras = $this->getRequest()->getParam('extras');
 
 
 
 
 
327
  $debug = $this->getRequest()->getParam('mwdebug', 'false') === 'true';
328
 
329
- if($extras && strlen($extras)) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
330
  $extras = explode(',', $extras);
331
- for($i = 0;$i < sizeof($extras);$i++) {
 
332
  $extras[$i] = trim($extras[$i]);
333
  $attributes[] = $extras[$i];
334
  }
335
  }
336
 
337
- if(isset($sections[3])) {
 
338
  // Looking for a specific product
339
  $productId = $sections[3];
 
 
340
 
341
- $product = Mage::getModel('catalog/product')->load($productId);
342
-
343
- $product = $this->getFormattedProduct($product, $extras, $debug);
344
- if($product !== null) {
345
- $products[] = $product;
346
- }else{
347
- throw new Exception("Failed to fetch the product");
348
  }
 
349
 
350
- } else {
351
  // Looking for a list of products
352
  $limit = $this->getRequest()->getParam('limit', 100);
353
  $offset = $this->getRequest()->getParam('offset', 1);
354
 
355
  $productsCollection = Mage::getModel('catalog/product')->getCollection();
356
 
 
357
  // Get only enabled products
358
- $productsCollection
359
- ->addAttributeToSelect($attributes)
360
- ->addAttributeToFilter('status', array('eq' => Mage_Catalog_Model_Product_Status::STATUS_ENABLED))
361
- ->getSelect()->limit($limit, $offset) //we can specify how many products we want to show on this page
362
- ;
 
363
 
364
- foreach($productsCollection as $product) {
365
- $product = $this->getFormattedProduct($product, $extras, $debug);
366
- if($product !== null) {
367
- $products[] = $product;
368
- }else{
369
- throw new Exception("Failed to fetch the product");
370
- }
371
  }
372
 
373
- }
 
 
 
374
 
375
- $currency = Mage::app()->getStore()->getCurrentCurrencyCode();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
376
 
377
  $this->getResponse()
378
- ->setBody(json_encode(array('products' => $products, 'currency' => $currency, 'version' => self::API_VERSION)))
379
  ->setHttpResponseCode(200)
380
  ->setHeader('Content-type', 'application/json', true);
381
-
382
-
383
- } catch(Exception $e) {
384
  $this->getResponse()
385
- ->setBody(json_encode(array('status' => 'error', 'message' => 'Internal server error', 'version' => self::API_VERSION)))
 
 
 
 
 
386
  ->setHttpResponseCode(500)
387
  ->setHeader('Content-type', 'application/json', true);
388
  }
@@ -391,9 +740,10 @@ class ChoiceAI_Personalisation_ApiController extends Mage_Core_Controller_Front_
391
  }
392
 
393
 
394
- public function categoriesAction() {
 
395
  try {
396
- if(!$this->_authorise()) {
397
  return $this;
398
  }
399
 
@@ -406,6 +756,7 @@ class ChoiceAI_Personalisation_ApiController extends Mage_Core_Controller_Front_
406
  'path',
407
  'is_anchor',
408
  'is_active',
 
409
  'created_at',
410
  'updated_at'
411
  );
@@ -417,25 +768,30 @@ class ChoiceAI_Personalisation_ApiController extends Mage_Core_Controller_Front_
417
  $active = $this->getRequest()->getParam('active', 'false') === 'true';
418
  $isAnchor = $this->getRequest()->getParam('is_anchor', 'false') === 'true';
419
 
420
- if($level && strlen($level)) {
421
- $level = intval($level);
422
  } else {
423
  $level = null;
424
  }
425
 
426
- if(isset($sections[3])) {
427
  // Looking for a specific category
428
  $categoryId = $sections[3];
429
-
430
- $category = Mage::getModel('catalog/category')->load($categoryId);
431
-
432
- $category = $this->getFormattedCategory($category);
433
- if($category !== null && $category->id != null && is_array($category)) {
434
  $categories[] = $category;
435
- } else{
436
- throw new Exception("Category ".$categoryId." not found");
437
- }
438
 
 
 
 
 
 
 
 
439
  } else {
440
  // Looking for a list of categories
441
  $limit = $this->getRequest()->getParam('limit', 100);
@@ -443,51 +799,54 @@ class ChoiceAI_Personalisation_ApiController extends Mage_Core_Controller_Front_
443
 
444
  $categoriesCollection = Mage::getModel('catalog/category')->getCollection();
445
 
446
- if($level != null) {
447
  $categoriesCollection
448
  ->addAttributeToFilter('level', $level) //we can specify the level of categories to be fetched
449
  ;
450
  }
451
 
452
- if($active != null) {
453
  $categoriesCollection
454
  ->addAttributeToFilter('is_active', 1) //if you want only active categories
455
  ;
456
  }
457
 
458
- if($isAnchor != null) {
459
  $categoriesCollection
460
- ->addAttributeToFilter('is_anchor', 1) // If you want categories which are shown in search facets
461
  ;
462
  }
463
 
464
  $categoriesCollection
465
  ->addAttributeToSelect($attributes)
466
- ->getSelect()->limit($limit, $offset) //we can specify how many categories we want to show on this page
467
  ;
468
 
469
- foreach($categoriesCollection as $category) {
470
  $category = $this->getFormattedCategory($category);
471
- if($category !== null && is_array($category)) {
472
  $categories[] = $category;
473
  }
474
  }
475
-
476
  }
477
 
478
  $this->getResponse()
479
  ->setBody(json_encode(array('categories' => $categories, 'version' => self::API_VERSION)))
480
  ->setHttpResponseCode(200)
481
  ->setHeader('Content-type', 'application/json', true);
482
-
483
- } catch(\Exception $e) {
484
- if($e->getMessage() != null)
485
  $message = $e->getMessage();
486
  else
487
  $message = 'Internal server error';
488
 
489
  $this->getResponse()
490
- ->setBody(json_encode(array('status' => 'error', 'message' => $message, 'version' => self::API_VERSION)))
 
 
 
 
 
491
  ->setHttpResponseCode(500)
492
  ->setHeader('Content-type', 'application/json', true);
493
  }
@@ -496,9 +855,10 @@ class ChoiceAI_Personalisation_ApiController extends Mage_Core_Controller_Front_
496
  }
497
 
498
 
499
- public function facetattributesAction() {
 
500
  try {
501
- if(!$this->_authorise()) {
502
  return $this;
503
  }
504
 
@@ -509,9 +869,9 @@ class ChoiceAI_Personalisation_ApiController extends Mage_Core_Controller_Front_
509
 
510
  $collection = Mage::getResourceModel('catalog/product_attribute_collection');
511
 
512
- if($isFilterable && is_numeric($isFilterable)){
513
- $collection->addFieldToFilter('is_filterable', (int) $isFilterable);
514
- } else{
515
  // Give only is filterable with results
516
  $collection->addFieldToFilter('is_filterable', 1);
517
  }
@@ -532,20 +892,20 @@ class ChoiceAI_Personalisation_ApiController extends Mage_Core_Controller_Front_
532
  // "is_used_for_price_rules",
533
  // "used_in_product_listing"
534
  // );
535
- // //@TODO: Mysql error coming up
536
  // $collection->addFieldToSelect($fieldsToSelect);
537
 
538
  $collection->setOrder('position', 'ASC');
539
- $collection->setPageSize((int) $limit);
540
- $collection->setCurPage((int) $page);
541
  $collection->load();
542
 
543
- if($limit * ($page-1) < $collection->getSize()) {
544
  $allAttrs = array();
545
 
546
  foreach ($collection as $attr) {
547
  $newAttr = array();
548
- if($fields=="all") {
549
  $newAttr['attribute_id'] = $attr->getAttributeId();
550
  $newAttr['is_filterable'] = $attr->getIsFilterable();
551
  $newAttr['attribute_code'] = $attr->getAttributeCode();
@@ -560,7 +920,7 @@ class ChoiceAI_Personalisation_ApiController extends Mage_Core_Controller_Front_
560
  $newAttr['is_used_for_promo_rules'] = $attr->getIsUsedForPromoRules();
561
  $newAttr['is_used_for_price_rules'] = $attr->getIsUsedForPriceRules();
562
  $newAttr['used_in_product_listing'] = $attr->getUsedInProductListing();
563
- } else{
564
  $newAttr['attribute_id'] = $attr->getAttributeId();
565
  $newAttr['is_filterable'] = $attr->getIsFilterable();
566
  $newAttr['attribute_code'] = $attr->getAttributeCode();
@@ -575,7 +935,7 @@ class ChoiceAI_Personalisation_ApiController extends Mage_Core_Controller_Front_
575
 
576
  $allAttrs[] = $newAttr;
577
  }
578
- } else{
579
  $allAttrs = array();
580
  }
581
 
@@ -583,9 +943,14 @@ class ChoiceAI_Personalisation_ApiController extends Mage_Core_Controller_Front_
583
  ->setBody(json_encode(array('attributes' => $allAttrs, 'version' => self::API_VERSION)))
584
  ->setHttpResponseCode(200)
585
  ->setHeader('Content-type', 'application/json', true);
586
- } catch(Exception $e) {
587
  $this->getResponse()
588
- ->setBody(json_encode(array('status' => 'error', 'message' => 'Internal server error', 'version' => self::API_VERSION)))
 
 
 
 
 
589
  ->setHttpResponseCode(500)
590
  ->setHeader('Content-type', 'application/json', true);
591
  }
@@ -594,16 +959,17 @@ class ChoiceAI_Personalisation_ApiController extends Mage_Core_Controller_Front_
594
  }
595
 
596
 
597
- public function modifyfacetattributeAction() {
 
598
  try {
599
- if(!$this->_authorise()) {
600
  return $this;
601
  }
602
 
603
  $facetData = $this->getRequest()->getParam('newData', false);
604
  $newFacetData = json_decode($facetData);
605
 
606
- if(!$newFacetData){
607
  if ($facetData == "") {
608
  $input = file_get_contents('php://input');
609
  $input = utf8_encode($input);
@@ -611,8 +977,8 @@ class ChoiceAI_Personalisation_ApiController extends Mage_Core_Controller_Front_
611
  }
612
  }
613
 
614
- if(!$newFacetData && !$newFacetData->attribute_id)
615
- throw new Exception("Insufficient data");
616
 
617
  // updates the facet
618
  $resourceModel = Mage::getResourceModel('catalog/product_attribute_collection');
@@ -624,25 +990,31 @@ class ChoiceAI_Personalisation_ApiController extends Mage_Core_Controller_Front_
624
  foreach ($newFacetData as $key => $value) {
625
  $attributeObj->setData($key, $value);
626
  }
 
627
  $updateStatus = $attributeObj->save();
628
 
629
- if(!$updateStatus)
630
- throw new Exception("Couldn't update");
631
  else
632
  Mage::app()->getCache()->save(null, "sysFacets", array("facets"));
633
 
634
  $this->getResponse()
635
- ->setBody(json_encode(array("status"=>"ok", 'version' => self::API_VERSION)))
636
  ->setHttpResponseCode(200)
637
  ->setHeader('Content-type', 'application/json', true);
638
- } catch(Exception $e) {
639
- if($e->getMessage())
640
  $errorMsg = $e->getMessage();
641
  else
642
  $errorMsg = "Internal server error";
643
 
644
  $this->getResponse()
645
- ->setBody(json_encode(array('status' => 'error', 'message' => $errorMsg, 'version' => self::API_VERSION)))
 
 
 
 
 
646
  ->setHttpResponseCode(500)
647
  ->setHeader('Content-type', 'application/json', true);
648
  }
@@ -651,7 +1023,8 @@ class ChoiceAI_Personalisation_ApiController extends Mage_Core_Controller_Front_
651
  }
652
 
653
  // Formats the attribute for save
654
- private function _prepareAttributeForSave($data) {
 
655
  /** @var $helperCatalog Mage_Catalog_Helper_Data */
656
  $helperCatalog = Mage::helper('catalog');
657
 
@@ -666,15 +1039,19 @@ class ChoiceAI_Personalisation_ApiController extends Mage_Core_Controller_Front_
666
  if (!isset($data['is_configurable'])) {
667
  $data['is_configurable'] = 0;
668
  }
 
669
  if (!isset($data['is_filterable'])) {
670
  $data['is_filterable'] = 1;
671
  }
 
672
  if (!isset($data['is_filterable_in_search'])) {
673
  $data['is_filterable_in_search'] = 1;
674
  }
 
675
  if (!isset($data['apply_to'])) {
676
  $data['apply_to'] = array();
677
  }
 
678
  // set frontend labels array with store_id as keys
679
  if (isset($data['frontend_label']) && is_array($data['frontend_label'])) {
680
  $labels = array();
@@ -683,13 +1060,16 @@ class ChoiceAI_Personalisation_ApiController extends Mage_Core_Controller_Front_
683
  $labelText = $helperCatalog->stripTags($label['label']);
684
  $labels[$storeId] = $labelText;
685
  }
 
686
  $data['frontend_label'] = $labels;
687
  }
 
688
  // set additional fields
689
  if (isset($data['additional_fields']) && is_array($data['additional_fields'])) {
690
  $data = array_merge($data, $data['additional_fields']);
691
  unset($data['additional_fields']);
692
  }
 
693
  //default value
694
  if (!empty($data['default_value'])) {
695
  $data['default_value'] = $helperCatalog->stripTags($data['default_value']);
@@ -699,31 +1079,33 @@ class ChoiceAI_Personalisation_ApiController extends Mage_Core_Controller_Front_
699
  }
700
 
701
 
702
- public function addfacetattributeAction() {
 
703
  try {
704
- if(!$this->_authorise()) {
705
  return $this;
706
  }
707
 
708
  $facetData = $this->getRequest()->getParam('newData', false);
709
  $newFacetData = json_decode($facetData);
710
 
711
- if(!$newFacetData){
712
  if ($facetData == "") {
713
  $input = file_get_contents('php://input');
714
  $input = utf8_encode($input);
715
  $newFacetData = json_decode($input)->newData;
716
  }
717
  }
 
718
  $newFacetData = (array)$newFacetData;
719
 
720
  if (!$newFacetData || empty($newFacetData['attribute_code']) || !isset($newFacetData['frontend_label'])) {
721
- throw new Exception("Insufficient data");
722
  }
723
 
724
  //validate attribute_code
725
  if (!preg_match('/^[a-z][a-z_0-9]{0,254}$/', $newFacetData['attribute_code'])) {
726
- throw new Exception("Invalid attribute_code");
727
  }
728
 
729
  /** @var $model Mage_Catalog_Model_Resource_Eav_Attribute */
@@ -734,7 +1116,7 @@ class ChoiceAI_Personalisation_ApiController extends Mage_Core_Controller_Front_
734
 
735
  $newFacetData['source_model'] = $helper->getAttributeSourceModelByInputType('multiselect');
736
  $newFacetData['backend_model'] = $helper->getAttributeBackendModelByInputType('multiselect');
737
- if (is_null($model->getIsUserDefined()) || $model->getIsUserDefined() != 0) {
738
  $newFacetData['backend_type'] = $model->getBackendTypeByInput('multiselect');
739
  }
740
 
@@ -752,17 +1134,22 @@ class ChoiceAI_Personalisation_ApiController extends Mage_Core_Controller_Front_
752
  Mage::app()->cleanCache(array(Mage_Core_Model_Translate::CACHE_TAG));
753
 
754
  $this->getResponse()
755
- ->setBody(json_encode(array("status"=>"ok", 'version' => self::API_VERSION)))
756
  ->setHttpResponseCode(200)
757
  ->setHeader('Content-type', 'application/json', true);
758
- } catch(Exception $e) {
759
- if($e->getMessage())
760
  $errorMsg = $e->getMessage();
761
  else
762
  $errorMsg = "Internal server error";
763
 
764
  $this->getResponse()
765
- ->setBody(json_encode(array('status' => 'error', 'message' => $errorMsg, 'version' => self::API_VERSION)))
 
 
 
 
 
766
  ->setHttpResponseCode(500)
767
  ->setHeader('Content-type', 'application/json', true);
768
  }
@@ -771,11 +1158,11 @@ class ChoiceAI_Personalisation_ApiController extends Mage_Core_Controller_Front_
771
  }
772
 
773
 
774
- public function usersAction() {
 
775
 
776
  try {
777
-
778
- if(!$this->_authorise()) {
779
  return $this;
780
  }
781
 
@@ -795,29 +1182,25 @@ class ChoiceAI_Personalisation_ApiController extends Mage_Core_Controller_Front_
795
 
796
  $usersCollection = Mage::getModel('customer/customer')->getCollection();
797
 
798
- if($email != null && strlen($email) > 0) {
799
-
800
  $usersCollection
801
  ->addAttributeToFilter('email', $email);
802
-
803
  } else {
804
-
805
- $created_at_min = $this->getRequest()->getParam('created_at_min');
806
- $created_at_max = $this->getRequest()->getParam('created_at_max');
807
 
808
  $usersCollection->addAttributeToSelect($attributes);
809
 
810
- if($created_at_min != null && strlen($created_at_min) > 0) {
811
- $usersCollection->addAttributeToFilter('created_at', array('from' => $created_at_min));
812
  }
813
 
814
- if($created_at_max != null && strlen($created_at_max) > 0) {
815
- $usersCollection->addAttributeToFilter('created_at', array('to' => $created_at_max));
816
  }
817
 
818
  $usersCollection->getSelect()
819
  ->limit($limit, $offset);
820
-
821
  }
822
 
823
  $users = array();
@@ -838,10 +1221,14 @@ class ChoiceAI_Personalisation_ApiController extends Mage_Core_Controller_Front_
838
  ->setBody(json_encode(array('users' => $users, 'version' => self::API_VERSION)))
839
  ->setHttpResponseCode(200)
840
  ->setHeader('Content-type', 'application/json', true);
841
-
842
- } catch(Exception $e) {
843
  $this->getResponse()
844
- ->setBody(json_encode(array('status' => 'error', 'message' => 'Internal server error', 'version' => self::API_VERSION)))
 
 
 
 
 
845
  ->setHttpResponseCode(500)
846
  ->setHeader('Content-type', 'application/json', true);
847
  }
@@ -850,168 +1237,485 @@ class ChoiceAI_Personalisation_ApiController extends Mage_Core_Controller_Front_
850
 
851
  }
852
 
 
 
 
 
 
 
853
 
854
- public function stockAction() {
 
 
 
 
 
 
 
 
 
855
 
856
- try {
 
 
 
 
 
 
857
 
858
- if(!$this->_authorise()) {
859
- return $this;
 
 
 
 
 
860
  }
861
 
862
- $productId = $this->getRequest()->getParam('pid');
863
- $sku = $this->getRequest()->getParam('sku', 'false') === 'true';
 
 
864
 
 
865
 
866
- if(!$productId || strlen($productId) <= 0) {
 
 
 
 
 
 
 
867
 
868
- $this->getResponse()
869
- ->setBody(json_encode(array('status' => 'error', 'message' => 'product id required', 'version' => self::API_VERSION)))
870
- ->setHttpResponseCode(500)
871
- ->setHeader('Content-type', 'application/json', true);
872
 
873
- } else {
 
 
 
874
 
875
- // load product if sku is given
876
- if($sku) {
877
- $product = Mage::getModel('catalog/product')->loadByAttribute('sku', $productId);
878
- if($product == null) {
879
- $this->getResponse()
880
- ->setBody(json_encode(array('status' => 'error', 'message' => 'invalid sku', 'version' => self::API_VERSION)))
881
- ->setHttpResponseCode(500)
882
- ->setHeader('Content-type', 'application/json', true);
883
- return $this;
 
 
 
 
884
  }
885
- $productId = $product->getId();
886
  }
 
887
 
888
- // get stock info
889
- $stockObj = Mage::getModel('cataloginventory/stock_item')->loadByProduct($productId);
 
 
 
 
 
 
 
 
 
 
 
 
 
890
 
891
- $stock = $stockObj->getQty();
 
892
 
893
- $this->getResponse()
894
- ->setBody(json_encode(array('id' => $productId, 'stock' => $stock, 'version' => self::API_VERSION)))
895
- ->setHttpResponseCode(200)
896
- ->setHeader('Content-type', 'application/json', true);
 
 
 
 
 
897
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
898
  }
899
 
900
- } catch(Exception $e) {
901
  $this->getResponse()
902
- ->setBody(json_encode(array('status' => 'error', 'message' => 'Internal server error', 'version' => self::API_VERSION)))
 
 
 
 
 
 
 
 
 
 
903
  ->setHttpResponseCode(500)
904
  ->setHeader('Content-type', 'application/json', true);
905
  }
906
 
907
  return $this;
 
 
 
 
 
908
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
909
  }
910
 
 
 
911
 
912
- private function getFormattedProduct($product, $extras, $debug) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
913
 
914
  $formattedProduct = null;
915
 
916
  try {
917
  $formattedProduct = array(
918
- 'id' => $product->getId(),
919
- 'sku' => $product->getSku(),
920
- 'name' => $product->getName(),
921
- 'cat' => array(),
922
- 'manufacturer' => $product->getAttributeText('manufacturer'),
923
- 'price' => $product->getPrice(),
924
- 'final_price' => $product->getFinalPrice(),
925
- 'special_price' => $product->getSpecialPrice(),
926
- 'image' => $product->getImageUrl(),
927
- 'url' => $product->getProductUrl(),
928
- 'info' => $product->getShortDescription(),
929
- 'status' => $product->getStatus(),
930
- 'type' => $product->getTypeId(),
931
- 'created_at' => $product->getCreatedAt(),
932
- 'updated_at' => $product->getUpdatedAt()
 
 
 
 
 
933
  );
934
- if(!$formattedProduct['manufacturer'] || strlen($formattedProduct['manufacturer']) === 0) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
935
  $product = Mage::getModel('catalog/product')->load($product->getId());
936
  $formattedProduct['manufacturer'] = $product->getAttributeText('manufacturer');
937
  }
938
 
939
- if($formattedProduct['type'] == "configurable") {
940
- // get associated product ids
941
- $associatedProducts = Mage::getModel('catalog/product_type_configurable')->getChildrenIds($formattedProduct['id']);
942
- $formattedProduct['associated_products'] = $associatedProducts;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
943
  }
944
 
945
- // get stock info
946
- $stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product);
947
- $formattedProduct['stock'] = $stock->getQty();
 
 
 
 
 
 
948
 
949
- if($debug) {
950
- $attributes = $product->getAttributes();
951
- foreach($attributes as $key => $value) {
952
- // Sanity check, or else crashes without proper error handling
953
- $existenceCheck = $product->getResource()->getAttribute($key);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
954
 
955
- if($existenceCheck)
956
- $formattedProduct['extras'][$key] = $product->getAttributeText($key);
957
  }
 
 
 
 
 
 
 
 
 
958
  } else {
959
- foreach($extras as $key) {
960
  // Sanity check, or else crashes without proper error handling
961
  $existenceCheck = $product->getResource()->getAttribute($key);
962
 
963
- if($existenceCheck)
964
  $formattedProduct['extras'][$key] = $product->getAttributeText($key);
965
  }
966
  }
967
 
968
  $categories = $product->getCategoryCollection()
969
  ->addAttributeToSelect('id')
970
- ->addAttributeToSelect('name')
971
- ->addAttributeToSelect('path')
972
- ->addAttributeToSelect('level');
973
- foreach($categories as $category) {
974
- $formattedCategory = array();
975
- $formattedCategory['id'] = $category->getId();
976
- $formattedCategory['name'] = $category->getName();
977
- $formattedCategory['level'] = $category->getLevel();
978
- $formattedCategory['path'] = $category->getPath();
979
- $formattedProduct['cat'][$formattedCategory['id']] = $formattedCategory;
 
980
  }
981
 
982
  return $formattedProduct;
983
- } catch(Exception $e) {
984
  return NULL;
985
  }
986
  }
987
 
988
- private function getFormattedCategory($category) {
 
989
 
990
  $formattedCategory = null;
991
 
992
  try {
993
  $formattedCategory = array(
994
- 'id' => $category->getId(),
995
- 'name' => $category->getName(),
996
- 'image' => $category->getImageUrl(),
997
- 'url' => $category->getUrl(),
998
- 'level' => $category->getLevel(),
999
- 'path' => $category->getPath(),
1000
- 'is_anchor' => $category->getIsAnchor(),
1001
- 'is_active' => $category->getIsActive(),
1002
- 'created_at' => $category->getCreatedAt(),
1003
- 'updated_at' => $category->getUpdatedAt()
 
 
1004
  );
1005
-
1006
- } catch(Exception $e) {}
1007
 
1008
  return $formattedCategory;
1009
  }
1010
 
1011
- public function sortbyAction() {
 
1012
 
1013
  try {
1014
- if(!$this->_authorise()) {
1015
  return $this;
1016
  }
1017
 
@@ -1036,22 +1740,31 @@ class ChoiceAI_Personalisation_ApiController extends Mage_Core_Controller_Front_
1036
 
1037
  foreach ($attributesData as $key => $attributeData) {
1038
  $sortByOptions[] = array(
1039
- "_id"=> $key,
1040
- "name"=> $attributeData,
1041
- "default"=> $key==$defaultSort ? true:false,
1042
- "order"=> $i
1043
  );
1044
  $i++;
1045
  }
1046
 
1047
  $this->getResponse()
1048
- ->setBody(json_encode(array('status' => "ok", 'options' => $sortByOptions, 'version' => self::API_VERSION)))
 
 
 
 
 
1049
  ->setHttpResponseCode(200)
1050
  ->setHeader('Content-type', 'application/json', true);
1051
-
1052
- } catch(Exception $e) {
1053
  $this->getResponse()
1054
- ->setBody(json_encode(array('status' => 'error', 'message' => 'Internal server error', 'version' => self::API_VERSION)))
 
 
 
 
 
1055
  ->setHttpResponseCode(500)
1056
  ->setHeader('Content-type', 'application/json', true);
1057
  }
@@ -1059,15 +1772,16 @@ class ChoiceAI_Personalisation_ApiController extends Mage_Core_Controller_Front_
1059
  return $this;
1060
  }
1061
 
1062
- public function choicepageAction() {
1063
- try{
1064
- if(!$this->_authorise()) {
 
1065
  return $this;
1066
  }
1067
 
1068
  $responseObj = array();
1069
-
1070
- if ($_SERVER["REQUEST_METHOD"] == "POST") {
1071
  // Create Choice page
1072
  // Expects title, url_path and content
1073
 
@@ -1077,12 +1791,13 @@ class ChoiceAI_Personalisation_ApiController extends Mage_Core_Controller_Front_
1077
  $input = utf8_encode($input);
1078
  $pageData = json_encode(json_decode($input)->data);
1079
  }
 
1080
  $pageData = json_decode($pageData);
1081
  $urlPath = $pageData->url_path;
1082
 
1083
- if(!$urlPath){
1084
  // What can this poor guy do without the path?
1085
- throw new Exception("No urlPath");
1086
  }
1087
 
1088
  // Ensure no other existing page contains the same URL path or say "identifier"
@@ -1093,18 +1808,20 @@ class ChoiceAI_Personalisation_ApiController extends Mage_Core_Controller_Front_
1093
  // $existingPages = Mage::getModel('cms/page')->load($collection->getFirstItem()->getId());
1094
 
1095
  // There can be multiple pages with same url path, but different store scope. Let's disable all
1096
- foreach ($collection as $existingPage){
1097
  // Page already exists && is_active
1098
- if($existingPage->getId()) {
1099
  // Delete all other pages having same URL
1100
  // and then continue adding our new page
1101
- // $this->updateChoiceUrl(false, $existingPage->getId(), array("is_active" => 0, "identifier" => $urlPath));
 
1102
  $existingPage->delete();
1103
  }
1104
  }
1105
 
1106
  // Setting default content if not provided
1107
- $pageContent = (!isset($pageData->content)) ? "<script>window._caichoicePage = true;</script><div id='caichoicePage'></div>" : $pageData->content;
 
</