Copernica_MarketingSoftware - Version 3.0.0

Version Notes

New features:
- Uses the new Copernica REST API
- Improved performance

Download this release

Release Info

Developer Cream
Extension Copernica_MarketingSoftware
Version 3.0.0
Comparing to
See all releases


Code changes from version 2.2.0 to 3.0.0

Files changed (136) hide show
  1. app/code/community/Copernica/MarketingSoftware/Block/Adminhtml/Marketingsoftware/Export.php +12 -4
  2. app/code/community/Copernica/MarketingSoftware/Block/Adminhtml/Marketingsoftware/Link.php +148 -12
  3. app/code/community/Copernica/MarketingSoftware/Block/Adminhtml/Marketingsoftware/Settings.php +29 -13
  4. app/code/community/Copernica/MarketingSoftware/Block/Adminhtml/Marketingsoftware/Sync.php +120 -0
  5. app/code/community/Copernica/MarketingSoftware/Controller/Base.php +178 -0
  6. app/code/community/Copernica/MarketingSoftware/Exception.php +80 -0
  7. app/code/community/Copernica/MarketingSoftware/Helper/Api.php +357 -1208
  8. app/code/community/Copernica/MarketingSoftware/Helper/ApiBase.php +452 -0
  9. app/code/community/Copernica/MarketingSoftware/Helper/ApiBuilder.php +264 -0
  10. app/code/community/Copernica/MarketingSoftware/Helper/ApiValidator.php +383 -0
  11. app/code/community/Copernica/MarketingSoftware/Helper/Config.php +921 -793
  12. app/code/community/Copernica/MarketingSoftware/Helper/Data.php +436 -400
  13. app/code/community/Copernica/MarketingSoftware/Helper/DataWriter.php +255 -0
  14. app/code/community/Copernica/MarketingSoftware/Helper/Profile.php +246 -0
  15. app/code/community/Copernica/MarketingSoftware/Helper/RESTRequest.php +370 -0
  16. app/code/community/Copernica/MarketingSoftware/Model/{Marketingsoftware.php → AbandonedCart.php} +62 -58
  17. app/code/community/Copernica/MarketingSoftware/Model/AbandonedCartsProcessor.php +154 -0
  18. app/code/community/Copernica/MarketingSoftware/Model/Abstraction/Address.php +301 -301
  19. app/code/community/Copernica/MarketingSoftware/Model/Abstraction/Attributes.php +127 -127
  20. app/code/community/Copernica/MarketingSoftware/Model/Abstraction/Customer.php +303 -280
  21. app/code/community/Copernica/MarketingSoftware/Model/Abstraction/Name.php +123 -123
  22. app/code/community/Copernica/MarketingSoftware/Model/Abstraction/Order.php +333 -347
  23. app/code/community/Copernica/MarketingSoftware/Model/Abstraction/Order/Item.php +180 -180
  24. app/code/community/Copernica/MarketingSoftware/Model/Abstraction/Order/Item/Options.php +151 -151
  25. app/code/community/Copernica/MarketingSoftware/Model/Abstraction/Price.php +243 -239
  26. app/code/community/Copernica/MarketingSoftware/Model/Abstraction/Product.php +340 -414
  27. app/code/community/Copernica/MarketingSoftware/Model/Abstraction/Quote.php +347 -320
  28. app/code/community/Copernica/MarketingSoftware/Model/Abstraction/Quote/Item.php +181 -181
  29. app/code/community/Copernica/MarketingSoftware/Model/Abstraction/Quote/Item/Options.php +155 -155
  30. app/code/community/Copernica/MarketingSoftware/Model/Abstraction/Storeview.php +173 -173
  31. app/code/community/Copernica/MarketingSoftware/Model/Abstraction/Subscription.php +160 -160
  32. app/code/community/Copernica/MarketingSoftware/Model/Abstraction/Viewedproduct.php +426 -426
  33. app/code/community/Copernica/MarketingSoftware/Model/AsyncPomSoapClient.php +0 -328
  34. app/code/community/Copernica/MarketingSoftware/Model/Config.php +61 -56
  35. app/code/community/Copernica/MarketingSoftware/Model/Copernica/Abstract.php +1 -0
  36. app/code/community/Copernica/MarketingSoftware/Model/Copernica/Cartitem/Subprofile.php +148 -135
  37. app/code/community/Copernica/MarketingSoftware/Model/Copernica/Entity.php +91 -0
  38. app/code/community/Copernica/MarketingSoftware/Model/Copernica/Entity/Address.php +215 -0
  39. app/code/community/Copernica/MarketingSoftware/Model/Copernica/Entity/CartItem.php +87 -0
  40. app/code/community/Copernica/MarketingSoftware/Model/Copernica/Entity/Customer.php +299 -0
  41. app/code/community/Copernica/MarketingSoftware/Model/Copernica/Entity/Item.php +218 -0
  42. app/code/community/Copernica/MarketingSoftware/Model/Copernica/Entity/Order.php +333 -0
  43. app/code/community/Copernica/MarketingSoftware/Model/Copernica/Entity/Product.php +257 -0
  44. app/code/community/Copernica/MarketingSoftware/Model/Copernica/Entity/Quote.php +79 -0
  45. app/code/community/Copernica/MarketingSoftware/Model/Copernica/Entity/Subscription.php +142 -0
  46. app/code/community/Copernica/MarketingSoftware/Model/Copernica/Order/Subprofile.php +5 -2
  47. app/code/community/Copernica/MarketingSoftware/Model/Copernica/Orderitem/Subprofile.php +132 -118
  48. app/code/community/Copernica/MarketingSoftware/Model/Copernica/Profile.php +12 -0
  49. app/code/community/Copernica/MarketingSoftware/Model/Copernica/Profilecustomer.php +122 -93
  50. app/code/community/Copernica/MarketingSoftware/Model/Copernica/Profileorder.php +38 -2
  51. app/code/community/Copernica/MarketingSoftware/Model/Copernica/Profilequote.php +49 -7
  52. app/code/community/Copernica/MarketingSoftware/Model/Copernica/Profilesubscription.php +26 -1
  53. app/code/community/Copernica/MarketingSoftware/Model/Copernica/Viewedproduct/Subprofile.php +112 -112
  54. app/code/community/Copernica/MarketingSoftware/Model/Error.php +0 -140
  55. app/code/community/Copernica/MarketingSoftware/Model/ErrorQueue.php +118 -0
  56. app/code/community/Copernica/MarketingSoftware/Model/Mysql4/AbandonedCart.php +39 -0
  57. app/code/community/Copernica/MarketingSoftware/Model/Mysql4/AbandonedCart/Collection.php +12 -0
  58. app/code/community/Copernica/MarketingSoftware/Model/Mysql4/Config.php +32 -32
  59. app/code/community/Copernica/MarketingSoftware/Model/Mysql4/Config/Collection.php +32 -32
  60. app/code/community/Copernica/MarketingSoftware/Model/Mysql4/ErrorQueue.php +36 -0
  61. app/code/community/Copernica/MarketingSoftware/Model/Mysql4/ErrorQueue/Collection.php +36 -0
  62. app/code/community/Copernica/MarketingSoftware/Model/Mysql4/ProfileCache.php +39 -0
  63. app/code/community/Copernica/MarketingSoftware/Model/Mysql4/ProfileCache/Collection.php +33 -0
  64. app/code/community/Copernica/MarketingSoftware/Model/Mysql4/Queue.php +30 -0
  65. app/code/community/Copernica/MarketingSoftware/Model/Mysql4/Queue/Collection.php +66 -63
  66. app/code/community/Copernica/MarketingSoftware/Model/Mysql4/SyncProfile.php +36 -0
  67. app/code/community/Copernica/MarketingSoftware/Model/Mysql4/SyncProfile/Collection.php +33 -0
  68. app/code/community/Copernica/MarketingSoftware/Model/Observer.php +442 -404
  69. app/code/community/Copernica/MarketingSoftware/Model/PomSoapClient.php +0 -309
  70. app/code/community/Copernica/MarketingSoftware/Model/ProfileCache.php +80 -0
  71. app/code/community/Copernica/MarketingSoftware/Model/Queue.php +253 -94
  72. app/code/community/Copernica/MarketingSoftware/Model/QueueEvent/Abstract.php +118 -62
  73. app/code/community/Copernica/MarketingSoftware/Model/QueueEvent/Checkout.php +96 -0
  74. app/code/community/Copernica/MarketingSoftware/Model/QueueEvent/Customer.php +161 -0
  75. app/code/community/Copernica/MarketingSoftware/Model/QueueEvent/CustomerAdd.php +51 -51
  76. app/code/community/Copernica/MarketingSoftware/Model/QueueEvent/CustomerFull.php +241 -121
  77. app/code/community/Copernica/MarketingSoftware/Model/QueueEvent/CustomerModify.php +125 -51
  78. app/code/community/Copernica/MarketingSoftware/Model/QueueEvent/CustomerRemove.php +58 -51
  79. app/code/community/Copernica/MarketingSoftware/Model/QueueEvent/CustomerUpgrade.php +89 -0
  80. app/code/community/Copernica/MarketingSoftware/Model/QueueEvent/Factory.php +94 -87
  81. app/code/community/Copernica/MarketingSoftware/Model/QueueEvent/Item.php +178 -0
  82. app/code/community/Copernica/MarketingSoftware/Model/QueueEvent/Order.php +167 -0
  83. app/code/community/Copernica/MarketingSoftware/Model/QueueEvent/OrderAdd.php +34 -34
  84. app/code/community/Copernica/MarketingSoftware/Model/QueueEvent/OrderModify.php +170 -106
  85. app/code/community/Copernica/MarketingSoftware/Model/QueueEvent/Quote.php +103 -0
  86. app/code/community/Copernica/MarketingSoftware/Model/QueueEvent/QuoteItem.php +123 -77
  87. app/code/community/Copernica/MarketingSoftware/Model/QueueEvent/QuoteItemAdd.php +39 -39
  88. app/code/community/Copernica/MarketingSoftware/Model/QueueEvent/QuoteItemModify.php +40 -40
  89. app/code/community/Copernica/MarketingSoftware/Model/QueueEvent/QuoteItemRemove.php +40 -40
  90. app/code/community/Copernica/MarketingSoftware/Model/QueueEvent/QuoteModify.php +131 -81
  91. app/code/community/Copernica/MarketingSoftware/Model/QueueEvent/StartSync.php +289 -267
  92. app/code/community/Copernica/MarketingSoftware/Model/QueueEvent/Subscription.php +102 -0
  93. app/code/community/Copernica/MarketingSoftware/Model/QueueEvent/SubscriptionAdd.php +34 -34
  94. app/code/community/Copernica/MarketingSoftware/Model/QueueEvent/SubscriptionModify.php +72 -80
  95. app/code/community/Copernica/MarketingSoftware/Model/QueueEvent/SubscriptionRemove.php +34 -34
  96. app/code/community/Copernica/MarketingSoftware/Model/QueueEvent/View.php +51 -0
  97. app/code/community/Copernica/MarketingSoftware/Model/QueueEvent/ViewedProductAdd.php +71 -67
  98. app/code/community/Copernica/MarketingSoftware/Model/QueueProcessor.php +286 -0
  99. app/code/community/Copernica/MarketingSoftware/Model/REST.php +144 -0
  100. app/code/community/Copernica/MarketingSoftware/Model/REST/Address.php +111 -0
  101. app/code/community/Copernica/MarketingSoftware/Model/REST/CartItem.php +74 -0
  102. app/code/community/Copernica/MarketingSoftware/Model/REST/Customer.php +94 -0
  103. app/code/community/Copernica/MarketingSoftware/Model/REST/Item.php +129 -0
  104. app/code/community/Copernica/MarketingSoftware/Model/REST/Order.php +166 -0
  105. app/code/community/Copernica/MarketingSoftware/Model/REST/Product.php +85 -0
  106. app/code/community/Copernica/MarketingSoftware/Model/REST/Quote.php +74 -0
  107. app/code/community/Copernica/MarketingSoftware/Model/REST/Subscription.php +115 -0
  108. app/code/community/Copernica/MarketingSoftware/Model/SyncProfile.php +189 -0
  109. app/code/community/Copernica/MarketingSoftware/Model/SyncStatus.php +140 -0
  110. app/code/community/Copernica/MarketingSoftware/controllers/Adminhtml/Marketingsoftware/AjaxcollectionController.php +324 -0
  111. app/code/community/Copernica/MarketingSoftware/controllers/Adminhtml/Marketingsoftware/AjaxcollectionfieldController.php +167 -0
  112. app/code/community/Copernica/MarketingSoftware/controllers/Adminhtml/Marketingsoftware/AjaxdatabaseController.php +271 -0
  113. app/code/community/Copernica/MarketingSoftware/controllers/Adminhtml/Marketingsoftware/AjaxdatabasefieldController.php +192 -0
  114. app/code/community/Copernica/MarketingSoftware/controllers/Adminhtml/Marketingsoftware/ExportController.php +170 -140
  115. app/code/community/Copernica/MarketingSoftware/controllers/Adminhtml/Marketingsoftware/LinkController.php +302 -202
  116. app/code/community/Copernica/MarketingSoftware/controllers/Adminhtml/Marketingsoftware/SettingsController.php +215 -149
  117. app/code/community/Copernica/MarketingSoftware/controllers/Adminhtml/Marketingsoftware/SyncController.php +198 -0
  118. app/code/community/Copernica/MarketingSoftware/controllers/ProductController.php +274 -274
  119. app/code/community/Copernica/MarketingSoftware/controllers/UnsubscribeController.php +71 -70
  120. app/code/community/Copernica/MarketingSoftware/cron/clearCopernicaDatabase.php +62 -0
  121. app/code/community/Copernica/MarketingSoftware/cron/detectAbandonedCarts.php +59 -0
  122. app/code/community/Copernica/MarketingSoftware/cron/fullSync.php +70 -0
  123. app/code/community/Copernica/MarketingSoftware/cron/processQueue.php +152 -0
  124. app/code/community/Copernica/MarketingSoftware/data/marketingsoftware_setup/data-upgrade-3.4.0-3.4.1.php +66 -0
  125. app/code/community/Copernica/MarketingSoftware/etc/adminhtml.xml +54 -47
  126. app/code/community/Copernica/MarketingSoftware/etc/config.xml +193 -172
  127. app/code/community/Copernica/MarketingSoftware/sql/marketingsoftware_setup/mysql4-upgrade-2.1.0.1-2.2.0.php +35 -35
  128. app/code/community/Copernica/MarketingSoftware/sql/marketingsoftware_setup/mysql4-upgrade-2.2.0.1-2.3.0.php +36 -0
  129. app/code/community/Copernica/MarketingSoftware/sql/marketingsoftware_setup/mysql4-upgrade-2.3.0-2.3.1.php +48 -0
  130. app/code/community/Copernica/MarketingSoftware/sql/marketingsoftware_setup/mysql4-upgrade-2.3.1-2.3.2.php +55 -0
  131. app/code/community/Copernica/MarketingSoftware/sql/marketingsoftware_setup/mysql4-upgrade-2.3.2-2.3.3.php +82 -0
  132. app/code/community/Copernica/MarketingSoftware/sql/marketingsoftware_setup/mysql4-upgrade-2.3.3-2.3.4.php +65 -0
  133. app/code/community/Copernica/MarketingSoftware/sql/marketingsoftware_setup/mysql4-upgrade-2.3.4-2.3.5.php +53 -0
  134. app/code/community/Copernica/MarketingSoftware/sql/marketingsoftware_setup/mysql4-upgrade-2.3.8-2.4.0.php +94 -0
  135. app/code/community/Copernica/MarketingSoftware/sql/marketingsoftware_setup/mysql4-upgrade-2.4.0-2.4.1.php +78 -0
  136. app/code/community/Copernica/MarketingSoftware/sql/marketingsoftware_setup/mysql4-upgrade-2.4.1-2.4.2.php +62 -0
app/code/community/Copernica/MarketingSoftware/Block/Adminhtml/Marketingsoftware/Export.php CHANGED
@@ -30,10 +30,9 @@
30
  */
31
  class Copernica_MarketingSoftware_Block_Adminhtml_Marketingsoftware_Export extends Mage_Core_Block_Template
32
  {
33
- /**
34
- * Constructor
35
- *
36
- */
37
  public function __construct()
38
  {
39
  parent::__construct();
@@ -59,4 +58,13 @@ class Copernica_MarketingSoftware_Block_Adminhtml_Marketingsoftware_Export exten
59
  {
60
  return $this->getUrl('*/*/get', array('_secure' => true));
61
  }
 
 
 
 
 
 
 
 
 
62
  }
30
  */
31
  class Copernica_MarketingSoftware_Block_Adminhtml_Marketingsoftware_Export extends Mage_Core_Block_Template
32
  {
33
+ /**
34
+ * Constructor
35
+ */
 
36
  public function __construct()
37
  {
38
  parent::__construct();
58
  {
59
  return $this->getUrl('*/*/get', array('_secure' => true));
60
  }
61
+
62
+ /**
63
+ * Get url that will try to start data sync.
64
+ * @return string
65
+ */
66
+ public function getDataUrl()
67
+ {
68
+ return $this->getUrl('*/*/dataSync', array('_secure' => true));
69
+ }
70
  }
app/code/community/Copernica/MarketingSoftware/Block/Adminhtml/Marketingsoftware/Link.php CHANGED
@@ -30,10 +30,10 @@
30
  */
31
  class Copernica_MarketingSoftware_Block_Adminhtml_Marketingsoftware_Link extends Mage_Core_Block_Template
32
  {
33
- /**
34
- * Constructor
35
- *
36
- */
37
  public function __construct()
38
  {
39
  parent::__construct();
@@ -41,22 +41,158 @@ class Copernica_MarketingSoftware_Block_Adminhtml_Marketingsoftware_Link extends
41
  }
42
 
43
  /**
44
- * Returns the ajax URL.
45
  *
46
  * @return string
47
  */
48
- public function getAjaxUrl()
49
  {
50
- return $this->getUrl('*/*/checkajax', array('_secure' => true));
51
  }
52
 
53
  /**
54
- * Returns the post URL.
55
- *
56
- * @return string
57
  */
58
- public function getPostUrl()
59
  {
60
- return $this->getUrl('*/*/saveProfilesAndCollections', array('_secure' => true));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
  }
62
  }
30
  */
31
  class Copernica_MarketingSoftware_Block_Adminhtml_Marketingsoftware_Link extends Mage_Core_Block_Template
32
  {
33
+ /**
34
+ * Constructor
35
+ *
36
+ */
37
  public function __construct()
38
  {
39
  parent::__construct();
41
  }
42
 
43
  /**
44
+ * Returns the post URL.
45
  *
46
  * @return string
47
  */
48
+ public function getPostUrl()
49
  {
50
+ return $this->getUrl('*/*/saveProfilesAndCollections', array('_secure' => true));
51
  }
52
 
53
  /**
54
+ * Get supported collection fields
55
+ * @param string
56
+ * @return array
57
  */
58
+ public function getSupportedCollectionFields($collectionType)
59
  {
60
+ switch ($collectionType)
61
+ {
62
+ case 'cartproducts':
63
+ return Mage::helper('marketingsoftware')->supportedCartItemFields();
64
+ case 'orders':
65
+ return Mage::helper('marketingsoftware')->supportedOrderFields();
66
+ case 'orderproducts':
67
+ return Mage::helper('marketingsoftware')->supportedOrderItemFields();
68
+ case 'addresses':
69
+ return Mage::helper('marketingsoftware')->supportedAddressFields();
70
+ case 'viewedproduct':
71
+ return Mage::helper('marketingsoftware')->supportedViewedProductFields();
72
+ // if we have non supported collection type, just return empty array
73
+ default:
74
+ return array();
75
+ }
76
+ }
77
+
78
+ /**
79
+ * Below. New Ajax Links
80
+ */
81
+
82
+ /**
83
+ * Get the url to account settings
84
+ * @return string
85
+ */
86
+ public function getAccountSettingsUrl()
87
+ {
88
+ return $this->getUrl('*/marketingsoftware_settings/index', array('_secure' => true));
89
+ }
90
+
91
+ /**
92
+ * Return Ajax url that will be used to save whole form.
93
+ * @return string
94
+ */
95
+ public function getSaveFormUrl()
96
+ {
97
+ return $this->getUrl('*/*/saveForm', array('_secure' => true));
98
+ }
99
+
100
+ /**
101
+ * Return Ajax url that will answer AJAX requests about copernica's database repairs.
102
+ * @return string
103
+ */
104
+ public function getAjaxDatabaseValidateUrl()
105
+ {
106
+ return $this->getUrl('*/marketingsoftware_ajaxdatabase/validate', array('_secure' => true));
107
+ }
108
+
109
+ /**
110
+ * Return Ajax url that will answer AJAX request about copernica's database creations.
111
+ * @return string
112
+ */
113
+ public function getAjaxDatabaseCreateUrl()
114
+ {
115
+ return $this->getUrl('*/marketingsoftware_ajaxdatabase/create', array('_secure' => true));
116
+ }
117
+
118
+ /**
119
+ * Return Ajax url that can be used to fetch database field
120
+ * @return string
121
+ */
122
+ public function getAjaxDatabaseFetchUrl()
123
+ {
124
+ return $this->getUrl('*/marketingsoftware_ajaxdatabase/fetchField', array('_secure' => true));
125
+ }
126
+
127
+ /**
128
+ * Rerturn Ajax url that can be used to validate database field
129
+ * @return string
130
+ */
131
+ public function getAjaxDatabaseFieldValidateUrl()
132
+ {
133
+ return $this->getUrl('*/marketingsoftware_ajaxdatabasefield/validate', array('_secure' => true));
134
+ }
135
+
136
+ /**
137
+ * Return Ajax url that can be used to create database field
138
+ * @return string
139
+ */
140
+ public function getAjaxDatabaseFieldCreateUrl()
141
+ {
142
+ return $this->getUrl('*/marketingsoftware_ajaxdatabasefield/create', array('_secure' => true));
143
+ }
144
+
145
+ /**
146
+ * Return Ajax url that can be used to validate collection
147
+ * @return string
148
+ */
149
+ public function getAjaxCollectionValidateUrl()
150
+ {
151
+ return $this->getUrl('*/marketingsoftware_ajaxcollection/validate', array('_secure' => true));
152
+ }
153
+
154
+ /**
155
+ * Return Ajax url that can be used to create collection
156
+ * @return string
157
+ */
158
+ public function getAjaxCollectionCreaetUrl()
159
+ {
160
+ return $this->getUrl('*/marketingsoftware_ajaxcollection/create', array('_secure' => true));
161
+ }
162
+
163
+ /**
164
+ * Return Ajax url that can be used to fetch infromation about collection
165
+ * @return string
166
+ */
167
+ public function getAjaxCollectionInfoUrl()
168
+ {
169
+ return $this->getUrl('*/marketingsoftware_ajaxcollection/info', array('_secure' => true));
170
+ }
171
+
172
+ /**
173
+ * Return Ajax url that can be used ti fetch information about collection field
174
+ * @return string
175
+ */
176
+ public function getAjaxCollectionFetchUrl()
177
+ {
178
+ return $this->getUrl('*/marketingsoftware_ajaxcollection/fetch', array('_secure' => true));
179
+ }
180
+
181
+ /**
182
+ * Return Ajax url that can be used to validate collection fields
183
+ * @return string
184
+ */
185
+ public function getAjaxCollectionFieldValidateUrl()
186
+ {
187
+ return $this->getUrl('*/marketingsoftware_ajaxcollectionfield/validate', array('_secure' => true));
188
+ }
189
+
190
+ /**
191
+ * Return Ajax url that can be used to create collection fields
192
+ * @return string
193
+ */
194
+ public function getAjaxCollectionFieldCreateUrl()
195
+ {
196
+ return $this->getUrl('*/marketingsoftware_ajaxcollectionfield/create', array('_secure' => true));
197
  }
198
  }
app/code/community/Copernica/MarketingSoftware/Block/Adminhtml/Marketingsoftware/Settings.php CHANGED
@@ -26,14 +26,13 @@
26
 
27
  /**
28
  * Settings Block
29
- *
30
  */
31
  class Copernica_MarketingSoftware_Block_Adminhtml_Marketingsoftware_Settings extends Mage_Core_Block_Template
32
  {
33
- /**
34
- * Constructor
35
- *
36
- */
37
  public function __construct()
38
  {
39
  parent::__construct();
@@ -41,22 +40,39 @@ class Copernica_MarketingSoftware_Block_Adminhtml_Marketingsoftware_Settings ext
41
  }
42
 
43
  /**
44
- * Returns the check settings URL.
45
  *
46
  * @return string
47
  */
48
- public function getCheckSettingsUrl()
49
  {
50
- return $this->getUrl('*/*/checker', array('_secure' => true));
51
  }
52
 
53
  /**
54
- * Returns the post URL.
55
- *
56
- * @return string
57
  */
58
- public function getPostUrl()
59
  {
60
- return $this->getUrl('*/*/send', array('_secure' => true));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
  }
62
  }
26
 
27
  /**
28
  * Settings Block
 
29
  */
30
  class Copernica_MarketingSoftware_Block_Adminhtml_Marketingsoftware_Settings extends Mage_Core_Block_Template
31
  {
32
+ /**
33
+ * Constructor
34
+ *
35
+ */
36
  public function __construct()
37
  {
38
  parent::__construct();
40
  }
41
 
42
  /**
43
+ * Returns the post URL.
44
  *
45
  * @return string
46
  */
47
+ public function getPostUrl()
48
  {
49
+ return $this->getUrl('*/*/send', array('_secure' => true));
50
  }
51
 
52
  /**
53
+ * Returns the state URL
54
+ * @return string
 
55
  */
56
+ public function getStateUrl()
57
  {
58
+ return Mage::helper('adminhtml')->getUrl('*/*/state');
59
+ }
60
+
61
+ /**
62
+ * Returns the queue URL
63
+ * @return string
64
+ */
65
+ public function getQueuePostUrl()
66
+ {
67
+ return Mage::helper('adminhtml')->getUrl('*/*/queue');
68
+ }
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
  }
app/code/community/Copernica/MarketingSoftware/Block/Adminhtml/Marketingsoftware/Sync.php ADDED
@@ -0,0 +1,120 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ * Sync block
29
+ */
30
+ class Copernica_MarketingSoftware_Block_Adminhtml_MarketingSoftware_Sync extends Mage_Core_Block_Template
31
+ {
32
+ /**
33
+ * Construct block
34
+ */
35
+ public function __construct()
36
+ {
37
+ parent::__construct();
38
+ $this->setTemplate('marketingsoftware/sync.phtml');
39
+ }
40
+
41
+ /**
42
+ * Get url that will point to 'post' action in contoller
43
+ * @return string
44
+ */
45
+ public function getPostUrl()
46
+ {
47
+ return $this->getUrl('*/*/post', array('_secure' => true));
48
+ }
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()
64
+ {
65
+ return $this->getUrl('*/*/getProfile', array('_secure' => true));
66
+ }
67
+
68
+ /**
69
+ * Get url that will point to delete action in controller
70
+ * @return string
71
+ */
72
+ public function getDeleteUrl()
73
+ {
74
+ return $this->getUrl('*/*/delete', array('_secure' => true));
75
+ }
76
+
77
+ /**
78
+ * Get url that will point to state action in controller
79
+ * @return string
80
+ */
81
+ public function getStateUrl()
82
+ {
83
+ return Mage::helper('adminhtml')->getUrl('*/*/state');
84
+ return $this->getUrl('*/*/state', array('_secure' => true));
85
+ }
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
+ }
app/code/community/Copernica/MarketingSoftware/Controller/Base.php ADDED
@@ -0,0 +1,178 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 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
34
+ * version we will use our own one. All notes that we will want to display
35
+ * will be stored in this arrays as entries. For now we will just store it
36
+ * in super easy format.
37
+ *
38
+ * array (
39
+ * 'errors' => string[],
40
+ * 'warnings' => string[]
41
+ * )
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
+ }
app/code/community/Copernica/MarketingSoftware/Exception.php ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 holds implementation for exception class that will be used in this
29
+ * extension. To throw an exception use
30
+ *
31
+ * throw Mage::exception('Copernica_MarketingSoftware', $message, $code);
32
+ */
33
+ class Copernica_MarketingSoftware_Exception extends Exception
34
+ {
35
+ /*
36
+ * List of error codes as constants
37
+ */
38
+
39
+ // whenever we have a situation when user is not supplying is with correct input
40
+ const INVALID_INPUT = 1;
41
+
42
+ // when we have an error on Api request
43
+ const API_REQUEST_ERROR = 2;
44
+
45
+ // when database does not exists
46
+ const DATABASE_NOT_EXISTS = 3;
47
+
48
+ // when database does have invalid structure
49
+ const DATABASE_STRUCT_INVALID = 4;
50
+
51
+ // magento field is not linked to copernica field
52
+ const FIELD_NOT_LINKED = 5;
53
+
54
+ // field on copernica platform does not exists
55
+ const FIELD_NOT_EXISTS = 6;
56
+
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
64
+ const COLLECTION_INVALID_TYPE = 9;
65
+
66
+ // collection does not have a proper structure
67
+ const COLLECTION_STRUCT_INVALID = 10;
68
+
69
+ // missing valid event type
70
+ const EVENT_NO_TYPE = 11;
71
+
72
+ // event class does not exists or it can not be loaded
73
+ const EVENT_TYPE_NOT_EXISTS = 12;
74
+
75
+ // when something really, really bad happens with API
76
+ const API_ERROR = 13;
77
+
78
+ // when we want to access customer that does no longer exists
79
+ const CUSTOMER_NOT_EXISTS = 100;
80
+ }
app/code/community/Copernica/MarketingSoftware/Helper/Api.php CHANGED
@@ -1,1208 +1,357 @@
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_MarketingSoftware_Helper_Api
29
- * This file holds the class that is used to communicate with Copernica
30
- * Copernica Marketing Software v 1.2.0
31
- * March 2011
32
- * http://www.copernica.com/
33
- */
34
-
35
- /** Require some additional file for exceptions */
36
- require_once dirname(__FILE__).'/../Model/Error.php';
37
-
38
- /**
39
- * CopernicaAPI class
40
- * Class with methods to communicate with the Copernica API
41
- */
42
- class Copernica_MarketingSoftware_Helper_Api extends Mage_Core_Helper_Abstract
43
- {
44
- /**
45
- * The main SOAPAPI object that will be used for all calls
46
- * @var SoapClient
47
- */
48
- public $soapclient;
49
-
50
- /**
51
- * The Copernica account name that is sent with the constructor
52
- * @var string
53
- */
54
- protected $account;
55
-
56
- /**
57
- * Store the id of the database
58
- * @var integer
59
- */
60
- protected $databaseID = false;
61
-
62
- /**
63
- * Store the id of the collection
64
- * @var integer
65
- */
66
- protected $collectionID = array();
67
-
68
- /**
69
- * Checks the connection settings and initializes the soap client
70
- * @param string URL of the copernica API
71
- * @param string Account name
72
- * @param string User name
73
- * @param string Password
74
- */
75
- public function init($url, $username, $accountname, $password)
76
- {
77
- $helper = Mage::helper('marketingsoftware');
78
-
79
- // store error information
80
- $this->account = $accountname;
81
-
82
- try {
83
- // construct SOAPAPI object
84
- $this->soapclient = Mage::getModel('marketingsoftware/asyncPomSoapClient', array(
85
- 'url' => $url,
86
- 'login' => $username,
87
- 'account' => $accountname,
88
- 'password' => $password
89
- ));
90
- } catch (Exception $e) {
91
-
92
- // there is no connection
93
- $this->soapclient = false;
94
-
95
- if ($e instanceOf CopernicaError) {
96
- throw $e;
97
- } else {
98
- // throw an error
99
- throw new CopernicaError(COPERNICAERROR_UNREACHABLE);
100
- }
101
- }
102
- return $this;
103
- }
104
-
105
- /**
106
- * Is this API instance valid?
107
- * @param boolean also validate the configuration
108
- * @throws Exception
109
- * @return boolean
110
- */
111
- public function check($extensive = false)
112
- {
113
- // if no soapclient object exists, there is a problem with the connection
114
- if (!is_object($this->soapclient)) throw new CopernicaError(COPERNICAERROR_UNREACHABLE);
115
-
116
- // check for invalid login
117
- $objarray = get_object_vars($this->soapclient);
118
-
119
- // return API Error
120
- if (isset($objarray['__soap_fault'])) throw new CopernicaError(COPERNICAERROR_LOGINFAILURE);
121
-
122
- // If we should not do an extensive check, return now
123
- if (!$extensive) return true;
124
-
125
- // Get the config
126
- $config = Mage::helper('marketingsoftware/config');
127
-
128
- // check the database and collection names, might throw an exception
129
- $this->getDatabaseId();
130
- $this->getCollectionId($config->getCartItemsCollectionName());
131
- $this->getCollectionId($config->getOrdersCollectionName());
132
- $this->getCollectionId($config->getOrderItemsCollectionName());
133
- $this->getCollectionId($config->getAddressesCollectionName());
134
- $this->getCollectionId($config->getViewedProductCollectionName());
135
-
136
- // else we have a valid login
137
- return true;
138
- }
139
-
140
- /**
141
- * Get the database id
142
- * @param string
143
- * @return integer
144
- */
145
- protected function getDatabaseId($databaseName = false)
146
- {
147
- // Did we already have this id, return it
148
- if ($this->databaseID !== false) return $this->databaseID;
149
-
150
- // Get the database name
151
- $identifier = ($databaseName === false) ? Mage::helper('marketingsoftware/config')->getDatabaseName() : $databaseName;
152
-
153
- Varien_Profiler::start('Copernica SOAP call: Account_Database');
154
-
155
- // Get the database object
156
- $request = $this->soapclient->Account_Database(array('identifier' => $identifier));
157
-
158
- // Get the response object from the request
159
- $object = $this->soapclient->result($request);
160
-
161
- Varien_Profiler::stop('Copernica SOAP call: Account_Database');
162
-
163
- // If it not an object, throw an error
164
- if (!is_object($object)) throw (new CopernicaError(COPERNICAERROR_NODATABASE));
165
-
166
- // if no databasename was given, store it
167
- if ($databaseName === false) $this->databaseID = $object->id;
168
-
169
- // store and return the object
170
- return $object->id;
171
- }
172
-
173
- /**
174
- * Get the collection id
175
- * @param string
176
- * @return integer
177
- */
178
- protected function getCollectionId($name, $databaseName = '')
179
- {
180
- // Did we already have this id, return it
181
- if (isset($this->collectionID[$databaseName.$name])) return $this->collectionID[$databaseName.$name];
182
-
183
- Varien_Profiler::start('Copernica SOAP call: Database_Collection');
184
-
185
- // Get the soap collection object
186
- $request = $this->soapclient->Database_Collection(array(
187
- 'id' => $this->getDatabaseId($databaseName ? $databaseName : false),
188
- 'identifier' => $name
189
- ));
190
-
191
- // Get the response object from the request
192
- $object = $this->soapclient->result($request);
193
-
194
- Varien_Profiler::stop('Copernica SOAP call: Database_Collection');
195
-
196
- // no object is returned
197
- if (!is_object($object)) throw (new CopernicaError(COPERNICAERROR_NOCOLLECTION));
198
-
199
- // store and return the object
200
- return $this->collectionID[$databaseName.$name] = $object->id;
201
- }
202
-
203
- /**
204
- * Get the Copernica profiles from the api, which match a certain identifier
205
- *
206
- * @param identifier
207
- */
208
- public function searchProfiles($identifier)
209
- {
210
- Varien_Profiler::start('Copernica SOAP call: Database_SearchProfiles');
211
-
212
- // Search the profiles
213
- $profiles = $this->soapclient->result(
214
- $this->soapclient->Database_SearchProfiles(array(
215
- 'id' => $this->getDatabaseId(),
216
- 'requirements' => array(
217
- $this->soapclient->toObject(array(
218
- 'fieldname' => 'customer_id',
219
- 'value' => $identifier,
220
- 'operator' => '='
221
- ))
222
- )
223
- ))
224
- );
225
-
226
- Varien_Profiler::stop('Copernica SOAP call: Database_SearchProfiles');
227
-
228
- return $profiles;
229
- }
230
-
231
- /**
232
- * Merge the given array of profiles
233
- * @param array
234
- * @return array
235
- */
236
- public function mergeProfiles($profiles)
237
- {
238
- // Get the first item
239
- $first = $profiles[0];
240
-
241
- // iterate over the rest of the items
242
- for ($i = 1; $i < count($profiles); $i++)
243
- {
244
- $this->soapclient->Profile_Move(array(
245
- 'id' => $first->id,
246
- 'profile' => $this->soapclient->toObject(array(
247
- 'id' => $profiles[$i]->id
248
- )),
249
- ));
250
- }
251
- }
252
-
253
- /**
254
- * Update the profiles given a customer and return the found profiles
255
- * @param Copernica_MarketingSoftware_Model_Copernica_ProfileCustomer
256
- * @param String optional identifier for this update action
257
- * @return array
258
- */
259
- public function updateProfiles($data)
260
- {
261
- // if we are calling this function with an incorrect profile, just
262
- // return because we will damage too much
263
- if ($data->id() == false)
264
- {
265
- // Log the profile identifier that is given
266
- Mage::log("Identifier has type: ".gettype($data->id()) ." and value ".$data->id() ? 'true':'false');
267
- Mage::log("Data is of type: ".get_class($data));
268
- Mage::log("Data: ".print_r($data->toArray(), true));
269
- foreach (debug_backtrace() as $tr) Mage::log(" ".$tr['file'].''.$tr['line']);
270
-
271
- // and get lost
272
- return;
273
- }
274
-
275
- Varien_Profiler::start('Copernica SOAP call: Database_updateProfiles');
276
-
277
- // Update the profiles and wait for the result because, we want to search for it
278
- $this->soapclient->result($this->soapclient->Database_updateProfiles(array(
279
- 'id' => $this->getDatabaseId(),
280
- 'requirements' => array(
281
- $this->soapclient->toObject(array(
282
- 'fieldname' => 'customer_id',
283
- 'value' => $data->originalId(),
284
- 'operator' => '='
285
- ))
286
- ),
287
- 'create' => true,
288
- 'fields' => $data->toArray()
289
- )));
290
-
291
- Varien_Profiler::stop('Copernica SOAP call: Database_updateProfiles');
292
- }
293
-
294
- /**
295
- * Remove the profile
296
- *
297
- * @param Copernica_MarketingSoftware_Model_Copernica_ProfileCustomer $data
298
- * @return void
299
- */
300
- public function removeProfiles($data)
301
- {
302
- $profileIds = array();
303
- $profiles = $this->searchProfiles($data->id());
304
-
305
- foreach($profiles->items as $item) {
306
- $profileIds[] = $item->id;
307
- }
308
-
309
- $this->soapclient->Database_removeProfiles(array(
310
- 'id' => $this->getDatabaseId(),
311
- 'ids' => $profileIds
312
- ));
313
- }
314
-
315
- /**
316
- * Update the subprofiles given, the profile identifier
317
- * the collection name and the data
318
- * @param string customer identifier
319
- * @param Copernica_MarketingSoftware_Model_Copernica_Subprofile
320
- */
321
- public function updateCartItemSubProfiles($profileID, $data)
322
- {
323
- // The collection name and id are determined
324
- $collectionName = Mage::helper('marketingsoftware/config')->getCartItemsCollectionName();
325
- $collectionId = $this->getCollectionId($collectionName);
326
-
327
- // Update the subprofiles
328
- $this->soapclient->Profile_updateSubProfiles(array(
329
- 'id' => $profileID,
330
- 'requirements' => array(
331
- $this->soapclient->toObject(array(
332
- 'fieldname' => 'item_id',
333
- 'value' => $data->id(),
334
- 'operator' => '='
335
- ))
336
- ),
337
- 'collection' => $this->soapclient->toObject(array('id' => $collectionId)),
338
- 'create' => true,
339
- 'fields' => $data->toArray(),
340
- ));
341
- }
342
-
343
- /**
344
- * Remove the cart items which have been purchased
345
- *
346
- * @param string customer identifier
347
- * @param integer quote item id
348
- */
349
- public function removeOldCartItems($profileID, $quoteId)
350
- {
351
- // The collection name and id are determined
352
- $collectionName = Mage::helper('marketingsoftware/config')->getCartItemsCollectionName();
353
- $collectionId = $this->getCollectionId($collectionName);
354
-
355
- Varien_Profiler::start('Copernica SOAP call: Profile_searchSubProfiles');
356
-
357
- // find the subprofiles
358
- $subprofiles = $this->soapclient->result($this->soapclient->Profile_searchSubProfiles(array(
359
- 'id' => $profileID,
360
- 'requirements' => array(
361
- $this->soapclient->toObject(array(
362
- 'fieldname' => 'quote_id',
363
- 'value' => $quoteId,
364
- 'operator' => '='
365
- )),
366
- $this->soapclient->toObject(array(
367
- 'fieldname' => 'status',
368
- 'value' => 'deleted',
369
- 'operator' => '!='
370
- ))
371
- ),
372
- 'collection' => $this->soapclient->toObject(array('id' => $collectionId)),
373
- )));
374
-
375
- Varien_Profiler::stop('Copernica SOAP call: Profile_searchSubProfiles');
376
-
377
- // Build an array of ids
378
- $ids = array();
379
- foreach ($subprofiles->items as $subprofile) $ids[] = $subprofile->id;
380
-
381
- // Remove the subprofiles
382
- $this->soapclient->Collection_removeSubProfiles(array(
383
- 'id' => $collectionId,
384
- 'ids' => $ids
385
- ));
386
- }
387
-
388
- /**
389
- * Update the subprofiles given, the profile identifier
390
- * the collection name and the data
391
- *
392
- * @param string customer identifier
393
- * @param Copernica_MarketingSoftware_Model_Copernica_Subprofile
394
- */
395
- public function updateOrderSubProfile($profileID, $data)
396
- {
397
- // The collection name and id are determined
398
- $collectionName = Mage::helper('marketingsoftware/config')->getOrdersCollectionName();
399
- $collectionId = $this->getCollectionId($collectionName);
400
-
401
- // Update the subprofiles
402
- $this->soapclient->Profile_updateSubProfiles(array(
403
- 'id' => $profileID,
404
- 'requirements' => array(
405
- $this->soapclient->toObject(array(
406
- 'fieldname' => 'order_id',
407
- 'value' => $data->id(),
408
- 'operator' => '='
409
- ))
410
- ),
411
- 'collection' => $this->soapclient->toObject(array('id' => $collectionId)),
412
- 'create' => true,
413
- 'fields' => $data->toArray(),
414
- ));
415
- }
416
-
417
- /**
418
- * Update the subprofiles given, the profile identifier
419
- * the collection name and the data
420
- *
421
- * @param string customer identifier
422
- * @param Copernica_MarketingSoftware_Model_Copernica_Subprofile
423
- */
424
- public function updateOrderItemSubProfiles($profileID, $data)
425
- {
426
- // The collection name and id are determined
427
- $collectionName = Mage::helper('marketingsoftware/config')->getOrderItemsCollectionName();
428
- $collectionId = $this->getCollectionId($collectionName);
429
-
430
- // Update the subprofiles
431
- $this->soapclient->Profile_updateSubProfiles(array(
432
- 'id' => $profileID,
433
- 'requirements' => array(
434
- $this->soapclient->toObject(array(
435
- 'fieldname' => 'item_id',
436
- 'value' => $data->id(),
437
- 'operator' => '='
438
- ))
439
- ),
440
- 'collection' => $this->soapclient->toObject(array('id' => $collectionId)),
441
- 'create' => true,
442
- 'fields' => $data->toArray(),
443
- ));
444
- }
445
-
446
- /**
447
- * Update the subprofiles given, the profile identifier
448
- * the collection name and the data
449
- *
450
- * @param string customer identifier
451
- * @param Copernica_MarketingSoftware_Model_Copernica_Subprofile
452
- */
453
- public function updateAddressSubProfiles($profileID, $data)
454
- {
455
- // The collection name and id are determined
456
- $collectionName = Mage::helper('marketingsoftware/config')->getAddressesCollectionName();
457
- $collectionId = $this->getCollectionId($collectionName);
458
-
459
- // Update the subprofiles
460
- $this->soapclient->Profile_updateSubProfiles(array(
461
- 'id' => $profileID,
462
- 'requirements' => array(
463
- $this->soapclient->toObject(array(
464
- 'fieldname' => 'address_id',
465
- 'value' => $data->id(),
466
- 'operator' => '='
467
- ))
468
- ),
469
- 'collection' => $this->soapclient->toObject(array('id' => $collectionId)),
470
- 'create' => true,
471
- 'fields' => $data->toArray(),
472
- ));
473
- }
474
-
475
- /**
476
- * Update the subprofiles given, the profile identifier
477
- * the collection name and the data
478
- * @param string customer identifier
479
- * @param Copernica_MarketingSoftware_Model_Copernica_Subprofile
480
- */
481
- public function updateViewedProductSubProfiles($profileID, $data)
482
- {
483
- // The collection name and id are determined
484
- $collectionName = Mage::helper('marketingsoftware/config')->getViewedProductCollectionName();
485
- $collectionId = $this->getCollectionId($collectionName);
486
-
487
- // Update the subprofiles
488
- $this->soapclient->Profile_updateSubProfiles(array(
489
- 'id' => $profileID,
490
- 'requirements' => array(
491
- $this->soapclient->toObject(array(
492
- 'fieldname' => 'id',
493
- 'value' => $data->id(),
494
- 'operator' => '='
495
- ))
496
- ),
497
- 'collection' => $this->soapclient->toObject(array('id' => $collectionId)),
498
- 'create' => true,
499
- 'fields' => $data->toArray(),
500
- ));
501
- }
502
-
503
- /**
504
- * Does a database with the given name exist?.
505
- *
506
- * @param string
507
- * @return boolean
508
- */
509
- public function databaseExist($databaseName)
510
- {
511
- // We have a valid id
512
- try
513
- {
514
- return $this->getDatabaseId($databaseName) > 0;
515
- }
516
- catch (Exception $e)
517
- {
518
- return false;
519
- }
520
- }
521
-
522
- /**
523
- * Is the database with the given name valid?
524
- *
525
- * @param string
526
- * @return string 'ok', 'notexists', 'notvalid'
527
- */
528
- public function validateDatabase($databaseName)
529
- {
530
- try
531
- {
532
- // Get the database id
533
- $id = $this->getDatabaseId($databaseName);
534
- }
535
- catch (Exception $e)
536
- {
537
- // the database does not exist?
538
- return 'notexists';
539
- }
540
-
541
- // we should check whether this database has the crucial field: customer_id
542
- $response = $this->soapclient->result($this->soapclient->Database_field(array(
543
- 'id' => $id,
544
- 'identifier' => 'customer_id'
545
- )));
546
-
547
- // is there are respone object?
548
- if (!is_object($response) || $response->id == 0) return 'notvalid';
549
-
550
- // everything seems ok
551
- return 'ok';
552
- }
553
-
554
- /**
555
- * Repair the database with the given name valid?
556
- *
557
- * @param string
558
- * @return string 'ok', 'notexists', 'notvalid'
559
- */
560
- public function repairDatabase($databaseName)
561
- {
562
- try
563
- {
564
- // Get the database id
565
- $id = $this->getDatabaseId($databaseName);
566
- }
567
- catch (Exception $e)
568
- {
569
- // create the database
570
- $response = $this->soapclient->result($this->soapclient->Account_createDatabase(array('name' => $databaseName)));
571
-
572
- // Get the database id
573
- $id = $response->id;
574
- }
575
-
576
- // create the field customer id
577
- $this->soapclient->Database_createField(array(
578
- 'id' => $id,
579
- 'name' => 'customer_id',
580
- 'type' => 'text'
581
- ));
582
-
583
- // everything seems ok
584
- return $this->validateDatabase($databaseName);
585
- }
586
-
587
- /**
588
- * Is the collection in the given database, with the given type
589
- * and name valid?
590
- *
591
- * @param string $databaseName name of the database
592
- * @param string $collectionType 'cartproducts', 'orders', 'orderproducts', 'addresses'
593
- * @param string $collectionName name of the collection
594
- * @return string 'ok', 'notexists', 'notvalid'
595
- */
596
- public function validateCollection($databaseName, $collectionType, $collectionName)
597
- {
598
- // Get the database id
599
- try
600
- {
601
- $collectionId = $this->getCollectionId($collectionName, $databaseName);
602
- }
603
- catch (Exception $e)
604
- {
605
- // does the collection exist?
606
- return 'notexists';
607
- }
608
-
609
- // there are some required fields, check if they exist
610
- $requiredFields = array(
611
- 'cartproducts' => array('item_id', 'quote_id', 'status'),
612
- 'orders' => array('order_id', 'quote_id'),
613
- 'orderproducts' => array('item_id', 'order_id'),
614
- 'addresses' => array('address_id'),
615
- 'viewedproduct' => array('product_id'),
616
- );
617
-
618
- // is this a known collection type
619
- if (!isset($requiredFields[$collectionType])) return 'impossible';
620
-
621
- // initialize the request array
622
- $requests = array();
623
-
624
- foreach($requiredFields[$collectionType] as $field)
625
- {
626
- // we should check whether this collection has the crucial fields
627
- $requests[] = $this->soapclient->Collection_field(array(
628
- 'id' => $collectionId,
629
- 'identifier' => $field
630
- ));
631
- }
632
-
633
- // iterate over the requests
634
- foreach ($requests as $request)
635
- {
636
- // Get the response
637
- $response = $this->soapclient->result($request);
638
-
639
- // is there a respone object?
640
- if (!is_object($response) || $response->id == 0) return 'notvalid';
641
- }
642
-
643
- // everything seems ok
644
- return 'ok';
645
- }
646
-
647
- /**
648
- * Repair the collection in the given database, with the given type
649
- * and name
650
- *
651
- * @param string $databaseName name of the database
652
- * @param string $collectionType 'cartproducts', 'orders', 'orderproducts', 'addresses'
653
- * @param string $collectionName name of the collection
654
- * @return string 'ok', 'notexists', 'notvalid'
655
- */
656
- public function repairCollection($databaseName, $collectionType, $collectionName)
657
- {
658
- try
659
- {
660
- // Get the collection id
661
- $collectionId = $this->getCollectionId($collectionName, $databaseName);
662
- }
663
- catch (Exception $e)
664
- {
665
- // Get the Database id
666
- $id = $this->getDatabaseId($databaseName);
667
-
668
- // The collection should be created
669
- $response = $this->soapclient->result($this->soapclient->Database_createCollection(array(
670
- 'id' => $id,
671
- 'name' => $collectionName
672
- )));
673
-
674
- // Get the collection id
675
- $collectionId = $response->id;
676
- }
677
-
678
- // there are some required fields, check if they exist
679
- $requiredFields = array(
680
- 'cartproducts' => array('item_id', 'quote_id', 'status'),
681
- 'orders' => array('order_id', 'quote_id'),
682
- 'orderproducts' => array('item_id', 'order_id'),
683
- 'addresses' => array('address_id'),
684
- 'viewedproduct' => array('product_id'),
685
- );
686
-
687
- // is this a known collection type
688
- if (!isset($requiredFields[$collectionType])) return 'impossible';
689
-
690
- // iterate over the required fields and them if needed
691
- foreach($requiredFields[$collectionType] as $field)
692
- {
693
- // we should check whether this collection has the crucial fields
694
- $this->soapclient->Collection_createField(array(
695
- 'id' => $collectionId,
696
- 'name' => $field,
697
- 'type' => 'text',
698
- ));
699
- }
700
-
701
- // everything seems ok
702
- return 'ok';
703
- }
704
-
705
- /**
706
- * Validate the field, given the collection name.
707
- * When the collection name is empty the check is performed for the database
708
- *
709
- * @param String fieldname in our Magento plug-in
710
- * @param String fieldname in the customers Copernica environment
711
- * @param String name of the database in the customers Copernica environment
712
- * @param String collection name (bool)false, 'cartproducts', 'orders', 'orderproducts', 'addresses'
713
- * @param String name of the collection in the customers Copernica environment
714
- * @return 'ok', 'notexists', 'notvalid'
715
- */
716
- public function validateField($magentoFieldName, $copernicaFieldName, $database, $collection = false, $collectionName = false)
717
- {
718
- switch($collection)
719
- {
720
- case "cartproducts": return $this->validateCartProductsField($database, $collectionName, $magentoFieldName, $copernicaFieldName);
721
- case "orders": return $this->validateOrdersField($database, $collectionName, $magentoFieldName, $copernicaFieldName);
722
- case "orderproducts": return $this->validateOrderProductsField($database, $collectionName, $magentoFieldName, $copernicaFieldName);
723
- case "addresses": return $this->validateAddressesField($database, $collectionName, $magentoFieldName, $copernicaFieldName);
724
- case "viewedproduct": return $this->validateViewedProductField($database, $collectionName, $magentoFieldName, $copernicaFieldName);
725
-
726
- // no collection given use database
727
- default: return $this->validateDatabaseField($database, $magentoFieldName, $copernicaFieldName);
728
- }
729
- }
730
-
731
- /**
732
- * Validate the field from the database
733
- *
734
- * @param String name of the database in the customers Copernica environment
735
- * @param String name of the field in our Magento plug-in
736
- * @param String fieldname in the customers Copernica environment
737
- * @return 'ok', 'notexists', 'notvalid'
738
- */
739
- protected function validateDatabaseField($databaseName, $magentoFieldName, $copernicaFieldName)
740
- {
741
- // Get the database id
742
- $id = $this->getDatabaseId($databaseName);
743
-
744
- // Get the response
745
- $response = $this->soapclient->result($this->soapclient->Database_field(array(
746
- 'id' => $id,
747
- 'identifier' => $copernicaFieldName
748
- )));
749
-
750
- // does the field exist
751
- if (!is_object($response) || $response->id == 0) return 'notexists';
752
-
753
- // any extra checks base on which field we are checking
754
- switch($magentoFieldName)
755
- {
756
- case "email": return ($response->type != 'email') ? 'notvalid' : 'ok';
757
- case "newsletter":
758
- $retrieveDatabaseCall = $this->soapclient->Database_retrieve(array('id' => $id));
759
- $unsubscribeValuesCall = $this->soapclient->Database_unsubscribeValues(array('id' => $id));
760
- $databaseCallbacksCall = $this->soapclient->Database_callbacks(array('id' => $id));
761
-
762
- // is the unsubscribe behaviour set?
763
- $database = $this->soapclient->result($retrieveDatabaseCall);
764
-
765
- // unsubscribe behaviour should be update
766
- if ($database->unsubscribebehavior != 'update') return 'notvalid';
767
-
768
- // get the the unsubscribe values
769
- $unsubscribeValues = $this->soapclient->result($unsubscribeValuesCall);
770
-
771
- // by default the right field is not found
772
- $found = false;
773
-
774
- // iterate over the fields
775
- foreach ($unsubscribeValues as $field => $newValue)
776
- {
777
- // ignore all other fields
778
- if ($field != $copernicaFieldName) continue;
779
-
780
- // we found it
781
- $found = true;
782
-
783
- // The new value should be 'unsubscribed_copernica'
784
- if ($newValue != 'unsubscribed_copernica') return 'notvalid';
785
- }
786
-
787
- // the correct unsubscribe field should be found
788
- if (!$found) return 'notvalid';
789
-
790
- // are the callbacks installed?
791
- $callbacks = $this->soapclient->result($databaseCallbacksCall);
792
-
793
- // No valid result is returned, it is not valid
794
- if (!isset($callbacks->items)) return 'notvalid';
795
-
796
- // Get the url for the callback
797
- $callbackUrl = Mage::helper('marketingsoftware')->unsubscribeCallbackUrl();
798
-
799
- // We assume the callback is not there
800
- $found = false;
801
-
802
- // check all returned callbacks
803
- foreach ($callbacks->items as $callback)
804
- {
805
- if (
806
- $callback->url == $callbackUrl &&
807
- $callback->method == 'json' &&
808
- $callback->expression == "profile.$copernicaFieldName == 'unsubscribed_copernica';"
809
- ) $found = true;
810
- }
811
-
812
- // did we find the correct thingy
813
- return $found ? 'ok' :'notvalid';
814
- }
815
-
816
- // everyhing seems to be ok
817
- return 'ok';
818
- }
819
-
820
- /**
821
- * Get the field object from a certain collection with a certain name
822
- * @param String name of the database in the customers Copernica environment
823
- * @param String name of the collection in the customers Copernica environment
824
- * @param String fieldname in the customers Copernica environment
825
- * @return object SOAP object
826
- */
827
- protected function collectionFieldData($database, $collectionName, $fieldName)
828
- {
829
- // Get the id of this collection
830
- $id = $this->getCollectionId($collectionName, $database);
831
-
832
- // return the response
833
- return $this->soapclient->result($this->soapclient->Collection_field(array(
834
- 'id' => $id,
835
- 'identifier' => $fieldName
836
- )));
837
- }
838
-
839
- /**
840
- * Validate the field from the cart items collection
841
- * @param String name of the database in the customers Copernica environment
842
- * @param String name of the collection in the customers Copernica environment
843
- * @param String name of the field in our Magento plug-in
844
- * @param String fieldname in the customers Copernica environment
845
- * @return 'ok', 'notexists', 'notvalid'
846
- */
847
- protected function validateCartProductsField($database, $collectionName, $magentoFieldName, $copernicaFieldName)
848
- {
849
- // Get the id of this collection
850
- $object = $this->collectionFieldData($database, $collectionName, $copernicaFieldName);
851
-
852
- // does the field exist
853
- if (!is_object($object) || $object->id == 0) return 'notexists';
854
-
855
- // some special cases
856
- switch($magentoFieldName)
857
- {
858
- case 'timestamp': return (in_array($object->type, array('empty_datetime', 'datetime'))) ? 'ok' : 'notvalid';
859
- case 'url':
860
- case 'image': return ($object->length > 100) ? 'ok' : 'notvalid';
861
- case 'categories':
862
- case 'options':
863
- case 'attributes': return (($object->length > 150 || $object->big) && isset($object->lines) && $object->lines > 1) ? 'ok' : 'notvalid';
864
- }
865
-
866
- // default it is okay
867
- return 'ok';
868
- }
869
-
870
- /**
871
- * Validate the field from the orders collection
872
- * @param String name of the database in the customers Copernica environment
873
- * @param String name of the collection in the customers Copernica environment
874
- * @param String name of the field in our Magento plug-in
875
- * @param String fieldname in the customers Copernica environment
876
- * @return 'ok', 'notexists', 'notvalid'
877
- */
878
- protected function validateOrdersField($database, $collectionName, $magentoFieldName, $copernicaFieldName)
879
- {
880
- // Get the id of this collection
881
- $object = $this->collectionFieldData($database, $collectionName, $copernicaFieldName);
882
-
883
- // does the field exist
884
- if (!is_object($object) || $object->id == 0) return 'notexists';
885
-
886
- // some special cases
887
- switch($magentoFieldName)
888
- {
889
- case 'timestamp': return (in_array($object->type, array('empty_datetime', 'datetime'))) ? 'ok' : 'notvalid';
890
- }
891
-
892
- // default it is okay
893
- return 'ok';
894
- }
895
-
896
- /**
897
- * Validate the field from the order items collection
898
- * @param String name of the database in the customers Copernica environment
899
- * @param String name of the collection in the customers Copernica environment
900
- * @param String name of the field in our Magento plug-in
901
- * @param String fieldname in the customers Copernica environment
902
- * @return 'ok', 'notexists', 'notvalid'
903
- */
904
- protected function validateOrderProductsField($database, $collectionName, $magentoFieldName, $copernicaFieldName)
905
- {
906
- // Get the id of this collection
907
- $object = $this->collectionFieldData($database, $collectionName, $copernicaFieldName);
908
-
909
- // does the field exist
910
- if (!is_object($object) || $object->id == 0) return 'notexists';
911
-
912
- // some special cases
913
- switch($magentoFieldName)
914
- {
915
- case 'timestamp': return (in_array($object->type, array('empty_datetime', 'datetime'))) ? 'ok' : 'notvalid';
916
- case 'url':
917
- case 'image': return ($object->length > 100) ? 'ok' : 'notvalid';
918
- case 'categories':
919
- case 'options':
920
- case 'attributes': return (($object->length > 150 || $object->big) && $object->lines > 1) ? 'ok' : 'notvalid';
921
- }
922
-
923
- // default it is okay
924
- return 'ok';
925
- }
926
-
927
- /**
928
- * Validate the field from the addresses collection
929
- * @param String name of the database in the customers Copernica environment
930
- * @param String name of the collection in the customers Copernica environment
931
- * @param String name of the field in our Magento plug-in
932
- * @param String fieldname in the customers Copernica environment
933
- * @return 'ok', 'notexists', 'notvalid'
934
- */
935
- protected function validateAddressesField($database, $collectionName, $magentoFieldName, $copernicaFieldName)
936
- {
937
- // Get the id of this collection
938
- $object = $this->collectionFieldData($database, $collectionName, $copernicaFieldName);
939
-
940
- // does the field exist
941
- if (!is_object($object) || $object->id == 0) return 'notexists';
942
-
943
- // some special cases
944
- switch($magentoFieldName)
945
- {
946
- case "email": return ($object->type != 'email') ? 'notvalid' : 'ok';
947
- case "telephone": return ($object->type != 'phone_gsm') ? 'notvalid' : 'ok';
948
- case "fax": return ($object->type != 'phone_fax') ? 'notvalid' : 'ok';
949
- }
950
-
951
- // default it is okay
952
- return 'ok';
953
- }
954
-
955
- /**
956
- * Validate the field from the cart items collection
957
- * @param String name of the database in the customers Copernica environment
958
- * @param String name of the collection in the customers Copernica environment
959
- * @param String name of the field in our Magento plug-in
960
- * @param String fieldname in the customers Copernica environment
961
- * @return 'ok', 'notexists', 'notvalid'
962
- */
963
- protected function validateViewedProductField($database, $collectionName, $magentoFieldName, $copernicaFieldName)
964
- {
965
- // Get the id of this collection
966
- $object = $this->collectionFieldData($database, $collectionName, $copernicaFieldName);
967
-
968
- // does the field exist
969
- if (!is_object($object) || $object->id == 0) return 'notexists';
970
-
971
- // some special cases
972
- switch($magentoFieldName)
973
- {
974
- case 'timestamp': return (in_array($object->type, array('empty_datetime', 'datetime'))) ? 'ok' : 'notvalid';
975
- case 'url':
976
- case 'image': return ($object->length > 100) ? 'ok' : 'notvalid';
977
- case 'categories':
978
- case 'options':
979
- case 'attributes': return (($object->length > 150 || $object->big) && isset($object->lines) && $object->lines > 1) ? 'ok' : 'notvalid';
980
- }
981
-
982
- // default it is okay
983
- return 'ok';
984
- }
985
-
986
- /**
987
- * Repair the field, given the collection name.
988
- * When the collection name is empty the check is performed for the database
989
- * @param String fieldname in our Magento plug-in
990
- * @param String fieldname in the customers Copernica environment
991
- * @param String collection name (bool)false, 'cartproducts', 'orders', 'orderproducts', 'addresses'
992
- * @param String name of the collection in the customers Copernica environment
993
- * @return 'ok', 'notexists', 'notvalid'
994
- */
995
- public function repairField($magentoFieldName, $copernicaFieldName, $database, $collection = false, $containerName = false)
996
- {
997
- if ($collection == false) return $this->repairDatabaseField($database, $magentoFieldName, $copernicaFieldName);
998
- else return $this->repairCollectionField($database, $containerName, $collection, $magentoFieldName, $copernicaFieldName);
999
- }
1000
-
1001
- /**
1002
- * Repair the field from the cart items collection
1003
- * @param String name of the collection in the customers Copernica environment
1004
- * @param String name of the field in our Magento plug-in
1005
- * @param String fieldname in the customers Copernica environment
1006
- * @return 'ok', 'notexists', 'notvalid'
1007
- */
1008
- protected function repairDatabaseField($databaseName, $magentoFieldName, $copernicaFieldName)
1009
- {
1010
- $id = $this->getDatabaseId($databaseName);
1011
-
1012
- // Get the field object
1013
- $object = $this->soapclient->result($this->soapclient->Database_field(array(
1014
- 'id' => $id,
1015
- 'identifier' => $copernicaFieldName
1016
- )));
1017
-
1018
- // does the field exist
1019
- if (!is_object($object) || $object->id == 0)
1020
- {
1021
- // we have to create this field
1022
- $definition = array(
1023
- 'id' => $id,
1024
- 'name' => $copernicaFieldName,
1025
- 'display'=> '1',
1026
- 'type' => 'text',
1027
- );
1028
- }
1029
- else
1030
- {
1031
- // we have to create this field
1032
- $definition = array('id' => $object->id);
1033
- }
1034
-
1035
- // What field is this?
1036
- switch($magentoFieldName)
1037
- {
1038
- case "email": $definition['type'] = 'email'; break;
1039
- case "store_view": $definition['length'] = 100; break;
1040
- case "newsletter":
1041
- // is the unsubscribe behaviour set?
1042
- $database = $this->soapclient->result($this->soapclient->Database_retrieve(array('id' => $id)));
1043
-
1044
- // unsubscribe behaviour should be update
1045
- if ($database->unsubscribebehavior != 'update')
1046
- {
1047
- $this->soapclient->Database_update(array(
1048
- 'id' => $id,
1049
- 'unsubscribebehavior' => 'update'
1050
- ));
1051
- }
1052
-
1053
- // get the the unsubscribe values
1054
- $unsubscribeValues = $this->soapclient->result($this->soapclient->Database_unsubscribeValues(array('id' => $id)));
1055
-
1056
- // the correct unsubscribe field should be added
1057
- $unsubscribeValues[$copernicaFieldName] = 'unsubscribed_copernica';
1058
-
1059
- // store these values
1060
- $this->soapclient->Database_setUnsubscribeValues(array('id' => $id, 'fields' => $unsubscribeValues));
1061
-
1062
- // are the callbacks installed?
1063
- $callbacks = $this->soapclient->result($this->soapclient->Database_callbacks(array('id' => $id)));
1064
-
1065
- // Get the url for the callback
1066
- $callbackUrl = Mage::helper('marketingsoftware')->unsubscribeCallbackUrl();
1067
-
1068
- // We assume the callback is not there
1069
- $found = false;
1070
-
1071
- // did we receive callbacks?
1072
- if (isset($callbacks->items))
1073
- {
1074
- // check all returned callbacks
1075
- foreach ($callbacks->items as $callback)
1076
- {
1077
- if (
1078
- $callback->url == $callbackUrl &&
1079
- $callback->method == 'json' &&
1080
- $callback->expression == "profile.$copernicaFieldName == 'unsubscribed_copernica';"
1081
- ) $found = true;
1082
- }
1083
- }
1084
-
1085
- // there is no valid callback found
1086
- if (!$found)
1087
- {
1088
- // create the callback
1089
- $this->soapclient->Database_createCallback(array(
1090
- 'id' => $id,
1091
- 'url' => $callbackUrl,
1092
- 'method' => 'json',
1093
- 'expression' => "profile.$copernicaFieldName == 'unsubscribed_copernica';",
1094
- ));
1095
- }
1096
-
1097
- break;
1098
- }
1099
-
1100
- // create the field or update the existing field
1101
- if (!is_object($object) || $object->id == 0) $result = $this->soapclient->Database_createField($definition);
1102
- elseif (count($definition) > 1) $result = $this->soapclient->Field_update($definition);
1103
-
1104
- // wait for the field update / creation to be finished
1105
- if (isset($result)) $this->soapclient->result($result);
1106
-
1107
- // recheck the field
1108
- return $this->validateField($magentoFieldName, $copernicaFieldName, $databaseName);
1109
- }
1110
-
1111
- /**
1112
- * Repair the field from the cart items collection
1113
- * @param String name of the collection in the customers Copernica environment
1114
- * @param String collection name 'cartproducts', 'orders', 'orderproducts', 'addresses'
1115
- * @param String name of the field in our Magento plug-in
1116
- * @param String fieldname in the customers Copernica environment
1117
- * @return 'ok', 'notexists', 'notvalid'
1118
- */
1119
- protected function repairCollectionField($database, $collectionName, $collection, $magentoFieldName, $copernicaFieldName)
1120
- {
1121
- // Get the id of this collection
1122
- $object = $this->collectionFieldData($database, $collectionName, $copernicaFieldName);
1123
-
1124
- // does the field exist
1125
- if (!is_object($object) || $object->id == 0)
1126
- {
1127
- // we have to create this field
1128
- $definition = array(
1129
- 'id' => $this->getCollectionId($collectionName, $database),
1130
- 'name' => $copernicaFieldName,
1131
- 'display'=> '1',
1132
- 'type' => 'text',
1133
- );
1134
- }
1135
- else
1136
- {
1137
- // we have to create this field
1138
- $definition = array('id' => $object->id);
1139
- }
1140
-
1141
- // enrich the definition, given the collection
1142
- $definition = $this->getFieldDefinition($collection, $magentoFieldName, $definition);
1143
-
1144
- // create the field or update the existing field
1145
- if (!is_object($object) || $object->id == 0) $this->soapclient->Collection_createField($definition);
1146
- elseif (count($definition) > 1) $this->soapclient->CollectionField_update($definition);
1147
-
1148
- // Assume the field exists now
1149
- return 'ok';
1150
- }
1151
-
1152
- /**
1153
- * Enrich the field collection
1154
- * @param String collection name 'cartproducts', 'orders', 'orderproducts', 'addresses'
1155
- * @param String name of the field in our Magento plug-in
1156
- * @param array definition
1157
- * @return array with enriched definition
1158
- */
1159
- protected function getFieldDefinition($collection, $fieldName, $definition)
1160
- {
1161
- if ($collection == 'cartproducts' || $collection == 'orderproducts' || $collection == 'viewedproducts')
1162
- {
1163
- // some special cases, for the cart / order products
1164
- switch($fieldName)
1165
- {
1166
- case 'timestamp': $definition['type'] = 'datetime'; break;
1167
- case 'url':
1168
- case 'image': $definition['length'] = 155; break;
1169
- case 'categories': case 'options': case 'attributes':
1170
- $definition['length'] = 255;
1171
-
1172
- // both are needed for the current version
1173
- $definition['textlines'] = $definition['lines'] = 4;
1174
- break;
1175
- }
1176
- }
1177
- elseif ($collection == 'orders' && $fieldName == 'timestamp')
1178
- {
1179
- $definition['type'] = 'datetime';
1180
- }
1181
- elseif ($collection == 'addresses')
1182
- {
1183
- switch($fieldName)
1184
- {
1185
- case "email": $definition['type'] = 'email'; break;
1186
- case "telephone": $definition['type'] = 'phone_gsm'; break;
1187
- case "fax": $definition['type'] = 'phone_fax'; break;
1188
- }
1189
- }
1190
-
1191
- if ($fieldName == 'store_view') $definition['length'] = 100;
1192
-
1193
- // return the definition
1194
- return $definition;
1195
- }
1196
-
1197
- /**
1198
- * Method that handles the calls to the API
1199
- * @param string Name of the method
1200
- * @param array Associative array of parameters
1201
- * @return mixed
1202
- */
1203
- public function __call($methodname, $params)
1204
- {
1205
- // Call the soap client directly
1206
- return $this->soapclient->__call($methodname, $params);
1207
- }
1208
- }
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
+ * Coppernica REST API class.
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 (
109
+ 'fields[]' => 'customer_id=='.$customer->originalId(),
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
+ );
122
+ }
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')
171
+ );
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')
226
+ );
227
+ }
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')
268
+ );
269
+ }
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(
311
+ 'quote_id=='.$quoteId,
312
+ 'status!=deleted'
313
+ )
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)
348
+ {
349
+ try {
350
+ return $this->getDatabaseId($databaseName) > 0;
351
+ }
352
+ catch(Exception $e)
353
+ {
354
+ return false;
355
+ }
356
+ }
357
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app/code/community/Copernica/MarketingSoftware/Helper/ApiBase.php ADDED
@@ -0,0 +1,452 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 ADDED
@@ -0,0 +1,264 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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(
195
+ 'collection/'.$collectionId.'/fields',
196
+ array_merge($fieldDefinition, array(
197
+ 'name' => $copernicaName
198
+ ))
199
+ );
200
+ }
201
+
202
+ /**
203
+ * Get collection Id by it's name and database name that it's in.
204
+ * @param string database name
205
+ * @param string collection name
206
+ * @return numeric collection Id
207
+ * @throws Copernica_MarketingSoftware_Exception
208
+ */
209
+ private function getCollectionIdFromDatabase($databaseName, $collectionName)
210
+ {
211
+ // now we want to get collections from database
212
+ $output = $this->request()->get( 'database/'.urlencode($databaseName).'/collections' );
213
+
214
+ // got errors ?
215
+ if (isset($output['error'])) {
216
+ /*
217
+ * We want to detect if error says that database does not exist.
218
+ * Rest of API errors we will just output with general API_REQUEST_ERROR
219
+ * code, cause we don't really want to handle all possible api
220
+ * errors.
221
+ */
222
+ if (strpos($output['error']['message'], 'No database') !== false) throw Mage::exception('Copernica_MarketingSoftware', 'No database', Copernica_MarketingSoftware_Exception::DATABASE_NOT_EXISTS);
223
+ else throw Mage::exception('Copernica_MarketingSoftware', $output['message'], Copernica_MarketingSoftware_Exception::API_REQUEST_ERROR);
224
+ }
225
+
226
+ // if we don't have a proper output, we want to throw an exception, cause
227
+ // we can not do anything useful anyway.
228
+ else if (!is_array($output['data'])) {
229
+ throw Mage::exception('Copernica_MarketingSoftware', 'Unknown error' ,Copernica_MarketingSoftware_Exception::API_REQUEST_ERROR);
230
+ }
231
+
232
+ // collection Id
233
+ $collectionId = false;
234
+
235
+ // iterate over all collections to get one that we did just created
236
+ foreach ($output['data'] as $collection)
237
+ {
238
+ if ($collection['name'] == $collectionName) $collectionId = $collection['ID'];
239
+ }
240
+
241
+ // if we don't have collection id we want to break here
242
+ if ($collectionId === false) throw Mage::exception('Copernica_MarketingSoftware', 'Collection does not exists', Copernica_MarketingSoftware_Exception::COLLECTION_NOT_EXISTS);
243
+
244
+ // return proper collection Id
245
+ return $collectionId;
246
+ }
247
+
248
+ /**
249
+ * Get required fields for a collection of given type.
250
+ * @param string
251
+ * @return array
252
+ */
253
+ private function requiredCollectionFields($collectionType)
254
+ {
255
+ switch ($collectionType)
256
+ {
257
+ case 'cartproducts': return Mage::helper('marketingsoftware')->requiredCartItemFields();
258
+ case 'orders' : return Mage::helper('marketingsoftware')->requiredOrderFields();
259
+ case 'orderproducts': return Mage::helper('marketingsoftware')->requiredOrderItemFields();
260
+ case 'addresses': return Mage::helper('marketingsoftware')->requiredAddressFields();
261
+ case 'viewedproduct': return Mage::helper('marketingsoftware')->requiredViewedProductFields();
262
+ }
263
+ }
264
+ }
app/code/community/Copernica/MarketingSoftware/Helper/ApiValidator.php ADDED
@@ -0,0 +1,383 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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(
43
+ 'database/'.urlencode($databaseName)
44
+ );
45
+
46
+ // check if api say that we have a problem
47
+ if (isset($output['error']) || !isset($output['data']))
48
+ {
49
+ // check if Api tells that database does not exists or something else is wrong
50
+ if (strpos($output['error']['message'], 'No database') !== false)
51
+ throw Mage::exception('Copernica_MarketingSoftware', 'Database does not exists', Copernica_MarketingSoftware_Exception::DATABASE_NOT_EXISTS);
52
+ else
53
+ throw Mage::exception('Copernica_MarketingSoftware', 'Database could not be checked', Copernica_MarketingSoftware_Exception::API_REQUEST_ERROR);
54
+ }
55
+
56
+ // check if database have required fields
57
+ foreach ($output['fields']['data'] as $field) if ($field['name'] == 'customer_id') return;
58
+
59
+ // database does not have required field
60
+ throw Mage::exception('Copernica_MarketingSoftware', 'Database does not have required customer_id field', Copernica_MarketingSoftware_Exception::DATABASE_STRUCT_INVALID);
61
+ }
62
+
63
+ /**
64
+ * Validate database field.
65
+ * @param string the database name
66
+ * @param string the field name
67
+ * @throws Copernica_MarketingSoftware_Exception
68
+ */
69
+ public function validateDatabaseField($databaseName, $fieldName, $magentoField)
70
+ {
71
+ // if field name is just empty string then we can say that field is not linked at all
72
+ if (trim($fieldName) == '') throw Mage::exception('Copernica_MarketingSoftware', 'Field not linked', Copernica_MarketingSoftware_Exception::FIELD_NOT_LINKED);
73
+
74
+ // get database fields
75
+ $output = $this->request()->get('database/'.urlencode($databaseName).'/fields');
76
+
77
+ // check if api say that we have a problem
78
+ if (isset($output['error']))
79
+ {
80
+ // check if Api tells that database does not exists or something else is wrong
81
+ if (strpos($output['error']['message'], 'No database') !== false)
82
+ throw Mage::exception('Copernica_MarketingSoftware', 'Database does not exists', Copernica_MarketingSoftware_Exception::DATABASE_NOT_EXISTS);
83
+ else
84
+ throw Mage::exception('Copernica_MarketingSoftware', 'Database could not be checked', Copernica_MarketingSoftware_Exception::API_REQUEST_ERROR);
85
+ }
86
+
87
+ // iterate over all fields
88
+ foreach ($output['data'] as $field)
89
+ {
90
+ // do we have a proper field ?
91
+ if ($field['name'] != $fieldName) continue;
92
+
93
+ // if field have an invalid type throw an exception about that
94
+ if (!$this->checkDatabaseFieldType($magentoField, $field, $databaseName)) throw Mage::exception('Copernica_MarketingSoftware', 'Field is invalid', Copernica_MarketingSoftware_Exception::FIELD_STRUCT_INVALID);
95
+
96
+ // we are good
97
+ return;
98
+ }
99
+
100
+ // desired field does not exists
101
+ throw Mage::exception('Copernica_MarketingSoftware', 'Field does not exists', Copernica_MarketingSoftware_Exception::FIELD_NOT_EXISTS);
102
+ }
103
+
104
+ /**
105
+ * Validate collection.
106
+ * @param string database name
107
+ * @param string collection name
108
+ * @param string collection type
109
+ * @throws Copenica_MarketingSoftware_Exception
110
+ */
111
+ public function validateCollection($databaseName, $collectionName, $collectionType)
112
+ {
113
+ // get database structure
114
+ $output = $this->request()->get('database/'.$databaseName.'/collections');
115
+
116
+ // check if we have an error
117
+ if (isset($output['error']))
118
+ {
119
+ if (strpos($output['error']['message'], 'No database') !== false)
120
+ throw Mage::exception('Copernica_MarketingSoftware', 'Database does not exists', Copernica_MarketingSoftware_Exception::DATABASE_NOT_EXISTS);
121
+ else
122
+ throw Mage::exception('Copernica_MarketingSoftware', 'Collection could not be checked', Copernica_MarketingSoftware_Exception::API_REQUEST_ERROR);
123
+ }
124
+
125
+ // check if we have any kind of output
126
+ if ($output['total'] == 0) throw Mage::exception('Copernica_MarketingSoftware', 'Collection does not exists', Copernica_MarketingSoftware_Exception::COLLECTION_NOT_EXISTS);
127
+
128
+ // check all collection if we have a matching one
129
+ foreach ($output['data'] as $collection)
130
+ {
131
+ // skip if not a match
132
+ if ($collection['name'] != $collectionName) continue;
133
+
134
+ // check if collection have proper fields
135
+ if ($this->isCollectionFieldsValid($collection['fields'], $collectionType)) return;
136
+
137
+ // collection does not have all required fields, thus, invalid structure
138
+ throw Mage::exception('Copernica_MarketingSoftware', 'Collection structure is invalid.', Copernica_MarketingSoftware_Exception::COLLECTION_STRUCT_INVALID);
139
+ }
140
+
141
+ // we didn't found a valid collection...
142
+ throw Mage::exception('Copernica_MarketingSoftware', 'Collection does not exists', Copernica_MarketingSoftware_Exception::COLLECTION_NOT_EXISTS);
143
+ }
144
+
145
+ /**
146
+ * Check collection field structure.
147
+ * @param assoc can be an output from 'field' property from API
148
+ * @param string type of collection
149
+ * @return bool
150
+ */
151
+ private function isCollectionFieldsValid($collectionFieldStruct, $collectionType)
152
+ {
153
+ // get required fields types
154
+ $requiredFields = $this->requiredCollectionFields($collectionType);
155
+
156
+ // current collection fields
157
+ $collectionFields = array();
158
+
159
+ // iterater over all field
160
+ foreach ($collectionFieldStruct['data'] as $field)
161
+ {
162
+ // add field to collection fields
163
+ $collectionFields[] = $field['name'];
164
+ }
165
+
166
+ // check if collection have all required fields
167
+ if (count(array_intersect($collectionFields, $requiredFields)) == count($requiredFields)) return true;
168
+
169
+ // collection does not have all required fields so structure is invalid
170
+ return false;
171
+ }
172
+
173
+ /**
174
+ * Validate collection field.
175
+ * @param string the database name
176
+ * @param string the collection name
177
+ * @param string the collection type
178
+ * @param string magento field name
179
+ * @param string copernica field name
180
+ * @throws Copernica_MarketingSoftware_Exception
181
+ */
182
+ public function validateCollectionField($databaseName, $collectionName, $collectionType, $magentoFieldName, $copernicaFieldName)
183
+ {
184
+ // get database collections
185
+ $output = $this->request()->get(
186
+ 'database/'.$databaseName.'/collections'
187
+ );
188
+
189
+ // check if rest output have an error
190
+ if (isset($output['error'])) {
191
+ // check if api is telling us that database does not exists
192
+ if (strpos($output['error']['message'], 'No database') !== false) throw Mage::exception('Copernica_MarketingSoftware', 'Database does not exists', Copernica_MarketingSoftware_Exception::DATABASE_NOT_EXISTS);
193
+
194
+ // something is wrong with request or connection
195
+ else throw Mage::exception('Copernica_MarketingSoftware', 'API error', Copernica_MarketingSoftware_Exception::API_REQUEST_ERROR);
196
+ }
197
+
198
+ // check if we have any collections
199
+ if ($output['total'] == 0) throw Mage::exception('Copernica_MarketingSoftware', 'Collection does not exists', Copernica_MarketingSoftware_Exception::COLLECTION_NOT_EXISTS);
200
+
201
+ // placeholder for collection Id
202
+ $collectionId = false;
203
+
204
+ // iterate over collections to find one that we need
205
+ foreach ($output['data'] as $collection)
206
+ {
207
+ if ($collection['name'] == $collectionName) $collectionId = $collection['ID'];
208
+ }
209
+
210
+ // check if we don't have collection Id
211
+ if ($collectionId === false) throw Mage::exception('Copernica_MarketingSoftware', 'Collection does not exits', Copernica_MarketingSoftware_Exception::COLLECTION_NOT_EXISTS);
212
+
213
+ // get all current collections fields
214
+ $output = $this->request()->get( 'collection/'.$collectionId.'/fields' );
215
+
216
+ // check if we have some kind of error from API
217
+ if (isset($output['error']) || !isset($output['data']))
218
+ {
219
+ /*
220
+ * We don't check if API is telling us that collection does not exists
221
+ * cause couple of linew above we did confirm that. So any error
222
+ * will be cause of API error or connection error.
223
+ */
224
+ throw Mage::exception('Copernica_MarketingSoftware', 'API error', Copernica_MarketingSoftware_Exception::API_REQUEST_ERROR);
225
+ }
226
+
227
+ // check if we have any fields
228
+ if ($output['total'] == 0)
229
+ {
230
+ throw Mage::exception('Copernica_MarketingSoftware', 'Field does not exists', Copernica_MarketingSoftware_Exception::FIELD_NOT_EXISTS);
231
+ }
232
+
233
+ // iterate over all fields and check matching one
234
+ foreach ($output['data'] as $field)
235
+ {
236
+ // skip not matching fields
237
+ if ($field['name'] != $copernicaFieldName) continue;
238
+
239
+ // get field definition
240
+ $fieldDefinition = Mage::helper('marketingsoftware/data')->getCollectionFieldDefinition($collectionType, $magentoFieldName);
241
+
242
+ /*
243
+ * Why we only check type of the field? Copernica REST API does
244
+ * output only ID, name, and type properties when we ask for collection
245
+ * field. Thus we can only validate type of the field and check
246
+ * if it's valid.
247
+ */
248
+ if ($fieldDefinition['type'] != $field['type'])
249
+ throw Mage::exception('Copernica_MarketingSoftware', 'Field has invalid structure. Field should have \''.$fieldDefinition['type'].'\' type.', Copernica_MarketingSoftware_Exception::FIELD_STRUCT_INVALID);
250
+
251
+ // if we are here that means field exists and have proper structure
252
+ return;
253
+ }
254
+
255
+ // we did not found a proper field
256
+ throw Mage::exception('Copernica_MarketingSoftware', 'Field does not exists', Copernica_MarketingSoftware_Exception::FIELD_NOT_EXISTS);
257
+ }
258
+
259
+ /**
260
+ * Get required fields for a collection of given type.
261
+ * @param string
262
+ * @return array
263
+ */
264
+ private function requiredCollectionFields($collectionType)
265
+ {
266
+ switch ($collectionType)
267
+ {
268
+ case 'cartproducts': return Mage::helper('marketingsoftware')->requiredCartItemFields();
269
+ case 'orders' : return Mage::helper('marketingsoftware')->requiredOrderFields();
270
+ case 'orderproducts': return Mage::helper('marketingsoftware')->requiredOrderItemFields();
271
+ case 'addresses': return Mage::helper('marketingsoftware')->requiredAddressFields();
272
+ case 'viewedproduct': return Mage::helper('marketingsoftware')->requiredViewedProductFields();
273
+
274
+ // by default we not require any collection fields
275
+ default: return array();
276
+ }
277
+ }
278
+
279
+ /**
280
+ * Check database field if copernica type match magento type.
281
+ * @param string Name of magento field
282
+ * @param assoc Assoc with structure of the field.
283
+ * @param string Database name that will be used to check the field.
284
+ * @return boolean
285
+ */
286
+ private function checkDatabaseFieldType($magentoField, $copernicaStructure, $databaseName)
287
+ {
288
+ switch ($magentoField) {
289
+ case 'email' : return $this->checkEmailField($copernicaStructure);
290
+ case 'newsletter': return $this->checkNewsletterField($copernicaStructure, $databaseName);
291
+ case 'birthdate': return $this->checkDateField($copernicaStructure);
292
+ case 'registrationDate': return $this->checkDatetimeField($copernicaStructure);
293
+ default: return $this->checkDefaultField($copernicaStructure);
294
+ }
295
+ }
296
+
297
+ /**
298
+ * Check if newsletter field is correct
299
+ * @return boolean
300
+ */
301
+ private function checkNewsletterField($copernicaStructure, $databaseName)
302
+ {
303
+ // get info about unsubscribe options set on database
304
+ $databaseUnsubscribe = $this->request()->get('database/'.urlencode($databaseName).'/unsubscribe');
305
+
306
+ // if we have a general error we can not check the field
307
+ if (isset($databaseUnsubscribe['error'])) throw Mage::exception('Copernica_MarketingSoftware', 'Field could not be checked', Copernica_MarketingSoftware_Exception::API_REQUEST_ERROR);
308
+
309
+ // check if unsubbscibe behavior is set to update
310
+ if ($databaseUnsubscribe['behavior'] != 'update') return false;
311
+
312
+ // get the field name
313
+ $fieldName = $copernicaStructure['name'];
314
+
315
+ // if current field is not in unsub fields then field is wrong
316
+ if (!array_key_exists($fieldName, $databaseUnsubscribe['fields'])) return false;
317
+
318
+ // if new value is not fixed one then field is invalid
319
+ if ($databaseUnsubscribe['fields'][$fieldName] != 'unsubscribed_copernica') return false;
320
+
321
+ // get all callbacks tied to database
322
+ $databaseCallbacks = $this->request()->get('database/'.urlencode($databaseName).'/callbacks');
323
+
324
+ // if we have an error we want to throw an exception
325
+ if (isset($databaseCallbacks['error'])) throw Mage::exception('Copernica_MarketingSoftware', 'Field could not be checked', Copernica_MarketingSoftware_Exception::API_REQUEST_ERROR);
326
+
327
+ // get the magento callback url
328
+ $callbackUrl = Mage::helper('marketingsoftware')->unsubscribeCallbackUrl();
329
+
330
+ // do we have any callbacks?
331
+ if ($databaseCallbacks['total'] == 0) return false;
332
+
333
+ // we have to check all callbacks
334
+ foreach ($databaseCallbacks['data'] as $callback)
335
+ {
336
+ // check if we have a proper callback
337
+ if ($callback['url'] == $callbackUrl && $callback['method'] == 'json' && $callback['expression'] == "profile.$fieldName == 'unsubscribed_copernica';") return true;
338
+ }
339
+
340
+ // we didn't found a matching callback
341
+ return false;
342
+ }
343
+
344
+ /**
345
+ * Check field if it's a email field.
346
+ * @param assoc Assoc array that describes structure of the field
347
+ * @return boolean
348
+ */
349
+ private function checkEmailField($copernicaStructure)
350
+ {
351
+ return $copernicaStructure['type'] == 'email';
352
+ }
353
+
354
+ /**
355
+ * Check field if it's a date field.
356
+ * @param assoc Assoc array that describes structure of the field
357
+ * @return boolean
358
+ */
359
+ private function checkDateField($copernicaStructure)
360
+ {
361
+ return $copernicaStructure['type'] == 'date';
362
+ }
363
+
364
+ /**
365
+ * Check field if it's a datetime field
366
+ * @param assoc Assoc array that describes structure of the field
367
+ * @return boolean
368
+ */
369
+ private function checkDatetimeField($copernicaStructure)
370
+ {
371
+ return $copernicaStructure['type'] == 'datetime';
372
+ }
373
+
374
+ /**
375
+ * Check field if it's a default field.
376
+ * @param assoc Assoc array that describes structure of the field
377
+ * @return boolean
378
+ */
379
+ private function checkDefaultField($copernicaStructure)
380
+ {
381
+ return $copernicaStructure['type'] == 'text';
382
+ }
383
+ }
app/code/community/Copernica/MarketingSoftware/Helper/Config.php CHANGED
@@ -1,794 +1,922 @@
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 config helper
29
- *
30
- *
31
- */
32
- class Copernica_MarketingSoftware_Helper_Config extends Mage_Core_Helper_Abstract
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);
66
-
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]);
75
-
76
- break;
77
-
78
- case 'has':
79
- $key = $this->_toKeyName(substr($method, 3));
80
- return $this->_hasCustomConfig($key);
81
-
82
- break;
83
-
84
- case 'uns':
85
- $key = $this->_toKeyName(substr($method, 3));
86
- $model = $this->_getModel($key);
87
-
88
- if ($model !== false) {
89
- try {
90
- $model->delete();
91
-
92
- if (isset(self::$_configEntryCache[$key])) {
93
- self::$_configEntryCache[$key] = null;
94
- }
95
- } catch (Exception $e) {
96
- Mage::log('Marketingsoftware Config: ' . $e->getMessage());
97
- }
98
- }
99
-
100
- break;
101
- }
102
-
103
- return false;
104
- }
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
- {
113
- if (isset(self::$_configEntryCache[$key])) {
114
- return self::$_configEntryCache[$key];
115
- }
116
-
117
- $model = $this->_getModel($key);
118
- if ($model !== false) {
119
- return $model->getValue();
120
- }
121
-
122
- return null;
123
- }
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
- {
132
- $model = $this->_getModel($key);
133
-
134
- if ($model === false) {
135
- $model = Mage::getModel('marketingsoftware/config');
136
- }
137
-
138
- try {
139
- $model->setKeyName($key);
140
- $model->setValue($value);
141
- $model->save();
142
-
143
- self::$_configEntryCache[$key] = $model->getValue();
144
-
145
- return $model->getValue();
146
-
147
- } catch (Exception $e) {
148
- Mage::log('Marketingsoftware Config: ' . $e->getMessage());
149
- }
150
- }
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
- {
159
- return ((isset(self::$_configEntryCache[$key]) && !empty(self::$_configEntryCache[$key])) || ($this->_getModel($key) !== false));
160
- }
161
-
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
- {
170
- $model = Mage::getModel('marketingsoftware/config')->loadByKey($key);
171
-
172
- if ($model && $model->getId()) {
173
- self::$_configEntryCache[$key] = $model->getValue();
174
- return $model;
175
- }
176
-
177
- return false;
178
- }
179
-
180
- /**
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
- {
189
- if (isset(self::$_keyNameCache[$name])) {
190
- return self::$_keyNameCache[$name];
191
- }
192
-
193
- $result = strtolower(preg_replace('/(.)([A-Z])/', "$1_$2", $name));
194
-
195
- self::$_keyNameCache[$name] = $result;
196
- return $result;
197
- }
198
-
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
- {
206
- if ($this->_hasCustomConfig($name)) {
207
- return $this->_getCustomConfig($name);
208
- } else {
209
- // scope is added to the beginning of the path
210
- return (string) Mage::getConfig()->getNode(self::CONFIG_BASE . $name, 'default', 0);
211
- }
212
- }
213
-
214
- /**
215
- * Set the config item from the basic magento component
216
- * @param string $name Name of the config parameter
217
- * @param string $value Value that should be stored in the config
218
- */
219
- protected function _setConfig($name, $value)
220
- {
221
- // is this value new the same as the existing value
222
- if ($value === $this->_getConfig($name)) return;
223
-
224
- // Store the value in the custom config
225
- $this->_setCustomConfig($name, $value);
226
-
227
- // some config items are not that interesting
228
- if (in_array($name, array(
229
- 'customer_progress_status',
230
- 'order_progress_status',
231
- 'subscription_progress_status',
232
- 'cronjob_starttime',
233
- 'cronjob_endtime',
234
- 'cronjob_processedtasks',
235
- ))) return;
236
-
237
- // We have to reset the progress status
238
- $this->setCustomerProgressStatus('0');
239
- $this->setOrderProgressStatus('0');
240
- $this->setSubscriptionProgressStatus('0');
241
- }
242
-
243
- /**
244
- * Get the hostname from the config
245
- * @return String
246
- */
247
- public function getHostname()
248
- {
249
- return $this->_getConfig('hostname');
250
- }
251
-
252
- /**
253
- * Set the hostname from the config
254
- * @return String
255
- * @return Copernica_MarketingSoftware_Helper_Config
256
- */
257
- public function setHostname($value)
258
- {
259
- $this->_setConfig('hostname', $value);
260
- return $this;
261
- }
262
-
263
- /**
264
- * Get the username from the config
265
- * @return String
266
- */
267
- public function getUsername()
268
- {
269
- return $this->_getConfig('username');
270
- }
271
-
272
- /**
273
- * Set the username to the config
274
- * @param String
275
- * @return Copernica_MarketingSoftware_Helper_Config
276
- */
277
- public function setUsername($value)
278
- {
279
- $this->_setConfig('username', $value);
280
- return $this;
281
- }
282
-
283
- /**
284
- * Get the accountname from the config
285
- * @return String
286
- */
287
- public function getAccount()
288
- {
289
- return $this->_getConfig('account');
290
- }
291
-
292
- /**
293
- * Store the accountname in the config
294
- * @param String
295
- * @return Copernica_MarketingSoftware_Helper_Config
296
- */
297
- public function setAccount($value)
298
- {
299
- $this->_setConfig('account', $value);
300
- return $this;
301
- }
302
-
303
- /**
304
- * Get the password from the config
305
- * @return String
306
- */
307
- public function getPassword()
308
- {
309
- return $this->_getConfig('password');
310
- }
311
-
312
- /**
313
- * Set the password in the config
314
- * @param String
315
- */
316
- public function setPassword($value)
317
- {
318
- $this->_setConfig('password', $value);
319
- return $this;
320
- }
321
-
322
- /**
323
- * Get the name of the database
324
- * @return String
325
- */
326
- public function getDatabaseName()
327
- {
328
- return $this->_getConfig('database');
329
- }
330
-
331
- /**
332
- * Set the name of the database
333
- * @param String
334
- * @return Copernica_MarketingSoftware_Helper_Config
335
- */
336
- public function setDatabaseName($value)
337
- {
338
- $this->_setConfig('database', $value);
339
- return $this;
340
- }
341
-
342
- /**
343
- * Get the linked customer fields
344
- * @return array assoc array of fields which have been linked
345
- */
346
- public function getLinkedCustomerFields()
347
- {
348
- // Get the value
349
- $value = $this->_getConfig('linked_customer_fields');
350
-
351
- // What value is found?
352
- $value = empty($value) ? array() : json_decode($value, true);
353
-
354
- // is this an old data entry (prior to 1.2.0)
355
- if (!isset($value['customer_email'])) return $value;
356
-
357
- // yes this is old data... time for a small conversion
358
- $oldValues = $value;
359
- $newValues = array();
360
-
361
- // iterate over the data
362
- foreach ($oldValues as $key => $value)
363
- {
364
- $key = str_replace('customer_', '', $key);
365
- $newValues[$key] = $value;
366
- }
367
-
368
- // store the converted values
369
- $this->setLinkedCustomerFields($newValues);
370
-
371
- // return the new Values
372
- return $newValues;
373
- }
374
-
375
- /**
376
- * Set the linked customer fields
377
- * @param array assoc array of fields which have been linked
378
- * @return Copernica_MarketingSoftware_Helper_Config
379
- */
380
- public function setLinkedCustomerFields($value)
381
- {
382
- $this->_setConfig('linked_customer_fields', json_encode($value), true);
383
- return $this;
384
- }
385
-
386
- /**
387
- * Get the name of the not-ordered products collection
388
- * @return string
389
- */
390
- public function getCartItemsCollectionName()
391
- {
392
- return $this->_getConfig('cart_items_collection_name');
393
- }
394
-
395
- /**
396
- * Set the name of the not-ordered products collection
397
- * @param String
398
- * @return Copernica_MarketingSoftware_Helper_Config
399
- */
400
- public function setCartItemsCollectionName($value)
401
- {
402
- $this->_setConfig('cart_items_collection_name', $value);
403
- return $this;
404
- }
405
-
406
- /**
407
- * Get the linked customer fields
408
- * @return array assoc array of fields which have been linked
409
- */
410
- public function getLinkedCartItemFields()
411
- {
412
- $value = $this->_getConfig('linked_cart_item_fields');
413
-
414
- // What value is found?
415
- if (empty($value)) return array();
416
- else return json_decode($value, true);
417
- }
418
-
419
- /**
420
- * Get the linked customer fields
421
- * @param array assoc array of fields which have been linked
422
- * @return Copernica_MarketingSoftware_Helper_Config
423
- */
424
- public function setLinkedCartItemFields($value)
425
- {
426
- $this->_setConfig('linked_cart_item_fields', json_encode($value), true);
427
- return $this;
428
- }
429
-
430
- /**
431
- * Get the name of the orders collection
432
- * @return String
433
- */
434
- public function getOrdersCollectionName()
435
- {
436
- return $this->_getConfig('orders_collection_name');
437
- }
438
-
439
- /**
440
- * Set the name of the orders collection
441
- * @param String
442
- * @return Copernica_MarketingSoftware_Helper_Config
443
- */
444
- public function setOrdersCollectionName($value)
445
- {
446
- $this->_setConfig('orders_collection_name', $value);
447
- return $this;
448
- }
449
-
450
- /**
451
- * Get the linked order fields
452
- * @return array assoc array of fields which have been linked
453
- */
454
- public function getLinkedOrderFields()
455
- {
456
- // Get the value
457
- $value = $this->_getConfig('linked_order_fields');
458
-
459
- // What value is found?
460
- $value = empty($value) ? array() : json_decode($value, true);
461
-
462
- // is this an old data entry (prior to 1.2.0)
463
- if (!isset($value['order_timestamp'])) return $value;
464
-
465
- // yes this is old data... time for a small conversion
466
- $oldValues = $value;
467
- $newValues = array();
468
-
469
- // iterate over the data
470
- foreach ($oldValues as $key => $value)
471
- {
472
- // remove the order prefix and rename the qty field
473
- $key = ($key == 'order_qty') ? 'quantity' : str_replace('order_', '', $key);
474
- $newValues[$key] = $value;
475
- }
476
-
477
- // store the converted values
478
- $this->setLinkedOrderFields($newValues);
479
-
480
- // return the new Values
481
- return $newValues;
482
- }
483
-
484
- /**
485
- * Set the linked order fields
486
- * @param array assoc array of fields which have been linked
487
- * @return Copernica_MarketingSoftware_Helper_Config
488
- */
489
- public function setLinkedOrderFields($value)
490
- {
491
- $this->_setConfig('linked_order_fields', json_encode($value), true);
492
- return $this;
493
- }
494
-
495
- /**
496
- * Get the name of the collection were all the orders are stored
497
- * @return String
498
- * @return Copernica_MarketingSoftware_Helper_Config
499
- */
500
- public function getOrderItemsCollectionName()
501
- {
502
- return $this->_getConfig('order_items_collection_name');
503
- }
504
-
505
- /**
506
- * Get the name of the collection were all the orders are stored
507
- * @param String
508
- */
509
- public function setOrderItemsCollectionName($value)
510
- {
511
- $this->_setConfig('order_items_collection_name', $value);
512
- return $this;
513
- }
514
-
515
- /**
516
- * Get the linked order item fields
517
- * @return array assoc array of fields which have been linked
518
- */
519
- public function getLinkedOrderItemFields()
520
- {
521
- $value = $this->_getConfig('linked_order_item_fields');
522
-
523
- // What value is found?
524
- $value = empty($value) ? array() : json_decode($value, true);
525
-
526
- // is this an old data entry (prior to 1.2.0)
527
- if (!isset($value['product_internal_id'])) return $value;
528
-
529
- // yes this is old data... time for a small conversion
530
- $oldValues = $value;
531
- $newValues = array();
532
-
533
- // iterate over the data
534
- foreach ($oldValues as $key => $value)
535
- {
536
- // remove the order prefix and rename the qty field
537
- if ($key == 'product_qty') $key = 'quantity';
538
- elseif ($key == 'product_base_row_total') $key = 'total_price';
539
- else $key = str_replace('product_', '', $key);
540
-
541
- // assign it to the new values
542
- $newValues[$key] = $value;
543
- }
544
-
545
- // store the converted values
546
- $this->setLinkedOrderItemFields($newValues);
547
-
548
- // return the new Values
549
- return $newValues;
550
- }
551
-
552
- /**
553
- * Set the linked order item fields
554
- * @param array assoc array of fields which have been linked
555
- * @return Copernica_MarketingSoftware_Helper_Config
556
- */
557
- public function setLinkedOrderItemFields($value)
558
- {
559
- $this->_setConfig('linked_order_item_fields', json_encode($value), true);
560
- return $this;
561
- }
562
-
563
- /**
564
- * Get the address collection name
565
- * @return String
566
- */
567
- public function getAddressesCollectionName()
568
- {
569
- return $this->_getConfig('address_collection_name');
570
- }
571
-
572
- /**
573
- * Set the name of the collection with addresses
574
- */
575
- public function setAddressesCollectionName($value)
576
- {
577
- $this->_setConfig('address_collection_name', $value);
578
- return $this;
579
- }
580
-
581
- /**
582
- * Get the linked address fields
583
- * @return array assoc array of fields which have been linked
584
- */
585
- public function getLinkedAddressFields()
586
- {
587
- $value = $this->_getConfig('linked_address_fields');
588
-
589
- // What value is found?
590
- $value = empty($value) ? array() : json_decode($value, true);
591
-
592
- // is this an old data entry (prior to 1.2.0)
593
- if (!isset($value['address_firstname'])) return $value;
594
-
595
- // yes this is old data... time for a small conversion
596
- $oldValues = $value;
597
- $newValues = array();
598
-
599
- // iterate over the data
600
- foreach ($oldValues as $key => $value)
601
- {
602
- // remove the order prefix and rename the qty field
603
- $key = str_replace('address_', '', $key);
604
- $newValues[$key] = $value;
605
- }
606
-
607
- // store the converted values
608
- $this->setLinkedAddressFields($newValues);
609
-
610
- // return the new Values
611
- return $newValues;
612
- }
613
-
614
- /**
615
- * set the linked address fields
616
- * @param array assoc array of fields which have been linked
617
- * @return Copernica_MarketingSoftware_Helper_Config
618
- */
619
- public function setLinkedAddressFields($value)
620
- {
621
- $this->_setConfig('linked_address_fields', json_encode($value), true);
622
- return $this;
623
- }
624
-
625
- /**
626
- * Get the name of the viewed products collection
627
- * @return string
628
- */
629
- public function getViewedProductCollectionName()
630
- {
631
- return $this->_getConfig('viewed_product_collection_name');
632
- }
633
-
634
- /**
635
- * Set the name of the viewed products collection
636
- * @param String
637
- * @return Copernica_MarketingSoftware_Helper_Config
638
- */
639
- public function setViewedProductCollectionName($value)
640
- {
641
- $this->_setConfig('viewed_product_collection_name', $value);
642
- return $this;
643
- }
644
-
645
- /**
646
- * Get the linked customer fields
647
- * @return array assoc array of fields which have been linked
648
- */
649
- public function getLinkedViewedProductFields()
650
- {
651
- $value = $this->_getConfig('linked_viewed_product_fields');
652
-
653
- // What value is found?
654
- if (empty($value)) return array();
655
- else return json_decode($value, true);
656
- }
657
-
658
- /**
659
- * Get the linked customer fields
660
- * @param array assoc array of fields which have been linked
661
- * @return Copernica_MarketingSoftware_Helper_Config
662
- */
663
- public function setLinkedViewedProductFields($value)
664
- {
665
- $this->_setConfig('linked_viewed_product_fields', json_encode($value), true);
666
- return $this;
667
- }
668
-
669
- /**
670
- * Get the progress status for customers
671
- * This is the created timestamp of the most recent customer which has
672
- * been queued for synchronisation
673
- * @return datetime
674
- */
675
- public function getCustomerProgressStatus()
676
- {
677
- return $this->_getConfig('customer_progress_status');
678
- }
679
-
680
- /**
681
- * Set the progress status for customers
682
- * This is the created timestamp of the most recent customer which has
683
- * been queued for synchronisation
684
- * @param datetime
685
- */
686
- public function setCustomerProgressStatus($value)
687
- {
688
- $this->_setConfig('customer_progress_status', $value);
689
- return $this;
690
- }
691
-
692
- /**
693
- * Get the progress status for orders
694
- * This is the created timestamp of the most recent order which has
695
- * been queued for synchronisation
696
- * @return datetime
697
- */
698
- public function getOrderProgressStatus()
699
- {
700
- return $this->_getConfig('order_progress_status');
701
- }
702
-
703
- /**
704
- * Set the progress status for orders
705
- * This is the created timestamp of the most recent order which has
706
- * been queued for synchronisation
707
- * @param datetime
708
- */
709
- public function setOrderProgressStatus($value)
710
- {
711
- $this->_setConfig('order_progress_status', $value);
712
- return $this;
713
- }
714
-
715
- /**
716
- * Get the progress status for subscriptions
717
- * This is the created timestamp of the most recent subscription which has
718
- * been queued for synchronisation
719
- * @return datetime
720
- */
721
- public function getSubscriptionProgressStatus()
722
- {
723
- return $this->_getConfig('subscription_progress_status');
724
- }
725
-
726
- /**
727
- * Set the progress status for subscriptions
728
- * This is the created timestamp of the most recent subscription which has
729
- * been queued for synchronisation
730
- * @param datetime
731
- */
732
- public function setSubscriptionProgressStatus($value)
733
- {
734
- $this->_setConfig('subscription_progress_status', $value);
735
- return $this;
736
- }
737
-
738
- /**
739
- * Get the last start time of the cronjob.
740
- * @return datetime
741
- */
742
- public function getLastStartTimeCronjob()
743
- {
744
- return $this->_getConfig('cronjob_starttime');
745
- }
746
-
747
- /**
748
- * Set the last start time of the cronjob.
749
- * @param datetime
750
- */
751
- public function setLastStartTimeCronjob($value)
752
- {
753
- $this->_setConfig('cronjob_starttime', $value);
754
- return $this;
755
- }
756
-
757
- /**
758
- * Get the last end time of the cronjob.
759
- * @return datetime
760
- */
761
- public function getLastEndTimeCronjob()
762
- {
763
- return $this->_getConfig('cronjob_endtime');
764
- }
765
-
766
- /**
767
- * Set the last end time of the cronjob.
768
- * @param datetime
769
- */
770
- public function setLastEndTimeCronjob($value)
771
- {
772
- $this->_setConfig('cronjob_endtime', $value, true);
773
- return $this;
774
- }
775
-
776
- /**
777
- * Get the number of processed records of the last cronjob run.
778
- * @return integer
779
- */
780
- public function getLastCronjobProcessedTasks()
781
- {
782
- return (int)$this->_getConfig('cronjob_processedtasks');
783
- }
784
-
785
- /**
786
- * Set the last end time of the cronjob.
787
- * @param integer
788
- */
789
- public function setLastCronjobProcessedTasks($value)
790
- {
791
- $this->_setConfig('cronjob_processedtasks', $value);
792
- return $this;
793
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
794
  }
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 config helper
29
+ *
30
+ * @todo check if this clas can be better...
31
+ */
32
+ class Copernica_MarketingSoftware_Helper_Config extends Mage_Core_Helper_Abstract
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);
66
+
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]);
75
+
76
+ break;
77
+
78
+ case 'has':
79
+ $key = $this->_toKeyName(substr($method, 3));
80
+ return $this->_hasCustomConfig($key);
81
+
82
+ break;
83
+
84
+ case 'uns':
85
+ $key = $this->_toKeyName(substr($method, 3));
86
+ $model = $this->_getModel($key);
87
+
88
+ if ($model !== false) {
89
+ try {
90
+ $model->delete();
91
+
92
+ if (isset(self::$_configEntryCache[$key])) {
93
+ self::$_configEntryCache[$key] = null;
94
+ }
95
+ } catch (Exception $e) {
96
+ Mage::log('Marketingsoftware Config: ' . $e->getMessage());
97
+ }
98
+ }
99
+
100
+ break;
101
+ }
102
+
103
+ return false;
104
+ }
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
+ {
113
+ if (isset(self::$_configEntryCache[$key])) {
114
+ return self::$_configEntryCache[$key];
115
+ }
116
+
117
+ $model = $this->_getModel($key);
118
+ if ($model !== false) {
119
+ return $model->getValue();
120
+ }
121
+
122
+ return null;
123
+ }
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
+ {
132
+ $model = $this->_getModel($key);
133
+
134
+ if ($model === false) {
135
+ $model = Mage::getModel('marketingsoftware/config');
136
+ }
137
+
138
+ try {
139
+ $model->setKeyName($key);
140
+ $model->setValue($value);
141
+ $model->save();
142
+
143
+ self::$_configEntryCache[$key] = $model->getValue();
144
+
145
+ return $model->getValue();
146
+
147
+ } catch (Exception $e) {
148
+ Mage::log('Marketingsoftware Config: ' . $e->getMessage());
149
+ }
150
+ }
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
+ {
159
+ return ((isset(self::$_configEntryCache[$key]) && !empty(self::$_configEntryCache[$key])) || ($this->_getModel($key) !== false));
160
+ }
161
+
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
+ {
170
+ $model = Mage::getModel('marketingsoftware/config')->loadByKey($key);
171
+
172
+ if ($model && $model->getId()) {
173
+ self::$_configEntryCache[$key] = $model->getValue();
174
+ return $model;
175
+ }
176
+
177
+ return false;
178
+ }
179
+
180
+ /**
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
+ {
189
+ if (isset(self::$_keyNameCache[$name])) {
190
+ return self::$_keyNameCache[$name];
191
+ }
192
+
193
+ $result = strtolower(preg_replace('/(.)([A-Z])/', "$1_$2", $name));
194
+
195
+ self::$_keyNameCache[$name] = $result;
196
+ return $result;
197
+ }
198
+
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
+ {
206
+ return $this->_getCustomConfig($name);
207
+
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',
233
+ 'subscription_progress_status',
234
+ 'cronjob_starttime',
235
+ 'cronjob_endtime',
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
+ {
280
+ return unserialize($this->_getConfig('enabled_stores'));
281
+ }
282
+
283
+ /**
284
+ * Get the hostname from the config
285
+ * @return String
286
+ */
287
+ public function getHostname()
288
+ {
289
+ return $this->_getConfig('hostname');
290
+ }
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
+ {
309
+ return $this->_getConfig('database');
310
+ }
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
+ {
658
+ return $this->_getConfig('customer_progress_status');
659
+ }
660
+
661
+ /**
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
+
673
+ /**
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()
680
+ {
681
+ return $this->_getConfig('order_progress_status');
682
+ }
683
+
684
+ /**
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
+
696
+ /**
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()
703
+ {
704
+ return $this->_getConfig('subscription_progress_status');
705
+ }
706
+
707
+ /**
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
+ {
725
+ return $this->_getConfig('cronjob_starttime');
726
+ }
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
+ {
744
+ return $this->_getConfig('cronjob_endtime');
745
+ }
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()
762
+ {
763
+ return (int)$this->_getConfig('cronjob_processedtasks');
764
+ }
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
+ {
794
+ return (bool)$this->_getConfig('vanilla_crons');
795
+ }
796
+
797
+ /**
798
+ * Get last timestamp that we used to check magento forgotten carts
799
+ * @return string
800
+ */
801
+ public function getAbandonedLastCheck()
802
+ {
803
+ $lastTimestamp = $this->_getConfig('lastAbandonedCheck');
804
+
805
+ return $lastTimestamp ? $lastTimestamp : '0000-00-00 00:00:00';
806
+ }
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
+ {
829
+ $this->_setConfig('abandondedTimeout', $timeout);
830
+
831
+ return $this;
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
+ {
853
+ $this->_setConfig('abandondedPeriod', $timeout);
854
+
855
+ return $this;
856
+ }
857
+
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
+
880
+ return $this;
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
+ }
891
+
892
+ /**
893
+ * This method will purge all data about linked collections.
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
  }
app/code/community/Copernica/MarketingSoftware/Helper/Data.php CHANGED
@@ -1,400 +1,436 @@
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
- /** The CopernicaError is required **/
28
- require_once(dirname(__FILE__).'/../Model/Error.php');
29
-
30
- /**
31
- * The base helper for the Copernica Marketingsoftware plug-in
32
- */
33
- class Copernica_MarketingSoftware_Helper_Data extends Mage_Core_Helper_Abstract
34
- {
35
- /**
36
- * Helper method returns all supported customer fields
37
- * @return array
38
- */
39
- public function supportedCustomerFields()
40
- {
41
- return array(
42
- 'gender' => 'Gender',
43
- 'firstname' => 'Firstname',
44
- 'middlename' => 'Middlename',
45
- 'lastname' => 'Lastname',
46
- 'email' => 'E-mail',
47
- 'group' => 'Customer group',
48
- 'newsletter' => 'Newsletter',
49
- 'store_view' => 'Store view',
50
- );
51
- }
52
-
53
- /**
54
- * Helper method returns all supported fields for
55
- * the cart item collection
56
- * @return array
57
- */
58
- public function supportedCartItemFields()
59
- {
60
- return array(
61
- 'product_id' => 'product id',
62
- 'name' => 'Product name',
63
- 'sku' => 'SKU',
64
- 'attribute_set' => 'Attribute set',
65
- 'weight' => 'Weight',
66
- 'quantity' => 'Quantity',
67
- 'price' => 'Price',
68
- 'timestamp' => 'Modified',
69
- 'store_view' => 'Store view',
70
- 'total_price' => 'Total price',
71
- 'url' => 'Details URL',
72
- 'image' => 'Image URL',
73
- 'categories' => 'Categories',
74
- 'options' => 'Product options',
75
- 'attributes' => 'Product Attributes',
76
- );
77
- }
78
-
79
- /**
80
- * Helper method returns all supported fields for
81
- * the order collection
82
- * @return array
83
- */
84
- public function supportedOrderFields()
85
- {
86
- return array(
87
- 'increment_id' => 'Increment id',
88
- 'timestamp' => 'Timestamp',
89
- 'quantity' => 'Quantity',
90
- //'subtotal' => 'Subtotal',
91
- 'shipping' => 'Shipping costs',
92
- 'total' => 'Order total',
93
- 'weight' => 'Total weight',
94
- 'currency' => 'Currency',
95
- 'status' => 'Order status',
96
- 'store_view' => 'Store view',
97
- 'remote_ip' => 'Order ip address',
98
- 'shipping_description' => 'Shipping description',
99
- 'payment_description' => 'Payment description',
100
- 'shipping_address_id' => 'Shipping Address id',
101
- 'billing_address_id' => 'Billing Address id',
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
- 'product_id' => 'product id',
114
- 'increment_id' => 'Increment id',
115
- 'name' => 'Product name',
116
- 'sku' => 'SKU',
117
- 'attribute_set' => 'Attribute set',
118
- 'weight' => 'Weight',
119
- 'quantity' => 'Quantity',
120
- 'price' => 'Price',
121
- 'timestamp' => 'Modified',
122
- 'store_view' => 'Store view',
123
- 'total_price' => 'Total price',
124
- 'url' => 'Details URL',
125
- 'image' => 'Image URL',
126
- 'categories' => 'Categories',
127
- 'options' => 'Product options',
128
- 'attributes' => 'Product Attributes',
129
- );
130
- }
131
-
132
- /**
133
- * Helper method returns all supported fields for
134
- * the address collection
135
- * @return array
136
- */
137
- public function supportedAddressFields()
138
- {
139
- return array(
140
- 'firstname' => 'Firstname',
141
- 'middlename' => 'Middlename',
142
- 'prefix' => 'Prefix',
143
- 'lastname' => 'Lastname',
144
- 'email' => 'E-mail',
145
- 'company' => 'Company',
146
- 'street' => 'Street',
147
- 'city' => 'City',
148
- 'state' => 'State',
149
- 'zipcode' => 'Zip code',
150
- 'country_id' => 'Country',
151
- 'telephone' => 'Telephone number',
152
- 'fax' => 'Fax number',
153
- );
154
- }
155
-
156
- /**
157
- * Helper method returns all supported fields for
158
- * the viewed product collection
159
- * @return array
160
- */
161
- public function supportedViewedProductFields()
162
- {
163
- return array(
164
- 'product_id' => 'product id',
165
- 'name' => 'Product name',
166
- 'sku' => 'SKU',
167
- 'attribute_set' => 'Attribute set',
168
- 'weight' => 'Weight',
169
- 'price' => 'Price',
170
- 'store_view' => 'Store view',
171
- 'total_price' => 'Total price',
172
- 'url' => 'Details URL',
173
- 'image' => 'Image URL',
174
- 'categories' => 'Categories',
175
- 'options' => 'Product options',
176
- 'attributes' => 'Product Attributes',
177
- 'timestamp' => 'Timestamp',
178
- );
179
- }
180
-
181
- /**
182
- * Get the version of this extension.
183
- *
184
- * @return string version number
185
- */
186
- public function getExtensionVersion()
187
- {
188
- // Get the config and return the version from the config
189
- $config = Mage::getConfig()->getModuleConfig('Copernica_MarketingSoftware')->asArray();
190
- return $config['version'];
191
- }
192
-
193
- /**
194
- * Check if there is a new version of the extension.
195
- *
196
- * @return boolean|Strubg Either false or the version number
197
- */
198
- public function checkNewVersion()
199
- {
200
- // if we cannot access remote URLs, don't return anything. A notice for this will already be shown
201
- if (!$this->checkURL()) return false;
202
-
203
- // version URL
204
- $url = 'http://www.copernica.com/magento_extension_version.txt';
205
-
206
- // get the version information from the URL
207
- if (ini_get('allow_url_fopen') == '1') $data = @file_get_contents($url);
208
- else
209
- {
210
- $ch = curl_init($url);
211
- curl_setopt($ch, CURLOPT_TIMEOUT, 5);
212
- curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
213
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
214
- $data = curl_exec($ch);
215
- curl_close($ch);
216
- }
217
-
218
- // trim the retrieved version
219
- $data = trim($data);
220
-
221
- // if the official version is smaller or equal to the current version, then we are ok
222
- if (version_compare($data, $this->getExtensionVersion()) <= 0) return false;
223
-
224
- // else, return the new available version for further processing
225
- return $data;
226
- }
227
-
228
- /**
229
- * Check if the connection URL does exists
230
- * and if it does exists, check to see if it's SOAP
231
- * @param string Connection URL (optional)
232
- * @return boolean
233
- */
234
- public function checkConnectionURL($connectionURL = null)
235
- {
236
- // construct the full SOAP url, based on the default url setting or a custom one
237
- if ($connectionURL) $url = $connectionURL."?SOAPAPI=WSDL";
238
- else $url = Mage::helper('marketingsoftware/config')->getHostname()."?SOAPAPI=WSDL";
239
-
240
- // validate the url either with file_get_contents and get_headers if it's available
241
- if (ini_get('allow_url_fopen') == '1')
242
- {
243
- // check to see if it's alive
244
- $checkURL = @file_get_contents($url, NULL, NULL, 0, 1);
245
-
246
- // check to see if it's SOAP
247
- if ($checkURL)
248
- {
249
- $contents = get_headers($url, 1);
250
- if($contents['Content-Type'] == 'text/xml') return true;
251
- else return false;
252
- }
253
- else return false;
254
- }
255
-
256
- // or validate via CURL
257
- else
258
- {
259
- // let CURL process our data
260
- $ch = curl_init($url);
261
- curl_setopt($ch, CURLOPT_TIMEOUT, 5);
262
- curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
263
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
264
- $data = curl_exec($ch);
265
- $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
266
- $contentType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
267
- curl_close($ch);
268
-
269
- // check to see if it's SOAP
270
- if($httpCode >= 200 && $httpCode < 300 && $contentType == 'text/xml') return true;
271
- else return false;
272
- }
273
- }
274
-
275
- /**
276
- * Get the url for the unsubscribe callback
277
- * @return String
278
- */
279
- public function unsubscribeCallbackUrl()
280
- {
281
- return Mage::getModel('core/url')->getUrl('copernica/unsubscribe/process', array());
282
- }
283
-
284
- /**
285
- * Check if SOAP is enabled
286
- * @return boolean|CopernicaError
287
- */
288
- public function checkSoap()
289
- {
290
- if (!extension_loaded('soap')) throw new CopernicaError(COPERNICAERROR_SOAPNOTENABLED);
291
- else return true;
292
- }
293
-
294
- /**
295
- * Check if we can use CURL or FOPEN
296
- * @return boolean|CopernicaError
297
- */
298
- public function checkURL()
299
- {
300
- if (!function_exists('curl_version') && !ini_get('allow_url_fopen')) throw new CopernicaError(COPERNICAERROR_CURLNOTENABLED);
301
- else return true;
302
- }
303
-
304
- /**
305
- * Does the queue contain the magic token, which indicates that the synchronisation
306
- * should be started?
307
- * @return boolean
308
- */
309
- public function isSynchronisationStartScheduled()
310
- {
311
- // Construct a new resource for this because caching fucks it all up
312
- $count = Mage::getResourceModel('marketingsoftware/queue_collection')
313
- ->addFieldToFilter('action', 'start_sync')
314
- ->getSize();
315
-
316
- // Reset the count
317
- return ($count > 0);
318
- }
319
-
320
- /**
321
- * Is the Copernica module enabled?
322
- * @return boolean
323
- */
324
- public function enabled()
325
- {
326
- // Get the setting from 'advanced/modules_disable_output/Copernica_MarketingSoftware'
327
- return (Mage::getConfig()->getNode('advanced/modules_disable_output/Copernica_MarketingSoftware', 'default', 0) == 0);
328
- }
329
-
330
- /**
331
- * Perform some default check to validate that the plug-in is configured correctly and working
332
- * like it should be working.
333
- * Note: this plug-in does not return anything but adds messages to the adminhtml session
334
- */
335
- public function validatePluginBehaviour()
336
- {
337
- // A new version is available
338
- if ($version = $this->checkNewVersion()) Mage::getSingleton('adminhtml/session')->addNotice("A new version ($version) of the Magento-Copernica extension is available. Click <a href='http://www.magentocommerce.com/magento-connect/copernica-marketing-software-8325.html'>here</a> to download it.");
339
-
340
- // Perform the checks, an exception might be thrown, not that in in this way we can show only one error
341
- try
342
- {
343
- $this->checkSoap();
344
- $this->checkUrl();
345
- }
346
- catch (Exception $e)
347
- {
348
- // Add the exception to the session
349
- Mage::getSingleton('adminhtml/session')->addException($e, (string)$e);
350
- }
351
-
352
- // Check the queue length and the oldest record to have an idea of the performance of the plug-in
353
- $collection = Mage::getResourceModel('marketingsoftware/queue_collection');
354
- $length = $collection->getSize();
355
- $oldestTimestamp = $collection->getQueueStartTime();
356
- $printableTime = Mage::helper('core')->formatDate($oldestTimestamp, 'short', true);
357
- $oldestRecordAge = is_null($oldestTimestamp) ? 0 : time() - strtotime($oldestTimestamp);
358
-
359
- // Is the queue length too big or are there old records
360
- if ($length > 100 || $oldestRecordAge > 60*60*24)
361
- {
362
- // A basic message should be added
363
- $message = "Note: Your Copernica database is not up-to-date at this moment.";
364
-
365
- // Is the length bigger or the oldest record in the queue of a long time
366
- if ($length > 100) $message .= " There is queue of $length local modifications waiting to be processed.";
367
- if ($oldestRecordAge > 60*60*24) $message .= " There is still a modification of $printableTime that is not synchronized with Copernica.";
368
-
369
- // A warning should be added because one of the two problems is the case
370
- Mage::getSingleton('adminhtml/session')->addWarning($message);
371
-
372
- // Is this problem caused by the copernica being unreachable, or the login being invalid
373
- try
374
- {
375
- $api = Mage::getSingleton('marketingsoftware/marketingsoftware')->api();
376
- $result = $api->check(true);
377
- }
378
- catch(Exception $e)
379
- {
380
- // No valid result has been retrieved
381
- $result = false;
382
-
383
- // An exception is found add it to the session
384
- Mage::getSingleton('adminhtml/session')->addException($e,(string)$e);
385
- }
386
- }
387
- }
388
-
389
- /**
390
- * Generates a unique customer ID based on the e-mail address and the storeview.
391
- *
392
- * @param string $email
393
- * @param string $storeview
394
- * @return string
395
- */
396
- public function generateCustomerId($email, $storeview)
397
- {
398
- return md5(strtolower($email) . $storeview);
399
- }
400
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ * The base helper for the Copernica Marketingsoftware plug-in
29
+ */
30
+ 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',
73
+ 'options' => 'Product options',
74
+ 'attributes' => 'Product Attributes',
75
+ );
76
+ }
77
+
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()
187
+ {
188
+ return array('customer_id');
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()
205
+ {
206
+ return array('order_id', 'quote_id');
207
+ }
208
+
209
+ /**
210
+ * Required fields for copernica order items collection
211
+ * @return array
212
+ */
213
+ public function requiredOrderItemFields()
214
+ {
215
+ return array('item_id', 'order_id');
216
+ }
217
+
218
+ /**
219
+ * Required fields for copernica address collection
220
+ * @return array
221
+ */
222
+ public function requiredAddressFields()
223
+ {
224
+ return array('address_id');
225
+ }
226
+
227
+ /**
228
+ * Required fields for copernica viewed products collection
229
+ * @return array
230
+ */
231
+ public function requiredViewedProductFields()
232
+ {
233
+ return array('product_id');
234
+ }
235
+
236
+ /**
237
+ * Get field definition by collection type and magento field name.
238
+ *
239
+ * Some of collections fields should have special definitions. That is mostly
240
+ * caused by meaning of data. Fox emaple when there is an email field we
241
+ * want to be able to recognize data from that field as email. So, in copernica
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 (
255
+ 'type' => 'datetime'
256
+ ),
257
+ 'url' => array (
258
+ 'type' => 'text',
259
+ 'length' => 255
260
+ ),
261
+ 'image' => array (
262
+ 'type' => 'text',
263
+ 'length' => 255
264
+ ),
265
+ 'categories' => array (
266
+ 'type' => 'text',
267
+ 'length' => 255,
268
+ 'textlines' => 4,
269
+ 'lines' => 4
270
+ ),
271
+ 'storeView' => array (
272
+ 'type' => 'text',
273
+ 'length' => 100
274
+ )
275
+ ),
276
+ 'orderproducts' => array (
277
+ 'timestamp' => array (
278
+ 'type' => 'datetime'
279
+ ),
280
+ 'url' => array (
281
+ 'type' => 'text',
282
+ 'length' => 255
283
+ ),
284
+ 'image' => array (
285
+ 'type' => 'text',
286
+ 'length' => 255
287
+ ),
288
+ 'categories' => array (
289
+ 'type' => 'text',
290
+ 'length' => 255,
291
+ 'textlines' => 4,
292
+ 'lines' => 4
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 (
314
+ 'timestamp' => array (
315
+ 'type' => 'datetime'
316
+ ),
317
+ 'url' => array (
318
+ 'type' => 'text',
319
+ 'length' => 255
320
+ ),
321
+ 'image' => array (
322
+ 'type' => 'text',
323
+ 'length' => 255
324
+ ),
325
+ 'categories' => array (
326
+ 'type' => 'text',
327
+ 'length' => 255,
328
+ 'textlines' => 4,
329
+ 'lines' => 4
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 (
348
+ 'timestamp' => array (
349
+ 'type' => 'datetime'
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',
363
+ 'length' => 255
364
+ )
365
+ ),
366
+ 'addresses' => array (
367
+ 'email' => array (
368
+ 'type' => 'email',
369
+ ),
370
+ 'telephone' => array (
371
+ 'type' => 'phone_gsm'
372
+ ),
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
+ {
408
+ return Mage::getModel('core/url')->getUrl('copernica/unsubscribe/process', array());
409
+ }
410
+
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
+ }
app/code/community/Copernica/MarketingSoftware/Helper/DataWriter.php ADDED
@@ -0,0 +1,255 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 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
+
108
+ /**
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
+ }
172
+
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
+ /**
221
+ * Clear all data files.
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
+ }
app/code/community/Copernica/MarketingSoftware/Helper/Profile.php ADDED
@@ -0,0 +1,246 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 help with customer-profile relation.
29
+ *
30
+ * Profiles in copernica plarform represents either a customer in store view,
31
+ * or email in store view. It's important to understand that there could be more
32
+ * than one profile for one customer. It's cause one customer can be in more
33
+ * than one store view. Same applies to email addresses cause of same reasons.
34
+ */
35
+ 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 )
232
+ );
233
+ }
234
+
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
+ }
246
+ }
app/code/community/Copernica/MarketingSoftware/Helper/RESTRequest.php ADDED
@@ -0,0 +1,370 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ * We use normal PHP constructor cause Helpers are not childs of
42
+ * Varien_Object class, so no _construct is called.
43
+ */
44
+ public function __construct()
45
+ {
46
+ // get config into local scope
47
+ $config = Mage::helper('marketingsoftware/config');
48
+
49
+ // if we have a hostname in config we will use it
50
+ if ($hostname = $config->getApiHostname()) $this->hostname = $hostname;
51
+
52
+ // if we don't have a hostname in config we will use default one, and
53
+ // set the config
54
+ else {
55
+ // use default one
56
+ $this->hostname = 'https://api.copernica.com';
57
+
58
+ // set default hostname in config
59
+ $config->setApiHostname($this->hostname);
60
+ }
61
+
62
+ // try to get access token from configuration
63
+ $accessToken = $config->getAccessToken();
64
+
65
+ // check if we have a valid access token
66
+ if ($accessToken) $this->accessToken = $accessToken;
67
+ }
68
+
69
+ /**
70
+ * Destructor for this object.
71
+ */
72
+ public function __destruct()
73
+ {
74
+ // if we have an active curl interface we should release resource.
75
+ if (!is_null($this->multi)) $this->commit();
76
+ }
77
+
78
+ /**
79
+ * Check request instance. This method will check all essentials to make an
80
+ * API call.
81
+ * @return bool
82
+ */
83
+ public function check ()
84
+ {
85
+ // check if we have access token
86
+ if (empty($this->accessToken)) return false;
87
+
88
+ // check if we have a hostname
89
+ if (empty($this->hostname)) return false;
90
+
91
+ // seems that everything is just peachy
92
+ return true;
93
+ }
94
+
95
+ /**
96
+ * Helper method to build up a query string
97
+ * @param assoc
98
+ * @return string
99
+ */
100
+ protected function buildQueryString($data)
101
+ {
102
+ // start result parts
103
+ $parts = array();
104
+
105
+ // iterate over whole data
106
+ foreach ($data as $key => $value)
107
+ {
108
+ // check if our parameter is an array
109
+ if (is_array($value))
110
+ {
111
+ // iterate over all value items
112
+ foreach ($value as $valueItem) {
113
+ $parts[] = $key.'[]='.urlencode(strval($valueItem));
114
+ }
115
+ }
116
+
117
+ // if we don't have an array we can just use string value
118
+ else $parts[] = $key.'='.urlencode(strval($value));
119
+ }
120
+
121
+ // return result
122
+ return '?'.implode('&', $parts);
123
+ }
124
+
125
+ /**
126
+ * Make a GET requst
127
+ * @param string Request string
128
+ * @param assoc (Optional) Data to be passed with request
129
+ * @return assoc Decoded JSON from
130
+ */
131
+ public function get($request, $data = null)
132
+ {
133
+ // reset curl options
134
+ $curl = curl_init();
135
+
136
+ // if we have access token then we want to append it to request
137
+ if ($this->accessToken) $request.=$this->buildQueryString(array_merge(array(
138
+ 'access_token' => $this->accessToken),
139
+ is_null($data) ? array() : $data
140
+ ));
141
+
142
+ // well, we don't have an access token
143
+ else $request.=$this->buildQueryString(is_null($data) ? array() : $data);
144
+
145
+ // set url that we want to receive
146
+ curl_setopt($curl, CURLOPT_URL, $this->hostname.'/'.$request);
147
+
148
+ // we want to get response from API
149
+ curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
150
+
151
+ // decode output
152
+ $output = json_decode(curl_exec($curl), true);
153
+
154
+ // close curl
155
+ curl_close($curl);
156
+
157
+ // get the output
158
+ return $output;
159
+ }
160
+
161
+ /**
162
+ * Make a POST request
163
+ * @param string Request string
164
+ * @param assoc (Optional) Data to be passed with request
165
+ */
166
+ public function post($request, $data = null, $query = null)
167
+ {
168
+ // check if we have any parameters
169
+ if (is_array($query))
170
+ {
171
+ $request.= $this->buildQueryString(array_merge( $query, array('access_token' => $this->accessToken) ));
172
+ }
173
+
174
+ // append access token to our request
175
+ else $request.='?access_token='.$this->accessToken;
176
+
177
+ // create curl
178
+ $curl = curl_init();
179
+
180
+ // set url that we want to receive
181
+ curl_setopt($curl, CURLOPT_URL, $this->hostname.'/'.$request);
182
+
183
+ // we want to make POST
184
+ curl_setopt($curl, CURLOPT_POST, true);
185
+
186
+ // set custom method
187
+ curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
188
+
189
+ // set HTTP headers
190
+ curl_setopt($curl, CURLOPT_HTTPHEADER, array(
191
+ 'content-type: application/json',
192
+ 'accept: application/json'
193
+ ));
194
+
195
+ // set data
196
+ curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
197
+
198
+ // try to append to multi interface
199
+ if ($this->appendMulti($curl)) return true;
200
+
201
+ // execute
202
+ curl_exec($curl);
203
+
204
+ // close curl
205
+ curl_close($curl);
206
+
207
+ // allow chaining
208
+ return true;
209
+ }
210
+
211
+ /**
212
+ * Make a PUT request
213
+ * @param string Request string
214
+ * @param assoc (Optional) Data to be passed with request
215
+ */
216
+ public function put($request, $data = null, $query = null)
217
+ {
218
+ // check if we have any parameters
219
+ if (is_array($query))
220
+ {
221
+ $request.= $this->buildQueryString(array_merge( $query, array('access_token' => $this->accessToken) ));
222
+ }
223
+
224
+ // append access token to our request
225
+ else $request.='?access_token='.$this->accessToken;
226
+
227
+ // reset curl options
228
+ $curl = curl_init();
229
+
230
+ // set url that we want to receive
231
+ curl_setopt($curl, CURLOPT_URL, $this->hostname.'/'.$request);
232
+
233
+ // we want to make POST
234
+ curl_setopt($curl, CURLOPT_POST, true);
235
+
236
+ // set custom method
237
+ curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
238
+
239
+ // set HTTP headers
240
+ curl_setopt($curl, CURLOPT_HTTPHEADER, array(
241
+ 'content-type: application/json',
242
+ 'accept: application/json'
243
+ ));
244
+
245
+ // set data
246
+ curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
247
+
248
+ // we want to get the return
249
+ // curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
250
+
251
+ // try to append to multi interface
252
+ if ($this->appendMulti($curl)) return true;
253
+
254
+ // execute
255
+ curl_exec($curl);
256
+
257
+ // close curl
258
+ curl_close($curl);
259
+
260
+ // allow chaining
261
+ return true;
262
+ }
263
+
264
+ /**
265
+ * Make a DELETE request
266
+ * @param string Request string
267
+ * @param assoc (Optional) Data to be passed with request
268
+ */
269
+ public function delete($request, $data = null)
270
+ {
271
+ // reset curl options
272
+ $curl = curl_init();
273
+
274
+ // append access token to our request
275
+ $request.='?access_token='.$this->accessToken;
276
+
277
+ // set url that we want to receive
278
+ curl_setopt($curl, CURLOPT_URL, $this->hostname.'/'.$request);
279
+
280
+ // we want to set custom request
281
+ curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE");
282
+
283
+ // we want all communication in json
284
+ curl_setopt($curl, CURLOPT_HTTPHEADER, array(
285
+ 'content-type: application/json',
286
+ 'accept: application/json'
287
+ ));
288
+
289
+
290
+ // try to append to multi interface
291
+ if ($this->appendMulti($curl)) return true;
292
+
293
+ // execure curl request
294
+ curl_exec($curl);
295
+
296
+ // close curl
297
+ curl_close($curl);
298
+
299
+ // return output
300
+ return true;
301
+ }
302
+
303
+ /**
304
+ * This method will start preparing calls to execute them later on with
305
+ * 'multi' interface.
306
+ */
307
+ public function prepare()
308
+ {
309
+ // init multi interface
310
+ $this->multi = curl_multi_init();
311
+
312
+ return $this;
313
+ }
314
+
315
+ /**
316
+ * Commit all prepared calls
317
+ */
318
+ public function commit()
319
+ {
320
+ // is it active?
321
+ $active = true;
322
+
323
+ // if we don't have a multi interface handler we really don't want to process it
324
+ if (is_null($this->multi)) return $this;
325
+
326
+ /*
327
+ * Execute multi curl
328
+ */
329
+ do {
330
+ while (CURLM_CALL_MULTI_PERFORM === curl_multi_exec($this->multi, $running));
331
+ if (!$running) break;
332
+ while (($res = curl_multi_select($this->multi)) === 0) {};
333
+ if ($res === false) {
334
+ break;
335
+ }
336
+ } while(true);
337
+
338
+ // free all children
339
+ foreach($this->children as $child) curl_multi_remove_handle($this->multi, $child);
340
+
341
+ // clean up
342
+ curl_multi_close($this->multi);
343
+
344
+ // set multi interface to null
345
+ $this->multi = null;
346
+
347
+ // allow chaining
348
+ return $this;
349
+ }
350
+
351
+ /**
352
+ * Append another curl request to multi interface
353
+ */
354
+ private function appendMulti($curl)
355
+ {
356
+ if (!is_resource($this->multi)) return false;
357
+
358
+ $code = curl_multi_add_handle($this->multi, $curl);
359
+
360
+ if ($code == 0) {
361
+
362
+ $this->children[] = $curl;
363
+
364
+ return true;
365
+ }
366
+
367
+ return false;
368
+ }
369
+
370
+ }
app/code/community/Copernica/MarketingSoftware/Model/{Marketingsoftware.php → AbandonedCart.php} RENAMED
@@ -1,59 +1,63 @@
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_Model_MarketingSoftware extends Varien_Object
28
- {
29
- /**
30
- * Holds API object
31
- *
32
- * @var Copernica_MarketingSoftware_Helper_Api
33
- */
34
- protected $_api;
35
-
36
- /**
37
- * Get the API helper object with the settings as defined in the config.
38
- *
39
- * @return Copernica_MarketingSoftware_Helper_Api
40
- */
41
- public function api()
42
- {
43
- if (!$this->_api) {
44
- // Get the config helper
45
- $config = Mage::helper('marketingsoftware/config');
46
-
47
- // Get an api which is logged on on the right Copernica env
48
- $this->_api = Mage::helper('marketingsoftware/api')->init(
49
- $config->getHostname(),
50
- $config->getUsername(),
51
- $config->getAccount(),
52
- $config->getPassword()
53
- );
54
- }
55
-
56
- // Return the api object
57
- return $this->_api;
58
- }
 
 
 
 
59
  }
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 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;
53
+ }
54
+
55
+ /**
56
+ * Get quote Id
57
+ * @return int
58
+ */
59
+ public function getQuoteId()
60
+ {
61
+ return parent::getData('quote_id');
62
+ }
63
  }
app/code/community/Copernica/MarketingSoftware/Model/AbandonedCartsProcessor.php ADDED
@@ -0,0 +1,154 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ * Since magento does not have a valid way to detect proper abandoned carts, we
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
+ }
71
+
72
+ return $notMarkedQuotes;
73
+ }
74
+
75
+ /**
76
+ * This function will determine abandoned carts that we should
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')
141
+ ->setName('quote')
142
+ ->setEntityId($quote->getEntityId())
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
+ }
app/code/community/Copernica/MarketingSoftware/Model/Abstraction/Address.php CHANGED
@@ -1,302 +1,302 @@
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 Address
29
- */
30
- class Copernica_MarketingSoftware_Model_Abstraction_Address implements Serializable
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;
130
- }
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
- }
152
- }
153
-
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(),
264
- $this->name(),
265
- $this->email(),
266
- $this->street(),
267
- $this->city(),
268
- $this->zipcode(),
269
- $this->state(),
270
- $this->countryCode(),
271
- $this->telephone(),
272
- $this->fax(),
273
- $this->company(),
274
- is_object($customer = $this->customer()) ? $customer->id() : null,
275
- ));
276
- }
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
  }
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 Address
29
+ */
30
+ class Copernica_MarketingSoftware_Model_Abstraction_Address implements Serializable
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;
130
+ }
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
+ }
152
+ }
153
+
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(),
264
+ $this->name(),
265
+ $this->email(),
266
+ $this->street(),
267
+ $this->city(),
268
+ $this->zipcode(),
269
+ $this->state(),
270
+ $this->countryCode(),
271
+ $this->telephone(),
272
+ $this->fax(),
273
+ $this->company(),
274
+ is_object($customer = $this->customer()) ? $customer->id() : null,
275
+ ));
276
+ }
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
  }
app/code/community/Copernica/MarketingSoftware/Model/Abstraction/Attributes.php CHANGED
@@ -1,127 +1,127 @@
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 attributes
29
- */
30
- class Copernica_MarketingSoftware_Model_Abstraction_Attributes implements Serializable
31
- {
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(),
110
- ));
111
- }
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
- }
127
-
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 attributes
29
+ */
30
+ class Copernica_MarketingSoftware_Model_Abstraction_Attributes implements Serializable
31
+ {
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(),
110
+ ));
111
+ }
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
+ }
127
+
app/code/community/Copernica/MarketingSoftware/Model/Abstraction/Customer.php CHANGED
@@ -1,280 +1,303 @@
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 magento Customer
29
- */
30
- class Copernica_MarketingSoftware_Model_Abstraction_Customer implements Serializable
31
- {
32
- /**
33
- * Predefine the internal fields
34
- */
35
- protected $id;
36
- protected $name;
37
- protected $email;
38
- protected $oldemail;
39
- protected $subscription;
40
- protected $group;
41
- protected $addresses;
42
- protected $gender;
43
- protected $storeview;
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
- $this->id = $original->getId();
54
- $this->name = Mage::getModel('marketingsoftware/abstraction_name')->setOriginal($original);
55
- $this->email = $original->getEmail();
56
- $this->oldemail = $original->getOrigData('email');
57
- $this->group = Mage::getModel('customer/group')->load($original->getGroupId())->getCode();
58
- $this->storeview = Mage::getModel('marketingsoftware/abstraction_storeview')->setOriginal($original->getStore());
59
-
60
- $options = $original->getAttribute('gender')->getSource()->getAllOptions();
61
-
62
- foreach ($options as $option) {
63
- if ($option['value'] == $original->getGender()) {
64
- $this->gender = $option['label'];
65
- }
66
- }
67
-
68
- $subscriber = Mage::getModel('newsletter/subscriber');
69
- if ($subscriber->loadByCustomer($original)->getId()) {
70
- if ($subscriber->getStoreId() === $original->getStoreId()) {
71
- $this->subscription = Mage::getModel('marketingsoftware/abstraction_subscription')->setOriginal($subscriber);
72
- }
73
- }
74
-
75
- $data = array();
76
- //retrieve this customer's addresses
77
- $addresses = $original->getAddressesCollection();
78
- foreach ($addresses as $address) {
79
- $data[] = Mage::getModel('marketingsoftware/abstraction_address')->setOriginal($address);
80
- }
81
- $this->addresses = $data;
82
-
83
- return $this;
84
- }
85
-
86
- /**
87
- * Returns the original model
88
- * @return Mage_Customer_Model_Customer
89
- */
90
- protected function original()
91
- {
92
- return $this->original;
93
- }
94
-
95
- /**
96
- * Loads a customer model
97
- * @param integer $customerId
98
- * @return Copernica_MarketingSoftware_Model_Abstraction_Storeview
99
- */
100
- public function loadCustomer($customerId)
101
- {
102
- $customer = Mage::getModel('customer/customer')->load($customerId);
103
- if ($customer->getId()) {
104
- //set the original model if the customer exists
105
- $this->setOriginal($customer);
106
- } else {
107
- // We did load a customer to make sure that it works more
108
- // or less, we assign the customer id here
109
- $this->id = $customerId;
110
- }
111
- return $this;
112
- }
113
-
114
- /**
115
- * Return the id of the customer
116
- * @return string
117
- */
118
- public function id()
119
- {
120
- return $this->id;
121
- }
122
-
123
- /**
124
- * Return the name of this customer
125
- * Note that null may also be returned to indicate that the name is not known
126
- * @return Copernica_MarketingSoftware_Model_Abstraction_Name
127
- */
128
- public function name()
129
- {
130
- return $this->name;
131
- }
132
-
133
- /**
134
- * Return the e-mail address of the customer
135
- * @return string
136
- */
137
- public function email()
138
- {
139
- return $this->email;
140
- }
141
-
142
- /**
143
- * Method to retrieve the previous email if possible
144
- * Falls back on self::email()
145
- *
146
- * @return string
147
- */
148
- public function oldEmail()
149
- {
150
- if (isset($this->oldemail)) {
151
- return $this->oldemail;
152
- } else {
153
- return $this->email();
154
- }
155
- }
156
-
157
- /**
158
- * Returns the gender
159
- * @return string
160
- */
161
- public function gender()
162
- {
163
- return $this->gender;
164
- }
165
-
166
- /**
167
- * Return the subscription of the customer
168
- * @return Copernica_MarketingSoftware_Model_Abstraction_Subscription
169
- */
170
- public function subscription()
171
- {
172
- return $this->subscription;
173
- }
174
-
175
- /**
176
- * Return the group to which this customer belongs
177
- * @return string
178
- */
179
- public function group()
180
- {
181
- return $this->group;
182
- }
183
-
184
- /**
185
- * Get the quotes for this customer
186
- * @return array of Copernica_MarketingSoftware_Model_Abstraction_Quote
187
- */
188
- public function quotes()
189
- {
190
- $data = array();
191
-
192
- //retrieve this customer's quote ids
193
- $quoteIds = Mage::getResourceModel('sales/quote_collection')
194
- ->addFieldToFilter('customer_id', $this->id())->getAllIds();
195
-
196
- foreach ($quoteIds as $id) {
197
- $data[] = Mage::getModel('marketingsoftware/abstraction_quote')->loadQuote($id);
198
- }
199
- return $data;
200
- }
201
-
202
- /**
203
- * Get the orders for this customer
204
- * @return array of Copernica_MarketingSoftware_Model_Abstraction_Order
205
- */
206
- public function orders()
207
- {
208
- $data = array();
209
-
210
- //retrieve this customer's order ids
211
- $orderIds = Mage::getResourceModel('sales/order_collection')
212
- ->addAttributeToFilter('customer_id', $this->id())->getAllIds();
213
-
214
- foreach ($orderIds as $id) {
215
- $data[] = Mage::getModel('marketingsoftware/abstraction_order')->loadOrder($id);
216
- }
217
- return $data;
218
- }
219
-
220
- /**
221
- * Get the addresses for this customer
222
- * @return array of Copernica_MarketingSoftware_Model_Abstraction_Address
223
- */
224
- public function addresses()
225
- {
226
- return $this->addresses;
227
- }
228
-
229
- /**
230
- * To what storeview does this order belong
231
- * @return Copernica_MarketingSoftware_Model_Abstraction_Storeview
232
- */
233
- public function storeview()
234
- {
235
- return $this->storeview;
236
- }
237
-
238
- /**
239
- * Serialize the object
240
- * @return string
241
- */
242
- public function serialize()
243
- {
244
- // serialize the data
245
- return serialize(array(
246
- $this->id(),
247
- $this->name(),
248
- $this->email(),
249
- $this->oldEmail(),
250
- $this->subscription(),
251
- $this->group(),
252
- $this->addresses(),
253
- $this->gender(),
254
- $this->storeview(),
255
- ));
256
- }
257
-
258
- /**
259
- * Unserialize the object
260
- * @param string
261
- * @return Copernica_MarketingSoftware_Model_Abstraction_Customer
262
- */
263
- public function unserialize($string)
264
- {
265
- // assign the data to the internal vars
266
- list(
267
- $this->id,
268
- $this->name,
269
- $this->email,
270
- $this->oldemail,
271
- $this->subscription,
272
- $this->group,
273
- $this->addresses,
274
- $this->gender,
275
- $this->storeview
276
- ) = unserialize($string);
277
-
278
- return $this;
279
- }
280
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ * @documentation public
26
+ */
27
+
28
+ /**
29
+ * A wrapper object around a magento Customer
30
+ */
31
+ class Copernica_MarketingSoftware_Model_Abstraction_Customer implements Serializable
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
+ }
app/code/community/Copernica/MarketingSoftware/Model/Abstraction/Name.php CHANGED
@@ -1,123 +1,123 @@
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 name, note this is not an Magento object
29
- */
30
- class Copernica_MarketingSoftware_Model_Abstraction_Name implements Serializable
31
- {
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(),
103
- $this->middlename(),
104
- $this->lastname(),
105
- ));
106
- }
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
-
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 name, note this is not an Magento object
29
+ */
30
+ class Copernica_MarketingSoftware_Model_Abstraction_Name implements Serializable
31
+ {
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(),
103
+ $this->middlename(),
104
+ $this->lastname(),
105
+ ));
106
+ }
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
+
app/code/community/Copernica/MarketingSoftware/Model/Abstraction/Order.php CHANGED
@@ -1,347 +1,333 @@
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 Order
29
- */
30
- class Copernica_MarketingSoftware_Model_Abstraction_Order implements Serializable
31
- {
32
- /**
33
- * Getting payment name does not work with Klarna. Klarna gets the
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
- $this->id = $original->getId();
70
- $this->incrementId = $original->getIncrementId();
71
- $this->quoteId = $original->getQuoteId();
72
- $this->state = $original->getState();
73
- $this->status = $original->getStatus();
74
- $this->quantity = $original->getTotalQtyOrdered();
75
- $this->currency = $original->getOrderCurrencyCode();
76
- $this->price = Mage::getModel('marketingsoftware/abstraction_price')->setOriginal($original);
77
- $this->weight = $original->getWeight();
78
- $this->storeview = Mage::getModel('marketingsoftware/abstraction_storeview')->setOriginal($original->getStore());
79
- $this->timestamp = $original->getUpdatedAt();
80
- $this->shippingDescription = $original->getShippingDescription();
81
- $this->customerIP = $original->getRemoteIp();
82
-
83
- $data = array();
84
- $items = $original->getAllVisibleItems();
85
- foreach ($items as $item) {
86
- $data[] = Mage::getModel('marketingsoftware/abstraction_order_item')->setOriginal($item);
87
- }
88
- $this->items = $data;
89
-
90
- //the order model only returns a customer if it exists
91
- if ($customerId = $original->getCustomerId()) {
92
- $this->customerId = $customerId;
93
- }
94
-
95
- $data = array();
96
- //retrieve this quote's addresses
97
- $addresses = $original->getAddressesCollection();
98
- foreach ($addresses as $address) {
99
- $data[] = Mage::getModel('marketingsoftware/abstraction_address')->setOriginal($address);
100
- }
101
- $this->addresses = $data;
102
-
103
- if ($payment = $original->getPayment()) {
104
- try {
105
- if ($payment->getMethod() == self::PAYMENT_METHOD_KLARNA) {
106
- $this->paymentDescription = 'Klarna';
107
- } else {
108
- $this->paymentDescription = $payment->getMethodInstance()->getTitle();
109
- }
110
- } catch (Mage_Core_Exception $exception) { }
111
- }
112
-
113
- return $this;
114
- }
115
-
116
- /**
117
- * Loads an order model
118
- * @param integer $orderId
119
- * @return Copernica_MarketingSoftware_Model_Abstraction_Order
120
- */
121
- public function loadOrder($orderId)
122
- {
123
- $order = Mage::getModel('sales/order')->load($orderId);
124
- if ($order->getId()) {
125
- //set the original model if the quote exists
126
- $this->setOriginal($order);
127
- }
128
- return $this;
129
- }
130
-
131
- /**
132
- * The id of this order object
133
- * @return integer
134
- */
135
- public function id()
136
- {
137
- return $this->id;
138
- }
139
-
140
- /**
141
- * The increment (longer) id of this order object
142
- * @return integer
143
- */
144
- public function incrementId()
145
- {
146
- return $this->incrementId;
147
- }
148
-
149
- /**
150
- * The quote id of this order object
151
- * @return integer
152
- */
153
- public function quoteId()
154
- {
155
- return $this->quoteId;
156
- }
157
-
158
- /**
159
- * The state of this order
160
- * @return string
161
- */
162
- public function state()
163
- {
164
- return $this->state;
165
- }
166
-
167
- /**
168
- * The status of this order
169
- * @return string
170
- */
171
- public function status()
172
- {
173
- return $this->status;
174
- }
175
-
176
- /**
177
- * The number of items present in this order
178
- * @return integer
179
- */
180
- public function quantity()
181
- {
182
- return $this->quantity;
183
- }
184
-
185
- /**
186
- * The number of items present in this order
187
- * @return integer
188
- */
189
- public function currency()
190
- {
191
- return $this->currency;
192
- }
193
-
194
- /**
195
- * The price
196
- * Note that an object is returned, which may consist of multiple components
197
- * @return Copernica_MarketingSoftware_Model_Abstraction_Price
198
- */
199
- public function price()
200
- {
201
- return $this->price;
202
- }
203
-
204
- /**
205
- * The weight
206
- * @return float
207
- */
208
- public function weight()
209
- {
210
- return $this->weight;
211
- }
212
-
213
- /**
214
- * To what storeview does this order belong
215
- * @return Copernica_MarketingSoftware_Model_Abstraction_Storeview
216
- */
217
- public function storeview()
218
- {
219
- return $this->storeview;
220
- }
221
-
222
- /**
223
- * Get the items from the order
224
- * @return array of Copernica_MarketingSoftware_Model_Abstraction_Quote_Item
225
- */
226
- public function items()
227
- {
228
- return $this->items;
229
- }
230
-
231
- /**
232
- * The timestamp at which this order was modified
233
- * @return string
234
- */
235
- public function timestamp()
236
- {
237
- return $this->timestamp;
238
- }
239
-
240
- /**
241
- * The customer may return null
242
- * @return Copernica_MarketingSoftware_Model_Abstraction_Customer
243
- */
244
- public function customer()
245
- {
246
- if ($this->customerId) {
247
- return Mage::getModel('marketingsoftware/abstraction_customer')->loadCustomer($this->customerId);
248
- } else {
249
- return null;
250
- }
251
- }
252
-
253
- /**
254
- * The addresses of the order
255
- * @return array of Copernica_MarketingSoftware_Model_Abstraction_Address
256
- */
257
- public function addresses()
258
- {
259
- return $this->addresses;
260
- }
261
-
262
- /**
263
- * The shipping method of the order
264
- * @return string
265
- */
266
- public function shippingDescription()
267
- {
268
- return $this->shippingDescription;
269
- }
270
-
271
- /**
272
- * The payment method of the order
273
- * @return string
274
- */
275
- public function paymentDescription()
276
- {
277
- return $this->paymentDescription;
278
- }
279
-
280
- /**
281
- * The IP from which this order was constructed
282
- * @return string
283
- */
284
- public function customerIP()
285
- {
286
- return $this->customerIP;
287
- }
288
-
289
- /**
290
- * Serialize the object
291
- * @return string
292
- */
293
- public function serialize()
294
- {
295
- // serialize the data
296
- return serialize(array(
297
- $this->id(),
298
- $this->incrementId(),
299
- $this->quoteId(),
300
- $this->quantity(),
301
- $this->currency(),
302
- $this->timestamp(),
303
- $this->customerIP(),
304
- $this->items(),
305
- $this->storeview(),
306
- is_object($customer = $this->customer()) ? $customer->id() : null,
307
- $this->addresses(),
308
- $this->price(),
309
- $this->weight(),
310
- $this->state(),
311
- $this->status(),
312
- $this->shippingDescription(),
313
- $this->paymentDescription()
314
- ));
315
- }
316
-
317
- /**
318
- * Unserialize the object
319
- * @param string
320
- * @return Copernica_MarketingSoftware_Model_Abstraction_Order
321
- */
322
- public function unserialize($string)
323
- {
324
- list(
325
- $this->id,
326
- $this->incrementId,
327
- $this->quoteId,
328
- $this->quantity,
329
- $this->currency,
330
- $this->timestamp,
331
- $this->customerIP,
332
- $this->items,
333
- $this->storeview,
334
- $this->customerId,
335
- $this->addresses,
336
- $this->price,
337
- $this->weight,
338
- $this->state,
339
- $this->status,
340
- $this->shippingDescription,
341
- $this->paymentDescription
342
- ) = unserialize($string);
343
-
344
- return $this;
345
- }
346
- }
347
-
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 Order
29
+ */
30
+ class Copernica_MarketingSoftware_Model_Abstraction_Order implements Serializable
31
+ {
32
+ /**
33
+ * Getting payment name does not work with Klarna. Klarna gets the
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
+ }
266
+ }
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
+ }
332
+ }
333
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app/code/community/Copernica/MarketingSoftware/Model/Abstraction/Order/Item.php CHANGED
@@ -1,180 +1,180 @@
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 Order Item
29
- */
30
- class Copernica_MarketingSoftware_Model_Abstraction_Order_Item implements Serializable
31
- {
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;
66
- }
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
- }
180
-
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 Order Item
29
+ */
30
+ class Copernica_MarketingSoftware_Model_Abstraction_Order_Item implements Serializable
31
+ {
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;
66
+ }
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
+ }
180
+
app/code/community/Copernica/MarketingSoftware/Model/Abstraction/Order/Item/Options.php CHANGED
@@ -1,151 +1,151 @@
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 order item options
29
- */
30
- class Copernica_MarketingSoftware_Model_Abstraction_Order_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
- * @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
-
63
- if ($attributes) {
64
- foreach ($attributes as $attribute) {
65
- $data[$attribute['label']] = $attribute['value'];
66
- }
67
- $this->attributes = $data;
68
- }
69
-
70
- return $this;
71
- }
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
- }
151
-
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 order item options
29
+ */
30
+ class Copernica_MarketingSoftware_Model_Abstraction_Order_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
+ * @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
+
63
+ if ($attributes) {
64
+ foreach ($attributes as $attribute) {
65
+ $data[$attribute['label']] = $attribute['value'];
66
+ }
67
+ $this->attributes = $data;
68
+ }
69
+
70
+ return $this;
71
+ }
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
+ }
151
+
app/code/community/Copernica/MarketingSoftware/Model/Abstraction/Price.php CHANGED
@@ -1,239 +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
- * A wrapper object around a price
29
- * This is not representing a magento object
30
- * Note that the price can consist of a lot of components:
31
- * - price
32
- * - shippingcost
33
- * - discount
34
- * - additional_fees (is a list of custom configurable fees)
35
- * - tax
36
- * - total_price
37
- */
38
- class Copernica_MarketingSoftware_Model_Abstraction_Price implements Serializable
39
- {
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
- // Used for quote items and order items
95
- elseif ($originalPrice = $original->getOriginalPrice())
96
- $this->originalPrice = $originalPrice;
97
-
98
- if ($discountAmount = $original->getDiscountAmount())
99
- $this->discount = $discountAmount;
100
-
101
- if ($taxAmount = $original->getTaxAmount())
102
- $this->tax = $taxAmount;
103
-
104
- // Shipping is only available for quotes and orders, but not for items
105
- if ($original instanceOf Mage_Sales_Model_Quote || $original instanceOf Mage_Sales_Model_Order)
106
- {
107
- // Get the shipping amount
108
- if ($shippingAmount = $original->getShippingAmount())
109
- $this->shipping = $shippingAmount;
110
- else
111
- $this->shipping = 0;
112
- }
113
- else $this->shipping = 0;
114
-
115
- if ($currency = $original->getOrderCurrencyCode())
116
- $this->currency = $currency;
117
- elseif ($currency = $original->getQuoteCurrencyCode())
118
- $this->currency = $currency;
119
- elseif (($order = $original->getOrder()) && ($currency = $order->getOrderCurrencyCode()))
120
- $this->currency = $currency;
121
- elseif (($quote = $original->getQuote()) && ($currency = $quote->getQuoteCurrencyCode()))
122
- $this->currency = $currency;
123
- else
124
- $this->currency = '';
125
-
126
- return $this;
127
- }
128
-
129
- /**
130
- * Return the total price
131
- * @return float
132
- */
133
- public function total()
134
- {
135
- return $this->total;
136
- }
137
-
138
- /**
139
- * Return the price for the individual item
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
- * @return float
150
- */
151
- public function itemPrice()
152
- {
153
- return $this->itemPrice;
154
- }
155
-
156
- /**
157
- * Return the original price for the individual item
158
- * @return float
159
- */
160
- public function originalPrice()
161
- {
162
- return $this->originalPrice;
163
- }
164
-
165
- /**
166
- * Return the discount which was given
167
- * @return float
168
- */
169
- public function discount()
170
- {
171
- return $this->discount;
172
- }
173
-
174
- /**
175
- * Return the tax which was paid
176
- * @return float
177
- */
178
- public function tax()
179
- {
180
- return $this->tax;
181
- }
182
-
183
- /**
184
- * Return the shipping costs
185
- * @return float
186
- */
187
- public function shipping()
188
- {
189
- return $this->shipping;
190
- }
191
-
192
- /**
193
- * Return the currency code
194
- * @return float
195
- */
196
- public function currency()
197
- {
198
- return $this->currency;
199
- }
200
-
201
- /**
202
- * Serialize the object
203
- * @return string
204
- */
205
- public function serialize()
206
- {
207
- // serialize the data
208
- return serialize(array(
209
- $this->total(),
210
- $this->costs(),
211
- $this->itemPrice(),
212
- $this->originalPrice(),
213
- $this->discount(),
214
- $this->tax(),
215
- $this->shipping(),
216
- $this->currency(),
217
- ));
218
- }
219
-
220
- /**
221
- * Unserialize the object
222
- * @param string
223
- * @return Copernica_MarketingSoftware_Model_Abstraction_Price
224
- */
225
- public function unserialize($string)
226
- {
227
- list(
228
- $this->total,
229
- $this->costs,
230
- $this->itemPrice,
231
- $this->originalPrice,
232
- $this->discount,
233
- $this->tax,
234
- $this->shipping,
235
- $this->currency
236
- ) = unserialize($string);
237
- }
238
- }
239
-
 
 
 
 
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 price
29
+ * This is not representing a magento object
30
+ * Note that the price can consist of a lot of components:
31
+ * - price
32
+ * - shippingcost
33
+ * - discount
34
+ * - additional_fees (is a list of custom configurable fees)
35
+ * - tax
36
+ * - total_price
37
+ */
38
+ class Copernica_MarketingSoftware_Model_Abstraction_Price implements Serializable
39
+ {
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(),
215
+ $this->itemPrice(),
216
+ $this->originalPrice(),
217
+ $this->discount(),
218
+ $this->tax(),
219
+ $this->shipping(),
220
+ $this->currency(),
221
+ ));
222
+ }
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
+
app/code/community/Copernica/MarketingSoftware/Model/Abstraction/Product.php CHANGED
@@ -1,415 +1,341 @@
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 magento Product
29
- */
30
- class Copernica_MarketingSoftware_Model_Abstraction_Product implements Serializable
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 $isNew;
50
- protected $price;
51
- protected $specialPrice;
52
- protected $created;
53
- protected $modified;
54
- protected $attributes;
55
- protected $attributeSet;
56
-
57
- /**
58
- * Sets the original model
59
- * @param Mage_Catalog_Model_Product|Mage_Sales_Model_Quote_Item|Mage_Sales_Model_Order_Item $original
60
- * @return Copernica_MarketingSoftware_Model_Abstraction_Product
61
- */
62
- public function setOriginal($original)
63
- {
64
- if ($original instanceof Mage_Catalog_Model_Product) {
65
- //this is the original product
66
- $this->id = $original->getId();
67
- $this->sku = $original->getSku();
68
- $this->name = $original->getName();
69
- $this->description = $original->getShortDescription();
70
- $this->price = $original->getPrice();
71
- $this->specialPrice = $original->getSpecialPrice();
72
- $this->created = $original->getCreatedAt();
73
- $this->modified = $original->getUpdatedAt();
74
- $this->productUrl = $original->getProductUrl();
75
- $this->imagePath = 'catalog' . DS . 'product' . $original->getImage();
76
- $this->weight = $original->getWeight();
77
-
78
- $data = array();
79
- $categoryIds = $original->getCategoryIds();
80
- foreach ($categoryIds as $categoryId) {
81
- $category = Mage::getModel('catalog/category')->load($categoryId);
82
- $data[] = $this->_getFullCategoryName($category);
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
- if ($from || $to) {
89
- $new = true;
90
- $now = Zend_Date::now();
91
- if ($from) {
92
- $new = $new && $from->isEarlier($now);
93
- }
94
- if ($to) {
95
- $new = $new && $to->isLater($now);
96
- }
97
- $this->isNew = $new;
98
- } else {
99
- $this->isNew = false;
100
- }
101
-
102
- $this->attributes = Mage::getModel('marketingsoftware/abstraction_attributes')->setOriginal($original);
103
-
104
- $attributeSetModel = Mage::getModel("eav/entity_attribute_set");
105
- $attributeSetModel->load($original->getAttributeSetId());
106
-
107
- $this->attributeSet = $attributeSetModel->getAttributeSetName();
108
-
109
- $this->timestamp = time();
110
-
111
-
112
- return $this;
113
- } else {
114
- //the quote item or order item has a product id
115
- $product = Mage::getModel('catalog/product')->load($original->getProductId());
116
- if ($product->getId()) {
117
- //the product exists
118
- $this->id = $product->getId();
119
- $this->sku = $product->getSku();
120
- $this->name = $product->getName();
121
- $this->description = $product->getShortDescription();
122
- $this->price = $product->getPrice();
123
- $this->specialPrice = $product->getSpecialPrice();
124
- $this->created = $product->getCreatedAt();
125
- $this->modified = $product->getUpdatedAt();
126
- $this->productUrl = $product->getProductUrl();
127
- $this->imagePath = 'catalog' . DS . 'product' . $product->getImage();
128
- $this->weight = $product->getWeight();
129
-
130
- $data = array();
131
- $categoryIds = $product->getCategoryIds();
132
- foreach ($categoryIds as $categoryId) {
133
- $category = Mage::getModel('catalog/category')->load($categoryId);
134
- $data[] = $this->_getFullCategoryName($category);
135
- }
136
- $this->categories = $data;
137
-
138
- $from = $product->getNewsFromDate() ? Mage::app()->getLocale()->date($product->getNewsFromDate()) : null;
139
- $to = $product->getNewsToDate() ? Mage::app()->getLocale()->date($product->getNewsToDate()) : null;
140
- if ($from || $to) {
141
- $new = true;
142
- $now = Zend_Date::now();
143
- if ($from) {
144
- $new = $new && $from->isEarlier($now);
145
- }
146
- if ($to) {
147
- $new = $new && $to->isLater($now);
148
- }
149
- $this->isNew = $new;
150
- } else {
151
- $this->isNew = false;
152
- }
153
-
154
- $this->attributes = Mage::getModel('marketingsoftware/abstraction_attributes')->setOriginal($product);
155
-
156
- $attributeSetModel = Mage::getModel("eav/entity_attribute_set");
157
- $attributeSetModel->load($product->getAttributeSetId());
158
-
159
- $this->attributeSet = $attributeSetModel->getAttributeSetName();
160
-
161
- $this->timestamp = time();
162
- } else {
163
- // unfortunately we do not have the product any more, but we have the information
164
- // so we can fill a lot of fields, so the functions still work
165
- $this->id = $original->getProductId();
166
- $this->sku = $original->getSKU();
167
- $this->attributeSet = '';
168
- $this->name = $original->getName();
169
- $this->description = $original->getDescription();
170
- $this->productUrl = '';
171
- $this->imagePath = '';
172
- $this->weight = $original->getWeight();
173
- $this->categories = array();
174
- $this->isNew = false;
175
- $this->price = $original->getPrice();
176
- $this->created = '';
177
- $this->modified = '';
178
- $this->attributes = '';
179
- }
180
-
181
- return $this;
182
- }
183
- }
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
- {
210
- return $this->id;
211
- }
212
-
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
- /**
364
- * Serialize the object
365
- * @return string
366
- */
367
- public function serialize()
368
- {
369
- // serialize the data
370
- return serialize(array(
371
- $this->id(),
372
- $this->sku(),
373
- $this->attributeSet(),
374
- $this->name(),
375
- $this->description(),
376
- $this->productUrl(),
377
- $this->imageUrl(false), // gets the image path if store 'false' is supplied
378
- $this->weight(),
379
- $this->categories(),
380
- $this->isNew(),
381
- $this->price(),
382
- $this->specialPrice(),
383
- $this->created(),
384
- $this->modified(),
385
- $this->attributes(),
386
- ));
387
- }
388
-
389
- /**
390
- * Unserialize the object
391
- * @param string
392
- * @return Copernica_MarketingSoftware_Model_Abstraction_Product
393
- */
394
- public function unserialize($string)
395
- {
396
- list(
397
- $this->id,
398
- $this->sku,
399
- $this->attributeSet,
400
- $this->name,
401
- $this->description,
402
- $this->productUrl,
403
- $this->imagePath,
404
- $this->weight,
405
- $this->categories,
406
- $this->isNew,
407
- $this->price,
408
- $this->specialPrice,
409
- $this->created,
410
- $this->modified,
411
- $this->attributes
412
- ) = unserialize($string);
413
- return $this;
414
- }
415
  }
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 magento Product
29
+ */
30
+ class Copernica_MarketingSoftware_Model_Abstraction_Product implements Serializable
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
+
89
+ return $this;
90
+ }
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
  }
app/code/community/Copernica/MarketingSoftware/Model/Abstraction/Quote.php CHANGED
@@ -1,321 +1,348 @@
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 Quote
29
- */
30
- class Copernica_MarketingSoftware_Model_Abstraction_Quote implements Serializable
31
- {
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
- $this->quoteId = $original->getId();
58
- $this->active = (bool) $original->getIsActive();
59
- $this->quantity = $original->getItemsQty();
60
- $this->currency = $original->getQuoteCurrencyCode();
61
- $this->price = Mage::getModel('marketingsoftware/abstraction_price')->setOriginal($original);
62
- $this->storeview = Mage::getModel('marketingsoftware/abstraction_storeview')->setOriginal($original->getStore());
63
- $this->timestamp = $original->getUpdatedAt();
64
- $this->customerIP = $original->getRemoteIp();
65
-
66
- if ($address = $original->getShippingAddress()) {
67
- $this->weight = $address->getWeight();
68
- }
69
-
70
- $data = array();
71
- $items = $original->getAllVisibleItems();
72
- foreach ($items as $item) {
73
- $data[] = Mage::getModel('marketingsoftware/abstraction_quote_item')->setOriginal($item);
74
- }
75
- $this->items = $data;
76
-
77
- // The quote model only returns a customer if it exists
78
- if ($customerId = $original->getCustomerId()) {
79
- $this->customerId = $customerId;
80
- }
81
-
82
- $data = array();
83
- //retrieve this quote's addresses
84
- //Note: this may return empty addresses, since quotes always have address records. Check the email field of the address.
85
- $addresses = $original->getAddressesCollection();
86
- foreach ($addresses as $address) {
87
- $data[] = Mage::getModel('marketingsoftware/abstraction_address')->setOriginal($address);
88
- }
89
- $this->addresses = $data;
90
-
91
- if ($address = $original->getShippingAddress()) {
92
- $this->shippingDescription = $address->getShippingDescription();
93
- }
94
-
95
- if ($payment = $original->getPayment()) {
96
- //this try/catch is needed because getMethodInstance throws an exception instead of returning null
97
- try {
98
- $this->paymentDescription = $payment->getMethodInstance()->getTitle();
99
- } catch (Mage_Core_Exception $exception) { }
100
- }
101
-
102
- return $this;
103
- }
104
-
105
- /**
106
- * Loads a quote model
107
- * @param integer $quoteId
108
- * @return Copernica_MarketingSoftware_Model_Abstraction_Quote
109
- */
110
- public function loadQuote($quoteId)
111
- {
112
- // Get the model
113
- $quote = Mage::getModel('sales/quote');
114
-
115
- // Difference between Magento 1.4 / 1.5
116
- if (!is_callable($quote, 'loadByIdWithoutStore'))
117
- {
118
- // construct an array with store ids
119
- $storeIDs = array();
120
- foreach (Mage::app()->getStores() as $id => $store) $storeIDs[] = $id;
121
-
122
- // The store ids are used for loading the quote, independant of the store
123
- $quote->setSharedStoreIds($storeIDs);
124
- $quote->load($quoteId);
125
- }
126
- else $quote->loadByIdWithoutStore($quoteId);
127
-
128
- // we did load a valid quote, set the original model
129
- if ($quote->getId()) {
130
- $this->setOriginal($quote);
131
- } else {
132
- $this->quoteId = $quoteId;
133
- }
134
-
135
- // return this
136
- return $this;
137
- }
138
-
139
- /**
140
- * The quote id of this quote object
141
- * @return integer
142
- */
143
- public function id()
144
- {
145
- return $this->quoteId;
146
- }
147
-
148
- /**
149
- * Is this quote still active
150
- * @return boolean
151
- */
152
- public function active()
153
- {
154
- return $this->active;
155
- }
156
-
157
- /**
158
- * The number of items present in this quote
159
- * @return integer
160
- */
161
- public function quantity()
162
- {
163
- return $this->quantity;
164
- }
165
-
166
- /**
167
- * The payment currency of this quote
168
- * @return string
169
- */
170
- public function currency()
171
- {
172
- return $this->currency;
173
- }
174
-
175
- /**
176
- * The price
177
- * Note that an object is returned, which may consist of multiple components
178
- * @return Copernica_MarketingSoftware_Model_Abstraction_Price
179
- */
180
- public function price()
181
- {
182
- return $this->price;
183
- }
184
-
185
- /**
186
- * The weight
187
- * @return float
188
- */
189
- public function weight()
190
- {
191
- return $this->weight;
192
- }
193
-
194
- /**
195
- * To what storeview does this quote belong
196
- * @return Copernica_MarketingSoftware_Model_Abstraction_Storeview
197
- */
198
- public function storeview()
199
- {
200
- return $this->storeview;
201
- }
202
-
203
- /**
204
- * Get the items from the quote
205
- * @return array of Copernica_MarketingSoftware_Model_Abstraction_Quote_Item
206
- */
207
- public function items()
208
- {
209
- return $this->items;
210
- }
211
-
212
- /**
213
- * The timestamp at which this quote was modified
214
- * @return string
215
- */
216
- public function timestamp()
217
- {
218
- return $this->timestamp;
219
- }
220
-
221
- /**
222
- * The customer may return null
223
- * @return Copernica_MarketingSoftware_Model_Abstraction_Customer
224
- */
225
- public function customer()
226
- {
227
- if ($this->customerId) {
228
- return Mage::getModel('marketingsoftware/abstraction_customer')->loadCustomer($this->customerId);
229
- } else {
230
- // default fallback
231
- return null;
232
- }
233
- }
234
-
235
- /**
236
- * The addresses of this quote
237
- * @return array of Copernica_MarketingSoftware_Model_Abstraction_Address
238
- */
239
- public function addresses()
240
- {
241
- return $this->addresses;
242
- }
243
-
244
- /**
245
- * The IP from which this quote was constructed
246
- * @return string
247
- */
248
- public function customerIP()
249
- {
250
- return $this->customerIP;
251
- }
252
-
253
- /**
254
- * The shipping method of this quote
255
- * @return string
256
- */
257
- public function shippingDescription()
258
- {
259
- return $this->shippingDescription;
260
- }
261
-
262
- /**
263
- * The payment method of this quote
264
- * @return string
265
- */
266
- public function paymentDescription()
267
- {
268
- return $this->paymentDescription;
269
- }
270
-
271
- /**
272
- * Serialize the object
273
- * @return string
274
- */
275
- public function serialize()
276
- {
277
- // serialize the data
278
- return serialize(array(
279
- $this->id(),
280
- $this->quantity(),
281
- $this->currency(),
282
- $this->timestamp(),
283
- $this->customerIP(),
284
- $this->items(),
285
- $this->storeview(),
286
- is_object($customer = $this->customer()) ? $customer->id() : null,
287
- $this->addresses(),
288
- $this->price(),
289
- $this->weight(),
290
- $this->active(),
291
- $this->shippingDescription(),
292
- $this->paymentDescription()
293
- ));
294
- }
295
-
296
- /**
297
- * Unserialize the object
298
- * @param string
299
- * @return Copernica_MarketingSoftware_Model_Abstraction_Quote
300
- */
301
- public function unserialize($string)
302
- {
303
- list(
304
- $this->quoteId,
305
- $this->quantity,
306
- $this->currency,
307
- $this->timestamp,
308
- $this->customerIP,
309
- $this->items,
310
- $this->storeview,
311
- $this->customerId,
312
- $this->addresses,
313
- $this->price,
314
- $this->weight,
315
- $this->active,
316
- $this->shippingDescription,
317
- $this->paymentDescription
318
- ) = unserialize($string);
319
- return $this;
320
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
321
  }
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 Quote
29
+ */
30
+ class Copernica_MarketingSoftware_Model_Abstraction_Quote implements Serializable
31
+ {
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
+ {
289
+ return serialize(array(
290
+ $this->id()
291
+ ));
292
+
293
+ // serialize the data
294
+ return serialize(array(
295
+ $this->id(),
296
+ $this->quantity(),
297
+ $this->currency(),
298
+ $this->timestamp(),
299
+ $this->customerIP(),
300
+ $this->items(),
301
+ $this->storeview(),
302
+ is_object($customer = $this->customer()) ? $customer->id() : null,
303
+ $this->addresses(),
304
+ $this->price(),
305
+ $this->weight(),
306
+ $this->active(),
307
+ $this->shippingDescription(),
308
+ $this->paymentDescription()
309
+ ));
310
+ }
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
  }
app/code/community/Copernica/MarketingSoftware/Model/Abstraction/Quote/Item.php CHANGED
@@ -1,182 +1,182 @@
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 Quote Item
29
- */
30
- class Copernica_MarketingSoftware_Model_Abstraction_Quote_Item implements Serializable
31
- {
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;
69
- }
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,
154
- $this->quantity(),
155
- $this->price(),
156
- $this->weight(),
157
- $this->timestamp(),
158
- $this->options(),
159
- $this->product(),
160
- ));
161
- }
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
  }
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 Quote Item
29
+ */
30
+ class Copernica_MarketingSoftware_Model_Abstraction_Quote_Item implements Serializable
31
+ {
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;
69
+ }
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,
154
+ $this->quantity(),
155
+ $this->price(),
156
+ $this->weight(),
157
+ $this->timestamp(),
158
+ $this->options(),
159
+ $this->product(),
160
+ ));
161
+ }
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
  }
app/code/community/Copernica/MarketingSoftware/Model/Abstraction/Quote/Item/Options.php CHANGED
@@ -1,155 +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
- * A wrapper object around quote item options
29
- */
30
- class Copernica_MarketingSoftware_Model_Abstraction_Quote_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
- * @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
-
66
- if ($attributes) {
67
- foreach ($attributes as $attribute) {
68
- $data[$attribute['label']] = $attribute['value'];
69
- }
70
- $this->attributes = $data;
71
- }
72
- }
73
-
74
- return $this;
75
- }
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(),
138
- ));
139
- }
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
- }
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
+ * A wrapper object around quote item options
29
+ */
30
+ class Copernica_MarketingSoftware_Model_Abstraction_Quote_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
+ * @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
+
66
+ if ($attributes) {
67
+ foreach ($attributes as $attribute) {
68
+ $data[$attribute['label']] = $attribute['value'];
69
+ }
70
+ $this->attributes = $data;
71
+ }
72
+ }
73
+
74
+ return $this;
75
+ }
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(),
138
+ ));
139
+ }
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
+ }
155
+
app/code/community/Copernica/MarketingSoftware/Model/Abstraction/Storeview.php CHANGED
@@ -1,174 +1,174 @@
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 Store
29
- */
30
- class Copernica_MarketingSoftware_Model_Abstraction_Storeview implements Serializable
31
- {
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;
61
- }
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
- {
132
- return implode(' > ', array(
133
- $this->websiteLabel(),
134
- $this->storeLabel(),
135
- $this->viewLabel(),
136
- ));
137
- }
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(),
149
- $this->websiteLabel(),
150
- $this->storeCode(),
151
- $this->storeLabel(),
152
- $this->viewCode(),
153
- $this->viewLabel(),
154
- ));
155
- }
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
  }
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 Store
29
+ */
30
+ class Copernica_MarketingSoftware_Model_Abstraction_Storeview implements Serializable
31
+ {
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;
61
+ }
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
+ {
132
+ return implode(' > ', array(
133
+ $this->websiteLabel(),
134
+ $this->storeLabel(),
135
+ $this->viewLabel(),
136
+ ));
137
+ }
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(),
149
+ $this->websiteLabel(),
150
+ $this->storeCode(),
151
+ $this->storeLabel(),
152
+ $this->viewCode(),
153
+ $this->viewLabel(),
154
+ ));
155
+ }
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
  }
app/code/community/Copernica/MarketingSoftware/Model/Abstraction/Subscription.php CHANGED
@@ -1,160 +1,160 @@
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 Newsletter Subscription
29
- */
30
- class Copernica_MarketingSoftware_Model_Abstraction_Subscription implements Serializable
31
- {
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
-
73
- return $this;
74
- }
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
- }
116
- }
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(),
137
- $this->status(),
138
- $this->storeview(),
139
- is_object($customer = $this->customer()) ? $customer->id() : null,
140
- ));
141
- }
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
- }
160
-
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 Newsletter Subscription
29
+ */
30
+ class Copernica_MarketingSoftware_Model_Abstraction_Subscription implements Serializable
31
+ {
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
+
73
+ return $this;
74
+ }
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
+ }
116
+ }
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(),
137
+ $this->status(),
138
+ $this->storeview(),
139
+ is_object($customer = $this->customer()) ? $customer->id() : null,
140
+ ));
141
+ }
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
+ }
160
+
app/code/community/Copernica/MarketingSoftware/Model/Abstraction/Viewedproduct.php CHANGED
@@ -1,427 +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 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
- }
180
-
181
- return $this;
182
- }
183
- }
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
- {
210
- return $this->id;
211
- }
212
-
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
  }
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 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
+ }
180
+
181
+ return $this;
182
+ }
183
+ }
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
+ {
210
+ return $this->id;
211
+ }
212
+
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
  }
app/code/community/Copernica/MarketingSoftware/Model/AsyncPomSoapClient.php DELETED
@@ -1,328 +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
- * Asynchronous SOAP api client.
29
- * This is an extension to the normal soap client, because it can run multiple
30
- * calls at the same time. It differs from the normal PomSoapClient class because
31
- * the PomSoapClient::methodToCall() method does not return the result, but
32
- * a handle that can be queried to see if it has already returned data.
33
- *
34
- * Example use:
35
- *
36
- * $client = new PomAsyncSoapClient($url, $login, $account, $password);
37
- * $req1 = $client->someSoapMethod(...);
38
- * $req2 = $client->someSoapMethod(...);
39
- * $req3 = $client->someSoapMethod(...);
40
- * $answer1 = $client->result($req1);
41
- * $answer2 = $client->result($req2);
42
- * $answer3 = $client->result($req3);
43
- */
44
- class Copernica_MarketingSoftware_Model_AsyncPomSoapClient extends Copernica_MarketingSoftware_Model_PomSoapClient
45
- {
46
- /**
47
- * The content type header to use for soap requests
48
- *
49
- * @var string
50
- */
51
- const HTTP_HEADER_SOAP = 'application/soap+xml;charset=UTF-8';
52
-
53
- /**
54
- * The curl multi handle
55
- * @var resource
56
- */
57
- protected $curl = false;
58
-
59
- /**
60
- * Set of pending requests ID's
61
- * This is an assoc array: request ID maps to a array with handle and request
62
- * @var array
63
- */
64
- protected $pending = array();
65
-
66
- /**
67
- * Set of requests for which the answer has been received
68
- * This is an assoc array: request ID maps to the received answer
69
- * @var array
70
- */
71
- protected $completed = array();
72
-
73
- /**
74
- * The last assigned request ID
75
- * @var integer
76
- */
77
- protected $freeID = 0;
78
-
79
- /**
80
- * Name of the cookie file
81
- * @var string
82
- */
83
- protected $cookies = false;
84
-
85
- /**
86
- * Is the object currently busy parsing an async answer?
87
- * @var resource CURL resource identifier of the request that is internally processed
88
- */
89
- protected $internalRequest = false;
90
-
91
-
92
- /**
93
- * Destructor
94
- */
95
- public function __destruct()
96
- {
97
- // skip if no calls were made
98
- if (!$this->cookies) return;
99
-
100
- // wait until everything is ready
101
- $this->run();
102
-
103
- // remove the cookie file
104
- //unlink($this->cookies);
105
-
106
- // close the connections
107
- if(is_resource($this->curl)) curl_multi_close($this->curl);
108
- }
109
-
110
- /**
111
- * Method returns the ID's of all requests, both the pending ones and the
112
- * ones that have already been completed
113
- * @return array of int
114
- */
115
- public function allRequests()
116
- {
117
- return array_merge($this->pendingRequests(), $this->completedRequests());
118
- }
119
-
120
- /**
121
- * Method to retrieve the ID's of all pending requests
122
- * @return array
123
- */
124
- public function pendingRequests()
125
- {
126
- return array_keys($this->pending);
127
- }
128
-
129
- /**
130
- * Method to retrieve the ID's of all completed requests
131
- * @return array
132
- */
133
- public function completedRequests()
134
- {
135
- return array_keys($this->completed);
136
- }
137
-
138
- /**
139
- * Get the result of a certain request
140
- * This method will block until the request has been completed
141
- * @param integer ID of a request
142
- * @return mixed The response from the request
143
- */
144
- public function result($requestID)
145
- {
146
- // if this a request for which the result was already found
147
- if (isset($this->completed[$requestID])) return $this->completed[$requestID];
148
-
149
- // skip if an invalid ID was supplied
150
- if (!isset($this->pending[$requestID])) return false;
151
-
152
- // wait for the next call
153
- $this->wait();
154
-
155
- // fetch the result (with recursion)
156
- return $this->result($requestID);
157
- }
158
-
159
- /**
160
- * Wait for the next pending request to complete
161
- * This method will block until a SOAP call completes
162
- * It returns the request ID of the soap call that completed
163
- * @param float Timeout in seconds
164
- * @return integer The ID of the request that was completed
165
- */
166
- public function wait($timeout = 1.0)
167
- {
168
- // not possible when nothing is pending
169
- if (count($this->pending) == 0) return false;
170
-
171
- // exec the connections
172
- $active = null;
173
- while(($execrun = curl_multi_exec($this->curl, $active)) == CURLM_CALL_MULTI_PERFORM) { /* do nothing */ }
174
-
175
- // run a select call to wait for a connection to become ready
176
- $ready = curl_multi_select($this->curl, $timeout);
177
-
178
- // find all requests that are ready
179
- while ($info = curl_multi_info_read($this->curl))
180
- {
181
- // find the request ID
182
- $requestID = $this->resource2id($info['handle']);
183
- $info = $this->pending[$requestID];
184
-
185
- // make an internal call to find the answer
186
- $this->internalRequest = $info['handle'];
187
- $answer = $this->__call($info['method'], $info['params']);
188
- $this->internalRequest = false;
189
-
190
- // we have the answer
191
- $this->completed[$requestID] = $answer;
192
-
193
- // request is no longer pending
194
- unset($this->pending[$requestID]);
195
-
196
- // resource is no longer busy
197
- curl_multi_remove_handle($this->curl, $info['handle']);
198
- curl_close($info['handle']);
199
-
200
- // done
201
- return $requestID;
202
- }
203
-
204
- // not found
205
- return false;
206
- }
207
-
208
- /**
209
- * Run all requests
210
- * This method will process all requests, until none of them is pending
211
- */
212
- public function run()
213
- {
214
- // keep waiting until all requests are completed
215
- while (count($this->pending) > 0) $this->wait();
216
- }
217
-
218
- /**
219
- * Helper method to map a curl resource to a request ID
220
- * @param resource CURL resource
221
- * @return integer Request ID
222
- */
223
- protected function resource2id($resource)
224
- {
225
- // loop through all pending requests
226
- foreach ($this->pending as $request => $data)
227
- {
228
- // compare ID's
229
- if ($data['handle'] == $resource) return $request;
230
- }
231
-
232
- // not found
233
- return false;
234
- }
235
-
236
- /**
237
- * Overridden implementation of the __doRequest call. This method filters
238
- * all calls, and adds them to the set of pending calls.
239
- * @param string SOAP XML string to send to the server
240
- * @param string URL to connect to
241
- * @param string The SOAP action
242
- * @param integer The SOAP version
243
- * @param integer One way traffic, no result is expected
244
- */
245
- public function __doRequest($request, $location, $action, $version, $one_way = 0)
246
- {
247
- // is this an internal request, in that case we have already started the
248
- // connection, and only need to fetch the data
249
- if ($this->internalRequest) return curl_multi_getcontent($this->internalRequest);
250
-
251
- // create a new curl resource
252
- $curl = curl_init($location);
253
-
254
- // do we already have a cookie file?
255
- if (!$this->cookies) $this->cookies = Mage::getBaseDir('tmp') .'copernica.tmp';
256
-
257
- // set all options for it
258
- curl_setopt_array($curl, array(
259
- CURLOPT_POST => true,
260
- CURLOPT_RETURNTRANSFER => true,
261
- CURLOPT_COOKIEFILE => $this->cookies,
262
- CURLOPT_COOKIEJAR => $this->cookies,
263
- CURLOPT_POSTFIELDS => $request,
264
- CURLOPT_HTTPHEADER => array('Content-Type: '.self::HTTP_HEADER_SOAP),
265
- ));
266
-
267
- // find the method name
268
- $methodName = preg_match('/#(.*)$/', $action, $matches) ? $matches[1] : '';
269
-
270
- // is this a call that should be done immediately, and not asynchronous?
271
- if (in_array($methodName, array('login')))
272
- {
273
- // the login call should not be postponed
274
- return curl_exec($curl);
275
- }
276
- else
277
- {
278
- // do we a resource for multiple connections?
279
- if (!$this->curl) $this->curl = curl_multi_init();
280
-
281
- // add the curl handle
282
- curl_multi_add_handle($this->curl, $curl);
283
-
284
- // store the handle in the array of pending requests
285
- $this->pending[$this->freeID] = array(
286
- 'handle' => $curl,
287
- );
288
-
289
- // return the handle
290
- return $this->freeID++;
291
- }
292
- }
293
-
294
- /**
295
- * Method that handles the calls to the API
296
- * @param string Name of the method
297
- * @param array Associative array of parameters
298
- * @return mixed
299
- */
300
- public function __call($methodname, $params)
301
- {
302
- // get all current pending requests
303
- $pending = $this->pendingRequests();
304
-
305
- try
306
- {
307
- // make the call
308
- $result = parent::__call($methodname, $params);
309
- return $result;
310
- }
311
- catch (Exception $e)
312
- {
313
- // do we have a new pending request
314
- $newPending = array_values(array_diff($this->pendingRequests(), $pending));
315
- if (count($newPending) < 1) return false;
316
-
317
- // we have the request ID
318
- $requestID = $newPending[0];
319
-
320
- // we must add the methodname and parameters to the internal data structure
321
- $this->pending[$requestID]['method'] = $methodname;
322
- $this->pending[$requestID]['params'] = $params;
323
-
324
- // return the result
325
- return $requestID;
326
- }
327
- }
328
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app/code/community/Copernica/MarketingSoftware/Model/Config.php CHANGED
@@ -1,57 +1,62 @@
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
- * With the config object the configuration options of the marketing
29
- * software module are accessible.
30
- *
31
- * We do not use the Core_Config model, because everytime a config
32
- * value is saved it will empty the config cache, which might throw
33
- * errors on a high traffic website.
34
- *
35
- */
36
- class Copernica_MarketingSoftware_Model_Config extends Mage_Core_Model_Abstract
37
- {
38
- /**
39
- * Constructor
40
- *
41
- * @see Varien_Object::_construct()
42
- */
43
- protected function _construct()
44
- {
45
- $this->_init('marketingsoftware/config');
46
- }
47
-
48
- public function loadByKey($key)
49
- {
50
- $this->_beforeLoad($key, 'key_name');
51
- $this->_getResource()->load($this, $key, 'key_name');
52
- $this->_afterLoad();
53
- $this->setOrigData();
54
- $this->_hasDataChanges = false;
55
- return $this;
56
- }
 
 
 
 
 
57
  }
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
+ * With the config object the configuration options of the marketing
29
+ * software module are accessible.
30
+ *
31
+ * We do not use the Core_Config model, because everytime a config
32
+ * value is saved it will empty the config cache, which might throw
33
+ * errors on a high traffic website.
34
+ *
35
+ */
36
+ class Copernica_MarketingSoftware_Model_Config extends Mage_Core_Model_Abstract
37
+ {
38
+ /**
39
+ * Constructor
40
+ *
41
+ * @see Varien_Object::_construct()
42
+ */
43
+ protected function _construct()
44
+ {
45
+ $this->_init('marketingsoftware/config');
46
+ }
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)
54
+ {
55
+ $this->_beforeLoad($key, 'key_name');
56
+ $this->_getResource()->load($this, $key, 'key_name');
57
+ $this->_afterLoad();
58
+ $this->setOrigData();
59
+ $this->_hasDataChanges = false;
60
+ return $this;
61
+ }
62
  }
app/code/community/Copernica/MarketingSoftware/Model/Copernica/Abstract.php CHANGED
@@ -30,6 +30,7 @@
30
  abstract class Copernica_MarketingSoftware_Model_Copernica_Abstract implements ArrayAccess
31
  {
32
  /**
 
33
  * @var String
34
  */
35
  protected $direction = 'copernica';
30
  abstract class Copernica_MarketingSoftware_Model_Copernica_Abstract implements ArrayAccess
31
  {
32
  /**
33
+ * To where data will be saved? Posible values are 'copernica' and 'magento'
34
  * @var String
35
  */
36
  protected $direction = 'copernica';
app/code/community/Copernica/MarketingSoftware/Model/Copernica/Cartitem/Subprofile.php CHANGED
@@ -1,136 +1,149 @@
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_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()
58
- {
59
- return $this['item_id'];
60
- }
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()
87
- {
88
- return array('item_id', 'quote_id', 'status');
89
- }
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
-
101
- // Get the store id to make sure that we retrieve the correct url's
102
- if (($quote = $quoteItem->quote()) && ($storeview = $quote->storeview())) $storeId = $storeview->id();
103
- else $storeId = null;
104
-
105
- // flatten the categories
106
- $categories = array();
107
- if ($product->categories()) {
108
- foreach ($product->categories() as $category) $categories[] = implode(' > ', $category);
109
- }
110
-
111
- // Get the price object
112
- $price = $quoteItem->price();
113
-
114
- // construct an array of data
115
- return array(
116
- 'item_id' => $quoteItem->id(),
117
- 'quote_id' => $quoteItem->quote()->id(),
118
- 'product_id' => $product->id(),
119
- 'price' => is_object($price) ? $price->itemPrice() : null,
120
- 'status' => $this->status,
121
- 'name' => $product->name(),
122
- 'sku' => $product->sku(),
123
- 'attribute_set' => $product->attributeSet(),
124
- 'weight' => $quoteItem->weight(),
125
- 'quantity' => $quoteItem->quantity(),
126
- 'timestamp' => $quoteItem->timestamp(),
127
- 'store_view' => (string)$quoteItem->quote()->storeView(),
128
- 'total_price' => is_object($price) ? $price->total() : null,
129
- 'url' => $product->productUrl($storeId),
130
- 'image' => $product->imageUrl($storeId),
131
- 'categories' => implode("\n", $categories),
132
- 'attributes' => (string)$product->attributes(),
133
- 'options' => (string)$quoteItem->options(),
134
- );
135
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
136
  }
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_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()
58
+ {
59
+ return $this['item_id'];
60
+ }
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()
87
+ {
88
+ return array('item_id', 'quote_id', 'status');
89
+ }
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(),
111
+ 'weight' => $quoteItem->weight(),
112
+ 'quantity' => $quoteItem->quantity(),
113
+ 'timestamp' => $quoteItem->timestamp(),
114
+ 'attributes' => (string)$product->attributes(),
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()));
146
+
147
+ return $data;
148
+ }
149
  }
app/code/community/Copernica/MarketingSoftware/Model/Copernica/Entity.php ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 is a base class for all bridge classes between Magento and Copernica
29
+ */
30
+ 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
+ }
app/code/community/Copernica/MarketingSoftware/Model/Copernica/Entity/Address.php ADDED
@@ -0,0 +1,215 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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_Model_Copernica_Entity_Address extends Copernica_MarketingSoftware_Model_Copernica_Entity
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
+ {
105
+ return $this->getName()->firstname();
106
+ }
107
+
108
+ /**
109
+ * Fetch middlename
110
+ * @return string
111
+ */
112
+ public function fetchMiddlename()
113
+ {
114
+ return $this->getName()->middlename();
115
+ }
116
+
117
+ /**
118
+ * Fetch lastname
119
+ * @return string
120
+ */
121
+ public function fetchLastname()
122
+ {
123
+ return $this->getName()->lastname();
124
+ }
125
+
126
+ /**
127
+ * Fetch prefix
128
+ * @return string
129
+ */
130
+ public function fetchPrefix()
131
+ {
132
+ return $this->getName()->prefix();
133
+ }
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
+ }
app/code/community/Copernica/MarketingSoftware/Model/Copernica/Entity/CartItem.php ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 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
+ }
app/code/community/Copernica/MarketingSoftware/Model/Copernica/Entity/Customer.php ADDED
@@ -0,0 +1,299 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 is a bridge class between magento customer and copernica profile
29
+ */
30
+ class Copernica_MarketingSoftware_Model_Copernica_Entity_Customer extends Copernica_MarketingSoftware_Model_Copernica_Entity
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()
95
+ {
96
+ return $this->getId().'|'.$this->getStoreView()->id();
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()
113
+ {
114
+ return $this->getName()->firstname();
115
+ }
116
+
117
+ /**
118
+ * Get customer middlename
119
+ * @return string
120
+ */
121
+ public function fetchMiddlename()
122
+ {
123
+ return $this->getName()->middlename();
124
+ }
125
+
126
+ /**
127
+ * Fetch customer lastname
128
+ * @return string
129
+ */
130
+ public function fetchLastname()
131
+ {
132
+ return $this->getName()->lastname();
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
+ }
app/code/community/Copernica/MarketingSoftware/Model/Copernica/Entity/Item.php ADDED
@@ -0,0 +1,218 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 ADDED
@@ -0,0 +1,333 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 copernica and magento
29
+ */
30
+ class Copernica_MarketingSoftware_Model_Copernica_Entity_Order extends Copernica_MarketingSoftware_Model_Copernica_Entity
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()
112
+ {
113
+ return $this->getPrice()->currency();
114
+ }
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()
131
+ {
132
+ return $this->getPrice()->shipping();
133
+ }
134
+
135
+ /**
136
+ * Fetch total prive
137
+ * @return string
138
+ */
139
+ public function fetchTotal()
140
+ {
141
+ return $this->getPrice()->total();
142
+ }
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 '';
206
+ }
207
+
208
+ /**
209
+ * Get shipping address. When no valid address can be found this method will
210
+ * return null value.
211
+ *
212
+ * @return Copernica_MarketingSoftware_Model_Copernica_Entity_Address|null
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
+
229
+ /**
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
+ }
app/code/community/Copernica/MarketingSoftware/Model/Copernica/Entity/Product.php ADDED
@@ -0,0 +1,257 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ * Representation of a product for Copernica platform
29
+ */
30
+ class Copernica_MarketingSoftware_Model_Copernica_Entity_Product extends Copernica_MarketingSoftware_Model_Copernica_Entity
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
+ }
app/code/community/Copernica/MarketingSoftware/Model/Copernica/Entity/Quote.php ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 quote and copernica subprofile
29
+ */
30
+ class Copernica_MarketingSoftware_Model_Copernica_Entity_Quote extends Copernica_MarketingSoftware_Model_Copernica_Entity
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
+ }
app/code/community/Copernica/MarketingSoftware/Model/Copernica/Entity/Subscription.php ADDED
@@ -0,0 +1,142 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 subscription model
29
+ */
30
+ class Copernica_MarketingSoftware_Model_Copernica_Entity_Subscription extends Copernica_MarketingSoftware_Model_Copernica_Entity
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
+ }
74
+ }
75
+
76
+ /**
77
+ * Fetch group
78
+ * @return string
79
+ */
80
+ public function fetchGroup()
81
+ {
82
+ return Mage::getModel('customer/group')->load(0)->getCode();
83
+ }
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
+ }
app/code/community/Copernica/MarketingSoftware/Model/Copernica/Order/Subprofile.php CHANGED
@@ -45,7 +45,7 @@ class Copernica_MarketingSoftware_Model_Copernica_Order_Subprofile extends Coper
45
 
46
  /**
47
  * Try to store a quote item
48
- * @param Copernica_MarketingSoftware_Model_Copernica_Order_Subprofile
49
  */
50
  public function setOrder($order)
51
  {
@@ -79,9 +79,12 @@ class Copernica_MarketingSoftware_Model_Copernica_Order_Subprofile extends Coper
79
  {
80
  // initialize the addresses
81
  $billingAddress = $shippingAddress = false;
 
 
 
82
 
83
  // Get the addresses
84
- foreach ($this->order->addresses() as $address) {
85
  if (in_array('billing', $address->type())) $billingAddress = $address;
86
  if (in_array('shipping', $address->type())) $shippingAddress = $address;
87
  }
45
 
46
  /**
47
  * Try to store a quote item
48
+ * @param Copernica_MarketingSoftware_Model_Abstract
49
  */
50
  public function setOrder($order)
51
  {
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
  }
app/code/community/Copernica/MarketingSoftware/Model/Copernica/Orderitem/Subprofile.php CHANGED
@@ -1,119 +1,133 @@
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_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()
42
- {
43
- return $this['item_id'];
44
- }
45
-
46
- /**
47
- * Try to store a quote item
48
- * @param 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()
61
- {
62
- return Mage::helper('marketingsoftware/config')->getLinkedOrderItemFields();
63
- }
64
-
65
- /**
66
- * Get the required fields
67
- * @return array
68
- */
69
- public function requiredFields()
70
- {
71
- return array('item_id','order_id');
72
- }
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
- // Get the store id to make sure that we retrieve the correct url's
85
- if (($order = $orderItem->order()) && is_object($order) && ($storeview = $order->storeview())) $storeId = $storeview->id();
86
- else $storeId = null;
87
-
88
- // flatten the categories
89
- $categories = array();
90
- if ($product->categories()) {
91
- foreach ($product->categories() as $category) $categories[] = implode(' > ', $category);
92
- }
93
-
94
- // Get the price
95
- $price = $orderItem->price();
96
-
97
- // construct an array of data
98
- return array(
99
- 'item_id' => $orderItem->id(),
100
- 'order_id' => is_object($order) ? $order->id() : null,
101
- 'increment_id' => is_object($order) ? $order->incrementId() : null,
102
- 'product_id' => $product->id(),
103
- 'price' => is_object($price) ? $price->itemPrice() : null,
104
- 'name' => $product->name(),
105
- 'sku' => $product->sku(),
106
- 'attribute_set' => $product->attributeSet(),
107
- 'weight' => $orderItem->weight(),
108
- 'quantity' => $orderItem->quantity(),
109
- 'timestamp' => $orderItem->timestamp(),
110
- 'store_view' => is_object($order) ? (string)$order->storeView() : null,
111
- 'total_price' => is_object($price) ? $price->total() : null,
112
- 'url' => $product->productUrl($storeId),
113
- 'image' => $product->imageUrl($storeId),
114
- 'categories' => implode("\n", $categories),
115
- 'attributes' => (string)$product->attributes(),
116
- 'options' => (string)$orderItem->options(),
117
- );
118
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
119
  }
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_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()
42
+ {
43
+ return $this['item_id'];
44
+ }
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()
61
+ {
62
+ return Mage::helper('marketingsoftware/config')->getLinkedOrderItemFields();
63
+ }
64
+
65
+ /**
66
+ * Get the required fields
67
+ * @return array
68
+ */
69
+ public function requiredFields()
70
+ {
71
+ return array('item_id','order_id');
72
+ }
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(
85
+ 'item_id' => $orderItem->id(),
86
+ 'product_id' => $product->id(),
87
+ 'name' => $product->name(),
88
+ 'sku' => $product->sku(),
89
+ 'attribute_set' => $product->attributeSet(),
90
+ 'attributes' => (string)$product->attributes(),
91
+ 'weight' => $orderItem->weight(),
92
+ 'quantity' => $orderItem->quantity(),
93
+ 'timestamp' => $orderItem->timestamp(),
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
+
106
+ $storeView = $order->storeView();
107
+ $storeId = $storeView->id();
108
+
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()));
130
+
131
+ return $data;
132
+ }
133
  }
app/code/community/Copernica/MarketingSoftware/Model/Copernica/Profile.php CHANGED
@@ -38,6 +38,18 @@ abstract class Copernica_MarketingSoftware_Model_Copernica_Profile extends Coper
38
  return $this['customer_id'];
39
  }
40
 
 
 
 
 
 
 
 
 
 
 
 
 
41
  /**
42
  * Fallback method for profile objects not having this method.
43
  *
38
  return $this['customer_id'];
39
  }
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();
52
+
53
  /**
54
  * Fallback method for profile objects not having this method.
55
  *
app/code/community/Copernica/MarketingSoftware/Model/Copernica/Profilecustomer.php CHANGED
@@ -1,93 +1,122 @@
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_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
-
48
- /**
49
- * Returns customer_id based on the original (previous) email, if possible.
50
- * Falls back on the customer_id using the current email.
51
- *
52
- * @return string
53
- */
54
- public function originalId()
55
- {
56
- // If the email address has been changed return the old id
57
- if ($this->customer->oldEmail() != $this->customer->email()) {
58
- $email = $this->customer->oldEmail();
59
- } else {
60
- $email = $this->customer->email();
61
- }
62
-
63
- return Mage::helper('marketingsoftware')->generateCustomerId($email, (string) $this->customer->storeview());
64
- }
65
-
66
- /**
67
- * Retrieve the data for this object
68
- * @return array
69
- */
70
- protected function _data()
71
- {
72
- // Get the subscription
73
- $subscription = $this->customer->subscription();
74
-
75
- // fetch the name object
76
- $name = $this->customer->name();
77
-
78
- $email = $this->customer->email();
79
-
80
- // return an array with customer data
81
- return array(
82
- 'customer_id' => Mage::helper('marketingsoftware')->generateCustomerId($email, (string)$this->customer->storeview()),
83
- 'store_view' => (string)$this->customer->storeview(),
84
- 'firstname' => is_object($name) ? $name->firstname() : null,
85
- 'middlename' => is_object($name) ? $name->middlename() : null,
86
- 'lastname' => is_object($name) ? $name->lastname() : null,
87
- 'email' => $email,
88
- 'group' => $this->customer->group(),
89
- 'newsletter' => is_object($subscription) ? $subscription->status() : 'unknown',
90
- 'gender' => $this->customer->gender() ? $this->customer->gender() : 'unknown'
91
- );
92
- }
93
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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_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
+
48
+ /**
49
+ * Returns customer_id based on the original (previous) email, if possible.
50
+ * Falls back on the customer_id using the current email.
51
+ *
52
+ * @return string
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,
112
+ 'firstname' => is_object($name) ? $name->firstname() : null,
113
+ 'middlename' => is_object($name) ? $name->middlename() : null,
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
+ }
app/code/community/Copernica/MarketingSoftware/Model/Copernica/Profileorder.php CHANGED
@@ -30,7 +30,7 @@
30
  class Copernica_MarketingSoftware_Model_Copernica_ProfileOrder extends Copernica_MarketingSoftware_Model_Copernica_Profile
31
  {
32
  /**
33
- * @var Copernica_MarketingSoftware_Model_Abstraction_Customer
34
  */
35
  protected $order = false;
36
 
@@ -44,6 +44,33 @@ class Copernica_MarketingSoftware_Model_Copernica_ProfileOrder extends Copernica
44
  $this->order = $order;
45
  return $this;
46
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
 
48
  /**
49
  * Retrieve the data for this object
@@ -69,9 +96,18 @@ class Copernica_MarketingSoftware_Model_Copernica_ProfileOrder extends Copernica
69
  // fetch the name object
70
  $name = $address->name();
71
 
 
 
 
 
 
 
 
 
 
72
  // return an array with customer data
73
  return array(
74
- 'customer_id' => Mage::helper('marketingsoftware')->generateCustomerId($address->email(), (string)$this->order->storeview()),
75
  'store_view' => (string)$this->order->storeview(),
76
  'firstname' => is_object($name) ? $name->firstname() : null,
77
  'middlename' => is_object($name) ? $name->middlename() : null,
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
 
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
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,
app/code/community/Copernica/MarketingSoftware/Model/Copernica/Profilequote.php CHANGED
@@ -30,7 +30,7 @@
30
  class Copernica_MarketingSoftware_Model_Copernica_ProfileQuote extends Copernica_MarketingSoftware_Model_Copernica_Profile
31
  {
32
  /**
33
- * @var Copernica_MarketingSoftware_Model_Abstraction_Customer
34
  */
35
  protected $quote = false;
36
 
@@ -44,6 +44,33 @@ class Copernica_MarketingSoftware_Model_Copernica_ProfileQuote extends Copernica
44
  $this->quote = $quote;
45
  return $this;
46
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
 
48
  /**
49
  * Retrieve the data for this object
@@ -54,10 +81,16 @@ class Copernica_MarketingSoftware_Model_Copernica_ProfileQuote extends Copernica
54
  // Get the addresses
55
  $addresses = $this->quote->addresses();
56
 
 
 
 
57
  // Select an address
58
- if (count($addresses) == 1) $address = $addresses[0];
59
- else foreach ($addresses as $addr) if (in_array('billing', $addr->type())) $address = $addr;
60
-
 
 
 
61
  // Get subscription
62
  if (Mage::getModel('newsletter/subscriber')->loadByEmail($address->email())->getId())
63
  {
@@ -67,11 +100,20 @@ class Copernica_MarketingSoftware_Model_Copernica_ProfileQuote extends Copernica
67
  else $subscription = false;
68
 
69
  // fetch the name object
70
- $name = $address->name();
 
 
 
 
 
 
 
 
 
71
 
72
  // return an array with customer data
73
  return array(
74
- 'customer_id' => Mage::helper('marketingsoftware')->generateCustomerId($address->email(), (string)$this->quote->storeview()),
75
  'store_view' => (string)$this->quote->storeview(),
76
  'firstname' => is_object($name) ? $name->firstname() : null,
77
  'middlename' => is_object($name) ? $name->middlename() : null,
@@ -81,4 +123,4 @@ class Copernica_MarketingSoftware_Model_Copernica_ProfileQuote extends Copernica
81
  'newsletter' => is_object($subscription) ? $subscription->status() : 'unknown',
82
  );
83
  }
84
- }
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
 
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
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
  {
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,
123
  'newsletter' => is_object($subscription) ? $subscription->status() : 'unknown',
124
  );
125
  }
126
+ }
app/code/community/Copernica/MarketingSoftware/Model/Copernica/Profilesubscription.php CHANGED
@@ -45,6 +45,24 @@ class Copernica_MarketingSoftware_Model_Copernica_ProfileSubscription extends Co
45
  $this->subscription = $customer;
46
  return $this;
47
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
 
49
  /**
50
  * Retrieve the data for this object.
@@ -53,9 +71,16 @@ class Copernica_MarketingSoftware_Model_Copernica_ProfileSubscription extends Co
53
  */
54
  protected function _data()
55
  {
 
 
 
 
 
 
 
56
  // return an array with customer data
57
  return array(
58
- 'customer_id' => Mage::helper('marketingsoftware')->generateCustomerId($this->subscription->email(), (string)$this->subscription->storeView()),
59
  'store_view' => (string)$this->subscription->storeView(),
60
  'email' => $this->subscription->email(),
61
  'group' => Mage::getModel('customer/group')->load(0)->getCode(),
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
  /**
68
  * Retrieve the data for this object.
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(),
app/code/community/Copernica/MarketingSoftware/Model/Copernica/Viewedproduct/Subprofile.php CHANGED
@@ -1,113 +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
- * 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()
66
- {
67
- return Mage::helper('marketingsoftware/config')->getLinkedViewedProductFields();
68
- }
69
-
70
- /**
71
- * Get the required fields
72
- * @return array
73
- */
74
- public function requiredFields()
75
- {
76
- return array('id');
77
- }
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(),
101
- 'name' => $product->name(),
102
- 'sku' => $product->sku(),
103
- 'attribute_set' => $product->attributeSet(),
104
- 'weight' => $product->weight(),
105
- 'total_price' => $product->price(),
106
- 'url' => $product->productUrl($storeId),
107
- 'image' => $product->imageUrl($storeId),
108
- 'categories' => implode("\n", $categories),
109
- 'attributes' => (string)$product->attributes(),
110
- 'timestamp' => $product->timestamp()
111
- );
112
- }
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
+ * 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()
66
+ {
67
+ return Mage::helper('marketingsoftware/config')->getLinkedViewedProductFields();
68
+ }
69
+
70
+ /**
71
+ * Get the required fields
72
+ * @return array
73
+ */
74
+ public function requiredFields()
75
+ {
76
+ return array('product_id');
77
+ }
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(),
101
+ 'name' => $product->name(),
102
+ 'sku' => $product->sku(),
103
+ 'attribute_set' => $product->attributeSet(),
104
+ 'weight' => $product->weight(),
105
+ 'total_price' => $product->price(),
106
+ 'url' => $product->productUrl($storeId),
107
+ 'image' => $product->imageUrl($storeId),
108
+ 'categories' => implode("\n", $categories),
109
+ 'attributes' => (string)$product->attributes(),
110
+ 'timestamp' => $product->timestamp()
111
+ );
112
+ }
113
  }
app/code/community/Copernica/MarketingSoftware/Model/Error.php DELETED
@@ -1,140 +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 file holds the implmentation of the Copernica_MarketingSoftware_Model_Error class.
29
- * Copernica Marketing Software v 1.2.0
30
- * March 2011
31
- * http://www.copernica.com/
32
- */
33
-
34
- /**
35
- * All error codes that are defined
36
- */
37
- define('COPERNICAERROR_UNREACHABLE', 1); // The SOAP API is unreachable
38
- define('COPERNICAERROR_LOGINFAILURE', 2); // The login information for the API is not correct
39
- define('COPERNICAERROR_NODATABASE', 3); // The database does not exist, or could not be created
40
- define('COPERNICAERROR_INVALIDDATABASE', 4); // The database structure is wrong
41
- define('COPERNICAERROR_NOCOLLECTION', 5); // The collection does not exist, or could not created
42
- define('COPERNICAERROR_DEFAULTFIELDSEXISTS', 6); // The database default fields already exists
43
- define('COPERNICAERROR_NOFIELD', 7); // The field doesn't exists
44
- define('COPERNICAERROR_CANNOTCREATEFIELD', 8); // The field can't be created
45
- define('COPERNICAERROR_NOCOLLECTIONFIELDS', 9); // The default collection fields set doesn't exists
46
- define('COPERNICAERROR_NOCOLLECTIONFIELDSMISSING', 10); // There aren't any default collection fields missing
47
- define('COPERNICAERROR_PCNTLNOTENABLED', 11); // The PCNTL module is not enabled
48
- define('COPERNICAERROR_CLINOTENABLED', 12); // The CLI is not enabled
49
- define('COPERNICAERROR_SOAPNOTENABLED', 13); // The SOAP is not enabled
50
- define('COPERNICAERROR_CURLNOTENABLED', 14); // The CURL is not enabled
51
- define('COPERNICAERROR_CANNOTSEARCHPROFILE', 15); // Cannot search profile
52
- define('COPERNICAERROR_CANNOTUPDATESUBPROFILE', 16); // Cannot update subprofile
53
- define('COPERNICAERROR_CANNOTMATCHPROFILE', 17); // Cannot match the profile
54
- define('COPERNICAERROR_CANNOTREMOVESUBPROFILE', 18); // Cannot remove the subprofile
55
- define('COPERNICAERROR_LOGINNOTVALID', 19); // The login credentials/hostname are not valid
56
- define('COPERNICAERROR_FIELDLINKSNOTVALID', 20); // Not all required fields are linked
57
- define('COPERNICAERROR_UPGRADEWARNING118', 21); // We upgraded to this version, so a warning has to given
58
- define('COPERNICAERROR_UNRECOGNIZEDEVENT', 22);
59
-
60
- /**
61
- * Instances of the Copernica_MarketingSoftware_Model_Error class are returned whenever our Model and API
62
- * objects need to report an error
63
- */
64
- class CopernicaError extends Exception
65
- {
66
- /**
67
- * Construct the exception
68
- * @param integer numeric value indicating a certain error message
69
- */
70
- public function __construct($code)
71
- {
72
- $this->code = $code;
73
- parent::__construct($this->getDescription(), $code);
74
- }
75
-
76
- /**
77
- * Return an string representation
78
- * @return string
79
- */
80
- public function __toString()
81
- {
82
- return $this->getDescription();
83
- }
84
-
85
- /**
86
- * Return an error description
87
- * @return string
88
- */
89
- public function getDescription()
90
- {
91
- // we loop throught our error codes to send back the error description
92
- switch ($this->code)
93
- {
94
- case COPERNICAERROR_UNREACHABLE:
95
- return "Invalid hostname. Please use the following example as format: http://publisher.copernica.nl";
96
- case COPERNICAERROR_LOGINFAILURE:
97
- return "Invalid credentials, please check your accountname, username and password.";
98
- case COPERNICAERROR_NODATABASE:
99
- return "The database does not exist";
100
- case COPERNICAERROR_INVALIDDATABASE:
101
- return "notvalid";
102
- case COPERNICAERROR_NOCOLLECTION:
103
- return "One of the collections does not exist";
104
- case COPERNICAERROR_DEFAULTFIELDSEXISTS:
105
- return "The database default fields already exists";
106
- case COPERNICAERROR_NOFIELD:
107
- return "notexists";
108
- case COPERNICAERROR_CANNOTCREATEFIELD:
109
- return "notexists";
110
- case COPERNICAERROR_NOCOLLECTIONFIELDS:
111
- return "notvalid";
112
- case COPERNICAERROR_NOCOLLECTIONFIELDSMISSING:
113
- return "There aren't any default collection fields missing";
114
- case COPERNICAERROR_PCNTLNOTENABLED:
115
- return "Warning: The PCNTL module is not enabled in your PHP installation. You need this module to activate Copernica background processing feature.";
116
- case COPERNICAERROR_CLINOTENABLED:
117
- return "Warning: The CLI (command line) module is not enabled in your PHP installation. You need this module to activate Copernica background processing feature.";
118
- case COPERNICAERROR_SOAPNOTENABLED:
119
- return "Error: The SOAP module is not enabled in your PHP installation. You need this module to activate Copernica extension";
120
- case COPERNICAERROR_CURLNOTENABLED:
121
- return "Error: The CURL module is not enabled in your PHP installation, and 'allow_url_fopen' is set to 'Off' in your php.ini configuration. To activate the Copernica extension, either the allow_url_fopen setting should be set to 'On' or the CURL module should be installed";
122
- case COPERNICAERROR_LOGINNOTVALID:
123
- return "Error: The supplied hostname and/or credentials are not correct, you must <a href=\"../settings/\">correct this</a> in order to make the plugin function.";
124
- case COPERNICAERROR_FIELDLINKSNOTVALID:
125
- return "Error: Some of the required fields are not linked, you must <a href=\"../link/\">correct this</a> in order to make the plugin function.";
126
- case COPERNICAERROR_CANNOTSEARCHPROFILE:
127
- return "Cannot search profile";
128
- case COPERNICAERROR_CANNOTUPDATESUBPROFILE:
129
- return "Cannot update subprofile";
130
- case COPERNICAERROR_CANNOTMATCHPROFILE:
131
- return "Cannot match the profile";
132
- case COPERNICAERROR_CANNOTREMOVESUBPROFILE:
133
- return "Cannot remove the subprofile";
134
- case COPERNICAERROR_UNRECOGNIZEDEVENT:
135
- return "This event has not been recognized.";
136
- default:
137
- return "There has been an error with your request";
138
- }
139
- }
140
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app/code/community/Copernica/MarketingSoftware/Model/ErrorQueue.php ADDED
@@ -0,0 +1,118 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 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()
45
+ {
46
+ return unserialize(parent::getData('object'));
47
+ }
48
+
49
+ /**
50
+ * Set object instance
51
+ * @param object
52
+ */
53
+ public function setObject($object)
54
+ {
55
+ return parent::setData('object', serialize($object));
56
+ }
57
+
58
+ /**
59
+ * Set customer Id
60
+ * @param int
61
+ */
62
+ public function setCustomerId($customerId)
63
+ {
64
+ return parent::setData('customer', $customerId);
65
+ }
66
+
67
+ /**
68
+ * Get customer Id
69
+ * @return int
70
+ */
71
+ public function getCustomerId()
72
+ {
73
+ return parent::getData('customer');
74
+ }
75
+
76
+ /**
77
+ * Save model
78
+ */
79
+ public function save()
80
+ {
81
+ return parent::save();
82
+ }
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
+ }
app/code/community/Copernica/MarketingSoftware/Model/Mysql4/AbandonedCart.php ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ * 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
+ }
app/code/community/Copernica/MarketingSoftware/Model/Mysql4/AbandonedCart/Collection.php ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
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/Config.php CHANGED
@@ -1,33 +1,33 @@
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_Model_Mysql4_Config extends Mage_Core_Model_Mysql4_Abstract
28
- {
29
- protected function _construct()
30
- {
31
- $this->_init('marketingsoftware/config_data', 'config_id');
32
- }
33
  }
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_Model_Mysql4_Config extends Mage_Core_Model_Mysql4_Abstract
28
+ {
29
+ protected function _construct()
30
+ {
31
+ $this->_init('marketingsoftware/config_data', 'config_id');
32
+ }
33
  }
app/code/community/Copernica/MarketingSoftware/Model/Mysql4/Config/Collection.php CHANGED
@@ -1,33 +1,33 @@
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_Model_Mysql4_Config_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract
28
- {
29
- protected function _construct()
30
- {
31
- $this->_init('marketingsoftware/config');
32
- }
33
  }
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_Model_Mysql4_Config_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract
28
+ {
29
+ protected function _construct()
30
+ {
31
+ $this->_init('marketingsoftware/config');
32
+ }
33
  }
app/code/community/Copernica/MarketingSoftware/Model/Mysql4/ErrorQueue.php ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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_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
+ }
app/code/community/Copernica/MarketingSoftware/Model/Mysql4/ErrorQueue/Collection.php ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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_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
+ }
app/code/community/Copernica/MarketingSoftware/Model/Mysql4/ProfileCache.php ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 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
+ }
app/code/community/Copernica/MarketingSoftware/Model/Mysql4/ProfileCache/Collection.php ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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_Model_Mysql4_ProfileCache_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract
28
+ {
29
+ protected function _construct()
30
+ {
31
+ $this->_init('marketingsoftware/profileCache');
32
+ }
33
+ }
app/code/community/Copernica/MarketingSoftware/Model/Mysql4/Queue.php CHANGED
@@ -33,4 +33,34 @@ class Copernica_MarketingSoftware_Model_Mysql4_Queue extends Mage_Core_Model_Mys
33
  {
34
  $this->_init('marketingsoftware/queue', 'id');
35
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
  }
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
  }
app/code/community/Copernica/MarketingSoftware/Model/Mysql4/Queue/Collection.php CHANGED
@@ -1,64 +1,67 @@
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_Model_Mysql4_Queue_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract
28
- {
29
- protected function _construct()
30
- {
31
- $this->_init('marketingsoftware/queue');
32
- }
33
-
34
- /**
35
- * Add a default order, sorted ascending by queue time
36
- * @return Copernica_MarketingSoftware_Model_Mysql4_Queue_Collection
37
- */
38
- public function addDefaultOrder()
39
- {
40
- // If a result was processed before, we should process it if we have nothing
41
- // else to process, we want to import the queue items without an result_time
42
- // first and then in order of queue time.
43
- return $this->addOrder('result_time', self::SORT_ORDER_ASC)
44
- ->addOrder('queue_time', self::SORT_ORDER_ASC);
45
- }
46
-
47
- /**
48
- * Get the time of the oldest record
49
- * @return string mysql formatted date timestamp
50
- */
51
- public function getQueueStartTime()
52
- {
53
- return $this->addDefaultOrder()->setPageSize(1)->getFirstItem()->getQueueTime();
54
- }
55
-
56
- /**
57
- * Get the result of the oldest record
58
- * @return string message
59
- */
60
- public function getOldestResult()
61
- {
62
- return $this->addDefaultOrder()->setPageSize(1)->getFirstItem()->getResult();
63
- }
 
 
 
64
  }
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_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
+ {
56
+ return $this->addDefaultOrder()->setPageSize(1)->getFirstItem()->getQueueTime();
57
+ }
58
+
59
+ /**
60
+ * Get the result of the oldest record
61
+ * @return string message
62
+ */
63
+ public function getOldestResult()
64
+ {
65
+ return $this->addDefaultOrder()->setPageSize(1)->getFirstItem()->getResult();
66
+ }
67
  }
app/code/community/Copernica/MarketingSoftware/Model/Mysql4/SyncProfile.php ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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_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
+ }
app/code/community/Copernica/MarketingSoftware/Model/Mysql4/SyncProfile/Collection.php ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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_Model_Mysql4_SyncProfile_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract
28
+ {
29
+ protected function _construct()
30
+ {
31
+ $this->_init('marketingsoftware/syncProfile');
32
+ }
33
+ }
app/code/community/Copernica/MarketingSoftware/Model/Observer.php CHANGED
@@ -1,404 +1,442 @@
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
- * Observer object.
29
- *
30
- */
31
- class Copernica_MarketingSoftware_Model_Observer
32
- {
33
- /**
34
- * Method for the following events:
35
- * 'checkout_controller_onepage_save_shipping_method'
36
- * 'checkout_controller_multishipping_shipping_post'
37
- * This method is fired during checkout process, after the customer has entered billing address
38
- * and saved the shipping method
39
- * @param $observer Varien_Event_Observer
40
- */
41
- public function checkoutSaveStep(Varien_Event_Observer $observer)
42
- {
43
- // if the plug-in is not enabled, skip this
44
- if (!$this->enabled()) return;
45
-
46
- // Do we have a valid item?
47
- if ($quote = $observer->getEvent()->getQuote())
48
- {
49
- // wrap the object
50
- $object = Mage::getModel('marketingsoftware/abstraction_quote')->setOriginal($quote);
51
-
52
- // This quote should be added to the queue
53
- $queue = Mage::getModel('marketingsoftware/queue')
54
- ->setObject($object)
55
- ->save();
56
- }
57
- }
58
-
59
- /**
60
- * Method for event 'sales_quote_item_delete_before'.
61
- * An item is removed from a quote
62
- */
63
- public function quoteItemRemoved(Varien_Event_Observer $observer)
64
- {
65
- // if the plug-in is not enabled, skip this
66
- if (!$this->enabled()) return;
67
-
68
- // Do we have a valid item?
69
- if ($item = $observer->getEvent()->getItem())
70
- {
71
- if ($item->getParentItemId()) {
72
- //this item has a parent so its data is already contained within the parent item
73
- return;
74
- }
75
-
76
- if (!$item->getQuote()->getCustomerId()) {
77
- //this item cannot be linked to a customer, so is not relevant at this moment
78
- return;
79
- }
80
-
81
- // wrap the object
82
- $object = Mage::getModel('marketingsoftware/abstraction_quote_item')->setOriginal($item);
83
-
84
- // This quote item should be added to the queue
85
- $queue = Mage::getModel('marketingsoftware/queue')
86
- ->setObject($object)
87
- ->setAction('remove')
88
- ->save();
89
- }
90
- }
91
-
92
- /**
93
- * Method for event 'sales_quote_item_save_after'.
94
- * An item is added or modified
95
- */
96
- public function quoteItemModified(Varien_Event_Observer $observer)
97
- {
98
- // if the plug-in is not enabled, skip this
99
- if (!$this->enabled()) return;
100
-
101
- // Do we have a valid item?
102
- if ($item = $observer->getEvent()->getItem())
103
- {
104
- // unfortunately hasDataChanges is only available in Mage 1.5 and up
105
- if (!$item->hasDataChanges() && method_exists($item, 'hasDataChanges')) {
106
- //an event is triggered every time the object is saved, even when nothing has changed
107
- //for example, when an item is added to the quote
108
- //however, the update date may have changed (even by 1 second) which will trigger a new queue item any way
109
- return;
110
- }
111
-
112
- if ($item->getParentItemId()) {
113
- //this item has a parent so its data is already contained within the parent item
114
- return;
115
- }
116
-
117
- if (!$item->getQuote()->getCustomerId()) {
118
- //this item cannot be linked to a customer, so is not relevant at this moment
119
- return;
120
- }
121
-
122
- // wrap the object
123
- $object = Mage::getModel('marketingsoftware/abstraction_quote_item')->setOriginal($item);
124
-
125
- // This quote item should be added to the queue
126
- $queue = Mage::getModel('marketingsoftware/queue')
127
- ->setObject($object)
128
- ->setAction($item->isObjectNew() ? 'add' : 'modify')
129
- ->save();
130
- }
131
- }
132
-
133
- /**
134
- * Method for event 'sales_order_save_after'.
135
- * An order is added or modified
136
- */
137
- public function orderModified(Varien_Event_Observer $observer)
138
- {
139
- // if the plug-in is not enabled, skip this
140
- if (!$this->enabled()) return;
141
-
142
- // Do we have a valid item?
143
- if ($order = $observer->getEvent()->getOrder())
144
- {
145
- if (!$order->getState()) {
146
- //if an order has no state, it will get one in the next call (usually a few seconds later)
147
- return;
148
- }
149
-
150
- // wrap the object
151
- $object = Mage::getModel('marketingsoftware/abstraction_order')->setOriginal($order);
152
-
153
- // This order should be added to the queue
154
- $queue = Mage::getModel('marketingsoftware/queue')
155
- ->setObject($object)
156
- ->setAction($order->isObjectNew() ? 'add' : 'modify')
157
- ->save();
158
- }
159
- }
160
-
161
- /**
162
- * Method for event 'newsletter_subscriber_delete_before'.
163
- * The newsletter subscription is deleted, do something with it,
164
- */
165
- public function newsletterSubscriptionRemoved(Varien_Event_Observer $observer)
166
- {
167
- // if the plug-in is not enabled, skip this
168
- if (!$this->enabled()) return;
169
-
170
- // Do we have a valid item?
171
- if ($subscriber = $observer->getEvent()->getSubscriber())
172
- {
173
- // wrap the object
174
- $object = Mage::getModel('marketingsoftware/abstraction_subscription')->setOriginal($subscriber);
175
-
176
- // This newsletter subscription should be added to the queue
177
- $queue = Mage::getModel('marketingsoftware/queue')
178
- ->setObject($object)
179
- ->setAction('remove')
180
- ->save();
181
- }
182
- }
183
-
184
- /**
185
- * Method for event 'newsletter_subscriber_save_after'.
186
- * The newsletter subscription is added or modified
187
- */
188
- public function newsletterSubscriptionModified(Varien_Event_Observer $observer)
189
- {
190
- // if the plug-in is not enabled, skip this
191
- if (!$this->enabled()) return;
192
-
193
- // Do we have a valid item?
194
- if ($subscriber = $observer->getEvent()->getSubscriber())
195
- {
196
- // unfortunately hasDataChanges is only available in Mage 1.5 and up
197
- if (!$subscriber->hasDataChanges() && method_exists($subscriber, 'hasDataChanges')) {
198
- // an event is triggered every time the object is saved, even when nothing has changed
199
- // for example, when an order is placed
200
- return;
201
- }
202
-
203
- // wrap the object
204
- $object = Mage::getModel('marketingsoftware/abstraction_subscription')->setOriginal($subscriber);
205
-
206
- // This newsletter subscription should be added to the queue
207
- $queue = Mage::getModel('marketingsoftware/queue')
208
- ->setObject($object)
209
- ->setAction($subscriber->isObjectNew() ? 'add' : 'modify')
210
- ->save();
211
- }
212
- }
213
-
214
- /**
215
- * Method for event 'customer_delete_before'.
216
- * The customer is deleted, do something with it,
217
- */
218
- public function customerRemoved(Varien_Event_Observer $observer)
219
- {
220
- // if the plug-in is not enabled, skip this
221
- if (!$this->enabled()) return;
222
-
223
- // Do we have a valid item?
224
- if ($customer = $observer->getEvent()->getCustomer())
225
- {
226
- // wrap the object
227
- $object = Mage::getModel('marketingsoftware/abstraction_customer')->setOriginal($customer);
228
-
229
- // This customer should be added to the queue
230
- $queue = Mage::getModel('marketingsoftware/queue')
231
- ->setObject($object)
232
- ->setAction('remove')
233
- ->save();
234
- }
235
- }
236
-
237
- /**
238
- * Method for event 'customer_save_after'.
239
- * The customer is added or modified, do something with it,
240
- */
241
- public function customerModified(Varien_Event_Observer $observer)
242
- {
243
- // if the plug-in is not enabled, skip this
244
- if (!$this->enabled()) return;
245
-
246
- // Do we have a valid item?
247
- if ($customer = $observer->getEvent()->getCustomer())
248
- {
249
- // wrap the object
250
- $object = Mage::getModel('marketingsoftware/abstraction_customer')->setOriginal($customer);
251
-
252
- // This customer should be added to the queue
253
- $queue = Mage::getModel('marketingsoftware/queue')
254
- ->setObject($object)
255
- ->setAction($customer->isObjectNew() ? 'add' : 'modify')
256
- ->save();
257
- }
258
- }
259
-
260
- /**
261
- * Method for event 'sales_quote_item_save_after'.
262
- * An item is added or modified
263
- */
264
- public function productViewed(Varien_Event_Observer $observer)
265
- {
266
- // if the plug-in is not enabled, skip this
267
- if (!$this->enabled()) return;
268
-
269
- // Do we have a valid item?
270
- if ($item = $observer->getEvent()->getProduct())
271
- {
272
- $customer = Mage::getSingleton('customer/session')->getCustomer();
273
- $id = $customer->getId();
274
-
275
- if (!$id) {
276
- //this item cannot be linked to a customer, so is not relevant at this moment
277
- return;
278
- }
279
-
280
- // wrap the object
281
- $object = Mage::getModel('marketingsoftware/abstraction_viewedproduct')->setOriginal($item, $id);
282
-
283
- // This quote item should be added to the queue
284
- $queue = Mage::getModel('marketingsoftware/queue')
285
- ->setObject($object)
286
- ->setAction('add')
287
- ->save();
288
- }
289
- }
290
-
291
-
292
- /**
293
- * Is the Copernica module enabled?
294
- *
295
- * @return boolean
296
- */
297
- protected function enabled()
298
- {
299
- // get the result from the helper
300
- return Mage::helper('marketingsoftware')->enabled();
301
- }
302
-
303
- /**
304
- * Process the queue. This function will stop running after 180
305
- * seconds. It will then start processing the rest of the queue
306
- * the next time the cron event is fired.
307
- *
308
- */
309
- public function processQueue()
310
- {
311
- // get the config
312
- $config = Mage::helper('marketingsoftware/config');
313
- $config->setLastStartTimeCronjob(date("Y-m-d H:i:s"));
314
-
315
- // if the plug-in is not enabled, skip this
316
- if (!$this->enabled()) return;
317
-
318
- // Set the the time limit to a high number
319
- set_time_limit(0);
320
- $collection = Mage::getResourceModel('marketingsoftware/queue_collection')
321
- ->addDefaultOrder()->setPageSize(300);
322
-
323
- // store the start time
324
- $time = time();
325
-
326
- // are there any items?
327
- if (count($collection) == 0)
328
- {
329
- // set some debug info
330
- $config->setLastCronjobProcessedTasks(0);
331
- $config->setLastEndTimeCronjob(date("Y-m-d H:i:s"));
332
- return true;
333
- }
334
-
335
- try {
336
- // Perform some basic checks first
337
- Mage::getSingleton('marketingsoftware/marketingsoftware')->api()->check(true);
338
- } catch (CopernicaError $e) {
339
- // log the message
340
- Mage::log("Copernica/marketingSoftware: ".(string)$e);
341
-
342
- // Do not process any records
343
- return;
344
- } catch(Exception $e) {
345
- Mage::logException($e);
346
- return;
347
- }
348
-
349
- // get the number of processed iterations
350
- $processedTasks = 0;
351
-
352
- // iterate over the collection
353
- foreach ($collection as $queue)
354
- {
355
- // Is the timer already expired
356
- if (time() > ($time + 3 * 60)) break;
357
-
358
- try
359
- {
360
- // we still have time, so lets process an item from the queue
361
- $success = $queue->process();
362
-
363
- // the processing is successful, remove the item from the queue
364
- if ($success)
365
- {
366
- // increment the counter for the processed tasks
367
- $processedTasks++;
368
-
369
- // also delete the item from the queue
370
- $queue->delete();
371
- }
372
- }
373
- catch(Exception $e)
374
- {
375
- // If its not an Copernica Error than get the message
376
- if ($e instanceOf CopernicaError)
377
- {
378
- // assign the message
379
- $message = (string)$e;
380
- }
381
- else
382
- {
383
- // Log the message and the exception
384
- $message = '['.get_class($e).'] '.$e->getMessage();
385
-
386
- // Write the error to the log
387
- Mage::logException($e);
388
- }
389
-
390
- // store the log message
391
- Mage::log($message);
392
-
393
- // write the error to the database and store the time of the error
394
- $queue->setResult($message)
395
- ->setResultTime(date('Y-m-d H:i:s'))
396
- ->save();
397
- }
398
- }
399
-
400
- // Finished
401
- $config->setLastCronjobProcessedTasks($processedTasks);
402
- $config->setLastEndTimeCronjob(date("Y-m-d H:i:s"));
403
- }
404
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ * Observer object.
29
+ */
30
+ 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
+
45
+ /**
46
+ * Method for the following events:
47
+ * 'checkout_controller_onepage_save_shipping_method'
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
+ ->serName('quote')
225
+ ->setEntityId($quote->getId())
226
+ ->save();
227
+ }
228
+
229
+ /**
230
+ * Method for event 'sales_order_save_after'.
231
+ * An order is added or modified
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
+ }
281
+
282
+ /**
283
+ * Method for event 'newsletter_subscriber_save_after'.
284
+ * The newsletter subscription is added or modified
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
+ }
313
+
314
+ /**
315
+ * Method for event 'customer_delete_before'.
316
+ * The customer is deleted, do something with it,
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
+
342
+ /**
343
+ * Method for event 'customer_save_after'.
344
+ * The customer is added or modified, do something with it,
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
+
370
+ /**
371
+ * Method for event 'sales_quote_item_save_after'.
372
+ * An item is added or modified
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
+
399
+ /**
400
+ * This function should run in fixed time period, and it's suppouse to detect
401
+ * all carts that were forgotten by clients
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
+
415
+ /**
416
+ * Is the Copernica module enabled?
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
+
426
+ /**
427
+ * This function will process current queue. Note that not whole queue can
428
+ * be processed in one run. User can specify the time and number of items
429
+ * to be processed in one run.
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
+ }
app/code/community/Copernica/MarketingSoftware/Model/PomSoapClient.php DELETED
@@ -1,309 +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 Soap Client
29
- * @version 1.1
30
- */
31
- class Copernica_MarketingSoftware_Model_PomSoapClient extends SoapClient
32
- {
33
- /**
34
- * Sets connection settings
35
- * @param string URL of the application
36
- * @param string Login name
37
- * @param string Account name
38
- * @param string Password
39
- */
40
- public function __construct($connectionSettings)
41
- {
42
- // parameters for the SOAP connection
43
- $params = array(
44
- 'soap_version' => SOAP_1_1,
45
- 'cache_wsdl' => WSDL_CACHE_BOTH,
46
- 'compression' => SOAP_COMPRESSION_ACCEPT | SOAP_COMPRESSION_GZIP,
47
- );
48
-
49
- // url of the wsdl
50
- $url = $connectionSettings['url']."?SOAPAPI=WSDL";
51
-
52
- // create connection
53
- parent::__construct($url, $params);
54
-
55
- Varien_Profiler::start('Copernica SOAP call: login');
56
-
57
- // try to login
58
- // @todo check session time
59
- $result = $this->login(array(
60
- 'username' => $connectionSettings['login'],
61
- 'password' => $connectionSettings['password'],
62
- 'account' => $connectionSettings['account'],
63
- ));
64
-
65
- Varien_Profiler::stop('Copernica SOAP call: login');
66
- }
67
-
68
- /**
69
- * Method that handles the calls to the API
70
- * @param string Name of the method
71
- * @param array Associative array of parameters
72
- * @return mixed
73
- */
74
- public function __call($methodname, $params)
75
- {
76
- // one parameter is required
77
- $params = count($params) == 0 ? array() : $params[0];
78
-
79
- // check if the first param was an array
80
- if (!is_array($params)) trigger_error("Invalid parameters, array is required");
81
-
82
- // convert the parameters
83
- foreach ($params as $key => $value)
84
- {
85
- // check the type of the value, and so some conversions
86
- if (self::isAssoc($value)) {
87
- $params[$key] = self::encodeAssoc($value);
88
- } elseif (is_array($value)) {
89
- $params[$key] = self::encodeArray($value);
90
- } elseif (is_object($value)) {
91
- $params[$key] = self::encodeObject($value);
92
- } else {
93
- $params[$key] = $value;
94
- }
95
- }
96
-
97
- // convert the parameters to an object
98
- $params = self::toObject($params);
99
-
100
- // call the method
101
- $result = parent::__call($methodname, array($params));
102
-
103
- return self::decodeResult($result);
104
- }
105
-
106
- /**
107
- * Helper method that converts the result
108
- * @param object with the result
109
- * @return mixed
110
- */
111
- protected static function decodeResult($result)
112
- {
113
- // is this a regular, scalar, result?
114
- if (isset($result->value)) return $result->value;
115
-
116
- // is this an array result?
117
- if (isset($result->array))
118
- {
119
- // check if there are items
120
- if (!isset($result->array->item)) return array();
121
-
122
- // get the items, and make sure they are an array
123
- $items = $result->array->item;
124
- return array($items);
125
- }
126
-
127
- // is this an assoc result
128
- if (isset($result->map))
129
- {
130
- // check if there are pairs
131
- if (!isset($result->map->pair)) return array();
132
-
133
- // get the pairs and make sure they are an array
134
- $pairs = $result->map->pair;
135
- if (!is_array($pairs)) $pairs = array($pairs);
136
-
137
- // loop through the pairs and convert them to an array
138
- $result = array();
139
- foreach ($pairs as $pair) $result[$pair->key] = $pair->value;
140
- return $result;
141
- }
142
-
143
- // is this a collection?
144
- if (isset($result->start) && isset($result->length) && isset($result->total) && isset($result->items))
145
- {
146
- // empty array
147
- $items = array();
148
-
149
- // what is the name of the collection?
150
- $vars = array_keys(get_object_vars($result->items));
151
- foreach (array_unique($vars) as $membername)
152
- {
153
- // get the members
154
- $members = isset($result->items->$membername) ? $result->items->$membername : array();
155
- if (!is_array($members)) $members = array($members);
156
-
157
- // loop through the members
158
- foreach ($members as $member)
159
- {
160
- // replace the items
161
- $items[] = self::decodeObject($member);
162
- }
163
- }
164
-
165
- // done
166
- $result->items = $items;
167
- return $result;
168
- }
169
-
170
- // finally, we assume this is an entity
171
- $vars = array_keys(get_object_vars($result));
172
- if (count($vars) == 0) return false;
173
- $membername = $vars[0];
174
-
175
- // return just the member
176
- return self::decodeObject($result->$membername);
177
- }
178
-
179
- /**
180
- * Encode an associative array to be used as parameter
181
- * @param associative array
182
- * @return array
183
- */
184
- protected static function encodeAssoc($array)
185
- {
186
- // we are going to construct an array of pairs
187
- $pairs = array();
188
-
189
- // loop through all keys and values in the array
190
- foreach ($array as $key => $value)
191
- {
192
- // check if the assoc array is nested
193
- if ((!is_scalar($key) && !is_null($key)) || (!is_scalar($value) && !is_null($value)))
194
- {
195
- trigger_error('Nested assoc array is not supported');
196
- continue;
197
- }
198
-
199
- // create a pair
200
- $pairs[] = self::toObject(array('key' => $key, 'value' => $value));
201
- }
202
-
203
- // done
204
- return $pairs;
205
- }
206
-
207
- /**
208
- * Encode a normal array to be used as parameter
209
- * @param Normal array
210
- * @return array
211
- */
212
- protected static function encodeArray($array)
213
- {
214
- // the result array
215
- $result = array();
216
-
217
- // loop through the values
218
- foreach ($array as $value)
219
- {
220
- // array values should be objects
221
- if (is_object($value)) $result[] = self::encodeObject($value);
222
- elseif (is_array($value)) trigger_error('Invalid parameter: arrays of objects are not supported');
223
- else $result[] = $value;
224
- }
225
-
226
- // done
227
- return $result;
228
- }
229
-
230
- /**
231
- * Encode an object to be used as parameter
232
- * @param object
233
- * @return object
234
- */
235
- protected static function encodeObject($object)
236
- {
237
- // result object
238
- $result = new stdClass();
239
-
240
- // loop through the object vars
241
- foreach (get_object_vars($object) as $key => $value)
242
- {
243
- // check if the assoc array is nested
244
- if ((!is_scalar($key) && !is_null($key)) || (!is_scalar($value) && !is_null($value)))
245
- {
246
- trigger_error('Nested object is not supported');
247
- continue;
248
- }
249
-
250
- // add the var
251
- $result->$key = $value;
252
- }
253
-
254
- // done
255
- return $result;
256
- }
257
-
258
- /**
259
- * Decode an object to be used as result
260
- * @param object
261
- * @return object
262
- */
263
- protected static function decodeObject($object)
264
- {
265
- // result object
266
- $result = new stdClass();
267
-
268
- // loop through the object vars
269
- foreach (get_object_vars($object) as $key => $value)
270
- {
271
- if (is_object($value)) $value = self::decodeObject($value);
272
-
273
- // add the var
274
- $result->$key = $value;
275
- }
276
-
277
- // done
278
- return $result;
279
- }
280
-
281
- /**
282
- * Helper function checks if an array is associative
283
- * @param array
284
- * @return boolean
285
- */
286
- public static function isAssoc($array)
287
- {
288
- if (!is_array($array)) return false;
289
- foreach (array_keys($array) as $k => $v)
290
- {
291
- if ($k !== $v) return true;
292
- }
293
- return false;
294
- }
295
-
296
- /**
297
- * Helper function that maps an assoc array to an object
298
- * @param associative array
299
- * @return object
300
- */
301
- public static function toObject($array)
302
- {
303
- $object = new stdClass();
304
- foreach ($array as $key => $value) $object->$key = $value;
305
- return $object;
306
- }
307
- }
308
-
309
- ?>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app/code/community/Copernica/MarketingSoftware/Model/ProfileCache.php ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 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()
45
+ {
46
+ return parent::getData('profile_id');
47
+ }
48
+
49
+ /**
50
+ * Get the customer Id
51
+ */
52
+ public function getCustomerId()
53
+ {
54
+ return parent::getData('customer_id');
55
+ }
56
+
57
+ /**
58
+ * Set profile Id
59
+ * @param int
60
+ * @return Copernica_MarketingSoftware_Model_ProfileCache
61
+ */
62
+ public function setProfileId($profileId)
63
+ {
64
+ parent::setData('profile_id', $profileId);
65
+
66
+ return $this;
67
+ }
68
+
69
+ /**
70
+ * Set customer Id
71
+ * @param string
72
+ * @return Copernica_MarketingSoftware_Model_ProfileCache
73
+ */
74
+ public function setCustomerId($customerId)
75
+ {
76
+ parent::setData('customer_id', $customerId);
77
+
78
+ return $this;
79
+ }
80
+ }
app/code/community/Copernica/MarketingSoftware/Model/Queue.php CHANGED
@@ -1,94 +1,253 @@
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
- * 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 the data from the model
43
- *
44
- * @return mixed
45
- */
46
- public function getObject()
47
- {
48
- // return the value
49
- return unserialize(parent::getData('object'));
50
- }
51
-
52
- /**
53
- * Set the data to the model
54
- *
55
- * @return mixed
56
- */
57
- public function setObject($object)
58
- {
59
- // set the value from the parent implementation
60
- return parent::setData('object', serialize($object));
61
- }
62
-
63
- /**
64
- * Process this item in the queue. Returns true if the event was
65
- * successfully processed, otherwise returns false.
66
- *
67
- * @return boolean
68
- */
69
- public function process()
70
- {
71
- // Get QueueEvent classname
72
- $factory = Mage::getSingleton('marketingsoftware/QueueEvent_Factory');
73
-
74
- // Wrap the event in a command object (pattern alarm!)
75
- $event = $factory->get($this);
76
-
77
- // call the process function on the object
78
- return $event->process();
79
- }
80
-
81
- /**
82
- * Function to save the correct queue time
83
- *
84
- * @return Copernica_MarketingSoftware_Model_Queue
85
- */
86
- public function save()
87
- {
88
- // save the queuetime
89
- $this->setQueueTime(date("Y-m-d H:i:s"));
90
-
91
- // rely on parent
92
- return parent::save();
93
- }
94
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ * 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()
46
+ {
47
+ return $this->getResource()->getFirstFree();
48
+ }
49
+
50
+ /**
51
+ * Get the data from the model
52
+ *
53
+ * @return mixed
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
+ /**
170
+ * Process this item in the queue. Returns true if the event was
171
+ * successfully processed, otherwise returns false.
172
+ *
173
+ * @return boolean
174
+ */
175
+ public function process()
176
+ {
177
+ $event = Copernica_MarketingSoftware_Model_QueueEvent_Abstract::create($this);
178
+
179
+ return $event->process();
180
+ }
181
+
182
+ /**
183
+ * When saving queue event we should check if it should be saved. When on
184
+ * queue is already same event we don't have to save it again. This way
185
+ * we can save some CPU and network.
186
+ * Also if we are inserting 'remove' event there is no point in syncing all
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
+ }
app/code/community/Copernica/MarketingSoftware/Model/QueueEvent/Abstract.php CHANGED
@@ -1,63 +1,119 @@
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
- require_once(dirname(__FILE__).'/../Error.php');
28
- /**
29
- * A wrapper object around an event
30
- */
31
- abstract class Copernica_MarketingSoftware_Model_QueueEvent_Abstract
32
- {
33
- /**
34
- * What queue item was used to construct this item
35
- * @var Copernica_MarketingSoftware_Model_Queue
36
- */
37
- protected $queueItem;
38
-
39
- /**
40
- * Construct the item given the queueitem
41
- *
42
- * @param Copernica_MarketingSoftware_Model_Queue $queueItem
43
- */
44
- public function __construct($queueItem)
45
- {
46
- $this->queueItem = $queueItem;
47
- }
48
-
49
- /**
50
- * Get the object for this queue item
51
- * @return Abstraction object
52
- */
53
- protected function getObject()
54
- {
55
- return $this->queueItem->getObject();
56
- }
57
-
58
- /**
59
- * Process this item in the queue
60
- * @return boolean was the processing successfull
61
- */
62
- public abstract function process();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
  }
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 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
+ * 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/Customer.php ADDED
@@ -0,0 +1,161 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 actions performed with customer object.
29
+ * So any modification or additions should go via this class. Also this class
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
+ {
41
+ return $this->actionModify();
42
+ }
43
+
44
+ /**
45
+ * Handle customer modify action. This action should be called everytime
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
+
75
+ /**
76
+ * Handle customer full sync action. This action should be called only when
77
+ * we want to sync whole customer. For example initial data sync when
78
+ * extension is installed for 1st time, or when changing copernica database.
79
+ *
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
+
112
+ /**
113
+ * This action should be called only when there is a need to remove a customer
114
+ * from copernica platform.
115
+ *
116
+ * @return boolean
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
+ }
app/code/community/Copernica/MarketingSoftware/Model/QueueEvent/CustomerAdd.php CHANGED
@@ -1,52 +1,52 @@
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::getSingleton('marketingsoftware/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
  }
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 CHANGED
@@ -1,122 +1,242 @@
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
- * 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::getSingleton('marketingsoftware/marketingsoftware')->api();
40
-
41
- // Get the customer object
42
- $customer = $this->getObject();
43
-
44
- // Get the customer
45
- $customerData = Mage::getModel('marketingsoftware/copernica_profilecustomer')
46
- ->setCustomer($customer)
47
- ->setDirection('copernica');
48
-
49
- // Update the profiles given the customer and return the found profiles
50
- $api->updateProfiles($customerData);
51
- $profiles = $api->searchProfiles($customerData->id());
52
-
53
- // Process all the profiles
54
- foreach ($profiles->items as $profile)
55
- {
56
- // add all addresses to the address collection
57
- foreach ($customer->addresses() as $address)
58
- {
59
- // Get the information of this item
60
- $addressData = Mage::getModel('marketingsoftware/copernica_address_subprofile')
61
- ->setAddress($address)
62
- ->setDirection('copernica');
63
-
64
- // Update the subprofile of this profile
65
- $api->updateAddressSubProfiles($profile->id, $addressData);
66
- }
67
-
68
- $processedQuotes = array();
69
-
70
- // Add all the orders + items
71
- foreach ($customer->orders() as $order)
72
- {
73
- // Add an record to the order collection of the profile.
74
- // Get the order data to prepare for sending it to Copernica
75
- $orderData = Mage::getModel('marketingsoftware/copernica_order_subprofile')
76
- ->setOrder($order)
77
- ->setDirection('copernica');
78
-
79
- // Update the subprofile in the order collection
80
- $api->updateOrderSubProfile($profile->id, $orderData);
81
-
82
- // add all order items to the order items collection
83
- foreach ($order->items() as $orderItem)
84
- {
85
- // Get the information of this item
86
- $itemData = Mage::getModel('marketingsoftware/copernica_orderitem_subprofile')
87
- ->setOrderItem($orderItem)
88
- ->setDirection('copernica');
89
-
90
- // Update the subprofile of this profile
91
- $api->updateOrderItemSubProfiles($profile->id, $itemData);
92
- }
93
-
94
- // Add this orders quote_id to the list of processed quotes
95
- $processedQuotes[] = $order->quoteId();
96
- }
97
-
98
- // add all the cart items
99
- foreach ($customer->quotes() as $quote)
100
- {
101
- // this has alread become an order
102
- if (in_array($quote->id(), $processedQuotes)) continue;
103
-
104
- // iterate over the items to add them to the cart items collection
105
- foreach ($quote->items() as $quoteItem)
106
- {
107
- // Get the cart item data
108
- $cartItemData = Mage::getModel('marketingsoftware/copernica_cartitem_subprofile')
109
- ->setQuoteItem($quoteItem)
110
- ->setDirection('copernica')
111
- ->setStatus('basket');
112
-
113
- // add / update the quote item
114
- $api->updateCartItemSubProfiles($profile->id, $cartItemData);
115
- }
116
- }
117
- }
118
-
119
- // this was processed
120
- return true;
121
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
122
  }
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 CHANGED
@@ -1,52 +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 wrapper object around an event
29
- */
30
- class Copernica_MarketingSoftware_Model_QueueEvent_CustomerModify 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::getSingleton('marketingsoftware/marketingsoftware')->api();
40
-
41
- // Get the customer
42
- $customerData = Mage::getModel('marketingsoftware/copernica_profilecustomer')
43
- ->setCustomer($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
  }
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 CHANGED
@@ -1,52 +1,59 @@
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 the copernica API
39
- $api = Mage::getSingleton('marketingsoftware/marketingsoftware')->api();
40
-
41
- // Get the customer
42
- $customerData = Mage::getModel('marketingsoftware/copernica_profilecustomer')
43
- ->setCustomer($customer = $this->getObject())
44
- ->setDirection('copernica');
45
-
46
- // Remove the profiles given the customer
47
- $api->removeProfiles($customerData);
48
-
49
- // this customer is processed
50
- return true;
51
- }
 
 
 
 
 
 
 
52
  }
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 ADDED
@@ -0,0 +1,89 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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/Factory.php CHANGED
@@ -1,88 +1,95 @@
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
- require_once(dirname(__FILE__).'/../Error.php');
28
-
29
- /**
30
- * Factory class for getting queue event objects
31
- */
32
- class Copernica_MarketingSoftware_Model_QueueEvent_Factory
33
- {
34
- /**
35
- * Get the right object
36
- *
37
- * @return Copernica_MarketingSoftware_Model_QueueEvent_Abstract
38
- */
39
- public function get($queueItem)
40
- {
41
- // If we want to start a full synchronisation, we should return a start sync object
42
- if ($queueItem->getAction() == 'start_sync') {
43
- $classname = Mage::getConfig()->getModelClassName('marketingsoftware/QueueEvent_StartSync');
44
- return new $classname($queueItem);
45
- }
46
-
47
- // Prepare the action, to append it to the classname
48
- $action = ucfirst($queueItem->getAction());
49
-
50
- // What kind of class is given
51
- switch (get_class($queueItem->getObject()))
52
- {
53
- case "Copernica_MarketingSoftware_Model_Abstraction_Quote":
54
- $classname = "marketingsoftware/QueueEvent_Quote".$action;
55
- break;
56
-
57
- case "Copernica_MarketingSoftware_Model_Abstraction_Quote_Item":
58
- $classname = "marketingsoftware/QueueEvent_QuoteItem".$action;
59
- break;
60
-
61
- case "Copernica_MarketingSoftware_Model_Abstraction_Customer":
62
- $classname = "marketingsoftware/QueueEvent_Customer".$action;
63
- break;
64
-
65
- case "Copernica_MarketingSoftware_Model_Abstraction_Order":
66
- $classname = "marketingsoftware/QueueEvent_Order".$action;
67
- break;
68
-
69
- case "Copernica_MarketingSoftware_Model_Abstraction_Subscription":
70
- $classname = "marketingsoftware/QueueEvent_Subscription".$action;
71
- break;
72
-
73
- case "Copernica_MarketingSoftware_Model_Abstraction_Viewedproduct":
74
- $classname = "marketingsoftware/QueueEvent_ViewedProduct".$action;
75
- break;
76
- }
77
-
78
- // No classname, throw an error
79
- if (!isset($classname)) throw new CopernicaError(COPERNICAERROR_UNRECOGNIZEDEVENT);
80
-
81
- // Get correct classname
82
- $classname = Mage::getConfig()->getModelClassName($classname);
83
- if (!class_exists($classname)) throw new CopernicaError(COPERNICAERROR_UNRECOGNIZEDEVENT);
84
-
85
- // construct the object
86
- return new $classname($queueItem);
87
- }
 
 
 
 
 
 
 
88
  }
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
+ * 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
  }
app/code/community/Copernica/MarketingSoftware/Model/QueueEvent/Item.php ADDED
@@ -0,0 +1,178 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 ADDED
@@ -0,0 +1,167 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 CHANGED
@@ -1,35 +1,35 @@
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
  }
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 CHANGED
@@ -1,107 +1,171 @@
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
- * Process this item in the queue
34
- * @return boolean was the processing successfull
35
- */
36
- public function process()
37
- {
38
- // Get the copernica API and config helper
39
- $api = Mage::getSingleton('marketingsoftware/marketingsoftware')->api();
40
-
41
- // Get the subscription which has been modified
42
- $order = $this->getObject();
43
-
44
- // is there a customer?
45
- if (is_object($customer = $order->customer()))
46
- {
47
- $customerData = Mage::getModel('marketingsoftware/copernica_profilecustomer')
48
- ->setCustomer($customer);
49
- }
50
- else
51
- {
52
- $customerData = Mage::getModel('marketingsoftware/copernica_profileorder')
53
- ->setOrder($order);
54
- }
55
-
56
- // The direction should be set
57
- $customerData->setDirection('copernica');
58
-
59
- // Update the profiles given the customer and return the found profiles
60
- $api->updateProfiles($customerData);
61
- $profiles = $api->searchProfiles($customerData->id());
62
-
63
- // Process all the profiles
64
- foreach ($profiles->items as $profile)
65
- {
66
- // Remove any cart items belonging to this quote, which have not been
67
- // deleted
68
- $api->removeOldCartItems($profile->id, $order->quoteId());
69
-
70
- // Add an record to the order collection of the profile.
71
- // Get the order data to prepare for sending it to Copernica
72
- $orderData = Mage::getModel('marketingsoftware/copernica_order_subprofile')
73
- ->setOrder($order)
74
- ->setDirection('copernica');
75
-
76
- // Update the subprofile in the order collection
77
- $api->updateOrderSubProfile($profile->id, $orderData);
78
-
79
- // add all order items to the order items collection
80
- foreach ($order->items() as $orderItem)
81
- {
82
- // Get the information of this item
83
- $itemData = Mage::getModel('marketingsoftware/copernica_orderitem_subprofile')
84
- ->setOrderItem($orderItem)
85
- ->setDirection('copernica');
86
-
87
- // Update the subprofile of this profile
88
- $api->updateOrderItemSubProfiles($profile->id, $itemData);
89
- }
90
-
91
- // add all addresses to the address collection
92
- foreach ($order->addresses() as $address)
93
- {
94
- // Get the information of this item
95
- $addressData = Mage::getModel('marketingsoftware/copernica_address_subprofile')
96
- ->setAddress($address)
97
- ->setDirection('copernica');
98
-
99
- // Update the subprofile of this profile
100
- $api->updateAddressSubProfiles($profile->id, $addressData);
101
- }
102
- }
103
-
104
- // This order was successfully synchronized
105
- return true;
106
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
107
  }
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/Quote.php ADDED
@@ -0,0 +1,103 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 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
+ }
app/code/community/Copernica/MarketingSoftware/Model/QueueEvent/QuoteItem.php CHANGED
@@ -1,78 +1,124 @@
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
- * Process this item in the queue
34
- * @return boolean was the processing successfull
35
- */
36
- public function process()
37
- {
38
- // Get the copernica API and config helper
39
- $api = Mage::getSingleton('marketingsoftware/marketingsoftware')->api();
40
-
41
- // Get the subscription which has been modified
42
- $quoteItem = $this->getObject();
43
- $quote = $quoteItem->quote();
44
-
45
- // if there is no customer, we cannot process this so return immediately
46
- if (!is_object($customer = $quote->customer())) return true;
47
-
48
- // Get the customer
49
- $customerData = Mage::getModel('marketingsoftware/copernica_profilecustomer')
50
- ->setCustomer($quote->customer())
51
- ->setDirection('copernica');
52
-
53
- // Get the profiles from the api
54
- $api->updateProfiles($customerData);
55
- $profiles = $api->searchProfiles($customerData->id());
56
-
57
- // Get the cart item data
58
- $cartItemData = Mage::getModel('marketingsoftware/copernica_cartitem_subprofile')
59
- ->setQuoteItem($quoteItem)
60
- ->setDirection('copernica')
61
- ->setStatus($this->status());
62
-
63
- // Iterate over the matching profiles and add / update the quote item
64
- foreach ($profiles->items as $profile)
65
- {
66
- $api->updateCartItemSubProfiles($profile->id, $cartItemData);
67
- }
68
-
69
- // all went allright
70
- return true;
71
- }
72
-
73
- /**
74
- * In what status is this cart item
75
- * @return String
76
- */
77
- abstract protected function status();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78
  }
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/QuoteItemAdd.php CHANGED
@@ -1,40 +1,40 @@
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_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
  }
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_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
  }
app/code/community/Copernica/MarketingSoftware/Model/QueueEvent/QuoteItemModify.php CHANGED
@@ -1,41 +1,41 @@
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
  }
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 CHANGED
@@ -1,41 +1,41 @@
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
  }
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 CHANGED
@@ -1,82 +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
- * A wrapper object around an event
29
- */
30
- class Copernica_MarketingSoftware_Model_QueueEvent_QuoteModify 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 and config helper
39
- $api = Mage::getSingleton('marketingsoftware/marketingsoftware')->api();
40
-
41
- // Get the subscription which has been modified
42
- $quote = $this->getObject();
43
-
44
- // is there a customer?
45
- if (is_object($customer = $quote->customer()))
46
- {
47
- $customerData = Mage::getModel('marketingsoftware/copernica_profilecustomer')
48
- ->setCustomer($customer);
49
- }
50
- else
51
- {
52
- $customerData = Mage::getModel('marketingsoftware/copernica_profilequote')
53
- ->setQuote($quote);
54
- }
55
-
56
- // The direction should be set
57
- $customerData->setDirection('copernica');
58
-
59
- // Update the profiles given the customer and return the found profiles
60
- $api->updateProfiles($customerData);
61
- $profiles = $api->searchProfiles($customerData->id());
62
-
63
- // iterate over the items to add them to the cart items collection
64
- foreach ($quote->items() as $quoteItem)
65
- {
66
- // Get the cart item data
67
- $cartItemData = Mage::getModel('marketingsoftware/copernica_cartitem_subprofile')
68
- ->setQuoteItem($quoteItem)
69
- ->setDirection('copernica')
70
- ->setStatus('basket');
71
-
72
- // Iterate over the matching profiles and add / update the quote item
73
- foreach ($profiles->items as $profile)
74
- {
75
- $api->updateCartItemSubProfiles($profile->id, $cartItemData);
76
- }
77
- }
78
-
79
- // This quote was successfully synchronized
80
- return true;
81
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82
  }
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/StartSync.php CHANGED
@@ -1,268 +1,290 @@
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_StartSync extends Copernica_MarketingSoftware_Model_QueueEvent_Abstract
31
- {
32
- /**
33
- * @var integer store the start time
34
- */
35
- protected $startTime;
36
-
37
- /**
38
- * @var integer store the time limit
39
- */
40
- protected $timeLimit;
41
-
42
-
43
- /**
44
- * Process this item in the queue
45
- * @return boolean was the processing successfull
46
- */
47
- public function process()
48
- {
49
- // store the start time
50
- $this->startTime = time();
51
-
52
- // store the time limit
53
- $this->timeLimit = 3*60;
54
-
55
- // A page size to limit the data in memory
56
- $pageSize = 20;
57
-
58
- // add all the customers to the queue
59
- if (!$this->_addCustomersToQueue($pageSize)) return false;
60
-
61
- // add all the orders (which don't have a customer) to the queue
62
- if (!$this->_addOrdersToQueue($pageSize)) return false;
63
-
64
- // add the newsletter subscriptions to the queue
65
- if (!$this->_addSubscriptionsToQueue($pageSize)) return false;
66
-
67
- // It succeeded
68
- return true;
69
- }
70
-
71
- /**
72
- * Add all the customers to the queue
73
- * this piece of code has been improved by Cream (www.cream.nl)
74
- * @param integer page size
75
- */
76
- protected function _addCustomersToQueue($pageSize)
77
- {
78
- // Get the config helper
79
- $config = Mage::helper('marketingsoftware/config');
80
-
81
- // get the customers,
82
- // this piece of code has been improved by Cream (www.cream.nl)
83
- $customers = Mage::getModel('customer/customer')
84
- ->getCollection()
85
- ->setPageSize($pageSize)
86
- ->addAttributeToSort('updated_at')
87
- ->addAttributeToFilter('store_id', array('notnull' => true))
88
- ->addAttributeToFilter('website_id', array('notnull' => true))
89
- ->addAttributeToFilter('updated_at', array(
90
- 'from' => $config->getCustomerProgressStatus()
91
- ));
92
-
93
- // Get the id of last customer which has been processed via this synchronisation
94
- $progressDateTime = $config->getCustomerProgressStatus();
95
-
96
- // iterate over the pages with customers
97
- for ($page = 1; $page <= $customers->getLastPageNumber(); $page++)
98
- {
99
- // load the data for this page
100
- $customers->setPage($page, $pageSize)->load();
101
-
102
- // iterate over the customers
103
- foreach ($customers as $customer)
104
- {
105
- // Was this record changed or modified after the last synchronisation
106
- if ($customer->getCreatedAt() < $progressDateTime &&
107
- $customer->getUpdatedAt() < $progressDateTime ) continue;
108
-
109
- // wrap the object
110
- $object = Mage::getModel('marketingsoftware/abstraction_customer')->loadCustomer($customer->getEntityId());
111
-
112
- // This customer should be added to the queue
113
- $queue = Mage::getModel('marketingsoftware/queue')
114
- ->setObject($object)
115
- ->setAction('full')
116
- ->save();
117
-
118
- // Get the maximum progress time
119
- $progressDateTime = max($progressDateTime, $customer->getUpdatedAt());
120
-
121
- // get rid of the customer and the object
122
- unset($customer);
123
- unset($object);
124
- }
125
-
126
- // Store the progress status
127
- $config->setCustomerProgressStatus($progressDateTime);
128
-
129
- // Clear the cached customers
130
- $customers->clear();
131
-
132
- // Did we already spend to long on processing the records
133
- if (time() > ($this->startTime + $this->timeLimit)) return false;
134
- }
135
- // clear the customers variable
136
- unset($customers);
137
-
138
- // Store the progress status
139
- $config->setCustomerProgressStatus(date('Y-m-d H:i:s'));
140
-
141
- // we did complete
142
- return true;
143
- }
144
-
145
- /**
146
- * Add all the orders to the queue, which are placed by guest subscribers
147
- * this piece of code has been improved by Cream (www.cream.nl)
148
- * @param integer page size
149
- */
150
- protected function _addOrdersToQueue($pageSize)
151
- {
152
- // Get the config helper
153
- $config = Mage::helper('marketingsoftware/config');
154
-
155
- // get the orders, which don't have a customer_id
156
- // this piece of code has been improved by Cream (www.cream.nl)
157
- $orders = Mage::getModel('sales/order')
158
- ->getCollection()
159
- ->addAttributeToSort('updated_at')
160
- ->addAttributeToFilter('store_id', array('notnull' => true))
161
- ->addAttributeToFilter('updated_at', array(
162
- 'from' => $config->getOrderProgressStatus()
163
- ));
164
-
165
- // The add field to search filter is not supported in Magento 1.4
166
- //if (is_callable(array($orders, 'addFieldToSearchFilter')))
167
- //{
168
- // $orders->addFieldToSearchFilter('customer_id', 0)
169
- // ->addFieldToSearchFilter('customer_id', array('null' => 1));
170
- //}
171
-
172
- // Set the page size
173
- $orders->setPageSize($pageSize);
174
-
175
- // Get the id of last order which has been processed via this synchronisation
176
- $progressDateTime = $config->getOrderProgressStatus();
177
-
178
- // iterate over the pages with orders
179
- for ($page = 1; $page <= $orders->getLastPageNumber(); $page++)
180
- {
181
- // load the data for this page
182
- $orders->setPage($page, $pageSize)->load();
183
-
184
- // iterate over the orders
185
- foreach ($orders as $order)
186
- {
187
- // Was this record changed or modified after the last synchronisation
188
- if ($order->getCreatedAt() < $progressDateTime &&
189
- $order->getUpdatedAt() < $progressDateTime ) continue;
190
-
191
- // Get the maximum progress time
192
- $progressDateTime = max($progressDateTime, $order->getUpdatedAt());
193
-
194
- // Only sync guest orders
195
- if ($order->getCustomerId()) continue;
196
-
197
- // wrap the object
198
- $object = Mage::getModel('marketingsoftware/abstraction_order')->loadOrder($order->getEntityId());
199
-
200
- // This order should be added to the queue
201
- $queue = Mage::getModel('marketingsoftware/queue')
202
- ->setObject($object)
203
- ->save();
204
-
205
- // get rid of the order and the object
206
- unset($order);
207
- unset($object);
208
- }
209
-
210
- // Store the progress status
211
- $config->setOrderProgressStatus($progressDateTime);
212
-
213
- // Clear the cached orders
214
- $orders->clear();
215
-
216
- // Did we already spend to long on processing the records
217
- if (time() > ($this->startTime + $this->timeLimit)) return false;
218
- }
219
-
220
- // clear the orders variable
221
- unset($orders);
222
-
223
- // Store the progress status
224
- $config->setOrderProgressStatus(date('Y-m-d H:i:s'));
225
-
226
- // we did complete
227
- return true;
228
- }
229
-
230
- /**
231
- * Add all the subscriptions to the queue, which are placed by guest subscribers
232
- * @param integer page size
233
- */
234
- protected function _addSubscriptionsToQueue($pageSize)
235
- {
236
- // Get the config helper
237
- $config = Mage::helper('marketingsoftware/config');
238
-
239
- // get the subscriptions
240
- $subscriptions = Mage::getModel('newsletter/subscriber')
241
- ->getCollection()
242
- ->addFieldToFilter('customer_id', array(0, 'null'))
243
- ->addFieldToFilter('store_id', array('notnull' => true));
244
-
245
- // iterate over the subscriptions
246
- foreach ($subscriptions as $subscription)
247
- {
248
- // wrap the object
249
- $object = Mage::getModel('marketingsoftware/abstraction_subscription')
250
- ->setOriginal($subscription);
251
-
252
- // This subscription should be added to the queue
253
- $queue = Mage::getModel('marketingsoftware/queue')
254
- ->setObject($object)
255
- ->save();
256
-
257
- // get rid of the subscription and the object
258
- unset($subscription);
259
- unset($object);
260
- }
261
-
262
- // clear the subscriptions variable
263
- unset($subscriptions);
264
-
265
- // we did complete
266
- return true;
267
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
268
  }
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 when it's being processed should spaw customer/orders/subscriptions
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
+
77
+ /**
78
+ * Respawn start sync event.
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')
153
+ ->setName('customer')
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')
211
+ ->setName('order')
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
+
229
+ /**
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')
276
+ ->setName('subscription')
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
  }
app/code/community/Copernica/MarketingSoftware/Model/QueueEvent/Subscription.php ADDED
@@ -0,0 +1,102 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 CHANGED
@@ -1,35 +1,35 @@
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
  }
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 CHANGED
@@ -1,81 +1,73 @@
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::getSingleton('marketingsoftware/marketingsoftware')->api();
40
-
41
- // Get the subscription which has been modified
42
- $subscription = $this->getObject();
43
-
44
- // is there a customer?
45
- if (is_object($customer = $subscription->customer()))
46
- {
47
- // Maybe this is an old subscription which is updated, use the subscriber
48
- // identifier, so that the old record is updated, the typo 'subcr' should
49
- // remain there for backwards compatibility
50
- $identifier = 'subcr_'.$subscription->id();
51
-
52
- // get the customer data
53
- $customerData = Mage::getModel('marketingsoftware/copernica_profilecustomer')
54
- ->setCustomer($customer);
55
- }
56
- else
57
- {
58
- // use the normal identifier
59
- $identifier = false;
60
-
61
- // get the customer data
62
- $customerData = Mage::getModel('marketingsoftware/copernica_profilesubscription')
63
- ->setSubscription($subscription);
64
- }
65
-
66
- // The direction should be set
67
- $customerData->setDirection('copernica');
68
-
69
- // Update the profiles given the customer
70
- $api->updateProfiles($customerData);
71
-
72
- // this might result in two profiles with the same customer_id
73
- $profiles = $api->searchProfiles($customerData->id());
74
-
75
- // we received an invalid response
76
- if (!is_object($profiles)) return false;
77
-
78
- // this subscription is processed
79
- return true;
80
- }
81
  }
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 CHANGED
@@ -1,35 +1,35 @@
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
  }
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/View.php ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 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
35
+ * @return boolean
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
+ }
app/code/community/Copernica/MarketingSoftware/Model/QueueEvent/ViewedProductAdd.php CHANGED
@@ -1,68 +1,72 @@
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::getSingleton('marketingsoftware/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
- $customer = Mage::getModel('customer/customer')->load($productData->customerId());
51
-
52
- $storeView = Mage::getModel('marketingsoftware/abstraction_storeview')->setOriginal($tmpStore);
53
-
54
- $copernicaId = Mage::helper('marketingsoftware')->generateCustomerId($customer->getEmail(), (string)$storeView);
55
-
56
- $profiles = $api->searchProfiles($copernicaId);
57
-
58
- // Process all the profiles
59
- foreach ($profiles->items as $profile)
60
- {
61
- // Update the profiles given the customer
62
- $api->updateViewedProductSubProfiles($profile->id, $productData);
63
- }
64
-
65
- // this viewed product is processed
66
- return true;
67
- }
 
 
 
 
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
+ /**
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 ADDED
@@ -0,0 +1,286 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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.php ADDED
@@ -0,0 +1,144 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ /**
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,
141
+ 'email' => $email,
142
+ ));
143
+ }
144
+ }
app/code/community/Copernica/MarketingSoftware/Model/REST/Address.php ADDED
@@ -0,0 +1,111 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ * 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
+ }
app/code/community/Copernica/MarketingSoftware/Model/REST/CartItem.php ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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/Customer.php ADDED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ * 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
+ }
app/code/community/Copernica/MarketingSoftware/Model/REST/Item.php ADDED
@@ -0,0 +1,129 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 ADDED
@@ -0,0 +1,166 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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/Product.php ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ * 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
+ }
app/code/community/Copernica/MarketingSoftware/Model/REST/Quote.php ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ * 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
+ }
app/code/community/Copernica/MarketingSoftware/Model/REST/Subscription.php ADDED
@@ -0,0 +1,115 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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/SyncProfile.php ADDED
@@ -0,0 +1,189 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 be responsible for storing and loading sync profiles.
29
+ *
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()
48
+ {
49
+ return parent::getData('client_key');
50
+ }
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
+
65
+ return $this;
66
+ }
67
+
68
+ /**
69
+ * This function will return client secret
70
+ * @return string
71
+ */
72
+ public function getClientSecret()
73
+ {
74
+ return parent::getData('client_secret');
75
+ }
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
+
88
+ return $this;
89
+ }
90
+
91
+ /**
92
+ * Get access token of this sync profile
93
+ * @return string
94
+ */
95
+ public function getAccessToken()
96
+ {
97
+ return parent::getData('access_token');
98
+ }
99
+
100
+ /**
101
+ * Set access token for this sync profile
102
+ * @param string
103
+ * @return self
104
+ */
105
+ public function setAccessToken($accessToken)
106
+ {
107
+ parent::setData('access_token', $accessToken);
108
+
109
+ return $this;
110
+ }
111
+
112
+ /**
113
+ * Get name of this sync profile
114
+ * @return string
115
+ */
116
+ public function getName()
117
+ {
118
+ return parent::getData('name');
119
+ }
120
+
121
+ /**
122
+ * Set name for this sync profile
123
+ * @param string
124
+ * @return self
125
+ */
126
+ public function setName($name)
127
+ {
128
+ parent::setData('name', $name);
129
+
130
+ return $this;
131
+ }
132
+
133
+ /**
134
+ * This function will return array of stores that this profile affect.
135
+ * @return array
136
+ */
137
+ public function getStores()
138
+ {
139
+ //@todo implement
140
+ return array();
141
+ }
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);
184
+
185
+ //@todo implement
186
+ return $this;
187
+ }
188
+
189
+ }
app/code/community/Copernica/MarketingSoftware/Model/SyncStatus.php ADDED
@@ -0,0 +1,140 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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/AjaxcollectionController.php ADDED
@@ -0,0 +1,324 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 answer any Ajax call that is asking info about Copernica
29
+ * collection.
30
+ */
31
+ class Copernica_MarketingSoftware_Adminhtml_Marketingsoftware_AjaxcollectionController extends Mage_Adminhtml_Controller_Action
32
+ {
33
+ /**
34
+ * Valdiate collection
35
+ */
36
+ public function validateAction()
37
+ {
38
+ // get POST variables
39
+ $post = $this->getRequest()->getPost();
40
+
41
+ try
42
+ {
43
+ // if we don't have a database name we want to tell user that he should provide us with one
44
+ if (!isset($post['databaseName'])) throw Mage::exception('Copernica_MarketingSoftware', 'No valid database', Copernica_MarketingSoftware_Exception::DATABASE_NOT_EXISTS);
45
+
46
+ // if we don't have collection name we want to tell user that his input was wrong
47
+ if (!isset($post['collectionName']) || !isset($post['collectionType'])) throw Mage::exception('Copernica_MarketingSoftware', 'Invalid input', Copernica_MarketingSoftware_Exception::INVALID_INPUT);
48
+
49
+ // try to valdiate collection
50
+ $this->validateCollection($post['databaseName'], $post['collectionName'], $post['collectionType']);
51
+
52
+ // if we are here then everything is just dandy
53
+ $this->setResponse('Collection is valid');
54
+ }
55
+ /**
56
+ * All important to us errors will be reported as custom exceptions.
57
+ * We can handle them here.
58
+ */
59
+ catch (Copernica_MarketingSoftware_Exception $copernicaException)
60
+ {
61
+ // determine if we have a fix for problem
62
+ switch ($copernicaException->getCode())
63
+ {
64
+ case Copernica_MarketingSoftware_Exception::COLLECTION_NOT_EXISTS:
65
+ $fix = 'create';
66
+ break;
67
+ case Copernica_MarketingSoftware_Exception::DATABASE_NOT_EXISTS:
68
+ $fix = 'create database';
69
+ break;
70
+ default:
71
+ $fix = '';
72
+ break;
73
+ }
74
+
75
+ // set response for user
76
+ $this->setResponse($copernicaException->getMessage(), true, $fix);
77
+ }
78
+ /**
79
+ * General exceptions should not happen but, just in case we want to
80
+ * handle them here.
81
+ */
82
+ catch (Exception $exception)
83
+ {
84
+ // tell magento to log exception
85
+ Mage::logException($exception);
86
+
87
+ // we have an critical error
88
+ $this->setResponse('Critical error. Check error logs form more info.', true);
89
+ }
90
+ }
91
+
92
+ /**
93
+ * Action that can be used to create collections
94
+ */
95
+ public function createAction()
96
+ {
97
+ // get POST variables
98
+ $post = $this->getRequest()->getPost();
99
+
100
+ try
101
+ {
102
+ // if we don't have a database name we want to tell user that he should provide us with one
103
+ if (!isset($post['databaseName'])) throw Mage::exception('Copernica_MarketingSoftware', 'No valid database', Copernica_MarketingSoftware_Exception::DATABASE_NOT_EXISTS);
104
+
105
+ // if we don't have collection name we want to tell user that his input was wrong
106
+ if (!isset($post['collectionName']) || !isset($post['collectionType'])) throw Mage::exception('Copernica_MarketingSoftware', 'Invalid input', Copernica_MarketingSoftware_Exception::INVALID_INPUT);
107
+
108
+ // create collection
109
+ $this->createCollection($post['databaseName'], $post['collectionName'], $post['collectionType']);
110
+
111
+ // we are good
112
+ $this->setResponse('Collection was created');
113
+ }
114
+ /**
115
+ * All relevant errors will be reported as exception. We can handle
116
+ * them here.
117
+ */
118
+ catch (Copernica_MarketingSoftware_Exception $copernicaException)
119
+ {
120
+ $this->setResponse($copernicaException->getMessage(), true);
121
+ }
122
+ /**
123
+ * All general exception can be just logged by magento.
124
+ */
125
+ catch (Exception $exception)
126
+ {
127
+ // tell mageton to log exception
128
+ Mage::logException($exception);
129
+
130
+ // tell user that we didn't anything due to critical error
131
+ $this->setResponse('Critical error. Check error logs for more info.', true);
132
+ }
133
+ }
134
+
135
+ /**
136
+ * Action that can be used to get information about collection
137
+ */
138
+ public function infoAction()
139
+ {
140
+ // get POST variables
141
+ $post = $this->getRequest()->getPost();
142
+
143
+ // get response instance
144
+ $response = $this->getResponse();
145
+
146
+ // Ajax response should be encoded with json
147
+ $response->setHeader('Content-Type', 'application/json');
148
+
149
+ // check if we have collection type?
150
+ if (!$post['collectionType'])
151
+ {
152
+ // clear response body
153
+ $response->clearBody();
154
+
155
+ // send nice response
156
+ $response->setBody(json_encode(array(
157
+ 'error' => 1,
158
+ 'message' => 'invalid input'
159
+ )));
160
+
161
+ // we are done here
162
+ return;
163
+ }
164
+
165
+ // get proper collection name
166
+ switch ($post['collectionType']) {
167
+ case 'cartproducts':
168
+ $collectionName = Mage::helper('marketingsoftware/config')->getCartItemsCollectionName();
169
+ break;
170
+ case 'orders':
171
+ $collectionName = Mage::helper('marketingsoftware/config')->getOrdersCollectionName();
172
+ break;
173
+ case 'orderproducts':
174
+ $collectionName = Mage::helper('marketingsoftware/config')->getOrderItemsCollectionName();
175
+ break;
176
+ case 'addresses':
177
+ $collectionName = Mage::helper('marketingsoftware/config')->getAddressCollectionName();
178
+ break;
179
+ case 'viewedproduct':
180
+ $collectionName = Mage::helper('marketingsoftware/config')->getViewedProductCollectionName();
181
+ break;
182
+ default:
183
+ $collectionName = '';
184
+ break;
185
+ }
186
+
187
+ // set the response
188
+ $response->setBody(json_encode(array(
189
+ 'error' => 0,
190
+ 'collectionName' => $collectionName
191
+ )));
192
+ }
193
+
194
+ /**
195
+ * This ajax call can be used to fetch information about certain field in collection
196
+ */
197
+ public function fetchAction()
198
+ {
199
+ // get post variables
200
+ $post = $this->getRequest()->getPost();
201
+
202
+ // prepare ajax response
203
+ $this->prepareAjaxResponse();
204
+
205
+ // get response instance into local scope
206
+ $response = $this->getResponse();
207
+
208
+ // check if we have all required parameters
209
+ if (!isset($post['collectionType']) || !isset($post['magentoField'])) {
210
+ $response->setBody(json_encode(array(
211
+ 'message' => 'Invalid input',
212
+ 'error' => 1
213
+ )));
214
+ }
215
+
216
+ // get collection linked fields
217
+ $collectionLinkedField = $this->getLinkedFieldByCollectionType($post['collectionType']);
218
+
219
+ // check if we have any linked field
220
+ if (empty($collectionLinkedField))
221
+ {
222
+ $response->setBody(json_encode(array(
223
+ 'message' => 'Invalid collection',
224
+ 'error' => 1
225
+ )));
226
+
227
+ // we are done here
228
+ return;
229
+ }
230
+
231
+ // check if we have desired field in supported fields
232
+ if (!array_key_exists($post['magentoField'], $collectionLinkedField))
233
+ {
234
+ $response->setBody(json_encode(array(
235
+ 'message' => 'Invalid field',
236
+ 'error' => 1
237
+ )));
238
+
239
+ // we are done here
240
+ return;
241
+ }
242
+
243
+ // set proper response body
244
+ $response->setBody(json_encode(array(
245
+ 'message' => 'Field data fetched',
246
+ 'fieldData' => $collectionLinkedField[$post['magentoField']],
247
+ 'error' => 0
248
+ )));
249
+ }
250
+
251
+ /**
252
+ * Get all linked field of collection by it's type
253
+ * @param string collection type
254
+ * @return array
255
+ */
256
+ private function getLinkedFieldByCollectionType($collectionType)
257
+ {
258
+ switch ($collectionType) {
259
+ case 'cartproducts': return Mage::helper('marketingsoftware/config')->getLinkedCartItemFields();
260
+ case 'orders': return Mage::helper('marketingsoftware/config')->getLinkedOrderFields();
261
+ case 'orderproducts': return Mage::helper('marketingsoftware/config')->getLinkedOrderItemFields();
262
+ case 'addresses': return Mage::helper('marketingsoftware/config')->getLinkedAddressFields();
263
+ case 'viewedproduct': return Mage::helper('marketingsoftware/config')->getLinkedViewedProductFields();
264
+ default: return array();
265
+ }
266
+ }
267
+
268
+ /**
269
+ * Validate collection.
270
+ * @param string database name
271
+ * @param string collection name
272
+ * @param string collection type
273
+ */
274
+ private function validateCollection($databaseName, $collectionName, $collectionType)
275
+ {
276
+ // tell helper to validata collection
277
+ Mage::helper('marketingsoftware/ApiValidator')->validateCollection($databaseName, $collectionName, $collectionType);
278
+ }
279
+
280
+ /**
281
+ * Create collection
282
+ * @param string database name
283
+ * @param string collection name
284
+ * @param string collection type
285
+ */
286
+ private function createCollection($databaseName, $collectionName, $collectionType)
287
+ {
288
+ // tell helper to create collection
289
+ Mage::helper('marketingsoftware/ApiBuilder')->createCollection($databaseName, $collectionName, $collectionType);
290
+ }
291
+
292
+ /**
293
+ * Prepare response object to fix Ajax communication.
294
+ */
295
+ private function prepareAjaxResponse()
296
+ {
297
+ // get response instance
298
+ $response = $this->getResponse();
299
+
300
+ // clear current response body
301
+ $response->clearBody();
302
+
303
+ // all Ajax responses should be encoded with JSON
304
+ $response->setHeader('Content-Type', 'application/json');
305
+ }
306
+
307
+ /**
308
+ * @param string Message for user
309
+ * @param bool It's an error?
310
+ * @param string Do we have an fix for error?
311
+ */
312
+ private function setResponse($message, $error = false, $fix = '')
313
+ {
314
+ // prepare response
315
+ $this->prepareAjaxResponse();
316
+
317
+ // assign response
318
+ $this->getResponse()->setBody(json_encode(array(
319
+ 'message' => $message,
320
+ 'error' => $error ? 1 : 0,
321
+ 'fix' => ucfirst($fix)
322
+ )));
323
+ }
324
+ }
app/code/community/Copernica/MarketingSoftware/controllers/Adminhtml/Marketingsoftware/AjaxcollectionfieldController.php ADDED
@@ -0,0 +1,167 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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_AjaxcollectionfieldController extends Mage_Adminhtml_Controller_Action
28
+ {
29
+ /**
30
+ * Validata collection field
31
+ */
32
+ public function validateAction()
33
+ {
34
+ // get Post variables
35
+ $post = $this->getRequest()->getPost();
36
+
37
+ try
38
+ {
39
+ // check if all required parameters are in place
40
+ if (!isset($post['databaseName']) || !isset($post['collectionName']) || !isset($post['collectionType']) || !isset($post['copernicaName']) || !isset($post['magentoName']))
41
+ throw Mage::exception('Copernica_MarketingSoftware', 'Invalid input', Copernica_MarketingSoftware_Exception::INVALID_INPUT);
42
+
43
+ // validate field
44
+ Mage::helper('marketingsoftware/ApiValidator')->validateCollectionField($post['databaseName'], $post['collectionName'], $post['collectionType'], $post['magentoName'], $post['copernicaName']);
45
+
46
+ // if we are here that means field is valid
47
+ $this->setResponse('Field valid');
48
+ }
49
+ /**
50
+ * All errors from validation will be reported as exceptions. We will
51
+ * just take care of them here.
52
+ */
53
+ catch (Copernica_MarketingSoftware_Exception $copernicaException)
54
+ {
55
+ // check if we have a fix for error
56
+ switch ($copernicaException->getCode())
57
+ {
58
+ case Copernica_MarketingSoftware_Exception::FIELD_NOT_EXISTS:
59
+ $fix = 'create';
60
+ break;
61
+ case Copernica_MarketingSoftware_Exception::DATABASE_NOT_EXISTS:
62
+ $fix = 'Create database';
63
+ break;
64
+ default:
65
+ $fix = '';
66
+ }
67
+
68
+ // set response
69
+ $this->setResponse($copernicaException->getMessage(), true, $fix);
70
+
71
+ // we are done with this function
72
+ return;
73
+ }
74
+ /**
75
+ * General exception should not happen, but just to be sure we will
76
+ * handle them here. We want to present a proper response to user.
77
+ */
78
+ catch (Exception $exception)
79
+ {
80
+ // tell magento to log exception
81
+ Mage::logException($exception);
82
+
83
+ // set response
84
+ $this->setResponse('Critical error. Check error logs for more info', true);
85
+ }
86
+ }
87
+
88
+ /**
89
+ * Create collection field
90
+ */
91
+ public function createAction()
92
+ {
93
+ // createCollectionField($databaseName, $collectionName, $collectionType, $copernicaName, $magentoName)
94
+
95
+ $post = $this->getRequest()->getPost();
96
+
97
+ try {
98
+ // check if we have all required data
99
+ if (!isset($post['databaseName']) || !isset($post['collectionName']) || !isset($post['collectionType']) || !isset($post['copernicaName']) || !isset($post['magentoName']))
100
+ throw Mage::exception('Copernica_MarketingSoftware', 'Invalid input', Copernica_MarketingSoftware_Exception::INVALID_INPUT);
101
+
102
+ // create field
103
+ Mage::helper('marketingsoftware/ApiBuilder')->createCollectionField($post['databaseName'], $post['collectionName'], $post['collectionType'], $post['copernicaName'], $post['magentoName']);
104
+
105
+ // set success message
106
+ $this->setResponse('Field was created');
107
+ }
108
+ /**
109
+ * All errors from creation will be reported as customer exception.
110
+ * We can handle all errors here.
111
+ */
112
+ catch (Copernica_MarketingSoftware_Exception $copernicaException)
113
+ {
114
+ // set response
115
+ $this->setResponse($copernicaException->getMessage(), true);
116
+
117
+ // we are done with this function
118
+ return;
119
+ }
120
+ /**
121
+ * General exceptions should not happen, but we want to be safe. We will
122
+ * catch them here and tell user that critical one occured.
123
+ */
124
+ catch (Exception $exception)
125
+ {
126
+ // tell magento to log exception
127
+ Mage::logException($exception);
128
+
129
+ // set nice response
130
+ $this->setResponse('Critical error. Check error logs for more info', true);
131
+ }
132
+ }
133
+
134
+ /**
135
+ * Prepare response objrect to be used with AJAX
136
+ */
137
+ private function prepareAjaxResponse()
138
+ {
139
+ // get response instance
140
+ $response = $this->getResponse();
141
+
142
+ // clear current response body
143
+ $response->clearBody();
144
+
145
+ // all Ajax responses are encoded in JSON
146
+ $response->setHeader('Content-Type', 'application/json');
147
+ }
148
+
149
+ /**
150
+ * Set ajax response.
151
+ * @param string the message for user
152
+ * @param bool is it an error?
153
+ * @param string the fix for error
154
+ */
155
+ private function setResponse($message, $error = false, $fix = '')
156
+ {
157
+ // prepare response to be used along ajax
158
+ $this->prepareAjaxResponse();
159
+
160
+ // set response body
161
+ $this->getResponse()->setBody(json_encode(array(
162
+ 'message' => $message,
163
+ 'error' => $error ? 1 : 0,
164
+ 'fix' => ucfirst($fix)
165
+ )));
166
+ }
167
+ }
app/code/community/Copernica/MarketingSoftware/controllers/Adminhtml/Marketingsoftware/AjaxdatabaseController.php ADDED
@@ -0,0 +1,271 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 answer any Ajax call that is asking info about Copernica
29
+ * database.
30
+ */
31
+ class Copernica_MarketingSoftware_Adminhtml_Marketingsoftware_AjaxdatabaseController extends Mage_Adminhtml_Controller_Action
32
+ {
33
+ /**
34
+ * This action should be called when we want to get current info about certain
35
+ * magento field that we can sync with copernica's customer.
36
+ *
37
+ * This function does not return any value. Instead it does modify respone
38
+ * instance. Response instance will hold all relevant data that we will
39
+ * pass to browser.
40
+ */
41
+ public function fetchFieldAction()
42
+ {
43
+ // get post variables
44
+ $post = $this->getRequest()->getPost();
45
+
46
+ // check if we have a name for field
47
+ if (!isset($post['name']))
48
+ {
49
+ // just notify user about error
50
+ $this->setResponse('Invalid input', true);
51
+
52
+ // we are done here
53
+ return;
54
+ }
55
+
56
+ // get linked fields
57
+ $customerFields = Mage::helper('marketingsoftware/config')->getLinkedCustomerFields();
58
+
59
+ // check if we have gived field name in linked ones
60
+ if (!array_key_exists($post['name'], $customerFields) || strlen($customerFields[$post['name']]) == 0)
61
+ {
62
+ // it's not really an error, so just notify
63
+ $this->setResponse('Field not linked');
64
+
65
+ // we are done here
66
+ return;
67
+ }
68
+
69
+ // we have a field, so we can return it's data to user
70
+ $this->setFieldResponse('Field is valid', array(
71
+ 'magentoName' => $post['name'],
72
+ 'copernicaName' => $customerFields[$post['name']]
73
+ ));
74
+ }
75
+
76
+ /**
77
+ * Set response to provide data about database field
78
+ * @param string message about the field
79
+ * @param array field data
80
+ * @param bool do we have an error?
81
+ * @param string do we have a fix for error?
82
+ */
83
+ private function setFieldResponse($message, $fieldData, $error = false, $fix = '')
84
+ {
85
+ // prepare response for ajax
86
+ $this->prepareAjaxResponse();
87
+
88
+ // set response data
89
+ $this->getResponse()->setBody(json_encode(Array(
90
+ 'message' => $message,
91
+ 'error' => $error ? 1 : 0,
92
+ 'fix' => ucfirst($fix),
93
+ 'data' => $fieldData
94
+ )));
95
+ }
96
+
97
+ /**
98
+ * This action should be called when Ajax is asking to validate database.
99
+ *
100
+ * This function does not return any value. Instead it does modify respone
101
+ * instance. Response instance will hold all relevant data that we will
102
+ * pass to browser.
103
+ */
104
+ public function validateAction()
105
+ {
106
+ // get post variables
107
+ $post = $this->getRequest()->getPost();
108
+
109
+ // errors will be reported as exceptions
110
+ try
111
+ {
112
+ // check if we have proper input
113
+ if (!isset($post['name'])) throw Mage::exception('Copernica_MarketingSoftware', 'Missing input', Copernica_MarketingSoftware_Exception::INVALID_INPUT);
114
+
115
+ // validate database by it's name
116
+ $this->validateDatabase($post['name']);
117
+
118
+ // at this point we should be just dandy
119
+ $this->setResponse('Database is valid');
120
+ }
121
+ /*
122
+ * Any relevant for us erros should be thrown an custom exception. We
123
+ * can handle them here.
124
+ */
125
+ catch (Copernica_MarketingSoftware_Exception $copernicaException)
126
+ {
127
+ // check if we have a simple fix
128
+ switch ($copernicaException->getCode())
129
+ {
130
+ case Copernica_MarketingSoftware_Exception::DATABASE_NOT_EXISTS:
131
+ $fix = 'create';
132
+ break;
133
+ case Copernica_MarketingSoftware_Exception::API_ERROR:
134
+ $fix = 'Fix account';
135
+ break;
136
+ default:
137
+ $fix = '';
138
+ }
139
+
140
+ // set the response
141
+ $this->setResponse($copernicaException->getMessage(), true, $fix);
142
+ }
143
+ /*
144
+ * We don't really expect general exceptions here, but better be safe
145
+ * than sorry. We will handle general exception, but we will tell
146
+ * magento to log them and just move along.
147
+ */
148
+ catch (Exception $exception)
149
+ {
150
+ // tell magento to log the exception
151
+ Mage::logException($exception);
152
+
153
+ // tell user that we have a critical error. Admin can check error
154
+ // logs to get more info what just happend
155
+ $this->setResponse('Critical error. Check error log', true);
156
+ }
157
+ }
158
+
159
+ /**
160
+ * Validate database
161
+ * @param string the name of the database that will be validated
162
+ * @throws Copernica_MarketingSoftware_Exception
163
+ */
164
+ private function validateDatabase($databaseName)
165
+ {
166
+ Mage::helper('marketingsoftware/ApiValidator')->validateDatabase($databaseName);
167
+ }
168
+
169
+ /**
170
+ * This action should be called when we Ajax is asking for repair.
171
+ *
172
+ * This function does not return any value. Instead it does modify respone
173
+ * instance. Response instance will hold all relevant data that we will
174
+ * pass to browser.
175
+ */
176
+ public function createAction()
177
+ {
178
+ // get post variables
179
+ $post = $this->getRequest()->getPost();
180
+
181
+ // errors will be reported as exceptions, so we want to handle them here
182
+ try
183
+ {
184
+ // check if we have database name
185
+ if(!isset($post['name'])) throw Mage::exception('Copernica_MarketingSoftware', 'Missing input', Copernica_MarketingSoftware_Exception::INVALID_INPUT);
186
+
187
+ // create database
188
+ $this->createDatabase($post['name']);
189
+
190
+ // we should be good here
191
+ $this->setResponse('Database created');
192
+ }
193
+ /*
194
+ * Any relevant for us erros should be thrown an custom exception. We
195
+ * can handle them here.
196
+ */
197
+ catch (Copernica_MarketingSoftware_Exception $copernicaException)
198
+ {
199
+ // check if we can repair this situation
200
+ switch ($copernicaException->getCode())
201
+ {
202
+ case Copernica_MarketingSoftware_Exception::API_ERROR:
203
+ $fix = 'Fix account';
204
+ break;
205
+ default:
206
+ $fix = '';
207
+ }
208
+
209
+ // set sesponse for browser
210
+ $this->setResponse($copernicaException->getMessage(), true, $fix);
211
+ }
212
+ /*
213
+ * We don't really expect general exceptions here, but better be safe
214
+ * than sorry. We will handle general exception, but we will tell
215
+ * magento to log them and just move along.
216
+ */
217
+ catch (Exception $exception)
218
+ {
219
+ // tell magento to log exception
220
+ Mage::logException($exception);
221
+
222
+ // tell user that we have a critical error.
223
+ $this->setResponse('Critical error. Check error log.', true);
224
+ }
225
+ }
226
+
227
+ /**
228
+ * Tell Api to create a database
229
+ * @param string the database name that will be used
230
+ */
231
+ private function createDatabase($databaseName)
232
+ {
233
+ Mage::helper('marketingsoftware/ApiBuilder')->createDatabase($databaseName);
234
+ }
235
+
236
+ /**
237
+ * Set response that we will sent to browser.
238
+ * Note that this method will SET response. It will override/discard all
239
+ * previous changes and will build up response from scratch.
240
+ * @param string message to show
241
+ * @param bool did we have an error?
242
+ * @param string fix type for error
243
+ */
244
+ private function setResponse($message, $error = false, $fix = '')
245
+ {
246
+ // prepare response for ajax
247
+ $this->prepareAjaxResponse();
248
+
249
+ // set response data
250
+ $this->getResponse()->setBody(json_encode(Array(
251
+ 'message' => $message,
252
+ 'error' => $error ? 1 : 0,
253
+ 'fix' => ucfirst($fix)
254
+ )));
255
+ }
256
+
257
+ /**
258
+ * This method will prepare Ajax response
259
+ */
260
+ private function prepareAjaxResponse()
261
+ {
262
+ // get the response
263
+ $response = $this->getResponse();
264
+
265
+ // clear response body
266
+ $response->clearBody();
267
+
268
+ // all Ajax responses are encoded in JSON
269
+ $response->setHeader('Content-Type', 'application/json');
270
+ }
271
+ }
app/code/community/Copernica/MarketingSoftware/controllers/Adminhtml/Marketingsoftware/AjaxdatabasefieldController.php ADDED
@@ -0,0 +1,192 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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_AjaxdatabasefieldController extends Mage_Adminhtml_Controller_Action
28
+ {
29
+ /**
30
+ * This action should be called when we want to validate database field
31
+ */
32
+ public function validateAction()
33
+ {
34
+ // get POST variables
35
+ $post = $this->getRequest()->getPost();
36
+
37
+ try
38
+ {
39
+ // if we don't have database name or field name, we can not validate
40
+ if (!isset($post['databaseName']) || !isset($post['fieldName']) || !isset($post['magentoField'])) throw Mage::exception('Copernica_MarketingSoftware', 'Invalid input', Copernica_MarketingSoftware_Exception::INVALID_INPUT);
41
+
42
+ // validate field
43
+ $this->validateField($post['databaseName'], $post['fieldName'], $post['magentoField']);
44
+
45
+ // field should be valid
46
+ $this->setResponse('Field is valid');
47
+ }
48
+ /*
49
+ * Any error during validation will be thrown as exception. We can handle
50
+ * them here.
51
+ */
52
+ catch (Copernica_MarketingSoftware_Exception $copernicaException)
53
+ {
54
+ // check if can fix the current problem with field
55
+ switch ($copernicaException->getCode())
56
+ {
57
+ case Copernica_MarketingSoftware_Exception::FIELD_NOT_EXISTS:
58
+ $fix = 'create';
59
+ break;
60
+ case Copernica_MarketingSoftware_Exception::DATABASE_NOT_EXISTS:
61
+ $fix = 'Create database';
62
+ break;
63
+ default:
64
+ $fix = '';
65
+ }
66
+
67
+ // tell user what is wrong
68
+ $this->setResponse($copernicaException->getMessage(), true, $fix);
69
+ }
70
+ /**
71
+ * We don't really expect any general errors here, but just in case
72
+ * we want to catch them and tell magento to log them
73
+ */
74
+ catch (Exception $exception)
75
+ {
76
+ // tell magento to log exception
77
+ Mage::logException($exception);
78
+
79
+ // we have an error
80
+ $this->setResponse('Critical error. Check logs for more information.', true);
81
+ }
82
+ }
83
+
84
+ /**
85
+ * Validata field
86
+ * @param string database name
87
+ * @param string field name
88
+ * @param string magento field name
89
+ */
90
+ private function validateField($databaseName, $fieldName, $magentoField)
91
+ {
92
+ Mage::helper('marketingsoftware/ApiValidator')->validateDatabaseField($databaseName, $fieldName, $magentoField);
93
+ }
94
+
95
+ /**
96
+ * This action should be called when we want to create database field
97
+ */
98
+ public function createAction()
99
+ {
100
+ // get POST variables
101
+ $post = $this->getRequest()->getPost();
102
+
103
+ try
104
+ {
105
+ // check if we have required input
106
+ if (!isset($post['databaseName']) || !isset($post['fieldName']) || !isset($post['magentoField'])) throw Mage::exception('Copernica_MarketingSoftware', 'Invalid input', Copernica_MarketingSoftware_Exception::INVALID_INPUT);
107
+
108
+ // create field
109
+ $this->createField($post['databaseName'], $post['fieldName'], $post['magentoField']);
110
+
111
+ // set the response
112
+ $this->setResponse('Field was created');
113
+ }
114
+ /**
115
+ * All errors will be returned as exceptions. We can handle them here
116
+ */
117
+ catch (Copernica_MarketingSoftware_Exception $copernicaException)
118
+ {
119
+ $this->setResponse($copernicaException->getMessage(), true);
120
+ }
121
+ /**
122
+ * Just to be safe we want to handle all general exceptions here. We
123
+ * will just tell magento to log them and notify user that we did encounter
124
+ * a critical error
125
+ */
126
+ catch (Exception $exception)
127
+ {
128
+ // tell magento to log exception
129
+ Mage::logException($exception);
130
+
131
+ // notify user that we did encounted a critical error
132
+ $this->setResponse('Critical error. Check logs for more information', true);
133
+ }
134
+ }
135
+
136
+ /**
137
+ * Create field inside given database.
138
+ * @param string the name of the database that will be used
139
+ * @param string the name of the field
140
+ * @param string the magento field
141
+ */
142
+ private function createField($databaseName, $fieldName, $magentoField)
143
+ {
144
+ // check what kind of field we want to create
145
+ switch ($magentoField)
146
+ {
147
+ case 'email': Mage::helper('marketingsoftware/ApiBuilder')->createDatabaseEmailField($databaseName, $fieldName);
148
+ break;
149
+ case 'newsletter' : Mage::helper('marketingsoftware/ApiBuilder')->createDatabaseNewsletterField($databaseName, $fieldName);
150
+ break;
151
+ case 'birthdate' : Mage::helper('marketingsoftware/ApiBuilder')->createDatabaseDateField($databaseName, $fieldName);
152
+ break;
153
+ case 'storeView': Mage::helper('marketingsoftware/ApiBuilder')->createDatabaseField($databaseName, $fieldName, array( 'length' => 100 ));
154
+ break;
155
+ case 'registrationDate' : Mage::helper('marketingsoftware/ApiBuilder')->createDatabaseDatetimeField($databaseName, $fieldName);
156
+ break;
157
+ default: Mage::helper('marketingsoftware/ApiBuilder')->createDatabaseField($databaseName, $fieldName);
158
+ break;
159
+ }
160
+ }
161
+
162
+ /**
163
+ * Helper method to prepare response for Ajax
164
+ */
165
+ private function prepareAjaxResponse()
166
+ {
167
+ // get response instance
168
+ $response = $this->getResponse();
169
+
170
+ // clear response body
171
+ $response->clearBody();
172
+
173
+ // all AJAX responsens should be json encoded
174
+ $response->setHeader('Content-Type', 'application/json');
175
+ }
176
+
177
+ /**
178
+ * Helper method to set response
179
+ */
180
+ private function setResponse($message, $error = false, $fix = '')
181
+ {
182
+ // prepare response for AJAX
183
+ $this->prepareAjaxResponse();
184
+
185
+ // set response body
186
+ $this->getResponse()->setBody(json_encode(Array (
187
+ 'message' => $message,
188
+ 'error' => $error ? 1 : 0,
189
+ 'fix' => ucfirst($fix)
190
+ )));
191
+ }
192
+ }
app/code/community/Copernica/MarketingSoftware/controllers/Adminhtml/Marketingsoftware/ExportController.php CHANGED
@@ -1,141 +1,171 @@
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
- * Export Controller takes care of the export data menu.
29
- *
30
- */
31
- class Copernica_MarketingSoftware_Adminhtml_Marketingsoftware_ExportController extends Mage_Adminhtml_Controller_Action
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
- // Call the helper, to validate the settings
41
- Mage::helper('marketingsoftware')->validatePluginBehaviour();
42
-
43
- // Load the layout
44
- $this->loadLayout();
45
-
46
- // The copernica Menu is active
47
- $this->_setActiveMenu('copernica');
48
-
49
- $this->getLayout()
50
- ->getBlock('content')->append(
51
- $this->getLayout()->createBlock('marketingsoftware/adminhtml_marketingsoftware_export')
52
- );
53
- $this->getLayout()->getBlock('head')->setTitle($this->__('Synchronize Data / Copernica Marketing Software / Magento Admin'));
54
-
55
- $this->renderLayout();
56
- }
57
-
58
- /**
59
- * progressAction() takes care of placing a loader
60
- * during the background export action
61
- * Returns a 'completed' or 'in progress' message, depending
62
- * on the state of the sync tool
63
- * @return string
64
- */
65
- public function progressAction()
66
- {
67
- // get the Collection and the helper
68
- $queueCollection = Mage::getResourceModel('marketingsoftware/queue_collection');
69
- $helper = Mage::helper('marketingsoftware');
70
-
71
- // Get the response, set the header and clear the body
72
- $response = $this->getResponse();
73
- $response->setHeader('Content-Type', 'text/plain', true);
74
- $response->clearBody();
75
-
76
- // Send the headers
77
- $response->sendHeaders();
78
-
79
- // Is the synchronisation ready to be started?
80
- if ($helper->isSynchronisationStartScheduled())
81
- {
82
- $string = "Synchronisation scheduled to be started.";
83
- }
84
- elseif ($queueCollection->getSize() > 0)
85
- {
86
- $string = "<b>Number of records</b> : " . $queueCollection->getSize();
87
- $string .= "<br/><b>Oldest record</b> : " . $queueCollection->getQueueStartTime();
88
- }
89
- else
90
- {
91
- $string = 'Idle, no recods in queue.';
92
- }
93
-
94
- // Sent the data
95
- $response->setBody($string);
96
- return;
97
- }
98
-
99
- /**
100
- * getAction() takes care of exporting customers account information
101
- * from Magento to Copernica.
102
- *
103
- * @return string Returns the current page reloaded, containing an information message
104
- */
105
- public function getAction()
106
- {
107
- // get all POST values
108
- $post = $this->getRequest()->getPost();
109
-
110
- // check to see if there is any POST data along
111
- if (empty($post))
112
- {
113
- Mage::getSingleton('adminhtml/session')->addError('Invalid data.');
114
- return $this->_redirect('*/*');
115
- }
116
-
117
- // Get the helper
118
- $helper = Mage::helper('marketingsoftware');
119
-
120
- // Is the synchronisation ready to be started?
121
- if ($helper->isSynchronisationStartScheduled())
122
- {
123
- // The item has been scheduled already
124
- Mage::getSingleton('adminhtml/session')
125
- ->addError('A synchronization has already been scheduled, please be patient for it to finish.');
126
- }
127
- else
128
- {
129
- // The start synch token must be added to the queue
130
- $queue = Mage::getModel('marketingsoftware/queue')
131
- ->setObject(null)
132
- ->setAction('start_sync')
133
- ->save();
134
-
135
- // The item has been scheduled successfully
136
- Mage::getSingleton('adminhtml/session')->addSuccess("The synchronization process has been scheduled!");
137
- }
138
-
139
- return $this->_redirect('*/*');
140
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
141
  }
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
+ * 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
+
65
+ /**
66
+ * progressAction() takes care of placing a loader during the background
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
+ }
105
+
106
+ /**
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
+
140
+ /**
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
  }
app/code/community/Copernica/MarketingSoftware/controllers/Adminhtml/Marketingsoftware/LinkController.php CHANGED
@@ -1,203 +1,303 @@
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
- * Link Controller takes care of the link fields menu.
29
- *
30
- */
31
- class Copernica_MarketingSoftware_Adminhtml_Marketingsoftware_LinkController extends Mage_Adminhtml_Controller_Action
32
- {
33
- /**
34
- * indexAction() takes care of displaying the form which
35
- * contains the details used for the SOAP connection
36
- */
37
- public function indexAction()
38
- {
39
- // Call the helper, to validate the settings
40
- Mage::helper('marketingsoftware')->validatePluginBehaviour();
41
-
42
- // Load the layout
43
- $this->loadLayout();
44
-
45
- // The copernica Menu is active
46
- $this->_setActiveMenu('copernica');
47
-
48
- $this->getLayout()
49
- ->getBlock('content')->append(
50
- $this->getLayout()->createBlock('marketingsoftware/adminhtml_marketingsoftware_link')
51
- );
52
- $this->getLayout()->getBlock('head')->setTitle($this->__('Link Fields / Copernica Marketing Software / Magento Admin'));
53
-
54
- // Add the javascript headers
55
- $this->getLayout()->getBlock('head')->addJs('copernica/marketingsoftware/field.js');
56
- $this->getLayout()->getBlock('head')->addJs('copernica/marketingsoftware/collection.js');
57
- $this->getLayout()->getBlock('head')->addJs('copernica/marketingsoftware/database.js');
58
- $this->getLayout()->getBlock('head')->addJs('varien/form.js');
59
-
60
- // Render the layout
61
- $this->renderLayout();
62
- }
63
-
64
- /**
65
- * Check and process incoming ajax request
66
- * The error description, or 'ok' if not error was detected
67
- * @return string
68
- */
69
- public function checkAjaxAction()
70
- {
71
- // get all post values
72
- $data = $this->getRequest()->getPost();
73
-
74
- // Get the response, set the header and clear the body
75
- $response = $this->getResponse();
76
- $response->setHeader('Content-Type', 'text/plain', true);
77
- $response->clearBody();
78
-
79
- // Send the headers
80
- $response->sendHeaders();
81
-
82
- // check to see if there is any POST data along
83
- if (empty($data))
84
- {
85
- $response->setBody('Invalid Ajax call');
86
- return;
87
- }
88
-
89
- // get access to the copernica API
90
- $api = Mage::getSingleton('marketingsoftware/marketingsoftware')->api();
91
-
92
- // now we need to process the request
93
- switch ($data['type'])
94
- {
95
- case 'check_database':
96
- $result = $api->validateDatabase($data['database']);
97
- break;
98
- case 'repair_database':
99
- case 'create_database':
100
- $result = $api->repairDatabase($data['database']);
101
- break;
102
- case 'check_collection':
103
- $result = $api->validateCollection($data['database'], $data['collection_type'], $data['collection']);
104
- break;
105
- case 'create_collection':
106
- case 'repair_collection':
107
- $result = $api->repairCollection($data['database'], $data['collection_type'], $data['collection']);
108
- break;
109
- case 'check_field':
110
- $result = $api->validateField($data['field_system_name'], $data['field'], $data['database'], $data['collection'] == 'database' ? false : $data['collection'], $data['collectionName']);
111
- break;
112
- case 'repair_field':
113
- case 'create_field':
114
- $result = $api->repairField($data['field_system_name'], $data['field'], $data['database'], $data['collection'] == 'database' ? false : $data['collection'], $data['collectionName']);
115
- break;
116
- default:
117
- $result = "impossible";
118
- }
119
-
120
- // store the result
121
- $response->setBody($result);
122
- }
123
-
124
- /**
125
- * saveProfilesAndCollectionsAction() takes care of saving Customer Profile and Orders/Products Collection details.
126
- * @return Object Returns the '_redirect' object that loads the parent page
127
- */
128
- public function saveProfilesAndCollectionsAction()
129
- {
130
- // get all POST values
131
- $post = $this->getRequest()->getPost();
132
-
133
- // check to see if there is any POST data along
134
- if (empty($post)) Mage::getSingleton('adminhtml/session')->addError('Invalid data.');
135
- else
136
- {
137
- // we set up some arrays to store the content of each section (customer, products, orders)
138
- $customer_array = array();
139
- $cartproducts_array = array();
140
- $orders_array = array();
141
- $orderproducts_array = array();
142
- $address_array = array();
143
- $viewedproduct_array = array();
144
-
145
- // we loop throught the POST data and store each data inside the array it belongs to
146
- foreach ($post as $fieldname => $fieldvalue)
147
- {
148
- if (strpos($fieldname, 'input_customer') !== false)
149
- {
150
- $fieldname = str_replace('input_customer_', '', $fieldname);
151
- $customer_array[$fieldname] = $fieldvalue;
152
- }
153
- elseif (strpos($fieldname, 'input_cartproducts') !== false)
154
- {
155
- $fieldname = str_replace('input_cartproducts_', '', $fieldname);
156
- $cartproducts_array[$fieldname] = $fieldvalue;
157
- }
158
- elseif (strpos($fieldname, 'input_orderproducts') !== false)
159
- {
160
- $fieldname = str_replace('input_orderproducts_', '', $fieldname);
161
- $orderproducts_array[$fieldname] = $fieldvalue;
162
- }
163
- elseif (strpos($fieldname, 'input_orders') !== false)
164
- {
165
- $fieldname = str_replace('input_orders_', '', $fieldname);
166
- $orders_array[$fieldname] = $fieldvalue;
167
- }
168
- elseif (strpos($fieldname, 'input_addresses') !== false)
169
- {
170
- $fieldname = str_replace('input_addresses_', '', $fieldname);
171
- $address_array[$fieldname] = $fieldvalue;
172
- }
173
- elseif (strpos($fieldname, 'input_viewedproduct') !== false)
174
- {
175
- $fieldname = str_replace('input_viewedproduct_', '', $fieldname);
176
- $viewedproduct_array[$fieldname] = $fieldvalue;
177
- }
178
- }
179
-
180
- // store the database and collection names
181
- $config = Mage::helper('marketingsoftware/config')
182
- ->setDatabaseName($post['db_input'])
183
- ->setCartItemsCollectionName($post['cartproducts_input'])
184
- ->setOrdersCollectionName($post['orders_input'])
185
- ->setOrderItemsCollectionName($post['orderproducts_input'])
186
- ->setAddressesCollectionName($post['addresses_input'])
187
- ->setViewedProductCollectionName($post['viewedproduct_input'])
188
- ->setLinkedCustomerFields($customer_array)
189
- ->setLinkedCartItemFields($cartproducts_array)
190
- ->setLinkedOrderFields($orders_array)
191
- ->setLinkedOrderItemFields($orderproducts_array)
192
- ->setLinkedAddressFields($address_array)
193
- ->setLinkedViewedProductFields($viewedproduct_array);
194
-
195
- // add a success notice
196
- Mage::getSingleton('adminhtml/session')->addSuccess('Settings were successfully saved.');
197
- }
198
-
199
- // reload the link fields page
200
- return $this->_redirect('*/*');
201
-
202
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
203
  }
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
+ * 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
35
+ * contains the details used for the SOAP connection
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
+ )));
302
+ }
303
  }
app/code/community/Copernica/MarketingSoftware/controllers/Adminhtml/Marketingsoftware/SettingsController.php CHANGED
@@ -1,150 +1,216 @@
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
- * Settings Controller, which takes care of the settings menu.
29
- *
30
- */
31
- class Copernica_MarketingSoftware_Adminhtml_Marketingsoftware_SettingsController extends Mage_Adminhtml_Controller_Action
32
- {
33
- /**
34
- * indexAction() takes care of displaying the form which
35
- * contains the details used for the SOAP connection
36
- */
37
- public function indexAction()
38
- {
39
- // Call the helper, to validate the settings
40
- Mage::helper('marketingsoftware')->validatePluginBehaviour();
41
-
42
- // Load the layout
43
- $this->loadLayout();
44
-
45
- // The copernica Menu is active
46
- $this->_setActiveMenu('copernica');
47
-
48
- $this->getLayout()
49
- ->getBlock('content')->append(
50
- $this->getLayout()->createBlock('marketingsoftware/adminhtml_marketingsoftware_settings')
51
- );
52
- $this->getLayout()->getBlock('head')->setTitle($this->__('Settings / Copernica Marketing Software / Magento Admin'));
53
-
54
- // Render the layout
55
- $this->renderLayout();
56
- }
57
-
58
- /**
59
- * sendAction() takes care of checking and storing the login details to the SOAP
60
- * It also performs checks on database and in case it doesn't exists, it will create it.
61
- * @return Object Returns the '_redirect' object that loads the parent page
62
- */
63
- public function sendAction()
64
- {
65
- // get all post values from the request
66
- $post = $this->getRequest()->getPost();
67
-
68
- // check to see if there is any POST data along
69
- if (empty($post))
70
- {
71
- Mage::getSingleton('adminhtml/session')->addError('Invalid data.');
72
- }
73
- // we have post data check if its correct
74
- else
75
- {
76
- // check connection based on sent post data
77
- $api = Mage::helper('marketingsoftware/api')->init($post['cp_host'], $post['cp_user'], $post['cp_account'], $post['cp_pass']);
78
-
79
- try
80
- {
81
- // check the data
82
- $result = $api->check();
83
- }
84
- catch(Exception $e)
85
- {
86
- // No valid result has been retrieved
87
- $result = false;
88
-
89
- // An exception is found add it to the session
90
- Mage::getSingleton('adminhtml/session')->addException($e,(string)$e);
91
- }
92
-
93
- // The data is verified, store it
94
- if ($result)
95
- {
96
- // everything is fine store the data in the config
97
- Mage::helper('marketingsoftware/config')
98
- ->setHostname($post['cp_host'])
99
- ->setUsername($post['cp_user'])
100
- ->setAccount($post['cp_account'])
101
- ->setPassword($post['cp_pass']);
102
-
103
- // Settings were successfully stored, add the success message
104
- Mage::getSingleton('adminhtml/session')->addSuccess('Settings were successfully saved.');
105
- }
106
- }
107
-
108
- // load the initial page
109
- return $this->_redirect('*/*');
110
- }
111
-
112
- /**
113
- * recheckAction() takes care of an ajax settings checker
114
- */
115
- public function checkerAction()
116
- {
117
- // Get the response, set the header and clear the body
118
- $response = $this->getResponse();
119
- $response->setHeader('Content-Type', 'text/plain', true);
120
- $response->clearBody();
121
-
122
- // Send the headers
123
- $response->sendHeaders();
124
-
125
- // get all POST values
126
- $post = $this->getRequest()->getPost();
127
-
128
- // check to see if there is any POST data along
129
- if (empty($post))
130
- {
131
- $response->setBody('Invalid Ajax call');
132
- return;
133
- }
134
-
135
- try
136
- {
137
- // check connection based on sent post data
138
- $api = Mage::helper('marketingsoftware/api')->init($post['cp_host'], $post['cp_user'], $post['cp_account'], $post['cp_pass']);
139
-
140
- // check the data
141
- $result = $api->check();
142
- }
143
- catch (Exception $e)
144
- {
145
- $response->setBody((String)$e);
146
- }
147
-
148
- // everything seems to be OK, we already cleared the body
149
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
150
  }
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
+ * 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
35
+ * contains the details used for the SOAP connection
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']));
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
+ /**
177
+ * Since we have to generate a state code for REST API we want to make
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
+ }
187
+
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
+ // check if we have a client key
199
+ if (!isset($post['cp_client_key'])) return $this->_redirect('*/*');
200
+
201
+ // get config to local scope
202
+ $config = Mage::helper('marketingsoftware/config');
203
+
204
+ // set client key inside config file
205
+ $config->setClientKey($post['cp_client_key']);
206
+
207
+ // set client secret inside config gile
208
+ $config->setClientSecret($post['cp_client_secret']);
209
+
210
+ // unset access token
211
+ $config->unsAccessToken();
212
+
213
+ // we will not be doing anything in this method
214
+ return $this->_redirect('*/*');
215
+ }
216
  }
app/code/community/Copernica/MarketingSoftware/controllers/Adminhtml/Marketingsoftware/SyncController.php ADDED
@@ -0,0 +1,198 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ * 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
+
60
+ /**
61
+ * This action will update or create a sync profile with data received from
62
+ * user.
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(),
103
+ $store->getName()
104
+ ));
105
+ }
106
+ }
107
+ }
108
+
109
+ return $stores;
110
+ }
111
+
112
+ /**
113
+ * This action will take care of action that requies us to fetch data about
114
+ * a profile.
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
+ /**
140
+ * This action will handle removal of a profile.
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
+
157
+ /**
158
+ * This action will handle finalization of sync profile. This action should
159
+ * be invoked only when user is returning from Copernica webpage with state
160
+ * and code as query parameters.
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(),
181
+ $params['code'],
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
+ }
app/code/community/Copernica/MarketingSoftware/controllers/ProductController.php CHANGED
@@ -1,275 +1,275 @@
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
- * 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
  }
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
+ * 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
  }
app/code/community/Copernica/MarketingSoftware/controllers/UnsubscribeController.php CHANGED
@@ -1,71 +1,72 @@
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
- * Class to unsubscribe
29
- *
30
- */
31
- class Copernica_MarketingSoftware_UnsubscribeController extends Mage_Core_Controller_Front_Action
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) && $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 (
59
- $subscriber->loadByCustomer($customer)->getId() ||
60
- $subscriber->loadByEmail($email)->getId()
61
- ) {
62
- // we have a valid subscriber object now, so unsubscribe the user
63
- $subscriber->setSubscriberStatus(Mage_Newsletter_Model_Subscriber::STATUS_UNSUBSCRIBED)->save();
64
- echo 'ok';
65
- return;
66
- }
67
- }
68
- }
69
- echo 'not ok';
70
- }
 
71
  }
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
+ * Class to unsubscribe
29
+ *
30
+ */
31
+ class Copernica_MarketingSoftware_UnsubscribeController extends Mage_Core_Controller_Front_Action
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
+
66
+ echo 'ok';
67
+ return;
68
+ }
69
+ }
70
+ echo 'not ok';
71
+ }
72
  }
app/code/community/Copernica/MarketingSoftware/cron/clearCopernicaDatabase.php ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/php
2
+ <?php
3
+ /**
4
+ * Copernica Marketing Software
5
+ *
6
+ * NOTICE OF LICENSE
7
+ *
8
+ * This source file is subject to the Open Software License (OSL 3.0).
9
+ * It is available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you are unable to obtain a copy of the license through the
12
+ * world-wide-web, please send an email to copernica@support.cream.nl
13
+ * so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade this software
18
+ * to newer versions in the future. If you wish to customize this module
19
+ * for your needs please refer to http://www.magento.com/ for more
20
+ * information.
21
+ *
22
+ * @category Copernica
23
+ * @package Copernica_MarketingSoftware
24
+ * @copyright Copyright (c) 2011-2012 Copernica & Cream. (http://docs.cream.nl/)
25
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
26
+ */
27
+
28
+ // we have to set current dir to magento root
29
+ chdir(dirname(__FILE__));
30
+ chdir('../../../../../../');
31
+
32
+ /**
33
+ * We need to require magento facade. Since this script should always be in same
34
+ * relative place we can require by relative path.
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
+
app/code/community/Copernica/MarketingSoftware/cron/detectAbandonedCarts.php ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/php
2
+ <?php
3
+ /**
4
+ * Copernica Marketing Software
5
+ *
6
+ * NOTICE OF LICENSE
7
+ *
8
+ * This source file is subject to the Open Software License (OSL 3.0).
9
+ * It is available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you are unable to obtain a copy of the license through the
12
+ * world-wide-web, please send an email to copernica@support.cream.nl
13
+ * so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade this software
18
+ * to newer versions in the future. If you wish to customize this module
19
+ * for your needs please refer to http://www.magento.com/ for more
20
+ * information.
21
+ *
22
+ * @category Copernica
23
+ * @package Copernica_MarketingSoftware
24
+ * @copyright Copyright (c) 2011-2012 Copernica & Cream. (http://docs.cream.nl/)
25
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
26
+ */
27
+
28
+ /*
29
+ * This script is mainly for testing. It will enforce detection of forgotten carts.
30
+ */
31
+
32
+ // we have to set current dir to magento root
33
+ chdir(dirname(__FILE__));
34
+ chdir('../../../../../../');
35
+
36
+ /**
37
+ * We need to require magento facade. Since this script should always be in same
38
+ * relative place we can require by relative path.
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
+
app/code/community/Copernica/MarketingSoftware/cron/fullSync.php ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/php
2
+ <?php
3
+ /**
4
+ * Copernica Marketing Software
5
+ *
6
+ * NOTICE OF LICENSE
7
+ *
8
+ * This source file is subject to the Open Software License (OSL 3.0).
9
+ * It is available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you are unable to obtain a copy of the license through the
12
+ * world-wide-web, please send an email to copernica@support.cream.nl
13
+ * so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade this software
18
+ * to newer versions in the future. If you wish to customize this module
19
+ * for your needs please refer to http://www.magento.com/ for more
20
+ * information.
21
+ *
22
+ * @category Copernica
23
+ * @package Copernica_MarketingSoftware
24
+ * @copyright Copyright (c) 2011-2012 Copernica & Cream. (http://docs.cream.nl/)
25
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
26
+ */
27
+
28
+ // we have to set current dir to magento root
29
+ chdir(dirname(__FILE__));
30
+ chdir('../../../../../../');
31
+
32
+ /**
33
+ * We need to require magento facade. Since this script should always be in same
34
+ * relative place we can require by relative path.
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();
app/code/community/Copernica/MarketingSoftware/cron/processQueue.php ADDED
@@ -0,0 +1,152 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/php
2
+ <?php
3
+ /**
4
+ * Copernica Marketing Software
5
+ *
6
+ * NOTICE OF LICENSE
7
+ *
8
+ * This source file is subject to the Open Software License (OSL 3.0).
9
+ * It is available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you are unable to obtain a copy of the license through the
12
+ * world-wide-web, please send an email to copernica@support.cream.nl
13
+ * so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade this software
18
+ * to newer versions in the future. If you wish to customize this module
19
+ * for your needs please refer to http://www.magento.com/ for more
20
+ * information.
21
+ *
22
+ * @category Copernica
23
+ * @package Copernica_MarketingSoftware
24
+ * @copyright Copyright (c) 2011-2012 Copernica & Cream. (http://docs.cream.nl/)
25
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
26
+ */
27
+
28
+ /**
29
+ * This script will be able to run queue processing by itself. This way it will
30
+ * be possible to run parallel processing via supervisor or similiar tools.
31
+ */
32
+
33
+ // we have to set current dir to magento root
34
+ chdir(dirname(__FILE__));
35
+ 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;
45
+ echo "".PHP_EOL;
46
+ echo " Usage:".PHP_EOL;
47
+ echo " ./".basename(__FILE__)." [[--customer=CUSTOMER_ID|-c=CUSTOMER_ID]|[--lock|-l]] [--verbose=FORMAT|-v=FORMAT] [--remote|-r]".PHP_EOL;
48
+ echo " php ".basename(__FILE__)." [[--customer=CUSTOMER_ID|-c=CUSTOMER_ID]|[--lock|-l]] [--verbose=FORMAT|-v=FORMAT] [--remote|-r]".PHP_EOL;
49
+ echo "".PHP_EOL;
50
+ echo " This script should be used to process queue of synchronization tasks".PHP_EOL;
51
+ echo "".PHP_EOL;
52
+ echo " It's possible to supply this script with options that will influence".PHP_EOL;
53
+ echo " how processing is done. Note that long version of option will always".PHP_EOL;
54
+ echo " supress short version.".PHP_EOL;
55
+ echo "".PHP_EOL;
56
+ echo " --customer option with ID of customer that should be processed.".PHP_EOL;
57
+ echo "".PHP_EOL;
58
+ echo " --lock option will try to aqcuire a lock on 1st free customer".PHP_EOL;
59
+ echo " and process that customer only. This way it's possible ".PHP_EOL;
60
+ echo " to run multiple instances of processing script (via ".PHP_EOL;
61
+ echo " Supervisor or similiar software)".PHP_EOL;
62
+ echo "".PHP_EOL;
63
+ echo " --runtime minimun amount of time that script will sleep when lock".PHP_EOL;
64
+ echo " could not be aqcuired. By default it's set to 45 seconds".PHP_EOL;
65
+ echo "".PHP_EOL;
66
+ echo " --verbose option will force script to output result of run via one".PHP_EOL;
67
+ echo " of formats: TEXT or JSON".PHP_EOL;
68
+ echo "".PHP_EOL;
69
+
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);
app/code/community/Copernica/MarketingSoftware/data/marketingsoftware_setup/data-upgrade-3.4.0-3.4.1.php ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to copernica@support.cream.nl so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Copernica Marketing Software to newer
18
+ * versions in the future. If you wish to customize this module for your
19
+ * needs please refer to http://www.copernica.com/ for more 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
+ return;
28
+
29
+ // array of all configs entries that are describing linked fields
30
+ $linkedFieldConfigs = array('linked_customer_fields', 'linked_cart_item_fields', 'linked_order_fields', 'linked_order_item_fields', 'linked_address_fields', 'linked_viewed_product_fields');
31
+
32
+ // iterate over all linked fields configs
33
+ foreach ($linkedFieldConfigs as $config)
34
+ {
35
+ // get model
36
+ $model = Mage::getModel('marketingsoftware/config')->loadByKey($config);
37
+
38
+ // we have tocheck if model is ok
39
+ if ($model->getId())
40
+ {
41
+ // get json value
42
+ $json = $model->getValue();
43
+
44
+ // decode json
45
+ $array = json_decode($json, true);
46
+
47
+ // we have to convert all unverscore case keys to camel case keys
48
+ foreach ($array as $key => $value)
49
+ {
50
+ // unset old value
51
+ unset($array[$key]);
52
+
53
+ // camelize
54
+ $key = str_replace(' ', '', ucwords(preg_replace('/[^a-z0-9]+/i', ' ', $key)));
55
+
56
+ // cause some really old PHP can be used...
57
+ $key{0} = strtolower($key{0});
58
+
59
+ // set new value
60
+ $array[$key] = $value;
61
+ }
62
+
63
+ // store converted array
64
+ $model->setValue(json_encode($array));
65
+ }
66
+ }
app/code/community/Copernica/MarketingSoftware/etc/adminhtml.xml CHANGED
@@ -1,48 +1,55 @@
1
- <config>
2
- <menu>
3
- <copernica translate="title" module="marketingsoftware">
4
- <title>Copernica</title>
5
- <sort_order>120</sort_order>
6
- <depends>
7
- <module>Copernica_MarketingSoftware</module>
8
- </depends>
9
- <children>
10
- <settings translate="title" module="marketingsoftware">
11
- <title>Account Settings</title>
12
- <action>adminhtml/marketingsoftware_settings</action>
13
- </settings>
14
- <links translate="title" module="marketingsoftware">
15
- <title>Link Fields</title>
16
- <action>adminhtml/marketingsoftware_link</action>
17
- </links>
18
- <export translate="title" module="marketingsoftware">
19
- <title>Synchronize Data</title>
20
- <action>adminhtml/marketingsoftware_export</action>
21
- </export>
22
- </children>
23
- </copernica>
24
- </menu>
25
- <acl>
26
- <resources>
27
- <admin>
28
- <children>
29
- <copernica translate="title" module="marketingsoftware">
30
- <title>Copernica Marketing Software</title>
31
- <sort_order>120</sort_order>
32
- <children>
33
- <settings translate="title">
34
- <title>Settings</title>
35
- </settings>
36
- <links translate="title">
37
- <title>Link Fields</title>
38
- </links>
39
- <export translate="title">
40
- <title>Synchronize Data</title>
41
- </export>
42
- </children>
43
- </copernica>
44
- </children>
45
- </admin>
46
- </resources>
47
- </acl>
 
 
 
 
 
 
 
48
  </config>
1
+ <config>
2
+ <menu>
3
+ <copernica translate="title" module="marketingsoftware">
4
+ <title>Copernica</title>
5
+ <sort_order>120</sort_order>
6
+ <depends>
7
+ <module>Copernica_MarketingSoftware</module>
8
+ </depends>
9
+ <children>
10
+ <settings translate="title" module="marketingsoftware">
11
+ <title>Account Settings</title>
12
+ <action>adminhtml/marketingsoftware_settings</action>
13
+ </settings>
14
+ <links translate="title" module="marketingsoftware">
15
+ <title>Link Fields</title>
16
+ <action>adminhtml/marketingsoftware_link</action>
17
+ </links>
18
+ <export translate="title" module="marketingsoftware">
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>
25
+ </sync> -->
26
+ </children>
27
+ </copernica>
28
+ </menu>
29
+ <acl>
30
+ <resources>
31
+ <admin>
32
+ <children>
33
+ <copernica translate="title" module="marketingsoftware">
34
+ <title>Copernica Marketing Software</title>
35
+ <sort_order>120</sort_order>
36
+ <children>
37
+ <settings translate="title">
38
+ <title>Settings</title>
39
+ </settings>
40
+ <links translate="title">
41
+ <title>Link Fields</title>
42
+ </links>
43
+ <export translate="title">
44
+ <title>Synchronize Data</title>
45
+ </export>
46
+ <sync translate="title">
47
+ <title>Synchronization profiles</title>
48
+ </sync>
49
+ </children>
50
+ </copernica>
51
+ </children>
52
+ </admin>
53
+ </resources>
54
+ </acl>
55
  </config>
app/code/community/Copernica/MarketingSoftware/etc/config.xml CHANGED
@@ -1,172 +1,193 @@
1
- <?xml version="1.0"?>
2
- <config>
3
- <modules>
4
- <Copernica_MarketingSoftware>
5
- <version>2.2.0</version>
6
- </Copernica_MarketingSoftware>
7
- </modules>
8
- <global>
9
- <models>
10
- <marketingsoftware>
11
- <class>Copernica_MarketingSoftware_Model</class>
12
- <resourceModel>marketingsoftware_mysql4</resourceModel>
13
- </marketingsoftware>
14
- <marketingsoftware_mysql4>
15
- <class>Copernica_MarketingSoftware_Model_Mysql4</class>
16
- <entities>
17
- <queue>
18
- <table>copernica_queue</table>
19
- </queue>
20
- <marketingsoftware>
21
- <table>copernica_marketingsoftware</table>
22
- </marketingsoftware>
23
- <config_data>
24
- <table>copernica_config_data</table>
25
- </config_data>
26
- </entities>
27
- </marketingsoftware_mysql4>
28
- </models>
29
- <events>
30
- <checkout_controller_onepage_save_shipping_method>
31
- <observers>
32
- <marketingsoftware_observer>
33
- <class>marketingsoftware/observer</class>
34
- <method>checkoutSaveStep</method>
35
- </marketingsoftware_observer>
36
- </observers>
37
- </checkout_controller_onepage_save_shipping_method>
38
- <checkout_controller_multishipping_shipping_post>
39
- <observers>
40
- <marketingsoftware_observer>
41
- <class>marketingsoftware/observer</class>
42
- <method>checkoutSaveStep</method>
43
- </marketingsoftware_observer>
44
- </observers>
45
- </checkout_controller_multishipping_shipping_post>
46
- <sales_quote_item_delete_before>
47
- <observers>
48
- <marketingsoftware_observer>
49
- <class>marketingsoftware/observer</class>
50
- <method>quoteItemRemoved</method>
51
- </marketingsoftware_observer>
52
- </observers>
53
- </sales_quote_item_delete_before>
54
- <sales_quote_item_save_after>
55
- <observers>
56
- <marketingsoftware_observer>
57
- <class>marketingsoftware/observer</class>
58
- <method>quoteItemModified</method>
59
- </marketingsoftware_observer>
60
- </observers>
61
- </sales_quote_item_save_after>
62
- <sales_order_save_after>
63
- <observers>
64
- <marketingsoftware_observer>
65
- <class>marketingsoftware/observer</class>
66
- <method>orderModified</method>
67
- </marketingsoftware_observer>
68
- </observers>
69
- </sales_order_save_after>
70
- <newsletter_subscriber_delete_before>
71
- <observers>
72
- <marketingsoftware_observer>
73
- <class>marketingsoftware/observer</class>
74
- <method>newsletterSubscriptionRemoved</method>
75
- </marketingsoftware_observer>
76
- </observers>
77
- </newsletter_subscriber_delete_before>
78
- <newsletter_subscriber_save_after>
79
- <observers>
80
- <marketingsoftware_observer>
81
- <class>marketingsoftware/observer</class>
82
- <method>newsletterSubscriptionModified</method>
83
- </marketingsoftware_observer>
84
- </observers>
85
- </newsletter_subscriber_save_after>
86
- <customer_delete_before>
87
- <observers>
88
- <marketingsoftware_observer>
89
- <class>marketingsoftware/observer</class>
90
- <method>customerRemoved</method>
91
- </marketingsoftware_observer>
92
- </observers>
93
- </customer_delete_before>
94
- <customer_save_after>
95
- <observers>
96
- <marketingsoftware_observer>
97
- <class>marketingsoftware/observer</class>
98
- <method>customerModified</method>
99
- </marketingsoftware_observer>
100
- </observers>
101
- </customer_save_after>
102
- <catalog_controller_product_view>
103
- <observers>
104
- <marketingsoftware_observer>
105
- <class>marketingsoftware/observer</class>
106
- <method>productViewed</method>
107
- </marketingsoftware_observer>
108
- </observers>
109
- </catalog_controller_product_view>
110
- </events>
111
- <resources>
112
- <marketingsoftware_setup>
113
- <setup>
114
- <module>Copernica_MarketingSoftware</module>
115
- </setup>
116
- <connection>
117
- <use>core_setup</use>
118
- </connection>
119
- </marketingsoftware_setup>
120
- <marketingsoftware_write>
121
- <connection>
122
- <use>core_write</use>
123
- </connection>
124
- </marketingsoftware_write>
125
- <marketingsoftware_read>
126
- <connection>
127
- <use>core_read</use>
128
- </connection>
129
- </marketingsoftware_read>
130
- </resources>
131
- <blocks>
132
- <marketingsoftware>
133
- <class>Copernica_MarketingSoftware_Block</class>
134
- </marketingsoftware>
135
- </blocks>
136
- <helpers>
137
- <marketingsoftware>
138
- <class>Copernica_MarketingSoftware_Helper</class>
139
- </marketingsoftware>
140
- </helpers>
141
- </global>
142
- <frontend>
143
- <routers>
144
- <marketingsoftware>
145
- <use>standard</use>
146
- <args>
147
- <module>Copernica_MarketingSoftware</module>
148
- <frontName>copernica</frontName>
149
- </args>
150
- </marketingsoftware>
151
- </routers>
152
- </frontend>
153
- <admin>
154
- <routers>
155
- <adminhtml>
156
- <args>
157
- <modules>
158
- <Copernica_MarketingSoftware after="Mage_Adminhtml">Copernica_MarketingSoftware_Adminhtml</Copernica_MarketingSoftware>
159
- </modules>
160
- </args>
161
- </adminhtml>
162
- </routers>
163
- </admin>
164
- <crontab>
165
- <jobs>
166
- <marketingsoftware_process_queue>
167
- <schedule><cron_expr>*/5 * * * *</cron_expr></schedule>
168
- <run><model>marketingsoftware/observer::processQueue</model></run>
169
- </marketingsoftware_process_queue>
170
- </jobs>
171
- </crontab>
172
- </config>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <config>
3
+ <modules>
4
+ <Copernica_MarketingSoftware>
5
+ <version>3.0.0</version>
6
+ </Copernica_MarketingSoftware>
7
+ </modules>
8
+ <global>
9
+ <models>
10
+ <marketingsoftware>
11
+ <class>Copernica_MarketingSoftware_Model</class>
12
+ <resourceModel>marketingsoftware_mysql4</resourceModel>
13
+ </marketingsoftware>
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>
38
+ <events>
39
+ <checkout_controller_onepage_save_shipping_method>
40
+ <observers>
41
+ <marketingsoftware_observer>
42
+ <class>marketingsoftware/observer</class>
43
+ <method>checkoutSaveStep</method>
44
+ </marketingsoftware_observer>
45
+ </observers>
46
+ </checkout_controller_onepage_save_shipping_method>
47
+ <checkout_controller_multishipping_shipping_post>
48
+ <observers>
49
+ <marketingsoftware_observer>
50
+ <class>marketingsoftware/observer</class>
51
+ <method>checkoutSaveStep</method>
52
+ </marketingsoftware_observer>
53
+ </observers>
54
+ </checkout_controller_multishipping_shipping_post>
55
+ <sales_quote_item_delete_before>
56
+ <observers>
57
+ <marketingsoftware_observer>
58
+ <class>marketingsoftware/observer</class>
59
+ <method>quoteItemRemoved</method>
60
+ </marketingsoftware_observer>
61
+ </observers>
62
+ </sales_quote_item_delete_before>
63
+ <sales_quote_item_save_after>
64
+ <observers>
65
+ <marketingsoftware_observer>
66
+ <class>marketingsoftware/observer</class>
67
+ <method>quoteItemModified</method>
68
+ </marketingsoftware_observer>
69
+ </observers>
70
+ </sales_quote_item_save_after>
71
+ <sales_order_save_after>
72
+ <observers>
73
+ <marketingsoftware_observer>
74
+ <class>marketingsoftware/observer</class>
75
+ <method>orderModified</method>
76
+ </marketingsoftware_observer>
77
+ </observers>
78
+ </sales_order_save_after>
79
+ <sales_quote_delete_before>
80
+ <observers>
81
+ <marketingsoftware_observer>
82
+ <class>marketingsoftware/observer</class>
83
+ <method>quoteDelete</method>
84
+ </marketingsoftware_observer>
85
+ </observers>
86
+ </sales_quote_delete_before>
87
+ <newsletter_subscriber_delete_before>
88
+ <observers>
89
+ <marketingsoftware_observer>
90
+ <class>marketingsoftware/observer</class>
91
+ <method>newsletterSubscriptionRemoved</method>
92
+ </marketingsoftware_observer>
93
+ </observers>
94
+ </newsletter_subscriber_delete_before>
95
+ <newsletter_subscriber_save_commit_after>
96
+ <observers>
97
+ <marketingsoftware_observer>
98
+ <class>marketingsoftware/observer</class>
99
+ <method>newsletterSubscriptionModified</method>
100
+ </marketingsoftware_observer>
101
+ </observers>
102
+ </newsletter_subscriber_save_commit_after>
103
+ <customer_delete_before>
104
+ <observers>
105
+ <marketingsoftware_observer>
106
+ <class>marketingsoftware/observer</class>
107
+ <method>customerRemoved</method>
108
+ </marketingsoftware_observer>
109
+ </observers>
110
+ </customer_delete_before>
111
+ <customer_save_after>
112
+ <observers>
113
+ <marketingsoftware_observer>
114
+ <class>marketingsoftware/observer</class>
115
+ <method>customerModified</method>
116
+ </marketingsoftware_observer>
117
+ </observers>
118
+ </customer_save_after>
119
+ <catalog_controller_product_view>
120
+ <observers>
121
+ <marketingsoftware_observer>
122
+ <class>marketingsoftware/observer</class>
123
+ <method>productViewed</method>
124
+ </marketingsoftware_observer>
125
+ </observers>
126
+ </catalog_controller_product_view>
127
+ </events>
128
+ <resources>
129
+ <marketingsoftware_setup>
130
+ <setup>
131
+ <module>Copernica_MarketingSoftware</module>
132
+ </setup>
133
+ <connection>
134
+ <use>core_setup</use>
135
+ </connection>
136
+ </marketingsoftware_setup>
137
+ <marketingsoftware_write>
138
+ <connection>
139
+ <use>core_write</use>
140
+ </connection>
141
+ </marketingsoftware_write>
142
+ <marketingsoftware_read>
143
+ <connection>
144
+ <use>core_read</use>
145
+ </connection>
146
+ </marketingsoftware_read>
147
+ </resources>
148
+ <blocks>
149
+ <marketingsoftware>
150
+ <class>Copernica_MarketingSoftware_Block</class>
151
+ </marketingsoftware>
152
+ </blocks>
153
+ <helpers>
154
+ <marketingsoftware>
155
+ <class>Copernica_MarketingSoftware_Helper</class>
156
+ </marketingsoftware>
157
+ </helpers>
158
+ </global>
159
+ <frontend>
160
+ <routers>
161
+ <marketingsoftware>
162
+ <use>standard</use>
163
+ <args>
164
+ <module>Copernica_MarketingSoftware</module>
165
+ <frontName>copernica</frontName>
166
+ </args>
167
+ </marketingsoftware>
168
+ </routers>
169
+ </frontend>
170
+ <admin>
171
+ <routers>
172
+ <adminhtml>
173
+ <args>
174
+ <modules>
175
+ <Copernica_MarketingSoftware after="Mage_Adminhtml">Copernica_MarketingSoftware_Adminhtml</Copernica_MarketingSoftware>
176
+ </modules>
177
+ </args>
178
+ </adminhtml>
179
+ </routers>
180
+ </admin>
181
+ <crontab>
182
+ <jobs>
183
+ <marketingsoftware_process_queue>
184
+ <schedule><cron_expr>*/5 * * * *</cron_expr></schedule>
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>
192
+ </crontab>
193
+ </config>
app/code/community/Copernica/MarketingSoftware/sql/marketingsoftware_setup/mysql4-upgrade-2.1.0.1-2.2.0.php CHANGED
@@ -1,36 +1,36 @@
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
- * that is bundled with this package in the file LICENSE.txt.
9
- * It is also available through the world-wide-web at this URL:
10
- * http://opensource.org/licenses/osl-3.0.php
11
- * If you did not receive a copy of the license and are unable to
12
- * obtain it through the world-wide-web, please send an email
13
- * to copernica@support.cream.nl so we can send you a copy immediately.
14
- *
15
- * DISCLAIMER
16
- *
17
- * Do not edit or add to this file if you wish to upgrade Copernica Marketing Software to newer
18
- * versions in the future. If you wish to customize this module for your
19
- * needs please refer to http://www.copernica.com/ for more 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
- $installer = $this;
28
- $installer->startSetup();
29
-
30
- try {
31
- $installer->run("
32
- ALTER TABLE {$this->getTable('marketingsoftware/queue')}
33
- MODIFY object LONGTEXT;");
34
- } catch(Exception $e) {}
35
-
36
  $installer->endSetup();
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
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to copernica@support.cream.nl so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Copernica Marketing Software to newer
18
+ * versions in the future. If you wish to customize this module for your
19
+ * needs please refer to http://www.copernica.com/ for more 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
+ $installer = $this;
28
+ $installer->startSetup();
29
+
30
+ try {
31
+ $installer->run("
32
+ ALTER TABLE {$this->getTable('marketingsoftware/queue')}
33
+ MODIFY object LONGTEXT;");
34
+ } catch(Exception $e) {}
35
+
36
  $installer->endSetup();
app/code/community/Copernica/MarketingSoftware/sql/marketingsoftware_setup/mysql4-upgrade-2.2.0.1-2.3.0.php ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to copernica@support.cream.nl so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Copernica Marketing Software to newer
18
+ * versions in the future. If you wish to customize this module for your
19
+ * needs please refer to http://www.copernica.com/ for more 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
+ $installer = $this;
28
+ $installer->startSetup();
29
+
30
+ try {
31
+ $installer->run("
32
+ ALTER TABLE {$this->getTable('marketingsoftware/queue')}
33
+ ADD COLUMN customer int NULL DEFAULT NULL;");
34
+ } catch(Exception $e) {}
35
+
36
+ $installer->endSetup();
app/code/community/Copernica/MarketingSoftware/sql/marketingsoftware_setup/mysql4-upgrade-2.3.0-2.3.1.php ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to copernica@support.cream.nl so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Copernica Marketing Software to newer
18
+ * versions in the future. If you wish to customize this module for your
19
+ * needs please refer to http://www.copernica.com/ for more 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
+ // start setup
28
+ $installer = $this;
29
+ $installer->startSetup();
30
+
31
+ try {
32
+ // create profile cache table
33
+ $installer->run("
34
+ DROP TABLE IF EXISTS `{$installer->getTable('marketingsoftware/profileCache')}`;
35
+ CREATE TABLE `{$installer->getTable('marketingsoftware/profileCache')}` (
36
+ `id` int(10) unsigned auto_increment,
37
+ `customer_id` varchar(255) NOT NULL,
38
+ `profile_id` int(10) unsigned NOT NULL,
39
+ PRIMARY KEY (`id`)
40
+ ) ENGINE=InnoDB default CHARSET=utf8;
41
+ ");
42
+
43
+ // finalize setup
44
+ $installer->endSetup();
45
+ } catch(Exception $e) {
46
+ Mage::logException($e);
47
+ }
48
+
app/code/community/Copernica/MarketingSoftware/sql/marketingsoftware_setup/mysql4-upgrade-2.3.1-2.3.2.php ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to copernica@support.cream.nl so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Copernica Marketing Software to newer
18
+ * versions in the future. If you wish to customize this module for your
19
+ * needs please refer to http://www.copernica.com/ for more 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
+ // start setup
28
+ $installer = $this;
29
+ $installer->startSetup();
30
+
31
+ try {
32
+ // create error queue table
33
+ $installer->run("
34
+ DROP TABLE IF EXISTS `{$installer->getTable('marketingsoftware/errorqueue')}`;
35
+ CREATE TABLE `{$installer->getTable('marketingsoftware/errorqueue')}` (
36
+ `id` int(10) unsigned auto_increment,
37
+ `old_id` int(10) unsigned,
38
+ `object` longtext,
39
+ `action` enum('add', 'remove', 'modify', 'full', 'start_sync') NOT NULL DEFAULT 'modify',
40
+ `queue_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
41
+ `result` text,
42
+ `result_time` timestamp,
43
+ `customer` int(11),
44
+ PRIMARY KEY (`id`)
45
+ ) ENGINE=InnoDB default CHARSET=utf8;
46
+ ");
47
+
48
+ // finish setup
49
+ $installer->endSetup();
50
+ } catch(Exception $e) {
51
+ Mage::logException($e);
52
+ }
53
+
54
+
55
+
app/code/community/Copernica/MarketingSoftware/sql/marketingsoftware_setup/mysql4-upgrade-2.3.2-2.3.3.php ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ // get installer into local scope
4
+ $installer = $this;
5
+
6
+ // start setup process
7
+ $installer->startSetup();
8
+
9
+ // there may be exceptions
10
+ try {
11
+ // get table name for profile cache table
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
19
+ * required to order connection to create table with this definition at
20
+ * the end of the script.
21
+ */
22
+ $table = $installer->getConnection()->newTable($tableName);
23
+
24
+ // add Id column
25
+ $table->addColumn('id', Varien_Db_Ddl_Table::TYPE_INTEGER, null, array(
26
+ 'unsigned' => true,
27
+ 'nullable' => false,
28
+ 'primary' => true,
29
+ 'identity' => true,
30
+ ), 'Profile Cache Id');
31
+
32
+ // add customer Id column
33
+ $table->addColumn('customer_id', Varien_Db_Ddl_Table::TYPE_INTEGER, null, array(
34
+ 'unsigned' => true
35
+ ), 'Customer Id');
36
+
37
+ // add copernica customer Id column
38
+ $table->addColumn('copernica_id', Varien_Db_Ddl_Table::TYPE_VARCHAR, 255, array(
39
+ 'nullable' => true
40
+ ), 'Copernica customer Id');
41
+
42
+ // add profile id column
43
+ $table->addColumn('profile_id', Varien_Db_Ddl_Table::TYPE_INTEGER, null, array(
44
+ 'nullable' => true
45
+ ), 'Profile Id');
46
+
47
+ // add email column
48
+ $table->addColumn('email', Varien_Db_Ddl_Table::TYPE_VARCHAR, 255, array(
49
+ 'nullable' => true
50
+ ), 'Email address');
51
+
52
+ // add store view column
53
+ $table->addColumn('store_view', Varien_Db_Ddl_Table::TYPE_VARCHAR, 255, array(
54
+ 'nullable' => true
55
+ ), 'Store view');
56
+
57
+ // tell connection to create table
58
+ $installer->getConnection()->createTable($table);
59
+
60
+ // get queue table name
61
+ $queueTableName = $installer->getTable('marketingsoftware/queue');
62
+
63
+ /*
64
+ * Varien DDL does not support enum as a column type. That is why we will
65
+ * have to use normal sql query to modify that column.
66
+ */
67
+
68
+ // change action column to support upgrade action
69
+ $installer->run("ALTER TABLE $queueTableName MODIFY action enum ('add', 'remove', 'modify', 'full', 'start_sync', 'upgrade') NOT NULL DEFAULT 'modify'");
70
+
71
+ // We don't really need a full class to pass some simple values to object
72
+ // so we will use stdClass.
73
+ $customerUpgradeObject = new stdClass;
74
+ $customerUpgradeObject->start = null;
75
+
76
+ // end setup process
77
+ $installer->endSetup();
78
+
79
+ } catch (Exception $exception) {
80
+ // tell magento to log exception.
81
+ Mage::logException($exception);
82
+ }
app/code/community/Copernica/MarketingSoftware/sql/marketingsoftware_setup/mysql4-upgrade-2.3.3-2.3.4.php ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to copernica@support.cream.nl so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Copernica Marketing Software to newer
18
+ * versions in the future. If you wish to customize this module for your
19
+ * needs please refer to http://www.copernica.com/ for more 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
+ * With 2.3.4 stat_sync events are little bit more intelligent. They now store
29
+ * SyncStatus object as way of tracking progress of synchronization. Old events
30
+ * would cause errors cause they don't have such objects. That is why we want
31
+ * to regenerate start_sync event (if they are present).
32
+ */
33
+
34
+ // get installer into local scope
35
+ $installer = $this;
36
+
37
+ // start setup
38
+ $installer->startSetup();
39
+
40
+ try {
41
+
42
+ // we want to check if there is a synchronization scheduled
43
+ if (Mage::helper('marketingsoftware')->isSynchronisationStartScheduled()) {
44
+ // get old sync event
45
+ $syncEvent = Mage::getResourceModel('marketingsoftware/queue_collection')
46
+ ->addFieldToFilter('action', 'start_sync')->getFirstItem();
47
+
48
+ // delete old sync event
49
+ $syncEvent->delete();
50
+
51
+ // create sync status object
52
+ $syncStatus = Mage::getModel('marketingsoftware/SyncStatus');
53
+
54
+ // create new sync event
55
+ Mage::getModel('marketingsoftware/queue')
56
+ ->setObject($syncStatus)
57
+ ->setAction('start_sync')
58
+ ->save();
59
+ }
60
+ } catch(Exception $e) {
61
+ Mage::logException($e);
62
+ }
63
+
64
+ // end setup
65
+ $installer->endSetup();
app/code/community/Copernica/MarketingSoftware/sql/marketingsoftware_setup/mysql4-upgrade-2.3.4-2.3.5.php ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to copernica@support.cream.nl so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Copernica Marketing Software to newer
18
+ * versions in the future. If you wish to customize this module for your
19
+ * needs please refer to http://www.copernica.com/ for more 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
+ // get installer into local scope
28
+ $installer = $this;
29
+
30
+ // order installer to start setup
31
+ $installer->startSetup();
32
+
33
+ try {
34
+ // get queue table name
35
+ $queueTableName = $installer->getTable('marketingsoftware/queue');
36
+
37
+ /*
38
+ * Varien DDL does not support enum as a column type. That is why we will
39
+ * have to use normal sql query to modify that column.
40
+ */
41
+
42
+ // change action column to support upgrade action
43
+ $installer->run("ALTER TABLE $queueTableName MODIFY action enum ('add', 'remove', 'modify', 'full', 'start_sync', 'upgrade', 'file_sync') NOT NULL DEFAULT 'modify'");
44
+
45
+ // end setup
46
+ $installer->endSetup();
47
+
48
+ } catch (Exception $exception) {
49
+ Mage::logException($exception);
50
+ }
51
+
52
+
53
+
app/code/community/Copernica/MarketingSoftware/sql/marketingsoftware_setup/mysql4-upgrade-2.3.8-2.4.0.php ADDED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to copernica@support.cream.nl so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Copernica Marketing Software to newer
18
+ * versions in the future. If you wish to customize this module for your
19
+ * needs please refer to http://www.copernica.com/ for more 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
+ // bring installer to local scope
28
+ $installer = $this;
29
+
30
+ try
31
+ {
32
+ // start setup
33
+ $installer->startSetup();
34
+
35
+ // get name of the EventQueue table
36
+ $queueTableName = $installer->getTable('marketingsoftware/queue');
37
+
38
+ /*
39
+ * We want to get list of distinct customers that should be synchronized.
40
+ */
41
+ $result = $installer->getConnection()->fetchAll("SELECT DISTINCT customer FROM $queueTableName");
42
+
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
53
+ ));
54
+
55
+ // we will need associated entity for our events
56
+ $installer->getConnection()->addColumn($queueTableName, 'entity_id', array(
57
+ 'comment' => 'entity id associated with event',
58
+ 'type' => Varien_Db_Ddl_Table::TYPE_INTEGER,
59
+ 'nullable' => true,
60
+ 'default' => null
61
+ ));
62
+
63
+ // iterate over all unique customer that we found on old queue
64
+ foreach ($result as $row)
65
+ {
66
+ // create new events to sync all unique custoemr that we found on old queue
67
+ Mage::getModel('marketingsoftware/queue')
68
+ ->setObject(null)
69
+ ->setCustomer($row['customer'])
70
+ ->setEntityId($row['customer'])
71
+ ->setName('customer')
72
+ ->setAction('full')
73
+ ->save();
74
+ }
75
+
76
+ /*
77
+ * With this version we did also change how queue is processed. Basically
78
+ * it's now possible to rely on magento cron scheduler as it was before, but
79
+ * als we can rely on pure executing processQueue.php script that will process
80
+ * queue.
81
+ * By default we want to set new option for that to true, so it will have
82
+ * to be unchecked by user to enable new functionality.
83
+ */
84
+ Mage::helper('marketingsoftware/config')->setVanillaCrons(true);
85
+
86
+ // we are done here
87
+ $installer->endSetup();
88
+ }
89
+
90
+ // catch any exceptions and log them as exceptions
91
+ catch(Exception $exception)
92
+ {
93
+ Mage::log($exception->getMessage());
94
+ }
app/code/community/Copernica/MarketingSoftware/sql/marketingsoftware_setup/mysql4-upgrade-2.4.0-2.4.1.php ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to copernica@support.cream.nl so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Copernica Marketing Software to newer
18
+ * versions in the future. If you wish to customize this module for your
19
+ * needs please refer to http://www.copernica.com/ for more 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
+ * For some odd reason data-upgrade-* scripts don't work always. So our data
29
+ * upgrade logic we will just put here.
30
+ */
31
+
32
+ $installer = $this;
33
+
34
+ $installer->startSetup();
35
+
36
+ // array of all configs entries that are describing linked fields
37
+ $linkedFieldConfigs = array('linked_customer_fields', 'linked_cart_item_fields', 'linked_order_fields', 'linked_order_item_fields', 'linked_address_fields', 'linked_viewed_product_fields');
38
+
39
+ // iterate over all linked fields configs
40
+ foreach ($linkedFieldConfigs as $config)
41
+ {
42
+ // get model
43
+ $model = Mage::getModel('marketingsoftware/config')->loadByKey($config);
44
+
45
+ // we have to check if model is ok
46
+ if ($model->getId())
47
+ {
48
+ // get json value
49
+ $json = $model->getValue();
50
+
51
+ // decode json
52
+ $array = json_decode($json, true);
53
+
54
+ // we have to convert all underscore case keys to camel case keys
55
+ foreach ($array as $key => $value)
56
+ {
57
+ // unset old value
58
+ unset($array[$key]);
59
+
60
+ // camelize
61
+ $key = str_replace(' ', '', ucwords(preg_replace('/[^a-z0-9]+/i', ' ', $key)));
62
+
63
+ // cause some really old PHP can be used...
64
+ $key{0} = strtolower($key{0});
65
+
66
+ // set new value
67
+ $array[$key] = $value;
68
+ }
69
+
70
+ // store converted array
71
+ $model->setValue(json_encode($array));
72
+
73
+ // save the model
74
+ $model->save();
75
+ }
76
+ }
77
+
78
+ $installer->endSetup();
app/code/community/Copernica/MarketingSoftware/sql/marketingsoftware_setup/mysql4-upgrade-2.4.1-2.4.2.php ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to copernica@support.cream.nl so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Copernica Marketing Software to newer
18
+ * versions in the future. If you wish to customize this module for your
19
+ * needs please refer to http://www.copernica.com/ for more 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
+ // bring installer to local scope
28
+ $installer = $this;
29
+
30
+ try
31
+ {
32
+ // start setup
33
+ $installer->startSetup();
34
+
35
+ // get table name for sync profile table
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
43
+ * required to order connection to create table with this definition at
44
+ * the end of the script.
45
+ */
46
+ $table = $installer->getConnection()->newTable($tableName);
47
+
48
+ // add Id column
49
+ $table->addColumn('id', Varien_Db_Ddl_Table::TYPE_INTEGER, null, array(
50
+ 'unsigned' => true,
51
+ 'nullable' => false,
52
+ 'primary' => true,
53
+ 'identity' => true,
54
+ ), 'Sync profile Id');
55
+
56
+ // add customer Id column
57
+ $table->addColumn('client_key', Varien_Db_Ddl_Table::TYPE_VARCHAR, 255, array(
58
+ 'nullable' => false
59
+ ), 'Copernica client key');
60
+
61
+ // add copernica customer Id column
62
+ $table->addColumn('client_secret', Varien_Db_Ddl