Copernica_MarketingSoftware - Version 3.1.0

Version Notes

- Using the REST API.
- Synchronise wishlists of customers.

Download this release

Release Info

Developer Cream
Extension Copernica_MarketingSoftware
Version 3.1.0
Comparing to
See all releases


Code changes from version 3.0.1 to 3.1.0

Files changed (157) hide show
  1. app/code/community/Copernica/MarketingSoftware/{Model/QueueEvent/QuoteItemAdd.php → Block/Adminhtml/Marketingsoftware/Accesstoken.php} +27 -7
  2. app/code/community/Copernica/MarketingSoftware/Block/Adminhtml/Marketingsoftware/Export.php +1 -0
  3. app/code/community/Copernica/MarketingSoftware/Block/Adminhtml/Marketingsoftware/Link.php +4 -0
  4. app/code/community/Copernica/MarketingSoftware/Block/Adminhtml/Marketingsoftware/Settings.php +13 -0
  5. app/code/community/Copernica/MarketingSoftware/Block/Adminhtml/Marketingsoftware/Sync.php +12 -14
  6. app/code/community/Copernica/MarketingSoftware/Controller/{Base.php → Action.php} +54 -51
  7. app/code/community/Copernica/MarketingSoftware/Exception.php +1 -1
  8. app/code/community/Copernica/MarketingSoftware/Helper/Api.php +151 -146
  9. app/code/community/Copernica/MarketingSoftware/Helper/Api/Abstract.php +427 -0
  10. app/code/community/Copernica/MarketingSoftware/Helper/Api/Builder.php +243 -0
  11. app/code/community/Copernica/MarketingSoftware/Helper/Api/Validator.php +382 -0
  12. app/code/community/Copernica/MarketingSoftware/Helper/ApiBase.php +0 -452
  13. app/code/community/Copernica/MarketingSoftware/Helper/ApiBuilder.php +0 -261
  14. app/code/community/Copernica/MarketingSoftware/Helper/ApiValidator.php +0 -381
  15. app/code/community/Copernica/MarketingSoftware/Helper/Config.php +489 -187
  16. app/code/community/Copernica/MarketingSoftware/Helper/Data.php +206 -110
  17. app/code/community/Copernica/MarketingSoftware/Helper/DataWriter.php +70 -105
  18. app/code/community/Copernica/MarketingSoftware/Helper/Profile.php +65 -94
  19. app/code/community/Copernica/MarketingSoftware/Helper/RESTRequest.php +0 -414
  20. app/code/community/Copernica/MarketingSoftware/Helper/Rest/Request.php +361 -0
  21. app/code/community/Copernica/MarketingSoftware/Model/{AbandonedCart.php → Abandoned/Cart.php} +6 -4
  22. app/code/community/Copernica/MarketingSoftware/Model/{AbandonedCartsProcessor.php → Abandoned/Carts/Processor.php} +30 -55
  23. app/code/community/Copernica/MarketingSoftware/Model/Abstraction/Address.php +106 -88
  24. app/code/community/Copernica/MarketingSoftware/Model/Abstraction/Attributes.php +34 -25
  25. app/code/community/Copernica/MarketingSoftware/Model/Abstraction/Customer.php +100 -94
  26. app/code/community/Copernica/MarketingSoftware/Model/Abstraction/Name.php +34 -26
  27. app/code/community/Copernica/MarketingSoftware/Model/Abstraction/Order.php +121 -93
  28. app/code/community/Copernica/MarketingSoftware/Model/Abstraction/Order/Item.php +65 -54
  29. app/code/community/Copernica/MarketingSoftware/Model/Abstraction/Order/Item/Options.php +42 -36
  30. app/code/community/Copernica/MarketingSoftware/Model/Abstraction/Price.php +92 -87
  31. app/code/community/Copernica/MarketingSoftware/Model/Abstraction/Product.php +125 -133
  32. app/code/community/Copernica/MarketingSoftware/Model/Abstraction/{Viewedproduct.php → Product/Viewed.php} +149 -158
  33. app/code/community/Copernica/MarketingSoftware/Model/Abstraction/Quote.php +128 -107
  34. app/code/community/Copernica/MarketingSoftware/Model/Abstraction/Quote/Item.php +77 -47
  35. app/code/community/Copernica/MarketingSoftware/Model/Abstraction/Quote/Item/Options.php +44 -36
  36. app/code/community/Copernica/MarketingSoftware/Model/Abstraction/Storeview.php +53 -41
  37. app/code/community/Copernica/MarketingSoftware/Model/Abstraction/Subscription.php +56 -37
  38. app/code/community/Copernica/MarketingSoftware/Model/Abstraction/Wishlist.php +369 -0
  39. app/code/community/Copernica/MarketingSoftware/Model/Abstraction/Wishlist/Item.php +209 -0
  40. app/code/community/Copernica/MarketingSoftware/Model/Abstraction/Wishlist/Item/Options.php +163 -0
  41. app/code/community/Copernica/MarketingSoftware/Model/Config.php +4 -2
  42. app/code/community/Copernica/MarketingSoftware/Model/Copernica/Abstract.php +31 -27
  43. app/code/community/Copernica/MarketingSoftware/Model/Copernica/Address/Subprofile.php +9 -7
  44. app/code/community/Copernica/MarketingSoftware/Model/Copernica/Entity.php +25 -34
  45. app/code/community/Copernica/MarketingSoftware/Model/Copernica/Entity/Address.php +94 -61
  46. app/code/community/Copernica/MarketingSoftware/Model/Copernica/Entity/Customer.php +177 -92
  47. app/code/community/Copernica/MarketingSoftware/Model/Copernica/Entity/Item.php +0 -218
  48. app/code/community/Copernica/MarketingSoftware/Model/Copernica/Entity/Order.php +101 -70
  49. app/code/community/Copernica/MarketingSoftware/Model/Copernica/Entity/Order/Item.php +195 -0
  50. app/code/community/Copernica/MarketingSoftware/Model/Copernica/Entity/Product.php +256 -77
  51. app/code/community/Copernica/MarketingSoftware/Model/Copernica/Entity/Quote.php +35 -25
  52. app/code/community/Copernica/MarketingSoftware/Model/Copernica/Entity/{CartItem.php → Quote/Item.php} +31 -25
  53. app/code/community/Copernica/MarketingSoftware/Model/Copernica/Entity/Subscription.php +50 -40
  54. app/code/community/Copernica/MarketingSoftware/Model/Copernica/Entity/Wishlist.php +99 -0
  55. app/code/community/Copernica/MarketingSoftware/Model/Copernica/Entity/Wishlist/Item.php +155 -0
  56. app/code/community/Copernica/MarketingSoftware/Model/Copernica/Order/Subprofile.php +35 -26
  57. app/code/community/Copernica/MarketingSoftware/Model/Copernica/Orderitem/Subprofile.php +15 -16
  58. app/code/community/Copernica/MarketingSoftware/Model/Copernica/{Viewedproduct → Product/Viewed}/Subprofile.php +19 -14
  59. app/code/community/Copernica/MarketingSoftware/Model/Copernica/Profile.php +5 -0
  60. app/code/community/Copernica/MarketingSoftware/Model/Copernica/{Profilecustomer.php → Profile/Customer.php} +23 -26
  61. app/code/community/Copernica/MarketingSoftware/Model/Copernica/{Profileorder.php → Profile/Order.php} +36 -32
  62. app/code/community/Copernica/MarketingSoftware/Model/Copernica/{Profilequote.php → Profile/Quote.php} +39 -36
  63. app/code/community/Copernica/MarketingSoftware/Model/Copernica/{Profilesubscription.php → Profile/Subscription.php} +21 -18
  64. app/code/community/Copernica/MarketingSoftware/Model/Copernica/{Cartitem → Quote/Item}/Subprofile.php +25 -23
  65. app/code/community/Copernica/MarketingSoftware/Model/Copernica/Wishlist/Item/Subprofile.php +132 -0
  66. app/code/community/Copernica/MarketingSoftware/Model/{ErrorQueue.php → Error/Queue.php} +12 -24
  67. app/code/community/Copernica/MarketingSoftware/Model/Mysql4/{AbandonedCart.php → Abandoned/Cart.php} +2 -2
  68. app/code/community/Copernica/MarketingSoftware/Model/Mysql4/Abandoned/Cart/Collection.php +12 -0
  69. app/code/community/Copernica/MarketingSoftware/Model/Mysql4/AbandonedCart/Collection.php +0 -12
  70. app/code/community/Copernica/MarketingSoftware/Model/Mysql4/{ErrorQueue.php → Error/Queue.php} +2 -2
  71. app/code/community/Copernica/MarketingSoftware/Model/Mysql4/{SyncProfile → Error/Queue}/Collection.php +5 -2
  72. app/code/community/Copernica/MarketingSoftware/Model/Mysql4/{ProfileCache.php → Profile/Cache.php} +2 -2
  73. app/code/community/Copernica/MarketingSoftware/Model/Mysql4/{ErrorQueue → Profile/Cache}/Collection.php +2 -5
  74. app/code/community/Copernica/MarketingSoftware/Model/Mysql4/{Queue.php → Queue/Item.php} +8 -12
  75. app/code/community/Copernica/MarketingSoftware/Model/Mysql4/Queue/{Collection.php → Item/Collection.php} +8 -8
  76. app/code/community/Copernica/MarketingSoftware/Model/Mysql4/{SyncProfile.php → Sync/Profile.php} +2 -2
  77. app/code/community/Copernica/MarketingSoftware/Model/Mysql4/{ProfileCache → Sync/Profile}/Collection.php +2 -2
  78. app/code/community/Copernica/MarketingSoftware/Model/Observer.php +243 -214
  79. app/code/community/Copernica/MarketingSoftware/Model/{ProfileCache.php → Profile/Cache.php} +9 -6
  80. app/code/community/Copernica/MarketingSoftware/Model/Queue/Event/Abstract.php +109 -0
  81. app/code/community/Copernica/MarketingSoftware/Model/Queue/Event/Checkout.php +114 -0
  82. app/code/community/Copernica/MarketingSoftware/Model/{QueueEvent → Queue/Event}/Customer.php +50 -53
  83. app/code/community/Copernica/MarketingSoftware/Model/{QueueEvent → Queue/Event}/Factory.php +27 -26
  84. app/code/community/Copernica/MarketingSoftware/Model/Queue/Event/Item.php +176 -0
  85. app/code/community/Copernica/MarketingSoftware/Model/Queue/Event/Order.php +159 -0
  86. app/code/community/Copernica/MarketingSoftware/Model/{QueueEvent → Queue/Event}/Quote.php +28 -33
  87. app/code/community/Copernica/MarketingSoftware/Model/{QueueEvent/StartSync.php → Queue/Event/Startsync.php} +58 -105
  88. app/code/community/Copernica/MarketingSoftware/Model/Queue/Event/Subscription.php +96 -0
  89. app/code/community/Copernica/MarketingSoftware/Model/{QueueEvent → Queue/Event}/View.php +14 -8
  90. app/code/community/Copernica/MarketingSoftware/Model/Queue/Event/Wishlist/Item.php +113 -0
  91. app/code/community/Copernica/MarketingSoftware/Model/{Queue.php → Queue/Item.php} +64 -68
  92. app/code/community/Copernica/MarketingSoftware/Model/Queue/Processor.php +243 -0
  93. app/code/community/Copernica/MarketingSoftware/Model/QueueEvent/Abstract.php +0 -119
  94. app/code/community/Copernica/MarketingSoftware/Model/QueueEvent/Checkout.php +0 -96
  95. app/code/community/Copernica/MarketingSoftware/Model/QueueEvent/CustomerAdd.php +0 -52
  96. app/code/community/Copernica/MarketingSoftware/Model/QueueEvent/CustomerFull.php +0 -242
  97. app/code/community/Copernica/MarketingSoftware/Model/QueueEvent/CustomerModify.php +0 -126
  98. app/code/community/Copernica/MarketingSoftware/Model/QueueEvent/CustomerRemove.php +0 -59
  99. app/code/community/Copernica/MarketingSoftware/Model/QueueEvent/CustomerUpgrade.php +0 -89
  100. app/code/community/Copernica/MarketingSoftware/Model/QueueEvent/Item.php +0 -178
  101. app/code/community/Copernica/MarketingSoftware/Model/QueueEvent/Order.php +0 -167
  102. app/code/community/Copernica/MarketingSoftware/Model/QueueEvent/OrderAdd.php +0 -35
  103. app/code/community/Copernica/MarketingSoftware/Model/QueueEvent/OrderModify.php +0 -171
  104. app/code/community/Copernica/MarketingSoftware/Model/QueueEvent/QuoteItem.php +0 -124
  105. app/code/community/Copernica/MarketingSoftware/Model/QueueEvent/QuoteItemModify.php +0 -41
  106. app/code/community/Copernica/MarketingSoftware/Model/QueueEvent/QuoteItemRemove.php +0 -41
  107. app/code/community/Copernica/MarketingSoftware/Model/QueueEvent/QuoteModify.php +0 -132
  108. app/code/community/Copernica/MarketingSoftware/Model/QueueEvent/Subscription.php +0 -102
  109. app/code/community/Copernica/MarketingSoftware/Model/QueueEvent/SubscriptionAdd.php +0 -35
  110. app/code/community/Copernica/MarketingSoftware/Model/QueueEvent/SubscriptionModify.php +0 -73
  111. app/code/community/Copernica/MarketingSoftware/Model/QueueEvent/SubscriptionRemove.php +0 -35
  112. app/code/community/Copernica/MarketingSoftware/Model/QueueEvent/ViewedProductAdd.php +0 -72
  113. app/code/community/Copernica/MarketingSoftware/Model/QueueProcessor.php +0 -286
  114. app/code/community/Copernica/MarketingSoftware/Model/REST/CartItem.php +0 -74
  115. app/code/community/Copernica/MarketingSoftware/Model/REST/Item.php +0 -129
  116. app/code/community/Copernica/MarketingSoftware/Model/REST/Order.php +0 -166
  117. app/code/community/Copernica/MarketingSoftware/Model/REST/Subscription.php +0 -115
  118. app/code/community/Copernica/MarketingSoftware/Model/{REST.php → Rest.php} +38 -56
  119. app/code/community/Copernica/MarketingSoftware/Model/{REST → Rest}/Address.php +50 -48
  120. app/code/community/Copernica/MarketingSoftware/Model/{REST → Rest}/Customer.php +29 -39
  121. app/code/community/Copernica/MarketingSoftware/Model/Rest/Order.php +158 -0
  122. app/code/community/Copernica/MarketingSoftware/Model/Rest/Order/Item.php +126 -0
  123. app/code/community/Copernica/MarketingSoftware/Model/{REST → Rest}/Product.php +41 -31
  124. app/code/community/Copernica/MarketingSoftware/Model/{REST → Rest}/Quote.php +24 -18
  125. app/code/community/Copernica/MarketingSoftware/Model/Rest/Quote/Item.php +80 -0
  126. app/code/community/Copernica/MarketingSoftware/Model/Rest/Subscription.php +96 -0
  127. app/code/community/Copernica/MarketingSoftware/Model/Rest/Wishlist.php +70 -0
  128. app/code/community/Copernica/MarketingSoftware/Model/Rest/Wishlist/Item.php +80 -0
  129. app/code/community/Copernica/MarketingSoftware/Model/{SyncProfile.php → Sync/Profile.php} +41 -32
  130. app/code/community/Copernica/MarketingSoftware/Model/Sync/Status.php +149 -0
  131. app/code/community/Copernica/MarketingSoftware/Model/SyncStatus.php +0 -140
  132. app/code/community/Copernica/MarketingSoftware/controllers/Adminhtml/Marketingsoftware/AccessTokenController.php +68 -0
  133. app/code/community/Copernica/MarketingSoftware/controllers/Adminhtml/Marketingsoftware/AjaxcollectionController.php +149 -158
  134. app/code/community/Copernica/MarketingSoftware/controllers/Adminhtml/Marketingsoftware/ExportController.php +18 -50
  135. app/code/community/Copernica/MarketingSoftware/controllers/Adminhtml/Marketingsoftware/LinkController.php +73 -136
  136. app/code/community/Copernica/MarketingSoftware/controllers/Adminhtml/Marketingsoftware/SettingsController.php +54 -76
  137. app/code/community/Copernica/MarketingSoftware/controllers/Adminhtml/Marketingsoftware/SyncController.php +37 -55
  138. app/code/community/Copernica/MarketingSoftware/controllers/ProductController.php +236 -203
  139. app/code/community/Copernica/MarketingSoftware/controllers/UnsubscribeController.php +6 -11
  140. app/code/community/Copernica/MarketingSoftware/cron/clearCopernicaDatabase.php +8 -14
  141. app/code/community/Copernica/MarketingSoftware/cron/detectAbandonedCarts.php +4 -10
  142. app/code/community/Copernica/MarketingSoftware/cron/fullSync.php +9 -17
  143. app/code/community/Copernica/MarketingSoftware/cron/processQueue.php +55 -65
  144. app/code/community/Copernica/MarketingSoftware/data/marketingsoftware_setup/data-upgrade-3.4.0-3.4.1.php +5 -1
  145. app/code/community/Copernica/MarketingSoftware/etc/adminhtml.xml +4 -0
  146. app/code/community/Copernica/MarketingSoftware/etc/config.xml +20 -12
  147. app/code/community/Copernica/MarketingSoftware/sql/marketingsoftware_setup/mysql4-upgrade-2.3.2-2.3.3.php +1 -1
  148. app/code/community/Copernica/MarketingSoftware/sql/marketingsoftware_setup/mysql4-upgrade-2.3.8-2.4.0.php +8 -1
  149. app/code/community/Copernica/MarketingSoftware/sql/marketingsoftware_setup/mysql4-upgrade-2.4.1-2.4.2.php +1 -1
  150. app/code/community/Copernica/MarketingSoftware/sql/marketingsoftware_setup/mysql4-upgrade-2.4.2-2.4.3.php +2 -2
  151. app/code/community/Copernica/MarketingSoftware/sql/marketingsoftware_setup/mysql4-upgrade-2.4.3-2.4.4.php +1 -1
  152. app/design/adminhtml/default/default/template/marketingsoftware/accessToken.phtml +43 -0
  153. app/design/adminhtml/default/default/template/marketingsoftware/export.phtml +1 -1
  154. app/design/adminhtml/default/default/template/marketingsoftware/link.phtml +16 -9
  155. app/design/adminhtml/default/default/template/marketingsoftware/settings.phtml +24 -17
  156. js/copernica/marketingsoftware/link.js +11 -7
  157. package.xml +6 -6
app/code/community/Copernica/MarketingSoftware/{Model/QueueEvent/QuoteItemAdd.php → Block/Adminhtml/Marketingsoftware/Accesstoken.php} RENAMED
@@ -24,17 +24,37 @@
24
  * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
  */
26
 
27
- /**
28
- * A wrapper object around an event
29
  */
30
- class Copernica_MarketingSoftware_Model_QueueEvent_QuoteItemAdd extends Copernica_MarketingSoftware_Model_QueueEvent_QuoteItem
31
  {
32
  /**
33
- * In what status is this cart item
34
- * @return String
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
  */
36
- protected function status()
37
  {
38
- return 'basket';
39
  }
40
  }
24
  * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
  */
26
 
27
+ /**
28
+ * Accesstoken Block
29
  */
30
+ class Copernica_MarketingSoftware_Block_Adminhtml_Marketingsoftware_Accesstoken extends Mage_Core_Block_Template
31
  {
32
  /**
33
+ * Constructor
34
+ */
35
+ public function __construct()
36
+ {
37
+ parent::__construct();
38
+ $this->setTemplate('marketingsoftware/accessToken.phtml');
39
+ }
40
+
41
+ /**
42
+ * Returns the post URL.
43
+ *
44
+ * @return string
45
+ */
46
+ public function getPostUrl()
47
+ {
48
+ return $this->getUrl('*/*/send', array('_secure' => true));
49
+ }
50
+
51
+ /**
52
+ * Get current access token value
53
+ *
54
+ * @return string
55
  */
56
+ public function getAccessTokenValue()
57
  {
58
+ return Mage::helper('marketingsoftware/config')->getAccessToken();
59
  }
60
  }
app/code/community/Copernica/MarketingSoftware/Block/Adminhtml/Marketingsoftware/Export.php CHANGED
@@ -61,6 +61,7 @@ class Copernica_MarketingSoftware_Block_Adminhtml_Marketingsoftware_Export exten
61
 
62
  /**
63
  * Get url that will try to start data sync.
 
64
  * @return string
65
  */
66
  public function getDataUrl()
61
 
62
  /**
63
  * Get url that will try to start data sync.
64
+ *
65
  * @return string
66
  */
67
  public function getDataUrl()
app/code/community/Copernica/MarketingSoftware/Block/Adminhtml/Marketingsoftware/Link.php CHANGED
@@ -41,6 +41,7 @@ class Copernica_MarketingSoftware_Block_Adminhtml_Marketingsoftware_Link extends
41
 
42
  /**
43
  * Return Ajax url that can be used to validate collection
 
44
  * @return string
45
  */
46
  public function getAjaxCollectionValidateUrl()
@@ -50,6 +51,7 @@ class Copernica_MarketingSoftware_Block_Adminhtml_Marketingsoftware_Link extends
50
 
51
  /**
52
  * Return Ajax url that can be used ti fetch information about collection field
 
53
  * @return string
54
  */
55
  public function getAjaxCollectionFetchUrl()
@@ -59,6 +61,7 @@ class Copernica_MarketingSoftware_Block_Adminhtml_Marketingsoftware_Link extends
59
 
60
  /**
61
  * Return Ajax url that can be used to store collection.
 
62
  * @return string
63
  */
64
  public function getAjaxCollectionStoreUrl()
@@ -68,6 +71,7 @@ class Copernica_MarketingSoftware_Block_Adminhtml_Marketingsoftware_Link extends
68
 
69
  /**
70
  * Return Ajax url that can be used to create default structure.
 
71
  * @return string
72
  */
73
  public function getAjaxCollectionDefaultUrl()
41
 
42
  /**
43
  * Return Ajax url that can be used to validate collection
44
+ *
45
  * @return string
46
  */
47
  public function getAjaxCollectionValidateUrl()
51
 
52
  /**
53
  * Return Ajax url that can be used ti fetch information about collection field
54
+ *
55
  * @return string
56
  */
57
  public function getAjaxCollectionFetchUrl()
61
 
62
  /**
63
  * Return Ajax url that can be used to store collection.
64
+ *
65
  * @return string
66
  */
67
  public function getAjaxCollectionStoreUrl()
71
 
72
  /**
73
  * Return Ajax url that can be used to create default structure.
74
+ *
75
  * @return string
76
  */
77
  public function getAjaxCollectionDefaultUrl()
app/code/community/Copernica/MarketingSoftware/Block/Adminhtml/Marketingsoftware/Settings.php CHANGED
@@ -51,6 +51,7 @@ class Copernica_MarketingSoftware_Block_Adminhtml_Marketingsoftware_Settings ext
51
 
52
  /**
53
  * Returns the state URL
 
54
  * @return string
55
  */
56
  public function getStateUrl()
@@ -60,6 +61,7 @@ class Copernica_MarketingSoftware_Block_Adminhtml_Marketingsoftware_Settings ext
60
 
61
  /**
62
  * Returns the queue URL
 
63
  * @return string
64
  */
65
  public function getQueuePostUrl()
@@ -69,10 +71,21 @@ class Copernica_MarketingSoftware_Block_Adminhtml_Marketingsoftware_Settings ext
69
 
70
  /**
71
  * Returns the stores URL
 
72
  * @return string
73
  */
74
  public function getStoresPostUrl()
75
  {
76
  return Mage::helper('adminhtml')->getUrl('*/*/stores');
77
  }
 
 
 
 
 
 
 
 
 
 
78
  }
51
 
52
  /**
53
  * Returns the state URL
54
+ *
55
  * @return string
56
  */
57
  public function getStateUrl()
61
 
62
  /**
63
  * Returns the queue URL
64
+ *
65
  * @return string
66
  */
67
  public function getQueuePostUrl()
71
 
72
  /**
73
  * Returns the stores URL
74
+ *
75
  * @return string
76
  */
77
  public function getStoresPostUrl()
78
  {
79
  return Mage::helper('adminhtml')->getUrl('*/*/stores');
80
  }
81
+
82
+ /**
83
+ * Access token form url
84
+ *
85
+ * @return string
86
+ */
87
+ public function getAccessTokenUrl()
88
+ {
89
+ return Mage::helper('adminhtml')->getUrl('*/marketingsoftware_accessToken/index');
90
+ }
91
  }
app/code/community/Copernica/MarketingSoftware/Block/Adminhtml/Marketingsoftware/Sync.php CHANGED
@@ -40,6 +40,7 @@ class Copernica_MarketingSoftware_Block_Adminhtml_MarketingSoftware_Sync extends
40
 
41
  /**
42
  * Get url that will point to 'post' action in contoller
 
43
  * @return string
44
  */
45
  public function getPostUrl()
@@ -49,15 +50,17 @@ class Copernica_MarketingSoftware_Block_Adminhtml_MarketingSoftware_Sync extends
49
 
50
  /**
51
  * Get sync profiles collection
 
52
  * @return Copernica_MarketingSoftware_Model_Mysql4_SyncProfile_Collection
53
  */
54
  public function getSyncProfiles()
55
  {
56
- return Mage::getModel('marketingsoftware/syncProfile')->getCollection();
57
  }
58
 
59
  /**
60
  * Get url that will point to 'get profile' action in contoller
 
61
  * @return string
62
  */
63
  public function getProfileUrl()
@@ -67,6 +70,7 @@ class Copernica_MarketingSoftware_Block_Adminhtml_MarketingSoftware_Sync extends
67
 
68
  /**
69
  * Get url that will point to delete action in controller
 
70
  * @return string
71
  */
72
  public function getDeleteUrl()
@@ -76,6 +80,8 @@ class Copernica_MarketingSoftware_Block_Adminhtml_MarketingSoftware_Sync extends
76
 
77
  /**
78
  * Get url that will point to state action in controller
 
 
79
  * @return string
80
  */
81
  public function getStateUrl()
@@ -86,35 +92,27 @@ class Copernica_MarketingSoftware_Block_Adminhtml_MarketingSoftware_Sync extends
86
 
87
  /**
88
  * Return authorization string
 
89
  * @return string
90
  */
91
  public function getAuthorizationUrl()
92
  {
93
- // auth url without query
94
  $authUrl = 'https://www.copernica.com/en/authorize';
95
 
96
  $query = array (
97
- // get access to all resources
98
  'scope' => 'all',
99
-
100
- // we want response in form of a code
101
  'response_type' => 'code',
102
-
103
- // we do store state inside session. State will be assigned in javascript
104
  // 'state' => Mage::getSingleton('adminhtml/session')->getState(),
105
-
106
- // where we should return after we authorize
107
  'redirect_uri' => $this->getStateUrl(),
108
-
109
- // client id will be appended in javascript
110
  // 'client_id' => ''
111
  );
112
 
113
- // create qeury string
114
  $parts = array();
115
- foreach ($query as $key => $value) $parts[] = implode('=', array($key, urlencode($value)));
 
 
 
116
 
117
- // return nearly completed auth url
118
  return $authUrl.'?'.implode('&', $parts);
119
  }
120
  }
40
 
41
  /**
42
  * Get url that will point to 'post' action in contoller
43
+ *
44
  * @return string
45
  */
46
  public function getPostUrl()
50
 
51
  /**
52
  * Get sync profiles collection
53
+ *
54
  * @return Copernica_MarketingSoftware_Model_Mysql4_SyncProfile_Collection
55
  */
56
  public function getSyncProfiles()
57
  {
58
+ return Mage::getModel('marketingsoftware/sync_profile')->getCollection();
59
  }
60
 
61
  /**
62
  * Get url that will point to 'get profile' action in contoller
63
+ *
64
  * @return string
65
  */
66
  public function getProfileUrl()
70
 
71
  /**
72
  * Get url that will point to delete action in controller
73
+ *
74
  * @return string
75
  */
76
  public function getDeleteUrl()
80
 
81
  /**
82
  * Get url that will point to state action in controller
83
+ *
84
+ * @todo Two returns????
85
  * @return string
86
  */
87
  public function getStateUrl()
92
 
93
  /**
94
  * Return authorization string
95
+ *
96
  * @return string
97
  */
98
  public function getAuthorizationUrl()
99
  {
 
100
  $authUrl = 'https://www.copernica.com/en/authorize';
101
 
102
  $query = array (
 
103
  'scope' => 'all',
 
 
104
  'response_type' => 'code',
 
 
105
  // 'state' => Mage::getSingleton('adminhtml/session')->getState(),
 
 
106
  'redirect_uri' => $this->getStateUrl(),
 
 
107
  // 'client_id' => ''
108
  );
109
 
 
110
  $parts = array();
111
+
112
+ foreach ($query as $key => $value) {
113
+ $parts[] = implode('=', array($key, urlencode($value)));
114
+ }
115
 
 
116
  return $authUrl.'?'.implode('&', $parts);
117
  }
118
  }
app/code/community/Copernica/MarketingSoftware/Controller/{Base.php → Action.php} RENAMED
@@ -27,7 +27,7 @@
27
  /**
28
  * This class will be a base for our visual constrollers.
29
  */
30
- class Copernica_MarketingSoftware_Controller_Base extends Mage_Adminhtml_Controller_Action
31
  {
32
  /**
33
  * Since default magento notification system is being derpy from version to
@@ -42,137 +42,140 @@ class Copernica_MarketingSoftware_Controller_Base extends Mage_Adminhtml_Control
42
  *
43
  * @var array
44
  */
45
- private $notes = array();
46
 
47
  /**
48
  * index action.
49
  */
50
  public function _construct()
51
  {
52
- // get queue
53
- $queue = Mage::getResourceModel('marketingsoftware/queue_collection');
 
54
 
55
- // check current queue state
56
- $this->checkQueueSize($queue);
57
 
58
- // check current queue time
59
- $this->checkQueueTime($queue);
60
-
61
- // we want to check api access
62
- $this->checkApiAccess();
63
  }
64
 
65
  /**
66
  * Check if queue have too many items. This method will output messages on
67
  * admin session to inform him about current state.
68
- * @param Copernica_MarketingSoftware_Model_Mysql4_Queue_Collection
 
69
  */
70
- private function checkQueueSize($queue)
71
  {
72
- // get queue size
73
  $queueSize = $queue->getSize();
74
 
75
- // we will not panic if queue size is below 100
76
- if ($queueSize < 100) return;
 
77
 
78
- // we will use our own notification system
79
- $this->addWarning(Mage::helper('marketingsoftware')->__("There is queue of %d local modification waiting to be processed", $queueSize));
80
  }
81
 
82
  /**
83
  * Check if queue have too old items.
84
- * @param Copernica_MarketingSoftware_Model_Mysql4_Queue_Collection
 
85
  */
86
- private function checkQueueTime($queue)
87
  {
88
- // get queue oldest item timestamp
89
  $oldestItemTimestamp = $queue->getQueueStartTime();
90
 
91
- // check if there is an oldest timestamp
92
- if (is_null($oldestItemTimestamp)) return;
 
93
 
94
- // if oldest item age is less than 24 hours we will not panic
95
- if (time() - strtotime($oldestItemTimestamp) < 60*60*24) return;
 
96
 
97
- // we want to get a printable time
98
  $printableTime = Mage::helper('core')->formatDate($oldestItemTimestamp, 'short', true);
99
 
100
- // add warning to controller
101
- $this->addWarning(Mage::helper('marketingsoftware')->__("There is still a modification of %s that is not synchronized with Copernica.", $printableTime));
102
  }
103
 
104
  /**
105
  * This function will check if we have an access to API.
106
  */
107
- private function checkApiAccess()
108
  {
109
- // try to get access token from config file
110
  $accessToken = Mage::helper('marketingsoftware/config')->getAccessToken();
111
 
112
- // if we have someting in access token we will assume that it's correct
113
- if ($accessToken) return;
 
114
 
115
- // add warning to controller
116
- $this->addWarning(Mage::helper('marketingsoftware')->__('There is no access token for API communication.'));
117
  }
118
 
119
  /**
120
  * Add warning message to controller.
121
- * @param string
 
122
  * @return self
123
  */
124
- protected function addWarning($text)
125
  {
126
- $this->notes['warnings'][] = $text;
 
127
  return $this;
128
  }
129
 
130
  /**
131
  * Add error message to controller.
132
- * @param string
 
 
133
  * @return self
134
  */
135
- protected function addError($text)
136
  {
137
- $this->notes['errors'][] = $text;
 
138
  return $this;
139
  }
140
 
141
  /**
142
  * Get current list of warnings.
 
143
  * @return array
144
  */
145
  public function getWarnings()
146
  {
147
- return $this->getNotifications('warnings');
148
  }
149
 
150
  /**
151
  * Get current list of errors
 
152
  * @return array
153
  */
154
  public function getErrors()
155
  {
156
- return $this->getNotifications('errors');
157
  }
158
 
159
  /**
160
  * Get notifications of given type
161
- * @param string
 
162
  * @return array
163
  */
164
- private function getNotifications($type)
165
  {
166
- // check if we did set some notes of given type
167
- if (!array_key_exists($type, $this->notes)) return array();
 
168
 
169
- // get notes of given type array
170
- $output = $this->notes[$type];
171
 
172
- // ensure that output is an array
173
- if(is_array($output)) return $output;
 
174
 
175
- // return empty array
176
  return array();
177
  }
178
  }
27
  /**
28
  * This class will be a base for our visual constrollers.
29
  */
30
+ class Copernica_MarketingSoftware_Controller_Action extends Mage_Adminhtml_Controller_Action
31
  {
32
  /**
33
  * Since default magento notification system is being derpy from version to
42
  *
43
  * @var array
44
  */
45
+ protected $_notes = array();
46
 
47
  /**
48
  * index action.
49
  */
50
  public function _construct()
51
  {
52
+ $queue = Mage::getModel('marketingsoftware/queue_item')->getCollection();
53
+
54
+ $this->_checkQueueSize($queue);
55
 
56
+ $this->_checkQueueTime($queue);
 
57
 
58
+ $this->_checkApiAccess();
 
 
 
 
59
  }
60
 
61
  /**
62
  * Check if queue have too many items. This method will output messages on
63
  * admin session to inform him about current state.
64
+ *
65
+ * @param Copernica_MarketingSoftware_Model_Mysql4_Queue_Item_Collection $queue
66
  */
67
+ protected function _checkQueueSize(Copernica_MarketingSoftware_Model_Mysql4_Queue_Item_Collection $queue)
68
  {
 
69
  $queueSize = $queue->getSize();
70
 
71
+ if ($queueSize < 100) {
72
+ return;
73
+ }
74
 
75
+ $this->_addWarning(Mage::helper('marketingsoftware')->__("There is queue of %d local modification waiting to be processed", $queueSize));
 
76
  }
77
 
78
  /**
79
  * Check if queue have too old items.
80
+ *
81
+ * @param Copernica_MarketingSoftware_Model_Mysql4_Queue_Item_Collection $queue
82
  */
83
+ protected function _checkQueueTime(Copernica_MarketingSoftware_Model_Mysql4_Queue_Item_Collection $queue)
84
  {
 
85
  $oldestItemTimestamp = $queue->getQueueStartTime();
86
 
87
+ if (is_null($oldestItemTimestamp)) {
88
+ return;
89
+ }
90
 
91
+ if (time() - strtotime($oldestItemTimestamp) < 60*60*24) {
92
+ return;
93
+ }
94
 
 
95
  $printableTime = Mage::helper('core')->formatDate($oldestItemTimestamp, 'short', true);
96
 
97
+ $this->_addWarning(Mage::helper('marketingsoftware')->__("There is still a modification of %s that is not synchronized with Copernica.", $printableTime));
 
98
  }
99
 
100
  /**
101
  * This function will check if we have an access to API.
102
  */
103
+ protected function _checkApiAccess()
104
  {
 
105
  $accessToken = Mage::helper('marketingsoftware/config')->getAccessToken();
106
 
107
+ if ($accessToken) {
108
+ return;
109
+ }
110
 
111
+ $this->_addWarning(Mage::helper('marketingsoftware')->__('There is no access token for API communication.'));
 
112
  }
113
 
114
  /**
115
  * Add warning message to controller.
116
+ *
117
+ * @param string $text
118
  * @return self
119
  */
120
+ protected function _addWarning($text)
121
  {
122
+ $this->_notes['warnings'][] = $text;
123
+
124
  return $this;
125
  }
126
 
127
  /**
128
  * Add error message to controller.
129
+ *
130
+ * @todo Not used???
131
+ * @param string $text
132
  * @return self
133
  */
134
+ protected function _addError($text)
135
  {
136
+ $this->_notes['errors'][] = $text;
137
+
138
  return $this;
139
  }
140
 
141
  /**
142
  * Get current list of warnings.
143
+ *
144
  * @return array
145
  */
146
  public function getWarnings()
147
  {
148
+ return $this->_getNotifications('warnings');
149
  }
150
 
151
  /**
152
  * Get current list of errors
153
+ *
154
  * @return array
155
  */
156
  public function getErrors()
157
  {
158
+ return $this->_getNotifications('errors');
159
  }
160
 
161
  /**
162
  * Get notifications of given type
163
+ *
164
+ * @param string $type
165
  * @return array
166
  */
167
+ protected function _getNotifications($type)
168
  {
169
+ if (!array_key_exists($type, $this->_notes)) {
170
+ return array();
171
+ }
172
 
173
+ $output = $this->_notes[$type];
 
174
 
175
+ if(is_array($output)) {
176
+ return $output;
177
+ }
178
 
 
179
  return array();
180
  }
181
  }
app/code/community/Copernica/MarketingSoftware/Exception.php CHANGED
@@ -57,7 +57,7 @@ class Copernica_MarketingSoftware_Exception extends Exception
57
  // structure of field is invalid
58
  const FIELD_STRUCT_INVALID = 7;
59
 
60
- // collection does not exists
61
  const COLLECTION_NOT_EXISTS = 8;
62
 
63
  // invalid collection type
57
  // structure of field is invalid
58
  const FIELD_STRUCT_INVALID = 7;
59
 
60
+ // collection does not exist
61
  const COLLECTION_NOT_EXISTS = 8;
62
 
63
  // invalid collection type
app/code/community/Copernica/MarketingSoftware/Helper/Api.php CHANGED
@@ -29,80 +29,74 @@
29
  * This class holds methods to communicate with Copernica REST API. It's also
30
  * a facade for validation and creation classes.
31
  */
32
- class Copernica_MarketingSoftware_Helper_Api extends Copernica_MarketingSoftware_Helper_ApiBase
33
  {
34
  /**
35
  * Upgrade request token data into access token via REST call.
36
- * @param string The client Id
37
- * @param string The client secret
38
- * @param string Code that we did get from Copernica authorization page
39
- * @param string Our landing page for state handling
40
- * @return string The access token or false when we can not upgrade
 
41
  */
42
- public function upgradeRequest($clientId, $clientSecret, $code, $redirectUri)
43
  {
44
- // make an upgrade request
45
- $output = $this->request()->get('token', array(
46
  'client_id' => $clientId,
47
- 'client_secret' => $clientSecret,
48
- 'code' => $code,
49
- 'redirect_uri' => $redirectUri
50
  ));
51
 
52
- // check if we have proper access token
53
- if (isset($output['access_token'])) return $output['access_token'];
 
54
 
55
- // return output from API
56
  return false;
57
  }
58
 
59
  /**
60
  * Search for profiles that match certain identifier
61
- * @param string
 
62
  * @return array
63
  */
64
  public function searchProfiles($identifier)
65
  {
66
- // get the profiles
67
- $profiles = $this->request()->get(
68
  'database/'.$this->getDatabaseId().'/profiles',
69
  array(
70
  'fields[]' => 'customer_id=='.$identifier
71
  )
72
  );
73
 
74
- // return profiels
75
  return $profiles;
76
  }
77
 
78
  /**
79
  * Update the profiles given a customer.
80
- * @param Copernica_MarketingSoftware_Model_Copernica_ProfileCustomer
 
81
  */
82
- public function updateProfiles($customer)
83
  {
84
- // if we are calling this function with an incorrect customer, we want
85
- // to break execution. We just don't want to make damages.
86
- if ($customer->originalId() == false)
87
- {
88
- // log data that could help with fixing this problem
89
  Mage::log('Identifier has type'.gettype($customer->id()).' and value '.($customer->id() ? 'true' : 'false'));
90
  Mage::log('Data is of type: '.get_class($customer));
91
  Mage::log('Data: '.print_r($data->toArray(), true));
92
- foreach (debug_backtrace() as $tr) Mage::log(' '.$tr['file'].''.$tr['line']);
 
 
 
93
 
94
- // we are done here
95
  return;
96
  }
97
 
98
- // try to get profile Id
99
  $profileId = $this->getProfileId($customer);
100
 
101
- // if we don't have a profile Id we will put customet to copernica
102
- if ($profileId === false)
103
- {
104
- // update profiles
105
- $this->request()->put(
106
  'database/'.$this->getDatabaseId().'/profiles',
107
  $customer->toArray(),
108
  array (
@@ -110,12 +104,8 @@ class Copernica_MarketingSoftware_Helper_Api extends Copernica_MarketingSoftware
110
  'create' => 'true'
111
  )
112
  );
113
- }
114
-
115
- // for various reasons we did indentified the profile
116
- else
117
- {
118
- $this->request()->put(
119
  'profile/'.$profileId.'/fields',
120
  $customer->toArray()
121
  );
@@ -123,48 +113,47 @@ class Copernica_MarketingSoftware_Helper_Api extends Copernica_MarketingSoftware
123
  }
124
 
125
  /**
126
- * Remove the profile by customer instane
127
- * @param Copernica_MarketingSoftware_Model_Copernica_ProfileCustomer
 
128
  */
129
- public function removeProfiles($customer)
130
  {
131
- // check if we have a valid id
132
- if ($customer->id() === false) return false;
 
133
 
134
- // we have to find a profile to remove it
135
- $output = $this->request()->get(
136
  'database/'.$this->getDatabaseId().'/profiles',
137
  array (
138
  'fields[]' => 'customer_id=='.$customer->originalId()
139
  )
140
  );
141
 
142
- // error was returned
143
- if (!isset($output['data'])) return;
 
144
 
145
- /*
146
- * Iterate over all profiles to delete. We do not care about output cause
147
- * API will tell us that it did remove profile or there was no profile
148
- * to remove. Either way, there is no profile.
149
- */
150
- foreach ($output['data'] as $profile) $this->request()->delete('profile/'.$profile['ID']);
151
  }
152
 
153
  /**
154
- * Update or create cart item sub profile.
155
- * @param string customer identifier
156
- * @param Copernica_MarketingSoftware_Model_Copernica_Subprofile
 
157
  */
158
- public function updateCartItemSubProfiles($profileID, $data)
159
  {
160
- // get Id for car item subprofiles collection
161
- $collectionId = Mage::helper('marketingsoftware/config')->getCartItemsCollectionId();
162
 
163
- // check if we have collection Id
164
- if (empty($collectionId)) return false;
 
165
 
166
- // send a request to REST server
167
- $this->request()->put(
168
  'profile/'.$profileID.'/subprofiles/'.$collectionId,
169
  $data->toArray(),
170
  array('fields[]' => 'item_id=='.$data->id(), 'create' => 'true')
@@ -172,54 +161,54 @@ class Copernica_MarketingSoftware_Helper_Api extends Copernica_MarketingSoftware
172
  }
173
 
174
  /**
175
- * Remove old cart item
176
- * @param string customer identifier
177
- * @param integer quote item id
 
178
  */
179
- public function removeOldCartItem($profileID, $quoteID)
180
  {
181
- // get collection Id
182
- $collectionId = Mage::helper('marketingsoftware/config')->getCartItemsCollectionId();
183
 
184
- // check if we have a collection Id
185
- if (empty($collectionId)) return false;
 
186
 
187
- // try to get an item to delete
188
- $output = $this->request()->get(
189
  'profile/'.$profileID.'/subprofiles/'.$collectionId,
190
  array (
191
  'fields[]' => 'quote_id=='.$quote_id
192
  )
193
  );
194
 
195
- // check if we have an error
196
- if (!isset($output['total'])) return false;
 
197
 
198
- // check if we have an item to remove
199
- if ($output['total'] == 0) return true;
 
200
 
201
- // iterate over all items to remove
202
- foreach($output['data'] as $subprofile)
203
- {
204
- $this->request()->delete('subprofile/'.$subprofile['ID']);
205
  }
206
  }
207
 
208
  /**
209
  * Update order subprofile
210
- * @param string the customer identifier
211
- * @param Copernica_MarketingSoftware_Model_Copernica_Subprofile
 
212
  */
213
- public function updateOrderSubProfile($profileID, $data)
214
  {
215
- // get Id for order subprofiles collection
216
  $collectionId = Mage::helper('marketingsoftware/config')->getOrdersCollectionId();
217
 
218
- // check if we have a collection Id
219
- if (empty($collectionId)) return false;
 
220
 
221
- // send a request to REST server
222
- $this->request()->put(
223
  'profile/'.$profileID.'/subprofiles/'.$collectionId,
224
  $data->toArray(),
225
  array('fields[]' => 'order_id=='.$data->id(), 'create' => 'true')
@@ -228,40 +217,61 @@ class Copernica_MarketingSoftware_Helper_Api extends Copernica_MarketingSoftware
228
 
229
  /**
230
  * Update the order item subprofiles in a profile.
231
- * @param string customer identifier
232
- * @param Copernica_MarketingSoftware_Model_Copernica_Subprofile
 
233
  */
234
- public function updateOrderItemSubProfiles($profileID, $data)
235
  {
236
- // get Id for order items subprofiles collection
237
- $collectionId = Mage::helper('marketingsoftware/config')->getOrderItemsCollectionId();
238
 
239
- // check if we have a collection Id
240
- if (empty($collectionId)) return false;
 
241
 
242
- // send a request to REST server
243
- $this->request()->put(
244
  'profile/'.$profileID.'/subprofiles/'.$collectionId,
245
  $data->toArray(),
246
  array('fields[]' => 'item_id=='.$data->id(), 'create' => 'true')
247
  );
248
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
249
 
250
  /**
251
  * Update address subprofile in a profile
252
- * @param string customer identifier
253
- * @param Copernica_MarketingSoftware_Model_Copernica_Subprofile
 
254
  */
255
- public function updateAddressSubProfiles($profileID, $data)
256
  {
257
- // get Id for address subprofiles collection
258
  $collectionId = Mage::helper('marketingsoftware/config')->getAddressesCollectionId();
259
 
260
- // check if we have a collection Id
261
- if (empty($collectionId)) return false;
 
262
 
263
- // send a request to REST server
264
- $this->request()->put(
265
  'profile/'.$profileID.'/subprofiles/'.$collectionId,
266
  $data->toArray(),
267
  array('fields[]' => 'address_id=='.$data->id(), 'create' => 'true')
@@ -270,41 +280,43 @@ class Copernica_MarketingSoftware_Helper_Api extends Copernica_MarketingSoftware
270
 
271
  /**
272
  * Update product views subprofile in a certain profile.
273
- * @param string customer identifier
274
- * @param Copernica_MarketingSoftware_Model_Copernica_Subprofile
 
275
  */
276
- public function updateViewedProductSubProfiles($profileID, $data)
277
  {
278
- // get Id for viewed products subprofiles collection
279
  $collectionId = Mage::helper('marketingsoftware/config')->getViewedProductCollectionId();
280
 
281
- // check if we have a collection Id
282
- if (empty($collectionId)) return false;
 
283
 
284
- // send a request to REST server
285
- $this->request()->put(
286
- 'profile/'.$profileID.'/subprofiles/'.$collectionId,
287
- $data->toArray(),
288
- array('fields[]' => 'id=='.$data->id(), 'create' => 'true')
289
  );
290
  }
291
 
292
  /**
293
- * Remove old cart items
294
- * @param string Profile ID
295
- * @param string quote ID
 
296
  * @return bool
297
  */
298
- public function removeOldCartItems($profileID, $quoteId)
299
  {
300
- // get collection Id
301
- $collectionId = Mage::helper('marketingsoftware/config')->getCartItemsCollectionId();
302
 
303
- // check if we have a collection Id
304
- if (empty($collectionId)) return false;
 
305
 
306
- // get all subprofiles that we want to remove as old cart items
307
- $output = $this->request()->get(
308
  'profile/'.$profileID.'/subprofiles/'.$collectionId,
309
  array (
310
  'fields' => array(
@@ -314,34 +326,28 @@ class Copernica_MarketingSoftware_Helper_Api extends Copernica_MarketingSoftware
314
  )
315
  );
316
 
317
- // check if we have an item
318
- if (isset($output['error'])) return false;
319
-
320
- // get request into local scope
321
- $request = $this->request();
322
 
323
- // prepare request to sent multiple calls at once
324
  $request->prepare();
325
 
326
- // iterate over all subprofiles data
327
- foreach ($output['data'] as $subprofile)
328
- {
329
- // send a delete call
330
- $this->request()->delete(
331
  'subprofile/'.$subprofile['ID']
332
  );
333
  }
334
 
335
- // commit all calls at once
336
  $request->commit();
337
 
338
- // we are ready here
339
  return true;
340
  }
341
 
342
  /**
343
  * Check if database exists
344
- * @param string
 
345
  * @return boolean
346
  */
347
  public function databaseExists($databaseName)
@@ -349,8 +355,7 @@ class Copernica_MarketingSoftware_Helper_Api extends Copernica_MarketingSoftware
349
  try {
350
  return $this->getDatabaseId($databaseName) > 0;
351
  }
352
- catch(Exception $e)
353
- {
354
  return false;
355
  }
356
  }
29
  * This class holds methods to communicate with Copernica REST API. It's also
30
  * a facade for validation and creation classes.
31
  */
32
+ class Copernica_MarketingSoftware_Helper_Api extends Copernica_MarketingSoftware_Helper_Api_Abstract
33
  {
34
  /**
35
  * Upgrade request token data into access token via REST call.
36
+ *
37
+ * @param string $clientId
38
+ * @param string $clientSecret
39
+ * @param string $redirectUri
40
+ * @param string $code
41
+ * @return string
42
  */
43
+ public function upgradeRequest($clientId, $clientSecret, $redirectUri, $code)
44
  {
45
+ $output = $this->_restRequest()->get('token', array(
 
46
  'client_id' => $clientId,
47
+ 'client_secret' => $clientSecret,
48
+ 'redirect_uri' => $redirectUri,
49
+ 'code' => $code
50
  ));
51
 
52
+ if (isset($output['access_token'])) {
53
+ return $output['access_token'];
54
+ }
55
 
 
56
  return false;
57
  }
58
 
59
  /**
60
  * Search for profiles that match certain identifier
61
+ *
62
+ * @param string $identifier
63
  * @return array
64
  */
65
  public function searchProfiles($identifier)
66
  {
67
+ $profiles = $this->_restRequest()->get(
 
68
  'database/'.$this->getDatabaseId().'/profiles',
69
  array(
70
  'fields[]' => 'customer_id=='.$identifier
71
  )
72
  );
73
 
 
74
  return $profiles;
75
  }
76
 
77
  /**
78
  * Update the profiles given a customer.
79
+ *
80
+ * @param Copernica_MarketingSoftware_Model_Copernica_Profile_Customer $customer
81
  */
82
+ public function updateProfiles(Copernica_MarketingSoftware_Model_Copernica_Profile_Customer $customer)
83
  {
84
+ if ($customer->originalId() == false) {
 
 
 
 
85
  Mage::log('Identifier has type'.gettype($customer->id()).' and value '.($customer->id() ? 'true' : 'false'));
86
  Mage::log('Data is of type: '.get_class($customer));
87
  Mage::log('Data: '.print_r($data->toArray(), true));
88
+
89
+ foreach (debug_backtrace() as $tr) {
90
+ Mage::log(' '.$tr['file'].''.$tr['line']);
91
+ }
92
 
 
93
  return;
94
  }
95
 
 
96
  $profileId = $this->getProfileId($customer);
97
 
98
+ if ($profileId === false) {
99
+ $this->_restRequest()->put(
 
 
 
100
  'database/'.$this->getDatabaseId().'/profiles',
101
  $customer->toArray(),
102
  array (
104
  'create' => 'true'
105
  )
106
  );
107
+ } else {
108
+ $this->_restRequest()->put(
 
 
 
 
109
  'profile/'.$profileId.'/fields',
110
  $customer->toArray()
111
  );
113
  }
114
 
115
  /**
116
+ * Remove the profile by customer instance
117
+ *
118
+ * @param Copernica_MarketingSoftware_Model_Copernica_Profile_Customer $customer
119
  */
120
+ public function removeProfiles(Copernica_MarketingSoftware_Model_Copernica_Profile_Customer $customer)
121
  {
122
+ if ($customer->getId() === false) {
123
+ return false;
124
+ }
125
 
126
+ $output = $this->_restRequest()->get(
 
127
  'database/'.$this->getDatabaseId().'/profiles',
128
  array (
129
  'fields[]' => 'customer_id=='.$customer->originalId()
130
  )
131
  );
132
 
133
+ if (!isset($output['data'])) {
134
+ return;
135
+ }
136
 
137
+ foreach ($output['data'] as $profile) {
138
+ $this->_restRequest()->delete('profile/'.$profile['ID']);
139
+ }
 
 
 
140
  }
141
 
142
  /**
143
+ * Update or create quote item sub profile.
144
+ *
145
+ * @param string $profileID
146
+ * @param Copernica_MarketingSoftware_Model_Copernica_Subprofile $data
147
  */
148
+ public function updateQuoteItemSubProfiles($profileID, Copernica_MarketingSoftware_Model_Copernica_Subprofile $data)
149
  {
150
+ $collectionId = Mage::helper('marketingsoftware/config')->getQuoteItemCollectionId();
 
151
 
152
+ if (empty($collectionId)) {
153
+ return false;
154
+ }
155
 
156
+ $this->_restRequest()->put(
 
157
  'profile/'.$profileID.'/subprofiles/'.$collectionId,
158
  $data->toArray(),
159
  array('fields[]' => 'item_id=='.$data->id(), 'create' => 'true')
161
  }
162
 
163
  /**
164
+ * Remove old quote item
165
+ *
166
+ * @param string $profileID
167
+ * @param integer $quoteID
168
  */
169
+ public function removeOldQuoteItem($profileID, $quoteID)
170
  {
171
+ $collectionId = Mage::helper('marketingsoftware/config')->getQuoteItemCollectionId();
 
172
 
173
+ if (empty($collectionId)) {
174
+ return false;
175
+ }
176
 
177
+ $output = $this->_restRequest()->get(
 
178
  'profile/'.$profileID.'/subprofiles/'.$collectionId,
179
  array (
180
  'fields[]' => 'quote_id=='.$quote_id
181
  )
182
  );
183
 
184
+ if (!isset($output['total'])) {
185
+ return false;
186
+ }
187
 
188
+ if ($output['total'] == 0) {
189
+ return true;
190
+ }
191
 
192
+ foreach($output['data'] as $subprofile) {
193
+ $this->_restRequest()->delete('subprofile/'.$subprofile['ID']);
 
 
194
  }
195
  }
196
 
197
  /**
198
  * Update order subprofile
199
+ *
200
+ * @param string $profileID
201
+ * @param Copernica_MarketingSoftware_Model_Copernica_Subprofile $data
202
  */
203
+ public function updateOrderSubProfile($profileID, Copernica_MarketingSoftware_Model_Copernica_Subprofile $data)
204
  {
 
205
  $collectionId = Mage::helper('marketingsoftware/config')->getOrdersCollectionId();
206
 
207
+ if (empty($collectionId)) {
208
+ return false;
209
+ }
210
 
211
+ $this->_restRequest()->put(
 
212
  'profile/'.$profileID.'/subprofiles/'.$collectionId,
213
  $data->toArray(),
214
  array('fields[]' => 'order_id=='.$data->id(), 'create' => 'true')
217
 
218
  /**
219
  * Update the order item subprofiles in a profile.
220
+ *
221
+ * @param string customer identifier $profileID
222
+ * @param Copernica_MarketingSoftware_Model_Copernica_Orderitem_Subprofile $data
223
  */
224
+ public function updateOrderItemSubProfiles($profileID, Copernica_MarketingSoftware_Model_Copernica_Orderitem_Subprofile $data)
225
  {
226
+ $collectionId = Mage::helper('marketingsoftware/config')->getOrderItemCollectionId();
 
227
 
228
+ if (empty($collectionId)) {
229
+ return false;
230
+ }
231
 
232
+ $this->_restRequest()->put(
 
233
  'profile/'.$profileID.'/subprofiles/'.$collectionId,
234
  $data->toArray(),
235
  array('fields[]' => 'item_id=='.$data->id(), 'create' => 'true')
236
  );
237
+ }
238
+
239
+ /**
240
+ * Update the wishlist item subprofiles in a profile.
241
+ *
242
+ * @param string customer identifier $profileID
243
+ * @param Copernica_MarketingSoftware_Model_Copernica_Wishlist_Item_Subprofile $data
244
+ */
245
+ public function updateWishlistItemSubProfiles($profileID, Copernica_MarketingSoftware_Model_Copernica_Wishlist_Item_Subprofile $data)
246
+ {
247
+ $collectionId = Mage::helper('marketingsoftware/config')->getWishlistItemCollectionId();
248
+
249
+ if (empty($collectionId)) {
250
+ return false;
251
+ }
252
+
253
+ $this->_restRequest()->put(
254
+ 'profile/'.$profileID.'/subprofiles/'.$collectionId,
255
+ $data->toArray(),
256
+ array('fields[]' => 'item_id=='.$data->id(), 'create' => 'true')
257
+ );
258
+ }
259
 
260
  /**
261
  * Update address subprofile in a profile
262
+ *
263
+ * @param string $profileID
264
+ * @param Copernica_MarketingSoftware_Model_Copernica_Subprofile $data
265
  */
266
+ public function updateAddressSubProfiles($profileID, Copernica_MarketingSoftware_Model_Copernica_Subprofile $data)
267
  {
 
268
  $collectionId = Mage::helper('marketingsoftware/config')->getAddressesCollectionId();
269
 
270
+ if (empty($collectionId)) {
271
+ return false;
272
+ }
273
 
274
+ $this->_restRequest()->put(
 
275
  'profile/'.$profileID.'/subprofiles/'.$collectionId,
276
  $data->toArray(),
277
  array('fields[]' => 'address_id=='.$data->id(), 'create' => 'true')
280
 
281
  /**
282
  * Update product views subprofile in a certain profile.
283
+ *
284
+ * @param string $profileID
285
+ * @param Copernica_MarketingSoftware_Model_Copernica_Product_Viewed_Subprofile $data
286
  */
287
+ public function updateViewedProductSubProfiles($profileID, Copernica_MarketingSoftware_Model_Copernica_Product_Viewed_Subprofile $data)
288
  {
 
289
  $collectionId = Mage::helper('marketingsoftware/config')->getViewedProductCollectionId();
290
 
291
+ if (empty($collectionId)) {
292
+ return false;
293
+ }
294
 
295
+ $this->_restRequest()->put(
296
+ 'profile/'.$profileID.'/subprofiles/'.$collectionId, $data->toArray(), array(
297
+ 'fields[]' => 'id=='.$data->id(),
298
+ 'create' => 'true'
299
+ )
300
  );
301
  }
302
 
303
  /**
304
+ * Remove old quote items
305
+ *
306
+ * @param string $profileID
307
+ * @param string $quoteId
308
  * @return bool
309
  */
310
+ public function removeOldQuoteItems($profileID, $quoteId)
311
  {
312
+ $collectionId = Mage::helper('marketingsoftware/config')->getQuoteItemCollectionId();
 
313
 
314
+ if (empty($collectionId)) {
315
+ return false;
316
+ }
317
 
318
+ // Get all subprofiles that we want to remove as old quote items
319
+ $output = $this->_restRequest()->get(
320
  'profile/'.$profileID.'/subprofiles/'.$collectionId,
321
  array (
322
  'fields' => array(
326
  )
327
  );
328
 
329
+ if (isset($output['error'])) {
330
+ return false;
331
+ }
 
 
332
 
333
+ $request = $this->_restRequest();
334
  $request->prepare();
335
 
336
+ foreach ($output['data'] as $subprofile) {
337
+ $this->_restRequest()->delete(
 
 
 
338
  'subprofile/'.$subprofile['ID']
339
  );
340
  }
341
 
 
342
  $request->commit();
343
 
 
344
  return true;
345
  }
346
 
347
  /**
348
  * Check if database exists
349
+ *
350
+ * @param string $databaseName
351
  * @return boolean
352
  */
353
  public function databaseExists($databaseName)
355
  try {
356
  return $this->getDatabaseId($databaseName) > 0;
357
  }
358
+ catch(Exception $e) {
 
359
  return false;
360
  }
361
  }
app/code/community/Copernica/MarketingSoftware/Helper/Api/Abstract.php ADDED
@@ -0,0 +1,427 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Copernica Marketing Software
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0).
8
+ * It is available through the world-wide-web at this URL:
9
+ * http://opensource.org/licenses/osl-3.0.php
10
+ * If you are unable to obtain a copy of the license through the
11
+ * world-wide-web, please send an email to copernica@support.cream.nl
12
+ * so we can send you a copy immediately.
13
+ *
14
+ * DISCLAIMER
15
+ *
16
+ * Do not edit or add to this file if you wish to upgrade this software
17
+ * to newer versions in the future. If you wish to customize this module
18
+ * for your needs please refer to http://www.magento.com/ for more
19
+ * information.
20
+ *
21
+ * @category Copernica
22
+ * @package Copernica_MarketingSoftware
23
+ * @copyright Copyright (c) 2011-2012 Copernica & Cream. (http://docs.cream.nl/)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ /**
28
+ * A base class for API helpers. This class should have only the most general
29
+ * method that we will use.
30
+ */
31
+ class Copernica_MarketingSoftware_Helper_Api_Abstract extends Mage_Core_Helper_Abstract
32
+ {
33
+ /**
34
+ * Since Magento is cheating with theirs singleton implementation, we want
35
+ * to use static fields for storing request instance.
36
+ *
37
+ * @var Copernica_MarketingSoftware_Helper_Rest_Request
38
+ */
39
+ static protected $_restRequest = null;
40
+
41
+ /**
42
+ * Database id that is on Copernica platform.
43
+ *
44
+ * @var int
45
+ */
46
+ static protected $_databaseId = false;
47
+
48
+ /**
49
+ * Cache for collection ids
50
+ *
51
+ * @var array
52
+ */
53
+ static protected $_collectionIdCache = array();
54
+
55
+ /**
56
+ * Should we use profile cache?
57
+ *
58
+ * @var bool
59
+ */
60
+ protected $_useProfileCache = false;
61
+
62
+ /**
63
+ * Public, standard PHP constructor. Mage_Core_Helper_Abstract is not a child
64
+ * of Varien_Object, so we want to use good old PHP constructor.
65
+ */
66
+ public function __construct()
67
+ {
68
+ $this->init();
69
+ }
70
+
71
+ /**
72
+ * Initialize helper.
73
+ *
74
+ * @return Copernica_MarketingSoftware_Helper_Api_Abstract
75
+ */
76
+ public function init()
77
+ {
78
+ $this->_restRequest();
79
+
80
+ $this->_useProfileCache = Mage::helper('marketingsoftware/config')->getProfileCache();
81
+
82
+ return $this;
83
+ }
84
+
85
+ /**
86
+ * Get Request instance.
87
+ * We do cache/shield request instance mostly cause it's very specific to
88
+ * API and circumstances. That is why we don't want to rely on magento
89
+ * helpers as a final solution for reqeust instance.
90
+ *
91
+ * @return Copernica_MarketingSoftware_Helper_Rest_Request
92
+ */
93
+ protected function _restRequest()
94
+ {
95
+ if (!is_null(self::$_restRequest)) {
96
+ return self::$_restRequest;
97
+ }
98
+
99
+ return self::$_restRequest = Mage::helper('marketingsoftware/rest_request');
100
+ }
101
+
102
+ /**
103
+ * Set database Id
104
+ *
105
+ * @param int $id
106
+ */
107
+ protected function _setDatabaseId($id)
108
+ {
109
+ self::$_databaseId = $id;
110
+ }
111
+
112
+ /**
113
+ * Check if this API instance is valid.
114
+ *
115
+ * @param boolean $extensive
116
+ * @return boolean
117
+ */
118
+ public function check($extensive = false)
119
+ {
120
+ return $this->_restRequest()->check();
121
+ }
122
+
123
+ /**
124
+ * Get stored database Id. This function will return false when we can not
125
+ * get stored database Id.
126
+ *
127
+ * @return int|false
128
+ */
129
+ protected function _getStoredDatabaseId()
130
+ {
131
+ $databaseId = Mage::helper('marketingsoftware/config')->getDatabaseId();
132
+
133
+ if ($databaseId) {
134
+ return $databaseId;
135
+ }
136
+
137
+ $databaseName = Mage::helper('marketingsoftware/config')->getDatabaseName();
138
+
139
+ if (!$databaseName) {
140
+ return false;
141
+ }
142
+
143
+ $databaseId = $this->_getDatabaseIdByName($databaseName);
144
+
145
+ Mage::helper('marketingsoftware/config')->setDatabaseId($databaseId);
146
+
147
+ $this->_setDatabaseId($databaseId);
148
+
149
+ return $databaseId;
150
+ }
151
+
152
+ /**
153
+ * Get database Id by its name. This method will return false when we can
154
+ * not fetch database.
155
+ *
156
+ * @param string $databaseName
157
+ * @return int|false
158
+ */
159
+ protected function _getDatabaseIdByName($databaseName)
160
+ {
161
+ $output = $this->_restRequest()->get(
162
+ 'database/'.$databaseName
163
+ );
164
+
165
+ if (isset($output['error'])) {
166
+ return false;
167
+ }
168
+
169
+ return isset($output['ID']) ? $output['ID'] : false;
170
+ }
171
+
172
+ /**
173
+ * Get database id that will be used inside Copernica platform
174
+ *
175
+ * @param string|false $databaseName
176
+ * @return int
177
+ */
178
+ public function getDatabaseId($databaseName = false)
179
+ {
180
+ if ($databaseName === false) {
181
+ $databaseId = $this->_getStoredDatabaseID();
182
+ } else {
183
+ $databaseId = $this->_getDatabaseIdByName($databaseName);
184
+ }
185
+
186
+ return $databaseId;
187
+ }
188
+
189
+ /**
190
+ * Translate collection name into an ID.
191
+ *
192
+ * @param string $name
193
+ * @return int
194
+ */
195
+ public function getCollectionId($name)
196
+ {
197
+ if (!is_null(self::$_collectionIdCache) && array_key_exists($name, self::$_collectionIdCache)) {
198
+ return self::$_collectionIdCache[$name];
199
+ }
200
+
201
+ $output = $this->_restRequest()->get(
202
+ 'database/'.$this->getDatabaseId().'/collections'
203
+ );
204
+
205
+ if ($output['total'] == 0) {
206
+ return false;
207
+ }
208
+
209
+ self::$_collectionIdCache = array();
210
+
211
+ foreach ($output['data'] as $collection) {
212
+ self::$_collectionIdCache[$collection['name']] = $collection['ID'];
213
+ }
214
+
215
+ if (array_key_exists($name, self::$_collectionIdCache)) {
216
+ return self::$_collectionIdCache[$name];
217
+ }
218
+
219
+ return false;
220
+ }
221
+
222
+ /**
223
+ * Get copernica profile Id by magento customer Id.
224
+ * This method is little bit more flexible. It's possible to supply customer
225
+ * data by copernica model or by array with 'id', 'storeView', 'email' fields.
226
+ *
227
+ * @param Copernica_MarketingSoftware_Model_Copernica_Entity_Customer|array $customer
228
+ * @return int the copernica profile Id
229
+ */
230
+ public function getProfileId($customer)
231
+ {
232
+ if (!is_array($customer)) {
233
+ $customerId = $customer->getId();
234
+ $storeView = $customer->getStoreView();
235
+ $email = $customer->getEmail();
236
+ } else {
237
+ $customerId = $customer['id'];
238
+ $storeView = $customer['storeView'];
239
+ $email = $customer['email'];
240
+ }
241
+
242
+ $profileId = false;
243
+
244
+ if ($customerId === false) {
245
+ return false;
246
+ }
247
+
248
+ $profileCacheCollection = Mage::getModel('marketingsoftware/profile_cache')
249
+ ->getCollection()
250
+ ->setPageSize(1)
251
+ ->addFieldToFilter('copernica_id', $customerId)
252
+ ->addFieldToFilter('store_view', $storeView);
253
+
254
+ $profileCache = $profileCacheCollection->getFirstItem();
255
+
256
+ if (!$profileCache->isObjectNew()) {
257
+ $profileId = $profileCache->getProfileId();
258
+
259
+ if ($profileId > 0 && $profileId !== false && !is_null($profileId)) {
260
+ return $profileId;
261
+ } else {
262
+ $profileCache->delete();
263
+ }
264
+ }
265
+
266
+ if (strlen($email) && strlen($storeView)) {
267
+ $profileCacheCollection = Mage::getModel('marketingsoftware/profile_cache')
268
+ ->getCollection()
269
+ ->setPageSize(1)
270
+ ->addFieldToFilter('email', $email)
271
+ ->addFieldToFilter('store_view', $storeView);
272
+
273
+ $profileCache = $profileCacheCollection->getFirstItem();
274
+
275
+ $profileCopernicaId = $profileCache->getCopernicaId();
276
+
277
+ // if ($profileCopernicaId != $customerId && !is_null($customerId))
278
+ // {
279
+ // if (strpos($profileCopernicaId, '|') === false) {
280
+ // $profileCache->setCopernicaId($customerId);
281
+ // $profileCache->save();
282
+ // } elseif (is_string(array_shift(explode('|', $profileCopernicaId)))) {
283
+ // if (is_numeric(array_shift(explode('|', $customerId)))) {
284
+ // $profileCache->setCopernicaId($customerId);
285
+ // $profileCache->save();
286
+ // }
287
+ // }
288
+ // }
289
+
290
+ $profileId = $profileCache->getProfileId();
291
+
292
+ if ($profileId > 0 && $profileId !== false && !is_null($profileId)) {
293
+ return $profileId;
294
+ }
295
+ }
296
+
297
+ /**
298
+ * Could be that we will try to get a profile Id by email and store view
299
+ * only. In such case we should just skip this step cause we don't have
300
+ * a customer Id.
301
+ */
302
+ if (!is_null($customerId) && strlen($customerId)) {
303
+ $profileId = $this->_getProfileIdByCustomerId($customerId);
304
+ }
305
+
306
+ if ($profileId !== false) {
307
+ if ($profileId > 0 && $profileId !== false && !is_null($profileId)) {
308
+ if (strlen($email)) {
309
+ $profileCache->setEmail($email);
310
+ }
311
+
312
+ if (strlen($storeView)) {
313
+ $profileCache->setStoreView($storeView);
314
+ }
315
+
316
+ if (!is_null($customerId)) {
317
+ $profileCache->setCopernicaId($customerId);
318
+ }
319
+
320
+ $profileCache->setProfileId($profileId);
321
+ $profileCache->save();
322
+
323
+ return $profileId;
324
+ }
325
+ }
326
+
327
+ if (strlen($email) == 0) {
328
+ return false;
329
+ }
330
+
331
+ $profileId = $this->_getProfileIdByEmail($email, $storeView);
332
+
333
+ if ($profileId !== false) {
334
+ if (!is_null($customerId)) {
335
+ $profileCache->setCopernicaId($customerId);
336
+ }
337
+
338
+ $profileCache->setStoreView($storeView);
339
+ $profileCache->setEmail($email);
340
+ $profileCache->setProfileId($profileId);
341
+ $profileCache->save();
342
+
343
+ return $profileId;
344
+ }
345
+
346
+ return false;
347
+ }
348
+
349
+ /**
350
+ * Get profile Id by customer Id. When profile can not be found FALSE will
351
+ * be returned.
352
+ *
353
+ * @param string $customerId
354
+ * @return int|false
355
+ */
356
+ protected function _getProfileIdByCustomerId($customerId)
357
+ {
358
+ $profiles = $this->_restRequest()->get(
359
+ 'database/'.$this->getDatabaseId().'/profiles',
360
+ array (
361
+ 'fields[]' => 'customer_id=='.$customerId
362
+ )
363
+ );
364
+
365
+ if (!isset($profiles['data'][0])) {
366
+ return false;
367
+ }
368
+
369
+ if (is_null($profiles['data'][0]['ID']) || (int)$profiles['data'][0]['ID'] == 0) {
370
+ return false;
371
+ }
372
+
373
+ return $profiles['data'][0]['ID'];
374
+ }
375
+
376
+ /**
377
+ * Get profile Id by email address. FALSE will be returned if profile was not
378
+ * found.
379
+ *
380
+ * @param string $email
381
+ * @param string $storeView
382
+ * @return int
383
+ */
384
+ protected function _getProfileIdByEmail($email, $storeView)
385
+ {
386
+ $customerLinkedFields = Mage::helper('marketingsoftware/config')->getLinkedCustomerFields();
387
+
388
+ $requestParams = array(
389
+ $customerLinkedFields['email'].'=='.$email,
390
+ $customerLinkedFields['storeView'].'=='.$storeView
391
+ );
392
+
393
+ $profiles = $this->_restRequest()->get('database/'.$this->getDatabaseId().'/profiles', array ( 'fields' => $requestParams ));
394
+
395
+ if (!isset($profiles['data'][0])) {
396
+ return false;
397
+ }
398
+
399
+ if (is_null($profiles['data'][0]['ID']) || (int)$profiles['data'][0]['ID'] == 0) {
400
+ return false;
401
+ }
402
+
403
+ return $profiles['data'][0]['ID'];
404
+ }
405
+
406
+ /**
407
+ * Get profile Id from cache
408
+ *
409
+ * @todo not used, why??
410
+ * @param string $customerId
411
+ * @return int
412
+ */
413
+ protected function _getProfileIdFromCache($customerId)
414
+ {
415
+ $profileCache = Mage::getModel('marketingsoftware/profile_cache')
416
+ ->getCollection()
417
+ ->addFieldToFilter('customer_id', $customerId)
418
+ ->setPageSize(1)
419
+ ->getFirstItem();
420
+
421
+ $profileId = $profileCache->getProfileId();
422
+
423
+ if (!is_null($profileId)) {
424
+ return $profileId;
425
+ }
426
+ }
427
+ }
app/code/community/Copernica/MarketingSoftware/Helper/Api/Builder.php ADDED
@@ -0,0 +1,243 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Copernica Marketing Software
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0).
8
+ * It is available through the world-wide-web at this URL:
9
+ * http://opensource.org/licenses/osl-3.0.php
10
+ * If you are unable to obtain a copy of the license through the
11
+ * world-wide-web, please send an email to copernica@support.cream.nl
12
+ * so we can send you a copy immediately.
13
+ *
14
+ * DISCLAIMER
15
+ *
16
+ * Do not edit or add to this file if you wish to upgrade this software
17
+ * to newer versions in the future. If you wish to customize this module
18
+ * for your needs please refer to http://www.magento.com/ for more
19
+ * information.
20
+ *
21
+ * @category Copernica
22
+ * @package Copernica_MarketingSoftware
23
+ * @copyright Copyright (c) 2011-2012 Copernica & Cream. (http://docs.cream.nl/)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ /**
28
+ * This class will assist in any creation process performed on API.
29
+ */
30
+ class Copernica_MarketingSoftware_Helper_Api_Builder extends Copernica_MarketingSoftware_Helper_Api_Abstract
31
+ {
32
+ /**
33
+ * Will create proper database in Copernica platform
34
+ *
35
+ * @param string $databaseName
36
+ */
37
+ public function createDatabase($databaseName)
38
+ {
39
+ $data['name'] = $databaseName;
40
+ $data['description'] = 'Database created by magento extension.';
41
+
42
+ $this->_restRequest()->post('databases', $data);
43
+
44
+ $this->createDatabaseField($databaseName, 'customer_id', array(
45
+ 'type' => 'text',
46
+ 'length' => 64
47
+ ));
48
+ }
49
+
50
+ /**
51
+ * Create field in given database.
52
+ * For more info about what can be supplied in $options argument go:
53
+ * @see https://www.copernica.com/en/support/rest/database-fields
54
+ *
55
+ * @param string $databaseName
56
+ * @param string $fieldName
57
+ * @param array $options
58
+ */
59
+ public function createDatabaseField($databaseName, $fieldName, $options = array())
60
+ {
61
+ $options = array_merge( array ('name' => $fieldName), array('displayed' => true), $options);
62
+
63
+ $this->_restRequest()->post( 'database/'.urlencode($databaseName).'/fields', $options );
64
+ }
65
+
66
+ /**
67
+ * Helper method to create date fields.
68
+ *
69
+ * @param string $databaseName
70
+ * @param string $fieldName
71
+ */
72
+ public function createDatabaseDateField($databaseName, $fieldName)
73
+ {
74
+ $options['type'] = 'date';
75
+
76
+ $this->createDatabaseField($databaseName, $fieldName, $options);
77
+ }
78
+
79
+ /**
80
+ * Helper method to create datetime fields.
81
+ *
82
+ * @param string $databaseName
83
+ * @param string $fieldName
84
+ */
85
+ public function createDatabaseDatetimeField($databaseName, $fieldName)
86
+ {
87
+ $options['type'] = 'datetime';
88
+
89
+ $this->createDatabaseField($databaseName, $fieldName, $options);
90
+ }
91
+
92
+ /**
93
+ * Helper method to create email fields.
94
+ *
95
+ * @param string $databaseName
96
+ * @param string $fieldName
97
+ */
98
+ public function createDatabaseEmailField($databaseName, $fieldName)
99
+ {
100
+ $options['type'] = 'email';
101
+ $options['length'] = 160;
102
+
103
+ $this->createDatabaseField($databaseName, $fieldName, $options);
104
+ }
105
+
106
+ /**
107
+ * Helper method to create newsletter fields.
108
+ *
109
+ * @param string $databaseName
110
+ * @param string $fieldName
111
+ */
112
+ public function createDatabaseNewsletterField($databaseName, $fieldName)
113
+ {
114
+ $options['type'] = 'text';
115
+
116
+ $this->createDatabaseField($databaseName, $fieldName, $options);
117
+
118
+ $this->_restRequest()->post(
119
+ 'database/'.urlencode($databaseName).'/unsubscribe',
120
+ array (
121
+ 'behavior' => 'update',
122
+ 'fields' => array ( $fieldName => 'unsubscribed_copernica')
123
+ )
124
+ );
125
+
126
+ $this->_restRequest()->post(
127
+ 'database/'.urlencode($databaseName).'/callbacks',
128
+ array (
129
+ 'url' => Mage::helper('marketingsoftware')->unsubscribeCallbackUrl(),
130
+ 'method' => 'json',
131
+ 'expression' => "profile.$fieldName == 'unsubscribed_copernica';"
132
+ )
133
+ );
134
+ }
135
+
136
+ /**
137
+ * Create collection inside given database.
138
+ *
139
+ * @param string $databaseName
140
+ * @param string $collectionName
141
+ * @param string $collectionType
142
+ * @throws Copernica_MarketingSoftware_Exception
143
+ */
144
+ public function createCollection($databaseName, $collectionName, $collectionType)
145
+ {
146
+ $this->_restRequest()->post( 'database/'.urlencode($databaseName).'/collections', array ( 'name' => $collectionName ) );
147
+
148
+ $requiredFields = $this->_requiredCollectionFields($collectionType);
149
+
150
+ foreach ($requiredFields as $field) {
151
+ $this->createCollectionField($databaseName, $collectionName, $collectionType, $field, $field);
152
+ }
153
+ }
154
+
155
+ /**
156
+ * This method created collection field.
157
+ *
158
+ * @param string $databaseName
159
+ * @param string $collectionName
160
+ * @param string $collectionType
161
+ * @param string $copernicaName
162
+ * @param string $magentoName
163
+ * @throws Copernica_MarketingSoftware_Exception
164
+ */
165
+ public function createCollectionField($databaseName, $collectionName, $collectionType, $copernicaName, $magentoName)
166
+ {
167
+ $fieldDefinition = Mage::helper('marketingsoftware/data')->getCollectionFieldDefinition($collectionType, $magentoName);
168
+
169
+ $fieldDefinition = array_merge(array ('displayed' => true), $fieldDefinition);
170
+
171
+ $collectionId = $this->_getCollectionIdFromDatabase($databaseName, $collectionName);
172
+
173
+ $this->_restRequest()->post( 'collection/'.$collectionId.'/fields', array_merge($fieldDefinition, array(
174
+ 'name' => $copernicaName
175
+ )));
176
+ }
177
+
178
+ /**
179
+ * Get collection Id by it's name and database name that it's in.
180
+ *
181
+ * @param string $databaseName
182
+ * @param string $collectionName
183
+ * @return numeric
184
+ * @throws Copernica_MarketingSoftware_Exception
185
+ */
186
+ protected function _getCollectionIdFromDatabase($databaseName, $collectionName)
187
+ {
188
+ $output = $this->_restRequest()->get('database/'. urlencode($databaseName) .'/collections');
189
+
190
+ if (isset($output['error'])) {
191
+ if (strpos($output['error']['message'], 'No database') !== false) {
192
+ throw Mage::exception('Copernica_MarketingSoftware', 'No database', Copernica_MarketingSoftware_Exception::DATABASE_NOT_EXISTS);
193
+ } else {
194
+ throw Mage::exception('Copernica_MarketingSoftware', $output['message'], Copernica_MarketingSoftware_Exception::API_REQUEST_ERROR);
195
+ }
196
+ } else if (!is_array($output['data'])) {
197
+ throw Mage::exception('Copernica_MarketingSoftware', 'Unknown error' ,Copernica_MarketingSoftware_Exception::API_REQUEST_ERROR);
198
+ }
199
+
200
+ $collectionId = false;
201
+
202
+ foreach ($output['data'] as $collection) {
203
+ if ($collection['name'] == $collectionName) {
204
+ $collectionId = $collection['ID'];
205
+ }
206
+ }
207
+
208
+ if ($collectionId === false) {
209
+ throw Mage::exception('Copernica_MarketingSoftware', 'Collection does not exists', Copernica_MarketingSoftware_Exception::COLLECTION_NOT_EXISTS);
210
+ }
211
+
212
+ return $collectionId;
213
+ }
214
+
215
+ /**
216
+ * Get required fields for a collection of given type.
217
+ *
218
+ * @param string $collectionType
219
+ * @return array
220
+ */
221
+ protected function _requiredCollectionFields($collectionType)
222
+ {
223
+ switch ($collectionType) {
224
+ case 'cartproducts':
225
+ return Mage::helper('marketingsoftware')->requiredQuoteItemFields();
226
+
227
+ case 'orders' :
228
+ return Mage::helper('marketingsoftware')->requiredOrderFields();
229
+
230
+ case 'orderproducts':
231
+ return Mage::helper('marketingsoftware')->requiredOrderItemFields();
232
+
233
+ case 'addresses':
234
+ return Mage::helper('marketingsoftware')->requiredAddressFields();
235
+
236
+ case 'viewedproducts':
237
+ return Mage::helper('marketingsoftware')->requiredViewedProductFields();
238
+
239
+ case 'wishlistproducts':
240
+ return Mage::helper('marketingsoftware')->requiredWishlistItemFields();
241
+ }
242
+ }
243
+ }
app/code/community/Copernica/MarketingSoftware/Helper/Api/Validator.php ADDED
@@ -0,0 +1,382 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Copernica Marketing Software
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0).
8
+ * It is available through the world-wide-web at this URL:
9
+ * http://opensource.org/licenses/osl-3.0.php
10
+ * If you are unable to obtain a copy of the license through the
11
+ * world-wide-web, please send an email to copernica@support.cream.nl
12
+ * so we can send you a copy immediately.
13
+ *
14
+ * DISCLAIMER
15
+ *
16
+ * Do not edit or add to this file if you wish to upgrade this software
17
+ * to newer versions in the future. If you wish to customize this module
18
+ * for your needs please refer to http://www.magento.com/ for more
19
+ * information.
20
+ *
21
+ * @category Copernica
22
+ * @package Copernica_MarketingSoftware
23
+ * @copyright Copyright (c) 2011-2012 Copernica & Cream. (http://docs.cream.nl/)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ /**
28
+ * Copernica API validator.
29
+ * This class holds methods that should validate structures connected to API.
30
+ */
31
+ class Copernica_MarketingSoftware_Helper_Api_Validator extends Copernica_MarketingSoftware_Helper_Api_Abstract
32
+ {
33
+ /**
34
+ * Validate database. When something is wrong this method will throw
35
+ * custom exception to report that.
36
+ *
37
+ * @param string $databaseName
38
+ * @throws Copernica_MarketingSoftware_Exception
39
+ */
40
+ public function validateDatabase($databaseName)
41
+ {
42
+ $output = $this->_restRequest()->get( 'database/'.urlencode($databaseName) );
43
+
44
+ if (isset($output['error']) || !isset($output['name'])) {
45
+ if (strpos($output['error']['message'], 'No database') !== false) {
46
+ throw Mage::exception('Copernica_MarketingSoftware', 'Database does not exists', Copernica_MarketingSoftware_Exception::DATABASE_NOT_EXISTS);
47
+ } else {
48
+ throw Mage::exception('Copernica_MarketingSoftware', 'Database could not be checked', Copernica_MarketingSoftware_Exception::API_REQUEST_ERROR);
49
+ }
50
+ }
51
+
52
+ foreach ($output['fields']['data'] as $field) {
53
+ if ($field['name'] == 'customer_id') {
54
+ return;
55
+ }
56
+ }
57
+
58
+ throw Mage::exception('Copernica_MarketingSoftware', 'Database does not have required customer_id field', Copernica_MarketingSoftware_Exception::DATABASE_STRUCT_INVALID);
59
+ }
60
+
61
+ /**
62
+ * Validate database field.
63
+ *
64
+ * @param string $databaseName
65
+ * @param string $fieldName
66
+ * @param string $magentoField
67
+ * @throws Copernica_MarketingSoftware_Exception
68
+ */
69
+ public function validateDatabaseField($databaseName, $fieldName, $magentoField)
70
+ {
71
+ if (trim($fieldName) == '') {
72
+ throw Mage::exception('Copernica_MarketingSoftware', 'Field not linked', Copernica_MarketingSoftware_Exception::FIELD_NOT_LINKED);
73
+ }
74
+
75
+ $output = $this->_restRequest()->get('database/'.urlencode($databaseName).'/fields');
76
+
77
+ if (isset($output['error'])) {
78
+ if (strpos($output['error']['message'], 'No database') !== false) {
79
+ throw Mage::exception('Copernica_MarketingSoftware', 'Database does not exists', Copernica_MarketingSoftware_Exception::DATABASE_NOT_EXISTS);
80
+ } else {
81
+ throw Mage::exception('Copernica_MarketingSoftware', 'Database could not be checked', Copernica_MarketingSoftware_Exception::API_REQUEST_ERROR);
82
+ }
83
+ }
84
+
85
+ foreach ($output['data'] as $field) {
86
+ if ($field['name'] != $fieldName) {
87
+ continue;
88
+ }
89
+
90
+ if (!$this->_checkDatabaseFieldType($magentoField, $field, $databaseName)) {
91
+ throw Mage::exception('Copernica_MarketingSoftware', 'Field is invalid', Copernica_MarketingSoftware_Exception::FIELD_STRUCT_INVALID);
92
+ }
93
+
94
+ return;
95
+ }
96
+
97
+ throw Mage::exception('Copernica_MarketingSoftware', 'Field does not exists', Copernica_MarketingSoftware_Exception::FIELD_NOT_EXISTS);
98
+ }
99
+
100
+ /**
101
+ * Validate collection.
102
+ *
103
+ * @param string $databaseName
104
+ * @param string $collectionName
105
+ * @param string $collectionType
106
+ * @throws Copenica_MarketingSoftware_Exception
107
+ */
108
+ public function validateCollection($databaseName, $collectionName, $collectionType)
109
+ {
110
+ $output = $this->_restRequest()->get('database/'.$databaseName.'/collections');
111
+
112
+ if (isset($output['error'])) {
113
+ if (strpos($output['error']['message'], 'No database') !== false) {
114
+ throw Mage::exception('Copernica_MarketingSoftware', 'Database does not exists', Copernica_MarketingSoftware_Exception::DATABASE_NOT_EXISTS);
115
+ } else {
116
+ throw Mage::exception('Copernica_MarketingSoftware', 'Collection could not be checked', Copernica_MarketingSoftware_Exception::API_REQUEST_ERROR);
117
+ }
118
+ }
119
+
120
+ if ($output['total'] == 0) {
121
+ throw Mage::exception('Copernica_MarketingSoftware', 'Collection does not exists', Copernica_MarketingSoftware_Exception::COLLECTION_NOT_EXISTS);
122
+ }
123
+
124
+ foreach ($output['data'] as $collection) {
125
+ if ($collection['name'] != $collectionName) {
126
+ continue;
127
+ }
128
+
129
+ if ($this->_isCollectionFieldsValid($collection['fields'], $collectionType)) {
130
+ return;
131
+ }
132
+
133
+ throw Mage::exception('Copernica_MarketingSoftware', 'Collection structure is invalid.', Copernica_MarketingSoftware_Exception::COLLECTION_STRUCT_INVALID);
134
+ }
135
+
136
+ throw Mage::exception('Copernica_MarketingSoftware', 'Collection does not exists', Copernica_MarketingSoftware_Exception::COLLECTION_NOT_EXISTS);
137
+ }
138
+
139
+ /**
140
+ * Check collection field structure.
141
+ *
142
+ * @param assoc $collectionFieldStruct
143
+ * @param string $collectionType
144
+ * @return bool
145
+ */
146
+ protected function _isCollectionFieldsValid($collectionFieldStruct, $collectionType)
147
+ {
148
+ $requiredFields = $this->_requiredCollectionFields($collectionType);
149
+
150
+ $collectionFields = array();
151
+
152
+ foreach ($collectionFieldStruct['data'] as $field) {
153
+ $collectionFields[] = $field['name'];
154
+ }
155
+
156
+ if (count(array_intersect($collectionFields, $requiredFields)) == count($requiredFields)) {
157
+ return true;
158
+ }
159
+
160
+ return false;
161
+ }
162
+
163
+ /**
164
+ * Validate collection field.
165
+ *
166
+ * @param string $databaseName
167
+ * @param string $collectionName
168
+ * @param string $collectionType
169
+ * @param string $magentoFieldName
170
+ * @param string $copernicaFieldName
171
+ * @throws Copernica_MarketingSoftware_Exception
172
+ */
173
+ public function validateCollectionField($databaseName, $collectionName, $collectionType, $magentoFieldName, $copernicaFieldName)
174
+ {
175
+ $output = $this->_restRequest()->get(
176
+ 'database/'.$databaseName.'/collections'
177
+ );
178
+
179
+ if (isset($output['error'])) {
180
+ if (strpos($output['error']['message'], 'No database') !== false) {
181
+ throw Mage::exception('Copernica_MarketingSoftware', 'Database does not exists', Copernica_MarketingSoftware_Exception::DATABASE_NOT_EXISTS);
182
+ } else {
183
+ throw Mage::exception('Copernica_MarketingSoftware', 'API error', Copernica_MarketingSoftware_Exception::API_REQUEST_ERROR);
184
+ }
185
+ }
186
+
187
+ if ($output['total'] == 0) {
188
+ throw Mage::exception('Copernica_MarketingSoftware', 'Collection does not exists', Copernica_MarketingSoftware_Exception::COLLECTION_NOT_EXISTS);
189
+ }
190
+
191
+ $collectionId = false;
192
+
193
+ foreach ($output['data'] as $collection) {
194
+ if ($collection['name'] == $collectionName) {
195
+ $collectionId = $collection['ID'];
196
+ }
197
+ }
198
+
199
+ if ($collectionId === false) {
200
+ throw Mage::exception('Copernica_MarketingSoftware', 'Collection does not exits', Copernica_MarketingSoftware_Exception::COLLECTION_NOT_EXISTS);
201
+ }
202
+
203
+ $output = $this->_restRequest()->get( 'collection/'.$collectionId.'/fields' );
204
+
205
+ if (isset($output['error']) || !isset($output['data'])) {
206
+ throw Mage::exception('Copernica_MarketingSoftware', 'API error', Copernica_MarketingSoftware_Exception::API_REQUEST_ERROR);
207
+ }
208
+
209
+ if ($output['total'] == 0) {
210
+ throw Mage::exception('Copernica_MarketingSoftware', 'Field does not exists', Copernica_MarketingSoftware_Exception::FIELD_NOT_EXISTS);
211
+ }
212
+
213
+ foreach ($output['data'] as $field) {
214
+ if ($field['name'] != $copernicaFieldName) {
215
+ continue;
216
+ }
217
+
218
+ $fieldDefinition = Mage::helper('marketingsoftware/data')->getCollectionFieldDefinition($collectionType, $magentoFieldName);
219
+
220
+ if ($fieldDefinition['type'] != $field['type']) {
221
+ throw Mage::exception('Copernica_MarketingSoftware', 'Field has invalid structure. Field should have \''.$fieldDefinition['type'].'\' type.', Copernica_MarketingSoftware_Exception::FIELD_STRUCT_INVALID);
222
+ }
223
+
224
+ return;
225
+ }
226
+
227
+ throw Mage::exception('Copernica_MarketingSoftware', 'Field does not exists', Copernica_MarketingSoftware_Exception::FIELD_NOT_EXISTS);
228
+ }
229
+
230
+ /**
231
+ * Get required fields for a collection of given type.
232
+ *
233
+ * @param string $collectionType
234
+ * @return array
235
+ */
236
+ protected function _requiredCollectionFields($collectionType)
237
+ {
238
+ switch ($collectionType)
239
+ {
240
+ case 'cartproducts':
241
+ return Mage::helper('marketingsoftware')->requiredQuoteItemFields();
242
+
243
+ case 'orders' :
244
+ return Mage::helper('marketingsoftware')->requiredOrderFields();
245
+
246
+ case 'orderproducts':
247
+ return Mage::helper('marketingsoftware')->requiredOrderItemFields();
248
+
249
+ case 'addresses':
250
+ return Mage::helper('marketingsoftware')->requiredAddressFields();
251
+
252
+ case 'viewedproduct':
253
+ return Mage::helper('marketingsoftware')->requiredViewedProductFields();
254
+
255
+ case 'wishlistproducts':
256
+ return Mage::helper('marketingsoftware')->requiredWishlistItemFields();
257
+
258
+ default:
259
+ return array();
260
+ }
261
+ }
262
+
263
+ /**
264
+ * Check database field if copernica type match magento type.
265
+ *
266
+ * @param string $magentoField
267
+ * @param assoc $copernicaStructure
268
+ * @param string $databaseName
269
+ * @return boolean
270
+ */
271
+ protected function _checkDatabaseFieldType($magentoField, $copernicaStructure, $databaseName)
272
+ {
273
+ switch ($magentoField) {
274
+ case 'email' :
275
+ return $this->_checkEmailField($copernicaStructure);
276
+
277
+ case 'newsletter':
278
+ return $this->_checkNewsletterField($copernicaStructure, $databaseName);
279
+
280
+ case 'birthdate':
281
+ return $this->_checkDateField($copernicaStructure);
282
+
283
+ case 'registrationDate':
284
+ return $this->_checkDatetimeField($copernicaStructure);
285
+
286
+ default:
287
+ return $this->_checkDefaultField($copernicaStructure);
288
+ }
289
+ }
290
+
291
+ /**
292
+ * Check if newsletter field is correct
293
+ *
294
+ * @return boolean
295
+ */
296
+ protected function _checkNewsletterField($copernicaStructure, $databaseName)
297
+ {
298
+ $databaseUnsubscribe = $this->_restRequest()->get('database/'.urlencode($databaseName).'/unsubscribe');
299
+
300
+ if (isset($databaseUnsubscribe['error'])) {
301
+ throw Mage::exception('Copernica_MarketingSoftware', 'Field could not be checked', Copernica_MarketingSoftware_Exception::API_REQUEST_ERROR);
302
+ }
303
+
304
+ if ($databaseUnsubscribe['behavior'] != 'update') {
305
+ return false;
306
+ }
307
+
308
+ $fieldName = $copernicaStructure['name'];
309
+
310
+ if (!array_key_exists($fieldName, $databaseUnsubscribe['fields'])) {
311
+ return false;
312
+ }
313
+
314
+ if ($databaseUnsubscribe['fields'][$fieldName] != 'unsubscribed_copernica') {
315
+ return false;
316
+ }
317
+
318
+ $databaseCallbacks = $this->_restRequest()->get('database/'.urlencode($databaseName).'/callbacks');
319
+
320
+ if (isset($databaseCallbacks['error'])) {
321
+ throw Mage::exception('Copernica_MarketingSoftware', 'Field could not be checked', Copernica_MarketingSoftware_Exception::API_REQUEST_ERROR);
322
+ }
323
+
324
+ $callbackUrl = Mage::helper('marketingsoftware')->unsubscribeCallbackUrl();
325
+
326
+ if ($databaseCallbacks['total'] == 0) {
327
+ return false;
328
+ }
329
+
330
+ foreach ($databaseCallbacks['data'] as $callback) {
331
+ if ($callback['url'] == $callbackUrl && $callback['method'] == 'json' && $callback['expression'] == "profile.$fieldName == 'unsubscribed_copernica';") {
332
+ return true;
333
+ }
334
+ }
335
+
336
+ return false;
337
+ }
338
+
339
+ /**
340
+ * Check field if it's a email field.
341
+ *
342
+ * @param assoc $copernicaStructure
343
+ * @return boolean
344
+ */
345
+ protected function _checkEmailField($copernicaStructure)
346
+ {
347
+ return $copernicaStructure['type'] == 'email';
348
+ }
349
+
350
+ /**
351
+ * Check field if it's a date field.
352
+ *
353
+ * @param assoc $copernicaStructure
354
+ * @return boolean
355
+ */
356
+ protected function _checkDateField($copernicaStructure)
357
+ {
358
+ return $copernicaStructure['type'] == 'date';
359
+ }
360
+
361
+ /**
362
+ * Check field if it's a datetime field
363
+ *
364
+ * @param assoc $copernicaStructure
365
+ * @return boolean
366
+ */
367
+ protected function _checkDatetimeField($copernicaStructure)
368
+ {
369
+ return $copernicaStructure['type'] == 'datetime';
370
+ }
371
+
372
+ /**
373
+ * Check field if it's a default field.
374
+ *
375
+ * @param assoc $copernicaStructure
376
+ * @return boolean
377
+ */
378
+ protected function _checkDefaultField($copernicaStructure)
379
+ {
380
+ return $copernicaStructure['type'] == 'text';
381
+ }
382
+ }
app/code/community/Copernica/MarketingSoftware/Helper/ApiBase.php DELETED
@@ -1,452 +0,0 @@
1
- <?php
2
- /**
3
- * Copernica Marketing Software
4
- *
5
- * NOTICE OF LICENSE
6
- *
7
- * This source file is subject to the Open Software License (OSL 3.0).
8
- * It is available through the world-wide-web at this URL:
9
- * http://opensource.org/licenses/osl-3.0.php
10
- * If you are unable to obtain a copy of the license through the
11
- * world-wide-web, please send an email to copernica@support.cream.nl
12
- * so we can send you a copy immediately.
13
- *
14
- * DISCLAIMER
15
- *
16
- * Do not edit or add to this file if you wish to upgrade this software
17
- * to newer versions in the future. If you wish to customize this module
18
- * for your needs please refer to http://www.magento.com/ for more
19
- * information.
20
- *
21
- * @category Copernica
22
- * @package Copernica_MarketingSoftware
23
- * @copyright Copyright (c) 2011-2012 Copernica & Cream. (http://docs.cream.nl/)
24
- * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
- */
26
-
27
- /**
28
- * A base class for API helpers. This class should have only the most general
29
- * method that we will use.
30
- */
31
- class Copernica_MarketingSoftware_Helper_ApiBase extends Mage_Core_Helper_Abstract
32
- {
33
- /**
34
- * Since Magento is cheating with theirs singleton implementation, we want
35
- * to use static fields for storing request instance.
36
- *
37
- * @var Copernica_MarketingSoftware_Helper_RESTRequest
38
- */
39
- static protected $request = null;
40
-
41
- /**
42
- * Database id that is on Copernica platform.
43
- * @var int
44
- */
45
- static protected $databaseId = false;
46
-
47
- /**
48
- * Cache for collection ids
49
- * @var array
50
- */
51
- static protected $collectionIdCache = array();
52
-
53
- /**
54
- * SHould we use profile cache?
55
- * @var bool
56
- */
57
- protected $useProfileCache = false;
58
-
59
- /**
60
- * Public, standard PHP constructor. Mage_Core_Helper_Abstract is not a child
61
- * of Varien_Object, so we want to use good old PHP constructor.
62
- */
63
- public function __construct()
64
- {
65
- // initialize api
66
- $this->init();
67
- }
68
-
69
- /**
70
- * Initialize helper.
71
- * @return Copernica_MarketingSoftware_Helper_ApiBase
72
- */
73
- public function init()
74
- {
75
- // just initialize request instance
76
- $this->request();
77
-
78
- // should we use profile cache?
79
- $this->useProfileCache = Mage::helper('marketingsoftware/config')->getProfileCache();
80
-
81
- // allow chaining
82
- return $this;
83
- }
84
-
85
- /**
86
- * Get Request instance.
87
- * We do cache/shield request instance mostly cause it's very specific to
88
- * API and circumstances. That is why we don't want to rely on magento
89
- * helpers as a final solution for reqeust instance.
90
- * @return Copernica_MarketingSoftware_Helper_RESTRequest
91
- */
92
- protected function request()
93
- {
94
- // check if we have a request instance
95
- if (!is_null(self::$request)) return self::$request;
96
-
97
- // return cached request instance
98
- return self::$request = Mage::helper('marketingsoftware/RESTRequest');
99
- }
100
-
101
- /**
102
- * Set database Id
103
- * @param int
104
- */
105
- protected function setDatabaseId($id)
106
- {
107
- self::$databaseId = $id;
108
- }
109
-
110
- /**
111
- * Check if this API instance is valid.
112
- * @param boolean also validata configuration
113
- * @return boolean
114
- */
115
- public function check($extensive = false)
116
- {
117
- // just check the request
118
- return $this->request()->check();
119
- }
120
-
121
- /**
122
- * Get stored database Id. This function will return false when we can not
123
- * get stored database Id.
124
- * @return int|false
125
- */
126
- private function getStoredDatabaseId()
127
- {
128
- // try to get cached Id from config
129
- $databaseId = Mage::helper('marketingsoftware/config')->getDatabaseId();
130
-
131
- // check if we have a database ID
132
- if ($databaseId) return $databaseId;
133
-
134
- // get database name from config
135
- $databaseName = Mage::helper('marketingsoftware/config')->getDatabaseName();
136
-
137
- // check if database name is valid
138
- if (!$databaseName)
139
- {
140
- return false;
141
- }
142
-
143
- // get database Id from API
144
- $databaseId = $this->getDatabaseIdByName($databaseName);
145
-
146
- // store database id in config
147
- Mage::helper('marketingsoftware/config')->setDatabaseId($databaseId);
148
-
149
- // set database Id in cache
150
- $this->setDatabaseId($databaseId);
151
-
152
- // return database Id
153
- return $databaseId;
154
- }
155
-
156
- /**
157
- * Get database Id by its name. This method will return false when we can
158
- * not fetch database.
159
- * @param string database name
160
- * @return int|false
161
- */
162
- private function getDatabaseIdByName($databaseName)
163
- {
164
- // fetch database info from API
165
- $output = $this->request()->get(
166
- 'database/'.$databaseName
167
- );
168
-
169
- // check if we have an error
170
- if (isset($output['error']))
171
- {
172
- return false;
173
- }
174
-
175
- // output ID
176
- return isset($output['ID']) ? $output['ID'] : false;
177
- }
178
-
179
- /**
180
- * Get database id that will be used inside Copernica platform
181
- * @param string|false Database name or false when we want to get stored database Id
182
- * @return int
183
- */
184
- public function getDatabaseId($databaseName = false)
185
- {
186
- // try to get id from stored database
187
- if ($databaseName === false) $databaseId = $this->getStoredDatabaseID();
188
-
189
- // well we have to fetch database Id from API
190
- else $databaseId = $this->getDatabaseIdByName($databaseName);
191
-
192
- // return database Id
193
- return $databaseId;
194
- }
195
-
196
- /**
197
- * Translate collection name into an ID.
198
- * @param string name of the collection
199
- * @return int
200
- */
201
- public function getCollectionId($name)
202
- {
203
- // check if we did fetch collection from REST and we have a proper collection name
204
- if (!is_null(self::$collectionIdCache) && array_key_exists($name, self::$collectionIdCache)) return self::$collectionIdCache[$name];
205
-
206
- // get all collections in database
207
- $output = $this->request()->get(
208
- 'database/'.$this->getDatabaseId().'/collections'
209
- );
210
-
211
- // check if we have a valid collections collection
212
- if ($output['total'] == 0) return false;
213
-
214
- // reset collecion cache
215
- self::$collectionIdCache = array();
216
-
217
- // iterate over all collections
218
- foreach ($output['data'] as $collection)
219
- {
220
- self::$collectionIdCache[$collection['name']] = $collection['ID'];
221
- }
222
-
223
- // if we have a valid collection name then return it
224
- if (array_key_exists($name, self::$collectionIdCache)) return self::$collectionIdCache[$name];
225
-
226
- // we don't have a valid collection name
227
- return false;
228
- }
229
-
230
- /**
231
- * Get copernica profile Id by magento customer Id.
232
- *
233
- * This method is little bit more flexible. It's possible to supply customer
234
- * data by copernica model or by array with 'id', 'storeView', 'email' fields.
235
- *
236
- * @param Copernica_MarketingSoftware_Model_Copernica_Profile|array
237
- * @return int the copernica profile Id
238
- */
239
- public function getProfileId($customer)
240
- {
241
- // if it's not an array we can just use it as object
242
- if (!is_array($customer))
243
- {
244
- $customerId = $customer->id();
245
- $storeView = $customer->storeView();
246
- $email = $customer->email();
247
- }
248
- else
249
- {
250
- $customerId = $customer['id'];
251
- $storeView = $customer['storeView'];
252
- if (is_object($storeView)) $storeView = (string)$storeView;
253
- $email = $customer['email'];
254
- }
255
-
256
- // placeholder for profile Id
257
- $profileId = false;
258
-
259
- // return false
260
- if ($customerId === false) return false;
261
-
262
- // try to get profile cache entry
263
- $profileCacheCollection = Mage::getModel('marketingsoftware/profileCache')
264
- ->getCollection()
265
- ->setPageSize(1)
266
- ->addFieldToFilter('copernica_id', $customerId);
267
-
268
- // get profile cache
269
- $profileCache = $profileCacheCollection->getFirstItem();
270
-
271
- // check if we have a profile cache that we can use
272
- if (!$profileCache->isObjectNew())
273
- {
274
- // get profile Id
275
- $profileId = $profileCache->getProfileId();
276
-
277
- // return profile Id if we have one
278
- if ($profileId > 0 && $profileId !== false && !is_null($profileId)) return $profileId;
279
-
280
- // if we have a profile cache with copernica id and no profile id, it's
281
- // useless, so for sake of planet earth we want to remove that particular
282
- // profile cache instance
283
- else ($profileCache->delete());
284
- }
285
-
286
- // if we have email and store view we can try to fetch the profile Id by it
287
- if (strlen($email) && strlen($storeView))
288
- {
289
- // try to get a profile by email+store_view combination
290
- $profileCacheCollection = Mage::getModel('marketingsoftware/profileCache')
291
- ->getCollection()
292
- ->setPageSize(1)
293
- ->addFieldToFilter('email', $email)
294
- ->addFieldToFilter('store_view', $storeView);
295
-
296
- // get 1st item
297
- $profileCache = $profileCacheCollection->getFirstItem();
298
-
299
- // get profile copernica Id from profile cache
300
- $profileCopernicaId = $profileCache->getCopernicaId();
301
-
302
- // should we update copernica Id?
303
- if ($profileCopernicaId != $customerId && !is_null($customerId))
304
- {
305
- // check if it's an really old format.
306
- if (strpos($profileCopernicaId, '|') === false)
307
- {
308
- $profileCache->setCopernicaId($customerId);
309
- $profileCache->save();
310
- }
311
- elseif (is_string(array_shift(explode('|', $profileCopernicaId))))
312
- {
313
- if (is_numeric(array_shift(explode('|', $customerId))))
314
- {
315
- $profileCache->setCopernicaId($customerId);
316
- $profileCache->save();
317
- }
318
- }
319
- }
320
-
321
- // get profile Id
322
- $profileId = $profileCache->getProfileId();
323
-
324
- // check if we have a proper profile Id and return it
325
- if ($profileId > 0 && $profileId !== false && !is_null($profileId)) return $profileId;
326
- }
327
-
328
- /**
329
- * Could be that we will try to get a profile Id by email and store view
330
- * only. In such case we should just skip this step cause we don't have
331
- * a customer Id.
332
- */
333
- if (!is_null($customerId) && strlen($customerId)) $profileId = $this->getProfileIdByCustomerId($customerId);
334
-
335
- // check if we have a proper profile Id
336
- if ($profileId !== false)
337
- {
338
- if ($profileId > 0 && $profileId !== false && !is_null($profileId))
339
- {
340
- // set profile Id
341
- if (strlen($email)) $profileCache->setEmail($email);
342
- if (strlen($storeView)) $profileCache->setStoreView($storeView);
343
- if (!is_null($customerId)) $profileCache->setCopernicaId($customerId);
344
- $profileCache->setProfileId($profileId);
345
- $profileCache->save();
346
-
347
- // return profile Id
348
- return $profileId;
349
- }
350
- }
351
-
352
- // if we don't have an email address, don't bother with another rest call
353
- if (strlen($email) == 0) return false;
354
-
355
- // get profile Id
356
- $profileId = $this->getProfileIdByEmail($email, $storeView);
357
-
358
- // check if we really have a profile Id
359
- if ($profileId !== false)
360
- {
361
- // set profile Id
362
- if (!is_null($customerId)) $profileCache->setCopernicaId($customerId);
363
- $profileCache->setStoreView($storeView);
364
- $profileCache->setEmail($email);
365
- $profileCache->setProfileId($profileId);
366
- $profileCache->save();
367
-
368
- // return profile Id
369
- return $profileId;
370
- }
371
-
372
- // return false cause we can not
373
- return false;
374
- }
375
-
376
- /**
377
- * Get profile Id by customer Id. When profile can not be found FALSE will
378
- * be returned.
379
- * @param string
380
- * @return int|false
381
- */
382
- private function getProfileIdByCustomerId($customerId)
383
- {
384
- // get profiles
385
- $profiles = $this->request()->get(
386
- 'database/'.$this->getDatabaseId().'/profiles',
387
- array (
388
- 'fields[]' => 'customer_id=='.$customerId
389
- )
390
- );
391
-
392
- // check if we have data
393
- if (!isset($profiles['data'][0])) return false;
394
-
395
- // make some more sanity checks on what we got from rest
396
- if (is_null($profiles['data'][0]['ID']) || (int)$profiles['data'][0]['ID'] == 0) return false;
397
-
398
- // return profile Id
399
- return $profiles['data'][0]['ID'];
400
- }
401
-
402
- /**
403
- * Get profile Id by email address. FALSE will be returned if profile was not
404
- * found.
405
- * @param string
406
- * @param string
407
- * @return int
408
- */
409
- private function getProfileIdByEmail($email, $storeView)
410
- {
411
- // get linked fields mapping
412
- $customerLinkedFields = Mage::helper('marketingsoftware/config')->getLinkedCustomerFields();
413
-
414
- // get request params
415
- $requestParams = array(
416
- $customerLinkedFields['email'].'=='.$email,
417
- $customerLinkedFields['storeView'].'=='.$storeView
418
- );
419
- // make a REST request
420
- $profiles = $this->request()->get('database/'.$this->getDatabaseId().'/profiles', array ( 'fields' => $requestParams ));
421
-
422
- // check if we have data
423
- if (!isset($profiles['data'][0])) return false;
424
-
425
- // make some more sanity checks on what we got from rest
426
- if (is_null($profiles['data'][0]['ID']) || (int)$profiles['data'][0]['ID'] == 0) return false;
427
-
428
- // return profile Id
429
- return $profiles['data'][0]['ID'];
430
- }
431
-
432
- /**
433
- * Get profile Id from cache
434
- * @param string the magento customer Id
435
- * @return int copernica profile Id
436
- */
437
- private function getProfileIdFromCache($customerId)
438
- {
439
- // get profile Cache
440
- $profileCache = Mage::getModel('marketingsoftware/profileCache')
441
- ->getCollection()
442
- ->addFieldToFilter('customer_id', $customerId)
443
- ->setPageSize(1)
444
- ->getFirstItem();
445
-
446
- // get profile Id
447
- $profileId = $profileCache->getProfileId();
448
-
449
- // check if we have profile Id in cache
450
- if (!is_null($profileId)) return $profileId;
451
- }
452
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app/code/community/Copernica/MarketingSoftware/Helper/ApiBuilder.php DELETED
@@ -1,261 +0,0 @@
1
- <?php
2
- /**
3
- * Copernica Marketing Software
4
- *
5
- * NOTICE OF LICENSE
6
- *
7
- * This source file is subject to the Open Software License (OSL 3.0).
8
- * It is available through the world-wide-web at this URL:
9
- * http://opensource.org/licenses/osl-3.0.php
10
- * If you are unable to obtain a copy of the license through the
11
- * world-wide-web, please send an email to copernica@support.cream.nl
12
- * so we can send you a copy immediately.
13
- *
14
- * DISCLAIMER
15
- *
16
- * Do not edit or add to this file if you wish to upgrade this software
17
- * to newer versions in the future. If you wish to customize this module
18
- * for your needs please refer to http://www.magento.com/ for more
19
- * information.
20
- *
21
- * @category Copernica
22
- * @package Copernica_MarketingSoftware
23
- * @copyright Copyright (c) 2011-2012 Copernica & Cream. (http://docs.cream.nl/)
24
- * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
- */
26
-
27
- /**
28
- * This class will assist in any creation process performed on API.
29
- */
30
- class Copernica_MarketingSoftware_Helper_ApiBuilder extends Copernica_MarketingSoftware_Helper_ApiBase
31
- {
32
- /**
33
- * Will create proper database in Copernica platform
34
- * @param string the name of database
35
- */
36
- public function createDatabase($databaseName)
37
- {
38
- // data that we will send with POST request
39
- $data['name'] = $databaseName;
40
- $data['description'] = 'Database created by magento extension.';
41
-
42
- // try to create database
43
- $this->request()->post( 'databases', $data );
44
-
45
- /*
46
- * To make synchronization possible we have to create one required field
47
- * inside copernica database: customer_id. It's our own customer identifier.
48
- */
49
- $this->createDatabaseField($databaseName, 'customer_id', array(
50
- 'type' => 'text',
51
- 'length' => 64
52
- ));
53
- }
54
-
55
- /**
56
- * Create field in given database.
57
- *
58
- * For more info about what can be supplied in $options argument go:
59
- * @see https://www.copernica.com/en/support/rest/database-fields
60
- *
61
- * @param string database name
62
- * @param string field name
63
- * @param array Optional additional argumets
64
- */
65
- public function createDatabaseField($databaseName, $fieldName, $options = array())
66
- {
67
- // create final options
68
- $options = array_merge( array ('name' => $fieldName), array('displayed' => true), $options);
69
-
70
- // tell API to create new field inside database
71
- $this->request()->post( 'database/'.urlencode($databaseName).'/fields', $options );
72
- }
73
-
74
- /**
75
- * Helper method to create date fields.
76
- * @param string database name
77
- * @param string field name
78
- */
79
- public function createDatabaseDateField($databaseName, $fieldName)
80
- {
81
- // set field options
82
- $options['type'] = 'date';
83
-
84
- // create field
85
- $this->createDatabaseField($databaseName, $fieldName, $options);
86
- }
87
-
88
- /**
89
- * Helper method to create datetime fields.
90
- * @param string database name
91
- * @param string field name
92
- */
93
- public function createDatabaseDatetimeField($databaseName, $fieldName)
94
- {
95
- // set field options
96
- $options['type'] = 'datetime';
97
-
98
- // create field
99
- $this->createDatabaseField($databaseName, $fieldName, $options);
100
- }
101
-
102
- /**
103
- * Helper method to create email fields.
104
- * @param string database name
105
- * @param string field name
106
- */
107
- public function createDatabaseEmailField($databaseName, $fieldName)
108
- {
109
- // set field options
110
- $options['type'] = 'email';
111
- $options['length'] = 160;
112
-
113
- // create field
114
- $this->createDatabaseField($databaseName, $fieldName, $options);
115
- }
116
-
117
- /**
118
- * Helper method to create newsletter fields.
119
- * @param string database name
120
- * @param string field name
121
- */
122
- public function createDatabaseNewsletterField($databaseName, $fieldName)
123
- {
124
- // set field options
125
- $options['type'] = 'text';
126
-
127
- // create database field
128
- $this->createDatabaseField($databaseName, $fieldName, $options);
129
-
130
- // since we should have a field we have to create callbacks and unsubscribe actions on database
131
- $this->request()->post(
132
- 'database/'.urlencode($databaseName).'/unsubscribe',
133
- array (
134
- 'behavior' => 'update',
135
- 'fields' => array ( $fieldName => 'unsubscribed_copernica')
136
- )
137
- );
138
-
139
- // we do have a field and unsubscribe action, now we want to create callback
140
- $this->request()->post(
141
- 'database/'.urlencode($databaseName).'/callbacks',
142
- array (
143
- 'url' => Mage::helper('marketingsoftware')->unsubscribeCallbackUrl(),
144
- 'method' => 'json',
145
- 'expression' => "profile.$fieldName == 'unsubscribed_copernica';"
146
- )
147
- );
148
- }
149
-
150
- /**
151
- * Create collection inside given database.
152
- * @param string databse name
153
- * @param string collection name
154
- * @param string type of a collection
155
- * @throws Copernica_MarketingSoftware_Exception
156
- */
157
- public function createCollection($databaseName, $collectionName, $collectionType)
158
- {
159
- // create collection in database
160
- $this->request()->post( 'database/'.urlencode($databaseName).'/collections', array ( 'name' => $collectionName ) );
161
-
162
- // get required fields that should make to collection
163
- $requiredFields = $this->requiredCollectionFields($collectionType);
164
-
165
- // create all required collection fields
166
- foreach ($requiredFields as $field)
167
- {
168
- // create collection field
169
- $this->createCollectionField($databaseName, $collectionName, $collectionType, $field, $field);
170
- }
171
- }
172
-
173
- /**
174
- * This method created collection field.
175
- * @param string database name
176
- * @param string collection name
177
- * @param string collection type
178
- * @param string copernica name
179
- * @param string magento name
180
- * @throws Copernica_MarketingSoftware_Exception
181
- */
182
- public function createCollectionField($databaseName, $collectionName, $collectionType, $copernicaName, $magentoName)
183
- {
184
- // get field definition
185
- $fieldDefinition = Mage::helper('marketingsoftware/data')->getCollectionFieldDefinition($collectionType, $magentoName);
186
-
187
- // when we create a collection field we want to set it to be displayed by default
188
- $fieldDefinition = array_merge(array ('displayed' => true), $fieldDefinition);
189
-
190
- // get collection Id
191
- $collectionId = $this->getCollectionIdFromDatabase($databaseName, $collectionName);
192
-
193
- // create the field
194
- $this->request()->post( 'collection/'.$collectionId.'/fields', array_merge($fieldDefinition, array(
195
- 'name' => $copernicaName
196
- )));
197
- }
198
-
199
- /**
200
- * Get collection Id by it's name and database name that it's in.
201
- * @param string database name
202
- * @param string collection name
203
- * @return numeric collection Id
204
- * @throws Copernica_MarketingSoftware_Exception
205
- */
206
- private function getCollectionIdFromDatabase($databaseName, $collectionName)
207
- {
208
- // now we want to get collections from database
209
- $output = $this->request()->get( 'database/'.urlencode($databaseName).'/collections' );
210
-
211
- // got errors ?
212
- if (isset($output['error'])) {
213
- /*
214
- * We want to detect if error says that database does not exist.
215
- * Rest of API errors we will just output with general API_REQUEST_ERROR
216
- * code, cause we don't really want to handle all possible api
217
- * errors.
218
- */
219
- if (strpos($output['error']['message'], 'No database') !== false) throw Mage::exception('Copernica_MarketingSoftware', 'No database', Copernica_MarketingSoftware_Exception::DATABASE_NOT_EXISTS);
220
- else throw Mage::exception('Copernica_MarketingSoftware', $output['message'], Copernica_MarketingSoftware_Exception::API_REQUEST_ERROR);
221
- }
222
-
223
- // if we don't have a proper output, we want to throw an exception, cause
224
- // we can not do anything useful anyway.
225
- else if (!is_array($output['data'])) {
226
- throw Mage::exception('Copernica_MarketingSoftware', 'Unknown error' ,Copernica_MarketingSoftware_Exception::API_REQUEST_ERROR);
227
- }
228
-
229
- // collection Id
230
- $collectionId = false;
231
-
232
- // iterate over all collections to get one that we did just created
233
- foreach ($output['data'] as $collection)
234
- {
235
- if ($collection['name'] == $collectionName) $collectionId = $collection['ID'];
236
- }
237
-
238
- // if we don't have collection id we want to break here
239
- if ($collectionId === false) throw Mage::exception('Copernica_MarketingSoftware', 'Collection does not exists', Copernica_MarketingSoftware_Exception::COLLECTION_NOT_EXISTS);
240
-
241
- // return proper collection Id
242
- return $collectionId;
243
- }
244
-
245
- /**
246
- * Get required fields for a collection of given type.
247
- * @param string
248
- * @return array
249
- */
250
- private function requiredCollectionFields($collectionType)
251
- {
252
- switch ($collectionType)
253
- {
254
- case 'cartproducts': return Mage::helper('marketingsoftware')->requiredCartItemFields();
255
- case 'orders' : return Mage::helper('marketingsoftware')->requiredOrderFields();
256
- case 'orderproducts': return Mage::helper('marketingsoftware')->requiredOrderItemFields();
257
- case 'addresses': return Mage::helper('marketingsoftware')->requiredAddressFields();
258
- case 'viewedproduct': return Mage::helper('marketingsoftware')->requiredViewedProductFields();
259
- }
260
- }
261
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app/code/community/Copernica/MarketingSoftware/Helper/ApiValidator.php DELETED
@@ -1,381 +0,0 @@
1
- <?php
2
- /**
3
- * Copernica Marketing Software
4
- *
5
- * NOTICE OF LICENSE
6
- *
7
- * This source file is subject to the Open Software License (OSL 3.0).
8
- * It is available through the world-wide-web at this URL:
9
- * http://opensource.org/licenses/osl-3.0.php
10
- * If you are unable to obtain a copy of the license through the
11
- * world-wide-web, please send an email to copernica@support.cream.nl
12
- * so we can send you a copy immediately.
13
- *
14
- * DISCLAIMER
15
- *
16
- * Do not edit or add to this file if you wish to upgrade this software
17
- * to newer versions in the future. If you wish to customize this module
18
- * for your needs please refer to http://www.magento.com/ for more
19
- * information.
20
- *
21
- * @category Copernica
22
- * @package Copernica_MarketingSoftware
23
- * @copyright Copyright (c) 2011-2012 Copernica & Cream. (http://docs.cream.nl/)
24
- * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
- */
26
-
27
- /**
28
- * Copernica API validator.
29
- * This class holds methods that should validate structures connected to API.
30
- */
31
- class Copernica_MarketingSoftware_Helper_ApiValidator extends Copernica_MarketingSoftware_Helper_ApiBase
32
- {
33
- /**
34
- * Validate database. When something is wrong this method will throw
35
- * custom exception to report that.
36
- * @param string Database name
37
- * @throws Copernica_MarketingSoftware_Exception
38
- */
39
- public function validateDatabase($databaseName)
40
- {
41
- // make a request to API to get database structure
42
- $output = $this->request()->get( 'database/'.urlencode($databaseName) );
43
-
44
- // check if api say that we have a problem
45
- if (isset($output['error']) || !isset($output['name']))
46
- {
47
- // check if Api tells that database does not exists or something else is wrong
48
- if (strpos($output['error']['message'], 'No database') !== false)
49
- throw Mage::exception('Copernica_MarketingSoftware', 'Database does not exists', Copernica_MarketingSoftware_Exception::DATABASE_NOT_EXISTS);
50
- else
51
- throw Mage::exception('Copernica_MarketingSoftware', 'Database could not be checked', Copernica_MarketingSoftware_Exception::API_REQUEST_ERROR);
52
- }
53
-
54
- // check if database have required fields
55
- foreach ($output['fields']['data'] as $field) if ($field['name'] == 'customer_id') return;
56
-
57
- // database does not have required field
58
- throw Mage::exception('Copernica_MarketingSoftware', 'Database does not have required customer_id field', Copernica_MarketingSoftware_Exception::DATABASE_STRUCT_INVALID);
59
- }
60
-
61
- /**
62
- * Validate database field.
63
- * @param string the database name
64
- * @param string the field name
65
- * @throws Copernica_MarketingSoftware_Exception
66
- */
67
- public function validateDatabaseField($databaseName, $fieldName, $magentoField)
68
- {
69
- // if field name is just empty string then we can say that field is not linked at all
70
- if (trim($fieldName) == '') throw Mage::exception('Copernica_MarketingSoftware', 'Field not linked', Copernica_MarketingSoftware_Exception::FIELD_NOT_LINKED);
71
-
72
- // get database fields
73
- $output = $this->request()->get('database/'.urlencode($databaseName).'/fields');
74
-
75
- // check if api say that we have a problem
76
- if (isset($output['error']))
77
- {
78
- // check if Api tells that database does not exists or something else is wrong
79
- if (strpos($output['error']['message'], 'No database') !== false)
80
- throw Mage::exception('Copernica_MarketingSoftware', 'Database does not exists', Copernica_MarketingSoftware_Exception::DATABASE_NOT_EXISTS);
81
- else
82
- throw Mage::exception('Copernica_MarketingSoftware', 'Database could not be checked', Copernica_MarketingSoftware_Exception::API_REQUEST_ERROR);
83
- }
84
-
85
- // iterate over all fields
86
- foreach ($output['data'] as $field)
87
- {
88
- // do we have a proper field ?
89
- if ($field['name'] != $fieldName) continue;
90
-
91
- // if field have an invalid type throw an exception about that
92
- if (!$this->checkDatabaseFieldType($magentoField, $field, $databaseName)) throw Mage::exception('Copernica_MarketingSoftware', 'Field is invalid', Copernica_MarketingSoftware_Exception::FIELD_STRUCT_INVALID);
93
-
94
- // we are good
95
- return;
96
- }
97
-
98
- // desired field does not exists
99
- throw Mage::exception('Copernica_MarketingSoftware', 'Field does not exists', Copernica_MarketingSoftware_Exception::FIELD_NOT_EXISTS);
100
- }
101
-
102
- /**
103
- * Validate collection.
104
- * @param string database name
105
- * @param string collection name
106
- * @param string collection type
107
- * @throws Copenica_MarketingSoftware_Exception
108
- */
109
- public function validateCollection($databaseName, $collectionName, $collectionType)
110
- {
111
- // get database structure
112
- $output = $this->request()->get('database/'.$databaseName.'/collections');
113
-
114
- // check if we have an error
115
- if (isset($output['error']))
116
- {
117
- if (strpos($output['error']['message'], 'No database') !== false)
118
- throw Mage::exception('Copernica_MarketingSoftware', 'Database does not exists', Copernica_MarketingSoftware_Exception::DATABASE_NOT_EXISTS);
119
- else
120
- throw Mage::exception('Copernica_MarketingSoftware', 'Collection could not be checked', Copernica_MarketingSoftware_Exception::API_REQUEST_ERROR);
121
- }
122
-
123
- // check if we have any kind of output
124
- if ($output['total'] == 0) throw Mage::exception('Copernica_MarketingSoftware', 'Collection does not exists', Copernica_MarketingSoftware_Exception::COLLECTION_NOT_EXISTS);
125
-
126
- // check all collection if we have a matching one
127
- foreach ($output['data'] as $collection)
128
- {
129
- // skip if not a match
130
- if ($collection['name'] != $collectionName) continue;
131
-
132
- // check if collection have proper fields
133
- if ($this->isCollectionFieldsValid($collection['fields'], $collectionType)) return;
134
-
135
- // collection does not have all required fields, thus, invalid structure
136
- throw Mage::exception('Copernica_MarketingSoftware', 'Collection structure is invalid.', Copernica_MarketingSoftware_Exception::COLLECTION_STRUCT_INVALID);
137
- }
138
-
139
- // we didn't found a valid collection...
140
- throw Mage::exception('Copernica_MarketingSoftware', 'Collection does not exists', Copernica_MarketingSoftware_Exception::COLLECTION_NOT_EXISTS);
141
- }
142
-
143
- /**
144
- * Check collection field structure.
145
- * @param assoc can be an output from 'field' property from API
146
- * @param string type of collection
147
- * @return bool
148
- */
149
- private function isCollectionFieldsValid($collectionFieldStruct, $collectionType)
150
- {
151
- // get required fields types
152
- $requiredFields = $this->requiredCollectionFields($collectionType);
153
-
154
- // current collection fields
155
- $collectionFields = array();
156
-
157
- // iterater over all field
158
- foreach ($collectionFieldStruct['data'] as $field)
159
- {
160
- // add field to collection fields
161
- $collectionFields[] = $field['name'];
162
- }
163
-
164
- // check if collection have all required fields
165
- if (count(array_intersect($collectionFields, $requiredFields)) == count($requiredFields)) return true;
166
-
167
- // collection does not have all required fields so structure is invalid
168
- return false;
169
- }
170
-
171
- /**
172
- * Validate collection field.
173
- * @param string the database name
174
- * @param string the collection name
175
- * @param string the collection type
176
- * @param string magento field name
177
- * @param string copernica field name
178
- * @throws Copernica_MarketingSoftware_Exception
179
- */
180
- public function validateCollectionField($databaseName, $collectionName, $collectionType, $magentoFieldName, $copernicaFieldName)
181
- {
182
- // get database collections
183
- $output = $this->request()->get(
184
- 'database/'.$databaseName.'/collections'
185
- );
186
-
187
- // check if rest output have an error
188
- if (isset($output['error'])) {
189
- // check if api is telling us that database does not exists
190
- if (strpos($output['error']['message'], 'No database') !== false) throw Mage::exception('Copernica_MarketingSoftware', 'Database does not exists', Copernica_MarketingSoftware_Exception::DATABASE_NOT_EXISTS);
191
-
192
- // something is wrong with request or connection
193
- else throw Mage::exception('Copernica_MarketingSoftware', 'API error', Copernica_MarketingSoftware_Exception::API_REQUEST_ERROR);
194
- }
195
-
196
- // check if we have any collections
197
- if ($output['total'] == 0) throw Mage::exception('Copernica_MarketingSoftware', 'Collection does not exists', Copernica_MarketingSoftware_Exception::COLLECTION_NOT_EXISTS);
198
-
199
- // placeholder for collection Id
200
- $collectionId = false;
201
-
202
- // iterate over collections to find one that we need
203
- foreach ($output['data'] as $collection)
204
- {
205
- if ($collection['name'] == $collectionName) $collectionId = $collection['ID'];
206
- }
207
-
208
- // check if we don't have collection Id
209
- if ($collectionId === false) throw Mage::exception('Copernica_MarketingSoftware', 'Collection does not exits', Copernica_MarketingSoftware_Exception::COLLECTION_NOT_EXISTS);
210
-
211
- // get all current collections fields
212
- $output = $this->request()->get( 'collection/'.$collectionId.'/fields' );
213
-
214
- // check if we have some kind of error from API
215
- if (isset($output['error']) || !isset($output['data']))
216
- {
217
- /*
218
- * We don't check if API is telling us that collection does not exists
219
- * cause couple of linew above we did confirm that. So any error
220
- * will be cause of API error or connection error.
221
- */
222
- throw Mage::exception('Copernica_MarketingSoftware', 'API error', Copernica_MarketingSoftware_Exception::API_REQUEST_ERROR);
223
- }
224
-
225
- // check if we have any fields
226
- if ($output['total'] == 0)
227
- {
228
- throw Mage::exception('Copernica_MarketingSoftware', 'Field does not exists', Copernica_MarketingSoftware_Exception::FIELD_NOT_EXISTS);
229
- }
230
-
231
- // iterate over all fields and check matching one
232
- foreach ($output['data'] as $field)
233
- {
234
- // skip not matching fields
235
- if ($field['name'] != $copernicaFieldName) continue;
236
-
237
- // get field definition
238
- $fieldDefinition = Mage::helper('marketingsoftware/data')->getCollectionFieldDefinition($collectionType, $magentoFieldName);
239
-
240
- /*
241
- * Why we only check type of the field? Copernica REST API does
242
- * output only ID, name, and type properties when we ask for collection
243
- * field. Thus we can only validate type of the field and check
244
- * if it's valid.
245
- */
246
- if ($fieldDefinition['type'] != $field['type'])
247
- throw Mage::exception('Copernica_MarketingSoftware', 'Field has invalid structure. Field should have \''.$fieldDefinition['type'].'\' type.', Copernica_MarketingSoftware_Exception::FIELD_STRUCT_INVALID);
248
-
249
- // if we are here that means field exists and have proper structure
250
- return;
251
- }
252
-
253
- // we did not found a proper field
254
- throw Mage::exception('Copernica_MarketingSoftware', 'Field does not exists', Copernica_MarketingSoftware_Exception::FIELD_NOT_EXISTS);
255
- }
256
-
257
- /**
258
- * Get required fields for a collection of given type.
259
- * @param string
260
- * @return array
261
- */
262
- private function requiredCollectionFields($collectionType)
263
- {
264
- switch ($collectionType)
265
- {
266
- case 'cartproducts': return Mage::helper('marketingsoftware')->requiredCartItemFields();
267
- case 'orders' : return Mage::helper('marketingsoftware')->requiredOrderFields();
268
- case 'orderproducts': return Mage::helper('marketingsoftware')->requiredOrderItemFields();
269
- case 'addresses': return Mage::helper('marketingsoftware')->requiredAddressFields();
270
- case 'viewedproduct': return Mage::helper('marketingsoftware')->requiredViewedProductFields();
271
-
272
- // by default we not require any collection fields
273
- default: return array();
274
- }
275
- }
276
-
277
- /**
278
- * Check database field if copernica type match magento type.
279
- * @param string Name of magento field
280
- * @param assoc Assoc with structure of the field.
281
- * @param string Database name that will be used to check the field.
282
- * @return boolean
283
- */
284
- private function checkDatabaseFieldType($magentoField, $copernicaStructure, $databaseName)
285
- {
286
- switch ($magentoField) {
287
- case 'email' : return $this->checkEmailField($copernicaStructure);
288
- case 'newsletter': return $this->checkNewsletterField($copernicaStructure, $databaseName);
289
- case 'birthdate': return $this->checkDateField($copernicaStructure);
290
- case 'registrationDate': return $this->checkDatetimeField($copernicaStructure);
291
- default: return $this->checkDefaultField($copernicaStructure);
292
- }
293
- }
294
-
295
- /**
296
- * Check if newsletter field is correct
297
- * @return boolean
298
- */
299
- private function checkNewsletterField($copernicaStructure, $databaseName)
300
- {
301
- // get info about unsubscribe options set on database
302
- $databaseUnsubscribe = $this->request()->get('database/'.urlencode($databaseName).'/unsubscribe');
303
-
304
- // if we have a general error we can not check the field
305
- if (isset($databaseUnsubscribe['error'])) throw Mage::exception('Copernica_MarketingSoftware', 'Field could not be checked', Copernica_MarketingSoftware_Exception::API_REQUEST_ERROR);
306
-
307
- // check if unsubbscibe behavior is set to update
308
- if ($databaseUnsubscribe['behavior'] != 'update') return false;
309
-
310
- // get the field name
311
- $fieldName = $copernicaStructure['name'];
312
-
313
- // if current field is not in unsub fields then field is wrong
314
- if (!array_key_exists($fieldName, $databaseUnsubscribe['fields'])) return false;
315
-
316
- // if new value is not fixed one then field is invalid
317
- if ($databaseUnsubscribe['fields'][$fieldName] != 'unsubscribed_copernica') return false;
318
-
319
- // get all callbacks tied to database
320
- $databaseCallbacks = $this->request()->get('database/'.urlencode($databaseName).'/callbacks');
321
-
322
- // if we have an error we want to throw an exception
323
- if (isset($databaseCallbacks['error'])) throw Mage::exception('Copernica_MarketingSoftware', 'Field could not be checked', Copernica_MarketingSoftware_Exception::API_REQUEST_ERROR);
324
-
325
- // get the magento callback url
326
- $callbackUrl = Mage::helper('marketingsoftware')->unsubscribeCallbackUrl();
327
-
328
- // do we have any callbacks?
329
- if ($databaseCallbacks['total'] == 0) return false;
330
-
331
- // we have to check all callbacks
332
- foreach ($databaseCallbacks['data'] as $callback)
333
- {
334
- // check if we have a proper callback
335
- if ($callback['url'] == $callbackUrl && $callback['method'] == 'json' && $callback['expression'] == "profile.$fieldName == 'unsubscribed_copernica';") return true;
336
- }
337
-
338
- // we didn't found a matching callback
339
- return false;
340
- }
341
-
342
- /**
343
- * Check field if it's a email field.
344
- * @param assoc Assoc array that describes structure of the field
345
- * @return boolean
346
- */
347
- private function checkEmailField($copernicaStructure)
348
- {
349
- return $copernicaStructure['type'] == 'email';
350
- }
351
-
352
- /**
353
- * Check field if it's a date field.
354
- * @param assoc Assoc array that describes structure of the field
355
- * @return boolean
356
- */
357
- private function checkDateField($copernicaStructure)
358
- {
359
- return $copernicaStructure['type'] == 'date';
360
- }
361
-
362
- /**
363
- * Check field if it's a datetime field
364
- * @param assoc Assoc array that describes structure of the field
365
- * @return boolean
366
- */
367
- private function checkDatetimeField($copernicaStructure)
368
- {
369
- return $copernicaStructure['type'] == 'datetime';
370
- }
371
-
372
- /**
373
- * Check field if it's a default field.
374
- * @param assoc Assoc array that describes structure of the field
375
- * @return boolean
376
- */
377
- private function checkDefaultField($copernicaStructure)
378
- {
379
- return $copernicaStructure['type'] == 'text';
380
- }
381
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app/code/community/Copernica/MarketingSoftware/Helper/Config.php CHANGED
@@ -33,33 +33,35 @@ class Copernica_MarketingSoftware_Helper_Config extends Mage_Core_Helper_Abstrac
33
  {
34
  /**
35
  * Define a prefix used for the config
36
- * @name CONFIG_BASE a prefix
 
37
  */
38
  const CONFIG_BASE = 'marketingsoftware/';
39
 
40
  /**
41
  * Holds a list of previously requested key names
42
- * @var array
 
43
  */
44
  protected static $_keyNameCache = array();
45
 
46
  /**
47
  * List of already requested or used config entries
48
  *
49
- * @var array
50
  */
51
  protected static $_configEntryCache = array();
52
 
53
  /**
54
  * Magic method to get configurations from the database
55
- * @param string $method
56
- * @param array $params
57
- * @return string
 
58
  */
59
  public function __call($method, $params)
60
  {
61
  switch (substr($method, 0, 3)) {
62
-
63
  case 'get':
64
  $key = $this->_toKeyName(substr($method, 3));
65
  return $this->_getCustomConfig($key);
@@ -67,8 +69,9 @@ class Copernica_MarketingSoftware_Helper_Config extends Mage_Core_Helper_Abstrac
67
  break;
68
 
69
  case 'set':
70
- // Check if the first parameter is set
71
- if (!isset($params) || !isset($params[0])) return false;
 
72
 
73
  $key = $this->_toKeyName(substr($method, 3));
74
  $this->_setCustomConfig($key, $params[0]);
@@ -105,8 +108,9 @@ class Copernica_MarketingSoftware_Helper_Config extends Mage_Core_Helper_Abstrac
105
 
106
  /**
107
  * Tries to get config value from custom config table
108
- * @param string $key
109
- * @return string
 
110
  */
111
  protected function _getCustomConfig($key)
112
  {
@@ -115,6 +119,7 @@ class Copernica_MarketingSoftware_Helper_Config extends Mage_Core_Helper_Abstrac
115
  }
116
 
117
  $model = $this->_getModel($key);
 
118
  if ($model !== false) {
119
  return $model->getValue();
120
  }
@@ -124,8 +129,9 @@ class Copernica_MarketingSoftware_Helper_Config extends Mage_Core_Helper_Abstrac
124
 
125
  /**
126
  * Sets a config entry in the custom config tab
127
- * @param string $key
128
- * @param string $value
 
129
  */
130
  protected function _setCustomConfig($key, $value)
131
  {
@@ -143,7 +149,6 @@ class Copernica_MarketingSoftware_Helper_Config extends Mage_Core_Helper_Abstrac
143
  self::$_configEntryCache[$key] = $model->getValue();
144
 
145
  return $model->getValue();
146
-
147
  } catch (Exception $e) {
148
  Mage::log('Marketingsoftware Config: ' . $e->getMessage());
149
  }
@@ -151,8 +156,9 @@ class Copernica_MarketingSoftware_Helper_Config extends Mage_Core_Helper_Abstrac
151
 
152
  /**
153
  * Checks if an entry exists in the custom config table
154
- * @param string $key
155
- * @return boolean
 
156
  */
157
  protected function _hasCustomConfig($key)
158
  {
@@ -162,8 +168,8 @@ class Copernica_MarketingSoftware_Helper_Config extends Mage_Core_Helper_Abstrac
162
  /**
163
  * Loads the requested model config object if possible
164
  *
165
- * @param string $key
166
- * @return Copernica_MarketingSoftware_Model_Config
167
  */
168
  protected function _getModel($key)
169
  {
@@ -171,6 +177,7 @@ class Copernica_MarketingSoftware_Helper_Config extends Mage_Core_Helper_Abstrac
171
 
172
  if ($model && $model->getId()) {
173
  self::$_configEntryCache[$key] = $model->getValue();
 
174
  return $model;
175
  }
176
 
@@ -181,8 +188,8 @@ class Copernica_MarketingSoftware_Helper_Config extends Mage_Core_Helper_Abstrac
181
  * Prepends uppercase characters with underscores and lowers
182
  * the whole string
183
  *
184
- * @param string $name
185
- * @return string
186
  */
187
  protected function _toKeyName($name)
188
  {
@@ -199,7 +206,8 @@ class Copernica_MarketingSoftware_Helper_Config extends Mage_Core_Helper_Abstrac
199
  /**
200
  * Get the config item from the custom config table, otherwise from
201
  * the basic magento component.
202
- * @param string $name Name of the config parameter
 
203
  */
204
  protected function _getConfig($name)
205
  {
@@ -208,25 +216,25 @@ class Copernica_MarketingSoftware_Helper_Config extends Mage_Core_Helper_Abstrac
208
  if ($this->_hasCustomConfig($name)) {
209
  return $this->_getCustomConfig($name);
210
  } else {
211
- // scope is added to the beginning of the path
212
  return (string) Mage::getConfig()->getNode(self::CONFIG_BASE . $name, 'default', 0);
213
  }
214
  }
215
 
216
  /**
217
  * Set the config item from the basic magento component
218
- * @param string $name Name of the config parameter
219
- * @param string $value Value that should be stored in the config
 
220
  */
221
  protected function _setConfig($name, $value)
222
  {
223
- // is this value new the same as the existing value
224
- if ($value === $this->_getConfig($name)) return;
 
225
 
226
- // Store the value in the custom config
227
  $this->_setCustomConfig($name, $value);
228
 
229
- // some config items are not that interesting
230
  if (in_array($name, array(
231
  'customer_progress_status',
232
  'order_progress_status',
@@ -236,44 +244,47 @@ class Copernica_MarketingSoftware_Helper_Config extends Mage_Core_Helper_Abstrac
236
  'cronjob_processedtasks',
237
  ))) return;
238
 
239
- // We have to reset the progress status
240
  $this->setCustomerProgressStatus('0');
 
241
  $this->setOrderProgressStatus('0');
 
242
  $this->setSubscriptionProgressStatus('0');
243
  }
244
 
245
  /**
246
  * Check if store is enabled.
247
- * @param int
248
- * @return bool
 
249
  */
250
  public function isEnabledStore($storeId)
251
  {
252
- // get enabled stores
253
  $stores = @unserialize($this->_getConfig('enabled_stores'));
254
 
255
- // if we don't have an array we will return true
256
- if (!is_array($stores)) return true;
 
257
 
258
- // check if store Id is in enabled array
259
  return in_array($storeId, $stores);
260
  }
261
 
262
  /**
263
  * Set stores. Pass null to disable store filtering.
264
- * @param array|null
265
- * @return Copernica_MarketingSoftware_Helper_Config
 
266
  */
267
  public function setEnabledStores($collectionOfStoresId)
268
  {
269
- // set stores
270
  $this->_setConfig('enabled_stores', serialize($collectionOfStoresId));
 
271
  return $this;
272
  }
273
 
274
  /**
275
  * Get list of enabled stores
276
- * @return array|null
 
277
  */
278
  public function getEnabledStores()
279
  {
@@ -282,7 +293,8 @@ class Copernica_MarketingSoftware_Helper_Config extends Mage_Core_Helper_Abstrac
282
 
283
  /**
284
  * Get the hostname from the config
285
- * @return String
 
286
  */
287
  public function getHostname()
288
  {
@@ -291,18 +303,20 @@ class Copernica_MarketingSoftware_Helper_Config extends Mage_Core_Helper_Abstrac
291
 
292
  /**
293
  * Set the hostname from the config
294
- * @return String
295
- * @return Copernica_MarketingSoftware_Helper_Config
296
  */
297
  public function setHostname($value)
298
  {
299
  $this->_setConfig('hostname', $value);
 
300
  return $this;
301
  }
302
 
303
  /**
304
  * Get the name of the database
305
- * @return String
 
306
  */
307
  public function getDatabaseName()
308
  {
@@ -311,347 +325,603 @@ class Copernica_MarketingSoftware_Helper_Config extends Mage_Core_Helper_Abstrac
311
 
312
  /**
313
  * Set the name of the database
314
- * @param String
315
- * @return Copernica_MarketingSoftware_Helper_Config
 
316
  */
317
  public function setDatabaseName($value)
318
  {
319
  $this->_setConfig('database', $value);
 
320
  return $this;
321
  }
322
 
323
  /**
324
  * Get the linked customer fields
325
- * @return array assoc array of fields which have been linked
 
326
  */
327
  public function getLinkedCustomerFields()
328
  {
329
- // Get the value
330
  $value = $this->_getConfig('linked_customer_fields');
331
-
332
- // What value is found?
333
  $value = empty($value) ? array() : json_decode($value, true);
334
 
335
- // is this an old data entry (prior to 1.2.0)
336
- if (!isset($value['customer_email'])) return $value;
 
337
 
338
- // yes this is old data... time for a small conversion
339
  $oldValues = $value;
 
340
  $newValues = array();
341
 
342
- // iterate over the data
343
- foreach ($oldValues as $key => $value)
344
- {
345
  $key = str_replace('customer_', '', $key);
 
346
  $newValues[$key] = $value;
347
  }
348
 
349
- // store the converted values
350
  $this->setLinkedCustomerFields($newValues);
351
 
352
- // return the new Values
353
  return $newValues;
354
  }
355
 
356
  /**
357
  * Set the linked customer fields
358
- * @param array assoc array of fields which have been linked
359
- * @return Copernica_MarketingSoftware_Helper_Config
 
360
  */
361
  public function setLinkedCustomerFields($value)
362
  {
363
  $this->_setConfig('linked_customer_fields', json_encode($value), true);
 
364
  return $this;
365
  }
366
 
367
  /**
368
- * Get the name of the not-ordered products collection
369
- * @return string
 
370
  */
371
- public function getCartItemsCollectionName()
372
  {
373
- return $this->_getConfig('cart_items_collection_name');
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
374
  }
375
 
376
  /**
377
- * Set the name of the not-ordered products collection
378
- * @param String
379
- * @return Copernica_MarketingSoftware_Helper_Config
 
380
  */
381
- public function setCartItemsCollectionName($value)
382
  {
383
- $this->_setConfig('cart_items_collection_name', $value);
 
384
  return $this;
385
  }
386
 
387
  /**
388
- * Get the linked customer fields
389
- * @return array assoc array of fields which have been linked
 
390
  */
391
- public function getLinkedCartItemFields()
392
  {
393
- $value = $this->_getConfig('linked_cart_item_fields');
394
 
395
- // What value is found?
396
- if (empty($value)) return array();
397
- else return json_decode($value, true);
 
 
398
  }
399
 
400
  /**
401
- * Get the linked customer fields
402
- * @param array assoc array of fields which have been linked
403
- * @return Copernica_MarketingSoftware_Helper_Config
 
404
  */
405
- public function setLinkedCartItemFields($value)
406
  {
407
- $this->_setConfig('linked_cart_item_fields', json_encode($value), true);
 
408
  return $this;
409
  }
410
 
411
  /**
412
  * Get the name of the orders collection
413
- * @return String
 
414
  */
415
  public function getOrdersCollectionName()
416
  {
417
  return $this->_getConfig('orders_collection_name');
418
  }
419
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
420
  /**
421
  * Set the name of the orders collection
422
- * @param String
423
- * @return Copernica_MarketingSoftware_Helper_Config
 
424
  */
425
  public function setOrdersCollectionName($value)
426
  {
427
  $this->_setConfig('orders_collection_name', $value);
 
428
  return $this;
429
  }
430
 
431
  /**
432
  * Get the linked order fields
433
- * @return array assoc array of fields which have been linked
 
434
  */
435
  public function getLinkedOrderFields()
436
  {
437
- // Get the value
438
  $value = $this->_getConfig('linked_order_fields');
439
-
440
- // What value is found?
441
  $value = empty($value) ? array() : json_decode($value, true);
442
 
443
- // is this an old data entry (prior to 1.2.0)
444
- if (!isset($value['order_timestamp'])) return $value;
 
445
 
446
- // yes this is old data... time for a small conversion
447
  $oldValues = $value;
 
448
  $newValues = array();
449
 
450
- // iterate over the data
451
- foreach ($oldValues as $key => $value)
452
- {
453
- // remove the order prefix and rename the qty field
454
  $key = ($key == 'order_qty') ? 'quantity' : str_replace('order_', '', $key);
 
455
  $newValues[$key] = $value;
456
  }
457
 
458
- // store the converted values
459
  $this->setLinkedOrderFields($newValues);
460
 
461
- // return the new Values
462
  return $newValues;
463
  }
464
 
465
  /**
466
  * Set the linked order fields
467
- * @param array assoc array of fields which have been linked
468
- * @return Copernica_MarketingSoftware_Helper_Config
 
469
  */
470
  public function setLinkedOrderFields($value)
471
  {
472
  $this->_setConfig('linked_order_fields', json_encode($value), true);
 
473
  return $this;
474
  }
475
 
476
  /**
477
  * Get the name of the collection were all the orders are stored
478
- * @return String
479
- * @return Copernica_MarketingSoftware_Helper_Config
 
 
 
 
 
 
 
 
 
 
480
  */
481
- public function getOrderItemsCollectionName()
482
  {
483
- return $this->_getConfig('order_items_collection_name');
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
484
  }
485
 
486
  /**
487
  * Get the name of the collection were all the orders are stored
488
- * @param String
 
489
  */
490
- public function setOrderItemsCollectionName($value)
491
  {
492
- $this->_setConfig('order_items_collection_name', $value);
 
493
  return $this;
494
  }
495
 
496
  /**
497
  * Get the linked order item fields
498
- * @return array assoc array of fields which have been linked
 
499
  */
500
  public function getLinkedOrderItemFields()
501
  {
502
  $value = $this->_getConfig('linked_order_item_fields');
503
-
504
- // What value is found?
505
  $value = empty($value) ? array() : json_decode($value, true);
506
 
507
- // is this an old data entry (prior to 1.2.0)
508
- if (!isset($value['product_internal_id'])) return $value;
 
509
 
510
- // yes this is old data... time for a small conversion
511
  $oldValues = $value;
 
512
  $newValues = array();
513
 
514
- // iterate over the data
515
- foreach ($oldValues as $key => $value)
516
- {
517
- // remove the order prefix and rename the qty field
518
- if ($key == 'product_qty') $key = 'quantity';
519
- elseif ($key == 'product_base_row_total') $key = 'total_price';
520
- else $key = str_replace('product_', '', $key);
 
 
 
 
 
 
 
521
 
522
- // assign it to the new values
523
  $newValues[$key] = $value;
524
  }
525
 
526
- // store the converted values
527
  $this->setLinkedOrderItemFields($newValues);
528
 
529
- // return the new Values
530
  return $newValues;
531
  }
532
 
533
  /**
534
  * Set the linked order item fields
535
- * @param array assoc array of fields which have been linked
536
- * @return Copernica_MarketingSoftware_Helper_Config
 
537
  */
538
  public function setLinkedOrderItemFields($value)
539
  {
540
  $this->_setConfig('linked_order_item_fields', json_encode($value), true);
 
541
  return $this;
542
  }
543
 
544
  /**
545
  * Get the address collection name
546
- * @return String
 
547
  */
548
  public function getAddressesCollectionName()
549
  {
550
  return $this->_getConfig('address_collection_name');
551
  }
552
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
553
  /**
554
  * Set the name of the collection with addresses
555
  */
556
- public function setAddressesCollectionName($value)
557
  {
558
  $this->_setConfig('address_collection_name', $value);
 
559
  return $this;
560
  }
561
 
562
  /**
563
  * Get the linked address fields
564
- * @return array assoc array of fields which have been linked
 
565
  */
566
  public function getLinkedAddressFields()
567
  {
568
  $value = $this->_getConfig('linked_address_fields');
569
-
570
- // What value is found?
571
  $value = empty($value) ? array() : json_decode($value, true);
 
 
 
 
572
 
573
- // is this an old data entry (prior to 1.2.0)
574
- if (!isset($value['address_firstname'])) return $value;
575
-
576
- // yes this is old data... time for a small conversion
577
  $oldValues = $value;
 
578
  $newValues = array();
579
 
580
- // iterate over the data
581
- foreach ($oldValues as $key => $value)
582
- {
583
- // remove the order prefix and rename the qty field
584
  $key = str_replace('address_', '', $key);
 
585
  $newValues[$key] = $value;
586
  }
587
 
588
- // store the converted values
589
  $this->setLinkedAddressFields($newValues);
590
 
591
- // return the new Values
592
  return $newValues;
593
  }
594
 
595
  /**
596
  * set the linked address fields
597
- * @param array assoc array of fields which have been linked
598
- * @return Copernica_MarketingSoftware_Helper_Config
 
599
  */
600
  public function setLinkedAddressFields($value)
601
  {
602
  $this->_setConfig('linked_address_fields', json_encode($value), true);
 
603
  return $this;
604
  }
605
 
606
  /**
607
  * Get the name of the viewed products collection
608
- * @return string
 
609
  */
610
  public function getViewedProductCollectionName()
611
  {
612
  return $this->_getConfig('viewed_product_collection_name');
613
  }
614
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
615
  /**
616
  * Set the name of the viewed products collection
617
- * @param String
 
618
  * @return Copernica_MarketingSoftware_Helper_Config
619
  */
620
  public function setViewedProductCollectionName($value)
621
  {
622
  $this->_setConfig('viewed_product_collection_name', $value);
 
623
  return $this;
624
  }
625
 
626
  /**
627
  * Get the linked customer fields
628
- * @return array assoc array of fields which have been linked
 
629
  */
630
  public function getLinkedViewedProductFields()
631
  {
632
  $value = $this->_getConfig('linked_viewed_product_fields');
633
 
634
- // What value is found?
635
- if (empty($value)) return array();
636
- else return json_decode($value, true);
 
 
637
  }
638
 
639
  /**
640
  * Get the linked customer fields
641
- * @param array assoc array of fields which have been linked
642
- * @return Copernica_MarketingSoftware_Helper_Config
 
643
  */
644
  public function setLinkedViewedProductFields($value)
645
  {
646
  $this->_setConfig('linked_viewed_product_fields', json_encode($value), true);
 
647
  return $this;
648
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
649
 
650
  /**
651
  * Get the progress status for customers
652
  * This is the created timestamp of the most recent customer which has
653
  * been queued for synchronisation
654
- * @return datetime
 
655
  */
656
  public function getCustomerProgressStatus()
657
  {
@@ -662,11 +932,14 @@ class Copernica_MarketingSoftware_Helper_Config extends Mage_Core_Helper_Abstrac
662
  * Set the progress status for customers
663
  * This is the created timestamp of the most recent customer which has
664
  * been queued for synchronisation
665
- * @param datetime
 
 
666
  */
667
  public function setCustomerProgressStatus($value)
668
  {
669
  $this->_setConfig('customer_progress_status', $value);
 
670
  return $this;
671
  }
672
 
@@ -674,6 +947,7 @@ class Copernica_MarketingSoftware_Helper_Config extends Mage_Core_Helper_Abstrac
674
  * Get the progress status for orders
675
  * This is the created timestamp of the most recent order which has
676
  * been queued for synchronisation
 
677
  * @return datetime
678
  */
679
  public function getOrderProgressStatus()
@@ -685,11 +959,14 @@ class Copernica_MarketingSoftware_Helper_Config extends Mage_Core_Helper_Abstrac
685
  * Set the progress status for orders
686
  * This is the created timestamp of the most recent order which has
687
  * been queued for synchronisation
688
- * @param datetime
 
 
689
  */
690
  public function setOrderProgressStatus($value)
691
  {
692
  $this->_setConfig('order_progress_status', $value);
 
693
  return $this;
694
  }
695
 
@@ -697,6 +974,7 @@ class Copernica_MarketingSoftware_Helper_Config extends Mage_Core_Helper_Abstrac
697
  * Get the progress status for subscriptions
698
  * This is the created timestamp of the most recent subscription which has
699
  * been queued for synchronisation
 
700
  * @return datetime
701
  */
702
  public function getSubscriptionProgressStatus()
@@ -708,17 +986,21 @@ class Copernica_MarketingSoftware_Helper_Config extends Mage_Core_Helper_Abstrac
708
  * Set the progress status for subscriptions
709
  * This is the created timestamp of the most recent subscription which has
710
  * been queued for synchronisation
711
- * @param datetime
 
 
712
  */
713
  public function setSubscriptionProgressStatus($value)
714
  {
715
  $this->_setConfig('subscription_progress_status', $value);
 
716
  return $this;
717
  }
718
 
719
  /**
720
  * Get the last start time of the cronjob.
721
- * @return datetime
 
722
  */
723
  public function getLastStartTimeCronjob()
724
  {
@@ -727,17 +1009,21 @@ class Copernica_MarketingSoftware_Helper_Config extends Mage_Core_Helper_Abstrac
727
 
728
  /**
729
  * Set the last start time of the cronjob.
730
- * @param datetime
 
 
731
  */
732
  public function setLastStartTimeCronjob($value)
733
  {
734
  $this->_setConfig('cronjob_starttime', $value);
 
735
  return $this;
736
  }
737
 
738
  /**
739
  * Get the last end time of the cronjob.
740
- * @return datetime
 
741
  */
742
  public function getLastEndTimeCronjob()
743
  {
@@ -746,16 +1032,20 @@ class Copernica_MarketingSoftware_Helper_Config extends Mage_Core_Helper_Abstrac
746
 
747
  /**
748
  * Set the last end time of the cronjob.
749
- * @param datetime
 
 
750
  */
751
  public function setLastEndTimeCronjob($value)
752
  {
753
  $this->_setConfig('cronjob_endtime', $value, true);
 
754
  return $this;
755
  }
756
 
757
  /**
758
  * Get the number of processed records of the last cronjob run.
 
759
  * @return integer
760
  */
761
  public function getLastCronjobProcessedTasks()
@@ -765,29 +1055,35 @@ class Copernica_MarketingSoftware_Helper_Config extends Mage_Core_Helper_Abstrac
765
 
766
  /**
767
  * Set the last end time of the cronjob.
768
- * @param integer
 
 
769
  */
770
  public function setLastCronjobProcessedTasks($value)
771
  {
772
  $this->_setConfig('cronjob_processedtasks', $value);
 
773
  return $this;
774
  }
775
 
776
  /**
777
  * Should our extension use vanilla magento cron schedulers to execute
778
  * queue? Or should we sync all data by processQueue.php file?
779
- * @param boolean
780
- * @return self
 
781
  */
782
  public function setVanillaCrons($vanilla)
783
  {
784
  $this->_setConfig('vanilla_crons', (bool) $vanilla ? 1 : 0);
 
785
  return $this;
786
  }
787
 
788
  /**
789
  * Get stored config about vanilla crons schedulers.
790
- * @return boolean
 
791
  */
792
  public function getVanillaCrons()
793
  {
@@ -796,6 +1092,7 @@ class Copernica_MarketingSoftware_Helper_Config extends Mage_Core_Helper_Abstrac
796
 
797
  /**
798
  * Get last timestamp that we used to check magento forgotten carts
 
799
  * @return string
800
  */
801
  public function getAbandonedLastCheck()
@@ -807,22 +1104,23 @@ class Copernica_MarketingSoftware_Helper_Config extends Mage_Core_Helper_Abstrac
807
 
808
  /**
809
  * Set timestamp when we did check forgotten carts list
810
- * @param string
811
- * @return self
 
812
  */
813
  public function setAbandonedLastCheck($time = null)
814
  {
815
- // set time
816
  $this->_setConfig('lastAbandonedCheck', $time ? $time : date("Y-m-d H:i:s"));
817
 
818
- // allow chaining
819
  return $this;
820
  }
821
 
822
  /**
823
- * Set number of minutes that have to pass from last item cart update to consider
824
  * cart abandonded.
825
- * @param int
 
 
826
  */
827
  public function setAbandonedTimeout($timeout)
828
  {
@@ -832,21 +1130,24 @@ class Copernica_MarketingSoftware_Helper_Config extends Mage_Core_Helper_Abstrac
832
  }
833
 
834
  /**
835
- * Get number of minutes that have to pass from last item cart update to consider
 
 
836
  * @return int
837
  */
838
  public function getAbandonedTimeout()
839
  {
840
  $timeout = $this->_getConfig('abandondedTimeout');
841
 
842
- // return stored timeout value or default value (90 minutes)
843
  return is_numeric($timeout) ? $timeout : 90;
844
  }
845
 
846
  /**
847
  * Carts older than supplied number of minutes will not be synchronized
848
  * with Copernica.
849
- * @param int
 
 
850
  */
851
  public function setAbandonedPeriod($timeout)
852
  {
@@ -858,22 +1159,23 @@ class Copernica_MarketingSoftware_Helper_Config extends Mage_Core_Helper_Abstrac
858
  /**
859
  * Carts older than supplied number of minutes will not be synchronized
860
  * with Copernica.
 
861
  * @return int
862
  */
863
  public function getAbandonedPeriod()
864
  {
865
  $timeout = $this->_getConfig('abandondedPeriod');
866
 
867
- // return stored period value or default value (21600 minutes)
868
- return is_numeric($timeout) ? $timeout : 21600; // (60 minutes * 24 hours * 15 days)
869
  }
870
 
871
  /**
872
- * Should finished (removed or ordered) cart items be removed from profile?
873
- * @param boolean
874
- * @return self
 
875
  */
876
- public function setRemoveFinishedCartItems($remove)
877
  {
878
  $this->_setConfig('removeFinished', $remove ? 1 : 0);
879
 
@@ -881,10 +1183,11 @@ class Copernica_MarketingSoftware_Helper_Config extends Mage_Core_Helper_Abstrac
881
  }
882
 
883
  /**
884
- * Should finished (removed or ordered) cart items be removed from profile?
 
885
  * @return bool
886
  */
887
- public function getRemoveFinishedCartItems()
888
  {
889
  return (bool)$this->_getConfig('removeFinished');
890
  }
@@ -894,29 +1197,28 @@ class Copernica_MarketingSoftware_Helper_Config extends Mage_Core_Helper_Abstrac
894
  */
895
  public function clearLinkedCollections()
896
  {
897
- // clear data about cart items
898
- $this->unsCartItemsCollectionName();
899
- $this->unsCartItemsCollectionID();
900
- $this->unsLinkedCartItemFields();
901
 
902
- // clear data about orders
903
  $this->unsOrdersCollectionName();
904
  $this->unsOrdersCollectionId();
905
  $this->unsLinkedOrderFields();
906
 
907
- // clear data about order items
908
- $this->unsOrderItemsCollectionName();
909
- $this->unsOrderItemsCollectionId();
910
  $this->unsLinkedOrderItemFields();
911
 
912
- // clear data about addresses
913
  $this->unsAddressCollectionName();
914
  $this->unsAddressCollectionId();
915
  $this->unsLinkedAddressFields();
916
 
917
- // clear data about viewed products
918
  $this->unsViewedProductCollectionName();
919
  $this->unsViewedProductCollectionId();
920
  $this->unsLinkedViewedProductFields();
 
 
 
 
921
  }
922
  }
33
  {
34
  /**
35
  * Define a prefix used for the config
36
+ *
37
+ * @name CONFIG_BASE
38
  */
39
  const CONFIG_BASE = 'marketingsoftware/';
40
 
41
  /**
42
  * Holds a list of previously requested key names
43
+ *
44
+ * @var array
45
  */
46
  protected static $_keyNameCache = array();
47
 
48
  /**
49
  * List of already requested or used config entries
50
  *
51
+ * @var array
52
  */
53
  protected static $_configEntryCache = array();
54
 
55
  /**
56
  * Magic method to get configurations from the database
57
+ *
58
+ * @param string $method
59
+ * @param array $params
60
+ * @return string
61
  */
62
  public function __call($method, $params)
63
  {
64
  switch (substr($method, 0, 3)) {
 
65
  case 'get':
66
  $key = $this->_toKeyName(substr($method, 3));
67
  return $this->_getCustomConfig($key);
69
  break;
70
 
71
  case 'set':
72
+ if (!isset($params) || !isset($params[0])) {
73
+ return false;
74
+ }
75
 
76
  $key = $this->_toKeyName(substr($method, 3));
77
  $this->_setCustomConfig($key, $params[0]);
108
 
109
  /**
110
  * Tries to get config value from custom config table
111
+ *
112
+ * @param string $key
113
+ * @return string
114
  */
115
  protected function _getCustomConfig($key)
116
  {
119
  }
120
 
121
  $model = $this->_getModel($key);
122
+
123
  if ($model !== false) {
124
  return $model->getValue();
125
  }
129
 
130
  /**
131
  * Sets a config entry in the custom config tab
132
+ *
133
+ * @param string $key
134
+ * @param string $value
135
  */
136
  protected function _setCustomConfig($key, $value)
137
  {
149
  self::$_configEntryCache[$key] = $model->getValue();
150
 
151
  return $model->getValue();
 
152
  } catch (Exception $e) {
153
  Mage::log('Marketingsoftware Config: ' . $e->getMessage());
154
  }
156
 
157
  /**
158
  * Checks if an entry exists in the custom config table
159
+ *
160
+ * @param string $key
161
+ * @return boolean
162
  */
163
  protected function _hasCustomConfig($key)
164
  {
168
  /**
169
  * Loads the requested model config object if possible
170
  *
171
+ * @param string $key
172
+ * @return Copernica_MarketingSoftware_Model_Config
173
  */
174
  protected function _getModel($key)
175
  {
177
 
178
  if ($model && $model->getId()) {
179
  self::$_configEntryCache[$key] = $model->getValue();
180
+
181
  return $model;
182
  }
183
 
188
  * Prepends uppercase characters with underscores and lowers
189
  * the whole string
190
  *
191
+ * @param string $name
192
+ * @return string
193
  */
194
  protected function _toKeyName($name)
195
  {
206
  /**
207
  * Get the config item from the custom config table, otherwise from
208
  * the basic magento component.
209
+ *
210
+ * @param string $name
211
  */
212
  protected function _getConfig($name)
213
  {
216
  if ($this->_hasCustomConfig($name)) {
217
  return $this->_getCustomConfig($name);
218
  } else {
 
219
  return (string) Mage::getConfig()->getNode(self::CONFIG_BASE . $name, 'default', 0);
220
  }
221
  }
222
 
223
  /**
224
  * Set the config item from the basic magento component
225
+ *
226
+ * @param string $name
227
+ * @param string $value
228
  */
229
  protected function _setConfig($name, $value)
230
  {
231
+ if ($value === $this->_getConfig($name)) {
232
+ return;
233
+ }
234
 
 
235
  $this->_setCustomConfig($name, $value);
236
 
237
+ // Some config items are not needed
238
  if (in_array($name, array(
239
  'customer_progress_status',
240
  'order_progress_status',
244
  'cronjob_processedtasks',
245
  ))) return;
246
 
 
247
  $this->setCustomerProgressStatus('0');
248
+
249
  $this->setOrderProgressStatus('0');
250
+
251
  $this->setSubscriptionProgressStatus('0');
252
  }
253
 
254
  /**
255
  * Check if store is enabled.
256
+ *
257
+ * @param int
258
+ * @return bool
259
  */
260
  public function isEnabledStore($storeId)
261
  {
 
262
  $stores = @unserialize($this->_getConfig('enabled_stores'));
263
 
264
+ if (!is_array($stores)) {
265
+ return true;
266
+ }
267
 
 
268
  return in_array($storeId, $stores);
269
  }
270
 
271
  /**
272
  * Set stores. Pass null to disable store filtering.
273
+ *
274
+ * @param array|null
275
+ * @return Copernica_MarketingSoftware_Helper_Config
276
  */
277
  public function setEnabledStores($collectionOfStoresId)
278
  {
 
279
  $this->_setConfig('enabled_stores', serialize($collectionOfStoresId));
280
+
281
  return $this;
282
  }
283
 
284
  /**
285
  * Get list of enabled stores
286
+ *
287
+ * @return array|null
288
  */
289
  public function getEnabledStores()
290
  {
293
 
294
  /**
295
  * Get the hostname from the config
296
+ *
297
+ * @return string
298
  */
299
  public function getHostname()
300
  {
303
 
304
  /**
305
  * Set the hostname from the config
306
+ *
307
+ * @return Copernica_MarketingSoftware_Helper_Config
308
  */
309
  public function setHostname($value)
310
  {
311
  $this->_setConfig('hostname', $value);
312
+
313
  return $this;
314
  }
315
 
316
  /**
317
  * Get the name of the database
318
+ *
319
+ * @return string
320
  */
321
  public function getDatabaseName()
322
  {
325
 
326
  /**
327
  * Set the name of the database
328
+ *
329
+ * @param string $value
330
+ * @return Copernica_MarketingSoftware_Helper_Config
331
  */
332
  public function setDatabaseName($value)
333
  {
334
  $this->_setConfig('database', $value);
335
+
336
  return $this;
337
  }
338
 
339
  /**
340
  * Get the linked customer fields
341
+ *
342
+ * @return array
343
  */
344
  public function getLinkedCustomerFields()
345
  {
 
346
  $value = $this->_getConfig('linked_customer_fields');
 
 
347
  $value = empty($value) ? array() : json_decode($value, true);
348
 
349
+ if (!isset($value['customer_email'])) {
350
+ return $value;
351
+ }
352
 
 
353
  $oldValues = $value;
354
+
355
  $newValues = array();
356
 
357
+ foreach ($oldValues as $key => $value) {
 
 
358
  $key = str_replace('customer_', '', $key);
359
+
360
  $newValues[$key] = $value;
361
  }
362
 
 
363
  $this->setLinkedCustomerFields($newValues);
364
 
 
365
  return $newValues;
366
  }
367
 
368
  /**
369
  * Set the linked customer fields
370
+ *
371
+ * @param array $value
372
+ * @return Copernica_MarketingSoftware_Helper_Config
373
  */
374
  public function setLinkedCustomerFields($value)
375
  {
376
  $this->_setConfig('linked_customer_fields', json_encode($value), true);
377
+
378
  return $this;
379
  }
380
 
381
  /**
382
+ * Get the id of the quote item collection
383
+ *
384
+ * @return int
385
  */
386
+ public function getQuoteItemCollectionId()
387
  {
388
+ $id = $this->_getConfig('quote_item_collection_id');
389
+
390
+ if ($id) {
391
+ return $id;
392
+ }
393
+
394
+ $name = $this->getQuoteItemCollectionName();
395
+
396
+ if (!$name) {
397
+ return null;
398
+ }
399
+
400
+ $id = Mage::helper('marketingsoftware/api_abstract')->getCollectionId($name);
401
+
402
+ if (!$id) {
403
+ return null;
404
+ }
405
+
406
+ $this->_setConfig('quote_item_collection_id', $id);
407
+
408
+ return $id;
409
+ }
410
+
411
+ /**
412
+ * Get the name of the quote item collection
413
+ *
414
+ * @return string
415
+ */
416
+ public function getQuoteItemCollectionName()
417
+ {
418
+ return $this->_getConfig('quote_item_collection_name');
419
  }
420
 
421
  /**
422
+ * Set the name of the quote item collection
423
+ *
424
+ * @param string $value
425
+ * @return Copernica_MarketingSoftware_Helper_Config
426
  */
427
+ public function setQuoteItemCollectionName($value)
428
  {
429
+ $this->_setConfig('quote_item_collection_name', $value);
430
+
431
  return $this;
432
  }
433
 
434
  /**
435
+ * Get the linked quote item fields
436
+ *
437
+ * @return array
438
  */
439
+ public function getLinkedQuoteItemFields()
440
  {
441
+ $value = $this->_getConfig('linked_quote_item_fields');
442
 
443
+ if (empty($value)) {
444
+ return array();
445
+ } else {
446
+ return json_decode($value, true);
447
+ }
448
  }
449
 
450
  /**
451
+ * Get the linked quote item fields
452
+ *
453
+ * @param array $value
454
+ * @return Copernica_MarketingSoftware_Helper_Config
455
  */
456
+ public function setLinkedQuoteItemFields($value)
457
  {
458
+ $this->_setConfig('linked_quote_item_fields', json_encode($value), true);
459
+
460
  return $this;
461
  }
462
 
463
  /**
464
  * Get the name of the orders collection
465
+ *
466
+ * @return string
467
  */
468
  public function getOrdersCollectionName()
469
  {
470
  return $this->_getConfig('orders_collection_name');
471
  }
472
 
473
+ /**
474
+ * Get the id of the orders collection
475
+ *
476
+ * @return int
477
+ */
478
+ public function getOrdersCollectionId()
479
+ {
480
+ $id = $this->_getConfig('orders_collection_id');
481
+
482
+ if ($id) {
483
+ return $id;
484
+ }
485
+
486
+ $name = $this->getOrdersCollectionName();
487
+
488
+ if (!$name) {
489
+ return null;
490
+ }
491
+
492
+ $id = Mage::helper('marketingsoftware/api_abstract')->getCollectionId($name);
493
+
494
+ if (!$id) {
495
+ return null;
496
+ }
497
+
498
+ $this->_setConfig('orders_collection_id', $id);
499
+
500
+ return $id;
501
+ }
502
+
503
  /**
504
  * Set the name of the orders collection
505
+ *
506
+ * @param string $value
507
+ * @return Copernica_MarketingSoftware_Helper_Config
508
  */
509
  public function setOrdersCollectionName($value)
510
  {
511
  $this->_setConfig('orders_collection_name', $value);
512
+
513
  return $this;
514
  }
515
 
516
  /**
517
  * Get the linked order fields
518
+ *
519
+ * @return array
520
  */
521
  public function getLinkedOrderFields()
522
  {
 
523
  $value = $this->_getConfig('linked_order_fields');
 
 
524
  $value = empty($value) ? array() : json_decode($value, true);
525
 
526
+ if (!isset($value['order_timestamp'])) {
527
+ return $value;
528
+ }
529
 
 
530
  $oldValues = $value;
531
+
532
  $newValues = array();
533
 
534
+ foreach ($oldValues as $key => $value) {
 
 
 
535
  $key = ($key == 'order_qty') ? 'quantity' : str_replace('order_', '', $key);
536
+
537
  $newValues[$key] = $value;
538
  }
539
 
 
540
  $this->setLinkedOrderFields($newValues);
541
 
 
542
  return $newValues;
543
  }
544
 
545
  /**
546
  * Set the linked order fields
547
+ *
548
+ * @param array $value
549
+ * @return Copernica_MarketingSoftware_Helper_Config
550
  */
551
  public function setLinkedOrderFields($value)
552
  {
553
  $this->_setConfig('linked_order_fields', json_encode($value), true);
554
+
555
  return $this;
556
  }
557
 
558
  /**
559
  * Get the name of the collection were all the orders are stored
560
+ *
561
+ * @return string
562
+ */
563
+ public function getOrderItemCollectionName()
564
+ {
565
+ return $this->_getConfig('order_item_collection_name');
566
+ }
567
+
568
+ /**
569
+ * Get the id of the order item collection
570
+ *
571
+ * @return int
572
  */
573
+ public function getOrderItemCollectionId()
574
  {
575
+ $id = $this->_getConfig('order_item_collection_id');
576
+
577
+ if ($id) {
578
+ return $id;
579
+ }
580
+
581
+ $name = $this->getOrderItemCollectionName();
582
+
583
+ if (!$name) {
584
+ return null;
585
+ }
586
+
587
+ $id = Mage::helper('marketingsoftware/api_abstract')->getCollectionId($name);
588
+
589
+ if (!$id) {
590
+ return null;
591
+ }
592
+
593
+ $this->_setConfig('order_item_collection_id', $id);
594
+
595
+ return $id;
596
  }
597
 
598
  /**
599
  * Get the name of the collection were all the orders are stored
600
+ *
601
+ * @param string
602
  */
603
+ public function setOrderItemCollectionName($value)
604
  {
605
+ $this->_setConfig('order_item_collection_name', $value);
606
+
607
  return $this;
608
  }
609
 
610
  /**
611
  * Get the linked order item fields
612
+ *
613
+ * @return array
614
  */
615
  public function getLinkedOrderItemFields()
616
  {
617
  $value = $this->_getConfig('linked_order_item_fields');
 
 
618
  $value = empty($value) ? array() : json_decode($value, true);
619
 
620
+ if (!isset($value['product_internal_id'])) {
621
+ return $value;
622
+ }
623
 
 
624
  $oldValues = $value;
625
+
626
  $newValues = array();
627
 
628
+ foreach ($oldValues as $key => $value) {
629
+ switch($key) {
630
+ case 'product_qty':
631
+ $key = 'quantity';
632
+ break;
633
+
634
+ case 'product_base_row_total':
635
+ $key = 'total_price';
636
+ break;
637
+
638
+ default:
639
+ $key = str_replace('product_', '', $key);
640
+ break;
641
+ }
642
 
 
643
  $newValues[$key] = $value;
644
  }
645
 
 
646
  $this->setLinkedOrderItemFields($newValues);
647
 
 
648
  return $newValues;
649
  }
650
 
651
  /**
652
  * Set the linked order item fields
653
+ *
654
+ * @param array $value
655
+ * @return Copernica_MarketingSoftware_Helper_Config
656
  */
657
  public function setLinkedOrderItemFields($value)
658
  {
659
  $this->_setConfig('linked_order_item_fields', json_encode($value), true);
660
+
661
  return $this;
662
  }
663
 
664
  /**
665
  * Get the address collection name
666
+ *
667
+ * @return string
668
  */
669
  public function getAddressesCollectionName()
670
  {
671
  return $this->_getConfig('address_collection_name');
672
  }
673
 
674
+ /**
675
+ * Get the id of the address collection
676
+ * @return int
677
+ */
678
+ public function getAddressesCollectionId()
679
+ {
680
+ $id = $this->_getConfig('address_collection_id');
681
+
682
+ if ($id) {
683
+ return $id;
684
+ }
685
+
686
+ $name = $this->getAddressesCollectionName();
687
+
688
+ if (!$name) {
689
+ return null;
690
+ }
691
+
692
+ $id = Mage::helper('marketingsoftware/api_abstract')->getCollectionId($name);
693
+
694
+ if (!$id) {
695
+ return null;
696
+ }
697
+
698
+ $this->_setConfig('address_collection_id', $id);
699
+
700
+ return $id;
701
+ }
702
+
703
  /**
704
  * Set the name of the collection with addresses
705
  */
706
+ public function setAddressCollectionName($value)
707
  {
708
  $this->_setConfig('address_collection_name', $value);
709
+
710
  return $this;
711
  }
712
 
713
  /**
714
  * Get the linked address fields
715
+ *
716
+ * @return array
717
  */
718
  public function getLinkedAddressFields()
719
  {
720
  $value = $this->_getConfig('linked_address_fields');
 
 
721
  $value = empty($value) ? array() : json_decode($value, true);
722
+
723
+ if (!isset($value['address_firstname'])) {
724
+ return $value;
725
+ }
726
 
 
 
 
 
727
  $oldValues = $value;
728
+
729
  $newValues = array();
730
 
731
+ foreach ($oldValues as $key => $value) {
 
 
 
732
  $key = str_replace('address_', '', $key);
733
+
734
  $newValues[$key] = $value;
735
  }
736
 
 
737
  $this->setLinkedAddressFields($newValues);
738
 
 
739
  return $newValues;
740
  }
741
 
742
  /**
743
  * set the linked address fields
744
+ *
745
+ * @param array $value
746
+ * @return Copernica_MarketingSoftware_Helper_Config
747
  */
748
  public function setLinkedAddressFields($value)
749
  {
750
  $this->_setConfig('linked_address_fields', json_encode($value), true);
751
+
752
  return $this;
753
  }
754
 
755
  /**
756
  * Get the name of the viewed products collection
757
+ *
758
+ * @return string
759
  */
760
  public function getViewedProductCollectionName()
761
  {
762
  return $this->_getConfig('viewed_product_collection_name');
763
  }
764
 
765
+ /**
766
+ * Get the id of the address collection
767
+ *
768
+ * @return int
769
+ */
770
+ public function getViewedProductCollectionId()
771
+ {
772
+ $id = $this->_getConfig('viewed_product_collection_id');
773
+
774
+ if ($id) {
775
+ return $id;
776
+ }
777
+
778
+ $name = $this->getViewedProductCollectionName();
779
+
780
+ if (!$name) {
781
+ return null;
782
+ }
783
+
784
+ $id = Mage::helper('marketingsoftware/api_abstract')->getCollectionId($name);
785
+
786
+ if (!$id) {
787
+ return null;
788
+ }
789
+
790
+ $this->_setConfig('viewed_product_collection_id', $id);
791
+
792
+ return $id;
793
+ }
794
+
795
  /**
796
  * Set the name of the viewed products collection
797
+ *
798
+ * @param string $value
799
  * @return Copernica_MarketingSoftware_Helper_Config
800
  */
801
  public function setViewedProductCollectionName($value)
802
  {
803
  $this->_setConfig('viewed_product_collection_name', $value);
804
+
805
  return $this;
806
  }
807
 
808
  /**
809
  * Get the linked customer fields
810
+ *
811
+ * @return array
812
  */
813
  public function getLinkedViewedProductFields()
814
  {
815
  $value = $this->_getConfig('linked_viewed_product_fields');
816
 
817
+ if (empty($value)) {
818
+ return array();
819
+ } else {
820
+ return json_decode($value, true);
821
+ }
822
  }
823
 
824
  /**
825
  * Get the linked customer fields
826
+ *
827
+ * @param array $value
828
+ * @return Copernica_MarketingSoftware_Helper_Config
829
  */
830
  public function setLinkedViewedProductFields($value)
831
  {
832
  $this->_setConfig('linked_viewed_product_fields', json_encode($value), true);
833
+
834
  return $this;
835
  }
836
+
837
+ /**
838
+ * Get the name of the wishlist item collection
839
+ *
840
+ * @return string
841
+ */
842
+ public function getWishlistItemCollectionName()
843
+ {
844
+ return $this->_getConfig('wishlist_item_collection_name');
845
+ }
846
+
847
+ /**
848
+ * Get the id of the wishlist item collection
849
+ *
850
+ * @return int
851
+ */
852
+ public function getWishlistItemCollectionId()
853
+ {
854
+ $id = $this->_getConfig('wishlist_item_collection_id');
855
+
856
+ if ($id) {
857
+ return $id;
858
+ }
859
+
860
+ $name = $this->getWishlistItemCollectionName();
861
+
862
+ if (!$name) {
863
+ return null;
864
+ }
865
+
866
+ $id = Mage::helper('marketingsoftware/api_abstract')->getCollectionId($name);
867
+
868
+ if (!$id) {
869
+ return null;
870
+ }
871
+
872
+ $this->_setConfig('wishlist_item_collection_id', $id);
873
+
874
+ return $id;
875
+ }
876
+
877
+ /**
878
+ * Set the name of the wishlist item collection
879
+ *
880
+ * @param string $value
881
+ * @return Copernica_MarketingSoftware_Helper_Config
882
+ */
883
+ public function setWishlistItemCollectionName($value)
884
+ {
885
+ $this->_setConfig('wishlist_item_collection_name', $value);
886
+
887
+ return $this;
888
+ }
889
+
890
+ /**
891
+ * Get the linked wishlist item fields
892
+ *
893
+ * @return array
894
+ */
895
+ public function getLinkedWishlistItemFields()
896
+ {
897
+ $value = $this->_getConfig('linked_wishlist_item_fields');
898
+
899
+ if (empty($value)) {
900
+ return array();
901
+ } else {
902
+ return json_decode($value, true);
903
+ }
904
+ }
905
+
906
+ /**
907
+ * Set the linked wishlist item fields
908
+ *
909
+ * @param array $value
910
+ * @return Copernica_MarketingSoftware_Helper_Config
911
+ */
912
+ public function setLinkedWishlistItemFields($value)
913
+ {
914
+ $this->_setConfig('linked_wishlist_item_fields', json_encode($value), true);
915
+
916
+ return $this;
917
+ }
918
 
919
  /**
920
  * Get the progress status for customers
921
  * This is the created timestamp of the most recent customer which has
922
  * been queued for synchronisation
923
+ *
924
+ * @return datetime
925
  */
926
  public function getCustomerProgressStatus()
927
  {
932
  * Set the progress status for customers
933
  * This is the created timestamp of the most recent customer which has
934
  * been queued for synchronisation
935
+ *
936
+ * @param datetime $value
937
+ * @return Copernica_MarketingSoftware_Helper_Config
938
  */
939
  public function setCustomerProgressStatus($value)
940
  {
941
  $this->_setConfig('customer_progress_status', $value);
942
+
943
  return $this;
944
  }
945
 
947
  * Get the progress status for orders
948
  * This is the created timestamp of the most recent order which has
949
  * been queued for synchronisation
950
+ *
951
  * @return datetime
952
  */
953
  public function getOrderProgressStatus()
959
  * Set the progress status for orders
960
  * This is the created timestamp of the most recent order which has
961
  * been queued for synchronisation
962
+ *
963
+ * @param datetime $value
964
+ * @return Copernica_MarketingSoftware_Helper_Config
965
  */
966
  public function setOrderProgressStatus($value)
967
  {
968
  $this->_setConfig('order_progress_status', $value);
969
+
970
  return $this;
971
  }
972
 
974
  * Get the progress status for subscriptions
975
  * This is the created timestamp of the most recent subscription which has
976
  * been queued for synchronisation
977
+ *
978
  * @return datetime
979
  */
980
  public function getSubscriptionProgressStatus()
986
  * Set the progress status for subscriptions
987
  * This is the created timestamp of the most recent subscription which has
988
  * been queued for synchronisation
989
+ *
990
+ * @param datetime $value
991
+ * @return Copernica_MarketingSoftware_Helper_Config
992
  */
993
  public function setSubscriptionProgressStatus($value)
994
  {
995
  $this->_setConfig('subscription_progress_status', $value);
996
+
997
  return $this;
998
  }
999
 
1000
  /**
1001
  * Get the last start time of the cronjob.
1002
+ *
1003
+ * @return datetime
1004
  */
1005
  public function getLastStartTimeCronjob()
1006
  {
1009
 
1010
  /**
1011
  * Set the last start time of the cronjob.
1012
+ *
1013
+ * @param datetime $value
1014
+ * @return Copernica_MarketingSoftware_Helper_Config
1015
  */
1016
  public function setLastStartTimeCronjob($value)
1017
  {
1018
  $this->_setConfig('cronjob_starttime', $value);
1019
+
1020
  return $this;
1021
  }
1022
 
1023
  /**
1024
  * Get the last end time of the cronjob.
1025
+ *
1026
+ * @return datetime
1027
  */
1028
  public function getLastEndTimeCronjob()
1029
  {
1032
 
1033
  /**
1034
  * Set the last end time of the cronjob.
1035
+ *
1036
+ * @param datetime $value
1037
+ * @return Copernica_MarketingSoftware_Helper_Config
1038
  */
1039
  public function setLastEndTimeCronjob($value)
1040
  {
1041
  $this->_setConfig('cronjob_endtime', $value, true);
1042
+
1043
  return $this;
1044
  }
1045
 
1046
  /**
1047
  * Get the number of processed records of the last cronjob run.
1048
+ *
1049
  * @return integer
1050
  */
1051
  public function getLastCronjobProcessedTasks()
1055
 
1056
  /**
1057
  * Set the last end time of the cronjob.
1058
+ *
1059
+ * @param integer $value
1060
+ * @return Copernica_MarketingSoftware_Helper_Config
1061
  */
1062
  public function setLastCronjobProcessedTasks($value)
1063
  {
1064
  $this->_setConfig('cronjob_processedtasks', $value);
1065
+
1066
  return $this;
1067
  }
1068
 
1069
  /**
1070
  * Should our extension use vanilla magento cron schedulers to execute
1071
  * queue? Or should we sync all data by processQueue.php file?
1072
+ *
1073
+ * @param boolean $vanilla
1074
+ * @return Copernica_MarketingSoftware_Helper_Config
1075
  */
1076
  public function setVanillaCrons($vanilla)
1077
  {
1078
  $this->_setConfig('vanilla_crons', (bool) $vanilla ? 1 : 0);
1079
+
1080
  return $this;
1081
  }
1082
 
1083
  /**
1084
  * Get stored config about vanilla crons schedulers.
1085
+ *
1086
+ * @return boolean
1087
  */
1088
  public function getVanillaCrons()
1089
  {
1092
 
1093
  /**
1094
  * Get last timestamp that we used to check magento forgotten carts
1095
+ *
1096
  * @return string
1097
  */
1098
  public function getAbandonedLastCheck()
1104
 
1105
  /**
1106
  * Set timestamp when we did check forgotten carts list
1107
+ *
1108
+ * @param string $time
1109
+ * @return Copernica_MarketingSoftware_Helper_Config
1110
  */
1111
  public function setAbandonedLastCheck($time = null)
1112
  {
 
1113
  $this->_setConfig('lastAbandonedCheck', $time ? $time : date("Y-m-d H:i:s"));
1114
 
 
1115
  return $this;
1116
  }
1117
 
1118
  /**
1119
+ * Set number of minutes that have to pass from last quote item update to consider
1120
  * cart abandonded.
1121
+ *
1122
+ * @param int $timeout
1123
+ * @return Copernica_MarketingSoftware_Helper_Config
1124
  */
1125
  public function setAbandonedTimeout($timeout)
1126
  {
1130
  }
1131
 
1132
  /**
1133
+ * Get number of minutes that have to pass from last quote item update to consider
1134
+ * cart abandonded.
1135
+ *
1136
  * @return int
1137
  */
1138
  public function getAbandonedTimeout()
1139
  {
1140
  $timeout = $this->_getConfig('abandondedTimeout');
1141
 
 
1142
  return is_numeric($timeout) ? $timeout : 90;
1143
  }
1144
 
1145
  /**
1146
  * Carts older than supplied number of minutes will not be synchronized
1147
  * with Copernica.
1148
+ *
1149
+ * @param int $timeout
1150
+ * @return Copernica_MarketingSoftware_Helper_Config
1151
  */
1152
  public function setAbandonedPeriod($timeout)
1153
  {
1159
  /**
1160
  * Carts older than supplied number of minutes will not be synchronized
1161
  * with Copernica.
1162
+ *
1163
  * @return int
1164
  */
1165
  public function getAbandonedPeriod()
1166
  {
1167
  $timeout = $this->_getConfig('abandondedPeriod');
1168
 
1169
+ return is_numeric($timeout) ? $timeout : 21600;
 
1170
  }
1171
 
1172
  /**
1173
+ * Should finished (removed or ordered) quote item be removed from profile?
1174
+ *
1175
+ * @param boolean $remove
1176
+ * @return Copernica_MarketingSoftware_Helper_Config
1177
  */
1178
+ public function setRemoveFinishedQuoteItem($remove)
1179
  {
1180
  $this->_setConfig('removeFinished', $remove ? 1 : 0);
1181
 
1183
  }
1184
 
1185
  /**
1186
+ * Should finished (removed or ordered) quote item be removed from profile?
1187
+ *
1188
  * @return bool
1189
  */
1190
+ public function getRemoveFinishedQuoteItem()
1191
  {
1192
  return (bool)$this->_getConfig('removeFinished');
1193
  }
1197
  */
1198
  public function clearLinkedCollections()
1199
  {
1200
+ $this->unsQuoteItemCollectionName();
1201
+ $this->unsQuoteItemCollectionId();
1202
+ $this->unsLinkedQuoteItemFields();
 
1203
 
 
1204
  $this->unsOrdersCollectionName();
1205
  $this->unsOrdersCollectionId();
1206
  $this->unsLinkedOrderFields();
1207
 
1208
+ $this->unsOrderItemCollectionName();
1209
+ $this->unsOrderItemCollectionId();
 
1210
  $this->unsLinkedOrderItemFields();
1211
 
 
1212
  $this->unsAddressCollectionName();
1213
  $this->unsAddressCollectionId();
1214
  $this->unsLinkedAddressFields();
1215
 
 
1216
  $this->unsViewedProductCollectionName();
1217
  $this->unsViewedProductCollectionId();
1218
  $this->unsLinkedViewedProductFields();
1219
+
1220
+ $this->unsWishlistItemCollectionName();
1221
+ $this->unsWishlistItemCollectionId();
1222
+ $this->unsLinkedWishlistItemFields();
1223
  }
1224
  }
app/code/community/Copernica/MarketingSoftware/Helper/Data.php CHANGED
@@ -31,42 +31,44 @@ class Copernica_MarketingSoftware_Helper_Data extends Mage_Core_Helper_Abstract
31
  {
32
  /**
33
  * Helper method returns all supported customer fields
 
34
  * @return array
35
  */
36
  public function supportedCustomerFields()
37
  {
38
  return array(
39
- 'gender' => 'Gender',
40
- 'firstname' => 'Firstname',
41
- 'middlename' => 'Middlename',
42
- 'lastname' => 'Lastname',
43
- 'email' => 'E-mail',
44
- 'birthdate' => 'Birth date',
45
- 'group' => 'Customer group',
46
- 'newsletter' => 'Newsletter',
47
- 'storeView' => 'Store view',
48
- 'registrationDate' => 'Registration date',
49
  );
50
  }
51
 
52
  /**
53
  * Helper method returns all supported fields for
54
- * the cart item collection
 
55
  * @return array
56
  */
57
- public function supportedCartItemFields()
58
  {
59
  return array(
60
- 'productId' => 'product id',
61
  'name' => 'Product name',
62
  'sku' => 'SKU',
63
- 'attributeSet' => 'Attribute set',
64
  'weight' => 'Weight',
65
  'quantity' => 'Quantity',
66
  'price' => 'Price',
 
67
  'timestamp' => 'Modified',
68
- 'storeView' => 'Store view',
69
- 'totalPrice' => 'Total price',
70
  'url' => 'Details URL',
71
  'image' => 'Image URL',
72
  'categories' => 'Categories',
@@ -78,109 +80,141 @@ class Copernica_MarketingSoftware_Helper_Data extends Mage_Core_Helper_Abstract
78
  /**
79
  * Helper method returns all supported fields for
80
  * the order collection
81
- * @return array
 
82
  */
83
  public function supportedOrderFields()
84
  {
85
  return array(
86
- 'incrementId' => 'Increment id',
87
- 'timestamp' => 'Timestamp',
88
- 'quantity' => 'Quantity',
89
  //'subtotal' => 'Subtotal',
90
- 'shipping' => 'Shipping costs',
91
- 'total' => 'Order total',
92
- 'weight' => 'Total weight',
93
- 'currency' => 'Currency',
94
- 'status' => 'Order status',
95
- 'storeView' => 'Store view',
96
- 'remoteIp' => 'Order ip address',
97
- 'shippingDescription' => 'Shipping description',
98
- 'paymentDescription' => 'Payment description',
99
- 'shippingAddressId' => 'Shipping Address id',
100
- 'billingAddressId' => 'Billing Address id',
101
- 'couponCode' => 'Coupon code',
102
  );
103
  }
104
 
105
  /**
106
  * Helper method returns all supported fields for
107
- * the cart item collection
 
108
  * @return array
109
  */
110
  public function supportedOrderItemFields()
111
  {
112
  return array(
113
- 'productId' => 'product id',
114
- 'incrementId' => 'Increment id',
115
- 'name' => 'Product name',
116
- 'sku' => 'SKU',
117
- 'attributeSet' => 'Attribute set',
118
- 'weight' => 'Weight',
119
- 'quantity' => 'Quantity',
120
- 'price' => 'Price',
121
- 'timestamp' => 'Modified',
122
- 'storeView' => 'Store view',
123
- 'totalPrice' => 'Total price',
124
- 'url' => 'Details URL',
125
- 'image' => 'Image URL',
126
- 'categories' => 'Categories',
127
- 'options' => 'Product options',
128
- 'attributes' => 'Product Attributes',
129
- 'salesRules' => 'Sales rules',
130
  );
131
  }
132
 
133
  /**
134
  * Helper method returns all supported fields for
135
  * the address collection
 
136
  * @return array
137
  */
138
  public function supportedAddressFields()
139
  {
140
  return array(
141
- 'firstname' => 'Firstname',
142
- 'middlename' => 'Middlename',
143
- 'prefix' => 'Prefix',
144
- 'lastname' => 'Lastname',
145
- 'email' => 'E-mail',
146
- 'company' => 'Company',
147
- 'street' => 'Street',
148
- 'city' => 'City',
149
- 'state' => 'State',
150
- 'zipcode' => 'Zip code',
151
- 'countryId' => 'Country',
152
- 'telephone' => 'Telephone number',
153
- 'fax' => 'Fax number',
154
  );
155
  }
156
 
157
  /**
158
  * Helper method returns all supported fields for
159
  * the viewed product collection
 
160
  * @return array
161
  */
162
  public function supportedViewedProductFields()
163
  {
164
  return array(
165
- 'productId' => 'product id',
166
  'name' => 'Product name',
167
  'sku' => 'SKU',
168
- 'attributeSet' => 'Attribute set',
169
  'weight' => 'Weight',
170
  'price' => 'Price',
171
- 'storeView' => 'Store view',
172
- 'totalPrice' => 'Total price',
173
  'url' => 'Details URL',
174
  'image' => 'Image URL',
175
  'categories' => 'Categories',
176
  'options' => 'Product options',
177
- 'attributes' => 'Product Attributes',
178
- 'timestamp' => 'Timestamp',
179
  );
180
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
181
 
182
  /**
183
  * Required fields for copernica customer profile.
 
184
  * @return array
185
  */
186
  public function requiredCustomerFields()
@@ -189,16 +223,18 @@ class Copernica_MarketingSoftware_Helper_Data extends Mage_Core_Helper_Abstract
189
  }
190
 
191
  /**
192
- * Required fields for copernica cart items collection.
 
193
  * @return array
194
  */
195
- public function requiredCartItemFields()
196
  {
197
  return array('item_id', 'quote_id', 'status');
198
  }
199
 
200
  /**
201
  * Required fields for copernica orders collection
 
202
  * @return array
203
  */
204
  public function requiredOrderFields()
@@ -207,7 +243,8 @@ class Copernica_MarketingSoftware_Helper_Data extends Mage_Core_Helper_Abstract
207
  }
208
 
209
  /**
210
- * Required fields for copernica order items collection
 
211
  * @return array
212
  */
213
  public function requiredOrderItemFields()
@@ -217,6 +254,7 @@ class Copernica_MarketingSoftware_Helper_Data extends Mage_Core_Helper_Abstract
217
 
218
  /**
219
  * Required fields for copernica address collection
 
220
  * @return array
221
  */
222
  public function requiredAddressFields()
@@ -226,6 +264,7 @@ class Copernica_MarketingSoftware_Helper_Data extends Mage_Core_Helper_Abstract
226
 
227
  /**
228
  * Required fields for copernica viewed products collection
 
229
  * @return array
230
  */
231
  public function requiredViewedProductFields()
@@ -233,6 +272,16 @@ class Copernica_MarketingSoftware_Helper_Data extends Mage_Core_Helper_Abstract
233
  return array('product_id');
234
  }
235
 
 
 
 
 
 
 
 
 
 
 
236
  /**
237
  * Get field definition by collection type and magento field name.
238
  *
@@ -242,13 +291,12 @@ class Copernica_MarketingSoftware_Helper_Data extends Mage_Core_Helper_Abstract
242
  * platform we should also have a field of type email. Same goes for phones,
243
  * dates, etc.
244
  *
245
- * @param string collection type
246
- * @param string magento field name
247
  * @return array
248
  */
249
  public function getCollectionFieldDefinition($collectionType, $magentoName)
250
  {
251
- // table with field definitions.
252
  $definitions = array(
253
  'cartproducts' => array (
254
  'timestamp' => array (
@@ -270,8 +318,18 @@ class Copernica_MarketingSoftware_Helper_Data extends Mage_Core_Helper_Abstract
270
  ),
271
  'storeView' => array (
272
  'type' => 'text',
273
- 'length' => 100
274
- )
 
 
 
 
 
 
 
 
 
 
275
  ),
276
  'orderproducts' => array (
277
  'timestamp' => array (
@@ -293,21 +351,22 @@ class Copernica_MarketingSoftware_Helper_Data extends Mage_Core_Helper_Abstract
293
  ),
294
  'storeView' => array (
295
  'type' => 'text',
296
- 'length' => 100
297
  ),
298
- // small note on following 2 fields. They can be very big. For
299
- // options, when we have a bundled product it will contain
300
- // title, product name, product price and quantity of product.
301
- // For attributes it will contain a key-value string of every
302
- // attribute. That is why we want to set the type to big.
303
  'options' => array (
304
- 'type' => 'big'
 
 
305
  ),
306
  'attributes' => array (
307
- 'type' => 'big'
 
 
308
  ),
309
  'salesRules' => array (
310
- 'type' => 'big'
 
 
311
  ),
312
  ),
313
  'viewedproducts' => array (
@@ -330,18 +389,17 @@ class Copernica_MarketingSoftware_Helper_Data extends Mage_Core_Helper_Abstract
330
  ),
331
  'storeView' => array (
332
  'type' => 'text',
333
- 'length' => 100
334
  ),
335
- // small note on following 2 fields. They can be very big. For
336
- // options, when we have a bundled product it will contain
337
- // title, product name, product price and quantity of product.
338
- // For attributes it will contain a key-value string of every
339
- // attribute. That is why we want to set the type to big.
340
  'options' => array (
341
- 'type' => 'big'
 
 
342
  ),
343
  'attributes' => array (
344
- 'type' => 'big'
 
 
345
  )
346
  ),
347
  'orders' => array (
@@ -350,13 +408,17 @@ class Copernica_MarketingSoftware_Helper_Data extends Mage_Core_Helper_Abstract
350
  ),
351
  'storeView' => array (
352
  'type' => 'text',
353
- 'length' => 100
354
  ),
355
  'shippingDescription' => array (
356
- 'type' => 'big'
 
 
357
  ),
358
  'paymentDescription' => array (
359
- 'type' => 'big'
 
 
360
  ),
361
  'couponCode' => array (
362
  'type' => 'text',
@@ -373,35 +435,70 @@ class Copernica_MarketingSoftware_Helper_Data extends Mage_Core_Helper_Abstract
373
  'fax' => array (
374
  'type' => 'phone_fax'
375
  )
376
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
377
  );
378
 
379
- // check if we have a special definition for field
380
- if (!isset($definitions[$collectionType]) || !isset($definitions[$collectionType][$magentoName]))
381
- {
382
- // by default we will say that field should be a text field
383
  return array ('type' => 'text');
384
  }
385
 
386
- // return field definition
387
  return $definitions[$collectionType][$magentoName];
388
  }
389
 
390
  /**
391
  * Get the version of this extension.
392
  *
393
- * @return string version number
394
  */
395
  public function getExtensionVersion()
396
  {
397
- // Get the config and return the version from the config
398
  $config = Mage::getConfig()->getModuleConfig('Copernica_MarketingSoftware')->asArray();
 
399
  return $config['version'];
400
  }
401
 
402
  /**
403
  * Get the url for the unsubscribe callback
404
- * @return String
 
405
  */
406
  public function unsubscribeCallbackUrl()
407
  {
@@ -411,26 +508,25 @@ class Copernica_MarketingSoftware_Helper_Data extends Mage_Core_Helper_Abstract
411
  /**
412
  * Does the queue contain the magic token, which indicates that the synchronisation
413
  * should be started?
 
414
  * @return boolean
415
  */
416
  public function isSynchronisationStartScheduled()
417
  {
418
- // Construct a new resource for this because caching fucks it all up
419
- $count = Mage::getResourceModel('marketingsoftware/queue_collection')
420
  ->addFieldToFilter('action', 'start_sync')
421
  ->getSize();
422
 
423
- // Reset the count
424
  return ($count > 0);
425
  }
426
 
427
  /**
428
  * Is the Copernica module enabled?
 
429
  * @return boolean
430
  */
431
  public function enabled()
432
  {
433
- // Get the setting from 'advanced/modules_disable_output/Copernica_MarketingSoftware'
434
  return (Mage::getConfig()->getNode('advanced/modules_disable_output/Copernica_MarketingSoftware', 'default', 0) == 0);
435
  }
436
  }
31
  {
32
  /**
33
  * Helper method returns all supported customer fields
34
+ *
35
  * @return array
36
  */
37
  public function supportedCustomerFields()
38
  {
39
  return array(
40
+ 'gender' => 'Gender',
41
+ 'firstname' => 'Firstname',
42
+ 'middlename' => 'Middlename',
43
+ 'lastname' => 'Lastname',
44
+ 'email' => 'E-mail',
45
+ 'birthdate' => 'Birth date',
46
+ 'group' => 'Customer group',
47
+ 'newsletter' => 'Newsletter',
48
+ 'storeView' => 'Store view',
49
+ 'registrationDate' => 'Registration date',
50
  );
51
  }
52
 
53
  /**
54
  * Helper method returns all supported fields for
55
+ * the quote item collection
56
+ *
57
  * @return array
58
  */
59
+ public function supportedQuoteItemFields()
60
  {
61
  return array(
62
+ 'productId' => 'Product id',
63
  'name' => 'Product name',
64
  'sku' => 'SKU',
65
+ 'attributeSet' => 'Attribute set',
66
  'weight' => 'Weight',
67
  'quantity' => 'Quantity',
68
  'price' => 'Price',
69
+ 'totalPrice' => 'Total price',
70
  'timestamp' => 'Modified',
71
+ 'storeView' => 'Store view',
 
72
  'url' => 'Details URL',
73
  'image' => 'Image URL',
74
  'categories' => 'Categories',
80
  /**
81
  * Helper method returns all supported fields for
82
  * the order collection
83
+ *
84
+ * @return array
85
  */
86
  public function supportedOrderFields()
87
  {
88
  return array(
89
+ 'incrementId' => 'Increment id',
90
+ 'timestamp' => 'Timestamp',
91
+ 'quantity' => 'Quantity',
92
  //'subtotal' => 'Subtotal',
93
+ 'shipping' => 'Shipping costs',
94
+ 'total' => 'Order total',
95
+ 'weight' => 'Total weight',
96
+ 'currency' => 'Currency',
97
+ 'status' => 'Order status',
98
+ 'storeView' => 'Store view',
99
+ 'remoteIp' => 'Order ip address',
100
+ 'shippingDescription' => 'Shipping description',
101
+ 'paymentDescription' => 'Payment description',
102
+ 'shippingAddressId' => 'Shipping Address id',
103
+ 'billingAddressId' => 'Billing Address id',
104
+ 'couponCode' => 'Coupon code',
105
  );
106
  }
107
 
108
  /**
109
  * Helper method returns all supported fields for
110
+ * the order item collection
111
+ *
112
  * @return array
113
  */
114
  public function supportedOrderItemFields()
115
  {
116
  return array(
117
+ 'productId' => 'Product id',
118
+ 'incrementId' => 'Increment id',
119
+ 'name' => 'Product name',
120
+ 'sku' => 'SKU',
121
+ 'attributeSet' => 'Attribute set',
122
+ 'weight' => 'Weight',
123
+ 'quantity' => 'Quantity',
124
+ 'price' => 'Price',
125
+ 'totalPrice' => 'Total price',
126
+ 'timestamp' => 'Modified',
127
+ 'storeView' => 'Store view',
128
+ 'url' => 'Details URL',
129
+ 'image' => 'Image URL',
130
+ 'categories' => 'Categories',
131
+ 'options' => 'Product options',
132
+ 'attributes' => 'Product Attributes',
133
+ 'salesRules' => 'Sales rules',
134
  );
135
  }
136
 
137
  /**
138
  * Helper method returns all supported fields for
139
  * the address collection
140
+ *
141
  * @return array
142
  */
143
  public function supportedAddressFields()
144
  {
145
  return array(
146
+ 'firstname' => 'Firstname',
147
+ 'middlename' => 'Middlename',
148
+ 'prefix' => 'Prefix',
149
+ 'lastname' => 'Lastname',
150
+ 'email' => 'E-mail',
151
+ 'company' => 'Company',
152
+ 'street' => 'Street',
153
+ 'city' => 'City',
154
+ 'state' => 'State',
155
+ 'zipcode' => 'Zip code',
156
+ 'countryId' => 'Country',
157
+ 'telephone' => 'Telephone number',
158
+ 'fax' => 'Fax number',
159
  );
160
  }
161
 
162
  /**
163
  * Helper method returns all supported fields for
164
  * the viewed product collection
165
+ *
166
  * @return array
167
  */
168
  public function supportedViewedProductFields()
169
  {
170
  return array(
171
+ 'productId' => 'Product id',
172
  'name' => 'Product name',
173
  'sku' => 'SKU',
174
+ 'attributeSet' => 'Attribute set',
175
  'weight' => 'Weight',
176
  'price' => 'Price',
177
+ 'timestamp' => 'Timestamp',
178
+ 'storeView' => 'Store view',
179
  'url' => 'Details URL',
180
  'image' => 'Image URL',
181
  'categories' => 'Categories',
182
  'options' => 'Product options',
183
+ 'attributes' => 'Product Attributes',
 
184
  );
185
  }
186
+
187
+ /**
188
+ * Helper method returns all supported fields for
189
+ * the wishlist item collection
190
+ *
191
+ * @return array
192
+ */
193
+ public function supportedWishlistItemFields()
194
+ {
195
+ return array(
196
+ 'productId' => 'Product id',
197
+ 'name' => 'Product name',
198
+ 'sku' => 'SKU',
199
+ 'attributeSet' => 'Attribute set',
200
+ 'weight' => 'Weight',
201
+ 'quantity' => 'Quantity',
202
+ 'price' => 'Price',
203
+ 'totalPrice' => 'Total price',
204
+ 'timestamp' => 'Timestamp',
205
+ 'description' => 'Description',
206
+ 'storeView' => 'Store view',
207
+ 'url' => 'Details URL',
208
+ 'image' => 'Image URL',
209
+ 'categories' => 'Categories',
210
+ 'options' => 'Product options',
211
+ 'attributes' => 'Product Attributes',
212
+ );
213
+ }
214
 
215
  /**
216
  * Required fields for copernica customer profile.
217
+ *
218
  * @return array
219
  */
220
  public function requiredCustomerFields()
223
  }
224
 
225
  /**
226
+ * Required fields for copernica quote item collection.
227
+ *
228
  * @return array
229
  */
230
+ public function requiredQuoteItemFields()
231
  {
232
  return array('item_id', 'quote_id', 'status');
233
  }
234
 
235
  /**
236
  * Required fields for copernica orders collection
237
+ *
238
  * @return array
239
  */
240
  public function requiredOrderFields()
243
  }
244
 
245
  /**
246
+ * Required fields for copernica order item collection
247
+ *
248
  * @return array
249
  */
250
  public function requiredOrderItemFields()
254
 
255
  /**
256
  * Required fields for copernica address collection
257
+ *
258
  * @return array
259
  */
260
  public function requiredAddressFields()
264
 
265
  /**
266
  * Required fields for copernica viewed products collection
267
+ *
268
  * @return array
269
  */
270
  public function requiredViewedProductFields()
272
  return array('product_id');
273
  }
274
 
275
+ /**
276
+ * Required fields for copernica wislist collection
277
+ *
278
+ * @return array
279
+ */
280
+ public function requiredWishlistItemFields()
281
+ {
282
+ return array('item_id', 'wishlist_id');
283
+ }
284
+
285
  /**
286
  * Get field definition by collection type and magento field name.
287
  *
291
  * platform we should also have a field of type email. Same goes for phones,
292
  * dates, etc.
293
  *
294
+ * @param string $collectionType
295
+ * @param string $magentoName
296
  * @return array
297
  */
298
  public function getCollectionFieldDefinition($collectionType, $magentoName)
299
  {
 
300
  $definitions = array(
301
  'cartproducts' => array (
302
  'timestamp' => array (
318
  ),
319
  'storeView' => array (
320
  'type' => 'text',
321
+ 'length' => 250
322
+ ),
323
+ 'options' => array (
324
+ 'type' => 'big',
325
+ 'textlines' => 4,
326
+ 'lines' => 4
327
+ ),
328
+ 'attributes' => array (
329
+ 'type' => 'big',
330
+ 'textlines' => 4,
331
+ 'lines' => 4
332
+ ),
333
  ),
334
  'orderproducts' => array (
335
  'timestamp' => array (
351
  ),
352
  'storeView' => array (
353
  'type' => 'text',
354
+ 'length' => 250
355
  ),
 
 
 
 
 
356
  'options' => array (
357
+ 'type' => 'big',
358
+ 'textlines' => 4,
359
+ 'lines' => 4
360
  ),
361
  'attributes' => array (
362
+ 'type' => 'big',
363
+ 'textlines' => 4,
364
+ 'lines' => 4
365
  ),
366
  'salesRules' => array (
367
+ 'type' => 'big',
368
+ 'textlines' => 4,
369
+ 'lines' => 4
370
  ),
371
  ),
372
  'viewedproducts' => array (
389
  ),
390
  'storeView' => array (
391
  'type' => 'text',
392
+ 'length' => 250
393
  ),
 
 
 
 
 
394
  'options' => array (
395
+ 'type' => 'big',
396
+ 'textlines' => 4,
397
+ 'lines' => 4
398
  ),
399
  'attributes' => array (
400
+ 'type' => 'big',
401
+ 'textlines' => 4,
402
+ 'lines' => 4
403
  )
404
  ),
405
  'orders' => array (
408
  ),
409
  'storeView' => array (
410
  'type' => 'text',
411
+ 'length' => 250
412
  ),
413
  'shippingDescription' => array (
414
+ 'type' => 'big',
415
+ 'textlines' => 4,
416
+ 'lines' => 4
417
  ),
418
  'paymentDescription' => array (
419
+ 'type' => 'big',
420
+ 'textlines' => 4,
421
+ 'lines' => 4
422
  ),
423
  'couponCode' => array (
424
  'type' => 'text',
435
  'fax' => array (
436
  'type' => 'phone_fax'
437
  )
438
+ ),
439
+ 'wishlistproducts' => array (
440
+ 'timestamp' => array (
441
+ 'type' => 'datetime'
442
+ ),
443
+ 'description' => array (
444
+ 'type' => 'big',
445
+ 'textlines' => 4,
446
+ 'lines' => 4
447
+ ),
448
+ 'url' => array (
449
+ 'type' => 'text',
450
+ 'length' => 255
451
+ ),
452
+ 'image' => array (
453
+ 'type' => 'text',
454
+ 'length' => 255
455
+ ),
456
+ 'categories' => array (
457
+ 'type' => 'text',
458
+ 'length' => 255,
459
+ 'textlines' => 4,
460
+ 'lines' => 4
461
+ ),
462
+ 'storeView' => array (
463
+ 'type' => 'text',
464
+ 'length' => 250
465
+ ),
466
+ 'options' => array (
467
+ 'type' => 'big',
468
+ 'textlines' => 4,
469
+ 'lines' => 4
470
+ ),
471
+ 'attributes' => array (
472
+ 'type' => 'big',
473
+ 'textlines' => 4,
474
+ 'lines' => 4
475
+ ),
476
+ )
477
  );
478
 
479
+ if (!isset($definitions[$collectionType]) || !isset($definitions[$collectionType][$magentoName])) {
 
 
 
480
  return array ('type' => 'text');
481
  }
482
 
 
483
  return $definitions[$collectionType][$magentoName];
484
  }
485
 
486
  /**
487
  * Get the version of this extension.
488
  *
489
+ * @return string
490
  */
491
  public function getExtensionVersion()
492
  {
 
493
  $config = Mage::getConfig()->getModuleConfig('Copernica_MarketingSoftware')->asArray();
494
+
495
  return $config['version'];
496
  }
497
 
498
  /**
499
  * Get the url for the unsubscribe callback
500
+ *
501
+ * @return string
502
  */
503
  public function unsubscribeCallbackUrl()
504
  {
508
  /**
509
  * Does the queue contain the magic token, which indicates that the synchronisation
510
  * should be started?
511
+ *
512
  * @return boolean
513
  */
514
  public function isSynchronisationStartScheduled()
515
  {
516
+ $count = Mage::getResourceModel('marketingsoftware/queue_item_collection')
 
517
  ->addFieldToFilter('action', 'start_sync')
518
  ->getSize();
519
 
 
520
  return ($count > 0);
521
  }
522
 
523
  /**
524
  * Is the Copernica module enabled?
525
+ *
526
  * @return boolean
527
  */
528
  public function enabled()
529
  {
 
530
  return (Mage::getConfig()->getNode('advanced/modules_disable_output/Copernica_MarketingSoftware', 'default', 0) == 0);
531
  }
532
  }
app/code/community/Copernica/MarketingSoftware/Helper/DataWriter.php CHANGED
@@ -28,80 +28,74 @@
28
  * This helper will help us with writing data into set of files that can be
29
  * processed by Copernica employes.
30
  */
31
- class Copernica_MarketingSoftware_Helper_DataWriter
32
  {
33
  /**
34
- * Handle to a file that we can use to write REST requests
 
35
  * @var resource
36
  */
37
- private $fileHandle = null;
38
 
39
  /**
40
  * Current file that will be used to write data to files.
 
41
  * @var string
42
  */
43
- private $fileName = 'profiles.1.data';
44
 
45
  /**
46
  * Path to working direcotry. Working directory will contain file with data.
 
47
  * @var string
48
  */
49
- private $workingDir = '';
50
 
51
  /**
52
  * Constructor.
53
  */
54
  public function __construct()
55
  {
56
- // ensure that working directoru is valid. It will prepare working
57
- // directory if there is none.
58
- $this->ensureWorkingDir();
59
-
60
- // we want to have a quick peak on what files are already in working dir,
61
- // and check from what file we should start
62
- $this->detectCurrentFile();
63
-
64
- // validate current data file and set proper current data file.
65
- $this->validateDataFile();
66
-
67
- // open handler to current data file
68
- $this->fileHandle = fopen($this->getFilePath(), 'a+');
69
  }
70
 
71
  /**
72
- * Destructur.
73
  */
74
  public function __destruct()
75
  {
76
- if (!is_null($this->fileHandle)) fclose($this->fileHandle);
 
 
77
  }
78
 
79
  /**
80
  * We want to detect current working file.
81
  */
82
- private function detectCurrentFile()
83
  {
84
- // open working directory
85
- $directoryHandle = opendir($this->workingDir);
86
 
87
- // data numbers
88
  $dataNumbers = array();
89
 
90
- while (false !== ($entry = readdir($directoryHandle)))
91
- {
92
- // if file starts with dot we want to skip it
93
- if (strpos($entry, '.') === 0) continue;
94
 
95
  $fileNameParts = explode('.', $entry);
96
 
97
  $dataNumbers[] = $fileNameParts[1];
98
  }
99
 
100
- // check if we have some infor about number of data file
101
- if (count($dataNumbers) > 0)
102
- {
103
- // set filename
104
- $this->fileName = 'profiles.'.max($dataNumbers).'.data';
105
  }
106
  }
107
 
@@ -109,63 +103,50 @@ class Copernica_MarketingSoftware_Helper_DataWriter
109
  * Ensure that working directory is valid. This method will create working
110
  * directory if there is none.
111
  */
112
- private function ensureWorkingDir()
113
- {
114
- // get magento var dir. We will use it to store REST requests
115
  $dir = Mage::getBaseDir('var');
116
 
117
- // check if we have a working dir
118
  if (!is_dir($dir.'/copernica_data')) {
119
- // create one if needed
120
  mkdir ($dir.'/copernica_data');
121
  }
122
 
123
- // store working directory
124
- $this->workingDir = $dir.'/copernica_data';
125
  }
126
 
127
  /**
128
  * This method will check if we can write to current data file and will
129
  * rotate files when they are too large.
130
  */
131
- private function validateDataFile()
132
  {
133
  // 100mb in bytes. That would be our max size for one data file
134
  $mbInBytes = 104857600;
135
 
136
- // for debug we want to set lower amount of space as upper limit
137
- // $mbInBytes = 1024;
138
-
139
- // if we don't have a file then we want to create one
140
- if (!is_file($this->getFilePath())) $this->createDataFile($this->getFilePath());
141
 
142
- // check if current file is above 100mb
143
- if (filesize($this->getFilePath()) > $mbInBytes)
144
- {
145
- // get incremented file path
146
- $newFilePath = $this->getFilePath(+1);
147
 
148
- // create new data file
149
- $this->createDataFile($newFilePath);
150
 
151
- // store new file path
152
- $this->fileName = end(explode('/', $newFilePath));
153
  }
154
  }
155
 
156
  /**
157
  * Create new data file at given path.
158
- * @param string
 
159
  */
160
- private function createDataFile($path)
161
  {
162
  if (is_file($path)) {
163
  // we have a file. Most likely we should handle such situation.
164
  // we will see what we can do with it
165
- }
166
- else
167
- {
168
- // this will create empty data file
169
  touch($path);
170
  }
171
  }
@@ -173,48 +154,38 @@ class Copernica_MarketingSoftware_Helper_DataWriter
173
  /**
174
  * Get file path to data file. Supplying null value as a parameter will
175
  * output current data file.
176
- * @param int Increment number of data file from current file.
177
- * @return string
 
178
  */
179
- private function getFilePath($inc = null)
180
  {
181
- // get the current file name into local scope
182
- $fileName = $this->fileName;
183
-
184
- // we should increment file and return incremeneted file
185
- if (!is_null($inc))
186
- {
187
- // explode file name with dots
188
- $fileNameParts = explode('.', $this->fileName);
189
 
190
- // second part is the number of the data file. Increment it.
 
191
  $fileNameParts[1] += $inc;
192
 
193
- // implode with dots file name parts. That will give us new file name
194
  $fileName = implode('.', $fileNameParts);
 
 
195
  }
196
-
197
- // get the current file name
198
- else $fileName = $this->fileName;
199
-
200
- // return file path
201
- return $this->workingDir.'/'.$fileName;
202
  }
203
 
204
  /**
205
  * Write
206
- * @param assoc Array with data that we want to write
 
207
  */
208
- private function write($data)
209
  {
210
- // craete line of data
211
  $dataLine = json_encode($data);
212
 
213
- // write data to file
214
- fwrite($this->fileHandle, $dataLine.PHP_EOL);
215
 
216
- // validate data file
217
- $this->validateDataFile();
218
  }
219
 
220
  /**
@@ -222,34 +193,28 @@ class Copernica_MarketingSoftware_Helper_DataWriter
222
  */
223
  public function clearDataFiles()
224
  {
225
- // open working dir
226
- $directoryHandle = opendir($this->workingDir);
227
-
228
- // loop over every file in working dir and remove data files
229
- while (false !== ($entry = readdir($directoryHandle)))
230
- {
231
- // if entry starts with a dot then that means it's a special file
232
- // that we should not touch.
233
- if (strpos($entry, '.') === 0) continue;
234
-
235
- // remove data file
236
- unlink($this->workingDir.'/'.$entry);
237
  }
238
 
239
- // close directory handle.
240
  closedir($directoryHandle);
241
 
242
- // we want to revalidate data file
243
- $this->validateDataFile();
244
  }
245
 
246
  /**
247
  * Store customer profile.
248
- * @param assoc
 
249
  */
250
  public function storeProfile($profile)
251
  {
252
- // write profile data
253
- $this->write($profile);
254
  }
255
  }
28
  * This helper will help us with writing data into set of files that can be
29
  * processed by Copernica employes.
30
  */
31
+ class Copernica_MarketingSoftware_Helper_Datawriter
32
  {
33
  /**
34
+ * Handle to a file that we can use to write REST requests
35
+ *
36
  * @var resource
37
  */
38
+ protected $_fileHandle = null;
39
 
40
  /**
41
  * Current file that will be used to write data to files.
42
+ *
43
  * @var string
44
  */
45
+ protected $_fileName = 'profiles.1.data';
46
 
47
  /**
48
  * Path to working direcotry. Working directory will contain file with data.
49
+ *
50
  * @var string
51
  */
52
+ protected $_workingDir = '';
53
 
54
  /**
55
  * Constructor.
56
  */
57
  public function __construct()
58
  {
59
+ $this->_ensureWorkingDir();
60
+
61
+ $this->_detectCurrentFile();
62
+
63
+ $this->_validateDataFile();
64
+
65
+ $this->_fileHandle = fopen($this->_getFilePath(), 'a+');
 
 
 
 
 
 
66
  }
67
 
68
  /**
69
+ * Destructor.
70
  */
71
  public function __destruct()
72
  {
73
+ if (!is_null($this->_fileHandle)) {
74
+ fclose($this->_fileHandle);
75
+ }
76
  }
77
 
78
  /**
79
  * We want to detect current working file.
80
  */
81
+ protected function _detectCurrentFile()
82
  {
83
+ $directoryHandle = opendir($this->_workingDir);
 
84
 
 
85
  $dataNumbers = array();
86
 
87
+ while (false !== ($entry = readdir($directoryHandle))) {
88
+ if (strpos($entry, '.') === 0) {
89
+ continue;
90
+ }
91
 
92
  $fileNameParts = explode('.', $entry);
93
 
94
  $dataNumbers[] = $fileNameParts[1];
95
  }
96
 
97
+ if (count($dataNumbers) > 0) {
98
+ $this->_fileName = 'profiles.'.max($dataNumbers).'.data';
 
 
 
99
  }
100
  }
101
 
103
  * Ensure that working directory is valid. This method will create working
104
  * directory if there is none.
105
  */
106
+ protected function _ensureWorkingDir()
107
+ {
 
108
  $dir = Mage::getBaseDir('var');
109
 
 
110
  if (!is_dir($dir.'/copernica_data')) {
 
111
  mkdir ($dir.'/copernica_data');
112
  }
113
 
114
+ $this->_workingDir = $dir.'/copernica_data';
 
115
  }
116
 
117
  /**
118
  * This method will check if we can write to current data file and will
119
  * rotate files when they are too large.
120
  */
121
+ protected function _validateDataFile()
122
  {
123
  // 100mb in bytes. That would be our max size for one data file
124
  $mbInBytes = 104857600;
125
 
126
+ if (!is_file($this->_getFilePath())) {
127
+ $this->_createDataFile($this->_getFilePath());
128
+ }
 
 
129
 
130
+ if (filesize($this->_getFilePath()) > $mbInBytes) {
131
+ $newFilePath = $this->_getFilePath(+1);
 
 
 
132
 
133
+ $this->_createDataFile($newFilePath);
 
134
 
135
+ $this->_fileName = end(explode('/', $newFilePath));
 
136
  }
137
  }
138
 
139
  /**
140
  * Create new data file at given path.
141
+ *
142
+ * @param string $path
143
  */
144
+ protected function _createDataFile($path)
145
  {
146
  if (is_file($path)) {
147
  // we have a file. Most likely we should handle such situation.
148
  // we will see what we can do with it
149
+ } else {
 
 
 
150
  touch($path);
151
  }
152
  }
154
  /**
155
  * Get file path to data file. Supplying null value as a parameter will
156
  * output current data file.
157
+ *
158
+ * @param int $inc
159
+ * @return string
160
  */
161
+ protected function _getFilePath($inc = null)
162
  {
163
+ $fileName = $this->_fileName;
 
 
 
 
 
 
 
164
 
165
+ if (!is_null($inc)) {
166
+ $fileNameParts = explode('.', $this->_fileName);
167
  $fileNameParts[1] += $inc;
168
 
 
169
  $fileName = implode('.', $fileNameParts);
170
+ } else {
171
+ $fileName = $this->_fileName;
172
  }
173
+
174
+ return $this->_workingDir.'/'.$fileName;
 
 
 
 
175
  }
176
 
177
  /**
178
  * Write
179
+ *
180
+ * @param assoc $data
181
  */
182
+ protected function _write($data)
183
  {
 
184
  $dataLine = json_encode($data);
185
 
186
+ fwrite($this->_fileHandle, $dataLine.PHP_EOL);
 
187
 
188
+ $this->_validateDataFile();
 
189
  }
190
 
191
  /**
193
  */
194
  public function clearDataFiles()
195
  {
196
+ $directoryHandle = opendir($this->_workingDir);
197
+
198
+ while (false !== ($entry = readdir($directoryHandle))) {
199
+ if (strpos($entry, '.') === 0) {
200
+ continue;
201
+ }
202
+
203
+ unlink($this->_workingDir.'/'.$entry);
 
 
 
 
204
  }
205
 
 
206
  closedir($directoryHandle);
207
 
208
+ $this->_validateDataFile();
 
209
  }
210
 
211
  /**
212
  * Store customer profile.
213
+ *
214
+ * @param assoc $profile
215
  */
216
  public function storeProfile($profile)
217
  {
218
+ $this->_write($profile);
 
219
  }
220
  }
app/code/community/Copernica/MarketingSoftware/Helper/Profile.php CHANGED
@@ -36,196 +36,167 @@ class Copernica_MarketingSoftware_Helper_Profile
36
  {
37
  /**
38
  * Get copernica Id based on something and store view.
39
- * @param mixed
40
- * @param Copernica_MarketingSoftware_Model_Abstraction_StoreView
 
41
  */
42
- public function getCopernicaId($something, $storeView)
43
  {
44
  switch (true) {
45
  // is it a customer instance?
46
- case ($something instanceof Copernica_MarketingSoftware_Model_Copernica_Customer):
47
- return $this->getCustomerCopernicaId($something, $storeView);
48
 
49
  // is it a string? most likely it's an email address
50
  case (is_string($something)):
51
- return $this->getEmailCopernicaId($something, $storeView);
52
  }
53
  }
54
 
55
  /**
56
  * Get copernica Id by customer instance
57
- * @param Copernica_MarketingSoftware_Model_Copernica_Customer
58
- * @param Copernica_MarketingSoftware_Model_Abstraction_StoreView
 
59
  * @return string
60
  */
61
- public function getCustomerCopernicaId($customer, $storeView)
62
  {
63
- // get customer Id
64
  $customerId = $customer->id();
65
 
66
- // try to get a profile cache that matches customer and store view
67
- $profileCache = Mage::getModel('marketingsoftware/profileCache')
68
  ->getCollection()
69
  ->addFieldToFilter('customer_id', $customerId)
70
- ->addFieldToFilter('store_view', $storeView->id())
71
  ->setPageSize(1);
72
 
73
- // return copernica Id
74
- if ($profileCache->count()) return $profileCache->getFirstItem()->getCopernicaId();
75
-
76
- /*
77
- * Since same customer could be already subscribed to newsletter, we
78
- * want to check if we have an entry that matches customer email address.
79
- */
80
 
81
- $profileCache = Mage::getModel('marketingsoftware/profileCache')
82
  ->getCollection()
83
- ->addFieldToFilter('store_view', $storeView->id())
84
  ->addFieldToFilter('email', $customer->email())
85
  ->setPageSize(1);
86
 
87
- // check if we have some cached profiles
88
- if ($profileCache->count())
89
- {
90
- // we want a specific item
91
  $profileCache = $profileCache->getFirstItem();
92
-
93
- // set customer Id
94
  $profileCache->setCustomerId($customer->id());
95
 
96
- // upgrade subscriber copernica Id
97
- $this->upgradeSubscriberCopernicaId($profileCache);
98
 
99
- // generate copernica Id
100
- $copernicaId = $this->generateCustomerCopernicaId($customer, $storeView);
101
 
102
- // convert old customer Id on copernica platform
103
- $this->convertCustomerId($this->generateEmailCopernicaId($customer->email(), $storeView), $copernicaId);
104
 
105
- // return new customer copernica Id
106
  return $copernicaId;
107
  }
108
 
109
- // we want a specific item
110
  $profileCache = $profileCache->getFirstItem();
111
 
112
- // generate copernica Id
113
- $copernicaId = $this->generateCustomerCopernicaId($customer, $storeView);
114
 
115
- // set copernica Id
116
  $profileCache->setCopernicaId($copernicaId);
117
- $profileCache->setStoreView($storeView->id());
118
  $profileCache->setCustomerId($customer->id());
119
-
120
- // store profile cache instance
121
  $profileCache->save();
122
 
123
- // generate old copernica Id
124
- $oldCopernicaId = $this->generateOldCopernicaId($customer->oldEmail(), (string)$storeView);
125
 
126
- // We want to convert old coeprnica Id with new one.
127
- $this->convertCustomerId($oldCopernicaId, $copernicaId);
128
 
129
- // return copernica Id
130
  return $copernicaId;
131
  }
132
 
133
  /**
134
  * This method will upgrade profile cache data.
135
- * @param Copernica_MarketingSoftware_Model_ProfileCache
 
136
  */
137
- private function upgradeSubscriberCopernicaId($profileCache)
138
  {
139
- // get customer Id and store view
140
  $customerId = $profileCache->getCustomerId();
141
- $storeViewId = $profileCache->getStoreView();
 
142
 
143
- // set new copernica Id
144
- $profileCache->setCopernicaId($customerId.'|'.$storeViewId);
145
-
146
- // save profile cache right away
147
  $profileCache->save();
148
  }
149
 
150
  /**
151
  * Generate Copernica Id from our customer instance and store view
152
- * @param Copernica_MarketingSoftware_Abstraction_Customer
153
- * @param Copernica_MarketingSoftware_Abstraction_StoreView
 
154
  * @return string
155
  */
156
- public function generateCustomerCopernicaId($customer, $storeView)
157
  {
158
- return $customer->id().'|'.$storeView->id();
159
  }
160
 
161
  /**
162
  * Generate copernica Id from an email address and store View.
163
- * @param string
164
- * @param Copernica_MarketingSoftware_Abstraction_StoreView
 
165
  * @return string
166
  */
167
- public function getEmailCopernicaId($email, $storeView)
168
  {
169
- // fetch profile cache
170
- $profileCache = Mage::getModel('marketingsoftware/profileCache')
171
  ->getCollection()
172
  ->addFieldToFilter('email', $email)
173
- ->addFieldToFilter('store_view', $storeView->id())
174
  ->setPageSize(1);
175
 
176
- // return copernica Id
177
- if ($profileCache->count()) return $profileCache->getFirstItem()->getCopernicaId();
 
178
 
179
- // we want a specific entity
180
  $profileCache = $profileCache->getFirstItem();
181
 
182
- // get old copernica Id
183
- $oldCopernicaId = $this->generateOldCopernicaId($email, (string)$storeView);
184
 
185
- // generate copernica Id from email and store view
186
- $copernicaId = $this->generateEmailCopernicaId($email, $storeView);
187
 
188
- // convert profiles in copernica platform also
189
- $this->convertCustomerId($oldCopernicaId, $copernicaId);
190
 
191
- // set data for profile cache
192
  $profileCache->setEmail($email);
193
  $profileCache->setCopernicaId($copernicaId);
194
- $profileCache->setStoreView($storeView->id());
195
 
196
- // save profile cache entry
197
  $profileCache->save();
198
 
199
- // return copernica Id
200
  return $copernicaId;
201
  }
202
 
203
  /**
204
  * Generate Copernica Id from our email and store view
205
- * @param Copernica_MarketingSoftware_Model_Copernica_Customer
206
- * @param Copernica_MarketingSoftware_Model_Abstraction_StoreView
207
- * @return string
 
208
  */
209
- public function generateEmailCopernicaId($email, $storeView)
210
  {
211
- return $email.'|'.$storeView->id();
212
  }
213
 
214
  /**
215
  * Basically it's a helper method that will convert old customer Id in
216
  * Copernica platform into normal Magento customer Id.
217
- * @param string
218
- * @param string
 
219
  */
220
- private function convertCustomerId($oldCopernicaId, $newCustomerId)
221
  {
222
- // we will need a REST request instance
223
- $request = Mage::helper('marketingsoftware/RESTRequest');
224
 
225
- // get database Id
226
- $databaseId = Mage::helper('marketingsoftware/Api')->getDatabaseId();
227
 
228
- // we want to update profile with new customer Id
229
  $request->put('database/'.$databaseId.'/profiles',
230
  array( 'customer_id' => $newCustomerId),
231
  array( 'fields[]' => 'customer_id=='.$oldCopernicaId )
@@ -235,11 +206,11 @@ class Copernica_MarketingSoftware_Helper_Profile
235
  /**
236
  * Generates a unique customer ID based on the e-mail address and the storeview.
237
  *
238
- * @param string
239
- * @param string
240
  * @return string
241
  */
242
- private function generateOldCopernicaId($email, $storeview)
243
  {
244
  return md5(strtolower($email) . $storeview);
245
  }
36
  {
37
  /**
38
  * Get copernica Id based on something and store view.
39
+ *
40
+ * @param mixed $something
41
+ * @param Copernica_MarketingSoftware_Model_Abstraction_Storeview $storeview
42
  */
43
+ public function getCopernicaId($something, Copernica_MarketingSoftware_Model_Abstraction_Storeview $storeview)
44
  {
45
  switch (true) {
46
  // is it a customer instance?
47
+ case ($something instanceof Copernica_MarketingSoftware_Model_Copernica_Profile_Customer):
48
+ return $this->getCustomerCopernicaId($something, $storeview);
49
 
50
  // is it a string? most likely it's an email address
51
  case (is_string($something)):
52
+ return $this->getEmailCopernicaId($something, $storeview);
53
  }
54
  }
55
 
56
  /**
57
  * Get copernica Id by customer instance
58
+ *
59
+ * @param Copernica_MarketingSoftware_Model_Abstraction_Customer $customer
60
+ * @param Copernica_MarketingSoftware_Model_Abstraction_Storeview $storeview
61
  * @return string
62
  */
63
+ public function getCustomerCopernicaId(Copernica_MarketingSoftware_Model_Abstraction_Customer $customer, Copernica_MarketingSoftware_Model_Abstraction_Storeview $storeview)
64
  {
 
65
  $customerId = $customer->id();
66
 
67
+ $profileCache = Mage::getModel('marketingsoftware/profile_cache')
 
68
  ->getCollection()
69
  ->addFieldToFilter('customer_id', $customerId)
70
+ ->addFieldToFilter('store_view', $storeview->id())
71
  ->setPageSize(1);
72
 
73
+ if ($profileCache->count()) {
74
+ return $profileCache->getFirstItem()->getCopernicaId();
75
+ }
 
 
 
 
76
 
77
+ $profileCache = Mage::getModel('marketingsoftware/profile_cache')
78
  ->getCollection()
79
+ ->addFieldToFilter('store_view', $storeview->id())
80
  ->addFieldToFilter('email', $customer->email())
81
  ->setPageSize(1);
82
 
83
+ if ($profileCache->count()) {
 
 
 
84
  $profileCache = $profileCache->getFirstItem();
 
 
85
  $profileCache->setCustomerId($customer->id());
86
 
87
+ $this->_upgradeSubscriberCopernicaId($profileCache);
 
88
 
89
+ $copernicaId = $this->generateCustomerCopernicaId($customer, $storeview);
 
90
 
91
+ $this->_convertCustomerId($this->generateEmailCopernicaId($customer->email(), $storeview), $copernicaId);
 
92
 
 
93
  return $copernicaId;
94
  }
95
 
 
96
  $profileCache = $profileCache->getFirstItem();
97
 
98
+ $copernicaId = $this->generateCustomerCopernicaId($customer, $storeview);
 
99
 
 
100
  $profileCache->setCopernicaId($copernicaId);
101
+ $profileCache->setStoreView($storeview->id());
102
  $profileCache->setCustomerId($customer->id());
 
 
103
  $profileCache->save();
104
 
105
+ $oldCopernicaId = $this->_generateOldCopernicaId($customer->oldEmail(), (string)$storeview);
 
106
 
107
+ $this->_convertCustomerId($oldCopernicaId, $copernicaId);
 
108
 
 
109
  return $copernicaId;
110
  }
111
 
112
  /**
113
  * This method will upgrade profile cache data.
114
+ *
115
+ * @param Copernica_MarketingSoftware_Model_Profile_Cache $profileCache
116
  */
117
+ protected function _upgradeSubscriberCopernicaId(Copernica_MarketingSoftware_Model_Profile_Cache $profileCache)
118
  {
 
119
  $customerId = $profileCache->getCustomerId();
120
+
121
+ $storeviewId = $profileCache->getStoreView();
122
 
123
+ $profileCache->setCopernicaId($customerId.'|'.$storeviewId);
 
 
 
124
  $profileCache->save();
125
  }
126
 
127
  /**
128
  * Generate Copernica Id from our customer instance and store view
129
+ *
130
+ * @param Copernica_MarketingSoftware_Model_Abstraction_Customer $customer
131
+ * @param Copernica_MarketingSoftware_Model_Abstraction_Storeview $storeview
132
  * @return string
133
  */
134
+ public function generateCustomerCopernicaId(Copernica_MarketingSoftware_Model_Abstraction_Customer $customer, Copernica_MarketingSoftware_Model_Abstraction_Storeview $storeview)
135
  {
136
+ return $customer->id().'|'.$storeview->id();
137
  }
138
 
139
  /**
140
  * Generate copernica Id from an email address and store View.
141
+ *
142
+ * @param string $email
143
+ * @param Copernica_MarketingSoftware_Model_Abstraction_Storeview $storeview
144
  * @return string
145
  */
146
+ public function getEmailCopernicaId($email, Copernica_MarketingSoftware_Model_Abstraction_Storeview $storeview)
147
  {
148
+ $profileCache = Mage::getModel('marketingsoftware/profile_cache')
 
149
  ->getCollection()
150
  ->addFieldToFilter('email', $email)
151
+ ->addFieldToFilter('store_view', $storeview->id())
152
  ->setPageSize(1);
153
 
154
+ if ($profileCache->count()) {
155
+ return $profileCache->getFirstItem()->getCopernicaId();
156
+ }
157
 
 
158
  $profileCache = $profileCache->getFirstItem();
159
 
160
+ $oldCopernicaId = $this->_generateOldCopernicaId($email, (string)$storeview);
 
161
 
162
+ $copernicaId = $this->generateEmailCopernicaId($email, $storeview);
 
163
 
164
+ $this->_convertCustomerId($oldCopernicaId, $copernicaId);
 
165
 
 
166
  $profileCache->setEmail($email);
167
  $profileCache->setCopernicaId($copernicaId);
168
+ $profileCache->setStoreView($storeview->id());
169
 
 
170
  $profileCache->save();
171
 
 
172
  return $copernicaId;
173
  }
174
 
175
  /**
176
  * Generate Copernica Id from our email and store view
177
+ *
178
+ * @param string $email
179
+ * @param Copernica_MarketingSoftware_Model_Abstraction_Storeview $storeview
180
+ * @return string
181
  */
182
+ public function generateEmailCopernicaId($email, Copernica_MarketingSoftware_Model_Abstraction_Storeview $storeview)
183
  {
184
+ return $email.'|'.$storeview->id();
185
  }
186
 
187
  /**
188
  * Basically it's a helper method that will convert old customer Id in
189
  * Copernica platform into normal Magento customer Id.
190
+ *
191
+ * @param string $oldCopernicaId
192
+ * @param string $newCustomerId
193
  */
194
+ protected function _convertCustomerId($oldCopernicaId, $newCustomerId)
195
  {
196
+ $request = Mage::helper('marketingsoftware/rest_request');
 
197
 
198
+ $databaseId = Mage::helper('marketingsoftware/api')->getDatabaseId();
 
199
 
 
200
  $request->put('database/'.$databaseId.'/profiles',
201
  array( 'customer_id' => $newCustomerId),
202
  array( 'fields[]' => 'customer_id=='.$oldCopernicaId )
206
  /**
207
  * Generates a unique customer ID based on the e-mail address and the storeview.
208
  *
209
+ * @param string $email
210
+ * @param Copernica_MarketingSoftware_Model_Abstraction_Storeview $storeview
211
  * @return string
212
  */
213
+ protected function _generateOldCopernicaId($email, Copernica_MarketingSoftware_Model_Abstraction_Storeview $storeview)
214
  {
215
  return md5(strtolower($email) . $storeview);
216
  }
app/code/community/Copernica/MarketingSoftware/Helper/RESTRequest.php DELETED
@@ -1,414 +0,0 @@
1
- <?php
2
- /**
3
- * Copernica Marketing Software
4
- *
5
- * @category Copernica
6
- * @package Copernica_MarketingSoftware
7
- */
8
-
9
- /**
10
- * Copernica REST request helper. This helper is a reusable API request. To
11
- * utilize REST API use 4 basic methods: self::get, self::post(), self::put(),
12
- * self::delete().
13
- */
14
- class Copernica_MarketingSoftware_Helper_RESTRequest
15
- {
16
- /**
17
- * Access token that we want to use with our request.
18
- * @var string
19
- */
20
- protected $accessToken = '';
21
-
22
- /**
23
- * The url to API that we will use.
24
- * @var string
25
- */
26
- protected $hostname = 'https://api.copernica.com';
27
-
28
- /**
29
- * The curl objects
30
- * @var array
31
- */
32
- protected $children = array();
33
-
34
- /**
35
- * Multi curl interface
36
- * @var resource
37
- */
38
- protected $multi;
39
-
40
- /**
41
- * Cipher lists for different crypto libs
42
- * @var array
43
- */
44
- static protected $cipherList = array(
45
- 'openssl' => "ECDHE-RSA-AES128-SHA256:AES128-GCM-SHA256:RC4:+HIGH",
46
- 'nss' => "ecdhe_rsa_aes_256_sha:rc4",
47
- );
48
-
49
- /**
50
- * The currently used crypto lib.
51
- * @var string
52
- */
53
- static protected $cryptoLib = null;
54
-
55
- /**
56
- * We use normal PHP constructor cause Helpers are not childs of
57
- * Varien_Object class, so no _construct is called.
58
- */
59
- public function __construct()
60
- {
61
- // get config into local scope
62
- $config = Mage::helper('marketingsoftware/config');
63
-
64
- // if we have a hostname in config we will use it
65
- if ($hostname = $config->getApiHostname()) $this->hostname = $hostname;
66
-
67
- // if we don't have a hostname in config we will use default one, and
68
- // set the config
69
- else {
70
- // use default one
71
- $this->hostname = 'https://api.copernica.com';
72
-
73
- // set default hostname in config
74
- $config->setApiHostname($this->hostname);
75
- }
76
-
77
- // try to get access token from configuration
78
- $accessToken = $config->getAccessToken();
79
-
80
- // check if we have a valid access token
81
- if ($accessToken) $this->accessToken = $accessToken;
82
- }
83
-
84
- /**
85
- * Destructor for this object.
86
- */
87
- public function __destruct()
88
- {
89
- // if we have an active curl interface we should release resource.
90
- if (!is_null($this->multi)) $this->commit();
91
- }
92
-
93
- /**
94
- * Check request instance. This method will check all essentials to make an
95
- * API call.
96
- * @return bool
97
- */
98
- public function check ()
99
- {
100
- // check if we have access token
101
- if (empty($this->accessToken)) return false;
102
-
103
- // check if we have a hostname
104
- if (empty($this->hostname)) return false;
105
-
106
- // seems that everything is just peachy
107
- return true;
108
- }
109
-
110
- /**
111
- * Helper method to build up a query string
112
- * @param assoc
113
- * @return string
114
- */
115
- protected function buildQueryString($data)
116
- {
117
- // start result parts
118
- $parts = array();
119
-
120
- // iterate over whole data
121
- foreach ($data as $key => $value)
122
- {
123
- // check if our parameter is an array
124
- if (is_array($value))
125
- {
126
- // iterate over all value items
127
- foreach ($value as $valueItem) {
128
- $parts[] = $key.'[]='.urlencode(strval($valueItem));
129
- }
130
- }
131
-
132
- // if we don't have an array we can just use string value
133
- else $parts[] = $key.'='.urlencode(strval($value));
134
- }
135
-
136
- // return result
137
- return '?'.implode('&', $parts);
138
- }
139
-
140
- /**
141
- * Prepare a proper version of curl instance
142
- * @return resource
143
- */
144
- protected function prepareCurl()
145
- {
146
- // prepare curl instance
147
- $curl = curl_init();
148
-
149
- // well, should we make some detective work?
150
- if (is_null(self::$cryptoLib))
151
- {
152
- // get curl version
153
- $version = curl_version();
154
-
155
- // are we using nss?
156
- if (strpos($version['ssl_version'], 'NSS') !== false) self::$cryptoLib = 'nss';
157
-
158
- // ssl engines should work with open ssl cipher names
159
- else self::$cryptoLib = 'openssl';
160
- }
161
-
162
- // set curl option
163
- curl_setopt($curl, CURLOPT_SSL_CIPHER_LIST, self::$cipherList[self::$cryptoLib]);
164
-
165
- // return prepared curl instance
166
- return $curl;
167
- }
168
-
169
- /**
170
- * Make a GET requst
171
- * @param string Request string
172
- * @param assoc (Optional) Data to be passed with request
173
- * @return assoc Decoded JSON from
174
- */
175
- public function get($request, $data = null)
176
- {
177
- // reset curl options
178
- $curl = $this->prepareCurl();
179
-
180
- // if we have access token then we want to append it to request
181
- if ($this->accessToken) $request.=$this->buildQueryString(array_merge(array(
182
- 'access_token' => $this->accessToken),
183
- is_null($data) ? array() : $data
184
- ));
185
-
186
- // well, we don't have an access token
187
- else $request.=$this->buildQueryString(is_null($data) ? array() : $data);
188
-
189
- // set url that we want to receive
190
- curl_setopt($curl, CURLOPT_URL, $this->hostname.'/'.$request);
191
-
192
- // we want to get response from API
193
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
194
-
195
- // decode output
196
- $output = json_decode(curl_exec($curl), true);
197
-
198
- // close curl
199
- curl_close($curl);
200
-
201
- // get the output
202
- return $output;
203
- }
204
-
205
- /**
206
- * Make a POST request
207
- * @param string Request string
208
- * @param assoc (Optional) Data to be passed with request
209
- */
210
- public function post($request, $data = null, $query = null)
211
- {
212
- // check if we have any parameters
213
- if (is_array($query))
214
- {
215
- $request.= $this->buildQueryString(array_merge( $query, array('access_token' => $this->accessToken) ));
216
- }
217
-
218
- // append access token to our request
219
- else $request.='?access_token='.$this->accessToken;
220
-
221
- // create curl
222
- $curl = $this->prepareCurl();
223
-
224
- // set url that we want to receive
225
- curl_setopt($curl, CURLOPT_URL, $this->hostname.'/'.$request);
226
-
227
- // we want to make POST
228
- curl_setopt($curl, CURLOPT_POST, true);
229
-
230
- // set custom method
231
- curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
232
-
233
- // set HTTP headers
234
- curl_setopt($curl, CURLOPT_HTTPHEADER, array(
235
- 'content-type: application/json',
236
- 'accept: application/json'
237
- ));
238
-
239
- // set data
240
- curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
241
-
242
- // try to append to multi interface
243
- if ($this->appendMulti($curl)) return true;
244
-
245
- // execute
246
- curl_exec($curl);
247
-
248
- // close curl
249
- curl_close($curl);
250
-
251
- // allow chaining
252
- return true;
253
- }
254
-
255
- /**
256
- * Make a PUT request
257
- * @param string Request string
258
- * @param assoc (Optional) Data to be passed with request
259
- */
260
- public function put($request, $data = null, $query = null)
261
- {
262
- // check if we have any parameters
263
- if (is_array($query))
264
- {
265
- $request.= $this->buildQueryString(array_merge( $query, array('access_token' => $this->accessToken) ));
266
- }
267
-
268
- // append access token to our request
269
- else $request.='?access_token='.$this->accessToken;
270
-
271
- // reset curl options
272
- $curl = $this->prepareCurl();
273
-
274
- // set url that we want to receive
275
- curl_setopt($curl, CURLOPT_URL, $this->hostname.'/'.$request);
276
-
277
- // we want to make POST
278
- curl_setopt($curl, CURLOPT_POST, true);
279
-
280
- // set custom method
281
- curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
282
-
283
- // set HTTP headers
284
- curl_setopt($curl, CURLOPT_HTTPHEADER, array(
285
- 'content-type: application/json',
286
- 'accept: application/json'
287
- ));
288
-
289
- // set data
290
- curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
291
-
292
- // we want to get the return
293
- // curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
294
-
295
- // try to append to multi interface
296
- if ($this->appendMulti($curl)) return true;
297
-
298
- // execute
299
- curl_exec($curl);
300
-
301
- // close curl
302
- curl_close($curl);
303
-
304
- // allow chaining
305
- return true;
306
- }
307
-
308
- /**
309
- * Make a DELETE request
310
- * @param string Request string
311
- * @param assoc (Optional) Data to be passed with request
312
- */
313
- public function delete($request, $data = null)
314
- {
315
- // reset curl options
316
- $curl = $this->prepareCurl();
317
-
318
- // append access token to our request
319
- $request.='?access_token='.$this->accessToken;
320
-
321
- // set url that we want to receive
322
- curl_setopt($curl, CURLOPT_URL, $this->hostname.'/'.$request);
323
-
324
- // we want to set custom request
325
- curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE");
326
-
327
- // we want all communication in json
328
- curl_setopt($curl, CURLOPT_HTTPHEADER, array(
329
- 'content-type: application/json',
330
- 'accept: application/json'
331
- ));
332
-
333
-
334
- // try to append to multi interface
335
- if ($this->appendMulti($curl)) return true;
336
-
337
- // execure curl request
338
- curl_exec($curl);
339
-
340
- // close curl
341
- curl_close($curl);
342
-
343
- // return output
344
- return true;
345
- }
346
-
347
- /**
348
- * This method will start preparing calls to execute them later on with
349
- * 'multi' interface.
350
- */
351
- public function prepare()
352
- {
353
- // init multi interface
354
- $this->multi = curl_multi_init();
355
-
356
- return $this;
357
- }
358
-
359
- /**
360
- * Commit all prepared calls
361
- */
362
- public function commit()
363
- {
364
- // is it active?
365
- $active = true;
366
-
367
- // if we don't have a multi interface handler we really don't want to process it
368
- if (is_null($this->multi)) return $this;
369
-
370
- /*
371
- * Execute multi curl
372
- */
373
- do {
374
- while (CURLM_CALL_MULTI_PERFORM === curl_multi_exec($this->multi, $running));
375
- if (!$running) break;
376
- while (($res = curl_multi_select($this->multi)) === 0) {};
377
- if ($res === false) {
378
- break;
379
- }
380
- } while(true);
381
-
382
- // free all children
383
- foreach($this->children as $child) curl_multi_remove_handle($this->multi, $child);
384
-
385
- // clean up
386
- curl_multi_close($this->multi);
387
-
388
- // set multi interface to null
389
- $this->multi = null;
390
-
391
- // allow chaining
392
- return $this;
393
- }
394
-
395
- /**
396
- * Append another curl request to multi interface
397
- */
398
- private function appendMulti($curl)
399
- {
400
- if (!is_resource($this->multi)) return false;
401
-
402
- $code = curl_multi_add_handle($this->multi, $curl);
403
-
404
- if ($code == 0) {
405
-
406
- $this->children[] = $curl;
407
-
408
- return true;
409
- }
410
-
411
- return false;
412
- }
413
-
414
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app/code/community/Copernica/MarketingSoftware/Helper/Rest/Request.php ADDED
@@ -0,0 +1,361 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Copernica Marketing Software
4
+ *
5
+ * @category Copernica
6
+ * @package Copernica_MarketingSoftware
7
+ */
8
+
9
+ /**
10
+ * Copernica REST request helper. This helper is a reusable API request. To
11
+ * utilize REST API use 4 basic methods: self::get, self::post(), self::put(),
12
+ * self::delete().
13
+ */
14
+ class Copernica_MarketingSoftware_Helper_Rest_Request
15
+ {
16
+ /**
17
+ * Access token that we want to use with our request.
18
+ *
19
+ * @var string
20
+ */
21
+ protected $_accessToken = '';
22
+
23
+ /**
24
+ * The url to API that we will use.
25
+ *
26
+ * @var string
27
+ */
28
+ protected $_hostname = 'https://api.copernica.com';
29
+
30
+ /**
31
+ * The curl objects
32
+ *
33
+ * @var array
34
+ */
35
+ protected $_children = array();
36
+
37
+ /**
38
+ * Multi curl interface
39
+ *
40
+ * @var resource
41
+ */
42
+ protected $_multi;
43
+
44
+ /**
45
+ * Cipher lists for different crypto libs
46
+ *
47
+ * @var array
48
+ */
49
+ static protected $_cipherList = array(
50
+ 'openssl' => "ECDHE-RSA-AES128-SHA256:AES128-GCM-SHA256:RC4:+HIGH",
51
+ 'nss' => "ecdhe_rsa_aes_256_sha:rc4",
52
+ );
53
+
54
+ /**
55
+ * The currently used crypto lib.
56
+ *
57
+ * @var string
58
+ */
59
+ static protected $_cryptoLib = null;
60
+
61
+ /**
62
+ * We use normal PHP constructor cause Helpers are not childs of
63
+ * Varien_Object class, so no _construct is called.
64
+ */
65
+ public function __construct()
66
+ {
67
+ $config = Mage::helper('marketingsoftware/config');
68
+
69
+ if ($hostname = $config->getApiHostname()) {
70
+ $this->_hostname = $hostname;
71
+ } else {
72
+ $this->_hostname = 'https://api.copernica.com';
73
+
74
+ $config->setApiHostname($this->_hostname);
75
+ }
76
+
77
+ $accessToken = $config->getAccessToken();
78
+
79
+ if ($accessToken) {
80
+ $this->_accessToken = $accessToken;
81
+ }
82
+ }
83
+
84
+ /**
85
+ * Destructor for this object.
86
+ */
87
+ public function __destruct()
88
+ {
89
+ if (!is_null($this->_multi)) {
90
+ $this->commit();
91
+ }
92
+ }
93
+
94
+ /**
95
+ * Check request instance. This method will check all essentials to make an
96
+ * API call.
97
+ *
98
+ * @return bool
99
+ */
100
+ public function check ()
101
+ {
102
+ if (empty($this->_accessToken)) {
103
+ return false;
104
+ }
105
+
106
+ if (empty($this->_hostname)) {
107
+ return false;
108
+ }
109
+
110
+ return true;
111
+ }
112
+
113
+ /**
114
+ * Helper method to build up a query string
115
+ *
116
+ * @param assoc $data
117
+ * @return string
118
+ */
119
+ protected function _buildQueryString($data)
120
+ {
121
+ $parts = array();
122
+
123
+ foreach ($data as $key => $value) {
124
+ if (is_array($value)) {
125
+ foreach ($value as $valueItem) {
126
+ $parts[] = $key.'[]='.urlencode(strval($valueItem));
127
+ }
128
+ } else {
129
+ $parts[] = $key.'='.urlencode(strval($value));
130
+ }
131
+ }
132
+
133
+ return '?'.implode('&', $parts);
134
+ }
135
+
136
+ /**
137
+ * Prepare a proper version of curl instance
138
+ *
139
+ * @return resource
140
+ */
141
+ protected function _prepareCurl()
142
+ {
143
+ $curl = curl_init();
144
+
145
+ if (is_null(self::$_cryptoLib)) {
146
+ $version = curl_version();
147
+
148
+ if (strpos($version['ssl_version'], 'NSS') !== false) {
149
+ self::$_cryptoLib = 'nss';
150
+ } else {
151
+ self::$_cryptoLib = 'openssl';
152
+ }
153
+ }
154
+
155
+ curl_setopt($curl, CURLOPT_SSL_CIPHER_LIST, self::$_cipherList[self::$_cryptoLib]);
156
+ curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
157
+
158
+ return $curl;
159
+ }
160
+
161
+ /**
162
+ * Make a GET requst
163
+ *
164
+ * @param string $request
165
+ * @param assoc $data
166
+ * @return assoc
167
+ */
168
+ public function get($request, $data = null)
169
+ {
170
+ $curl = $this->_prepareCurl();
171
+
172
+ if ($this->_accessToken) {
173
+ $request.=$this->_buildQueryString(array_merge(array(
174
+ 'access_token' => $this->_accessToken),
175
+ is_null($data) ? array() : $data
176
+ ));
177
+ } else {
178
+ $request.=$this->_buildQueryString(is_null($data) ? array() : $data);
179
+ }
180
+
181
+ curl_setopt($curl, CURLOPT_URL, $this->_hostname.'/'.$request);
182
+ curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
183
+
184
+ $output = json_decode(curl_exec($curl), true);
185
+
186
+ curl_close($curl);
187
+
188
+ return $output;
189
+ }
190
+
191
+ /**
192
+ * Make a POST request
193
+ *
194
+ * @param string $request
195
+ * @param assoc $data
196
+ * @param assoc $query
197
+ */
198
+ public function post($request, $data = null, $query = null)
199
+ {
200
+ if (is_array($query)) {
201
+ $request.= $this->_buildQueryString(array_merge( $query, array('access_token' => $this->_accessToken) ));
202
+ } else {
203
+ $request.='?access_token='.$this->_accessToken;
204
+ }
205
+
206
+ $curl = $this->_prepareCurl();
207
+
208
+ curl_setopt($curl, CURLOPT_URL, $this->_hostname.'/'.$request);
209
+ curl_setopt($curl, CURLOPT_POST, true);
210
+ curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
211
+ curl_setopt($curl, CURLOPT_HTTPHEADER, array(
212
+ 'content-type: application/json',
213
+ 'accept: application/json'
214
+ ));
215
+ curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
216
+
217
+ if ($this->_appendMulti($curl)) {
218
+ return true;
219
+ }
220
+
221
+ curl_exec($curl);
222
+
223
+ curl_close($curl);
224
+
225
+ return true;
226
+ }
227
+
228
+ /**
229
+ * Make a PUT request
230
+ *
231
+ * @param string $request
232
+ * @param assoc $data
233
+ * @param assoc $query
234
+ */
235
+ public function put($request, $data = null, $query = null)
236
+ {
237
+ if (is_array($query)) {
238
+ $request.= $this->_buildQueryString(array_merge( $query, array('access_token' => $this->_accessToken) ));
239
+ } else {
240
+ $request.='?access_token='.$this->_accessToken;
241
+ }
242
+
243
+ $curl = $this->_prepareCurl();
244
+
245
+ curl_setopt($curl, CURLOPT_URL, $this->_hostname.'/'.$request);
246
+ curl_setopt($curl, CURLOPT_POST, true);
247
+ curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
248
+ curl_setopt($curl, CURLOPT_HTTPHEADER, array(
249
+ 'content-type: application/json',
250
+ 'accept: application/json'
251
+ ));
252
+ curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
253
+ // curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
254
+
255
+ if ($this->_appendMulti($curl)) {
256
+ return true;
257
+ }
258
+
259
+ curl_exec($curl);
260
+
261
+ curl_close($curl);
262
+
263
+ return true;
264
+ }
265
+
266
+ /**
267
+ * Make a DELETE request
268
+ *
269
+ * @param string $request
270
+ * @param assoc $data
271
+ */
272
+ public function delete($request, $data = null)
273
+ {
274
+ $curl = $this->_prepareCurl();
275
+
276
+ $request.='?access_token='.$this->_accessToken;
277
+
278
+ curl_setopt($curl, CURLOPT_URL, $this->_hostname.'/'.$request);
279
+ curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE");
280
+ curl_setopt($curl, CURLOPT_HTTPHEADER, array(
281
+ 'content-type: application/json',
282
+ 'accept: application/json'
283
+ ));
284
+
285
+ if ($this->_appendMulti($curl)) {
286
+ return true;
287
+ }
288
+
289
+ curl_exec($curl);
290
+
291
+ curl_close($curl);
292
+
293
+ return true;
294
+ }
295
+
296
+ /**
297
+ * This method will start preparing calls to execute them later on with
298
+ * 'multi' interface.
299
+ */
300
+ public function prepare()
301
+ {
302
+ $this->_multi = curl_multi_init();
303
+
304
+ return $this;
305
+ }
306
+
307
+ /**
308
+ * Commit all prepared calls
309
+ */
310
+ public function commit()
311
+ {
312
+ $active = true;
313
+
314
+ if (is_null($this->_multi)) {
315
+ return $this;
316
+ }
317
+
318
+ do {
319
+ while (CURLM_CALL_MULTI_PERFORM === curl_multi_exec($this->_multi, $running));
320
+ if (!$running) {
321
+ break;
322
+ }
323
+
324
+ while (($res = curl_multi_select($this->_multi)) === 0){};
325
+
326
+ if ($res === false) {
327
+ break;
328
+ }
329
+ } while(true);
330
+
331
+ foreach ($this->_children as $child) {
332
+ curl_multi_remove_handle($this->_multi, $child);
333
+ }
334
+
335
+ curl_multi_close($this->_multi);
336
+
337
+ $this->_multi = null;
338
+
339
+ return $this;
340
+ }
341
+
342
+ /**
343
+ * Append another curl request to multi interface
344
+ */
345
+ protected function _appendMulti($curl)
346
+ {
347
+ if (!is_resource($this->_multi)) {
348
+ return false;
349
+ }
350
+
351
+ $code = curl_multi_add_handle($this->_multi, $curl);
352
+
353
+ if ($code == 0) {
354
+ $this->_children[] = $curl;
355
+
356
+ return true;
357
+ }
358
+
359
+ return false;
360
+ }
361
+ }
app/code/community/Copernica/MarketingSoftware/Model/{AbandonedCart.php → Abandoned/Cart.php} RENAMED
@@ -27,26 +27,27 @@
27
  /**
28
  * This model will be used to track all abandoned carts that we sent to copernica.
29
  */
30
- class Copernica_MarketingSoftware_Model_AbandonedCart extends Mage_Core_Model_Abstract
31
  {
32
  /**
33
  * Construct abandoned cart model
34
  */
35
  protected function _construct()
36
  {
37
- $this->_init('marketingsoftware/abandonedCart');
38
  }
39
 
40
  /**
41
  * Set quote Id
42
- * @param int
 
43
  */
44
  public function setQuoteId($quoteID)
45
  {
46
  parent::setData('quote_id', $quoteID);
47
 
48
- // Since we just modified quote Id we want to set timestamp also.
49
  $timestamp = new DateTime();
 
50
  parent::setData('timestamp', $timestamp->format("Y-m-d H:i:s"));
51
 
52
  return $this;
@@ -54,6 +55,7 @@ class Copernica_MarketingSoftware_Model_AbandonedCart extends Mage_Core_Model_Ab
54
 
55
  /**
56
  * Get quote Id
 
57
  * @return int
58
  */
59
  public function getQuoteId()
27
  /**
28
  * This model will be used to track all abandoned carts that we sent to copernica.
29
  */
30
+ class Copernica_MarketingSoftware_Model_Abandoned_Cart extends Mage_Core_Model_Abstract
31
  {
32
  /**
33
  * Construct abandoned cart model
34
  */
35
  protected function _construct()
36
  {
37
+ $this->_init('marketingsoftware/abandoned_cart');
38
  }
39
 
40
  /**
41
  * Set quote Id
42
+ *
43
+ * @param int $quoteID
44
  */
45
  public function setQuoteId($quoteID)
46
  {
47
  parent::setData('quote_id', $quoteID);
48
 
 
49
  $timestamp = new DateTime();
50
+
51
  parent::setData('timestamp', $timestamp->format("Y-m-d H:i:s"));
52
 
53
  return $this;
55
 
56
  /**
57
  * Get quote Id
58
+ *
59
  * @return int
60
  */
61
  public function getQuoteId()
app/code/community/Copernica/MarketingSoftware/Model/{AbandonedCartsProcessor.php → Abandoned/Carts/Processor.php} RENAMED
@@ -29,42 +29,36 @@
29
  * have to do it by ourselfs. This class will take care of such detection and
30
  * will spawn proper events on queue to sync all relevant carts.
31
  */
32
- class Copernica_MarketingSoftware_Model_AbandonedCartsProcessor
33
  {
34
  /**
35
  * Check if we did already marked
36
- * @param Collection of quotes
 
37
  * @return boolean
38
  */
39
- private function filterNotMarked($quoteCollection)
40
  {
41
- /*
42
- * At first we have get all quotes Ids. This way we will be able to
43
- * check if we have any quotes that were already marked as abandoned
44
- * carts.
45
- */
46
  $quotesIds = array();
47
- foreach ($quoteCollection as $quote) $quotesIds[] = $quote->getId();
48
-
49
- /*
50
- * Now we will need an array of marked abandoned carts that we will use
51
- * to determine quotes that were not marked yet. We will do it by
52
- * asking collection of such carts for all quotes ids that are contained
53
- * in collection that we reveived. Sencondly we will construct array of
54
- * ids of quotes that were already marked.
55
- */
56
- $markedQuotesIds = Mage::getModel('marketingsoftware/abandonedCart')->getCollection()->addFieldToFilter('quote_id', array('in' => $quotesIds));
57
  $markedArray = array();
58
- foreach ($markedQuotesIds as $abandonedCart) $markedArray[] = $abandonedCart->getQuoteId();
 
 
 
59
 
60
- /*
61
- * Construct array of quotes that were note marked already. Then we can
62
- * safely return that array.
63
- */
64
  $notMarkedQuotes = array();
65
- foreach ($quoteCollection as $quote)
66
- {
67
- if (in_array($quote->getId(), $markedArray)) continue;
 
 
68
 
69
  $notMarkedQuotes[] = $quote;
70
  }
@@ -77,64 +71,47 @@ class Copernica_MarketingSoftware_Model_AbandonedCartsProcessor
77
  */
78
  public function detectAbandonedCarts()
79
  {
80
- // get quote collection from magento reports module
81
  $collection = Mage::getResourceModel('reports/quote_collection');
82
 
83
- // get config helper
84
  $config = Mage::helper('marketingsoftware/config');
85
 
86
- // get stores Ids
87
  $storeIds = $config->getEnabledStores();
88
 
89
- // ensure that we have an array
90
- if (!is_array($storeIds)) $storeIds = array();
 
91
 
92
- // create proper timeout limit
93
  $timeoutLimit = new DateTime();
94
  $timeoutInterval = new DateInterval("PT".(int)($config->getAbandonedTimeout())."M");
95
  $timeoutInterval->invert = 1;
96
  $timeoutLimit->add($timeoutInterval);
97
 
98
- /*
99
- * Magento is not clearing queue table from quotes that are active. This
100
- * way after a time that table does containt a lot of quotes that were
101
- * abandoned for customers. Thus, for marketing purpouses, we don't need
102
- * such carts, we need ones that are relevant to us.
103
- */
104
  $createdLimit = new DateTime();
105
  $createdInterval = new DateInterval("PT".(int)($config->getAbandonedPeriod())."M");
106
  $createdInterval->invert = 1;
107
  $createdLimit->add($createdInterval);
108
 
109
- // prepare collection for abandoned carts
110
- $collection->prepareForAbandonedReport($storeIds);
111
-
112
- // we don't care about
113
  $collection->addFieldToFilter('main_table.updated_at', array('lt' => $timeoutLimit->format("Y-m-d H:i:s")));
114
  $collection->addFieldToFilter('main_table.created_at', array('gt' => $createdLimit->format("Y-m-d H:i:s")));
115
  $collection->setPageSize(500);
116
 
117
- // get number of pages
118
  $pages = $collection->getLastPageNumber();
 
119
  $currentPage = 1;
120
 
121
- // iterate over collection
122
  do {
123
- // set current page and load data
124
  $collection->setCurPage($currentPage);
125
  $collection->load();
126
 
127
- // filter quotes that we did already marked as abandoned
128
- $filteredCollection = $this->filterNotMarked($collection);
129
 
130
- // iterate over quotes and mark them as abandoned
131
- foreach ($filteredCollection as $quote)
132
- {
133
- $abandonedCart = Mage::getModel('marketingsoftware/abandonedCart')
134
  ->setQuoteId($quote->getId())
135
  ->save();
136
 
137
- $queue = Mage::getModel('marketingsoftware/queue')
138
  ->setObject()
139
  ->setCustomer($quote->getCustomerId())
140
  ->setAction('modify')
@@ -143,12 +120,10 @@ class Copernica_MarketingSoftware_Model_AbandonedCartsProcessor
143
  ->save();
144
  }
145
 
146
- // increment current page
147
  $currentPage++;
148
 
149
- // clear collection so it will not hammer our memory
150
  $collection->clear();
151
 
152
- } while($currentPage <= $pages);
153
  }
154
  }
29
  * have to do it by ourselfs. This class will take care of such detection and
30
  * will spawn proper events on queue to sync all relevant carts.
31
  */
32
+ class Copernica_MarketingSoftware_Model_Abandoned_Carts_Processor
33
  {
34
  /**
35
  * Check if we did already marked
36
+ *
37
+ * @param array $quoteCollection
38
  * @return boolean
39
  */
40
+ protected function _filterNotMarked($quoteCollection)
41
  {
 
 
 
 
 
42
  $quotesIds = array();
43
+
44
+ foreach ($quoteCollection as $quote) {
45
+ $quotesIds[] = $quote->getId();
46
+ }
47
+
48
+ $markedQuotesIds = Mage::getModel('marketingsoftware/abandoned_cart')->getCollection()->addFieldToFilter('quote_id', array('in' => $quotesIds));
49
+
 
 
 
50
  $markedArray = array();
51
+
52
+ foreach ($markedQuotesIds as $abandonedCart) {
53
+ $markedArray[] = $abandonedCart->getQuoteId();
54
+ }
55
 
 
 
 
 
56
  $notMarkedQuotes = array();
57
+
58
+ foreach ($quoteCollection as $quote) {
59
+ if (in_array($quote->getId(), $markedArray)) {
60
+ continue;
61
+ }
62
 
63
  $notMarkedQuotes[] = $quote;
64
  }
71
  */
72
  public function detectAbandonedCarts()
73
  {
 
74
  $collection = Mage::getResourceModel('reports/quote_collection');
75
 
 
76
  $config = Mage::helper('marketingsoftware/config');
77
 
 
78
  $storeIds = $config->getEnabledStores();
79
 
80
+ if (!is_array($storeIds)) {
81
+ $storeIds = array();
82
+ }
83
 
 
84
  $timeoutLimit = new DateTime();
85
  $timeoutInterval = new DateInterval("PT".(int)($config->getAbandonedTimeout())."M");
86
  $timeoutInterval->invert = 1;
87
  $timeoutLimit->add($timeoutInterval);
88
 
 
 
 
 
 
 
89
  $createdLimit = new DateTime();
90
  $createdInterval = new DateInterval("PT".(int)($config->getAbandonedPeriod())."M");
91
  $createdInterval->invert = 1;
92
  $createdLimit->add($createdInterval);
93
 
94
+ $collection->prepareForAbandonedReport($storeIds);
 
 
 
95
  $collection->addFieldToFilter('main_table.updated_at', array('lt' => $timeoutLimit->format("Y-m-d H:i:s")));
96
  $collection->addFieldToFilter('main_table.created_at', array('gt' => $createdLimit->format("Y-m-d H:i:s")));
97
  $collection->setPageSize(500);
98
 
 
99
  $pages = $collection->getLastPageNumber();
100
+
101
  $currentPage = 1;
102
 
 
103
  do {
 
104
  $collection->setCurPage($currentPage);
105
  $collection->load();
106
 
107
+ $filteredCollection = $this->_filterNotMarked($collection);
 
108
 
109
+ foreach ($filteredCollection as $quote) {
110
+ $abandonedCart = Mage::getModel('marketingsoftware/abandoned_cart')
 
 
111
  ->setQuoteId($quote->getId())
112
  ->save();
113
 
114
+ $queue = Mage::getModel('marketingsoftware/queue_item')
115
  ->setObject()
116
  ->setCustomer($quote->getCustomerId())
117
  ->setAction('modify')
120
  ->save();
121
  }
122
 
 
123
  $currentPage++;
124
 
 
125
  $collection->clear();
126
 
127
+ } while ($currentPage <= $pages);
128
  }
129
  }
app/code/community/Copernica/MarketingSoftware/Model/Abstraction/Address.php CHANGED
@@ -31,99 +31,102 @@ class Copernica_MarketingSoftware_Model_Abstraction_Address implements Serializa
31
  {
32
  /**
33
  * The original object
34
- * @param Mage_Sales_Model_Order_Address|Mage_Sales_Model_Quote_Address|Mage_Customer_Model_Address
 
 
35
  */
36
- protected $original;
37
 
38
  /**
39
  * Predefine the internal fields
40
  */
41
- protected $id;
42
- protected $type;
43
- protected $name;
44
- protected $email;
45
- protected $street;
46
- protected $city;
47
- protected $zipcode;
48
- protected $state;
49
- protected $countryCode;
50
- protected $telephone;
51
- protected $fax;
52
- protected $company;
53
- protected $customerId = null;
54
 
55
  /**
56
  * Sets the original model
57
- * @param Mage_Sales_Model_Order_Address|Mage_Sales_Model_Quote_Address|Mage_Customer_Model_Address $original
58
- * @return Copernica_MarketingSoftware_Model_Abstraction_Address
 
59
  */
60
  public function setOriginal($original)
61
  {
62
- $this->name = Mage::getModel('marketingsoftware/abstraction_name')->setOriginal($original);
63
- $this->street = $original->getStreetFull();
64
- $this->city = $original->getCity();
65
- $this->zipcode = $original->getPostcode();
66
- $this->state = $original->getRegion();
67
- $this->company = $original->getCompany();
68
- $this->countryCode = $original->getCountryId();
69
- $this->telephone = $original->getTelephone();
70
- $this->fax = $original->getFax();
71
 
72
  if ($type = $original->getAddressType()) {
73
- $this->type = array($type);
74
  } else {
75
  $types = array();
 
76
  if ($customer = $original->getCustomer()) {
77
  $id = $original->getId();
 
78
  if ($customer->getData('default_billing') == $id) {
79
  $types[] = 'billing';
80
  }
 
81
  if ($customer->getData('default_shipping') == $id) {
82
  $types[] = 'shipping';
83
  }
84
  }
85
- $this->type = $types;
86
  }
87
 
88
- //the order quote address model only returns a customer if it exists
89
  if ($customerId = $original->getCustomerId()) {
90
- $this->customerId = $original->getCustomerId();
91
  } elseif ($customer = $original->getCustomer()) {
92
- $this->customerId = $customer->getId();
93
  }
94
 
95
- // Get the normal identifier
96
  $id = $original->getId();
97
 
98
- // switch depending on the type
99
  switch(get_class($original)) {
100
  case "Mage_Sales_Model_Order_Address":
101
  if ($cid = $original->getCustomerAddressId()) {
102
- $this->id = 'ca_'.$cid;
103
  } else {
104
- $this->id = 'oa_'.$id;
105
  }
106
  break;
107
  case "Mage_Sales_Model_Quote_Address":
108
  if ($cid = $original->getCustomerAddressId()) {
109
- $this->id = 'ca_'.$cid;
110
  } else {
111
- $this->id = 'qa_'.$id;
112
  }
113
  break;
114
  case "Mage_Customer_Model_Address":
115
- $this->id = 'ca_'.$id;
116
  break;
117
  default:
118
- $this->id = $id;
119
  }
120
 
121
  if ($email = $original->getEmail()) {
122
- $this->email = $email;
123
  } elseif (is_object($order = $original->getOrder()) && $customerEmail = $order->getCustomerEmail()) {
124
- $this->email = $customerEmail;
125
  } elseif (is_object($quote = $original->getQuote()) && $customerEmail = $quote->getCustomerEmail()) {
126
- $this->email = $customerEmail;
127
  }
128
 
129
  return $this;
@@ -131,21 +134,23 @@ class Copernica_MarketingSoftware_Model_Abstraction_Address implements Serializa
131
 
132
  /**
133
  * Return the type of this address
134
- * @return array {0-2} (billing|shipping)
 
135
  */
136
  public function type()
137
  {
138
- return $this->type;
139
  }
140
 
141
  /**
142
  * The customer may return null
143
- * @return Copernica_MarketingSoftware_Model_Abstraction_Customer
 
144
  */
145
  public function customer()
146
  {
147
- if ($this->customerId) {
148
- return Mage::getModel('marketingsoftware/abstraction_customer')->loadCustomer($this->customerId);
149
  } else {
150
  return null;
151
  }
@@ -154,110 +159,121 @@ class Copernica_MarketingSoftware_Model_Abstraction_Address implements Serializa
154
  /**
155
  * We want to return a unique id, but to do this we need to append
156
  * a prefix based on the type of address
 
157
  * @return string
158
  */
159
  public function id()
160
  {
161
- return $this->id;
162
  }
163
 
164
  /**
165
  * Return the name belonging to this address
166
- * @return Copernica_MarketingSoftware_Model_Abstraction_Name
 
167
  */
168
  public function name()
169
  {
170
- return $this->name;
171
  }
172
 
173
  /**
174
  * Return the e-mail address of this address
175
- * @return string
 
176
  */
177
  public function email()
178
  {
179
- return $this->email;
180
  }
181
 
182
  /**
183
  * Return the street of this addresses
184
- * @return string
 
185
  */
186
  public function street()
187
  {
188
- return $this->street;
189
  }
190
 
191
  /**
192
  * Get the city of this addresses
193
- * @return string
 
194
  */
195
  public function city()
196
  {
197
- return $this->city;
198
  }
199
 
200
  /**
201
  * Get the zipcode of this addresses
202
- * @return string
 
203
  */
204
  public function zipcode()
205
  {
206
- return $this->zipcode;
207
  }
208
 
209
  /**
210
  * Get the state of this addresses
211
- * @return string
 
212
  */
213
  public function state()
214
  {
215
- return $this->state;
216
  }
217
 
218
  /**
219
- * Get the countrycode of this addresses
220
- * @return string
 
221
  */
222
- public function countryCode()
223
  {
224
- return $this->countryCode;
225
  }
226
 
227
  /**
228
  * Get the telephone number of this addresses
229
- * @return string
 
230
  */
231
  public function telephone()
232
  {
233
- return $this->telephone;
234
  }
235
 
236
  /**
237
  * Get the fax number of this addresses
238
- * @return string
 
239
  */
240
  public function fax()
241
  {
242
- return $this->fax;
243
  }
244
 
245
  /**
246
  * Get the company of this address
247
- * @return string
 
248
  */
249
  public function company()
250
  {
251
- return $this->company;
252
  }
253
 
254
  /**
255
  * Serialize the object
256
- * @return string
 
257
  */
258
  public function serialize()
259
  {
260
- // serialize the data
261
  return serialize(array(
262
  $this->id(),
263
  $this->type(),
@@ -267,7 +283,7 @@ class Copernica_MarketingSoftware_Model_Abstraction_Address implements Serializa
267
  $this->city(),
268
  $this->zipcode(),
269
  $this->state(),
270
- $this->countryCode(),
271
  $this->telephone(),
272
  $this->fax(),
273
  $this->company(),
@@ -277,26 +293,28 @@ class Copernica_MarketingSoftware_Model_Abstraction_Address implements Serializa
277
 
278
  /**
279
  * Unserialize the object
280
- * @param string
281
- * @return Copernica_MarketingSoftware_Model_Abstraction_Address
 
282
  */
283
  public function unserialize($string)
284
  {
285
  list(
286
- $this->id,
287
- $this->type,
288
- $this->name,
289
- $this->email,
290
- $this->street,
291
- $this->city,
292
- $this->zipcode,
293
- $this->state,
294
- $this->countryCode,
295
- $this->telephone,
296
- $this->fax,
297
- $this->company,
298
- $this->customerId
299
  ) = unserialize($string);
 
300
  return $this;
301
  }
302
  }
31
  {
32
  /**
33
  * The original object
34
+ *
35
+ * @todo Not used???
36
+ * @var Mage_Sales_Model_Order_Address|Mage_Sales_Model_Quote_Address|Mage_Customer_Model_Address
37
  */
38
+ protected $_original;
39
 
40
  /**
41
  * Predefine the internal fields
42
  */
43
+ protected $_id;
44
+ protected $_type;
45
+ protected $_name;
46
+ protected $_email;
47
+ protected $_street;
48
+ protected $_city;
49
+ protected $_zipcode;
50
+ protected $_state;
51
+ protected $_countryId;
52
+ protected $_telephone;
53
+ protected $_fax;
54
+ protected $_company;
55
+ protected $_customerId = null;
56
 
57
  /**
58
  * Sets the original model
59
+ *
60
+ * @param Mage_Sales_Model_Order_Address|Mage_Sales_Model_Quote_Address|Mage_Customer_Model_Address $original
61
+ * @return Copernica_MarketingSoftware_Model_Abstraction_Address
62
  */
63
  public function setOriginal($original)
64
  {
65
+ $this->_name = Mage::getModel('marketingsoftware/abstraction_name')->setOriginal($original);
66
+ $this->_street = $original->getStreetFull();
67
+ $this->_city = $original->getCity();
68
+ $this->_zipcode = $original->getPostcode();
69
+ $this->_state = $original->getRegion();
70
+ $this->_company = $original->getCompany();
71
+ $this->_countryId = $original->getCountryId();
72
+ $this->_telephone = $original->getTelephone();
73
+ $this->_fax = $original->getFax();
74
 
75
  if ($type = $original->getAddressType()) {
76
+ $this->_type = array($type);
77
  } else {
78
  $types = array();
79
+
80
  if ($customer = $original->getCustomer()) {
81
  $id = $original->getId();
82
+
83
  if ($customer->getData('default_billing') == $id) {
84
  $types[] = 'billing';
85
  }
86
+
87
  if ($customer->getData('default_shipping') == $id) {
88
  $types[] = 'shipping';
89
  }
90
  }
91
+ $this->_type = $types;
92
  }
93
 
 
94
  if ($customerId = $original->getCustomerId()) {
95
+ $this->_customerId = $original->getCustomerId();
96
  } elseif ($customer = $original->getCustomer()) {
97
+ $this->_customerId = $customer->getId();
98
  }
99
 
 
100
  $id = $original->getId();
101
 
 
102
  switch(get_class($original)) {
103
  case "Mage_Sales_Model_Order_Address":
104
  if ($cid = $original->getCustomerAddressId()) {
105
+ $this->_id = 'ca_'.$cid;
106
  } else {
107
+ $this->_id = 'oa_'.$id;
108
  }
109
  break;
110
  case "Mage_Sales_Model_Quote_Address":
111
  if ($cid = $original->getCustomerAddressId()) {
112
+ $this->_id = 'ca_'.$cid;
113
  } else {
114
+ $this->_id = 'qa_'.$id;
115
  }
116
  break;
117
  case "Mage_Customer_Model_Address":
118
+ $this->_id = 'ca_'.$id;
119
  break;
120
  default:
121
+ $this->_id = $id;
122
  }
123
 
124
  if ($email = $original->getEmail()) {
125
+ $this->_email = $email;
126
  } elseif (is_object($order = $original->getOrder()) && $customerEmail = $order->getCustomerEmail()) {
127
+ $this->_email = $customerEmail;
128
  } elseif (is_object($quote = $original->getQuote()) && $customerEmail = $quote->getCustomerEmail()) {
129
+ $this->_email = $customerEmail;
130
  }
131
 
132
  return $this;
134
 
135
  /**
136
  * Return the type of this address
137
+ *
138
+ * @return array
139
  */
140
  public function type()
141
  {
142
+ return $this->_type;
143
  }
144
 
145
  /**
146
  * The customer may return null
147
+ *
148
+ * @return Copernica_MarketingSoftware_Model_Abstraction_Customer|null
149
  */
150
  public function customer()
151
  {
152
+ if ($this->_customerId) {
153
+ return Mage::getModel('marketingsoftware/abstraction_customer')->loadCustomer($this->_customerId);
154
  } else {
155
  return null;
156
  }
159
  /**
160
  * We want to return a unique id, but to do this we need to append
161
  * a prefix based on the type of address
162
+ *
163
  * @return string
164
  */
165
  public function id()
166
  {
167
+ return $this->_id;
168
  }
169
 
170
  /**
171
  * Return the name belonging to this address
172
+ *
173
+ * @return Copernica_MarketingSoftware_Model_Abstraction_Name
174
  */
175
  public function name()
176
  {
177
+ return $this->_name;
178
  }
179
 
180
  /**
181
  * Return the e-mail address of this address
182
+ *
183
+ * @return string
184
  */
185
  public function email()
186
  {
187
+ return $this->_email;
188
  }
189
 
190
  /**
191
  * Return the street of this addresses
192
+ *
193
+ * @return string
194
  */
195
  public function street()
196
  {
197
+ return $this->_street;
198
  }
199
 
200
  /**
201
  * Get the city of this addresses
202
+ *
203
+ * @return string
204
  */
205
  public function city()
206
  {
207
+ return $this->_city;
208
  }
209
 
210
  /**
211
  * Get the zipcode of this addresses
212
+ *
213
+ * @return string
214
  */
215
  public function zipcode()
216
  {
217
+ return $this->_zipcode;
218
  }
219
 
220
  /**
221
  * Get the state of this addresses
222
+ *
223
+ * @return string
224
  */
225
  public function state()
226
  {
227
+ return $this->_state;
228
  }
229
 
230
  /**
231
+ * Get the country id of this addresses
232
+ *
233
+ * @return string
234
  */
235
+ public function countryId()
236
  {
237
+ return $this->_countryId;
238
  }
239
 
240
  /**
241
  * Get the telephone number of this addresses
242
+ *
243
+ * @return string
244
  */
245
  public function telephone()
246
  {
247
+ return $this->_telephone;
248
  }
249
 
250
  /**
251
  * Get the fax number of this addresses
252
+ *
253
+ * @return string
254
  */
255
  public function fax()
256
  {
257
+ return $this->_fax;
258
  }
259
 
260
  /**
261
  * Get the company of this address
262
+ *
263
+ * @return string
264
  */
265
  public function company()
266
  {
267
+ return $this->_company;
268
  }
269
 
270
  /**
271
  * Serialize the object
272
+ *
273
+ * @return string
274
  */
275
  public function serialize()
276
  {
 
277
  return serialize(array(
278
  $this->id(),
279
  $this->type(),
283
  $this->city(),
284
  $this->zipcode(),
285
  $this->state(),
286
+ $this->countryId(),
287
  $this->telephone(),
288
  $this->fax(),
289
  $this->company(),
293
 
294
  /**
295
  * Unserialize the object
296
+ *
297
+ * @param string $string
298
+ * @return Copernica_MarketingSoftware_Model_Abstraction_Address
299
  */
300
  public function unserialize($string)
301
  {
302
  list(
303
+ $this->_id,
304
+ $this->_type,
305
+ $this->_name,
306
+ $this->_email,
307
+ $this->_street,
308
+ $this->_city,
309
+ $this->_zipcode,
310
+ $this->_state,
311
+ $this->_countryId,
312
+ $this->_telephone,
313
+ $this->_fax,
314
+ $this->_company,
315
+ $this->_customerId
316
  ) = unserialize($string);
317
+
318
  return $this;
319
  }
320
  }
app/code/community/Copernica/MarketingSoftware/Model/Abstraction/Attributes.php CHANGED
@@ -32,78 +32,85 @@ class Copernica_MarketingSoftware_Model_Abstraction_Attributes implements Serial
32
  /**
33
  * Predefine the internal fields
34
  */
35
- protected $name;
36
- protected $attributes;
37
 
38
  /**
39
  * Sets the original model
40
- * @param Mage_Catalog_Model_Product $original
41
- * @return Copernica_MarketingSoftware_Model_Abstraction_Attributes
 
42
  */
43
  public function setOriginal(Mage_Catalog_Model_Product $original)
44
  {
45
  if ($attributeSet = Mage::getModel('eav/entity_attribute_set')->load($original->getAttributeSetId())) {
46
- $this->name = $attributeSet->getAttributeSetName();
47
  }
48
 
49
  $data = array();
 
50
  $attributes = $original->getAttributes();
51
 
52
  foreach ($attributes as $attribute) {
53
  if (
54
- $attribute->getIsUserDefined() &&
55
- in_array($attribute->getFrontendInput(), array('text', 'select', 'multiline', 'textarea', 'price', 'date', 'multiselect')) &&
56
- ($label = $attribute->getAttributeCode()) &&
57
- ($value = $attribute->getFrontend()->getValue($original))
58
  ) {
59
- // is this an object which is not serializable
60
- // add the value to the array of data
61
  $data[$label] = $value;
62
  }
63
  }
64
- $this->attributes = $data;
 
65
 
66
-
67
  return $this;
68
  }
69
 
70
  /**
71
  * The name of this product
72
- * @return integer
 
73
  */
74
  public function name()
75
  {
76
- return $this->name;
77
  }
78
 
79
  /**
80
  * Return an assoc array with attributes.
81
  *
82
- * @param Bool $useAttribcode
83
- * @return array
84
  */
85
  public function attributes($useAttribCode = false)
86
  {
87
- return $this->attributes;
88
  }
89
 
90
  /**
91
  * Return a string representation
 
 
92
  */
93
  public function __toString()
94
  {
95
  $options = "";
96
- foreach ($this->attributes() as $key => $value) $options .= "$key: $value\n";
 
 
 
 
97
  return $options;
98
  }
99
 
100
  /**
101
  * Serialize the object
102
- * @return string
 
103
  */
104
  public function serialize()
105
  {
106
- // serialize the data
107
  return serialize(array(
108
  $this->name(),
109
  $this->attributes(),
@@ -112,15 +119,17 @@ class Copernica_MarketingSoftware_Model_Abstraction_Attributes implements Serial
112
 
113
  /**
114
  * Unserialize the object
115
- * @param string
116
- * @return Copernica_MarketingSoftware_Model_Abstraction_Attributes
 
117
  */
118
  public function unserialize($string)
119
  {
120
  list(
121
- $this->name,
122
- $this->attributes
123
  ) = unserialize($string);
 
124
  return $this;
125
  }
126
  }
32
  /**
33
  * Predefine the internal fields
34
  */
35
+ protected $_name;
36
+ protected $_attributes;
37
 
38
  /**
39
  * Sets the original model
40
+ *
41
+ * @param Mage_Catalog_Model_Product $original
42
+ * @return Copernica_MarketingSoftware_Model_Abstraction_Attributes
43
  */
44
  public function setOriginal(Mage_Catalog_Model_Product $original)
45
  {
46
  if ($attributeSet = Mage::getModel('eav/entity_attribute_set')->load($original->getAttributeSetId())) {
47
+ $this->_name = $attributeSet->getAttributeSetName();
48
  }
49
 
50
  $data = array();
51
+
52
  $attributes = $original->getAttributes();
53
 
54
  foreach ($attributes as $attribute) {
55
  if (
56
+ $attribute->getIsUserDefined() &&
57
+ in_array($attribute->getFrontendInput(), array('text', 'select', 'multiline', 'textarea', 'price', 'date', 'multiselect')) &&
58
+ ($label = $attribute->getAttributeCode()) &&
59
+ ($value = $attribute->getFrontend()->getValue($original))
60
  ) {
 
 
61
  $data[$label] = $value;
62
  }
63
  }
64
+
65
+ $this->_attributes = $data;
66
 
 
67
  return $this;
68
  }
69
 
70
  /**
71
  * The name of this product
72
+ *
73
+ * @return integer
74
  */
75
  public function name()
76
  {
77
+ return $this->_name;
78
  }
79
 
80
  /**
81
  * Return an assoc array with attributes.
82
  *
83
+ * @param Bool $useAttribcode
84
+ * @return array
85
  */
86
  public function attributes($useAttribCode = false)
87
  {
88
+ return $this->_attributes;
89
  }
90
 
91
  /**
92
  * Return a string representation
93
+ *
94
+ * @return string
95
  */
96
  public function __toString()
97
  {
98
  $options = "";
99
+
100
+ foreach ($this->attributes() as $key => $value) {
101
+ $options .= "$key: $value\n";
102
+ }
103
+
104
  return $options;
105
  }
106
 
107
  /**
108
  * Serialize the object
109
+ *
110
+ * @return string
111
  */
112
  public function serialize()
113
  {
 
114
  return serialize(array(
115
  $this->name(),
116
  $this->attributes(),
119
 
120
  /**
121
  * Unserialize the object
122
+ *
123
+ * @param string $string
124
+ * @return Copernica_MarketingSoftware_Model_Abstraction_Attributes
125
  */
126
  public function unserialize($string)
127
  {
128
  list(
129
+ $this->_name,
130
+ $this->_attributes
131
  ) = unserialize($string);
132
+
133
  return $this;
134
  }
135
  }
app/code/community/Copernica/MarketingSoftware/Model/Abstraction/Customer.php CHANGED
@@ -32,272 +32,278 @@ class Copernica_MarketingSoftware_Model_Abstraction_Customer implements Serializ
32
  {
33
  /**
34
  * The id
35
- * @var int
 
36
  */
37
- protected $id;
38
 
39
  /**
40
  * The original object
41
- * @var Mage_Customer_Model_Customer
 
42
  */
43
- protected $original;
44
-
45
 
46
  /**
47
  * Sets the original model
48
- * @param Mage_Customer_Model_Customer $original
49
- * @return Copernica_MarketingSoftware_Model_Abstraction_Customer
 
50
  */
51
  public function setOriginal(Mage_Customer_Model_Customer $original)
52
  {
53
- // we will store only the Id
54
- $this->original = $original;
55
- $this->id = $original->getId();
56
 
57
- // allow chaining
58
  return $this;
59
  }
60
 
61
  /**
62
  * Returns the original model
63
- * @return Mage_Customer_Model_Customer
 
64
  */
65
- protected function original()
66
  {
67
- return $this->original;
68
  }
69
 
70
  /**
71
  * Loads a customer model
72
- * @param int $customerId
73
- * @return self
 
74
  */
75
  public function loadCustomer($customerId)
76
  {
77
- // load customer using magento accessor
78
  $customer = Mage::getModel('customer/customer')->load($customerId);
79
 
80
- // if we have a proper customer we can set it as original object
81
- if ($customer->getId()) $this->setOriginal($customer);
 
82
 
83
- // allow chaining
84
  return $this;
85
  }
86
 
87
  /**
88
  * Return the id of the customer
89
- * @return string
 
90
  */
91
  public function id()
92
  {
93
- /*
94
- * Why? Cause even when we do have id, but we don't have original that
95
- * id does not matter. Mostly cause there is no object that will be accessible
96
- * under that id. So at such point, id is just some number that does not
97
- * matter for us at all.
98
- */
99
- if (!$this->original()) return null;
100
-
101
- // return id of original object
102
- return $this->original()->getId();
103
  }
104
 
105
  /**
106
  * Return the name of this customer
107
  * Note that null may also be returned to indicate that the name is not known
108
- * @return Copernica_MarketingSoftware_Model_Abstraction_Name
 
109
  */
110
  public function name()
111
  {
112
- // get the data
113
- return Mage::getModel('marketingsoftware/abstraction_name')->setOriginal($this->original());
114
  }
115
 
116
  /**
117
  * Return the e-mail address of the customer
118
- * @return string
 
119
  */
120
  public function email()
121
  {
122
- // fetch data
123
- return $this->original()->getEmail();
124
  }
125
 
126
  /**
127
  * Return a customer's date of birth
128
- * @return string
 
129
  */
130
  public function birthDate()
131
  {
132
- // Return the birthdate
133
- return $this->original()->getDob();
134
  }
135
 
136
  /**
137
  * Method to retrieve the previous email if possible
138
  * Falls back on self::email()
139
- *
140
- * @return string
141
  */
142
  public function oldEmail()
143
  {
144
- // try to get old email
145
- $oldEmail = $this->original()->getOrigData('email');
146
 
147
- // check if we have old email
148
- if (isset($oldEmail)) return $oldEmail;
 
149
 
150
- // return current email
151
  return $this->email();
152
  }
153
 
154
  /**
155
  * Returns the gender
156
- * @return string
 
157
  */
158
  public function gender()
159
  {
160
- // get original object
161
- $original = $this->original();
162
 
163
- // get gender options
164
  $options = $original->getAttribute('gender')->getSource()->getAllOptions();
165
 
166
- // iterater over all options to get proper gender
167
  foreach ($options as $option) {
168
  if ($option['value'] == $original->getGender()) {
169
  return $option['label'];
170
  }
171
  }
172
 
173
- // we don't know
174
  return 'unknown';
175
  }
176
 
177
  /**
178
  * Return the subscription of the customer
179
- * @return Copernica_MarketingSoftware_Model_Abstraction_Subscription
 
180
  */
181
  public function subscription()
182
  {
183
- // get subscriber moder
184
  $subscriber = Mage::getModel('newsletter/subscriber');
185
 
186
- // check if we have a subscriber
187
- if (!$subscriber->loadByCustomer($this->original())->getId()) return null;
 
188
 
189
- // do subscriber belongs to store ?
190
- if ($subscriber->getStoreId() !== $this->original()->getStoreId()) return null;
 
191
 
192
- // return the subscription object
193
  return Mage::getModel('marketingsoftware/abstraction_subscription')->setOriginal($subscriber);
194
  }
195
 
196
  /**
197
  * Return the group to which this customer belongs
198
- * @return string
 
199
  */
200
  public function group()
201
  {
202
- // fetch customer group
203
- return Mage::getModel('customer/group')->load($this->original()->getGroupId())->getCode();
204
  }
205
 
206
  /**
207
  * Get the quotes for this customer
208
- * @return array of Copernica_MarketingSoftware_Model_Abstraction_Quote
 
209
  */
210
  public function quotes()
211
  {
212
- // placeholder for result data
213
  $data = array();
214
 
215
- //retrieve this customer's quote ids
216
  $quoteIds = Mage::getResourceModel('sales/quote_collection')
217
  ->addFieldToFilter('customer_id', $this->id())->getAllIds();
218
 
219
- // create data to return
220
- foreach ($quoteIds as $id) $data[] = Mage::getModel('marketingsoftware/abstraction_quote')->loadQuote($id);
 
221
 
222
- // return data
223
  return $data;
224
  }
225
 
226
  /**
227
  * Get the orders for this customer
228
- * @return array of Copernica_MarketingSoftware_Model_Abstraction_Order
 
229
  */
230
  public function orders()
231
  {
232
- // placeholder for data to return
233
  $data = array();
234
 
235
- //retrieve this customer's order ids
236
  $orderIds = Mage::getResourceModel('sales/order_collection')
237
  ->addAttributeToFilter('customer_id', $this->id())->getAllIds();
238
 
239
- // create data
240
- foreach ($orderIds as $id) $data[] = Mage::getModel('marketingsoftware/abstraction_order')->loadOrder($id);
 
241
 
242
- // return data
243
  return $data;
244
  }
245
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
246
  /**
247
  * Get the addresses for this customer
248
- * @return array of Copernica_MarketingSoftware_Model_Abstraction_Address
 
249
  */
250
  public function addresses()
251
  {
252
- // placeholder for result data
253
  $data = array();
254
 
255
- //retrieve this customer's addresses
256
- $addresses = $this->original()->getAddressesCollection();
257
 
258
- // make customer copernica address instance
259
  foreach ($addresses as $address) {
260
  $data[] = Mage::getModel('marketingsoftware/abstraction_address')->setOriginal($address);
261
  }
262
 
263
- // return addresses
264
  return $data;
265
  }
266
 
267
  /**
268
  * To what storeview does this order belong
269
- * @return Copernica_MarketingSoftware_Model_Abstraction_Storeview
 
270
  */
271
  public function storeview()
272
- {
273
- // fetch store view
274
- return Mage::getModel('marketingsoftware/abstraction_storeview')->setOriginal($this->original()->getStore());
275
  }
276
 
277
  /**
278
  * Serialize the object
279
- * @return string
 
280
  */
281
  public function serialize()
282
  {
283
- // serialize the data
284
- return serialize(array( $this->id() ));
285
  }
286
 
287
  /**
288
  * Unserialize the object
289
- * @param string
290
- * @return Copernica_MarketingSoftware_Model_Abstraction_Customer
 
291
  */
292
  public function unserialize($string)
293
  {
294
- // assign the data to the internal vars
295
- list( $id ) = unserialize($string);
296
 
297
- // load customer from database
298
  $this->loadCustomer($id);
299
 
300
- // allow chaining
301
  return $this;
302
  }
303
  }
32
  {
33
  /**
34
  * The id
35
+ *
36
+ * @var int
37
  */
38
+ protected $_id;
39
 
40
  /**
41
  * The original object
42
+ *
43
+ * @var Mage_Customer_Model_Customer
44
  */
45
+ protected $_original;
 
46
 
47
  /**
48
  * Sets the original model
49
+ *
50
+ * @param Mage_Customer_Model_Customer $original
51
+ * @return Copernica_MarketingSoftware_Model_Abstraction_Customer
52
  */
53
  public function setOriginal(Mage_Customer_Model_Customer $original)
54
  {
55
+ $this->_original = $original;
56
+ $this->_id = $original->getId();
 
57
 
 
58
  return $this;
59
  }
60
 
61
  /**
62
  * Returns the original model
63
+ *
64
+ * @return Mage_Customer_Model_Customer
65
  */
66
+ protected function _original()
67
  {
68
+ return $this->_original;
69
  }
70
 
71
  /**
72
  * Loads a customer model
73
+ *
74
+ * @param int $customerId
75
+ * @return Copernica_MarketingSoftware_Model_Abstraction_Customer
76
  */
77
  public function loadCustomer($customerId)
78
  {
 
79
  $customer = Mage::getModel('customer/customer')->load($customerId);
80
 
81
+ if ($customer->getId()) {
82
+ $this->setOriginal($customer);
83
+ }
84
 
 
85
  return $this;
86
  }
87
 
88
  /**
89
  * Return the id of the customer
90
+ *
91
+ * @return string
92
  */
93
  public function id()
94
  {
95
+ if (!$this->_original()) {
96
+ return null;
97
+ }
98
+
99
+ return $this->_original()->getId();
 
 
 
 
 
100
  }
101
 
102
  /**
103
  * Return the name of this customer
104
  * Note that null may also be returned to indicate that the name is not known
105
+ *
106
+ * @return Copernica_MarketingSoftware_Model_Abstraction_Name
107
  */
108
  public function name()
109
  {
110
+ return Mage::getModel('marketingsoftware/abstraction_name')->setOriginal($this->_original());
 
111
  }
112
 
113
  /**
114
  * Return the e-mail address of the customer
115
+ *
116
+ * @return string
117
  */
118
  public function email()
119
  {
120
+ return $this->_original()->getEmail();
 
121
  }
122
 
123
  /**
124
  * Return a customer's date of birth
125
+ *
126
+ * @return string
127
  */
128
  public function birthDate()
129
  {
130
+ return $this->_original()->getDob();
 
131
  }
132
 
133
  /**
134
  * Method to retrieve the previous email if possible
135
  * Falls back on self::email()
136
+ *
137
+ * @return string
138
  */
139
  public function oldEmail()
140
  {
141
+ $oldEmail = $this->_original()->getOrigData('email');
 
142
 
143
+ if (isset($oldEmail)) {
144
+ return $oldEmail;
145
+ }
146
 
 
147
  return $this->email();
148
  }
149
 
150
  /**
151
  * Returns the gender
152
+ *
153
+ * @return string
154
  */
155
  public function gender()
156
  {
157
+ $original = $this->_original();
 
158
 
 
159
  $options = $original->getAttribute('gender')->getSource()->getAllOptions();
160
 
 
161
  foreach ($options as $option) {
162
  if ($option['value'] == $original->getGender()) {
163
  return $option['label'];
164
  }
165
  }
166
 
 
167
  return 'unknown';
168
  }
169
 
170
  /**
171
  * Return the subscription of the customer
172
+ *
173
+ * @return Copernica_MarketingSoftware_Model_Abstraction_Subscription
174
  */
175
  public function subscription()
176
  {
 
177
  $subscriber = Mage::getModel('newsletter/subscriber');
178
 
179
+ if (!$subscriber->loadByCustomer($this->_original())->getId()) {
180
+ return null;
181
+ }
182
 
183
+ if ($subscriber->getStoreId() !== $this->_original()->getStoreId()) {
184
+ return null;
185
+ }
186
 
 
187
  return Mage::getModel('marketingsoftware/abstraction_subscription')->setOriginal($subscriber);
188
  }
189
 
190
  /**
191
  * Return the group to which this customer belongs
192
+ *
193
+ * @return string
194
  */
195
  public function group()
196
  {
197
+ return Mage::getModel('customer/group')->load($this->_original()->getGroupId())->getCode();
 
198
  }
199
 
200
  /**
201
  * Get the quotes for this customer
202
+ *
203
+ * @return array
204
  */
205
  public function quotes()
206
  {
 
207
  $data = array();
208
 
 
209
  $quoteIds = Mage::getResourceModel('sales/quote_collection')
210
  ->addFieldToFilter('customer_id', $this->id())->getAllIds();
211
 
212
+ foreach ($quoteIds as $id) {
213
+ $data[] = Mage::getModel('marketingsoftware/abstraction_quote')->loadQuote($id);
214
+ }
215
 
 
216
  return $data;
217
  }
218
 
219
  /**
220
  * Get the orders for this customer
221
+ *
222
+ * @return array
223
  */
224
  public function orders()
225
  {
 
226
  $data = array();
227
 
 
228
  $orderIds = Mage::getResourceModel('sales/order_collection')
229
  ->addAttributeToFilter('customer_id', $this->id())->getAllIds();
230
 
231
+ foreach ($orderIds as $id) {
232
+ $data[] = Mage::getModel('marketingsoftware/abstraction_order')->loadOrder($id);
233
+ }
234
 
 
235
  return $data;
236
  }
237
 
238
+ /**
239
+ * Get the wishlist for this customer
240
+ *
241
+ * @return array
242
+ */
243
+ public function wishlist()
244
+ {
245
+ $data = array();
246
+
247
+ $wishlistIds = Mage::getResourceModel('wishlist/wishlist_collection')
248
+ ->addAttributeToFilter('customer_id', $this->id())->getAllIds();
249
+
250
+ foreach ($wishlistIds as $id) {
251
+ $data[] = Mage::getModel('marketingsoftware/abstraction_wishlist')->loadWishlist($id);
252
+ }
253
+
254
+ return $data;
255
+ }
256
+
257
  /**
258
  * Get the addresses for this customer
259
+ *
260
+ * @return array
261
  */
262
  public function addresses()
263
  {
 
264
  $data = array();
265
 
266
+ $addresses = $this->_original()->getAddressesCollection();
 
267
 
 
268
  foreach ($addresses as $address) {
269
  $data[] = Mage::getModel('marketingsoftware/abstraction_address')->setOriginal($address);
270
  }
271
 
 
272
  return $data;
273
  }
274
 
275
  /**
276
  * To what storeview does this order belong
277
+ *
278
+ * @return Copernica_MarketingSoftware_Model_Abstraction_Storeview
279
  */
280
  public function storeview()
281
+ {
282
+ return Mage::getModel('marketingsoftware/abstraction_storeview')->setOriginal($this->_original()->getStore());
 
283
  }
284
 
285
  /**
286
  * Serialize the object
287
+ *
288
+ * @return string
289
  */
290
  public function serialize()
291
  {
292
+ return serialize(array($this->id()));
 
293
  }
294
 
295
  /**
296
  * Unserialize the object
297
+ *
298
+ * @param string $string
299
+ * @return Copernica_MarketingSoftware_Model_Abstraction_Customer
300
  */
301
  public function unserialize($string)
302
  {
303
+ list($id) = unserialize($string);
 
304
 
 
305
  $this->loadCustomer($id);
306
 
 
307
  return $this;
308
  }
309
  }
app/code/community/Copernica/MarketingSoftware/Model/Abstraction/Name.php CHANGED
@@ -32,71 +32,76 @@ class Copernica_MarketingSoftware_Model_Abstraction_Name implements Serializable
32
  /**
33
  * Predefine the internal fields
34
  */
35
- protected $firstname;
36
- protected $prefix;
37
- protected $middlename;
38
- protected $lastname;
39
 
40
  /**
41
  * Sets the original model
42
- * @param Mage_Customer_Model_Customer|Mage_Customer_Model_Customer_Address|Mage_Sales_Model_Order_Address|Mage_Sales_Model_Quote_Address|Mage_Customer_Model_Address
43
- * @return Copernica_MarketingSoftware_Model_Abstraction_Name
 
44
  */
45
  public function setOriginal($original)
46
  {
47
- $this->firstname = $original->getFirstname();
48
- $this->prefix = $original->getPrefix();
49
- $this->middlename = $original->getMiddlename();
50
- $this->lastname = $original->getLastname();
51
 
52
  return $this;
53
  }
54
 
55
  /**
56
  * Return the firstname of the customer
57
- * @return string
 
58
  */
59
  public function firstname()
60
  {
61
- return $this->firstname;
62
  }
63
 
64
  /**
65
  * Return the prefix of the customer
66
  * NOTE: the prefix field is not displayed by default
67
- * @return string
 
68
  */
69
  public function prefix()
70
  {
71
- return $this->prefix;
72
  }
73
 
74
  /**
75
  * Return the middlename of the customer
76
  * NOTE: the middlename field is not displayed by default
77
- * @return string
 
78
  */
79
  public function middlename()
80
  {
81
- return $this->middlename;
82
  }
83
 
84
  /**
85
  * Return the lastname of the customer
86
- * @return string
 
87
  */
88
  public function lastname()
89
  {
90
- return $this->lastname;
91
  }
92
 
93
  /**
94
  * Serialize the object
95
- * @return string
 
96
  */
97
  public function serialize()
98
  {
99
- // serialize the data
100
  return serialize(array(
101
  $this->firstname(),
102
  $this->prefix(),
@@ -107,17 +112,20 @@ class Copernica_MarketingSoftware_Model_Abstraction_Name implements Serializable
107
 
108
  /**
109
  * Unserialize the object
110
- * @param string
111
- * @return Copernica_MarketingSoftware_Model_Abstraction_Name
 
112
  */
113
  public function unserialize($string)
114
  {
115
  list(
116
- $this->firstname,
117
- $this->prefix,
118
- $this->middlename,
119
- $this->lastname
120
  ) = unserialize($string);
 
 
121
  }
122
  }
123
 
32
  /**
33
  * Predefine the internal fields
34
  */
35
+ protected $_firstname;
36
+ protected $_prefix;
37
+ protected $_middlename;
38
+ protected $_lastname;
39
 
40
  /**
41
  * Sets the original model
42
+ *
43
+ * @param Mage_Customer_Model_Customer|Mage_Customer_Model_Customer_Address|Mage_Sales_Model_Order_Address|Mage_Sales_Model_Quote_Address|Mage_Customer_Model_Address
44
+ * @return Copernica_MarketingSoftware_Model_Abstraction_Name
45
  */
46
  public function setOriginal($original)
47
  {
48
+ $this->_firstname = $original->getFirstname();
49
+ $this->_prefix = $original->getPrefix();
50
+ $this->_middlename = $original->getMiddlename();
51
+ $this->_lastname = $original->getLastname();
52
 
53
  return $this;
54
  }
55
 
56
  /**
57
  * Return the firstname of the customer
58
+ *
59
+ * @return string
60
  */
61
  public function firstname()
62
  {
63
+ return $this->_firstname;
64
  }
65
 
66
  /**
67
  * Return the prefix of the customer
68
  * NOTE: the prefix field is not displayed by default
69
+ *
70
+ * @return string
71
  */
72
  public function prefix()
73
  {
74
+ return $this->_prefix;
75
  }
76
 
77
  /**
78
  * Return the middlename of the customer
79
  * NOTE: the middlename field is not displayed by default
80
+ *
81
+ * @return string
82
  */
83
  public function middlename()
84
  {
85
+ return $this->_middlename;
86
  }
87
 
88
  /**
89
  * Return the lastname of the customer
90
+ *
91
+ * @return string
92
  */
93
  public function lastname()
94
  {
95
+ return $this->_lastname;
96
  }
97
 
98
  /**
99
  * Serialize the object
100
+ *
101
+ * @return string
102
  */
103
  public function serialize()
104
  {
 
105
  return serialize(array(
106
  $this->firstname(),
107
  $this->prefix(),
112
 
113
  /**
114
  * Unserialize the object
115
+ *
116
+ * @param string $string
117
+ * @return Copernica_MarketingSoftware_Model_Abstraction_Name
118
  */
119
  public function unserialize($string)
120
  {
121
  list(
122
+ $this->_firstname,
123
+ $this->_prefix,
124
+ $this->_middlename,
125
+ $this->_lastname
126
  ) = unserialize($string);
127
+
128
+ return $this;
129
  }
130
  }
131
 
app/code/community/Copernica/MarketingSoftware/Model/Abstraction/Order.php CHANGED
@@ -34,232 +34,258 @@ class Copernica_MarketingSoftware_Model_Abstraction_Order implements Serializabl
34
  * payment name from a quote shipping address. Since this is an order
35
  * a quote is no longer available.
36
  *
37
- * @var string
38
  */
39
  const PAYMENT_METHOD_KLARNA = 'klarna_partpayment';
40
 
41
  /**
42
  * Predefine the internal fields
43
  */
44
- protected $id;
45
- protected $incrementId;
46
- protected $quoteId;
47
- protected $quantity;
48
- protected $currency;
49
- protected $timestamp;
50
- protected $customerIP;
51
- protected $items;
52
- protected $storeview;
53
- protected $customerId;
54
- protected $addresses;
55
- protected $price;
56
- protected $weight;
57
- protected $state;
58
- protected $status;
59
- protected $shippingDescription;
60
- protected $paymentDescription;
 
 
 
 
 
 
 
61
 
62
  /**
63
  * Sets the original model
64
- * @param Mage_Sales_Model_Order $original
65
- * @return Copernica_MarketingSoftware_Model_Abstraction_Order
 
66
  */
67
  public function setOriginal(Mage_Sales_Model_Order $original)
68
  {
69
- // set only the Id
70
- $this->id = $original->getId();
71
 
72
- // we are done here
73
  return $this;
74
 
75
  }
76
 
77
  /**
78
  * This method will set the state of this order from original magento order
79
- * @param Mage_Sales_Model_Order
 
 
80
  */
81
  public function importFromOriginal(Mage_Sales_Model_Order $original)
82
  {
83
- $this->id = $original->getId();
84
- $this->incrementId = $original->getIncrementId();
85
- $this->quoteId = $original->getQuoteId();
86
- $this->state = $original->getState();
87
- $this->status = $original->getStatus();
88
- $this->quantity = $original->getTotalQtyOrdered();
89
- $this->currency = $original->getOrderCurrencyCode();
90
- $this->price = Mage::getModel('marketingsoftware/abstraction_price')->setOriginal($original);
91
- $this->weight = $original->getWeight();
92
- $this->storeview = Mage::getModel('marketingsoftware/abstraction_storeview')->setOriginal($original->getStore());
93
- $this->timestamp = $original->getUpdatedAt();
94
- $this->shippingDescription = $original->getShippingDescription();
95
- $this->customerIP = $original->getRemoteIp();
96
 
97
  $data = array();
 
98
  $items = $original->getAllVisibleItems();
 
99
  foreach ($items as $item) {
100
  $data[] = Mage::getModel('marketingsoftware/abstraction_order_item')->setOriginal($item);
101
  }
102
- $this->items = $data;
 
103
 
104
- //the order model only returns a customer if it exists
105
  if ($customerId = $original->getCustomerId()) {
106
- $this->customerId = $customerId;
107
  }
108
 
109
  $data = array();
110
- //retrieve this quote's addresses
111
  $addresses = $original->getAddressesCollection();
 
112
  foreach ($addresses as $address) {
113
  $data[] = Mage::getModel('marketingsoftware/abstraction_address')->setOriginal($address);
114
  }
115
- $this->addresses = $data;
 
116
 
117
  if ($payment = $original->getPayment()) {
118
  try {
119
  if ($payment->getMethod() == self::PAYMENT_METHOD_KLARNA) {
120
- $this->paymentDescription = 'Klarna';
121
  } else {
122
- $this->paymentDescription = $payment->getMethodInstance()->getTitle();
123
  }
124
  } catch (Mage_Core_Exception $exception) { }
125
  }
126
 
127
- // allow chaining
128
  return $this;
129
  }
130
 
131
  /**
132
  * Loads an order model
133
- * @param integer $orderId
134
- * @return Copernica_MarketingSoftware_Model_Abstraction_Order
 
135
  */
136
  public function loadOrder($orderId)
137
  {
138
  $order = Mage::getModel('sales/order')->load($orderId);
 
139
  if ($order->getId()) {
140
- //set the original model if the quote exists
141
  $this->importFromOriginal($order);
142
  }
 
143
  return $this;
144
  }
145
 
146
  /**
147
  * The id of this order object
148
- * @return integer
 
149
  */
150
  public function id()
151
  {
152
- return $this->id;
153
  }
154
 
155
  /**
156
  * The increment (longer) id of this order object
157
- * @return integer
 
158
  */
159
  public function incrementId()
160
  {
161
- return $this->incrementId;
162
  }
163
 
164
  /**
165
  * The quote id of this order object
166
- * @return integer
 
167
  */
168
  public function quoteId()
169
  {
170
- return $this->quoteId;
171
  }
172
 
173
  /**
174
  * The state of this order
175
- * @return string
 
176
  */
177
  public function state()
178
  {
179
- return $this->state;
180
  }
181
 
182
  /**
183
  * The status of this order
184
- * @return string
 
185
  */
186
  public function status()
187
  {
188
- return $this->status;
189
  }
190
 
191
  /**
192
  * The number of items present in this order
193
- * @return integer
 
194
  */
195
  public function quantity()
196
  {
197
- return $this->quantity;
198
  }
199
 
200
  /**
201
  * The number of items present in this order
202
- * @return integer
 
203
  */
204
  public function currency()
205
  {
206
- return $this->currency;
207
  }
208
 
209
  /**
210
  * The price
211
  * Note that an object is returned, which may consist of multiple components
212
- * @return Copernica_MarketingSoftware_Model_Abstraction_Price
 
213
  */
214
  public function price()
215
  {
216
- return $this->price;
217
  }
218
 
219
  /**
220
  * The weight
221
- * @return float
 
222
  */
223
  public function weight()
224
  {
225
- return $this->weight;
226
  }
227
 
228
  /**
229
  * To what storeview does this order belong
230
- * @return Copernica_MarketingSoftware_Model_Abstraction_Storeview
 
231
  */
232
  public function storeview()
233
  {
234
- return $this->storeview;
235
  }
236
 
237
  /**
238
  * Get the items from the order
239
- * @return array of Copernica_MarketingSoftware_Model_Abstraction_Quote_Item
 
240
  */
241
  public function items()
242
  {
243
- return $this->items;
244
  }
245
 
246
  /**
247
  * The timestamp at which this order was modified
248
- * @return string
 
249
  */
250
  public function timestamp()
251
  {
252
- return $this->timestamp;
253
  }
254
 
255
  /**
256
  * The customer may return null
257
- * @return Copernica_MarketingSoftware_Model_Abstraction_Customer
 
258
  */
259
  public function customer()
260
  {
261
- if ($this->customerId) {
262
- return Mage::getModel('marketingsoftware/abstraction_customer')->loadCustomer($this->customerId);
263
  } else {
264
  return null;
265
  }
@@ -267,65 +293,67 @@ class Copernica_MarketingSoftware_Model_Abstraction_Order implements Serializabl
267
 
268
  /**
269
  * The addresses of the order
270
- * @return array of Copernica_MarketingSoftware_Model_Abstraction_Address
 
271
  */
272
  public function addresses()
273
  {
274
- return $this->addresses;
275
  }
276
 
277
  /**
278
  * The shipping method of the order
279
- * @return string
 
280
  */
281
  public function shippingDescription()
282
  {
283
- return $this->shippingDescription;
284
  }
285
 
286
  /**
287
  * The payment method of the order
288
- * @return string
 
289
  */
290
  public function paymentDescription()
291
  {
292
- return $this->paymentDescription;
293
  }
294
 
295
  /**
296
  * The IP from which this order was constructed
297
- * @return string
 
298
  */
299
  public function customerIP()
300
  {
301
- return $this->customerIP;
302
  }
303
 
304
  /**
305
  * Serialize the object
306
- * @return string
 
307
  */
308
  public function serialize()
309
  {
310
- // serialize the data
311
- return serialize(array(
312
- $this->id()
313
- ));
314
  }
315
 
316
  /**
317
  * Unserialize the object
318
- * @param string
319
- * @return Copernica_MarketingSoftware_Model_Abstraction_Order
 
320
  */
321
  public function unserialize($string)
322
  {
323
  list(
324
- $this->id
325
  ) = unserialize($string);
326
 
327
- // load order by it's Id
328
- $this->loadOrder($this->id);
329
 
330
  return $this;
331
  }
34
  * payment name from a quote shipping address. Since this is an order
35
  * a quote is no longer available.
36
  *
37
+ * @var string
38
  */
39
  const PAYMENT_METHOD_KLARNA = 'klarna_partpayment';
40
 
41
  /**
42
  * Predefine the internal fields
43
  */
44
+ protected $_id;
45
+ protected $_incrementId;
46
+ protected $_quoteId;
47
+ protected $_quantity;
48
+ protected $_currency;
49
+ protected $_timestamp;
50
+ protected $_customerIP;
51
+ protected $_items;
52
+
53
+ /**
54
+ * The storeview object
55
+ *
56
+ * @var Copernica_MarketingSoftware_Model_Abstraction_Storeview
57
+ */
58
+ protected $_storeview;
59
+
60
+ protected $_customerId;
61
+ protected $_addresses;
62
+ protected $_price;
63
+ protected $_weight;
64
+ protected $_state;
65
+ protected $_status;
66
+ protected $_shippingDescription;
67
+ protected $_paymentDescription;
68
 
69
  /**
70
  * Sets the original model
71
+ *
72
+ * @param Mage_Sales_Model_Order $original
73
+ * @return Copernica_MarketingSoftware_Model_Abstraction_Order
74
  */
75
  public function setOriginal(Mage_Sales_Model_Order $original)
76
  {
77
+ $this->_id = $original->getId();
 
78
 
 
79
  return $this;
80
 
81
  }
82
 
83
  /**
84
  * This method will set the state of this order from original magento order
85
+ *
86
+ * @param Mage_Sales_Model_Order $original
87
+ * @return Copernica_MarketingSoftware_Model_Abstraction_Order
88
  */
89
  public function importFromOriginal(Mage_Sales_Model_Order $original)
90
  {
91
+ $this->_id = $original->getId();
92
+ $this->_incrementId = $original->getIncrementId();
93
+ $this->_quoteId = $original->getQuoteId();
94
+ $this->_state = $original->getState();
95
+ $this->_status = $original->getStatus();
96
+ $this->_quantity = $original->getTotalQtyOrdered();
97
+ $this->_currency = $original->getOrderCurrencyCode();
98
+ $this->_price = Mage::getModel('marketingsoftware/abstraction_price')->setOriginal($original);
99
+ $this->_weight = $original->getWeight();
100
+ $this->_storeview = Mage::getModel('marketingsoftware/abstraction_storeview')->setOriginal($original->getStore());
101
+ $this->_timestamp = $original->getUpdatedAt();
102
+ $this->_shippingDescription = $original->getShippingDescription();
103
+ $this->_customerIP = $original->getRemoteIp();
104
 
105
  $data = array();
106
+
107
  $items = $original->getAllVisibleItems();
108
+
109
  foreach ($items as $item) {
110
  $data[] = Mage::getModel('marketingsoftware/abstraction_order_item')->setOriginal($item);
111
  }
112
+
113
+ $this->_items = $data;
114
 
 
115
  if ($customerId = $original->getCustomerId()) {
116
+ $this->_customerId = $customerId;
117
  }
118
 
119
  $data = array();
120
+
121
  $addresses = $original->getAddressesCollection();
122
+
123
  foreach ($addresses as $address) {
124
  $data[] = Mage::getModel('marketingsoftware/abstraction_address')->setOriginal($address);
125
  }
126
+
127
+ $this->_addresses = $data;
128
 
129
  if ($payment = $original->getPayment()) {
130
  try {
131
  if ($payment->getMethod() == self::PAYMENT_METHOD_KLARNA) {
132
+ $this->_paymentDescription = 'Klarna';
133
  } else {
134
+ $this->_paymentDescription = $payment->getMethodInstance()->getTitle();
135
  }
136
  } catch (Mage_Core_Exception $exception) { }
137
  }
138
 
 
139
  return $this;
140
  }
141
 
142
  /**
143
  * Loads an order model
144
+ *
145
+ * @param integer $orderId
146
+ * @return Copernica_MarketingSoftware_Model_Abstraction_Order
147
  */
148
  public function loadOrder($orderId)
149
  {
150
  $order = Mage::getModel('sales/order')->load($orderId);
151
+
152
  if ($order->getId()) {
 
153
  $this->importFromOriginal($order);
154
  }
155
+
156
  return $this;
157
  }
158
 
159
  /**
160
  * The id of this order object
161
+ *
162
+ * @return integer
163
  */
164
  public function id()
165
  {
166
+ return $this->_id;
167
  }
168
 
169
  /**
170
  * The increment (longer) id of this order object
171
+ *
172
+ * @return integer
173
  */
174
  public function incrementId()
175
  {
176
+ return $this->_incrementId;
177
  }
178
 
179
  /**
180
  * The quote id of this order object
181
+ *
182
+ * @return integer
183
  */
184
  public function quoteId()
185
  {
186
+ return $this->_quoteId;
187
  }
188
 
189
  /**
190
  * The state of this order
191
+ *
192
+ * @return string
193
  */
194
  public function state()
195
  {
196
+ return $this->_state;
197
  }
198
 
199
  /**
200
  * The status of this order
201
+ *
202
+ * @return string
203
  */
204
  public function status()
205
  {
206
+ return $this->_status;
207
  }
208
 
209
  /**
210
  * The number of items present in this order
211
+ *
212
+ * @return integer
213
  */
214
  public function quantity()
215
  {
216
+ return $this->_quantity;
217
  }
218
 
219
  /**
220
  * The number of items present in this order
221
+ *
222
+ * @return integer
223
  */
224
  public function currency()
225
  {
226
+ return $this->_currency;
227
  }
228
 
229
  /**
230
  * The price
231
  * Note that an object is returned, which may consist of multiple components
232
+ *
233
+ * @return Copernica_MarketingSoftware_Model_Abstraction_Price
234
  */
235
  public function price()
236
  {
237
+ return $this->_price;
238
  }
239
 
240
  /**
241
  * The weight
242
+ *
243
+ * @return float
244
  */
245
  public function weight()
246
  {
247
+ return $this->_weight;
248
  }
249
 
250
  /**
251
  * To what storeview does this order belong
252
+ *
253
+ * @return Copernica_MarketingSoftware_Model_Abstraction_Storeview
254
  */
255
  public function storeview()
256
  {
257
+ return $this->_storeview;
258
  }
259
 
260
  /**
261
  * Get the items from the order
262
+ *
263
+ * @return array
264
  */
265
  public function items()
266
  {
267
+ return $this->_items;
268
  }
269
 
270
  /**
271
  * The timestamp at which this order was modified
272
+ *
273
+ * @return string
274
  */
275
  public function timestamp()
276
  {
277
+ return $this->_timestamp;
278
  }
279
 
280
  /**
281
  * The customer may return null
282
+ *
283
+ * @return Copernica_MarketingSoftware_Model_Abstraction_Customer|null
284
  */
285
  public function customer()
286
  {
287
+ if ($this->_customerId) {
288
+ return Mage::getModel('marketingsoftware/abstraction_customer')->loadCustomer($this->_customerId);
289
  } else {
290
  return null;
291
  }
293
 
294
  /**
295
  * The addresses of the order
296
+ *
297
+ * @return array of Copernica_MarketingSoftware_Model_Abstraction_Address
298
  */
299
  public function addresses()
300
  {
301
+ return $this->_addresses;
302
  }
303
 
304
  /**
305
  * The shipping method of the order
306
+ *
307
+ * @return string
308
  */
309
  public function shippingDescription()
310
  {
311
+ return $this->_shippingDescription;
312
  }
313
 
314
  /**
315
  * The payment method of the order
316
+ *
317
+ * @return string
318
  */
319
  public function paymentDescription()
320
  {
321
+ return $this->_paymentDescription;
322
  }
323
 
324
  /**
325
  * The IP from which this order was constructed
326
+ *
327
+ * @return string
328
  */
329
  public function customerIP()
330
  {
331
+ return $this->_customerIP;
332
  }
333
 
334
  /**
335
  * Serialize the object
336
+ *
337
+ * @return string
338
  */
339
  public function serialize()
340
  {
341
+ return serialize(array($this->id()));
 
 
 
342
  }
343
 
344
  /**
345
  * Unserialize the object
346
+ *
347
+ * @param string $string
348
+ * @return Copernica_MarketingSoftware_Model_Abstraction_Order
349
  */
350
  public function unserialize($string)
351
  {
352
  list(
353
+ $this->_id
354
  ) = unserialize($string);
355
 
356
+ $this->loadOrder($this->_id);
 
357
 
358
  return $this;
359
  }
app/code/community/Copernica/MarketingSoftware/Model/Abstraction/Order/Item.php CHANGED
@@ -32,34 +32,35 @@ class Copernica_MarketingSoftware_Model_Abstraction_Order_Item implements Serial
32
  /**
33
  * Predefine the internal fields
34
  */
35
- protected $id;
36
- protected $orderId;
37
- protected $quantity;
38
- protected $price;
39
- protected $weight;
40
- protected $timestamp;
41
- protected $options;
42
- protected $product;
43
 
44
  /**
45
  * Sets the original model
46
- * @param Mage_Sales_Model_Order_Item $original
47
- * @return Copernica_MarketingSoftware_Model_Abstraction_Order_Item
 
48
  */
49
  public function setOriginal(Mage_Sales_Model_Order_Item $original)
50
  {
51
- $this->id = $original->getId();
52
- $this->orderId = $original->getOrder()->getId();
53
- $this->quantity = $original->getQtyOrdered();
54
- $this->price = Mage::getModel('marketingsoftware/abstraction_price')->setOriginal($original);
55
- $this->weight = $original->getWeight();
56
- $this->timestamp = $original->getUpdatedAt();
57
- $this->product = Mage::getModel('marketingsoftware/abstraction_product')->setOriginal($original);
58
 
59
  $options = Mage::getModel('marketingsoftware/abstraction_order_item_options')->setOriginal($original);
 
60
  if ($options->attributes()) {
61
- //only return option object if it this order actually has options
62
- $this->options = $options;
63
  }
64
 
65
  return $this;
@@ -67,113 +68,123 @@ class Copernica_MarketingSoftware_Model_Abstraction_Order_Item implements Serial
67
 
68
  /**
69
  * The id of this order item object
70
- * @return integer
 
71
  */
72
  public function id()
73
  {
74
- return $this->id;
75
  }
76
 
77
  /**
78
  * Get the order to which this item belongs
79
- * @return Copernica_MarketingSoftware_Model_Abstraction_Order
 
80
  */
81
  public function order()
82
  {
83
- return Mage::getModel('marketingsoftware/abstraction_order')->loadOrder($this->orderId);
84
  }
85
 
86
  /**
87
  * The amount of this order item
88
- * @return integer
 
89
  */
90
  public function quantity()
91
  {
92
- return $this->quantity;
93
  }
94
 
95
  /**
96
  * The price
97
  * Note that an object is returned, which may consist of multiple components
98
- * @return Copernica_MarketingSoftware_Model_Abstraction_Price
 
99
  */
100
  public function price()
101
  {
102
- return $this->price;
103
  }
104
 
105
  /**
106
  * The weight
107
- * @return float
 
108
  */
109
  public function weight()
110
  {
111
- return $this->weight;
112
  }
113
 
114
  /**
115
  * The timestamp at which this order was modified
116
- * @return string
 
117
  */
118
  public function timestamp()
119
  {
120
- return $this->timestamp;
121
  }
122
 
123
  /**
124
  * Get the options of this order item
125
- * @return Copernica_MarketingSoftware_Model_Abstraction_Order_Item_Options
 
126
  */
127
  public function options()
128
  {
129
- return $this->options;
130
  }
131
 
132
  /**
133
  * Get the product which belongs to this item
134
- * @return Copernica_MarketingSoftware_Model_Abstraction_Product
 
135
  */
136
  public function product()
137
  {
138
- return $this->product;
139
  }
140
 
141
  /**
142
  * Serialize the object
143
- * @return string
 
144
  */
145
  public function serialize()
146
  {
147
- // serialize the data
148
  return serialize(array(
149
- $this->id(),
150
  is_object($order = $this->order()) ? $order->id() : null,
151
- $this->quantity(),
152
- $this->price(),
153
- $this->weight(),
154
- $this->timestamp(),
155
- $this->options(),
156
- $this->product(),
157
  ));
158
  }
159
 
160
  /**
161
  * Unserialize the object
162
- * @param string
163
- * @return Copernica_MarketingSoftware_Model_Abstraction_Order_Item
 
164
  */
165
  public function unserialize($string)
166
  {
167
  list(
168
- $this->id,
169
- $this->orderId,
170
- $this->quantity,
171
- $this->price,
172
- $this->weight,
173
- $this->timestamp,
174
- $this->options,
175
- $this->product
176
  ) = unserialize($string);
 
177
  return $this;
178
  }
179
  }
32
  /**
33
  * Predefine the internal fields
34
  */
35
+ protected $_id;
36
+ protected $_orderId;
37
+ protected $_quantity;
38
+ protected $_price;
39
+ protected $_weight;
40
+ protected $_timestamp;
41
+ protected $_options;
42
+ protected $_product;
43
 
44
  /**
45
  * Sets the original model
46
+ *
47
+ * @param Mage_Sales_Model_Order_Item $original
48
+ * @return Copernica_MarketingSoftware_Model_Abstraction_Order_Item
49
  */
50
  public function setOriginal(Mage_Sales_Model_Order_Item $original)
51
  {
52
+ $this->_id = $original->getId();
53
+ $this->_orderId = $original->getOrder()->getId();
54
+ $this->_quantity = $original->getQtyOrdered();
55
+ $this->_price = Mage::getModel('marketingsoftware/abstraction_price')->setOriginal($original);
56
+ $this->_weight = $original->getWeight();
57
+ $this->_timestamp = $original->getUpdatedAt();
58
+ $this->_product = Mage::getModel('marketingsoftware/abstraction_product')->setOriginal($original);
59
 
60
  $options = Mage::getModel('marketingsoftware/abstraction_order_item_options')->setOriginal($original);
61
+
62
  if ($options->attributes()) {
63
+ $this->_options = $options;
 
64
  }
65
 
66
  return $this;
68
 
69
  /**
70
  * The id of this order item object
71
+ *
72
+ * @return integer
73
  */
74
  public function id()
75
  {
76
+ return $this->_id;
77
  }
78
 
79
  /**
80
  * Get the order to which this item belongs
81
+ *
82
+ * @return Copernica_MarketingSoftware_Model_Abstraction_Order
83
  */
84
  public function order()
85
  {
86
+ return Mage::getModel('marketingsoftware/abstraction_order')->loadOrder($this->_orderId);
87
  }
88
 
89
  /**
90
  * The amount of this order item
91
+ *
92
+ * @return integer
93
  */
94
  public function quantity()
95
  {
96
+ return $this->_quantity;
97
  }
98
 
99
  /**
100
  * The price
101
  * Note that an object is returned, which may consist of multiple components
102
+ *
103
+ * @return Copernica_MarketingSoftware_Model_Abstraction_Price
104
  */
105
  public function price()
106
  {
107
+ return $this->_price;
108
  }
109
 
110
  /**
111
  * The weight
112
+ *
113
+ * @return float
114
  */
115
  public function weight()
116
  {
117
+ return $this->_weight;
118
  }
119
 
120
  /**
121
  * The timestamp at which this order was modified
122
+ *
123
+ * @return string
124
  */
125
  public function timestamp()
126
  {
127
+ return $this->_timestamp;
128
  }
129
 
130
  /**
131
  * Get the options of this order item
132
+ *
133
+ * @return Copernica_MarketingSoftware_Model_Abstraction_Order_Item_Options
134
  */
135
  public function options()
136
  {
137
+ return $this->_options;
138
  }
139
 
140
  /**
141
  * Get the product which belongs to this item
142
+ *
143
+ * @return Copernica_MarketingSoftware_Model_Abstraction_Product
144
  */
145
  public function product()
146
  {
147
+ return $this->_product;
148
  }
149
 
150
  /**
151
  * Serialize the object
152
+ *
153
+ * @return string
154
  */
155
  public function serialize()
156
  {
 
157
  return serialize(array(
158
+ $this->_id(),
159
  is_object($order = $this->order()) ? $order->id() : null,
160
+ $this->_quantity(),
161
+ $this->_price(),
162
+ $this->_weight(),
163
+ $this->_timestamp(),
164
+ $this->_options(),
165
+ $this->_product(),
166
  ));
167
  }
168
 
169
  /**
170
  * Unserialize the object
171
+ *
172
+ * @param string $string
173
+ * @return Copernica_MarketingSoftware_Model_Abstraction_Order_Item
174
  */
175
  public function unserialize($string)
176
  {
177
  list(
178
+ $this->_id,
179
+ $this->_orderId,
180
+ $this->_quantity,
181
+ $this->_price,
182
+ $this->_weight,
183
+ $this->_timestamp,
184
+ $this->_options,
185
+ $this->_product
186
  ) = unserialize($string);
187
+
188
  return $this;
189
  }
190
  }
app/code/community/Copernica/MarketingSoftware/Model/Abstraction/Order/Item/Options.php CHANGED
@@ -32,31 +32,29 @@ class Copernica_MarketingSoftware_Model_Abstraction_Order_Item_Options implement
32
  /**
33
  * Predefine the internal fields
34
  */
35
- protected $name;
36
- protected $attributes = null;
37
 
38
 
39
  /**
40
  * Sets the original model
41
- * @param Mage_Sales_Model_Order_Item $original
42
- * @return Copernica_MarketingSoftware_Model_Abstraction_Order_Item_Options
 
43
  */
44
  public function setOriginal(Mage_Sales_Model_Order_Item $original)
45
  {
46
- $this->name = $original->getName();
47
 
48
  $attributes = array();
49
  $data = array();
50
  $options = $original->getProductOptions();
51
 
52
  if (isset($options['attributes_info'])) {
53
- //configurable products
54
  $attributes = $options['attributes_info'];
55
  } elseif (isset($options['bundle_options'])) {
56
- //bundle products
57
  $attributes = $options['bundle_options'];
58
  } elseif (isset($options['options'])) {
59
- //generic products
60
  $attributes = $options['options'];
61
  }
62
 
@@ -64,7 +62,8 @@ class Copernica_MarketingSoftware_Model_Abstraction_Order_Item_Options implement
64
  foreach ($attributes as $attribute) {
65
  $data[$attribute['label']] = $attribute['value'];
66
  }
67
- $this->attributes = $data;
 
68
  }
69
 
70
  return $this;
@@ -72,79 +71,86 @@ class Copernica_MarketingSoftware_Model_Abstraction_Order_Item_Options implement
72
 
73
  /**
74
  * The name of this order item
75
- * @return integer
 
76
  */
77
  public function name()
78
  {
79
- return $this->name;
80
  }
81
 
82
  /**
83
  * Return an assoc array with attributes
84
- * @return array
 
85
  */
86
  public function attributes()
87
  {
88
- return $this->attributes;
89
  }
90
 
91
  /**
92
  * Return a string representation
93
- * @return String
 
94
  */
95
  public function __toString()
96
  {
97
- return $this->arrayToString($this->attributes());
98
  }
99
 
100
  /**
101
  * Return a string representation of an array
102
- * @param array
103
- * @return String
 
 
104
  */
105
- protected function arrayToString($value, $prefix = '')
106
  {
107
  $string = "";
108
- foreach ($value as $key => $value)
109
- {
110
- // is the value an array
111
- if (is_array($value))
112
- {
113
- // if there is only one subvalue, use that instead
114
- if (isset($value[0]) && count($value) == 1) $value = $value[0];
115
 
116
- // compose the string
117
- $string .= $prefix.$key.":\n".$this->arrayToString($value, $prefix.' ');
 
118
  }
119
- else $string.= $prefix.$key.": $value\n";
120
  }
 
121
  return $string;
122
  }
123
 
124
  /**
125
  * Serialize the object
126
- * @return string
 
127
  */
128
  public function serialize()
129
  {
130
- // serialize the data
131
  return serialize(array(
132
- $this->name(),
133
- $this->attributes(),
134
  ));
135
  }
136
 
137
  /**
138
  * Unserialize the object
139
- * @param string
140
- * @return Copernica_MarketingSoftware_Model_Abstraction_Order_Item_Options
 
141
  */
142
  public function unserialize($string)
143
  {
144
  list(
145
- $this->name,
146
- $this->attributes
147
  ) = unserialize($string);
 
148
  return $this;
149
  }
150
  }
32
  /**
33
  * Predefine the internal fields
34
  */
35
+ protected $_name;
36
+ protected $_attributes = null;
37
 
38
 
39
  /**
40
  * Sets the original model
41
+ *
42
+ * @param Mage_Sales_Model_Order_Item $original
43
+ * @return Copernica_MarketingSoftware_Model_Abstraction_Order_Item_Options
44
  */
45
  public function setOriginal(Mage_Sales_Model_Order_Item $original)
46
  {
47
+ $this->_name = $original->getName();
48
 
49
  $attributes = array();
50
  $data = array();
51
  $options = $original->getProductOptions();
52
 
53
  if (isset($options['attributes_info'])) {
 
54
  $attributes = $options['attributes_info'];
55
  } elseif (isset($options['bundle_options'])) {
 
56
  $attributes = $options['bundle_options'];
57
  } elseif (isset($options['options'])) {
 
58
  $attributes = $options['options'];
59
  }
60
 
62
  foreach ($attributes as $attribute) {
63
  $data[$attribute['label']] = $attribute['value'];
64
  }
65
+
66
+ $this->_attributes = $data;
67
  }
68
 
69
  return $this;
71
 
72
  /**
73
  * The name of this order item
74
+ *
75
+ * @return integer
76
  */
77
  public function name()
78
  {
79
+ return $this->_name;
80
  }
81
 
82
  /**
83
  * Return an assoc array with attributes
84
+ *
85
+ * @return array
86
  */
87
  public function attributes()
88
  {
89
+ return $this->_attributes;
90
  }
91
 
92
  /**
93
  * Return a string representation
94
+ *
95
+ * @return string
96
  */
97
  public function __toString()
98
  {
99
+ return $this->_arrayToString($this->_attributes());
100
  }
101
 
102
  /**
103
  * Return a string representation of an array
104
+ *
105
+ * @param array $value
106
+ * @param string $prefix
107
+ * @return string
108
  */
109
+ protected function _arrayToString($value, $prefix = '')
110
  {
111
  $string = "";
112
+
113
+ foreach ($value as $key => $value) {
114
+ if (is_array($value)) {
115
+ if (isset($value[0]) && count($value) == 1) {
116
+ $value = $value[0];
117
+ }
 
118
 
119
+ $string .= $prefix.$key.":\n".$this->_arrayToString($value, $prefix.' ');
120
+ } else {
121
+ $string.= $prefix.$key.": $value\n";
122
  }
 
123
  }
124
+
125
  return $string;
126
  }
127
 
128
  /**
129
  * Serialize the object
130
+ *
131
+ * @return string
132
  */
133
  public function serialize()
134
  {
 
135
  return serialize(array(
136
+ $this->_name(),
137
+ $this->_attributes(),
138
  ));
139
  }
140
 
141
  /**
142
  * Unserialize the object
143
+ *
144
+ * @param string $string
145
+ * @return Copernica_MarketingSoftware_Model_Abstraction_Order_Item_Options
146
  */
147
  public function unserialize($string)
148
  {
149
  list(
150
+ $this->_name,
151
+ $this->_attributes
152
  ) = unserialize($string);
153
+
154
  return $this;
155
  }
156
  }
app/code/community/Copernica/MarketingSoftware/Model/Abstraction/Price.php CHANGED
@@ -40,175 +40,177 @@ class Copernica_MarketingSoftware_Model_Abstraction_Price implements Serializabl
40
  /**
41
  * Predefine the internal fields
42
  */
43
- protected $total;
44
- protected $costs;
45
- protected $itemPrice;
46
- protected $originalPrice;
47
- protected $discount;
48
- protected $tax;
49
- protected $shipping;
50
- protected $currency;
51
 
52
  /**
53
  * Sets the original model
54
- * @param Mage_Sales_Model_Quote|Mage_Sales_Model_Order|Mage_Sales_Model_Quote_Item|Mage_Sales_Model_Order_Item $original
55
- * @return Copernica_MarketingSoftware_Model_Abstraction_Price
 
56
  */
57
  public function setOriginal($original)
58
  {
59
  if ($grandTotal = $original->getGrandTotal()) {
60
- $this->total = $grandTotal;
61
  } elseif ($rowTotalInclTax = $original->getRowTotalInclTax()) {
62
- $this->total = $rowTotalInclTax;
63
  } elseif ($baseRowTotal = $original->getBaseRowTotal()) {
64
- $this->total = $baseRowTotal;
65
  }
66
 
67
- // Used for quotes and orders
68
  if ($original instanceOf Mage_Sales_Model_Quote || $original instanceOf Mage_Sales_Model_Order) {
69
  $costs = 0;
70
 
71
- // iterate over all visisble items
72
  foreach ($original->getAllVisibleItems() as $item) {
73
  $costs += $item->getBaseCost();
74
  }
75
 
76
- // return the costs
77
- $this->costs = $costs;
78
  } elseif ($baseCost = $original->getBaseCost()) {
79
- $this->costs = $baseCost;
80
  }
81
 
82
- // no price for an individual item
83
- if ($original instanceOf Mage_Sales_Model_Quote || $original instanceOf Mage_Sales_Model_Order)
84
- $this->itemPrice = 0;
85
-
86
- // Used for quote items and order items
87
- elseif ($price = $original->getPrice())
88
- $this->itemPrice = $price;
89
 
90
- // no price for an individual item
91
- if ($original instanceOf Mage_Sales_Model_Quote || $original instanceOf Mage_Sales_Model_Order)
92
- $this->originalPrice = 0;
93
-
94
- // it's not safe to call ::getOriginalPrice() on quote item
95
- elseif ($original instanceOf Mage_Sales_Model_Quote_Item)
96
- $this->originalPrice = 0;
97
-
98
- // Used for quote items and order items
99
- elseif ($originalPrice = $original->getOriginalPrice())
100
- $this->originalPrice = $originalPrice;
101
 
102
- if ($discountAmount = $original->getDiscountAmount())
103
- $this->discount = $discountAmount;
 
104
 
105
- if ($taxAmount = $original->getTaxAmount())
106
- $this->tax = $taxAmount;
 
107
 
108
- // Shipping is only available for quotes and orders, but not for items
109
- if ($original instanceOf Mage_Sales_Model_Quote || $original instanceOf Mage_Sales_Model_Order)
110
- {
111
- // Get the shipping amount
112
- if ($shippingAmount = $original->getShippingAmount())
113
- $this->shipping = $shippingAmount;
114
- else
115
- $this->shipping = 0;
116
  }
117
- else $this->shipping = 0;
118
 
119
- if ($currency = $original->getOrderCurrencyCode())
120
- $this->currency = $currency;
121
- elseif ($currency = $original->getQuoteCurrencyCode())
122
- $this->currency = $currency;
123
- elseif (($order = $original->getOrder()) && ($currency = $order->getOrderCurrencyCode()))
124
- $this->currency = $currency;
125
- elseif (($quote = $original->getQuote()) && ($currency = $quote->getQuoteCurrencyCode()))
126
- $this->currency = $currency;
127
- else
128
- $this->currency = '';
 
129
 
130
  return $this;
131
  }
132
 
133
  /**
134
  * Return the total price
135
- * @return float
 
136
  */
137
  public function total()
138
  {
139
- return $this->total;
140
  }
141
 
142
  /**
143
  * Return the price for the individual item
144
- * @return float
 
145
  */
146
  public function costs()
147
  {
148
- return $this->costs;
149
  }
150
 
151
  /**
152
  * Return the price for the individual item
153
- * @return float
 
154
  */
155
  public function itemPrice()
156
  {
157
- return $this->itemPrice;
158
  }
159
 
160
  /**
161
  * Return the original price for the individual item
162
- * @return float
 
163
  */
164
  public function originalPrice()
165
  {
166
- return $this->originalPrice;
167
  }
168
 
169
  /**
170
  * Return the discount which was given
171
- * @return float
 
172
  */
173
  public function discount()
174
  {
175
- return $this->discount;
176
  }
177
 
178
  /**
179
  * Return the tax which was paid
180
- * @return float
 
181
  */
182
  public function tax()
183
  {
184
- return $this->tax;
185
  }
186
 
187
  /**
188
  * Return the shipping costs
189
- * @return float
 
190
  */
191
  public function shipping()
192
  {
193
- return $this->shipping;
194
  }
195
 
196
  /**
197
  * Return the currency code
198
- * @return float
 
199
  */
200
  public function currency()
201
  {
202
- return $this->currency;
203
  }
204
 
205
  /**
206
  * Serialize the object
207
- * @return string
 
208
  */
209
  public function serialize()
210
  {
211
- // serialize the data
212
  return serialize(array(
213
  $this->total(),
214
  $this->costs(),
@@ -223,21 +225,24 @@ class Copernica_MarketingSoftware_Model_Abstraction_Price implements Serializabl
223
 
224
  /**
225
  * Unserialize the object
226
- * @param string
227
- * @return Copernica_MarketingSoftware_Model_Abstraction_Price
 
228
  */
229
  public function unserialize($string)
230
  {
231
  list(
232
- $this->total,
233
- $this->costs,
234
- $this->itemPrice,
235
- $this->originalPrice,
236
- $this->discount,
237
- $this->tax,
238
- $this->shipping,
239
- $this->currency
240
  ) = unserialize($string);
 
 
241
  }
242
  }
243
 
40
  /**
41
  * Predefine the internal fields
42
  */
43
+ protected $_total;
44
+ protected $_costs;
45
+ protected $_itemPrice;
46
+ protected $_originalPrice;
47
+ protected $_discount;
48
+ protected $_tax;
49
+ protected $_shipping;
50
+ protected $_currency;
51
 
52
  /**
53
  * Sets the original model
54
+ *
55
+ * @param Mage_Sales_Model_Quote|Mage_Sales_Model_Order|Mage_Sales_Model_Quote_Item|Mage_Sales_Model_Order_Item $original
56
+ * @return Copernica_MarketingSoftware_Model_Abstraction_Price
57
  */
58
  public function setOriginal($original)
59
  {
60
  if ($grandTotal = $original->getGrandTotal()) {
61
+ $this->_total = $grandTotal;
62
  } elseif ($rowTotalInclTax = $original->getRowTotalInclTax()) {
63
+ $this->_total = $rowTotalInclTax;
64
  } elseif ($baseRowTotal = $original->getBaseRowTotal()) {
65
+ $this->_total = $baseRowTotal;
66
  }
67
 
 
68
  if ($original instanceOf Mage_Sales_Model_Quote || $original instanceOf Mage_Sales_Model_Order) {
69
  $costs = 0;
70
 
 
71
  foreach ($original->getAllVisibleItems() as $item) {
72
  $costs += $item->getBaseCost();
73
  }
74
 
75
+ $this->_costs = $costs;
 
76
  } elseif ($baseCost = $original->getBaseCost()) {
77
+ $this->_costs = $baseCost;
78
  }
79
 
80
+ if ($original instanceOf Mage_Sales_Model_Quote || $original instanceOf Mage_Sales_Model_Order) {
81
+ $this->_itemPrice = 0;
82
+ } elseif ($price = $original->getPrice()) {
83
+ $this->_itemPrice = $price;
84
+ }
 
 
85
 
86
+ if ($original instanceOf Mage_Sales_Model_Quote || $original instanceOf Mage_Sales_Model_Order) {
87
+ $this->_originalPrice = 0;
88
+ } elseif ($original instanceOf Mage_Sales_Model_Quote_Item) {
89
+ $this->_originalPrice = 0;
90
+ } elseif ($originalPrice = $original->getOriginalPrice()) {
91
+ $this->_originalPrice = $originalPrice;
92
+ }
 
 
 
 
93
 
94
+ if ($discountAmount = $original->getDiscountAmount()) {
95
+ $this->_discount = $discountAmount;
96
+ }
97
 
98
+ if ($taxAmount = $original->getTaxAmount()) {
99
+ $this->_tax = $taxAmount;
100
+ }
101
 
102
+ if ($original instanceOf Mage_Sales_Model_Quote || $original instanceOf Mage_Sales_Model_Order) {
103
+ if ($shippingAmount = $original->getShippingAmount()) {
104
+ $this->_shipping = $shippingAmount;
105
+ } else {
106
+ $this->_shipping = 0;
107
+ }
108
+ } else {
109
+ $this->_shipping = 0;
110
  }
 
111
 
112
+ if ($currency = $original->getOrderCurrencyCode()) {
113
+ $this->_currency = $currency;
114
+ } elseif ($currency = $original->getQuoteCurrencyCode()) {
115
+ $this->_currency = $currency;
116
+ } elseif (($order = $original->getOrder()) && ($currency = $order->getOrderCurrencyCode())) {
117
+ $this->_currency = $currency;
118
+ } elseif (($quote = $original->getQuote()) && ($currency = $quote->getQuoteCurrencyCode())) {
119
+ $this->_currency = $currency;
120
+ } else {
121
+ $this->_currency = '';
122
+ }
123
 
124
  return $this;
125
  }
126
 
127
  /**
128
  * Return the total price
129
+ *
130
+ * @return float
131
  */
132
  public function total()
133
  {
134
+ return $this->_total;
135
  }
136
 
137
  /**
138
  * Return the price for the individual item
139
+ *
140
+ * @return float
141
  */
142
  public function costs()
143
  {
144
+ return $this->_costs;
145
  }
146
 
147
  /**
148
  * Return the price for the individual item
149
+ *
150
+ * @return float
151
  */
152
  public function itemPrice()
153
  {
154
+ return $this->_itemPrice;
155
  }
156
 
157
  /**
158
  * Return the original price for the individual item
159
+ *
160
+ * @return float
161
  */
162
  public function originalPrice()
163
  {
164
+ return $this->_originalPrice;
165
  }
166
 
167
  /**
168
  * Return the discount which was given
169
+ *
170
+ * @return float
171
  */
172
  public function discount()
173
  {
174
+ return $this->_discount;
175
  }
176
 
177
  /**
178
  * Return the tax which was paid
179
+ *
180
+ * @return float
181
  */
182
  public function tax()
183
  {
184
+ return $this->_tax;
185
  }
186
 
187
  /**
188
  * Return the shipping costs
189
+ *
190
+ * @return float
191
  */
192
  public function shipping()
193
  {
194
+ return $this->_shipping;
195
  }
196
 
197
  /**
198
  * Return the currency code
199
+ *
200
+ * @return float
201
  */
202
  public function currency()
203
  {
204
+ return $this->_currency;
205
  }
206
 
207
  /**
208
  * Serialize the object
209
+ *
210
+ * @return string
211
  */
212
  public function serialize()
213
  {
 
214
  return serialize(array(
215
  $this->total(),
216
  $this->costs(),
225
 
226
  /**
227
  * Unserialize the object
228
+ *
229
+ * @param string $string
230
+ * @return Copernica_MarketingSoftware_Model_Abstraction_Price
231
  */
232
  public function unserialize($string)
233
  {
234
  list(
235
+ $this->_total,
236
+ $this->_costs,
237
+ $this->_itemPrice,
238
+ $this->_originalPrice,
239
+ $this->_discount,
240
+ $this->_tax,
241
+ $this->_shipping,
242
+ $this->_currency
243
  ) = unserialize($string);
244
+
245
+ return $this;
246
  }
247
  }
248
 
app/code/community/Copernica/MarketingSoftware/Model/Abstraction/Product.php CHANGED
@@ -31,58 +31,51 @@ class Copernica_MarketingSoftware_Model_Abstraction_Product implements Serializa
31
  {
32
  /**
33
  * The original object
34
- * @param Mage_Catalog_Model_Product
 
 
35
  */
36
- protected $original;
37
 
38
  /**
39
  * Predefine the internal fields
40
  */
41
- protected $id;
42
- protected $sku;
43
- protected $name;
44
- protected $description;
45
- protected $productUrl = '';
46
- protected $imagePath = '';
47
- protected $weight;
48
- protected $categories = array();
49
- protected $price;
50
- protected $created = '';
51
- protected $modified = '';
52
- protected $attributes = '';
53
- protected $attributeSet = '';
54
 
55
  /**
56
  * Sets the original model
57
- * @param Mage_Catalog_Model_Product|Mage_Sales_Model_Quote_Item|Mage_Sales_Model_Order_Item $original
58
- * @return Copernica_MarketingSoftware_Model_Abstraction_Product
 
59
  */
60
  public function setOriginal($original)
61
  {
62
- // if original object is an instance of catalog product model we can create
63
- // our product from that instance
64
- if ($original instanceof Mage_Catalog_Model_Product) $this->createFromProductModel($original);
65
- else {
66
- // try to get product instance
67
  $product = Mage::getModel('catalog/product')->load($original->getProductId());
68
 
69
- // check if product still exists
70
- if ($product->getID()) $this->createFromProductModel($product);
71
-
72
- /*
73
- * Now we have a situation when Item still exists (or we have data from it),
74
- * but we don't have Product model. We can not really get product
75
- * model from thin air, but we have some information about it that,
76
- * we can use.
77
- */
78
- else
79
- {
80
- $this->id = $original->getProductId();
81
- $this->sku = $original->getSKU();
82
- $this->name = $original->getName();
83
- $this->description = $original->getDescription();
84
- $this->weight = $original->getWeight();
85
- $this->price = $original->getPrice();
86
  }
87
  }
88
 
@@ -91,251 +84,250 @@ class Copernica_MarketingSoftware_Model_Abstraction_Product implements Serializa
91
 
92
  /**
93
  * Create product from model.
94
- * @param Mage_Catalog_Model_Product
 
95
  */
96
- private function createFromProductModel(Mage_Catalog_Model_Product $model)
97
  {
98
- // get basic properties
99
- $this->id = $model->getId();
100
- $this->sku = $model->getSku();
101
- $this->name = $model->getName();
102
- $this->description = $model->getShortDescription();
103
- $this->price = $model->getPrice();
104
- $this->created = $model->getCreatedAt();
105
- $this->modified = $model->getUpdatedAt();
106
- $this->productUrl = $model->getProductUrl();
107
- $this->imagePath = $model->getImageUrl();
108
- $this->weight = $model->getWeight();
109
 
110
  $data = array();
 
111
  $categoryIds = $model->getCategoryIds();
 
112
  foreach ($categoryIds as $categoryId) {
113
  $category = Mage::getModel('catalog/category')->load($categoryId);
 
114
  $data[] = $this->_getFullCategoryName($category);
115
  }
116
- $this->categories = $data;
117
 
118
- $this->attributes = Mage::getModel('marketingsoftware/abstraction_attributes')->setOriginal($model);
 
 
119
 
120
  $attributeSetModel = Mage::getModel("eav/entity_attribute_set");
121
  $attributeSetModel->load($model->getAttributeSetId());
122
 
123
- $this->attributeSet = $attributeSetModel->getAttributeSetName();
124
 
125
  $this->timestamp = time();
126
  }
127
 
128
  /**
129
  * Loads a product model
130
- * @param integer $productId
131
- * @return Copernica_MarketingSoftware_Model_Abstraction_Product
 
132
  */
133
  public function loadProduct($productId)
134
  {
135
  $product = Mage::getModel('catalog/product')->load($productId);
 
136
  if ($product->getId()) {
137
- //set the original model if the product exists
138
  $this->setOriginal($product);
 
 
139
  }
140
- else
141
- {
142
- $this->id = $productId;
143
- }
144
  return $this;
145
  }
146
 
147
  /**
148
  * Return the identifier for this object
149
- * @return integer
 
150
  */
151
  public function id()
152
  {
153
- return $this->id;
154
  }
155
 
156
  /**
157
  * Return the sku (stock keeping unit), which is an unique identifier
158
  * for a magento product
159
- * @return string
 
160
  */
161
  public function sku()
162
  {
163
- return $this->sku;
164
  }
165
 
166
  /**
167
  * Return the name of this magento product
168
- * @return string
 
169
  */
170
  public function name()
171
  {
172
- return $this->name;
173
  }
174
 
175
  /**
176
  * Return the description of this magento product
177
- * @return string
 
178
  */
179
  public function description()
180
  {
181
- return $this->description;
182
  }
183
 
184
  /**
185
  * Return the price of this magento product
186
- * @return string
 
187
  */
188
  public function price()
189
  {
190
- return $this->price;
191
  }
192
 
193
  /**
194
  * Return the creation date of this magento product
195
- * @return string
 
196
  */
197
  public function created()
198
  {
199
- return $this->created;
200
  }
201
 
202
  /**
203
  * Return the modification date of this magento product
204
- * @return string
 
205
  */
206
  public function modified()
207
  {
208
- return $this->modified;
209
  }
210
 
211
  /**
212
  * Return the product url of this magento product
213
- * @param integer optional store id
214
- * @return string
 
215
  */
216
  public function productUrl($storeId = null)
217
  {
218
- return $this->productUrl;
219
  }
220
 
221
  /**
222
  * Return the image url of this magento product
223
- * @param integer|boolean optional store id, if false is given only the path is returned
224
- * @return string
 
225
  */
226
  public function imageUrl($storeId = null)
227
  {
228
- return $this->imagePath;
229
  }
230
 
231
  /**
232
  * Return the weight of this magento product
233
- * @return float
 
234
  */
235
  public function weight()
236
  {
237
- return $this->weight;
238
  }
239
 
240
  /**
241
  * Return the categories of this product
242
- * @return array of category ids to category names in a path from the root
 
243
  */
244
  public function categories()
245
  {
246
- return $this->categories;
247
  }
248
 
249
  /**
250
  * Return the flattened tree of the given category
251
- * @param Mage_Catalog_Model_Category $category
252
- * @return array
 
253
  */
254
  protected function _getFullCategoryName(Mage_Catalog_Model_Category $category)
255
  {
256
- // is there a parent?
257
- if ($category->getParentId() > 1)
258
- {
259
- // get the parent
260
  $parent = $category->getParentCategory();
 
261
  $data = $this->_getFullCategoryName($parent);
262
  } else {
263
  $data = array();
264
  }
265
 
266
- // append the current name
267
  $data[$category->getId()] = $category->getName();
268
 
269
- // return the data
270
  return $data;
271
  }
272
 
273
  /**
274
  * Return the attributes for this product
275
- * @return Copernica_MarketingSoftware_Model_Abstraction_Attributes
 
276
  */
277
  public function attributes()
278
  {
279
- return $this->attributes;
280
  }
281
 
282
  public function attributeSet()
283
  {
284
- return $this->attributeSet;
285
  }
286
 
287
  /**
288
  * Serialize the object
289
- * @todo This method is even used?
290
- * @return string
 
291
  */
292
  public function serialize()
293
  {
294
- // serialize the data
295
- return serialize(array(
296
- $this->id(),
297
- $this->sku(),
298
- $this->attributeSet(),
299
- $this->name(),
300
- $this->description(),
301
- $this->productUrl(),
302
- $this->imageUrl(false), // gets the image path if store 'false' is supplied
303
- $this->weight(),
304
- $this->categories(),
305
- false, // old isNew
306
- $this->price(),
307
- '',
308
- $this->created(),
309
- $this->modified(),
310
- $this->attributes(),
311
- ));
312
  }
313
 
314
  /**
315
  * Unserialize the object
316
- * @todo This method is even used?
317
- * @param string
318
- * @return Copernica_MarketingSoftware_Model_Abstraction_Product
 
319
  */
320
  public function unserialize($string)
321
  {
322
  list(
323
- $this->id,
324
- $this->sku,
325
- $this->attributeSet,
326
- $this->name,
327
- $this->description,
328
- $this->productUrl,
329
- $this->imagePath,
330
- $this->weight,
331
- $this->categories,
332
  $isNew,
333
- $this->price,
334
  $specialPrice,
335
- $this->created,
336
- $this->modified,
337
- $this->attributes
338
  ) = unserialize($string);
 
339
  return $this;
340
  }
341
  }
31
  {
32
  /**
33
  * The original object
34
+ *
35
+ * @todo Not used???
36
+ * @var Mage_Catalog_Model_Product|Mage_Sales_Model_Quote_Item|Mage_Sales_Model_Order_Item
37
  */
38
+ protected $_original;
39
 
40
  /**
41
  * Predefine the internal fields
42
  */
43
+ protected $_id;
44
+ protected $_sku;
45
+ protected $_name;
46
+ protected $_description;
47
+ protected $_productUrl = '';
48
+ protected $_imagePath = '';
49
+ protected $_weight;
50
+ protected $_categories = array();
51
+ protected $_price;
52
+ protected $_created = '';
53
+ protected $_modified = '';
54
+ protected $_attributes = '';
55
+ protected $_attributeSet = '';
56
 
57
  /**
58
  * Sets the original model
59
+ *
60
+ * @param Mage_Catalog_Model_Product|Mage_Sales_Model_Quote_Item|Mage_Sales_Model_Order_Item $original
61
+ * @return Copernica_MarketingSoftware_Model_Abstraction_Product
62
  */
63
  public function setOriginal($original)
64
  {
65
+ if ($original instanceof Mage_Catalog_Model_Product) {
66
+ $this->_createFromProductModel($original);
67
+ } else {
 
 
68
  $product = Mage::getModel('catalog/product')->load($original->getProductId());
69
 
70
+ if ($product->getID()) {
71
+ $this->_createFromProductModel($product);
72
+ } else {
73
+ $this->_id = $original->getProductId();
74
+ $this->_sku = $original->getSKU();
75
+ $this->_name = $original->getName();
76
+ $this->_description = $original->getDescription();
77
+ $this->_weight = $original->getWeight();
78
+ $this->_price = $original->getPrice();
 
 
 
 
 
 
 
 
79
  }
80
  }
81
 
84
 
85
  /**
86
  * Create product from model.
87
+ *
88
+ * @param Mage_Catalog_Model_Product $model
89
  */
90
+ protected function _createFromProductModel(Mage_Catalog_Model_Product $model)
91
  {
92
+ $this->_id = $model->getId();
93
+ $this->_sku = $model->getSku();
94
+ $this->_name = $model->getName();
95
+ $this->_description = $model->getShortDescription();
96
+ $this->_price = $model->getPrice();
97
+ $this->_created = $model->getCreatedAt();
98
+ $this->_modified = $model->getUpdatedAt();
99
+ $this->_productUrl = $model->getProductUrl();
100
+ $this->_imagePath = $model->getImageUrl();
101
+ $this->_weight = $model->getWeight();
 
102
 
103
  $data = array();
104
+
105
  $categoryIds = $model->getCategoryIds();
106
+
107
  foreach ($categoryIds as $categoryId) {
108
  $category = Mage::getModel('catalog/category')->load($categoryId);
109
+
110
  $data[] = $this->_getFullCategoryName($category);
111
  }
 
112
 
113
+ $this->_categories = $data;
114
+
115
+ $this->_attributes = Mage::getModel('marketingsoftware/abstraction_attributes')->setOriginal($model);
116
 
117
  $attributeSetModel = Mage::getModel("eav/entity_attribute_set");
118
  $attributeSetModel->load($model->getAttributeSetId());
119
 
120
+ $this->_attributeSet = $attributeSetModel->getAttributeSetName();
121
 
122
  $this->timestamp = time();
123
  }
124
 
125
  /**
126
  * Loads a product model
127
+ *
128
+ * @param integer $productId
129
+ * @return Copernica_MarketingSoftware_Model_Abstraction_Product
130
  */
131
  public function loadProduct($productId)
132
  {
133
  $product = Mage::getModel('catalog/product')->load($productId);
134
+
135
  if ($product->getId()) {
 
136
  $this->setOriginal($product);
137
+ } else {
138
+ $this->_id = $productId;
139
  }
140
+
 
 
 
141
  return $this;
142
  }
143
 
144
  /**
145
  * Return the identifier for this object
146
+ *
147
+ * @return integer
148
  */
149
  public function id()
150
  {
151
+ return $this->_id;
152
  }
153
 
154
  /**
155
  * Return the sku (stock keeping unit), which is an unique identifier
156
  * for a magento product
157
+ *
158
+ * @return string
159
  */
160
  public function sku()
161
  {
162
+ return $this->_sku;
163
  }
164
 
165
  /**
166
  * Return the name of this magento product
167
+ *
168
+ * @return string
169
  */
170
  public function name()
171
  {
172
+ return $this->_name;
173
  }
174
 
175
  /**
176
  * Return the description of this magento product
177
+ *
178
+ * @return string
179
  */
180
  public function description()
181
  {
182
+ return $this->_description;
183
  }
184
 
185
  /**
186
  * Return the price of this magento product
187
+ *
188
+ * @return string
189
  */
190
  public function price()
191
  {
192
+ return $this->_price;
193
  }
194
 
195
  /**
196
  * Return the creation date of this magento product
197
+ *
198
+ * @return string
199
  */
200
  public function created()
201
  {
202
+ return $this->_created;
203
  }
204
 
205
  /**
206
  * Return the modification date of this magento product
207
+ *
208
+ * @return string
209
  */
210
  public function modified()
211
  {
212
+ return $this->_modified;
213
  }
214
 
215
  /**
216
  * Return the product url of this magento product
217
+ *
218
+ * @param integer $storeId
219
+ * @return string
220
  */
221
  public function productUrl($storeId = null)
222
  {
223
+ return $this->_productUrl;
224
  }
225
 
226
  /**
227
  * Return the image url of this magento product
228
+ *
229
+ * @param integer $storeId
230
+ * @return string
231
  */
232
  public function imageUrl($storeId = null)
233
  {
234
+ return $this->_imagePath;
235
  }
236
 
237
  /**
238
  * Return the weight of this magento product
239
+ *
240
+ * @return float
241
  */
242
  public function weight()
243
  {
244
+ return $this->_weight;
245
  }
246
 
247
  /**
248
  * Return the categories of this product
249
+ *
250
+ * @return array
251
  */
252
  public function categories()
253
  {
254
+ return $this->_categories;
255
  }
256
 
257
  /**
258
  * Return the flattened tree of the given category
259
+ *
260
+ * @param Mage_Catalog_Model_Category $category
261
+ * @return array
262
  */
263
  protected function _getFullCategoryName(Mage_Catalog_Model_Category $category)
264
  {
265
+ if ($category->getParentId() > 1) {
 
 
 
266
  $parent = $category->getParentCategory();
267
+
268
  $data = $this->_getFullCategoryName($parent);
269
  } else {
270
  $data = array();
271
  }
272
 
 
273
  $data[$category->getId()] = $category->getName();
274
 
 
275
  return $data;
276
  }
277
 
278
  /**
279
  * Return the attributes for this product
280
+ *
281
+ * @return Copernica_MarketingSoftware_Model_Abstraction_Attributes
282
  */
283
  public function attributes()
284
  {
285
+ return $this->_attributes;
286
  }
287
 
288
  public function attributeSet()
289
  {
290
+ return $this->_attributeSet;
291
  }
292
 
293
  /**
294
  * Serialize the object
295
+ *
296
+ * @todo This method, is it even used?
297
+ * @return string
298
  */
299
  public function serialize()
300
  {
301
+ return serialize(array($this->id()));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
302
  }
303
 
304
  /**
305
  * Unserialize the object
306
+ *
307
+ * @todo This method, is it even used? And $isNew???
308
+ * @param string $string
309
+ * @return Copernica_MarketingSoftware_Model_Abstraction_Product
310
  */
311
  public function unserialize($string)
312
  {
313
  list(
314
+ $this->_id,
315
+ $this->_sku,
316
+ $this->_attributeSet,
317
+ $this->_name,
318
+ $this->_description,
319
+ $this->_productUrl,
320
+ $this->_imagePath,
321
+ $this->_weight,
322
+ $this->_categories,
323
  $isNew,
324
+ $this->_price,
325
  $specialPrice,
326
+ $this->_created,
327
+ $this->_modified,
328
+ $this->_attributes
329
  ) = unserialize($string);
330
+
331
  return $this;
332
  }
333
  }
app/code/community/Copernica/MarketingSoftware/Model/Abstraction/{Viewedproduct.php → Product/Viewed.php} RENAMED
@@ -27,153 +27,169 @@
27
  /**
28
  * A wrapper object around a magento Product
29
  */
30
- class Copernica_MarketingSoftware_Model_Abstraction_Viewedproduct implements Serializable
31
  {
32
- /**
33
  * Predefine the internal fields
34
  */
35
  public $id;
36
- protected $sku;
37
- protected $name;
38
- protected $description;
39
- protected $productUrl;
40
- protected $imagePath;
41
- protected $weight;
42
- protected $categories = array();
43
- protected $isNew;
44
- protected $price;
45
- protected $specialPrice;
46
- protected $created;
47
- protected $modified;
48
- protected $attributes;
49
- protected $attributeSet;
50
  public $customerId;
51
- protected $timestamp;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
 
53
  /**
54
  * Sets the original model
55
- * @param Mage_Catalog_Model_Product|Mage_Sales_Model_Quote_Item|Mage_Sales_Model_Order_Item $original
56
- * @return Copernica_MarketingSoftware_Model_Abstraction_Product
 
57
  */
58
  public function setOriginal($original, $id)
59
  {
60
  if ($original instanceof Mage_Catalog_Model_Product) {
61
- //this is the original product
62
  $this->id = $original->getId();
63
- $this->sku = $original->getSku();
64
- $this->name = $original->getName();
65
- $this->description = $original->getShortDescription();
66
- $this->price = $original->getPrice();
67
- $this->specialPrice = $original->getSpecialPrice();
68
- $this->created = $original->getCreatedAt();
69
- $this->modified = $original->getUpdatedAt();
70
- $this->productUrl = $original->getProductUrl();
71
- $this->imagePath = 'catalog' . DS . 'product' . $original->getImage();
72
- $this->weight = $original->getWeight();
73
 
74
  $data = array();
 
75
  $categoryIds = $original->getCategoryIds();
 
76
  foreach ($categoryIds as $categoryId) {
77
  $category = Mage::getModel('catalog/category')->load($categoryId);
 
78
  $data[] = $this->_getFullCategoryName($category);
79
  }
80
- $this->categories = $data;
 
81
 
82
  $from = $original->getNewsFromDate() ? Mage::app()->getLocale()->date($original->getNewsFromDate()) : null;
83
  $to = $original->getNewsToDate() ? Mage::app()->getLocale()->date($original->getNewsToDate()) : null;
 
84
  if ($from || $to) {
85
  $new = true;
 
86
  $now = Zend_Date::now();
 
87
  if ($from) {
88
  $new = $new && $from->isEarlier($now);
89
  }
 
90
  if ($to) {
91
  $new = $new && $to->isLater($now);
92
  }
93
- $this->isNew = $new;
 
94
  } else {
95
- $this->isNew = false;
96
  }
97
 
98
- $this->attributes = Mage::getModel('marketingsoftware/abstraction_attributes')->setOriginal($original);
99
 
100
  $attributeSetModel = Mage::getModel("eav/entity_attribute_set");
101
  $attributeSetModel->load($original->getAttributeSetId());
102
 
103
- $this->attributeSet = $attributeSetModel->getAttributeSetName();
104
 
105
- $this->timestamp = time();
106
 
107
  $this->customerId = $id;
 
108
  $this->storeId = Mage::app()->getStore()->getStoreId();
109
 
110
  return $this;
111
  } else {
112
- //the quote item or order item has a product id
113
  $product = Mage::getModel('catalog/product')->load($original->getProductId());
 
114
  if ($product->getId()) {
115
- //the product exists
116
  $this->id = $product->getId();
117
- $this->sku = $product->getSku();
118
- $this->name = $product->getName();
119
- $this->description = $product->getShortDescription();
120
- $this->price = $product->getPrice();
121
- $this->specialPrice = $product->getSpecialPrice();
122
- $this->created = $product->getCreatedAt();
123
- $this->modified = $product->getUpdatedAt();
124
- $this->productUrl = $product->getProductUrl();
125
- $this->imagePath = 'catalog' . DS . 'product' . $product->getImage();
126
- $this->weight = $product->getWeight();
127
 
128
  $data = array();
 
129
  $categoryIds = $product->getCategoryIds();
 
130
  foreach ($categoryIds as $categoryId) {
131
  $category = Mage::getModel('catalog/category')->load($categoryId);
 
132
  $data[] = $this->_getFullCategoryName($category);
133
  }
134
- $this->categories = $data;
 
135
 
136
  $from = $product->getNewsFromDate() ? Mage::app()->getLocale()->date($product->getNewsFromDate()) : null;
137
  $to = $product->getNewsToDate() ? Mage::app()->getLocale()->date($product->getNewsToDate()) : null;
 
138
  if ($from || $to) {
139
  $new = true;
 
140
  $now = Zend_Date::now();
 
141
  if ($from) {
142
  $new = $new && $from->isEarlier($now);
143
  }
 
144
  if ($to) {
145
  $new = $new && $to->isLater($now);
146
  }
147
- $this->isNew = $new;
 
148
  } else {
149
- $this->isNew = false;
150
  }
151
 
152
- $this->attributes = Mage::getModel('marketingsoftware/abstraction_attributes')->setOriginal($product);
153
 
154
  $attributeSetModel = Mage::getModel("eav/entity_attribute_set");
155
  $attributeSetModel->load($product->getAttributeSetId());
156
 
157
- $this->attributeSet = $attributeSetModel->getAttributeSetName();
158
 
159
- $this->timestamp = time();
160
  } else {
161
- // unfortunately we do not have the product any more, but we have the information
162
- // so we can fill a lot of fields, so the functions still work
163
  $this->id = $original->getProductId();
164
- $this->sku = $original->getSKU();
165
- $this->attributeSet = '';
166
- $this->name = $original->getName();
167
- $this->description = $original->getDescription();
168
- $this->productUrl = '';
169
- $this->imagePath = '';
170
- $this->weight = $original->getWeight();
171
- $this->categories = array();
172
- $this->isNew = false;
173
- $this->price = $original->getPrice();
174
- $this->created = '';
175
- $this->modified = '';
176
- $this->attributes = '';
177
  $this->customerId = $id;
178
  $this->storeId = Mage::app()->getStore()->getStoreId();
179
  }
@@ -184,26 +200,27 @@ class Copernica_MarketingSoftware_Model_Abstraction_Viewedproduct implements Ser
184
 
185
  /**
186
  * Loads a product model
187
- * @param integer $productId
188
- * @return Copernica_MarketingSoftware_Model_Abstraction_Product
 
189
  */
190
  public function loadProduct($productId)
191
  {
192
  $product = Mage::getModel('catalog/product')->load($productId);
 
193
  if ($product->getId()) {
194
- //set the original model if the product exists
195
  $this->setOriginal($product);
196
- }
197
- else
198
- {
199
  $this->id = $productId;
200
  }
 
201
  return $this;
202
  }
203
 
204
  /**
205
  * Return the identifier for this object
206
- * @return integer
 
207
  */
208
  public function id()
209
  {
@@ -213,215 +230,189 @@ class Copernica_MarketingSoftware_Model_Abstraction_Viewedproduct implements Ser
213
  /**
214
  * Return the sku (stock keeping unit), which is an unique identifier
215
  * for a magento product
216
- * @return string
 
217
  */
218
  public function sku()
219
  {
220
- return $this->sku;
221
  }
222
 
223
  /**
224
  * Return the name of this magento product
225
- * @return string
 
226
  */
227
  public function name()
228
  {
229
- return $this->name;
230
  }
231
 
232
  /**
233
  * Return the description of this magento product
234
- * @return string
 
235
  */
236
  public function description()
237
  {
238
- return $this->description;
239
  }
240
 
241
  /**
242
  * Return the price of this magento product
243
- * @return string
 
244
  */
245
  public function price()
246
  {
247
- return $this->price;
248
  }
249
 
250
  /**
251
  * Return the price of this magento product
252
- * @return string
 
253
  */
254
  public function specialPrice()
255
  {
256
- return $this->specialPrice;
257
  }
258
 
259
 
260
  /**
261
  * Return the creation date of this magento product
262
- * @return string
 
263
  */
264
  public function created()
265
  {
266
- return $this->created;
267
  }
268
 
269
  /**
270
  * Return the modification date of this magento product
271
- * @return string
 
272
  */
273
  public function modified()
274
  {
275
- return $this->modified;
276
  }
277
 
278
  /**
279
  * Return the product url of this magento product
280
- * @param integer optional store id
281
- * @return string
 
282
  */
283
  public function productUrl($storeId = null)
284
  {
285
- return $this->productUrl;
286
  }
287
 
288
  /**
289
  * Return the image url of this magento product
290
- * @param integer|boolean optional store id, if false is given only the path is returned
291
- * @return string
 
292
  */
293
  public function imageUrl($storeId = null)
294
  {
295
- return $this->imagePath;
296
  }
297
 
298
  /**
299
  * Return the weight of this magento product
300
- * @return float
 
301
  */
302
  public function weight()
303
  {
304
- return $this->weight;
305
  }
306
 
307
  /**
308
  * Return the categories of this product
309
- * @return array of category ids to category names in a path from the root
 
310
  */
311
  public function categories()
312
  {
313
- return $this->categories;
314
  }
315
 
316
  /**
317
  * Return the flattened tree of the given category
318
- * @param Mage_Catalog_Model_Category $category
319
- * @return array
 
320
  */
321
  protected function _getFullCategoryName(Mage_Catalog_Model_Category $category)
322
  {
323
- // is there a parent?
324
- if ($category->getParentId() > 1)
325
- {
326
- // get the parent
327
  $parent = $category->getParentCategory();
 
328
  $data = $this->_getFullCategoryName($parent);
329
  } else {
330
  $data = array();
331
  }
332
 
333
- // append the current name
334
  $data[$category->getId()] = $category->getName();
335
 
336
- // return the data
337
  return $data;
338
  }
339
 
340
  /**
341
  * Return whether this product is new
342
- * @return boolean
 
343
  */
344
  public function isNew()
345
  {
346
- return $this->isNew;
347
  }
348
 
349
  /**
350
  * Return the attributes for this product
351
- * @return Copernica_MarketingSoftware_Model_Abstraction_Attributes
 
352
  */
353
  public function attributes()
354
  {
355
- return $this->attributes;
356
  }
357
 
358
  public function attributeSet()
359
  {
360
- return $this->attributeSet;
361
  }
362
 
363
  public function timestamp()
364
  {
365
- return $this->timestamp;
366
  }
367
 
368
 
369
  /**
370
  * Serialize the object
371
- * @return string
 
372
  */
373
  public function serialize()
374
  {
375
- // serialize the data
376
- return serialize(array(
377
- $this->id(),
378
- $this->sku(),
379
- $this->attributeSet(),
380
- $this->name(),
381
- $this->description(),
382
- $this->productUrl(),
383
- $this->imageUrl(false), // gets the image path if store 'false' is supplied
384
- $this->weight(),
385
- $this->categories(),
386
- $this->isNew(),
387
- $this->price(),
388
- $this->specialPrice(),
389
- $this->created(),
390
- $this->modified(),
391
- $this->attributes(),
392
- $this->customerId,
393
- $this->storeId,
394
- $this->timestamp()
395
- ));
396
  }
397
 
398
  /**
399
  * Unserialize the object
400
- * @param string
401
- * @return Copernica_MarketingSoftware_Model_Abstraction_Product
 
402
  */
403
  public function unserialize($string)
404
  {
405
- list(
406
- $this->id,
407
- $this->sku,
408
- $this->attributeSet,
409
- $this->name,
410
- $this->description,
411
- $this->productUrl,
412
- $this->imagePath,
413
- $this->weight,
414
- $this->categories,
415
- $this->isNew,
416
- $this->price,
417
- $this->specialPrice,
418
- $this->created,
419
- $this->modified,
420
- $this->attributes,
421
- $this->customerId,
422
- $this->storeId,
423
- $this->timestamp
424
- ) = unserialize($string);
425
  return $this;
426
  }
427
  }
27
  /**
28
  * A wrapper object around a magento Product
29
  */
30
+ class Copernica_MarketingSoftware_Model_Abstraction_Product_Viewed implements Serializable
31
  {
32
+ /**
33
  * Predefine the internal fields
34
  */
35
  public $id;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
  public $customerId;
37
+ protected $_sku;
38
+ protected $_name;
39
+ protected $_description;
40
+ protected $_productUrl;
41
+ protected $_imagePath;
42
+ protected $_weight;
43
+ protected $_categories = array();
44
+ protected $_isNew;
45
+ protected $_price;
46
+ protected $_specialPrice;
47
+ protected $_created;
48
+ protected $_modified;
49
+ protected $_attributes;
50
+ protected $_attributeSet;
51
+ protected $_timestamp;
52
 
53
  /**
54
  * Sets the original model
55
+ *
56
+ * @param Mage_Catalog_Model_Product|Mage_Sales_Model_Quote_Item|Mage_Sales_Model_Order_Item $original
57
+ * @return Copernica_MarketingSoftware_Model_Abstraction_Product
58
  */
59
  public function setOriginal($original, $id)
60
  {
61
  if ($original instanceof Mage_Catalog_Model_Product) {
 
62
  $this->id = $original->getId();
63
+ $this->_sku = $original->getSku();
64
+ $this->_name = $original->getName();
65
+ $this->_description = $original->getShortDescription();
66
+ $this->_price = $original->getPrice();
67
+ $this->_specialPrice = $original->getSpecialPrice();
68
+ $this->_created = $original->getCreatedAt();
69
+ $this->_modified = $original->getUpdatedAt();
70
+ $this->_productUrl = $original->getProductUrl();
71
+ $this->_imagePath = 'catalog' . DS . 'product' . $original->getImage();
72
+ $this->_weight = $original->getWeight();
73
 
74
  $data = array();
75
+
76
  $categoryIds = $original->getCategoryIds();
77
+
78
  foreach ($categoryIds as $categoryId) {
79
  $category = Mage::getModel('catalog/category')->load($categoryId);
80
+
81
  $data[] = $this->_getFullCategoryName($category);
82
  }
83
+
84
+ $this->_categories = $data;
85
 
86
  $from = $original->getNewsFromDate() ? Mage::app()->getLocale()->date($original->getNewsFromDate()) : null;
87
  $to = $original->getNewsToDate() ? Mage::app()->getLocale()->date($original->getNewsToDate()) : null;
88
+
89
  if ($from || $to) {
90
  $new = true;
91
+
92
  $now = Zend_Date::now();
93
+
94
  if ($from) {
95
  $new = $new && $from->isEarlier($now);
96
  }
97
+
98
  if ($to) {
99
  $new = $new && $to->isLater($now);
100
  }
101
+
102
+ $this->_isNew = $new;
103
  } else {
104
+ $this->_isNew = false;
105
  }
106
 
107
+ $this->_attributes = Mage::getModel('marketingsoftware/abstraction_attributes')->setOriginal($original);
108
 
109
  $attributeSetModel = Mage::getModel("eav/entity_attribute_set");
110
  $attributeSetModel->load($original->getAttributeSetId());
111
 
112
+ $this->_attributeSet = $attributeSetModel->getAttributeSetName();
113
 
114
+ $this->_timestamp = time();
115
 
116
  $this->customerId = $id;
117
+
118
  $this->storeId = Mage::app()->getStore()->getStoreId();
119
 
120
  return $this;
121
  } else {
 
122
  $product = Mage::getModel('catalog/product')->load($original->getProductId());
123
+
124
  if ($product->getId()) {
 
125
  $this->id = $product->getId();
126
+ $this->_sku = $product->getSku();
127
+ $this->_name = $product->getName();
128
+ $this->_description = $product->getShortDescription();
129
+ $this->_price = $product->getPrice();
130
+ $this->_specialPrice = $product->getSpecialPrice();
131
+ $this->_created = $product->getCreatedAt();
132
+ $this->_modified = $product->getUpdatedAt();
133
+ $this->_productUrl = $product->getProductUrl();
134
+ $this->_imagePath = 'catalog' . DS . 'product' . $product->getImage();
135
+ $this->_weight = $product->getWeight();
136
 
137
  $data = array();
138
+
139
  $categoryIds = $product->getCategoryIds();
140
+
141
  foreach ($categoryIds as $categoryId) {
142
  $category = Mage::getModel('catalog/category')->load($categoryId);
143
+
144
  $data[] = $this->_getFullCategoryName($category);
145
  }
146
+
147
+ $this->_categories = $data;
148
 
149
  $from = $product->getNewsFromDate() ? Mage::app()->getLocale()->date($product->getNewsFromDate()) : null;
150
  $to = $product->getNewsToDate() ? Mage::app()->getLocale()->date($product->getNewsToDate()) : null;
151
+
152
  if ($from || $to) {
153
  $new = true;
154
+
155
  $now = Zend_Date::now();
156
+
157
  if ($from) {
158
  $new = $new && $from->isEarlier($now);
159
  }
160
+
161
  if ($to) {
162
  $new = $new && $to->isLater($now);
163
  }
164
+
165
+ $this->_isNew = $new;
166
  } else {
167
+ $this->_isNew = false;
168
  }
169
 
170
+ $this->_attributes = Mage::getModel('marketingsoftware/abstraction_attributes')->setOriginal($product);
171
 
172
  $attributeSetModel = Mage::getModel("eav/entity_attribute_set");
173
  $attributeSetModel->load($product->getAttributeSetId());
174
 
175
+ $this->_attributeSet = $attributeSetModel->getAttributeSetName();
176
 
177
+ $this->_timestamp = time();
178
  } else {
 
 
179
  $this->id = $original->getProductId();
180
+ $this->_sku = $original->getSKU();
181
+ $this->_attributeSet = '';
182
+ $this->_name = $original->getName();
183
+ $this->_description = $original->getDescription();
184
+ $this->_productUrl = '';
185
+ $this->_imagePath = '';
186
+ $this->_weight = $original->getWeight();
187
+ $this->_categories = array();
188
+ $this->_isNew = false;
189
+ $this->_price = $original->getPrice();
190
+ $this->_created = '';
191
+ $this->_modified = '';
192
+ $this->_attributes = '';
193
  $this->customerId = $id;
194
  $this->storeId = Mage::app()->getStore()->getStoreId();
195
  }
200
 
201
  /**
202
  * Loads a product model
203
+ *
204
+ * @param integer $productId
205
+ * @return Copernica_MarketingSoftware_Model_Abstraction_Product
206
  */
207
  public function loadProduct($productId)
208
  {
209
  $product = Mage::getModel('catalog/product')->load($productId);
210
+
211
  if ($product->getId()) {
 
212
  $this->setOriginal($product);
213
+ } else {
 
 
214
  $this->id = $productId;
215
  }
216
+
217
  return $this;
218
  }
219
 
220
  /**
221
  * Return the identifier for this object
222
+ *
223
+ * @return integer
224
  */
225
  public function id()
226
  {
230
  /**
231
  * Return the sku (stock keeping unit), which is an unique identifier
232
  * for a magento product
233
+ *
234
+ * @return string
235
  */
236
  public function sku()
237
  {
238
+ return $this->_sku;
239
  }
240
 
241
  /**
242
  * Return the name of this magento product
243
+ *
244
+ * @return string
245
  */
246
  public function name()
247
  {
248
+ return $this->_name;
249
  }
250
 
251
  /**
252
  * Return the description of this magento product
253
+ *
254
+ * @return string
255
  */
256
  public function description()
257
  {
258
+ return $this->_description;
259
  }
260
 
261
  /**
262
  * Return the price of this magento product
263
+ *
264
+ * @return string
265
  */
266
  public function price()
267
  {
268
+ return $this->_price;
269
  }
270
 
271
  /**
272
  * Return the price of this magento product
273
+ *
274
+ * @return string
275
  */
276
  public function specialPrice()
277
  {
278
+ return $this->_specialPrice;
279
  }
280
 
281
 
282
  /**
283
  * Return the creation date of this magento product
284
+ *
285
+ * @return string
286
  */
287
  public function created()
288
  {
289
+ return $this->_created;
290
  }
291
 
292
  /**
293
  * Return the modification date of this magento product
294
+ *
295
+ * @return string
296
  */
297
  public function modified()
298
  {
299
+ return $this->_modified;
300
  }
301
 
302
  /**
303
  * Return the product url of this magento product
304
+ *
305
+ * @param integer $storeId
306
+ * @return string
307
  */
308
  public function productUrl($storeId = null)
309
  {
310
+ return $this->_productUrl;
311
  }
312
 
313
  /**
314
  * Return the image url of this magento product
315
+ *
316
+ * @param integer $storeId
317
+ * @return string
318
  */
319
  public function imageUrl($storeId = null)
320
  {
321
+ return $this->_imagePath;
322
  }
323
 
324
  /**
325
  * Return the weight of this magento product
326
+ *
327
+ * @return float
328
  */
329
  public function weight()
330
  {
331
+ return $this->_weight;
332
  }
333
 
334
  /**
335
  * Return the categories of this product
336
+ *
337
+ * @return array
338
  */
339
  public function categories()
340
  {
341
+ return $this->_categories;
342
  }
343
 
344
  /**
345
  * Return the flattened tree of the given category
346
+ *
347
+ * @param Mage_Catalog_Model_Category $category
348
+ * @return array
349
  */
350
  protected function _getFullCategoryName(Mage_Catalog_Model_Category $category)
351
  {
352
+ if ($category->getParentId() > 1) {
 
 
 
353
  $parent = $category->getParentCategory();
354
+
355
  $data = $this->_getFullCategoryName($parent);
356
  } else {
357
  $data = array();
358
  }
359
 
 
360
  $data[$category->getId()] = $category->getName();
361
 
 
362
  return $data;
363
  }
364
 
365
  /**
366
  * Return whether this product is new
367
+ *
368
+ * @return boolean
369
  */
370
  public function isNew()
371
  {
372
+ return $this->_isNew;
373
  }
374
 
375
  /**
376
  * Return the attributes for this product
377
+ *
378
+ * @return Copernica_MarketingSoftware_Model_Abstraction_Attributes
379
  */
380
  public function attributes()
381
  {
382
+ return $this->_attributes;
383
  }
384
 
385
  public function attributeSet()
386
  {
387
+ return $this->_attributeSet;
388
  }
389
 
390
  public function timestamp()
391
  {
392
+ return $this->_timestamp;
393
  }
394
 
395
 
396
  /**
397
  * Serialize the object
398
+ *
399
+ * @return string
400
  */
401
  public function serialize()
402
  {
403
+ return serialize(array($this->id()));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
404
  }
405
 
406
  /**
407
  * Unserialize the object
408
+ *
409
+ * @param string $string
410
+ * @return Copernica_MarketingSoftware_Model_Abstraction_Product
411
  */
412
  public function unserialize($string)
413
  {
414
+ list($this->id) = unserialize($string);
415
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
416
  return $this;
417
  }
418
  }
app/code/community/Copernica/MarketingSoftware/Model/Abstraction/Quote.php CHANGED
@@ -32,257 +32,279 @@ class Copernica_MarketingSoftware_Model_Abstraction_Quote implements Serializabl
32
  /**
33
  * Predefine the internal fields
34
  */
35
- protected $quoteId;
36
- protected $quantity;
37
- protected $currency;
38
- protected $timestamp;
39
- protected $customerIP;
40
- protected $items;
41
- protected $storeview;
42
- protected $customerId;
43
- protected $addresses;
44
- protected $price;
45
- protected $weight;
46
- protected $active;
47
- protected $shippingDescription;
48
- protected $paymentDescription;
 
 
 
 
 
 
 
49
 
50
  /**
51
  * Sets the original model
52
- * @param Mage_Sales_Model_Quote $original
53
- * @return Copernica_MarketingSoftware_Model_Abstraction_Quote
 
54
  */
55
  public function setOriginal(Mage_Sales_Model_Quote $original)
56
  {
57
- // store quote ID
58
- $this->quoteId = $original->getId();
59
 
60
- // we do not want to set whole class in this method
61
  return $this;
62
  }
63
 
64
  /**
65
  * Loads a quote model
66
- * @param integer $quoteId
67
- * @return Copernica_MarketingSoftware_Model_Abstraction_Quote
 
68
  */
69
  public function loadQuote($quoteId)
70
  {
71
- // Get the model
72
  $quote = Mage::getModel('sales/quote');
73
 
74
- // Difference between Magento 1.4 / 1.5
75
- if (!is_callable($quote, 'loadByIdWithoutStore'))
76
- {
77
- // construct an array with store ids
78
  $storeIDs = array();
79
- foreach (Mage::app()->getStores() as $id => $store) $storeIDs[] = $id;
 
 
 
80
 
81
- // The store ids are used for loading the quote, independant of the store
82
  $quote->setSharedStoreIds($storeIDs);
83
  $quote->load($quoteId);
 
 
84
  }
85
- else $quote->loadByIdWithoutStore($quoteId);
86
 
87
- // we did load a valid quote, set the original model
88
  if ($quote->getId()) {
89
- $this->importFromObject($quote);
90
  } else {
91
- $this->quoteId = $quoteId;
92
  }
93
 
94
- // return this
95
  return $this;
96
  }
97
 
98
  /**
99
  * Import this abstract from a real magento one
100
- * @param Mage_Sales_Model_Quote
101
- * @return Copernica_MarketingSoftware_Model_Abstraction_Quote
 
102
  */
103
- protected function importFromObject(Mage_Sales_Model_Quote $original)
104
  {
105
- $this->quoteId = $original->getId();
106
- $this->active = (bool) $original->getIsActive();
107
- $this->quantity = $original->getItemsQty();
108
- $this->currency = $original->getQuoteCurrencyCode();
109
- $this->price = Mage::getModel('marketingsoftware/abstraction_price')->setOriginal($original);
110
- $this->storeview = Mage::getModel('marketingsoftware/abstraction_storeview')->setOriginal($original->getStore());
111
- $this->timestamp = $original->getUpdatedAt();
112
- $this->customerIP = $original->getRemoteIp();
113
 
114
  if ($address = $original->getShippingAddress()) {
115
- $this->weight = $address->getWeight();
116
  }
117
 
118
  $data = array();
 
119
  $items = $original->getAllVisibleItems();
 
120
  foreach ($items as $item) {
121
  $data[] = Mage::getModel('marketingsoftware/abstraction_quote_item')->setOriginal($item);
122
  }
123
- $this->items = $data;
124
 
125
- // The quote model only returns a customer if it exists
 
126
  if ($customerId = $original->getCustomerId()) {
127
- $this->customerId = $customerId;
128
  }
129
 
130
  $data = array();
131
- //retrieve this quote's addresses
132
- //Note: this may return empty addresses, since quotes always have address records. Check the email field of the address.
133
  $addresses = $original->getAddressesCollection();
 
134
  foreach ($addresses as $address) {
135
  $data[] = Mage::getModel('marketingsoftware/abstraction_address')->setOriginal($address);
136
  }
137
- $this->addresses = $data;
 
138
 
139
  if ($address = $original->getShippingAddress()) {
140
- $this->shippingDescription = $address->getShippingDescription();
141
  }
142
 
143
  if ($payment = $original->getPayment()) {
144
- //this try/catch is needed because getMethodInstance throws an exception instead of returning null
145
  try {
146
- $this->paymentDescription = $payment->getMethodInstance()->getTitle();
147
  } catch (Mage_Core_Exception $exception) { }
148
  }
149
  }
150
 
151
  /**
152
  * The quote id of this quote object
153
- * @return integer
 
154
  */
155
  public function id()
156
  {
157
- return $this->quoteId;
158
  }
159
 
160
  /**
161
  * Is this quote still active
162
- * @return boolean
 
163
  */
164
  public function active()
165
  {
166
- return $this->active;
167
  }
168
 
169
  /**
170
  * The number of items present in this quote
171
- * @return integer
 
172
  */
173
  public function quantity()
174
  {
175
- return $this->quantity;
176
  }
177
 
178
  /**
179
  * The payment currency of this quote
180
- * @return string
 
181
  */
182
  public function currency()
183
  {
184
- return $this->currency;
185
  }
186
 
187
  /**
188
  * The price
189
  * Note that an object is returned, which may consist of multiple components
190
- * @return Copernica_MarketingSoftware_Model_Abstraction_Price
 
191
  */
192
  public function price()
193
  {
194
- return $this->price;
195
  }
196
 
197
  /**
198
  * The weight
199
- * @return float
 
200
  */
201
  public function weight()
202
  {
203
- return $this->weight;
204
  }
205
 
206
  /**
207
  * To what storeview does this quote belong
208
- * @return Copernica_MarketingSoftware_Model_Abstraction_Storeview
 
209
  */
210
  public function storeview()
211
  {
212
- return $this->storeview;
213
  }
214
 
215
  /**
216
  * Get the items from the quote
217
- * @return array of Copernica_MarketingSoftware_Model_Abstraction_Quote_Item
 
218
  */
219
  public function items()
220
  {
221
- return $this->items;
222
  }
223
 
224
  /**
225
  * The timestamp at which this quote was modified
226
- * @return string
 
227
  */
228
  public function timestamp()
229
  {
230
- return $this->timestamp;
231
  }
232
 
233
  /**
234
  * The customer may return null
235
- * @return Copernica_MarketingSoftware_Model_Abstraction_Customer
 
236
  */
237
  public function customer()
238
  {
239
- if ($this->customerId) {
240
- return Mage::getModel('marketingsoftware/abstraction_customer')->loadCustomer($this->customerId);
241
  } else {
242
- // default fallback
243
  return null;
244
  }
245
  }
246
 
247
  /**
248
  * The addresses of this quote
249
- * @return array of Copernica_MarketingSoftware_Model_Abstraction_Address
 
250
  */
251
  public function addresses()
252
  {
253
- return $this->addresses;
254
  }
255
 
256
  /**
257
  * The IP from which this quote was constructed
258
- * @return string
 
259
  */
260
  public function customerIP()
261
  {
262
- return $this->customerIP;
263
  }
264
 
265
  /**
266
  * The shipping method of this quote
267
- * @return string
 
268
  */
269
  public function shippingDescription()
270
  {
271
- return $this->shippingDescription;
272
  }
273
 
274
  /**
275
  * The payment method of this quote
276
- * @return string
 
277
  */
278
  public function paymentDescription()
279
  {
280
- return $this->paymentDescription;
281
  }
282
 
283
  /**
284
  * Serialize the object
285
- * @return string
 
 
286
  */
287
  public function serialize()
288
  {
@@ -290,7 +312,6 @@ class Copernica_MarketingSoftware_Model_Abstraction_Quote implements Serializabl
290
  $this->id()
291
  ));
292
 
293
- // serialize the data
294
  return serialize(array(
295
  $this->id(),
296
  $this->quantity(),
@@ -311,38 +332,38 @@ class Copernica_MarketingSoftware_Model_Abstraction_Quote implements Serializabl
311
 
312
  /**
313
  * Unserialize the object
314
- * @param string
315
- * @return Copernica_MarketingSoftware_Model_Abstraction_Quote
 
 
316
  */
317
  public function unserialize($string)
318
  {
319
- // unserialize data
320
  list (
321
- $this->quoteId
322
  ) = unserialize($string);
323
 
324
- // load quote by it's Id
325
- $this->loadQuote($this->quoteId);
326
 
327
- // return same object
328
  return $this;
329
 
330
  list(
331
- $this->quoteId,
332
- $this->quantity,
333
- $this->currency,
334
- $this->timestamp,
335
- $this->customerIP,
336
- $this->items,
337
- $this->storeview,
338
- $this->customerId,
339
- $this->addresses,
340
- $this->price,
341
- $this->weight,
342
- $this->active,
343
- $this->shippingDescription,
344
- $this->paymentDescription
345
  ) = unserialize($string);
 
346
  return $this;
347
  }
348
  }
32
  /**
33
  * Predefine the internal fields
34
  */
35
+ protected $_quoteId;
36
+ protected $_quantity;
37
+ protected $_currency;
38
+ protected $_timestamp;
39
+ protected $_customerIP;
40
+ protected $_items;
41
+
42
+ /**
43
+ * The storeview object
44
+ *
45
+ * @var Copernica_MarketingSoftware_Model_Abstraction_Storeview
46
+ */
47
+ protected $_storeview;
48
+
49
+ protected $_customerId;
50
+ protected $_addresses;
51
+ protected $_price;
52
+ protected $_weight;
53
+ protected $_active;
54
+ protected $_shippingDescription;
55
+ protected $_paymentDescription;
56
 
57
  /**
58
  * Sets the original model
59
+ *
60
+ * @param Mage_Sales_Model_Quote $original
61
+ * @return Copernica_MarketingSoftware_Model_Abstraction_Quote
62
  */
63
  public function setOriginal(Mage_Sales_Model_Quote $original)
64
  {
65
+ $this->_quoteId = $original->getId();
 
66
 
 
67
  return $this;
68
  }
69
 
70
  /**
71
  * Loads a quote model
72
+ *
73
+ * @param integer $quoteId
74
+ * @return Copernica_MarketingSoftware_Model_Abstraction_Quote
75
  */
76
  public function loadQuote($quoteId)
77
  {
 
78
  $quote = Mage::getModel('sales/quote');
79
 
80
+ if (!is_callable($quote, 'loadByIdWithoutStore')) {
 
 
 
81
  $storeIDs = array();
82
+
83
+ foreach (Mage::app()->getStores() as $id => $store) {
84
+ $storeIDs[] = $id;
85
+ }
86
 
 
87
  $quote->setSharedStoreIds($storeIDs);
88
  $quote->load($quoteId);
89
+ } else {
90
+ $quote->loadByIdWithoutStore($quoteId);
91
  }
 
92
 
 
93
  if ($quote->getId()) {
94
+ $this->_importFromObject($quote);
95
  } else {
96
+ $this->_quoteId = $quoteId;
97
  }
98
 
 
99
  return $this;
100
  }
101
 
102
  /**
103
  * Import this abstract from a real magento one
104
+ *
105
+ * @param Mage_Sales_Model_Quote $original
106
+ * @return Copernica_MarketingSoftware_Model_Abstraction_Quote
107
  */
108
+ protected function _importFromObject(Mage_Sales_Model_Quote $original)
109
  {
110
+ $this->_quoteId = $original->getId();
111
+ $this->_active = (bool) $original->getIsActive();
112
+ $this->_quantity = $original->getItemsQty();
113
+ $this->_currency = $original->getQuoteCurrencyCode();
114
+ $this->_price = Mage::getModel('marketingsoftware/abstraction_price')->setOriginal($original);
115
+ $this->_storeview = Mage::getModel('marketingsoftware/abstraction_storeview')->setOriginal($original->getStore());
116
+ $this->_timestamp = $original->getUpdatedAt();
117
+ $this->_customerIP = $original->getRemoteIp();
118
 
119
  if ($address = $original->getShippingAddress()) {
120
+ $this->_weight = $address->getWeight();
121
  }
122
 
123
  $data = array();
124
+
125
  $items = $original->getAllVisibleItems();
126
+
127
  foreach ($items as $item) {
128
  $data[] = Mage::getModel('marketingsoftware/abstraction_quote_item')->setOriginal($item);
129
  }
 
130
 
131
+ $this->_items = $data;
132
+
133
  if ($customerId = $original->getCustomerId()) {
134
+ $this->_customerId = $customerId;
135
  }
136
 
137
  $data = array();
138
+
 
139
  $addresses = $original->getAddressesCollection();
140
+
141
  foreach ($addresses as $address) {
142
  $data[] = Mage::getModel('marketingsoftware/abstraction_address')->setOriginal($address);
143
  }
144
+
145
+ $this->_addresses = $data;
146
 
147
  if ($address = $original->getShippingAddress()) {
148
+ $this->_shippingDescription = $address->getShippingDescription();
149
  }
150
 
151
  if ($payment = $original->getPayment()) {
 
152
  try {
153
+ $this->_paymentDescription = $payment->getMethodInstance()->getTitle();
154
  } catch (Mage_Core_Exception $exception) { }
155
  }
156
  }
157
 
158
  /**
159
  * The quote id of this quote object
160
+ *
161
+ * @return integer
162
  */
163
  public function id()
164
  {
165
+ return $this->_quoteId;
166
  }
167
 
168
  /**
169
  * Is this quote still active
170
+ *
171
+ * @return boolean
172
  */
173
  public function active()
174
  {
175
+ return $this->_active;
176
  }
177
 
178
  /**
179
  * The number of items present in this quote
180
+ *
181
+ * @return integer
182
  */
183
  public function quantity()
184
  {
185
+ return $this->_quantity;
186
  }
187
 
188
  /**
189
  * The payment currency of this quote
190
+ *
191
+ * @return string
192
  */
193
  public function currency()
194
  {
195
+ return $this->_currency;
196
  }
197
 
198
  /**
199
  * The price
200
  * Note that an object is returned, which may consist of multiple components
201
+ *
202
+ * @return Copernica_MarketingSoftware_Model_Abstraction_Price
203
  */
204
  public function price()
205
  {
206
+ return $this->_price;
207
  }
208
 
209
  /**
210
  * The weight
211
+ *
212
+ * @return float
213
  */
214
  public function weight()
215
  {
216
+ return $this->_weight;
217
  }
218
 
219
  /**
220
  * To what storeview does this quote belong
221
+ *
222
+ * @return Copernica_MarketingSoftware_Model_Abstraction_Storeview
223
  */
224
  public function storeview()
225
  {
226
+ return $this->_storeview;
227
  }
228
 
229
  /**
230
  * Get the items from the quote
231
+ *
232
+ * @return array
233
  */
234
  public function items()
235
  {
236
+ return $this->_items;
237
  }
238
 
239
  /**
240
  * The timestamp at which this quote was modified
241
+ *
242
+ * @return string
243
  */
244
  public function timestamp()
245
  {
246
+ return $this->_timestamp;
247
  }
248
 
249
  /**
250
  * The customer may return null
251
+ *
252
+ * @return Copernica_MarketingSoftware_Model_Abstraction_Customer|null
253
  */
254
  public function customer()
255
  {
256
+ if ($this->_customerId) {
257
+ return Mage::getModel('marketingsoftware/abstraction_customer')->loadCustomer($this->_customerId);
258
  } else {
 
259
  return null;
260
  }
261
  }
262
 
263
  /**
264
  * The addresses of this quote
265
+ *
266
+ * @return array
267
  */
268
  public function addresses()
269
  {
270
+ return $this->_addresses;
271
  }
272
 
273
  /**
274
  * The IP from which this quote was constructed
275
+ *
276
+ * @return string
277
  */
278
  public function customerIP()
279
  {
280
+ return $this->_customerIP;
281
  }
282
 
283
  /**
284
  * The shipping method of this quote
285
+ *
286
+ * @return string
287
  */
288
  public function shippingDescription()
289
  {
290
+ return $this->_shippingDescription;
291
  }
292
 
293
  /**
294
  * The payment method of this quote
295
+ *
296
+ * @return string
297
  */
298
  public function paymentDescription()
299
  {
300
+ return $this->_paymentDescription;
301
  }
302
 
303
  /**
304
  * Serialize the object
305
+ *
306
+ * @todo Two returns??
307
+ * @return string
308
  */
309
  public function serialize()
310
  {
312
  $this->id()
313
  ));
314
 
 
315
  return serialize(array(
316
  $this->id(),
317
  $this->quantity(),
332
 
333
  /**
334
  * Unserialize the object
335
+ *
336
+ * @todo Two returns??
337
+ * @param string $string
338
+ * @return Copernica_MarketingSoftware_Model_Abstraction_Quote
339
  */
340
  public function unserialize($string)
341
  {
 
342
  list (
343
+ $this->_quoteId
344
  ) = unserialize($string);
345
 
346
+ $this->loadQuote($this->_quoteId);
 
347
 
 
348
  return $this;
349
 
350
  list(
351
+ $this->_quoteId,
352
+ $this->_quantity,
353
+ $this->_currency,
354
+ $this->_timestamp,
355
+ $this->_customerIP,
356
+ $this->_items,
357
+ $this->_storeview,
358
+ $this->_customerId,
359
+ $this->_addresses,
360
+ $this->_price,
361
+ $this->_weight,
362
+ $this->_active,
363
+ $this->_shippingDescription,
364
+ $this->_paymentDescription
365
  ) = unserialize($string);
366
+
367
  return $this;
368
  }
369
  }
app/code/community/Copernica/MarketingSoftware/Model/Abstraction/Quote/Item.php CHANGED
@@ -32,37 +32,38 @@ class Copernica_MarketingSoftware_Model_Abstraction_Quote_Item implements Serial
32
  /**
33
  * Predefine the internal fields
34
  */
35
- protected $id;
36
- protected $quoteId;
37
- protected $quantity;
38
- protected $price;
39
- protected $weight;
40
- protected $timestamp;
41
- protected $options;
42
- protected $product;
43
 
44
  /**
45
  * Sets the original model
46
- * @param Mage_Sales_Model_Quote_Item $original
47
- * @return Copernica_MarketingSoftware_Model_Abstraction_Quote_Item
 
48
  */
49
  public function setOriginal(Mage_Sales_Model_Quote_Item $original)
50
  {
51
- $this->id = $original->getId();
52
- $this->quantity = $original->getQty();
53
- $this->price = Mage::getModel('marketingsoftware/abstraction_price')->setOriginal($original);
54
- $this->weight = $original->getWeight();
55
- $this->timestamp = $original->getUpdatedAt();
56
- $this->product = Mage::getModel('marketingsoftware/abstraction_product')->setOriginal($original);
57
 
58
  $options = Mage::getModel('marketingsoftware/abstraction_quote_item_options')->setOriginal($original);
 
59
  if ($options->attributes()) {
60
- //only return option object if it this quote actually has options
61
- $this->options = $options;
62
  }
63
 
64
  if ($quote = $original->getQuote()) {
65
- $this->quoteId = $quote->getId();
66
  }
67
 
68
  return $this;
@@ -70,84 +71,111 @@ class Copernica_MarketingSoftware_Model_Abstraction_Quote_Item implements Serial
70
 
71
  /**
72
  * The id of this quote item object
73
- * @return integer
 
74
  */
75
  public function id()
76
  {
77
- return $this->id;
78
  }
79
 
80
  /**
81
  * Get the quote to which this item belongs
82
- * @return Copernica_MarketingSoftware_Model_Abstraction_Quote
 
83
  */
84
  public function quote()
85
  {
86
- return Mage::getModel('marketingsoftware/abstraction_quote')->loadQuote($this->quoteId);
87
  }
88
 
89
  /**
90
  * The amount of this quote item
91
- * @return integer
 
92
  */
93
  public function quantity()
94
  {
95
- return $this->quantity;
96
  }
97
 
98
  /**
99
  * The price
100
  * Note that an object is returned, which may consist of multiple components
101
- * @return Copernica_MarketingSoftware_Model_Abstraction_Price
 
102
  */
103
  public function price()
104
  {
105
- return $this->price;
106
  }
107
 
108
  /**
109
  * The weight
110
- * @return float
 
111
  */
112
  public function weight()
113
  {
114
- return $this->weight;
115
  }
116
 
117
  /**
118
  * The timestamp at which this quote item was modified
119
- * @return string
 
120
  */
121
  public function timestamp()
122
  {
123
- return $this->timestamp;
124
  }
125
 
126
  /**
127
  * Get the options of this quote item
128
- * @return Copernica_MarketingSoftware_Model_Abstraction_Quote_Item_Options
 
129
  */
130
  public function options()
131
  {
132
- return $this->options;
133
  }
134
 
135
  /**
136
  * Get the product which belongs to this item
137
- * @return Copernica_MarketingSoftware_Model_Abstraction_Product
 
138
  */
139
  public function product()
140
  {
141
- return $this->product;
142
  }
143
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
144
  /**
145
  * Serialize the object
146
- * @return string
 
147
  */
148
  public function serialize()
149
  {
150
- // serialize the data
151
  return serialize(array(
152
  $this->id(),
153
  is_object($quote = $this->quote()) ? $quote->id() : null,
@@ -162,21 +190,23 @@ class Copernica_MarketingSoftware_Model_Abstraction_Quote_Item implements Serial
162
 
163
  /**
164
  * Unserialize the object
165
- * @param string
166
- * @return Copernica_MarketingSoftware_Model_Abstraction_Quote_Item
 
167
  */
168
  public function unserialize($string)
169
  {
170
  list(
171
- $this->id,
172
- $this->quoteId,
173
- $this->quantity,
174
- $this->price,
175
- $this->weight,
176
- $this->timestamp,
177
- $this->options,
178
- $this->product
179
  ) = unserialize($string);
 
180
  return $this;
181
  }
182
  }
32
  /**
33
  * Predefine the internal fields
34
  */
35
+ protected $_id;
36
+ protected $_quoteId;
37
+ protected $_quantity;
38
+ protected $_price;
39
+ protected $_weight;
40
+ protected $_timestamp;
41
+ protected $_options;
42
+ protected $_product;
43
 
44
  /**
45
  * Sets the original model
46
+ *
47
+ * @param Mage_Sales_Model_Quote_Item $original
48
+ * @return Copernica_MarketingSoftware_Model_Abstraction_Quote_Item
49
  */
50
  public function setOriginal(Mage_Sales_Model_Quote_Item $original)
51
  {
52
+ $this->_id = $original->getId();
53
+ $this->_quantity = $original->getQty();
54
+ $this->_price = Mage::getModel('marketingsoftware/abstraction_price')->setOriginal($original);
55
+ $this->_weight = $original->getWeight();
56
+ $this->_timestamp = $original->getUpdatedAt();
57
+ $this->_product = Mage::getModel('marketingsoftware/abstraction_product')->setOriginal($original);
58
 
59
  $options = Mage::getModel('marketingsoftware/abstraction_quote_item_options')->setOriginal($original);
60
+
61
  if ($options->attributes()) {
62
+ $this->_options = $options;
 
63
  }
64
 
65
  if ($quote = $original->getQuote()) {
66
+ $this->_quoteId = $quote->getId();
67
  }
68
 
69
  return $this;
71
 
72
  /**
73
  * The id of this quote item object
74
+ *
75
+ * @return integer
76
  */
77
  public function id()
78
  {
79
+ return $this->_id;
80
  }
81
 
82
  /**
83
  * Get the quote to which this item belongs
84
+ *
85
+ * @return Copernica_MarketingSoftware_Model_Abstraction_Quote
86
  */
87
  public function quote()
88
  {
89
+ return Mage::getModel('marketingsoftware/abstraction_quote')->loadQuote($this->_quoteId);
90
  }
91
 
92
  /**
93
  * The amount of this quote item
94
+ *
95
+ * @return integer
96
  */
97
  public function quantity()
98
  {
99
+ return $this->_quantity;
100
  }
101
 
102
  /**
103
  * The price
104
  * Note that an object is returned, which may consist of multiple components
105
+ *
106
+ * @return Copernica_MarketingSoftware_Model_Abstraction_Price
107
  */
108
  public function price()
109
  {
110
+ return $this->_price;
111
  }
112
 
113
  /**
114
  * The weight
115
+ *
116
+ * @return float
117
  */
118
  public function weight()
119
  {
120
+ return $this->_weight;
121
  }
122
 
123
  /**
124
  * The timestamp at which this quote item was modified
125
+ *
126
+ * @return string
127
  */
128
  public function timestamp()
129
  {
130
+ return $this->_timestamp;
131
  }
132
 
133
  /**
134
  * Get the options of this quote item
135
+ *
136
+ * @return Copernica_MarketingSoftware_Model_Abstraction_Quote_Item_Options
137
  */
138
  public function options()
139
  {
140
+ return $this->_options;
141
  }
142
 
143
  /**
144
  * Get the product which belongs to this item
145
+ *
146
+ * @return Copernica_MarketingSoftware_Model_Abstraction_Product
147
  */
148
  public function product()
149
  {
150
+ return $this->_product;
151
  }
152
 
153
+ /**
154
+ * Loads a product model
155
+ *
156
+ * @param integer $productId
157
+ * @return Copernica_MarketingSoftware_Model_Abstraction_Product
158
+ */
159
+ public function loadQuoteItem($quoteId)
160
+ {
161
+ $quoteItem = Mage::getModel('sales/quote_item')->load($quoteId);
162
+
163
+ if ($quoteItem->getId()) {
164
+ $this->setOriginal($quoteItem);
165
+ } else {
166
+ $this->id = $quoteId;
167
+ }
168
+
169
+ return $this;
170
+ }
171
+
172
  /**
173
  * Serialize the object
174
+ *
175
+ * @return string
176
  */
177
  public function serialize()
178
  {
 
179
  return serialize(array(
180
  $this->id(),
181
  is_object($quote = $this->quote()) ? $quote->id() : null,
190
 
191
  /**
192
  * Unserialize the object
193
+ *
194
+ * @param string $string
195
+ * @return Copernica_MarketingSoftware_Model_Abstraction_Quote_Item
196
  */
197
  public function unserialize($string)
198
  {
199
  list(
200
+ $this->_id,
201
+ $this->_quoteId,
202
+ $this->_quantity,
203
+ $this->_price,
204
+ $this->_weight,
205
+ $this->_timestamp,
206
+ $this->_options,
207
+ $this->_product
208
  ) = unserialize($string);
209
+
210
  return $this;
211
  }
212
  }
app/code/community/Copernica/MarketingSoftware/Model/Abstraction/Quote/Item/Options.php CHANGED
@@ -32,34 +32,34 @@ class Copernica_MarketingSoftware_Model_Abstraction_Quote_Item_Options implement
32
  /**
33
  * Predefine the internal fields
34
  */
35
- protected $name;
36
- protected $attributes = null;
37
 
38
 
39
  /**
40
  * Sets the original model
41
- * @param Mage_Sales_Model_Quote_Item $original
42
- * @return Copernica_MarketingSoftware_Model_Abstraction_Quote_Item_Options
 
43
  */
44
  public function setOriginal(Mage_Sales_Model_Quote_Item $original)
45
  {
46
- $this->name = $original->getName();
47
 
48
  $data = array();
 
49
  if ($optionData = $original->getOptionByCode('info_buyRequest')) {
50
  $product = $optionData->getProduct();
51
- //this converts the options to a usable format (same as order items)
52
- //see: Mage_Sales_Model_Convert_Quote::itemToOrderItem
53
  $options = $original->getProduct()->getTypeInstance(true)->getOrderOptions($original->getProduct());
 
54
  $attributes = array();
 
55
  if (isset($options['attributes_info'])) {
56
- //configurable products
57
  $attributes = $options['attributes_info'];
58
  } elseif (isset($options['bundle_options'])) {
59
- //bundle products
60
  $attributes = $options['bundle_options'];
61
  } elseif (isset($options['options'])) {
62
- //generic products
63
  $attributes = $options['options'];
64
  }
65
 
@@ -67,7 +67,8 @@ class Copernica_MarketingSoftware_Model_Abstraction_Quote_Item_Options implement
67
  foreach ($attributes as $attribute) {
68
  $data[$attribute['label']] = $attribute['value'];
69
  }
70
- $this->attributes = $data;
 
71
  }
72
  }
73
 
@@ -76,62 +77,67 @@ class Copernica_MarketingSoftware_Model_Abstraction_Quote_Item_Options implement
76
 
77
  /**
78
  * The name of this set of options
79
- * @return integer
 
80
  */
81
  public function name()
82
  {
83
- return $this->name;
84
  }
85
 
86
  /**
87
  * Return an assoc array with attributes
88
- * @return array
 
89
  */
90
  public function attributes()
91
  {
92
- return $this->attributes;
93
  }
94
 
95
  /**
96
  * Return a string representation
97
- * @return String
 
98
  */
99
  public function __toString()
100
  {
101
- return $this->arrayToString($this->attributes());
102
  }
103
 
104
  /**
105
  * Return a string representation of an array
106
- * @param array
107
- * @return String
 
 
108
  */
109
- protected function arrayToString($value, $prefix = '')
110
  {
111
  $string = "";
112
- foreach ($value as $key => $value)
113
- {
114
- // is the value an array
115
- if (is_array($value))
116
- {
117
- // if there is only one subvalue, use that instead
118
- if (isset($value[0]) && count($value) == 1) $value = $value[0];
119
 
120
- // compose the string
121
- $string .= $prefix.$key.":\n".$this->arrayToString($value, $prefix.' ');
 
122
  }
123
- else $string.= $prefix.$key.": $value\n";
124
  }
 
125
  return $string;
126
  }
127
 
128
  /**
129
  * Serialize the object
130
- * @return string
 
131
  */
132
  public function serialize()
133
  {
134
- // serialize the data
135
  return serialize(array(
136
  $this->name(),
137
  $this->attributes(),
@@ -140,15 +146,17 @@ class Copernica_MarketingSoftware_Model_Abstraction_Quote_Item_Options implement
140
 
141
  /**
142
  * Unserialize the object
143
- * @param string
144
- * @return Copernica_MarketingSoftware_Model_Abstraction_Quote_Item_Options
 
145
  */
146
  public function unserialize($string)
147
  {
148
  list(
149
- $this->name,
150
- $this->attributes
151
  ) = unserialize($string);
 
152
  return $this;
153
  }
154
  }
32
  /**
33
  * Predefine the internal fields
34
  */
35
+ protected $_name;
36
+ protected $_attributes = null;
37
 
38
 
39
  /**
40
  * Sets the original model
41
+ *
42
+ * @param Mage_Sales_Model_Quote_Item $original
43
+ * @return Copernica_MarketingSoftware_Model_Abstraction_Quote_Item_Options
44
  */
45
  public function setOriginal(Mage_Sales_Model_Quote_Item $original)
46
  {
47
+ $this->_name = $original->getName();
48
 
49
  $data = array();
50
+
51
  if ($optionData = $original->getOptionByCode('info_buyRequest')) {
52
  $product = $optionData->getProduct();
53
+
 
54
  $options = $original->getProduct()->getTypeInstance(true)->getOrderOptions($original->getProduct());
55
+
56
  $attributes = array();
57
+
58
  if (isset($options['attributes_info'])) {
 
59
  $attributes = $options['attributes_info'];
60
  } elseif (isset($options['bundle_options'])) {
 
61
  $attributes = $options['bundle_options'];
62
  } elseif (isset($options['options'])) {
 
63
  $attributes = $options['options'];
64
  }
65
 
67
  foreach ($attributes as $attribute) {
68
  $data[$attribute['label']] = $attribute['value'];
69
  }
70
+
71
+ $this->_attributes = $data;
72
  }
73
  }
74
 
77
 
78
  /**
79
  * The name of this set of options
80
+ *
81
+ * @return integer
82
  */
83
  public function name()
84
  {
85
+ return $this->_name;
86
  }
87
 
88
  /**
89
  * Return an assoc array with attributes
90
+ *
91
+ * @return array
92
  */
93
  public function attributes()
94
  {
95
+ return $this->_attributes;
96
  }
97
 
98
  /**
99
  * Return a string representation
100
+ *
101
+ * @return string
102
  */
103
  public function __toString()
104
  {
105
+ return $this->_arrayToString($this->_attributes());
106
  }
107
 
108
  /**
109
  * Return a string representation of an array
110
+ *
111
+ * @param array $value
112
+ * @param string $prefix
113
+ * @return string
114
  */
115
+ protected function _arrayToString($value, $prefix = '')
116
  {
117
  $string = "";
118
+
119
+ foreach ($value as $key => $value) {
120
+ if (is_array($value)) {
121
+ if (isset($value[0]) && count($value) == 1) {
122
+ $value = $value[0];
123
+ }
 
124
 
125
+ $string .= $prefix.$key.":\n".$this->_arrayToString($value, $prefix.' ');
126
+ } else {
127
+ $string.= $prefix.$key.": $value\n";
128
  }
 
129
  }
130
+
131
  return $string;
132
  }
133
 
134
  /**
135
  * Serialize the object
136
+ *
137
+ * @return string
138
  */
139
  public function serialize()
140
  {
 
141
  return serialize(array(
142
  $this->name(),
143
  $this->attributes(),
146
 
147
  /**
148
  * Unserialize the object
149
+ *
150
+ * @param string $string
151
+ * @return Copernica_MarketingSoftware_Model_Abstraction_Quote_Item_Options
152
  */
153
  public function unserialize($string)
154
  {
155
  list(
156
+ $this->_name,
157
+ $this->_attributes
158
  ) = unserialize($string);
159
+
160
  return $this;
161
  }
162
  }
app/code/community/Copernica/MarketingSoftware/Model/Abstraction/Storeview.php CHANGED
@@ -32,29 +32,30 @@ class Copernica_MarketingSoftware_Model_Abstraction_Storeview implements Seriali
32
  /**
33
  * Predefine the internal fields
34
  */
35
- protected $id;
36
- protected $websiteCode;
37
- protected $websiteLabel;
38
- protected $storeCode;
39
- protected $storeLabel;
40
- protected $viewCode;
41
- protected $viewLabel;
42
 
43
  /**
44
  * Sets the original model
45
- * @param Mage_Core_Model_Store $original
46
- * @return Copernica_MarketingSoftware_Model_Abstraction_Storeview
 
47
  */
48
  public function setOriginal(Mage_Core_Model_Store $original)
49
  {
50
  if ($original->getWebsite() instanceof Mage_Core_Model_Website) {
51
- $this->id = $original->getId();
52
- $this->websiteCode = $original->getWebsite()->getCode();
53
- $this->websiteLabel = $original->getWebsite()->getName();
54
- $this->storeCode = $original->getGroup()->getId();
55
- $this->storeLabel = $original->getGroup()->getName();
56
- $this->viewCode = $original->getCode();
57
- $this->viewLabel = $original->getName();
58
  }
59
 
60
  return $this;
@@ -62,70 +63,78 @@ class Copernica_MarketingSoftware_Model_Abstraction_Storeview implements Seriali
62
 
63
  /**
64
  * Return the id for the storeview
65
- * @return int
 
66
  */
67
  public function id()
68
  {
69
- return $this->id;
70
  }
71
 
72
  /**
73
  * Return the code for the website
74
- * @return string
 
75
  */
76
  public function websiteCode()
77
  {
78
- return $this->websiteCode;
79
  }
80
 
81
  /**
82
  * Return the label for the website
83
- * @return string
 
84
  */
85
  public function websiteLabel()
86
  {
87
- return $this->websiteLabel;
88
  }
89
 
90
  /**
91
  * Return the code for the store
92
- * @return string
 
93
  */
94
  public function storeCode()
95
  {
96
- return $this->storeCode;
97
  }
98
 
99
  /**
100
  * Return the label for the store
101
- * @return string
 
102
  */
103
  public function storeLabel()
104
  {
105
- return $this->storeLabel;
106
  }
107
 
108
  /**
109
  * Return the code for the store
110
- * @return string
 
111
  */
112
  public function viewCode()
113
  {
114
- return $this->viewCode;
115
  }
116
 
117
  /**
118
  * Return the label for the store
119
- * @return string
 
120
  */
121
  public function viewLabel()
122
  {
123
- return $this->viewLabel;
124
  }
125
 
126
  /**
127
  * Convert this value to a string
128
- * @return String
 
129
  */
130
  public function __toString()
131
  {
@@ -138,11 +147,11 @@ class Copernica_MarketingSoftware_Model_Abstraction_Storeview implements Seriali
138
 
139
  /**
140
  * Serialize the object
141
- * @return string
 
142
  */
143
  public function serialize()
144
  {
145
- // serialize the data
146
  return serialize(array(
147
  $this->id(),
148
  $this->websiteCode(),
@@ -156,19 +165,22 @@ class Copernica_MarketingSoftware_Model_Abstraction_Storeview implements Seriali
156
 
157
  /**
158
  * Unserialize the object
159
- * @param Copernica_MarketingSoftware_Model_Abstraction_Storeview
 
 
160
  */
161
  public function unserialize($string)
162
  {
163
  list(
164
- $this->id,
165
- $this->websiteCode,
166
- $this->websiteLabel,
167
- $this->storeCode,
168
- $this->storeLabel,
169
- $this->viewCode,
170
- $this->viewLabel
171
  ) = unserialize($string);
 
172
  return $this;
173
  }
174
  }
32
  /**
33
  * Predefine the internal fields
34
  */
35
+ protected $_id;
36
+ protected $_websiteCode;
37
+ protected $_websiteLabel;
38
+ protected $_storeCode;
39
+ protected $_storeLabel;
40
+ protected $_viewCode;
41
+ protected $_viewLabel;
42
 
43
  /**
44
  * Sets the original model
45
+ *
46
+ * @param Mage_Core_Model_Store $original
47
+ * @return Copernica_MarketingSoftware_Model_Abstraction_Storeview
48
  */
49
  public function setOriginal(Mage_Core_Model_Store $original)
50
  {
51
  if ($original->getWebsite() instanceof Mage_Core_Model_Website) {
52
+ $this->_id = $original->getId();
53
+ $this->_websiteCode = $original->getWebsite()->getCode();
54
+ $this->_websiteLabel = $original->getWebsite()->getName();
55
+ $this->_storeCode = $original->getGroup()->getId();
56
+ $this->_storeLabel = $original->getGroup()->getName();
57
+ $this->_viewCode = $original->getCode();
58
+ $this->_viewLabel = $original->getName();
59
  }
60
 
61
  return $this;
63
 
64
  /**
65
  * Return the id for the storeview
66
+ *
67
+ * @return int
68
  */
69
  public function id()
70
  {
71
+ return $this->_id;
72
  }
73
 
74
  /**
75
  * Return the code for the website
76
+ *
77
+ * @return string
78
  */
79
  public function websiteCode()
80
  {
81
+ return $this->_websiteCode;
82
  }
83
 
84
  /**
85
  * Return the label for the website
86
+ *
87
+ * @return string
88
  */
89
  public function websiteLabel()
90
  {
91
+ return $this->_websiteLabel;
92
  }
93
 
94
  /**
95
  * Return the code for the store
96
+ *
97
+ * @return string
98
  */
99
  public function storeCode()
100
  {
101
+ return $this->_storeCode;
102
  }
103
 
104
  /**
105
  * Return the label for the store
106
+ *
107
+ * @return string
108
  */
109
  public function storeLabel()
110
  {
111
+ return $this->_storeLabel;
112
  }
113
 
114
  /**
115
  * Return the code for the store
116
+ *
117
+ * @return string
118
  */
119
  public function viewCode()
120
  {
121
+ return $this->_viewCode;
122
  }
123
 
124
  /**
125
  * Return the label for the store
126
+ *
127
+ * @return string
128
  */
129
  public function viewLabel()
130
  {
131
+ return $this->_viewLabel;
132
  }
133
 
134
  /**
135
  * Convert this value to a string
136
+ *
137
+ * @return String
138
  */
139
  public function __toString()
140
  {
147
 
148
  /**
149
  * Serialize the object
150
+ *
151
+ * @return string
152
  */
153
  public function serialize()
154
  {
 
155
  return serialize(array(
156
  $this->id(),
157
  $this->websiteCode(),
165
 
166
  /**
167
  * Unserialize the object
168
+ *
169
+ * @param string $string
170
+ * @return Copernica_MarketingSoftware_Model_Abstraction_Storeview
171
  */
172
  public function unserialize($string)
173
  {
174
  list(
175
+ $this->_id,
176
+ $this->_websiteCode,
177
+ $this->_websiteLabel,
178
+ $this->_storeCode,
179
+ $this->_storeLabel,
180
+ $this->_viewCode,
181
+ $this->_viewLabel
182
  ) = unserialize($string);
183
+
184
  return $this;
185
  }
186
  }
app/code/community/Copernica/MarketingSoftware/Model/Abstraction/Subscription.php CHANGED
@@ -32,41 +32,54 @@ class Copernica_MarketingSoftware_Model_Abstraction_Subscription implements Seri
32
  /**
33
  * Predefine the internal fields
34
  */
35
- protected $id;
36
- protected $email;
37
- protected $status;
38
- protected $storeview;
39
- protected $customerId;
 
 
 
 
 
 
 
40
 
41
  /**
42
  * Sets the original model
43
- * @param Mage_Newsletter_Model_Subscriber $original
44
- * @return Copernica_MarketingSoftware_Model_Abstraction_Storeview
 
45
  */
46
  public function setOriginal(Mage_Newsletter_Model_Subscriber $original)
47
  {
48
- $this->id = $original->getId();
49
- $this->email = $original->getEmail();
50
- $this->customerId = $original->getCustomerId();
51
 
52
  $store = Mage::getModel('core/store')->load($original->getStoreId());
53
- $this->storeview = Mage::getModel('marketingsoftware/abstraction_storeview')->setOriginal($store);
 
54
 
55
  switch ($original->getStatus()) {
56
  case Mage_Newsletter_Model_Subscriber::STATUS_SUBSCRIBED:
57
- $this->status = 'subscribed';
58
  break;
 
59
  case Mage_Newsletter_Model_Subscriber::STATUS_NOT_ACTIVE:
60
- $this->status = 'not active';
61
  break;
 
62
  case Mage_Newsletter_Model_Subscriber::STATUS_UNSUBSCRIBED:
63
- $this->status = 'unsubscribed';
64
  break;
 
65
  case Mage_Newsletter_Model_Subscriber::STATUS_UNCONFIRMED:
66
- $this->status = 'unconfirmed';
67
  break;
 
68
  default:
69
- $this->status = 'unknown';
70
  break;
71
  }
72
 
@@ -75,41 +88,44 @@ class Copernica_MarketingSoftware_Model_Abstraction_Subscription implements Seri
75
 
76
  /**
77
  * Return the identifier for this object
78
- * @return integer
 
79
  */
80
  public function id()
81
  {
82
- return $this->id;
83
  }
84
 
85
  /**
86
  * Return the e-mail address with which the user is subscribed
87
- * @return string
 
88
  */
89
  public function email()
90
  {
91
- return $this->email;
92
  }
93
 
94
  /**
95
  * Return the status of this subscription
96
  * Note that subscribed might be returned but the record is currently removed
97
- * @return string
 
98
  */
99
  public function status()
100
  {
101
- return $this->status;
102
  }
103
 
104
  /**
105
  * The customer may return null
106
- * @return Copernica_MarketingSoftware_Model_Abstraction_Customer
 
107
  */
108
  public function customer()
109
  {
110
- if ($this->customerId) {
111
- // construct an object given the identifier
112
- return Mage::getModel('marketingsoftware/abstraction_customer')->loadCustomer($this->customerId);
113
  } else {
114
  return null;
115
  }
@@ -117,20 +133,21 @@ class Copernica_MarketingSoftware_Model_Abstraction_Subscription implements Seri
117
 
118
  /**
119
  * Return the storeview for this subscription
120
- * @return Copernica_MarketingSoftware_Model_Abstraction_Storeview
 
121
  */
122
  public function storeView()
123
  {
124
- return $this->storeview;
125
  }
126
 
127
  /**
128
  * Serialize the object
129
- * @return string
 
130
  */
131
  public function serialize()
132
  {
133
- // serialize the data
134
  return serialize(array(
135
  $this->id(),
136
  $this->email(),
@@ -142,18 +159,20 @@ class Copernica_MarketingSoftware_Model_Abstraction_Subscription implements Seri
142
 
143
  /**
144
  * Unserialize the object
145
- * @param string
146
- * @return Copernica_MarketingSoftware_Model_Abstraction_Subscription
 
147
  */
148
  public function unserialize($string)
149
  {
150
  list(
151
- $this->id,
152
- $this->email,
153
- $this->status,
154
- $this->storeview,
155
- $this->customerId
156
  ) = unserialize($string);
 
157
  return $this;
158
  }
159
  }
32
  /**
33
  * Predefine the internal fields
34
  */
35
+ protected $_id;
36
+ protected $_email;
37
+ protected $_status;
38
+
39
+ /**
40
+ * The storeview object
41
+ *
42
+ * @var Copernica_MarketingSoftware_Model_Abstraction_Storeview
43
+ */
44
+ protected $_storeview;
45
+
46
+ protected $_customerId;
47
 
48
  /**
49
  * Sets the original model
50
+ *
51
+ * @param Mage_Newsletter_Model_Subscriber $original
52
+ * @return Copernica_MarketingSoftware_Model_Abstraction_Subscription
53
  */
54
  public function setOriginal(Mage_Newsletter_Model_Subscriber $original)
55
  {
56
+ $this->_id = $original->getId();
57
+ $this->_email = $original->getEmail();
58
+ $this->_customerId = $original->getCustomerId();
59
 
60
  $store = Mage::getModel('core/store')->load($original->getStoreId());
61
+
62
+ $this->_storeview = Mage::getModel('marketingsoftware/abstraction_storeview')->setOriginal($store);
63
 
64
  switch ($original->getStatus()) {
65
  case Mage_Newsletter_Model_Subscriber::STATUS_SUBSCRIBED:
66
+ $this->_status = 'subscribed';
67
  break;
68
+
69
  case Mage_Newsletter_Model_Subscriber::STATUS_NOT_ACTIVE:
70
+ $this->_status = 'not active';
71
  break;
72
+
73
  case Mage_Newsletter_Model_Subscriber::STATUS_UNSUBSCRIBED:
74
+ $this->_status = 'unsubscribed';
75
  break;
76
+
77
  case Mage_Newsletter_Model_Subscriber::STATUS_UNCONFIRMED:
78
+ $this->_status = 'unconfirmed';
79
  break;
80
+
81
  default:
82
+ $this->_status = 'unknown';
83
  break;
84
  }
85
 
88
 
89
  /**
90
  * Return the identifier for this object
91
+ *
92
+ * @return integer
93
  */
94
  public function id()
95
  {
96
+ return $this->_id;
97
  }
98
 
99
  /**
100
  * Return the e-mail address with which the user is subscribed
101
+ *
102
+ * @return string
103
  */
104
  public function email()
105
  {
106
+ return $this->_email;
107
  }
108
 
109
  /**
110
  * Return the status of this subscription
111
  * Note that subscribed might be returned but the record is currently removed
112
+ *
113
+ * @return string
114
  */
115
  public function status()
116
  {
117
+ return $this->_status;
118
  }
119
 
120
  /**
121
  * The customer may return null
122
+ *
123
+ * @return Copernica_MarketingSoftware_Model_Abstraction_Customer
124
  */
125
  public function customer()
126
  {
127
+ if ($this->_customerId) {
128
+ return Mage::getModel('marketingsoftware/abstraction_customer')->loadCustomer($this->_customerId);
 
129
  } else {
130
  return null;
131
  }
133
 
134
  /**
135
  * Return the storeview for this subscription
136
+ *
137
+ * @return Copernica_MarketingSoftware_Model_Abstraction_Storeview
138
  */
139
  public function storeView()
140
  {
141
+ return $this->_storeview;
142
  }
143
 
144
  /**
145
  * Serialize the object
146
+ *
147
+ * @return string
148
  */
149
  public function serialize()
150
  {
 
151
  return serialize(array(
152
  $this->id(),
153
  $this->email(),
159
 
160
  /**
161
  * Unserialize the object
162
+ *
163
+ * @param string $string
164
+ * @return Copernica_MarketingSoftware_Model_Abstraction_Subscription
165
  */
166
  public function unserialize($string)
167
  {
168
  list(
169
+ $this->_id,
170
+ $this->_email,
171
+ $this->_status,
172
+ $this->_storeview,
173
+ $this->_customerId
174
  ) = unserialize($string);
175
+
176
  return $this;
177
  }
178
  }
app/code/community/Copernica/MarketingSoftware/Model/Abstraction/Wishlist.php ADDED
@@ -0,0 +1,369 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Copernica Marketing Software
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0).
8
+ * It is available through the world-wide-web at this URL:
9
+ * http://opensource.org/licenses/osl-3.0.php
10
+ * If you are unable to obtain a copy of the license through the
11
+ * world-wide-web, please send an email to copernica@support.cream.nl
12
+ * so we can send you a copy immediately.
13
+ *
14
+ * DISCLAIMER
15
+ *
16
+ * Do not edit or add to this file if you wish to upgrade this software
17
+ * to newer versions in the future. If you wish to customize this module
18
+ * for your needs please refer to http://www.magento.com/ for more
19
+ * information.
20
+ *
21
+ * @category Copernica
22
+ * @package Copernica_MarketingSoftware
23
+ * @copyright Copyright (c) 2011-2012 Copernica & Cream. (http://docs.cream.nl/)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ /**
28
+ * A wrapper object around a wishlist
29
+ */
30
+ class Copernica_MarketingSoftware_Model_Abstraction_Wishlist implements Serializable
31
+ {
32
+ /**
33
+ * Predefine the internal fields
34
+ */
35
+ protected $_wishlistId;
36
+ protected $_quantity;
37
+ protected $_currency;
38
+ protected $_timestamp;
39
+ protected $_customerIP;
40
+ protected $_items;
41
+
42
+ /**
43
+ * The storeview object
44
+ *
45
+ * @var Copernica_MarketingSoftware_Model_Abstraction_Storeview
46
+ */
47
+ protected $_storeview;
48
+
49
+ protected $_customerId;
50
+ protected $_addresses;
51
+ protected $_price;
52
+ protected $_weight;
53
+ protected $_active;
54
+ protected $_shippingDescription;
55
+ protected $_paymentDescription;
56
+
57
+ /**
58
+ * Sets the original model
59
+ *
60
+ * @param Mage_Wishlist_Model_Wishlist $original
61
+ * @return Copernica_MarketingSoftware_Model_Abstraction_Wishlist
62
+ */
63
+ public function setOriginal(Mage_Wishlist_Model_Wishlist $original)
64
+ {
65
+ $this->_wishlistId = $original->getId();
66
+
67
+ return $this;
68
+ }
69
+
70
+ /**
71
+ * Loads a wishlist model
72
+ *
73
+ * @param integer $wishlistId
74
+ * @return Copernica_MarketingSoftware_Model_Abstraction_Wishlist
75
+ */
76
+ public function loadWishlist($wishlistId)
77
+ {
78
+ $wishlist = Mage::getModel('wishlist/wishlist');
79
+
80
+ if (!is_callable($wishlist, 'loadByIdWithoutStore')) {
81
+ $storeIDs = array();
82
+
83
+ foreach (Mage::app()->getStores() as $id => $store) {
84
+ $storeIDs[] = $id;
85
+ }
86
+
87
+ $wishlist->setSharedStoreIds($storeIDs);
88
+ $wishlist->load($wishlistId);
89
+ } else {
90
+ $wishlist->loadByIdWithoutStore($wishlistId);
91
+ }
92
+
93
+ if ($wishlist->getId()) {
94
+ $this->_importFromObject($wishlist);
95
+ } else {
96
+ $this->_wishlistId = $wishlistId;
97
+ }
98
+
99
+ return $this;
100
+ }
101
+
102
+ /**
103
+ * Import this abstract from a real magento one
104
+ *
105
+ * @param Mage_Wishlist_Model_Wishlist $original
106
+ * @return Copernica_MarketingSoftware_Model_Abstraction_Wishlist
107
+ */
108
+ protected function _importFromObject(Mage_Wishlist_Model_Wishlist $original)
109
+ {
110
+ $this->_wishlistId = $original->getId();
111
+ $this->_active = (bool) $original->getIsActive();
112
+ $this->_quantity = $original->getItemsQty();
113
+ $this->_currency = $original->getWishlistCurrencyCode();
114
+ $this->_price = Mage::getModel('marketingsoftware/abstraction_price')->setOriginal($original);
115
+ $this->_storeview = Mage::getModel('marketingsoftware/abstraction_storeview')->setOriginal($original->getStore());
116
+ $this->_timestamp = $original->getUpdatedAt();
117
+ $this->_customerIP = $original->getRemoteIp();
118
+
119
+ if ($address = $original->getShippingAddress()) {
120
+ $this->_weight = $address->getWeight();
121
+ }
122
+
123
+ $data = array();
124
+
125
+ $items = $original->getAllVisibleItems();
126
+
127
+ foreach ($items as $item) {
128
+ $data[] = Mage::getModel('marketingsoftware/abstraction_wishlist_item')->setOriginal($item);
129
+ }
130
+
131
+ $this->_items = $data;
132
+
133
+ if ($customerId = $original->getCustomerId()) {
134
+ $this->_customerId = $customerId;
135
+ }
136
+
137
+ $data = array();
138
+
139
+ $addresses = $original->getAddressesCollection();
140
+
141
+ foreach ($addresses as $address) {
142
+ $data[] = Mage::getModel('marketingsoftware/abstraction_address')->setOriginal($address);
143
+ }
144
+
145
+ $this->_addresses = $data;
146
+
147
+ if ($address = $original->getShippingAddress()) {
148
+ $this->_shippingDescription = $address->getShippingDescription();
149
+ }
150
+
151
+ if ($payment = $original->getPayment()) {
152
+ try {
153
+ $this->_paymentDescription = $payment->getMethodInstance()->getTitle();
154
+ } catch (Mage_Core_Exception $exception) { }
155
+ }
156
+ }
157
+
158
+ /**
159
+ * The wishlist id of this wishlist object
160
+ *
161
+ * @return integer
162
+ */
163
+ public function id()
164
+ {
165
+ return $this->_wishlistId;
166
+ }
167
+
168
+ /**
169
+ * Is this wishlist still active
170
+ *
171
+ * @return boolean
172
+ */
173
+ public function active()
174
+ {
175
+ return $this->_active;
176
+ }
177
+
178
+ /**
179
+ * The number of items present in this wishlist
180
+ *
181
+ * @return integer
182
+ */
183
+ public function quantity()
184
+ {
185
+ return $this->_quantity;
186
+ }
187
+
188
+ /**
189
+ * The payment currency of this wishlist
190
+ *
191
+ * @return string
192
+ */
193
+ public function currency()
194
+ {
195
+ return $this->_currency;
196
+ }
197
+
198
+ /**
199
+ * The price
200
+ * Note that an object is returned, which may consist of multiple components
201
+ *
202
+ * @return Copernica_MarketingSoftware_Model_Abstraction_Price
203
+ */
204
+ public function price()
205
+ {
206
+ return $this->_price;
207
+ }
208
+
209
+ /**
210
+ * The weight
211
+ *
212
+ * @return float
213
+ */
214
+ public function weight()
215
+ {
216
+ return $this->_weight;
217
+ }
218
+
219
+ /**
220
+ * To what storeview does this wishlist belong
221
+ *
222
+ * @return Copernica_MarketingSoftware_Model_Abstraction_Storeview
223
+ */
224
+ public function storeview()
225
+ {
226
+ return $this->_storeview;
227
+ }
228
+
229
+ /**
230
+ * Get the items from the wishlist
231
+ *
232
+ * @return array
233
+ */
234
+ public function items()
235
+ {
236
+ return $this->_items;
237
+ }
238
+
239
+ /**
240
+ * The timestamp at which this wishlist was modified
241
+ *
242
+ * @return string
243
+ */
244
+ public function timestamp()
245
+ {
246
+ return $this->_timestamp;
247
+ }
248
+
249
+ /**
250
+ * The customer may return null
251
+ *
252
+ * @return Copernica_MarketingSoftware_Model_Abstraction_Customer|null
253
+ */
254
+ public function customer()
255
+ {
256
+ if ($this->_customerId) {
257
+ return Mage::getModel('marketingsoftware/abstraction_customer')->loadCustomer($this->_customerId);
258
+ } else {
259
+ return null;
260
+ }
261
+ }
262
+
263
+ /**
264
+ * The addresses of this wishlist
265
+ *
266
+ * @return array
267
+ */
268
+ public function addresses()
269
+ {
270
+ return $this->_addresses;
271
+ }
272
+
273
+ /**
274
+ * The IP from which this wishlist was constructed
275
+ *
276
+ * @return string
277
+ */
278
+ public function customerIP()
279
+ {
280
+ return $this->_customerIP;
281
+ }
282
+
283
+ /**
284
+ * The shipping method of this wishlist
285
+ *
286
+ * @return string
287
+ */
288
+ public function shippingDescription()
289
+ {
290
+ return $this->_shippingDescription;
291
+ }
292
+
293
+ /**
294
+ * The payment method of this wishlist
295
+ *
296
+ * @return string
297
+ */
298
+ public function paymentDescription()
299
+ {
300
+ return $this->_paymentDescription;
301
+ }
302
+
303
+ /**
304
+ * Serialize the object
305
+ *
306
+ * @todo Two returns??
307
+ * @return string
308
+ */
309
+ public function serialize()
310
+ {
311
+ return serialize(array(
312
+ $this->id()
313
+ ));
314
+
315
+ return serialize(array(
316
+ $this->id(),
317
+ $this->quantity(),
318
+ $this->currency(),
319
+ $this->timestamp(),
320
+ $this->customerIP(),
321
+ $this->items(),
322
+ $this->storeview(),
323
+ is_object($customer = $this->customer()) ? $customer->id() : null,
324
+ $this->addresses(),
325
+ $this->price(),
326
+ $this->weight(),
327
+ $this->active(),
328
+ $this->shippingDescription(),
329
+ $this->paymentDescription()
330
+ ));
331
+ }
332
+
333
+ /**
334
+ * Unserialize the object
335
+ *
336
+ * @todo Two returns??
337
+ * @param string $string
338
+ * @return Copernica_MarketingSoftware_Model_Abstraction_Wishlist
339
+ */
340
+ public function unserialize($string)
341
+ {
342
+ list (
343
+ $this->_wishlistId
344
+ ) = unserialize($string);
345
+
346
+ $this->loadWishlist($this->_wishlistId);
347
+
348
+ return $this;
349
+
350
+ list(
351
+ $this->_wishlistId,
352
+ $this->_quantity,
353
+ $this->_currency,
354
+ $this->_timestamp,
355
+ $this->_customerIP,
356
+ $this->_items,
357
+ $this->_storeview,
358
+ $this->_customerId,
359
+ $this->_addresses,
360
+ $this->_price,
361
+ $this->_weight,
362
+ $this->_active,
363
+ $this->_shippingDescription,
364
+ $this->_paymentDescription
365
+ ) = unserialize($string);
366
+
367
+ return $this;
368
+ }
369
+ }
app/code/community/Copernica/MarketingSoftware/Model/Abstraction/Wishlist/Item.php ADDED
@@ -0,0 +1,209 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Copernica Marketing Software
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0).
8
+ * It is available through the world-wide-web at this URL:
9
+ * http://opensource.org/licenses/osl-3.0.php
10
+ * If you are unable to obtain a copy of the license through the
11
+ * world-wide-web, please send an email to copernica@support.cream.nl
12
+ * so we can send you a copy immediately.
13
+ *
14
+ * DISCLAIMER
15
+ *
16
+ * Do not edit or add to this file if you wish to upgrade this software
17
+ * to newer versions in the future. If you wish to customize this module
18
+ * for your needs please refer to http://www.magento.com/ for more
19
+ * information.
20
+ *
21
+ * @category Copernica
22
+ * @package Copernica_MarketingSoftware
23
+ * @copyright Copyright (c) 2011-2012 Copernica & Cream. (http://docs.cream.nl/)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ /**
28
+ * A wrapper object around a wishlist Item
29
+ */
30
+ class Copernica_MarketingSoftware_Model_Abstraction_Wishlist_Item implements Serializable
31
+ {
32
+ /**
33
+ * Predefine the internal fields
34
+ */
35
+ protected $_id;
36
+ protected $_wishlistId;
37
+ protected $_quantity;
38
+ protected $_price;
39
+ protected $_weight;
40
+ protected $_timestamp;
41
+ protected $_options;
42
+ protected $_product;
43
+
44
+ /**
45
+ * Sets the original model
46
+ *
47
+ * @param Mage_Wishlist_Model_Item $original
48
+ * @return Copernica_MarketingSoftware_Model_Abstraction_Wishlist_Item
49
+ */
50
+ public function setOriginal(Mage_Wishlist_Model_Item $original)
51
+ {
52
+ $this->_id = $original->getId();
53
+ $this->_wishlistId = $original->getWishlist()->getId();
54
+ $this->_quantity = $original->getQty();
55
+ $this->_price = Mage::getModel('marketingsoftware/abstraction_price')->setOriginal($original);
56
+ $this->_weight = $original->getWeight();
57
+ $this->_timestamp = $original->getUpdatedAt();
58
+ $this->_product = Mage::getModel('marketingsoftware/abstraction_product')->setOriginal($original);
59
+
60
+ $options = Mage::getModel('marketingsoftware/abstraction_wishlist_item_options')->setOriginal($original);
61
+
62
+ if ($options->attributes()) {
63
+ $this->_options = $options;
64
+ }
65
+
66
+ return $this;
67
+ }
68
+
69
+ /**
70
+ * The id of this wishlist item object
71
+ *
72
+ * @return integer
73
+ */
74
+ public function id()
75
+ {
76
+ return $this->_id;
77
+ }
78
+
79
+ /**
80
+ * Get the wishlist to which this item belongs
81
+ *
82
+ * @return Copernica_MarketingSoftware_Model_Abstraction_Wishlist
83
+ */
84
+ public function wishlist()
85
+ {
86
+ return Mage::getModel('marketingsoftware/abstraction_wishlist')->loadWishlist($this->_wishlistId);
87
+ }
88
+
89
+ /**
90
+ * The amount of this wishlist item
91
+ *
92
+ * @return integer
93
+ */
94
+ public function quantity()
95
+ {
96
+ return $this->_quantity;
97
+ }
98
+
99
+ /**
100
+ * The price
101
+ * Note that an object is returned, which may consist of multiple components
102
+ *
103
+ * @return Copernica_MarketingSoftware_Model_Abstraction_Price
104
+ */
105
+ public function price()
106
+ {
107
+ return $this->_price;
108
+ }
109
+
110
+ /**
111
+ * The weight
112
+ *
113
+ * @return float
114
+ */
115
+ public function weight()
116
+ {
117
+ return $this->_weight;
118
+ }
119
+
120
+ /**
121
+ * The timestamp at which this wishlist item was modified
122
+ *
123
+ * @return string
124
+ */
125
+ public function timestamp()
126
+ {
127
+ return $this->_timestamp;
128
+ }
129
+
130
+ /**
131
+ * Get the options of this wishlist item
132
+ *
133
+ * @return Copernica_MarketingSoftware_Model_Abstraction_Wishlist_Item_Options
134
+ */
135
+ public function options()
136
+ {
137
+ return $this->_options;
138
+ }
139
+
140
+ /**
141
+ * Get the product which belongs to this item
142
+ *
143
+ * @return Copernica_MarketingSoftware_Model_Abstraction_Product
144
+ */
145
+ public function product()
146
+ {
147
+ return $this->_product;
148
+ }
149
+
150
+ /**
151
+ * Loads a wishlist model
152
+ *
153
+ * @param integer $wishlistId
154
+ * @return Copernica_MarketingSoftware_Model_Abstraction_Wishlist_Item
155
+ */
156
+ public function loadWishlistItem($wishlistItemId)
157
+ {
158
+ $wishlistItem = Mage::getModel('wishlist/item')->load($wishlistItemId);
159
+
160
+ if ($wishlistItem->getId()) {
161
+ $this->setOriginal($wishlistItem);
162
+ } else {
163
+ $this->id = $wishlistItemId;
164
+ }
165
+
166
+ return $this;
167
+ }
168
+
169
+ /**
170
+ * Serialize the object
171
+ *
172
+ * @return string
173
+ */
174
+ public function serialize()
175
+ {
176
+ return serialize(array(
177
+ $this->id(),
178
+ is_object($wishlist = $this->wishlist()) ? $wishlist->id() : null,
179
+ $this->quantity(),
180
+ $this->price(),
181
+ $this->weight(),
182
+ $this->timestamp(),
183
+ $this->options(),
184
+ $this->product(),
185
+ ));
186
+ }
187
+
188
+ /**
189
+ * Unserialize the object
190
+ *
191
+ * @param string $string
192
+ * @return Copernica_MarketingSoftware_Model_Abstraction_Wishlist_Item
193
+ */
194
+ public function unserialize($string)
195
+ {
196
+ list(
197
+ $this->_id,
198
+ $this->_wishlistId,
199
+ $this->_quantity,
200
+ $this->_price,
201
+ $this->_weight,
202
+ $this->_timestamp,
203
+ $this->_options,
204
+ $this->_product
205
+ ) = unserialize($string);
206
+
207
+ return $this;
208
+ }
209
+ }
app/code/community/Copernica/MarketingSoftware/Model/Abstraction/Wishlist/Item/Options.php ADDED
@@ -0,0 +1,163 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Copernica Marketing Software
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0).
8
+ * It is available through the world-wide-web at this URL:
9
+ * http://opensource.org/licenses/osl-3.0.php
10
+ * If you are unable to obtain a copy of the license through the
11
+ * world-wide-web, please send an email to copernica@support.cream.nl
12
+ * so we can send you a copy immediately.
13
+ *
14
+ * DISCLAIMER
15
+ *
16
+ * Do not edit or add to this file if you wish to upgrade this software
17
+ * to newer versions in the future. If you wish to customize this module
18
+ * for your needs please refer to http://www.magento.com/ for more
19
+ * information.
20
+ *
21
+ * @category Copernica
22
+ * @package Copernica_MarketingSoftware
23
+ * @copyright Copyright (c) 2011-2012 Copernica & Cream. (http://docs.cream.nl/)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ /**
28
+ * A wrapper object around wishlist item options
29
+ */
30
+ class Copernica_MarketingSoftware_Model_Abstraction_Wishlist_Item_Options implements Serializable
31
+ {
32
+ /**
33
+ * Predefine the internal fields
34
+ */
35
+ protected $_name;
36
+ protected $_attributes = null;
37
+
38
+
39
+ /**
40
+ * Sets the original model
41
+ *
42
+ * @param Mage_Wishlist_Model_Item $original
43
+ * @return Copernica_MarketingSoftware_Model_Abstraction_Wishlist_Item_Options
44
+ */
45
+ public function setOriginal(Mage_Wishlist_Model_Item $original)
46
+ {
47
+ $this->_name = $original->getName();
48
+
49
+ $data = array();
50
+
51
+ if ($optionData = $original->getOptionByCode('info_buyRequest')) {
52
+ $product = $optionData->getProduct();
53
+
54
+ $options = $original->getProduct()->getTypeInstance(true)->getOrderOptions($original->getProduct());
55
+
56
+ $attributes = array();
57
+
58
+ if (isset($options['attributes_info'])) {
59
+ $attributes = $options['attributes_info'];
60
+ } elseif (isset($options['bundle_options'])) {
61
+ $attributes = $options['bundle_options'];
62
+ } elseif (isset($options['options'])) {
63
+ $attributes = $options['options'];
64
+ }
65
+
66
+ if ($attributes) {
67
+ foreach ($attributes as $attribute) {
68
+ $data[$attribute['label']] = $attribute['value'];
69
+ }
70
+
71
+ $this->_attributes = $data;
72
+ }
73
+ }
74
+
75
+ return $this;
76
+ }
77
+
78
+ /**
79
+ * The name of this set of options
80
+ *
81
+ * @return integer
82
+ */
83
+ public function name()
84
+ {
85
+ return $this->_name;
86
+ }
87
+
88
+ /**
89
+ * Return an assoc array with attributes
90
+ *
91
+ * @return array
92
+ */
93
+ public function attributes()
94
+ {
95
+ return $this->_attributes;
96
+ }
97
+
98
+ /**
99
+ * Return a string representation
100
+ *
101
+ * @return string
102
+ */
103
+ public function __toString()
104
+ {
105
+ return $this->_arrayToString($this->_attributes());
106
+ }
107
+
108
+ /**
109
+ * Return a string representation of an array
110
+ *
111
+ * @param array $value
112
+ * @param string $prefix
113
+ * @return string
114
+ */
115
+ protected function _arrayToString($value, $prefix = '')
116
+ {
117
+ $string = "";
118
+
119
+ foreach ($value as $key => $value) {
120
+ if (is_array($value)) {
121
+ if (isset($value[0]) && count($value) == 1) {
122
+ $value = $value[0];
123
+ }
124
+
125
+ $string .= $prefix.$key.":\n".$this->_arrayToString($value, $prefix.' ');
126
+ } else {
127
+ $string.= $prefix.$key.": $value\n";
128
+ }
129
+ }
130
+
131
+ return $string;
132
+ }
133
+
134
+ /**
135
+ * Serialize the object
136
+ *
137
+ * @return string
138
+ */
139
+ public function serialize()
140
+ {
141
+ return serialize(array(
142
+ $this->name(),
143
+ $this->attributes(),
144
+ ));
145
+ }
146
+
147
+ /**
148
+ * Unserialize the object
149
+ *
150
+ * @param string $string
151
+ * @return Copernica_MarketingSoftware_Model_Abstraction_Wishlist_Item_Options
152
+ */
153
+ public function unserialize($string)
154
+ {
155
+ list(
156
+ $this->_name,
157
+ $this->_attributes
158
+ ) = unserialize($string);
159
+
160
+ return $this;
161
+ }
162
+ }
163
+
app/code/community/Copernica/MarketingSoftware/Model/Config.php CHANGED
@@ -38,7 +38,7 @@ class Copernica_MarketingSoftware_Model_Config extends Mage_Core_Model_Abstract
38
  /**
39
  * Constructor
40
  *
41
- * @see Varien_Object::_construct()
42
  */
43
  protected function _construct()
44
  {
@@ -47,7 +47,8 @@ class Copernica_MarketingSoftware_Model_Config extends Mage_Core_Model_Abstract
47
 
48
  /**
49
  * Load configuration row by it's key.
50
- * @param string
 
51
  * @return Copernica_MarketingSoftware_Model_Config
52
  */
53
  public function loadByKey($key)
@@ -57,6 +58,7 @@ class Copernica_MarketingSoftware_Model_Config extends Mage_Core_Model_Abstract
57
  $this->_afterLoad();
58
  $this->setOrigData();
59
  $this->_hasDataChanges = false;
 
60
  return $this;
61
  }
62
  }
38
  /**
39
  * Constructor
40
  *
41
+ * @see Varien_Object::_construct()
42
  */
43
  protected function _construct()
44
  {
47
 
48
  /**
49
  * Load configuration row by it's key.
50
+ *
51
+ * @param string $key
52
  * @return Copernica_MarketingSoftware_Model_Config
53
  */
54
  public function loadByKey($key)
58
  $this->_afterLoad();
59
  $this->setOrigData();
60
  $this->_hasDataChanges = false;
61
+
62
  return $this;
63
  }
64
  }
app/code/community/Copernica/MarketingSoftware/Model/Copernica/Abstract.php CHANGED
@@ -31,70 +31,75 @@ abstract class Copernica_MarketingSoftware_Model_Copernica_Abstract implements A
31
  {
32
  /**
33
  * To where data will be saved? Posible values are 'copernica' and 'magento'
34
- * @var String
 
35
  */
36
- protected $direction = 'copernica';
37
 
38
  /**
39
  * Set the direction for this synchronisation
40
- * @param String
41
- * @return Copernica_MarketingSoftware_Model_Copernica_Orderitem_Subprofile
 
42
  */
43
  public function setDirection($direction)
44
  {
45
- $this->direction = $direction;
 
46
  return $this;
47
  }
48
 
49
  /**
50
  * Return the identifier for this profile
 
51
  * @return string
52
  */
53
  abstract public function id();
54
 
55
  /**
56
  * Retrieve the data for this object
 
57
  * @return array
58
  */
59
  abstract protected function _data();
60
 
61
  /**
62
  * Get linked fields
 
63
  * @return array
64
  */
65
  abstract public function linkedFields();
66
 
67
  /**
68
  * Get the required fields
 
69
  * @return array
70
  */
71
  abstract public function requiredFields();
72
 
73
  /**
74
  * Check if offset exists in array
75
- * @param mixed
 
76
  * @return boolean
77
  */
78
  public function offsetExists($offset)
79
  {
80
- // Get the data from this profile
81
  $data = $this->toArray();
82
 
83
- // Return the data based on whether it exists
84
  return isset($data[$offset]);
85
  }
86
 
87
  /**
88
  * Get offset in array
89
- * @param mixed
 
90
  * @return mixed
91
  */
92
  public function offsetGet($offset)
93
  {
94
- // Get the data from this profile
95
  $data = $this->toArray();
96
 
97
- // Return the data based on whether it exists
98
  return isset($data[$offset]) ? $data[$offset] : null;
99
  }
100
 
@@ -105,37 +110,35 @@ abstract class Copernica_MarketingSoftware_Model_Copernica_Abstract implements A
105
  */
106
  public function toArray()
107
  {
108
- // Get the data
109
  $data = $this->_data();
110
 
111
- // this data is meant for magento, return the data as is
112
- if ($this->direction == 'magento') return $data;
 
113
 
114
- // construct an array which contains the required base record
115
  $returndata = array();
116
 
117
- // check the required fields
118
- foreach ($this->requiredFields() as $field) $returndata[$field] = $data[$field];
 
119
 
120
- // iterate over the linked fields
121
- foreach ($this->linkedFields() as $magentoField => $copernicaField)
122
- {
123
- // Not linked to a field, skip
124
- if (empty($copernicaField) || !isset($data[$magentoField])) continue;
125
 
126
- // Append it to the returned array
127
  $returndata[$copernicaField] = $data[$magentoField];
128
  }
129
 
130
- // Return the mapped data
131
  return $returndata;
132
  }
133
 
134
  /**
135
  * Set entry in offset of array
 
136
  * @deprecated
137
- * @param mixed
138
- * @param mixed
139
  */
140
  public function offsetSet($offset, $value)
141
  {
@@ -144,8 +147,9 @@ abstract class Copernica_MarketingSoftware_Model_Copernica_Abstract implements A
144
 
145
  /**
146
  * Remove entry by offset in array
 
147
  * @deprecated
148
- * @param mixed
149
  */
150
  public function offsetUnset($offset)
151
  {
31
  {
32
  /**
33
  * To where data will be saved? Posible values are 'copernica' and 'magento'
34
+ *
35
+ * @var String
36
  */
37
+ protected $_direction = 'copernica';
38
 
39
  /**
40
  * Set the direction for this synchronisation
41
+ *
42
+ * @param string $direction
43
+ * @return Copernica_MarketingSoftware_Model_Copernica_Orderitem_Subprofile
44
  */
45
  public function setDirection($direction)
46
  {
47
+ $this->_direction = $direction;
48
+
49
  return $this;
50
  }
51
 
52
  /**
53
  * Return the identifier for this profile
54
+ *
55
  * @return string
56
  */
57
  abstract public function id();
58
 
59
  /**
60
  * Retrieve the data for this object
61
+ *
62
  * @return array
63
  */
64
  abstract protected function _data();
65
 
66
  /**
67
  * Get linked fields
68
+ *
69
  * @return array
70
  */
71
  abstract public function linkedFields();
72
 
73
  /**
74
  * Get the required fields
75
+ *
76
  * @return array
77
  */
78
  abstract public function requiredFields();
79
 
80
  /**
81
  * Check if offset exists in array
82
+ *
83
+ * @param mixed $offset
84
  * @return boolean
85
  */
86
  public function offsetExists($offset)
87
  {
 
88
  $data = $this->toArray();
89
 
 
90
  return isset($data[$offset]);
91
  }
92
 
93
  /**
94
  * Get offset in array
95
+ *
96
+ * @param mixed $offset
97
  * @return mixed
98
  */
99
  public function offsetGet($offset)
100
  {
 
101
  $data = $this->toArray();
102
 
 
103
  return isset($data[$offset]) ? $data[$offset] : null;
104
  }
105
 
110
  */
111
  public function toArray()
112
  {
 
113
  $data = $this->_data();
114
 
115
+ if ($this->_direction == 'magento') {
116
+ return $data;
117
+ }
118
 
 
119
  $returndata = array();
120
 
121
+ foreach ($this->requiredFields() as $field) {
122
+ $returndata[$field] = $data[$field];
123
+ }
124
 
125
+ foreach ($this->linkedFields() as $magentoField => $copernicaField) {
126
+ if (empty($copernicaField) || !isset($data[$magentoField])) {
127
+ continue;
128
+ }
 
129
 
 
130
  $returndata[$copernicaField] = $data[$magentoField];
131
  }
132
 
 
133
  return $returndata;
134
  }
135
 
136
  /**
137
  * Set entry in offset of array
138
+ *
139
  * @deprecated
140
+ * @param mixed $offset
141
+ * @param mixed $value
142
  */
143
  public function offsetSet($offset, $value)
144
  {
147
 
148
  /**
149
  * Remove entry by offset in array
150
+ *
151
  * @deprecated
152
+ * @param mixed $offset
153
  */
154
  public function offsetUnset($offset)
155
  {
app/code/community/Copernica/MarketingSoftware/Model/Copernica/Address/Subprofile.php CHANGED
@@ -31,6 +31,7 @@ class Copernica_MarketingSoftware_Model_Copernica_Address_Subprofile extends Cop
31
  {
32
  /**
33
  * Return the identifier for this profile
 
34
  * @return string
35
  */
36
  public function id()
@@ -40,16 +41,19 @@ class Copernica_MarketingSoftware_Model_Copernica_Address_Subprofile extends Cop
40
 
41
  /**
42
  * Try to store a quote item
43
- * @param Copernica_MarketingSoftware_Model_Abstraction_Address
 
44
  */
45
  public function setAddress($address)
46
  {
47
  $this->address = $address;
 
48
  return $this;
49
  }
50
 
51
  /**
52
  * Get linked fields
 
53
  * @return array
54
  */
55
  public function linkedFields()
@@ -59,6 +63,7 @@ class Copernica_MarketingSoftware_Model_Copernica_Address_Subprofile extends Cop
59
 
60
  /**
61
  * Get the required fields
 
62
  * @return array
63
  */
64
  public function requiredFields()
@@ -68,20 +73,17 @@ class Copernica_MarketingSoftware_Model_Copernica_Address_Subprofile extends Cop
68
 
69
  /**
70
  * Retrieve the data for this object
 
71
  * @return array
72
  */
73
  protected function _data()
74
  {
75
- // We might need to get the e-mail from the customer
76
- if (($email = $this->address->email()) == "" && is_object($customer = $this->address->customer()))
77
- {
78
  $email = $customer->email();
79
  }
80
 
81
- // fetch the name object
82
  $name = $this->address->name();
83
 
84
- // Combine the data
85
  return array(
86
  'address_id' => $this->address->id(),
87
  'firstname' => is_object($name) ? $name->firstname() : null,
@@ -94,7 +96,7 @@ class Copernica_MarketingSoftware_Model_Copernica_Address_Subprofile extends Cop
94
  'city' => $this->address->city(),
95
  'state' => $this->address->state(),
96
  'zipcode' => $this->address->zipcode(),
97
- 'country_id' => $this->address->countryCode(),
98
  'telephone' => $this->address->telephone(),
99
  'fax' => $this->address->fax(),
100
  );
31
  {
32
  /**
33
  * Return the identifier for this profile
34
+ *
35
  * @return string
36
  */
37
  public function id()
41
 
42
  /**
43
  * Try to store a quote item
44
+ *
45
+ * @param Copernica_MarketingSoftware_Model_Abstraction_Address $address
46
  */
47
  public function setAddress($address)
48
  {
49
  $this->address = $address;
50
+
51
  return $this;
52
  }
53
 
54
  /**
55
  * Get linked fields
56
+ *
57
  * @return array
58
  */
59
  public function linkedFields()
63
 
64
  /**
65
  * Get the required fields
66
+ *
67
  * @return array
68
  */
69
  public function requiredFields()
73
 
74
  /**
75
  * Retrieve the data for this object
76
+ *
77
  * @return array
78
  */
79
  protected function _data()
80
  {
81
+ if (($email = $this->address->email()) == "" && is_object($customer = $this->address->customer())) {
 
 
82
  $email = $customer->email();
83
  }
84
 
 
85
  $name = $this->address->name();
86
 
 
87
  return array(
88
  'address_id' => $this->address->id(),
89
  'firstname' => is_object($name) ? $name->firstname() : null,
96
  'city' => $this->address->city(),
97
  'state' => $this->address->state(),
98
  'zipcode' => $this->address->zipcode(),
99
+ 'country_id' => $this->address->countryId(),
100
  'telephone' => $this->address->telephone(),
101
  'fax' => $this->address->fax(),
102
  );
app/code/community/Copernica/MarketingSoftware/Model/Copernica/Entity.php CHANGED
@@ -31,61 +31,52 @@ abstract class Copernica_MarketingSoftware_Model_Copernica_Entity
31
  {
32
  /**
33
  * Cached data.
34
- * @var array
 
35
  */
36
- protected $data = array();
37
-
38
- /**
39
- * This cosntructor has to be overriden inside child.
40
- * @param int Id of magento entity
41
- */
42
- protected function __construct($itemId)
43
- {
44
- // override in child class
45
- }
46
 
47
  /**
48
  * This is a factory method that will produce proper entities.
49
- * @param string
50
- * @param string|int
 
51
  * @return Copernica_MarketingSoftware_Model_Copernica_Entity
52
  */
53
- public function create($itemName, $itemId)
54
  {
55
- // construct proper child class
56
- $childClass = 'Copernica_MarketingSoftware_Model_Copernica_Entity_'.ucfirst($itemName);
57
-
58
- // check if child class exists
59
- if (!class_exists($childClass)) return null;
60
-
61
- // create new child class
62
- return new $childClass($itemId);
63
  }
64
 
65
  /**
66
- * @param string method name
 
67
  */
68
  public function __call($methodName, $arguments)
69
  {
70
- // check if we want to get something
71
- if (substr($methodName, 0, 3) != 'get') return null;
 
72
 
73
- // get the name of the property
74
  $property = substr($methodName, 3);
75
-
76
- // cause some really old PHP can be used...
77
  $property{0} = strtolower($property{0});
78
 
79
- // check if property was alredy fetched
80
- if (array_key_exists($property, $this->data)) return $this->data[$property];
 
81
 
82
- // construct fetch method name
83
  $fetchMethod = 'fetch'.ucfirst($property);
84
 
85
- // try to fetch data and store it inside registry
86
- if (method_exists($this, $fetchMethod) && !is_null($value = $this->$fetchMethod())) return $this->data[$property] = $value;
 
87
 
88
- // we didn't found anything
89
  return null;
90
  }
91
  }
31
  {
32
  /**
33
  * Cached data.
34
+ *
35
+ * @var array
36
  */
37
+ protected $_data = array();
 
 
 
 
 
 
 
 
 
38
 
39
  /**
40
  * This is a factory method that will produce proper entities.
41
+ *
42
+ * @todo Verify whether it is actually needed
43
+ * @param string $itemName
44
  * @return Copernica_MarketingSoftware_Model_Copernica_Entity
45
  */
46
+ public function create($itemName)
47
  {
48
+ $modelName = 'marketingsoftware/copernica_entity_'. $itemName;
49
+
50
+ if (!class_exists(Mage::getConfig()->getModelClassName($modelName))) {
51
+ return null;
52
+ }
53
+
54
+ return Mage::getModel($modelName);
 
55
  }
56
 
57
  /**
58
+ * @todo param arguments
59
+ * @param string $methodName
60
  */
61
  public function __call($methodName, $arguments)
62
  {
63
+ if (substr($methodName, 0, 3) != 'get') {
64
+ return null;
65
+ }
66
 
 
67
  $property = substr($methodName, 3);
 
 
68
  $property{0} = strtolower($property{0});
69
 
70
+ if (array_key_exists($property, $this->_data)) {
71
+ return $this->_data[$property];
72
+ }
73
 
 
74
  $fetchMethod = 'fetch'.ucfirst($property);
75
 
76
+ if (method_exists($this, $fetchMethod) && !is_null($value = $this->$fetchMethod())) {
77
+ return $this->_data[$property] = $value;
78
+ }
79
 
 
80
  return null;
81
  }
82
  }
app/code/community/Copernica/MarketingSoftware/Model/Copernica/Entity/Address.php CHANGED
@@ -28,77 +28,83 @@ class Copernica_MarketingSoftware_Model_Copernica_Entity_Address extends Coperni
28
  {
29
  /**
30
  * Magento address model
31
- * @var Mage_Customer_Model_Address
32
- */
33
- protected $address = null;
34
-
35
- /**
36
- * Construct address entity
37
- *
38
- * Constructor is expecting to receive one of magento address types. It would
39
- * be so more helpful if all addresses would be unified by an interface
40
- * or common class that we can use, but that seems to be wishful thinking.
41
  *
42
- * @param Mage_Customer_Model_Address|Mage_Sales_Model_Order_Address|Mage_Sales_Model_Quote_Address
43
  */
44
- public function __construct($address)
45
- {
46
- $this->address = $address;
47
- }
48
 
49
  /**
50
  * Due to legacy code this method is complicated.
 
51
  * @return string
52
  */
53
  public function fetchId()
54
  {
55
- switch (get_class($this->address))
56
- {
57
- case 'Mage_Customer_Model_Address': return 'ca_'.$this->address->getId();
 
58
  case 'Mage_Sales_Model_Order_Address':
59
- if ($customerAddressId = $this->address->getCustomerAddressId()) return 'ca_'.$customerAddressId;
60
- else return 'oa_'.$this->address->getId();
 
 
 
 
 
61
  case 'Mage_Sales_Model_Quote_Address':
62
- if ($customerAddressId = $this->address->getCustomerAddressId()) return 'ca_'.$customerAddressId;
63
- else return 'ca_'.$customerAddressId;
64
- default: return $this->address->getId();
 
 
 
 
 
 
65
  }
66
  }
67
 
68
  /**
69
- * Get address email
70
- * @return string
 
71
  */
72
  public function fetchEmail()
73
  {
74
- // try to get email from address model
75
- if ($email = $this->address->getEmail()) return $email;
 
76
 
77
- // well, we can try to get email from order model
78
- if (is_object($order = $this->address->getOrder()) && $email = $order->getCustomerEmail()) return $email;
 
79
 
80
- // well, another try... Quote may have an email address that we can use
81
- if (is_object($quote = $this->address->getQuote()) && $email = $quote->getCustomerEmail()) return $email;
 
82
 
83
- // maybe customer will have an email ?
84
- if (is_object($customer = $this->address->getCustomer()) && $email = $customer->getEmail()) return $email;
 
85
 
86
- // nope, we have no clue about email
87
  return '';
88
  }
89
 
90
  /**
91
  * Fetch name object for this address
92
- * @return Copernica_MarketingSoftware_Model_Abstraction_Name
 
93
  */
94
  public function fetchName()
95
  {
96
- return Mage::getModel('marketingsoftware/abstraction_name')->setOriginal($this->address);
97
  }
98
 
99
  /**
100
  * Fetch firstname
101
- * @return string
 
102
  */
103
  public function fetchFirstname()
104
  {
@@ -107,7 +113,8 @@ class Copernica_MarketingSoftware_Model_Copernica_Entity_Address extends Coperni
107
 
108
  /**
109
  * Fetch middlename
110
- * @return string
 
111
  */
112
  public function fetchMiddlename()
113
  {
@@ -116,7 +123,8 @@ class Copernica_MarketingSoftware_Model_Copernica_Entity_Address extends Coperni
116
 
117
  /**
118
  * Fetch lastname
119
- * @return string
 
120
  */
121
  public function fetchLastname()
122
  {
@@ -125,7 +133,8 @@ class Copernica_MarketingSoftware_Model_Copernica_Entity_Address extends Coperni
125
 
126
  /**
127
  * Fetch prefix
128
- * @return string
 
129
  */
130
  public function fetchPrefix()
131
  {
@@ -134,82 +143,106 @@ class Copernica_MarketingSoftware_Model_Copernica_Entity_Address extends Coperni
134
 
135
  /**
136
  * Fetch street
137
- * @return string
 
138
  */
139
  public function fetchStreet()
140
  {
141
- return $this->address->getStreetFull();
142
  }
143
 
144
  /**
145
  * Fetch city
146
- * @return string
 
147
  */
148
  public function fetchCity()
149
  {
150
- return $this->address->getCity();
151
  }
152
 
153
  /**
154
- * Fetch postal code
155
- * @return string
 
156
  */
157
  public function fetchZipcode()
158
  {
159
- return $this->address->getPostcode();
160
  }
161
 
162
  /**
163
  * Fetch state
164
- * @return string
 
165
  */
166
  public function fetchState()
167
  {
168
- return $this->address->getRegion();
169
  }
170
 
171
  /**
172
  * Fetch country
173
- * @return string
 
174
  */
175
  public function fetchCountryId()
176
  {
177
- return $this->address->getCountryCode();
178
  }
179
 
180
  /**
181
  * Fetch company
182
- * @return string
 
183
  */
184
  public function fetchCompany()
185
  {
186
- return $this->address->getCompany();
187
  }
188
 
189
  /**
190
  * Fetch telephone
191
- * @return string
 
192
  */
193
  public function fetchTelephone()
194
  {
195
- return $this->address->getTelephone();
196
  }
197
 
198
  /**
199
  * Fetch fax
200
- * @return string
 
201
  */
202
  public function fetchFax()
203
  {
204
- return $this->address->getFax();
205
  }
206
 
207
  /**
208
- * Get rest entity for given address
209
- * @return Copernica_MarketingSoftware_Model_REST_Address
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
210
  */
211
- public function getREST()
212
  {
213
- return new Copernica_MarketingSoftware_Model_REST_Address($this);
214
  }
215
  }
28
  {
29
  /**
30
  * Magento address model
 
 
 
 
 
 
 
 
 
 
31
  *
32
+ * @var Mage_Customer_Model_Address
33
  */
34
+ protected $_address = null;
 
 
 
35
 
36
  /**
37
  * Due to legacy code this method is complicated.
38
+ *
39
  * @return string
40
  */
41
  public function fetchId()
42
  {
43
+ switch (get_class($this->_address)) {
44
+ case 'Mage_Customer_Model_Address':
45
+ return 'ca_'.$this->_address->getId();
46
+
47
  case 'Mage_Sales_Model_Order_Address':
48
+ if ($customerAddressId = $this->_address->getCustomerAddressId()) {
49
+ return 'ca_'.$customerAddressId;
50
+ }
51
+ else {
52
+ return 'oa_'.$this->_address->getId();
53
+ }
54
+
55
  case 'Mage_Sales_Model_Quote_Address':
56
+ if ($customerAddressId = $this->_address->getCustomerAddressId()) {
57
+ return 'ca_'.$customerAddressId;
58
+ }
59
+ else {
60
+ return 'ca_'.$customerAddressId;
61
+ }
62
+
63
+ default:
64
+ return $this->_address->getId();
65
  }
66
  }
67
 
68
  /**
69
+ * Fetch email
70
+ *
71
+ * @return string
72
  */
73
  public function fetchEmail()
74
  {
75
+ if ($email = $this->_address->getEmail()) {
76
+ return $email;
77
+ }
78
 
79
+ if (is_object($order = $this->_address->getOrder()) && $email = $order->getCustomerEmail()) {
80
+ return $email;
81
+ }
82
 
83
+ if (is_object($quote = $this->_address->getQuote()) && $email = $quote->getCustomerEmail()) {
84
+ return $email;
85
+ }
86
 
87
+ if (is_object($customer = $this->_address->getCustomer()) && $email = $customer->getEmail()) {
88
+ return $email;
89
+ }
90
 
 
91
  return '';
92
  }
93
 
94
  /**
95
  * Fetch name object for this address
96
+ *
97
+ * @return Copernica_MarketingSoftware_Model_Abstraction_Name
98
  */
99
  public function fetchName()
100
  {
101
+ return Mage::getModel('marketingsoftware/abstraction_name')->setOriginal($this->_address);
102
  }
103
 
104
  /**
105
  * Fetch firstname
106
+ *
107
+ * @return string
108
  */
109
  public function fetchFirstname()
110
  {
113
 
114
  /**
115
  * Fetch middlename
116
+ *
117
+ * @return string
118
  */
119
  public function fetchMiddlename()
120
  {
123
 
124
  /**
125
  * Fetch lastname
126
+ *
127
+ * @return string
128
  */
129
  public function fetchLastname()
130
  {
133
 
134
  /**
135
  * Fetch prefix
136
+ *
137
+ * @return string
138
  */
139
  public function fetchPrefix()
140
  {
143
 
144
  /**
145
  * Fetch street
146
+ *
147
+ * @return string
148
  */
149
  public function fetchStreet()
150
  {
151
+ return $this->_address->getStreetFull();
152
  }
153
 
154
  /**
155
  * Fetch city
156
+ *
157
+ * @return string
158
  */
159
  public function fetchCity()
160
  {
161
+ return $this->_address->getCity();
162
  }
163
 
164
  /**
165
+ * Fetch postal code
166
+ *
167
+ * @return string
168
  */
169
  public function fetchZipcode()
170
  {
171
+ return $this->_address->getPostcode();
172
  }
173
 
174
  /**
175
  * Fetch state
176
+ *
177
+ * @return string
178
  */
179
  public function fetchState()
180
  {
181
+ return $this->_address->getRegion();
182
  }
183
 
184
  /**
185
  * Fetch country
186
+ *
187
+ * @return string
188
  */
189
  public function fetchCountryId()
190
  {
191
+ return $this->_address->getCountryId();
192
  }
193
 
194
  /**
195
  * Fetch company
196
+ *
197
+ * @return string
198
  */
199
  public function fetchCompany()
200
  {
201
+ return $this->_address->getCompany();
202
  }
203
 
204
  /**
205
  * Fetch telephone
206
+ *
207
+ * @return string
208
  */
209
  public function fetchTelephone()
210
  {
211
+ return $this->_address->getTelephone();
212
  }
213
 
214
  /**
215
  * Fetch fax
216
+ *
217
+ * @return string
218
  */
219
  public function fetchFax()
220
  {
221
+ return $this->_address->getFax();
222
  }
223
 
224
  /**
225
+ * Get REST address entity
226
+ *
227
+ * @return Copernica_MarketingSoftware_Model_Rest_Address
228
+ */
229
+ public function getRestAddress()
230
+ {
231
+ $restAddress = Mage::getModel('marketingsoftware/rest_address');
232
+ $restAddress->setAddressEntity($this);
233
+
234
+ return $restAddress;
235
+ }
236
+
237
+
238
+ /**
239
+ * Set address entity
240
+ * It is expecting to receive one of magento address types.
241
+ *
242
+ * @param Mage_Customer_Model_Address|Mage_Sales_Model_Order_Address|Mage_Sales_Model_Quote_Address $address
243
  */
244
+ public function setAddress($address)
245
  {
246
+ $this->_address = $address;
247
  }
248
  }
app/code/community/Copernica/MarketingSoftware/Model/Copernica/Entity/Customer.php CHANGED
@@ -31,64 +31,74 @@ class Copernica_MarketingSoftware_Model_Copernica_Entity_Customer extends Copern
31
  {
32
  /**
33
  * Magento customer
34
- * @var Mage_Customer_Model_Customer
 
35
  */
36
- private $customer = null;
37
 
38
  /**
39
  * Array of customer addresses
40
- * @var array
 
41
  */
42
- private $addresses = null;
43
 
44
  /**
45
  * Array of customer orders
46
- * @var array
 
47
  */
48
- private $orders = null;
 
 
 
 
 
 
 
 
49
 
50
  /**
51
  * Cache profile Id
52
- * @var string
 
53
  */
54
- private $profileId = null;
55
-
 
56
  /**
57
- * Construct customer entity
58
- * @param int
59
  */
60
- public function __construct($customerId)
61
- {
62
- // get magento customer
63
- $customer = Mage::getModel('customer/customer')->load($customerId);
64
-
65
- // check if it's a new customer
66
- if (!$customer->isObjectNew()) $this->customer = $customer;
67
-
68
- // it's an existing customer
69
- else throw Mage::exception('Copernica_MarketingSoftware', 'Customer does not exists', Copernica_MarketingSoftware_Exception::CUSTOMER_NOT_EXISTS);
70
- }
71
 
72
  /**
73
- * Get customer store view
74
- * @return Copernica_MarketingSoftware_Model_Abstraction_StoreView
 
75
  */
76
  public function fetchStoreView()
77
- {
78
- return Mage::getModel('marketingsoftware/abstraction_storeview')->setOriginal($this->customer->getStore());
 
 
 
 
79
  }
80
 
81
  /**
82
- * Get customer id. Magento customer Id
 
83
  * @return string
84
  */
85
  public function fetchId()
86
  {
87
- return $this->customer->getId();
88
  }
89
 
90
  /**
91
- * Our unique customer ID in form of customer_ID|storeView_ID
 
92
  * @return string
93
  */
94
  public function fetchCustomerId()
@@ -97,16 +107,18 @@ class Copernica_MarketingSoftware_Model_Copernica_Entity_Customer extends Copern
97
  }
98
 
99
  /**
100
- * Get customer name
 
101
  * @return Copernica_MarketingSoftware_Model_Abstraction_Name
102
  */
103
  public function fetchName()
104
  {
105
- return Mage::getModel('marketingsoftware/abstraction_name')->setOriginal($this->customer);
106
  }
107
 
108
  /**
109
- * Get customer firstname
 
110
  * @return string
111
  */
112
  public function fetchFirstname()
@@ -115,7 +127,8 @@ class Copernica_MarketingSoftware_Model_Copernica_Entity_Customer extends Copern
115
  }
116
 
117
  /**
118
- * Get customer middlename
 
119
  * @return string
120
  */
121
  public function fetchMiddlename()
@@ -124,7 +137,8 @@ class Copernica_MarketingSoftware_Model_Copernica_Entity_Customer extends Copern
124
  }
125
 
126
  /**
127
- * Fetch customer lastname
 
128
  * @return string
129
  */
130
  public function fetchLastname()
@@ -133,167 +147,238 @@ class Copernica_MarketingSoftware_Model_Copernica_Entity_Customer extends Copern
133
  }
134
 
135
  /**
136
- * Get customer email
 
137
  * @return string
138
  */
139
  public function fetchEmail()
140
  {
141
- return $this->customer->getEmail();
142
  }
143
 
144
  /**
145
- * Get registration date of a customer
 
146
  * @return string
147
  */
148
  public function fetchRegistrationDate()
149
  {
150
- return date('Y-m-d H:i:s', $this->customer->getCreatedAtTimestamp());
151
  }
152
 
153
  /**
154
  * Fetch newsletter status
 
155
  * @return string
156
  */
157
  public function fetchNewsletter()
158
  {
159
- // get magento subscriber model
160
- $subscriber = Mage::getModel('newsletter/subscriber')->loadByCustomer($this->customer);
161
 
162
- // if thre is no subscriber we just will return unknown status
163
- if (!$subscriber->getId()) return 'unknown';
 
164
 
165
- // return diffetent result depending on subscriber status
166
- switch($subscriber->getStatus())
167
- {
168
  case Mage_Newsletter_Model_Subscriber::STATUS_SUBSCRIBED:
169
  return 'subscribed';
 
170
  case Mage_Newsletter_Model_Subscriber::STATUS_NOT_ACTIVE:
171
  return 'not active';
 
172
  case Mage_Newsletter_Model_Subscriber::STATUS_UNSUBSCRIBED:
173
  return 'unsubscribed';
 
174
  case Mage_Newsletter_Model_Subscriber::STATUS_UNCONFIRMED:
175
  return 'unconfirmed';
 
176
  default:
177
  return 'unknown';
178
  }
179
  }
180
 
181
  /**
182
- * Get customer gender
 
183
  * @return string
184
  */
185
  public function fetchGender()
186
  {
187
- // get gender options
188
- $options = $this->customer->getAttribute('gender')->getSource()->getAllOptions();
189
 
190
- // get customer idx
191
- $customerGenderIdx = $this->customer->getGender();
192
 
193
- // iterater over all options to get proper gender
194
- foreach ($options as $option)
195
- {
196
- if ($option['value'] == $customerGenderIdx) return $option['label'];
197
  }
198
 
199
- // we don't know
200
  return 'unknown';
201
  }
202
 
203
  /**
204
- * Get customer date of birth
 
205
  * @return string
206
  */
207
  public function getBirthDate()
208
  {
209
- return $this->customer->getDob();
210
  }
211
 
212
  /**
213
  * Return all customer addresses
 
214
  * @return array
215
  */
216
  public function getAddresses()
217
  {
218
- if (!is_null($this->addresses)) return $this->addresses;
 
 
219
 
220
- // data holder for customer addresses
221
  $addresses = array();
222
 
223
- // create copernica entities from addresses
224
- foreach ($this->customer->getAddresses() as $address)
225
- {
226
- $addresses[] = new Copernica_MarketingSoftware_Model_Copernica_Entity_Address($address);
 
227
  }
228
 
229
- // cache and return all customer addresses
230
- return $this->addresses = $addresses;
231
  }
232
 
233
  /**
234
  * Get all customer orders
 
235
  * @return array
236
  */
237
  public function getOrders()
238
  {
239
- // check if we did already cached orders
240
- if (!is_null($this->orders)) return $this->orders;
 
241
 
242
- // data holder for customer orders
243
  $orders = array();
244
 
245
- // get collection with all customers orders
246
- $ordersCollection = Mage::getModel('sales/order')->getCollection()->addFieldToFilter('customer_id', $this->customer->getId());
247
 
248
- // iterate over all orders
249
- foreach ($ordersCollection as $order)
250
- {
251
- $orders[] = new Copernica_MarketingSoftware_Model_Copernica_Entity_Order($order);
 
252
  }
253
 
254
- // return all customer orders and cache them
255
- return $orders;
256
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
257
 
258
  /**
259
- * Get customer group
 
260
  * @return string
261
  */
262
  public function fetchGroup()
263
  {
264
- // fetch customer group
265
- return Mage::getModel('customer/group')->load($this->customer->getGroupId())->getCode();
266
  }
267
 
268
  /**
269
- * Get profile
270
- * @return Copernica_MarketingSoftware_Model_REST_Customer
 
271
  */
272
- public function getREST()
273
  {
274
- return new Copernica_MarketingSoftware_Model_REST_Customer($this);
 
 
 
275
  }
276
 
277
  /**
278
  * Get profile Id
 
 
 
279
  * @return string|false
280
  */
281
  public function getProfileId()
282
  {
283
- // check if we already stored profile Id
284
- if (!is_null($this->profileId)) return $this->profileId;
285
-
286
- // try to fetch profile Id from API helper
287
  $profileId = Mage::helper('marketingsoftware/api')->getProfileId(array(
288
  'id' => $this->getCustomerId(),
289
  'email' => $this->getEmail(),
290
- 'storeView' => strval($this->getStoreView())
291
  ));
 
 
 
 
292
 
293
- // if we have a profile Id we can cache it and return it
294
- if ($profileId) return $this->profileId = $profileId;
295
-
296
- // we don't have any sensible info, so just return false
297
  return false;
298
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
299
  }
31
  {
32
  /**
33
  * Magento customer
34
+ *
35
+ * @var Mage_Customer_Model_Customer
36
  */
37
+ protected $_customer = null;
38
 
39
  /**
40
  * Array of customer addresses
41
+ *
42
+ * @var array
43
  */
44
+ protected $_addresses = null;
45
 
46
  /**
47
  * Array of customer orders
48
+ *
49
+ * @var array
50
  */
51
+ protected $_orders = null;
52
+
53
+
54
+ /**
55
+ * Array of customer wishlist items
56
+ *
57
+ * @var array
58
+ */
59
+ protected $_wishlistItems = null;
60
 
61
  /**
62
  * Cache profile Id
63
+ *
64
+ * @var string
65
  */
66
+ protected $_profileId = null;
67
+
68
+
69
  /**
70
+ *
71
+ * @var Copernica_MarketingSoftware_Model_Abstraction_Storeview
72
  */
73
+ protected $_store = null;
 
 
 
 
 
 
 
 
 
 
74
 
75
  /**
76
+ * Fetch store view
77
+ *
78
+ * @return Copernica_MarketingSoftware_Model_Abstraction_Storeview
79
  */
80
  public function fetchStoreView()
81
+ {
82
+ if ($this->_store) {
83
+ return $this->_store;
84
+ } else {
85
+ return Mage::getModel('marketingsoftware/abstraction_storeview')->setOriginal($this->_customer->getStore());
86
+ }
87
  }
88
 
89
  /**
90
+ * Fetch magento customer id
91
+ *
92
  * @return string
93
  */
94
  public function fetchId()
95
  {
96
+ return $this->_customer->getId();
97
  }
98
 
99
  /**
100
+ * Our unique customer ID in the form of customer_ID|storeView_ID
101
+ *
102
  * @return string
103
  */
104
  public function fetchCustomerId()
107
  }
108
 
109
  /**
110
+ * Fetch name
111
+ *
112
  * @return Copernica_MarketingSoftware_Model_Abstraction_Name
113
  */
114
  public function fetchName()
115
  {
116
+ return Mage::getModel('marketingsoftware/abstraction_name')->setOriginal($this->_customer);
117
  }
118
 
119
  /**
120
+ * Fetch firstname
121
+ *
122
  * @return string
123
  */
124
  public function fetchFirstname()
127
  }
128
 
129
  /**
130
+ * Fetch middlename
131
+ *
132
  * @return string
133
  */
134
  public function fetchMiddlename()
137
  }
138
 
139
  /**
140
+ * Fetch lastname
141
+ *
142
  * @return string
143
  */
144
  public function fetchLastname()
147
  }
148
 
149
  /**
150
+ * Fetch email
151
+ *
152
  * @return string
153
  */
154
  public function fetchEmail()
155
  {
156
+ return $this->_customer->getEmail();
157
  }
158
 
159
  /**
160
+ * Fetch registration date
161
+ *
162
  * @return string
163
  */
164
  public function fetchRegistrationDate()
165
  {
166
+ return date('Y-m-d H:i:s', $this->_customer->getCreatedAtTimestamp());
167
  }
168
 
169
  /**
170
  * Fetch newsletter status
171
+ *
172
  * @return string
173
  */
174
  public function fetchNewsletter()
175
  {
176
+ $subscriber = Mage::getModel('newsletter/subscriber')->loadByCustomer($this->_customer);
 
177
 
178
+ if (!$subscriber->getId()) {
179
+ return 'unknown';
180
+ }
181
 
182
+ switch($subscriber->getStatus()) {
 
 
183
  case Mage_Newsletter_Model_Subscriber::STATUS_SUBSCRIBED:
184
  return 'subscribed';
185
+
186
  case Mage_Newsletter_Model_Subscriber::STATUS_NOT_ACTIVE:
187
  return 'not active';
188
+
189
  case Mage_Newsletter_Model_Subscriber::STATUS_UNSUBSCRIBED:
190
  return 'unsubscribed';
191
+
192
  case Mage_Newsletter_Model_Subscriber::STATUS_UNCONFIRMED:
193
  return 'unconfirmed';
194
+
195
  default:
196
  return 'unknown';
197
  }
198
  }
199
 
200
  /**
201
+ * Fetch gender
202
+ *
203
  * @return string
204
  */
205
  public function fetchGender()
206
  {
207
+ $options = $this->_customer->getAttribute('gender')->getSource()->getAllOptions();
 
208
 
209
+ $customerGenderIdx = $this->_customer->getGender();
 
210
 
211
+ foreach ($options as $option) {
212
+ if ($option['value'] == $customerGenderIdx) {
213
+ return $option['label'];
214
+ }
215
  }
216
 
 
217
  return 'unknown';
218
  }
219
 
220
  /**
221
+ * Fetch date of birth
222
+ *
223
  * @return string
224
  */
225
  public function getBirthDate()
226
  {
227
+ return $this->_customer->getDob();
228
  }
229
 
230
  /**
231
  * Return all customer addresses
232
+ *
233
  * @return array
234
  */
235
  public function getAddresses()
236
  {
237
+ if (!is_null($this->_addresses)) {
238
+ return $this->_addresses;
239
+ }
240
 
 
241
  $addresses = array();
242
 
243
+ foreach ($this->_customer->getAddresses() as $address) {
244
+ $addressEntity = Mage::getModel('marketingsoftware/copernica_entity_address');
245
+ $addressEntity->setAddress($address);
246
+
247
+ $addresses[] = $addressEntity;
248
  }
249
 
250
+ return $this->_addresses = $addresses;
 
251
  }
252
 
253
  /**
254
  * Get all customer orders
255
+ *
256
  * @return array
257
  */
258
  public function getOrders()
259
  {
260
+ if (!is_null($this->_orders)) {
261
+ return $this->_orders;
262
+ }
263
 
 
264
  $orders = array();
265
 
266
+ $ordersCollection = Mage::getModel('sales/order')->getCollection()->addFieldToFilter('customer_id', $this->_customer->getId());
 
267
 
268
+ foreach ($ordersCollection as $order) {
269
+ $orderEntity = Mage::getModel('marketingsoftware/copernica_entity_order');
270
+ $orderEntity->setOrder($order);
271
+
272
+ $orders[] = $orderEntity;
273
  }
274
 
275
+ return $this->_orders = $orders;
 
276
  }
277
+
278
+ /**
279
+ * Get all customer wishlist items
280
+ *
281
+ * @return array
282
+ */
283
+ public function getWishlistItems()
284
+ {
285
+ if (!is_null($this->_wishlistItems)) {
286
+ return $this->_wishlistItems;
287
+ }
288
+
289
+ $wishlistItems = array();
290
+
291
+ $wishlist = Mage::getModel('wishlist/wishlist')->loadByCustomer($this->getId());
292
+
293
+ $wishlistItemCollection = Mage::getModel('wishlist/item')->getCollection()->addFieldToFilter('wishlist_id', $wishlist->getId());
294
+
295
+ foreach ($wishlistItemCollection as $wishlistItem) {
296
+ $wishlistItemEntity = Mage::getModel('marketingsoftware/copernica_entity_wishlist_item');
297
+ $wishlistItemEntity->setWishlistItem($wishlistItem);
298
+
299
+ $wishlistItems[] = $wishlistItemEntity;
300
+ }
301
+
302
+ return $this->_wishlistItems = $wishlistItems;
303
+ }
304
 
305
  /**
306
+ * Fetch group
307
+ *
308
  * @return string
309
  */
310
  public function fetchGroup()
311
  {
312
+ return Mage::getModel('customer/group')->load($this->_customer->getGroupId())->getCode();
 
313
  }
314
 
315
  /**
316
+ * Get REST customer
317
+ *
318
+ * @return Copernica_MarketingSoftware_Model_Rest_Customer
319
  */
320
+ public function getRestCustomer()
321
  {
322
+ $restCustomer = Mage::getModel('marketingsoftware/rest_customer');
323
+ $restCustomer->setCustomerEntity($this);
324
+
325
+ return $restCustomer;
326
  }
327
 
328
  /**
329
  * Get profile Id
330
+ *
331
+ * @param string $storeviewText
332
+ * @param int $id
333
  * @return string|false
334
  */
335
  public function getProfileId()
336
  {
337
+ if (!is_null($this->_profileId)) {
338
+ return $this->_profileId;
339
+ }
340
+
341
  $profileId = Mage::helper('marketingsoftware/api')->getProfileId(array(
342
  'id' => $this->getCustomerId(),
343
  'email' => $this->getEmail(),
344
+ 'storeView' => strval($this->getStoreView()),
345
  ));
346
+
347
+ if ($profileId) {
348
+ return $this->_profileId = $profileId;
349
+ }
350
 
 
 
 
 
351
  return false;
352
  }
353
+
354
+ /**
355
+ * Set customer entity
356
+ *
357
+ * @param int $customerId
358
+ */
359
+ public function setCustomer($customerId)
360
+ {
361
+ $customer = Mage::getModel('customer/customer')->load($customerId);
362
+
363
+ if (!$customer->isObjectNew()) {
364
+ $this->_customer = $customer;
365
+ } else {
366
+ throw Mage::exception('Copernica_MarketingSoftware', 'Customer does not exists', Copernica_MarketingSoftware_Exception::CUSTOMER_NOT_EXISTS);
367
+ }
368
+ }
369
+
370
+ /**
371
+ * Set the storeView
372
+ *
373
+ * @param Copernica_MarketingSoftware_Model_Abstraction_Storeview $store
374
+ */
375
+ public function setStore($store)
376
+ {
377
+ if (!$this->_store || $this->_store->id() != $store->id()) {
378
+ unset($this->_data['storeView']);
379
+ unset($this->_data['customerId']);
380
+ $this->_profileId = null;
381
+ $this->_store = $store;
382
+ }
383
+ }
384
  }
app/code/community/Copernica/MarketingSoftware/Model/Copernica/Entity/Item.php DELETED
@@ -1,218 +0,0 @@
1
- <?php
2
- /**
3
- * Copernica Marketing Software
4
- *
5
- * NOTICE OF LICENSE
6
- *
7
- * This source file is subject to the Open Software License (OSL 3.0).
8
- * It is available through the world-wide-web at this URL:
9
- * http://opensource.org/licenses/osl-3.0.php
10
- * If you are unable to obtain a copy of the license through the
11
- * world-wide-web, please send an email to copernica@support.cream.nl
12
- * so we can send you a copy immediately.
13
- *
14
- * DISCLAIMER
15
- *
16
- * Do not edit or add to this file if you wish to upgrade this software
17
- * to newer versions in the future. If you wish to customize this module
18
- * for your needs please refer to http://www.magento.com/ for more
19
- * information.
20
- *
21
- * @category Copernica
22
- * @package Copernica_MarketingSoftware
23
- * @copyright Copyright (c) 2011-2012 Copernica & Cream. (http://docs.cream.nl/)
24
- * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
- */
26
-
27
- /**
28
- * Bridge class between magento item and copernica subprofile
29
- */
30
- class Copernica_MarketingSoftware_Model_Copernica_Entity_Item extends Copernica_MarketingSoftware_Model_Copernica_Entity_Product
31
- {
32
- /**
33
- * Cached item
34
- * @var Mage_Sales_Model_Order_Item
35
- */
36
- protected $item = null;
37
-
38
- /**
39
- * Construct item
40
- * @param Mage_Sales_Model_Order_Item|Mage_Sales_Model_Quote_Item
41
- */
42
- public function __construct($item)
43
- {
44
- $this->item = $item;
45
-
46
- /**
47
- * We have to fetch product from catalog module. It's very important to
48
- * ask catalog rather than item. Item model has also a method getProduct()
49
- * thus it will fail with a fatal error only cause item can not fetch a
50
- * quote and fetch a store view from that quote. It's not that quote is
51
- * not existing, data is there and it can be accessed in certain situations
52
- * thus by asking sales module directly or item (with getQuote() method)
53
- * it will return new object instead proper quote. That is why we want
54
- * to ask for product directly, to not deal with all that
55
- * not-so-well-designed API.
56
- */
57
- $this->product = Mage::getModel('catalog/product')->load($item->getProductId());
58
- }
59
-
60
- /**
61
- * Fetch quantity
62
- * @return string
63
- */
64
- public function fetchQuantity()
65
- {
66
- if ($this->item instanceOf Mage_Sales_Model_Quote_Item) return $this->item->getQty();
67
-
68
- return $this->item->getQtyOrdered();
69
- }
70
-
71
- /**
72
- * Fetch price
73
- * @return Copernia_MarketingSoftware_Model_Abstraction_Price
74
- */
75
- public function fetchFullPrice()
76
- {
77
- return Mage::getModel('marketingsoftware/abstraction_price')->setOriginal($this->item);
78
- }
79
-
80
- /**
81
- * Fetch total
82
- * @return string
83
- */
84
- public function fetchTotalPrice()
85
- {
86
- return $this->getFullPrice()->total();
87
- }
88
-
89
- /**
90
- * Fetch price
91
- * @return string
92
- */
93
- public function fetchPrice()
94
- {
95
- return $this->getFullPrice()->itemPrice();
96
- }
97
-
98
- /**
99
- * Fetch timestamp
100
- * @return string
101
- */
102
- public function fetchTimestamp()
103
- {
104
- return $this->item->getUpdatedAt();
105
- }
106
-
107
- /**
108
- * Fetch store view
109
- * @return string
110
- */
111
- public function fetchStoreView()
112
- {
113
- // placeholder for store model
114
- $store = null;
115
-
116
- // get store Id
117
- $store = Mage::getModel('core/store')->load($this->getStoreId());
118
-
119
- // if we don't have a store we are just about done here
120
- if (is_null($store)) return '';
121
-
122
- // parse store to string
123
- return implode(' > ', array(
124
- $store->getWebsite()->getName(),
125
- $store->getGroup()->getName(),
126
- $store->getName())
127
- );
128
- }
129
-
130
- /**
131
- * Get store Id
132
- * @return int
133
- */
134
- public function getStoreId()
135
- {
136
- // get store
137
- if ($this->item instanceof Mage_Sales_Model_Quote_Item) return $this->item->getStoreId();
138
- elseif ($this->item instanceof Mage_Sales_Model_Order_Item) return $this->item->getOrder()->getStoreId();
139
-
140
- // let's go with admin option
141
- return 0;
142
- }
143
-
144
- /**
145
- * Fetch options that were set with this item.
146
- * @return string
147
- */
148
- public function fetchOptions()
149
- {
150
- // get all options
151
- $options = $this->product->getTypeInstance(true)->getOrderOptions($this->product);
152
-
153
- // options that we have to convert to string
154
- $neededOptions = array();
155
-
156
- /**
157
- * Depending on what kind of item/situation we have we have to fetch
158
- * differente options to parse.
159
- */
160
- if (isset($options['attributes_info']))
161
- {
162
- $neededOptions = $options['attributes_info'];
163
- }
164
- elseif (isset($options['bundle_options']))
165
- {
166
- $neededOptions = $options['bundle_options'];
167
- }
168
- elseif (isset($options['options']))
169
- {
170
- $neeededOptions = $options['options'];
171
- }
172
-
173
- // stringify options and return them
174
- return $this->stringifyOptions($neededOptions);
175
- }
176
-
177
- /**
178
- * Get ids of sales rules used for this product.
179
- * @return string
180
- */
181
- public function fetchSalesRules()
182
- {
183
- return $this->item->getAppliedRuleIds();
184
- }
185
-
186
- /**
187
- * Options ca be nested so this function will allow us to parse them in
188
- * recursive manner.
189
- * @param mixed
190
- * @param string
191
- * @return string
192
- */
193
- private function stringifyOptions($values, $prefix = '')
194
- {
195
- $result = '';
196
- foreach ($values as $value)
197
- {
198
- if (is_array($value['value']))
199
- {
200
- if (isset($value['value'][0]) && count($value['value']) == 1) $value['value'] = $value['value'][0];
201
-
202
- $result .= $prefix.$value['label'].":\n".$this->stringifyOptions($value['value'], $prefix.' ');
203
- }
204
- else $result .= $prefix.$value['label'].":".$value['value']."\n";
205
- }
206
-
207
- return $result;
208
- }
209
-
210
- /**
211
- * Get REST entity
212
- * @return Copernica_MarketingSoftware_Model_REST_Item
213
- */
214
- public function getREST()
215
- {
216
- return new Copernica_MarketingSoftware_Model_REST_Item($this);
217
- }
218
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app/code/community/Copernica/MarketingSoftware/Model/Copernica/Entity/Order.php CHANGED
@@ -31,81 +31,81 @@ class Copernica_MarketingSoftware_Model_Copernica_Entity_Order extends Copernica
31
  {
32
  /**
33
  * Cached order instance
34
- * @var Mage_Sales_Model_Order
 
35
  */
36
- private $order = null;
37
 
38
  /**
39
  * Cache for order items
40
- * @var array
 
41
  */
42
- private $items = null;
43
-
44
- /**
45
- * Construct copernica order
46
- * @param Mage_Sales_Model_Order
47
- */
48
- public function __construct($order)
49
- {
50
- $this->order = $order;
51
- }
52
 
53
  /**
54
  * Fetch order Id
 
55
  * @return string
56
  */
57
  public function fetchId()
58
  {
59
- return $this->order->getId();
60
  }
61
 
62
  /**
63
  * Fetch increment Id
 
64
  * @return string
65
  */
66
  public function fetchIncrementId()
67
  {
68
- return $this->order->getIncrementId();
69
  }
70
 
71
  /**
72
  * Fetch quote Id
 
73
  * @return string
74
  */
75
  public function fetchQuoteId()
76
  {
77
- return $this->order->getQuoteId();
78
  }
79
 
80
  /**
81
  * Fetch status
 
82
  * @return string
83
  */
84
  public function fetchStatus()
85
  {
86
- return $this->order->getStatus();
87
  }
88
 
89
  /**
90
  * Fetch state
 
91
  * @return string
92
  */
93
  public function fetchState()
94
  {
95
- return $this->order->getState();
96
  }
97
 
98
  /**
99
  * Fetch quantity
 
100
  * @return string
101
  */
102
  public function fetchQuantity()
103
  {
104
- return $this->order->getTotalQtyOrdered();
105
  }
106
 
107
  /**
108
  * Fetch currency
 
109
  * @return string
110
  */
111
  public function fetchCurrency()
@@ -115,16 +115,18 @@ class Copernica_MarketingSoftware_Model_Copernica_Entity_Order extends Copernica
115
 
116
  /**
117
  * Fetch prive
 
118
  * @return string
119
  */
120
  public function fetchPrice()
121
  {
122
  // @todo really? new class to parse a price ?
123
- return Mage::getModel('marketingsoftware/abstraction_price')->setOriginal($this->order);
124
  }
125
 
126
  /**
127
  * Fetch shipping cost
 
128
  * @return string
129
  */
130
  public function fetchShipping()
@@ -134,6 +136,7 @@ class Copernica_MarketingSoftware_Model_Copernica_Entity_Order extends Copernica
134
 
135
  /**
136
  * Fetch total prive
 
137
  * @return string
138
  */
139
  public function fetchTotal()
@@ -143,63 +146,66 @@ class Copernica_MarketingSoftware_Model_Copernica_Entity_Order extends Copernica
143
 
144
  /**
145
  * Fetch store view
 
146
  * @return string
147
  */
148
  public function fetchStoreView()
149
  {
150
- return (string)Mage::getModel('marketingsoftware/abstraction_storeview')->setOriginal($this->order->getStore());
151
  }
152
 
153
  /**
154
  * Fetch weight
 
155
  * @return string
156
  */
157
  public function fetchWeight()
158
  {
159
- return $this->order->getWeight();
160
  }
161
 
162
  /**
163
  * Fetch timestamp
 
164
  * @return string
165
  */
166
  public function fetchTimestamp()
167
  {
168
- return $this->order->getUpdatedAt();
169
  }
170
 
171
  /**
172
  * Fetch shipping description
 
173
  * @return string
174
  */
175
  public function fetchShippingDescription()
176
  {
177
- return $this->order->getShippingDescription();
178
  }
179
 
180
  /**
181
  * Fetch customer IP
 
182
  * @return string
183
  */
184
  public function fetchRemoteIp()
185
  {
186
- return $this->order->getRemoteIp();
187
  }
188
 
189
  /**
190
  * Fetch payment description
 
191
  * @return string
192
  */
193
  public function fetchPaymentDescription()
194
  {
195
- if ($payment = $this->order->getPayment())
196
- {
197
  try {
198
  if ($payment->getMethod() == 'klarna_partpayment') return 'Klarna';
199
  else return $payment->getMethodInstance()->getTitle();
200
- }
201
-
202
- catch (Mage_Core_Exception $exception) { }
203
  }
204
 
205
  return '';
@@ -213,16 +219,17 @@ class Copernica_MarketingSoftware_Model_Copernica_Entity_Order extends Copernica
213
  */
214
  public function fetchShippingAddress()
215
  {
216
- // get address from order model
217
- $address = $this->order->getShippingAddress();
218
 
219
- // check if we have an address model
220
- if (is_object($address)) return new Copernica_MarketingSoftware_Model_Copernica_Entity_Address($address);
 
 
 
 
221
 
222
- // get addresses collection
223
- // $addresses = $this->order->getAddressesCollection();
224
 
225
- // we can not pinpoint a shipping address
226
  return null;
227
  }
228
 
@@ -230,104 +237,128 @@ class Copernica_MarketingSoftware_Model_Copernica_Entity_Order extends Copernica
230
  * Get billing address. When no valid address can be found this method will
231
  * return null value.
232
  *
233
- * @return Copernica_MarketingSoftware_Model_Copernica_Entity_Address
234
  */
235
  public function fetchBillingAddress()
236
  {
237
- // get address from order model
238
- $address = $this->order->getBillingAddress();
239
 
240
- // check if we have an address model
241
- if (is_object($address)) return new Copernica_MarketingSoftware_Model_Copernica_Entity_Address($address);
 
 
 
 
242
 
243
- // get customer Id
244
- // foreach($this->order->getAddressCollection() as $magentoAddress)
245
- // {
246
  // $address = new Copernica_MarketingSoftware_Model_Copernica_Entity_Address($magentoAddress);
247
  // }
248
 
249
- // we can not pinpoint a shipping address
250
  return null;
251
  }
252
 
253
  /**
254
- * Get shipping address Id
 
255
  * @return string
256
  */
257
  public function fetchShippingAddressId()
258
  {
259
- if (is_object($address = $this->getShippingAddress())) return $address->getId();
 
 
260
 
261
  return null;
262
  }
263
 
264
  /**
265
- * Get shipping address Id
 
266
  * @return string
267
  */
268
  public function fetchBillingAddressId()
269
  {
270
- if (is_object($address = $this->getBillingAddress())) return $address->getId();
 
 
271
 
272
  return null;
273
  }
274
 
275
  /**
276
  * Return array of all orders items
 
277
  * @return array
278
  */
279
  public function getItems()
280
  {
281
- // return cached items
282
- if (!is_null($this->items)) return $this->items;
 
283
 
284
- // placeholder for items
285
  $data = array();
286
 
287
- // conver all items to copernica entities
288
- foreach ($this->order->getAllItems() as $item) $data[] = new Copernica_MarketingSoftware_Model_Copernica_Entity_Item($item);
 
 
 
 
289
 
290
- // return and cache items
291
- return $this->items = $data;
292
  }
293
 
294
  /**
295
  * Return array of all addresses
 
296
  * @return array
297
  */
298
  public function getAddresses()
299
  {
300
- // get all order addresses
301
- $addresses = Mage::getModel('sales/order_address')->getCollection()->addFieldToFilter('order_id', $this->order->getId());
302
 
303
- // get all addresses
304
  $convertedAddresses = array();
305
 
306
- // iterate over all addresses and convert them to extension entities
307
- foreach ($addresses as $address)
308
- {
309
- $convertedAddresses[] = new Copernica_MarketingSoftware_Model_Copernica_Entity_Address($address);
 
310
  }
311
 
312
- // return all addresses
313
  return $convertedAddresses;
314
  }
315
 
316
  /**
317
  * Return coupon code that was used when finalizing this order.
 
318
  * @return string
319
  */
320
  public function fetchCouponCode()
321
  {
322
- // get coupon code
323
- return $this->order->getCouponCode();
324
  }
325
 
326
  /**
327
- * Get RESTEntity for this order.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
328
  */
329
- public function getREST()
330
  {
331
- return new Copernica_MarketingSoftware_Model_REST_Order($this);
332
  }
333
  }
31
  {
32
  /**
33
  * Cached order instance
34
+ *
35
+ * @var Mage_Sales_Model_Order
36
  */
37
+ protected $_order = null;
38
 
39
  /**
40
  * Cache for order items
41
+ *
42
+ * @var array
43
  */
44
+ protected $_items = null;
 
 
 
 
 
 
 
 
 
45
 
46
  /**
47
  * Fetch order Id
48
+ *
49
  * @return string
50
  */
51
  public function fetchId()
52
  {
53
+ return $this->_order->getId();
54
  }
55
 
56
  /**
57
  * Fetch increment Id
58
+ *
59
  * @return string
60
  */
61
  public function fetchIncrementId()
62
  {
63
+ return $this->_order->getIncrementId();
64
  }
65
 
66
  /**
67
  * Fetch quote Id
68
+ *
69
  * @return string
70
  */
71
  public function fetchQuoteId()
72
  {
73
+ return $this->_order->getQuoteId();
74
  }
75
 
76
  /**
77
  * Fetch status
78
+ *
79
  * @return string
80
  */
81
  public function fetchStatus()
82
  {
83
+ return $this->_order->getStatus();
84
  }
85
 
86
  /**
87
  * Fetch state
88
+ *
89
  * @return string
90
  */
91
  public function fetchState()
92
  {
93
+ return $this->_order->getState();
94
  }
95
 
96
  /**
97
  * Fetch quantity
98
+ *
99
  * @return string
100
  */
101
  public function fetchQuantity()
102
  {
103
+ return $this->_order->getTotalQtyOrdered();
104
  }
105
 
106
  /**
107
  * Fetch currency
108
+ *
109
  * @return string
110
  */
111
  public function fetchCurrency()
115
 
116
  /**
117
  * Fetch prive
118
+ *
119
  * @return string
120
  */
121
  public function fetchPrice()
122
  {
123
  // @todo really? new class to parse a price ?
124
+ return Mage::getModel('marketingsoftware/abstraction_price')->setOriginal($this->_order);
125
  }
126
 
127
  /**
128
  * Fetch shipping cost
129
+ *
130
  * @return string
131
  */
132
  public function fetchShipping()
136
 
137
  /**
138
  * Fetch total prive
139
+ *
140
  * @return string
141
  */
142
  public function fetchTotal()
146
 
147
  /**
148
  * Fetch store view
149
+ *
150
  * @return string
151
  */
152
  public function fetchStoreView()
153
  {
154
+ return Mage::getModel('marketingsoftware/abstraction_storeview')->setOriginal($this->_order->getStore());
155
  }
156
 
157
  /**
158
  * Fetch weight
159
+ *
160
  * @return string
161
  */
162
  public function fetchWeight()
163
  {
164
+ return $this->_order->getWeight();
165
  }
166
 
167
  /**
168
  * Fetch timestamp
169
+ *
170
  * @return string
171
  */
172
  public function fetchTimestamp()
173
  {
174
+ return $this->_order->getUpdatedAt();
175
  }
176
 
177
  /**
178
  * Fetch shipping description
179
+ *
180
  * @return string
181
  */
182
  public function fetchShippingDescription()
183
  {
184
+ return $this->_order->getShippingDescription();
185
  }
186
 
187
  /**
188
  * Fetch customer IP
189
+ *
190
  * @return string
191
  */
192
  public function fetchRemoteIp()
193
  {
194
+ return $this->_order->getRemoteIp();
195
  }
196
 
197
  /**
198
  * Fetch payment description
199
+ *
200
  * @return string
201
  */
202
  public function fetchPaymentDescription()
203
  {
204
+ if ($payment = $this->_order->getPayment()) {
 
205
  try {
206
  if ($payment->getMethod() == 'klarna_partpayment') return 'Klarna';
207
  else return $payment->getMethodInstance()->getTitle();
208
+ } catch (Mage_Core_Exception $exception) { }
 
 
209
  }
210
 
211
  return '';
219
  */
220
  public function fetchShippingAddress()
221
  {
222
+ $address = $this->_order->getShippingAddress();
 
223
 
224
+ if (is_object($address)) {
225
+ $addressEntity = Mage::getModel('marketingsoftware/copernica_entity_address');
226
+ $addressEntity->setAddress($address);
227
+
228
+ return $addressEntity;
229
+ }
230
 
231
+ // $addresses = $this->_order->getAddressesCollection();
 
232
 
 
233
  return null;
234
  }
235
 
237
  * Get billing address. When no valid address can be found this method will
238
  * return null value.
239
  *
240
+ * @return Copernica_MarketingSoftware_Model_Copernica_Entity_Address
241
  */
242
  public function fetchBillingAddress()
243
  {
244
+ $address = $this->_order->getBillingAddress();
 
245
 
246
+ if (is_object($address)) {
247
+ $addressEntity = Mage::getModel('marketingsoftware/copernica_entity_address');
248
+ $addressEntity->setAddress($address);
249
+
250
+ return $addressEntity;
251
+ }
252
 
253
+ // foreach($this->_order->getAddressCollection() as $magentoAddress) {
 
 
254
  // $address = new Copernica_MarketingSoftware_Model_Copernica_Entity_Address($magentoAddress);
255
  // }
256
 
 
257
  return null;
258
  }
259
 
260
  /**
261
+ * Fetch shipping address id
262
+ *
263
  * @return string
264
  */
265
  public function fetchShippingAddressId()
266
  {
267
+ if (is_object($address = $this->getShippingAddress())) {
268
+ return $address->getId();
269
+ }
270
 
271
  return null;
272
  }
273
 
274
  /**
275
+ * Fetch billing address Id
276
+ *
277
  * @return string
278
  */
279
  public function fetchBillingAddressId()
280
  {
281
+ if (is_object($address = $this->getBillingAddress())) {
282
+ return $address->getId();
283
+ }
284
 
285
  return null;
286
  }
287
 
288
  /**
289
  * Return array of all orders items
290
+ *
291
  * @return array
292
  */
293
  public function getItems()
294
  {
295
+ if (!is_null($this->_items)) {
296
+ return $this->_items;
297
+ }
298
 
 
299
  $data = array();
300
 
301
+ foreach ($this->_order->getAllVisibleItems() as $orderItem) {
302
+ $orderItemEntity = Mage::getModel('marketingsoftware/copernica_entity_order_item');
303
+ $orderItemEntity->setOrderItem($orderItem);
304
+
305
+ $data[] = $orderItemEntity;
306
+ }
307
 
308
+ return $this->_items = $data;
 
309
  }
310
 
311
  /**
312
  * Return array of all addresses
313
+ *
314
  * @return array
315
  */
316
  public function getAddresses()
317
  {
318
+ $addresses = Mage::getModel('sales/order_address')->getCollection()->addFieldToFilter('order_id', $this->_order->getId());
 
319
 
 
320
  $convertedAddresses = array();
321
 
322
+ foreach ($addresses as $address) {
323
+ $addressEntity = Mage::getModel('marketingsoftware/copernica_entity_address');
324
+ $addressEntity->setAddress($address);
325
+
326
+ $convertedAddresses[] = $addressEntity;
327
  }
328
 
 
329
  return $convertedAddresses;
330
  }
331
 
332
  /**
333
  * Return coupon code that was used when finalizing this order.
334
+ *
335
  * @return string
336
  */
337
  public function fetchCouponCode()
338
  {
339
+ return $this->_order->getCouponCode();
 
340
  }
341
 
342
  /**
343
+ * Get REST order
344
+ *
345
+ * @return Copernica_MarketingSoftware_Model_Rest_Order
346
+ */
347
+ public function getRestOrder()
348
+ {
349
+ $restOrder = Mage::getModel('marketingsoftware/rest_order');
350
+ $restOrder->setOrderEntity($this);
351
+
352
+ return $restOrder;
353
+ }
354
+
355
+ /**
356
+ * Set copernica order
357
+ *
358
+ * @param Mage_Sales_Model_Order $order
359
  */
360
+ public function setOrder($order)
361
  {
362
+ $this->_order = $order;
363
  }
364
  }
app/code/community/Copernica/MarketingSoftware/Model/Copernica/Entity/Order/Item.php ADDED
@@ -0,0 +1,195 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Copernica Marketing Software
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0).
8
+ * It is available through the world-wide-web at this URL:
9
+ * http://opensource.org/licenses/osl-3.0.php
10
+ * If you are unable to obtain a copy of the license through the
11
+ * world-wide-web, please send an email to copernica@support.cream.nl
12
+ * so we can send you a copy immediately.
13
+ *
14
+ * DISCLAIMER
15
+ *
16
+ * Do not edit or add to this file if you wish to upgrade this software
17
+ * to newer versions in the future. If you wish to customize this module
18
+ * for your needs please refer to http://www.magento.com/ for more
19
+ * information.
20
+ *
21
+ * @category Copernica
22
+ * @package Copernica_MarketingSoftware
23
+ * @copyright Copyright (c) 2011-2012 Copernica & Cream. (http://docs.cream.nl/)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ /**
28
+ * Bridge class between magento item and copernica subprofile
29
+ */
30
+ class Copernica_MarketingSoftware_Model_Copernica_Entity_Order_Item extends Copernica_MarketingSoftware_Model_Copernica_Entity_Product
31
+ {
32
+ /**
33
+ * Cached item
34
+ *
35
+ * @var Mage_Sales_Model_Order_Item|Mage_Sales_Model_Quote_Item
36
+ */
37
+ protected $_orderItem = null;
38
+
39
+ /**
40
+ * Get item Id
41
+ *
42
+ * @return string
43
+ */
44
+ public function fetchId()
45
+ {
46
+ return $this->_orderItem->getId();
47
+ }
48
+
49
+ /**
50
+ * Fetch quantity
51
+ *
52
+ * @return string
53
+ */
54
+ public function fetchQuantity()
55
+ {
56
+ if ($this->_orderItem instanceOf Mage_Sales_Model_Quote_Item) {
57
+ return $this->_orderItem->getQty();
58
+ }
59
+
60
+ return $this->_orderItem->getQtyOrdered();
61
+ }
62
+
63
+ /**
64
+ * Fetch price
65
+ *
66
+ * @return Copernia_MarketingSoftware_Model_Abstraction_Price
67
+ */
68
+ public function fetchFullPrice()
69
+ {
70
+ return Mage::getModel('marketingsoftware/abstraction_price')->setOriginal($this->_orderItem);
71
+ }
72
+
73
+ /**
74
+ * Fetch total
75
+ *
76
+ * @return string
77
+ */
78
+ public function fetchTotalPrice()
79
+ {
80
+ return $this->getFullPrice()->total();
81
+ }
82
+
83
+ /**
84
+ * Fetch price
85
+ *
86
+ * @return string
87
+ */
88
+ public function fetchPrice()
89
+ {
90
+ return $this->getFullPrice()->itemPrice();
91
+ }
92
+
93
+ /**
94
+ * Fetch timestamp
95
+ *
96
+ * @return string
97
+ */
98
+ public function fetchTimestamp()
99
+ {
100
+ return $this->_orderItem->getUpdatedAt();
101
+ }
102
+
103
+ /**
104
+ * Fetch store view
105
+ *
106
+ * @return string
107
+ */
108
+ public function fetchStoreView()
109
+ {
110
+ $store = Mage::getModel('core/store')->load($this->getStoreId());
111
+
112
+ return Mage::getModel('marketingsoftware/abstraction_storeview')->setOriginal($store);
113
+ }
114
+
115
+ /**
116
+ * Get store Id
117
+ *
118
+ * @return int
119
+ */
120
+ public function getStoreId()
121
+ {
122
+ if ($this->_orderItem instanceof Mage_Sales_Model_Quote_Item) {
123
+ if ($this->_orderItem->getQuote()) {
124
+ return $this->_orderItem->getQuote()->getStoreId();
125
+ } else {
126
+ return Mage::getModel('sales/quote')->load($this->_orderItem->getQuoteId())->getStoreId();
127
+ }
128
+ } elseif ($this->_orderItem instanceof Mage_Sales_Model_Order_Item) {
129
+ return $this->_orderItem->getOrder()->getStoreId();
130
+ }
131
+
132
+ return 0;
133
+ }
134
+
135
+ /**
136
+ * Fetch sales rules
137
+ *
138
+ * @return string
139
+ */
140
+ public function fetchSalesRules()
141
+ {
142
+ return $this->_orderItem->getAppliedRuleIds();
143
+ }
144
+
145
+ /**
146
+ * Options can be nested so this function will allow us to parse them in recursive manner.
147
+ *
148
+ * @param mixed $values
149
+ * @param string $prefix
150
+ * @return string
151
+ */
152
+ protected function _stringifyOptions($values, $prefix = '')
153
+ {
154
+ $result = '';
155
+
156
+ foreach ($values as $value) {
157
+ if (is_array($value['value'])) {
158
+ if (isset($value['value'][0]) && count($value['value']) == 1) {
159
+ $value['value'] = $value['value'][0];
160
+ }
161
+
162
+ $result .= $prefix.$value['label'].":\n".$this->_stringifyOptions($value['value'], $prefix.' ');
163
+ } else {
164
+ $result .= $prefix.$value['label'].":".$value['value']."\n";
165
+ }
166
+ }
167
+
168
+ return $result;
169
+ }
170
+
171
+ /**
172
+ * Get REST order item entity
173
+ *
174
+ * @return Copernica_MarketingSoftware_Model_Rest_Order_Item
175
+ */
176
+ public function getRestOrderItem()
177
+ {
178
+ $restOrderItem = Mage::getModel('marketingsoftware/rest_order_item');
179
+ $restOrderItem->setOrderItemEntity($this);
180
+
181
+ return $restOrderItem;
182
+ }
183
+
184
+ /**
185
+ * Set copernica order item
186
+ *
187
+ * @param Mage_Sales_Model_Order_Item|Mage_Sales_Model_Quote_Item $orderItem
188
+ */
189
+ public function setOrderItem($orderItem)
190
+ {
191
+ $this->_orderItem = $orderItem;
192
+
193
+ $this->setProduct($orderItem->getProductId());
194
+ }
195
+ }
app/code/community/Copernica/MarketingSoftware/Model/Copernica/Entity/Product.php CHANGED
@@ -31,227 +31,406 @@ class Copernica_MarketingSoftware_Model_Copernica_Entity_Product extends Coperni
31
  {
32
  /**
33
  * Magento product that will be used during sync
34
- * @var Mage_Catalog_Model_Product
 
35
  */
36
- protected $product = null;
 
 
 
 
 
 
 
37
 
38
  /**
39
- * Construct product entity by it's id
40
- * @param int
 
41
  */
42
- public function __construct($productId)
43
  {
44
- $this->product = Mage::getModel('catalog/product')->load($productId);
45
  }
46
 
47
  /**
48
  * Fetch product Id
 
49
  * @return string
50
  */
51
  public function fetchProductId()
52
  {
53
- return $this->product->getId();
54
  }
55
 
56
  /**
57
  * Get product name
 
58
  * @return string
59
  */
60
  public function fetchName()
61
  {
62
- return $this->product->getName();
63
  }
64
 
65
  /**
66
  * Get SKU
 
67
  * @return string
68
  */
69
  public function fetchSku()
70
  {
71
- return $this->product->getSku();
72
  }
73
 
74
  /**
75
  * Get description
 
76
  * @return string
77
  */
78
  public function fetchDescription()
79
  {
80
- return $this->product->getShortDescription();
81
  }
82
 
83
  /**
84
  * Get price
 
85
  * @return string
86
  */
87
  public function fetchPrice()
88
  {
89
- return $this->product->getPrice();
90
  }
91
 
92
  /**
93
- * Get total product total price
 
 
94
  * @return string
95
  */
96
- public function fetchTotal()
97
  {
98
- return $this->product->getTotal();
 
 
 
 
 
 
 
 
 
 
99
  }
100
 
101
  /**
102
  * Get url to product page
 
103
  * @return string
104
  */
105
  public function fetchUrl()
106
  {
107
- // get raw url
108
- $url = $this->product->getProductUrl($this->getStoreId());
109
-
110
- /*
111
- * This is funny one. Magento can produce url that will point to our
112
- * process queue script as a filename. It's even more funny cause it will
113
- * not point to correct location of that script but rather than that it
114
- * will point to such file in root directory. It's obviously wrong.
115
- * That is why we want to change that to index.php that should point to
116
- * actuall magento installation.
117
- */
118
- if (strpos($url, 'processQueue.php')) $url = str_replace('processQueue.php', 'index.php', $url);
119
-
120
- // return parsed url
121
  return $url;
122
  }
123
 
124
  /**
125
- * Get url to product image
126
- * @return string
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
127
  */
128
  public function fetchImage()
129
  {
130
- return $this->product->getImageUrl();
131
  }
132
 
133
  /**
134
- * This method should be overriden in child classes cause product can be
 
 
 
 
 
 
 
 
 
 
135
  * placed inside multiple stores so it's not possible to point to one certain
136
  * store Id.
 
137
  * @return int
138
  */
139
  public function getStoreId()
140
  {
141
- return 0;
 
 
 
 
142
  }
143
 
144
  /**
145
  * Get product Id
 
146
  * @return string
147
  */
148
  public function fetchId()
149
  {
150
- return $this->product->getId();
151
  }
152
 
 
 
 
 
 
 
 
 
 
 
 
 
153
  /**
154
  * Get product weight
 
155
  * @return string
156
  */
157
  public function fetchWeight()
158
  {
159
- return $this->product->getWeight();
160
  }
161
 
162
  /**
163
- * Get product category path
 
164
  * @return string
165
  */
166
- public function fetchCategories()
167
  {
168
- // get all categories ids
169
- $categoryIds = $this->product->getCategoryIds();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
170
 
171
- // placeholder for categories
172
  $data = array();
173
 
174
- // iterate over all categories
175
- foreach ($categoryIds as $id)
176
- {
177
- // array that will hold category name parts
178
  $categoryName = array();
179
 
180
- // assign current category as parent category
181
  $parent = Mage::getModel('catalog/category')->load($id);
182
 
183
- // while we have a parent we have to iterate
184
- while($parent->getId() > 1)
185
- {
186
  $categoryName [] = $parent->getName();
 
187
  $parent = $parent->getParentCategory();
188
  }
189
 
190
- // append next category name to data
191
- $data[] = implode(' > ', $categoryName);
192
  }
193
 
194
- // return whole category string
195
- return implode("\n", $data);
 
 
 
 
 
 
 
 
 
196
  }
197
 
198
  /**
199
  * Fetch options associated with given product.
 
200
  * @return string
201
  */
202
  public function fetchOptions()
203
  {
204
- // this is implemented in item class. Check if it can be moved here
205
- return 'options';
 
 
 
 
 
 
 
 
 
 
 
206
  }
207
 
208
  /**
209
- * Fetch attributes string representation.
210
- * @return string
 
211
  */
212
- public function fetchAttributes()
213
  {
214
- // get product attributes
215
- $attributes = $this->product->getAttributes();
216
 
217
- // data holder
 
218
  $stringRepresentation = '';
219
 
220
- // iterate over all attributes
221
- foreach ($attributes as $attr)
222
- {
223
- // we only want user defined
224
- if (!$attr->getIsUserDefined()) continue;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
225
 
226
- // we only want ones that have valid input
227
- if (!in_array($attr->getFrontendInput(), array('text', 'select', 'multiline', 'textarea', 'price', 'date', 'multiselect'))) continue;
 
 
 
 
 
228
 
229
- // check if we have valid label and value
230
- if ($label = $attr->getAttributeCode() && $value = $attr->getFrontend()->getValue($this->product))
231
- $stringRepresentation .= "$label: $value\n";
 
 
 
 
 
 
 
 
 
 
232
  }
233
 
234
- // return string representation
235
- return $stringRepresentation;
236
  }
237
 
238
  /**
239
  * Fetch attribute set name
 
240
  * @return string
241
  */
242
  public function fetchAttributeSet()
243
  {
244
- $set = Mage::getModel('eav/entity_attribute_set')->load($this->product->getAttributeSetId());
245
 
246
  return $set->getAttributeSetName();
247
  }
248
 
249
  /**
250
- * Get REST entity
251
- * @return Copernica_MarketingSoftware_Model_REST_Product
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
252
  */
253
- public function getREST()
254
  {
255
- return new Copernica_MarketingSoftware_Model_REST_Product($this);
256
  }
257
  }
31
  {
32
  /**
33
  * Magento product that will be used during sync
34
+ *
35
+ * @var Mage_Catalog_Model_Product
36
  */
37
+ protected $_product = null;
38
+
39
+ /**
40
+ * Timestamp of when the product was viewed
41
+ *
42
+ * @var int
43
+ */
44
+ protected $_timestamp;
45
 
46
  /**
47
+ * Get native magento object
48
+ *
49
+ * @return Mage_Catalog_Model_Product
50
  */
51
+ public function getNative()
52
  {
53
+ return $this->_product;
54
  }
55
 
56
  /**
57
  * Fetch product Id
58
+ *
59
  * @return string
60
  */
61
  public function fetchProductId()
62
  {
63
+ return $this->_product->getId();
64
  }
65
 
66
  /**
67
  * Get product name
68
+ *
69
  * @return string
70
  */
71
  public function fetchName()
72
  {
73
+ return $this->_product->getName();
74
  }
75
 
76
  /**
77
  * Get SKU
78
+ *
79
  * @return string
80
  */
81
  public function fetchSku()
82
  {
83
+ return $this->_product->getSku();
84
  }
85
 
86
  /**
87
  * Get description
88
+ *
89
  * @return string
90
  */
91
  public function fetchDescription()
92
  {
93
+ return $this->_product->getShortDescription();
94
  }
95
 
96
  /**
97
  * Get price
98
+ *
99
  * @return string
100
  */
101
  public function fetchPrice()
102
  {
103
+ return $this->_product->getPrice();
104
  }
105
 
106
  /**
107
+ * Fetch special price
108
+ *
109
+ * @todo test me!
110
  * @return string
111
  */
112
+ public function fetchSpecialPrice()
113
  {
114
+ return $this->_product->getSpecialPrice();
115
+ }
116
+
117
+ /**
118
+ * Get product viewed at
119
+ *
120
+ * @return string
121
+ */
122
+ public function fetchTimestamp()
123
+ {
124
+ return $this->_timestamp;
125
  }
126
 
127
  /**
128
  * Get url to product page
129
+ *
130
  * @return string
131
  */
132
  public function fetchUrl()
133
  {
134
+ $url = $this->_product->getProductUrl($this->getStoreId());
135
+
136
+ if (strpos($url, 'processQueue.php')) {
137
+ $url = str_replace('processQueue.php', 'index.php', $url);
138
+ }
139
+
 
 
 
 
 
 
 
 
140
  return $url;
141
  }
142
 
143
  /**
144
+ * Get image URL by it's type. Note that type should be compatible with
145
+ * magento internal types, so 'image' or 'thumbnail' can be used.
146
+ *
147
+ * When image can't be found or identified or magento has some other
148
+ * problems with beforementioned image empty string will be returned.
149
+ *
150
+ * @return string
151
+ */
152
+ protected function _getImageByType($type)
153
+ {
154
+ try {
155
+ return Mage::helper('catalog/image')->init($this->_product, $type);
156
+ } catch (Exception $e) {
157
+ Mage::logException($e);
158
+ return '';
159
+ }
160
+ }
161
+
162
+ /**
163
+ * Get URL to product image
164
+ *
165
+ * @return string
166
  */
167
  public function fetchImage()
168
  {
169
+ return $this->_getImageByType('image');
170
  }
171
 
172
  /**
173
+ * Get URL to product thumbnail
174
+ *
175
+ * @return string
176
+ */
177
+ public function fetchThumbnail()
178
+ {
179
+ return $this->_getImageByType('thumbnail');
180
+ }
181
+
182
+ /**
183
+ * This method should be overriden in child classes because product can be
184
  * placed inside multiple stores so it's not possible to point to one certain
185
  * store Id.
186
+ *
187
  * @return int
188
  */
189
  public function getStoreId()
190
  {
191
+ if ($this->_product->getStoreId()) {
192
+ return $this->_product->getStoreId();
193
+ } else {
194
+ return 0;
195
+ }
196
  }
197
 
198
  /**
199
  * Get product Id
200
+ *
201
  * @return string
202
  */
203
  public function fetchId()
204
  {
205
+ return $this->_product->getId();
206
  }
207
 
208
+ /**
209
+ * Fetch store view
210
+ *
211
+ * @return string
212
+ */
213
+ public function fetchStoreView()
214
+ {
215
+ $store = Mage::getModel('core/store')->load($this->getStoreId());
216
+
217
+ return Mage::getModel('marketingsoftware/abstraction_storeview')->setOriginal($store);
218
+ }
219
+
220
  /**
221
  * Get product weight
222
+ *
223
  * @return string
224
  */
225
  public function fetchWeight()
226
  {
227
+ return $this->_product->getWeight();
228
  }
229
 
230
  /**
231
+ * Get last modification date
232
+ *
233
  * @return string
234
  */
235
+ public function getModified()
236
  {
237
+ return $this->_product->getUpdatedAt();
238
+ }
239
+
240
+ /**
241
+ * Get product creation date
242
+ *
243
+ * @return string
244
+ */
245
+ public function getCreated()
246
+ {
247
+ return $this->_product->getCreatedAt();
248
+ }
249
+
250
+ /**
251
+ * Fetch categories list
252
+ *
253
+ * @return array
254
+ */
255
+ public function fetchCategoriesList()
256
+ {
257
+ $categoryIds = $this->_product->getCategoryIds();
258
 
 
259
  $data = array();
260
 
261
+ foreach ($categoryIds as $id) {
 
 
 
262
  $categoryName = array();
263
 
 
264
  $parent = Mage::getModel('catalog/category')->load($id);
265
 
266
+ while($parent->getId() > 1) {
 
 
267
  $categoryName [] = $parent->getName();
268
+
269
  $parent = $parent->getParentCategory();
270
  }
271
 
272
+ $data[$id] = implode(' > ', $categoryName);
 
273
  }
274
 
275
+ return $data;
276
+ }
277
+
278
+ /**
279
+ * Get product category path
280
+ *
281
+ * @return string
282
+ */
283
+ public function fetchCategories()
284
+ {
285
+ return implode("\n", $this->fetchCategoriesList());
286
  }
287
 
288
  /**
289
  * Fetch options associated with given product.
290
+ *
291
  * @return string
292
  */
293
  public function fetchOptions()
294
  {
295
+ $options = $this->_product->getTypeInstance(true)->getOrderOptions($this->_product);
296
+
297
+ $neededOptions = array();
298
+
299
+ if (isset($options['attributes_info'])) {
300
+ $neededOptions = $options['attributes_info'];
301
+ } elseif (isset($options['bundle_options'])) {
302
+ $neededOptions = $options['bundle_options'];
303
+ } elseif (isset($options['options'])) {
304
+ $neeededOptions = $options['options'];
305
+ }
306
+
307
+ return $this->_stringifyOptions($neededOptions);
308
  }
309
 
310
  /**
311
+ * Fetch attribute list
312
+ *
313
+ * @return array
314
  */
315
+ public function fetchAttributesList()
316
  {
317
+ $attributes = $this->_product->getAttributes();
 
318
 
319
+ $resultSet = array();
320
+
321
  $stringRepresentation = '';
322
 
323
+ foreach ($attributes as $attr) {
324
+ if (!$attr->getIsUserDefined()) {
325
+ continue;
326
+ }
327
+
328
+ $compareArray = array('text', 'select', 'multiline', 'textarea', 'price', 'date', 'multiselect');
329
+
330
+ if (!in_array($attr->getFrontendInput(), $compareArray)) {
331
+ continue;
332
+ }
333
+
334
+ if ($attr->getAttributeCode() && $value = $attr->getFrontend()->getValue($this->_product)) {
335
+ $resultSet []= array (
336
+ 'code' => $attr->getAttributeCode(),
337
+ 'value' => $value,
338
+ 'type' => $attr->getFrontendInput(),
339
+ 'label' => $attr->getFrontendLabel()
340
+ );
341
+ }
342
+ }
343
+
344
+ return $resultSet;
345
+ }
346
+
347
+ /**
348
+ * Is product a new product?
349
+ *
350
+ * @return boolean
351
+ */
352
+ public function fetchNew()
353
+ {
354
+ $newsFrom = $this->_product->getNewsFromDate();
355
+ $newsTo = $this->_product->getNewsToDate();
356
+
357
+ if (!$newsFrom && !$newsTo) {
358
+ return false;
359
+ }
360
+
361
+ $from = Mage::app()->getLocale()->date($newsFrom);
362
+ $to = Mage::app()->getLocale()->date($newsTo);
363
+ $now = Zend_Date::now();
364
 
365
+ $new = true;
366
+
367
+ $new = $from ? $new && $from->isEarlier($now) : $new;
368
+ $new = $to ? $new && $to->isLater($now) : $new;
369
+
370
+ return $new;
371
+ }
372
 
373
+ /**
374
+ * Fetch attributes string representation.
375
+ *
376
+ * @return string
377
+ */
378
+ public function fetchAttributes()
379
+ {
380
+ $attributes = array_map( function ($item) {
381
+ return sprintf("%s: %s", $item['code'], $item['value']);
382
+ }, $this->getAttributesList());
383
+
384
+ if (!is_array($attributes)) {
385
+ return '';
386
  }
387
 
388
+ return implode ("\n", $attributes);
 
389
  }
390
 
391
  /**
392
  * Fetch attribute set name
393
+ *
394
  * @return string
395
  */
396
  public function fetchAttributeSet()
397
  {
398
+ $set = Mage::getModel('eav/entity_attribute_set')->load($this->_product->getAttributeSetId());
399
 
400
  return $set->getAttributeSetName();
401
  }
402
 
403
  /**
404
+ * Get REST product entity
405
+ *
406
+ * @return Copernica_MarketingSoftware_Model_Rest_Product
407
+ */
408
+ public function getRestProduct()
409
+ {
410
+ $restProduct = Mage::getModel('marketingsoftware/rest_product');
411
+ $restProduct->setProductEntity($this);
412
+
413
+ return $restProduct;
414
+ }
415
+
416
+ /**
417
+ * Set product entity
418
+ *
419
+ * @param int $productId
420
+ */
421
+ public function setProduct($productId)
422
+ {
423
+ $this->_product = Mage::getModel('catalog/product')->load($productId);
424
+ }
425
+
426
+
427
+ /**
428
+ * Set the timestamp for when the product was viewed
429
+ *
430
+ * @param unknown $viewedAt
431
  */
432
+ public function setTimestamp($viewedAt)
433
  {
434
+ $this->_timestamp = $viewedAt;
435
  }
436
  }
app/code/community/Copernica/MarketingSoftware/Model/Copernica/Entity/Quote.php CHANGED
@@ -31,49 +31,59 @@ class Copernica_MarketingSoftware_Model_Copernica_Entity_Quote extends Copernica
31
  {
32
  /**
33
  * Cached quote instance
34
- * @var Mage_Sales_Model_Quote
 
35
  */
36
- private $quote;
37
 
38
  /**
39
  * Cached quote items
40
- * @var array
 
41
  */
42
- private $items;
43
-
44
- /**
45
- * Construct quote entity
46
- * @param Mage_Sales_Model_Quote
47
- */
48
- public function __construct($quote)
49
- {
50
- $this->quote = $quote;
51
- }
52
 
53
  /**
54
  * @return array
55
  */
56
  public function getItems()
57
  {
58
- // check if we already fetched all items
59
- if (!is_null($this->items)) return $this->items;
 
60
 
61
- // data holder for quote items
62
- $items = array();
63
 
64
- // convert all items into copernica entities
65
- foreach ($this->quote->getAllItems() as $item) $items[] = new Copernica_MarketingSoftware_Model_Copernica_Entity_CartItem($item);
 
 
 
 
66
 
67
- // cache and return items
68
- return $this->items = $items;
69
  }
70
 
71
  /**
72
- * Get RESTEntity for this quote.
73
- * @return Copernica_MarketingSoftware_Model_REST_Quote
 
74
  */
75
- public function getREST()
 
 
 
 
 
 
 
 
 
 
 
 
 
76
  {
77
- return new Copernica_MarketingSoftware_Model_REST_Quote($this);
78
  }
79
  }
31
  {
32
  /**
33
  * Cached quote instance
34
+ *
35
+ * @var Mage_Sales_Model_Quote
36
  */
37
+ protected $_quote;
38
 
39
  /**
40
  * Cached quote items
41
+ *
42
+ * @var array
43
  */
44
+ protected $_quoteItems;
 
 
 
 
 
 
 
 
 
45
 
46
  /**
47
  * @return array
48
  */
49
  public function getItems()
50
  {
51
+ if (!is_null($this->$_quoteItems)) {
52
+ return $this->$_quoteItems;
53
+ }
54
 
55
+ $quoteItems = array();
 
56
 
57
+ foreach ($this->_quote->getAllItems() as $quoteItem) {
58
+ $quoteItemEntity = Mage::getModel('marketingsoftware/copernica_entity_quote_item');
59
+ $quoteItemEntity->setQuoteItem($quoteItem);
60
+
61
+ $quoteItems[] = $quoteItemEntity;
62
+ }
63
 
64
+ return $this->_quoteItems = $quoteItems;
 
65
  }
66
 
67
  /**
68
+ * Get REST quote entity
69
+ *
70
+ * @return Copernica_MarketingSoftware_Model_Rest_Quote
71
  */
72
+ public function getRestQuote()
73
+ {
74
+ $restQuote = Mage::getModel('marketingsoftware/rest_quote');
75
+ $restQuote->setQuoteEntity($this);
76
+
77
+ return $restQuote;
78
+ }
79
+
80
+ /**
81
+ * Set quote entity
82
+ *
83
+ * @param Mage_Sales_Model_Quote $quote
84
+ */
85
+ public function setQuote($quote)
86
  {
87
+ $this->_quote = $quote;
88
  }
89
  }
app/code/community/Copernica/MarketingSoftware/Model/Copernica/Entity/{CartItem.php → Quote/Item.php} RENAMED
@@ -25,63 +25,69 @@
25
  */
26
 
27
  /**
28
- * Brigde class between Copernica subprofile and magetno cart item
29
  */
30
- class Copernica_MarketingSoftware_Model_Copernica_Entity_CartItem extends Copernica_MarketingSoftware_Model_Copernica_Entity_Item
31
- {
32
  /**
33
  * Fetch status of an item.
34
  *
35
  * Magento does not have ability to determine if item was removed or if it's
36
- * in cart that was forgotten by customer. We have to make that decistion here.
37
  *
38
  * @return string
39
  */
40
  public function fetchStatus()
41
- {
42
- /*
43
- * Get quote Id and quote model, we will need it to determine in what
44
- * kind of state item is right now
45
- */
46
- $quoteId = $this->item->getQuoteId();
47
  $quote = Mage::getModel('sales/quote');
48
  $quote->loadByIdWithoutStore($quoteId);
49
 
50
- // get order associated with fetched quote
51
  $order = Mage::getModel('sales/order')->getCollection()->addFieldToFilter('quote_id', $quoteId)->getFirstItem();
52
 
53
- // if order has an Id we could assume that item was ordered already
54
- if ($order->getId() > 0) return 'completed';
 
55
 
56
- // get forgotten carts collection
57
  $forgottenCollection = Mage::getResourceModel('reports/quote_collection');
58
  $forgottenCollection->addFieldToFilter('main_table.entity_id', $quoteId);
59
 
60
- // create proper timeout limit
61
  $timeoutLimit = new DateTime();
62
  $timeoutInterval = new DateInterval("PT".(int)(Mage::helper('marketingsoftware/config')->getAbandonedTimeout())."M");
63
  $timeoutInterval->invert = 1;
64
  $timeoutLimit->add($timeoutInterval);
65
 
66
- // we want to detect ones that are new to us in this run
67
  $forgottenCollection->addFieldToFilter('main_table.updated_at', array('lt' => $timeoutLimit->format("Y-m-d H:i:s")));
68
-
69
- // prepare collection for abandoned carts
70
  $forgottenCollection->prepareForAbandonedReport(array());
71
 
72
- // if we item's quote is considered abandoned we want to set status of that item properly
73
- if (count($forgottenCollection)) return 'abandoned';
 
74
 
75
- // by default we will think that cart item is inside a basket
76
  return 'basket';
77
  }
78
 
79
  /**
80
- * Get REST entity
81
- * @return Copernica_MarketingSoftware_Model_REST_CartItem
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82
  */
83
- public function getREST()
84
  {
85
- return new Copernica_MarketingSoftware_Model_REST_CartItem($this);
86
  }
87
  }
25
  */
26
 
27
  /**
28
+ * Brigde class between Copernica subprofile and magento quote item
29
  */
30
+ class Copernica_MarketingSoftware_Model_Copernica_Entity_Quote_Item extends Copernica_MarketingSoftware_Model_Copernica_Entity_Order_Item
31
+ {
32
  /**
33
  * Fetch status of an item.
34
  *
35
  * Magento does not have ability to determine if item was removed or if it's
36
+ * in cart that was forgotten by customer. We have to make that decision here.
37
  *
38
  * @return string
39
  */
40
  public function fetchStatus()
41
+ {
42
+ $quoteId = $this->_orderItem->getQuoteId();
43
+
 
 
 
44
  $quote = Mage::getModel('sales/quote');
45
  $quote->loadByIdWithoutStore($quoteId);
46
 
 
47
  $order = Mage::getModel('sales/order')->getCollection()->addFieldToFilter('quote_id', $quoteId)->getFirstItem();
48
 
49
+ if ($order->getId() > 0) {
50
+ return 'completed';
51
+ }
52
 
 
53
  $forgottenCollection = Mage::getResourceModel('reports/quote_collection');
54
  $forgottenCollection->addFieldToFilter('main_table.entity_id', $quoteId);
55
 
 
56
  $timeoutLimit = new DateTime();
57
  $timeoutInterval = new DateInterval("PT".(int)(Mage::helper('marketingsoftware/config')->getAbandonedTimeout())."M");
58
  $timeoutInterval->invert = 1;
59
  $timeoutLimit->add($timeoutInterval);
60
 
 
61
  $forgottenCollection->addFieldToFilter('main_table.updated_at', array('lt' => $timeoutLimit->format("Y-m-d H:i:s")));
 
 
62
  $forgottenCollection->prepareForAbandonedReport(array());
63
 
64
+ if (count($forgottenCollection)) {
65
+ return 'abandoned';
66
+ }
67
 
 
68
  return 'basket';
69
  }
70
 
71
  /**
72
+ * Get REST quote item entity
73
+ *
74
+ * @return Copernica_MarketingSoftware_Model_Rest_Quote_Item
75
+ */
76
+ public function getRestQuoteItem()
77
+ {
78
+ $restQuoteItem = Mage::getModel('marketingsoftware/rest_quote_item');
79
+ $restQuoteItem->setQuoteItemEntity($this);
80
+
81
+ return $restQuoteItem;
82
+ }
83
+
84
+ /**
85
+ * Set copernica quote item
86
+ *
87
+ * @param Mage_Sales_Model_Quote_Item $quoteItem
88
  */
89
+ public function setQuoteItem(Mage_Sales_Model_Quote_Item $quoteItem)
90
  {
91
+ $this->setOrderItem($quoteItem);
92
  }
93
  }
app/code/community/Copernica/MarketingSoftware/Model/Copernica/Entity/Subscription.php CHANGED
@@ -31,43 +31,41 @@ class Copernica_MarketingSoftware_Model_Copernica_Entity_Subscription extends Co
31
  {
32
  /**
33
  * Our subscriber
34
- * @var Mage_Newsletter_Model_Subscriber
 
35
  */
36
- private $subscription;
37
-
38
- /**
39
- * Construct subscription model
40
- * @param Mage_Newsletter_Model_Subscriber
41
- */
42
- public function __construct($subscription)
43
- {
44
- $this->subscription = $subscription;
45
- }
46
 
47
  /**
48
  * Fetch email address
 
49
  * @return string
50
  */
51
  public function fetchEmail()
52
  {
53
- return $this->subscription->getEmail();
54
  }
55
 
56
  /**
57
  * Fetch status
 
58
  * @return string
59
  */
60
  public function fetchStatus()
61
  {
62
- switch ($this->subscription->getStatus()) {
63
  case Mage_Newsletter_Model_Subscriber::STATUS_SUBSCRIBED:
64
  return 'subscribed';
 
65
  case Mage_Newsletter_Model_Subscriber::STATUS_NOT_ACTIVE:
66
  return 'not active';
 
67
  case Mage_Newsletter_Model_Subscriber::STATUS_UNSUBSCRIBED:
68
  return 'unsubscribed';
 
69
  case Mage_Newsletter_Model_Subscriber::STATUS_UNCONFIRMED:
70
  return 'unconfirmed';
 
71
  default:
72
  return 'unknown';
73
  }
@@ -75,6 +73,7 @@ class Copernica_MarketingSoftware_Model_Copernica_Entity_Subscription extends Co
75
 
76
  /**
77
  * Fetch group
 
78
  * @return string
79
  */
80
  public function fetchGroup()
@@ -84,59 +83,70 @@ class Copernica_MarketingSoftware_Model_Copernica_Entity_Subscription extends Co
84
 
85
  /**
86
  * Fetch store view
 
87
  * @return string
88
  */
89
  public function fetchStoreView()
90
  {
91
- // get magento store
92
- $store = Mage::getModel('core/store')->load($this->subscription->getStoreId());
93
-
94
- // if we don't have a store we are just about done here
95
- if (is_null($store)) return '';
96
-
97
- // parse store to string
98
- return implode(' > ', array(
99
- $store->getWebsite()->getName(),
100
- $store->getGroup()->getName(),
101
- $store->getName()
102
- ));
103
  }
104
 
105
  /**
106
  * Fetch subscriber customer Id
 
107
  * @return string
108
  */
109
  public function fetchCustomerId()
110
  {
111
- // get store model
112
- $store = Mage::getModel('core/store')->load($this->subscription->getStoreId());
113
-
114
- // get customer that would be associated with given email address
115
- $customer = Mage::getModel('customer/customer')->setWebsiteId($store->getWebsiteId())->loadByEmail($this->subscription->getEmail());
116
 
117
- // construct proper id
118
- if ($customer->isObjectNew()) $identifier = $this->subscription->getEmail();
119
- else $identifier = $customer->getId();
 
 
120
 
121
- // return customer Id
122
- return $identifier.'|'.$this->subscription->getStoreId();
123
  }
124
 
125
  /**
126
  * Get subscribtion store Id
 
127
  * @return int
128
  */
129
  public function getStoreId()
130
  {
131
- return $this->subscription->getStoreId();
 
 
 
 
132
  }
133
 
134
  /**
135
- * Construct REST entity that will take care of synchronization
136
- * @return Copernica_MarketingSoftware_Model_REST_Subscription
 
 
 
 
 
 
 
 
 
 
 
 
 
 
137
  */
138
- public function getREST()
139
  {
140
- return new Copernica_MarketingSoftware_Model_REST_Subscription($this);
141
  }
142
  }
31
  {
32
  /**
33
  * Our subscriber
34
+ *
35
+ * @var Mage_Newsletter_Model_Subscriber
36
  */
37
+ protected $_subscriber;
 
 
 
 
 
 
 
 
 
38
 
39
  /**
40
  * Fetch email address
41
+ *
42
  * @return string
43
  */
44
  public function fetchEmail()
45
  {
46
+ return $this->_subscriber->getEmail();
47
  }
48
 
49
  /**
50
  * Fetch status
51
+ *
52
  * @return string
53
  */
54
  public function fetchStatus()
55
  {
56
+ switch ($this->_subscriber->getStatus()) {
57
  case Mage_Newsletter_Model_Subscriber::STATUS_SUBSCRIBED:
58
  return 'subscribed';
59
+
60
  case Mage_Newsletter_Model_Subscriber::STATUS_NOT_ACTIVE:
61
  return 'not active';
62
+
63
  case Mage_Newsletter_Model_Subscriber::STATUS_UNSUBSCRIBED:
64
  return 'unsubscribed';
65
+
66
  case Mage_Newsletter_Model_Subscriber::STATUS_UNCONFIRMED:
67
  return 'unconfirmed';
68
+
69
  default:
70
  return 'unknown';
71
  }
73
 
74
  /**
75
  * Fetch group
76
+ *
77
  * @return string
78
  */
79
  public function fetchGroup()
83
 
84
  /**
85
  * Fetch store view
86
+ *
87
  * @return string
88
  */
89
  public function fetchStoreView()
90
  {
91
+ $store = Mage::getModel('core/store')->load($this->getStoreId());
92
+
93
+ return Mage::getModel('marketingsoftware/abstraction_storeview')->setOriginal($store);
 
 
 
 
 
 
 
 
 
94
  }
95
 
96
  /**
97
  * Fetch subscriber customer Id
98
+ *
99
  * @return string
100
  */
101
  public function fetchCustomerId()
102
  {
103
+ $store = Mage::getModel('core/store')->load($this->_subscriber->getStoreId());
104
+
105
+ $customer = Mage::getModel('customer/customer')->setWebsiteId($store->getWebsiteId())->loadByEmail($this->_subscriber->getEmail());
 
 
106
 
107
+ if ($customer->isObjectNew()) {
108
+ $identifier = $this->_subscriber->getEmail();
109
+ } else {
110
+ $identifier = $customer->getId();
111
+ }
112
 
113
+ return $identifier.'|'.$this->_subscriber->getStoreId();
 
114
  }
115
 
116
  /**
117
  * Get subscribtion store Id
118
+ *
119
  * @return int
120
  */
121
  public function getStoreId()
122
  {
123
+ if($this->_subscriber->getStoreId()) {
124
+ return $this->_subscriber->getStoreId();
125
+ } else {
126
+ return 0;
127
+ }
128
  }
129
 
130
  /**
131
+ * Get REST subscription entity
132
+ *
133
+ * @return Copernica_MarketingSoftware_Model_Rest_Subscription
134
+ */
135
+ public function getRestSubscription()
136
+ {
137
+ $restSubscription = Mage::getModel('marketingsoftware/rest_subscription');
138
+ $restSubscription->setSubscriptionEntity($this);
139
+
140
+ return $restSubscription;
141
+ }
142
+
143
+ /**
144
+ * Set subscription entity
145
+ *
146
+ * @param Mage_Newsletter_Model_Subscriber $subscriber
147
  */
148
+ public function setSubscription($subscriber)
149
  {
150
+ $this->_subscriber = $subscriber;
151
  }
152
  }
app/code/community/Copernica/MarketingSoftware/Model/Copernica/Entity/Wishlist.php ADDED
@@ -0,0 +1,99 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Copernica Marketing Software
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0).
8
+ * It is available through the world-wide-web at this URL:
9
+ * http://opensource.org/licenses/osl-3.0.php
10
+ * If you are unable to obtain a copy of the license through the
11
+ * world-wide-web, please send an email to copernica@support.cream.nl
12
+ * so we can send you a copy immediately.
13
+ *
14
+ * DISCLAIMER
15
+ *
16
+ * Do not edit or add to this file if you wish to upgrade this software
17
+ * to newer versions in the future. If you wish to customize this module
18
+ * for your needs please refer to http://www.magento.com/ for more
19
+ * information.
20
+ *
21
+ * @category Copernica
22
+ * @package Copernica_MarketingSoftware
23
+ * @copyright Copyright (c) 2011-2012 Copernica & Cream. (http://docs.cream.nl/)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ /**
28
+ * Bridge between magento wishlist and copernica subprofile
29
+ */
30
+ class Copernica_MarketingSoftware_Model_Copernica_Entity_Wishlist extends Copernica_MarketingSoftware_Model_Copernica_Entity
31
+ {
32
+ /**
33
+ * Cached wishlist instance
34
+ *
35
+ * @var Mage_Wishlist_Model_Wishlist
36
+ */
37
+ protected $_wishlist;
38
+
39
+ /**
40
+ * Cached wishlist items
41
+ *
42
+ * @var array
43
+ */
44
+ protected $_wishlistItems;
45
+
46
+ /**
47
+ * Fetch wishlist Id
48
+ *
49
+ * @return string
50
+ */
51
+ public function fetchId()
52
+ {
53
+ return $this->_wishlist->getId();
54
+ }
55
+
56
+ /**
57
+ * @return array
58
+ */
59
+ public function getItems()
60
+ {
61
+ if (!is_null($this->_wishlistItems)) {
62
+ return $this->_wishlistItems;
63
+ }
64
+
65
+ $wishlistItems = array();
66
+
67
+ foreach ($this->_wishlist->getItemCollection() as $wishlistItem) {
68
+ $wishlistItemEntity = Mage::getModel('marketingsoftware/copernica_entity_wishlist_item');
69
+ $wishlistItemEntity->setWishlistItem($wishlistItem);
70
+
71
+ $wishlistItems[] = $wishlistItemEntity;
72
+ }
73
+
74
+ return $this->_wishlistItems = $wishlistItems;
75
+ }
76
+
77
+ /**
78
+ * Get REST wishlist entity
79
+ *
80
+ * @return Copernica_MarketingSoftware_Model_Rest_Wishlist
81
+ */
82
+ public function getRestWishlist()
83
+ {
84
+ $restWishlist = Mage::getModel('marketingsoftware/rest_wishlist');
85
+ $restWishlist->setWishlistEntity($this);
86
+
87
+ return $restWishlist;
88
+ }
89
+
90
+ /**
91
+ * Set wishlist entity
92
+ *
93
+ * @param Mage_Wishlist_Model_Wishlist $wishlist
94
+ */
95
+ public function setWishlist(Mage_Wishlist_Model_Wishlist $wishlist)
96
+ {
97
+ $this->_wishlist = $wishlist;
98
+ }
99
+ }
app/code/community/Copernica/MarketingSoftware/Model/Copernica/Entity/Wishlist/Item.php ADDED
@@ -0,0 +1,155 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Copernica Marketing Software
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0).
8
+ * It is available through the world-wide-web at this URL:
9
+ * http://opensource.org/licenses/osl-3.0.php
10
+ * If you are unable to obtain a copy of the license through the
11
+ * world-wide-web, please send an email to copernica@support.cream.nl
12
+ * so we can send you a copy immediately.
13
+ *
14
+ * DISCLAIMER
15
+ *
16
+ * Do not edit or add to this file if you wish to upgrade this software
17
+ * to newer versions in the future. If you wish to customize this module
18
+ * for your needs please refer to http://www.magento.com/ for more
19
+ * information.
20
+ *
21
+ * @category Copernica
22
+ * @package Copernica_MarketingSoftware
23
+ * @copyright Copyright (c) 2011-2012 Copernica & Cream. (http://docs.cream.nl/)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ /**
28
+ * Brigde class between Copernica subprofile and magento wishlist item
29
+ */
30
+ class Copernica_MarketingSoftware_Model_Copernica_Entity_Wishlist_Item extends Copernica_MarketingSoftware_Model_Copernica_Entity_Product
31
+ {
32
+ /**
33
+ * Cached item
34
+ *
35
+ * @var Mage_Wishlist_Model_Item
36
+ */
37
+ protected $_wishlistItem = null;
38
+
39
+ /**
40
+ * Fetch wishlist item Id
41
+ *
42
+ * @return string
43
+ */
44
+ public function fetchId()
45
+ {
46
+ return $this->_wishlistItem->getId();
47
+ }
48
+
49
+ /**
50
+ * Fetch wishlist Id
51
+ *
52
+ * @return string
53
+ */
54
+ public function fetchWishlistId()
55
+ {
56
+ return $this->_wishlistItem->getWishlistId();
57
+ }
58
+
59
+ /**
60
+ * Fetch quantity
61
+ *
62
+ * @return string
63
+ */
64
+ public function fetchDescription()
65
+ {
66
+ return $this->_wishlistItem->getDescription();
67
+ }
68
+
69
+ /**
70
+ * Fetch quantity
71
+ *
72
+ * @return string
73
+ */
74
+ public function fetchQuantity()
75
+ {
76
+ return $this->_wishlistItem->getQty();
77
+ }
78
+
79
+ /**
80
+ * Fetch total
81
+ *
82
+ * @return string
83
+ */
84
+ public function fetchTotalPrice()
85
+ {
86
+ return $this->_wishlistItem->getProduct()->getPrice() * $this->getQuantity();
87
+ }
88
+
89
+ /**
90
+ * Fetch price
91
+ *
92
+ * @return string
93
+ */
94
+ public function fetchPrice()
95
+ {
96
+ return $this->_wishlistItem->getProduct()->getPrice();
97
+ }
98
+
99
+ /**
100
+ * Fetch timestamp
101
+ *
102
+ * @return string
103
+ */
104
+ public function fetchTimestamp()
105
+ {
106
+ return $this->_wishlistItem->getAddedAt();
107
+ }
108
+
109
+ /**
110
+ * Fetch store view
111
+ *
112
+ * @return string
113
+ */
114
+ public function fetchStoreView()
115
+ {
116
+ $store = Mage::getModel('core/store')->load($this->getStoreId());
117
+
118
+ return Mage::getModel('marketingsoftware/abstraction_storeview')->setOriginal($store);
119
+ }
120
+
121
+ /**
122
+ * Get store Id
123
+ *
124
+ * @return int
125
+ */
126
+ public function getStoreId()
127
+ {
128
+ return $this->_wishlistItem->getStoreId();
129
+ }
130
+
131
+ /**
132
+ * Get REST wishlist item entity
133
+ *
134
+ * @return Copernica_MarketingSoftware_Model_Rest_Wishlist_Item
135
+ */
136
+ public function getRestWishlistItem()
137
+ {
138
+ $restWishlistItem = Mage::getModel('marketingsoftware/rest_wishlist_item');
139
+ $restWishlistItem->setWishlistItemEntity($this);
140
+
141
+ return $restWishlistItem;
142
+ }
143
+
144
+ /**
145
+ * Set copernica wishlist item
146
+ *
147
+ * @param Mage_Wishlist_Model_Item $wishlistItem
148
+ */
149
+ public function setWishlistItem(Mage_Wishlist_Model_Item $wishlistItem)
150
+ {
151
+ $this->_wishlistItem = $wishlistItem;
152
+
153
+ $this->setProduct($wishlistItem->getProductId());
154
+ }
155
+ }
app/code/community/Copernica/MarketingSoftware/Model/Copernica/Order/Subprofile.php CHANGED
@@ -30,12 +30,13 @@
30
  class Copernica_MarketingSoftware_Model_Copernica_Order_Subprofile extends Copernica_MarketingSoftware_Model_Copernica_Abstract
31
  {
32
  /**
33
- * @var Copernica_MarketingSoftware_Model_Abstraction_Order
34
  */
35
- protected $order = false;
36
 
37
  /**
38
  * Return the identifier for this profile
 
39
  * @return string
40
  */
41
  public function id()
@@ -45,16 +46,20 @@ class Copernica_MarketingSoftware_Model_Copernica_Order_Subprofile extends Coper
45
 
46
  /**
47
  * Try to store a quote item
48
- * @param Copernica_MarketingSoftware_Model_Abstract
 
 
49
  */
50
- public function setOrder($order)
51
  {
52
- $this->order = $order;
 
53
  return $this;
54
  }
55
 
56
  /**
57
  * Get linked fields
 
58
  * @return array
59
  */
60
  public function linkedFields()
@@ -64,6 +69,7 @@ class Copernica_MarketingSoftware_Model_Copernica_Order_Subprofile extends Coper
64
 
65
  /**
66
  * Get the required fields
 
67
  * @return array
68
  */
69
  public function requiredFields()
@@ -73,41 +79,44 @@ class Copernica_MarketingSoftware_Model_Copernica_Order_Subprofile extends Coper
73
 
74
  /**
75
  * Retrieve the data for this object
 
76
  * @return array
77
  */
78
  protected function _data()
79
  {
80
- // initialize the addresses
81
  $billingAddress = $shippingAddress = false;
82
 
83
- // get all addresses associated with given order
84
- $addresses = $this->order->addresses();
85
 
86
- // Get the addresses
87
- if (is_array($addresses)) foreach ($addresses as $address) {
88
- if (in_array('billing', $address->type())) $billingAddress = $address;
89
- if (in_array('shipping', $address->type())) $shippingAddress = $address;
 
 
 
 
 
 
90
  }
91
 
92
- // Get the price
93
- $price = $this->order->price();
94
 
95
- // Return the data array
96
  return array(
97
- 'order_id' => $this->order->id(),
98
- 'quote_id' => $this->order->quoteId(),
99
- 'increment_id' => $this->order->incrementId(),
100
- 'timestamp' => $this->order->timestamp(),
101
- 'quantity' => $this->order->quantity(),
102
  'total' => is_object($price) ? $price->total() : null,
103
  'shipping' => is_object($price) ? $price->shipping() : null,
104
  'currency' => is_object($price) ? $price->currency() : null,
105
- 'weight' => $this->order->weight(),
106
- 'status' => $this->order->status(),
107
- 'store_view' => (string)$this->order->storeview(),
108
- 'remote_ip' => $this->order->customerIP(),
109
- 'shipping_description' => $this->order->shippingDescription(),
110
- 'payment_description' => $this->order->paymentDescription(),
111
  'shipping_address_id' => is_object($shippingAddress) ? $shippingAddress->id() : '',
112
  'billing_address_id' => is_object($billingAddress) ? $billingAddress->id() : '',
113
  );
30
  class Copernica_MarketingSoftware_Model_Copernica_Order_Subprofile extends Copernica_MarketingSoftware_Model_Copernica_Abstract
31
  {
32
  /**
33
+ * @var Copernica_MarketingSoftware_Model_Abstraction_Order
34
  */
35
+ protected $_order = false;
36
 
37
  /**
38
  * Return the identifier for this profile
39
+ *
40
  * @return string
41
  */
42
  public function id()
46
 
47
  /**
48
  * Try to store a quote item
49
+ *
50
+ * @param Copernica_MarketingSoftware_Model_Abstract $order
51
+ * @return Copernica_MarketingSoftware_Model_Copernica_Order_Subprofile
52
  */
53
+ public function setOrder(Copernica_MarketingSoftware_Model_Abstract $order)
54
  {
55
+ $this->_order = $order;
56
+
57
  return $this;
58
  }
59
 
60
  /**
61
  * Get linked fields
62
+ *
63
  * @return array
64
  */
65
  public function linkedFields()
69
 
70
  /**
71
  * Get the required fields
72
+ *
73
  * @return array
74
  */
75
  public function requiredFields()
79
 
80
  /**
81
  * Retrieve the data for this object
82
+ *
83
  * @return array
84
  */
85
  protected function _data()
86
  {
 
87
  $billingAddress = $shippingAddress = false;
88
 
89
+ $addresses = $this->_order->addresses();
 
90
 
91
+ if (is_array($addresses)) {
92
+ foreach ($addresses as $address) {
93
+ if (in_array('billing', $address->type())) {
94
+ $billingAddress = $address;
95
+ }
96
+
97
+ if (in_array('shipping', $address->type())) {
98
+ $shippingAddress = $address;
99
+ }
100
+ }
101
  }
102
 
103
+ $price = $this->_order->price();
 
104
 
 
105
  return array(
106
+ 'order_id' => $this->_order->id(),
107
+ 'quote_id' => $this->_order->quoteId(),
108
+ 'increment_id' => $this->_order->incrementId(),
109
+ 'timestamp' => $this->_order->timestamp(),
110
+ 'quantity' => $this->_order->quantity(),
111
  'total' => is_object($price) ? $price->total() : null,
112
  'shipping' => is_object($price) ? $price->shipping() : null,
113
  'currency' => is_object($price) ? $price->currency() : null,
114
+ 'weight' => $this->_order->weight(),
115
+ 'status' => $this->_order->status(),
116
+ 'store_view' => (string)$this->_order->storeview(),
117
+ 'remote_ip' => $this->_order->customerIP(),
118
+ 'shipping_description' => $this->_order->shippingDescription(),
119
+ 'payment_description' => $this->_order->paymentDescription(),
120
  'shipping_address_id' => is_object($shippingAddress) ? $shippingAddress->id() : '',
121
  'billing_address_id' => is_object($billingAddress) ? $billingAddress->id() : '',
122
  );
app/code/community/Copernica/MarketingSoftware/Model/Copernica/Orderitem/Subprofile.php CHANGED
@@ -30,12 +30,13 @@
30
  class Copernica_MarketingSoftware_Model_Copernica_Orderitem_Subprofile extends Copernica_MarketingSoftware_Model_Copernica_Abstract
31
  {
32
  /**
33
- * @var Copernica_MarketingSoftware_Model_Abstraction_Order_Item
34
  */
35
- protected $orderItem = false;
36
 
37
  /**
38
  * Return the identifier for this profile
 
39
  * @return string
40
  */
41
  public function id()
@@ -45,16 +46,19 @@ class Copernica_MarketingSoftware_Model_Copernica_Orderitem_Subprofile extends C
45
 
46
  /**
47
  * Try to store a quote item
 
48
  * @return Copernica_MarketingSoftware_Model_Copernica_Orderitem_Subprofile
49
  */
50
  public function setOrderItem($item)
51
  {
52
- $this->orderItem = $item;
 
53
  return $this;
54
  }
55
 
56
  /**
57
  * Get linked fields
 
58
  * @return array
59
  */
60
  public function linkedFields()
@@ -64,6 +68,7 @@ class Copernica_MarketingSoftware_Model_Copernica_Orderitem_Subprofile extends C
64
 
65
  /**
66
  * Get the required fields
 
67
  * @return array
68
  */
69
  public function requiredFields()
@@ -73,12 +78,13 @@ class Copernica_MarketingSoftware_Model_Copernica_Orderitem_Subprofile extends C
73
 
74
  /**
75
  * Retrieve the data for this object
 
76
  * @return array
77
  */
78
  protected function _data()
79
  {
80
- // Store the orderItem and the product localy
81
- $orderItem = $this->orderItem;
82
  $product = $orderItem->product();
83
 
84
  $data = array(
@@ -94,12 +100,9 @@ class Copernica_MarketingSoftware_Model_Copernica_Orderitem_Subprofile extends C
94
  'options' => (string)$orderItem->options()
95
  );
96
 
97
- // get order item
98
  $order = $orderItem->order();
99
 
100
- // check if we have a valid order object
101
- if (is_object($order))
102
- {
103
  $data['order_id'] = $order->id();
104
  $data['increment_id'] = $order->incrementId();
105
 
@@ -109,21 +112,17 @@ class Copernica_MarketingSoftware_Model_Copernica_Orderitem_Subprofile extends C
109
  $data['store_view'] = (string)$storeView;
110
  $data['url'] = $product->productUrl($storeId);
111
  $data['image'] = $product->imageUrl($storeId);
 
 
112
  }
113
- // even when we don't have order we should provide order_id key, since it's required
114
- else $data['order_id'] = null;
115
 
116
- // get price object
117
  $price = $orderItem->price();
118
 
119
- // check if we have a proper price
120
- if (is_object($price))
121
- {
122
  $data['price'] = $price->itemPrice();
123
  $data['total_price'] = $price->total();
124
  }
125
 
126
- // format categories
127
  $data['categories'] = implode("\n", array_map(function($category) {
128
  return implode(' > ', $category);
129
  }, $product->categories()));
30
  class Copernica_MarketingSoftware_Model_Copernica_Orderitem_Subprofile extends Copernica_MarketingSoftware_Model_Copernica_Abstract
31
  {
32
  /**
33
+ * @var Copernica_MarketingSoftware_Model_Abstraction_Order_Item
34
  */
35
+ protected $_orderItem = false;
36
 
37
  /**
38
  * Return the identifier for this profile
39
+ *
40
  * @return string
41
  */
42
  public function id()
46
 
47
  /**
48
  * Try to store a quote item
49
+ *
50
  * @return Copernica_MarketingSoftware_Model_Copernica_Orderitem_Subprofile
51
  */
52
  public function setOrderItem($item)
53
  {
54
+ $this->_orderItem = $item;
55
+
56
  return $this;
57
  }
58
 
59
  /**
60
  * Get linked fields
61
+ *
62
  * @return array
63
  */
64
  public function linkedFields()
68
 
69
  /**
70
  * Get the required fields
71
+ *
72
  * @return array
73
  */
74
  public function requiredFields()
78
 
79
  /**
80
  * Retrieve the data for this object
81
+ *
82
  * @return array
83
  */
84
  protected function _data()
85
  {
86
+ $orderItem = $this->_orderItem;
87
+
88
  $product = $orderItem->product();
89
 
90
  $data = array(
100
  'options' => (string)$orderItem->options()
101
  );
102
 
 
103
  $order = $orderItem->order();
104
 
105
+ if (is_object($order)) {
 
 
106
  $data['order_id'] = $order->id();
107
  $data['increment_id'] = $order->incrementId();
108
 
112
  $data['store_view'] = (string)$storeView;
113
  $data['url'] = $product->productUrl($storeId);
114
  $data['image'] = $product->imageUrl($storeId);
115
+ } else {
116
+ $data['order_id'] = null;
117
  }
 
 
118
 
 
119
  $price = $orderItem->price();
120
 
121
+ if (is_object($price)) {
 
 
122
  $data['price'] = $price->itemPrice();
123
  $data['total_price'] = $price->total();
124
  }
125
 
 
126
  $data['categories'] = implode("\n", array_map(function($category) {
127
  return implode(' > ', $category);
128
  }, $product->categories()));
app/code/community/Copernica/MarketingSoftware/Model/Copernica/{Viewedproduct → Product/Viewed}/Subprofile.php RENAMED
@@ -27,39 +27,43 @@
27
  /**
28
  * An object to wrap the Copernica profile
29
  */
30
- class Copernica_MarketingSoftware_Model_Copernica_Viewedproduct_Subprofile extends Copernica_MarketingSoftware_Model_Copernica_Abstract
31
  {
32
  /**
33
- * @var Copernica_MarketingSoftware_Model_Abstraction_Viewedproduct
34
  */
35
- protected $viewedProduct = false;
36
 
37
  /**
38
  * Return the identifier for this profile
 
39
  * @return string
40
  */
41
  public function id()
42
  {
43
- return $this->viewedProduct->id;
44
  }
45
 
46
  public function customerId()
47
  {
48
- return $this->viewedProduct->customerId;
49
  }
50
 
51
  /**
52
  * Try to store a quote item
53
- * @param Copernica_MarketingSoftware_Model_Copernica_Orderitem_Subprofile
 
54
  */
55
- public function setViewedProduct($product)
56
  {
57
- $this->viewedProduct = $product;
 
58
  return $this;
59
  }
60
 
61
  /**
62
  * Get linked fields
 
63
  * @return array
64
  */
65
  public function linkedFields()
@@ -69,6 +73,7 @@ class Copernica_MarketingSoftware_Model_Copernica_Viewedproduct_Subprofile exten
69
 
70
  /**
71
  * Get the required fields
 
72
  * @return array
73
  */
74
  public function requiredFields()
@@ -78,23 +83,23 @@ class Copernica_MarketingSoftware_Model_Copernica_Viewedproduct_Subprofile exten
78
 
79
  /**
80
  * Retrieve the data for this object
 
81
  * @return array
82
  */
83
  protected function _data()
84
  {
85
- // Store the quoteItem and the product localy
86
- $product = $this->viewedProduct;
87
 
88
- // Get the store id to make sure that we retrieve the correct url's
89
  $storeId = $product->storeId;
90
 
91
- // flatten the categories
92
  $categories = array();
 
93
  if ($product->categories()) {
94
- foreach ($product->categories() as $category) $categories[] = implode(' > ', $category);
 
 
95
  }
96
 
97
- // construct an array of data
98
  return array(
99
  'product_id' => $product->id(),
100
  'price' => $product->price(),
27
  /**
28
  * An object to wrap the Copernica profile
29
  */
30
+ class Copernica_MarketingSoftware_Model_Copernica_Product_Viewed_Subprofile extends Copernica_MarketingSoftware_Model_Copernica_Abstract
31
  {
32
  /**
33
+ * @var Copernica_MarketingSoftware_Model_Abstraction_Product_Viewed
34
  */
35
+ protected $_viewedProduct = false;
36
 
37
  /**
38
  * Return the identifier for this profile
39
+ *
40
  * @return string
41
  */
42
  public function id()
43
  {
44
+ return $this->_viewedProduct->id;
45
  }
46
 
47
  public function customerId()
48
  {
49
+ return $this->_viewedProduct->customerId;
50
  }
51
 
52
  /**
53
  * Try to store a quote item
54
+ *
55
+ * @param Copernica_MarketingSoftware_Model_Abstraction_Product_Viewed $product
56
  */
57
+ public function setViewedProduct(Copernica_MarketingSoftware_Model_Abstraction_Product_Viewed $product)
58
  {
59
+ $this->_viewedProduct = $product;
60
+
61
  return $this;
62
  }
63
 
64
  /**
65
  * Get linked fields
66
+ *
67
  * @return array
68
  */
69
  public function linkedFields()
73
 
74
  /**
75
  * Get the required fields
76
+ *
77
  * @return array
78
  */
79
  public function requiredFields()
83
 
84
  /**
85
  * Retrieve the data for this object
86
+ *
87
  * @return array
88
  */
89
  protected function _data()
90
  {
91
+ $product = $this->_viewedProduct;
 
92
 
 
93
  $storeId = $product->storeId;
94
 
 
95
  $categories = array();
96
+
97
  if ($product->categories()) {
98
+ foreach ($product->categories() as $category) {
99
+ $categories[] = implode(' > ', $category);
100
+ }
101
  }
102
 
 
103
  return array(
104
  'product_id' => $product->id(),
105
  'price' => $product->price(),
app/code/community/Copernica/MarketingSoftware/Model/Copernica/Profile.php CHANGED
@@ -31,6 +31,7 @@ abstract class Copernica_MarketingSoftware_Model_Copernica_Profile extends Coper
31
  {
32
  /**
33
  * Return the identifier for this profile
 
34
  * @return string
35
  */
36
  public function id()
@@ -40,12 +41,14 @@ abstract class Copernica_MarketingSoftware_Model_Copernica_Profile extends Coper
40
 
41
  /**
42
  * Return email associated with profile
 
43
  * @return string
44
  */
45
  abstract public function email();
46
 
47
  /**
48
  * Return store view associated with profile
 
49
  * @return string
50
  */
51
  abstract public function storeView();
@@ -62,6 +65,7 @@ abstract class Copernica_MarketingSoftware_Model_Copernica_Profile extends Coper
62
 
63
  /**
64
  * Get linked fields
 
65
  * @return array
66
  */
67
  public function linkedFields()
@@ -71,6 +75,7 @@ abstract class Copernica_MarketingSoftware_Model_Copernica_Profile extends Coper
71
 
72
  /**
73
  * Get the required fields
 
74
  * @return array
75
  */
76
  public function requiredFields()
31
  {
32
  /**
33
  * Return the identifier for this profile
34
+ *
35
  * @return string
36
  */
37
  public function id()
41
 
42
  /**
43
  * Return email associated with profile
44
+ *
45
  * @return string
46
  */
47
  abstract public function email();
48
 
49
  /**
50
  * Return store view associated with profile
51
+ *
52
  * @return string
53
  */
54
  abstract public function storeView();
65
 
66
  /**
67
  * Get linked fields
68
+ *
69
  * @return array
70
  */
71
  public function linkedFields()
75
 
76
  /**
77
  * Get the required fields
78
+ *
79
  * @return array
80
  */
81
  public function requiredFields()
app/code/community/Copernica/MarketingSoftware/Model/Copernica/{Profilecustomer.php → Profile/Customer.php} RENAMED
@@ -27,21 +27,23 @@
27
  /**
28
  * An object to wrap the Copernica profile
29
  */
30
- class Copernica_MarketingSoftware_Model_Copernica_ProfileCustomer extends Copernica_MarketingSoftware_Model_Copernica_Profile
31
  {
32
  /**
33
- * @var Copernica_MarketingSoftware_Model_Abstraction_Customer
34
  */
35
- protected $customer = false;
36
 
37
  /**
38
  * Set the customer object to this object
39
- * @param Copernica_MarketingSoftware_Model_Abstraction_Customer
40
- * @return Copernica_MarketingSoftware_Model_Copernica_ProfileCustomer
 
41
  */
42
- public function setCustomer($customer)
43
  {
44
- $this->customer = $customer;
 
45
  return $this;
46
  }
47
 
@@ -53,59 +55,54 @@ class Copernica_MarketingSoftware_Model_Copernica_ProfileCustomer extends Copern
53
  */
54
  public function originalId()
55
  {
56
- return Mage::helper('marketingsoftware/profile')->getCustomerCopernicaId($this->customer, $this->customer->storeview());
57
  }
58
 
59
  /**
60
  * Get profile email.
 
61
  * @return string
62
  */
63
  public function email()
64
  {
65
- return $this->customer->email();
66
  }
67
 
68
  /**
69
  * Return store view associated with profile.
 
70
  * @return string
71
  */
72
  public function storeView()
73
  {
74
- return (string)$this->customer->storeview();
75
  }
76
 
77
  /**
78
  * Retrieve the data for this object
 
79
  * @return array
80
  */
81
  protected function _data()
82
  {
83
- // Get the subscription
84
- $subscription = $this->customer->subscription();
85
 
86
- // fetch the name object
87
- $name = $this->customer->name();
88
 
89
- // fetch the email
90
- $email = $this->customer->email();
91
 
92
- // get store view
93
- $storeview = $this->customer->storeview();
94
 
95
- // get customer Id
96
- $customerId = Mage::helper('marketingsoftware/profile')->getCustomerCopernicaId($this->customer, $storeview);
97
 
98
- // Somehow a birthdate can have different types
99
- $customerBirthDate = $this->customer->birthDate();
100
 
101
- // Handle the birthdate
102
  if(is_object($customerBirthDate))
103
  $birthDate = $customerBirthDate->gmtDate();
104
  elseif(is_string($customerBirthDate) && strtotime($customerBirthDate))
105
  $birthDate = $customerBirthDate;
106
  else $birthDate = '0000-00-00';
107
 
108
- // return an array with customer data
109
  return array(
110
  'customer_id' => $customerId,
111
  'store_view' => (string)$storeview,
@@ -114,9 +111,9 @@ class Copernica_MarketingSoftware_Model_Copernica_ProfileCustomer extends Copern
114
  'lastname' => is_object($name) ? $name->lastname() : null,
115
  'email' => $email,
116
  'birthdate' => $birthDate,
117
- 'group' => $this->customer->group(),
118
  'newsletter' => is_object($subscription) ? $subscription->status() : 'unknown',
119
- 'gender' => $this->customer->gender()
120
  );
121
  }
122
  }
27
  /**
28
  * An object to wrap the Copernica profile
29
  */
30
+ class Copernica_MarketingSoftware_Model_Copernica_Profile_Customer extends Copernica_MarketingSoftware_Model_Copernica_Profile
31
  {
32
  /**
33
+ * @var Copernica_MarketingSoftware_Model_Abstraction_Customer
34
  */
35
+ protected $_customer = false;
36
 
37
  /**
38
  * Set the customer object to this object
39
+ *
40
+ * @param Copernica_MarketingSoftware_Model_Abstraction_Customer $customer
41
+ * @return Copernica_MarketingSoftware_Model_Copernica_Profile_Customer
42
  */
43
+ public function setCustomer(Copernica_MarketingSoftware_Model_Abstraction_Customer $customer)
44
  {
45
+ $this->_customer = $customer;
46
+
47
  return $this;
48
  }
49
 
55
  */
56
  public function originalId()
57
  {
58
+ return Mage::helper('marketingsoftware/profile')->getCustomerCopernicaId($this->_customer, $this->_customer->storeview());
59
  }
60
 
61
  /**
62
  * Get profile email.
63
+ *
64
  * @return string
65
  */
66
  public function email()
67
  {
68
+ return $this->_customer->email();
69
  }
70
 
71
  /**
72
  * Return store view associated with profile.
73
+ *
74
  * @return string
75
  */
76
  public function storeView()
77
  {
78
+ return (string)$this->_customer->storeview();
79
  }
80
 
81
  /**
82
  * Retrieve the data for this object
83
+ *
84
  * @return array
85
  */
86
  protected function _data()
87
  {
88
+ $subscription = $this->_customer->subscription();
 
89
 
90
+ $name = $this->_customer->name();
 
91
 
92
+ $email = $this->_customer->email();
 
93
 
94
+ $storeview = $this->_customer->storeview();
 
95
 
96
+ $customerId = Mage::helper('marketingsoftware/profile')->getCustomerCopernicaId($this->_customer, $storeview);
 
97
 
98
+ $customerBirthDate = $this->_customer->birthDate();
 
99
 
 
100
  if(is_object($customerBirthDate))
101
  $birthDate = $customerBirthDate->gmtDate();
102
  elseif(is_string($customerBirthDate) && strtotime($customerBirthDate))
103
  $birthDate = $customerBirthDate;
104
  else $birthDate = '0000-00-00';
105
 
 
106
  return array(
107
  'customer_id' => $customerId,
108
  'store_view' => (string)$storeview,
111
  'lastname' => is_object($name) ? $name->lastname() : null,
112
  'email' => $email,
113
  'birthdate' => $birthDate,
114
+ 'group' => $this->_customer->group(),
115
  'newsletter' => is_object($subscription) ? $subscription->status() : 'unknown',
116
+ 'gender' => $this->_customer->gender()
117
  );
118
  }
119
  }
app/code/community/Copernica/MarketingSoftware/Model/Copernica/{Profileorder.php → Profile/Order.php} RENAMED
@@ -27,88 +27,92 @@
27
  /**
28
  * An object to wrap the Copernica profile
29
  */
30
- class Copernica_MarketingSoftware_Model_Copernica_ProfileOrder extends Copernica_MarketingSoftware_Model_Copernica_Profile
31
  {
32
  /**
33
- * @var Copernica_MarketingSoftware_Model_Abstraction_Order
34
  */
35
- protected $order = false;
36
 
37
  /**
38
- * Set the customer object to this object
39
- * @param Copernica_MarketingSoftware_Model_Abstraction_Quote
40
- * @return Copernica_MarketingSoftware_Model_Copernica_ProfileCustomer
 
41
  */
42
- public function setOrder($order)
43
  {
44
- $this->order = $order;
45
  return $this;
46
  }
47
 
48
  /**
49
  * Get profile email.
 
50
  * @return string
51
  */
52
  public function email()
53
  {
54
- // get all addresses associated with order
55
- $addresses = $this->order->addresses();
56
 
57
- // find billing address
58
  foreach ($addresses as $address) {
59
- if (in_array('billing', $addr->type())) return $address->email();
 
 
60
  }
61
 
62
- // we don't have an address. Return empty string
63
  return '';
64
  }
65
 
66
  /**
67
  * Return store view associated with profile
 
68
  * @return string
69
  */
70
  public function storeView()
71
  {
72
- return (string)$this->order->storeview();
73
  }
74
 
75
  /**
76
  * Retrieve the data for this object
 
77
  * @return array
78
  */
79
  protected function _data()
80
  {
81
- // Get the addresses
82
- $addresses = $this->order->addresses();
83
 
84
- // Select an address
85
- if (count($addresses) == 1) $address = $addresses[0];
86
- else foreach ($addresses as $addr) if (in_array('billing', $addr->type())) $address = $addr;
 
 
 
 
 
 
87
 
88
- // Get subscription
89
- if (Mage::getModel('newsletter/subscriber')->loadByEmail($address->email())->getId())
90
- {
91
  $subscription = Mage::getModel('marketingsoftware/abstraction_subscription')
92
  ->setOriginal(Mage::getModel('newsletter/subscriber')->loadByEmail($address->email()));
 
 
93
  }
94
- else $subscription = false;
95
 
96
- // fetch the name object
97
  $name = $address->name();
98
 
99
- // placeholder for copeernica Id
100
  $customerId = null;
101
 
102
- // check if we have a customer to generate custoemr Id
103
- if ($customer = $this->order->customer()) $customerId = Mage::helper('marketingsoftware/profile')->getCustomerCopernicaId($customer, $this->order->storeview());
104
-
105
- // generate customer Id from email address
106
- else $customerId = Mage::helper('marketingsoftware/profile')->getEmailCopernicaId($address->email(), (string)$this->order->storeview());
107
 
108
- // return an array with customer data
109
  return array(
110
  'customer_id' => $customerId,
111
- 'store_view' => (string)$this->order->storeview(),
112
  'firstname' => is_object($name) ? $name->firstname() : null,
113
  'middlename' => is_object($name) ? $name->middlename() : null,
114
  'lastname' => is_object($name) ? $name->lastname() : null,
27
  /**
28
  * An object to wrap the Copernica profile
29
  */
30
+ class Copernica_MarketingSoftware_Model_Copernica_Profile_Order extends Copernica_MarketingSoftware_Model_Copernica_Profile
31
  {
32
  /**
33
+ * @var Copernica_MarketingSoftware_Model_Abstraction_Order
34
  */
35
+ protected $_order = false;
36
 
37
  /**
38
+ * Set the order object to this object
39
+ *
40
+ * @param Copernica_MarketingSoftware_Model_Abstraction_Order $order
41
+ * @return Copernica_MarketingSoftware_Model_Copernica_Profile_Order
42
  */
43
+ public function setOrder(Copernica_MarketingSoftware_Model_Abstraction_Order $order)
44
  {
45
+ $this->_order = $order;
46
  return $this;
47
  }
48
 
49
  /**
50
  * Get profile email.
51
+ *
52
  * @return string
53
  */
54
  public function email()
55
  {
56
+ $addresses = $this->_order->addresses();
 
57
 
 
58
  foreach ($addresses as $address) {
59
+ if (in_array('billing', $addr->type())) {
60
+ return $address->email();
61
+ }
62
  }
63
 
 
64
  return '';
65
  }
66
 
67
  /**
68
  * Return store view associated with profile
69
+ *
70
  * @return string
71
  */
72
  public function storeView()
73
  {
74
+ return (string)$this->_order->storeview();
75
  }
76
 
77
  /**
78
  * Retrieve the data for this object
79
+ *
80
  * @return array
81
  */
82
  protected function _data()
83
  {
84
+ $addresses = $this->_order->addresses();
 
85
 
86
+ if (count($addresses) == 1) {
87
+ $address = $addresses[0];
88
+ } else {
89
+ foreach ($addresses as $addr) {
90
+ if (in_array('billing', $addr->type())) {
91
+ $address = $addr;
92
+ }
93
+ }
94
+ }
95
 
96
+ if (Mage::getModel('newsletter/subscriber')->loadByEmail($address->email())->getId()) {
 
 
97
  $subscription = Mage::getModel('marketingsoftware/abstraction_subscription')
98
  ->setOriginal(Mage::getModel('newsletter/subscriber')->loadByEmail($address->email()));
99
+ } else {
100
+ $subscription = false;
101
  }
 
102
 
 
103
  $name = $address->name();
104
 
 
105
  $customerId = null;
106
 
107
+ if ($customer = $this->_order->customer()) {
108
+ $customerId = Mage::helper('marketingsoftware/profile')->getCustomerCopernicaId($customer, $this->_order->storeview());
109
+ } else {
110
+ $customerId = Mage::helper('marketingsoftware/profile')->getEmailCopernicaId($address->email(), (string)$this->_order->storeview());
111
+ }
112
 
 
113
  return array(
114
  'customer_id' => $customerId,
115
+ 'store_view' => (string)$this->_order->storeview(),
116
  'firstname' => is_object($name) ? $name->firstname() : null,
117
  'middlename' => is_object($name) ? $name->middlename() : null,
118
  'lastname' => is_object($name) ? $name->lastname() : null,
app/code/community/Copernica/MarketingSoftware/Model/Copernica/{Profilequote.php → Profile/Quote.php} RENAMED
@@ -27,94 +27,97 @@
27
  /**
28
  * An object to wrap the Copernica profile
29
  */
30
- class Copernica_MarketingSoftware_Model_Copernica_ProfileQuote extends Copernica_MarketingSoftware_Model_Copernica_Profile
31
  {
32
  /**
33
- * @var Copernica_MarketingSoftware_Model_Abstraction_Quote
34
  */
35
- protected $quote = false;
36
 
37
  /**
38
- * Set the customer object to this object
39
- * @param Copernica_MarketingSoftware_Model_Abstraction_Quote
40
- * @return Copernica_MarketingSoftware_Model_Copernica_ProfileCustomer
 
41
  */
42
- public function setQuote($quote)
43
  {
44
- $this->quote = $quote;
 
45
  return $this;
46
  }
47
 
48
  /**
49
  * Get profile email.
 
50
  * @return string
51
  */
52
  public function email()
53
  {
54
- // get all addresses associated with order
55
- $addresses = $this->quote->addresses();
56
 
57
- // find billing address
58
  foreach ($addresses as $address) {
59
- if (in_array('billing', $addr->type())) return $address->email();
 
 
60
  }
61
 
62
- // we don't have an address. Return empty string
63
  return '';
64
  }
65
 
66
  /**
67
  * Return store view associated with profile
68
- * @return string
 
69
  */
70
  public function storeView()
71
  {
72
- return (string)$this->quote->storeview();
73
  }
74
 
75
  /**
76
  * Retrieve the data for this object
 
77
  * @return array
78
  */
79
  protected function _data()
80
  {
81
- // Get the addresses
82
- $addresses = $this->quote->addresses();
83
 
84
- // placeholder for real address
85
  $address = null;
86
 
87
- // Select an address
88
- if (is_array($addresses))
89
- {
90
- if (count($addresses) == 1) $address = $addresses[0];
91
- else foreach ($addresses as $addr) if (in_array('billing', $addr->type())) $address = $addr;
 
 
 
 
 
92
  }
93
 
94
- // Get subscription
95
- if (Mage::getModel('newsletter/subscriber')->loadByEmail($address->email())->getId())
96
- {
97
  $subscription = Mage::getModel('marketingsoftware/abstraction_subscription')
98
  ->setOriginal(Mage::getModel('newsletter/subscriber')->loadByEmail($address->email()));
 
 
99
  }
100
- else $subscription = false;
101
 
102
- // fetch the name object
103
  $name = is_object($address) ? $address->name() : null;
104
 
105
- // placeholder for customer Id
106
  $customerId = null;
107
 
108
- // check if we have a customer to generate customer ID for copernica platform
109
- if ($customer = $this->quote->customer()) $customerId = Mage::helper('marketingsoftware/profile')->getCustomerCopernicaId($customer, $this->quote->storeview());
110
-
111
- // generate customer Id by email address and store view
112
- else $customerId = Mage::helper('marketingsoftware/profile')->getEmailCopernicaId($address->email(), $this->quote->storeview());
113
 
114
- // return an array with customer data
115
  return array(
116
  'customer_id' => $customerId,
117
- 'store_view' => (string)$this->quote->storeview(),
118
  'firstname' => is_object($name) ? $name->firstname() : null,
119
  'middlename' => is_object($name) ? $name->middlename() : null,
120
  'lastname' => is_object($name) ? $name->lastname() : null,
27
  /**
28
  * An object to wrap the Copernica profile
29
  */
30
+ class Copernica_MarketingSoftware_Model_Copernica_Profile_Quote extends Copernica_MarketingSoftware_Model_Copernica_Profile
31
  {
32
  /**
33
+ * @var Copernica_MarketingSoftware_Model_Abstraction_Quote
34
  */
35
+ protected $_quote = false;
36
 
37
  /**
38
+ * Set the quote object to this object
39
+ *
40
+ * @param Copernica_MarketingSoftware_Model_Abstraction_Quote $quote
41
+ * @return Copernica_MarketingSoftware_Model_Copernica_Profile_Quote
42
  */
43
+ public function setQuote(Copernica_MarketingSoftware_Model_Abstraction_Quote $quote)
44
  {
45
+ $this->_quote = $quote;
46
+
47
  return $this;
48
  }
49
 
50
  /**
51
  * Get profile email.
52
+ *
53
  * @return string
54
  */
55
  public function email()
56
  {
57
+ $addresses = $this->_quote->addresses();
 
58
 
 
59
  foreach ($addresses as $address) {
60
+ if (in_array('billing', $addr->type())) {
61
+ return $address->email();
62
+ }
63
  }
64
 
 
65
  return '';
66
  }
67
 
68
  /**
69
  * Return store view associated with profile
70
+ *
71
+ * @return string
72
  */
73
  public function storeView()
74
  {
75
+ return (string)$this->_quote->storeview();
76
  }
77
 
78
  /**
79
  * Retrieve the data for this object
80
+ *
81
  * @return array
82
  */
83
  protected function _data()
84
  {
85
+ $addresses = $this->_quote->addresses();
 
86
 
 
87
  $address = null;
88
 
89
+ if (is_array($addresses)) {
90
+ if (count($addresses) == 1) {
91
+ $address = $addresses[0];
92
+ } else {
93
+ foreach ($addresses as $addr) {
94
+ if (in_array('billing', $addr->type())) {
95
+ $address = $addr;
96
+ }
97
+ }
98
+ }
99
  }
100
 
101
+ if (Mage::getModel('newsletter/subscriber')->loadByEmail($address->email())->getId()) {
 
 
102
  $subscription = Mage::getModel('marketingsoftware/abstraction_subscription')
103
  ->setOriginal(Mage::getModel('newsletter/subscriber')->loadByEmail($address->email()));
104
+ } else {
105
+ $subscription = false;
106
  }
 
107
 
 
108
  $name = is_object($address) ? $address->name() : null;
109
 
 
110
  $customerId = null;
111
 
112
+ if ($customer = $this->_quote->customer()) {
113
+ $customerId = Mage::helper('marketingsoftware/profile')->getCustomerCopernicaId($customer, $this->_quote->storeview());
114
+ } else {
115
+ $customerId = Mage::helper('marketingsoftware/profile')->getEmailCopernicaId($address->email(), $this->_quote->storeview());
116
+ }
117
 
 
118
  return array(
119
  'customer_id' => $customerId,
120
+ 'store_view' => (string)$this->_quote->storeview(),
121
  'firstname' => is_object($name) ? $name->firstname() : null,
122
  'middlename' => is_object($name) ? $name->middlename() : null,
123
  'lastname' => is_object($name) ? $name->lastname() : null,
app/code/community/Copernica/MarketingSoftware/Model/Copernica/{Profilesubscription.php → Profile/Subscription.php} RENAMED
@@ -27,41 +27,44 @@
27
  /**
28
  * An object to wrap the Copernica profile
29
  */
30
- class Copernica_MarketingSoftware_Model_Copernica_ProfileSubscription extends Copernica_MarketingSoftware_Model_Copernica_Profile
31
  {
32
  /**
33
- * @var Copernica_MarketingSoftware_Model_Abstraction_Customer
34
  */
35
- protected $subscription = false;
36
 
37
  /**
38
- * Set the customer object to this object.
39
  *
40
- * @param Copernica_MarketingSoftware_Model_Abstraction_Subscription
41
- * @return Copernica_MarketingSoftware_Model_Copernica_ProfileCustomer
42
  */
43
- public function setSubscription($customer)
44
  {
45
- $this->subscription = $customer;
 
46
  return $this;
47
  }
48
 
49
  /**
50
  * Get profile email.
 
51
  * @return string
52
  */
53
  public function email()
54
  {
55
- return (string)$this->subscription->storeView();
56
  }
57
 
58
  /**
59
  * Return store view associated with profile
 
60
  * @return string
61
  */
62
  public function storeView()
63
  {
64
- return (string)$this->subscription->storeview();
65
  }
66
 
67
  /**
@@ -71,20 +74,20 @@ class Copernica_MarketingSoftware_Model_Copernica_ProfileSubscription extends Co
71
  */
72
  protected function _data()
73
  {
74
- // placeholder for customer Id
75
  $customerId = null;
76
 
77
- // check if we have a proper customer
78
- if ($customer = $this->subscription->customer()) $customerId = Mage::helper('marketingsoftware/profile')->getCustomerCopernicaId($customer, $this->subscription->storeView());
79
- else $customerId = Mage::helper('marketingsoftware/profile')->getEmailCopernicaId($this->subscription->email(), $this->subscription->storeView());
 
 
80
 
81
- // return an array with customer data
82
  return array(
83
  'customer_id' => $customerId,
84
- 'store_view' => (string)$this->subscription->storeView(),
85
- 'email' => $this->subscription->email(),
86
  'group' => Mage::getModel('customer/group')->load(0)->getCode(),
87
- 'newsletter' => $this->subscription->status(),
88
  );
89
  }
90
  }
27
  /**
28
  * An object to wrap the Copernica profile
29
  */
30
+ class Copernica_MarketingSoftware_Model_Copernica_Profile_Subscription extends Copernica_MarketingSoftware_Model_Copernica_Profile
31
  {
32
  /**
33
+ * @var Copernica_MarketingSoftware_Model_Abstraction_Subscription
34
  */
35
+ protected $_abstractSubscription = false;
36
 
37
  /**
38
+ * Set the subscription object to this object.
39
  *
40
+ * @param Copernica_MarketingSoftware_Model_Abstraction_Subscription $subscription
41
+ * @return Copernica_MarketingSoftware_Model_Copernica_Profile_Subscription
42
  */
43
+ public function setSubscription(Copernica_MarketingSoftware_Model_Abstraction_Subscription $subscription)
44
  {
45
+ $this->_abstractSubscription = $subscription;
46
+
47
  return $this;
48
  }
49
 
50
  /**
51
  * Get profile email.
52
+ *
53
  * @return string
54
  */
55
  public function email()
56
  {
57
+ return (string)$this->_abstractSubscription->storeView();
58
  }
59
 
60
  /**
61
  * Return store view associated with profile
62
+ *
63
  * @return string
64
  */
65
  public function storeView()
66
  {
67
+ return (string)$this->_abstractSubscription->storeview();
68
  }
69
 
70
  /**
74
  */
75
  protected function _data()
76
  {
 
77
  $customerId = null;
78
 
79
+ if ($customer = $this->_abstractSubscription->customer()) {
80
+ $customerId = Mage::helper('marketingsoftware/profile')->getCustomerCopernicaId($customer, $this->_abstractSubscription->storeView());
81
+ } else {
82
+ $customerId = Mage::helper('marketingsoftware/profile')->getEmailCopernicaId($this->_abstractSubscription->email(), $this->_abstractSubscription->storeView());
83
+ }
84
 
 
85
  return array(
86
  'customer_id' => $customerId,
87
+ 'store_view' => (string)$this->_abstractSubscription->storeView(),
88
+ 'email' => $this->_abstractSubscription->email(),
89
  'group' => Mage::getModel('customer/group')->load(0)->getCode(),
90
+ 'newsletter' => $this->_abstractSubscription->status(),
91
  );
92
  }
93
  }
app/code/community/Copernica/MarketingSoftware/Model/Copernica/{Cartitem → Quote/Item}/Subprofile.php RENAMED
@@ -27,31 +27,34 @@
27
  /**
28
  * An object to wrap the Copernica profile
29
  */
30
- class Copernica_MarketingSoftware_Model_Copernica_Cartitem_Subprofile extends Copernica_MarketingSoftware_Model_Copernica_Abstract
31
  {
32
  /**
33
  * @var Copernica_MarketingSoftware_Model_Abstraction_Quote_Item
34
  */
35
- protected $quoteItem = false;
36
 
37
  /**
38
  * @var string
39
  */
40
- protected $status = 'basket';
41
 
42
  /**
43
- * Set the status of this cart item
44
- * @param String
45
- * @return Copernica_MarketingSoftware_Model_Copernica_Cartitem_Subprofile
 
46
  */
47
  public function setStatus($status)
48
  {
49
- $this->status = $status;
 
50
  return $this;
51
  }
52
 
53
  /**
54
  * Return the identifier for this profile
 
55
  * @return string
56
  */
57
  public function id()
@@ -61,26 +64,30 @@ class Copernica_MarketingSoftware_Model_Copernica_Cartitem_Subprofile extends Co
61
 
62
  /**
63
  * Try to store a quote item
64
- * @param Copernica_MarketingSoftware_Model_Abstraction_Quote_Item
65
- * @return Copernica_MarketingSoftware_Model_Copernica_Cartitem_Subprofile;
 
66
  */
67
- public function setQuoteItem($item)
68
  {
69
- $this->quoteItem = $item;
 
70
  return $this;
71
  }
72
 
73
  /**
74
  * Get linked fields
 
75
  * @return array
76
  */
77
  public function linkedFields()
78
  {
79
- return Mage::helper('marketingsoftware/config')->getLinkedCartItemFields();
80
  }
81
 
82
  /**
83
  * Get the required fields
 
84
  * @return array
85
  */
86
  public function requiredFields()
@@ -90,21 +97,22 @@ class Copernica_MarketingSoftware_Model_Copernica_Cartitem_Subprofile extends Co
90
 
91
  /**
92
  * Retrieve the data for this object
 
93
  * @return array
94
  */
95
  protected function _data()
96
  {
97
- // Store the quoteItem and the product localy
98
- $quoteItem = $this->quoteItem;
99
  $product = $quoteItem->product();
 
100
  $quote = $quoteItem->quote();
101
 
102
- // construct most of the data from objects that should be available
103
  $data = array(
104
  'item_id' => $quoteItem->id(),
105
  'quote_id' => $quote->id(),
106
  'product_id' => $product->id(),
107
- 'status' => $this->status,
108
  'name' => $product->name(),
109
  'sku' => $product->sku(),
110
  'attribute_set' => $product->attributeSet(),
@@ -115,31 +123,25 @@ class Copernica_MarketingSoftware_Model_Copernica_Cartitem_Subprofile extends Co
115
  'options' => (string)$quoteItem->options()
116
  );
117
 
118
- // get store view from quote
119
  $storeView = $quote->storeView();
120
 
121
- // check if we have a valid store view
122
  if ($storeView) {
123
  $storeId = $storeView->id();
124
 
125
  $data['store_view'] = (string)$storeView;
126
  $data['url'] = $product->productUrl($storeId);
127
  $data['image'] = $product->imageUrl($storeId);
128
- }
129
- // if we don't have store view then just assign empty strings
130
- else {
131
  $data['store_view'] = '';
132
  $data['url'] = '';
133
  $data['image'] = '';
134
  }
135
 
136
- // assign price if we have one
137
  if ($price = $quoteItem->price()) {
138
  $data['price'] = $price->itemPrice();
139
  $data['total_price'] = $price->total();
140
  }
141
 
142
- // format categories
143
  $data['categories'] = implode("\n", array_map(function($category) {
144
  return implode(' > ', $category);
145
  }, $product->categories()));
27
  /**
28
  * An object to wrap the Copernica profile
29
  */
30
+ class Copernica_MarketingSoftware_Model_Copernica_Quote_Item_Subprofile extends Copernica_MarketingSoftware_Model_Copernica_Abstract
31
  {
32
  /**
33
  * @var Copernica_MarketingSoftware_Model_Abstraction_Quote_Item
34
  */
35
+ protected $_quoteItem = false;
36
 
37
  /**
38
  * @var string
39
  */
40
+ protected $_status = 'basket';
41
 
42
  /**
43
+ * Set the status of this quote item
44
+ *
45
+ * @param string $status
46
+ * @return Copernica_MarketingSoftware_Model_Copernica_Quote_Item_Subprofile
47
  */
48
  public function setStatus($status)
49
  {
50
+ $this->_status = $status;
51
+
52
  return $this;
53
  }
54
 
55
  /**
56
  * Return the identifier for this profile
57
+ *
58
  * @return string
59
  */
60
  public function id()
64
 
65
  /**
66
  * Try to store a quote item
67
+ *
68
+ * @param Copernica_MarketingSoftware_Model_Abstraction_Quote_Item $item
69
+ * @return Copernica_MarketingSoftware_Model_Copernica_Quote_Item_Subprofile;
70
  */
71
+ public function setQuoteItem(Copernica_MarketingSoftware_Model_Abstraction_Quote_Item $item)
72
  {
73
+ $this->_quoteItem = $item;
74
+
75
  return $this;
76
  }
77
 
78
  /**
79
  * Get linked fields
80
+ *
81
  * @return array
82
  */
83
  public function linkedFields()
84
  {
85
+ return Mage::helper('marketingsoftware/config')->getLinkedQuoteItemFields();
86
  }
87
 
88
  /**
89
  * Get the required fields
90
+ *
91
  * @return array
92
  */
93
  public function requiredFields()
97
 
98
  /**
99
  * Retrieve the data for this object
100
+ *
101
  * @return array
102
  */
103
  protected function _data()
104
  {
105
+ $quoteItem = $this->_quoteItem;
106
+
107
  $product = $quoteItem->product();
108
+
109
  $quote = $quoteItem->quote();
110
 
 
111
  $data = array(
112
  'item_id' => $quoteItem->id(),
113
  'quote_id' => $quote->id(),
114
  'product_id' => $product->id(),
115
+ 'status' => $this->_status,
116
  'name' => $product->name(),
117
  'sku' => $product->sku(),
118
  'attribute_set' => $product->attributeSet(),
123
  'options' => (string)$quoteItem->options()
124
  );
125
 
 
126
  $storeView = $quote->storeView();
127
 
 
128
  if ($storeView) {
129
  $storeId = $storeView->id();
130
 
131
  $data['store_view'] = (string)$storeView;
132
  $data['url'] = $product->productUrl($storeId);
133
  $data['image'] = $product->imageUrl($storeId);
134
+ } else {
 
 
135
  $data['store_view'] = '';
136
  $data['url'] = '';
137
  $data['image'] = '';
138
  }
139
 
 
140
  if ($price = $quoteItem->price()) {
141
  $data['price'] = $price->itemPrice();
142
  $data['total_price'] = $price->total();
143
  }
144
 
 
145
  $data['categories'] = implode("\n", array_map(function($category) {
146
  return implode(' > ', $category);
147
  }, $product->categories()));
app/code/community/Copernica/MarketingSoftware/Model/Copernica/Wishlist/Item/Subprofile.php ADDED
@@ -0,0 +1,132 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Copernica Marketing Software
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0).
8
+ * It is available through the world-wide-web at this URL:
9
+ * http://opensource.org/licenses/osl-3.0.php
10
+ * If you are unable to obtain a copy of the license through the
11
+ * world-wide-web, please send an email to copernica@support.cream.nl
12
+ * so we can send you a copy immediately.
13
+ *
14
+ * DISCLAIMER
15
+ *
16
+ * Do not edit or add to this file if you wish to upgrade this software
17
+ * to newer versions in the future. If you wish to customize this module
18
+ * for your needs please refer to http://www.magento.com/ for more
19
+ * information.
20
+ *
21
+ * @category Copernica
22
+ * @package Copernica_MarketingSoftware
23
+ * @copyright Copyright (c) 2011-2012 Copernica & Cream. (http://docs.cream.nl/)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ /**
28
+ * An object to wrap the Copernica profile
29
+ */
30
+ class Copernica_MarketingSoftware_Model_Copernica_Wishlist_Item_Subprofile extends Copernica_MarketingSoftware_Model_Copernica_Abstract
31
+ {
32
+ /**
33
+ * @var Copernica_MarketingSoftware_Model_Abstraction_Wishlist_Item
34
+ */
35
+ protected $_wishlistItem = false;
36
+
37
+ /**
38
+ * Return the identifier for this profile
39
+ *
40
+ * @return string
41
+ */
42
+ public function id()
43
+ {
44
+ return $this['item_id'];
45
+ }
46
+
47
+ /**
48
+ * Try to store a wishlist item
49
+ *
50
+ * @param Copernica_MarketingSoftware_Model_Abstraction_Wishlist_Item $item
51
+ * @return Copernica_MarketingSoftware_Model_Copernica_Wishlist_Item_Subprofile;
52
+ */
53
+ public function setWishlistItem(Copernica_MarketingSoftware_Model_Abstraction_Wishlist_Item $item)
54
+ {
55
+ $this->_wishlistItem = $item;
56
+
57
+ return $this;
58
+ }
59
+
60
+ /**
61
+ * Get linked fields
62
+ *
63
+ * @return array
64
+ */
65
+ public function linkedFields()
66
+ {
67
+ return Mage::helper('marketingsoftware/config')->getLinkedWishlistFields();
68
+ }
69
+
70
+ /**
71
+ * Get the required fields
72
+ *
73
+ * @return array
74
+ */
75
+ public function requiredFields()
76
+ {
77
+ return Mage::helper('marketingsoftware')->requiredWishlistItemFields();
78
+ }
79
+
80
+ /**
81
+ * Retrieve the data for this object
82
+ *
83
+ * @return array
84
+ */
85
+ protected function _data()
86
+ {
87
+ $wishlistItem = $this->_wishlistItem;
88
+
89
+ $product = $wishlistItem->product();
90
+
91
+ $wishlist = $wishlistItem->wishlist();
92
+
93
+ $data = array(
94
+ 'item_id' => $wishlistItem->id(),
95
+ 'wishlist_id' => $wishlist->id(),
96
+ 'product_id' => $product->id(),
97
+ 'name' => $product->name(),
98
+ 'sku' => $product->sku(),
99
+ 'attribute_set' => $product->attributeSet(),
100
+ 'weight' => $wishlistItem->weight(),
101
+ 'quantity' => $wishlistItem->quantity(),
102
+ 'timestamp' => $wishlistItem->timestamp(),
103
+ 'attributes' => (string)$product->attributes(),
104
+ 'options' => (string)$wishlistItem->options()
105
+ );
106
+
107
+ $storeView = $wishlist->storeView();
108
+
109
+ if ($storeView) {
110
+ $storeId = $storeView->id();
111
+
112
+ $data['store_view'] = (string)$storeView;
113
+ $data['url'] = $product->productUrl($storeId);
114
+ $data['image'] = $product->imageUrl($storeId);
115
+ } else {
116
+ $data['store_view'] = '';
117
+ $data['url'] = '';
118
+ $data['image'] = '';
119
+ }
120
+
121
+ if ($price = $wishlistItem->price()) {
122
+ $data['price'] = $price->itemPrice();
123
+ $data['total_price'] = $price->total();
124
+ }
125
+
126
+ $data['categories'] = implode("\n", array_map(function($category) {
127
+ return implode(' > ', $category);
128
+ }, $product->categories()));
129
+
130
+ return $data;
131
+ }
132
+ }
app/code/community/Copernica/MarketingSoftware/Model/{ErrorQueue.php → Error/Queue.php} RENAMED
@@ -27,18 +27,19 @@
27
  /**
28
  * This class will represent item from error queue.
29
  */
30
- class Copernica_MarketingSoftware_Model_ErrorQueue extends Mage_Core_Model_Abstract
31
  {
32
  /**
33
  * Construct the model
34
  */
35
  protected function _construct()
36
  {
37
- $this->_init('marketingsoftware/ErrorQueue');
38
  }
39
 
40
  /**
41
  * Get object instance.
 
42
  * @return object
43
  */
44
  public function getObject()
@@ -48,7 +49,8 @@ class Copernica_MarketingSoftware_Model_ErrorQueue extends Mage_Core_Model_Abstr
48
 
49
  /**
50
  * Set object instance
51
- * @param object
 
52
  */
53
  public function setObject($object)
54
  {
@@ -57,7 +59,8 @@ class Copernica_MarketingSoftware_Model_ErrorQueue extends Mage_Core_Model_Abstr
57
 
58
  /**
59
  * Set customer Id
60
- * @param int
 
61
  */
62
  public function setCustomerId($customerId)
63
  {
@@ -66,6 +69,7 @@ class Copernica_MarketingSoftware_Model_ErrorQueue extends Mage_Core_Model_Abstr
66
 
67
  /**
68
  * Get customer Id
 
69
  * @return int
70
  */
71
  public function getCustomerId()
@@ -83,36 +87,20 @@ class Copernica_MarketingSoftware_Model_ErrorQueue extends Mage_Core_Model_Abstr
83
 
84
  /**
85
  * This method will create instance from processing queue item
86
- * @param Copernica_MarketingSoftware_Model_Queue
87
- * @param Copernica_MarketingSoftware_Model_ErrorQueue
88
  */
89
- static public function createFromQueueItem(Copernica_MarketingSoftware_Model_Queue $queueItem)
90
  {
91
- // this will create a new error item model
92
- $errorModel = Mage::getModel('marketingsoftware/errorqueue');
93
-
94
- // store object
95
  $errorModel->setObject($queueItem->getObject());
96
-
97
- // store old Id
98
  $errorModel->setOldId($queueItem->getId());
99
-
100
- // store action
101
  $errorModel->setAction($queueItem->getAction());
102
-
103
- // store queue time
104
  $errorModel->setQueueTime($queueItem->getQueueTime());
105
-
106
- // store result
107
  $errorModel->setResult($queueItem->getResult());
108
-
109
- // store result time
110
  $errorModel->setResultTime($queueItem->getResultTime());
111
-
112
- // store customer
113
  $errorModel->setCustomerId($queueItem->getCustomerId());
114
 
115
- // return created error model
116
  return $errorModel;
117
  }
118
  }
27
  /**
28
  * This class will represent item from error queue.
29
  */
30
+ class Copernica_MarketingSoftware_Model_Error_Queue extends Mage_Core_Model_Abstract
31
  {
32
  /**
33
  * Construct the model
34
  */
35
  protected function _construct()
36
  {
37
+ $this->_init('marketingsoftware/error_queue');
38
  }
39
 
40
  /**
41
  * Get object instance.
42
+ *
43
  * @return object
44
  */
45
  public function getObject()
49
 
50
  /**
51
  * Set object instance
52
+ *
53
+ * @param object $object
54
  */
55
  public function setObject($object)
56
  {
59
 
60
  /**
61
  * Set customer Id
62
+ *
63
+ * @param int $customerId
64
  */
65
  public function setCustomerId($customerId)
66
  {
69
 
70
  /**
71
  * Get customer Id
72
+ *
73
  * @return int
74
  */
75
  public function getCustomerId()
87
 
88
  /**
89
  * This method will create instance from processing queue item
90
+ *
91
+ * @param Copernica_MarketingSoftware_Model_Queue_Event_Item $queueItem
92
  */
93
+ static public function createFromQueueItem(Copernica_MarketingSoftware_Model_Queue_Event_Item $queueItem)
94
  {
95
+ $errorModel = Mage::getModel('marketingsoftware/error_queue');
 
 
 
96
  $errorModel->setObject($queueItem->getObject());
 
 
97
  $errorModel->setOldId($queueItem->getId());
 
 
98
  $errorModel->setAction($queueItem->getAction());
 
 
99
  $errorModel->setQueueTime($queueItem->getQueueTime());
 
 
100
  $errorModel->setResult($queueItem->getResult());
 
 
101
  $errorModel->setResultTime($queueItem->getResultTime());
 
 
102
  $errorModel->setCustomerId($queueItem->getCustomerId());
103
 
 
104
  return $errorModel;
105
  }
106
  }
app/code/community/Copernica/MarketingSoftware/Model/Mysql4/{AbandonedCart.php → Abandoned/Cart.php} RENAMED
@@ -27,13 +27,13 @@
27
  /**
28
  * Resource that we will use to track abandoned carts sent to copernica.
29
  */
30
- class Copernica_MarketingSoftware_Model_Mysql4_AbandonedCart extends Mage_Core_Model_Mysql4_Abstract
31
  {
32
  /**
33
  * Construct model
34
  */
35
  protected function _construct()
36
  {
37
- $this->_init('marketingsoftware/abandonedCart', 'id');
38
  }
39
  }
27
  /**
28
  * Resource that we will use to track abandoned carts sent to copernica.
29
  */
30
+ class Copernica_MarketingSoftware_Model_Mysql4_Abandoned_Cart extends Mage_Core_Model_Mysql4_Abstract
31
  {
32
  /**
33
  * Construct model
34
  */
35
  protected function _construct()
36
  {
37
+ $this->_init('marketingsoftware/abandoned_cart', 'id');
38
  }
39
  }
app/code/community/Copernica/MarketingSoftware/Model/Mysql4/Abandoned/Cart/Collection.php ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class Copernica_MarketingSoftware_Model_Mysql4_Abandoned_Cart_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract
4
+ {
5
+ /**
6
+ * Construct and initialize object
7
+ */
8
+ protected function _construct()
9
+ {
10
+ $this->_init('marketingsoftware/abandoned_cart');
11
+ }
12
+ }
app/code/community/Copernica/MarketingSoftware/Model/Mysql4/AbandonedCart/Collection.php DELETED
@@ -1,12 +0,0 @@
1
- <?php
2
-
3
- class Copernica_MarketingSoftware_Model_Mysql4_AbandonedCart_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract
4
- {
5
- /**
6
- * Construct and initialize object
7
- */
8
- protected function _construct()
9
- {
10
- $this->_init('marketingsoftware/abandonedCart');
11
- }
12
- }
 
 
 
 
 
 
 
 
 
 
 
 
app/code/community/Copernica/MarketingSoftware/Model/Mysql4/{ErrorQueue.php → Error/Queue.php} RENAMED
@@ -24,13 +24,13 @@
24
  * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
  */
26
 
27
- class Copernica_MarketingSoftware_Model_Mysql4_ErrorQueue extends Mage_Core_Model_Mysql4_Abstract
28
  {
29
  /**
30
  * Fancy, magento-only constructor for initializing objects
31
  */
32
  protected function _construct()
33
  {
34
- $this->_init('marketingsoftware/errorqueue', 'id');
35
  }
36
  }
24
  * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
  */
26
 
27
+ class Copernica_MarketingSoftware_Model_Mysql4_Error_Queue extends Mage_Core_Model_Mysql4_Abstract
28
  {
29
  /**
30
  * Fancy, magento-only constructor for initializing objects
31
  */
32
  protected function _construct()
33
  {
34
+ $this->_init('marketingsoftware/error_queue', 'id');
35
  }
36
  }
app/code/community/Copernica/MarketingSoftware/Model/Mysql4/{SyncProfile → Error/Queue}/Collection.php RENAMED
@@ -24,10 +24,13 @@
24
  * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
  */
26
 
27
- class Copernica_MarketingSoftware_Model_Mysql4_SyncProfile_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract
28
  {
 
 
 
29
  protected function _construct()
30
  {
31
- $this->_init('marketingsoftware/syncProfile');
32
  }
33
  }
24
  * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
  */
26
 
27
+ class Copernica_MarketingSoftware_Model_Mysql4_Error_Queue_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract
28
  {
29
+ /**
30
+ * Construct and initialize object
31
+ */
32
  protected function _construct()
33
  {
34
+ $this->_init('marketingsoftware/error_queue');
35
  }
36
  }
app/code/community/Copernica/MarketingSoftware/Model/Mysql4/{ProfileCache.php → Profile/Cache.php} RENAMED
@@ -27,13 +27,13 @@
27
  /**
28
  * This class will present and interface to copernica_profile_cache mysql table.
29
  */
30
- class Copernica_MarketingSoftware_Model_Mysql4_ProfileCache extends Mage_Core_Model_Mysql4_Abstract
31
  {
32
  /**
33
  * Construct model
34
  */
35
  protected function _construct()
36
  {
37
- $this->_init('marketingsoftware/profileCache', 'id');
38
  }
39
  }
27
  /**
28
  * This class will present and interface to copernica_profile_cache mysql table.
29
  */
30
+ class Copernica_MarketingSoftware_Model_Mysql4_Profile_Cache extends Mage_Core_Model_Mysql4_Abstract
31
  {
32
  /**
33
  * Construct model
34
  */
35
  protected function _construct()
36
  {
37
+ $this->_init('marketingsoftware/profile_cache', 'id');
38
  }
39
  }
app/code/community/Copernica/MarketingSoftware/Model/Mysql4/{ErrorQueue → Profile/Cache}/Collection.php RENAMED
@@ -24,13 +24,10 @@
24
  * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
  */
26
 
27
- class Copernica_MarketingSoftware_Model_Mysql4_ErrorQueue_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract
28
  {
29
- /**
30
- * Construct and initialize object
31
- */
32
  protected function _construct()
33
  {
34
- $this->_init('marketingsoftware/errorqueue');
35
  }
36
  }
24
  * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
  */
26
 
27
+ class Copernica_MarketingSoftware_Model_Mysql4_Profile_Cache_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract
28
  {
 
 
 
29
  protected function _construct()
30
  {
31
+ $this->_init('marketingsoftware/profile_cache');
32
  }
33
  }
app/code/community/Copernica/MarketingSoftware/Model/Mysql4/{Queue.php → Queue/Item.php} RENAMED
@@ -24,43 +24,39 @@
24
  * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
  */
26
 
27
- class Copernica_MarketingSoftware_Model_Mysql4_Queue extends Mage_Core_Model_Mysql4_Abstract
28
  {
29
  /**
30
  * Construct the object and initialize the values
31
  */
32
  protected function _construct()
33
  {
34
- $this->_init('marketingsoftware/queue', 'id');
35
  }
36
 
37
  /**
38
  * Get first free lock that can be aqcuired
 
39
  * @return string|false
40
  */
41
  public function getFirstFree()
42
  {
43
- // we will need a adapter
44
  $adapter = $this->_getReadAdapter();
45
 
46
- // fetch all quniqu customer
47
  $result = $adapter->query('SELECT DISTINCT customer FROM '.$this->getMainTable().';')->fetchAll();
48
 
49
- // iterate over all unique customer
50
  foreach ($result as $row) {
51
-
52
- // check if lock is free
53
  $lockResult = $adapter->query("select is_free_lock('COPERNICA_".$row['customer']."') as 'lock'")->fetchAll();
54
 
55
- // if we have a potential lock to use try to lock it
56
- if ($lockResult[0]['lock'])
57
- {
58
  $lockResult = $adapter->query("select get_lock('COPERNICA_".$row['customer']."', 1) as 'lock'")->fetchAll();
59
- if ($lockResult[0]['lock'] == 1) return $row['customer'];
 
 
 
60
  }
61
  }
62
 
63
- // we don't have a lock
64
  return false;
65
  }
66
  }
24
  * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
  */
26
 
27
+ class Copernica_MarketingSoftware_Model_Mysql4_Queue_Item extends Mage_Core_Model_Mysql4_Abstract
28
  {
29
  /**
30
  * Construct the object and initialize the values
31
  */
32
  protected function _construct()
33
  {
34
+ $this->_init('marketingsoftware/queue_item', 'id');
35
  }
36
 
37
  /**
38
  * Get first free lock that can be aqcuired
39
+ *
40
  * @return string|false
41
  */
42
  public function getFirstFree()
43
  {
 
44
  $adapter = $this->_getReadAdapter();
45
 
 
46
  $result = $adapter->query('SELECT DISTINCT customer FROM '.$this->getMainTable().';')->fetchAll();
47
 
 
48
  foreach ($result as $row) {
 
 
49
  $lockResult = $adapter->query("select is_free_lock('COPERNICA_".$row['customer']."') as 'lock'")->fetchAll();
50
 
51
+ if ($lockResult[0]['lock']) {
 
 
52
  $lockResult = $adapter->query("select get_lock('COPERNICA_".$row['customer']."', 1) as 'lock'")->fetchAll();
53
+
54
+ if ($lockResult[0]['lock'] == 1) {
55
+ return $row['customer'];
56
+ }
57
  }
58
  }
59
 
 
60
  return false;
61
  }
62
  }
app/code/community/Copernica/MarketingSoftware/Model/Mysql4/Queue/{Collection.php → Item/Collection.php} RENAMED
@@ -24,32 +24,31 @@
24
  * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
  */
26
 
27
- class Copernica_MarketingSoftware_Model_Mysql4_Queue_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract
28
  {
29
  /**
30
  * Construct and initialize object
31
  */
32
  protected function _construct()
33
  {
34
- $this->_init('marketingsoftware/queue');
35
  }
36
 
37
  /**
38
  * Add a default order, sorted ascending by queue time
39
- * @return Copernica_MarketingSoftware_Model_Mysql4_Queue_Collection
 
40
  */
41
  public function addDefaultOrder()
42
  {
43
- // If a result was processed before, we should process it if we have nothing
44
- // else to process, we want to import the queue items without an result_time
45
- // first and then in order of queue time.
46
  return $this->addOrder('result_time', self::SORT_ORDER_ASC)
47
  ->addOrder('queue_time', self::SORT_ORDER_ASC);
48
  }
49
 
50
  /**
51
  * Get the time of the oldest record
52
- * @return string mysql formatted date timestamp
 
53
  */
54
  public function getQueueStartTime()
55
  {
@@ -58,7 +57,8 @@ class Copernica_MarketingSoftware_Model_Mysql4_Queue_Collection extends Mage_Cor
58
 
59
  /**
60
  * Get the result of the oldest record
61
- * @return string message
 
62
  */
63
  public function getOldestResult()
64
  {
24
  * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
  */
26
 
27
+ class Copernica_MarketingSoftware_Model_Mysql4_Queue_Item_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract
28
  {
29
  /**
30
  * Construct and initialize object
31
  */
32
  protected function _construct()
33
  {
34
+ $this->_init('marketingsoftware/queue_item');
35
  }
36
 
37
  /**
38
  * Add a default order, sorted ascending by queue time
39
+ *
40
+ * @return Copernica_MarketingSoftware_Model_Mysql4_Queue_Item_Collection
41
  */
42
  public function addDefaultOrder()
43
  {
 
 
 
44
  return $this->addOrder('result_time', self::SORT_ORDER_ASC)
45
  ->addOrder('queue_time', self::SORT_ORDER_ASC);
46
  }
47
 
48
  /**
49
  * Get the time of the oldest record
50
+ *
51
+ * @return string
52
  */
53
  public function getQueueStartTime()
54
  {
57
 
58
  /**
59
  * Get the result of the oldest record
60
+ *
61
+ * @return string
62
  */
63
  public function getOldestResult()
64
  {
app/code/community/Copernica/MarketingSoftware/Model/Mysql4/{SyncProfile.php → Sync/Profile.php} RENAMED
@@ -24,13 +24,13 @@
24
  * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
  */
26
 
27
- class Copernica_MarketingSoftware_Model_Mysql4_SyncProfile extends Mage_Core_Model_Mysql4_Abstract
28
  {
29
  /**
30
  * Construct model
31
  */
32
  protected function _construct()
33
  {
34
- $this->_init('marketingsoftware/syncProfile', 'id');
35
  }
36
  }
24
  * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
  */
26
 
27
+ class Copernica_MarketingSoftware_Model_Mysql4_Sync_Profile extends Mage_Core_Model_Mysql4_Abstract
28
  {
29
  /**
30
  * Construct model
31
  */
32
  protected function _construct()
33
  {
34
+ $this->_init('marketingsoftware/sync_profile', 'id');
35
  }
36
  }
app/code/community/Copernica/MarketingSoftware/Model/Mysql4/{ProfileCache → Sync/Profile}/Collection.php RENAMED
@@ -24,10 +24,10 @@
24
  * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
  */
26
 
27
- class Copernica_MarketingSoftware_Model_Mysql4_ProfileCache_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract
28
  {
29
  protected function _construct()
30
  {
31
- $this->_init('marketingsoftware/profileCache');
32
  }
33
  }
24
  * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
  */
26
 
27
+ class Copernica_MarketingSoftware_Model_Mysql4_Sync_Profile_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract
28
  {
29
  protected function _construct()
30
  {
31
+ $this->_init('marketingsoftware/sync_profile');
32
  }
33
  }
app/code/community/Copernica/MarketingSoftware/Model/Observer.php CHANGED
@@ -31,14 +31,13 @@ class Copernica_MarketingSoftware_Model_Observer
31
  {
32
  /**
33
  * Check if event is added in store that we want to sync.
 
34
  * @return bool
35
  */
36
- protected function isValidStore()
37
  {
38
- // get current store Id
39
  $currentStoreId = Mage::app()->getStore()->getId();
40
 
41
- // check if current store is enabled stores
42
  return Mage::helper('marketingsoftware/config')->isEnabledStore($currentStoreId);
43
  }
44
 
@@ -48,182 +47,174 @@ class Copernica_MarketingSoftware_Model_Observer
48
  * 'checkout_controller_multishipping_shipping_post'
49
  * This method is fired during checkout process, after the customer has entered billing address
50
  * and saved the shipping method
51
- * @param Varien_Event_Observer
 
52
  */
53
  public function checkoutSaveStep(Varien_Event_Observer $observer)
54
  {
55
- // if the plug-in is not enabled, skip this
56
- if (!$this->enabled() || !$this->isValidStore()) return;
57
-
58
- // Do we have a valid item?
59
- if (!is_object($quote = $observer->getEvent()->getQuote())) return;
60
-
61
- // get customer Id
62
- $customerId = $quote->getCustomerId();
63
-
64
- // if we don't have a customer this event is useless
65
- if (!$customerId) return;
66
 
67
- // get order
68
  $order = $quote->getOrder();
69
 
70
- // There is a slight difference between this event and order add/modify.
71
- // We have to clean up cart items.
72
- Mage::getModel('marketingsoftware/queue')
73
- ->setObject(array('customer' => $customerId))
74
  ->setName('checkout')
75
  ->setAction('add')
76
- ->setEntityId($quote->getId())
77
- ->setCustomer($customerId)
78
  ->save();
79
  }
80
 
81
  /**
82
  * Method for event 'sales_quote_item_delete_before'.
83
  * An item is removed from a quote
84
- * @param Varien_Event_Observer
 
85
  */
86
  public function quoteItemRemoved(Varien_Event_Observer $observer)
87
  {
88
- // if the plug-in is not enabled, skip this
89
- if (!$this->enabled() || !$this->isValidStore()) return;
90
-
91
- // Do we have a valid item?
92
- if (!is_object($item = $observer->getEvent()->getItem())) return;
93
-
94
- // if this item has a parent, it's data is already container within
95
- // parent item
96
- if ($item->getParentItemId()) return;
97
-
98
- // get quote into local scope
99
- $quote = $item->getQuote();
100
-
101
- // get customer Id
102
- $customerId = $quote->getCustomerId();
103
 
104
- // cehck if we have customer
105
- if (!$customerId) return;
106
 
107
- /*
108
- * Since this item is about to be removed from magento system we will have to
109
- * fetch all data right now and pass that data to queue event instance.
110
- */
111
- $item = new Copernica_MarketingSoftware_Model_Copernica_Entity_CartItem($item);
112
- $itemData = $item->getREST()->getCartSubprofileData($quote->getId());
113
- $itemData['status'] = 'deleted';
 
 
 
 
 
 
114
 
115
- // This quote item should be added to the queue
116
- $queue = Mage::getModel('marketingsoftware/queue')
117
- ->setObject(array('quote' => $quote->getId(), 'customer' => $customerId, 'item' => $itemData))
118
- ->setCustomer($customerId)
119
- ->setAction('remove')
 
 
 
 
 
 
 
120
  ->setName('item')
121
- ->setEntityId($item->getId())
 
122
  ->save();
123
  }
124
 
125
  /**
126
  * Method for event 'sales_quote_item_save_after'.
127
  * An item is added or modified
128
- * @param Varien_Event_Observer
 
129
  */
130
  public function quoteItemModified(Varien_Event_Observer $observer)
131
  {
132
- // if the plug-in is not enabled, skip this
133
- if (!$this->enabled() || !$this->isValidStore()) return;
134
-
135
- // Do we have a valid item?
136
- if (!is_object($item = $observer->getEvent()->getItem())) return;
137
-
138
- // unfortunately hasDataChanges is only available in Mage 1.5 and up
139
- if (method_exists($item, 'hasDataChanges') && !$item->hasDataChanges()) {
140
- //an event is triggered every time the object is saved, even when nothing has changed
141
- //for example, when an item is added to the quote
142
- //however, the update date may have changed (even by 1 second) which will trigger a new queue item any way
143
  return;
144
  }
145
-
146
- // if this item has a prent item, this item will also be synced during parent item sync
147
- if ($item->getParentItemId()) return;
148
-
149
- // get quote
150
- $quote = $item->getQuote();
151
-
152
- // get customer Id
153
  $customerId = $quote->getCustomerId();
154
 
155
- /**
156
- * Funny fakt: Magento does not always set customer id on quote instance.
157
- * For example when quote was not finalized. Thus we will check the
158
- * session user to get customer Id.
159
- */
160
- if (!$customerId)
161
- {
162
- // check if there is a user that is logged in
163
- if (!Mage::getSingleton('customer/session')->isLoggedIn()) return;
164
-
165
- // assign new user id
166
  $customerId = Mage::getSingleton('customer/session')->getCustomer()->getId();
167
 
168
- // well, a nasty error that we can not do anything about it
169
- if (!$customerId) return;
 
170
  }
171
-
172
- // This quote item should be added to the queue
173
- $queue = Mage::getModel('marketingsoftware/queue')
174
- ->setObject(array('quote' => $quote->getId(), 'customer' => $customerId))
175
- ->setCustomer($customerId)
176
- ->setAction($item->isObjectNew() ? 'add' : 'modify')
177
- ->setName('item')
178
- ->setEntityId($item->getId())
179
- ->save();
180
  }
181
 
182
  /**
183
  * Listen to when quote is removed.
184
- * @todo review this one
185
- * @param Varien_Event_Observer
 
186
  */
187
  public function quoteDelete(Varien_Event_Observer $observer)
188
  {
189
- // if the extension is not enabled, skip this event
190
- if (!$this->enabled() || !$this->isValidStore()) return;
191
-
192
- // check if we do have a quote instance
193
- if (!is_object($quote = $observer->getEvent()->getQuote())) return;
194
 
195
- // get customer Id
196
  $customerId = $quote->getCustomerId();
197
 
198
- // try to get email address from available data
 
 
 
 
 
 
 
 
 
 
 
199
  $email = $quote->getCustomerEmail();
200
  $email = $email ? $email : $quote->getBillingAddress()->getEmail();
201
  $email = $email ? $email : $quote->getShippingAddress()->getEmail();
202
 
203
- // get objects related to store
204
  $store = $quote->getStore();
205
  $website = $store->getWebsite();
206
  $group = $store->getGroup();
207
 
208
- // construct store view identifier
209
  $storeView = implode(' > ', array ($website->getName(), $group->getName(), $store->getName()));
210
 
211
- /*
212
- * If we don't have an email address it means that we can not pinpoint
213
- * a profile in copernica, so basically we can not do anything useful
214
- * with given quote. Thus, don't even create a queue event for it.
215
- */
216
- if ($email) Mage::getModel('marketingsoftware/queue')
217
- ->setObject(array('customer' => array(
218
- 'id' => $customerId,
219
- 'storeView' => $storeView,
220
- 'email' => $email
221
- )))
222
- ->setCustomer($customerId)
223
- ->setAction('remove')
224
- ->setName('quote')
225
- ->setEntityId($quote->getId())
226
- ->save();
227
  }
228
 
229
  /**
@@ -232,49 +223,67 @@ class Copernica_MarketingSoftware_Model_Observer
232
  */
233
  public function orderModified(Varien_Event_Observer $observer)
234
  {
235
- // if the plug-in is not enabled, skip this
236
- if (!$this->enabled() || !$this->isValidStore()) return;
237
-
238
- // Do we have a valid item?
239
- if (!is_object($order = $observer->getEvent()->getOrder())) return;
240
-
241
- // if an order has no state, it will get one in the next call (usually a few seconds later)
242
- if (!$order->getState()) return;
243
 
244
- // get customer Id
245
  $customerId = $order->getCustomerId();
246
 
247
- // This order should be added to the queue
248
- $queue = Mage::getModel('marketingsoftware/queue')
249
- ->setObject(array('customer' => $customerId))
250
  ->setCustomer($customerId)
251
- ->setAction($order->isObjectNew() ? 'add' : 'modify')
252
  ->setName('order')
 
253
  ->setEntityId($order->getEntityId())
254
  ->save();
255
  }
256
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
257
  /**
258
  * Method for event 'newsletter_subscriber_delete_before'.
259
  * The newsletter subscription is deleted, do something with it,
260
  */
261
  public function newsletterSubscriptionRemoved(Varien_Event_Observer $observer)
262
  {
263
- // if the plug-in is not enabled, skip this
264
- if (!$this->enabled() || !$this->isValidStore()) return;
265
-
266
- // Do we have a valid item?
267
- if (!is_object($subscriber = $observer->getEvent()->getSubscriber())) return;
268
 
269
- // get customer Id
270
  $customerId = $subscriber->getCustomerId();
271
 
272
- // This newsletter subscription should be added to the queue
273
- $queue = Mage::getModel('marketingsoftware/queue')
274
- ->setObject(array('store_id' => $subscriber->getStoreId(), 'email' => $subscriber->getSubscriberEmail()))
275
  ->setCustomer($customerId ? $customerId : null)
276
- ->setAction('remove')
277
  ->setName('subscription')
 
278
  ->setEntityId($subscriber->getId())
279
  ->save();
280
  }
@@ -285,28 +294,22 @@ class Copernica_MarketingSoftware_Model_Observer
285
  */
286
  public function newsletterSubscriptionModified(Varien_Event_Observer $observer)
287
  {
288
- // if the plug-in is not enabled, skip this
289
- if (!$this->enabled() || !$this->isValidStore()) return;
290
-
291
- // Do we have a valid item?
292
- if (!is_object($subscriber = $observer->getEvent()->getSubscriber())) return;
293
-
294
- // unfortunately hasDataChanges is only available in Mage 1.5 and up
295
  if (method_exists($subscriber, 'hasDataChanges') && !$subscriber->hasDataChanges()) {
296
- // an event is triggered every time the object is saved, even when nothing has changed
297
- // for example, when an order is placed
298
  return;
299
  }
300
-
301
- // get customer Id
302
  $customerId = $subscriber->getCustomerId();
303
 
304
- // This newsletter subscription should be added to the queue
305
- $queue = Mage::getModel('marketingsoftware/queue')
306
- ->setObject()
307
  ->setCustomer($customerId)
308
- ->setAction($subscriber->isObjectNew() ? 'add' : 'modify')
309
  ->setName('subscription')
 
310
  ->setEntityId($subscriber->getId())
311
  ->save();
312
  }
@@ -317,25 +320,35 @@ class Copernica_MarketingSoftware_Model_Observer
317
  */
318
  public function customerRemoved(Varien_Event_Observer $observer)
319
  {
320
- // if the plug-in is not enabled, skip this
321
- if (!$this->enabled() || !$this->isValidStore()) return;
322
-
323
- // Do we have a valid item?
324
- if (!is_object($customer = $observer->getEvent()->getCustomer())) return;
325
 
326
- // get customer Id
327
  $customerId = $customer->getId();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
328
 
329
- // get copernica customer
330
- $copernicaCustomer = new Copernica_MarketingSoftware_Model_Copernica_Entity_Customer($customerId);
331
-
332
- // This customer should be added to the queue
333
- $queue = Mage::getModel('marketingsoftware/queue')
334
- ->setObject(array('profileId' => $copernicaCustomer->getProfileId(), 'email' => $customer->getEmail(), 'store_id' => $customer->getStoreId()))
335
  ->setCustomer($customerId)
336
  ->setAction('remove')
337
  ->setName('customer')
338
- ->setEntityId($customerId)
339
  ->save();
340
  }
341
 
@@ -345,25 +358,38 @@ class Copernica_MarketingSoftware_Model_Observer
345
  */
346
  public function customerModified(Varien_Event_Observer $observer)
347
  {
348
- // if the plug-in is not enabled, skip this
349
- if (!$this->enabled() || !$this->isValidStore()) return;
350
-
351
- // Do we have a valid item?
352
- if (!is_object($customer = $observer->getEvent()->getCustomer())) return;
 
 
 
 
 
 
353
 
354
- // get customer Id
355
  $customerId = $customer->getId();
 
 
 
 
 
 
 
 
 
 
 
 
356
 
357
- // just to be sure
358
- if (!$customerId) return;
359
-
360
- // This customer should be added to the queue
361
- $queue = Mage::getModel('marketingsoftware/queue')
362
- ->setObject(null)
363
  ->setCustomer($customerId)
364
  ->setAction($customer->isObjectNew() ? 'add' : 'modify')
365
  ->setName('customer')
366
- ->setEntityId($customerId)
367
  ->save();
368
  }
369
 
@@ -373,26 +399,34 @@ class Copernica_MarketingSoftware_Model_Observer
373
  */
374
  public function productViewed(Varien_Event_Observer $observer)
375
  {
376
- // if the plug-in is not enabled, skip this
377
- if (!$this->enabled() || !$this->isValidStore()) return;
378
-
379
- // Do we have a valid item?
380
- if (!is_object($item = $observer->getEvent()->getProduct())) return;
381
 
382
- // get current customer instance and Id
383
  $customer = Mage::getSingleton('customer/session')->getCustomer();
 
384
  $customerId = $customer->getId();
385
-
386
- // this item cannot be linked to a customer, so is not relevant at this moment
387
- if (!$customerId) return;
388
-
389
- // This quote item should be added to the queue
390
- $queue = Mage::getModel('marketingsoftware/queue')
391
- ->setObject(array('customer' => $customerId))
 
 
 
 
 
 
 
 
392
  ->setCustomer($customerId)
393
  ->setAction('add')
394
  ->setName('view')
395
- ->setEntityId($item->getEntityId())
396
  ->save();
397
  }
398
 
@@ -402,13 +436,11 @@ class Copernica_MarketingSoftware_Model_Observer
402
  */
403
  public function detectAbandonedCarts()
404
  {
405
- // check if extension is enabled
406
- if (!$this->enabled()) return;
407
-
408
- // create abandoned carts processor
409
- $processor = new Copernica_MarketingSoftware_Model_AbandonedCartsProcessor();
410
 
411
- // detect abandoned carts
412
  $processor->detectAbandonedCarts();
413
  }
414
 
@@ -417,9 +449,8 @@ class Copernica_MarketingSoftware_Model_Observer
417
  *
418
  * @return boolean
419
  */
420
- protected function enabled()
421
  {
422
- // get the result from the helper
423
  return Mage::helper('marketingsoftware')->enabled();
424
  }
425
 
@@ -430,13 +461,11 @@ class Copernica_MarketingSoftware_Model_Observer
430
  */
431
  public function processQueue()
432
  {
433
- // if the plug-in is not enabled, skip this
434
- if (!$this->enabled() || !Mage::helper('marketingsoftware/config')->getVanillaCrons()) return;
435
-
436
- // create instance of queue processor
437
- $queueProcessor = Mage::getModel('Copernica_MarketingSoftware_Model_QueueProcessor');
438
 
439
- // process queue
440
  $queueProcessor->processQueue();
441
  }
442
  }
31
  {
32
  /**
33
  * Check if event is added in store that we want to sync.
34
+ *
35
  * @return bool
36
  */
37
+ protected function _isValidStore()
38
  {
 
39
  $currentStoreId = Mage::app()->getStore()->getId();
40
 
 
41
  return Mage::helper('marketingsoftware/config')->isEnabledStore($currentStoreId);
42
  }
43
 
47
  * 'checkout_controller_multishipping_shipping_post'
48
  * This method is fired during checkout process, after the customer has entered billing address
49
  * and saved the shipping method
50
+ *
51
+ * @param Varien_Event_Observer $observer
52
  */
53
  public function checkoutSaveStep(Varien_Event_Observer $observer)
54
  {
55
+ $quote = $observer->getEvent()->getQuote();
56
+
57
+ if (!$this->_enabled() || !$this->_isValidStore() || !is_object($quote)) {
58
+ return;
59
+ }
60
+
61
+ $customerId = $quote->getCustomerId();
 
 
 
 
62
 
63
+ // !!!! Why is this called?? !!!!
64
  $order = $quote->getOrder();
65
 
66
+ Mage::getModel('marketingsoftware/queue_item')
67
+ ->setObject(array('customerId' => $customerId, 'quoteId' => $quote->getId()))
68
+ ->setCustomer($customerId)
 
69
  ->setName('checkout')
70
  ->setAction('add')
71
+ ->setEntityId($quote->getEntityId())
 
72
  ->save();
73
  }
74
 
75
  /**
76
  * Method for event 'sales_quote_item_delete_before'.
77
  * An item is removed from a quote
78
+ *
79
+ * @param Varien_Event_Observer $observer
80
  */
81
  public function quoteItemRemoved(Varien_Event_Observer $observer)
82
  {
83
+ $quoteItem = $observer->getEvent()->getItem();
84
+
85
+ if (!$this->_enabled() || !$this->_isValidStore() || !is_object($quoteItem) || $quoteItem->getParentItemId()) {
86
+ return;
87
+ }
 
 
 
 
 
 
 
 
 
 
88
 
89
+ $quote = $quoteItem->getQuote();
 
90
 
91
+ $customerId = $quote->getCustomerId();
92
+
93
+ if (!$customerId) {
94
+ if (!Mage::getSingleton('customer/session')->isLoggedIn()) {
95
+ return;
96
+ }
97
+
98
+ $customerId = Mage::getSingleton('customer/session')->getCustomer()->getId();
99
+
100
+ if (!$customerId) {
101
+ return;
102
+ }
103
+ }
104
 
105
+ $quoteItemEntity = Mage::getModel('marketingsoftware/copernica_entity_quote_item');
106
+ $quoteItemEntity->setQuoteItem($quoteItem);
107
+
108
+ $quoteItemData = array(
109
+ 'item_id' => $quoteItemEntity->getId(),
110
+ 'storeview_id' => $quoteItemEntity->getStoreView()->id(),
111
+ 'status' => 'deleted',
112
+ );
113
+
114
+ $queue = Mage::getModel('marketingsoftware/queue_item')
115
+ ->setObject(array('quoteId' => $quote->getId(), 'customerId' => $customerId, 'quoteItem' => $quoteItemData))
116
+ ->setCustomer($customerId)
117
  ->setName('item')
118
+ ->setAction('remove')
119
+ ->setEntityId($quoteItem->getId())
120
  ->save();
121
  }
122
 
123
  /**
124
  * Method for event 'sales_quote_item_save_after'.
125
  * An item is added or modified
126
+ *
127
+ * @param Varien_Event_Observer $observer
128
  */
129
  public function quoteItemModified(Varien_Event_Observer $observer)
130
  {
131
+ $quoteItem = $observer->getEvent()->getItem();
132
+
133
+ if (!$this->_enabled() || !$this->_isValidStore() || !is_object($quoteItem) || $quoteItem->getParentItemId()) {
134
+ return;
135
+ }
136
+
137
+ if (method_exists($quoteItem, 'hasDataChanges') && !$quoteItem->hasDataChanges()) {
 
 
 
 
138
  return;
139
  }
140
+
141
+ $quote = $quoteItem->getQuote();
142
+
 
 
 
 
 
143
  $customerId = $quote->getCustomerId();
144
 
145
+ if (!$customerId) {
146
+ if (!Mage::getSingleton('customer/session')->isLoggedIn()) {
147
+ return;
148
+ }
149
+
 
 
 
 
 
 
150
  $customerId = Mage::getSingleton('customer/session')->getCustomer()->getId();
151
 
152
+ if (!$customerId) {
153
+ return;
154
+ }
155
  }
156
+
157
+ $queue = Mage::getModel('marketingsoftware/queue_item')
158
+ ->setObject(array('quoteItemId' => $quoteItem->getId(), 'quoteId' => $quote->getId(), 'customerId' => $customerId))
159
+ ->setCustomer($customerId)
160
+ ->setName('item')
161
+ ->setAction($quoteItem->isObjectNew() ? 'add' : 'modify')
162
+ ->setEntityId($quoteItem->getId())
163
+ ->save();
 
164
  }
165
 
166
  /**
167
  * Listen to when quote is removed.
168
+ *
169
+ * @todo Review this one
170
+ * @param Varien_Event_Observer $observer
171
  */
172
  public function quoteDelete(Varien_Event_Observer $observer)
173
  {
174
+ $quote = $observer->getEvent()->getQuote();
175
+
176
+ if (!$this->_enabled() || !$this->_isValidStore() || !is_object($quote)) {
177
+ return;
178
+ }
179
 
 
180
  $customerId = $quote->getCustomerId();
181
 
182
+ if (!$customerId) {
183
+ if (!Mage::getSingleton('customer/session')->isLoggedIn()) {
184
+ return;
185
+ }
186
+
187
+ $customerId = Mage::getSingleton('customer/session')->getCustomer()->getId();
188
+
189
+ if (!$customerId) {
190
+ return;
191
+ }
192
+ }
193
+
194
  $email = $quote->getCustomerEmail();
195
  $email = $email ? $email : $quote->getBillingAddress()->getEmail();
196
  $email = $email ? $email : $quote->getShippingAddress()->getEmail();
197
 
 
198
  $store = $quote->getStore();
199
  $website = $store->getWebsite();
200
  $group = $store->getGroup();
201
 
 
202
  $storeView = implode(' > ', array ($website->getName(), $group->getName(), $store->getName()));
203
 
204
+ if ($email) {
205
+ Mage::getModel('marketingsoftware/queue_item')
206
+ ->setObject(array(
207
+ 'customerInfo' => array(
208
+ 'id' => $customerId,
209
+ 'storeView' => $storeView,
210
+ 'email' => $email
211
+ ), 'quoteId' => $quote->getId()))
212
+ ->setCustomer($customerId)
213
+ ->setName('quote')
214
+ ->setAction('remove')
215
+ ->setEntityId($quote->getEntityId())
216
+ ->save();
217
+ }
 
 
218
  }
219
 
220
  /**
223
  */
224
  public function orderModified(Varien_Event_Observer $observer)
225
  {
226
+ $order = $observer->getEvent()->getOrder();
227
+
228
+ if (!$this->_enabled() || !$this->_isValidStore() || !is_object($order) || !$order->getState()) {
229
+ return;
230
+ }
 
 
 
231
 
 
232
  $customerId = $order->getCustomerId();
233
 
234
+ $queue = Mage::getModel('marketingsoftware/queue_item')
235
+ ->setObject(array('customerId' => $customerId, 'orderId' => $order->getId()))
 
236
  ->setCustomer($customerId)
 
237
  ->setName('order')
238
+ ->setAction($order->isObjectNew() ? 'add' : 'modify')
239
  ->setEntityId($order->getEntityId())
240
  ->save();
241
  }
242
 
243
+ /**
244
+ * Method for event 'wishlist_save_after'.
245
+ * An wishlist item is added or modified
246
+ */
247
+ public function wishlistItemModified(Varien_Event_Observer $observer)
248
+ {
249
+ $wishlistItem = $observer->getEvent()->getItem();
250
+
251
+ if (!$this->_enabled() || !$this->_isValidStore() || !is_object($wishlistItem)) {
252
+ return;
253
+ }
254
+
255
+ $wishlist = Mage::getModel('wishlist/wishlist')->load($wishlistItem->getWishlistId());
256
+
257
+ $customerId = $wishlist->getCustomerId();
258
+
259
+ $queue = Mage::getModel('marketingsoftware/queue_item')
260
+ ->setObject(array('wishlistItemId' => $wishlistItem->getId(), 'customerId' => $customerId))
261
+ ->setCustomer($customerId)
262
+ ->setName('wishlist_item')
263
+ ->setAction($wishlistItem->isObjectNew() ? 'add' : 'modify')
264
+ ->setEntityId($wishlistItem->getId())
265
+ ->save();
266
+ }
267
+
268
  /**
269
  * Method for event 'newsletter_subscriber_delete_before'.
270
  * The newsletter subscription is deleted, do something with it,
271
  */
272
  public function newsletterSubscriptionRemoved(Varien_Event_Observer $observer)
273
  {
274
+ $subscriber = $observer->getEvent()->getSubscriber();
275
+
276
+ if (!$this->_enabled() || !$this->_isValidStore() || !is_object($subscriber)) {
277
+ return;
278
+ }
279
 
 
280
  $customerId = $subscriber->getCustomerId();
281
 
282
+ $queue = Mage::getModel('marketingsoftware/queue_item')
283
+ ->setObject(array('storeId' => $subscriber->getStoreId(), 'email' => $subscriber->getSubscriberEmail()))
 
284
  ->setCustomer($customerId ? $customerId : null)
 
285
  ->setName('subscription')
286
+ ->setAction('remove')
287
  ->setEntityId($subscriber->getId())
288
  ->save();
289
  }
294
  */
295
  public function newsletterSubscriptionModified(Varien_Event_Observer $observer)
296
  {
297
+ $subscriber = $observer->getEvent()->getSubscriber();
298
+
299
+ if (!$this->_enabled() || !$this->_isValidStore() || !is_object($subscriber)) {
300
+ return;
301
+ }
302
+
 
303
  if (method_exists($subscriber, 'hasDataChanges') && !$subscriber->hasDataChanges()) {
 
 
304
  return;
305
  }
 
 
306
  $customerId = $subscriber->getCustomerId();
307
 
308
+ $queue = Mage::getModel('marketingsoftware/queue_item')
309
+ ->setObject(array('subscriberId' => $subscriber->getId()))
 
310
  ->setCustomer($customerId)
 
311
  ->setName('subscription')
312
+ ->setAction($subscriber->isObjectNew() ? 'add' : 'modify')
313
  ->setEntityId($subscriber->getId())
314
  ->save();
315
  }
320
  */
321
  public function customerRemoved(Varien_Event_Observer $observer)
322
  {
323
+ $customer = $observer->getEvent()->getCustomer();
324
+
325
+ if (!$this->_enabled() || !$this->_isValidStore() || !is_object($customer)) {
326
+ return;
327
+ }
328
 
 
329
  $customerId = $customer->getId();
330
+
331
+ if (!$customerId) {
332
+ if (!Mage::getSingleton('customer/session')->isLoggedIn()) {
333
+ return;
334
+ }
335
+
336
+ $customerId = Mage::getSingleton('customer/session')->getCustomer()->getId();
337
+
338
+ if (!$customerId) {
339
+ return;
340
+ }
341
+ }
342
+
343
+ $customerEntity = Mage::getModel('marketingsoftware/copernica_entity_customer');
344
+ $customerEntity->setCustomer($customerId);
345
 
346
+ $queue = Mage::getModel('marketingsoftware/queue_item')
347
+ ->setObject(array('profileId' => $customerEntity->getProfileId(), 'email' => $customer->getEmail(), 'storeId' => $customer->getStoreId()))
 
 
 
 
348
  ->setCustomer($customerId)
349
  ->setAction('remove')
350
  ->setName('customer')
351
+ ->setEntityId($customer->getEntityId())
352
  ->save();
353
  }
354
 
358
  */
359
  public function customerModified(Varien_Event_Observer $observer)
360
  {
361
+ $address = $observer->getEvent()->getCustomerAddress();
362
+
363
+ if(is_object($address)) {
364
+ $customer = $address->getCustomer();
365
+ } else {
366
+ $customer = $observer->getEvent()->getCustomer();
367
+ }
368
+
369
+ if (!$this->_enabled() || !$this->_isValidStore() || !is_object($customer)) {
370
+ return;
371
+ }
372
 
 
373
  $customerId = $customer->getId();
374
+
375
+ if (!$customerId) {
376
+ if (!Mage::getSingleton('customer/session')->isLoggedIn()) {
377
+ return;
378
+ }
379
+
380
+ $customerId = Mage::getSingleton('customer/session')->getCustomer()->getId();
381
+
382
+ if (!$customerId) {
383
+ return;
384
+ }
385
+ }
386
 
387
+ $queue = Mage::getModel('marketingsoftware/queue_item')
388
+ ->setObject(array('customerId' => $customerId))
 
 
 
 
389
  ->setCustomer($customerId)
390
  ->setAction($customer->isObjectNew() ? 'add' : 'modify')
391
  ->setName('customer')
392
+ ->setEntityId($customer->getEntityId())
393
  ->save();
394
  }
395
 
399
  */
400
  public function productViewed(Varien_Event_Observer $observer)
401
  {
402
+ $product = $observer->getEvent()->getProduct();
403
+
404
+ if (!$this->_enabled() || !$this->_isValidStore() || !is_object($product)) {
405
+ return;
406
+ }
407
 
 
408
  $customer = Mage::getSingleton('customer/session')->getCustomer();
409
+
410
  $customerId = $customer->getId();
411
+
412
+ if (!$customerId) {
413
+ if (!Mage::getSingleton('customer/session')->isLoggedIn()) {
414
+ return;
415
+ }
416
+
417
+ $customerId = Mage::getSingleton('customer/session')->getCustomer()->getId();
418
+
419
+ if (!$customerId) {
420
+ return;
421
+ }
422
+ }
423
+
424
+ $queue = Mage::getModel('marketingsoftware/queue_item')
425
+ ->setObject(array('customerId' => $customerId, 'productId' => $product->getId(), 'viewedAt' => (string) time()))
426
  ->setCustomer($customerId)
427
  ->setAction('add')
428
  ->setName('view')
429
+ ->setEntityId($product->getEntityId())
430
  ->save();
431
  }
432
 
436
  */
437
  public function detectAbandonedCarts()
438
  {
439
+ if (!$this->_enabled()) {
440
+ return;
441
+ }
 
 
442
 
443
+ $processor = Mage::getModel('marketingsoftware/abandoned_carts_processor');
444
  $processor->detectAbandonedCarts();
445
  }
446
 
449
  *
450
  * @return boolean
451
  */
452
+ protected function _enabled()
453
  {
 
454
  return Mage::helper('marketingsoftware')->enabled();
455
  }
456
 
461
  */
462
  public function processQueue()
463
  {
464
+ if (!$this->_enabled() || !Mage::helper('marketingsoftware/config')->getVanillaCrons()) {
465
+ return;
466
+ }
 
 
467
 
468
+ $queueProcessor = Mage::getModel('marketingsoftware/queue_processor');
469
  $queueProcessor->processQueue();
470
  }
471
  }
app/code/community/Copernica/MarketingSoftware/Model/{ProfileCache.php → Profile/Cache.php} RENAMED
@@ -27,18 +27,19 @@
27
  /**
28
  * This class will present and interface to copernica_profile_cache mysql table.
29
  */
30
- class Copernica_MarketingSoftware_Model_ProfileCache extends Mage_Core_Model_Abstract
31
  {
32
  /**
33
  * Construct model
34
  */
35
  protected function _construct()
36
  {
37
- $this->_init('marketingsoftware/profileCache');
38
  }
39
 
40
  /**
41
  * Get the profile Id
 
42
  * @return int
43
  */
44
  public function getProfileId()
@@ -56,8 +57,9 @@ class Copernica_MarketingSoftware_Model_ProfileCache extends Mage_Core_Model_Abs
56
 
57
  /**
58
  * Set profile Id
59
- * @param int
60
- * @return Copernica_MarketingSoftware_Model_ProfileCache
 
61
  */
62
  public function setProfileId($profileId)
63
  {
@@ -68,8 +70,9 @@ class Copernica_MarketingSoftware_Model_ProfileCache extends Mage_Core_Model_Abs
68
 
69
  /**
70
  * Set customer Id
71
- * @param string
72
- * @return Copernica_MarketingSoftware_Model_ProfileCache
 
73
  */
74
  public function setCustomerId($customerId)
75
  {
27
  /**
28
  * This class will present and interface to copernica_profile_cache mysql table.
29
  */
30
+ class Copernica_MarketingSoftware_Model_Profile_Cache extends Mage_Core_Model_Abstract
31
  {
32
  /**
33
  * Construct model
34
  */
35
  protected function _construct()
36
  {
37
+ $this->_init('marketingsoftware/profile_cache');
38
  }
39
 
40
  /**
41
  * Get the profile Id
42
+ *
43
  * @return int
44
  */
45
  public function getProfileId()
57
 
58
  /**
59
  * Set profile Id
60
+ *
61
+ * @param int $profileId
62
+ * @return Copernica_MarketingSoftware_Model_Profile_Cache
63
  */
64
  public function setProfileId($profileId)
65
  {
70
 
71
  /**
72
  * Set customer Id
73
+ *
74
+ * @param string $customerId
75
+ * @return Copernica_MarketingSoftware_Model_Profile_Cache
76
  */
77
  public function setCustomerId($customerId)
78
  {
app/code/community/Copernica/MarketingSoftware/Model/Queue/Event/Abstract.php ADDED
@@ -0,0 +1,109 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Copernica Marketing Software
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0).
8
+ * It is available through the world-wide-web at this URL:
9
+ * http://opensource.org/licenses/osl-3.0.php
10
+ * If you are unable to obtain a copy of the license through the
11
+ * world-wide-web, please send an email to copernica@support.cream.nl
12
+ * so we can send you a copy immediately.
13
+ *
14
+ * DISCLAIMER
15
+ *
16
+ * Do not edit or add to this file if you wish to upgrade this software
17
+ * to newer versions in the future. If you wish to customize this module
18
+ * for your needs please refer to http://www.magento.com/ for more
19
+ * information.
20
+ *
21
+ * @category Copernica
22
+ * @package Copernica_MarketingSoftware
23
+ * @copyright Copyright (c) 2011-2012 Copernica & Cream. (http://docs.cream.nl/)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ /**
28
+ * A wrapper object around an event
29
+ */
30
+ abstract class Copernica_MarketingSoftware_Model_Queue_Event_Abstract
31
+ {
32
+ /**
33
+ * What queue item was used to construct this item
34
+ *
35
+ * @var Copernica_MarketingSoftware_Model_Queue_Item
36
+ */
37
+ protected $_queueItem;
38
+
39
+ /**
40
+ * Construct the item given the queueitem
41
+ *
42
+ * @param Copernica_MarketingSoftware_Model_Queue_Item $item
43
+ */
44
+ public function setQueueItem(Copernica_MarketingSoftware_Model_Queue_Item $item)
45
+ {
46
+ $this->_queueItem = $item;
47
+ }
48
+
49
+ /**
50
+ * Get the object for this queue item
51
+ *
52
+ * @todo Not used?
53
+ * @return Abstraction object
54
+ */
55
+ protected function _getObject()
56
+ {
57
+ return $this->_queueItem->getObject();
58
+ }
59
+
60
+ /**
61
+ * Get the entity Id
62
+ *
63
+ * @todo Not used?
64
+ * @return int
65
+ */
66
+ protected function _getEntityId()
67
+ {
68
+ return $this->_queueItem->getEntityId();
69
+ }
70
+
71
+ /**
72
+ * Get the customer Id
73
+ *
74
+ * @todo Not used?
75
+ * @return int
76
+ */
77
+ protected function _getCustomerId()
78
+ {
79
+ return $this->_queueItem->getCustomerId();
80
+ }
81
+
82
+ /**
83
+ * Process this item in the queue
84
+ *
85
+ * @return boolean was the processing successfull
86
+ */
87
+ public function process()
88
+ {
89
+ $methodName = 'action'.ucfirst($this->_queueItem->getAction());
90
+
91
+ if (!method_exists($this, $methodName)) {
92
+ return false;
93
+ }
94
+
95
+ return $this->$methodName();
96
+ }
97
+
98
+ /**
99
+ * Respawn event on the queue.
100
+ */
101
+ public function respawn()
102
+ {
103
+ Mage::getModel('marketingsoftware/queue_item')
104
+ ->setObject($this->_queueItem->getObject())
105
+ ->setCustomer($this->_queueItem->getCustomer())
106
+ ->setAction($this->_queueItem->getAction())
107
+ ->save();
108
+ }
109
+ }
app/code/community/Copernica/MarketingSoftware/Model/Queue/Event/Checkout.php ADDED
@@ -0,0 +1,114 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Copernica Marketing Software
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0).
8
+ * It is available through the world-wide-web at this URL:
9
+ * http://opensource.org/licenses/osl-3.0.php
10
+ * If you are unable to obtain a copy of the license through the
11
+ * world-wide-web, please send an email to copernica@support.cream.nl
12
+ * so we can send you a copy immediately.
13
+ *
14
+ * DISCLAIMER
15
+ *
16
+ * Do not edit or add to this file if you wish to upgrade this software
17
+ * to newer versions in the future. If you wish to customize this module
18
+ * for your needs please refer to http://www.magento.com/ for more
19
+ * information.
20
+ *
21
+ * @category Copernica
22
+ * @package Copernica_MarketingSoftware
23
+ * @copyright Copyright (c) 2011-2012 Copernica & Cream. (http://docs.cream.nl/)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ /**
28
+ * This class should take care of all events related to checkout.
29
+ *
30
+ * This class does not matter. It should be not used cause it's same as
31
+ * order modify action.
32
+ */
33
+ class Copernica_MarketingSoftware_Model_Queue_Event_Checkout extends Copernica_MarketingSoftware_Model_Queue_Event_Abstract
34
+ {
35
+ /**
36
+ * Customer entity
37
+ *
38
+ * @var Copernica_MarketingSoftware_Model_Copernica_Entity_Customer
39
+ */
40
+ protected $_customerEntity;
41
+
42
+ /**
43
+ * Process add action
44
+ *
45
+ * @return boolean
46
+ */
47
+ public function actionAdd()
48
+ {
49
+ return $this->actionModify();
50
+ }
51
+
52
+ /**
53
+ * Modify action on checkout event
54
+ *
55
+ * @return boolean
56
+ */
57
+ public function actionModify()
58
+ {
59
+ $customerEntity = $this->_getCustomerEntity();
60
+
61
+ if (!$customerEntity) {
62
+ return false;
63
+ }
64
+
65
+ $quote = Mage::getModel('sales/quote')->loadByIdWithoutStore($this->_getEntityId());
66
+
67
+ $request = Mage::helper('marketingsoftware/rest_request');
68
+ $request->prepare();
69
+
70
+ if (Mage::helper('marketingsoftware/config')->getRemoveFinishedQuoteItem()) {
71
+ $quoteItemCollection = Mage::helper('marketingsoftware/config')->getQuoteItemCollectionId();
72
+
73
+ if ($quoteItemCollection) {
74
+ $response = $request->get('/profile/'.$customerEntity->getProfileId().'/subprofiles/'.$quoteItemCollection, array(
75
+ 'fields' => array('quote_id=='.$this->_getEntityId())
76
+ ));
77
+ }
78
+
79
+ foreach ($response['data'] as $subprofile) {
80
+ $request->delete('/subprofile/'.$subprofile['ID']);
81
+ }
82
+ } else {
83
+ foreach ($quote->getAllVisibleItems() as $quoteItem) {
84
+ $quoteItemEntity = Mage::getModel('marketingsoftware/copernica_entity_quote_item');
85
+ $quoteItemEntity->setQuoteItem($quoteItem);
86
+
87
+ $restQuoteItem = $quoteItemEntity->getRestQuoteItem();
88
+ $restQuoteItem->syncWithQuote($customerEntity, $quote->getId());
89
+ }
90
+ }
91
+
92
+ $request->commit();
93
+
94
+ return true;
95
+ }
96
+
97
+ protected function _getCustomerEntity()
98
+ {
99
+ if ($this->_customerEntity) {
100
+ return $this->_customerEntity;
101
+ } else {
102
+ $object = $this->_getObject();
103
+
104
+ if (property_exists($object, 'customerId') && is_numeric($object->customerId)) {
105
+ $customerEntity = Mage::getModel('marketingsoftware/copernica_entity_customer');
106
+ $customerEntity->setCustomer($object->customerId);
107
+
108
+ return $this->_customerEntity = $customerEntity;
109
+ } else {
110
+ return false;
111
+ }
112
+ }
113
+ }
114
+ }
app/code/community/Copernica/MarketingSoftware/Model/{QueueEvent → Queue/Event}/Customer.php RENAMED
@@ -30,11 +30,12 @@
30
  * can sync almost all customer data in one run via ::actionFull(). Full action
31
  * should be used only when there is need to that.
32
  */
33
- class Copernica_MarketingSoftware_Model_QueueEvent_Customer extends Copernica_MarketingSoftware_Model_QueueEvent_Abstract
34
  {
35
  /**
36
  * Handle customer add action. It's basicaly the same as modify action.
37
- * @return boolean
 
38
  */
39
  public function actionAdd()
40
  {
@@ -46,29 +47,26 @@ class Copernica_MarketingSoftware_Model_QueueEvent_Customer extends Copernica_Ma
46
  * when customer is modified. So on administrative edition or when customer
47
  * himself is modifying his data.
48
  *
49
- * @return boolean
50
  */
51
  public function actionModify()
52
  {
53
- // get customer entity
54
- $customer = new Copernica_MarketingSoftware_Model_Copernica_Entity_Customer($this->getEntityId());
55
 
56
- // set customer inside copernica platform
57
- $customer->getREST()->set();
58
 
59
- // get REST request into local scope
60
- $request = Mage::helper('marketingsoftware/RESTRequest');
61
 
62
- // prepare multi interface
63
- $request->prepare();
 
 
64
 
65
- // sync all customer addresses
66
- foreach ($customer->getAddresses() as $address) $address->getREST()->bindToCustomer($customer);
67
 
68
- // commit multi interface
69
- $request->commit();
70
-
71
- // we are just fine
72
  return true;
73
  }
74
 
@@ -80,32 +78,36 @@ class Copernica_MarketingSoftware_Model_QueueEvent_Customer extends Copernica_Ma
80
  * This action is expensive both with time and resources. It should be called
81
  * only when there is a need for that.
82
  *
83
- * @return boolean
84
  */
85
  public function actionFull()
86
  {
87
- // get customer entity
88
- $customer = new Copernica_MarketingSoftware_Model_Copernica_Entity_Customer($this->getEntityId());
89
 
90
- // set customer inside copernica platform
91
- $customer->getREST()->set();
92
 
93
- // get REST request into local scope
94
- $request = Mage::helper('marketingsoftware/RESTRequest');
95
 
96
- // prepare request for multi interface
97
- $request->prepare();
98
-
99
- // sync all addresses
100
- foreach ($customer->getAddresses() as $address) $address->getREST()->bindToCustomer($customer);
101
 
102
- // sync add orders
103
- foreach ($customer->getOrders() as $order) $order->getREST()->syncWithCustomer($customer);
104
-
105
- // commit all changes to copernica
106
- $request->commit();
 
 
 
 
 
 
107
 
108
- // we should be just fine
109
  return true;
110
  }
111
 
@@ -117,45 +119,40 @@ class Copernica_MarketingSoftware_Model_QueueEvent_Customer extends Copernica_Ma
117
  */
118
  public function actionRemove()
119
  {
120
- // get data object to local scope
121
  $object = $this->getObject();
122
 
123
- // check if we have email and store view data
124
- if (property_exists($object, 'email') && property_exists($object, 'store_id'))
125
- {
126
- // get objects related to store
127
- $store = Mage::getModel('core/store')->load($object->store_id);
128
  $website = $store->getWebsite();
 
129
  $group = $store->getGroup();
130
 
131
- // construct store view identifier
132
  $storeView = implode(' > ', array (
133
  $website->getName(),
134
  $group->getName(),
135
  $store->getName())
136
  );
137
 
138
- // try to get a profile cache by email+store_view combination
139
- $profileCacheCollection = Mage::getModel('marketingsoftware/profileCache')
140
  ->getCollection()
141
  ->setPageSize(1)
142
  ->addFieldToFilter('email', $object->email)
143
  ->addFieldToFilter('store_view', $storeView);
144
-
145
- // get 1st item
146
  $profileCache = $profileCacheCollection->getFirstItem();
147
 
148
- // we don't need profile cache for customer that we just removed
149
- if (!$profileCache->isObjectNew()) $profileCache->delete();
 
150
  }
151
 
152
- // if we don't have proper property we can not do anything
153
- if (!property_exists($object, 'profileId')) return false;
 
154
 
155
- // just ask copernica api to remove target profile
156
- Mage::helper('marketingsoftware/RESTRequest')->delete('/profile/'.$object->profileId);
157
 
158
- // we should be just fine here
159
  return true;
160
  }
161
  }
30
  * can sync almost all customer data in one run via ::actionFull(). Full action
31
  * should be used only when there is need to that.
32
  */
33
+ class Copernica_MarketingSoftware_Model_Queue_Event_Customer extends Copernica_MarketingSoftware_Model_Queue_Event_Abstract
34
  {
35
  /**
36
  * Handle customer add action. It's basicaly the same as modify action.
37
+ *
38
+ * @return boolean
39
  */
40
  public function actionAdd()
41
  {
47
  * when customer is modified. So on administrative edition or when customer
48
  * himself is modifying his data.
49
  *
50
+ * @return boolean
51
  */
52
  public function actionModify()
53
  {
54
+ $customerEntity = Mage::getModel('marketingsoftware/copernica_entity_customer');
55
+ $customerEntity->setCustomer($this->_getEntityId());
56
 
57
+ $restCustomer = $customerEntity->getRestCustomer();
58
+ $restCustomer->setProfile();
59
 
60
+ // $request = Mage::helper('marketingsoftware/rest_request');
61
+ // $request->prepare();
62
 
63
+ // foreach ($customerEntity->getAddresses() as $addressEntity) {
64
+ // $restAddress = $addressEntity->getRestAddress();
65
+ // $restAddress->bindToCustomer($customerEntity);
66
+ // }
67
 
68
+ // $request->commit();
 
69
 
 
 
 
 
70
  return true;
71
  }
72
 
78
  * This action is expensive both with time and resources. It should be called
79
  * only when there is a need for that.
80
  *
81
+ * @return boolean
82
  */
83
  public function actionFull()
84
  {
85
+ $customerEntity = Mage::getModel('marketingsoftware/copernica_entity_customer');
86
+ $customerEntity->setCustomer($this->_getEntityId());
87
 
88
+ $restCustomer = $customerEntity->getRestCustomer();
89
+ $restCustomer->setProfile();
90
 
91
+ //$request = Mage::helper('marketingsoftware/rest_request');
92
+ //$request->prepare();
93
 
94
+ // foreach ($customerEntity->getAddresses() as $addressEntity) {
95
+ // $restAddress = $addressEntity->getRestAddress();
96
+ // $restAddress->bindToCustomer($customerEntity);
97
+ // }
 
98
 
99
+ foreach ($customerEntity->getOrders() as $orderEntity) {
100
+ $restOrder = $orderEntity->getRestOrder();
101
+ $restOrder->syncWithCustomer($customerEntity);
102
+ }
103
+
104
+ foreach($customerEntity->getWishlistItems() as $wishlistItemEntity) {
105
+ $restWishlistItem = $wishlistItemEntity->getRestWishlistItem();
106
+ $restWishlistItem->syncWithCustomer($customerEntity);
107
+ }
108
+
109
+ //$request->commit();
110
 
 
111
  return true;
112
  }
113
 
119
  */
120
  public function actionRemove()
121
  {
 
122
  $object = $this->getObject();
123
 
124
+ if (property_exists($object, 'email') && property_exists($object, 'storeId')) {
125
+ $store = Mage::getModel('core/store')->load($object->storeId);
126
+
 
 
127
  $website = $store->getWebsite();
128
+
129
  $group = $store->getGroup();
130
 
 
131
  $storeView = implode(' > ', array (
132
  $website->getName(),
133
  $group->getName(),
134
  $store->getName())
135
  );
136
 
137
+ $profileCacheCollection = Mage::getModel('marketingsoftware/profile_cache')
 
138
  ->getCollection()
139
  ->setPageSize(1)
140
  ->addFieldToFilter('email', $object->email)
141
  ->addFieldToFilter('store_view', $storeView);
142
+
 
143
  $profileCache = $profileCacheCollection->getFirstItem();
144
 
145
+ if (!$profileCache->isObjectNew()) {
146
+ $profileCache->delete();
147
+ }
148
  }
149
 
150
+ if (!property_exists($object, 'profileId')) {
151
+ return false;
152
+ }
153
 
154
+ Mage::helper('marketingsoftware/rest_request')->delete('/profile/'.$object->profileId);
 
155
 
 
156
  return true;
157
  }
158
  }
app/code/community/Copernica/MarketingSoftware/Model/{QueueEvent → Queue/Event}/Factory.php RENAMED
@@ -27,69 +27,70 @@
27
  /**
28
  * Factory class for getting queue event objects
29
  */
30
- class Copernica_MarketingSoftware_Model_QueueEvent_Factory
31
  {
32
  /**
33
  * Get the right object
34
  *
35
- * @return Copernica_MarketingSoftware_Model_QueueEvent_Abstract
36
  */
37
  public function get($queueItem)
38
  {
39
- // If we want to start a full synchronisation, we should return a start sync object
40
- if ($queueItem->getAction() == 'start_sync')
41
- {
42
- $classname = Mage::getConfig()->getModelClassName('marketingsoftware/QueueEvent_StartSync');
43
  return new $classname($queueItem);
44
  }
45
 
46
- // if we want to upgrade customer, we should return proper event
47
- if ($queueItem->getAction() == 'upgrade')
48
- {
49
- $classname = Mage::getConfig()->getModelClassName('marketingsoftware/QueueEvent_CustomerUpgrade');
50
  return new $classname($queueItem);
51
  }
52
 
53
- // Prepare the action, to append it to the classname
54
  $action = ucfirst($queueItem->getAction());
55
 
56
- // What kind of class is given
57
  switch (get_class($queueItem->getObject()))
58
  {
59
  case "Copernica_MarketingSoftware_Model_Abstraction_Quote":
60
- $classname = "marketingsoftware/QueueEvent_Quote".$action;
61
  break;
62
 
63
- case "Copernica_MarketingSoftware_Model_Abstraction_Quote_Item":
64
- $classname = "marketingsoftware/QueueEvent_QuoteItem".$action;
65
  break;
66
 
67
  case "Copernica_MarketingSoftware_Model_Abstraction_Customer":
68
- $classname = "marketingsoftware/QueueEvent_Customer".$action;
69
  break;
70
 
71
  case "Copernica_MarketingSoftware_Model_Abstraction_Order":
72
- $classname = "marketingsoftware/QueueEvent_Order".$action;
73
  break;
74
 
75
  case "Copernica_MarketingSoftware_Model_Abstraction_Subscription":
76
- $classname = "marketingsoftware/QueueEvent_Subscription".$action;
77
  break;
78
 
79
- case "Copernica_MarketingSoftware_Model_Abstraction_Viewedproduct":
80
- $classname = "marketingsoftware/QueueEvent_ViewedProduct".$action;
81
  break;
 
 
 
 
82
  }
83
 
84
- if (!isset($classname))
85
  throw Mage::exception('Copernica_MarketingSoftware', 'Event type does not exists: '.$classname, Copernica_MarketingSoftware_Exception::EVENT_NO_TYPE);
86
-
87
- // Get correct classname
88
  $classname = Mage::getConfig()->getModelClassName($classname);
89
- if (!class_exists($classname))
 
90
  throw Mage::exception('Copernica_MarketingSoftware', 'Event type does not exists: '.$classname, Copernica_MarketingSoftware_Exception::EVENT_TYPE_NOT_EXISTS);
91
-
92
- // construct the object
93
  return new $classname($queueItem);
94
  }
95
  }
27
  /**
28
  * Factory class for getting queue event objects
29
  */
30
+ class Copernica_MarketingSoftware_Model_Queue_Event_Factory
31
  {
32
  /**
33
  * Get the right object
34
  *
35
+ * @return Copernica_MarketingSoftware_Model_Abstraction_Quote|Copernica_MarketingSoftware_Model_Abstraction_Quote_item|Copernica_MarketingSoftware_Model_Abstraction_Customer|Copernica_MarketingSoftware_Model_Abstraction_Order|Copernica_MarketingSoftware_Model_Abstraction_Subscription|Copernica_MarketingSoftware_Model_Abstraction_Product_Viewed
36
  */
37
  public function get($queueItem)
38
  {
39
+ if ($queueItem->getAction() == 'start_sync') {
40
+ $classname = Mage::getConfig()->getModelClassName('marketingsoftware/queue_event_startsync');
41
+
 
42
  return new $classname($queueItem);
43
  }
44
 
45
+ if ($queueItem->getAction() == 'upgrade') {
46
+ $classname = Mage::getConfig()->getModelClassName('marketingsoftware/queue_event_customer_upgrade');
47
+
 
48
  return new $classname($queueItem);
49
  }
50
 
 
51
  $action = ucfirst($queueItem->getAction());
52
 
 
53
  switch (get_class($queueItem->getObject()))
54
  {
55
  case "Copernica_MarketingSoftware_Model_Abstraction_Quote":
56
+ $classname = "marketingsoftware/queue_event_quote".$action;
57
  break;
58
 
59
+ case "Copernica_MarketingSoftware_Model_Abstraction_Quote_item":
60
+ $classname = "marketingsoftware/queue_event_quote_item".$action;
61
  break;
62
 
63
  case "Copernica_MarketingSoftware_Model_Abstraction_Customer":
64
+ $classname = "marketingsoftware/queue_event_customer".$action;
65
  break;
66
 
67
  case "Copernica_MarketingSoftware_Model_Abstraction_Order":
68
+ $classname = "marketingsoftware/queue_event_order".$action;
69
  break;
70
 
71
  case "Copernica_MarketingSoftware_Model_Abstraction_Subscription":
72
+ $classname = "marketingsoftware/queue_event_subscription".$action;
73
  break;
74
 
75
+ case "Copernica_MarketingSoftware_Model_Abstraction_Product_Viewed":
76
+ $classname = "marketingsoftware/queue_event_product_viewed".$action;
77
  break;
78
+
79
+ case "Copernica_MarketingSoftware_Model_Abstraction_Wishlist":
80
+ $classname = "marketingsoftware/queue_event_wishlist".$action;
81
+ break;
82
  }
83
 
84
+ if (!isset($classname)) {
85
  throw Mage::exception('Copernica_MarketingSoftware', 'Event type does not exists: '.$classname, Copernica_MarketingSoftware_Exception::EVENT_NO_TYPE);
86
+ }
87
+
88
  $classname = Mage::getConfig()->getModelClassName($classname);
89
+
90
+ if (!class_exists($classname)) {
91
  throw Mage::exception('Copernica_MarketingSoftware', 'Event type does not exists: '.$classname, Copernica_MarketingSoftware_Exception::EVENT_TYPE_NOT_EXISTS);
92
+ }
93
+
94
  return new $classname($queueItem);
95
  }
96
  }
app/code/community/Copernica/MarketingSoftware/Model/Queue/Event/Item.php ADDED
@@ -0,0 +1,176 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Copernica Marketing Software
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0).
8
+ * It is available through the world-wide-web at this URL:
9
+ * http://opensource.org/licenses/osl-3.0.php
10
+ * If you are unable to obtain a copy of the license through the
11
+ * world-wide-web, please send an email to copernica@support.cream.nl
12
+ * so we can send you a copy immediately.
13
+ *
14
+ * DISCLAIMER
15
+ *
16
+ * Do not edit or add to this file if you wish to upgrade this software
17
+ * to newer versions in the future. If you wish to customize this module
18
+ * for your needs please refer to http://www.magento.com/ for more
19
+ * information.
20
+ *
21
+ * @category Copernica
22
+ * @package Copernica_MarketingSoftware
23
+ * @copyright Copyright (c) 2011-2012 Copernica & Cream. (http://docs.cream.nl/)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ /**
28
+ * This class will take care of syncing items
29
+ */
30
+ class Copernica_MarketingSoftware_Model_Queue_Event_Item extends Copernica_MarketingSoftware_Model_Queue_Event_Abstract
31
+ {
32
+ /**
33
+ * Process modify action
34
+ *
35
+ * @return boolean
36
+ */
37
+ public function actionModify()
38
+ {
39
+ $object = $this->_getObject();
40
+
41
+ if(!$object->quoteId || !is_numeric($object->quoteId)) {
42
+ return false;
43
+ }
44
+
45
+ if ($object->customerId && is_numeric($object->customerId) ) {
46
+ $this->syncQuote();
47
+ }
48
+
49
+ $collection = Mage::getModel('marketingsoftware/abandoned_cart')->getCollection()->addFieldToFilter('quote_id', $object->quoteId);
50
+
51
+ $abandonedCart = $collection->getFirstItem();
52
+
53
+ if ($abandonedCart->isObjectNew()) {
54
+ return true;
55
+ }
56
+
57
+ $abandonedCart->delete();
58
+
59
+ $queue = Mage::getModel('marketingsoftware/queue_item')
60
+ ->setObject(array('quoteId' => $object->quoteId))
61
+ ->setCustomer($object->customerId)
62
+ ->setAction('modify')
63
+ ->setName('quote')
64
+ ->setEntityId($object->quoteId)
65
+ ->save();
66
+
67
+ return true;
68
+ }
69
+
70
+ /**
71
+ * Process add action.
72
+ *
73
+ * @return boolean
74
+ */
75
+ public function actionAdd()
76
+ {
77
+ return $this->actionModify();
78
+ }
79
+
80
+ /**
81
+ * Process remove action
82
+ *
83
+ * @return boolean
84
+ */
85
+ public function actionRemove()
86
+ {
87
+ $object = $this->_getObject();
88
+
89
+ if (!$object->quoteId || !is_numeric($object->quoteId) || !$object->customerId || !is_numeric($object->customerId) || !$object->quoteItem) {
90
+ return false;
91
+ }
92
+
93
+ $quoteItemData = get_object_vars($object->quoteItem);
94
+
95
+ $request = Mage::helper('marketingsoftware/rest_request');
96
+
97
+ $itemId = $quoteItemData['item_id'];
98
+
99
+ $quoteId = $object->quoteId;
100
+
101
+ $store = Mage::getModel('core/store')->load($quoteItemData['storeview_id']);
102
+
103
+ $storeview = Mage::getModel('marketingsoftware/abstraction_storeview')->setOriginal($store);
104
+
105
+ $customerEntity = Mage::getModel('marketingsoftware/copernica_entity_customer');
106
+ $customerEntity->setCustomer($object->customerId);
107
+ $customerEntity->setStore($storeview);
108
+
109
+ if (($profileId = $customerEntity->getProfileId()) === false) {
110
+ return false;
111
+ }
112
+
113
+ $quoteItemCollectionId = Mage::helper('marketingsoftware/config')->getQuoteItemCollectionId();
114
+
115
+
116
+ $response = $request->get('/profile/'.$profileId.'/subprofiles/'.$quoteItemCollectionId, array(
117
+ 'item_id' => $itemId,
118
+ 'quote_id' => $quoteId,
119
+ ));
120
+
121
+ if (!array_key_exists('data', $response) || count($response['data']) == 0) {
122
+ if (!Mage::helper('marketingsoftware/config')->getRemoveFinishedQuoteItem()) {
123
+ $request->post('/profile/'.$profileId.'/subprofiles/'.$quoteItemCollectionId, $quoteItemData);
124
+ }
125
+
126
+ return true;
127
+ }
128
+
129
+ if (Mage::helper('marketingsoftware/config')->getRemoveFinishedQuoteItem()) {
130
+ foreach ($response['data'] as $subprofile) {
131
+ $request->delete('/subprofile/'.$subprofile['ID']);
132
+ }
133
+ } else {
134
+ $request->put('/profile/'.$profileId.'/subprofiles/'.$quoteItemCollectionId, array(
135
+ 'status' => 'deleted'
136
+ ), array (
137
+ 'fields' => array(
138
+ 'quote_id=='.$quoteId,
139
+ 'item_id=='.$itemId
140
+ )
141
+ ));
142
+ }
143
+
144
+ return true;
145
+ }
146
+
147
+ /**
148
+ * Sync item with quote
149
+ */
150
+ public function syncQuote()
151
+ {
152
+ $object = $this->_getObject();
153
+
154
+ if (!$object->customerId || !is_numeric($object->customerId)) {
155
+ return false;
156
+ }
157
+
158
+ if($object->quoteId && is_numeric($object->quoteId)) {
159
+ $quoteId = $object->quoteId;
160
+ } else {
161
+ $quote = Mage::getModel('sales/quote')->loadByCustomer($object->customerId);
162
+ $quoteId = $quote->getId();
163
+ }
164
+
165
+ $quoteItem = Mage::getModel('sales/quote_item')->load($object->quoteItemId);
166
+
167
+ $customerEntity = Mage::getModel('marketingsoftware/copernica_entity_customer');
168
+ $customerEntity->setCustomer($object->customerId);
169
+
170
+ $quoteItemEntity = Mage::getModel('marketingsoftware/copernica_entity_quote_item');
171
+ $quoteItemEntity->setQuoteItem($quoteItem);
172
+
173
+ $restQuoteItem = $quoteItemEntity->getRestQuoteItem();
174
+ $restQuoteItem->syncWithQuote($customerEntity, $quoteId);
175
+ }
176
+ }
app/code/community/Copernica/MarketingSoftware/Model/Queue/Event/Order.php ADDED
@@ -0,0 +1,159 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Copernica Marketing Software
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0).
8
+ * It is available through the world-wide-web at this URL:
9
+ * http://opensource.org/licenses/osl-3.0.php
10
+ * If you are unable to obtain a copy of the license through the
11
+ * world-wide-web, please send an email to copernica@support.cream.nl
12
+ * so we can send you a copy immediately.
13
+ *
14
+ * DISCLAIMER
15
+ *
16
+ * Do not edit or add to this file if you wish to upgrade this software
17
+ * to newer versions in the future. If you wish to customize this module
18
+ * for your needs please refer to http://www.magento.com/ for more
19
+ * information.
20
+ *
21
+ * @category Copernica
22
+ * @package Copernica_MarketingSoftware
23
+ * @copyright Copyright (c) 2011-2012 Copernica & Cream. (http://docs.cream.nl/)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ /**
28
+ * This clas will take care of all events associated with order
29
+ */
30
+ class Copernica_MarketingSoftware_Model_Queue_Event_Order extends Copernica_MarketingSoftware_Model_Queue_Event_Abstract
31
+ {
32
+ /**
33
+ * Customer entity
34
+ *
35
+ * @var Copernica_MarketingSoftware_Model_Copernica_Entity_Customer
36
+ */
37
+ protected $_customerEntity;
38
+
39
+ /**
40
+ * This action will be run on order modify event
41
+ *
42
+ * @return boolean
43
+ */
44
+ public function actionModify()
45
+ {
46
+ $vanillaOrder = Mage::getModel('sales/order')->load($this->_getEntityId());
47
+
48
+ $orderEntity = Mage::getModel('marketingsoftware/copernica_entity_order');
49
+ $orderEntity->setOrder($vanillaOrder);
50
+
51
+ $restOrder = $orderEntity->getRestOrder();
52
+
53
+ $customerEntity = $this->_getCustomerEntity();
54
+
55
+ if ($customerEntity) {
56
+ return $restOrder->syncWithCustomer($customerEntity);
57
+ } else {
58
+ $data = array (
59
+ 'email' => $vanillaOrder->getCustomerEmail(),
60
+ 'storeView' => (string) $orderEntity->getStoreView(),
61
+ 'storeViewId' => $vanillaOrder->getStoreId(),
62
+ 'firstname' => $vanillaOrder->getCustomerFirstname(),
63
+ 'lastname' => $vanillaOrder->getCustomerLastname(),
64
+ );
65
+
66
+ if ($middlename = $vanillaOrder->getCustomerMiddlename()) {
67
+ $data['middlename'] = $middlename;
68
+ }
69
+
70
+ if ($dateOfBirth = $vanillaOrder->getCustomerDob()) {
71
+ $data['birthdate'] = date('Y-m-d H:i:s', strtotime($dateOfBirth));
72
+ }
73
+
74
+ if ($gender = $vanillaOrder->getCustomerGender()) {
75
+ $options = Mage::getModel('customer/customer')->getAttribute('gender')->getSource()->getAllOptions();
76
+
77
+ foreach ($options as $option) {
78
+ if ($option['value'] == $gender) {
79
+ $gender = $option['label'];
80
+ }
81
+ }
82
+
83
+ $data['gender'] = $gender;
84
+ }
85
+
86
+ $group = $vanillaOrder->getCustomerGroupId();
87
+ $group = Mage::getModel('customer/group')->load($group)->getCode();
88
+ $data['group'] = $group;
89
+
90
+ return $restOrder->syncWithGuest($data);
91
+ }
92
+ }
93
+
94
+ /**
95
+ * This action will be run on order add event
96
+ *
97
+ * @return boolean
98
+ */
99
+ public function actionAdd()
100
+ {
101
+ $this->actionModify();
102
+
103
+ $order = Mage::getModel('sales/order')->load($this->_getEntityId());
104
+
105
+ $customerEntity = $this->_getCustomerEntity();
106
+
107
+ if(!$customerEntity) {
108
+ return true;
109
+ }
110
+
111
+ $request = Mage::helper('marketingsoftware/rest_request');
112
+
113
+ $quoteItemCollection = Mage::helper('marketingsoftware/config')->getQuoteItemCollectionId();
114
+
115
+ if ($quoteItemCollection) {
116
+ $response = $request->get('/profile/'.$customerEntity->getProfileId().'/subprofiles/'.$quoteItemCollection, array(
117
+ 'fields' => array('quote_id=='.$order->getQuoteId())
118
+ ));
119
+ }
120
+
121
+ if (array_key_exists('data', $response) || count($response['data']) == 0) {
122
+ return true;
123
+ }
124
+
125
+ $request->prepare();
126
+
127
+ if (Mage::helper('marketingsoftware/config')->getRemoveFinishedQuoteItem()) {
128
+ foreach ($response['data'] as $subprofile) {
129
+ $request->delete('/subprofile/'.$subprofile['ID']);
130
+ }
131
+ } else {
132
+ foreach ($response['data'] as $subprofile) {
133
+ $request->put('/subprofile/'.$subprofile['ID'].'/fields/', array('status' => 'completed'));
134
+ }
135
+ }
136
+
137
+ $request->commit();
138
+
139
+ return true;
140
+ }
141
+
142
+ protected function _getCustomerEntity()
143
+ {
144
+ if ($this->_customerEntity) {
145
+ return $this->_customerEntity;
146
+ } else {
147
+ $object = $this->_getObject();
148
+
149
+ if ($object->customerId && is_numeric($object->customerId)) {
150
+ $customerEntity = Mage::getModel('marketingsoftware/copernica_entity_customer');
151
+ $customerEntity->setCustomer($object->customerId);
152
+
153
+ return $this->_customerEntity = $customerEntity;
154
+ } else {
155
+ return false;
156
+ }
157
+ }
158
+ }
159
+ }
app/code/community/Copernica/MarketingSoftware/Model/{QueueEvent → Queue/Event}/Quote.php RENAMED
@@ -27,77 +27,72 @@
27
  /**
28
  * Event to handler quote changes
29
  */
30
- class Copernica_MarketingSoftware_Model_QueueEvent_Quote extends Copernica_MarketingSoftware_Model_QueueEvent_Abstract
31
  {
32
  /**
33
  * Handle modify action
 
34
  * @return boolean
35
  */
36
  public function actionModify()
37
  {
38
- // get magento quote
39
  $quote = Mage::getModel('sales/quote');
40
- $quote->loadByIdWithoutStore($this->getEntityId());
41
 
42
- // we will need customer Id
43
  $customerId = $quote->getCustomerId();
44
 
45
- // create quote entity
46
- $quote = new Copernica_MarketingSoftware_Model_Copernica_Entity_Quote($quote);
47
 
48
- // fetch customer
49
- $customer = new Copernica_MarketingSoftware_Model_Copernica_Entity_Customer($customerId);
50
 
51
- // sync quote with customer
52
- $quote->getREST()->syncWithCustomer($customer);
53
 
54
- // we should be just fine here
55
  return true;
56
  }
57
 
58
  /**
59
  * Handle quote removal
 
60
  * @return boolean
61
  */
62
  public function actionRemove()
63
  {
64
- // quote item collection id
65
- $quoteItemsCollectionId = Mage::helper('marketingsoftware/config')->getCartItemsCollectionId();
 
 
 
 
 
66
 
67
- // profile that we want to use
68
- $customer = $this->getObject()->customer;
69
 
70
- // get profile id of customer
71
- $profileId = Mage::helper('marketingsoftware/Api')->getProfileId(array(
72
- 'id' => $customer->id,
73
- 'storeView' => $customer->storeView,
74
- 'email' => $customer->email
75
  ));
76
 
77
- // get request to local scope
78
- $request = Mage::helper('marketingsoftware/RESTRequest');
79
 
80
- // get all synced items from quote
81
- $result = $request->get('/profile/'.$profileId.'/subprofiles/'.$quoteItemsCollectionId, array(
82
  'fields' => array(
83
- 'quote_id=='.$this->getEntityId()
84
  )
85
  ));
86
 
87
- // prepare multi interface
88
  $request->prepare();
89
 
90
- // if we have proper data we want to remova that data
91
- if (array_key_exists('data', $result) && is_array($result['data'])) foreach ($result['data'] as $item)
92
- {
93
- // add another call to multi interface
94
  $request->delete('/subprofile/'.$item['ID']);
95
  }
96
 
97
- // commit multi interface
98
  $request->commit();
99
-
100
- // we are good
101
  return true;
102
  }
103
  }
27
  /**
28
  * Event to handler quote changes
29
  */
30
+ class Copernica_MarketingSoftware_Model_Queue_Event_Quote extends Copernica_MarketingSoftware_Model_Queue_Event_Abstract
31
  {
32
  /**
33
  * Handle modify action
34
+ *
35
  * @return boolean
36
  */
37
  public function actionModify()
38
  {
 
39
  $quote = Mage::getModel('sales/quote');
40
+ $quote->loadByIdWithoutStore($this->_getEntityId());
41
 
 
42
  $customerId = $quote->getCustomerId();
43
 
44
+ $quoteEntity = Mage::getModel('marketingsoftware/copernica_entity_quote');
45
+ $quoteEntity->setQuote($quote);
46
 
47
+ $customerEntity = Mage::getModel('marketingsoftware/copernica_entity_customer');
48
+ $customerEntity->setCustomer($customerId);
49
 
50
+ $restQuote = $quoteEntity->getRestQuote();
51
+ $restQuote->syncWithCustomer($customerEntity);
52
 
 
53
  return true;
54
  }
55
 
56
  /**
57
  * Handle quote removal
58
+ *
59
  * @return boolean
60
  */
61
  public function actionRemove()
62
  {
63
+ $object = $this->getObject();
64
+
65
+ if (!$object->customerId || !is_numeric($object->customerId)) {
66
+ return false;
67
+ }
68
+
69
+ $quoteItemCollectionId = Mage::helper('marketingsoftware/config')->getQuoteItemCollectionId();
70
 
71
+ $customerEntity = Mage::getModel('marketingsoftware/copernica_entity_customer');
72
+ $customerEntity->setCustomer($object->customerId);
73
 
74
+ $profileId = Mage::helper('marketingsoftware/api')->getProfileId(array(
75
+ 'id' => $customerEntity->fetchId(),
76
+ 'storeView' => $customerEntity->fetchStoreView(),
77
+ 'email' => $customerEntity->fetchEmail()
 
78
  ));
79
 
80
+ $request = Mage::helper('marketingsoftware/rest_request');
 
81
 
82
+ $result = $request->get('/profile/'.$profileId.'/subprofiles/'.$quoteItemCollectionId, array(
 
83
  'fields' => array(
84
+ 'quote_id=='.$this->_getEntityId()
85
  )
86
  ));
87
 
 
88
  $request->prepare();
89
 
90
+ if (array_key_exists('data', $result) && is_array($result['data'])) foreach ($result['data'] as $item) {
 
 
 
91
  $request->delete('/subprofile/'.$item['ID']);
92
  }
93
 
 
94
  $request->commit();
95
+
 
96
  return true;
97
  }
98
  }
app/code/community/Copernica/MarketingSoftware/Model/{QueueEvent/StartSync.php → Queue/Event/Startsync.php} RENAMED
@@ -29,48 +29,52 @@
29
  * sync events. It should spawn them in batches so event queue will not grow into
30
  * cosmic scale.
31
  */
32
- class Copernica_MarketingSoftware_Model_QueueEvent_StartSync extends Copernica_MarketingSoftware_Model_QueueEvent_Abstract
33
  {
34
  /**
35
  * How many events we want to spawn for each of customers/orders/subscriptions
36
- * @var int
 
37
  */
38
- private $eventsLimit = 5000;
39
 
40
  /**
41
  * Current event status. It contains informations about last entites that were
42
  * scheduled for synchronization. Also it does contain stores id that should
43
  * be used to filter entites results.
44
- * @var Copernica_MarketingSoftware_Model_SyncStatus
 
45
  */
46
- private $currentStatus;
47
 
48
  /**
49
  * Process event. If not all entities can be added to synchtonization this
50
  * event will respawn with new set of data. Note that this event will be
51
  * respawning till all entities will be scheduled to sync.
52
- * @return boolean Did we process this event without any errors?
 
53
  */
54
  public function process() {
55
- // get sync status object
56
- $this->currentStatus = Copernica_MarketingSoftware_Model_SyncStatus::fromStd($this->getObject());
57
 
58
- // flag to tell us if we should respawn the event
59
  $shouldRespawn = false;
60
 
61
- // we want to add customers events to queue
62
- if ($this->addCustomersToQueue() == $this->eventsLimit) $shouldRespawn = true;
 
63
 
64
- // we want to add orders events to queue
65
- if ($this->addOrdersToQueue() == $this->eventsLimit) $shouldRespawn = true;
 
66
 
67
- // we want to add subscriptions to queue
68
- if ($this->addSubscriptionsToQueue() == $this->eventsLimit) $shouldRespawn = true;
 
69
 
70
- // respawn the event
71
- if ($shouldRespawn) $this->respawn();
 
72
 
73
- // we did process the event with success
74
  return true;
75
  }
76
 
@@ -79,74 +83,60 @@ class Copernica_MarketingSoftware_Model_QueueEvent_StartSync extends Copernica_M
79
  */
80
  public function respawn()
81
  {
82
- // we want to recreate start sync task with current status object
83
- $queue = Mage::getModel('marketingsoftware/queue')
84
- ->setObject($this->currentStatus->toArray())
85
  ->setAction('start_sync')
86
- ->setName('startSync')
87
  ->save();
88
 
89
- // we should be just fine here
90
  return true;
91
  }
92
 
93
  /**
94
  * Get ::addAttributeToFilter() compatible array of stores that we want to
95
  * sync. Null will be returned when there is no stores to filter.
 
96
  * @return array|null
97
  */
98
- private function getStoresFilter()
99
  {
100
- // get enabled stores array
101
- $enabledStores = $this->currentStatus->getStoresFilter();
102
 
103
- // do we have stores to create a filter?
104
- if (count($enabledStores) == 0) return null;
 
105
 
106
- // placeholder for filter array
107
  $filterArray = array();
108
 
109
- // iterate over all stores and add them to filter array
110
- foreach ($enabledStores as $store)
111
- {
112
  $filterArray []= array('eq' => $store);
113
  }
114
 
115
- // return filter array
116
  return $filterArray;
117
  }
118
 
119
  /**
120
  * Add customers full sync events to queue
121
  * This method will return number of events that were added to queue.
 
122
  * @return int
123
  */
124
- private function addCustomersToQueue()
125
  {
126
- // counter that will report how many events we did add
127
  $addedEvents = 0;
128
 
129
- // get last customer id
130
- $lastCustomerId = $this->currentStatus->getLastCustomerId();
131
 
132
- // get customers collection
133
  $customersCollection = Mage::getModel('customer/customer')->getCollection();
134
-
135
- // we want to get collection in certain order
136
  $customersCollection->addAttributeToSort('entity_id', 'ASC');
 
137
 
138
- // set the customers collection
139
- $customersCollection->setPageSize($this->eventsLimit)->addAttributeToFilter('entity_id', array('gt' => $lastCustomerId ));
140
-
141
- // check if we should filter out some stores
142
- if ($filterArray = $this->getStoresFilter())
143
  $customersCollection->addAttributeToFilter('store_id', $filterArray);
 
144
 
145
- // iterate over customers collection
146
  foreach ($customersCollection as $customer) {
147
-
148
- // create new event on queue
149
- $queue = Mage::getModel('marketingsoftware/queue')
150
  ->setObject(null)
151
  ->setCustomer($customer->getId())
152
  ->setAction('full')
@@ -154,57 +144,42 @@ class Copernica_MarketingSoftware_Model_QueueEvent_StartSync extends Copernica_M
154
  ->setEntityId($customer->getId())
155
  ->save();
156
 
157
- // set new last customer Id
158
- $this->currentStatus->setLastCustomerId($customer->getEntityId());
159
 
160
- // increase events counter
161
  $addedEvents++;
162
  }
163
 
164
- // store when we did last update customers
165
  Mage::helper('marketingsoftware/config')->setCustomerProgressStatus(date('Y-m-d H:i:s'));
166
 
167
- // return counter
168
  return $addedEvents;
169
  }
170
 
171
  /**
172
  * Add all orders that don't have a customer instance tied to them aka 'guest orders'
 
173
  * @return int
174
  */
175
- private function addOrdersToQueue()
176
  {
177
- // new events counter
178
  $addedEvents = 0;
179
 
180
- // get orders collection
181
  $ordersCollection = Mage::getModel('sales/order')->getCollection();
182
-
183
- // we want to pick up from where we stoped
184
  $ordersCollection->addAttributeToFilter('entity_id', array (
185
- 'gt' => $this->currentStatus->getLastOrderId()
186
  ));
187
 
188
- // check if we should filter out some stores
189
- if ($filterArray = $this->getStoresFilter())
190
  $ordersCollection->addAttributeToFilter('store_id', $filterArray);
 
191
 
192
- // we want orders that don't have a customer
193
  $ordersCollection->addFieldToFilter('customer_id', array(
194
  'null' => true
195
  ));
196
-
197
- // we want to get collection in certain order
198
  $ordersCollection->addAttributeToSort('entity_id', 'ASC');
 
199
 
200
- // set limit on collection
201
- $ordersCollection->setPageSize($this->eventsLimit);
202
-
203
- // iterate over all orders in collection
204
  foreach ($ordersCollection as $order) {
205
-
206
- // create new event on queue
207
- $queue = Mage::getModel('marketingsoftware/queue')
208
  ->setObject(array('customer' => null))
209
  ->setCustomer(null)
210
  ->setAction('modify')
@@ -212,17 +187,13 @@ class Copernica_MarketingSoftware_Model_QueueEvent_StartSync extends Copernica_M
212
  ->setEntityId($order->getId())
213
  ->save();
214
 
215
- // increase events counter
216
  $addedEvents++;
217
 
218
- // set last order Id
219
- $this->currentStatus->setLastOrderId($order->getEntityId());
220
  }
221
-
222
- // store when we did last time update orders
223
  Mage::helper('marketingsoftware/config')->setOrderProgressStatus(date('Y-m-d H:i:s'));
224
 
225
- // return added events counter
226
  return $addedEvents;
227
  }
228
 
@@ -230,46 +201,31 @@ class Copernica_MarketingSoftware_Model_QueueEvent_StartSync extends Copernica_M
230
  * This method will spawn events that will synchronise subscriptions that
231
  * doen't have customer entity. This method will also return number of new
232
  * events spawned.
 
233
  * @return int
234
  */
235
- private function addSubscriptionsToQueue()
236
  {
237
- // this will be the event count
238
  $addedEvents = 0;
239
 
240
- // get subscribers collection
241
  $subscriptionsCollection = Mage::getModel('newsletter/subscriber')->getCollection();
242
-
243
- // we want to pick up from where we did stop last time
244
  $subscriptionsCollection->addFieldToFilter('subscriber_id', array(
245
- 'gt' => $this->currentStatus->getLastSubscriptionId()
246
  ));
247
-
248
- /*
249
- * As it occurs that, it's too hard to make one interface for all collections
250
- * in magento platform. Subscriptions is an non eav collection, so ::setOrder()
251
- * is the same ass ::addFieldToSort() on eav collections.
252
- */
253
  $subscriptionsCollection->setOrder('subscriber_id', 'ASC');
254
-
255
- // we want subscribers that don't have a customer
256
  $subscriptionsCollection->addFieldToFilter('customer_id', array(
257
  'eq' => 0,
258
  'null' => true
259
  ));
260
 
261
- // check if we should filter out some stores
262
- if ($filterArray = $this->getStoresFilter())
263
  $subscriptionsCollection->addFieldToFilter('store_id', $filterArray);
 
264
 
265
- // set a limit on collection
266
- $subscriptionsCollection->setPageSize($this->eventsLimit);
267
 
268
- // iterate over all subscriptions
269
- foreach ($subscriptionsCollection as $subscription)
270
- {
271
- // create new event on queue
272
- Mage::getModel('marketingsoftware/queue')
273
  ->setObject(null)
274
  ->setCustomer($subscription->getCustomerId())
275
  ->setAction('modify')
@@ -277,14 +233,11 @@ class Copernica_MarketingSoftware_Model_QueueEvent_StartSync extends Copernica_M
277
  ->setEntityId($subscription->getId())
278
  ->save();
279
 
280
- // store last subscriber Id
281
- $this->currentStatus->setLastSubscriptionId($subscription->getId());
282
 
283
- // increase events counter
284
  $addedEvents++;
285
  }
286
 
287
- // return count of events that we did add
288
  return $addedEvents;
289
  }
290
  }
29
  * sync events. It should spawn them in batches so event queue will not grow into
30
  * cosmic scale.
31
  */
32
+ class Copernica_MarketingSoftware_Model_Queue_Event_Startsync extends Copernica_MarketingSoftware_Model_Queue_Event_Abstract
33
  {
34
  /**
35
  * How many events we want to spawn for each of customers/orders/subscriptions
36
+ *
37
+ * @var int
38
  */
39
+ protected $_eventsLimit = 5000;
40
 
41
  /**
42
  * Current event status. It contains informations about last entites that were
43
  * scheduled for synchronization. Also it does contain stores id that should
44
  * be used to filter entites results.
45
+ *
46
+ * @var Copernica_MarketingSoftware_Model_Sync_Status
47
  */
48
+ protected $_currentStatus;
49
 
50
  /**
51
  * Process event. If not all entities can be added to synchtonization this
52
  * event will respawn with new set of data. Note that this event will be
53
  * respawning till all entities will be scheduled to sync.
54
+ *
55
+ * @return boolean
56
  */
57
  public function process() {
58
+ $this->_currentStatus = Copernica_MarketingSoftware_Model_Sync_Status::fromStd($this->_getObject());
 
59
 
 
60
  $shouldRespawn = false;
61
 
62
+ if ($this->_addCustomersToQueue() == $this->_eventsLimit) {
63
+ $shouldRespawn = true;
64
+ }
65
 
66
+ if ($this->_addOrdersToQueue() == $this->_eventsLimit) {
67
+ $shouldRespawn = true;
68
+ }
69
 
70
+ if ($this->_addSubscriptionsToQueue() == $this->_eventsLimit) {
71
+ $shouldRespawn = true;
72
+ }
73
 
74
+ if ($shouldRespawn) {
75
+ $this->respawn();
76
+ }
77
 
 
78
  return true;
79
  }
80
 
83
  */
84
  public function respawn()
85
  {
86
+ $queue = Mage::getModel('marketingsoftware/queue_item')
87
+ ->setObject($this->_currentStatus->toArray())
 
88
  ->setAction('start_sync')
89
+ ->setName('startsync')
90
  ->save();
91
 
 
92
  return true;
93
  }
94
 
95
  /**
96
  * Get ::addAttributeToFilter() compatible array of stores that we want to
97
  * sync. Null will be returned when there is no stores to filter.
98
+ *
99
  * @return array|null
100
  */
101
+ protected function _getStoresFilter()
102
  {
103
+ $enabledStores = $this->_currentStatus->getStoresFilter();
 
104
 
105
+ if (count($enabledStores) == 0) {
106
+ return null;
107
+ }
108
 
 
109
  $filterArray = array();
110
 
111
+ foreach ($enabledStores as $store) {
 
 
112
  $filterArray []= array('eq' => $store);
113
  }
114
 
 
115
  return $filterArray;
116
  }
117
 
118
  /**
119
  * Add customers full sync events to queue
120
  * This method will return number of events that were added to queue.
121
+ *
122
  * @return int
123
  */
124
+ protected function _addCustomersToQueue()
125
  {
 
126
  $addedEvents = 0;
127
 
128
+ $lastCustomerId = $this->_currentStatus->getLastCustomerId();
 
129
 
 
130
  $customersCollection = Mage::getModel('customer/customer')->getCollection();
 
 
131
  $customersCollection->addAttributeToSort('entity_id', 'ASC');
132
+ $customersCollection->setPageSize($this->_eventsLimit)->addAttributeToFilter('entity_id', array('gt' => $lastCustomerId ));
133
 
134
+ if ($filterArray = $this->_getStoresFilter()) {
 
 
 
 
135
  $customersCollection->addAttributeToFilter('store_id', $filterArray);
136
+ }
137
 
 
138
  foreach ($customersCollection as $customer) {
139
+ $queue = Mage::getModel('marketingsoftware/queue_item')
 
 
140
  ->setObject(null)
141
  ->setCustomer($customer->getId())
142
  ->setAction('full')
144
  ->setEntityId($customer->getId())
145
  ->save();
146
 
147
+ $this->_currentStatus->setLastCustomerId($customer->getEntityId());
 
148
 
 
149
  $addedEvents++;
150
  }
151
 
 
152
  Mage::helper('marketingsoftware/config')->setCustomerProgressStatus(date('Y-m-d H:i:s'));
153
 
 
154
  return $addedEvents;
155
  }
156
 
157
  /**
158
  * Add all orders that don't have a customer instance tied to them aka 'guest orders'
159
+ *
160
  * @return int
161
  */
162
+ protected function _addOrdersToQueue()
163
  {
 
164
  $addedEvents = 0;
165
 
 
166
  $ordersCollection = Mage::getModel('sales/order')->getCollection();
 
 
167
  $ordersCollection->addAttributeToFilter('entity_id', array (
168
+ 'gt' => $this->_currentStatus->getLastOrderId()
169
  ));
170
 
171
+ if ($filterArray = $this->_getStoresFilter()) {
 
172
  $ordersCollection->addAttributeToFilter('store_id', $filterArray);
173
+ }
174
 
 
175
  $ordersCollection->addFieldToFilter('customer_id', array(
176
  'null' => true
177
  ));
 
 
178
  $ordersCollection->addAttributeToSort('entity_id', 'ASC');
179
+ $ordersCollection->setPageSize($this->_eventsLimit);
180
 
 
 
 
 
181
  foreach ($ordersCollection as $order) {
182
+ $queue = Mage::getModel('marketingsoftware/queue_item')
 
 
183
  ->setObject(array('customer' => null))
184
  ->setCustomer(null)
185
  ->setAction('modify')
187
  ->setEntityId($order->getId())
188
  ->save();
189
 
 
190
  $addedEvents++;
191
 
192
+ $this->_currentStatus->setLastOrderId($order->getEntityId());
 
193
  }
194
+
 
195
  Mage::helper('marketingsoftware/config')->setOrderProgressStatus(date('Y-m-d H:i:s'));
196
 
 
197
  return $addedEvents;
198
  }
199
 
201
  * This method will spawn events that will synchronise subscriptions that
202
  * doen't have customer entity. This method will also return number of new
203
  * events spawned.
204
+ *
205
  * @return int
206
  */
207
+ protected function _addSubscriptionsToQueue()
208
  {
 
209
  $addedEvents = 0;
210
 
 
211
  $subscriptionsCollection = Mage::getModel('newsletter/subscriber')->getCollection();
 
 
212
  $subscriptionsCollection->addFieldToFilter('subscriber_id', array(
213
+ 'gt' => $this->_currentStatus->getLastSubscriptionId()
214
  ));
 
 
 
 
 
 
215
  $subscriptionsCollection->setOrder('subscriber_id', 'ASC');
 
 
216
  $subscriptionsCollection->addFieldToFilter('customer_id', array(
217
  'eq' => 0,
218
  'null' => true
219
  ));
220
 
221
+ if ($filterArray = $this->_getStoresFilter()) {
 
222
  $subscriptionsCollection->addFieldToFilter('store_id', $filterArray);
223
+ }
224
 
225
+ $subscriptionsCollection->setPageSize($this->_eventsLimit);
 
226
 
227
+ foreach ($subscriptionsCollection as $subscription) {
228
+ Mage::getModel('marketingsoftware/queue_item')
 
 
 
229
  ->setObject(null)
230
  ->setCustomer($subscription->getCustomerId())
231
  ->setAction('modify')
233
  ->setEntityId($subscription->getId())
234
  ->save();
235
 
236
+ $this->_currentStatus->setLastSubscriptionId($subscription->getId());
 
237
 
 
238
  $addedEvents++;
239
  }
240
 
 
241
  return $addedEvents;
242
  }
243
  }
app/code/community/Copernica/MarketingSoftware/Model/Queue/Event/Subscription.php ADDED
@@ -0,0 +1,96 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ /**
4
+ * This event should take care of any action done on subscription.
5
+ */
6
+ class Copernica_MarketingSoftware_Model_Queue_Event_Subscription extends Copernica_MarketingSoftware_Model_Queue_Event_Abstract
7
+ {
8
+ /**
9
+ * Handle add action
10
+ *
11
+ * @return boolean
12
+ */
13
+ public function actionAdd()
14
+ {
15
+ return $this->actionModify();
16
+ }
17
+
18
+ /**
19
+ * Handle remove action
20
+ *
21
+ * @return boolean
22
+ */
23
+ public function actionRemove()
24
+ {
25
+ $object = $this->getObject();
26
+
27
+ $profileLinkedFields = Mage::helper('marketingsoftware/config')->getLinkedCustomerFields();
28
+
29
+ if (!$object->email && !$object->store_id) {
30
+ return false;
31
+ }
32
+
33
+ $store = Mage::getModel('core/store')->load($object->store_id);
34
+ $website = $store->getWebsite();
35
+ $group = $store->getGroup();
36
+
37
+ $storeView = implode(' > ', array (
38
+ $website->getName(),
39
+ $group->getName(),
40
+ $store->getName())
41
+ );
42
+
43
+ $profileCacheCollection = Mage::getModel('marketingsoftware/profile_cache')
44
+ ->getCollection()
45
+ ->setPageSize(1)
46
+ ->addFieldToFilter('email', $object->email)
47
+ ->addFieldToFilter('store_view', $storeView);
48
+
49
+ $profileCache = $profileCacheCollection->getFirstItem();
50
+
51
+ if (!$profileCache->isObjectNew()) {
52
+ $profileCache->delete();
53
+ }
54
+
55
+ $request = Mage::helper('marketingsoftware/rest_request');
56
+
57
+ $databaseId = Mage::helper('marketingsoftware/config')->getDatabaseId();
58
+
59
+ $result = $request->get('/database/'.$databaseId.'/profiles', array('fields' => array(
60
+ $profileLinkedFields['email'].'=='.$object->email,
61
+ $profileLinkedFields['storeView'].'=='.$storeView
62
+ )));
63
+
64
+ if (!isset($result['total']) && $result['total'] == 0) {
65
+ return false;
66
+ }
67
+
68
+ foreach ($result['data'] as $profile) {
69
+ $request->delete('/profile/'.$profile['ID']);
70
+ }
71
+
72
+ return true;
73
+ }
74
+
75
+ /**
76
+ * Handle modify action
77
+ *
78
+ * @return boolean
79
+ */
80
+ public function actionModify()
81
+ {
82
+ $subscriber = Mage::getModel('newsletter/subscriber')->load($this->_getEntityId());
83
+
84
+ if ($subscriber->isObjectNew()) {
85
+ return true;
86
+ }
87
+
88
+ $subscriptionEntity = Mage::getModel('marketingsoftware/copernica_entity_subscription');
89
+ $subscriptionEntity->setSubscription($subscriber);
90
+
91
+ $restSubscription = $subscriptionEntity->getRestSubscription();
92
+ $restSubscription->sync();
93
+
94
+ return true;
95
+ }
96
+ }
app/code/community/Copernica/MarketingSoftware/Model/{QueueEvent → Queue/Event}/View.php RENAMED
@@ -28,7 +28,7 @@
28
  * This class will take care of syncing product view.
29
  * Everytime when user is visiting a page with a product this event is fired up.
30
  */
31
- class Copernica_MarketingSoftware_Model_QueueEvent_View extends Copernica_MarketingSoftware_Model_QueueEvent_Abstract
32
  {
33
  /**
34
  * Tell copernica platform that certain user did view certain product
@@ -36,16 +36,22 @@ class Copernica_MarketingSoftware_Model_QueueEvent_View extends Copernica_Market
36
  */
37
  public function actionAdd()
38
  {
39
- // get product
40
- $product = Copernica_MarketingSoftware_Model_Copernica_Entity::create('product', $this->queueItem->getEntityId());
 
 
 
41
 
42
- // get customer
43
- $customer = Copernica_MarketingSoftware_Model_Copernica_Entity::create('customer', $this->queueItem->getObject()->customer);
 
 
 
 
44
 
45
- // we want to tell copernica that product was viewed by customer
46
- $product->getREST()->viewedBy($customer);
47
 
48
- // everything went just dandy
49
  return true;
50
  }
51
  }
28
  * This class will take care of syncing product view.
29
  * Everytime when user is visiting a page with a product this event is fired up.
30
  */
31
+ class Copernica_MarketingSoftware_Model_Queue_Event_View extends Copernica_MarketingSoftware_Model_Queue_Event_Abstract
32
  {
33
  /**
34
  * Tell copernica platform that certain user did view certain product
36
  */
37
  public function actionAdd()
38
  {
39
+ $object = $this->_queueItem->getObject();
40
+
41
+ if (!$object->customerId || !is_numeric($object->customerId) || !$object->productId || !is_numeric($object->productId) || !$object->viewedAt || !is_numeric($object->viewedAt)) {
42
+ return false;
43
+ }
44
 
45
+ $customerEntity = Mage::getModel('marketingsoftware/copernica_entity_customer');
46
+ $customerEntity->setCustomer($object->customerId);
47
+
48
+ $productEntity = Mage::getModel('marketingsoftware/copernica_entity_product');
49
+ $productEntity->setProduct($object->productId);
50
+ $productEntity->setTimestamp(date('Y-m-d G:i:s', $object->viewedAt));
51
 
52
+ $restProduct = $productEntity->getRestProduct();
53
+ $restProduct->viewedBy($customerEntity);
54
 
 
55
  return true;
56
  }
57
  }
app/code/community/Copernica/MarketingSoftware/Model/Queue/Event/Wishlist/Item.php ADDED
@@ -0,0 +1,113 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Copernica Marketing Software
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0).
8
+ * It is available through the world-wide-web at this URL:
9
+ * http://opensource.org/licenses/osl-3.0.php
10
+ * If you are unable to obtain a copy of the license through the
11
+ * world-wide-web, please send an email to copernica@support.cream.nl
12
+ * so we can send you a copy immediately.
13
+ *
14
+ * DISCLAIMER
15
+ *
16
+ * Do not edit or add to this file if you wish to upgrade this software
17
+ * to newer versions in the future. If you wish to customize this module
18
+ * for your needs please refer to http://www.magento.com/ for more
19
+ * information.
20
+ *
21
+ * @category Copernica
22
+ * @package Copernica_MarketingSoftware
23
+ * @copyright Copyright (c) 2011-2012 Copernica & Cream. (http://docs.cream.nl/)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ /**
28
+ * Event to handler wishlsit changes
29
+ */
30
+ class Copernica_MarketingSoftware_Model_Queue_Event_Wishlist_Item extends Copernica_MarketingSoftware_Model_Queue_Event_Abstract
31
+ {
32
+ /**
33
+ * Add functionality is the same as that of modify
34
+ *
35
+ * @return boolean
36
+ */
37
+ public function actionAdd()
38
+ {
39
+ return $this->actionModify();
40
+ }
41
+
42
+ /**
43
+ * Handle modify action
44
+ *
45
+ * @return boolean
46
+ */
47
+ public function actionModify()
48
+ {
49
+ $object = $this->_getObject();
50
+
51
+ if (!$object->wishlistItemId || !is_numeric($object->wishlistItemId) || !$object->customerId || !is_numeric($object->customerId)) {
52
+ return false;
53
+ }
54
+
55
+ $wishlistItem = Mage::getModel('wishlist/item')->load($object->wishlistItemId);
56
+
57
+ $customerId = $object->customerId;
58
+
59
+ $wishlistItemEntity = Mage::getModel('marketingsoftware/copernica_entity_wishlist_item');
60
+ $wishlistItemEntity->setWishlistItem($wishlistItem);
61
+
62
+ $customerEntity = Mage::getModel('marketingsoftware/copernica_entity_customer');
63
+ $customerEntity->setCustomer($customerId);
64
+
65
+ $restWishlistItem = $wishlistItemEntity->getRestWishlistItem();
66
+ $restWishlistItem->syncWithCustomer($customerEntity);
67
+
68
+ return true;
69
+ }
70
+
71
+ /**
72
+ * Handle wishlist removal
73
+ *
74
+ * @return boolean
75
+ */
76
+ public function actionRemove()
77
+ {
78
+ $object = $this->getObject();
79
+
80
+ if (!$object->customerId || !is_numeric($object->customerId)) {
81
+ return false;
82
+ }
83
+
84
+ $wishlistItemCollectionId = Mage::helper('marketingsoftware/config')->getWishlistItemCollectionId();
85
+
86
+ $customerEntity = Mage::getModel('marketingsoftware/copernica_entity_customer');
87
+ $customerEntity->setCustomer($object->customerId);
88
+
89
+ $profileId = Mage::helper('marketingsoftware/api')->getProfileId(array(
90
+ 'id' => $customerEntity->fetchId(),
91
+ 'storeView' => $customerEntity->fetchStoreView(),
92
+ 'email' => $customerEntity->fetchEmail()
93
+ ));
94
+
95
+ $request = Mage::helper('marketingsoftware/rest_request');
96
+
97
+ $result = $request->get('/profile/'.$profileId.'/subprofiles/'.$wishlistItemCollectionId, array(
98
+ 'fields' => array(
99
+ 'item_id=='.$this->_getEntityId()
100
+ )
101
+ ));
102
+
103
+ $request->prepare();
104
+
105
+ if (array_key_exists('data', $result) && is_array($result['data'])) foreach ($result['data'] as $item) {
106
+ $request->delete('/subprofile/'.$item['ID']);
107
+ }
108
+
109
+ $request->commit();
110
+
111
+ return true;
112
+ }
113
+ }
app/code/community/Copernica/MarketingSoftware/Model/{Queue.php → Queue/Item.php} RENAMED
@@ -28,18 +28,19 @@
28
  * Queue object for accessing the events in the queue table.
29
  *
30
  */
31
- class Copernica_MarketingSoftware_Model_Queue extends Mage_Core_Model_Abstract
32
  {
33
  /**
34
  * Constructor for the model
35
  */
36
  protected function _construct()
37
  {
38
- $this->_init('marketingsoftware/queue');
39
  }
40
 
41
  /**
42
  * Get 1st free queue event that is not locked by any lock.
 
43
  * @return string
44
  */
45
  public function getFirstFree()
@@ -54,116 +55,105 @@ class Copernica_MarketingSoftware_Model_Queue extends Mage_Core_Model_Abstract
54
  */
55
  public function getObject()
56
  {
57
- // get raw data store in row
58
- $data = parent::getData('object');
59
 
60
- // try to decode object
61
  $object = json_decode($data);
62
 
63
- /*
64
- * Old format was storing serialized objects data in this field, thus
65
- * there is a possibility that some of that old data will be still
66
- * present in table data and json_decode will return null value. In
67
- * such case we will fallback on default unserialize function.
68
- */
69
- if (!is_null($object)) return $object;
70
 
71
- // unserialize data
72
- return unserialize(parent::getData('object'));
73
  }
74
 
75
  /**
76
  * Set the data to the model
77
- * @param object Object that will be serialized and stored with queue item
78
- * @return self
 
79
  */
80
  public function setObject($object = null)
81
  {
82
- // should we reset the data object
83
- if (is_null($object))
84
- {
85
- // set data object to empty string
86
- parent::setData('object', '');
87
 
88
- // allow chaining
89
  return $this;
90
  }
91
 
92
- // encode object
93
  $json = json_encode($object);
94
 
95
- // set the value from the parent implementation
96
- parent::setData('object', $json ? $json : '');
97
 
98
- // allow chaining
99
  return $this;
100
  }
101
 
102
  /**
103
  * Set the customer that is interested in queue item
104
- * @param int
105
- * @return self
 
106
  */
107
  public function setCustomerId($customerId)
108
  {
109
- parent::setData('customer', $customerId);
110
 
111
- // allow chaining
112
  return $this;
113
  }
114
 
115
  /**
116
  * Get customer that is
 
117
  * @return id
118
  */
119
  public function getCustomerId()
120
  {
121
- // get stored customer Id
122
- return parent::getData('customer');
123
  }
124
 
125
  /**
126
  * Set name of the event
127
- * @param string
128
- * @return self
 
129
  */
130
  public function setName($name)
131
  {
132
- parent::setData('name', $name);
133
 
134
- // allow chaining
135
  return $this;
136
  }
137
 
138
  /**
139
  * Get name of the event
 
140
  * @return string
141
  */
142
  public function getName()
143
  {
144
- return parent::getData('name');
145
  }
146
 
147
  /**
148
  * Set associated entity Id
149
- * @param int
150
- * @return self
 
151
  */
152
  public function setEntityId($id)
153
  {
154
- parent::setData('entity_id', $id);
155
 
156
- // allow chaining
157
  return $this;
158
  }
159
 
160
  /**
161
  * Get associate entity Id
 
162
  * @return int
163
  */
164
  public function getEntityId()
165
  {
166
- return parent::getData('entity_id');
167
  }
168
 
169
  /**
@@ -174,8 +164,15 @@ class Copernica_MarketingSoftware_Model_Queue extends Mage_Core_Model_Abstract
174
  */
175
  public function process()
176
  {
177
- $event = Copernica_MarketingSoftware_Model_QueueEvent_Abstract::create($this);
 
 
 
 
178
 
 
 
 
179
  return $event->process();
180
  }
181
 
@@ -187,67 +184,66 @@ class Copernica_MarketingSoftware_Model_Queue extends Mage_Core_Model_Abstract
187
  * modify/add events before this 'remove' event. We can remove such event
188
  * before we save one with 'remove'
189
  *
190
- * @return Copernica_MarketingSoftware_Model_Queue
191
  */
192
  public function save()
193
  {
194
- // save the queuetime
195
  $this->setQueueTime(date("Y-m-d H:i:s"));
196
 
197
- // check if we should remove non remove siblings
198
- if ($this->getAction() == 'remove') $this->clearNoRemoveSiblings();
 
199
 
200
- // we want to merge syblings only for 'add' and 'modify' events
201
- if ($this->getAction() == 'add' || $this->getAction() == 'modify')
202
- {
203
- if ($this->shouldSave()) parent::save();
204
- else return $this;
 
205
  }
206
 
207
- // rely on parent
208
  return parent::save();
209
  }
210
 
211
  /**
212
  * Clear all modify/add events that are before this event.
213
  */
214
- private function clearNoRemoveSiblings()
215
  {
216
- // try to get a collection with elements that are the same as current one but are 'add' actions
217
  $collection = $this->getCollection()
218
  ->addFilter('action', 'add')
219
  ->addFilter('name', $this->getName())
220
- ->addFilter('object', ($object = parent::getData('object')) ? $object : '')
221
- ->addFilter('entity_id', $this->getEntityId());
222
 
223
- // remove all add or modifi actions
224
- foreach ($collection as $event) $event->delete();
 
225
 
226
- // try to get a collection with elements that are the same as current one but are 'modify' actions
227
  $collection = $this->getCollection()
228
  ->addFilter('action', 'modify')
229
  ->addFilter('name', $this->getName())
230
- ->addFilter('object', ($object = parent::getData('object')) ? $object : '')
231
- ->addFilter('entity_id', $this->getEntityId());
232
 
233
- // remove all add or modifi actions
234
- foreach ($collection as $event) $event->delete();
 
235
  }
236
 
237
  /**
238
  * Merge all siblings
 
239
  * @return boolean
240
  */
241
- private function shouldSave()
242
  {
243
- // try to get a collection with elements that are the same as current one
244
  $collection = $this->getCollection()
245
  ->addFilter('action', $this->getAction())
246
  ->addFilter('name', $this->getName())
247
- ->addFilter('object', ($object = parent::getData('object')) ? $object : '')
248
  ->addFilter('entity_id', $this->getEntityId());
249
 
250
- // if there are the same element we should not save
251
  return $collection->count() == 0;
252
  }
253
  }
28
  * Queue object for accessing the events in the queue table.
29
  *
30
  */
31
+ class Copernica_MarketingSoftware_Model_Queue_Item extends Mage_Core_Model_Abstract
32
  {
33
  /**
34
  * Constructor for the model
35
  */
36
  protected function _construct()
37
  {
38
+ $this->_init('marketingsoftware/queue_item');
39
  }
40
 
41
  /**
42
  * Get 1st free queue event that is not locked by any lock.
43
+ *
44
  * @return string
45
  */
46
  public function getFirstFree()
55
  */
56
  public function getObject()
57
  {
58
+ $data = $this->getData('object');
 
59
 
 
60
  $object = json_decode($data);
61
 
62
+ if (!is_null($object)) {
63
+ return $object;
64
+ }
 
 
 
 
65
 
66
+ return unserialize($this->getData('object'));
 
67
  }
68
 
69
  /**
70
  * Set the data to the model
71
+ *
72
+ * @param object $object
73
+ * @return Copernica_MarketingSoftware_Model_Queue_Item
74
  */
75
  public function setObject($object = null)
76
  {
77
+ if (is_null($object)) {
78
+ $this->setData('object', '');
 
 
 
79
 
 
80
  return $this;
81
  }
82
 
 
83
  $json = json_encode($object);
84
 
85
+ $this->setData('object', $json ? $json : '');
 
86
 
 
87
  return $this;
88
  }
89
 
90
  /**
91
  * Set the customer that is interested in queue item
92
+ *
93
+ * @param int $customerId
94
+ * @return Copernica_MarketingSoftware_Model_Queue_Item
95
  */
96
  public function setCustomerId($customerId)
97
  {
98
+ $this->setData('customer', $customerId);
99
 
 
100
  return $this;
101
  }
102
 
103
  /**
104
  * Get customer that is
105
+ *
106
  * @return id
107
  */
108
  public function getCustomerId()
109
  {
110
+ return $this->getData('customer');
 
111
  }
112
 
113
  /**
114
  * Set name of the event
115
+ *
116
+ * @param string $name
117
+ * @return Copernica_MarketingSoftware_Model_Queue_Item
118
  */
119
  public function setName($name)
120
  {
121
+ $this->setData('name', $name);
122
 
 
123
  return $this;
124
  }
125
 
126
  /**
127
  * Get name of the event
128
+ *
129
  * @return string
130
  */
131
  public function getName()
132
  {
133
+ return $this->getData('name');
134
  }
135
 
136
  /**
137
  * Set associated entity Id
138
+ *
139
+ * @param int $id
140
+ * @return Copernica_MarketingSoftware_Model_Queue_Item
141
  */
142
  public function setEntityId($id)
143
  {
144
+ $this->setData('entity_id', $id);
145
 
 
146
  return $this;
147
  }
148
 
149
  /**
150
  * Get associate entity Id
151
+ *
152
  * @return int
153
  */
154
  public function getEntityId()
155
  {
156
+ return $this->getData('entity_id');
157
  }
158
 
159
  /**
164
  */
165
  public function process()
166
  {
167
+ $modelName = 'marketingsoftware/queue_event_'. $this->getName();
168
+
169
+ if (!class_exists(Mage::getConfig()->getModelClassName($modelName))) {
170
+ return null;
171
+ }
172
 
173
+ $event = Mage::getModel($modelName);
174
+ $event->setQueueItem($this);
175
+
176
  return $event->process();
177
  }
178
 
184
  * modify/add events before this 'remove' event. We can remove such event
185
  * before we save one with 'remove'
186
  *
187
+ * @return Copernica_MarketingSoftware_Model_Queue_Item
188
  */
189
  public function save()
190
  {
 
191
  $this->setQueueTime(date("Y-m-d H:i:s"));
192
 
193
+ if ($this->getAction() == 'remove') {
194
+ $this->_clearNoRemoveSiblings();
195
+ }
196
 
197
+ if ($this->getAction() == 'add' || $this->getAction() == 'modify') {
198
+ if ($this->_shouldSave()) {
199
+ parent::save();
200
+ } else {
201
+ return $this;
202
+ }
203
  }
204
 
 
205
  return parent::save();
206
  }
207
 
208
  /**
209
  * Clear all modify/add events that are before this event.
210
  */
211
+ protected function _clearNoRemoveSiblings()
212
  {
 
213
  $collection = $this->getCollection()
214
  ->addFilter('action', 'add')
215
  ->addFilter('name', $this->getName())
216
+ ->addFilter('object', ($object = $this->getData('object')) ? $object : '')
217
+ ->addFilter('entity_id', $this->_getEntityId());
218
 
219
+ foreach ($collection as $event) {
220
+ $event->delete();
221
+ }
222
 
 
223
  $collection = $this->getCollection()
224
  ->addFilter('action', 'modify')
225
  ->addFilter('name', $this->getName())
226
+ ->addFilter('object', ($object = $this->getData('object')) ? $object : '')
227
+ ->addFilter('entity_id', $this->_getEntityId());
228
 
229
+ foreach ($collection as $event) {
230
+ $event->delete();
231
+ }
232
  }
233
 
234
  /**
235
  * Merge all siblings
236
+ *
237
  * @return boolean
238
  */
239
+ protected function _shouldSave()
240
  {
 
241
  $collection = $this->getCollection()
242
  ->addFilter('action', $this->getAction())
243
  ->addFilter('name', $this->getName())
244
+ ->addFilter('object', ($object = $this->getData('object')) ? $object : '')
245
  ->addFilter('entity_id', $this->getEntityId());
246
 
 
247
  return $collection->count() == 0;
248
  }
249
  }
app/code/community/Copernica/MarketingSoftware/Model/Queue/Processor.php ADDED
@@ -0,0 +1,243 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Copernica Marketing Software
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0).
8
+ * It is available through the world-wide-web at this URL:
9
+ * http://opensource.org/licenses/osl-3.0.php
10
+ * If you are unable to obtain a copy of the license through the
11
+ * world-wide-web, please send an email to copernica@support.cream.nl
12
+ * so we can send you a copy immediately.
13
+ *
14
+ * DISCLAIMER
15
+ *
16
+ * Do not edit or add to this file if you wish to upgrade this software
17
+ * to newer versions in the future. If you wish to customize this module
18
+ * for your needs please refer to http://www.magento.com/ for more
19
+ * information.
20
+ *
21
+ * @category Copernica
22
+ * @package Copernica_MarketingSoftware
23
+ * @copyright Copyright (c) 2011-2012 Copernica & Cream. (http://docs.cream.nl/)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ /**
28
+ * This class will process task queue.
29
+ */
30
+ class Copernica_MarketingSoftware_Model_Queue_Processor
31
+ {
32
+ /**
33
+ * Number of processed tasks by this processor
34
+ *
35
+ * @var int
36
+ */
37
+ protected $_processedTasks = 0;
38
+
39
+ /**
40
+ * Timestamp when processor starts its job
41
+ */
42
+ protected $_startTime;
43
+
44
+ /**
45
+ * How many items we want to process in one run?
46
+ *
47
+ * @var int
48
+ */
49
+ protected $_itemsLimit = 10000000;
50
+
51
+ /**
52
+ * For what is our timelimit for queue processing? in seconds.
53
+ *
54
+ * @var int
55
+ */
56
+ protected $_timeLimit = 3075840000;
57
+
58
+ /**
59
+ * Currently locked customer
60
+ *
61
+ * @var int
62
+ */
63
+ protected $_currentCustomer = null;
64
+
65
+ /**
66
+ * Construct object.
67
+ *
68
+ * NOTE: This class is not a varien_object child!
69
+ */
70
+ public function __construct()
71
+ {
72
+ $config = Mage::helper('marketingsoftware/config');
73
+
74
+ if ($itemsLimit = $config->getItemsPerRun()) {
75
+ $this->_itemsLimit = $itemsLimit;
76
+ }
77
+
78
+ if ($timeLimit = $config->getTimePerRun()) {
79
+ $this->_timeLimit = $timeLimit;
80
+ }
81
+
82
+ $config = Mage::helper('marketingsoftware/config');
83
+ $config->setLastStartTimeCronjob(date("Y-m-d H:i:s"));
84
+ }
85
+
86
+ /**
87
+ * We want to make some final actions when this processor is beeing destroyed.
88
+ */
89
+ public function __destruct()
90
+ {
91
+ $config = Mage::helper('marketingsoftware/config');
92
+ $config->setLastEndTimeCronjob(date("Y-m-d H:i:s"));
93
+ $config->setLastCronjobProcessedTasks($this->_processedTasks);
94
+ }
95
+
96
+ /**
97
+ * Try to aqcuire 1st lock that can be used to sync data.
98
+ *
99
+ * @return string
100
+ */
101
+ public function aqcuireLock()
102
+ {
103
+ $firstFree = Mage::getModel('marketingsoftware/queue_item')->getFirstFree();
104
+
105
+ return $this->_currentCustomer = $firstFree;
106
+ }
107
+
108
+ /**
109
+ * Process queue with lock
110
+ *
111
+ * @param string $lock
112
+ */
113
+ public function processWithLocking($lock)
114
+ {
115
+ if (is_numeric($lock)) {
116
+ $this->processQueue($this->_currentCustomer);
117
+ } elseif (is_null($lock)) {
118
+ $this->processQueue(null);
119
+ }
120
+ }
121
+
122
+ /**
123
+ * Process queue
124
+ *
125
+ * @param string $customerId
126
+ */
127
+ public function processQueue($customerId = -1)
128
+ {
129
+ $maxExecutionTime = ini_get('max_execution_time');
130
+
131
+ set_time_limit(0);
132
+
133
+ $queue = Mage::getResourceModel('marketingsoftware/queue_item_collection')
134
+ ->addDefaultOrder()->setPageSize($this->_itemsLimit < 150 ? 150 : $this->_itemsLimit);
135
+
136
+ if (is_null($customerId)) {
137
+ $queue->addFieldToFilter('customer', array('null' => true));
138
+ } elseif ($customerId > -1) {
139
+ $queue->addFilter('customer', $customerId);
140
+ }
141
+
142
+ $this->_prepareProcessor();
143
+
144
+ foreach ($queue as $item) {
145
+ if ($this->_isLimitsReached()) {
146
+ break;
147
+ }
148
+
149
+ $this->_processItem($item);
150
+ }
151
+
152
+ set_time_limit($maxExecutionTime);
153
+ }
154
+
155
+ /**
156
+ * Make some preparations before we start processing queue
157
+ */
158
+ protected function _prepareProcessor()
159
+ {
160
+ $this->_startTime = microtime(true);
161
+ }
162
+
163
+ /**
164
+ * Check if we reached limits
165
+ *
166
+ * @return bool
167
+ */
168
+ protected function _isLimitsReached()
169
+ {
170
+ return $this->_processedTasks > $this->_itemsLimit || microtime(true) > $this->_startTime + $this->_timeLimit;
171
+ }
172
+
173
+ /**
174
+ * Process queue item
175
+ *
176
+ * @param Copernica_MarketingSoftware_Model_Queue_Item $item
177
+ */
178
+ protected function _processItem(Copernica_MarketingSoftware_Model_Queue_Item $item)
179
+ {
180
+ try {
181
+ if ($item->process()) {
182
+ $item->delete();
183
+ } else {
184
+ $this->_transferItemToErrorQueue($item);
185
+ }
186
+
187
+ $this->_processedTasks++;
188
+ } catch (Copernica_MarketingSoftware_Exception $copernicaException) {
189
+ Mage::log($copernicaException->getMessage(), null, 'copernica_queue_exceptions.log');
190
+
191
+ $this->_transferItemToErrorQueue($item);
192
+ } catch (Exception $exception) {
193
+ Mage::logException($exception);
194
+
195
+ $item->setResult($exception->getMessage())->setResultTime(date('Y-m-d H:i:s'));
196
+ }
197
+ }
198
+
199
+ /**
200
+ * Transfer queue item to error queue.
201
+ *
202
+ * @param Copernica_MarketingSoftware_Model_Queue_Item $item
203
+ */
204
+ protected function _transferItemToErrorQueue(Copernica_MarketingSoftware_Model_Queue_Item $item)
205
+ {
206
+ $errorItem = Copernica_MarketingSoftware_Model_Error_Queue::createFromQueueItem($item);
207
+ $errorItem->save();
208
+
209
+ $item->delete();
210
+ }
211
+
212
+ /**
213
+ * Fetch data about current run.
214
+ *
215
+ * @param string $type
216
+ */
217
+ public function fetchReport($type)
218
+ {
219
+ $data = array(
220
+ 'startTime' => date('Y-m-d H:i:s', (int)$this->_startTime),
221
+ 'runTime' => (microtime(true) - $this->_startTime),
222
+ 'processedTasks' => $this->_processedTasks,
223
+ 'itemsLimit' => $this->_itemsLimit,
224
+ 'timeLimit' => $this->_timeLimit,
225
+ 'lockedCustomer' => $this->_currentCustomer
226
+ );
227
+
228
+ $type = strtolower($type);
229
+
230
+ switch ($type) {
231
+ case 'json':
232
+ return json_encode($data);
233
+
234
+ default:
235
+ $report = " Started at ".$data['startTime'].' UTC'.PHP_EOL;
236
+ $report .= " Run took ".$data['runTime']." seconds".PHP_EOL;
237
+ $report .= " Processed ".$data['processedTasks']." tasks".PHP_EOL;
238
+ $report .= " Limited with ".$data['itemsLimit']." items and ".$data['timeLimit']." seconds".PHP_EOL;
239
+ $report .= " ".($data['lockedCustomer'] ? "Locked on ".$data['lockedCustomer'] : "Without a lock").PHP_EOL;
240
+ return $report;
241
+ }
242
+ }
243
+ }
app/code/community/Copernica/MarketingSoftware/Model/QueueEvent/Abstract.php DELETED
@@ -1,119 +0,0 @@
1
- <?php
2
- /**
3
- * Copernica Marketing Software
4
- *
5
- * NOTICE OF LICENSE
6
- *
7
- * This source file is subject to the Open Software License (OSL 3.0).
8
- * It is available through the world-wide-web at this URL:
9
- * http://opensource.org/licenses/osl-3.0.php
10
- * If you are unable to obtain a copy of the license through the
11
- * world-wide-web, please send an email to copernica@support.cream.nl
12
- * so we can send you a copy immediately.
13
- *
14
- * DISCLAIMER
15
- *
16
- * Do not edit or add to this file if you wish to upgrade this software
17
- * to newer versions in the future. If you wish to customize this module
18
- * for your needs please refer to http://www.magento.com/ for more
19
- * information.
20
- *
21
- * @category Copernica
22
- * @package Copernica_MarketingSoftware
23
- * @copyright Copyright (c) 2011-2012 Copernica & Cream. (http://docs.cream.nl/)
24
- * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
- */
26
-
27
- /**
28
- * A wrapper object around an event
29
- */
30
- abstract class Copernica_MarketingSoftware_Model_QueueEvent_Abstract
31
- {
32
- /**
33
- * What queue item was used to construct this item
34
- * @var Copernica_MarketingSoftware_Model_Queue
35
- */
36
- protected $queueItem;
37
-
38
- /**
39
- * Construct the item given the queueitem
40
- *
41
- * @param Copernica_MarketingSoftware_Model_Queue $queueItem
42
- */
43
- protected function __construct($queueItem)
44
- {
45
- $this->queueItem = $queueItem;
46
- }
47
-
48
- /**
49
- * We will use this factory method to create proper event.
50
- * @param Copernica_MarketingSoftware_Model_Queue
51
- * @return Copernica_MarketingSoftware_Model_QueueEvent_Abstract
52
- */
53
- static public function create($queueItem)
54
- {
55
- // get classname of event that we want to create
56
- $classname = 'Copernica_MarketingSoftware_Model_QueueEvent_'.ucfirst($queueItem->getName());
57
-
58
- // check if desired class exists
59
- if (!class_exists($classname)) return null;
60
-
61
- // create new queue event and return it
62
- return new $classname($queueItem);
63
- }
64
-
65
- /**
66
- * Get the object for this queue item
67
- * @return Abstraction object
68
- */
69
- protected function getObject()
70
- {
71
- return $this->queueItem->getObject();
72
- }
73
-
74
- /**
75
- * Get entity Id
76
- */
77
- protected function getEntityId()
78
- {
79
- return $this->queueItem->getEntityId();
80
- }
81
-
82
- /**
83
- * Get customer Id
84
- * @return int
85
- */
86
- protected function getCustomerId()
87
- {
88
- return $this->queueItem->getCustomerId();
89
- }
90
-
91
- /**
92
- * Process this item in the queue
93
- * @return boolean was the processing successfull
94
- */
95
- public function process()
96
- {
97
- // desired method that we want to call
98
- $methodName = 'action'.ucfirst($this->queueItem->getAction());
99
-
100
- // check if desired method exists
101
- if (!method_exists($this, $methodName)) return false;
102
-
103
- // run desired method
104
- return $this->$methodName();
105
- }
106
-
107
- /**
108
- * Respawn event on the queue.
109
- */
110
- public function respawn()
111
- {
112
- // create new item on event queue
113
- Mage::getModel('marketingsoftware/queue')
114
- ->setObject($this->queueItem->getObject())
115
- ->setCustomer($this->queueItem->getCustomer())
116
- ->setAction($this->queueItem->getAction())
117
- ->save();
118
- }
119
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app/code/community/Copernica/MarketingSoftware/Model/QueueEvent/Checkout.php DELETED
@@ -1,96 +0,0 @@
1
- <?php
2
- /**
3
- * Copernica Marketing Software
4
- *
5
- * NOTICE OF LICENSE
6
- *
7
- * This source file is subject to the Open Software License (OSL 3.0).
8
- * It is available through the world-wide-web at this URL:
9
- * http://opensource.org/licenses/osl-3.0.php
10
- * If you are unable to obtain a copy of the license through the
11
- * world-wide-web, please send an email to copernica@support.cream.nl
12
- * so we can send you a copy immediately.
13
- *
14
- * DISCLAIMER
15
- *
16
- * Do not edit or add to this file if you wish to upgrade this software
17
- * to newer versions in the future. If you wish to customize this module
18
- * for your needs please refer to http://www.magento.com/ for more
19
- * information.
20
- *
21
- * @category Copernica
22
- * @package Copernica_MarketingSoftware
23
- * @copyright Copyright (c) 2011-2012 Copernica & Cream. (http://docs.cream.nl/)
24
- * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
- */
26
-
27
- /**
28
- * This class should take care of all events related to checkout.
29
- *
30
- * This class does not matter. It should be not used cause it's same as
31
- * order modify action.
32
- */
33
- class Copernica_MarketingSoftware_Model_QueueEvent_Checkout extends Copernica_MarketingSoftware_Model_QueueEvent_Abstract
34
- {
35
- /**
36
- * Process add action
37
- * @return boolean
38
- */
39
- public function actionAdd()
40
- {
41
- return $this->actionModify();
42
- }
43
-
44
- /**
45
- * Modify action on checkout event
46
- * @return boolean
47
- */
48
- public function actionModify()
49
- {
50
- // get data object associated with this event
51
- $object = $this->getObject();
52
-
53
- // check if we have customer Id inside data object
54
- if (!property_exists($object, 'customer')) return false;
55
-
56
- // create customer entity
57
- $customer = new Copernica_MarketingSoftware_Model_Copernica_Entity_Customer($object->customer);
58
-
59
- // get matento quote
60
- $quote = Mage::getModel('sales/quote')->loadByIdWithoutStore($this->getEntityId());
61
-
62
- // bring request to local scope
63
- $request = Mage::helper('marketingsoftware/RESTRequest');
64
-
65
- // we want to sync all items at once
66
- $request->prepare();
67
-
68
- if (Mage::helper('marketingsoftware/config')->getRemoveFinishedCartItems())
69
- {
70
- // get cart items collection
71
- $cartItemsCollection = Mage::helper('marketingsoftware/config')->getCartItemsCollectionId();
72
-
73
- // get profiles data
74
- if ($cartItemsCollection) $response = $request->get('/profile/'.$customer->getProfileId().'/subprofiles/'.$cartItemsCollection, array(
75
- 'fields' => array('quote_id=='.$this->getEntityId())
76
- ));
77
-
78
- foreach ($response['data'] as $subprofile) $request->delete('/subprofile/'.$subprofile['ID']);
79
- }
80
- else
81
- {
82
- // sync items
83
- foreach ($quote->getAllVisibleItems() as $item)
84
- {
85
- $item = new Copernica_MarketingSoftware_Model_Copernica_Entity_CartItem($item);
86
- $item->getREST()->syncWithQuote($customer, $quote->getId());
87
- }
88
- }
89
-
90
- // commit request
91
- $request->commit();
92
-
93
- // we are done here
94
- return true;
95
- }
96
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app/code/community/Copernica/MarketingSoftware/Model/QueueEvent/CustomerAdd.php DELETED
@@ -1,52 +0,0 @@
1
- <?php
2
- /**
3
- * Copernica Marketing Software
4
- *
5
- * NOTICE OF LICENSE
6
- *
7
- * This source file is subject to the Open Software License (OSL 3.0).
8
- * It is available through the world-wide-web at this URL:
9
- * http://opensource.org/licenses/osl-3.0.php
10
- * If you are unable to obtain a copy of the license through the
11
- * world-wide-web, please send an email to copernica@support.cream.nl
12
- * so we can send you a copy immediately.
13
- *
14
- * DISCLAIMER
15
- *
16
- * Do not edit or add to this file if you wish to upgrade this software
17
- * to newer versions in the future. If you wish to customize this module
18
- * for your needs please refer to http://www.magento.com/ for more
19
- * information.
20
- *
21
- * @category Copernica
22
- * @package Copernica_MarketingSoftware
23
- * @copyright Copyright (c) 2011-2012 Copernica & Cream. (http://docs.cream.nl/)
24
- * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
- */
26
-
27
- /**
28
- * A wrapper object around an event
29
- */
30
- class Copernica_MarketingSoftware_Model_QueueEvent_CustomerAdd extends Copernica_MarketingSoftware_Model_QueueEvent_Abstract
31
- {
32
- /**
33
- * Process this item in the queue
34
- * @return boolean was the processing successfull
35
- */
36
- public function process()
37
- {
38
- // Get the copernica API
39
- $api = Mage::helper('marketingsoftware/api');
40
-
41
- // Get the customer
42
- $customerData = Mage::getModel('marketingsoftware/copernica_profilecustomer')
43
- ->setCustomer($customer = $this->getObject())
44
- ->setDirection('copernica');
45
-
46
- // Update the profiles given the customer
47
- $api->updateProfiles($customerData);
48
-
49
- // this customer is processed
50
- return true;
51
- }
52
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app/code/community/Copernica/MarketingSoftware/Model/QueueEvent/CustomerFull.php DELETED
@@ -1,242 +0,0 @@
1
- <?php
2
- /**
3
- * Copernica Marketing Software
4
- *
5
- * NOTICE OF LICENSE
6
- *
7
- * This source file is subject to the Open Software License (OSL 3.0).
8
- * It is available through the world-wide-web at this URL:
9
- * http://opensource.org/licenses/osl-3.0.php
10
- * If you are unable to obtain a copy of the license through the
11
- * world-wide-web, please send an email to copernica@support.cream.nl
12
- * so we can send you a copy immediately.
13
- *
14
- * DISCLAIMER
15
- *
16
- * Do not edit or add to this file if you wish to upgrade this software
17
- * to newer versions in the future. If you wish to customize this module
18
- * for your needs please refer to http://www.magento.com/ for more
19
- * information.
20
- *
21
- * @category Copernica
22
- * @package Copernica_MarketingSoftware
23
- * @copyright Copyright (c) 2011-2012 Copernica & Cream. (http://docs.cream.nl/)
24
- * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
- */
26
-
27
- /**
28
- * A wrapper object around an event
29
- */
30
- class Copernica_MarketingSoftware_Model_QueueEvent_CustomerFull extends Copernica_MarketingSoftware_Model_QueueEvent_Abstract
31
- {
32
- /**
33
- * The customer instance
34
- * @var Copernica_MarketingSoftware_Model_Copernica_CustomerProfile
35
- */
36
- private $customer;
37
-
38
- /**
39
- * The target profile Id
40
- * @var int
41
- */
42
- private $profileId;
43
-
44
- /**
45
- * Quotes that were processed.
46
- * @var array
47
- */
48
- private $processedQuotes = array();
49
-
50
- /**
51
- * Process this item in the queue
52
- * @return boolean was the processing successfull
53
- */
54
- public function process()
55
- {
56
- // Get the copernica API
57
- $api = Mage::helper('marketingsoftware/api');
58
- $this->customer = $this->getObject();
59
-
60
- // get customer data
61
- $customerData = $this->getCustomerData();
62
-
63
- // update/create profile
64
- $api->updateProfiles($customerData);
65
-
66
- // get profile Id
67
- $profileId = $api->getProfileId($customerData);
68
-
69
- /*
70
- * It's possible that we will be trying to update a customer that is not
71
- * yet present in copernica database. In such situation we should create
72
- * it's profile so we can use profileId for subprofiles. Thus there is
73
- * no point in waiting till profile is created. Instead we will send
74
- * request to create profile and respawn this event. This way we will not
75
- * be waitning and therefore we will not block other events.
76
- */
77
- if ($profileId === false)
78
- {
79
- // respawn this event with the same data
80
- $this->respawn();
81
-
82
- // we are done here
83
- return true;
84
- }
85
-
86
- // cache profile Id
87
- $this->profileId = $profileId;
88
-
89
- // get request to local scope
90
- $request = Mage::helper('marketingsoftware/RESTRequest');
91
-
92
- // start preparing calls
93
- $request->prepare();
94
-
95
- // update all customer addresses
96
- $this->updateCustomerAddresses();
97
-
98
- // update all customer orders
99
- $this->updateCustomerOrders();
100
-
101
- // update all customers quotes
102
- $this->updateCustomerQuotes();
103
-
104
- // execute all prepared calls
105
- $request->commit();
106
-
107
- // this was processed
108
- return true;
109
- }
110
-
111
- /**
112
- * Get customer data
113
- * @return Copernice_MarketingSoftware_Model_Copernice_ProfileCustomer
114
- */
115
- private function getCustomerData()
116
- {
117
- return Mage::getModel('marketingsoftware/copernica_profilecustomer')
118
- ->setCustomer($this->customer)
119
- ->setDirection('copernica');
120
- }
121
-
122
- /**
123
- * Update all customer addresses
124
- */
125
- private function updateCustomerAddresses()
126
- {
127
- // get Api instance
128
- $api = Mage::helper('marketingsoftware/api');
129
-
130
- // iterate over all addresses
131
- foreach($this->customer->addresses() as $address)
132
- {
133
- $api->updateAddressSubProfiles($this->profileId, $this->getAddressData($address));
134
- }
135
- }
136
-
137
- /**
138
- * Get address data
139
- * @param Copernica_MarketingSoftware_Model_Abstraction_Address
140
- * @return Copernica_MarketingSoftware_Model_Copernice_Address_Subprofile
141
- */
142
- private function getAddressData($address)
143
- {
144
- return Mage::getModel('marketingsoftware/copernica_address_subprofile')
145
- ->setAddress($address)
146
- ->setDirection('copernica');
147
- }
148
-
149
- /**
150
- * Update all customer orders
151
- */
152
- private function updateCustomerOrders()
153
- {
154
- // get Api instance
155
- $api = Mage::helper('marketingsoftware/api');
156
-
157
- // iterate over all orders
158
- foreach ($this->customer->orders() as $order)
159
- {
160
- // update order subprofile
161
- $api->updateOrderSubProfile($this->profileId, $this->getOrderData($order));
162
-
163
- // update all items of current order
164
- $this->updateOrderItems($order);
165
-
166
- // mark quote as processed
167
- $this->processedQuotes[] = $order->quoteId();
168
- }
169
- }
170
-
171
- /**
172
- * Get orderdata
173
- * @param Copernica_MarketingSoftware_Model_Abstraction_Order
174
- */
175
- private function getOrderData($order)
176
- {
177
- return Mage::getModel('marketingsoftware/copernica_order_subprofile')
178
- ->setOrder($order)
179
- ->setDirection('copernica');
180
- }
181
-
182
- /**
183
- * @param Copernice_MarketingSoftware_Model_Copernica_ProfileOrder
184
- */
185
- private function updateOrderItems($order)
186
- {
187
- // get Api instance
188
- $api = Mage::helper('marketingsoftware/api');
189
-
190
- // iterate over all order items
191
- foreach ($order->items() as $item)
192
- {
193
- // update order items
194
- $api->updateOrderItemSubProfiles($this->profileId, $this->getOrderItemData($item));
195
- }
196
- }
197
-
198
- /**
199
- * Get order item data
200
- * @param Copernica_MarketingSoftware_Model_Abstraction_Order_Item
201
- * @return Copernica_MarketingSoftware_Model_Copernica_OrderItem_SubProfile
202
- */
203
- private function getOrderItemData($item)
204
- {
205
- return Mage::getModel('marketingsoftware/copernica_orderitem_subprofile')
206
- ->setOrderItem($item)
207
- ->setDirection('copernica');
208
- }
209
-
210
- /**
211
- * Update customers quotes
212
- */
213
- private function updateCustomerQuotes()
214
- {
215
- // get api instance
216
- $api = Mage::helper('marketingsoftware/api');
217
-
218
- // iterate over all customer quotes
219
- foreach ($this->customer->quotes() as $quote)
220
- {
221
- // if quote was processed we don't want to process it again
222
- if (in_array($quote->id(), $this->processedQuotes)) continue;
223
-
224
- // iterate over all items in quote
225
- foreach ($quote->items() as $item)
226
- {
227
- $api->updateCartItemSubProfiles($this->profileId, $this->getQuoteItemData($item));
228
- }
229
- }
230
- }
231
-
232
- /**
233
- * Get quote item data
234
- */
235
- private function getQuoteItemData($quote)
236
- {
237
- return Mage::getModel('marketingsoftware/copernica_cartitem_subprofile')
238
- ->setDirection('copernica')
239
- ->setStatus('basket')
240
- ->setQuoteItem($quote);
241
- }
242
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app/code/community/Copernica/MarketingSoftware/Model/QueueEvent/CustomerModify.php DELETED
@@ -1,126 +0,0 @@
1
- <?php
2
- /**
3
- * Copernica Marketing Software
4
- *
5
- * NOTICE OF LICENSE
6
- *
7
- * This source file is subject to the Open Software License (OSL 3.0).
8
- * It is available through the world-wide-web at this URL:
9
- * http://opensource.org/licenses/osl-3.0.php
10
- * If you are unable to obtain a copy of the license through the
11
- * world-wide-web, please send an email to copernica@support.cream.nl
12
- * so we can send you a copy immediately.
13
- *
14
- * DISCLAIMER
15
- *
16
- * Do not edit or add to this file if you wish to upgrade this software
17
- * to newer versions in the future. If you wish to customize this module
18
- * for your needs please refer to http://www.magento.com/ for more
19
- * information.
20
- *
21
- * @category Copernica
22
- * @package Copernica_MarketingSoftware
23
- * @copyright Copyright (c) 2011-2012 Copernica & Cream. (http://docs.cream.nl/)
24
- * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
- */
26
-
27
- /**
28
- * A wrapper object around an event
29
- */
30
- class Copernica_MarketingSoftware_Model_QueueEvent_CustomerModify extends Copernica_MarketingSoftware_Model_QueueEvent_Abstract
31
- {
32
- /**
33
- * Customer instance
34
- */
35
- private $customer;
36
-
37
- /**
38
- * Profile Id
39
- * @var int
40
- */
41
- private $profileId;
42
-
43
- /**
44
- * Process this item in the queue
45
- * @return boolean was the processing successfull
46
- */
47
- public function process()
48
- {
49
- /*
50
- * We will need Api instance, customer instance, customer data and
51
- * target profile Id.
52
- */
53
- $api = Mage::helper('marketingsoftware/api');
54
- $this->customer = $this->getObject();
55
- $customerData = $this->getCustomerData();
56
-
57
- // update profiles, this will create a profile if it does not exists
58
- $api->updateProfiles($customerData);
59
-
60
- // get profile Id
61
- $profileId = $api->getProfileId($customerData);
62
-
63
- /*
64
- * It's possible that we will be trying to update a customer that is not
65
- * yet present in copernica database. In such situation we should create
66
- * it's profile so we can use profileId for subprofiles. Thus there is
67
- * no point in waiting till profile is created. Instead we will send
68
- * request to create profile and respawn this event. This way we will not
69
- * be waitning and therefore we will not block other events.
70
- */
71
- if ($profileId === false)
72
- {
73
- // respawn this event with the same data
74
- $this->respawn();
75
-
76
- // we are done here
77
- return true;
78
- }
79
-
80
- // cache profile Id
81
- $this->profileId = $profileId;
82
-
83
- // update customer addresses
84
- $this->updateCustomerAddresses();
85
-
86
- // this customer is processed
87
- return true;
88
- }
89
-
90
- /**
91
- * Get customer data
92
- * @return Copernica_MarketingSoftware_Model_Copernica_ProfileCustomer
93
- */
94
- private function getCustomerData()
95
- {
96
- return Mage::getModel('marketingsoftware/copernica_profilecustomer')
97
- ->setCustomer($this->customer)
98
- ->setDirection('copernica');
99
- }
100
-
101
- /**
102
- * Update all customer addresses
103
- */
104
- private function updateCustomerAddresses()
105
- {
106
- // get Api instance
107
- $api = Mage::helper('marketingsoftware/api');
108
-
109
- // iterate over all addresses
110
- foreach($this->customer->addresses() as $address)
111
- {
112
- $api->updateAddressSubProfiles($this->profileId, $this->getAddressData($address));
113
- }
114
- }
115
-
116
- /**
117
- * Get address data
118
- * @return Copernica_MarketingSoftware_Model_Copernica_Address_Subprofile
119
- */
120
- private function getAddressData($address)
121
- {
122
- return Mage::getModel('marketingsoftware/copernica_address_subprofile')
123
- ->setAddress($address)
124
- ->setDirection('copernica');
125
- }
126
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app/code/community/Copernica/MarketingSoftware/Model/QueueEvent/CustomerRemove.php DELETED
@@ -1,59 +0,0 @@
1
- <?php
2
- /**
3
- * Copernica Marketing Software
4
- *
5
- * NOTICE OF LICENSE
6
- *
7
- * This source file is subject to the Open Software License (OSL 3.0).
8
- * It is available through the world-wide-web at this URL:
9
- * http://opensource.org/licenses/osl-3.0.php
10
- * If you are unable to obtain a copy of the license through the
11
- * world-wide-web, please send an email to copernica@support.cream.nl
12
- * so we can send you a copy immediately.
13
- *
14
- * DISCLAIMER
15
- *
16
- * Do not edit or add to this file if you wish to upgrade this software
17
- * to newer versions in the future. If you wish to customize this module
18
- * for your needs please refer to http://www.magento.com/ for more
19
- * information.
20
- *
21
- * @category Copernica
22
- * @package Copernica_MarketingSoftware
23
- * @copyright Copyright (c) 2011-2012 Copernica & Cream. (http://docs.cream.nl/)
24
- * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
- */
26
-
27
- /**
28
- * A wrapper object around an event
29
- */
30
- class Copernica_MarketingSoftware_Model_QueueEvent_CustomerRemove extends Copernica_MarketingSoftware_Model_QueueEvent_Abstract
31
- {
32
- /**
33
- * Process this item in the queue
34
- * @return boolean was the processing successfull
35
- */
36
- public function process()
37
- {
38
- // get object into local scope
39
- $object = $this->getObject();
40
-
41
- /*
42
- * This bit is kinda silly, but we have to ensure that we have a proper
43
- * object to manipulate. If we don't have such object we will just return
44
- * from this event cause we can not do anything useful.
45
- */
46
- if (!$object->id()) return true;
47
-
48
- // Get the customer
49
- $customerData = Mage::getModel('marketingsoftware/copernica_profilecustomer')
50
- ->setCustomer($object)
51
- ->setDirection('copernica');
52
-
53
- // Remove the profiles given the customer
54
- Mage::helper('marketingsoftware/api')->removeProfiles($customerData);
55
-
56
- // this customer is processed
57
- return true;
58
- }
59
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app/code/community/Copernica/MarketingSoftware/Model/QueueEvent/CustomerUpgrade.php DELETED
@@ -1,89 +0,0 @@
1
- <?php
2
- /**
3
- * Copernica Marketing Software
4
- *
5
- * NOTICE OF LICENSE
6
- *
7
- * This source file is subject to the Open Software License (OSL 3.0).
8
- * It is available through the world-wide-web at this URL:
9
- * http://opensource.org/licenses/osl-3.0.php
10
- * If you are unable to obtain a copy of the license through the
11
- * world-wide-web, please send an email to copernica@support.cream.nl
12
- * so we can send you a copy immediately.
13
- *
14
- * DISCLAIMER
15
- *
16
- * Do not edit or add to this file if you wish to upgrade this software
17
- * to newer versions in the future. If you wish to customize this module
18
- * for your needs please refer to http://www.magento.com/ for more
19
- * information.
20
- *
21
- * @category Copernica
22
- * @package Copernica_MarketingSoftware
23
- * @copyright Copyright (c) 2011-2012 Copernica & Cream. (http://docs.cream.nl/)
24
- * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
- */
26
-
27
- /**
28
- * This event will be responsible for upgrading customers customer_id field
29
- * on Copernica platform.
30
- */
31
- class Copernica_MarketingSoftware_Model_QueueEvent_CustomerUpgrade extends Copernica_MarketingSoftware_Model_QueueEvent_Abstract {
32
- /**
33
- * How many customers will be updated in one run?
34
- * By default it should be 20
35
- * @var
36
- */
37
- private $pageLimit = 1;
38
-
39
- /**
40
- * Process event
41
- * @return boolean
42
- */
43
- public function process() {
44
- // get object with data about current run
45
- $options = $this->getObject();
46
-
47
- // cast start to int
48
- $page = (int)$options->start;
49
-
50
- // get collection that will hold all customers
51
- $customers = Mage::getModel('customer/customer')
52
- ->getCollection()
53
- ->setPageSize($this->pageLimit);
54
-
55
- // load data for given page
56
- $customers->setPage($page, $this->pageLimit)->load();
57
-
58
- /*
59
- * We have to check if current customers collection is empty. It would
60
- * mean that we did convert all customers, so we can just exit this event.
61
- */
62
- if ($customers->count() == 0) return true;
63
-
64
- // iterate over current batch of customers
65
- foreach ($customers as $customer) {
66
- // get our customer object
67
- $object = Mage::getModel('marketingsoftware/abstraction_customer')->loadCustomer($customer->getEntityId());
68
-
69
- /*
70
- * Try to get customer copernica Id. If there is no copernica Id,
71
- * then it will be created for it.
72
- */
73
- Mage::helper('marketingsoftware/profile')->getCustomerCopernicaId($object, $storeView);
74
- }
75
-
76
- // set next page as start for next run
77
- $options->start = ++$page;
78
-
79
- // create next event
80
- Mage::getModel('marketingsoftware/queue')
81
- ->setObject($options)
82
- ->setCustomer(null)
83
- ->setAction('upgrade')
84
- ->save();
85
-
86
- // we are done here
87
- return true;
88
- }
89
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app/code/community/Copernica/MarketingSoftware/Model/QueueEvent/Item.php DELETED
@@ -1,178 +0,0 @@
1
- <?php
2
- /**
3
- * Copernica Marketing Software
4
- *
5
- * NOTICE OF LICENSE
6
- *
7
- * This source file is subject to the Open Software License (OSL 3.0).
8
- * It is available through the world-wide-web at this URL:
9
- * http://opensource.org/licenses/osl-3.0.php
10
- * If you are unable to obtain a copy of the license through the
11
- * world-wide-web, please send an email to copernica@support.cream.nl
12
- * so we can send you a copy immediately.
13
- *
14
- * DISCLAIMER
15
- *
16
- * Do not edit or add to this file if you wish to upgrade this software
17
- * to newer versions in the future. If you wish to customize this module
18
- * for your needs please refer to http://www.magento.com/ for more
19
- * information.
20
- *
21
- * @category Copernica
22
- * @package Copernica_MarketingSoftware
23
- * @copyright Copyright (c) 2011-2012 Copernica & Cream. (http://docs.cream.nl/)
24
- * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
- */
26
-
27
- /**
28
- * This class will take care of syncing items
29
- */
30
- class Copernica_MarketingSoftware_Model_QueueEvent_Item extends Copernica_MarketingSoftware_Model_QueueEvent_Abstract
31
- {
32
- /**
33
- * Process modify action
34
- * @return boolean
35
- */
36
- public function actionModify()
37
- {
38
- // get object
39
- $object = $this->getObject();
40
-
41
- // check if we should sync a quote
42
- if (property_exists($object, 'quote')) $this->syncQuote();
43
-
44
- /*
45
- * We could have a situation when customer is editing an abandoned cart.
46
- * In such situation we have to revive that cart.
47
- */
48
-
49
- $collection = Mage::getModel('marketingsoftware/abandonedCart')->getCollection()->addFieldToFilter('quote_id', $object->quote);
50
- $abandonedCart = $collection->getFirstItem();
51
-
52
- // check if we have already abandoned cart or we just created a new model
53
- if ($abandonedCart->isObjectNew()) return true;
54
-
55
- // remove marker
56
- $abandonedCart->delete();
57
-
58
- // create event that will sync quote items and change cart status from
59
- // abandoned to basket
60
- $queue = Mage::getModel('marketingsoftware/queue')
61
- ->setObject()
62
- ->setCustomer($this->getCustomerId())
63
- ->setAction('modify')
64
- ->setName('quote')
65
- ->setEntityId($object->quote)
66
- ->save();
67
-
68
- // we are done here
69
- return true;
70
- }
71
-
72
- /**
73
- * Process add action.
74
- * @return boolean
75
- */
76
- public function actionAdd()
77
- {
78
- return $this->actionModify();
79
- }
80
-
81
- /**
82
- * Process remove action
83
- * @return boolean
84
- */
85
- public function actionRemove()
86
- {
87
- // get object into local scope
88
- $object = $this->getObject();
89
-
90
- // check if we have a quote to play with
91
- if (!property_exists($object, 'quote')) return false;
92
-
93
- // bring request into local scope
94
- $request = Mage::helper('marketingsoftware/RESTRequest');
95
-
96
- // get item Id and quote Id
97
- $itemId = $this->getEntityId();
98
- $quoteId = $object->quote;
99
-
100
- // get customer
101
- $customer = new Copernica_MarketingSoftware_Model_Copernica_Entity_Customer($object->customer);
102
-
103
- // if we don't have a customer we did something wrong
104
- if (($profileId = $customer->getProfileId()) === false) return false;
105
-
106
- // get cart item collection Id
107
- $cartItemCollectionId = Mage::helper('marketingsoftware/config')->getCartItemsCollectionId();
108
-
109
- // get item cart that we want to remove
110
- $response = $request->get('/profile/'.$profileId.'/subprofiles/'.$cartItemCollectionId, array(
111
- 'quote_id' => $quoteId,
112
- 'item_id' => $itemId
113
- ));
114
-
115
- // check if we have to create a subprofile that will have deleted status
116
- if (!array_key_exists('data', $response) || count($response['data']) == 0) {
117
-
118
- if (!Mage::helper('marketingsoftware/config')->getRemoveFinishedCartItems())
119
- $request->post('/profile/'.$profileId.'/subprofiles/'.$cartItemCollectionId, get_object_vars($object->item));
120
-
121
- return true;
122
- }
123
-
124
- /*
125
- * User can decide to remove subprofiles of items that were removed by
126
- * customer.
127
- */
128
- if (Mage::helper('marketingsoftware/config')->getRemoveFinishedCartItems())
129
- {
130
- foreach ($response['data'] as $subprofile) $request->delete('/subprofile/'.$subprofile['ID']);
131
- }
132
-
133
- // mark item as deleted
134
- else {
135
- $request->put('/profile/'.$profileId.'/subprofiles/'.$cartItemCollectionId, array(
136
- 'status' => 'deleted'
137
- ), array (
138
- 'fields' => array(
139
- 'quote_id=='.$quoteId,
140
- 'item_id=='.$itemId
141
- )
142
- ));
143
- }
144
-
145
- // we are just fine here
146
- return true;
147
- }
148
-
149
- /**
150
- * Sync item with quote
151
- */
152
- public function syncQuote()
153
- {
154
- // get item
155
- $item = Mage::getModel('sales/quote_item')->load($this->getEntityId());
156
-
157
- /**
158
- * In some cases this can still be executed even when item was removed.
159
- * So we prefere to check if we just fetched new item.
160
- */
161
- if ($item->isObjectNew()) return;
162
-
163
- // create copernica entity
164
- $item = new Copernica_MarketingSoftware_Model_Copernica_Entity_CartItem($item);
165
-
166
- // get event object
167
- $object = $this->getObject();
168
-
169
- // get customer
170
- $customer = new Copernica_MarketingSoftware_Model_Copernica_Entity_Customer($object->customer);
171
-
172
- // load quote
173
- $quote = Mage::getModel('sales/quote')->loadByCustomer($object->customer);
174
-
175
- // sync item with quote
176
- $item->getREST()->syncWithQuote($customer, $quote->getId());
177
- }
178
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app/code/community/Copernica/MarketingSoftware/Model/QueueEvent/Order.php DELETED
@@ -1,167 +0,0 @@
1
- <?php
2
- /**
3
- * Copernica Marketing Software
4
- *
5
- * NOTICE OF LICENSE
6
- *
7
- * This source file is subject to the Open Software License (OSL 3.0).
8
- * It is available through the world-wide-web at this URL:
9
- * http://opensource.org/licenses/osl-3.0.php
10
- * If you are unable to obtain a copy of the license through the
11
- * world-wide-web, please send an email to copernica@support.cream.nl
12
- * so we can send you a copy immediately.
13
- *
14
- * DISCLAIMER
15
- *
16
- * Do not edit or add to this file if you wish to upgrade this software
17
- * to newer versions in the future. If you wish to customize this module
18
- * for your needs please refer to http://www.magento.com/ for more
19
- * information.
20
- *
21
- * @category Copernica
22
- * @package Copernica_MarketingSoftware
23
- * @copyright Copyright (c) 2011-2012 Copernica & Cream. (http://docs.cream.nl/)
24
- * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
- */
26
-
27
- /**
28
- * This clas will take care of all events associated with order
29
- */
30
- class Copernica_MarketingSoftware_Model_QueueEvent_Order extends Copernica_MarketingSoftware_Model_QueueEvent_Abstract
31
- {
32
- /**
33
- * Customer entity
34
- * @var Copernica_MarketingSoftware_Model_Copernica_Entity_Customer
35
- */
36
- private $customer = null;
37
-
38
- /**
39
- * This action will be run on order modify event
40
- * @return boolean
41
- */
42
- public function actionModify()
43
- {
44
- // get data object associated with this event
45
- $object = $this->getObject();
46
-
47
- // get vanilla magento order model
48
- $vanillaOrder = Mage::getModel('sales/order')->load($this->getEntityId());
49
-
50
- // create extension representation of an order
51
- $order = new Copernica_MarketingSoftware_Model_Copernica_Entity_Order($vanillaOrder);
52
-
53
- // check if we have customer Id inside data object
54
- if (property_exists($object, 'customer') && is_numeric($object->customer))
55
- {
56
- // create customer entity
57
- $customer = new Copernica_MarketingSoftware_Model_Copernica_Entity_Customer($object->customer);
58
-
59
- // sync order with customer
60
- return $order->getREST()->syncWithCustomer($customer);
61
- }
62
-
63
- // the order is anonymous
64
- else
65
- {
66
- // data that should be always accessible
67
- $data = array (
68
- 'email' => $vanillaOrder->getCustomerEmail(),
69
- 'storeView' => $order->getStoreView(),
70
- 'storeViewId' => $vanillaOrder->getStoreId(),
71
- 'firstname' => $vanillaOrder->getCustomerFirstname(),
72
- 'lastname' => $vanillaOrder->getCustomerLastname(),
73
- );
74
-
75
- // assign middlename if we have middlename
76
- if ($middlename = $vanillaOrder->getCustomerMiddlename()) $data['middlename'] = $middlename;
77
- if ($dayOfBirth = $vanillaOrder->getCustomerDob()) $data['birthdate'] = $dateOfBirth;
78
-
79
- // get group
80
- $group = $vanillaOrder->getCustomerGroupId();
81
- $group = Mage::getModel('customer/group')->load($group)->getCode();
82
-
83
- // assign group code
84
- $data['group'] = $group;
85
-
86
- // sync with guest data
87
- return $order->getREST()->syncWithGuest($data);
88
- }
89
- }
90
-
91
- /**
92
- * This action will be run on order add event
93
- * @return boolean
94
- */
95
- public function actionAdd()
96
- {
97
- // we can call modify action. That will create proper order
98
- $this->actionModify();
99
-
100
- /*
101
- * We should also remove all cart items from profile, since customer
102
- * did checkout with his cart items are no longer considered as ones in
103
- * cart but instead, they were synced with order and are considered
104
- * bought items.
105
- */
106
-
107
- // we will need order model for a second
108
- $order = Mage::getModel('sales/order')->load($this->getEntityId());
109
-
110
- // get object into local scope
111
- $object = $this->getObject();
112
-
113
- // get customer
114
- $customer = $this->getCustomer();
115
-
116
- // if we don't have a customer then it's ok. That mean the order is a
117
- // guest order, so quote was never synced and thanks to that we don't have
118
- // to care about cart items collection
119
- if (!in_object($customer)) return true;
120
-
121
- // get request into local scope
122
- $request = Mage::helper('marketingsoftware/RESTRequest');
123
-
124
- // get cart items collection
125
- $cartItemsCollection = Mage::helper('marketingsoftware/config')->getCartItemsCollectionId();
126
-
127
- // get profiles data
128
- if ($cartItemsCollection) $response = $request->get('/profile/'.$customer->getProfileId().'/subprofiles/'.$cartItemsCollection, array(
129
- 'fields' => array('quote_id=='.$order->getQuoteId())
130
- ));
131
-
132
- // check if we have data to play with
133
- if (array_key_exists('data', $response) || count($response['data']) == 0) return true;
134
-
135
- // prepare multi interface
136
- $request->prepare();
137
-
138
- /*
139
- * User could decided that he want to keep information about removed
140
- * items inside profile (and mark them as 'deleted') or remove subprofiles
141
- * of ordered items.
142
- */
143
- if (Mage::helper('marketingsoftware/config')->getRemoveFinishedCartItems()){
144
- foreach ($response['data'] as $subprofile) $request->delete('/subprofile/'.$subprofile['ID']);
145
- }
146
- else {
147
- foreach ($response['data'] as $subprofile) $request->put('/subprofile/'.$subprofile['ID'].'/fields/', array('status' => 'completed'));
148
- }
149
-
150
- // submit all changes to copernica platform
151
- $request->commit();
152
-
153
- // we are just ok here
154
- return true;
155
- }
156
-
157
- /**
158
- * Get customer
159
- * @return Copernica_MarketingSoftware_Model_Copernica_Entity_Customer
160
- */
161
- private function getCustomer()
162
- {
163
- if (!is_null($this->customer)) return $this->customer;
164
-
165
- return $this->customer = new Copernica_MarketingSoftware_Model_Copernica_Entity_Customer($object->customer);
166
- }
167
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app/code/community/Copernica/MarketingSoftware/Model/QueueEvent/OrderAdd.php DELETED
@@ -1,35 +0,0 @@
1
- <?php
2
- /**
3
- * Copernica Marketing Software
4
- *
5
- * NOTICE OF LICENSE
6
- *
7
- * This source file is subject to the Open Software License (OSL 3.0).
8
- * It is available through the world-wide-web at this URL:
9
- * http://opensource.org/licenses/osl-3.0.php
10
- * If you are unable to obtain a copy of the license through the
11
- * world-wide-web, please send an email to copernica@support.cream.nl
12
- * so we can send you a copy immediately.
13
- *
14
- * DISCLAIMER
15
- *
16
- * Do not edit or add to this file if you wish to upgrade this software
17
- * to newer versions in the future. If you wish to customize this module
18
- * for your needs please refer to http://www.magento.com/ for more
19
- * information.
20
- *
21
- * @category Copernica
22
- * @package Copernica_MarketingSoftware
23
- * @copyright Copyright (c) 2011-2012 Copernica & Cream. (http://docs.cream.nl/)
24
- * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
- */
26
-
27
- /**
28
- * A wrapper object around an event
29
- */
30
- class Copernica_MarketingSoftware_Model_QueueEvent_OrderAdd extends Copernica_MarketingSoftware_Model_QueueEvent_OrderModify
31
- {
32
- /**
33
- * Process this item in the same way as the modification of an order
34
- */
35
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app/code/community/Copernica/MarketingSoftware/Model/QueueEvent/OrderModify.php DELETED
@@ -1,171 +0,0 @@
1
- <?php
2
- /**
3
- * Copernica Marketing Software
4
- *
5
- * NOTICE OF LICENSE
6
- *
7
- * This source file is subject to the Open Software License (OSL 3.0).
8
- * It is available through the world-wide-web at this URL:
9
- * http://opensource.org/licenses/osl-3.0.php
10
- * If you are unable to obtain a copy of the license through the
11
- * world-wide-web, please send an email to copernica@support.cream.nl
12
- * so we can send you a copy immediately.
13
- *
14
- * DISCLAIMER
15
- *
16
- * Do not edit or add to this file if you wish to upgrade this software
17
- * to newer versions in the future. If you wish to customize this module
18
- * for your needs please refer to http://www.magento.com/ for more
19
- * information.
20
- *
21
- * @category Copernica
22
- * @package Copernica_MarketingSoftware
23
- * @copyright Copyright (c) 2011-2012 Copernica & Cream. (http://docs.cream.nl/)
24
- * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
- */
26
-
27
- /**
28
- * A wrapper object around an event
29
- */
30
- class Copernica_MarketingSoftware_Model_QueueEvent_OrderModify extends Copernica_MarketingSoftware_Model_QueueEvent_Abstract
31
- {
32
- /**
33
- * Profile Id on copernica platform that will be updated
34
- * @var int
35
- */
36
- private $profileId;
37
-
38
- /**
39
- * Magento order
40
- */
41
- private $order;
42
-
43
- /**
44
- * Process this item in the queue
45
- * @return boolean was the processing successfull
46
- */
47
- public function process()
48
- {
49
- /*
50
- * We need to make some preparations. We will need Api, order instance,
51
- * customer and target profile Id.
52
- */
53
- $api = Mage::helper('marketingsoftware/api');
54
- $this->order = $this->getObject();
55
- $customerData = $this->getCustomerData();
56
-
57
- // update profiles, this will create a profile if it does not exists
58
- $api->updateProfiles($customerData);
59
-
60
- // get profile Id
61
- $profileId = $api->getProfileId($customerData);
62
-
63
- /*
64
- * It's possible that we will be trying to update a customer that is not
65
- * yet present in copernica database. In such situation we should create
66
- * it's profile so we can use profileId for subprofiles. Thus there is
67
- * no point in waiting till profile is created. Instead we will send
68
- * request to create profile and respawn this event. This way we will not
69
- * be waitning and therefore we will not block other events.
70
- */
71
- if ($profileId === false)
72
- {
73
- // respawn this event with the same data
74
- $this->respawn();
75
-
76
- // we are done here
77
- return true;
78
- }
79
-
80
- // cache profile Id
81
- $this->profileId = $profileId;
82
-
83
- // remove old cart items
84
- $api->removeOldCartItems($this->profileId, $this->order->quoteId());
85
-
86
- // get REST request
87
- $request = Mage::helper('marketingsoftware/RESTrequest');
88
-
89
- // start preparing calls
90
- $request->prepare();
91
-
92
- // update order subprofile with new info
93
- $api->updateOrderSubProfile($this->profileId, $this->getOrderData());
94
-
95
- // update order items
96
- $this->updateOrderItems();
97
-
98
- // update order addresses
99
- $this->updateOrderAddresses();
100
-
101
- // commit changes to API server
102
- $request->commit();
103
-
104
- // we are good
105
- return true;
106
- }
107
-
108
- /**
109
- * Get order data
110
- * @return Copernica_MarketingSoftware_Model_Copernica_Order_Subprofile
111
- */
112
- private function getOrderData()
113
- {
114
- // get order subprofile
115
- $orderData = Mage::getModel('marketingsoftware/copernica_order_subprofile');
116
- $orderData->setOrder($this->order)->setDirection('copernica');
117
-
118
- // return order data
119
- return $orderData;
120
- }
121
-
122
- /**
123
- * Get customer data
124
- * @return Copernica_MarketingSoftware_Model_Copernica_ProfileCustomer|Copernica_MarketingSoftware_Model_Copernica_ProfileOrder
125
- */
126
- private function getCustomerData()
127
- {
128
- // try to get customer instance from order
129
- $customer = $this->order->customer();
130
-
131
- // if we have a customer we want to return customer profile
132
- if (is_object($customer)) return Mage::getModel('marketingsoftware/copernica_profilecustomer')->setCustomer($customer)->setDirection('copernica');
133
-
134
- // we will just return order profile
135
- return Mage::getModel('marketingsoftware/copernica_profileorder')->setOrder($this->order)->setDirection('copernica');
136
- }
137
-
138
- /**
139
- * Update order items
140
- */
141
- private function updateOrderItems()
142
- {
143
- // update all order items
144
- foreach ($this->order->items() as $orderItem)
145
- {
146
- // get information about current order item
147
- $itemData = Mage::getModel('marketingsoftware/copernica_orderitem_subprofile');
148
- $itemData->setOrderItem($orderItem)->setDirection('copernica');
149
-
150
- // update order item subprofile
151
- Mage::helper('marketingsoftware/api')->updateOrderItemSubProfiles($this->profileId, $itemData);
152
- }
153
- }
154
-
155
- /**
156
- * Update order addresses
157
- */
158
- private function updateOrderAddresses()
159
- {
160
- // update all order addresses
161
- foreach ($this->order->addresses() as $address)
162
- {
163
- // get information about current address
164
- $addressData = Mage::getModel('marketingsoftware/copernica_address_subprofile');
165
- $addressData->setAddress($address)->setDirection('copernica');
166
-
167
- // update address subprofile
168
- Mage::helper('marketingsoftware/api')->updateAddressSubProfiles($this->profileId, $addressData);
169
- }
170
- }
171
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app/code/community/Copernica/MarketingSoftware/Model/QueueEvent/QuoteItem.php DELETED
@@ -1,124 +0,0 @@
1
- <?php
2
- /**
3
- * Copernica Marketing Software
4
- *
5
- * NOTICE OF LICENSE
6
- *
7
- * This source file is subject to the Open Software License (OSL 3.0).
8
- * It is available through the world-wide-web at this URL:
9
- * http://opensource.org/licenses/osl-3.0.php
10
- * If you are unable to obtain a copy of the license through the
11
- * world-wide-web, please send an email to copernica@support.cream.nl
12
- * so we can send you a copy immediately.
13
- *
14
- * DISCLAIMER
15
- *
16
- * Do not edit or add to this file if you wish to upgrade this software
17
- * to newer versions in the future. If you wish to customize this module
18
- * for your needs please refer to http://www.magento.com/ for more
19
- * information.
20
- *
21
- * @category Copernica
22
- * @package Copernica_MarketingSoftware
23
- * @copyright Copyright (c) 2011-2012 Copernica & Cream. (http://docs.cream.nl/)
24
- * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
- */
26
-
27
- /**
28
- * A wrapper object around an event
29
- */
30
- abstract class Copernica_MarketingSoftware_Model_QueueEvent_QuoteItem extends Copernica_MarketingSoftware_Model_QueueEvent_Abstract
31
- {
32
- /**
33
- * Quote Item
34
- */
35
- private $quoteItem;
36
-
37
- /**
38
- * Process this item in the queue
39
- * @return boolean was the processing successfull
40
- */
41
- public function process()
42
- {
43
- // Get the copernica API and config helper
44
- $api = Mage::helper('marketingsoftware/api');
45
-
46
- // Get the subscription which has been modified
47
- $this->quoteItem = $this->getObject();
48
-
49
- // Get the customer
50
- $customerData = $this->getCustomerData();
51
-
52
- // if we don't have customer data we don't really care about this item
53
- if ($customerData === false) return true;
54
-
55
- // get profile Id
56
- $profileId = $api->getProfileId($customerData);
57
-
58
- // Get the profiles from the api
59
- $api->updateProfiles($customerData);
60
-
61
- /*
62
- * It's possible that we will be trying to update a customer that is not
63
- * yet present in copernica database. In such situation we should create
64
- * it's profile so we can use profileId for subprofiles. Thus there is
65
- * no point in waiting till profile is created. Instead we will send
66
- * request to create profile and respawn this event. This way we will not
67
- * be waitning and therefore we will not block other events.
68
- */
69
- if ($profileId === false)
70
- {
71
- // respawn this event with the same data
72
- $this->respawn();
73
-
74
- // we are done here
75
- return true;
76
- }
77
-
78
- // update cart item subprofile
79
- $api->updateCartItemSubProfiles($profileId, $this->getCartItemData());
80
-
81
- // all went allright
82
- return true;
83
- }
84
-
85
- /**
86
- * Get customer data
87
- * @return Copernica_MarketingSoftware_Model_Copernica_ProfileCustomer
88
- */
89
- private function getCustomerData()
90
- {
91
- // get and prepare customer data
92
- $customerData = Mage::getModel('marketingsoftware/copernica_profilecustomer');
93
-
94
- // get the customer from the quote
95
- $customer = $this->quoteItem->quote()->customer();
96
-
97
- // if we don't have customer we can not return customer data (that makes sense...)
98
- if (!is_object($customer)) return false;
99
-
100
- // set customer and direction
101
- $customerData->setCustomer($customer)->setDirection('copernica');
102
-
103
- // return customer data
104
- return $customerData;
105
- }
106
-
107
- /**
108
- * Get cart item data
109
- * @return Copernica_MarketingSoftware_Model_Copernica_CartItemSubprofile
110
- */
111
- private function getCartItemData()
112
- {
113
- return Mage::getModel('marketingsoftware/copernica_cartitem_subprofile')
114
- ->setQuoteItem($this->quoteItem)
115
- ->setDirection('copernica')
116
- ->setStatus($this->status());
117
- }
118
-
119
- /**
120
- * In what status is this cart item
121
- * @return String
122
- */
123
- abstract protected function status();
124
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app/code/community/Copernica/MarketingSoftware/Model/QueueEvent/QuoteItemModify.php DELETED
@@ -1,41 +0,0 @@
1
- <?php
2
- /**
3
- * Copernica Marketing Software
4
- *
5
- * NOTICE OF LICENSE
6
- *
7
- * This source file is subject to the Open Software License (OSL 3.0).
8
- * It is available through the world-wide-web at this URL:
9
- * http://opensource.org/licenses/osl-3.0.php
10
- * If you are unable to obtain a copy of the license through the
11
- * world-wide-web, please send an email to copernica@support.cream.nl
12
- * so we can send you a copy immediately.
13
- *
14
- * DISCLAIMER
15
- *
16
- * Do not edit or add to this file if you wish to upgrade this software
17
- * to newer versions in the future. If you wish to customize this module
18
- * for your needs please refer to http://www.magento.com/ for more
19
- * information.
20
- *
21
- * @category Copernica
22
- * @package Copernica_MarketingSoftware
23
- * @copyright Copyright (c) 2011-2012 Copernica & Cream. (http://docs.cream.nl/)
24
- * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
- */
26
-
27
- /**
28
- * A wrapper object around an event
29
- */
30
- class Copernica_MarketingSoftware_Model_QueueEvent_QuoteItemModify extends Copernica_MarketingSoftware_Model_QueueEvent_QuoteItem
31
- {
32
- /**
33
- * In what status is this cart item
34
- * @return String
35
- */
36
- protected function status()
37
- {
38
- return 'basket';
39
- }
40
-
41
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app/code/community/Copernica/MarketingSoftware/Model/QueueEvent/QuoteItemRemove.php DELETED
@@ -1,41 +0,0 @@
1
- <?php
2
- /**
3
- * Copernica Marketing Software
4
- *
5
- * NOTICE OF LICENSE
6
- *
7
- * This source file is subject to the Open Software License (OSL 3.0).
8
- * It is available through the world-wide-web at this URL:
9
- * http://opensource.org/licenses/osl-3.0.php
10
- * If you are unable to obtain a copy of the license through the
11
- * world-wide-web, please send an email to copernica@support.cream.nl
12
- * so we can send you a copy immediately.
13
- *
14
- * DISCLAIMER
15
- *
16
- * Do not edit or add to this file if you wish to upgrade this software
17
- * to newer versions in the future. If you wish to customize this module
18
- * for your needs please refer to http://www.magento.com/ for more
19
- * information.
20
- *
21
- * @category Copernica
22
- * @package Copernica_MarketingSoftware
23
- * @copyright Copyright (c) 2011-2012 Copernica & Cream. (http://docs.cream.nl/)
24
- * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
- */
26
-
27
- /**
28
- * A wrapper object around an event
29
- */
30
- class Copernica_MarketingSoftware_Model_QueueEvent_QuoteItemRemove extends Copernica_MarketingSoftware_Model_QueueEvent_QuoteItem
31
- {
32
- /**
33
- * In what status is this cart item
34
- * @return String
35
- */
36
- protected function status()
37
- {
38
- return 'deleted';
39
- }
40
-
41
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app/code/community/Copernica/MarketingSoftware/Model/QueueEvent/QuoteModify.php DELETED
@@ -1,132 +0,0 @@
1
- <?php
2
- /**
3
- * Copernica Marketing Software
4
- *
5
- * NOTICE OF LICENSE
6
- *
7
- * This source file is subject to the Open Software License (OSL 3.0).
8
- * It is available through the world-wide-web at this URL:
9
- * http://opensource.org/licenses/osl-3.0.php
10
- * If you are unable to obtain a copy of the license through the
11
- * world-wide-web, please send an email to copernica@support.cream.nl
12
- * so we can send you a copy immediately.
13
- *
14
- * DISCLAIMER
15
- *
16
- * Do not edit or add to this file if you wish to upgrade this software
17
- * to newer versions in the future. If you wish to customize this module
18
- * for your needs please refer to http://www.magento.com/ for more
19
- * information.
20
- *
21
- * @category Copernica
22
- * @package Copernica_MarketingSoftware
23
- * @copyright Copyright (c) 2011-2012 Copernica & Cream. (http://docs.cream.nl/)
24
- * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
- */
26
-
27
- /**
28
- * A wrapper object around an event
29
- */
30
- class Copernica_MarketingSoftware_Model_QueueEvent_QuoteModify extends Copernica_MarketingSoftware_Model_QueueEvent_Abstract
31
- {
32
- /**
33
- * Quote object
34
- */
35
- private $quote;
36
-
37
- /**
38
- * Id of a profile in copernica env
39
- * @var int
40
- */
41
- private $profileId;
42
-
43
- /**
44
- * Process this item in the queue
45
- * @return boolean was the processing successfull
46
- */
47
- public function process()
48
- {
49
- /*
50
- * We will need Api instnace, quote, customer data and target profile Id
51
- */
52
- $api = Mage::helper('marketingsoftware/api');
53
- $this->quote = $this->getObject();
54
- $customerData = $this->getCustomerData();
55
-
56
- // update profiles, this will create a profile if it does not exists
57
- $api->updateProfiles($customerData);
58
-
59
- // get profile Id
60
- $profileId = $api->getProfileId($customerData);
61
-
62
- /*
63
- * It's possible that we will be trying to update a customer that is not
64
- * yet present in copernica database. In such situation we should create
65
- * it's profile so we can use profileId for subprofiles. Thus there is
66
- * no point in waiting till profile is created. Instead we will send
67
- * request to create profile and respawn this event. This way we will not
68
- * be waitning and therefore we will not block other events.
69
- */
70
- if ($profileId === false)
71
- {
72
-
73
- // respawn this event with the same data
74
- $this->respawn();
75
-
76
- // we are done here
77
- return true;
78
- }
79
-
80
- // cache profile Id
81
- $this->profileId = $profileId;
82
-
83
- // update quote items
84
- $this->updateQuoteItems();
85
-
86
- // This quote was successfully synchronized
87
- return true;
88
- }
89
-
90
- /**
91
- * Get customer data
92
- * @return Copernica_MarketingSoftware_Model_Copernica_ProfileQuote|Copernica_MarketingSoftware_Model_Copernica_ProfileCustomer
93
- */
94
- private function getCustomerData()
95
- {
96
- // get customer
97
- $customer = $this->quote->customer();
98
-
99
- // check if we have real customer instance
100
- if (!is_object($customer)) return Mage::getModel('marketingsoftware/copernica_profilequote')->setQuote($this->quote)->setDirection('copernica');
101
-
102
- // return customer data
103
- return Mage::getModel('marketingsoftware/copernica_profilecustomer')->setCustomer($customer)->setDirection('copernica');
104
- }
105
-
106
- /**
107
- * Update all quote items
108
- */
109
- private function updateQuoteItems()
110
- {
111
- // get Api instance
112
- $api = Mage::helper('marketingsoftware/api');
113
-
114
- // iterate over all quote items
115
- foreach ($this->quote->items() as $item)
116
- {
117
- // update item subprofile
118
- $api->updateCartItemSubProfiles($this->profileId, $this->getQuoteItemData($item));
119
- }
120
- }
121
-
122
- /**
123
- * Get quote item data
124
- */
125
- private function getQuoteItemData($quoteItem)
126
- {
127
- return Mage::getModel('marketingsoftware/copernica_cartitem_subprofile')
128
- ->setQuoteItem($quoteItem)
129
- ->setDirection('copernica')
130
- ->setStatus('basket');
131
- }
132
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app/code/community/Copernica/MarketingSoftware/Model/QueueEvent/Subscription.php DELETED
@@ -1,102 +0,0 @@
1
- <?php
2
-
3
- /**
4
- * This event should take care of any action done on subscription.
5
- */
6
- class Copernica_MarketingSoftware_Model_QueueEvent_Subscription extends Copernica_MarketingSoftware_Model_QueueEvent_Abstract
7
- {
8
- /**
9
- * Handle add action
10
- * @return boolean
11
- */
12
- public function actionAdd()
13
- {
14
- return $this->actionModify();
15
- }
16
-
17
- /**
18
- * Handle remove action
19
- * @return boolean
20
- */
21
- public function actionRemove()
22
- {
23
- // get object
24
- $object = $this->getObject();
25
-
26
- // get profile linked fields
27
- $profileLinkedFields = Mage::helper('marketingsoftware/config')->getLinkedCustomerFields();
28
-
29
- // if properties don't exists we can not do anything useful
30
- if (!property_exists($object, 'email') && !property_exists($object, 'store_id')) return false;
31
-
32
- // get objects related to store
33
- $store = Mage::getModel('core/store')->load($object->store_id);
34
- $website = $store->getWebsite();
35
- $group = $store->getGroup();
36
-
37
- // construct store view identifier
38
- $storeView = implode(' > ', array (
39
- $website->getName(),
40
- $group->getName(),
41
- $store->getName())
42
- );
43
-
44
- // try to get a profile by email+store_view combination
45
- $profileCacheCollection = Mage::getModel('marketingsoftware/profileCache')
46
- ->getCollection()
47
- ->setPageSize(1)
48
- ->addFieldToFilter('email', $object->email)
49
- ->addFieldToFilter('store_view', $storeView);
50
-
51
- // get 1st item
52
- $profileCache = $profileCacheCollection->getFirstItem();
53
-
54
- // we don't need profile cache for subscriber that we just removed
55
- if (!$profileCache->isObjectNew()) $profileCache->delete();
56
-
57
- // bring request into local scope
58
- $request = Mage::helper('marketingsoftware/RESTRequest');
59
-
60
- // get database if
61
- $databaseId = Mage::helper('marketingsoftware/config')->getDatabaseId();
62
-
63
- // get profiles that match email + store view combination, and remove them
64
- $result = $request->get('/database/'.$databaseId.'/profiles', array('fields' => array(
65
- $profileLinkedFields['email'].'=='.$object->email,
66
- $profileLinkedFields['storeView'].'=='.$storeView
67
- )));
68
-
69
- // if we don't have anything to remove we are just fine
70
- if (!isset($result['total']) && $result['total'] == 0) return false;
71
-
72
- // remove all profile that match email + store view
73
- foreach ($result['data'] as $profile) $request->delete('/profile/'.$profile['ID']);
74
-
75
- // we are done here
76
- return true;
77
- }
78
-
79
- /**
80
- * Handle modify action
81
- * @return boolean
82
- */
83
- public function actionModify()
84
- {
85
- // get magento subscriber model
86
- $subscriber = Mage::getModel('newsletter/subscriber')->load($this->getEntityId());
87
-
88
- // we want to check if subscriber is still present in magento
89
- // most likely if there is no subscriber that we can fetch we have already
90
- // a remove event that will remove that subscriber from the copernica
91
- if ($subscriber->isObjectNew()) return true;
92
-
93
- // construct subscription entity
94
- $subscription = new Copernica_MarketingSoftware_Model_Copernica_Entity_Subscription($subscriber);
95
-
96
- // sync subscription
97
- $subscription->getREST()->sync();
98
-
99
- // we should be just fine here
100
- return true;
101
- }
102
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app/code/community/Copernica/MarketingSoftware/Model/QueueEvent/SubscriptionAdd.php DELETED
@@ -1,35 +0,0 @@
1
- <?php
2
- /**
3
- * Copernica Marketing Software
4
- *
5
- * NOTICE OF LICENSE
6
- *
7
- * This source file is subject to the Open Software License (OSL 3.0).
8
- * It is available through the world-wide-web at this URL:
9
- * http://opensource.org/licenses/osl-3.0.php
10
- * If you are unable to obtain a copy of the license through the
11
- * world-wide-web, please send an email to copernica@support.cream.nl
12
- * so we can send you a copy immediately.
13
- *
14
- * DISCLAIMER
15
- *
16
- * Do not edit or add to this file if you wish to upgrade this software
17
- * to newer versions in the future. If you wish to customize this module
18
- * for your needs please refer to http://www.magento.com/ for more
19
- * information.
20
- *
21
- * @category Copernica
22
- * @package Copernica_MarketingSoftware
23
- * @copyright Copyright (c) 2011-2012 Copernica & Cream. (http://docs.cream.nl/)
24
- * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
- */
26
-
27
- /**
28
- * A wrapper object around an event
29
- */
30
- class Copernica_MarketingSoftware_Model_QueueEvent_SubscriptionAdd extends Copernica_MarketingSoftware_Model_QueueEvent_SubscriptionModify
31
- {
32
- /**
33
- * Process this item in the queue the behaviour is the same is for a modification
34
- */
35
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app/code/community/Copernica/MarketingSoftware/Model/QueueEvent/SubscriptionModify.php DELETED
@@ -1,73 +0,0 @@
1
- <?php
2
- /**
3
- * Copernica Marketing Software
4
- *
5
- * NOTICE OF LICENSE
6
- *
7
- * This source file is subject to the Open Software License (OSL 3.0).
8
- * It is available through the world-wide-web at this URL:
9
- * http://opensource.org/licenses/osl-3.0.php
10
- * If you are unable to obtain a copy of the license through the
11
- * world-wide-web, please send an email to copernica@support.cream.nl
12
- * so we can send you a copy immediately.
13
- *
14
- * DISCLAIMER
15
- *
16
- * Do not edit or add to this file if you wish to upgrade this software
17
- * to newer versions in the future. If you wish to customize this module
18
- * for your needs please refer to http://www.magento.com/ for more
19
- * information.
20
- *
21
- * @category Copernica
22
- * @package Copernica_MarketingSoftware
23
- * @copyright Copyright (c) 2011-2012 Copernica & Cream. (http://docs.cream.nl/)
24
- * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
- */
26
-
27
- /**
28
- * A wrapper object around an event
29
- */
30
- class Copernica_MarketingSoftware_Model_QueueEvent_SubscriptionModify extends Copernica_MarketingSoftware_Model_QueueEvent_Abstract
31
- {
32
- /**
33
- * Process this item in the queue
34
- * @return boolean was the processing successfull
35
- */
36
- public function process()
37
- {
38
- // Get the copernica API
39
- $api = Mage::helper('marketingsoftware/api');
40
-
41
- // Get the subscription which has been modified
42
- $subscription = $this->getObject();
43
-
44
- // is there a customer? We do also want to ensure that customer have an id
45
- if (is_object($customer = $subscription->customer()) && $customer->id())
46
- {
47
- // get the customer data
48
- $customerData = Mage::getModel('marketingsoftware/copernica_profilecustomer')
49
- ->setCustomer($customer);
50
- }
51
- else
52
- {
53
- // get the customer data
54
- $customerData = Mage::getModel('marketingsoftware/copernica_profilesubscription')
55
- ->setSubscription($subscription);
56
- }
57
-
58
- // The direction should be set
59
- $customerData->setDirection('copernica');
60
-
61
- // Update the profiles given the customer
62
- $api->updateProfiles($customerData);
63
-
64
- // this might result in two profiles with the same customer_id
65
- $profiles = $api->searchProfiles($customerData->id());
66
-
67
- // @todo make a more generic implementation of api result
68
- if (isset($profiles['total']) && $profiles['total'] > 0) return true;
69
-
70
- // we didn't confirmed that profile is subscribed to newsletter
71
- return false;
72
- }
73
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app/code/community/Copernica/MarketingSoftware/Model/QueueEvent/SubscriptionRemove.php DELETED
@@ -1,35 +0,0 @@
1
- <?php
2
- /**
3
- * Copernica Marketing Software
4
- *
5
- * NOTICE OF LICENSE
6
- *
7
- * This source file is subject to the Open Software License (OSL 3.0).
8
- * It is available through the world-wide-web at this URL:
9
- * http://opensource.org/licenses/osl-3.0.php
10
- * If you are unable to obtain a copy of the license through the
11
- * world-wide-web, please send an email to copernica@support.cream.nl
12
- * so we can send you a copy immediately.
13
- *
14
- * DISCLAIMER
15
- *
16
- * Do not edit or add to this file if you wish to upgrade this software
17
- * to newer versions in the future. If you wish to customize this module
18
- * for your needs please refer to http://www.magento.com/ for more
19
- * information.
20
- *
21
- * @category Copernica
22
- * @package Copernica_MarketingSoftware
23
- * @copyright Copyright (c) 2011-2012 Copernica & Cream. (http://docs.cream.nl/)
24
- * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
- */
26
-
27
- /**
28
- * A wrapper object around an event
29
- */
30
- class Copernica_MarketingSoftware_Model_QueueEvent_SubscriptionRemove extends Copernica_MarketingSoftware_Model_QueueEvent_SubscriptionModify
31
- {
32
- /**
33
- * Process this item in the queue the behaviour is the same is for a modification
34
- */
35
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app/code/community/Copernica/MarketingSoftware/Model/QueueEvent/ViewedProductAdd.php DELETED
@@ -1,72 +0,0 @@
1
- <?php
2
- /**
3
- * Copernica Marketing Software
4
- *
5
- * NOTICE OF LICENSE
6
- *
7
- * This source file is subject to the Open Software License (OSL 3.0).
8
- * It is available through the world-wide-web at this URL:
9
- * http://opensource.org/licenses/osl-3.0.php
10
- * If you are unable to obtain a copy of the license through the
11
- * world-wide-web, please send an email to copernica@support.cream.nl
12
- * so we can send you a copy immediately.
13
- *
14
- * DISCLAIMER
15
- *
16
- * Do not edit or add to this file if you wish to upgrade this software
17
- * to newer versions in the future. If you wish to customize this module
18
- * for your needs please refer to http://www.magento.com/ for more
19
- * information.
20
- *
21
- * @category Copernica
22
- * @package Copernica_MarketingSoftware
23
- * @copyright Copyright (c) 2011-2012 Copernica & Cream. (http://docs.cream.nl/)
24
- * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
- */
26
-
27
- /**
28
- * A wrapper object around an event
29
- */
30
- class Copernica_MarketingSoftware_Model_QueueEvent_ViewedProductAdd extends Copernica_MarketingSoftware_Model_QueueEvent_Abstract
31
- {
32
- /**
33
- * Process this item in the queue
34
- * @return boolean was the processing successfull
35
- */
36
- public function process()
37
- {
38
- // Get the copernica API
39
- $api = Mage::helper('marketingsoftware/api');
40
-
41
- $product = $this->getObject();
42
-
43
- // Get the customer
44
- $productData = Mage::getModel('marketingsoftware/copernica_viewedproduct_subprofile')
45
- ->setViewedProduct($product = $this->getObject())
46
- ->setDirection('copernica');
47
-
48
- $tmpStore = Mage::getModel('core/store')->load($product->storeId);
49
-
50
- $storeView = Mage::getModel('marketingsoftware/abstraction_storeview')->setOriginal($tmpStore);
51
-
52
- $customer = Mage::getModel('marketingsoftware/abstraction_customer')->loadCustomer($productData->customerId());
53
-
54
- // get customer Id
55
- $customerId = Mage::helper('marketingsoftware/profile')->getCustomerCopernicaId($customer, $storeView);
56
-
57
- $profiles = $api->searchProfiles($customerId);
58
-
59
- // check if we have any profiles
60
- if (!array_key_exists('data', $profiles)) return true;
61
-
62
- // Process all the profiles
63
- foreach ($profiles['data'] as $profile)
64
- {
65
- // Update the profiles given the customer
66
- $api->updateViewedProductSubProfiles($profile['ID'], $productData);
67
- }
68
-
69
- // this viewed product is processed
70
- return true;
71
- }
72
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app/code/community/Copernica/MarketingSoftware/Model/QueueProcessor.php DELETED
@@ -1,286 +0,0 @@
1
- <?php
2
- /**
3
- * Copernica Marketing Software
4
- *
5
- * NOTICE OF LICENSE
6
- *
7
- * This source file is subject to the Open Software License (OSL 3.0).
8
- * It is available through the world-wide-web at this URL:
9
- * http://opensource.org/licenses/osl-3.0.php
10
- * If you are unable to obtain a copy of the license through the
11
- * world-wide-web, please send an email to copernica@support.cream.nl
12
- * so we can send you a copy immediately.
13
- *
14
- * DISCLAIMER
15
- *
16
- * Do not edit or add to this file if you wish to upgrade this software
17
- * to newer versions in the future. If you wish to customize this module
18
- * for your needs please refer to http://www.magento.com/ for more
19
- * information.
20
- *
21
- * @category Copernica
22
- * @package Copernica_MarketingSoftware
23
- * @copyright Copyright (c) 2011-2012 Copernica & Cream. (http://docs.cream.nl/)
24
- * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
- */
26
-
27
- /**
28
- * This class will process task queue.
29
- */
30
- class Copernica_MarketingSoftware_Model_QueueProcessor
31
- {
32
- /**
33
- * Number of processed tasks by this processor
34
- * @var int
35
- */
36
- private $processedTasks = 0;
37
-
38
- /**
39
- * Timestamp when processor starts its job
40
- */
41
- private $startTime;
42
-
43
- /**
44
- * How many items we want to process in one run?
45
- * @var int
46
- */
47
- private $itemsLimit = 10000000;
48
-
49
- /**
50
- * For what is our timelimit for queue processing? in seconds.
51
- * @var int
52
- */
53
- private $timeLimit = 3075840000;
54
-
55
- /**
56
- * Currently locked customer
57
- * @var int
58
- */
59
- private $currentCustomer = null;
60
-
61
- /**
62
- * Construct object.
63
- *
64
- * NOTE: This class is not a varien_object child!
65
- */
66
- public function __construct()
67
- {
68
- // get config into local scope
69
- $config = Mage::helper('marketingsoftware/config');
70
-
71
- // check if we should limit how many items we can process in one run
72
- if ($itemsLimit = $config->getItemsPerRun()) $this->itemsLimit = $itemsLimit;
73
-
74
- // check if we should limit how much time we should spend on processing
75
- if ($timeLimit = $config->getTimePerRun()) $this->timeLimit = $timeLimit;
76
- }
77
-
78
- /**
79
- * We want to make some final actions when this processor is beeing destroyed.
80
- */
81
- public function __destruct()
82
- {
83
- // get config into local scope
84
- $config = Mage::helper('marketingsoftware/config');
85
-
86
- //
87
- $config->setLastStartTimeCronjob(date("Y-m-d H:i:s"));
88
-
89
- // set how many items we did process in last run
90
- $config->setLastCronjobProcessedTasks($this->processedTasks);
91
- }
92
-
93
- /**
94
- * Try to aqcuire 1st lock that can be used to sync data.
95
- * @return string
96
- */
97
- public function aqcuireLock()
98
- {
99
- $firstFree = Mage::getModel('marketingsoftware/queue')->getFirstFree();
100
-
101
- return $this->currentCustomer = $firstFree;
102
- }
103
-
104
- /**
105
- * Process queue with lock
106
- * @param string
107
- */
108
- public function processWithLocking($lock)
109
- {
110
- // process queue for one customer only
111
- if (is_numeric($lock)) $this->processQueue($this->currentCustomer);
112
- elseif (is_null($lock)) $this->processQueue(null);
113
- }
114
-
115
- /**
116
- * Process queue
117
- * @param string
118
- */
119
- public function processQueue($customerId = -1)
120
- {
121
- // what is the setting for max execution time?
122
- $maxExecutionTime = ini_get('max_execution_time');
123
-
124
- /*
125
- * set unlimited time for script execution. It does not matter that much
126
- * cause most time will be spent on database/curl calls and they do not
127
- * extend script execution time. We are setting this just to be sure that
128
- * script will not terminate in the middle of processing.
129
- * This is true for Linux machines. On windows machines it is super
130
- * important to set it to large value. Cause windows machines do use
131
- * real time to measure script execution time. When time limit is reached
132
- * it will terminate connection and will not gracefully come back to
133
- * script execution. Such situation will just leave mess in database.
134
- */
135
- set_time_limit(0);
136
-
137
- // get queue items collection
138
- $queue = Mage::getResourceModel('marketingsoftware/queue_collection')
139
- ->addDefaultOrder()->setPageSize($this->itemsLimit < 150 ? 150 : $this->itemsLimit);
140
-
141
- // should we pick up all events without a customer?
142
- if (is_null($customerId)) $queue->addFieldToFilter('customer', array('null' => true));
143
-
144
- // shoulf we pick up all events that are binded to customer?
145
- elseif ($customerId > -1) $queue->addFilter('customer', $customerId);
146
-
147
- // make some preparations before we start processing queue
148
- $this->prepareProcessor();
149
-
150
- // iterate over queue
151
- foreach ($queue as $item)
152
- {
153
- // check if we did reach limit
154
- if ($this->isLimitsReached()) break;
155
-
156
- // check if did manage to process item
157
- $this->processItem($item);
158
- }
159
-
160
- /*
161
- * Now, some explanation why we are doing such thing.
162
- * When we are processing tasks/events we are doing hell lot of
163
- * database/curl calls. They do not count into execution time cause cpu
164
- * is not spending time on script (it's halted). This is why we can not
165
- * rely on php time counter and that is why we are making our own check.
166
- * After we are done with processing, we will just reset time counter
167
- * for whole magento.
168
- * Note that this is true for Linux systems. On windows based machines
169
- * real time is used.
170
- */
171
- set_time_limit($maxExecutionTime);
172
- }
173
-
174
- /**
175
- * Make some preparations before we start processing queue
176
- */
177
- private function prepareProcessor()
178
- {
179
- // store time when we start
180
- $this->startTime = microtime(true);
181
- }
182
-
183
- /**
184
- * Check if we reached limits
185
- * @return bool
186
- */
187
- private function isLimitsReached()
188
- {
189
- // check if either we did reach maximum amount of items or we we processing too long
190
- return $this->processedTasks > $this->itemsLimit || microtime(true) > $this->startTime + $this->timeLimit;
191
- }
192
-
193
- /**
194
- * Process queue item
195
- * @param Copernica_MarketingSoftware_Queue
196
- */
197
- private function processItem($item)
198
- {
199
- try
200
- {
201
- // process item and remove it when everything went good
202
- if ($item->process()) $item->delete();
203
-
204
- // if we didn't process item correctly then transfer it to error queue
205
- else $this->transferItemToErrorQueue($item);
206
-
207
- // increment processed tasks counter
208
- $this->processedTasks++;
209
- }
210
- /*
211
- * Copernica exceptions do mean that we have something wrong with API
212
- * configuration or we are missing something important. We want to handle
213
- * them in more civilized way.
214
- */
215
- catch (Copernica_MarketingSoftware_Exception $copernicaException)
216
- {
217
- // log exception message to special log
218
- Mage::log($copernicaException->getMessage(), null, 'copernica_queue_exceptions.log');
219
-
220
- // we want to move problematic item to error queue
221
- $this->transferItemToErrorQueue($item);
222
- }
223
- /*
224
- * When we have a non copernica exception it means that we have little
225
- * controll over the reason why it happend. It could be numerous problems:
226
- * network failure, hard disk turning in fireball, magento stinky code.
227
- * Basically we can not determine what to do with it. We can tell
228
- * magento to log the exception and we just run with the queue as
229
- * we do usual.
230
- */
231
- catch (Exception $exception)
232
- {
233
- // tell magento to log exception
234
- Mage::logException($exception);
235
-
236
- // set result message on item and set result time
237
- $item->setResult($exception->getMessage())->setResultTime(date('Y-m-d H:i:s'));
238
- }
239
- }
240
-
241
- /**
242
- * Transfer queue item to error queue.
243
- * @param Copernica_MarketingSoftware_Queue
244
- */
245
- private function transferItemToErrorQueue($item)
246
- {
247
- // create error queue item
248
- $errorItem = Copernica_MarketingSoftware_Model_ErrorQueue::createFromQueueItem($item);
249
-
250
- // save error item
251
- $errorItem->save();
252
-
253
- // remove item
254
- $item->delete();
255
- }
256
-
257
- /**
258
- * Fetch data about current run.
259
- * @var string
260
- */
261
- public function fetchReport($type)
262
- {
263
- $data = array(
264
- 'startTime' => date('Y-m-d H:i:s', (int)$this->startTime),
265
- 'runTime' => (microtime(true) - $this->startTime),
266
- 'processedTasks' => $this->processedTasks,
267
- 'itemsLimit' => $this->itemsLimit,
268
- 'timeLimit' => $this->timeLimit,
269
- 'lockedCustomer' => $this->currentCustomer
270
- );
271
-
272
- $type = strtolower($type);
273
-
274
- switch ($type) {
275
- case 'json':
276
- return json_encode($data);
277
- default:
278
- $report = " Started at ".$data['startTime'].' UTC'.PHP_EOL;
279
- $report .= " Run took ".$data['runTime']." seconds".PHP_EOL;
280
- $report .= " Processed ".$data['processedTasks']." tasks".PHP_EOL;
281
- $report .= " Limited with ".$data['itemsLimit']." items and ".$data['timeLimit']." seconds".PHP_EOL;
282
- $report .= " ".($data['lockedCustomer'] ? "Locked on ".$data['lockedCustomer'] : "Without a lock").PHP_EOL;
283
- return $report;
284
- }
285
- }
286
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app/code/community/Copernica/MarketingSoftware/Model/REST/CartItem.php DELETED
@@ -1,74 +0,0 @@
1
- <?php
2
-
3
- class Copernica_MarketingSoftware_Model_REST_CartItem extends Copernica_MarketingSoftware_Model_REST_Item
4
- {
5
- /**
6
- * Sync item with quote
7
- * @param Copernica_MarketingSoftware_Model_Copernica_Entity_Customer
8
- * @param int
9
- * @return boolean
10
- */
11
- public function syncWithQuote($customer, $quoteId)
12
- {
13
- /**
14
- * One may wonder why we want a quote ID rather than have access to whole
15
- * quote instance? Reason is quite simple. Magento is broken when it
16
- * comes to quotes. For some odd and not clear reason quote is not accessible
17
- * via Mage::getModel('sales/quote')->load($quoteId). It always creates
18
- * a new quote instance, despite that we can see proper quote data inside
19
- * mage_sales_flat_quote. That could lead to conclusion that something
20
- * went extremly bad when quotes were designed...
21
- */
22
-
23
- // get profile Id
24
- $profileId = Mage::helper('marketingsoftware/api')->getProfileId(array(
25
- 'id' => $customer->getCustomerId(),
26
- 'storeView' => $customer->getStoreView(),
27
- 'email' => $customer->getEmail(),
28
- ));
29
-
30
- // we should be alble to create a customer profile
31
- if ($profileId == false && !($profileId = $this->createProfile($customer))) return false;
32
-
33
- // get quote collection Id
34
- $quoteCollectionId = Mage::helper('marketingsoftware/config')->getCartItemsCollectionId();
35
-
36
- // make a PUT request to create/modify item subprofile
37
- if ($quoteCollectionId) Mage::helper('marketingsoftware/RESTRequest')->put('/profile/'.$profileId.'/subprofiles/'.$quoteCollectionId, $this->getCartSubprofileData($quoteId), array(
38
- 'fields' => array(
39
- 'item_id=='.$this->item->getId(),
40
- 'quote_id=='.$quoteId
41
- ),
42
- 'create' => 'true'
43
- ));
44
-
45
- // we are all dandy
46
- return true;
47
- }
48
-
49
- /**
50
- * Prepare subprofile date
51
- * @param Copernica_MarketingSoftware_Model_Copernica_Entity_Quote
52
- * @return array
53
- */
54
- public function getCartSubprofileData($quoteId)
55
- {
56
- // get synced fields definition
57
- $syncedFields = Mage::helper('marketingsoftware/config')->getLinkedCartItemFields();
58
-
59
- // set increment Id
60
- $data = $this->getRequestData($this->item, $syncedFields);
61
-
62
- // assign quote Id
63
- $data['quote_id'] = $quoteId;
64
-
65
- // assign item Id
66
- $data['item_id'] = $this->item->getId();
67
-
68
- // assign status
69
- $data['status'] = $this->item->getStatus();
70
-
71
- // return prepared data
72
- return $data;
73
- }
74
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app/code/community/Copernica/MarketingSoftware/Model/REST/Item.php DELETED
@@ -1,129 +0,0 @@
1
- <?php
2
- /**
3
- * Copernica Marketing Software
4
- *
5
- * NOTICE OF LICENSE
6
- *
7
- * This source file is subject to the Open Software License (OSL 3.0).
8
- * It is available through the world-wide-web at this URL:
9
- * http://opensource.org/licenses/osl-3.0.php
10
- * If you are unable to obtain a copy of the license through the
11
- * world-wide-web, please send an email to copernica@support.cream.nl
12
- * so we can send you a copy immediately.
13
- *
14
- * DISCLAIMER
15
- *
16
- * Do not edit or add to this file if you wish to upgrade this software
17
- * to newer versions in the future. If you wish to customize this module
18
- * for your needs please refer to http://www.magento.com/ for more
19
- * information.
20
- *
21
- * @category Copernica
22
- * @package Copernica_MarketingSoftware
23
- * @copyright Copyright (c) 2011-2012 Copernica & Cream. (http://docs.cream.nl/)
24
- * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
- */
26
-
27
- /**
28
- * A bridge class between Magento Item and Copernica subprofile
29
- */
30
- class Copernica_MarketingSoftware_Model_REST_Item extends Copernica_MarketingSoftware_Model_REST
31
- {
32
- /**
33
- * Item that we want to use
34
- * @var Copernica_MarketingSoftware_Model_Copernica_Entity_Item
35
- */
36
- protected $item = null;
37
-
38
- /**
39
- * Construct item
40
- * @param Copernica_MarketingSoftware_Model_Copernica_Entity_Item
41
- */
42
- public function __construct($item)
43
- {
44
- $this->item = $item;
45
- }
46
-
47
- /**
48
- * Sync item with customer
49
- * @param Copernica_MarketingSoftware_Model_Copernica_Entity_Customer
50
- * @param Copernica_MarketingSoftware_Model_Copernica_Entity_Order
51
- * @return boolean
52
- */
53
- public function syncWithCustomer($customer, $order)
54
- {
55
- // get profile Id
56
- $profileId = Mage::helper('marketingsoftware/api')->getProfileId(array(
57
- 'id' => $customer->getCustomerId(),
58
- 'storeView' => $customer->getStoreView(),
59
- 'email' => $customer->getEmail(),
60
- ));
61
-
62
- // we should be alble to create a customer profile
63
- if ($profileId == false && !($profileId = $this->createProfile($customer))) return false;
64
-
65
- // sync data with a profile
66
- $this->syncWithProfile($profileId);
67
-
68
- // we are all dandy
69
- return true;
70
- }
71
-
72
- /**
73
- * Sync order items with certain profile
74
- * @param int
75
- * @param Copernica_MarketingSoftware_Model_Copernica_Entity_Order
76
- * @return bool Did we succeed?
77
- */
78
- public function syncWithProfile($profileId, $order)
79
- {
80
- // get items collection Id
81
- $itemsCollectionId = Mage::helper('marketingsoftware/config')->getOrderItemsCollectionId();
82
-
83
- // make a PUT request to create/modify item subprofile
84
- if ($itemsCollectionId)
85
- {
86
- Mage::helper('marketingsoftware/RESTRequest')->put('/profile/'.$profileId.'/subprofiles/'.$itemsCollectionId, $this->getSubprofileData($order), array(
87
- 'fields' => array (
88
- 'item_id=='.$this->item->getId(),
89
- 'order_id=='.$order->getId()
90
- ),
91
- 'create' => 'true'
92
- ));
93
-
94
- // we are all dandy
95
- return true;
96
-
97
- }
98
-
99
- // we failed
100
- else return false;
101
-
102
- }
103
-
104
- /**
105
- * Prepare subprogile data
106
- * @param Copernica_MarketingSoftware_Model_Copernica_Entity_Order
107
- * @return array
108
- */
109
- private function getSubprofileData($order)
110
- {
111
- // get synced fields definition
112
- $syncedFields = Mage::helper('marketingsoftware/config')->getLinkedOrderItemFields();
113
-
114
- // get synced fields
115
- $data = $this->getRequestData($this->item, $syncedFields);
116
-
117
- // set increment Id
118
- if (!empty($syncedFields['incrementId'])) $data['incrementId'] = $order->getIncrementId();
119
-
120
- // assign item Id
121
- $data['item_id'] = $this->item->getId();
122
-
123
- // assign order Id
124
- $data['order_id'] = $order->getId();
125
-
126
- // return prepare data
127
- return $data;
128
- }
129
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app/code/community/Copernica/MarketingSoftware/Model/REST/Order.php DELETED
@@ -1,166 +0,0 @@
1
- <?php
2
- /**
3
- * Copernica Marketing Software
4
- *
5
- * NOTICE OF LICENSE
6
- *
7
- * This source file is subject to the Open Software License (OSL 3.0).
8
- * It is available through the world-wide-web at this URL:
9
- * http://opensource.org/licenses/osl-3.0.php
10
- * If you are unable to obtain a copy of the license through the
11
- * world-wide-web, please send an email to copernica@support.cream.nl
12
- * so we can send you a copy immediately.
13
- *
14
- * DISCLAIMER
15
- *
16
- * Do not edit or add to this file if you wish to upgrade this software
17
- * to newer versions in the future. If you wish to customize this module
18
- * for your needs please refer to http://www.magento.com/ for more
19
- * information.
20
- *
21
- * @category Copernica
22
- * @package Copernica_MarketingSoftware
23
- * @copyright Copyright (c) 2011-2012 Copernica & Cream. (http://docs.cream.nl/)
24
- * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
- */
26
-
27
- /**
28
- * Order REST entity
29
- */
30
- class Copernica_MarketingSoftware_Model_REST_Order extends Copernica_MarketingSoftware_Model_REST
31
- {
32
- /**
33
- * Cached order entity
34
- * @var Copernica_MarketingSoftwarer_Model_Copernica_Entity_Order
35
- */
36
- private $order = null;
37
-
38
- /**
39
- * Construct order REST entity
40
- * @param Copernica_MarketingSoftwarer_Model_Copernica_Entity_Order
41
- */
42
- public function __construct($order)
43
- {
44
- $this->order = $order;
45
- }
46
-
47
- /**
48
- * Sync order
49
- * @param Copernica_MarketingSoftware_Model_Copernica_Entity_Order
50
- * @param bool did we succeed?
51
- */
52
- public function syncWithCustomer($customer)
53
- {
54
- // get profile Id
55
- $profileId = Mage::helper('marketingsoftware/api')->getProfileId(array(
56
- 'id' => $customer->getCustomerId(),
57
- 'storeView' => $customer->getStoreView(),
58
- 'email' => $customer->getEmail(),
59
- ));
60
-
61
- // we should be able to create a customer profile
62
- if ($profileId == false && !($profileId = $this->createProfile($customer))) return false;
63
-
64
- // sync data with profile
65
- $this->syncWithProfile($profileId);
66
-
67
- // we are done here
68
- return true;
69
- }
70
-
71
- /**
72
- * Sync with guest data.
73
- * @param array guest data
74
- * @return bool did we succeed?
75
- */
76
- public function syncWithGuest($guestData)
77
- {
78
- // get profile Id
79
- $profileId = Mage::helper('marketingsoftware/api')->getProfileId(array(
80
- 'storeView' => $this->order->getStoreView(),
81
- 'email' => $guestData['email'],
82
- ));
83
-
84
- // should we update profile with new data?
85
- if ($profileId)
86
- {
87
- // construct proper data to send it
88
- $data = array();
89
-
90
- // prepare data
91
- foreach (Mage::helper('marketingsoftware/data')->supportedCustomerFields() as $magentoField => $copernicaField)
92
- {
93
- // skip empty fields
94
- if (empty($data[$copernicaField]) || is_null($guestData[$magentoField])) continue;
95
-
96
- // assign data
97
- $data[$copernicaField] = $guestData[$magentoField];
98
- }
99
-
100
- // make the request
101
- Mage::helper('marketingsoftware/RESTRequest')->put('/profile/'.$profileId.'/fields', $data);
102
- }
103
-
104
- // try to create new profile
105
- else
106
- {
107
- // try to create a profile
108
- $profileId = $this->createProfile($guestData);
109
-
110
- // do we have a profile?
111
- if ($profileId == false) return false;
112
- }
113
-
114
- // sync data with profile
115
- $this->syncWithProfile($profileId);
116
-
117
- // we are done here
118
- return true;
119
- }
120
-
121
- /**
122
- * Sync order with certain profile
123
- * @param int
124
- */
125
- public function syncWithProfile($profileId)
126
- {
127
- // get order collection Id
128
- $collectionId = Mage::helper('marketingsoftware/config')->getOrdersCollectionId();
129
-
130
- // make a REST request
131
- if ($collectionId) Mage::helper('marketingsoftware/RESTRequest')->put('/profile/'.$profileId.'/subprofiles/'.$collectionId, $this->getSubprofileData(), array(
132
- 'fields' => array(
133
- 'order_id=='.$this->order->getId(),
134
- 'quote_id=='.$this->order->getQuoteId()
135
- ),
136
- 'create' => 'true'
137
- ));
138
-
139
- // get addresses
140
- $shippingAddress = $this->order->getShippingAddress();
141
- $billingAddress = $this->order->getBillingAddress();
142
-
143
- // sync billing and shipping addresses
144
- if ($shippingAddress) $shippingAddress->getREST()->syncWithProfile($profileId);
145
- if ($billingAddress) $billingAddress->getREST()->syncWithProfile($profileId);
146
-
147
- // we have to sync all order items
148
- foreach ($this->order->getItems() as $item) $item->getREST()->syncWithProfile($profileId, $this->order);
149
- }
150
-
151
- /**
152
- * Get subprofile data
153
- * @return array
154
- */
155
- private function getSubprofileData()
156
- {
157
- // get all fields that should be synced
158
- $syncedFields = Mage::helper('marketingsoftware/config')->getLinkedOrderFields();
159
-
160
- // get subprofile data
161
- $data = $this->getRequestData($this->order, $syncedFields);
162
-
163
- // return complete data
164
- return array_merge($data, array('order_id' => $this->order->getId(), 'quote_id' => $this->order->getQuoteId() ));
165
- }
166
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app/code/community/Copernica/MarketingSoftware/Model/REST/Subscription.php DELETED
@@ -1,115 +0,0 @@
1
- <?php
2
- /**
3
- * Copernica Marketing Software
4
- *
5
- * NOTICE OF LICENSE
6
- *
7
- * This source file is subject to the Open Software License (OSL 3.0).
8
- * It is available through the world-wide-web at this URL:
9
- * http://opensource.org/licenses/osl-3.0.php
10
- * If you are unable to obtain a copy of the license through the
11
- * world-wide-web, please send an email to copernica@support.cream.nl
12
- * so we can send you a copy immediately.
13
- *
14
- * DISCLAIMER
15
- *
16
- * Do not edit or add to this file if you wish to upgrade this software
17
- * to newer versions in the future. If you wish to customize this module
18
- * for your needs please refer to http://www.magento.com/ for more
19
- * information.
20
- *
21
- * @category Copernica
22
- * @package Copernica_MarketingSoftware
23
- * @copyright Copyright (c) 2011-2012 Copernica & Cream. (http://docs.cream.nl/)
24
- * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
- */
26
-
27
- /**
28
- * Entity that will take care of synchronizing subscription with copernica.
29
- */
30
- class Copernica_MarketingSoftware_Model_REST_Subscription extends Copernica_MarketingSoftware_Model_REST
31
- {
32
- /**
33
- * Subscription entity that we will use to create proper profile inside
34
- * copernica database.
35
- * @var Copernica_MarketingSoftware_Model_Copernica_Entity_Subscription
36
- */
37
- private $subscription;
38
-
39
- /**
40
- * Construct REST subscription model
41
- * @param Copernica_MarketingSoftware_Model_Copernica_Entity_Subscription
42
- */
43
- public function __construct($subscription)
44
- {
45
- $this->subscription = $subscription;
46
- }
47
-
48
- /**
49
- * Get data that should be update in copernica database
50
- * @return array
51
- */
52
- private function getProfileData($storeView)
53
- {
54
- // get profile fields
55
- $linkedCustomerFields = Mage::helper('marketingsoftware/config')->getLinkedCustomerFields();
56
-
57
- return array(
58
- 'customer_id' => $this->subscription->getCustomerId(),
59
- $linkedCustomerFields['email'] => $this->subscription->getEmail(),
60
- $linkedCustomerFields['group'] => $this->subscription->getGroup(),
61
- $linkedCustomerFields['newsletter'] => $this->subscription->getStatus(),
62
- $linkedCustomerFields['storeView'] => $storeView
63
- );
64
- }
65
-
66
- /**
67
- * Synchronize magento subscriber with copernica profile.
68
- * @param int
69
- * @return boolean
70
- */
71
- public function sync()
72
- {
73
- // get store view Id
74
- $storeViewId = $this->subscription->getStoreId();
75
-
76
- // get objects related to store
77
- $store = Mage::getModel('core/store')->load($storeViewId);
78
- $website = $store->getWebsite();
79
- $group = $store->getGroup();
80
-
81
- // construct store view identifier
82
- $storeView = implode(' > ', array (
83
- $website->getName(),
84
- $group->getName(),
85
- $store->getName())
86
- );
87
-
88
- // get profileId
89
- $profileId = Mage::helper('marketingsoftware/api')->getProfileId(array(
90
- 'id' => null,
91
- 'email' => $this->subscription->getEmail(),
92
- 'storeView' => $storeView
93
- ));
94
-
95
- // bring request into local scope
96
- $request = Mage::helper('marketingsoftware/RESTRequest');
97
-
98
- // check if we have profile Id
99
- if ($profileId) {
100
- $request->put('/profile/'.$profileId.'/fields/', $this->getProfileData($storeView));
101
- }
102
-
103
- // we can update old profile
104
- else {
105
- // get database Id
106
- $databaseId = Mage::helper('marketingsoftware/config')->getDatabaseId();
107
-
108
- // create new profile
109
- $request->post('/database/'.$databaseId.'/profiles/', $this->getProfileData($storeView));
110
- }
111
-
112
- // we should be fine
113
- return true;
114
- }
115
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app/code/community/Copernica/MarketingSoftware/Model/{REST.php → Rest.php} RENAMED
@@ -29,112 +29,94 @@
29
  * This is a base class for all classes that will be syncing entities via REST
30
  * API.
31
  */
32
- abstract class Copernica_MarketingSoftware_Model_REST
33
- {
34
  /**
35
  * Get request data that will can be passed to REST request
36
- * @param Copernica_MarketingSoftware_Model_Copernica_Entity
37
- * @param array
 
38
  * @return array
39
  */
40
- protected function getRequestData($entity, $syncedFields)
41
  {
42
- // placeholder for data
43
  $data = array();
44
 
45
- // iterate over all synced fields
46
- foreach ($syncedFields as $fieldType => $copernicaField)
47
- {
48
- // if copernica field name was not specified, just skip iteration
49
- if (empty($copernicaField)) continue;
50
 
51
- // construct name of a get method
52
  $getMethod = 'get'.ucfirst($fieldType);
53
 
54
- // assign field value to copernica field name
55
  $data[$copernicaField] = (string)$entity->$getMethod();
56
  }
57
 
58
- // return data
59
  return $data;
60
  }
61
 
62
  /**
63
  * Create profile. Parameter can be supplied as customer entity or array data.
64
  * Array data should correspond to supported customer fields.
65
- * @param array|Copernica_MarketingSoftware_Model_Copernica_Entity_Customer
66
- * @return int|false profleId or false if we could not create proper profile
 
67
  */
68
- protected function createProfile($data)
69
- {
70
- // if we have customer entity we can create
71
  if ($data instanceof Copernica_MarketingSoftware_Model_Copernica_Entity_Customer)
72
  {
73
- // set variables
74
  $id = $data->getCustomerId();
75
- $storeView = $data->getStoreView();
76
  $email = $data->getEmail();
 
 
 
 
 
 
 
 
 
77
 
78
- // just set the data via customer rest instance
79
- $data->getREST()->set();
80
- }
81
-
82
- // we have an array so it's more complicated
83
- else
84
- {
85
- // no array, no fun. Also we have to get store view
86
- if (!is_array($data) && !isset($data['storeViewId'])) return;
87
-
88
- // set data to proper values
89
  $profileData = array();
90
 
91
- // if we have the id or email we could use it
92
- if (isset($data['id'])) $profileData['customer_id'] = $data['id'].'|'.$data['storeViewId'];
93
- else if (isset($data['email'])) $profileData['customer_id'] = $data['email'].'|'.$data['storeViewId'];
94
-
95
- // we don't have proper data to set
96
- else false;
 
97
 
98
- // check if we have to create store view name
99
  if (!isset($data['storeView'])) {
100
- // we will need store instance for a sec
101
  $store = Mage::getModel('core/store')->load($data['storeViewId']);
102
 
103
- // ok we have to adjust store view
104
- $data['storeView'] = implode(' < ', array(
105
  $store->getWebsite()->getName(),
106
  $store->getGroup()->getName(),
107
  $store->getName(),
108
  ));
109
  }
110
 
111
- // set variables
112
  $id = $data['id'];
113
- $storeView = $data['storeView'];
114
  $email = $data['email'];
 
115
 
116
- // get linked customer fields
117
  $customerLinking = Mage::helper('marketingsoftware/config')->getLinkedCustomerFields();
118
 
119
- // iterate over linked fields and assign proper ones to profile data
120
- foreach ($customerLinking as $magentoField => $copernicaField)
121
- {
122
- // skip empty fields
123
- if (empty($copernicaField) || is_null($data[$magentoField])) continue;
124
 
125
- // assign magento data to copernica data
126
- // This one kinda funny.
127
  $profileData[$copernicaField] = $data[$magentoField];
128
  }
129
 
130
- // get database Id
131
  $databaseId = Mage::helper('marketingsoftware/config')->getDatabaseId();
132
 
133
- // make a POST request to create a profile
134
- Mage::helper('marketingsoftware/RESTRequest')->post('/database/'.$databaseId.'/profiles', $profileData);
135
  }
136
 
137
- // try to get profile Id
138
  return Mage::helper('marketingsoftware/api')->getProfileId(array(
139
  'id' => $id,
140
  'storeView' => $storeView,
29
  * This is a base class for all classes that will be syncing entities via REST
30
  * API.
31
  */
32
+ abstract class Copernica_MarketingSoftware_Model_Rest
33
+ {
34
  /**
35
  * Get request data that will can be passed to REST request
36
+ *
37
+ * @param Copernica_MarketingSoftware_Model_Copernica_Entity $entity
38
+ * @param array $syncedFields
39
  * @return array
40
  */
41
+ protected function _getRequestData(Copernica_MarketingSoftware_Model_Copernica_Entity $entity, $syncedFields)
42
  {
 
43
  $data = array();
44
 
45
+ foreach ($syncedFields as $fieldType => $copernicaField) {
46
+ if (empty($copernicaField)) {
47
+ continue;
48
+ }
 
49
 
 
50
  $getMethod = 'get'.ucfirst($fieldType);
51
 
 
52
  $data[$copernicaField] = (string)$entity->$getMethod();
53
  }
54
 
 
55
  return $data;
56
  }
57
 
58
  /**
59
  * Create profile. Parameter can be supplied as customer entity or array data.
60
  * Array data should correspond to supported customer fields.
61
+ *
62
+ * @param array|Copernica_MarketingSoftware_Model_Copernica_Entity_Customer $data
63
+ * @return int|false
64
  */
65
+ protected function _createProfile($data)
66
+ {
 
67
  if ($data instanceof Copernica_MarketingSoftware_Model_Copernica_Entity_Customer)
68
  {
 
69
  $id = $data->getCustomerId();
 
70
  $email = $data->getEmail();
71
+ $storeView = (string) $data->getStoreView();
72
+
73
+ $restCustomer = $data->getRestCustomer();
74
+ $restCustomer->setCustomerEntity($data);
75
+ $restCustomer->setProfile();
76
+ } else {
77
+ if (!is_array($data) && !isset($data['storeviewId'])) {
78
+ return;
79
+ }
80
 
 
 
 
 
 
 
 
 
 
 
 
81
  $profileData = array();
82
 
83
+ if (isset($data['id'])) {
84
+ $profileData['customer_id'] = $data['id'].'|'.$data['storeViewId'];
85
+ } else if (isset($data['email'])) {
86
+ $profileData['customer_id'] = $data['email'].'|'.$data['storeViewId'];
87
+ } else {
88
+ return false;
89
+ }
90
 
 
91
  if (!isset($data['storeView'])) {
 
92
  $store = Mage::getModel('core/store')->load($data['storeViewId']);
93
 
94
+ $data['storeView'] = implode(' > ', array(
 
95
  $store->getWebsite()->getName(),
96
  $store->getGroup()->getName(),
97
  $store->getName(),
98
  ));
99
  }
100
 
 
101
  $id = $data['id'];
 
102
  $email = $data['email'];
103
+ $storeView = $data['storeView'];
104
 
 
105
  $customerLinking = Mage::helper('marketingsoftware/config')->getLinkedCustomerFields();
106
 
107
+ foreach ($customerLinking as $magentoField => $copernicaField) {
108
+ if (empty($copernicaField) || is_null($data[$magentoField])) {
109
+ continue;
110
+ }
 
111
 
 
 
112
  $profileData[$copernicaField] = $data[$magentoField];
113
  }
114
 
 
115
  $databaseId = Mage::helper('marketingsoftware/config')->getDatabaseId();
116
 
117
+ Mage::helper('marketingsoftware/rest_request')->post('/database/'.$databaseId.'/profiles', $profileData);
 
118
  }
119
 
 
120
  return Mage::helper('marketingsoftware/api')->getProfileId(array(
121
  'id' => $id,
122
  'storeView' => $storeView,
app/code/community/Copernica/MarketingSoftware/Model/{REST → Rest}/Address.php RENAMED
@@ -27,85 +27,87 @@
27
  /**
28
  * Address REST entity
29
  */
30
- class Copernica_MarketingSoftware_Model_REST_Address extends Copernica_MarketingSoftware_Model_REST
31
  {
32
  /**
33
  * Cached address entity
34
- * @var Coeprnica_MarketingSoftware_Model_Copernica_Entity_Address
 
35
  */
36
- private $address;
37
-
38
- /**
39
- * Construct REST entity
40
- * @param Copernica_MarketingSoftware_Model_Copernica_Entity_Address
41
- */
42
- public function __construct($address)
43
- {
44
- $this->address = $address;
45
- }
46
 
47
  /**
48
  * Bind address to customer
49
- * @param Copernica_MarketingSoftware_Model_Copernica_Entity_Customer
 
50
  * @return boolean
51
  */
52
  public function bindToCustomer(Copernica_MarketingSoftware_Model_Copernica_Entity_Customer $customer)
53
- {
54
- // get profile Id
55
- $profileId = Mage::helper('marketingsoftware/api')->getProfileId(array(
56
- 'id' => $customer->getId(),
57
- 'email' => $customer->getEmail(),
58
- 'storeView' => $customer->getStoreView(),
59
- ));
60
-
61
- // sync data with profile
 
 
 
 
 
 
 
 
62
  $this->syncWithProfile($profileId);
63
 
64
- // everything went just ok
65
  return true;
66
  }
67
 
68
  /**
69
  * Sync address data with certain profile
70
- * @param int
71
- * @return bool Did we succeed?
 
72
  */
73
  public function syncWithProfile($profileId)
74
  {
75
- // get address collection Id
76
- $addressCollectionId = Mage::helper('marketingsoftware/config')->getAddressesCollectionId();
77
-
78
- // make a PUT request
79
- if ($addressCollectionId)
80
- {
81
- Mage::helper('marketingsoftware/RESTRequest')->put('/profile/'.$profileId.'/subprofiles/'.$addressCollectionId, $this->getSubprofileData(), array(
82
- 'fields[]' => 'address_id=='.$this->address->getId(),
83
- 'create' => true
84
- ));
85
-
86
  return true;
87
- }
88
-
89
- // we didn't make the sync
90
- else return false;
91
  }
92
 
93
  /**
94
  * Get subprofile data
 
95
  * @return array
96
  */
97
- private function getSubprofileData()
98
  {
99
- // get addresses synced fields
100
  $syncedFields = Mage::helper('marketingsoftware/config')->getLinkedAddressFields();
101
 
102
- // get data
103
- $data = $this->getRequestData($this->address, $syncedFields);
104
 
105
- // merge subprofile data and required fields
106
- $data = array_merge($data, array('address_id' => $this->address->getId()));
107
-
108
- // return subprofile data
109
  return $data;
110
  }
 
 
 
 
 
 
 
 
 
111
  }
27
  /**
28
  * Address REST entity
29
  */
30
+ class Copernica_MarketingSoftware_Model_Rest_Address extends Copernica_MarketingSoftware_Model_Rest
31
  {
32
  /**
33
  * Cached address entity
34
+ *
35
+ * @var Copernica_MarketingSoftware_Model_Copernica_Entity_Address
36
  */
37
+ protected $_addressEntity;
 
 
 
 
 
 
 
 
 
38
 
39
  /**
40
  * Bind address to customer
41
+ *
42
+ * @param Copernica_MarketingSoftware_Model_Copernica_Entity_Customer $customer
43
  * @return boolean
44
  */
45
  public function bindToCustomer(Copernica_MarketingSoftware_Model_Copernica_Entity_Customer $customer)
46
+ {
47
+ $customer->setStore($customer->getStoreview());
48
+
49
+ $profileId = Mage::helper('marketingsoftware/api')->getProfileId(array(
50
+ 'id' => $customer->getCustomerId(),
51
+ 'storeView' => (string) $customer->getStoreView(),
52
+ 'email' => $customer->getEmail(),
53
+ ));
54
+
55
+ if (!$profileId) {
56
+ $profileId = $this->_createProfile($customer);
57
+
58
+ if(!$profileId) {
59
+ return false;
60
+ }
61
+ }
62
+
63
  $this->syncWithProfile($profileId);
64
 
 
65
  return true;
66
  }
67
 
68
  /**
69
  * Sync address data with certain profile
70
+ *
71
+ * @param int $profileId
72
+ * @return bool
73
  */
74
  public function syncWithProfile($profileId)
75
  {
76
+ $addressCollectionId = Mage::helper('marketingsoftware/config')->getAddressesCollectionId();
77
+
78
+ if ($addressCollectionId) {
79
+ Mage::helper('marketingsoftware/rest_request')->put('/profile/'.$profileId.'/subprofiles/'.$addressCollectionId, $this->_getSubprofileData(), array(
80
+ 'fields[]' => 'address_id=='.$this->_addressEntity->getId(),
81
+ 'create' => 'true'
82
+ ));
83
+
 
 
 
84
  return true;
85
+ } else {
86
+ return false;
87
+ }
 
88
  }
89
 
90
  /**
91
  * Get subprofile data
92
+ *
93
  * @return array
94
  */
95
+ protected function _getSubprofileData()
96
  {
 
97
  $syncedFields = Mage::helper('marketingsoftware/config')->getLinkedAddressFields();
98
 
99
+ $data = $this->_getRequestData($this->_addressEntity, $syncedFields);
100
+ $data = array_merge($data, array('address_id' => $this->_addressEntity->getId()));
101
 
 
 
 
 
102
  return $data;
103
  }
104
+
105
+ /**
106
+ * Set REST address entity
107
+ * @param Copernica_MarketingSoftware_Model_Copernica_Entity_Address $address
108
+ */
109
+ public function setAddressEntity(Copernica_MarketingSoftware_Model_Copernica_Entity_Address $address)
110
+ {
111
+ $this->_addressEntity = $address;
112
+ }
113
  }
app/code/community/Copernica/MarketingSoftware/Model/{REST → Rest}/Customer.php RENAMED
@@ -27,68 +27,58 @@
27
  /**
28
  * REST bridge between magento customer and copernica platform
29
  */
30
- class Copernica_MarketingSoftware_Model_REST_Customer extends Copernica_MarketingSoftware_Model_REST
31
  {
32
  /**
33
  * Customer that will be used to send data
34
- * @var Copernica_MarketingSoftware_Model_Copernica_Entity_Customer
 
35
  */
36
- private $customer;
37
 
38
  /**
39
- * Construct REST entity
40
- * @param Copernica_MarketingSoftware_Model_Copernica_Entity_Customer
41
- */
42
- public function __construct(Copernica_MarketingSoftware_Model_Copernica_Entity_Customer $customer)
43
- {
44
- $this->customer = $customer;
45
- }
46
-
47
- /**
48
- * Set customer
49
  * @return bool
50
  */
51
- public function set()
52
- {
53
- // get profile Id
54
- $profileId = $this->customer->getProfileId();
55
-
56
- // do we have to create a profile
57
- if ($profileId)
58
- {
59
- // make a PUT request to create a profile
60
- Mage::helper('marketingsoftware/RESTRequest')->put('/profile/'.$profileId.'/fields', $this->getProfileData());
61
- }
62
- // we have to update the profile
63
- else
64
- {
65
- // get database Id
66
  $databaseId = Mage::helper('marketingsoftware/config')->getDatabaseId();
67
 
68
- // make a POST request to create a profile
69
- Mage::helper('marketingsoftware/RESTRequest')->post('/database/'.$databaseId.'/profiles', $this->getProfileData());
70
  }
71
 
72
- // we are done here
73
  return true;
74
  }
 
 
 
 
 
 
 
 
 
75
 
76
  /**
77
  * Get data that will be used to create a profile inside copernica platform
 
78
  * @return array
79
  */
80
- private function getProfileData()
81
  {
82
- // get synced fields
83
  $syncedFields = Mage::helper('marketingsoftware/config')->getLinkedCustomerFields();
84
 
85
- // get data to sync
86
- $data = $this->getRequestData($this->customer, $syncedFields);
87
-
88
- // we will also have to set the customer Id
89
- $data = array_merge($data, array('customer_id' => $this->customer->getCustomerId()));
90
 
91
- // return complete data
92
  return $data;
93
  }
94
  }
27
  /**
28
  * REST bridge between magento customer and copernica platform
29
  */
30
+ class Copernica_MarketingSoftware_Model_Rest_Customer extends Copernica_MarketingSoftware_Model_Rest
31
  {
32
  /**
33
  * Customer that will be used to send data
34
+ *
35
+ * @var Copernica_MarketingSoftware_Model_Copernica_Entity_Customer
36
  */
37
+ protected $_customerEntity;
38
 
39
  /**
40
+ * Set profile
41
+ *
 
 
 
 
 
 
 
 
42
  * @return bool
43
  */
44
+ public function setProfile()
45
+ {
46
+ $profileId = $this->_customerEntity->getProfileId();
47
+
48
+ $profileData = $this->_getProfileData();
49
+
50
+ if ($profileId) {
51
+ Mage::helper('marketingsoftware/rest_request')->put('/profile/'.$profileId.'/fields', $profileData);
52
+ } else {
 
 
 
 
 
 
53
  $databaseId = Mage::helper('marketingsoftware/config')->getDatabaseId();
54
 
55
+ Mage::helper('marketingsoftware/rest_request')->post('/database/'.$databaseId.'/profiles', $profileData);
 
56
  }
57
 
 
58
  return true;
59
  }
60
+
61
+ /**
62
+ * Set REST customer entity
63
+ *
64
+ * @param Copernica_MarketingSoftware_Model_Copernica_Entity_Customer $customer
65
+ */
66
+ public function setCustomerEntity(Copernica_MarketingSoftware_Model_Copernica_Entity_Customer $customerEntity) {
67
+ $this->_customerEntity = $customerEntity;
68
+ }
69
 
70
  /**
71
  * Get data that will be used to create a profile inside copernica platform
72
+ *
73
  * @return array
74
  */
75
+ protected function _getProfileData()
76
  {
 
77
  $syncedFields = Mage::helper('marketingsoftware/config')->getLinkedCustomerFields();
78
 
79
+ $data = $this->_getRequestData($this->_customerEntity, $syncedFields);
80
+ $data = array_merge($data, array('customer_id' => $this->_customerEntity->getCustomerId()));
 
 
 
81
 
 
82
  return $data;
83
  }
84
  }
app/code/community/Copernica/MarketingSoftware/Model/Rest/Order.php ADDED
@@ -0,0 +1,158 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Copernica Marketing Software
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0).
8
+ * It is available through the world-wide-web at this URL:
9
+ * http://opensource.org/licenses/osl-3.0.php
10
+ * If you are unable to obtain a copy of the license through the
11
+ * world-wide-web, please send an email to copernica@support.cream.nl
12
+ * so we can send you a copy immediately.
13
+ *
14
+ * DISCLAIMER
15
+ *
16
+ * Do not edit or add to this file if you wish to upgrade this software
17
+ * to newer versions in the future. If you wish to customize this module
18
+ * for your needs please refer to http://www.magento.com/ for more
19
+ * information.
20
+ *
21
+ * @category Copernica
22
+ * @package Copernica_MarketingSoftware
23
+ * @copyright Copyright (c) 2011-2012 Copernica & Cream. (http://docs.cream.nl/)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ /**
28
+ * Order REST entity
29
+ */
30
+ class Copernica_MarketingSoftware_Model_Rest_Order extends Copernica_MarketingSoftware_Model_Rest
31
+ {
32
+ /**
33
+ * Cached order entity
34
+ *
35
+ * @var Copernica_MarketingSoftware_Model_Copernica_Entity_Order
36
+ */
37
+ protected $_orderEntity = null;
38
+
39
+ /**
40
+ * Sync order
41
+ *
42
+ * @param Copernica_MarketingSoftware_Model_Copernica_Entity_Customer
43
+ */
44
+ public function syncWithCustomer(Copernica_MarketingSoftware_Model_Copernica_Entity_Customer $customer)
45
+ {
46
+ $customer->setStore($this->_orderEntity->getStoreView());
47
+
48
+ $profileId = Mage::helper('marketingsoftware/api')->getProfileId(array(
49
+ 'id' => $customer->getCustomerId(),
50
+ 'storeView' => (string) $customer->getStoreView(),
51
+ 'email' => $customer->getEmail(),
52
+ ));
53
+
54
+ if (!$profileId) {
55
+ $profileId = $this->_createProfile($customer);
56
+
57
+ if(!$profileId) {
58
+ return false;
59
+ }
60
+ }
61
+
62
+ $this->syncWithProfile($profileId);
63
+
64
+ return true;
65
+ }
66
+
67
+ /**
68
+ * Sync with guest data.
69
+ *
70
+ * @param array $guestData
71
+ * @return bool
72
+ */
73
+ public function syncWithGuest($guestData)
74
+ {
75
+ $profileId = Mage::helper('marketingsoftware/api')->getProfileId(array(
76
+ 'storeView' => $guestData['storeView'],
77
+ 'email' => $guestData['email'],
78
+ ));
79
+
80
+ if ($profileId) {
81
+ $data = array();
82
+
83
+ $syncedFields = Mage::helper('marketingsoftware/config')->getLinkedCustomerFields();
84
+
85
+ Mage::helper('marketingsoftware/rest_request')->post('/profile/'.$profileId.'/fields', $guestData);
86
+ } else {
87
+ $profileId = $this->_createProfile($guestData);
88
+
89
+ if ($profileId == false) {
90
+ return false;
91
+ }
92
+ }
93
+
94
+ $this->syncWithProfile($profileId);
95
+
96
+ return true;
97
+ }
98
+
99
+ /**
100
+ * Sync order with certain profile
101
+ *
102
+ * @param int $profileId
103
+ */
104
+ public function syncWithProfile($profileId)
105
+ {
106
+ $collectionId = Mage::helper('marketingsoftware/config')->getOrdersCollectionId();
107
+
108
+ if ($collectionId) Mage::helper('marketingsoftware/rest_request')->put('/profile/'.$profileId.'/subprofiles/'.$collectionId, $this->_getSubprofileData(), array(
109
+ 'fields' => array(
110
+ 'order_id=='.$this->_orderEntity->getId(),
111
+ 'quote_id=='.$this->_orderEntity->getQuoteId()
112
+ ),
113
+ 'create' => 'true'
114
+ ));
115
+
116
+ $shippingAddress = $this->_orderEntity->getShippingAddress();
117
+ $billingAddress = $this->_orderEntity->getBillingAddress();
118
+
119
+ if ($shippingAddress) {
120
+ $restAddress = $shippingAddress->getRestAddress();
121
+ $restAddress->syncWithProfile($profileId);
122
+ }
123
+
124
+ if ($billingAddress) {
125
+ $restAddress = $billingAddress->getRestAddress();
126
+ $restAddress->syncWithProfile($profileId);
127
+ }
128
+
129
+ foreach ($this->_orderEntity->getItems() as $orderItemEntity) {
130
+ $restOrderItem = $orderItemEntity->getRestOrderItem();
131
+ $restOrderItem->syncWithProfile($profileId, $this->_orderEntity);
132
+ }
133
+ }
134
+
135
+ /**
136
+ * Get subprofile data
137
+ *
138
+ * @return array
139
+ */
140
+ protected function _getSubprofileData()
141
+ {
142
+ $syncedFields = Mage::helper('marketingsoftware/config')->getLinkedOrderFields();
143
+
144
+ $data = $this->_getRequestData($this->_orderEntity, $syncedFields);
145
+
146
+ return array_merge($data, array('order_id' => $this->_orderEntity->getId(), 'quote_id' => $this->_orderEntity->getQuoteId() ));
147
+ }
148
+
149
+ /**
150
+ * Set REST order entity
151
+ *
152
+ * @param Copernica_MarketingSoftwarer_Model_Copernica_Entity_Order $orderEntity
153
+ */
154
+ public function setOrderEntity(Copernica_MarketingSoftwarer_Model_Copernica_Entity_Order $orderEntity)
155
+ {
156
+ $this->_orderEntity = $orderEntity;
157
+ }
158
+ }
app/code/community/Copernica/MarketingSoftware/Model/Rest/Order/Item.php ADDED
@@ -0,0 +1,126 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Copernica Marketing Software
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0).
8
+ * It is available through the world-wide-web at this URL:
9
+ * http://opensource.org/licenses/osl-3.0.php
10
+ * If you are unable to obtain a copy of the license through the
11
+ * world-wide-web, please send an email to copernica@support.cream.nl
12
+ * so we can send you a copy immediately.
13
+ *
14
+ * DISCLAIMER
15
+ *
16
+ * Do not edit or add to this file if you wish to upgrade this software
17
+ * to newer versions in the future. If you wish to customize this module
18
+ * for your needs please refer to http://www.magento.com/ for more
19
+ * information.
20
+ *
21
+ * @category Copernica
22
+ * @package Copernica_MarketingSoftware
23
+ * @copyright Copyright (c) 2011-2012 Copernica & Cream. (http://docs.cream.nl/)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ /**
28
+ * A bridge class between Magento Item and Copernica subprofile
29
+ */
30
+ class Copernica_MarketingSoftware_Model_Rest_Order_Item extends Copernica_MarketingSoftware_Model_Rest
31
+ {
32
+ /**
33
+ * Item that we want to use
34
+ *
35
+ * @var Copernica_MarketingSoftware_Model_Copernica_Entity_Order_Item
36
+ */
37
+ protected $_orderItemEntity = null;
38
+
39
+ /**
40
+ * Sync item with customer
41
+ *
42
+ * @param Copernica_MarketingSoftware_Model_Copernica_Entity_Customer $customer
43
+ * @param Copernica_MarketingSoftware_Model_Copernica_Entity_Order $order
44
+ * @return boolean
45
+ */
46
+ public function syncWithCustomer(Copernica_MarketingSoftware_Model_Copernica_Entity_Customer $customer, Copernica_MarketingSoftware_Model_Copernica_Entity_Order $order)
47
+ {
48
+ $customer->setStore($this->_orderItemEntity->getStoreView());
49
+
50
+ $profileId = Mage::helper('marketingsoftware/api')->getProfileId(array(
51
+ 'id' => $customer->getCustomerId(),
52
+ 'storeView' => (string) $customer->getStoreView(),
53
+ 'email' => $customer->getEmail(),
54
+ ));
55
+
56
+ if (!$profileId) {
57
+ $profileId = $this->_createProfile($customer);
58
+
59
+ if(!$profileId) {
60
+ return false;
61
+ }
62
+ }
63
+
64
+ $this->syncWithProfile($profileId);
65
+
66
+ return true;
67
+ }
68
+
69
+ /**
70
+ * Sync order items with certain profile
71
+ *
72
+ * @param int $profileId
73
+ * @param Copernica_MarketingSoftware_Model_Copernica_Entity_Order $order
74
+ * @return bool
75
+ */
76
+ public function syncWithProfile($profileId, Copernica_MarketingSoftware_Model_Copernica_Entity_Order $order)
77
+ {
78
+ $itemCollectionId = Mage::helper('marketingsoftware/config')->getOrderItemCollectionId();
79
+
80
+ if ($itemCollectionId) {
81
+ Mage::helper('marketingsoftware/rest_request')->put('/profile/'.$profileId.'/subprofiles/'.$itemCollectionId, $this->_getSubprofileData($order), array(
82
+ 'fields' => array (
83
+ 'item_id=='.$this->_orderItemEntity->getId(),
84
+ 'order_id=='.$order->getId()
85
+ ),
86
+ 'create' => 'true'
87
+ ));
88
+
89
+ return true;
90
+ } else {
91
+ return false;
92
+ }
93
+
94
+ }
95
+
96
+ /**
97
+ * Prepare subprofile data
98
+ *
99
+ * @param Copernica_MarketingSoftware_Model_Copernica_Entity_Order $order
100
+ * @return array
101
+ */
102
+ protected function _getSubprofileData(Copernica_MarketingSoftware_Model_Copernica_Entity_Order $order)
103
+ {
104
+ $syncedFields = Mage::helper('marketingsoftware/config')->getLinkedOrderItemFields();
105
+
106
+ $data = $this->_getRequestData($this->_orderItemEntity, $syncedFields);
107
+
108
+ if (!empty($syncedFields['incrementId'])) {
109
+ $data['incrementId'] = $order->getIncrementId();
110
+ }
111
+
112
+ $data['item_id'] = $this->_orderItemEntity->getId();
113
+ $data['order_id'] = $order->getId();
114
+
115
+ return $data;
116
+ }
117
+
118
+ /**
119
+ * Set REST order item entity
120
+ * @param Copernica_MarketingSoftware_Model_Copernica_Entity_Order_Item $orderItem
121
+ */
122
+ public function setOrderItemEntity(Copernica_MarketingSoftware_Model_Copernica_Entity_Order_Item $orderItem)
123
+ {
124
+ $this->_orderItemEntity = $orderItem;
125
+ }
126
+ }
app/code/community/Copernica/MarketingSoftware/Model/{REST → Rest}/Product.php RENAMED
@@ -27,59 +27,69 @@
27
  /**
28
  * REST entity that will communicate with Copernica platform
29
  */
30
- class Copernica_MarketingSoftware_Model_REST_Product extends Copernica_MarketingSoftware_Model_REST
31
  {
32
  /**
33
  * Cached product entity
34
- * @param Copernica_MarketingSoftware_Model_Copernica_Entity_Product
 
35
  */
36
- private $product;
37
-
38
- /**
39
- * Construct REST entity
40
- * @param Copernica_MarketingSoftware_Model_Copernica_Entity_Product
41
- */
42
- public function __construct($productEntity)
43
- {
44
- $this->product = $productEntity;
45
- }
46
 
47
  /**
48
  * Get subprofile data
 
49
  * @return array
50
  */
51
- private function getSubprofileData()
52
  {
53
- // get fields that are linked
54
- $syncedFields = Mage::helper('marketingsoftware/config')->getLinkedViewedProductFields();
55
 
56
- // get request data
57
- return $this->getRequestData($this->product, $syncedFields);
 
 
58
  }
59
 
60
  /**
61
  * Tell Copernica platform that product was viewed by a customer
62
- * @param Copernica_MarketingSoftware_Model_Copernica_Entity_Customer
 
63
  */
64
  public function viewedBy(Copernica_MarketingSoftware_Model_Copernica_Entity_Customer $customer)
65
  {
66
- // get profile Id
 
67
  $profileId = Mage::helper('marketingsoftware/api')->getProfileId(array(
68
  'id' => $customer->getCustomerId(),
69
- 'storeView' => $customer->getStoreView(),
70
  'email' => $customer->getEmail(),
71
- ));
 
 
 
 
 
 
 
 
72
 
73
- // check if we have a profile Id
74
- if ($profileId === false && !($profileId = $this->createProfile($customer))) return false;
75
-
76
- // get vieved product collection Id
77
  $collectionId = Mage::helper('marketingsoftware/config')->getViewedProductCollectionId();
78
-
79
- // make a PUT request to update/create subprofile
80
- if ($collectionId) Mage::helper('marketingsoftware/RESTRequest')->put('/profile/'.$profileId.'/subprofiles/'.$collectionId, $this->getSubprofileData(), array(
81
- 'fields[]' => 'id=='.$this->product->getId(),
82
- 'create' => 'true'
83
- ));
 
 
 
 
 
 
 
 
 
 
84
  }
85
  }
27
  /**
28
  * REST entity that will communicate with Copernica platform
29
  */
30
+ class Copernica_MarketingSoftware_Model_Rest_Product extends Copernica_MarketingSoftware_Model_Rest
31
  {
32
  /**
33
  * Cached product entity
34
+ *
35
+ * @var Copernica_MarketingSoftware_Model_Copernica_Entity_Product
36
  */
37
+ protected $_productEntity;
 
 
 
 
 
 
 
 
 
38
 
39
  /**
40
  * Get subprofile data
41
+ *
42
  * @return array
43
  */
44
+ protected function _getSubprofileData()
45
  {
46
+ $syncedFields = Mage::helper('marketingsoftware/config')->getLinkedViewedProductFields();
 
47
 
48
+ $data = $this->_getRequestData($this->_productEntity, $syncedFields);
49
+ $data['product_id'] = $this->_productEntity->getId();
50
+
51
+ return $data;
52
  }
53
 
54
  /**
55
  * Tell Copernica platform that product was viewed by a customer
56
+ *
57
+ * @param Copernica_MarketingSoftware_Model_Copernica_Entity_Customer $customer
58
  */
59
  public function viewedBy(Copernica_MarketingSoftware_Model_Copernica_Entity_Customer $customer)
60
  {
61
+ $customer->setStore($this->_productEntity->getStoreView());
62
+
63
  $profileId = Mage::helper('marketingsoftware/api')->getProfileId(array(
64
  'id' => $customer->getCustomerId(),
65
+ 'storeView' => (string) $customer->getStoreView(),
66
  'email' => $customer->getEmail(),
67
+ ));
68
+
69
+ if (!$profileId) {
70
+ $profileId = $this->_createProfile($customer);
71
+
72
+ if(!$profileId) {
73
+ return false;
74
+ }
75
+ }
76
 
 
 
 
 
77
  $collectionId = Mage::helper('marketingsoftware/config')->getViewedProductCollectionId();
78
+
79
+ if ($collectionId) {
80
+ Mage::helper('marketingsoftware/rest_request')->put('/profile/'.$profileId.'/subprofiles/'.$collectionId, $this->_getSubprofileData(), array(
81
+ 'fields[]' => 'product_id=='.$this->_productEntity->getId(),
82
+ 'create' => 'true'
83
+ ));
84
+ }
85
+ }
86
+
87
+ /**
88
+ * Set REST product entity
89
+ *
90
+ * @param Copernica_MarketingSoftware_Model_Copernica_Entity_Product $productEntity
91
+ */
92
+ public function setProductEntity(Copernica_MarketingSoftware_Model_Copernica_Entity_Product $productEntity) {
93
+ $this->_productEntity = $productEntity;
94
  }
95
  }
app/code/community/Copernica/MarketingSoftware/Model/{REST → Rest}/Quote.php RENAMED
@@ -27,48 +27,54 @@
27
  /**
28
  * Quote REST entity
29
  */
30
- class Copernica_MarketingSoftware_Model_REST_Quote extends Copernica_MarketingSoftware_Model_REST
31
  {
32
  /**
33
  * Copernica entity
34
- * @var Copernica_MarketingSoftware_Model_Copernica_Entity_Quote
 
35
  */
36
- private $quote;
37
-
38
- /**
39
- * Construct REST entity
40
- * @param Copernica_MarketingSoftware_Model_Copernica_Entity_Quote
41
- */
42
- public function __construct($quote)
43
- {
44
- $this->quote = $quote;
45
- }
46
 
47
  /**
48
  * Fetch quote Id
 
49
  * @return string
50
  */
51
  public function fetchId()
52
  {
53
- return $this->quote->getId();
54
  }
55
 
56
  /**
57
  * Fetch quote status
 
58
  * @return string
59
  */
60
  public function fetchStatus()
61
  {
62
- return $this->quote->getStatus();
63
  }
64
 
65
  /**
66
  * Sync quote with customer
67
- * @param Copernica_MarketingSoftware_Model_Copernica_Entity_Customer
 
68
  */
69
- public function syncWithCustomer($customer)
70
  {
71
- // sync all quote items with customer
72
- foreach ($this->quote->getItems() as $item) $item->getREST()->syncWithQuote($customer, $this->quote->getId());
 
 
 
 
 
 
 
 
 
 
 
73
  }
74
  }
27
  /**
28
  * Quote REST entity
29
  */
30
+ class Copernica_MarketingSoftware_Model_Rest_Quote extends Copernica_MarketingSoftware_Model_Rest
31
  {
32
  /**
33
  * Copernica entity
34
+ *
35
+ * @var Copernica_MarketingSoftware_Model_Copernica_Entity_Quote
36
  */
37
+ protected $_quoteEntity;
 
 
 
 
 
 
 
 
 
38
 
39
  /**
40
  * Fetch quote Id
41
+ *
42
  * @return string
43
  */
44
  public function fetchId()
45
  {
46
+ return $this->_quoteEntity->getId();
47
  }
48
 
49
  /**
50
  * Fetch quote status
51
+ *
52
  * @return string
53
  */
54
  public function fetchStatus()
55
  {
56
+ return $this->_quoteEntity->getStatus();
57
  }
58
 
59
  /**
60
  * Sync quote with customer
61
+ *
62
+ * @param Copernica_MarketingSoftware_Model_Copernica_Entity_Customer $customer
63
  */
64
+ public function syncWithCustomer(Copernica_MarketingSoftware_Model_Copernica_Entity_Customer $customer)
65
  {
66
+ foreach ($this->_quoteEntity->getItems() as $quoteItemEntity) {
67
+ $restQuoteItem = $quoteItemEntity->getRestQuoteItem();
68
+ $restQuoteItem->syncWithQuote($customer, $this->_quoteEntity->getId());
69
+ }
70
+ }
71
+
72
+ /**
73
+ * Set REST quote entity
74
+ *
75
+ * @param Copernica_MarketingSoftware_Model_Copernica_Entity_Quote $quoteEntity
76
+ */
77
+ public function setQuoteEntity(Copernica_MarketingSoftware_Model_Copernica_Entity_Quote $quoteEntity) {
78
+ $this->_quoteEntity = $quoteEntity;
79
  }
80
  }
app/code/community/Copernica/MarketingSoftware/Model/Rest/Quote/Item.php ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class Copernica_MarketingSoftware_Model_Rest_Quote_Item extends Copernica_MarketingSoftware_Model_Rest_Order_Item
4
+ {
5
+ /**
6
+ * Customer that will be used to send data
7
+ *
8
+ * @var Copernica_MarketingSoftware_Model_Copernica_Entity_Quote_Item
9
+ */
10
+ protected $_quoteItemEntity;
11
+
12
+ /**
13
+ * Sync item with quote
14
+ *
15
+ * @param Copernica_MarketingSoftware_Model_Copernica_Entity_Customer $customer
16
+ * @param int $quoteId
17
+ * @return boolean
18
+ */
19
+ public function syncWithQuote(Copernica_MarketingSoftware_Model_Copernica_Entity_Customer $customer, $quoteId)
20
+ {
21
+ $customer->setStore($this->_quoteItemEntity->getStoreView());
22
+
23
+ $profileId = Mage::helper('marketingsoftware/api')->getProfileId(array(
24
+ 'id' => $customer->getCustomerId(),
25
+ 'storeView' => (string) $customer->getStoreView(),
26
+ 'email' => $customer->getEmail(),
27
+ ));
28
+
29
+ if (!$profileId) {
30
+ $profileId = $this->_createProfile($customer);
31
+
32
+ if(!$profileId) {
33
+ return false;
34
+ }
35
+ }
36
+
37
+ $quoteItemCollectionId = Mage::helper('marketingsoftware/config')->getQuoteItemCollectionId();
38
+
39
+ if ($quoteItemCollectionId) {
40
+ Mage::helper('marketingsoftware/rest_request')->put('/profile/'.$profileId.'/subprofiles/'.$quoteItemCollectionId, $this->getQuoteItemSubprofileData($quoteId), array(
41
+ 'fields' => array(
42
+ 'item_id=='.$this->_quoteItemEntity->getId(),
43
+ 'quote_id=='.$quoteId
44
+ ),
45
+ 'create' => 'true'
46
+ ));
47
+ }
48
+
49
+ return true;
50
+ }
51
+
52
+ /**
53
+ * Prepare subprofile date
54
+ *
55
+ * @param int $quoteId
56
+ * @return array
57
+ */
58
+ public function getQuoteItemSubprofileData($quoteId)
59
+ {
60
+ $syncedFields = Mage::helper('marketingsoftware/config')->getLinkedQuoteItemFields();
61
+
62
+ $data = $this->_getRequestData($this->_quoteItemEntity, $syncedFields);
63
+ $data['quote_id'] = $quoteId;
64
+ $data['item_id'] = $this->_quoteItemEntity->getId();
65
+ $data['status'] = $this->_quoteItemEntity->getStatus();
66
+
67
+
68
+ return $data;
69
+ }
70
+
71
+ /**
72
+ * Set REST quote item entity
73
+ *
74
+ * @param Copernica_MarketingSoftware_Model_Copernica_Entity_Quote_Item $quoteItemEntity
75
+ */
76
+ public function setQuoteItemEntity(Copernica_MarketingSoftware_Model_Copernica_Entity_Quote_Item $quoteItemEntity)
77
+ {
78
+ $this->_quoteItemEntity = $quoteItemEntity;
79
+ }
80
+ }
app/code/community/Copernica/MarketingSoftware/Model/Rest/Subscription.php ADDED
@@ -0,0 +1,96 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Copernica Marketing Software
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0).
8
+ * It is available through the world-wide-web at this URL:
9
+ * http://opensource.org/licenses/osl-3.0.php
10
+ * If you are unable to obtain a copy of the license through the
11
+ * world-wide-web, please send an email to copernica@support.cream.nl
12
+ * so we can send you a copy immediately.
13
+ *
14
+ * DISCLAIMER
15
+ *
16
+ * Do not edit or add to this file if you wish to upgrade this software
17
+ * to newer versions in the future. If you wish to customize this module
18
+ * for your needs please refer to http://www.magento.com/ for more
19
+ * information.
20
+ *
21
+ * @category Copernica
22
+ * @package Copernica_MarketingSoftware
23
+ * @copyright Copyright (c) 2011-2012 Copernica & Cream. (http://docs.cream.nl/)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ /**
28
+ * Entity that will take care of synchronizing subscription with copernica.
29
+ */
30
+ class Copernica_MarketingSoftware_Model_Rest_Subscription extends Copernica_MarketingSoftware_Model_Rest
31
+ {
32
+ /**
33
+ * Subscription entity that we will use to create proper profile inside
34
+ * copernica database.
35
+ *
36
+ * @var Copernica_MarketingSoftware_Model_Copernica_Entity_Subscription
37
+ */
38
+ protected $_subscriptionEntity;
39
+
40
+ /**
41
+ * Get data that should be update in copernica database
42
+ *
43
+ * @param string $storeView
44
+ * @return array
45
+ */
46
+ protected function _getProfileData($storeview)
47
+ {
48
+ $linkedCustomerFields = Mage::helper('marketingsoftware/config')->getLinkedCustomerFields();
49
+
50
+ return array(
51
+ 'customer_id' => $this->_subscriptionEntity->getCustomerId(),
52
+ $linkedCustomerFields['email'] => $this->_subscriptionEntity->getEmail(),
53
+ $linkedCustomerFields['group'] => $this->_subscriptionEntity->getGroup(),
54
+ $linkedCustomerFields['newsletter'] => $this->_subscriptionEntity->getStatus(),
55
+ $linkedCustomerFields['storeView'] => $storeview
56
+ );
57
+ }
58
+
59
+ /**
60
+ * Synchronize magento subscriber with copernica profile.
61
+ *
62
+ * @return boolean
63
+ */
64
+ public function sync()
65
+ {
66
+ $storeview = (string) $this->_subscriptionEntity->getStoreView();
67
+
68
+ $profileId = Mage::helper('marketingsoftware/api')->getProfileId(array(
69
+ 'id' => null,
70
+ 'storeView' => $storeview,
71
+ 'email' => $this->_subscriptionEntity->getEmail(),
72
+ ));
73
+
74
+ $request = Mage::helper('marketingsoftware/rest_request');
75
+
76
+ if ($profileId) {
77
+ $request->post('/profile/'.$profileId.'/fields/', $this->_getProfileData($storeview));
78
+ } else {
79
+ $databaseId = Mage::helper('marketingsoftware/config')->getDatabaseId();
80
+
81
+ $request->post('/database/'.$databaseId.'/profiles/', $this->_getProfileData($storeview));
82
+ }
83
+
84
+ return true;
85
+ }
86
+
87
+ /**
88
+ * Set REST subscription entity
89
+ *
90
+ * @param Copernica_MarketingSoftware_Model_Copernica_Entity_Subscription $subscriptionEntity
91
+ */
92
+ public function setSubscriptionEntity(Copernica_MarketingSoftware_Model_Copernica_Entity_Quote $subscriptionEntity)
93
+ {
94
+ $this->_subscriptionEntity = $subscriptionEntity;
95
+ }
96
+ }
app/code/community/Copernica/MarketingSoftware/Model/Rest/Wishlist.php ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Copernica Marketing Software
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0).
8
+ * It is available through the world-wide-web at this URL:
9
+ * http://opensource.org/licenses/osl-3.0.php
10
+ * If you are unable to obtain a copy of the license through the
11
+ * world-wide-web, please send an email to copernica@support.cream.nl
12
+ * so we can send you a copy immediately.
13
+ *
14
+ * DISCLAIMER
15
+ *
16
+ * Do not edit or add to this file if you wish to upgrade this software
17
+ * to newer versions in the future. If you wish to customize this module
18
+ * for your needs please refer to http://www.magento.com/ for more
19
+ * information.
20
+ *
21
+ * @category Copernica
22
+ * @package Copernica_MarketingSoftware
23
+ * @copyright Copyright (c) 2011-2012 Copernica & Cream. (http://docs.cream.nl/)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ /**
28
+ * Wishlist REST entity
29
+ */
30
+ class Copernica_MarketingSoftware_Model_Rest_Wishlist extends Copernica_MarketingSoftware_Model_Rest
31
+ {
32
+ /**
33
+ * Copernica entity
34
+ *
35
+ * @var Copernica_MarketingSoftware_Model_Copernica_Entity_Wishlist
36
+ */
37
+ protected $_wishlistEntity;
38
+
39
+ /**
40
+ * Fetch wishlist Id
41
+ *
42
+ * @return string
43
+ */
44
+ public function fetchId()
45
+ {
46
+ return $this->_wishlistEntity->getId();
47
+ }
48
+
49
+ /**
50
+ * Sync wishlist with customer
51
+ *
52
+ * @param Copernica_MarketingSoftware_Model_Copernica_Entity_Customer $customer
53
+ */
54
+ public function syncWithCustomer(Copernica_MarketingSoftware_Model_Copernica_Entity_Customer $customer)
55
+ {
56
+ foreach ($this->_wishlistEntity->getItems() as $wishlistItemEntity) {
57
+ $restWishlistItem = $wishlistItemEntity->getRestWishlistItem();
58
+ $restWishlistItem->syncWithWishlist($customer, $this->_wishlistEntity->getId());
59
+ }
60
+ }
61
+
62
+ /**
63
+ * Set REST wishlist entity
64
+ *
65
+ * @param Copernica_MarketingSoftware_Model_Copernica_Entity_Wishlist $wishlistEntity
66
+ */
67
+ public function setWishlistEntity(Copernica_MarketingSoftware_Model_Copernica_Entity_Wishlist $wishlistEntity) {
68
+ $this->_wishlistEntity = $wishlistEntity;
69
+ }
70
+ }
app/code/community/Copernica/MarketingSoftware/Model/Rest/Wishlist/Item.php ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class Copernica_MarketingSoftware_Model_Rest_Wishlist_Item extends Copernica_MarketingSoftware_Model_Rest
4
+ {
5
+ /**
6
+ * Wishlist that will be used to send data
7
+ *
8
+ * @var Copernica_MarketingSoftware_Model_Copernica_Entity_Wishlist_Item
9
+ */
10
+ protected $_wishlistItemEntity;
11
+
12
+ /**
13
+ * Sync item with wishlist
14
+ *
15
+ * @param Copernica_MarketingSoftware_Model_Copernica_Entity_Customer $customer
16
+ * @param int $wishlistId
17
+ * @return boolean
18
+ */
19
+ public function syncWithCustomer(Copernica_MarketingSoftware_Model_Copernica_Entity_Customer $customer)
20
+ {
21
+ $customer->setStore($this->_wishlistItemEntity->getStoreView());
22
+
23
+ $profileId = Mage::helper('marketingsoftware/api')->getProfileId(array(
24
+ 'id' => $customer->getCustomerId(),
25
+ 'storeView' => (string) $customer->getStoreView(),
26
+ 'email' => $customer->getEmail(),
27
+ ));
28
+
29
+ if (!$profileId) {
30
+ $profileId = $this->_createProfile($customer);
31
+
32
+ if(!$profileId) {
33
+ return false;
34
+ }
35
+ }
36
+
37
+ $wishlistId = $this->_wishlistItemEntity->getWishlistId();
38
+
39
+ $wishlistItemCollectionId = Mage::helper('marketingsoftware/config')->getWishlistItemCollectionId();
40
+
41
+ if ($wishlistItemCollectionId) {
42
+ Mage::helper('marketingsoftware/rest_request')->put('/profile/'.$profileId.'/subprofiles/'.$wishlistItemCollectionId, $this->getWishlistSubprofileData($wishlistId), array(
43
+ 'fields' => array(
44
+ 'item_id=='.$this->_wishlistItemEntity->getId(),
45
+ 'wishlist_id=='.$wishlistId
46
+ ),
47
+ 'create' => 'true'
48
+ ));
49
+ }
50
+
51
+ return true;
52
+ }
53
+
54
+ /**
55
+ * Prepare subprofile date
56
+ *
57
+ * @param int $wishlistId
58
+ * @return array
59
+ */
60
+ public function getWishlistSubprofileData($wishlistId)
61
+ {
62
+ $syncedFields = Mage::helper('marketingsoftware/config')->getLinkedWishlistItemFields();
63
+
64
+ $data = $this->_getRequestData($this->_wishlistItemEntity, $syncedFields);
65
+ $data['wishlist_id'] = $wishlistId;
66
+ $data['item_id'] = $this->_wishlistItemEntity->getId();
67
+
68
+ return $data;
69
+ }
70
+
71
+ /**
72
+ * Set REST wishlist item entity
73
+ *
74
+ * @param Copernica_MarketingSoftware_Model_Copernica_Entity_Wishlist_Item $wishlistItemEntity
75
+ */
76
+ public function setWishlistItemEntity(Copernica_MarketingSoftware_Model_Copernica_Entity_Wishlist_Item $wishlistItemEntity)
77
+ {
78
+ $this->_wishlistItemEntity = $wishlistItemEntity;
79
+ }
80
+ }
app/code/community/Copernica/MarketingSoftware/Model/{SyncProfile.php → Sync/Profile.php} RENAMED
@@ -30,18 +30,19 @@
30
  * A sync profile is a combination of REST credentials that will be used as
31
  * target in copernica environment.
32
  */
33
- class Copernica_MarketingSoftware_Model_SyncProfile extends Mage_Core_Model_Abstract
34
  {
35
  /**
36
  * Construct sync profile
37
  */
38
  protected function _construct()
39
  {
40
- $this->_init('marketingsoftware/syncProfile');
41
  }
42
 
43
  /**
44
  * Get client key
 
45
  * @return string
46
  */
47
  public function getClientKey()
@@ -51,14 +52,15 @@ class Copernica_MarketingSoftware_Model_SyncProfile extends Mage_Core_Model_Abst
51
 
52
  /**
53
  * Set client key
54
- * @param string
55
- * @return self
 
56
  */
57
  public function setClientKey($clientKey)
58
  {
59
- // if we are changing client key we have to remove access token, since
60
- // it does not point to correct sync profile
61
- if (parent::getData('client_key') != $clientKey) parent::setData('access_token', '');
62
 
63
  parent::setData('client_key', $clientKey);
64
 
@@ -67,6 +69,7 @@ class Copernica_MarketingSoftware_Model_SyncProfile extends Mage_Core_Model_Abst
67
 
68
  /**
69
  * This function will return client secret
 
70
  * @return string
71
  */
72
  public function getClientSecret()
@@ -76,12 +79,15 @@ class Copernica_MarketingSoftware_Model_SyncProfile extends Mage_Core_Model_Abst
76
 
77
  /**
78
  * Set client secret for this sync profile
79
- * @param string
80
- * @return self
 
81
  */
82
  public function setClientSecret($clientSecret)
83
  {
84
- if (parent::getData('client_secret') != $clientSecret) parent::setData('access_token', '');
 
 
85
 
86
  parent::setData('client_secret', $clientSecret);
87
 
@@ -90,6 +96,7 @@ class Copernica_MarketingSoftware_Model_SyncProfile extends Mage_Core_Model_Abst
90
 
91
  /**
92
  * Get access token of this sync profile
 
93
  * @return string
94
  */
95
  public function getAccessToken()
@@ -99,8 +106,9 @@ class Copernica_MarketingSoftware_Model_SyncProfile extends Mage_Core_Model_Abst
99
 
100
  /**
101
  * Set access token for this sync profile
102
- * @param string
103
- * @return self
 
104
  */
105
  public function setAccessToken($accessToken)
106
  {
@@ -111,6 +119,7 @@ class Copernica_MarketingSoftware_Model_SyncProfile extends Mage_Core_Model_Abst
111
 
112
  /**
113
  * Get name of this sync profile
 
114
  * @return string
115
  */
116
  public function getName()
@@ -120,8 +129,9 @@ class Copernica_MarketingSoftware_Model_SyncProfile extends Mage_Core_Model_Abst
120
 
121
  /**
122
  * Set name for this sync profile
123
- * @param string
124
- * @return self
 
125
  */
126
  public function setName($name)
127
  {
@@ -142,42 +152,42 @@ class Copernica_MarketingSoftware_Model_SyncProfile extends Mage_Core_Model_Abst
142
 
143
  /**
144
  * Assign store view to current sync profile
145
- * @param StoreView|int
146
- * @return self
 
147
  */
148
  public function assignStoreView($storeView)
149
  {
150
- // we need id, not object
151
- if (is_object($storeView)) $storeView = $storeView->getId();
 
152
 
153
- // get sync profiles bindings
154
  $bindings = Mage::helper('marketingsoftware/config')->getSyncProfilesBindings();
155
 
156
- // ensure that we have an id for this sync profile
157
- if ($this->getId() >= 1) $this->save();
 
158
 
159
- // make binding for
160
  $bindings[$store] = $this->getId();
161
 
162
- // store updated bindings
163
  Mage::helper('marketingsoftware/config')->setSyncProfilesBindings($bindings);
164
 
165
- // allow chaining
166
  return $this;
167
  }
168
 
169
  /**
170
  * Clear assigned store views
171
- * @return self
 
172
  */
173
  public function clearStoreViews()
174
  {
175
- $bindigns = Mage::helper('marketingsoftware/config')->getSyncProfilesBindings();
176
 
177
- foreach ($bindings as $storeId => $profileId)
178
- {
179
- // uset all
180
- if ($profileId == $this->getId()) $bindings[$storeId] = -1;
181
  }
182
 
183
  Mage::helper('marketingsoftware/config')->setSyncProfilesBindings($bindings);
@@ -185,5 +195,4 @@ class Copernica_MarketingSoftware_Model_SyncProfile extends Mage_Core_Model_Abst
185
  //@todo implement
186
  return $this;
187
  }
188
-
189
- }
30
  * A sync profile is a combination of REST credentials that will be used as
31
  * target in copernica environment.
32
  */
33
+ class Copernica_MarketingSoftware_Model_Sync_Profile extends Mage_Core_Model_Abstract
34
  {
35
  /**
36
  * Construct sync profile
37
  */
38
  protected function _construct()
39
  {
40
+ $this->_init('marketingsoftware/sync_profile');
41
  }
42
 
43
  /**
44
  * Get client key
45
+ *
46
  * @return string
47
  */
48
  public function getClientKey()
52
 
53
  /**
54
  * Set client key
55
+ *
56
+ * @param string $clientKey
57
+ * @return Copernica_MarketingSoftware_Model_Sync_Profile
58
  */
59
  public function setClientKey($clientKey)
60
  {
61
+ if (parent::getData('client_key') != $clientKey) {
62
+ parent::setData('access_token', '');
63
+ }
64
 
65
  parent::setData('client_key', $clientKey);
66
 
69
 
70
  /**
71
  * This function will return client secret
72
+ *
73
  * @return string
74
  */
75
  public function getClientSecret()
79
 
80
  /**
81
  * Set client secret for this sync profile
82
+ *
83
+ * @param string $clientSecret
84
+ * @return Copernica_MarketingSoftware_Model_Sync_Profile
85
  */
86
  public function setClientSecret($clientSecret)
87
  {
88
+ if (parent::getData('client_secret') != $clientSecret) {
89
+ parent::setData('access_token', '');
90
+ }
91
 
92
  parent::setData('client_secret', $clientSecret);
93
 
96
 
97
  /**
98
  * Get access token of this sync profile
99
+ *
100
  * @return string
101
  */
102
  public function getAccessToken()
106
 
107
  /**
108
  * Set access token for this sync profile
109
+ *
110
+ * @param string $accessToken
111
+ * @return Copernica_MarketingSoftware_Model_Sync_Profile
112
  */
113
  public function setAccessToken($accessToken)
114
  {
119
 
120
  /**
121
  * Get name of this sync profile
122
+ *
123
  * @return string
124
  */
125
  public function getName()
129
 
130
  /**
131
  * Set name for this sync profile
132
+ *
133
+ * @param string $name
134
+ * @return Copernica_MarketingSoftware_Model_Sync_Profile
135
  */
136
  public function setName($name)
137
  {
152
 
153
  /**
154
  * Assign store view to current sync profile
155
+ *
156
+ * @param StoreView|int $storeView
157
+ * @return Copernica_MarketingSoftware_Model_Sync_Profile
158
  */
159
  public function assignStoreView($storeView)
160
  {
161
+ if (is_object($storeView)) {
162
+ $storeView = $storeView->getId();
163
+ }
164
 
 
165
  $bindings = Mage::helper('marketingsoftware/config')->getSyncProfilesBindings();
166
 
167
+ if ($this->getId() >= 1) {
168
+ $this->save();
169
+ }
170
 
 
171
  $bindings[$store] = $this->getId();
172
 
 
173
  Mage::helper('marketingsoftware/config')->setSyncProfilesBindings($bindings);
174
 
 
175
  return $this;
176
  }
177
 
178
  /**
179
  * Clear assigned store views
180
+ *
181
+ * @return Copernica_MarketingSoftware_Model_Sync_Profile
182
  */
183
  public function clearStoreViews()
184
  {
185
+ $bindings = Mage::helper('marketingsoftware/config')->getSyncProfilesBindings();
186
 
187
+ foreach ($bindings as $storeId => $profileId) {
188
+ if ($profileId == $this->getId()) {
189
+ $bindings[$storeId] = -1;
190
+ }
191
  }
192
 
193
  Mage::helper('marketingsoftware/config')->setSyncProfilesBindings($bindings);
195
  //@todo implement
196
  return $this;
197
  }
198
+ }
 
app/code/community/Copernica/MarketingSoftware/Model/Sync/Status.php ADDED
@@ -0,0 +1,149 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ /**
4
+ * This class will present synchronization status. As well options set with
5
+ * synchronization.
6
+ */
7
+ class Copernica_MarketingSoftware_Model_Sync_Status implements Serializable
8
+ {
9
+ /**
10
+ * What is the last customer Id
11
+ *
12
+ * @var int
13
+ */
14
+ protected $_lastCustomerId = 0;
15
+
16
+ /**
17
+ * What is the last order id
18
+ *
19
+ * @var int
20
+ */
21
+ protected $_lastOrderId = 0;
22
+
23
+ /**
24
+ * What is the last subscription id
25
+ *
26
+ * @var int
27
+ */
28
+ protected $_lastSubscriptionId = 0;
29
+
30
+ /**
31
+ * This array should contain all stores Ids that we want to use as a filter.
32
+ *
33
+ * @var array
34
+ */
35
+ protected $_storesFilter = array();
36
+
37
+ /**
38
+ * Serialize instance
39
+ *
40
+ * @return string
41
+ */
42
+ public function serialize()
43
+ {
44
+ return serialize(array(
45
+ 'lastCustomer' => $this->_lastCustomerId,
46
+ 'lastOrder' => $this->_lastOrderId,
47
+ 'lastSub' => $this->_lastSubscriptionId,
48
+ 'storesFilter' => $this->_storesFilter
49
+ ));
50
+ }
51
+
52
+ /**
53
+ * Unserialize instance
54
+ *
55
+ * @param string $data
56
+ */
57
+ public function unserialize($data)
58
+ {
59
+ $data = unserialize($data);
60
+
61
+ if(isset($data['lastCustomer'])) {
62
+ $this->_lastCustomerId = $data['lastCustomer'];
63
+ }
64
+
65
+ if(isset($data['lastOrder'])) {
66
+ $this->_lastOrderId = $data['lastOrder'];
67
+ }
68
+
69
+ if(isset($data['lastSub'])) {
70
+ $this->_lastSubscriptionId = $data['lastSub'];
71
+ }
72
+
73
+ if(isset($data['storesFilter'])) {
74
+ $this->_storesFilter = $data['storesFilter'];
75
+ }
76
+ }
77
+
78
+ /**
79
+ * Since we still support PHP 5.3 we don't have ability to use JsonSerializable
80
+ * interface, so we will serialize array representation of this object.
81
+ *
82
+ * @return array
83
+ */
84
+ public function toArray()
85
+ {
86
+ return array (
87
+ 'lastCustomer' => $this->_lastCustomerId,
88
+ 'lastOrder' => $this->_lastOrderId,
89
+ 'lastSub' => $this->_lastSubscriptionId,
90
+ 'storesFilter' => $this->_storesFilter
91
+ );
92
+ }
93
+
94
+ /**
95
+ * Create instance of this class from stdClass
96
+ *
97
+ * @param StdClass $stdObject
98
+ * @return Copernica_MarketingSoftware_Model_Sync_Status
99
+ */
100
+ static public function fromStd(StdClass $stdObject)
101
+ {
102
+ $instance = Mage::getModel('marketingsoftware/sync_status');
103
+
104
+ $trans = array(
105
+ 'lastCustomer' => '_lastCustomerId',
106
+ 'lastOrder' => '_lastOrderId',
107
+ 'lastSub' => '_lastSubscriptionId',
108
+ 'storesFilter' => '_storesFilter',
109
+ );
110
+
111
+ foreach ($trans as $inObject => $property) {
112
+ if (property_exists($stdObject, $inObject)) {
113
+ $instance->$property = $stdObject->$inObject;
114
+ }
115
+ }
116
+
117
+ return $instance;
118
+ }
119
+
120
+ /**
121
+ * We want to overload __call so we can define setters and getters.
122
+ *
123
+ * @param string $methodName
124
+ * @param array $arguments
125
+ * @return mixed
126
+ */
127
+ public function __call($methodName, $arguments)
128
+ {
129
+ $action = substr($methodName, 0, 3);
130
+
131
+ $property = '_';
132
+ $property .= substr($methodName, 3);
133
+ $property{1} = strtolower($property{1});
134
+
135
+ //if(!property_exists($this, $property)) {
136
+ // return parent::__call($methodName, $arguments);
137
+ //}
138
+
139
+ switch ($action) {
140
+ case 'set':
141
+ $this->$property = $arguments[0];
142
+
143
+ return $this;
144
+
145
+ case 'get':
146
+ return $this->$property;
147
+ }
148
+ }
149
+ }
app/code/community/Copernica/MarketingSoftware/Model/SyncStatus.php DELETED
@@ -1,140 +0,0 @@
1
- <?php
2
-
3
- /**
4
- * This class will present synchronization status. As well options set with
5
- * synchronization.
6
- */
7
- class Copernica_MarketingSoftware_Model_SyncStatus implements Serializable
8
- {
9
- /**
10
- * What is the last customer Id
11
- * @var int
12
- */
13
- private $lastCustomerId = 0;
14
-
15
- /**
16
- * What is the last order id
17
- * @var int
18
- */
19
- private $lastOrderId = 0;
20
-
21
- /**
22
- * What is the last subscription id
23
- * @var int
24
- */
25
- private $lastSubscriptionId = 0;
26
-
27
- /**
28
- * This array should contain all stores Ids that we want to use as a filter.
29
- * @var array
30
- */
31
- private $storesFilter = array();
32
-
33
- /**
34
- * Serialize instance
35
- * @return string
36
- */
37
- public function serialize()
38
- {
39
- // serialize object as data array
40
- return serialize(array(
41
- 'lastCustomer' => $this->lastCustomerId,
42
- 'lastOrder' => $this->lastOrderId,
43
- 'lastSub' => $this->lastSubscriptionId,
44
- 'storesFilter' => $this->storesFilter
45
- ));
46
- }
47
-
48
- /**
49
- * Unserialize instance
50
- * @param string
51
- */
52
- public function unserialize($data)
53
- {
54
- // unserialize data array
55
- $data = unserialize($data);
56
-
57
- // set members
58
- if(isset($data['lastCustomer'])) $this->lastCustomerId = $data['lastCustomer'];
59
- if(isset($data['lastOrder'])) $this->lastOrderId = $data['lastOrder'];
60
- if(isset($data['lastSub'])) $this->lastSubscriptionId = $data['lastSub'];
61
- if(isset($data['storesFilter'])) $this->storesFilter = $data['storesFilter'];
62
- }
63
-
64
- /**
65
- * Since we still support PHP 5.3 we don't have ability to use JsonSerializable
66
- * interface, so we will serialize array representation of this object.
67
- * @return array
68
- */
69
- public function toArray()
70
- {
71
- return array (
72
- 'lastCustomer' => $this->lastCustomerId,
73
- 'lastOrder' => $this->lastOrderId,
74
- 'lastSub' => $this->lastSubscriptionId,
75
- 'storesFilter' => $this->storesFilter
76
- );
77
- }
78
-
79
- /**
80
- * Create instance of this class from stdClass
81
- * @param StdClass
82
- * @return Copernica_MarketingSoftware_Model_SyncStatus
83
- */
84
- static public function fromStd($stdObject)
85
- {
86
- // create new instance
87
- $instance = new Copernica_MarketingSoftware_Model_SyncStatus();
88
-
89
- $trans = array(
90
- 'lastCustomer' => 'lastCustomerId',
91
- 'lastOrder' => 'lastOrderId',
92
- 'lastSub' => 'lastSubscriptionId',
93
- 'storesFilter' => 'storesFilter',
94
- );
95
-
96
- // iterate over all properties that we want to take into account
97
- foreach ($trans as $inObject => $property)
98
- {
99
- if (property_exists($stdObject, $inObject)) $instance->$property = $stdObject->$inObject;
100
- }
101
-
102
- // return instance
103
- return $instance;
104
- }
105
-
106
- /**
107
- * We want to overload __call so we can define setters and getters.
108
- * @param string
109
- * @param array
110
- * @return mixed
111
- */
112
- public function __call($methodName, $argumetns) {
113
- // get action
114
- $action = substr($methodName, 0, 3);
115
-
116
- // get the property
117
- $property = substr($methodName, 3);
118
-
119
- // cause some really old PHP can be used...
120
- $property{0} = strtolower($property{0});
121
-
122
- // we are magically serving only setters and getters
123
- if(!property_exists($this, $property)) return parent::__call($methodName, $argumetns);
124
-
125
- // what we want to do?
126
- switch ($action) {
127
- // do we want to set a property?
128
- case 'set':
129
- // well set the property
130
- $this->$property = $argumetns[0];
131
-
132
- // allow chainig
133
- return $this;
134
- // do we want to get a property?
135
- case 'get':
136
- // return property
137
- return $this->$property;
138
- }
139
- }
140
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app/code/community/Copernica/MarketingSoftware/controllers/Adminhtml/Marketingsoftware/AccessTokenController.php ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Copernica Marketing Software
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0).
8
+ * It is available through the world-wide-web at this URL:
9
+ * http://opensource.org/licenses/osl-3.0.php
10
+ * If you are unable to obtain a copy of the license through the
11
+ * world-wide-web, please send an email to copernica@support.cream.nl
12
+ * so we can send you a copy immediately.
13
+ *
14
+ * DISCLAIMER
15
+ *
16
+ * Do not edit or add to this file if you wish to upgrade this software
17
+ * to newer versions in the future. If you wish to customize this module
18
+ * for your needs please refer to http://www.magento.com/ for more
19
+ * information.
20
+ *
21
+ * @category Copernica
22
+ * @package Copernica_MarketingSoftware
23
+ * @copyright Copyright (c) 2011-2012 Copernica & Cream. (http://docs.cream.nl/)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ class Copernica_MarketingSoftware_AdminHtml_MarketingSoftware_AccesstokenController extends Copernica_MarketingSoftware_Controller_Action
28
+ {
29
+ /**
30
+ * Handler most basic index action
31
+ */
32
+ public function indexAction()
33
+ {
34
+ $this->loadLayout();
35
+
36
+ $this->_setActiveMenu('copernica');
37
+
38
+ $layout = $this->getLayout();
39
+
40
+ $page = $layout->createBlock('marketingsoftware/adminhtml_marketingsoftware_accesstoken');
41
+
42
+ $layout->getBlock('content')->append($page);
43
+
44
+ $layout->getBlock('head')->setTitle($this->__('Access Token / Copernica Marketing Software / Magento Admin'));
45
+
46
+ $session = Mage::getSingleton('adminhtml/session');
47
+
48
+ if (!$session->getState()) {
49
+ $session->setState($this->generateState());
50
+ }
51
+
52
+ $this->renderLayout();
53
+ }
54
+
55
+ /**
56
+ * Handle form action
57
+ *
58
+ * @return string
59
+ */
60
+ public function sendAction()
61
+ {
62
+ $data = $this->getRequest()->getPost();
63
+
64
+ Mage::helper('marketingsoftware/config')->setAccessToken($data['access_token']);
65
+
66
+ return $this->_redirect('*/marketingsoftware_settings/index');
67
+ }
68
+ }
app/code/community/Copernica/MarketingSoftware/controllers/Adminhtml/Marketingsoftware/AjaxcollectionController.php CHANGED
@@ -35,96 +35,60 @@ class Copernica_MarketingSoftware_Adminhtml_Marketingsoftware_AjaxcollectionCont
35
  */
36
  public function validateAction()
37
  {
38
- // get POST variables
39
  $post = $this->getRequest()->getPost();
40
 
41
- // get config helper
42
  $config = Mage::helper('marketingsoftware/config');
43
- $validator = Mage::helper('marketingsoftware/ApiValidator');
44
 
45
- // list of problems
46
  $problems = array();
47
 
48
- try
49
- {
50
- if ($post['type'] == 'main')
51
- {
52
- try
53
- {
54
- // validate database
55
  $validator->validateDatabase($post['name']);
56
- }
57
- catch (Copernica_MarketingSoftware_Exception $exception)
58
- {
59
  array_push($problems, $exception->getMessage());
60
  }
61
 
62
- // iterate over all fields
63
- foreach ($post['fields'] as $field)
64
- {
65
- // get field magento and copernica name
66
  list($magento, $copernica) = explode(',', $field);
67
 
68
- try
69
- {
70
- // validate database field
71
  $validator->validateDatabaseField($post['name'], $copernica, $magento);
72
- }
73
- catch (Copernica_MarketingSoftware_Exception $exception)
74
- {
75
  array_push($problems, $magento.','.$exception->getMessage());
76
  }
77
  }
78
- }
79
- else
80
- {
81
- // get database name
82
  $databaseName = $config->getDatabaseName();
83
 
84
- try
85
- {
86
- // validates collection
87
  $validator->validateCollection($databaseName, $post['name'], $post['type']);
88
- }
89
- catch (Copernica_MarketingSoftware_Exception $exception)
90
- {
91
  array_push($problems, $exception->getMessage());
92
  }
93
 
94
-
95
- // iterate over all fields
96
- foreach ($post['fields'] as $field)
97
- {
98
- // get field magento and copernica name
99
  list($magento, $copernica) = explode(',', $field);
100
 
101
- try
102
- {
103
- // validate collection field
104
  $validator->validateCollectionField($databaseName, $post['name'], $post['type'], $copernica, $magento);
105
- }
106
- catch (Copernica_MarketingSoftware_Exception $exception)
107
- {
108
  array_push($problems, $magento.','.$exception->getMessage());
109
  }
110
  }
111
  }
112
  }
113
-
114
  /**
115
  * General exceptions should not happen but, just in case we want to
116
  * handle them here.
117
  */
118
- catch (Exception $exception)
119
- {
120
- // tell magento to log exception
121
  Mage::logException($exception);
122
 
123
- // we have an critical error
124
  $this->getResponse()->setBody('Critical error. Check error logs form more info.');
125
  }
126
 
127
- // set the response
128
  $this->getResponse()->setBody(json_encode($problems));
129
  }
130
 
@@ -133,40 +97,40 @@ class Copernica_MarketingSoftware_Adminhtml_Marketingsoftware_AjaxcollectionCont
133
  */
134
  public function fetchAction()
135
  {
136
- // get post variables
137
  $post = $this->getRequest()->getPost();
138
 
139
- // get response
140
  $response = $this->getResponse();
141
 
142
- // check if we have a required name property
143
- if (!isset($post['name'])) return $response->setBody(json_encode(array(
144
- 'message' => 'Invalid input'
145
- )));
 
146
 
147
- // get config and data to local scope
148
  $config = Mage::helper('marketingsoftware/config');
149
  $data = Mage::helper('marketingsoftware/data');
150
 
151
- // get stored database name
152
  $database = $config->getDatabaseName();
153
 
154
- // check if we have a database to communicate
155
- if (empty($database) && $post['name'] != 'main') return $response->setBody(json_encode(array(
156
- 'error' => 'no database'
157
- )));
 
 
 
 
 
 
 
158
 
159
- // placeholder for linked name
160
  $linkedName = '';
161
 
162
- // placeholders
163
  $linkedFields = array();
164
  $supportedFields = array();
165
  $linkedFields = array();
166
 
167
- // get the collection name
168
- switch ($post['name'])
169
- {
170
  case 'main':
171
  $linkedName = $config->getDatabaseName();
172
  $supportedFields = $data->supportedCustomerFields();
@@ -182,7 +146,7 @@ class Copernica_MarketingSoftware_Adminhtml_Marketingsoftware_AjaxcollectionCont
182
  break;
183
 
184
  case 'orderproducts':
185
- $linkedName = $config->getOrderItemsCollectionName();
186
  $supportedFields = $data->supportedOrderItemFields();
187
  $linkedFields = $config->getLinkedOrderItemFields();
188
  $label = 'Orders items collection';
@@ -196,34 +160,37 @@ class Copernica_MarketingSoftware_Adminhtml_Marketingsoftware_AjaxcollectionCont
196
  break;
197
 
198
  case 'viewedproducts':
199
- $linkedName = $config->getViewedProductCollectionName();
200
  $supportedFields = $data->supportedViewedProductFields();
201
- $linkedFields = $config->getLinkedViewedProductFields();
202
  $label = 'Viewed products collection';
203
  break;
204
 
205
  case 'cartproducts':
206
- $linkedName = $config->getCartItemsCollectionName();
207
- $supportedFields = $data->supportedCartItemFields();
208
- $linkedFields = $config->getLinkedCartItemFields();
209
  $label = 'Cart items collection';
210
  break;
 
 
 
 
 
 
 
211
  }
212
 
213
- // placeholder for fields
214
  $fields = array();
215
 
216
- // iterate over supported fields and construct overall fields
217
- foreach ($supportedFields as $fieldName => $fieldLabel)
218
- {
219
  $fields[] = array (
220
  'magento' => $fieldName,
221
  'label' => $fieldLabel,
222
- 'copernica' => $linkedFields[$fieldName]
223
  );
224
  }
225
 
226
- // set response body
227
  $response->setBody(json_encode(array(
228
  'name' => $post['name'],
229
  'linkedName' => $linkedName,
@@ -234,33 +201,36 @@ class Copernica_MarketingSoftware_Adminhtml_Marketingsoftware_AjaxcollectionCont
234
 
235
  /**
236
  * Create field inside given database.
237
- * @param string the name of the database that will be used
238
- * @param string the name of the field
239
- * @param string the magento field
 
240
  */
241
- private function createDatabaseField($databaseName, $fieldName, $magentoField)
242
  {
243
- // get api builder
244
- $builder = Mage::helper('marketingsoftware/ApiBuilder');
245
 
246
- // check what kind of field we want to create
247
- switch ($magentoField)
248
- {
249
  case 'email':
250
  $builder->createDatabaseEmailField($databaseName, $fieldName);
251
  break;
 
252
  case 'newsletter' :
253
  $builder->createDatabaseNewsletterField($databaseName, $fieldName);
254
  break;
 
255
  case 'birthdate' :
256
  $builder->createDatabaseDateField($databaseName, $fieldName);
257
  break;
 
258
  case 'storeView':
259
- $builder->createDatabaseField($databaseName, $fieldName, array( 'length' => 100 ));
260
  break;
 
261
  case 'registrationDate' :
262
  $builder->createDatabaseDatetimeField($databaseName, $fieldName);
263
  break;
 
264
  default:
265
  $builder->createDatabaseField($databaseName, $fieldName);
266
  break;
@@ -272,91 +242,89 @@ class Copernica_MarketingSoftware_Adminhtml_Marketingsoftware_AjaxcollectionCont
272
  */
273
  public function storeAction()
274
  {
275
- // get post data
276
  $post = $this->getRequest()->getPost();
277
 
278
- // get builder and config
279
- $builder = Mage::helper('marketingsoftware/ApiBuilder');
280
- $config = Mage::helper('marketingsoftware/Config');
281
 
282
- // what collection we are doing?
283
- switch ($post['type'])
284
- {
285
- // are we making 'main' (database) collection?
286
  case 'main':
287
- // create database
288
  $builder->createDatabase($post['name']);
289
 
290
- // save database name
 
 
 
 
 
 
291
  $config->setDatabaseName($post['name']);
292
 
293
- // fields that we are linking
294
  $fields = array();
295
 
296
- // iterate over fields and create proper fields
297
- foreach ($post['fields'] as $field)
298
- {
299
- // get magneto and copernica name
300
  list($magento, $copernica) = explode(',', $field);
301
 
302
- // insert next field
303
  $fields[$magento] = $copernica;
304
 
305
- // create database field
306
- $this->createDatabaseField($post['name'], $copernica, $magento);
307
  }
308
 
309
- // store linked fields
310
- $config->setLinkedCustomerFields($fields);
311
-
312
  break;
313
-
314
- // are we dealing with normal collection
315
  default:
316
- // create collection
317
  $databaseName = Mage::helper('marketingsoftware/config')->getDatabaseName();
 
318
  $builder->createCollection($databaseName, $post['name'], $post['type']);
319
 
320
- // placeholder for fields
321
  $fields = array();
322
 
323
- // iterate over fields and create proper ones
324
- foreach ($post['fields'] as $field)
325
- {
326
- // get magento and copernica name
327
  list($magento, $copernica) = explode(',', $field);
328
 
329
- // assign field linking
330
  $fields[$magento] = $copernica;
331
 
332
- // create field
333
  $builder->createCollectionField($databaseName, $post['name'], $post['type'], $copernica, $magento);
334
  }
335
 
336
- switch ($post['type'])
337
- {
338
  case 'orders':
 
339
  $config->setOrdersCollectionName($post['name']);
340
  $config->setLinkedOrderFields($fields);
341
  break;
 
342
  case 'orderproducts':
343
- $config->setOrderItemsCollectionName($post['name']);
 
344
  $config->setLinkedOrderItemFields($fields);
345
  break;
 
346
  case 'addresses':
347
- $config->setAddressesCollectionName($post['name']);
 
348
  $config->setLinkedAddressFields($fields);
349
  break;
 
350
  case 'viewedproducts':
 
351
  $config->setViewedProductCollectionName($post['name']);
352
  $config->setLinkedViewedProductFields($fields);
353
  break;
 
354
  case 'cartproducts':
355
- $config->setCartItemsCollectionName($post['name']);
356
- $config->setLinkedCartItemFields($fields);
 
357
  break;
 
 
 
 
 
 
358
  }
359
-
360
  break;
361
  }
362
  }
@@ -366,84 +334,93 @@ class Copernica_MarketingSoftware_Adminhtml_Marketingsoftware_AjaxcollectionCont
366
  */
367
  public function defaultAction()
368
  {
369
- // assign database name
370
  $databaseName = 'Magento';
371
 
372
- // get builder
373
- $builder = Mage::helper('marketingsoftware/ApiBuilder');
374
  $config = Mage::helper('marketingsoftware/config');
375
  $data = Mage::helper('marketingsoftware/data');
376
 
377
- // create database
378
  $builder->createDatabase($databaseName);
 
 
379
  $config->setDatabaseName($databaseName);
 
 
380
 
381
- /*
382
- * Create database fields
383
- */
 
 
 
384
  $supportedFields = $data->supportedCustomerFields();
385
  $linkedFields = array();
386
- foreach ($supportedFields as $name => $label)
387
- {
388
- $this->createDatabaseField($databaseName, $name, $name);
389
  $linkedFields[$name] = $name;
390
  }
 
391
  $config->setLinkedCustomerFields($linkedFields);
392
 
393
  /*
394
- * Create cart items collection
395
- */
396
  $builder->createCollection($databaseName, 'Cart_Items' ,'cartproducts');
397
- $config->setCartItemsCollectionName('Cart_Items');
398
- $supportedFields = $data->supportedCartItemFields();
399
  $linkedFields = array();
400
- foreach ($supportedFields as $name => $label)
401
- {
402
  $builder->createCollectionField($databaseName, 'Cart_Items', 'cartproducts', $name, $name);
403
  $linkedFields[$name] = $name;
404
- }
405
- $config->setLinkedCartItemFields($linkedFields);
 
406
 
407
  /*
408
  * Orders collection
409
- */
410
  $builder->createCollection($databaseName, 'Orders' ,'orders');
411
  $config->setOrdersCollectionName('Orders');
412
  $supportedFields = $data->supportedOrderFields();
413
  $linkedFields = array();
414
- foreach ($supportedFields as $name => $label)
415
- {
416
  $builder->createCollectionField($databaseName, 'Orders', 'orders', $name, $name);
417
  $linkedFields[$name] = $name;
418
  }
 
419
  $config->setLinkedOrderFields($linkedFields);
420
 
421
  /*
422
  * Orders items collection
423
  */
424
  $builder->createCollection($databaseName, 'Orders_Items' ,'orderproducts');
425
- $config->setOrderItemsCollectionName('Orders_Items');
426
  $supportedFields = $data->supportedOrderItemFields();
427
  $linkedFields = array();
428
- foreach ($supportedFields as $name => $label)
429
- {
430
  $builder->createCollectionField($databaseName, 'Orders_Items', 'orderproducts', $name, $name);
431
  $linkedFields[$name] = $name;
432
  }
 
433
  $config->setLinkedOrderItemFields($linkedFields);
434
 
435
  /*
436
  * Addresses collection
437
  */
438
  $builder->createCollection($databaseName, 'Addresses' ,'addresses');
439
- $config->setAddressesCollectionName('Addresses');
440
  $supportedFields = $data->supportedAddressFields();
441
  $linkedFields = array();
442
- foreach ($supportedFields as $name => $label)
443
- {
444
  $builder->createCollectionField($databaseName, 'Addresses', 'addresses', $name, $name);
445
  $linkedFields[$name] = $name;
446
  }
 
447
  $config->setLinkedAddressFields($linkedFields);
448
 
449
  /*
@@ -453,11 +430,25 @@ class Copernica_MarketingSoftware_Adminhtml_Marketingsoftware_AjaxcollectionCont
453
  $config->setViewedProductCollectionName('Viewed_Products');
454
  $supportedFields = $data->supportedViewedProductFields();
455
  $linkedFields = array();
456
- foreach ($supportedFields as $name => $label)
457
- {
458
  $builder->createCollectionField($databaseName, 'Viewed_Products', 'viewedproducts', $name, $name);
459
  $linkedFields[$name] = $name;
460
  }
461
- $config->setLinkedViewedProductFields($linkedFields);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
462
  }
463
  }
35
  */
36
  public function validateAction()
37
  {
 
38
  $post = $this->getRequest()->getPost();
39
 
 
40
  $config = Mage::helper('marketingsoftware/config');
41
+ $validator = Mage::helper('marketingsoftware/api_validator');
42
 
 
43
  $problems = array();
44
 
45
+ try {
46
+ if ($post['type'] == 'main') {
47
+ try {
 
 
 
 
48
  $validator->validateDatabase($post['name']);
49
+ } catch (Copernica_MarketingSoftware_Exception $exception) {
 
 
50
  array_push($problems, $exception->getMessage());
51
  }
52
 
53
+ foreach ($post['fields'] as $field) {
 
 
 
54
  list($magento, $copernica) = explode(',', $field);
55
 
56
+ try {
 
 
57
  $validator->validateDatabaseField($post['name'], $copernica, $magento);
58
+ } catch (Copernica_MarketingSoftware_Exception $exception) {
 
 
59
  array_push($problems, $magento.','.$exception->getMessage());
60
  }
61
  }
62
+ } else {
 
 
 
63
  $databaseName = $config->getDatabaseName();
64
 
65
+ try {
 
 
66
  $validator->validateCollection($databaseName, $post['name'], $post['type']);
67
+ } catch (Copernica_MarketingSoftware_Exception $exception) {
 
 
68
  array_push($problems, $exception->getMessage());
69
  }
70
 
71
+ foreach ($post['fields'] as $field) {
 
 
 
 
72
  list($magento, $copernica) = explode(',', $field);
73
 
74
+ try {
 
 
75
  $validator->validateCollectionField($databaseName, $post['name'], $post['type'], $copernica, $magento);
76
+ } catch (Copernica_MarketingSoftware_Exception $exception) {
 
 
77
  array_push($problems, $magento.','.$exception->getMessage());
78
  }
79
  }
80
  }
81
  }
 
82
  /**
83
  * General exceptions should not happen but, just in case we want to
84
  * handle them here.
85
  */
86
+ catch (Exception $exception) {
 
 
87
  Mage::logException($exception);
88
 
 
89
  $this->getResponse()->setBody('Critical error. Check error logs form more info.');
90
  }
91
 
 
92
  $this->getResponse()->setBody(json_encode($problems));
93
  }
94
 
97
  */
98
  public function fetchAction()
99
  {
 
100
  $post = $this->getRequest()->getPost();
101
 
 
102
  $response = $this->getResponse();
103
 
104
+ if (!isset($post['name'])) {
105
+ return $response->setBody(json_encode(array(
106
+ 'message' => 'Invalid input'
107
+ )));
108
+ }
109
 
 
110
  $config = Mage::helper('marketingsoftware/config');
111
  $data = Mage::helper('marketingsoftware/data');
112
 
 
113
  $database = $config->getDatabaseName();
114
 
115
+ $db_not_needed = false;
116
+
117
+ if($post['name'] == 'main') {
118
+ $db_not_needed = true;
119
+ }
120
+
121
+ if (empty($database) && !$db_not_needed) {
122
+ return $response->setBody(json_encode(array(
123
+ 'error' => 'no database'
124
+ )));
125
+ }
126
 
 
127
  $linkedName = '';
128
 
 
129
  $linkedFields = array();
130
  $supportedFields = array();
131
  $linkedFields = array();
132
 
133
+ switch ($post['name']) {
 
 
134
  case 'main':
135
  $linkedName = $config->getDatabaseName();
136
  $supportedFields = $data->supportedCustomerFields();
146
  break;
147
 
148
  case 'orderproducts':
149
+ $linkedName = $config->getOrderItemCollectionName();
150
  $supportedFields = $data->supportedOrderItemFields();
151
  $linkedFields = $config->getLinkedOrderItemFields();
152
  $label = 'Orders items collection';
160
  break;
161
 
162
  case 'viewedproducts':
163
+ $linkedName = $config->getViewedProductsCollectionName();
164
  $supportedFields = $data->supportedViewedProductFields();
165
+ $linkedFields = $config->getLinkedViewedProductsFields();
166
  $label = 'Viewed products collection';
167
  break;
168
 
169
  case 'cartproducts':
170
+ $linkedName = $config->getQuoteItemCollectionName();
171
+ $supportedFields = $data->supportedQuoteItemFields();
172
+ $linkedFields = $config->getLinkedQuoteItemFields();
173
  $label = 'Cart items collection';
174
  break;
175
+
176
+ case 'wishlistproducts':
177
+ $linkedName = $config->getWishlistItemCollectionName();
178
+ $supportedFields = $data->supportedWishlistItemFields();
179
+ $linkedFields = $config->getLinkedWishlistItemFields();
180
+ $label = 'Wishlist items collection';
181
+ break;
182
  }
183
 
 
184
  $fields = array();
185
 
186
+ foreach ($supportedFields as $fieldName => $fieldLabel) {
 
 
187
  $fields[] = array (
188
  'magento' => $fieldName,
189
  'label' => $fieldLabel,
190
+ 'copernica' => array_key_exists($fieldName, $linkedFields) ? $linkedFields[$fieldName] : ''
191
  );
192
  }
193
 
 
194
  $response->setBody(json_encode(array(
195
  'name' => $post['name'],
196
  'linkedName' => $linkedName,
201
 
202
  /**
203
  * Create field inside given database.
204
+ *
205
+ * @param string $databaseName
206
+ * @param string $fieldName
207
+ * @param string $magentoField
208
  */
209
+ protected function _createDatabaseField($databaseName, $fieldName, $magentoField)
210
  {
211
+ $builder = Mage::helper('marketingsoftware/api_builder');
 
212
 
213
+ switch ($magentoField) {
 
 
214
  case 'email':
215
  $builder->createDatabaseEmailField($databaseName, $fieldName);
216
  break;
217
+
218
  case 'newsletter' :
219
  $builder->createDatabaseNewsletterField($databaseName, $fieldName);
220
  break;
221
+
222
  case 'birthdate' :
223
  $builder->createDatabaseDateField($databaseName, $fieldName);
224
  break;
225
+
226
  case 'storeView':
227
+ $builder->createDatabaseField($databaseName, $fieldName, array( 'length' => 250 ));
228
  break;
229
+
230
  case 'registrationDate' :
231
  $builder->createDatabaseDatetimeField($databaseName, $fieldName);
232
  break;
233
+
234
  default:
235
  $builder->createDatabaseField($databaseName, $fieldName);
236
  break;
242
  */
243
  public function storeAction()
244
  {
 
245
  $post = $this->getRequest()->getPost();
246
 
247
+ $builder = Mage::helper('marketingsoftware/api_builder');
248
+ $config = Mage::helper('marketingsoftware/config');
 
249
 
250
+ switch ($post['type']) {
 
 
 
251
  case 'main':
 
252
  $builder->createDatabase($post['name']);
253
 
254
+ $profileCacheCollection = Mage::getModel('marketingsoftware/profile_cache')->getCollection();
255
+
256
+ foreach($profileCacheCollection as $profileCache) {
257
+ $profileCache->delete();
258
+ }
259
+
260
+ $config->unsDatabaseId();
261
  $config->setDatabaseName($post['name']);
262
 
 
263
  $fields = array();
264
 
265
+ foreach ($post['fields'] as $field) {
 
 
 
266
  list($magento, $copernica) = explode(',', $field);
267
 
 
268
  $fields[$magento] = $copernica;
269
 
270
+ $this->_createDatabaseField($post['name'], $copernica, $magento);
 
271
  }
272
 
273
+ $config->setLinkedCustomerFields($fields);
 
 
274
  break;
275
+
 
276
  default:
 
277
  $databaseName = Mage::helper('marketingsoftware/config')->getDatabaseName();
278
+
279
  $builder->createCollection($databaseName, $post['name'], $post['type']);
280
 
 
281
  $fields = array();
282
 
283
+ foreach ($post['fields'] as $field) {
 
 
 
284
  list($magento, $copernica) = explode(',', $field);
285
 
 
286
  $fields[$magento] = $copernica;
287
 
 
288
  $builder->createCollectionField($databaseName, $post['name'], $post['type'], $copernica, $magento);
289
  }
290
 
291
+ switch ($post['type']) {
 
292
  case 'orders':
293
+ $config->unsOrdersCollectionId();
294
  $config->setOrdersCollectionName($post['name']);
295
  $config->setLinkedOrderFields($fields);
296
  break;
297
+
298
  case 'orderproducts':
299
+ $config->unsOrderItemCollectionId();
300
+ $config->setOrderItemCollectionName($post['name']);
301
  $config->setLinkedOrderItemFields($fields);
302
  break;
303
+
304
  case 'addresses':
305
+ $config->unsAddressCollectionId();
306
+ $config->setAddressCollectionName($post['name']);
307
  $config->setLinkedAddressFields($fields);
308
  break;
309
+
310
  case 'viewedproducts':
311
+ $config->unsViewedProductCollectionId();
312
  $config->setViewedProductCollectionName($post['name']);
313
  $config->setLinkedViewedProductFields($fields);
314
  break;
315
+
316
  case 'cartproducts':
317
+ $config->unsQuoteItemCollectionId();
318
+ $config->setQuoteItemCollectionName($post['name']);
319
+ $config->setLinkedQuoteItemFields($fields);
320
  break;
321
+
322
+ case 'wishlistproducts':
323
+ $config->unsWishlistItemCollectionId();
324
+ $config->setWishlistItemCollectionName($post['name']);
325
+ $config->setLinkedWishlistItemFields($fields);
326
+ break;
327
  }
 
328
  break;
329
  }
330
  }
334
  */
335
  public function defaultAction()
336
  {
 
337
  $databaseName = 'Magento';
338
 
339
+ $builder = Mage::helper('marketingsoftware/api_builder');
 
340
  $config = Mage::helper('marketingsoftware/config');
341
  $data = Mage::helper('marketingsoftware/data');
342
 
 
343
  $builder->createDatabase($databaseName);
344
+
345
+ $config->unsDatabaseId();
346
  $config->setDatabaseName($databaseName);
347
+
348
+ $config->clearLinkedCollections();
349
 
350
+ $profileCacheCollection = Mage::getModel('marketingsoftware/profile_cache')->getCollection();
351
+
352
+ foreach($profileCacheCollection as $profileCache) {
353
+ $profileCache->delete();
354
+ }
355
+
356
  $supportedFields = $data->supportedCustomerFields();
357
  $linkedFields = array();
358
+
359
+ foreach ($supportedFields as $name => $label) {
360
+ $this->_createDatabaseField($databaseName, $name, $name);
361
  $linkedFields[$name] = $name;
362
  }
363
+
364
  $config->setLinkedCustomerFields($linkedFields);
365
 
366
  /*
367
+ * Quote items collection
368
+ */
369
  $builder->createCollection($databaseName, 'Cart_Items' ,'cartproducts');
370
+ $config->setQuoteItemCollectionName('Cart_Items');
371
+ $supportedFields = $data->supportedQuoteItemFields();
372
  $linkedFields = array();
373
+
374
+ foreach ($supportedFields as $name => $label) {
375
  $builder->createCollectionField($databaseName, 'Cart_Items', 'cartproducts', $name, $name);
376
  $linkedFields[$name] = $name;
377
+ }
378
+
379
+ $config->setLinkedQuoteItemFields($linkedFields);
380
 
381
  /*
382
  * Orders collection
383
+ */
384
  $builder->createCollection($databaseName, 'Orders' ,'orders');
385
  $config->setOrdersCollectionName('Orders');
386
  $supportedFields = $data->supportedOrderFields();
387
  $linkedFields = array();
388
+
389
+ foreach ($supportedFields as $name => $label) {
390
  $builder->createCollectionField($databaseName, 'Orders', 'orders', $name, $name);
391
  $linkedFields[$name] = $name;
392
  }
393
+
394
  $config->setLinkedOrderFields($linkedFields);
395
 
396
  /*
397
  * Orders items collection
398
  */
399
  $builder->createCollection($databaseName, 'Orders_Items' ,'orderproducts');
400
+ $config->setOrderItemCollectionName('Orders_Items');
401
  $supportedFields = $data->supportedOrderItemFields();
402
  $linkedFields = array();
403
+
404
+ foreach ($supportedFields as $name => $label) {
405
  $builder->createCollectionField($databaseName, 'Orders_Items', 'orderproducts', $name, $name);
406
  $linkedFields[$name] = $name;
407
  }
408
+
409
  $config->setLinkedOrderItemFields($linkedFields);
410
 
411
  /*
412
  * Addresses collection
413
  */
414
  $builder->createCollection($databaseName, 'Addresses' ,'addresses');
415
+ $config->setAddressCollectionName('Addresses');
416
  $supportedFields = $data->supportedAddressFields();
417
  $linkedFields = array();
418
+
419
+ foreach ($supportedFields as $name => $label) {
420
  $builder->createCollectionField($databaseName, 'Addresses', 'addresses', $name, $name);
421
  $linkedFields[$name] = $name;
422
  }
423
+
424
  $config->setLinkedAddressFields($linkedFields);
425
 
426
  /*
430
  $config->setViewedProductCollectionName('Viewed_Products');
431
  $supportedFields = $data->supportedViewedProductFields();
432
  $linkedFields = array();
433
+
434
+ foreach ($supportedFields as $name => $label) {
435
  $builder->createCollectionField($databaseName, 'Viewed_Products', 'viewedproducts', $name, $name);
436
  $linkedFields[$name] = $name;
437
  }
438
+
439
+ /*
440
+ * Wishlist items collection
441
+ */
442
+ $builder->createCollection($databaseName, 'Wishlist_Items' ,'wishlistproducts');
443
+ $config->setWishlistItemCollectionName('Wishlist_Items');
444
+ $supportedFields = $data->supportedWishlistItemFields();
445
+ $linkedFields = array();
446
+
447
+ foreach ($supportedFields as $name => $label) {
448
+ $builder->createCollectionField($databaseName, 'Wishlist_Items', 'wishlistproducts', $name, $name);
449
+ $linkedFields[$name] = $name;
450
+ }
451
+
452
+ $config->setLinkedWishlistItemFields($linkedFields);
453
  }
454
  }
app/code/community/Copernica/MarketingSoftware/controllers/Adminhtml/Marketingsoftware/ExportController.php CHANGED
@@ -28,37 +28,28 @@
28
  * Export Controller takes care of the export data menu.
29
  *
30
  */
31
- class Copernica_MarketingSoftware_Adminhtml_Marketingsoftware_ExportController extends Copernica_MarketingSoftware_Controller_Base
32
  {
33
  /**
34
  * Takes care of displaying the form which
35
  * contains the details used for the SOAP connection.
36
- *
37
  */
38
  public function indexAction()
39
  {
40
- // Load the layout
41
  $this->loadLayout();
42
 
43
- // set menu
44
  $this->_setActiveMenu('copernica');
45
 
46
- // get layout
47
  $layout = $this->getLayout();
48
 
49
- // get content block
50
  $contentBlock = $layout->getBlock('content');
51
 
52
- // create export block
53
  $exportBlock = $layout->createBlock('marketingsoftware/adminhtml_marketingsoftware_export');
54
 
55
- // append export block to content block
56
  $contentBlock->append($exportBlock);
57
 
58
- // set title
59
  $layout->getBlock('head')->setTitle($this->__('Synchronize Data / Copernica Marketing Software / Magento Admin'));
60
 
61
- // render layout
62
  $this->renderLayout();
63
  }
64
 
@@ -67,38 +58,28 @@ class Copernica_MarketingSoftware_Adminhtml_Marketingsoftware_ExportController e
67
  * export action.
68
  * Returns a 'completed' or 'in progress' message, depending
69
  * on the state of the sync tool
 
70
  * @return string
71
  */
72
  public function progressAction()
73
  {
74
- // get the Collection and the helper
75
- $queueCollection = Mage::getResourceModel('marketingsoftware/queue_collection');
76
  $helper = Mage::helper('marketingsoftware');
77
 
78
- // Get the response, set the header and clear the body
79
  $response = $this->getResponse();
80
  $response->setHeader('Content-Type', 'text/plain', true);
81
  $response->clearBody();
82
-
83
- // Send the headers
84
  $response->sendHeaders();
85
 
86
- // Is the synchronisation ready to be started?
87
- if ($helper->isSynchronisationStartScheduled())
88
- {
89
  $string = "Synchronisation scheduled to be started.";
90
- }
91
- elseif ($queueCollection->getSize() > 0)
92
- {
93
  $string = "<b>Number of records</b> : " . $queueCollection->getSize();
94
  $string .= "<br/><b>Oldest record</b> : " . $queueCollection->getQueueStartTime();
95
- }
96
- else
97
- {
98
  $string = 'Idle, no recods in queue.';
99
  }
100
 
101
- // Sent the data
102
  $response->setBody($string);
103
  return;
104
  }
@@ -107,33 +88,26 @@ class Copernica_MarketingSoftware_Adminhtml_Marketingsoftware_ExportController e
107
  * getAction() takes care of exporting customers account information
108
  * from Magento to Copernica.
109
  *
110
- * @return string Returns the current page reloaded, containing an information message
111
  */
112
  public function getAction()
113
  {
114
- // get all POST values
115
  $post = $this->getRequest()->getPost();
116
 
117
- // check to see if there is any POST data along
118
- if (empty($post))
119
- {
120
  Mage::getSingleton('adminhtml/session')->addError('Invalid data.');
121
  return $this->_redirect('*/*');
122
  }
123
 
124
- // Get the helper
125
  $helper = Mage::helper('marketingsoftware');
126
 
127
- // Is the synchronisation ready to be started?
128
- if ($helper->isSynchronisationStartScheduled())
129
- {
130
- // The item has been scheduled already
131
  Mage::getSingleton('adminhtml/session')
132
  ->addError('A synchronization has already been scheduled, please be patient for it to finish.');
 
 
133
  }
134
- else $this->startSync();
135
 
136
- // reload the page
137
  return $this->_redirect('*/*');
138
  }
139
 
@@ -141,31 +115,25 @@ class Copernica_MarketingSoftware_Adminhtml_Marketingsoftware_ExportController e
141
  * Start synchronization process. This method should add new 'start_sync'
142
  * event on the event queue.
143
  */
144
- private function startSync()
145
  {
146
- // get config helper
147
  $config = Mage::helper('marketingsoftware/config');
148
 
149
- // set customer progress status to date when a-bomb hit Hiroshima.
150
- // we can be quite certain that no magento webshop was set up during that
151
- // time.
152
  $config->setCustomerProgressStatus('1945-08-06 08:15:00');
153
  $config->setOrderProgressStatus('1945-08-06 08:15:00');
154
 
155
- // create sync status object
156
- $syncStatus = Mage::getModel('marketingsoftware/SyncStatus');
157
 
158
- // check if current configuration is telling us to filter stores
159
- if ($enabledStores = $config->getEnabledStores()) $syncStatus->setStoresFilter($enabledStores);
 
160
 
161
- // The start sync token must be added to the queue
162
- $queue = Mage::getModel('marketingsoftware/queue')
163
  ->setObject($syncStatus->toArray())
164
  ->setAction('start_sync')
165
- ->setName('startSync')
166
  ->save();
167
 
168
- // The item has been scheduled successfully
169
  Mage::getSingleton('adminhtml/session')->addSuccess("The synchronization process has been scheduled!");
170
  }
171
  }
28
  * Export Controller takes care of the export data menu.
29
  *
30
  */
31
+ class Copernica_MarketingSoftware_Adminhtml_Marketingsoftware_ExportController extends Copernica_MarketingSoftware_Controller_Action
32
  {
33
  /**
34
  * Takes care of displaying the form which
35
  * contains the details used for the SOAP connection.
 
36
  */
37
  public function indexAction()
38
  {
 
39
  $this->loadLayout();
40
 
 
41
  $this->_setActiveMenu('copernica');
42
 
 
43
  $layout = $this->getLayout();
44
 
 
45
  $contentBlock = $layout->getBlock('content');
46
 
 
47
  $exportBlock = $layout->createBlock('marketingsoftware/adminhtml_marketingsoftware_export');
48
 
 
49
  $contentBlock->append($exportBlock);
50
 
 
51
  $layout->getBlock('head')->setTitle($this->__('Synchronize Data / Copernica Marketing Software / Magento Admin'));
52
 
 
53
  $this->renderLayout();
54
  }
55
 
58
  * export action.
59
  * Returns a 'completed' or 'in progress' message, depending
60
  * on the state of the sync tool
61
+ *
62
  * @return string
63
  */
64
  public function progressAction()
65
  {
66
+ $queueCollection = Mage::getResourceModel('marketingsoftware/queue_item_collection');
 
67
  $helper = Mage::helper('marketingsoftware');
68
 
 
69
  $response = $this->getResponse();
70
  $response->setHeader('Content-Type', 'text/plain', true);
71
  $response->clearBody();
 
 
72
  $response->sendHeaders();
73
 
74
+ if ($helper->isSynchronisationStartScheduled()) {
 
 
75
  $string = "Synchronisation scheduled to be started.";
76
+ } elseif ($queueCollection->getSize() > 0) {
 
 
77
  $string = "<b>Number of records</b> : " . $queueCollection->getSize();
78
  $string .= "<br/><b>Oldest record</b> : " . $queueCollection->getQueueStartTime();
79
+ } else {
 
 
80
  $string = 'Idle, no recods in queue.';
81
  }
82
 
 
83
  $response->setBody($string);
84
  return;
85
  }
88
  * getAction() takes care of exporting customers account information
89
  * from Magento to Copernica.
90
  *
91
+ * @return string
92
  */
93
  public function getAction()
94
  {
 
95
  $post = $this->getRequest()->getPost();
96
 
97
+ if (empty($post)) {
 
 
98
  Mage::getSingleton('adminhtml/session')->addError('Invalid data.');
99
  return $this->_redirect('*/*');
100
  }
101
 
 
102
  $helper = Mage::helper('marketingsoftware');
103
 
104
+ if ($helper->isSynchronisationStartScheduled()) {
 
 
 
105
  Mage::getSingleton('adminhtml/session')
106
  ->addError('A synchronization has already been scheduled, please be patient for it to finish.');
107
+ } else {
108
+ $this->_startSync();
109
  }
 
110
 
 
111
  return $this->_redirect('*/*');
112
  }
113
 
115
  * Start synchronization process. This method should add new 'start_sync'
116
  * event on the event queue.
117
  */
118
+ protected function _startSync()
119
  {
 
120
  $config = Mage::helper('marketingsoftware/config');
121
 
 
 
 
122
  $config->setCustomerProgressStatus('1945-08-06 08:15:00');
123
  $config->setOrderProgressStatus('1945-08-06 08:15:00');
124
 
125
+ $syncStatus = Mage::getModel('marketingsoftware/sync_status');
 
126
 
127
+ if ($enabledStores = $config->getEnabledStores()) {
128
+ $syncStatus->setStoresFilter($enabledStores);
129
+ }
130
 
131
+ $queue = Mage::getModel('marketingsoftware/queue_item')
 
132
  ->setObject($syncStatus->toArray())
133
  ->setAction('start_sync')
134
+ ->setName('startsync')
135
  ->save();
136
 
 
137
  Mage::getSingleton('adminhtml/session')->addSuccess("The synchronization process has been scheduled!");
138
  }
139
  }
app/code/community/Copernica/MarketingSoftware/controllers/Adminhtml/Marketingsoftware/LinkController.php CHANGED
@@ -28,7 +28,7 @@
28
  * Link Controller takes care of the link fields menu.
29
  *
30
  */
31
- class Copernica_MarketingSoftware_Adminhtml_Marketingsoftware_LinkController extends Copernica_MarketingSoftware_Controller_Base
32
  {
33
  /**
34
  * indexAction() takes care of displaying the form which
@@ -36,266 +36,203 @@ class Copernica_MarketingSoftware_Adminhtml_Marketingsoftware_LinkController ext
36
  */
37
  public function indexAction()
38
  {
39
- // Load the layout
40
  $this->loadLayout();
41
 
42
- // set menu
43
  $this->_setActiveMenu('copernica');
44
 
45
- // get current layout
46
  $layout = $this->getLayout();
47
 
48
- // get content block
49
  $contentBlock = $layout->getBlock('content');
50
 
51
- // create linkBlock
52
  $linkBlock = $layout->createBlock('marketingsoftware/adminhtml_marketingsoftware_link');
53
 
54
- // append link block to content block
55
  $contentBlock->append($linkBlock);
56
 
57
- // get head block
58
  $headBlock = $layout->getBlock('head');
59
-
60
- // set title
61
  $headBlock->setTitle($this->__('Link Fields / Copernica Marketing Software / Magento Admin'));
62
-
63
- // add javascript
64
  $headBlock->addJs('copernica/marketingsoftware/link.js');
65
 
66
- // Render the layout
67
  $this->renderLayout();
68
  }
69
 
70
  /**
71
  * This action will save all form data.
 
72
  */
73
  public function saveFormAction()
74
  {
75
- // get post variables
76
  $post = $this->getRequest()->getPost();
77
-
78
- /*
79
- * Magento + prototype do not really allow to send complex data via post,
80
- * or just use raw post body. When we try to send complex data via normal
81
- * post request that data is somewhere lost (Zend Framework? or Magento?),
82
- * when we try to use raw post body magento will complain about missing
83
- * form key (since it's raw post data, it does not know how to deal with it).
84
- * Cause of that we do serialize data on js side and we are sending it
85
- * as 'data' post field.
86
- */
87
  $post = json_decode($post['data'], true);
88
 
89
- // do we have a database ?
90
- if (isset($post['database']['name'])) $this->saveDatabaseData($post['database']);
 
91
 
92
- // do we have collections?
93
- if (isset($post['collections'])) $this->saveCollections($post['collections']);
 
94
 
95
- // set response as true
96
- $this->setResponse();
97
  }
98
 
99
  /**
100
  * This method will save database related informations.
101
- * @param assoc
 
102
  */
103
- private function saveDatabaseData($data)
104
  {
105
- // store database name
106
- $this->saveDatabaseName($data['name']);
107
 
108
- // store database fields
109
- $this->saveDatabaseFields($data['fields']);
110
  }
111
 
112
  /**
113
  * This method will store database name.
114
  */
115
- private function saveDatabaseName($name)
116
  {
117
- // store database name in config
118
  Mage::helper('marketingsoftware/config')->setDatabase($name);
119
 
120
- // get database Id from Api
121
- $databaseId = Mage::helper('marketingsoftware/ApiBase')->getDatabaseId($name);
122
 
123
- // store database Id in config
124
  Mage::helper('marketingsoftware/config')->setDatabaseId($databaseId);
125
  }
126
 
127
  /**
128
  * This method will save database fields
129
- * @param assoc
 
130
  */
131
- private function saveDatabaseFields($data)
132
  {
133
- // store linked customer fields inside config
134
  Mage::helper('marketingsoftware/config')->setLinkedCustomerFields($data);
135
  }
136
 
137
  /**
138
  * This method will save all collections from data
139
- * @param assoc
 
140
  */
141
- private function saveCollections($data)
142
  {
143
- // get config
144
  $config = Mage::helper('marketingsoftware/config');
145
 
146
- // we want to clear all data about linked collections
147
  $config->clearLinkedCollections();
148
-
149
- // do we have data for cart items?
150
- if (isset($data['cartproducts'])) $this->saveCartProductsCollection($data['cartproducts']);
151
-
152
- // do we have data for orders?
153
- if (isset($data['orders'])) $this->saveOrdersCollection($data['orders']);
154
-
155
- // do we have data for oreder items?
156
- if (isset($data['orderproducts'])) $this->saveOrderItemsCollection($data['orderproducts']);
157
-
158
- // do we have data for addresses?
159
- if (isset($data['addresses'])) $this->saveAddressesCollection($data['addresses']);
160
-
161
- // do we have data for viewed products?
162
- if (isset($data['viewedproduct'])) $this->saveViewedProductsCollection($data['viewedproduct']);
 
 
 
 
 
163
  }
164
 
165
  /**
166
  * Save cart products info
167
- * @param assoc
 
168
  */
169
- private function saveCartProductsCollection($data)
170
  {
171
- // get cart item collection Id
172
- $collectionId = Mage::helper('marketingsoftware/ApiBase')->getCollectionId($data['name']);
173
 
174
- // get config into local scope
175
  $config = Mage::helper('marketingsoftware/config');
176
-
177
- // set cart item name in config
178
- $config->setCartItemsCollectionName($data['name']);
179
-
180
- // set cart item Id in config
181
- $config->setCartItemsCollectionId($collectionId);
182
-
183
- // set fields
184
- $config->setLinkedCartItemFields($data['fields']);
185
  }
186
 
187
  /**
188
  * Save order collection info
189
- * @param assoc
 
190
  */
191
- private function saveOrdersCollection($data)
192
  {
193
- // get collection Id
194
- $collectionId = Mage::helper('marketingsoftware/ApiBase')->getCollectionId($data['name']);
195
 
196
- // get config instance into local scope
197
  $config = Mage::helper('marketingsoftware/config');
198
-
199
- // set collection name
200
  $config->setOrdersCollectionName($data['name']);
201
-
202
- // set collection Id
203
  $config->setOrdersCollectionId($collectionId);
204
-
205
- // set collection fields
206
  $config->setLinkedOrderFields($data['fields']);
207
  }
208
 
209
  /**
210
  * Save order items collection info
211
- * @param assoc
 
212
  */
213
- private function saveOrderItemsCollection($data)
214
  {
215
- // get collection Id
216
- $collectionId = Mage::helper('marketingsoftware/ApiBase')->getCollectionId($data['name']);
217
 
218
- // get config instance into local scope
219
  $config = Mage::helper('marketingsoftware/config');
220
-
221
- // set collection name
222
- $config->setOrderItemsCollectionName($data['name']);
223
-
224
- // set collection Id
225
- $config->setOrderItemsCollectionId($collectionId);
226
-
227
- // set collection fields
228
  $config->setLinkedOrderItemFields($data['fields']);
229
  }
230
 
231
  /**
232
  * Save addresses collection info
233
- * @param assoc
 
234
  */
235
- private function saveAddressesCollection($data)
236
  {
237
- // get collection Id
238
- $collectionId = Mage::helper('marketingsoftware/ApiBase')->getCollectionId($data['name']);
239
 
240
- // get config instance into local scope
241
  $config = Mage::helper('marketingsoftware/config');
242
-
243
- // set collection name
244
- $config->setAddressesCollectionName($data['name']);
245
-
246
- // set collection id
247
- $config->setAddressesCollectionId($collectionId);
248
-
249
- // set collection fields
250
  $config->setLinkedAddressFields($data['fields']);
251
  }
252
 
253
  /**
254
  * Save viewed products collection info
255
- * @param assoc
 
256
  */
257
- private function saveViewedProductsCollection($data)
258
  {
259
- // get collection Id
260
- $collectionId = Mage::helper('marketingsoftware/ApiBase')->getCollectionId($data['name']);
261
 
262
- // get config instance into local scope
263
  $config = Mage::helper('marketingsoftware/config');
264
-
265
- // set collection name
266
  $config->setViewedProductCollectionName($data['name']);
267
-
268
- // set collection Id
269
  $config->setViewedProductCollectionId($collectionId);
270
-
271
- // set collection fields
272
  $config->setLinkedViewedProductFields($data['fields']);
273
  }
274
 
275
  /**
276
  * Prepare response instance for AJAX response
277
  */
278
- private function prepareAjaxResponse()
279
  {
280
- // get the response instance
281
  $response = $this->getResponse();
282
-
283
- // clear response body fron anything that is there
284
  $response->clearBody();
285
-
286
- // all AJAX responses should be encoded with JSON
287
  $response->setHeader('Content-Type', 'application/json');
288
  }
289
 
290
  /**
291
- * @param bool did we succed ?
292
  */
293
- private function setResponse($error = false)
294
  {
295
- // prepare response instance to be an AJAX respone
296
- $this->prepareAjaxResponse();
297
 
298
- // set response body
299
  $this->getResponse()->setBody(json_encode(Array(
300
  'error' => $error ? 1 : 0
301
  )));
28
  * Link Controller takes care of the link fields menu.
29
  *
30
  */
31
+ class Copernica_MarketingSoftware_Adminhtml_Marketingsoftware_LinkController extends Copernica_MarketingSoftware_Controller_Action
32
  {
33
  /**
34
  * indexAction() takes care of displaying the form which
36
  */
37
  public function indexAction()
38
  {
 
39
  $this->loadLayout();
40
 
 
41
  $this->_setActiveMenu('copernica');
42
 
 
43
  $layout = $this->getLayout();
44
 
 
45
  $contentBlock = $layout->getBlock('content');
46
 
 
47
  $linkBlock = $layout->createBlock('marketingsoftware/adminhtml_marketingsoftware_link');
48
 
 
49
  $contentBlock->append($linkBlock);
50
 
 
51
  $headBlock = $layout->getBlock('head');
 
 
52
  $headBlock->setTitle($this->__('Link Fields / Copernica Marketing Software / Magento Admin'));
 
 
53
  $headBlock->addJs('copernica/marketingsoftware/link.js');
54
 
 
55
  $this->renderLayout();
56
  }
57
 
58
  /**
59
  * This action will save all form data.
60
+ * @todo Never used, Ajaxcollection is always called to perform the save per collection.
61
  */
62
  public function saveFormAction()
63
  {
 
64
  $post = $this->getRequest()->getPost();
 
 
 
 
 
 
 
 
 
 
65
  $post = json_decode($post['data'], true);
66
 
67
+ if (isset($post['database']['name'])) {
68
+ $this->_saveDatabaseData($post['database']);
69
+ }
70
 
71
+ if (isset($post['collections'])) {
72
+ $this->_saveCollections($post['collections']);
73
+ }
74
 
75
+ $this->_setResponse();
 
76
  }
77
 
78
  /**
79
  * This method will save database related informations.
80
+ *
81
+ * @param assoc $data
82
  */
83
+ protected function _saveDatabaseData($data)
84
  {
85
+ $this->_saveDatabaseName($data['name']);
 
86
 
87
+ $this->_saveDatabaseFields($data['fields']);
 
88
  }
89
 
90
  /**
91
  * This method will store database name.
92
  */
93
+ protected function _saveDatabaseName($name)
94
  {
 
95
  Mage::helper('marketingsoftware/config')->setDatabase($name);
96
 
97
+ $databaseId = Mage::helper('marketingsoftware/api_abstract')->getDatabaseId($name);
 
98
 
 
99
  Mage::helper('marketingsoftware/config')->setDatabaseId($databaseId);
100
  }
101
 
102
  /**
103
  * This method will save database fields
104
+ *
105
+ * @param assoc $data
106
  */
107
+ protected function _saveDatabaseFields($data)
108
  {
 
109
  Mage::helper('marketingsoftware/config')->setLinkedCustomerFields($data);
110
  }
111
 
112
  /**
113
  * This method will save all collections from data
114
+ *
115
+ * @param assoc $data
116
  */
117
+ protected function _saveCollections($data)
118
  {
 
119
  $config = Mage::helper('marketingsoftware/config');
120
 
 
121
  $config->clearLinkedCollections();
122
+
123
+ if (isset($data['cartproducts'])) {
124
+ $this->_saveQuoteProductsCollection($data['cartproducts']);
125
+ }
126
+
127
+ if (isset($data['orders'])) {
128
+ $this->_saveOrdersCollection($data['orders']);
129
+ }
130
+
131
+ if (isset($data['orderproducts'])) {
132
+ $this->_saveOrderItemsCollection($data['orderproducts']);
133
+ }
134
+
135
+ if (isset($data['addresses'])) {
136
+ $this->_saveAddressesCollection($data['addresses']);
137
+ }
138
+
139
+ if (isset($data['viewedproduct'])) {
140
+ $this->_saveViewedProductsCollection($data['viewedproduct']);
141
+ }
142
  }
143
 
144
  /**
145
  * Save cart products info
146
+ *
147
+ * @param assoc $data
148
  */
149
+ protected function _saveQuoteProductsCollection($data)
150
  {
151
+ $collectionId = Mage::helper('marketingsoftware/api_abstract')->getCollectionId($data['name']);
 
152
 
 
153
  $config = Mage::helper('marketingsoftware/config');
154
+ $config->setQuoteItemCollectionName($data['name']);
155
+ $config->setQuoteItemCollectionId($collectionId);
156
+ $config->setLinkedQuoteItemFields($data['fields']);
 
 
 
 
 
 
157
  }
158
 
159
  /**
160
  * Save order collection info
161
+ *
162
+ * @param assoc $data
163
  */
164
+ protected function _saveOrdersCollection($data)
165
  {
166
+ $collectionId = Mage::helper('marketingsoftware/api_abstract')->getCollectionId($data['name']);
 
167
 
 
168
  $config = Mage::helper('marketingsoftware/config');
 
 
169
  $config->setOrdersCollectionName($data['name']);
 
 
170
  $config->setOrdersCollectionId($collectionId);
 
 
171
  $config->setLinkedOrderFields($data['fields']);
172
  }
173
 
174
  /**
175
  * Save order items collection info
176
+ *
177
+ * @param assoc $data
178
  */
179
+ protected function _saveOrderItemsCollection($data)
180
  {
181
+ $collectionId = Mage::helper('marketingsoftware/api_abstract')->getCollectionId($data['name']);
 
182
 
 
183
  $config = Mage::helper('marketingsoftware/config');
184
+ $config->setOrderItemCollectionName($data['name']);
185
+ $config->setOrderItemCollectionId($collectionId);
 
 
 
 
 
 
186
  $config->setLinkedOrderItemFields($data['fields']);
187
  }
188
 
189
  /**
190
  * Save addresses collection info
191
+ *
192
+ * @param assoc $data
193
  */
194
+ protected function _saveAddressesCollection($data)
195
  {
196
+ $collectionId = Mage::helper('marketingsoftware/api_abstract')->getCollectionId($data['name']);
 
197
 
 
198
  $config = Mage::helper('marketingsoftware/config');
199
+ $config->setAddressCollectionName($data['name']);
200
+ $config->setAddressCollectionId($collectionId);
 
 
 
 
 
 
201
  $config->setLinkedAddressFields($data['fields']);
202
  }
203
 
204
  /**
205
  * Save viewed products collection info
206
+ *
207
+ * @param assoc $data
208
  */
209
+ protected function _saveViewedProductsCollection($data)
210
  {
211
+ $collectionId = Mage::helper('marketingsoftware/api_abstract')->getCollectionId($data['name']);
 
212
 
 
213
  $config = Mage::helper('marketingsoftware/config');
 
 
214
  $config->setViewedProductCollectionName($data['name']);
 
 
215
  $config->setViewedProductCollectionId($collectionId);
 
 
216
  $config->setLinkedViewedProductFields($data['fields']);
217
  }
218
 
219
  /**
220
  * Prepare response instance for AJAX response
221
  */
222
+ protected function _prepareAjaxResponse()
223
  {
 
224
  $response = $this->getResponse();
 
 
225
  $response->clearBody();
 
 
226
  $response->setHeader('Content-Type', 'application/json');
227
  }
228
 
229
  /**
230
+ * @param bool $error
231
  */
232
+ protected function _setResponse($error = false)
233
  {
234
+ $this->_prepareAjaxResponse();
 
235
 
 
236
  $this->getResponse()->setBody(json_encode(Array(
237
  'error' => $error ? 1 : 0
238
  )));
app/code/community/Copernica/MarketingSoftware/controllers/Adminhtml/Marketingsoftware/SettingsController.php CHANGED
@@ -28,7 +28,7 @@
28
  * Settings Controller, which takes care of the settings menu.
29
  *
30
  */
31
- class Copernica_MarketingSoftware_Adminhtml_Marketingsoftware_SettingsController extends Copernica_MarketingSoftware_Controller_Base
32
  {
33
  /**
34
  * indexAction() takes care of displaying the form which
@@ -36,141 +36,122 @@ class Copernica_MarketingSoftware_Adminhtml_Marketingsoftware_SettingsController
36
  */
37
  public function indexAction()
38
  {
39
- // Load the layout
40
  $this->loadLayout();
41
 
42
- // set menu
43
  $this->_setActiveMenu('copernica');
44
 
45
- // get layout
46
  $layout = $this->getLayout();
47
 
48
- // get content block
49
  $contentBlock = $layout->getBlock('content');
50
 
51
- // create settings block
52
  $settingsBlock = $layout->createBlock('marketingsoftware/adminhtml_marketingsoftware_settings');
53
 
54
- // append settings block to content block
55
  $contentBlock->append($settingsBlock);
56
 
57
- // set title
58
  $layout->getBlock('head')->setTitle($this->__('Settings / Copernica Marketing Software / Magento Admin'));
59
 
60
- // get session into local scope
61
  $session = Mage::getSingleton('adminhtml/session');
 
 
62
 
63
- // if we don't have state in session we want to create such entry
64
- if (!$session->getState()) $session->setState($this->generateState());
65
-
66
- // Render the layout
67
  $this->renderLayout();
68
  }
69
 
70
  /**
71
  * Handle urls that contain state variable.
 
72
  * @return Copernica_MarketingSoftware_Adminhtml_Marketingsoftware_SettingsController
73
  */
74
  public function stateAction()
75
  {
76
- // get state parameter
 
77
  $state = $this->getRequest()->getParam('state');
78
 
79
- // check if we have a correct state token
80
- if ($state != $this->generateState()) return $this->_redirect('*/*', array('response' => 'invalid-state'));
 
81
 
82
- // get code parameter
83
  $code = $this->getRequest()->getParam('code');
84
 
85
- // get request helper
86
- $request = Mage::helper('marketingsoftware/RESTRequest');
87
 
88
- // upgrade out request code into access token
89
  $accessToken = Mage::helper('marketingsoftware/api')->upgradeRequest(
90
  Mage::helper('marketingsoftware/config')->getClientKey(),
91
- Mage::helper('marketingsoftware/config')->getClientSecret(),
92
- $code,
93
- Mage::helper('adminhtml')->getUrl('*/*/state')
94
  );
95
 
96
- // if we have an error here we will just redirect to same view
97
- if ($accessToken === false)
98
- {
99
- // store error message as json inside session
100
- Mage::getSingleton('core/session')->setErrorMessage(json_encode($output['error']['message']));
101
 
102
- // well, we have an error and we have to tell the user that we have an
103
- // error
104
  return $this->_redirect('*/*', array('response' => 'authorize-error'));
105
  }
106
 
107
- // store access token inside config
108
  Mage::helper('marketingsoftware/config')->setAccessToken($accessToken);
109
 
110
- // return this
111
  return $this->_redirect('*/*', array('response' => 'new-access-token'));
112
  }
113
 
114
  /**
115
  * Handle queue settings storage
 
116
  * @return Copernica_MarketingSoftware_Adminhtml_Marketingsoftware_SettingsController
117
  */
118
  public function queueAction()
119
  {
120
- // get post variables
121
  $post = $this->getRequest()->getPost();
122
 
123
- // get config helper
124
  $config = Mage::helper('marketingsoftware/config');
125
 
126
- // set config variables
127
  $config->setTimePerRun($post['qs_max_time']);
128
  $config->setItemsPerRun($post['qs_max_items']);
129
  $config->setApiHostname($post['qs_api_server']);
130
  $config->setVanillaCrons(array_key_exists('qs_vanilla_crons', $post));
131
  $config->setAbandonedTimeout($post['qs_abandoned_timeout']);
132
- $config->setRemoveFinishedCartItems($post['qs_remove_finished']);
133
 
134
- // redirect to same page
 
 
135
  return $this->_redirect('*/*');
136
  }
137
 
138
  /**
139
  * Handles stores settings
 
140
  * @return Copernica_MarketingSoftware_Adminhtml_Marketingsoftware_SettingsController
141
  */
142
  public function storesAction()
143
  {
144
- // get post variables
145
  $post = $this->getRequest()->getPost();
146
-
147
- // get config helper
148
  $config = Mage::helper('marketingsoftware/config');
149
 
150
- // check if user want to disable store filter
151
- if (isset($post['chk-store-disable']))
152
- {
153
- // disable enabled stores filter
154
  $config->setEnabledStores(null);
155
-
156
- // redirect to same page
 
 
157
  return $this->_redirect('*/*');
158
  }
159
-
160
- // make an empty variable
161
- $enabledStores = array();
162
-
163
- // iterate over all enabled stores
164
- foreach($post['store'] as $store)
165
- {
166
- $enabledStores[] = $store;
167
- }
168
-
169
- // set enabled stores
170
- $config->setEnabledStores($enabledStores);
171
-
172
- // redirect to same page
173
- return $this->_redirect('*/*');
174
  }
175
 
176
  /**
@@ -178,9 +159,10 @@ class Copernica_MarketingSoftware_Adminhtml_Marketingsoftware_SettingsController
178
  * it in way that it is possible to regenerate such state for a user in
179
  * resonable time period (one certain day). We will use for that md5 hash
180
  * of user's session id and current day-month-year combination.
 
181
  * @return string
182
  */
183
- protected function generateState()
184
  {
185
  return md5(Mage::getSingleton('adminhtml/session')->getEncryptedSessionId().date('dmY'));
186
  }
@@ -188,38 +170,34 @@ class Copernica_MarketingSoftware_Adminhtml_Marketingsoftware_SettingsController
188
  /**
189
  * sendAction() takes care of checking and storing the login details to the SOAP
190
  * It also performs checks on database and in case it doesn't exists, it will create it.
 
191
  * @return Object Returns the '_redirect' object that loads the parent page
192
  */
193
  public function sendAction()
194
  {
195
- // get all post values from the request
196
  $post = $this->getRequest()->getPost();
197
 
198
- // get config to local scope
199
  $config = Mage::helper('marketingsoftware/config');
200
 
201
- // get current client key
202
  $clientKey = $config->getClientKey();
203
 
204
- // get current client secret
205
  $clientSecret = $config->getClientSecret();
206
 
207
- // if client key and secret does not change there is no point in doing anything
208
- if ($clientKey == $post['cp_client_key'] && $clientSecret == $post['cp_client_secret']) return $this->_redirect('*/*');
 
209
 
210
- // check if we have a client key
211
- if (!isset($post['cp_client_key'])) return $this->_redirect('*/*');
 
212
 
213
- // set client key inside config file
214
  $config->setClientKey($post['cp_client_key']);
215
-
216
- // set client secret inside config gile
217
  $config->setClientSecret($post['cp_client_secret']);
218
-
219
- // unset access token
220
  $config->unsAccessToken();
221
 
222
- // we will not be doing anything in this method
 
 
223
  return $this->_redirect('*/*');
224
  }
225
  }
28
  * Settings Controller, which takes care of the settings menu.
29
  *
30
  */
31
+ class Copernica_MarketingSoftware_Adminhtml_Marketingsoftware_SettingsController extends Copernica_MarketingSoftware_Controller_Action
32
  {
33
  /**
34
  * indexAction() takes care of displaying the form which
36
  */
37
  public function indexAction()
38
  {
 
39
  $this->loadLayout();
40
 
 
41
  $this->_setActiveMenu('copernica');
42
 
 
43
  $layout = $this->getLayout();
44
 
 
45
  $contentBlock = $layout->getBlock('content');
46
 
 
47
  $settingsBlock = $layout->createBlock('marketingsoftware/adminhtml_marketingsoftware_settings');
48
 
 
49
  $contentBlock->append($settingsBlock);
50
 
 
51
  $layout->getBlock('head')->setTitle($this->__('Settings / Copernica Marketing Software / Magento Admin'));
52
 
 
53
  $session = Mage::getSingleton('adminhtml/session');
54
+
55
+ $session->setState($this->_generateState());
56
 
 
 
 
 
57
  $this->renderLayout();
58
  }
59
 
60
  /**
61
  * Handle urls that contain state variable.
62
+ *
63
  * @return Copernica_MarketingSoftware_Adminhtml_Marketingsoftware_SettingsController
64
  */
65
  public function stateAction()
66
  {
67
+ $session = Mage::getSingleton('adminhtml/session');
68
+
69
  $state = $this->getRequest()->getParam('state');
70
 
71
+ if ($state != $session->getState()) {
72
+ return $this->_redirect('*/*', array('response' => 'invalid-state'));
73
+ }
74
 
 
75
  $code = $this->getRequest()->getParam('code');
76
 
77
+ $request = Mage::helper('marketingsoftware/rest_request');
 
78
 
 
79
  $accessToken = Mage::helper('marketingsoftware/api')->upgradeRequest(
80
  Mage::helper('marketingsoftware/config')->getClientKey(),
81
+ Mage::helper('marketingsoftware/config')->getClientSecret(),
82
+ Mage::helper('adminhtml')->getUrl('*/*/state'),
83
+ $code
84
  );
85
 
86
+ if ($accessToken === false) {
87
+ Mage::getSingleton('core/session')->setErrorMessage($this->__('No access token available'));
 
 
 
88
 
 
 
89
  return $this->_redirect('*/*', array('response' => 'authorize-error'));
90
  }
91
 
 
92
  Mage::helper('marketingsoftware/config')->setAccessToken($accessToken);
93
 
 
94
  return $this->_redirect('*/*', array('response' => 'new-access-token'));
95
  }
96
 
97
  /**
98
  * Handle queue settings storage
99
+ *
100
  * @return Copernica_MarketingSoftware_Adminhtml_Marketingsoftware_SettingsController
101
  */
102
  public function queueAction()
103
  {
 
104
  $post = $this->getRequest()->getPost();
105
 
 
106
  $config = Mage::helper('marketingsoftware/config');
107
 
 
108
  $config->setTimePerRun($post['qs_max_time']);
109
  $config->setItemsPerRun($post['qs_max_items']);
110
  $config->setApiHostname($post['qs_api_server']);
111
  $config->setVanillaCrons(array_key_exists('qs_vanilla_crons', $post));
112
  $config->setAbandonedTimeout($post['qs_abandoned_timeout']);
113
+ $config->setRemoveFinishedQuoteItem(array_key_exists('qs_remove_finished', $post));
114
 
115
+ Mage::getSingleton('core/session')->addSuccess('Synchronization settings have been saved');
116
+ session_write_close(); // To make sure the success message is passed
117
+
118
  return $this->_redirect('*/*');
119
  }
120
 
121
  /**
122
  * Handles stores settings
123
+ *
124
  * @return Copernica_MarketingSoftware_Adminhtml_Marketingsoftware_SettingsController
125
  */
126
  public function storesAction()
127
  {
 
128
  $post = $this->getRequest()->getPost();
129
+
 
130
  $config = Mage::helper('marketingsoftware/config');
131
 
132
+ if (isset($post['chk-store-disable'])) {
 
 
 
133
  $config->setEnabledStores(null);
134
+
135
+ Mage::getSingleton('core/session')->addSuccess('Stores settings have been saved');
136
+ session_write_close(); // To make sure the success message is passed
137
+
138
  return $this->_redirect('*/*');
139
  }
140
+
141
+ $enabledStores = array();
142
+
143
+ if(isset($post['store'])) {
144
+ foreach($post['store'] as $store) {
145
+ $enabledStores[] = $store;
146
+ }
147
+ }
148
+
149
+ $config->setEnabledStores($enabledStores);
150
+
151
+ Mage::getSingleton('core/session')->addSuccess('Stores settings have been saved');
152
+ session_write_close(); // To make sure the success message is passed
153
+
154
+ return $this->_redirect('*/*');
155
  }
156
 
157
  /**
159
  * it in way that it is possible to regenerate such state for a user in
160
  * resonable time period (one certain day). We will use for that md5 hash
161
  * of user's session id and current day-month-year combination.
162
+ *
163
  * @return string
164
  */
165
+ protected function _generateState()
166
  {
167
  return md5(Mage::getSingleton('adminhtml/session')->getEncryptedSessionId().date('dmY'));
168
  }
170
  /**
171
  * sendAction() takes care of checking and storing the login details to the SOAP
172
  * It also performs checks on database and in case it doesn't exists, it will create it.
173
+ *
174
  * @return Object Returns the '_redirect' object that loads the parent page
175
  */
176
  public function sendAction()
177
  {
 
178
  $post = $this->getRequest()->getPost();
179
 
 
180
  $config = Mage::helper('marketingsoftware/config');
181
 
 
182
  $clientKey = $config->getClientKey();
183
 
 
184
  $clientSecret = $config->getClientSecret();
185
 
186
+ if ($clientKey == $post['cp_client_key'] && $clientSecret == $post['cp_client_secret']) {
187
+ return $this->_redirect('*/*');
188
+ }
189
 
190
+ if (!isset($post['cp_client_key'])) {
191
+ return $this->_redirect('*/*');
192
+ }
193
 
 
194
  $config->setClientKey($post['cp_client_key']);
 
 
195
  $config->setClientSecret($post['cp_client_secret']);
 
 
196
  $config->unsAccessToken();
197
 
198
+ Mage::getSingleton('core/session')->addSuccess('REST settings have been saved');
199
+ session_write_close(); // To make sure the success message is passed
200
+
201
  return $this->_redirect('*/*');
202
  }
203
  }
app/code/community/Copernica/MarketingSoftware/controllers/Adminhtml/Marketingsoftware/SyncController.php CHANGED
@@ -28,32 +28,25 @@
28
  * Settings Controller, which takes care of the settings menu.
29
  *
30
  */
31
- class Copernica_MarketingSoftware_Adminhtml_Marketingsoftware_SyncController extends Copernica_MarketingSoftware_Controller_Base
32
  {
33
  /**
34
  * This action is a default one. Will be executed when user arrives on page.
35
  */
36
  public function indexAction()
37
  {
38
- // Load the layout
39
  $this->loadLayout();
40
 
41
- // set menu
42
  $this->_setActiveMenu('copernica');
43
 
44
- // get layout
45
  $layout = $this->getLayout();
46
 
47
- // get content block
48
  $contentBlock = $layout->getBlock('content');
49
 
50
- // append sync block to content block
51
  $contentBlock->append($layout->createBlock('marketingsoftware/adminhtml_marketingsoftware_sync'));
52
 
53
- // set title
54
  $layout->getBlock('head')->setTitle($this->__('Settings / Copernica Marketing Software / Magento Admin'));
55
 
56
- // render layout
57
  $this->renderLayout();
58
  }
59
 
@@ -63,40 +56,35 @@ class Copernica_MarketingSoftware_Adminhtml_Marketingsoftware_SyncController ext
63
  */
64
  public function postAction()
65
  {
66
- // get post data
67
  $post = $this->getRequest()->getPost();
68
 
69
- // get sync profile model
70
- $syncProfile = Mage::getModel('marketingsoftware/syncProfile');
71
 
72
- // if we have id for sync profile we should load it's data
73
- if (array_key_exists('id', $post) && $post['id']) $syncProfile->load($post['id']);
 
74
 
75
- // create or update a profile with data that we did receive from user
76
  $syncProfile
77
  ->setClientKey($post['client_key'])
78
  ->setClientSecret($post['client_secret'])
79
  ->setName($post['name'])
80
  ->save();
81
 
82
- // redirect to same webpage
83
  return $this->_redirect('*/*');
84
  }
85
 
86
  /**
87
  * Get array of all stores available
 
88
  * @return array
89
  */
90
- private function getAvailableStores($profileId)
91
  {
92
  $stores = array();
93
 
94
- foreach (Mage::app()->getWebsites() as $website)
95
- {
96
- foreach ($website->getGroups() as $group)
97
- {
98
- foreach ($group->getStores() as $store)
99
- {
100
  $stores[$store->getId()] = implode(' > ', array(
101
  $website->getName(),
102
  $group->getName(),
@@ -115,25 +103,21 @@ class Copernica_MarketingSoftware_Adminhtml_Marketingsoftware_SyncController ext
115
  */
116
  public function getProfileAction()
117
  {
118
- // get data that we did receive from user
119
  $post = $this->getRequest()->getPost();
120
 
121
- // set json as response type
122
  $this->getResponse()->setHeader('Content-Type', 'application/json');
123
 
124
- // do we have profile id ?
125
- if (array_key_exists('id', $post))
126
- {
127
- // create full response from profile and supply state code that can
128
- // be used with this profile
129
- $profile = Mage::getModel('marketingsoftware/syncProfile')->load($post['id']);
130
  $state = md5(Mage::getModel('adminhtml/session')->getEncryptedSessionId().date('dmY').$profile->getId());
131
- $stores = $this->getAvailableStores($post['id']);
 
 
132
  $this->getResponse()->setBody(json_encode(array_merge($profile->toArray(), array('state' => $state, 'stores' => $stores))));
 
 
133
  }
134
-
135
- // well, we can not do anything useful
136
- else $this->getResponse()->setBody(json_encode('error'));
137
  }
138
 
139
  /**
@@ -141,16 +125,15 @@ class Copernica_MarketingSoftware_Adminhtml_Marketingsoftware_SyncController ext
141
  */
142
  public function deleteAction()
143
  {
144
- // get post variables
145
  $post = $this->getRequest()->getPost();
146
 
147
- // remove sync profile when we have a id
148
- if (array_key_exists('id', $post)) Mage::getModel('marketingsoftware/syncProfile')
149
  ->load($post['id'])
150
  ->delete();
151
-
152
- // response with error
153
- else $this->getResponse()->setBody(json_encode('error'));
154
 
155
  }
156
 
@@ -161,20 +144,21 @@ class Copernica_MarketingSoftware_Adminhtml_Marketingsoftware_SyncController ext
161
  */
162
  public function stateAction()
163
  {
164
- // get all params
165
  $params = $this->getRequest()->getParams();
166
 
167
- // check if we have required params
168
- if (!array_key_exists('state', $params)) return $this->_redirect('*/*', array('result' => 'invalid-state'));
169
- if (!array_key_exists('code', $params)) return $this->_redirect('*/*', array('result' => 'invalid-code'));
 
 
 
 
170
 
171
- // iterate over profile and find one that was requested
172
- foreach (Mage::getModel('marketingsoftware/syncProfile')->getCollection() as $profile)
173
- {
174
- // if returned state does not match one that we are generating from current profile, skip it
175
- if ($params['state'] != md5(Mage::getSingleton('adminhtml/session')->getEncryptedSessionId().date('dmY').$profile->getId())) continue;
176
 
177
- // ask for access key
178
  $accessToken = Mage::helper('marketingsoftware/api')->upgradeRequest(
179
  $profile->getClientKey(),
180
  $profile->getClientSecret(),
@@ -182,17 +166,15 @@ class Copernica_MarketingSoftware_Adminhtml_Marketingsoftware_SyncController ext
182
  Mage::helper('adminhtml')->getUrl('*/*/state')
183
  );
184
 
185
- // check if we have an access token
186
- if ($accessToken == false) return $this->_redirect('*/*', array('result' => 'invalid-token'));
 
187
 
188
- // set access token on profile and save it
189
  $profile->setAccessToken($accessToken)->save();
190
 
191
- // we are good here so we can redirect with a happy message
192
  return $this->_redirect('*/*', array('result' => 'ok', 'profileId' => $profile->getId()));
193
  }
194
 
195
- // we don't have a valid state (didn't found one that would match profile)
196
  return $this->_redirect('*/*', array('result' => 'invalid-state', 'profileId' => $profile->getId()));
197
  }
198
  }
28
  * Settings Controller, which takes care of the settings menu.
29
  *
30
  */
31
+ class Copernica_MarketingSoftware_Adminhtml_Marketingsoftware_SyncController extends Copernica_MarketingSoftware_Controller_Action
32
  {
33
  /**
34
  * This action is a default one. Will be executed when user arrives on page.
35
  */
36
  public function indexAction()
37
  {
 
38
  $this->loadLayout();
39
 
 
40
  $this->_setActiveMenu('copernica');
41
 
 
42
  $layout = $this->getLayout();
43
 
 
44
  $contentBlock = $layout->getBlock('content');
45
 
 
46
  $contentBlock->append($layout->createBlock('marketingsoftware/adminhtml_marketingsoftware_sync'));
47
 
 
48
  $layout->getBlock('head')->setTitle($this->__('Settings / Copernica Marketing Software / Magento Admin'));
49
 
 
50
  $this->renderLayout();
51
  }
52
 
56
  */
57
  public function postAction()
58
  {
 
59
  $post = $this->getRequest()->getPost();
60
 
61
+ $syncProfile = Mage::getModel('marketingsoftware/sync_profile');
 
62
 
63
+ if (array_key_exists('id', $post) && $post['id']) {
64
+ $syncProfile->load($post['id']);
65
+ }
66
 
 
67
  $syncProfile
68
  ->setClientKey($post['client_key'])
69
  ->setClientSecret($post['client_secret'])
70
  ->setName($post['name'])
71
  ->save();
72
 
 
73
  return $this->_redirect('*/*');
74
  }
75
 
76
  /**
77
  * Get array of all stores available
78
+ *
79
  * @return array
80
  */
81
+ protected function _getAvailableStores($profileId)
82
  {
83
  $stores = array();
84
 
85
+ foreach (Mage::app()->getWebsites() as $website) {
86
+ foreach ($website->getGroups() as $group) {
87
+ foreach ($group->getStores() as $store) {
 
 
 
88
  $stores[$store->getId()] = implode(' > ', array(
89
  $website->getName(),
90
  $group->getName(),
103
  */
104
  public function getProfileAction()
105
  {
 
106
  $post = $this->getRequest()->getPost();
107
 
 
108
  $this->getResponse()->setHeader('Content-Type', 'application/json');
109
 
110
+ if (array_key_exists('id', $post)) {
111
+ $profile = Mage::getModel('marketingsoftware/sync_profile')->load($post['id']);
112
+
 
 
 
113
  $state = md5(Mage::getModel('adminhtml/session')->getEncryptedSessionId().date('dmY').$profile->getId());
114
+
115
+ $stores = $this->_getAvailableStores($post['id']);
116
+
117
  $this->getResponse()->setBody(json_encode(array_merge($profile->toArray(), array('state' => $state, 'stores' => $stores))));
118
+ } else {
119
+ $this->getResponse()->setBody(json_encode('error'));
120
  }
 
 
 
121
  }
122
 
123
  /**
125
  */
126
  public function deleteAction()
127
  {
 
128
  $post = $this->getRequest()->getPost();
129
 
130
+ if (array_key_exists('id', $post)) {
131
+ Mage::getModel('marketingsoftware/sync_profile')
132
  ->load($post['id'])
133
  ->delete();
134
+ } else {
135
+ $this->getResponse()->setBody(json_encode('error'));
136
+ }
137
 
138
  }
139
 
144
  */
145
  public function stateAction()
146
  {
 
147
  $params = $this->getRequest()->getParams();
148
 
149
+ if (!array_key_exists('state', $params)) {
150
+ return $this->_redirect('*/*', array('result' => 'invalid-state'));
151
+ }
152
+
153
+ if (!array_key_exists('code', $params)) {
154
+ return $this->_redirect('*/*', array('result' => 'invalid-code'));
155
+ }
156
 
157
+ foreach (Mage::getModel('marketingsoftware/sync_profile')->getCollection() as $profile) {
158
+ if ($params['state'] != md5(Mage::getSingleton('adminhtml/session')->getEncryptedSessionId().date('dmY').$profile->getId())) {
159
+ continue;
160
+ }
 
161
 
 
162
  $accessToken = Mage::helper('marketingsoftware/api')->upgradeRequest(
163
  $profile->getClientKey(),
164
  $profile->getClientSecret(),
166
  Mage::helper('adminhtml')->getUrl('*/*/state')
167
  );
168
 
169
+ if ($accessToken == false) {
170
+ return $this->_redirect('*/*', array('result' => 'invalid-token'));
171
+ }
172
 
 
173
  $profile->setAccessToken($accessToken)->save();
174
 
 
175
  return $this->_redirect('*/*', array('result' => 'ok', 'profileId' => $profile->getId()));
176
  }
177
 
 
178
  return $this->_redirect('*/*', array('result' => 'invalid-state', 'profileId' => $profile->getId()));
179
  }
180
  }
app/code/community/Copernica/MarketingSoftware/controllers/ProductController.php CHANGED
@@ -26,250 +26,283 @@
26
 
27
  /**
28
  * Controls the product actions.
29
- *
30
- *
31
  */
32
  class Copernica_MarketingSoftware_ProductController extends Mage_Core_Controller_Front_Action
33
- {
34
  /**
35
- * Handles a request to copernica/product/xml
36
- * Prints a XML with product information.
 
37
  */
38
- public function xmlAction()
 
 
 
 
 
39
  {
40
- //TODO: some security
41
  $request = $this->getRequest();
42
- if ($request->getParam('identifier') == "sku") {
43
- $product = $this->_getProductBySku($request->getParam('id'));
 
44
  } else {
45
- $product = $this->_getProduct($request->getParam('id'));
46
  }
47
 
48
- // Use attribute codes or labels?
49
- $useAttribCode = false;
50
- if ($request->getParam('attribkey') == 'code') {
51
- $useAttribCode = true;
52
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
 
54
- if ($product != NULL) {
55
- $xml = $this->_buildProductXML(array($product), $useAttribCode);
56
- $this->_prepareResponse($xml);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
  }
58
- elseif ($request->getParam('new'))
59
- {
60
- // Today it is:
61
- $todayDate = date('Y-m-d H:i:s');
62
-
63
- // Get the collection, add the filters and select all data
64
- $collection = Mage::getResourceModel('catalog/product_collection')
65
- ->addAttributeToFilter('news_from_date', array(
66
- 'date' => true,
67
- 'to' => $todayDate)
68
- )
69
- ->addAttributeToFilter('news_to_date', array(
70
- 'or'=> array(
71
- 0 => array('date' => true, 'from' => $todayDate),
72
- 1 => array('is' => new Zend_Db_Expr('null')))
73
- ), 'left'
74
- )
75
- ->addAttributeToSelect('id');
76
-
77
- // construct the XML
78
- $xml = $this->_buildProductXML($collection, $useAttribCode);
79
- $this->_prepareResponse($xml);
80
- } else {
81
- $this->norouteAction();
82
  }
 
 
83
  }
84
 
85
  /**
86
- * Constructs an XML object for the given product
87
- *
88
- * @param Mage_Catalog_Model_Product $product
89
- * @param bool $useAttribCode
90
- * @return SimpleXMLElement
91
  */
92
- protected function _buildProductXML($collection, $useAttribCode = false)
93
  {
94
- $xml = new SimpleXMLElement('<products/>');
95
-
96
- // iterate over the collection
97
- foreach ($collection as $product)
98
- {
99
- // Add a product node
100
- $element = $xml->addChild('product');
101
-
102
- // wrap the product
103
- $_product = Mage::getModel('marketingsoftware/abstraction_product')->loadProduct($product->getId());
104
-
105
- // Collection of relevant fields
106
- $fields = array(
107
- 'id',
108
- 'sku',
109
- 'name',
110
- 'description',
111
- 'price',
112
- 'specialPrice',
113
- 'modified',
114
- 'created',
115
- 'productUrl',
116
- 'imageUrl',
117
- 'weight',
118
- 'isNew',
119
- 'categories',
120
- 'attributes'
121
- );
122
-
123
- // Add the internal product fields to the database
124
- foreach ($fields as $name)
125
- {
126
- // Get the value
127
- $value = $_product->$name();
128
-
129
- // Get the attributes of the attributes
130
- if ($name == 'attributes') $value = $value->attributes($useAttribCode);
131
-
132
- if (is_bool($value))
133
- {
134
- $element->addChild($name, htmlspecialchars(html_entity_decode($value ? 'yes' : 'no')));
135
- continue;
136
- }
137
- elseif (!is_array($value))
138
- {
139
- if ($name == 'price' || $name == 'specialPrice') {
140
- $value = Mage::helper('core')->currency($value, true, false);
141
- }
142
-
143
- $element->addChild($name, htmlspecialchars(html_entity_decode((string)$value)));
144
- continue;
145
- }
146
-
147
- // We have an array here
148
-
149
- // Add an element, to bundle all the elements of the array
150
- $node = $element->addChild($name);
151
-
152
- // we have an array here
153
- foreach ($value as $key => $attribute)
154
- {
155
- // prepare the key
156
- if (is_numeric($key)) $key = 'items';
157
- else $key = str_replace(' ', '_', $key);
158
-
159
- // special treatment for categories and empty values
160
- if ($name == 'categories') $attribute = implode(' > ', $attribute);
161
- elseif (trim($attribute) === '') continue;
162
-
163
- // Add the child
164
- $node->addChild($key, htmlspecialchars(html_entity_decode((string)$attribute)));
165
- }
166
- }
167
-
168
- $parentIds = Mage::getResourceSingleton('catalog/product_type_configurable')->getParentIdsByChild($product->getId());
169
-
170
- if (isset($parentIds[0])) {
171
- $_product = Mage::getModel('marketingsoftware/abstraction_product')->loadProduct($parentIds[0]);
172
-
173
- // Add a product node
174
- $element = $xml->addChild('configurable_product');
175
-
176
- // Add the internal product fields to the database
177
- foreach ($fields as $name)
178
- {
179
- // Get the value
180
- $value = $_product->$name();
181
-
182
- // Get the attributes of the attributes
183
- if ($name == 'attributes') $value = $value->attributes($useAttribCode);
184
-
185
- if (is_bool($value))
186
- {
187
- $element->addChild($name, htmlspecialchars(html_entity_decode($value ? 'yes' : 'no')));
188
- continue;
189
- }
190
- elseif (!is_array($value))
191
- {
192
- if ($name == 'price' || $name == 'specialPrice') {
193
- $value = Mage::helper('core')->currency($value, true, false);
194
- }
195
-
196
- $element->addChild($name, htmlspecialchars(html_entity_decode((string)$value)));
197
- continue;
198
- }
199
-
200
- // We have an array here
201
-
202
- // Add an element, to bundle all the elements of the array
203
- $node = $element->addChild($name);
204
-
205
- // we have an array here
206
- foreach ($value as $key => $attribute)
207
- {
208
- // prepare the key
209
- if (is_numeric($key)) $key = 'items';
210
- else $key = str_replace(' ', '_', $key);
211
-
212
- // special treatment for categories and empty values
213
- if ($name == 'categories') $attribute = implode(' > ', $attribute);
214
- elseif (trim($attribute) === '') continue;
215
-
216
- // Add the child
217
- $node->addChild($key, htmlspecialchars(html_entity_decode((string)$attribute)));
218
- }
219
- }
220
  }
 
 
221
  }
222
 
223
- return $xml;
224
  }
225
 
226
  /**
227
- * Prepare response based on the given XML object
228
  *
229
- * @param SimpleXMLElement $xml
 
230
  */
231
- protected function _prepareResponse(SimpleXMLElement $xml)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
232
  {
233
- $response = $this->getResponse();
 
 
234
 
235
- //set correct header
236
- $response->setHeader('Content-Type', 'text/xml', true);
237
 
238
- //clear anything another controller may have set
239
- $response->clearBody();
240
 
241
- //send headers
 
 
242
  $response->sendHeaders();
243
-
244
- //set xml content
245
- $response->setBody($xml->asXML());
246
  }
247
 
248
  /**
249
- * Retrieves a product by ID
250
- *
251
- * @param int $productId
252
- * @return Mage_Catalog_Model_Product
253
  */
254
- protected function _getProduct($productId)
255
  {
256
- $product = Mage::getModel('catalog/product')->load($productId);
 
257
 
258
- // only a product with an id exists
259
- return $product->getId() ? $product : null;
260
  }
261
 
262
  /**
263
- * Retrieves a product by SKU
264
- *
265
- * @param String $productSku
266
- * @return Mage_Catalog_Model_Product
267
- */
268
- protected function _getProductBySku($productSku)
269
  {
270
- $product = Mage::getModel('catalog/product')->loadByAttribute('sku',$productSku);
 
 
 
 
 
 
 
 
 
 
271
 
272
- // only a product with an id exists
273
- return $product->getId() ? $product : null;
274
  }
275
  }
26
 
27
  /**
28
  * Controls the product actions.
 
 
29
  */
30
  class Copernica_MarketingSoftware_ProductController extends Mage_Core_Controller_Front_Action
31
+ {
32
  /**
33
+ * The DOM document that will be used to return XML content.
34
+ *
35
+ * @var DOMDocument
36
  */
37
+ protected $_document;
38
+
39
+ /**
40
+ * Show one single product in result collection
41
+ */
42
+ protected function _showProduct()
43
  {
 
44
  $request = $this->getRequest();
45
+
46
+ if ($request->getParam('identifier') == 'sku') {
47
+ $product = Mage::getModel('catalog/product')->loadByAttribute('sku', $request->getParam('id'));
48
  } else {
49
+ $product = Mage::getModel('catalog/product')->load($request->getParam('id'));
50
  }
51
 
52
+ if (!$product->getId()) {
53
+ return $this->norouteAction();
 
 
54
  }
55
+
56
+ $productEntity = Mage::getModel('marketingsoftware/copernica_entity_product');
57
+ $productEntity->setProduct($product->getId());
58
+
59
+ $xml = $this->_buildProductXML($productEntity);
60
+
61
+ $this->_prepareResponse($xml);
62
+ }
63
+
64
+ /**
65
+ * This is a helper method to append simple nodes to document tree
66
+ *
67
+ * @param DOMElement $parent
68
+ * @param string $name
69
+ * @param mixed $value
70
+ */
71
+ protected function _appendSimpleNode(DOMElement $parent, $name, $value)
72
+ {
73
+ $parent->appendChild($this->_document->createElement($name, htmlspecialchars(html_entity_decode((string)$value))));
74
+ }
75
+
76
+ /**
77
+ * Show whole collection of products
78
+ */
79
+ protected function _showCollection()
80
+ {
81
+ $today = date('Y-m-d H:i:s');
82
+
83
+ $collection = Mage::getResourceModel('catalog/product_collection');
84
+ $collection->addAttributeToFilter('news_from_date', array (
85
+ 'date' => true,
86
+ 'to' => $today
87
+ ));
88
+ $collection->addAttributeToFilter('news_to_date', array (
89
+ 'or' => array (
90
+ array ('date' => true, 'from' => $todayDate),
91
+ array ('is' => new Zend_Db_Expr('null'))
92
+ ), 'left'
93
+ ));
94
+ $collection->addAttributeToSelect('id');
95
+
96
+ $xml = $this->_buildCollectionXML($collection);
97
 
98
+ $this->_prepareResponse($xml);
99
+ }
100
+
101
+ /**
102
+ * Prepare xml tree for one product instance.
103
+ *
104
+ * @param Copernica_MarketingSoftware_Model_Copernica_Entity_Product $product
105
+ * @param bool $noParents
106
+ * @param bool $noChildren
107
+ * @return DOMElement
108
+ */
109
+ protected function _buildProductXML(Copernica_MarketingSoftware_Model_Copernica_Entity_Product $product, $noParents = false, $noChildren = false)
110
+ {
111
+ $element = $this->_document->createElement('product');
112
+ $element->setAttribute('xlink:href', Mage::getUrl('*/*/*', array ('id' => $product->getId())));
113
+ $element->setAttribute('xlink:title', 'Product details');
114
+ $element->setAttribute('xlink:type', 'resource');
115
+ $element->setAttribute('xlink:actuate', 'onRequest');
116
+ $element->setAttribute('xlink:show', 'new');
117
+
118
+ $this->_appendSimpleNode($element, 'id', $product->getProductId());
119
+ $this->_appendSimpleNode($element, 'sku', $product->getSku());
120
+ $this->_appendSimpleNode($element, 'name', $product->getName());
121
+ $this->_appendSimpleNode($element, 'description', $product->getDescription());
122
+ $this->_appendSimpleNode($element, 'modified', $product->getModified());
123
+ $this->_appendSimpleNode($element, 'created', $product->getCreated());
124
+ $this->_appendSimpleNode($element, 'productUrl', $product->getUrl());
125
+ $this->_appendSimpleNode($element, 'imageUrl', $product->getImage());
126
+ $this->_appendSimpleNode($element, 'thumbnailUrl', $product->getThumbnail());
127
+ $this->_appendSimpleNode($element, 'weight', $product->getWeight());
128
+
129
+ $value = Mage::helper('core')->currency($product->getPrice(), true, false);
130
+ $this->_appendSimpleNode($element, 'price', $value);
131
+ $value = Mage::helper('core')->currency($product->getSpecialPrice(), true, false);
132
+ $this->_appendSimpleNode($element, 'specialPrice', $value);
133
+
134
+ $this->_appendSimpleNode($element, 'isNew', $product->isNew() ? 'yes' : 'no');
135
+
136
+ $element->appendChild($this->_buildCategoriesXML($product));
137
+ $element->appendChild($this->_buildAttributesXML($product));
138
+
139
+ if (!$noParents) {
140
+ $element->appendChild($this->_buildParentsXML($product));
141
  }
142
+
143
+ if (!$noChildren) {
144
+ $element->appendChild($this->_buildChildrenXML($product));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
145
  }
146
+
147
+ return $element;
148
  }
149
 
150
  /**
151
+ * Build product categories XML
152
+ *
153
+ * @param Copernica_MarketingSoftware_Model_Copernica_Entity_Product $product
154
+ * @return DOMElement
 
155
  */
156
+ protected function _buildCategoriesXML(Copernica_MarketingSoftware_Model_Copernica_Entity_Product $product)
157
  {
158
+ $categories = $this->_document->createElement('categories');
159
+
160
+ foreach ($product->getCategoriesList() as $id => $category) {
161
+ $category = $this->_document->createElement('category', htmlspecialchars(html_entity_decode($category)));
162
+ $category->setAttribute('id', $id);
163
+ $categories->appendChild($category);
164
+ }
165
+
166
+ return $categories;
167
+ }
168
+
169
+ /**
170
+ * Build product attributes XML
171
+ *
172
+ * @param Copernica_MarketingSoftware_Model_Copernica_Entity_Product $product
173
+ * @return DOMElement
174
+ */
175
+ protected function _buildAttributesXML(Copernica_MarketingSoftware_Model_Copernica_Entity_Product $product)
176
+ {
177
+ $attributes = $this->_document->createElement('attributes');
178
+ $attributes->setAttribute('name', $product->getAttributeSet());
179
+
180
+ foreach ($product->getAttributesList() as $attribute) {
181
+ $attrElem = $this->_document->createElement($attribute['code'], $attribute['value']);
182
+ $attrElem->setAttribute('type', $attribute['type']);
183
+ $attrElem->setAttribute('label', $attribute['label']);
184
+
185
+ $attributes->appendChild($attrElem);
186
+ }
187
+
188
+ return $attributes;
189
+ }
190
+
191
+ /**
192
+ * Build parents xml
193
+ *
194
+ * @param Copernica_MarketingSoftware_Model_Copernica_Entity_Product $product
195
+ * @return DOMElement
196
+ */
197
+ protected function _buildParentsXML(Copernica_MarketingSoftware_Model_Copernica_Entity_Product $product)
198
+ {
199
+ $parentIds = $product->getNative()->getTypeInstance()->getParentIdsByChild($product->getId());
200
+
201
+ $parents = $this->_document->createElement('parents');
202
+
203
+ foreach ($parentIds as $id) {
204
+ $parents->appendChild($this->_buildProductXML(new Copernica_MarketingSoftware_Model_Copernica_Entity_Product($id), false, true));
205
+ }
206
+
207
+ return $parents;
208
+ }
209
+
210
+ /**
211
+ * Build children xml
212
+ *
213
+ * @param Copernica_MarketingSoftware_Model_Copernica_Entity_Product $product
214
+ * @return DOMElement
215
+ */
216
+ protected function _buildChildrenXML(Copernica_MarketingSoftware_Model_Copernica_Entity_Product $product)
217
+ {
218
+ $childrenIds = $product->getNative()->getTypeInstance()->getChildrenIds($product->getId());
219
+
220
+ $children = $this->_document->createElement('children');
221
+
222
+ foreach ($childrenIds as $groupIds) {
223
+ $group = $this->_document->createElement('group');
224
+
225
+ foreach ($groupIds as $id ) {
226
+ $group->appendChild($this->_buildProductXML(new Copernica_MarketingSoftware_Model_Copernica_Entity_Product($id), true, false));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
227
  }
228
+
229
+ $children->appendChild($group);
230
  }
231
 
232
+ return $children;
233
  }
234
 
235
  /**
236
+ * Prepare XML tree for whole collection of products.
237
  *
238
+ * @param Mage_Catalog_Model_Resource_Product_Collection $collection
239
+ * @return DOMElement
240
  */
241
+ protected function _buildCollectionXML(Mage_Catalog_Model_Resource_Product_Collection $collection)
242
+ {
243
+ $element = $this->_buildRootElement();
244
+
245
+ foreach ($collection as $product) {
246
+ $element->appendChild($this->_buildProductXML(new Copernica_MarketingSoftware_Model_Copernica_Entity_Product($product->getId())));
247
+ }
248
+
249
+ return $element;
250
+ }
251
+
252
+ /**
253
+ * Prepare response based on passed element.
254
+ *
255
+ * @param DOMElement $element
256
+ */
257
+ protected function _prepareResponse(DOMElement $element)
258
  {
259
+ if ($element->tagName == 'product') {
260
+ $newRoot = $this->_buildRootElement();
261
+ $newRoot->appendChild($element);
262
 
263
+ $element = $newRoot;
264
+ }
265
 
266
+ $this->_document->appendChild($element);
 
267
 
268
+ $response = $this->getResponse();
269
+ $response->setHeader('Content-Type', 'text/xml', true);
270
+ $response->clearBody();
271
  $response->sendHeaders();
272
+ $response->setBody($this->_document->saveXML());
 
 
273
  }
274
 
275
  /**
276
+ * Build a root element
277
+ *
278
+ * @return DOMElement
 
279
  */
280
+ protected function _buildRootElement()
281
  {
282
+ $rootElement = $this->_document->createElement('products');
283
+ $rootElement->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:xlink', 'http://www.w3.org/1999/xlink');
284
 
285
+ return $rootElement;
 
286
  }
287
 
288
  /**
289
+ * Handles a request to copernica/product/xml
290
+ * Prints a XML with product information.
291
+ */
292
+ public function xmlAction()
 
 
293
  {
294
+ $request = $this->getRequest();
295
+
296
+ $this->_document = new DOMDocument('1.0', 'utf-8');
297
+
298
+ if ($request->getParam('new')) {
299
+ return $this->_showCollection();
300
+ }
301
+
302
+ if ($request->getParam('id')) {
303
+ return $this->_showProduct();
304
+ }
305
 
306
+ $this->norouteAction();
 
307
  }
308
  }
app/code/community/Copernica/MarketingSoftware/controllers/UnsubscribeController.php CHANGED
@@ -32,34 +32,29 @@ class Copernica_MarketingSoftware_UnsubscribeController extends Mage_Core_Contro
32
  {
33
  /**
34
  * Handles a request to copernica/unsubscribe/process
35
- *
36
  */
37
  public function processAction()
38
  {
39
- // Get the post
40
  $post = $this->getRequest()->getPost();
41
 
42
- // there are parameters and they can be json decoded
43
- if (isset($post['json']) && $data = json_decode($post['json']))
44
- {
45
- // Get the linked customer fields
46
  $fields = Mage::helper('marketingsoftware/config')->getLinkedCustomerFields();
47
 
48
- // is the 'newsletter' field given
49
- if (isset($data->parameters) && property_exists($data->parameters, $fields['newsletter']) && $data->parameters->$fields['newsletter'] == 'unsubscribed_copernica')
50
- {
51
- // get the customer id
52
  $customerID = $data->profile->fields->customer_id;
 
53
  $customer = Mage::getModel('customer/customer')->load($customerID);
 
54
  $email = $data->profile->fields->$fields['email'];
55
 
56
- // Get the subscriber
57
  $subscriber = Mage::getModel('newsletter/subscriber');
 
58
  if ($customer) {
59
  $subscriber->loadByCustomer($customer)->getId();
60
  } else {
61
  $subscriber->loadByEmail($email)->getId();
62
  }
 
63
  $subscriber->setEmail($email);
64
  $subscriber->setSubscriberStatus(Mage_Newsletter_Model_Subscriber::STATUS_UNSUBSCRIBED)->save();
65
 
32
  {
33
  /**
34
  * Handles a request to copernica/unsubscribe/process
 
35
  */
36
  public function processAction()
37
  {
 
38
  $post = $this->getRequest()->getPost();
39
 
40
+ if (isset($post['json']) && $data = json_decode($post['json'])) {
 
 
 
41
  $fields = Mage::helper('marketingsoftware/config')->getLinkedCustomerFields();
42
 
43
+ if (isset($data->parameters) && property_exists($data->parameters, $fields['newsletter']) && $data->parameters->$fields['newsletter'] == 'unsubscribed_copernica') {
 
 
 
44
  $customerID = $data->profile->fields->customer_id;
45
+
46
  $customer = Mage::getModel('customer/customer')->load($customerID);
47
+
48
  $email = $data->profile->fields->$fields['email'];
49
 
 
50
  $subscriber = Mage::getModel('newsletter/subscriber');
51
+
52
  if ($customer) {
53
  $subscriber->loadByCustomer($customer)->getId();
54
  } else {
55
  $subscriber->loadByEmail($email)->getId();
56
  }
57
+
58
  $subscriber->setEmail($email);
59
  $subscriber->setSubscriberStatus(Mage_Newsletter_Model_Subscriber::STATUS_UNSUBSCRIBED)->save();
60
 
app/code/community/Copernica/MarketingSoftware/cron/clearCopernicaDatabase.php CHANGED
@@ -35,28 +35,22 @@ chdir('../../../../../../');
35
  */
36
  require_once 'app/Mage.php';
37
 
38
- // remove current mask
39
  umask(0);
40
 
41
- // if magento is not installed we will just exit this scrtipt
42
- if (!Mage::isInstalled()) exit;
 
43
 
44
- // don't use sessions
45
  Mage::app('admin')->setUseSessionInUrl(false);
46
 
47
- // init config
48
  Mage::getConfig()->init();
49
 
50
- // construct new request
51
- $request = Mage::helper('marketingsoftware/RESTRequest');
52
 
53
- // get current database Id
54
- $databaseId = Mage::helper('marketingsoftware/Config')->getDatabaseId();
55
 
56
- // get profiles
57
  $profiles = $request->get('database/'.$databaseId.'/profiles');
58
 
59
- // iterate over all profiles and remove each of them
60
- foreach ($profiles['data'] as $profile) $request->delete('profile/'.$profile['ID']);
61
-
62
-
35
  */
36
  require_once 'app/Mage.php';
37
 
 
38
  umask(0);
39
 
40
+ if (!Mage::isInstalled()) {
41
+ exit;
42
+ }
43
 
 
44
  Mage::app('admin')->setUseSessionInUrl(false);
45
 
 
46
  Mage::getConfig()->init();
47
 
48
+ $request = Mage::helper('marketingsoftware/rest_request');
 
49
 
50
+ $databaseId = Mage::helper('marketingsoftware/config')->getDatabaseId();
 
51
 
 
52
  $profiles = $request->get('database/'.$databaseId.'/profiles');
53
 
54
+ foreach ($profiles['data'] as $profile) {
55
+ $request->delete('profile/'.$profile['ID']);
56
+ }
 
app/code/community/Copernica/MarketingSoftware/cron/detectAbandonedCarts.php CHANGED
@@ -39,21 +39,15 @@ chdir('../../../../../../');
39
  */
40
  require_once 'app/Mage.php';
41
 
42
- // remove current mask
43
  umask(0);
44
 
45
- // if magento is not installed we will just exit this scrtipt
46
- if (!Mage::isInstalled()) exit;
 
47
 
48
- // don't use sessions
49
  Mage::app('admin')->setUseSessionInUrl(false);
50
 
51
- // init config
52
  Mage::getConfig()->init();
53
 
54
- // get observer
55
  $observer = Mage::getModel('marketingsoftware/observer');
56
-
57
- // detect abandoned carts
58
- $observer->detectAbandonedCarts();
59
-
39
  */
40
  require_once 'app/Mage.php';
41
 
 
42
  umask(0);
43
 
44
+ if (!Mage::isInstalled()) {
45
+ exit;
46
+ }
47
 
 
48
  Mage::app('admin')->setUseSessionInUrl(false);
49
 
 
50
  Mage::getConfig()->init();
51
 
 
52
  $observer = Mage::getModel('marketingsoftware/observer');
53
+ $observer->detectAbandonedCarts();
 
 
 
app/code/community/Copernica/MarketingSoftware/cron/fullSync.php CHANGED
@@ -35,36 +35,28 @@ chdir('../../../../../../');
35
  */
36
  require_once 'app/Mage.php';
37
 
38
- // remove current mask
39
  umask(0);
40
 
41
- // if magento is not installed we will just exit this scrtipt
42
- if (!Mage::isInstalled()) exit;
 
43
 
44
- // don't use sessions
45
  Mage::app('admin')->setUseSessionInUrl(false);
46
 
47
- // init config
48
  Mage::getConfig()->init();
49
 
50
- // get config helper
51
  $config = Mage::helper('marketingsoftware/config');
52
-
53
- // set customer progress status to date when a-bomb hit Hiroshima.
54
- // we can be quite certain that no magento webshop was set up during that
55
- // time.
56
  $config->setCustomerProgressStatus('1945-08-06 08:15:00');
57
  $config->setOrderProgressStatus('1945-08-06 08:15:00');
58
 
59
- // create sync status object
60
- $syncStatus = Mage::getModel('marketingsoftware/SyncStatus');
61
 
62
- // check if current configuration is telling us to filter stores
63
- if ($enabledStores = $config->getEnabledStores()) $syncStatus->setStoresFilter($enabledStores);
 
64
 
65
- // The start sync token must be added to the queue
66
- $queue = Mage::getModel('marketingsoftware/queue')
67
  ->setObject($syncStatus->toArray())
68
  ->setAction('start_sync')
69
- ->setName('startSync')
70
  ->save();
35
  */
36
  require_once 'app/Mage.php';
37
 
 
38
  umask(0);
39
 
40
+ if (!Mage::isInstalled()) {
41
+ exit;
42
+ }
43
 
 
44
  Mage::app('admin')->setUseSessionInUrl(false);
45
 
 
46
  Mage::getConfig()->init();
47
 
 
48
  $config = Mage::helper('marketingsoftware/config');
 
 
 
 
49
  $config->setCustomerProgressStatus('1945-08-06 08:15:00');
50
  $config->setOrderProgressStatus('1945-08-06 08:15:00');
51
 
52
+ $syncStatus = Mage::getModel('marketingsoftware/sync_status');
 
53
 
54
+ if ($enabledStores = $config->getEnabledStores()) {
55
+ $syncStatus->setStoresFilter($enabledStores);
56
+ }
57
 
58
+ $queue = Mage::getModel('marketingsoftware/queue_item')
 
59
  ->setObject($syncStatus->toArray())
60
  ->setAction('start_sync')
61
+ ->setName('startsync')
62
  ->save();
app/code/community/Copernica/MarketingSoftware/cron/processQueue.php CHANGED
@@ -36,9 +36,7 @@ chdir('../../../../../../');
36
 
37
  $cliOptions = getopt('h::c::l::v::r::', array('help::', 'customer::', 'lock::', 'verbose::', 'runtime::'));
38
 
39
- // should be display usage/help?
40
- if (array_key_exists('h', $cliOptions) || array_key_exists('help', $cliOptions))
41
- {
42
  echo "======================================================================".PHP_EOL;
43
  echo " Copernica Marketing Software Magento Extension".PHP_EOL;
44
  echo "======================================================================".PHP_EOL;
@@ -70,83 +68,75 @@ if (array_key_exists('h', $cliOptions) || array_key_exists('help', $cliOptions))
70
  exit();
71
  }
72
 
73
- $customerId = -1;
 
74
 
75
- // check if we should process events associated with one customer
76
- if (array_key_exists('c', $cliOptions) || array_key_exists('customer', $cliOptions))
77
- {
78
- $customerId = array_key_exists('c', $cliOptions) ? $cliOptions['c'] : 0;
79
- if (array_key_exists('customer', $cliOptions)) $customerId = $cliOptions['customer'];
80
- }
81
-
82
- $lock = false;
83
 
84
- if (array_key_exists('l', $cliOptions) || array_key_exists('lock', $cliOptions))
85
- {
86
- $lock = true;
87
- }
88
 
89
- $verbose = false;
 
 
90
 
91
- if (array_key_exists('v', $cliOptions) || array_key_exists('verbose', $cliOptions))
92
- {
93
- $verbose = array_key_exists('v', $cliOptions) ? $cliOptions['v'] : 'TEXT';
94
- if (array_key_exists('verbose', $cliOptions)) $verbose = $cliOptions['verbose'] ? $cliOptions['verbose'] : 'TEXT';
95
- }
96
 
97
- $runtime = 45;
 
 
 
 
 
 
98
 
99
- if (array_key_exists('r', $cliOptions) || array_key_exists('runtime', $cliOptions))
100
- {
101
- $runtime = array_key_exists('r', $cliOptions) ? $cliOptions['r'] : 45;
102
- if (array_key_exists('runtime', $cliOptions)) $runtime = $cliOptions['runtime'];
103
- }
104
 
105
- /**
106
- * We need to require magento facade. Since this script should always be in same
107
- * relative place we can require by relative path.
108
- */
109
- require_once 'app/Mage.php';
 
 
110
 
111
- // remove current mask
112
- umask(0);
113
 
114
- // if magento is not installed we will just exit this scrtipt
115
- if (!Mage::isInstalled()) exit;
116
 
117
- // don't use sessions
118
- Mage::app('admin')->setUseSessionInUrl(false);
 
119
 
120
- // init config
121
- Mage::getConfig()->init();
122
 
123
- // get queue processor
124
- $processor = Mage::getModel('marketingsoftware/QueueProcessor');
125
 
126
- // check if we should lock our processor
127
- if ($lock === false) $processor->processQueue($customerId);
128
 
129
- // we should lock our processor
130
- else {
 
 
131
 
132
- // try to aqcuire a lock that will be used when processing
133
- $lock = $processor->aqcuireLock();
 
 
 
 
134
 
135
- // check if we have a lock
136
- if ($lock === false)
137
- {
138
- /*
139
- * When this script is executed by supervisor or similiar software it
140
- * will be restarted just after it exits. To not hammer servers with
141
- * script super quick executions we will sleep for couple of seconds.
142
- */
143
- if ($runtime > 0 ) sleep($runtime);
144
- return;
145
  }
146
 
147
- // process queue with locking
148
- $processor->processWithLocking($lock);
149
- }
150
-
151
- // check if we should output something
152
- if ($verbose !== false) echo $processor->fetchReport($verbose);
36
 
37
  $cliOptions = getopt('h::c::l::v::r::', array('help::', 'customer::', 'lock::', 'verbose::', 'runtime::'));
38
 
39
+ if (array_key_exists('h', $cliOptions) || array_key_exists('help', $cliOptions)) {
 
 
40
  echo "======================================================================".PHP_EOL;
41
  echo " Copernica Marketing Software Magento Extension".PHP_EOL;
42
  echo "======================================================================".PHP_EOL;
68
  exit();
69
  }
70
 
71
+ try {
72
+ $customerId = -1;
73
 
74
+ if (array_key_exists('c', $cliOptions) || array_key_exists('customer', $cliOptions)) {
75
+ $customerId = array_key_exists('c', $cliOptions) ? $cliOptions['c'] : 0;
76
+
77
+ if (array_key_exists('customer', $cliOptions)) {
78
+ $customerId = $cliOptions['customer'];
79
+ }
80
+ }
 
81
 
82
+ $lock = false;
 
 
 
83
 
84
+ if (array_key_exists('l', $cliOptions) || array_key_exists('lock', $cliOptions)) {
85
+ $lock = true;
86
+ }
87
 
88
+ $verbose = false;
 
 
 
 
89
 
90
+ if (array_key_exists('v', $cliOptions) || array_key_exists('verbose', $cliOptions)) {
91
+ $verbose = array_key_exists('v', $cliOptions) ? $cliOptions['v'] : 'TEXT';
92
+
93
+ if (array_key_exists('verbose', $cliOptions)) {
94
+ $verbose = $cliOptions['verbose'] ? $cliOptions['verbose'] : 'TEXT';
95
+ }
96
+ }
97
 
98
+ $runtime = 45;
 
 
 
 
99
 
100
+ if (array_key_exists('r', $cliOptions) || array_key_exists('runtime', $cliOptions)) {
101
+ $runtime = array_key_exists('r', $cliOptions) ? $cliOptions['r'] : 45;
102
+
103
+ if (array_key_exists('runtime', $cliOptions)) {
104
+ $runtime = $cliOptions['runtime'];
105
+ }
106
+ }
107
 
108
+ require_once 'app/Mage.php';
 
109
 
110
+ umask(0);
 
111
 
112
+ if (!Mage::isInstalled()) {
113
+ exit;
114
+ }
115
 
116
+ Mage::app('admin')->setUseSessionInUrl(false);
 
117
 
118
+ Mage::getConfig()->init();
 
119
 
120
+ $processor = Mage::getModel('marketingsoftware/queue_processor');
 
121
 
122
+ if ($lock === false) {
123
+ $processor->processQueue($customerId);
124
+ } else {
125
+ $lock = $processor->aqcuireLock();
126
 
127
+ if ($lock === false) {
128
+ if ($runtime > 0 ) {
129
+ sleep($runtime);
130
+ }
131
+ return;
132
+ }
133
 
134
+ $processor->processWithLocking($lock);
 
 
 
 
 
 
 
 
 
135
  }
136
 
137
+ if ($verbose !== false) {
138
+ echo $processor->fetchReport($verbose);
139
+ }
140
+ } catch (Exception $e) {
141
+ print_r($e);
142
+ }
app/code/community/Copernica/MarketingSoftware/data/marketingsoftware_setup/data-upgrade-3.4.0-3.4.1.php CHANGED
@@ -24,6 +24,10 @@
24
  * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
  */
26
 
 
 
 
 
27
  return;
28
 
29
  // array of all configs entries that are describing linked fields
@@ -63,4 +67,4 @@ foreach ($linkedFieldConfigs as $config)
63
  // store converted array
64
  $model->setValue(json_encode($array));
65
  }
66
- }
24
  * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
  */
26
 
27
+ $config = Mage::helper('marketingsoftware/config');
28
+ $config->setVanillaCrons(true);
29
+ $config->setEnabledStores(null);
30
+
31
  return;
32
 
33
  // array of all configs entries that are describing linked fields
67
  // store converted array
68
  $model->setValue(json_encode($array));
69
  }
70
+ }
app/code/community/Copernica/MarketingSoftware/etc/adminhtml.xml CHANGED
@@ -19,6 +19,10 @@
19
  <title>Synchronize Data</title>
20
  <action>adminhtml/marketingsoftware_export</action>
21
  </export>
 
 
 
 
22
  <!-- <sync translate="title" module="marketingsoftware">
23
  <title>Synchronization profiles</title>
24
  <action>adminhtml/marketingsoftware_sync</action>
19
  <title>Synchronize Data</title>
20
  <action>adminhtml/marketingsoftware_export</action>
21
  </export>
22
+ <!-- <accessToken translate="title" module="marketingsoftware">
23
+ <title>Access Token</title>
24
+ <action>adminhtml/marketingsoftware_accesstoken</action>
25
+ </accessToken> -->
26
  <!-- <sync translate="title" module="marketingsoftware">
27
  <title>Synchronization profiles</title>
28
  <action>adminhtml/marketingsoftware_sync</action>
app/code/community/Copernica/MarketingSoftware/etc/config.xml CHANGED
@@ -2,7 +2,7 @@
2
  <config>
3
  <modules>
4
  <Copernica_MarketingSoftware>
5
- <version>3.0.1</version>
6
  </Copernica_MarketingSoftware>
7
  </modules>
8
  <global>
@@ -14,24 +14,24 @@
14
  <marketingsoftware_mysql4>
15
  <class>Copernica_MarketingSoftware_Model_Mysql4</class>
16
  <entities>
17
- <queue>
18
  <table>copernica_queue</table>
19
- </queue>
20
  <config_data>
21
  <table>copernica_config_data</table>
22
  </config_data>
23
- <profileCache>
24
  <table>copernica_profile_cache</table>
25
- </profileCache>
26
- <errorqueue>
27
  <table>copernica_error_queue</table>
28
- </errorqueue>
29
- <syncProfile>
30
  <table>copernica_sync_profile</table>
31
- </syncProfile>
32
- <abandonedCart>
33
  <table>copernica_abandoned_cart</table>
34
- </abandonedCart>
35
  </entities>
36
  </marketingsoftware_mysql4>
37
  </models>
@@ -124,6 +124,14 @@
124
  </marketingsoftware_observer>
125
  </observers>
126
  </catalog_controller_product_view>
 
 
 
 
 
 
 
 
127
  </events>
128
  <resources>
129
  <marketingsoftware_setup>
@@ -185,7 +193,7 @@
185
  <run><model>marketingsoftware/observer::processQueue</model></run>
186
  </marketingsoftware_process_queue>
187
  <marketingsoftware_detect_abandoned_carts>
188
- <schedule><cron_expr>*/5 * * * *</cron_expr></schedule>
189
  <run><model>marketingsoftware/observer::detectAbandonedCarts</model></run>
190
  </marketingsoftware_detect_abandoned_carts>
191
  </jobs>
2
  <config>
3
  <modules>
4
  <Copernica_MarketingSoftware>
5
+ <version>3.1.0</version>
6
  </Copernica_MarketingSoftware>
7
  </modules>
8
  <global>
14
  <marketingsoftware_mysql4>
15
  <class>Copernica_MarketingSoftware_Model_Mysql4</class>
16
  <entities>
17
+ <queue_item>
18
  <table>copernica_queue</table>
19
+ </queue_item>
20
  <config_data>
21
  <table>copernica_config_data</table>
22
  </config_data>
23
+ <profile_cache>
24
  <table>copernica_profile_cache</table>
25
+ </profile_cache>
26
+ <error_queue>
27
  <table>copernica_error_queue</table>
28
+ </error_queue>
29
+ <sync_profile>
30
  <table>copernica_sync_profile</table>
31
+ </sync_profile>
32
+ <abandoned_cart>
33
  <table>copernica_abandoned_cart</table>
34
+ </abandoned_cart>
35
  </entities>
36
  </marketingsoftware_mysql4>
37
  </models>
124
  </marketingsoftware_observer>
125
  </observers>
126
  </catalog_controller_product_view>
127
+ <wishlist_item_save_after>
128
+ <observers>
129
+ <marketingsoftware_observer>
130
+ <class>marketingsoftware/observer</class>
131
+ <method>wishlistItemModified</method>
132
+ </marketingsoftware_observer>
133
+ </observers>
134
+ </wishlist_item_save_after>
135
  </events>
136
  <resources>
137
  <marketingsoftware_setup>
193
  <run><model>marketingsoftware/observer::processQueue</model></run>
194
  </marketingsoftware_process_queue>
195
  <marketingsoftware_detect_abandoned_carts>
196
+ <schedule><cron_expr>*/30 * * * *</cron_expr></schedule>
197
  <run><model>marketingsoftware/observer::detectAbandonedCarts</model></run>
198
  </marketingsoftware_detect_abandoned_carts>
199
  </jobs>
app/code/community/Copernica/MarketingSoftware/sql/marketingsoftware_setup/mysql4-upgrade-2.3.2-2.3.3.php CHANGED
@@ -12,7 +12,7 @@ try {
12
  $tableName = $this->getTable('marketingsoftware/profileCache');
13
 
14
  // drop old table (it should be safe to drop such table)
15
- $installer->getConnection()->dropTable($tableName);
16
 
17
  /*
18
  * Create new table. Note that it will create DDL table definition. It's
12
  $tableName = $this->getTable('marketingsoftware/profileCache');
13
 
14
  // drop old table (it should be safe to drop such table)
15
+ $installer->run("DROP TABLE IF EXISTS {$tableName}");
16
 
17
  /*
18
  * Create new table. Note that it will create DDL table definition. It's
app/code/community/Copernica/MarketingSoftware/sql/marketingsoftware_setup/mysql4-upgrade-2.3.8-2.4.0.php CHANGED
@@ -43,10 +43,17 @@ try
43
  // we should remove all events that were on queue till this point
44
  $installer->getConnection()->query("DELETE FROM $queueTableName");
45
 
 
 
 
 
 
 
 
46
  // We will need a name for our events.
47
  $installer->getConnection()->addColumn($queueTableName, 'name', array(
48
  'comment' => 'name of the event',
49
- 'type' => Varien_Db_Ddl_Table::TYPE_TEXT,
50
  'length' => 255,
51
  'nullable' => true,
52
  'default' => null
43
  // we should remove all events that were on queue till this point
44
  $installer->getConnection()->query("DELETE FROM $queueTableName");
45
 
46
+ /**
47
+ * Varien lib is a piece of s***. It has problems with defining text types
48
+ * for mysql tables. This is why we have to check if we have our desired
49
+ * type and make a fallback on some auxiliary type.
50
+ */
51
+ $textType = defined('Varien_Db_Ddl_Table::TYPE_TEXT') ? Varien_Db_Ddl_Table::TYPE_TEXT : Varien_Db_Ddl_Table::TYPE_VARCHAR;
52
+
53
  // We will need a name for our events.
54
  $installer->getConnection()->addColumn($queueTableName, 'name', array(
55
  'comment' => 'name of the event',
56
+ 'type' => $textType,
57
  'length' => 255,
58
  'nullable' => true,
59
  'default' => null
app/code/community/Copernica/MarketingSoftware/sql/marketingsoftware_setup/mysql4-upgrade-2.4.1-2.4.2.php CHANGED
@@ -36,7 +36,7 @@ try
36
  $tableName = $this->getTable('marketingsoftware/syncProfile');
37
 
38
  // drop old table (it should be safe to drop such table)
39
- $installer->getConnection()->dropTable($tableName);
40
 
41
  /*
42
  * Create new table. Note that it will create DDL table definition. It's
36
  $tableName = $this->getTable('marketingsoftware/syncProfile');
37
 
38
  // drop old table (it should be safe to drop such table)
39
+ $installer->run("DROP TABLE IF EXISTS {$tableName}");
40
 
41
  /*
42
  * Create new table. Note that it will create DDL table definition. It's
app/code/community/Copernica/MarketingSoftware/sql/marketingsoftware_setup/mysql4-upgrade-2.4.2-2.4.3.php CHANGED
@@ -36,7 +36,7 @@ try
36
  $tableName = $this->getTable('marketingsoftware/abandonedCart');
37
 
38
  // drop old table (it should be safe to drop such table)
39
- $installer->getConnection()->dropTable($tableName);
40
 
41
  /*
42
  * Create new table. Note that it will create DDL table definition. It's
@@ -60,7 +60,7 @@ try
60
  ), 'Quote id associated with abandoned cart');
61
 
62
  // add copernica customer Id column
63
- $table->addColumn('timestamp', Varien_Db_Ddl_Table::TYPE_DATETIME, 255, array(
64
  'nullable' => false
65
  ), 'When cart was detected');
66
 
36
  $tableName = $this->getTable('marketingsoftware/abandonedCart');
37
 
38
  // drop old table (it should be safe to drop such table)
39
+ $installer->run("DROP TABLE IF EXISTS {$tableName}");
40
 
41
  /*
42
  * Create new table. Note that it will create DDL table definition. It's
60
  ), 'Quote id associated with abandoned cart');
61
 
62
  // add copernica customer Id column
63
+ $table->addColumn('timestamp', Varien_Db_Ddl_Table::TYPE_TIMESTAMP, 255, array(
64
  'nullable' => false
65
  ), 'When cart was detected');
66
 
app/code/community/Copernica/MarketingSoftware/sql/marketingsoftware_setup/mysql4-upgrade-2.4.3-2.4.4.php CHANGED
@@ -39,7 +39,7 @@ try
39
  $tableName = $this->getTable('marketingsoftware/profileCache');
40
 
41
  // drop old table (it should be safe to drop such table)
42
- $installer->getConnection()->dropTable($tableName);
43
 
44
  /*
45
  * Create new table. Note that it will create DDL table definition. It's
39
  $tableName = $this->getTable('marketingsoftware/profileCache');
40
 
41
  // drop old table (it should be safe to drop such table)
42
+ $installer->run("DROP TABLE IF EXISTS {$tableName}");
43
 
44
  /*
45
  * Create new table. Note that it will create DDL table definition. It's
app/design/adminhtml/default/default/template/marketingsoftware/accessToken.phtml ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * View for setting access token manually
4
+ * Copernica Marketing Software
5
+ * http://www.copernica.com
6
+ */
7
+
8
+ ?>
9
+
10
+ <div class="content-header">
11
+ <h3 class="icon-head head-system-account">
12
+ Copernica Marketing Software: Access Token
13
+ </h3>
14
+ </div>
15
+
16
+ <div class="entry-edit">
17
+ <form action="<?php echo $this->getPostUrl(); ?>" method="post">
18
+ <?php echo $this->getBlockHtml('formkey'); ?>
19
+ <div class="fieldset">
20
+ <div class="hor-scroll">
21
+ <table cellspacing="0" class="form-list">
22
+ <tbody>
23
+ <tr>
24
+ <td class="label">
25
+ <label>Access Token Value</label>
26
+ </td>
27
+ <td class="value">
28
+ <input type="text" class="required-entry input-text" title="Access Token" name="access_token" value="<?php echo $this->getAccessTokenValue(); ?>">
29
+ </td>
30
+ </tr>
31
+ <tr>
32
+ <td class="label">
33
+ <button>
34
+ Store access token
35
+ </button>
36
+ </td>
37
+ </tr>
38
+ </tbody>
39
+ </table>
40
+ </div>
41
+ </div>
42
+ </form>
43
+ </div>
app/design/adminhtml/default/default/template/marketingsoftware/export.phtml CHANGED
@@ -8,7 +8,7 @@
8
 
9
  $_helper = Mage::helper('marketingsoftware');
10
  $_config = Mage::helper('marketingsoftware/config');
11
- $_collection = Mage::getResourceModel('marketingsoftware/queue_collection');
12
  $_general_config = Mage::getConfig();
13
  $_core = Mage::helper('core');
14
 
8
 
9
  $_helper = Mage::helper('marketingsoftware');
10
  $_config = Mage::helper('marketingsoftware/config');
11
+ $_collection = Mage::getResourceModel('marketingsoftware/queue_item_collection');
12
  $_general_config = Mage::getConfig();
13
  $_core = Mage::helper('core');
14
 
app/design/adminhtml/default/default/template/marketingsoftware/link.phtml CHANGED
@@ -19,13 +19,20 @@
19
  Copernica structure
20
  </h3>
21
  <ul id="collection_list" class="tabs">
 
 
 
 
 
 
 
22
  <li data-target="main">
23
  <a class="tab-item-link">
24
  <span>
25
  Database
26
  </span>
27
  </a>
28
- </li>
29
  <li data-target="cartproducts">
30
  <a class="tab-item-link">
31
  <span>
@@ -43,7 +50,7 @@
43
  <li data-target="orderproducts">
44
  <a class="tab-item-link">
45
  <span>
46
- Order products collection
47
  </span>
48
  </a>
49
  </li>
@@ -61,13 +68,13 @@
61
  </span>
62
  </a>
63
  </li>
64
- <li data-target="default">
65
  <a class="tab-item-link">
66
  <span>
67
- Default structure
68
  </span>
69
  </a>
70
- </li>
71
  </ul>
72
  </div>
73
 
@@ -85,10 +92,10 @@
85
  // urls in controller and actual usage will be in js file.
86
  Copernica.ajaxUrls = {
87
  collection: {
88
- fetch: '<?php echo $this->getAjaxCollectionFetchUrl(); ?>',
89
- store: '<?php echo $this->getAjaxCollectionStoreUrl(); ?>',
90
- default: '<?php echo $this->getAjaxCollectionDefaultUrl(); ?>',
91
- validate: '<?php echo $this->getAjaxCollectionValidateUrl(); ?>'
92
  }
93
  };
94
 
19
  Copernica structure
20
  </h3>
21
  <ul id="collection_list" class="tabs">
22
+ <li data-target="default">
23
+ <a class="tab-item-link">
24
+ <span>
25
+ Default structure
26
+ </span>
27
+ </a>
28
+ </li>
29
  <li data-target="main">
30
  <a class="tab-item-link">
31
  <span>
32
  Database
33
  </span>
34
  </a>
35
+ </li>
36
  <li data-target="cartproducts">
37
  <a class="tab-item-link">
38
  <span>
50
  <li data-target="orderproducts">
51
  <a class="tab-item-link">
52
  <span>
53
+ Order items collection
54
  </span>
55
  </a>
56
  </li>
68
  </span>
69
  </a>
70
  </li>
71
+ <li data-target="wishlistproducts">
72
  <a class="tab-item-link">
73
  <span>
74
+ Wishlist items collection
75
  </span>
76
  </a>
77
+ </li>
78
  </ul>
79
  </div>
80
 
92
  // urls in controller and actual usage will be in js file.
93
  Copernica.ajaxUrls = {
94
  collection: {
95
+ default: '<?php echo $this->getAjaxCollectionDefaultUrl(); ?>',
96
+ fetch: '<?php echo $this->getAjaxCollectionFetchUrl(); ?>',
97
+ validate: '<?php echo $this->getAjaxCollectionValidateUrl(); ?>',
98
+ store: '<?php echo $this->getAjaxCollectionStoreUrl(); ?>'
99
  }
100
  };
101
 
app/design/adminhtml/default/default/template/marketingsoftware/settings.phtml CHANGED
@@ -13,20 +13,20 @@ $authorizeUrl = 'https://www.copernica.com/en/authorize';
13
 
14
  // what we will include in our query string
15
  $query = array(
 
 
 
 
 
 
 
 
 
 
 
 
16
  // get access to all resources
17
- 'scope' => 'all',
18
-
19
- // we want response in form of code
20
- 'response_type' => 'code',
21
-
22
- // we do store state inside session
23
- 'state' => Mage::getSingleton('adminhtml/session')->getState(),
24
-
25
- // client_id
26
- 'client_id' => $_config->getClientKey(),
27
-
28
- // where we should return after we authorize?
29
- 'redirect_uri' => $this->getStateUrl(),
30
  );
31
 
32
  // parts of the query string
@@ -51,15 +51,15 @@ Event.observe(window, 'load', init, false);
51
  function init() {
52
 
53
  // when button is clicked we want to save information on server
54
- $('cp_save').on('click', function(event) {
55
  $('cp_form').submit();
56
  });
57
 
58
- $('qs_save').on('click', function(event) {
59
  $('qs_form').submit();
60
  });
61
 
62
- $('ss_save').on('click', function(event) {
63
  $('ss_form').submit();
64
  });
65
  }
@@ -145,6 +145,13 @@ function init() {
145
  Fill Client key.
146
  </p>
147
  <?php } ?>
 
 
 
 
 
 
 
148
  </td>
149
  </tr>
150
  <?php if ($response = $this->getRequest()->getParam('response')) { ?>
@@ -206,7 +213,7 @@ function init() {
206
  Api server
207
  </td>
208
  <td class="value">
209
- <input type="text" name="qs_api_server" id="qs_api_server" class="input-text" value="<?php $host = $_config->getApiHostname(); if ($host) echo $host; else echo 'https://api.copernica.com;' ?>">
210
  </td>
211
  </tr>
212
  <tr>
13
 
14
  // what we will include in our query string
15
  $query = array(
16
+ // client_id
17
+ 'client_id' => $_config->getClientKey(),
18
+
19
+ // where we should return after we authorize?
20
+ 'redirect_uri' => $this->getStateUrl(),
21
+
22
+ // we do store state inside session
23
+ 'state' => Mage::getSingleton('adminhtml/session')->getState(),
24
+
25
+ // we want response in form of code
26
+ 'response_type' => 'code',
27
+
28
  // get access to all resources
29
+ 'scope' => 'all',
 
 
 
 
 
 
 
 
 
 
 
 
30
  );
31
 
32
  // parts of the query string
51
  function init() {
52
 
53
  // when button is clicked we want to save information on server
54
+ $('cp_save').observe('click', function(event) {
55
  $('cp_form').submit();
56
  });
57
 
58
+ $('qs_save').observe('click', function(event) {
59
  $('qs_form').submit();
60
  });
61
 
62
+ $('ss_save').observe('click', function(event) {
63
  $('ss_form').submit();
64
  });
65
  }
145
  Fill Client key.
146
  </p>
147
  <?php } ?>
148
+
149
+ </td>
150
+ </tr>
151
+ <tr>
152
+ <td class="label"></td>
153
+ <td class="value'">
154
+ <a href="<?php echo $this->getAccessTokenUrl(); ?>">inspect access token</a>
155
  </td>
156
  </tr>
157
  <?php if ($response = $this->getRequest()->getParam('response')) { ?>
213
  Api server
214
  </td>
215
  <td class="value">
216
+ <input type="text" name="qs_api_server" id="qs_api_server" class="input-text" value="<?php $host = $_config->getApiHostname(); if ($host) echo $host; else echo 'https://api.copernica.com'; ?>">
217
  </td>
218
  </tr>
219
  <tr>
js/copernica/marketingsoftware/link.js CHANGED
@@ -25,7 +25,7 @@ Copernica.MainView = function (rootElement) {
25
  */
26
  var current;
27
 
28
- // install on lick handler on collection list
29
  rootElement.select('#collection_list')[0].observe('click', function (event) {
30
 
31
  // change active tab
@@ -53,8 +53,8 @@ Copernica.MainView = function (rootElement) {
53
 
54
  });
55
 
56
- // chanage collection to main database collection (the database)
57
- rootElement.select('#collection_list [data-target="main"] a')[0].click();
58
 
59
  // install on click handler on save button
60
  $('save').observe('click', function (event) {
@@ -87,7 +87,7 @@ Copernica.DefaultView = function (rootElement) {
87
  requestHeaders: { Accept: 'application/json' },
88
  parameters: { name: name },
89
  onComplete: function (response) {
90
- window.location.reload();
91
  }
92
  });
93
  });
@@ -228,7 +228,7 @@ Copernica.CollectionView = function (rootElement, name) {
228
  * Show main collection
229
  */
230
  var showMainCollection = function () {
231
- $$('#collection_list [data-collection="main"] a')[0].click();
232
  };
233
 
234
  // show that we are fetching data
@@ -245,7 +245,11 @@ Copernica.CollectionView = function (rootElement, name) {
245
  var answer = response.responseText.evalJSON();
246
 
247
  // check if we have an error
248
- if (answer.error == 'no database') return showMainCollection();
 
 
 
 
249
 
250
  // clear root element
251
  rootElement.innerHTML = '';
@@ -289,7 +293,7 @@ Copernica.CollectionView = function (rootElement, name) {
289
  // create input
290
  var input = new Element('input', { class: 'input-text', name: 'collection_name' }).setValue(answer.linkedName);
291
  value.insert(input);
292
-
293
  // create fieldset
294
  var fieldSet = new Element('div', { class: 'fieldset fieldset-wide' });
295
  entryEdit.insert(fieldSet);
25
  */
26
  var current;
27
 
28
+ // install on click handler on collection list
29
  rootElement.select('#collection_list')[0].observe('click', function (event) {
30
 
31
  // change active tab
53
 
54
  });
55
 
56
+ // change collection to default structure
57
+ rootElement.select('#collection_list [data-target="default"] a')[0].click();
58
 
59
  // install on click handler on save button
60
  $('save').observe('click', function (event) {
87
  requestHeaders: { Accept: 'application/json' },
88
  parameters: { name: name },
89
  onComplete: function (response) {
90
+ //window.location.reload();
91
  }
92
  });
93
  });
228
  * Show main collection
229
  */
230
  var showMainCollection = function () {
231
+ $$('#collection_list [data-target="main"] a')[0].click();
232
  };
233
 
234
  // show that we are fetching data
245
  var answer = response.responseText.evalJSON();
246
 
247
  // check if we have an error
248
+ if (answer.error == 'no database') {
249
+ var html = '<ul class="messages" id="no-data-warning"><li class="warning-msg"><ul><li>A database has not been entered.</li></ul></li></ul>';
250
+ $('messages').update(html);
251
+ return showMainCollection();
252
+ }
253
 
254
  // clear root element
255
  rootElement.innerHTML = '';
293
  // create input
294
  var input = new Element('input', { class: 'input-text', name: 'collection_name' }).setValue(answer.linkedName);
295
  value.insert(input);
296
+
297
  // create fieldset
298
  var fieldSet = new Element('div', { class: 'fieldset fieldset-wide' });
299
  entryEdit.insert(fieldSet);
package.xml CHANGED
@@ -1,7 +1,7 @@
1
  <?xml version="1.0"?>
2
  <package>
3
  <name>Copernica_MarketingSoftware</name>
4
- <version>3.0.1</version>
5
  <stability>stable</stability>
6
  <license uri="http://opensource.org/licenses/osl-3.0.php">Open Software License (OSL 3.0)</license>
7
  <channel>community</channel>
@@ -10,12 +10,12 @@
10
  <description>When you install the integration, all contact, order and shop cart data from Magento will be automatically synchronized and stored in Copernica. By synchronizing data between your Magento webshop and Copernica, you are able to set up targeted email campaigns. For example you can consider emailing customers with abandoned shopcarts automatically every week. Your contacts will only receive information relevant to them, which will make them come back to your shop time and again. The available integration is also compatible with the Magento Enterprise edition.&#xD;
11
  &#xD;
12
  Please note that this extension does not work with PHP 5.4.X. and should be run with the Magento system requirements in mind.</description>
13
- <notes>- Several bug fixes which have been discovered by verious users in the last release. &#xD;
14
- </notes>
15
  <authors><author><name>Cream</name><user>creaminternet</user><email>info@cream.nl</email></author></authors>
16
- <date>2014-11-03</date>
17
- <time>22:26:13</time>
18
- <contents><target name="magecommunity"><dir name="Copernica"><dir name="MarketingSoftware"><dir name="Block"><dir name="Adminhtml"><dir name="Marketingsoftware"><file name="Export.php" hash="487cc0386e351c908d5bb2a6aa88c6d7"/><file name="Link.php" hash="d9e39fd2b3bb17017d0ede766fe4c857"/><file name="Settings.php" hash="105197d4382b03fadfa43b5616894bd4"/><file name="Sync.php" hash="f841cc8adcd5aaab0c339e8c3a4532bc"/></dir></dir></dir><dir name="Controller"><file name="Base.php" hash="5a5b8a0121ca6025fdd4707a76e34911"/></dir><file name="Exception.php" hash="ad3646aad8fcf3c678b7cdfa91d651e1"/><dir name="Helper"><file name="Api.php" hash="e5bb4038213a529f09ef8e51812b7877"/><file name="ApiBase.php" hash="58cdf8255a33bf8fb78060afc74430a8"/><file name="ApiBuilder.php" hash="632f066c5664299cc1a4c4e37231f743"/><file name="ApiValidator.php" hash="3e1db1fdbbf9a1e59b9f0222325209ed"/><file name="Config.php" hash="029fa7e372c67e6ad2d9cf97c4784782"/><file name="Data.php" hash="b6cdfe5ddbc2f467b446690511435727"/><file name="DataWriter.php" hash="af3bba3409fba39320f99e83a34fc44c"/><file name="Profile.php" hash="bb6ab328045fb12319de467ce9c7b2ae"/><file name="RESTRequest.php" hash="3637be80e677c7bf7da8bcc7ffccecf9"/></dir><dir name="Model"><file name="AbandonedCart.php" hash="51f3f1478a89de28ee253d9f94b3196c"/><file name="AbandonedCartsProcessor.php" hash="9cc44c9eaedabe38ef7860945d46c062"/><dir name="Abstraction"><file name="Address.php" hash="57e6404f732bb0b831e1498f28ac464c"/><file name="Attributes.php" hash="ac06dfb53f78e5210e12bc1f68cbb18e"/><file name="Customer.php" hash="e84dc90ae63c296641ed30119537a489"/><file name="Name.php" hash="a2edffc4a77440e2b7df7cefb0b43583"/><dir name="Order"><dir name="Item"><file name="Options.php" hash="806711e5c0303cedfc724ce02868c85d"/></dir><file name="Item.php" hash="366055e71f98f47ec329b070aacfe518"/></dir><file name="Order.php" hash="7cff6f7e9c3aebac647e35d2ebb99c66"/><file name="Price.php" hash="e48a7323579b230cf6dd487041784e08"/><file name="Product.php" hash="556476f42785f5554698da09567581c0"/><dir name="Quote"><dir name="Item"><file name="Options.php" hash="f836a70b98c345975ad8d91e6ef88d3f"/></dir><file name="Item.php" hash="c56b80f5bfb17051b42eff127b58b94d"/></dir><file name="Quote.php" hash="8df90763d3685b755a080691b06bca7d"/><file name="Storeview.php" hash="68664c1717bc95ff41765f94f48610d2"/><file name="Subscription.php" hash="7b163f295803d391557c22572ff81850"/><file name="Viewedproduct.php" hash="1a29f985c6e53d5188639d1a09c24344"/></dir><file name="Config.php" hash="d64086db5762e020049e309062e128f5"/><dir name="Copernica"><file name="Abstract.php" hash="31d026df826a9c8d716bd51388eda480"/><dir name="Address"><file name="Subprofile.php" hash="23367765c877ea7968c6b5d58cc21ea9"/></dir><dir name="Cartitem"><file name="Subprofile.php" hash="2cb3eb3f6add77dbd75f69787a9953fb"/></dir><dir name="Entity"><file name="Address.php" hash="0ae0020f4319fc112ddce2d14ceda209"/><file name="CartItem.php" hash="3fcffafcc80de592d81db7a5bfb2317e"/><file name="Customer.php" hash="e846a04aca3b2c81135379d0c2916b4a"/><file name="Item.php" hash="9c6c516a448bde9d36e2dee534a9a9da"/><file name="Order.php" hash="ce02b453677a27b5e65cb8544c7364fa"/><file name="Product.php" hash="feecd138d77c1ccf0bf32d89149fd16b"/><file name="Quote.php" hash="027d60b614f2b4b20a512776b588e853"/><file name="Subscription.php" hash="c58dae96eb1b9e2122fedd8581aad638"/></dir><file name="Entity.php" hash="e3cd012a331f11f0ec984c0937717a0e"/><dir name="Order"><file name="Subprofile.php" hash="3219e6835cccc7be482259576d64a9d4"/></dir><dir name="Orderitem"><file name="Subprofile.php" hash="ee7f2ab5e04fdca847c419b321639333"/></dir><file name="Profile.php" hash="506544ea302cbccd649d82844cb11375"/><file name="Profilecustomer.php" hash="14c61e3e7e4dad65a9f2f62de725c90c"/><file name="Profileorder.php" hash="f3fcf68822e87816b982b9a867503726"/><file name="Profilequote.php" hash="18f8ed2114886bc32911dbe772297f88"/><file name="Profilesubscription.php" hash="67a5d3404363c39b127e23dc9d7b2834"/><dir name="Viewedproduct"><file name="Subprofile.php" hash="b8b9ae88df26d3e18438e17147f77235"/></dir></dir><file name="ErrorQueue.php" hash="c85c754ce859704717913c9f8e9c8588"/><dir name="Mysql4"><dir name="AbandonedCart"><file name="Collection.php" hash="e72b2d443f5c009c29a86cf90ff53421"/></dir><file name="AbandonedCart.php" hash="ec9371ec8c58d550927f31ed1d565d68"/><dir name="Config"><file name="Collection.php" hash="ef43ccb80fd1b415c9a42ee133f80530"/></dir><file name="Config.php" hash="f71bbcca4066b9092700b5f8581cebfd"/><dir name="ErrorQueue"><file name="Collection.php" hash="ca34b0d7540914ae17951722adfa565c"/></dir><file name="ErrorQueue.php" hash="802920824d781f0aec913ef451b8393d"/><dir name="ProfileCache"><file name="Collection.php" hash="593426fd7ca9db0a8ae0e2093a89e2aa"/></dir><file name="ProfileCache.php" hash="a7236486592fe28548a0a30b0bfa6060"/><dir name="Queue"><file name="Collection.php" hash="529d1a0b0f741b5502cee205b6a974e6"/></dir><file name="Queue.php" hash="6290f87f7fbb03155665e237a50176c3"/><dir name="SyncProfile"><file name="Collection.php" hash="33e76bc39467b0093a7a77da44eef966"/></dir><file name="SyncProfile.php" hash="1ac7cbd0ea721dd80b7f35b864f6dc0a"/></dir><file name="Observer.php" hash="13a25b8f0d91f35ea29d5c0277c2857f"/><file name="ProfileCache.php" hash="6f228e87a3d430b654d1337285317083"/><file name="Queue.php" hash="48a6eeafa120dfdb39e5fb0601f15270"/><dir name="QueueEvent"><file name="Abstract.php" hash="666e449669bde675a77ebe27f38edb28"/><file name="Checkout.php" hash="524edeac5cb4023e105660992913f638"/><file name="Customer.php" hash="26f501661ad98905dc6fb6eee61612cd"/><file name="CustomerAdd.php" hash="1754e3cb0ef651fc33b7708daf25dc96"/><file name="CustomerFull.php" hash="36708490c408197fefbd1971ea7c1e37"/><file name="CustomerModify.php" hash="fcaec6161ce09f2548fd55f950d4b374"/><file name="CustomerRemove.php" hash="f65b6eb43b1340758fc190d06feabb7a"/><file name="CustomerUpgrade.php" hash="4af755a621195c0ef0dacf6eab88436c"/><file name="Factory.php" hash="a98bde41d9d3666573d4ded6d4143ed7"/><file name="Item.php" hash="b1271f745ee5b249d036881eddb10021"/><file name="Order.php" hash="bba1f7fe5457a4d0ddacb6656b687076"/><file name="OrderAdd.php" hash="34f2af84d5a1dbe2cded9c04b9ebed8d"/><file name="OrderModify.php" hash="2a8d3aeb200d22455aff15ad41f4b4aa"/><file name="Quote.php" hash="e8fec3ebe3264b0b0ee2da62fb90b2ce"/><file name="QuoteItem.php" hash="fc4bd84522a3d581ad5fde799748b9a2"/><file name="QuoteItemAdd.php" hash="ebcce56922150d281d02c41440bf0222"/><file name="QuoteItemModify.php" hash="113a7edee4d41ac1a6d2a0d4b9128850"/><file name="QuoteItemRemove.php" hash="d6c7fa98a137b7b13837214ae2e4132c"/><file name="QuoteModify.php" hash="9f6cc8bf0cb6679cab8c7121f1fbf7a2"/><file name="StartSync.php" hash="1c6fdb53acaf8e4729978f3bbe18536a"/><file name="Subscription.php" hash="09eba1cffe0b0fe341b2d6a67e444ee0"/><file name="SubscriptionAdd.php" hash="c9609ae3f3bbd03a842ff28175ecc5fe"/><file name="SubscriptionModify.php" hash="d61f5c0d8c15ffe377c0e3cd99db61a0"/><file name="SubscriptionRemove.php" hash="c6d79f1748025edf714d25128f299937"/><file name="View.php" hash="ae2e7e3e7638793779a275dc8a525d09"/><file name="ViewedProductAdd.php" hash="822e17d7fc86e65c3d107e6df1aaecba"/></dir><file name="QueueProcessor.php" hash="03ad8614260b2620209bfc742cea88ba"/><dir name="REST"><file name="Address.php" hash="8f189ab2e708e652adf6b1c3b61ab2b7"/><file name="CartItem.php" hash="4b8ac947cf1bdc9157499f722c881ef2"/><file name="Customer.php" hash="a76b90a87373b79ac87c0323ec4d3aa2"/><file name="Item.php" hash="9c3fc8c093bb11ea1efe386f63eeac8a"/><file name="Order.php" hash="f16add67f9bbc0ab26da435c8c3cf611"/><file name="Product.php" hash="f1a9e7bd1f0ed441db70177dd625d198"/><file name="Quote.php" hash="5fc78607e82f4696f1b8233af38ecc49"/><file name="Subscription.php" hash="dadd94f50786f597e85bb95b3cae6d0c"/></dir><file name="REST.php" hash="ec324f180c9e55ab0881d3393ff5aee3"/><file name="SyncProfile.php" hash="e1a9b4f71fea54b6c16a56b95f00ab4f"/><file name="SyncStatus.php" hash="365118aa88448b87a7b9c3253e5368e6"/></dir><dir name="controllers"><dir name="Adminhtml"><dir name="Marketingsoftware"><file name="AjaxcollectionController.php" hash="b423ee19fc7b4c91baa03e34654de730"/><file name="ExportController.php" hash="2deaf4c0184548b968f07d9e85aa3b16"/><file name="LinkController.php" hash="a7a42801f55dd644a9aa8521219c94cd"/><file name="SettingsController.php" hash="0a925ef070eb1e18d330bd29baf62cc0"/><file name="SyncController.php" hash="cd978f2fa07a5dd164c60ca2b20b1eec"/></dir></dir><file name="ProductController.php" hash="e4143728ae23fc9eddc88340fb67a604"/><file name="UnsubscribeController.php" hash="894d93a2d1abed032a38c0460ceff3a5"/></dir><dir name="cron"><file name="clearCopernicaDatabase.php" hash="8c4241ffab642db16749281d51314564"/><file name="detectAbandonedCarts.php" hash="b5d2243d2ac84a1b51572a087f9ddeb7"/><file name="fullSync.php" hash="5c578a08ad40e9a01794ef69dbc16c01"/><file name="processQueue.php" hash="05ddb7bf941860b01b307a5fdf164925"/></dir><dir name="data"><dir name="marketingsoftware_setup"><file name="data-upgrade-3.4.0-3.4.1.php" hash="efa8cff5993e7fdc9675dd1f4bfa7610"/></dir></dir><dir name="etc"><file name="adminhtml.xml" hash="2d8b3fa480311b7784152bcfaeb47c40"/><file name="config.xml" hash="9c014d99c3fb49a30b564e7ab9a860ef"/></dir><dir name="sql"><dir name="marketingsoftware_setup"><file name="mysql4-install-1.2.0.php" hash="edaa8a4c29da5a0bb4f5c06e92c4a6e5"/><file name="mysql4-upgrade-1.1.5-1.1.6.php" hash="4902356c13309b6415e1a61c3fcc805c"/><file name="mysql4-upgrade-1.1.6-1.1.7.php" hash="1bfa357933494cbded4c32f4d1d0a45c"/><file name="mysql4-upgrade-1.1.7-1.1.8.php" hash="860e3ce11771bd8e6f7c755f2afa8805"/><file name="mysql4-upgrade-1.1.8-1.2.0.php" hash="42db3d71333deb7430cdbbafb56e80c1"/><file name="mysql4-upgrade-2.0.1-2.0.2.php" hash="5374760e8bee6edb033475cd0cf8fd0a"/><file name="mysql4-upgrade-2.1.0.1-2.2.0.php" hash="83135bd0c7a7d9f15496654b0374d2bb"/><file name="mysql4-upgrade-2.2.0.1-2.3.0.php" hash="c7e1e82357c9e12a553e8adead231a48"/><file name="mysql4-upgrade-2.3.0-2.3.1.php" hash="8b50dd866977ee4f356ded37a2d2b3c3"/><file name="mysql4-upgrade-2.3.1-2.3.2.php" hash="b575862a68ab89511759c193e16200ba"/><file name="mysql4-upgrade-2.3.2-2.3.3.php" hash="d3c0e021d677d4d76d55760ae323e6af"/><file name="mysql4-upgrade-2.3.3-2.3.4.php" hash="5594882bfddb009d097ce00080df5a05"/><file name="mysql4-upgrade-2.3.4-2.3.5.php" hash="ac7c8e8d2b8d9d1c6a6398815aa4218b"/><file name="mysql4-upgrade-2.3.8-2.4.0.php" hash="a1ec382dba74745c7410b4243507e86f"/><file name="mysql4-upgrade-2.4.0-2.4.1.php" hash="589e40f7456b2f86558808e2c0b41ee9"/><file name="mysql4-upgrade-2.4.1-2.4.2.php" hash="87e94e7504bed6bae3065b78dc6c70bf"/><file name="mysql4-upgrade-2.4.2-2.4.3.php" hash="4a3de330af01de1deb618a0adab4ea8f"/><file name="mysql4-upgrade-2.4.3-2.4.4.php" hash="d24bbe6f6cc4b36fccee6461eb9f3d19"/></dir></dir></dir></dir></target><target name="mageetc"><dir name="modules"><file name="Copernica_MarketingSoftware.xml" hash="1d3e901ef934e74a837d779f815882d0"/></dir></target><target name="magedesign"><dir name="adminhtml"><dir name="default"><dir name="default"><dir name="template"><dir name="marketingsoftware"><file name="export.phtml" hash="8b32b8e0098fa4d469fff888cbc269ea"/><file name="link.phtml" hash="6a3bbaddb22ca3faba0ae04669a52725"/><file name="settings.phtml" hash="771ab56739000d1a2376b7ca418037ec"/><file name="sync.phtml" hash="ec3749674fab02f124bbc55d618a28e8"/></dir></dir></dir></dir></dir></target><target name="mageweb"><dir name="js"><dir name="copernica"><dir name="marketingsoftware"><file name="link.js" hash="26d1c1d662c24abf913f4ed31b8b3871"/></dir></dir></dir></target></contents>
19
  <compatible/>
20
  <dependencies><required><php><min>5.2.0</min><max>5.4.99</max></php><extension><name>PDO</name><min></min><max></max></extension></required></dependencies>
21
  </package>
1
  <?xml version="1.0"?>
2
  <package>
3
  <name>Copernica_MarketingSoftware</name>
4
+ <version>3.1.0</version>
5
  <stability>stable</stability>
6
  <license uri="http://opensource.org/licenses/osl-3.0.php">Open Software License (OSL 3.0)</license>
7
  <channel>community</channel>
10
  <description>When you install the integration, all contact, order and shop cart data from Magento will be automatically synchronized and stored in Copernica. By synchronizing data between your Magento webshop and Copernica, you are able to set up targeted email campaigns. For example you can consider emailing customers with abandoned shopcarts automatically every week. Your contacts will only receive information relevant to them, which will make them come back to your shop time and again. The available integration is also compatible with the Magento Enterprise edition.&#xD;
11
  &#xD;
12
  Please note that this extension does not work with PHP 5.4.X. and should be run with the Magento system requirements in mind.</description>
13
+ <notes>- Using the REST API.&#xD;
14
+ - Synchronise wishlists of customers.</notes>
15
  <authors><author><name>Cream</name><user>creaminternet</user><email>info@cream.nl</email></author></authors>
16
+ <date>2015-02-10</date>
17
+ <time>14:02:50</time>
18
+ <contents><target name="magecommunity"><dir name="Copernica"><dir name="MarketingSoftware"><dir name="Block"><dir name="Adminhtml"><dir name="Marketingsoftware"><file name="Accesstoken.php" hash="c2ef303045eccb665b63ff820a8f1d80"/><file name="Export.php" hash="a8a1203c12602345f9b3dd249ec77216"/><file name="Link.php" hash="4e91f2f43ab597e7a14846a9e19c4761"/><file name="Settings.php" hash="f18848ba6a75106d8bf6863c0a91dc80"/><file name="Sync.php" hash="889116b5fb52ee9f3276bd5990420ce9"/></dir></dir></dir><dir name="Controller"><file name="Action.php" hash="d765d30eb808c63d26678b469a1d88fc"/></dir><file name="Exception.php" hash="41190a584df0a30e2124dc115006be86"/><dir name="Helper"><dir name="Api"><file name="Abstract.php" hash="4d784cedc22ec3b1f3f55c80fbc7e5d0"/><file name="Builder.php" hash="6974d30a993d279214537346de3f9373"/><file name="Validator.php" hash="a80fb361447b0e7a06adaa62dd556090"/></dir><file name="Api.php" hash="71e4c4c9d1a4dcf62d03170a7d4dd66a"/><file name="Config.php" hash="5e0f35cb76766552aeac08c08aaa7dc0"/><file name="Data.php" hash="1475fbd84cc4b2167288783425f168b2"/><file name="DataWriter.php" hash="562cd6afe278dd8200a025d74a7cd1e1"/><file name="Profile.php" hash="515a752035737e19eb3d1d15feeaf66b"/><dir name="Rest"><file name="Request.php" hash="a6eda9272e2597b7112b32f5675c4bcc"/></dir></dir><dir name="Model"><dir name="Abandoned"><file name="Cart.php" hash="7aee6f9ad3e2eb22748eb60f0bc8442d"/><dir name="Carts"><file name="Processor.php" hash="e9f3a758ca046140cf4aba2787cf9fe1"/></dir></dir><dir name="Abstraction"><file name="Address.php" hash="010a2810e5bd7f2fabd307fd8404c5e5"/><file name="Attributes.php" hash="fa00ed08adb526b43129bd1bfe5256e6"/><file name="Customer.php" hash="ee826e5a9cb34f936ca19fbd7f19c0e5"/><file name="Name.php" hash="cf4ace0edabae7e3fffff9958fa499f7"/><dir name="Order"><dir name="Item"><file name="Options.php" hash="e5d997cae5d85bfddd5038c190ef7110"/></dir><file name="Item.php" hash="402ddcc1a00ac820e8ce20af0b176a51"/></dir><file name="Order.php" hash="af71f4ba15244a6adeafb594992f424f"/><file name="Price.php" hash="94e99b6b6ad30e01fbf35d624c52fe3f"/><dir name="Product"><file name="Viewed.php" hash="4ff032b199f94dfff12beaf449636736"/></dir><file name="Product.php" hash="0fefe0408623cfa9edabbd6b13a28554"/><dir name="Quote"><dir name="Item"><file name="Options.php" hash="45e172a730317d00572ba551c88b2aef"/></dir><file name="Item.php" hash="9393fd4037d8c2d98d79ce5e5be81790"/></dir><file name="Quote.php" hash="465b0ebdd5b3f3719fb5d49a9f1d58ed"/><file name="Storeview.php" hash="802580b3fe57fcf360c4096a986b194e"/><file name="Subscription.php" hash="209a4158cd12a8ab39ee33321cc41420"/><dir name="Wishlist"><dir name="Item"><file name="Options.php" hash="23dfff0dcddd939af08f50678492feec"/></dir><file name="Item.php" hash="89140d77b7a7897ecb1655dc36380c7e"/></dir><file name="Wishlist.php" hash="f9b59684cb43ff6a382feba59f6009e0"/></dir><file name="Config.php" hash="5f1696430061df5fe376ef2ba1fe4590"/><dir name="Copernica"><file name="Abstract.php" hash="e43e91e8d3a7c31cca75b0a11fe396d4"/><dir name="Address"><file name="Subprofile.php" hash="8da47fe822c3c67998d082afb798eb4b"/></dir><dir name="Entity"><file name="Address.php" hash="c984744bed05bb9a01f5b5a4372fd33b"/><file name="Customer.php" hash="312ca9ce41757f14545cd98702d8e843"/><dir name="Order"><file name="Item.php" hash="5b842be89855d2476ba933eea891e522"/></dir><file name="Order.php" hash="4220f3465a6338196485e1eef1dd58d4"/><file name="Product.php" hash="631fd0ed9e250ef627754f8c12291f4c"/><dir name="Quote"><file name="Item.php" hash="108ccbec6107e2f1db179d5a132f31e4"/></dir><file name="Quote.php" hash="e84c28cd718b6184c9c393cee7fb65ba"/><file name="Subscription.php" hash="768f31949925692a0516ee13cb47830d"/><dir name="Wishlist"><file name="Item.php" hash="d64fb7990cea2c3fa11f0a58c4d8420d"/></dir><file name="Wishlist.php" hash="fe66357cc0253d042b64efc5659fa422"/></dir><file name="Entity.php" hash="f55e044f00aa21188a7957a45361074d"/><dir name="Order"><file name="Subprofile.php" hash="4799f8ab8b2c47298b0c472483699a29"/></dir><dir name="Orderitem"><file name="Subprofile.php" hash="b4f238d1e68bd86252efb8dfc8551fff"/></dir><dir name="Product"><dir name="Viewed"><file name="Subprofile.php" hash="ecdaf08d3ed7c8eb9b70143c179f871d"/></dir></dir><dir name="Profile"><file name="Customer.php" hash="f52f7a2e228a6da8c000da1da26e6abd"/><file name="Order.php" hash="26c091f3cb789d5785371950084252b7"/><file name="Quote.php" hash="4b92449c77cce625640ef92e4d8e0204"/><file name="Subscription.php" hash="cda9cea09add3504d085c046ac2b4b72"/></dir><file name="Profile.php" hash="dd4f415edd5cd5f6706461e432096d96"/><dir name="Quote"><dir name="Item"><file name="Subprofile.php" hash="fba544a7bbb1738a1f7f3c33929ce860"/></dir></dir><dir name="Wishlist"><dir name="Item"><file name="Subprofile.php" hash="d6a2930a179506d66fcac3c543749756"/></dir></dir></dir><dir name="Error"><file name="Queue.php" hash="6108b654f56b5fc107b5ceeac1a216dc"/></dir><dir name="Mysql4"><dir name="Abandoned"><dir name="Cart"><file name="Collection.php" hash="ed5067e74f8fdae93da9ff605ca39a0d"/></dir><file name="Cart.php" hash="5672223fdb1e15943c06f10699918013"/></dir><dir name="Config"><file name="Collection.php" hash="ef43ccb80fd1b415c9a42ee133f80530"/></dir><file name="Config.php" hash="f71bbcca4066b9092700b5f8581cebfd"/><dir name="Error"><dir name="Queue"><file name="Collection.php" hash="f47f0a02784af8912a98e7e340863bf4"/></dir><file name="Queue.php" hash="4ee4e8a9ca9d5263f2fb6174bc71e45a"/></dir><dir name="Profile"><dir name="Cache"><file name="Collection.php" hash="cf7110d5cd31ead058bffa18655ad128"/></dir><file name="Cache.php" hash="58ce66a514e79724fcf702bddc1f46e5"/></dir><dir name="Queue"><dir name="Item"><file name="Collection.php" hash="991ef060e1edffa07e2d429803909c69"/></dir><file name="Item.php" hash="e995226766fbf7bf90dbcd51f26a9e35"/></dir><dir name="Sync"><dir name="Profile"><file name="Collection.php" hash="06bea0ec8d3356f0af28b501c8cc93de"/></dir><file name="Profile.php" hash="986992c4cef6c3086e31a169f756e640"/></dir></dir><file name="Observer.php" hash="54e4cdf6b0046fd21aae1beb00a71295"/><dir name="Profile"><file name="Cache.php" hash="e84ec44bf9c881aa30b9d567332c64ee"/></dir><dir name="Queue"><dir name="Event"><file name="Abstract.php" hash="19a286122e430fdb250e7748e4dbad48"/><file name="Checkout.php" hash="0b947290b10a6353e714d14e42e4c53e"/><file name="Customer.php" hash="205aa0f2513cc235e442b5c9360a7092"/><file name="Factory.php" hash="01208e36c71c333b0bd8cfcbc2391dc8"/><file name="Item.php" hash="b2d4f78ea5b5dd0331e8f4df17b02f1f"/><file name="Order.php" hash="fbc618e7d5167ec200e23fb6a7e6a153"/><file name="Quote.php" hash="99dc65b159754bb707ab8f74b1c9a80d"/><file name="Startsync.php" hash="1b7ac80bc0a33d861285508cc7bb8df9"/><file name="Subscription.php" hash="b0b61990112cf3584613b4c246fe86c1"/><file name="View.php" hash="728ad8b1149688a1dda670a33c8834ec"/><dir name="Wishlist"><file name="Item.php" hash="377fd52a28f562ea3c3245f2d308fc11"/></dir></dir><file name="Item.php" hash="9d3cfea725b9b6ce65921dbcb88b1923"/><file name="Processor.php" hash="eea9690ac1b9295ed9993e9b8b603472"/></dir><dir name="Rest"><file name="Address.php" hash="ee240d63e936a0bddcd2fffbbb07c2b3"/><file name="Customer.php" hash="3253c5e8621eac5db3c1271142f168af"/><dir name="Order"><file name="Item.php" hash="e703287631b20054601450ce45e059f9"/></dir><file name="Order.php" hash="e20cdd811d8498bc81427b4c5f6253be"/><file name="Product.php" hash="0bfbca96b41c56791ca9dc9da6eec7a0"/><dir name="Quote"><file name="Item.php" hash="57f232821402d9f586e53b25abf1baad"/></dir><file name="Quote.php" hash="0f6c57b2623a3faedf193050a9b0d57b"/><file name="Subscription.php" hash="f0f72235fa6a7c9293864137e48417b9"/><dir name="Wishlist"><file name="Item.php" hash="b410dc4c2e24c4b18d4684ca71c637ea"/></dir><file name="Wishlist.php" hash="628bef03562a326340a79054f1738236"/></dir><file name="Rest.php" hash="cedae0c825bffd872b0a969246eec4b5"/><dir name="Sync"><file name="Profile.php" hash="650bb9567bafa3691f7500af70b86cd4"/><file name="Status.php" hash="ca86c5a9baef97500a80191c8261a27c"/></dir></dir><dir name="controllers"><dir name="Adminhtml"><dir name="Marketingsoftware"><file name="AccessTokenController.php" hash="27a9f8dc6ed86db48936a1766d8574cb"/><file name="AjaxcollectionController.php" hash="c95966f264080bb6610b61e559c23771"/><file name="ExportController.php" hash="9109f406099267c5f7608a7469e780d3"/><file name="LinkController.php" hash="9996e592c743e5bfd32a01c0ea6fc303"/><file name="SettingsController.php" hash="4150a3ba8b7c9089ed187b6e03144074"/><file name="SyncController.php" hash="6df5bf36e2bfb8c81c6a7af6073b76ae"/></dir></dir><file name="ProductController.php" hash="befc0cb99d18bbcfb76f0c9d06512790"/><file name="UnsubscribeController.php" hash="63bbb626ac84d5ff12295b84f15b2a8d"/></dir><dir name="cron"><file name="clearCopernicaDatabase.php" hash="046d44507e4a09daa6779cf625426773"/><file name="detectAbandonedCarts.php" hash="9c154baf0a72d63301364b2e09c7c69c"/><file name="fullSync.php" hash="e37c4fb0d65a3e0636c2aef2587cedcc"/><file name="processQueue.php" hash="94853832b1466c1e03cf2ad6b593a697"/></dir><dir name="data"><dir name="marketingsoftware_setup"><file name="data-upgrade-3.4.0-3.4.1.php" hash="decbd8592f0dbee8645aa96b0abd02ae"/></dir></dir><dir name="etc"><file name="adminhtml.xml" hash="66a4f7ccc4b58f9b2172e5be85fd5726"/><file name="config.xml" hash="2c207414500144824e714102bef54ea9"/></dir><dir name="sql"><dir name="marketingsoftware_setup"><file name="mysql4-install-1.2.0.php" hash="edaa8a4c29da5a0bb4f5c06e92c4a6e5"/><file name="mysql4-upgrade-1.1.5-1.1.6.php" hash="4902356c13309b6415e1a61c3fcc805c"/><file name="mysql4-upgrade-1.1.6-1.1.7.php" hash="1bfa357933494cbded4c32f4d1d0a45c"/><file name="mysql4-upgrade-1.1.7-1.1.8.php" hash="860e3ce11771bd8e6f7c755f2afa8805"/><file name="mysql4-upgrade-1.1.8-1.2.0.php" hash="42db3d71333deb7430cdbbafb56e80c1"/><file name="mysql4-upgrade-2.0.1-2.0.2.php" hash="5374760e8bee6edb033475cd0cf8fd0a"/><file name="mysql4-upgrade-2.1.0.1-2.2.0.php" hash="83135bd0c7a7d9f15496654b0374d2bb"/><file name="mysql4-upgrade-2.2.0.1-2.3.0.php" hash="c7e1e82357c9e12a553e8adead231a48"/><file name="mysql4-upgrade-2.3.0-2.3.1.php" hash="8b50dd866977ee4f356ded37a2d2b3c3"/><file name="mysql4-upgrade-2.3.1-2.3.2.php" hash="b575862a68ab89511759c193e16200ba"/><file name="mysql4-upgrade-2.3.2-2.3.3.php" hash="0f7dcc230f3195c3fb8e76a3d913fc8c"/><file name="mysql4-upgrade-2.3.3-2.3.4.php" hash="5594882bfddb009d097ce00080df5a05"/><file name="mysql4-upgrade-2.3.4-2.3.5.php" hash="ac7c8e8d2b8d9d1c6a6398815aa4218b"/><file name="mysql4-upgrade-2.3.8-2.4.0.php" hash="1b411e0a96c4d8b369fcc38212412937"/><file name="mysql4-upgrade-2.4.0-2.4.1.php" hash="589e40f7456b2f86558808e2c0b41ee9"/><file name="mysql4-upgrade-2.4.1-2.4.2.php" hash="cc691f4a1ef9527be57b403735036820"/><file name="mysql4-upgrade-2.4.2-2.4.3.php" hash="b5502e1d3be8350b885e34ef465f24d0"/><file name="mysql4-upgrade-2.4.3-2.4.4.php" hash="704dcba8d045de9679764d7c9b651168"/></dir></dir></dir></dir></target><target name="mageetc"><dir name="modules"><file name="Copernica_MarketingSoftware.xml" hash="1d3e901ef934e74a837d779f815882d0"/></dir></target><target name="magedesign"><dir name="adminhtml"><dir name="default"><dir name="default"><dir name="template"><dir name="marketingsoftware"><file name="accessToken.phtml" hash="ea9628d6909cc135fc7b7f3dc045c721"/><file name="export.phtml" hash="ccc2835399f0a47a41d1999aadda2731"/><file name="link.phtml" hash="2ae8467a15d17a6da1038fd552dddcfa"/><file name="settings.phtml" hash="62dc3775ae153b7d351933e8eac70c15"/><file name="sync.phtml" hash="ec3749674fab02f124bbc55d618a28e8"/></dir></dir></dir></dir></dir></target><target name="mageweb"><dir name="js"><dir name="copernica"><dir name="marketingsoftware"><file name="link.js" hash="0b2c0e242bbf3b785065b3c85b9aa235"/></dir></dir></dir></target></contents>
19
  <compatible/>
20
  <dependencies><required><php><min>5.2.0</min><max>5.4.99</max></php><extension><name>PDO</name><min></min><max></max></extension></required></dependencies>
21
  </package>