Professio_BudgetMailer - Version 1.0.0

Version Notes

Professio BudgetMailer v 1.0.0 Stable

Download this release

Release Info

Developer J.S. Diertens
Extension Professio_BudgetMailer
Version 1.0.0
Comparing to
See all releases


Version 1.0.0

Files changed (62) hide show
  1. app/code/community/Professio/BudgetMailer/Block/Adminhtml/Contact.php +124 -0
  2. app/code/community/Professio/BudgetMailer/Block/Adminhtml/Contact/Edit.php +88 -0
  3. app/code/community/Professio/BudgetMailer/Block/Adminhtml/Contact/Edit/Form.php +51 -0
  4. app/code/community/Professio/BudgetMailer/Block/Adminhtml/Contact/Edit/Tab/Form.php +310 -0
  5. app/code/community/Professio/BudgetMailer/Block/Adminhtml/Contact/Edit/Tabs.php +71 -0
  6. app/code/community/Professio/BudgetMailer/Block/Adminhtml/Contact/Grid.php +279 -0
  7. app/code/community/Professio/BudgetMailer/Block/Adminhtml/Customer/Tab.php +165 -0
  8. app/code/community/Professio/BudgetMailer/Block/Adminhtml/Helper/Column/Renderer/Parent.php +95 -0
  9. app/code/community/Professio/BudgetMailer/Block/Adminhtml/List.php +46 -0
  10. app/code/community/Professio/BudgetMailer/Block/Adminhtml/List/Edit.php +82 -0
  11. app/code/community/Professio/BudgetMailer/Block/Adminhtml/List/Edit/Form.php +51 -0
  12. app/code/community/Professio/BudgetMailer/Block/Adminhtml/List/Edit/Tab/Form.php +110 -0
  13. app/code/community/Professio/BudgetMailer/Block/Adminhtml/List/Edit/Tabs.php +68 -0
  14. app/code/community/Professio/BudgetMailer/Block/Adminhtml/List/Grid.php +222 -0
  15. app/code/community/Professio/BudgetMailer/Block/Newsletter.php +103 -0
  16. app/code/community/Professio/BudgetMailer/Block/Subscribe.php +49 -0
  17. app/code/community/Professio/BudgetMailer/Block/Subscribe/Mini.php +44 -0
  18. app/code/community/Professio/BudgetMailer/Controller/Adminhtml/BudgetMailer.php +60 -0
  19. app/code/community/Professio/BudgetMailer/Exception.php +27 -0
  20. app/code/community/Professio/BudgetMailer/Form/Element/Tags.php +103 -0
  21. app/code/community/Professio/BudgetMailer/Helper/Config.php +230 -0
  22. app/code/community/Professio/BudgetMailer/Helper/Data.php +132 -0
  23. app/code/community/Professio/BudgetMailer/Helper/Mapper.php +539 -0
  24. app/code/community/Professio/BudgetMailer/Model/Client.php +826 -0
  25. app/code/community/Professio/BudgetMailer/Model/Config/Source/Address.php +62 -0
  26. app/code/community/Professio/BudgetMailer/Model/Config/Source/Delete.php +62 -0
  27. app/code/community/Professio/BudgetMailer/Model/Config/Source/Delete/Address.php +63 -0
  28. app/code/community/Professio/BudgetMailer/Model/Config/Source/List.php +102 -0
  29. app/code/community/Professio/BudgetMailer/Model/Config/Source/Update.php +62 -0
  30. app/code/community/Professio/BudgetMailer/Model/Contact.php +558 -0
  31. app/code/community/Professio/BudgetMailer/Model/Exporter.php +576 -0
  32. app/code/community/Professio/BudgetMailer/Model/Importer.php +480 -0
  33. app/code/community/Professio/BudgetMailer/Model/List.php +138 -0
  34. app/code/community/Professio/BudgetMailer/Model/Observer.php +651 -0
  35. app/code/community/Professio/BudgetMailer/Model/Resource/Contact.php +156 -0
  36. app/code/community/Professio/BudgetMailer/Model/Resource/Contact/Collection.php +79 -0
  37. app/code/community/Professio/BudgetMailer/Model/Resource/List.php +72 -0
  38. app/code/community/Professio/BudgetMailer/Model/Resource/List/Collection.php +177 -0
  39. app/code/community/Professio/BudgetMailer/Model/Resource/Setup.php +28 -0
  40. app/code/community/Professio/BudgetMailer/controllers/Adminhtml/Budgetmailer/ContactController.php +497 -0
  41. app/code/community/Professio/BudgetMailer/controllers/Adminhtml/BudgetmailerController.php +254 -0
  42. app/code/community/Professio/BudgetMailer/controllers/ManageController.php +157 -0
  43. app/code/community/Professio/BudgetMailer/controllers/SubscriberController.php +188 -0
  44. app/code/community/Professio/BudgetMailer/controllers/WebhookController.php +145 -0
  45. app/code/community/Professio/BudgetMailer/etc/adminhtml.xml +76 -0
  46. app/code/community/Professio/BudgetMailer/etc/config.xml +258 -0
  47. app/code/community/Professio/BudgetMailer/etc/system.xml +223 -0
  48. app/code/community/Professio/BudgetMailer/sql/budgetmailer_setup/install-1.0.0.php +236 -0
  49. app/code/community/Professio/BudgetMailer/sql/budgetmailer_setup_uninstall.sql +21 -0
  50. app/design/adminhtml/default/default/layout/budgetmailer.xml +104 -0
  51. app/design/adminhtml/default/default/template/budgetmailer/customer/tab.phtml +25 -0
  52. app/design/frontend/base/default/layout/budgetmailer.xml +58 -0
  53. app/design/frontend/base/default/template/budgetmailer/newsletter.phtml +73 -0
  54. app/design/frontend/base/default/template/budgetmailer/subscribe.phtml +64 -0
  55. app/design/frontend/base/default/template/budgetmailer/subscribe/mini.phtml +38 -0
  56. app/design/frontend/rwd/default/layout/budgetmailer.xml +57 -0
  57. app/design/frontend/rwd/default/template/budgetmailer/newsletter.phtml +52 -0
  58. app/design/frontend/rwd/default/template/budgetmailer/subscribe.phtml +51 -0
  59. app/design/frontend/rwd/default/template/budgetmailer/subscribe/mini.phtml +38 -0
  60. app/etc/modules/Professio_BudgetMailer.xml +10 -0
  61. app/locale/en_US/Professio_BudgetMailer.csv +89 -0
  62. package.xml +18 -0
app/code/community/Professio/BudgetMailer/Block/Adminhtml/Contact.php ADDED
@@ -0,0 +1,124 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Professio_BudgetMailer extension
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the MIT License
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/mit-license.php
11
+ *
12
+ * @category Professio
13
+ * @package Professio_BudgetMailer
14
+ * @copyright Copyright (c) 2015
15
+ * @license http://opensource.org/licenses/mit-license.php MIT License
16
+ */
17
+
18
+ /**
19
+ * Admin contact block
20
+ *
21
+ * @category Professio
22
+ * @package Professio_BudgetMailer
23
+ */
24
+ class Professio_BudgetMailer_Block_Adminhtml_Contact
25
+ extends Mage_Adminhtml_Block_Widget_Grid_Container
26
+ {
27
+ /**
28
+ * Contact - prepare widget
29
+ */
30
+ public function __construct()
31
+ {
32
+ $this->_controller = 'adminhtml_contact';
33
+ $this->_blockGroup = 'budgetmailer';
34
+
35
+ parent::__construct();
36
+
37
+ $this->_headerText = Mage::helper('budgetmailer')->__('Contact');
38
+
39
+ unset($this->_buttons[0]['add']);
40
+
41
+ $this->_addButton(
42
+ 'delete_orphans',
43
+ array(
44
+ 'label' => Mage::helper('budgetmailer')
45
+ ->__('Delete Orphans'),
46
+ 'onclick' => 'setLocation(\''
47
+ . $this->getDeleteOrphansUrl() .'\')',
48
+ 'class' => '',
49
+ )
50
+ );
51
+
52
+ $this->_addButton(
53
+ 'export_customers',
54
+ array(
55
+ 'label' => Mage::helper('budgetmailer')
56
+ ->__('Export Magento Customers'),
57
+ 'onclick' => 'setLocation(\''
58
+ . $this->getCustomersExportUrl() .'\')',
59
+ 'class' => '',
60
+ )
61
+ );
62
+
63
+ $this->_addButton(
64
+ 'export_subscribers',
65
+ array(
66
+ 'label' => Mage::helper('budgetmailer')
67
+ ->__('Export Magento Newsletter Subscribers'),
68
+ 'onclick' => 'setLocation(\''
69
+ . $this->getSubscribersExportUrl() .'\')',
70
+ 'class' => '',
71
+ )
72
+ );
73
+
74
+ $this->_addButton(
75
+ 'import_budgetmailer',
76
+ array(
77
+ 'label' => Mage::helper('budgetmailer')
78
+ ->__('Import BudgetMailer Contacts'),
79
+ 'onclick' => 'setLocation(\''
80
+ . $this->getBudgetmailerImportUrl() .'\')',
81
+ 'class' => '',
82
+ )
83
+ );
84
+ }
85
+
86
+ /**
87
+ * Get budgetmailer import url
88
+ * @return string
89
+ */
90
+ protected function getBudgetmailerImportUrl()
91
+ {
92
+ return $this->getUrl('*/budgetmailer/importbudgetmailer');
93
+ }
94
+
95
+ /**
96
+ * Get customers export url
97
+ *
98
+ * @return string
99
+ */
100
+ protected function getCustomersExportUrl()
101
+ {
102
+ return $this->getUrl('*/budgetmailer/exportcustomers');
103
+ }
104
+
105
+ /**
106
+ * Get delete orphans url
107
+ *
108
+ * @return string
109
+ */
110
+ protected function getDeleteOrphansUrl()
111
+ {
112
+ return $this->getUrl('*/budgetmailer/deleteorphans');
113
+ }
114
+
115
+ /**
116
+ * Get subscribers export url
117
+ *
118
+ * @return string
119
+ */
120
+ protected function getSubscribersExportUrl()
121
+ {
122
+ return $this->getUrl('*/budgetmailer/exportsubscribers');
123
+ }
124
+ }
app/code/community/Professio/BudgetMailer/Block/Adminhtml/Contact/Edit.php ADDED
@@ -0,0 +1,88 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Professio_BudgetMailer extension
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the MIT License
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/mit-license.php
11
+ *
12
+ * @category Professio
13
+ * @package Professio_BudgetMailer
14
+ * @copyright Copyright (c) 2015
15
+ * @license http://opensource.org/licenses/mit-license.php MIT License
16
+ */
17
+
18
+ /**
19
+ * Contact edit
20
+ *
21
+ * @category Professio
22
+ * @package Professio_BudgetMailer
23
+ */
24
+ class Professio_BudgetMailer_Block_Adminhtml_Contact_Edit
25
+ extends Mage_Adminhtml_Block_Widget_Form_Container
26
+ {
27
+ /**
28
+ * Constructor
29
+ *
30
+ * @return void
31
+ */
32
+ public function __construct()
33
+ {
34
+ parent::__construct();
35
+
36
+ $this->_blockGroup = 'budgetmailer';
37
+ $this->_controller = 'adminhtml_contact';
38
+
39
+ $this->_updateButton(
40
+ 'save',
41
+ 'label',
42
+ Mage::helper('budgetmailer')->__('Save Contact')
43
+ );
44
+
45
+ $this->_updateButton(
46
+ 'delete',
47
+ 'label', Mage::helper('budgetmailer')->__('Delete Contact')
48
+ );
49
+
50
+ $this->_addButton(
51
+ 'saveandcontinue',
52
+ array(
53
+ 'label' => Mage::helper('budgetmailer')
54
+ ->__('Save And Continue Edit'),
55
+ 'onclick' => 'saveAndContinueEdit()',
56
+ 'class' => 'save',
57
+ ),
58
+ -100
59
+ );
60
+
61
+ $this->_formScripts[] = "
62
+ function saveAndContinueEdit(){
63
+ editForm.submit($('edit_form').action+'back/edit/');
64
+ }
65
+ ";
66
+ }
67
+
68
+ /**
69
+ * Get the edit form header
70
+ *
71
+ * @return string
72
+ */
73
+ public function getHeaderText()
74
+ {
75
+ if (Mage::registry('current_contact')
76
+ && Mage::registry('current_contact')->getId()) {
77
+ return Mage::helper('budgetmailer')
78
+ ->__(
79
+ "Edit Contact '%s'",
80
+ $this->escapeHtml(
81
+ Mage::registry('current_contact')->getEmail()
82
+ )
83
+ );
84
+ } else {
85
+ return Mage::helper('budgetmailer')->__('Add Contact');
86
+ }
87
+ }
88
+ }
app/code/community/Professio/BudgetMailer/Block/Adminhtml/Contact/Edit/Form.php ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Professio_BudgetMailer extension
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the MIT License
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/mit-license.php
11
+ *
12
+ * @category Professio
13
+ * @package Professio_BudgetMailer
14
+ * @copyright Copyright (c) 2015
15
+ * @license http://opensource.org/licenses/mit-license.php MIT License
16
+ */
17
+
18
+ /**
19
+ * Contact edit form
20
+ *
21
+ * @category Professio
22
+ * @package Professio_BudgetMailer
23
+ */
24
+ class Professio_BudgetMailer_Block_Adminhtml_Contact_Edit_Form
25
+ extends Mage_Adminhtml_Block_Widget_Form
26
+ {
27
+ /**
28
+ * Prepare form
29
+ *
30
+ * @return Professio_BudgetMailer_Block_Adminhtml_Contact_Edit_Form
31
+ */
32
+ protected function _prepareForm()
33
+ {
34
+ $form = new Varien_Data_Form(
35
+ array(
36
+ 'id' => 'edit_form',
37
+ 'action' => $this->getUrl(
38
+ '*/*/save',
39
+ array('id' => $this->getRequest()->getParam('id'))
40
+ ),
41
+ 'method' => 'post',
42
+ 'enctype' => 'multipart/form-data'
43
+ )
44
+ );
45
+
46
+ $form->setUseContainer(true);
47
+ $this->setForm($form);
48
+
49
+ return parent::_prepareForm();
50
+ }
51
+ }
app/code/community/Professio/BudgetMailer/Block/Adminhtml/Contact/Edit/Tab/Form.php ADDED
@@ -0,0 +1,310 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Professio_BudgetMailer extension
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the MIT License
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/mit-license.php
11
+ *
12
+ * @category Professio
13
+ * @package Professio_BudgetMailer
14
+ * @copyright Copyright (c) 2015
15
+ * @license http://opensource.org/licenses/mit-license.php MIT License
16
+ */
17
+
18
+ /**
19
+ * Contact edit tab form
20
+ *
21
+ * @category Professio
22
+ * @package Professio_BudgetMailer
23
+ */
24
+ class Professio_BudgetMailer_Block_Adminhtml_Contact_Edit_Tab_Form
25
+ extends Mage_Adminhtml_Block_Widget_Form
26
+ {
27
+ /**
28
+ * @var array form fields definitions
29
+ */
30
+ protected $_fields = array(
31
+ array(
32
+ 'budgetmailer_id',
33
+ 'text',
34
+ array(
35
+ 'label' => 'Budgetmailer ID',
36
+ 'name' => 'budgetmailer_id',
37
+ //'required' => true,
38
+ //'class' => 'required-entry',
39
+ 'readonly' => true,
40
+ )
41
+ ),
42
+ array(
43
+ 'email',
44
+ 'text',
45
+ array(
46
+ 'label' => 'Email',
47
+ 'name' => 'email',
48
+ 'required' => true,
49
+ 'class' => 'required-entry',
50
+ )
51
+ ),
52
+ array(
53
+ 'company_name',
54
+ 'text',
55
+ array(
56
+ 'label' => 'Company',
57
+ 'name' => 'company_name',
58
+ 'required' => false,
59
+ )
60
+ ),
61
+ array(
62
+ 'first_name',
63
+ 'text',
64
+ array(
65
+ 'label' => 'First Name',
66
+ 'name' => 'first_name',
67
+ 'required' => false,
68
+ )
69
+ ),
70
+ array(
71
+ 'insertion',
72
+ 'text',
73
+ array(
74
+ 'label' => 'Insertion',
75
+ 'name' => 'insertion',
76
+ 'required' => false,
77
+ )
78
+ ),
79
+ array(
80
+ 'last_name',
81
+ 'text',
82
+ array(
83
+ 'label' => 'Last Name',
84
+ 'name' => 'last_name',
85
+ 'required' => false,
86
+ )
87
+ ),
88
+ array(
89
+ 'sex',
90
+ 'select',
91
+ array(
92
+ 'label' => 'Sex',
93
+ 'name' => 'sex',
94
+ 'required' => false,
95
+ 'values' => array(
96
+ array(
97
+ 'value' => 0,
98
+ 'label' => ''
99
+ ),
100
+ array(
101
+ 'value' => 1,
102
+ 'label' => 'Male'
103
+ ),
104
+ array(
105
+ 'value' => 2,
106
+ 'label' => 'Female'
107
+ ),
108
+ ),
109
+ )
110
+ ),
111
+ array(
112
+ 'address',
113
+ 'text',
114
+ array(
115
+ 'label' => 'Address',
116
+ 'name' => 'address',
117
+ 'required' => false,
118
+ )
119
+ ),
120
+ array(
121
+ 'postal_code',
122
+ 'text',
123
+ array(
124
+ 'label' => 'Postal Code',
125
+ 'name' => 'postal_code',
126
+ 'required' => false,
127
+ )
128
+ ),
129
+ array(
130
+ 'city',
131
+ 'text',
132
+ array(
133
+ 'label' => 'City',
134
+ 'name' => 'city',
135
+ 'required' => false,
136
+ )
137
+ ),
138
+ array(
139
+ 'country_code',
140
+ 'text',
141
+ array(
142
+ 'label' => 'Country Code',
143
+ 'name' => 'country_code',
144
+ 'required' => false,
145
+ )
146
+ ),
147
+ array(
148
+ 'telephone',
149
+ 'text',
150
+ array(
151
+ 'label' => 'Telephone',
152
+ 'name' => 'telephone',
153
+ 'required' => false,
154
+ )
155
+ ),
156
+ array(
157
+ 'mobile',
158
+ 'text',
159
+ array(
160
+ 'label' => 'Mobile',
161
+ 'name' => 'mobile',
162
+ 'required' => false,
163
+ )
164
+ ),
165
+ array(
166
+ 'remarks',
167
+ 'textarea',
168
+ array(
169
+ 'label' => 'Remarks',
170
+ 'name' => 'remarks',
171
+ 'required' => false,
172
+ )
173
+ ),
174
+ array(
175
+ 'tags',
176
+ 'budgetmailer_tags',
177
+ array(
178
+ 'label' => 'Tags',
179
+ 'name' => 'tags',
180
+ 'required' => false,
181
+ )
182
+ ),
183
+ array(
184
+ 'unsubscribed',
185
+ 'select',
186
+ array(
187
+ 'label' => 'Unsubscribed',
188
+ 'name' => 'unsubscribed',
189
+ 'readonly' => true,
190
+ 'required' => false,
191
+ 'values'=> array(
192
+ array(
193
+ 'value' => 1,
194
+ 'label' => 'Yes',
195
+ ),
196
+ array(
197
+ 'value' => 0,
198
+ 'label' => 'No',
199
+ ),
200
+ ),
201
+ )
202
+ )
203
+ );
204
+
205
+ /**
206
+ * Prepare the form
207
+ *
208
+ * @return Professio_BudgetMailer_Block_Adminhtml_Contact_Edit_Tab_Form
209
+ */
210
+ protected function _prepareForm()
211
+ {
212
+ $form = new Varien_Data_Form();
213
+
214
+ $form->setHtmlIdPrefix('contact_');
215
+ $form->setFieldNameSuffix('contact');
216
+ $this->setForm($form);
217
+
218
+ $fieldset = $form->addFieldset(
219
+ 'contact_form',
220
+ array(
221
+ 'legend' => Mage::helper('budgetmailer')->__('Contact')
222
+ )
223
+ );
224
+
225
+ $fieldset->addType(
226
+ 'budgetmailer_tags',
227
+ 'Professio_BudgetMailer_Form_Element_Tags'
228
+ );
229
+
230
+ $values = Mage::getResourceModel('budgetmailer/list_collection')
231
+ ->toOptionArray();
232
+ array_unshift($values, array('label'=>'', 'value'=>''));
233
+
234
+ $fieldset->addField(
235
+ 'list_id',
236
+ 'hidden',
237
+ array(
238
+ 'name' => 'list_id',
239
+ //'required' => true,
240
+ )
241
+ );
242
+
243
+ $subscribe = Mage::registry('current_contact')
244
+ && Mage::registry('current_contact')->getEntityId()
245
+ ? !Mage::registry('current_contact')->getUnsubscribed()
246
+ : false;
247
+
248
+ $fieldset->addField(
249
+ 'subscribe',
250
+ 'checkbox',
251
+ array(
252
+ 'label' => Mage::helper('budgetmailer')->__('Subscribe'),
253
+ 'name' => 'subscribe',
254
+ 'value' => 1,
255
+ 'checked' => $subscribe
256
+ )
257
+ );
258
+
259
+ $this->addFields($fieldset);
260
+
261
+ $formValues = Mage::registry('current_contact')->getDefaultValues();
262
+
263
+ if (!is_array($formValues)) {
264
+ $formValues = array();
265
+ }
266
+
267
+ if (Mage::getSingleton('adminhtml/session')->getContactData()) {
268
+ $formValues = array_merge(
269
+ $formValues,
270
+ Mage::getSingleton('adminhtml/session')->getContactData()
271
+ );
272
+ Mage::getSingleton('adminhtml/session')->setContactData(null);
273
+ } elseif (Mage::registry('current_contact')) {
274
+ $formValues = array_merge(
275
+ $formValues,
276
+ Mage::registry('current_contact')->getData()
277
+ );
278
+ }
279
+
280
+ $form->setValues($formValues);
281
+
282
+ return parent::_prepareForm();
283
+ }
284
+
285
+ /**
286
+ * Add fields from definition to fieldset
287
+ * @param Varien_Data_Form_Element_Fieldset $fieldset
288
+ */
289
+ protected function addFields($fieldset)
290
+ {
291
+ foreach ($this->_fields as $k => $field) {
292
+ if (isset($field[2]['label'])) {
293
+ $field[2]['label'] = Mage::helper('budgetmailer')
294
+ ->__($field[2]['label']);
295
+ }
296
+
297
+ if (isset($field[2]['values'])) {
298
+ foreach ($field[2]['values'] as $k => $v) {
299
+ if (isset($v['label'])) {
300
+ $field[2]['values'][$k]['label'] =
301
+ Mage::helper('budgetmailer')
302
+ ->__($v['label']);
303
+ }
304
+ }
305
+ }
306
+
307
+ $fieldset->addField($field[0], $field[1], $field[2]);
308
+ }
309
+ }
310
+ }
app/code/community/Professio/BudgetMailer/Block/Adminhtml/Contact/Edit/Tabs.php ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Professio_BudgetMailer extension
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the MIT License
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/mit-license.php
11
+ *
12
+ * @category Professio
13
+ * @package Professio_BudgetMailer
14
+ * @copyright Copyright (c) 2015
15
+ * @license http://opensource.org/licenses/mit-license.php MIT License
16
+ */
17
+
18
+ /**
19
+ * Contact edit tabs
20
+ *
21
+ * @category Professio
22
+ * @package Professio_BudgetMailer
23
+ */
24
+ class Professio_BudgetMailer_Block_Adminhtml_Contact_Edit_Tabs
25
+ extends Mage_Adminhtml_Block_Widget_Tabs
26
+ {
27
+ /**
28
+ * Initialize Tabs
29
+ *
30
+ * @return void
31
+ */
32
+ public function __construct()
33
+ {
34
+ parent::__construct();
35
+
36
+ $this->setId('contact_tabs');
37
+ $this->setDestElementId('edit_form');
38
+ $this->setTitle(Mage::helper('budgetmailer')->__('Contact'));
39
+ }
40
+
41
+ /**
42
+ * Before render html
43
+ *
44
+ * @return Professio_BudgetMailer_Block_Adminhtml_Contact_Edit_Tabs
45
+ */
46
+ protected function _beforeToHtml()
47
+ {
48
+ $this->addTab(
49
+ 'form_contact', array(
50
+ 'label' => Mage::helper('budgetmailer')->__('Contact'),
51
+ 'title' => Mage::helper('budgetmailer')->__('Contact'),
52
+ 'content' => $this->getLayout()
53
+ ->createBlock(
54
+ 'budgetmailer/adminhtml_contact_edit_tab_form'
55
+ )->toHtml(),
56
+ )
57
+ );
58
+
59
+ return parent::_beforeToHtml();
60
+ }
61
+
62
+ /**
63
+ * Retrieve contact entity
64
+ *
65
+ * @return Professio_BudgetMailer_Model_Contact
66
+ */
67
+ public function getContact()
68
+ {
69
+ return Mage::registry('current_contact');
70
+ }
71
+ }
app/code/community/Professio/BudgetMailer/Block/Adminhtml/Contact/Grid.php ADDED
@@ -0,0 +1,279 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Professio_BudgetMailer extension
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the MIT License
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/mit-license.php
11
+ *
12
+ * @category Professio
13
+ * @package Professio_BudgetMailer
14
+ * @copyright Copyright (c) 2015
15
+ * @license http://opensource.org/licenses/mit-license.php MIT License
16
+ */
17
+
18
+ /**
19
+ * Conctact edit grid
20
+ *
21
+ * @category Professio
22
+ * @package Professio_BudgetMailer
23
+ */
24
+ class Professio_BudgetMailer_Block_Adminhtml_Contact_Grid
25
+ extends Mage_Adminhtml_Block_Widget_Grid
26
+ {
27
+ /**
28
+ * @var array columns definition
29
+ */
30
+ protected $_columnsDefinition = array(
31
+ array(
32
+ 'entity_id',
33
+ array(
34
+ 'header' => 'Id',
35
+ 'index' => 'entity_id',
36
+ 'type' => 'number'
37
+ )
38
+ ),
39
+ array(
40
+ 'list_id',
41
+ array(
42
+ 'header' => 'List',
43
+ 'index' => 'list_id',
44
+ 'type' => 'options',
45
+ 'options' => array(),
46
+ 'renderer' =>
47
+ 'budgetmailer/adminhtml_helper_column_renderer_parent',
48
+ 'params' => array(
49
+ 'id' => 'getListId'
50
+ ),
51
+ 'base_link' => 'adminhtml/budgetmailer_list/edit'
52
+ )
53
+ ),
54
+ array(
55
+ 'budgetmailer_id',
56
+ array(
57
+ 'header' => 'Budgetmailer ID',
58
+ 'index' => 'budgetmailer_id',
59
+ 'type' => 'text',
60
+ )
61
+ ),
62
+ array(
63
+ 'email',
64
+ array(
65
+ 'header' => 'Email',
66
+ 'align' => 'left',
67
+ 'index' => 'email',
68
+ )
69
+ ),
70
+ array(
71
+ 'company_name',
72
+ array(
73
+ 'header' => 'Company Name',
74
+ 'index' => 'company_name',
75
+ 'type' => 'text',
76
+ )
77
+ ),
78
+ array(
79
+ 'first_name',
80
+ array(
81
+ 'header' => 'First Name',
82
+ 'index' => 'first_name',
83
+ 'type' => 'text',
84
+ )
85
+ ),
86
+ array(
87
+ 'last_name',
88
+ array(
89
+ 'header' => 'Last Name',
90
+ 'index' => 'last_name',
91
+ 'type' => 'text',
92
+ )
93
+ ),
94
+ array(
95
+ 'city',
96
+ array(
97
+ 'header' => 'City',
98
+ 'index' => 'city',
99
+ 'type' => 'text',
100
+ )
101
+ ),
102
+ array(
103
+ 'country_code',
104
+ array(
105
+ 'header' => 'Country Code',
106
+ 'index' => 'country_code',
107
+ 'type' => 'text',
108
+ )
109
+ ),
110
+ array(
111
+ 'unsubscribed',
112
+ array(
113
+ 'header' => 'Unsubscribed',
114
+ 'index' => 'unsubscribed',
115
+ 'type' => 'options',
116
+ 'options' => array(
117
+ '1' => 'Yes',
118
+ '0' => 'No',
119
+ )
120
+ )
121
+ ),
122
+ array(
123
+ 'action',
124
+ array(
125
+ 'header' => 'Action',
126
+ 'width' => '100',
127
+ 'type' => 'action',
128
+ 'getter' => 'getId',
129
+ 'actions' => array(
130
+ array(
131
+ 'caption' => 'Edit',
132
+ 'url' => array('base' => '*/*/edit'),
133
+ 'field' => 'id'
134
+ )
135
+ ),
136
+ 'filter' => false,
137
+ 'is_system' => true,
138
+ 'sortable' => false,
139
+ )
140
+ )
141
+ );
142
+
143
+ /**
144
+ * Constructor
145
+ *
146
+ * @return void
147
+ */
148
+ public function __construct()
149
+ {
150
+ parent::__construct();
151
+
152
+ $this->setId('contactGrid');
153
+ $this->setDefaultSort('entity_id');
154
+ $this->setDefaultDir('ASC');
155
+ $this->setSaveParametersInSession(true);
156
+ $this->setUseAjax(true);
157
+ }
158
+
159
+ /**
160
+ * Prepare collection
161
+ *
162
+ * @return Professio_BudgetMailer_Block_Adminhtml_Contact_Grid
163
+ */
164
+ protected function _prepareCollection()
165
+ {
166
+ $collection = Mage::getModel('budgetmailer/contact')->getCollection();
167
+ $this->setCollection($collection);
168
+
169
+ return parent::_prepareCollection();
170
+ }
171
+
172
+ /**
173
+ * Prepare grid collection
174
+ *
175
+ * @return Professio_BudgetMailer_Block_Adminhtml_Contact_Grid
176
+ */
177
+ protected function _prepareColumns()
178
+ {
179
+ foreach ($this->_columnsDefinition as $column) {
180
+ if (isset($column[1]['header'])) {
181
+ $column[1]['header'] = Mage::helper('budgetmailer')
182
+ ->__($column[1]['header']);
183
+ }
184
+
185
+ $this->addColumn($column[0], $column[1]);
186
+ }
187
+
188
+ $this->getColumn('list_id')->setData(
189
+ 'options',
190
+ Mage::getResourceModel('budgetmailer/list_collection')
191
+ ->toOptionHash()
192
+ );
193
+
194
+ return parent::_prepareColumns();
195
+ }
196
+
197
+ /**
198
+ * Prepare mass action
199
+ *
200
+ * @return Professio_BudgetMailer_Block_Adminhtml_Contact_Grid
201
+ */
202
+ protected function _prepareMassaction()
203
+ {
204
+ $this->setMassactionIdField('entity_id');
205
+ $this->getMassactionBlock()->setFormFieldName('contact');
206
+
207
+ $this->getMassactionBlock()->addItem(
208
+ 'delete',
209
+ array(
210
+ 'label' => Mage::helper('budgetmailer')->__('Delete'),
211
+ 'url' => $this->getUrl('*/*/massDelete'),
212
+ 'confirm' => Mage::helper('budgetmailer')->__('Are you sure?')
213
+ )
214
+ );
215
+
216
+ $this->getMassactionBlock()->addItem(
217
+ 'unsubscribed', array(
218
+ 'label' => Mage::helper('budgetmailer')->__('Change Unsubscribed'),
219
+ 'url' => $this->getUrl(
220
+ '*/*/massUnsubscribed',
221
+ array('_current' => true)
222
+ ),
223
+ 'additional' => array(
224
+ 'flag_unsubscribed' => array(
225
+ 'name' => 'flag_unsubscribed',
226
+ 'type' => 'select',
227
+ 'class' => 'required-entry',
228
+ 'label' => Mage::helper('budgetmailer')->__('Unsubscribed'),
229
+ 'values' => array(
230
+ '1' => Mage::helper('budgetmailer')->__('Yes'),
231
+ '0' => Mage::helper('budgetmailer')->__('No'),
232
+ )
233
+ )
234
+ )
235
+ )
236
+ );
237
+
238
+ $values = Mage::getResourceModel('budgetmailer/list_collection')
239
+ ->toOptionHash();
240
+ $values = array_reverse($values, true);
241
+ $values[''] = '';
242
+ $values = array_reverse($values, true);
243
+
244
+ return $this;
245
+ }
246
+
247
+ /**
248
+ * Get the row url
249
+ *
250
+ * @param Professio_BudgetMailer_Model_Contact
251
+ * @return string
252
+ */
253
+ public function getRowUrl($row)
254
+ {
255
+ return $this->getUrl('*/*/edit', array('id' => $row->getId()));
256
+ }
257
+
258
+ /**
259
+ * Get the grid url
260
+ *
261
+ * @return string
262
+ */
263
+ public function getGridUrl()
264
+ {
265
+ return $this->getUrl('*/*/grid', array('_current' => true));
266
+ }
267
+
268
+ /**
269
+ * After collection load
270
+ *
271
+ * @return Professio_BudgetMailer_Block_Adminhtml_Contact_Grid
272
+ */
273
+ protected function _afterLoadCollection()
274
+ {
275
+ $this->getCollection()->walk('afterLoad');
276
+
277
+ parent::_afterLoadCollection();
278
+ }
279
+ }
app/code/community/Professio/BudgetMailer/Block/Adminhtml/Customer/Tab.php ADDED
@@ -0,0 +1,165 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Professio_BudgetMailer extension
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the MIT License
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/mit-license.php
11
+ *
12
+ * @category Professio
13
+ * @package Professio_BudgetMailer
14
+ * @copyright Copyright (c) 2015
15
+ * @license http://opensource.org/licenses/mit-license.php MIT License
16
+ */
17
+
18
+ /**
19
+ * Customer BudgetMailer tab
20
+ *
21
+ * @category Professio
22
+ * @package Professio_BudgetMailer
23
+ */
24
+ class Professio_BudgetMailer_Block_Adminhtml_Customer_Tab
25
+ extends Mage_Adminhtml_Block_Template
26
+ implements Mage_Adminhtml_Block_Widget_Tab_Interface
27
+ {
28
+ /**
29
+ * Current contact
30
+ * @var type Professio_BudgetMailer_Model_Contact
31
+ */
32
+ protected $_contact;
33
+
34
+ /**
35
+ * Set the template for the block
36
+ *
37
+ * @return void
38
+ */
39
+ public function _construct()
40
+ {
41
+ parent::_construct();
42
+
43
+ $this->setTemplate('budgetmailer/customer/tab.phtml');
44
+ }
45
+
46
+ /**
47
+ * Retrieve the label used for the tab relating to this block
48
+ *
49
+ * @return string
50
+ */
51
+ public function getTabLabel()
52
+ {
53
+ return $this->__('BudgetMailer Newsletter');
54
+ }
55
+
56
+ /**
57
+ * Retrieve the title used by this tab
58
+ *
59
+ * @return string
60
+ */
61
+ public function getTabTitle()
62
+ {
63
+ return $this->__(
64
+ 'Click here to view Budgetmailer subscription and contact '
65
+ . 'information for this customer.'
66
+ );
67
+ }
68
+
69
+ /**
70
+ * Determines whether to display the tab
71
+ * Add logic here to decide whether you want the tab to display
72
+ *
73
+ * @return bool
74
+ */
75
+ public function canShowTab()
76
+ {
77
+ $customer = Mage::registry('current_customer');
78
+
79
+ return $customer->getId() > 0;
80
+ }
81
+
82
+ /**
83
+ * Stops the tab being hidden
84
+ *
85
+ * @return bool
86
+ */
87
+ public function isHidden()
88
+ {
89
+ return false;
90
+ }
91
+
92
+ /**
93
+ * Defines after which tab, this tab should be rendered
94
+ *
95
+ * @return string
96
+ */
97
+ public function getAfter()
98
+ {
99
+ return 'tags';
100
+ }
101
+
102
+ /**
103
+ * Before to HTML
104
+ *
105
+ * @return void
106
+ */
107
+ protected function _beforeToHtml()
108
+ {
109
+ parent::_beforeToHtml();
110
+
111
+ Mage::register('current_contact', $this->getContact());
112
+
113
+ $block = $this->getLayout()
114
+ ->createBlock(
115
+ 'budgetmailer/adminhtml_contact_edit_tab_form',
116
+ 'budgetmailer_customer_tab_edit'
117
+ );
118
+ $this->append($block, 'budgetmailer_customer_tab_edit');
119
+ }
120
+
121
+ /**
122
+ * Get current contact
123
+ *
124
+ * @return Professio_BudgetMailer_Model_Contact
125
+ */
126
+ protected function getContact()
127
+ {
128
+ if (!isset($this->_contact)) {
129
+ $this->_contact = Mage::getModel('budgetmailer/contact');
130
+ $this->_contact->loadByCustomer($this->getCustomer());
131
+ }
132
+
133
+ return $this->_contact;
134
+ }
135
+
136
+ /**
137
+ * Get current contact
138
+ *
139
+ * @return Mage_Customer_Model_Customer
140
+ */
141
+ protected function getCustomer()
142
+ {
143
+ return Mage::registry('current_customer');
144
+ }
145
+
146
+ /**
147
+ * Check if current contact is subscribed
148
+ *
149
+ * @return boolean
150
+ */
151
+ public function isSubscribed()
152
+ {
153
+ return !$this->getContact()->getUnsubscribed();
154
+ }
155
+
156
+ /**
157
+ * Get current contact tags
158
+ *
159
+ * @return array
160
+ */
161
+ public function getTags()
162
+ {
163
+ return $this->getContact()->getTags();
164
+ }
165
+ }
app/code/community/Professio/BudgetMailer/Block/Adminhtml/Helper/Column/Renderer/Parent.php ADDED
@@ -0,0 +1,95 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Professio_BudgetMailer extension
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the MIT License
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/mit-license.php
11
+ *
12
+ * @category Professio
13
+ * @package Professio_BudgetMailer
14
+ * @copyright Copyright (c) 2015
15
+ * @license http://opensource.org/licenses/mit-license.php MIT License
16
+ */
17
+
18
+ /**
19
+ * Parent entities column renderer
20
+ *
21
+ * @category Professio
22
+ * @package Professio_BudgetMailer
23
+ */
24
+ class Professio_BudgetMailer_Block_Adminhtml_Helper_Column_Renderer_Parent
25
+ extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Options
26
+ {
27
+ /**
28
+ * Render the column
29
+ *
30
+ * @param Varien_Object $row
31
+ * @return string
32
+ */
33
+ public function render(Varien_Object $row)
34
+ {
35
+ $base = $this->getColumn()->getBaseLink();
36
+
37
+ if (!$base) {
38
+ return parent::render($row);
39
+ }
40
+
41
+ $options = $this->getColumn()->getOptions();
42
+
43
+ if (!empty($options) && is_array($options)) {
44
+ return $this->renderOptions($options, $row);
45
+ }
46
+ }
47
+
48
+ /**
49
+ * Get params from row
50
+ *
51
+ * @param array $paramsData
52
+ * @param object $row
53
+ * @return array
54
+ */
55
+ public function getParams($paramsData, $row)
56
+ {
57
+ $params = array();
58
+
59
+ if (is_array($paramsData)) {
60
+ foreach ($paramsData as $name=>$getter) {
61
+ if (is_callable(array($row, $getter))) {
62
+ $params[$name] = $row->{$getter}();
63
+ }
64
+ }
65
+ }
66
+
67
+ $staticParamsData = $this->getColumn()->getData('static');
68
+
69
+ if (is_array($staticParamsData)) {
70
+ foreach ($staticParamsData as $key=>$value) {
71
+ $params[$key] = $value;
72
+ }
73
+ }
74
+
75
+ return $params;
76
+ }
77
+
78
+ /**
79
+ * Render options...
80
+ *
81
+ * @param aray $options
82
+ * @param type $row
83
+ * @return type
84
+ */
85
+ protected function renderOptions($options, $row)
86
+ {
87
+ $value = $row->getData($this->getColumn()->getIndex());
88
+
89
+ if (isset($options[$value])) {
90
+ return $options[$value];
91
+ } elseif (in_array($value, $options)) {
92
+ return $value;
93
+ }
94
+ }
95
+ }
app/code/community/Professio/BudgetMailer/Block/Adminhtml/List.php ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Professio_BudgetMailer extension
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the MIT License
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/mit-license.php
11
+ *
12
+ * @category Professio
13
+ * @package Professio_BudgetMailer
14
+ * @copyright Copyright (c) 2015
15
+ * @license http://opensource.org/licenses/mit-license.php MIT License
16
+ */
17
+
18
+ /**
19
+ * List admin block
20
+ *
21
+ * @category Professio
22
+ * @package Professio_BudgetMailer
23
+ */
24
+ class Professio_BudgetMailer_Block_Adminhtml_List
25
+ extends Mage_Adminhtml_Block_Widget_Grid_Container
26
+ {
27
+ /**
28
+ * Constructor
29
+ *
30
+ * @return void
31
+ */
32
+ public function __construct()
33
+ {
34
+ $this->_controller = 'adminhtml_list';
35
+ $this->_blockGroup = 'budgetmailer';
36
+
37
+ parent::__construct();
38
+
39
+ $this->_headerText = Mage::helper('budgetmailer')->__('List');
40
+ $this->_updateButton(
41
+ 'add',
42
+ 'label',
43
+ Mage::helper('budgetmailer')->__('Add List')
44
+ );
45
+ }
46
+ }
app/code/community/Professio/BudgetMailer/Block/Adminhtml/List/Edit.php ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Professio_BudgetMailer extension
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the MIT License
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/mit-license.php
11
+ *
12
+ * @category Professio
13
+ * @package Professio_BudgetMailer
14
+ * @copyright Copyright (c) 2015
15
+ * @license http://opensource.org/licenses/mit-license.php MIT License
16
+ */
17
+
18
+ /**
19
+ * List admin edit form
20
+ *
21
+ * @category Professio
22
+ * @package Professio_BudgetMailer
23
+ */
24
+ class Professio_BudgetMailer_Block_Adminhtml_List_Edit
25
+ extends Mage_Adminhtml_Block_Widget_Form_Container
26
+ {
27
+ /**
28
+ * Constructor
29
+ *
30
+ * @access public
31
+ * @return void
32
+ */
33
+ public function __construct()
34
+ {
35
+ parent::__construct();
36
+
37
+ $this->_blockGroup = 'budgetmailer';
38
+ $this->_controller = 'adminhtml_list';
39
+
40
+ $this->_updateButton(
41
+ 'save', 'label', Mage::helper('budgetmailer')->__('Save List')
42
+ );
43
+ $this->_updateButton(
44
+ 'delete', 'label', Mage::helper('budgetmailer')->__('Delete List')
45
+ );
46
+
47
+ $this->_addButton(
48
+ 'saveandcontinue', array(
49
+ 'label' => Mage::helper('budgetmailer')
50
+ ->__('Save And Continue Edit'),
51
+ 'onclick' => 'saveAndContinueEdit()',
52
+ 'class' => 'save',
53
+ ),
54
+ -100
55
+ );
56
+
57
+ $this->_formScripts[] = "
58
+ function saveAndContinueEdit(){
59
+ editForm.submit($('edit_form').action+'back/edit/');
60
+ }
61
+ ";
62
+ }
63
+
64
+ /**
65
+ * get the edit form header
66
+ *
67
+ * @return string
68
+ */
69
+ public function getHeaderText()
70
+ {
71
+ if (Mage::registry('current_list')
72
+ && Mage::registry('current_list')->getId()) {
73
+ return Mage::helper('budgetmailer')
74
+ ->__(
75
+ "Edit List '%s'",
76
+ $this->escapeHtml(Mage::registry('current_list')->getName())
77
+ );
78
+ } else {
79
+ return Mage::helper('budgetmailer')->__('Add List');
80
+ }
81
+ }
82
+ }
app/code/community/Professio/BudgetMailer/Block/Adminhtml/List/Edit/Form.php ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Professio_BudgetMailer extension
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the MIT License
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/mit-license.php
11
+ *
12
+ * @category Professio
13
+ * @package Professio_BudgetMailer
14
+ * @copyright Copyright (c) 2015
15
+ * @license http://opensource.org/licenses/mit-license.php MIT License
16
+ */
17
+
18
+ /**
19
+ * List edit form
20
+ *
21
+ * @category Professio
22
+ * @package Professio_BudgetMailer
23
+ */
24
+ class Professio_BudgetMailer_Block_Adminhtml_List_Edit_Form
25
+ extends Mage_Adminhtml_Block_Widget_Form
26
+ {
27
+ /**
28
+ * Prepare form
29
+ *
30
+ * @return Professio_BudgetMailer_Block_Adminhtml_List_Edit_Form
31
+ */
32
+ protected function _prepareForm()
33
+ {
34
+ $form = new Varien_Data_Form(
35
+ array(
36
+ 'id' => 'edit_form',
37
+ 'action' => $this->getUrl(
38
+ '*/*/save',
39
+ array('id' => $this->getRequest()->getParam('id'))
40
+ ),
41
+ 'method' => 'post',
42
+ 'enctype' => 'multipart/form-data'
43
+ )
44
+ );
45
+
46
+ $form->setUseContainer(true);
47
+ $this->setForm($form);
48
+
49
+ return parent::_prepareForm();
50
+ }
51
+ }
app/code/community/Professio/BudgetMailer/Block/Adminhtml/List/Edit/Tab/Form.php ADDED
@@ -0,0 +1,110 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Professio_BudgetMailer extension
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the MIT License
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/mit-license.php
11
+ *
12
+ * @category Professio
13
+ * @package Professio_BudgetMailer
14
+ * @copyright Copyright (c) 2015
15
+ * @license http://opensource.org/licenses/mit-license.php MIT License
16
+ */
17
+
18
+ /**
19
+ * List edit form tab
20
+ *
21
+ * @category Professio
22
+ * @package Professio_BudgetMailer
23
+ */
24
+ class Professio_BudgetMailer_Block_Adminhtml_List_Edit_Tab_Form
25
+ extends Mage_Adminhtml_Block_Widget_Form
26
+ {
27
+ /**
28
+ * Prepare the form
29
+ *
30
+ * @return Professio_BudgetMailer_Block_Adminhtml_List_Edit_Tab_Form
31
+ */
32
+ protected function _prepareForm()
33
+ {
34
+ $form = new Varien_Data_Form();
35
+
36
+ $form->setHtmlIdPrefix('list_');
37
+ $form->setFieldNameSuffix('list');
38
+
39
+ $this->setForm($form);
40
+
41
+ $fieldset = $form->addFieldset(
42
+ 'list_form',
43
+ array('legend' => Mage::helper('budgetmailer')->__('List'))
44
+ );
45
+
46
+ $fieldset->addField(
47
+ 'budgetmailer_id',
48
+ 'text',
49
+ array(
50
+ 'label' => Mage::helper('budgetmailer')->__('Budgetmailer ID'),
51
+ 'name' => 'budgetmailer_id',
52
+ 'required' => true,
53
+ 'class' => 'required-entry',
54
+ )
55
+ );
56
+
57
+ $fieldset->addField(
58
+ 'name',
59
+ 'text',
60
+ array(
61
+ 'label' => Mage::helper('budgetmailer')->__('Name'),
62
+ 'name' => 'name',
63
+ 'required' => true,
64
+ 'class' => 'required-entry',
65
+ )
66
+ );
67
+
68
+ $fieldset->addField(
69
+ 'status',
70
+ 'select',
71
+ array(
72
+ 'label' => Mage::helper('budgetmailer')->__('Status'),
73
+ 'name' => 'status',
74
+ 'values'=> array(
75
+ array(
76
+ 'value' => 1,
77
+ 'label' => Mage::helper('budgetmailer')->__('Enabled'),
78
+ ),
79
+ array(
80
+ 'value' => 0,
81
+ 'label' => Mage::helper('budgetmailer')->__('Disabled'),
82
+ ),
83
+ ),
84
+ )
85
+ );
86
+
87
+ $formValues = Mage::registry('current_list')->getDefaultValues();
88
+
89
+ if (!is_array($formValues)) {
90
+ $formValues = array();
91
+ }
92
+
93
+ if (Mage::getSingleton('adminhtml/session')->getListData()) {
94
+ $formValues = array_merge(
95
+ $formValues,
96
+ Mage::getSingleton('adminhtml/session')->getListData()
97
+ );
98
+ Mage::getSingleton('adminhtml/session')->setListData(null);
99
+ } elseif (Mage::registry('current_list')) {
100
+ $formValues = array_merge(
101
+ $formValues,
102
+ Mage::registry('current_list')->getData()
103
+ );
104
+ }
105
+
106
+ $form->setValues($formValues);
107
+
108
+ return parent::_prepareForm();
109
+ }
110
+ }
app/code/community/Professio/BudgetMailer/Block/Adminhtml/List/Edit/Tabs.php ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Professio_BudgetMailer extension
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the MIT License
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/mit-license.php
11
+ *
12
+ * @category Professio
13
+ * @package Professio_BudgetMailer
14
+ * @copyright Copyright (c) 2015
15
+ * @license http://opensource.org/licenses/mit-license.php MIT License
16
+ */
17
+
18
+ /**
19
+ * List admin edit tabs
20
+ *
21
+ * @category Professio
22
+ * @package Professio_BudgetMailer
23
+ */
24
+ class Professio_BudgetMailer_Block_Adminhtml_List_Edit_Tabs
25
+ extends Mage_Adminhtml_Block_Widget_Tabs
26
+ {
27
+ /**
28
+ * Constructor
29
+ */
30
+ public function __construct()
31
+ {
32
+ parent::__construct();
33
+
34
+ $this->setId('list_tabs');
35
+ $this->setDestElementId('edit_form');
36
+ $this->setTitle(Mage::helper('budgetmailer')->__('List'));
37
+ }
38
+
39
+ /**
40
+ * Before render html
41
+ *
42
+ * @return Professio_BudgetMailer_Block_Adminhtml_List_Edit_Tabs
43
+ */
44
+ protected function _beforeToHtml()
45
+ {
46
+ $this->addTab(
47
+ 'form_list', array(
48
+ 'label' => Mage::helper('budgetmailer')->__('List'),
49
+ 'title' => Mage::helper('budgetmailer')->__('List'),
50
+ 'content' => $this->getLayout()
51
+ ->createBlock('budgetmailer/adminhtml_list_edit_tab_form')
52
+ ->toHtml(),
53
+ )
54
+ );
55
+
56
+ return parent::_beforeToHtml();
57
+ }
58
+
59
+ /**
60
+ * Retrieve list entity
61
+ *
62
+ * @return Professio_BudgetMailer_Model_List
63
+ */
64
+ public function getList()
65
+ {
66
+ return Mage::registry('current_list');
67
+ }
68
+ }
app/code/community/Professio/BudgetMailer/Block/Adminhtml/List/Grid.php ADDED
@@ -0,0 +1,222 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Professio_BudgetMailer extension
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the MIT License
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/mit-license.php
11
+ *
12
+ * @category Professio
13
+ * @package Professio_BudgetMailer
14
+ * @copyright Copyright (c) 2015
15
+ * @license http://opensource.org/licenses/mit-license.php MIT License
16
+ */
17
+
18
+ /**
19
+ * List admin grid block
20
+ *
21
+ * @category Professio
22
+ * @package Professio_BudgetMailer
23
+ */
24
+ class Professio_BudgetMailer_Block_Adminhtml_List_Grid
25
+ extends Mage_Adminhtml_Block_Widget_Grid
26
+ {
27
+ /**
28
+ * Constructor
29
+ *
30
+ * @return void
31
+ */
32
+ public function __construct()
33
+ {
34
+ parent::__construct();
35
+
36
+ $this->setId('listGrid');
37
+ $this->setDefaultSort('entity_id');
38
+ $this->setDefaultDir('ASC');
39
+ $this->setSaveParametersInSession(true);
40
+ $this->setUseAjax(true);
41
+ }
42
+
43
+ /**
44
+ * Prepare collection
45
+ *
46
+ * @return Professio_BudgetMailer_Block_Adminhtml_List_Grid
47
+ */
48
+ protected function _prepareCollection()
49
+ {
50
+ $collection = Mage::getModel('budgetmailer/list')->getCollection();
51
+ $this->setCollection($collection);
52
+ return parent::_prepareCollection();
53
+ }
54
+
55
+ /**
56
+ * Prepare grid collection
57
+ *
58
+ * @return Professio_BudgetMailer_Block_Adminhtml_List_Grid
59
+ */
60
+ protected function _prepareColumns()
61
+ {
62
+ $this->addColumn(
63
+ 'entity_id',
64
+ array(
65
+ 'header' => Mage::helper('budgetmailer')->__('Id'),
66
+ 'index' => 'entity_id',
67
+ 'type' => 'number'
68
+ )
69
+ );
70
+
71
+ $this->addColumn(
72
+ 'name',
73
+ array(
74
+ 'header' => Mage::helper('budgetmailer')->__('Name'),
75
+ 'align' => 'left',
76
+ 'index' => 'name',
77
+ )
78
+ );
79
+
80
+ $this->addColumn(
81
+ 'status',
82
+ array(
83
+ 'header' => Mage::helper('budgetmailer')->__('Status'),
84
+ 'index' => 'status',
85
+ 'type' => 'options',
86
+ 'options' => array(
87
+ '1' => Mage::helper('budgetmailer')->__('Enabled'),
88
+ '0' => Mage::helper('budgetmailer')->__('Disabled'),
89
+ )
90
+ )
91
+ );
92
+
93
+ $this->addColumn(
94
+ 'budgetmailer_id', array(
95
+ 'header'=> Mage::helper('budgetmailer')->__('Budgetmailer ID'),
96
+ 'index' => 'budgetmailer_id',
97
+ 'type'=> 'text',
98
+
99
+ )
100
+ );
101
+
102
+ $this->addColumn(
103
+ 'created_at',
104
+ array(
105
+ 'header' => Mage::helper('budgetmailer')->__('Created at'),
106
+ 'index' => 'created_at',
107
+ 'width' => '120px',
108
+ 'type' => 'datetime',
109
+ )
110
+ );
111
+
112
+ $this->addColumn(
113
+ 'updated_at',
114
+ array(
115
+ 'header' => Mage::helper('budgetmailer')->__('Updated at'),
116
+ 'index' => 'updated_at',
117
+ 'width' => '120px',
118
+ 'type' => 'datetime',
119
+ )
120
+ );
121
+
122
+ $this->addColumn(
123
+ 'action',
124
+ array(
125
+ 'header'=> Mage::helper('budgetmailer')->__('Action'),
126
+ 'width' => '100',
127
+ 'type' => 'action',
128
+ 'getter'=> 'getId',
129
+ 'actions' => array(
130
+ array(
131
+ 'caption' => Mage::helper('budgetmailer')->__('Edit'),
132
+ 'url' => array('base'=> '*/*/edit'),
133
+ 'field' => 'id'
134
+ )
135
+ ),
136
+ 'filter'=> false,
137
+ 'is_system' => true,
138
+ 'sortable' => false,
139
+ )
140
+ );
141
+
142
+ return parent::_prepareColumns();
143
+ }
144
+
145
+ /**
146
+ * Prepare mass action
147
+ *
148
+ * @return Professio_BudgetMailer_Block_Adminhtml_List_Grid
149
+ */
150
+ protected function _prepareMassaction()
151
+ {
152
+ $this->setMassactionIdField('entity_id');
153
+ $this->getMassactionBlock()->setFormFieldName('list');
154
+
155
+ $this->getMassactionBlock()->addItem(
156
+ 'delete',
157
+ array(
158
+ 'label'=> Mage::helper('budgetmailer')->__('Delete'),
159
+ 'url' => $this->getUrl('*/*/massDelete'),
160
+ 'confirm' => Mage::helper('budgetmailer')->__('Are you sure?')
161
+ )
162
+ );
163
+
164
+ $this->getMassactionBlock()->addItem(
165
+ 'status',
166
+ array(
167
+ 'label'=> Mage::helper('budgetmailer')->__('Change status'),
168
+ 'url' => $this->getUrl(
169
+ '*/*/massStatus',
170
+ array('_current'=>true)
171
+ ),
172
+ 'additional' => array(
173
+ 'status' => array(
174
+ 'name' => 'status',
175
+ 'type' => 'select',
176
+ 'class' => 'required-entry',
177
+ 'label' => Mage::helper('budgetmailer')->__('Status'),
178
+ 'values' => array(
179
+ '1' => Mage::helper('budgetmailer')->__('Enabled'),
180
+ '0' => Mage::helper('budgetmailer')->__('Disabled'),
181
+ )
182
+ )
183
+ )
184
+ )
185
+ );
186
+
187
+ return $this;
188
+ }
189
+
190
+ /**
191
+ * Get the row url
192
+ *
193
+ * @param Professio_BudgetMailer_Model_List
194
+ * @return string
195
+ */
196
+ public function getRowUrl($row)
197
+ {
198
+ return $this->getUrl('*/*/edit', array('id' => $row->getId()));
199
+ }
200
+
201
+ /**
202
+ * Get the grid url
203
+ *
204
+ * @return string
205
+ */
206
+ public function getGridUrl()
207
+ {
208
+ return $this->getUrl('*/*/grid', array('_current'=>true));
209
+ }
210
+
211
+ /**
212
+ * After collection load
213
+ *
214
+ * @return Professio_BudgetMailer_Block_Adminhtml_List_Grid
215
+ */
216
+ protected function _afterLoadCollection()
217
+ {
218
+ $this->getCollection()->walk('afterLoad');
219
+
220
+ parent::_afterLoadCollection();
221
+ }
222
+ }
app/code/community/Professio/BudgetMailer/Block/Newsletter.php ADDED
@@ -0,0 +1,103 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Professio_BudgetMailer extension
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the MIT License
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/mit-license.php
11
+ *
12
+ * @category Professio
13
+ * @package Professio_BudgetMailer
14
+ * @copyright Copyright (c) 2015
15
+ * @license http://opensource.org/licenses/mit-license.php MIT License
16
+ */
17
+
18
+ /**
19
+ * Newsletter management block
20
+ *
21
+ * @category Professio
22
+ * @package Professio_BudgetMailer
23
+ */
24
+ class Professio_BudgetMailer_Block_Newsletter extends Mage_Core_Block_Template
25
+ {
26
+ /**
27
+ * Currenct contact
28
+ *
29
+ * @var Professio_BudgetMailer_Model_Contact
30
+ */
31
+ protected $_contact;
32
+
33
+ /**
34
+ * Constructor... set template
35
+ */
36
+ public function __construct()
37
+ {
38
+ parent::__construct();
39
+
40
+ $this->setTemplate('budgetmailer/newsletter.phtml');
41
+ }
42
+
43
+ /**
44
+ * Get URL for the form
45
+ *
46
+ * @return string
47
+ */
48
+ public function getAction()
49
+ {
50
+ return $this->getUrl('*/*/save');
51
+ }
52
+
53
+ /**
54
+ * Get config helper
55
+ *
56
+ * @return Professio_BudgetMailer_Helper_Config
57
+ */
58
+ public function getConfigHelper()
59
+ {
60
+ return Mage::helper('budgetmailer/config');
61
+ }
62
+
63
+ /**
64
+ * Get current contact
65
+ *
66
+ * @return Professio_BudgetMailer_Model_Contact
67
+ */
68
+ protected function getContact()
69
+ {
70
+ if (!isset($this->_contact)) {
71
+ $this->_contact = Mage::getModel('budgetmailer/contact');
72
+ $this->_contact->loadByCustomer($this->getCustomer());
73
+ }
74
+
75
+ return $this->_contact;
76
+ }
77
+
78
+ /**
79
+ * Get current customer
80
+ *
81
+ * @return Professio_Customer_Model_Customer
82
+ */
83
+ protected function getCustomer()
84
+ {
85
+ return Mage::getSingleton('customer/session')->getCustomer();
86
+ }
87
+
88
+ /**
89
+ * Check if current contact is subscribed
90
+ *
91
+ * @return boolean
92
+ */
93
+ public function isSubscribed()
94
+ {
95
+ if (!isset($this->isSubscribed)) {
96
+ $this->isSubscribed = $this->getContact()
97
+ && $this->getContact()->getEntityId()
98
+ && !$this->getContact()->getUnsubscribed();
99
+ }
100
+
101
+ return $this->isSubscribed;
102
+ }
103
+ }
app/code/community/Professio/BudgetMailer/Block/Subscribe.php ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Professio_BudgetMailer extension
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the MIT License
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/mit-license.php
11
+ *
12
+ * @category Professio
13
+ * @package Professio_BudgetMailer
14
+ * @copyright Copyright (c) 2015
15
+ * @license http://opensource.org/licenses/mit-license.php MIT License
16
+ */
17
+
18
+ /**
19
+ * Newsletter subscription form block
20
+ *
21
+ * @category Professio
22
+ * @package Professio_BudgetMailer
23
+ */
24
+ class Professio_BudgetMailer_Block_Subscribe extends Mage_Core_Block_Template
25
+ {
26
+ /**
27
+ * Retrieve form action url and set "secure" param to avoid confirm
28
+ * message when we submit form from secure page to unsecure
29
+ *
30
+ * @return string
31
+ */
32
+ public function getFormActionUrl()
33
+ {
34
+ return $this->getUrl(
35
+ 'budgetmailer/subscriber/subscribe',
36
+ array('_secure' => true)
37
+ );
38
+ }
39
+
40
+ /**
41
+ * Get config helper
42
+ *
43
+ * @return Professio_BudgetMailer_Helper_Config
44
+ */
45
+ public function getConfigHelper()
46
+ {
47
+ return Mage::helper('budgetmailer/config');
48
+ }
49
+ }
app/code/community/Professio/BudgetMailer/Block/Subscribe/Mini.php ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Professio_BudgetMailer extension
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the MIT License
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/mit-license.php
11
+ *
12
+ * @category Professio
13
+ * @package Professio_BudgetMailer
14
+ * @copyright Copyright (c) 2015
15
+ * @license http://opensource.org/licenses/mit-license.php MIT License
16
+ */
17
+
18
+ /**
19
+ * Mini subscribe widget (checkbox)
20
+ *
21
+ * @category Professio
22
+ * @package Professio_BudgetMailer
23
+ */
24
+ class Professio_BudgetMailer_Block_Subscribe_Mini
25
+ extends Mage_Core_Block_Template
26
+ {
27
+ /**
28
+ * Get config helper
29
+ * @return Professio_BudgetMailer_Helper_Config
30
+ */
31
+ public function getConfigHelper()
32
+ {
33
+ return Mage::helper('budgetmailer/config');
34
+ }
35
+
36
+ /**
37
+ * Get form data, in fact only returns new varien object
38
+ * @return Varien_Object
39
+ */
40
+ public function getFormData()
41
+ {
42
+ return new Varien_Object;
43
+ }
44
+ }
app/code/community/Professio/BudgetMailer/Controller/Adminhtml/BudgetMailer.php ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Professio_BudgetMailer extension
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the MIT License
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/mit-license.php
11
+ *
12
+ * @category Professio
13
+ * @package Professio_BudgetMailer
14
+ * @copyright Copyright (c) 2015
15
+ * @license http://opensource.org/licenses/mit-license.php MIT License
16
+ */
17
+
18
+ /**
19
+ * Module base admin controller
20
+ *
21
+ * @category Professio
22
+ * @package Professio_BudgetMailer
23
+ */
24
+ class Professio_BudgetMailer_Controller_Adminhtml_BudgetMailer
25
+ extends Mage_Adminhtml_Controller_Action
26
+ {
27
+ /**
28
+ * Upload file and get the uploaded name
29
+ *
30
+ * @param string $input
31
+ * @param string $destinationFolder
32
+ * @param array $data
33
+ * @return string
34
+ */
35
+ protected function _uploadAndGetName($input, $destinationFolder, $data)
36
+ {
37
+ try{
38
+ if (isset($data[$input]['delete'])) {
39
+ return '';
40
+ } else {
41
+ $uploader = new Varien_File_Uploader($input);
42
+ $uploader->setAllowRenameFiles(true);
43
+ $uploader->setFilesDispersion(true);
44
+ $uploader->setAllowCreateFolders(true);
45
+ $result = $uploader->save($destinationFolder);
46
+ return $result['file'];
47
+ }
48
+ }
49
+ catch (Exception $e) {
50
+ if ($e->getCode() != Varien_File_Uploader::TMP_NAME_EMPTY) {
51
+ throw $e;
52
+ } else {
53
+ if (isset($data[$input]['value'])) {
54
+ return $data[$input]['value'];
55
+ }
56
+ }
57
+ }
58
+ return '';
59
+ }
60
+ }
app/code/community/Professio/BudgetMailer/Exception.php ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Professio_BudgetMailer extension
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the MIT License
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/mit-license.php
11
+ *
12
+ * @category Professio
13
+ * @package Professio_BudgetMailer
14
+ * @copyright Copyright (c) 2015
15
+ * @license http://opensource.org/licenses/mit-license.php MIT License
16
+ */
17
+
18
+ /**
19
+ * Custom exception
20
+ *
21
+ * @category Professio
22
+ * @package Professio_BudgetMailer
23
+ */
24
+ class Professio_BudgetMailer_Exception extends Mage_Core_Exception
25
+ {
26
+
27
+ }
app/code/community/Professio/BudgetMailer/Form/Element/Tags.php ADDED
@@ -0,0 +1,103 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Professio_BudgetMailer extension
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the MIT License
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/mit-license.php
11
+ *
12
+ * @category Professio
13
+ * @package Professio_BudgetMailer
14
+ * @copyright Copyright (c) 2015
15
+ * @license http://opensource.org/licenses/mit-license.php MIT License
16
+ */
17
+
18
+ /**
19
+ * List collection resource
20
+ *
21
+ * @category Professio
22
+ * @package Professio_BudgetMailer
23
+ */
24
+ class Professio_BudgetMailer_Form_Element_Tags
25
+ extends Varien_Data_Form_Element_Abstract
26
+ {
27
+ /**
28
+ * Constructor
29
+ * @param array $data
30
+ */
31
+ public function __construct($data)
32
+ {
33
+ parent::__construct($data);
34
+
35
+ $this->setType('budgetmailer_tags');
36
+ }
37
+
38
+ /**
39
+ * Get element html
40
+ *
41
+ * @return string
42
+ */
43
+ public function getElementHtml()
44
+ {
45
+ $html = '<div id="budgetmailer_tags">';
46
+ $html .= '<!--<input id="budgetmailer_tags_remove" '
47
+ . 'name="tags_remove" type="hidden"/>-->';
48
+
49
+ $html .= '<script type="text/javascript">
50
+ var $budgetmailer_tags = $("budgetmailer_tags");
51
+ //var $budgetmailer_tags_remove = $("budgetmailer_tags_remove");
52
+
53
+ function budgetmailer_new(el) {
54
+ $budgetmailer_tags.insert("<input name=\"'
55
+ . $this->getName()
56
+ . '[]\" type=\"text\" value=\"\"/> <button class=\"budgetmailer-remove\" '
57
+ . 'onclick=\"return budgetmailer_remove(this);\">'
58
+ . Mage::helper('budgetmailer')->__('Remove')
59
+ . '</button><br>");
60
+
61
+ return false;
62
+ }
63
+
64
+ function budgetmailer_remove(el) {
65
+ var button = $(el);
66
+ var input = button.previous("input");
67
+ var br = button.next("br");
68
+ //var remove = $budgetmailer_tags_remove.getValue();
69
+ //$budgetmailer_tags_remove.setValue(remove + "," + input.getValue());
70
+
71
+ button.remove();
72
+ $(input).remove();
73
+ $(br).remove();
74
+
75
+ return false;
76
+ }
77
+ </script>';
78
+
79
+ $html .= '<button id="budgetmailer_add" onclick="return '
80
+ . 'budgetmailer_new(this);">'
81
+ . Mage::helper('budgetmailer')->__('Add Tag')
82
+ . '</button><br>';
83
+
84
+ if (is_array($this->getValue()) && count($this->getValue())) {
85
+ foreach ($this->getValue() as $tag) {
86
+ $html .= '<input name="'
87
+ . $this->getName()
88
+ . '[]" type="text" value="'
89
+ . $tag
90
+ . '"/> <button class="budgetmailer-remove" '
91
+ . 'onclick="return budgetmailer_remove(this);">'
92
+ . Mage::helper('budgetmailer')->__('Remove')
93
+ . '</button><br>';
94
+ }
95
+ } else {
96
+ $html .= Mage::helper('budgetmailer')->__('No Tags');
97
+ }
98
+
99
+ $html .= '</div>';
100
+
101
+ return $html;
102
+ }
103
+ }
app/code/community/Professio/BudgetMailer/Helper/Config.php ADDED
@@ -0,0 +1,230 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Professio_BudgetMailer extension
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the MIT License
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/mit-license.php
11
+ *
12
+ * @category Professio
13
+ * @package Professio_BudgetMailer
14
+ * @copyright Copyright (c) 2015
15
+ * @license http://opensource.org/licenses/mit-license.php MIT License
16
+ */
17
+
18
+ /**
19
+ * Config helper
20
+ *
21
+ * @category Professio
22
+ * @package Professio_BudgetMailer
23
+ */
24
+ class Professio_BudgetMailer_Helper_Config extends Mage_Core_Helper_Abstract
25
+ {
26
+ const CONFIG_PATH_ADVANCED_ADDRESS_TYPE =
27
+ 'budgetmailer/advanced/address_type';
28
+ const CONFIG_PATH_ADVANCED_ON_ADDRESS_DELETE =
29
+ 'budgetmailer/advanced/on_address_delete';
30
+ const CONFIG_PATH_ADVANCED_ON_ADDRESS_UPDATE =
31
+ 'budgetmailer/advanced/on_address_update';
32
+ const CONFIG_PATH_ADVANCED_ON_CUSTOMER_DELETE =
33
+ 'budgetmailer/advanced/on_customer_delete';
34
+ const CONFIG_PATH_ADVANCED_ON_CUSTOMER_UPDATE =
35
+ 'budgetmailer/advanced/on_customer_update';
36
+ const CONFIG_PATH_ADVANCED_ON_ORDER =
37
+ 'budgetmailer/advanced/on_order';
38
+ const CONFIG_PATH_ADVANCED_FRONTEND =
39
+ 'budgetmailer/advanced/frontend';
40
+
41
+ const CONFIG_PATH_API_ENDPOINT = 'budgetmailer/api/endpoint';
42
+ const CONFIG_PATH_API_KEY = 'budgetmailer/api/key';
43
+ const CONFIG_PATH_API_SECRET = 'budgetmailer/api/secret';
44
+
45
+ const CONFIG_PATH_GENERAL_LIST = 'budgetmailer/general/list';
46
+
47
+ const CONFIG_PATH_SYNC_CRON = 'budgetmailer/sync/cron';
48
+ const CONFIG_PATH_SYNC_WEBHOOK = 'budgetmailer/sync/webhook';
49
+ const CONFIG_PATH_SYNC_TTL = 'budgetmailer/sync/ttl';
50
+
51
+ /**
52
+ * Get address type
53
+ *
54
+ * @return string
55
+ */
56
+ public function getAdvancedAddressType()
57
+ {
58
+ return Mage::getStoreConfig(self::CONFIG_PATH_ADVANCED_ADDRESS_TYPE);
59
+ }
60
+
61
+ /**
62
+ * Get enabled front-end
63
+ *
64
+ * @return boolean
65
+ */
66
+ public function isAdvancedFrontendEnabled()
67
+ {
68
+ return Mage::getStoreConfig(self::CONFIG_PATH_ADVANCED_FRONTEND);
69
+ }
70
+
71
+ /**
72
+ * Get on address delete enabled
73
+ *
74
+ * @return boolean
75
+ */
76
+ public function isAdvancedOnAddressDeleteEnabled()
77
+ {
78
+ $c =
79
+ Professio_BudgetMailer_Model_Config_Source_Delete_Address
80
+ ::ON_DELETE_USE_NEW;
81
+
82
+ $v = Mage::getStoreConfig(
83
+ self::CONFIG_PATH_ADVANCED_ON_ADDRESS_DELETE
84
+ );
85
+
86
+ return $c == $v;
87
+ }
88
+
89
+ /**
90
+ * Get on address update enabled
91
+ *
92
+ * @return boolean
93
+ */
94
+ public function isAdvancedOnAddressUpdateEnabled()
95
+ {
96
+ $c =
97
+ Professio_BudgetMailer_Model_Config_Source_Update::ON_UPDATE_UPDATE;
98
+
99
+ $v = Mage::getStoreConfig(
100
+ self::CONFIG_PATH_ADVANCED_ON_ADDRESS_UPDATE
101
+ );
102
+
103
+ return $c == $v;
104
+ }
105
+
106
+ /**
107
+ * Get on customer delete enabled
108
+ *
109
+ * @return boolean
110
+ */
111
+ public function isAdvancedOnCustomerDeleteEnabled()
112
+ {
113
+ $c =
114
+ Professio_BudgetMailer_Model_Config_Source_Delete::ON_DELETE_DELETE;
115
+
116
+ $v = Mage::getStoreConfig(
117
+ self::CONFIG_PATH_ADVANCED_ON_CUSTOMER_DELETE
118
+ );
119
+
120
+ return $c == $v;
121
+ }
122
+
123
+ /**
124
+ * Get on customer update enabled
125
+ *
126
+ * @return boolean
127
+ */
128
+ public function isAdvancedOnCustomerUpdateEnabled()
129
+ {
130
+ $c =
131
+ Professio_BudgetMailer_Model_Config_Source_Update::ON_UPDATE_UPDATE;
132
+
133
+ $v = Mage::getStoreConfig(
134
+ self::CONFIG_PATH_ADVANCED_ON_CUSTOMER_UPDATE
135
+ );
136
+
137
+ return $c == $v;
138
+ }
139
+
140
+ /**
141
+ * Get on new order
142
+ *
143
+ * @return boolean
144
+ */
145
+ public function isAdvancedOnOrderEnabled()
146
+ {
147
+ return Mage::getStoreConfig(
148
+ self::CONFIG_PATH_ADVANCED_ON_ORDER
149
+ );
150
+ }
151
+
152
+ /**
153
+ * Get API endpoint
154
+ *
155
+ * @return string
156
+ */
157
+ public function getApiEndpoint()
158
+ {
159
+ return Mage::getStoreConfig(self::CONFIG_PATH_API_ENDPOINT);
160
+ }
161
+
162
+ /**
163
+ * Get API key
164
+ *
165
+ * @return string
166
+ */
167
+ public function getApiKey()
168
+ {
169
+ return Mage::getStoreConfig(self::CONFIG_PATH_API_KEY);
170
+ }
171
+
172
+ /**
173
+ * Get API secret
174
+ *
175
+ * @return string
176
+ */
177
+ public function getApiSecret()
178
+ {
179
+ return Mage::getStoreConfig(self::CONFIG_PATH_API_SECRET);
180
+ }
181
+
182
+ /**
183
+ * Get API list
184
+ *
185
+ * @return string
186
+ * @throws Professio_BudgetMailer_Exception
187
+ */
188
+ public function getGeneralList()
189
+ {
190
+ $generalList = Mage::getStoreConfig(self::CONFIG_PATH_GENERAL_LIST);
191
+
192
+ if (empty($generalList)) {
193
+ throw new Professio_BudgetMailer_Exception(
194
+ $this->__('BudgetMailer list is not set.')
195
+ );
196
+ }
197
+
198
+ return $generalList;
199
+ }
200
+
201
+ /**
202
+ * Get sync cron
203
+ *
204
+ * @return boolean
205
+ */
206
+ public function isSyncCronEnabled()
207
+ {
208
+ return Mage::getStoreConfig(self::CONFIG_PATH_SYNC_CRON);
209
+ }
210
+
211
+ /**
212
+ * Get sync webhook
213
+ *
214
+ * @return boolean
215
+ */
216
+ public function isSyncWebhookEnabled()
217
+ {
218
+ return Mage::getStoreConfig(self::CONFIG_PATH_SYNC_WEBHOOK);
219
+ }
220
+
221
+ /**
222
+ * Get sync ttl
223
+ *
224
+ * @return integer
225
+ */
226
+ public function getSyncTtl()
227
+ {
228
+ return Mage::getStoreConfig(self::CONFIG_PATH_SYNC_TTL);
229
+ }
230
+ }
app/code/community/Professio/BudgetMailer/Helper/Data.php ADDED
@@ -0,0 +1,132 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Professio_BudgetMailer extension
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the MIT License
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/mit-license.php
11
+ *
12
+ * @category Professio
13
+ * @package Professio_BudgetMailer
14
+ * @copyright Copyright (c) 2015
15
+ * @license http://opensource.org/licenses/mit-license.php MIT License
16
+ */
17
+
18
+ /**
19
+ * Data helper
20
+ *
21
+ * @category Professio
22
+ * @package Professio_BudgetMailer
23
+ */
24
+ class Professio_BudgetMailer_Helper_Data extends Mage_Core_Helper_Abstract
25
+ {
26
+ /**
27
+ * Test http headers signature
28
+ *
29
+ * @param array $headers
30
+ * @return boolean
31
+ */
32
+ public function checkSignature($headers)
33
+ {
34
+ $signature = hash_hmac(
35
+ 'sha256',
36
+ $headers['SALT'],
37
+ Mage::helper('budgetmailer/config')->getApiSecret(),
38
+ true
39
+ );
40
+
41
+ $signatureEncoded = base64_encode($signature);
42
+
43
+ Mage::log(
44
+ 'budgetmailer/data_helper::checkSignature() '
45
+ . 'headers: ' . json_encode($headers)
46
+ . 'signature: ' . $signature
47
+ . 'signature encoded: '
48
+ . str_replace('%3d', '=', $signatureEncoded)
49
+ );
50
+
51
+ return $signatureEncoded
52
+ == str_replace('%3d', '=', $signatureEncoded);
53
+ }
54
+
55
+ /**
56
+ * Get order tags (ordered products category names)
57
+ *
58
+ * @param Mage_Sales_Model_Order $order
59
+ * @return array
60
+ */
61
+ public function getOrderTags(Mage_Sales_Model_Order $order)
62
+ {
63
+ $items = $order->getAllItems();
64
+ $orderTags = array();
65
+
66
+ foreach ($items as $item) {
67
+ $product = $item->getProduct();
68
+ $categoryCollection = $product->getCategoryCollection();
69
+ $categoryCollection->addAttributeToSelect('name');
70
+ $categoryCollection->clear()->load();
71
+
72
+ foreach ($categoryCollection->getIterator() as $category) {
73
+ $orderTags[] = $category->getName();
74
+ }
75
+ }
76
+
77
+ return $orderTags;
78
+ }
79
+
80
+ /**
81
+ * Retrieve customer's primary address of configured type
82
+ * @param Mage_Customer_Model_Customer $customer
83
+ * @return Mage_Customer_Model_Address
84
+ */
85
+ public function getCustomersPrimaryAddress(
86
+ Mage_Customer_Model_Customer $customer
87
+ )
88
+ {
89
+ $addressType = Mage::helper('budgetmailer/config')
90
+ ->getAdvancedAddressType();
91
+
92
+ Mage::log(
93
+ 'budgetmailer/data_helper::getCustomersPrimaryAddress() '
94
+ . 'type: ' . $addressType
95
+ );
96
+
97
+ switch($addressType) {
98
+ case 'billing':
99
+ $address = $customer->getPrimaryBillingAddress();
100
+ break;
101
+ case 'shipping':
102
+ $address = $customer->getPrimaryShippingAddress();
103
+ break;
104
+ }
105
+
106
+ Mage::log(
107
+ 'budgetmailer/data_helper::getCustomersPrimaryAddress() '
108
+ . ' address id: ' . ( $address ? $address->getEntityId() : 'no' )
109
+ );
110
+
111
+ return $address;
112
+ }
113
+
114
+ /**
115
+ * Get default website id
116
+ *
117
+ * @return integer
118
+ */
119
+ public function getDefaultWebsiteId()
120
+ {
121
+ $resource = Mage::getSingleton('core/resource');
122
+ $sql = 'SELECT * FROM ' . $resource->getTableName('core/store')
123
+ . ' WHERE `code` = "default"';
124
+
125
+ $connection = Mage::getSingleton('core/resource')
126
+ ->getConnection('core_read');
127
+
128
+ $row = $connection->fetchOne($sql);
129
+
130
+ return $row ? $row : 1;
131
+ }
132
+ }
app/code/community/Professio/BudgetMailer/Helper/Mapper.php ADDED
@@ -0,0 +1,539 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Professio_BudgetMailer extension
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the MIT License
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/mit-license.php
11
+ *
12
+ * @category Professio
13
+ * @package Professio_BudgetMailer
14
+ * @copyright Copyright (c) 2015
15
+ * @license http://opensource.org/licenses/mit-license.php MIT License
16
+ */
17
+
18
+ /**
19
+ * Helper mapper
20
+ *
21
+ * @category Professio
22
+ * @package Professio_BudgetMailer
23
+ */
24
+ class Professio_BudgetMailer_Helper_Mapper extends Mage_Core_Helper_Abstract
25
+ {
26
+ /**
27
+ * Address model to contact model
28
+ * @var array
29
+ */
30
+ protected $_addressToContact = array(
31
+ 'companyName' => 'company',
32
+ // INFO for now not replacing multiple to single line
33
+ 'address' => 'street',
34
+ 'postalCode' => 'postcode',
35
+ 'city' => 'city',
36
+ 'countryCode' => 'country_id',
37
+ 'telephone' => 'telephone',
38
+ );
39
+
40
+ /**
41
+ * INFO not mapping names, because they are coming from customer model
42
+ *
43
+ * Address contact to model
44
+ * @var array
45
+ */
46
+ protected $_addressToModel = array(
47
+ // 'firstname' => 'first_name',
48
+ // 'lastname' => 'last_name',
49
+ 'company' => 'company_name',
50
+ 'city' => 'city',
51
+ 'postcode' => 'postal_code',
52
+ 'street' => 'address',
53
+ 'telephone' => 'telephone',
54
+ 'country_id' => 'country_code'
55
+ );
56
+
57
+ /**
58
+ * Contact to model
59
+ * @var array
60
+ */
61
+ protected $_contactToModel = array(
62
+ 'id' => 'budgetmailer_id',
63
+ 'list' => 'list_id',
64
+
65
+ 'email' => 'email',
66
+ // INFO not mapping company name, because the editable one is in address
67
+ // kind of need this when saving customer address (on front-end)
68
+ 'companyName' => 'company_name',
69
+ 'firstName' => 'first_name',
70
+ 'insertion' => 'insertion',
71
+ 'lastName' => 'last_name',
72
+ 'sex' => 'sex',
73
+ 'address' => 'address',
74
+ 'postalCode' => 'postal_code',
75
+ 'city' => 'city',
76
+ 'countryCode' => 'country_code',
77
+ 'telephone' => 'telephone',
78
+ 'mobile' => 'mobile',
79
+ 'remarks' => 'remarks',
80
+ 'tags' => 'tags',
81
+ 'unsubscribed' => 'unsubscribed',
82
+ 'subscribe' => 'subscribe'
83
+ );
84
+
85
+ /**
86
+ * List of countries by id
87
+ * @var array
88
+ */
89
+ protected $_countriesIdIso3 = array();
90
+ /**
91
+ * List of countries by ISO3 code
92
+ * @var array
93
+ */
94
+ protected $_countriesIso3Id = array();
95
+
96
+ /**
97
+ * Customer model to API object
98
+ * @var array
99
+ */
100
+ protected $_customerToApi = array(
101
+ 'email' => 'email',
102
+ 'firstname' => 'firstName',
103
+ 'lastname' => 'lastName',
104
+ 'sex' => 'gender',
105
+ );
106
+
107
+ /**
108
+ * Customer to contact model
109
+ * @var array
110
+ */
111
+ protected $_customerToModel = array(
112
+ 'email' => 'email',
113
+ 'firstname' => 'first_name',
114
+ 'lastname' => 'last_name',
115
+ 'gender' => 'sex'
116
+ );
117
+
118
+ /**
119
+ * Map list ids to names
120
+ * @var array
121
+ */
122
+ protected $_listIdToName;
123
+
124
+ /**
125
+ * Map list names to ids
126
+ * @var array
127
+ */
128
+ protected $_listNameToId;
129
+
130
+ /**
131
+ * Map list ids to budgetmailer ids
132
+ * @var array
133
+ */
134
+ protected $_listIdToBudgetmailerId;
135
+
136
+ /**
137
+ * Map list budgetmailer ids to ids
138
+ * @var array
139
+ */
140
+ protected $_listBudgetmailerIdToId;
141
+
142
+ /**
143
+ * INFO list->primary is not mapped
144
+ * List to model
145
+ * @var array
146
+ */
147
+ protected $_listToModel = array(
148
+ 'id' => 'budgetmailer_id',
149
+ 'list' => 'name',
150
+ );
151
+
152
+ /**
153
+ * Subscriber to model
154
+ * @var array
155
+ */
156
+ protected $_subscriberToModel = array(
157
+ 'subscriber_email' => 'email',
158
+ 'subscriber_status' => 'unsubscribed',
159
+ );
160
+
161
+ /**
162
+ * Map address to API contact object
163
+ *
164
+ * @param Mage_Customer_Model_Address $address
165
+ * @param stdClass $contactApi
166
+ */
167
+ public function addressToContact(
168
+ Mage_Customer_Model_Address $address, $contactApi
169
+ )
170
+ {
171
+ if (!is_object($contactApi)) {
172
+ $contactApi = new stdClass();
173
+ }
174
+
175
+ foreach ($this->_addressToContact as $keyApi => $keyModel) {
176
+ $contactApi->{$keyApi} = $address->getData($keyModel);
177
+ }
178
+
179
+ $contactApi->countryCode =
180
+ $this->countryIdToCountryCode($contactApi->countryCode);
181
+
182
+ return $contactApi;
183
+ }
184
+
185
+ /**
186
+ * Map address model to contact model
187
+ *
188
+ * @param Mage_Customer_Model_Address $address
189
+ * @param Professio_BudgetMailer_Model_Contact $contact
190
+ */
191
+ public function addressToModel(
192
+ Mage_Customer_Model_Address $address,
193
+ Professio_BudgetMailer_Model_Contact $contact
194
+ )
195
+ {
196
+ foreach ($this->_addressToModel as $keyAddress => $keyModel) {
197
+ $contact->setData($keyModel, $address->getData($keyAddress));
198
+ }
199
+
200
+ $contact->setData(
201
+ 'country_code',
202
+ $this->countryIdToCountryCode($contact->getData('country_code'))
203
+ );
204
+ }
205
+
206
+ /**
207
+ * Map API contact object to address model
208
+ *
209
+ * @param stdClass $contactApi
210
+ * @param Mage_Customer_Model_Address $address
211
+ */
212
+ public function contactToAddress(
213
+ $contactApi,
214
+ Mage_Customer_Model_Address $address
215
+ )
216
+ {
217
+ foreach ($this->_addressToContact as $keyApi => $keyModel) {
218
+ if (isset($contactApi->{$keyApi})) {
219
+ $address->setData($keyModel, $contactApi->{$keyApi});
220
+ }
221
+ }
222
+
223
+ $address->setData(
224
+ 'country_id',
225
+ $this->countryCodeToCountryId($contactApi->countryCode)
226
+ );
227
+ }
228
+
229
+ /**
230
+ * Map API contact object to contact model
231
+ *
232
+ * @param stdClass $contactApi
233
+ * @param Professio_BudgetMailer_Model_Contact $contact
234
+ */
235
+ public function contactToModel(
236
+ $contactApi,
237
+ Professio_BudgetMailer_Model_Contact $contact
238
+ )
239
+ {
240
+ foreach ($this->_contactToModel as $keyApi => $keyModel) {
241
+ if (isset($contactApi->{$keyApi})) {
242
+ $contact->setData($keyModel, $contactApi->{$keyApi});
243
+ }
244
+ }
245
+
246
+ $contact->setData(
247
+ 'list_id',
248
+ $this->listNameToListId($contact->getData('list_id'))
249
+ );
250
+ }
251
+
252
+ /**
253
+ * Map contact model to API object
254
+ *
255
+ * @param Professio_BudgetMailer_Model_Contact $contact
256
+ * @param stdClass $contactApi
257
+ *
258
+ * @return stdClass
259
+ */
260
+ public function contactToApi(
261
+ Professio_BudgetMailer_Model_Contact $contact,
262
+ $contactApi = null
263
+ )
264
+ {
265
+ if (!is_object($contactApi)) {
266
+ $contactApi = new stdClass();
267
+ }
268
+
269
+ foreach ($this->_contactToModel as $keyApi => $keyModel) {
270
+ $contactApi->{$keyApi} = $contact->getData($keyModel);
271
+ }
272
+
273
+ $contactApi->sex = (int)$contactApi->sex;
274
+ $contactApi->unsubscribed = (bool)$contactApi->unsubscribed;
275
+
276
+ $contact->setData(
277
+ 'list', $this->listIdToName($contact->getData('list'))
278
+ );
279
+
280
+ return $contactApi;
281
+ }
282
+
283
+ /**
284
+ * Map country code to country id
285
+ *
286
+ * @param string $iso3
287
+ *
288
+ * @return string
289
+ */
290
+ public function countryCodeToCountryId($iso3)
291
+ {
292
+ if (!isset($this->_countriesIso3Id[$iso3])) {
293
+ $country = Mage::getModel('directory/country')
294
+ ->loadByCode($iso3, 3);
295
+ $this->_countriesIso3Id[$iso3] = $country->getId();
296
+ }
297
+
298
+ return $this->_countriesIso3Id[$iso3];
299
+ }
300
+
301
+ /**
302
+ * Map country id to country code
303
+ *
304
+ * @param string $countryId
305
+ *
306
+ * @return string
307
+ */
308
+ public function countryIdToCountryCode($countryId)
309
+ {
310
+ if (!isset($this->_countriesIdIso3[$countryId])) {
311
+ $country = Mage::getModel('directory/country')->load($countryId);
312
+ $this->_countriesIdIso3[$countryId] = $country->getIso3Code();
313
+ }
314
+
315
+ return $this->_countriesIdIso3[$countryId];
316
+ }
317
+
318
+ /**
319
+ * Map customer model to api contact
320
+ *
321
+ * @param Mage_Customer_Model_Customer $customer
322
+ * @param stdClass $contactApi
323
+ */
324
+ public function customerToApi(
325
+ Mage_Customer_Model_Customer $customer,
326
+ $contactApi = null
327
+ )
328
+ {
329
+ if (!is_object($contactApi)) {
330
+ $contactApi = new stdClass();
331
+ }
332
+
333
+ foreach ($this->_customerToApi as $keyModel => $keyApi) {
334
+ $contactApi->{$keyApi} = $customer->getData($keyModel);
335
+ }
336
+
337
+ if (Professio_BudgetMailer_Model_Config_Source_Address::BILLING
338
+ == Mage::helper('budgetmailer/config')->getAdvancedAddressType()) {
339
+ $address = $customer->getDefaultBillingAddress();
340
+ } elseif (Professio_BudgetMailer_Model_Config_Source_Address::SHIPPING
341
+ == Mage::helper('budgetmailer/config')->getAdvancedAddressType()) {
342
+ $address = $customer->getDefaultShippingAddress();
343
+ } else {
344
+ $address = false;
345
+ }
346
+
347
+ if ($address && $address->getId()) {
348
+ $this->addressToContact($address, $contactApi);
349
+ }
350
+
351
+ return $contactApi;
352
+ }
353
+
354
+ /**
355
+ * Map customer model to contact model
356
+ *
357
+ * @param Mage_Customer_Model_Customer $customer
358
+ * @param Professio_BudgetMailer_Model_Contact $contact
359
+ */
360
+ public function customerToModel(
361
+ Mage_Customer_Model_Customer $customer,
362
+ Professio_BudgetMailer_Model_Contact $contact
363
+ )
364
+ {
365
+ foreach ($this->_customerToModel as $keyCustomer => $keyModel) {
366
+ $contact->setData($keyModel, $customer->getData($keyCustomer));
367
+ }
368
+ }
369
+
370
+ /**
371
+ * Prepare list maps
372
+ */
373
+ public function initListsMap()
374
+ {
375
+ // INFO in case of initiation of lists collection,
376
+ // we don't want to try load list collection again
377
+ if (Mage::registry('budgetmailer_list_initiation')) {
378
+ return;
379
+ }
380
+
381
+ if (!isset($this->_listIdToName)) {
382
+ $list = Mage::getModel('budgetmailer/list');
383
+ $collection = $list->getCollection();
384
+ $collection->load();
385
+
386
+ foreach ($collection->getIterator() as $list) {
387
+ $this->_listIdToName[$list->getEntityId()] = $list->getName();
388
+ $this->_listNameToId[$list->getName()] = $list->getEntityId();
389
+
390
+ $this->_listBudgetmailerIdToId[$list->getBudgetmailerId()] =
391
+ $list->getEntityId();
392
+ $this->_listIdToBudgetmailerId[$list->getEntityId()] =
393
+ $list->getBudgetmailerId();
394
+ }
395
+ }
396
+ }
397
+
398
+ /**
399
+ * Map list API object to list model
400
+ * @param stdClass $listApi
401
+ * @param Professio_BudgetMailer_Model_List $list
402
+ */
403
+ public function listToModel(
404
+ $listApi, Professio_BudgetMailer_Model_List $list
405
+ )
406
+ {
407
+ foreach ($this->_listToModel as $keyApi => $keyModel) {
408
+ if (isset($listApi->{$keyApi})) {
409
+ $list->setData($keyModel, $listApi->{$keyApi});
410
+ }
411
+ }
412
+
413
+ if (0 == (int)$list->getEntityId()) {
414
+ $list->setData(
415
+ 'entity_id',
416
+ $this->listBudgetmailerIdToListId($list->getEntityId())
417
+ );
418
+ }
419
+ }
420
+
421
+ /**
422
+ * Get list id by name
423
+ *
424
+ * @param string $name
425
+ *
426
+ * @return integer
427
+ */
428
+ public function listNameToListId($name)
429
+ {
430
+ $this->initListsMap();
431
+
432
+ return isset($this->_listNameToId[$name])
433
+ ? $this->_listNameToId[$name] : null;
434
+ }
435
+
436
+ /**
437
+ * Get list id by budgetmailer id
438
+ *
439
+ * @param string $budgetmailerId
440
+ *
441
+ * @return integer
442
+ */
443
+ public function listBudgetmailerIdToListId($budgetmailerId)
444
+ {
445
+ $this->initListsMap();
446
+
447
+ return isset($this->_listBudgetmailerIdToId[$budgetmailerId])
448
+ ? $this->_listBudgetmailerIdToId[$budgetmailerId] : null;
449
+ }
450
+
451
+ /**
452
+ * Get list budgetmailer id by id
453
+ * @param integer $listId
454
+ */
455
+ public function listIdToBudgetmailerId($listId)
456
+ {
457
+ $this->initListsMap();
458
+
459
+ return isset($this->_listIdToBudgetmailerId[$listId])
460
+ ? $this->_listIdToBudgetmailerId[$listId] : null;
461
+ }
462
+
463
+ /**
464
+ * Get list name by id
465
+ *
466
+ * @param integer $listId
467
+ *
468
+ * @return string
469
+ */
470
+ public function listIdToName($listId)
471
+ {
472
+ $this->initListsMap();
473
+
474
+ return isset($this->_listIdToName[$listId])
475
+ ? $this->_listIdToName[$listId] : null;
476
+ }
477
+
478
+ /**
479
+ * Map list model to API object
480
+ * @param Professio_BudgetMailer_Model_List $list
481
+ * @param stdClass $listApi
482
+ */
483
+ public function listToApi(
484
+ Professio_BudgetMailer_Model_List $list,
485
+ $listApi = null
486
+ )
487
+ {
488
+ if (!is_object($listApi)) {
489
+ $listApi = new stdClass();
490
+ }
491
+
492
+ foreach ($this->_listToModel as $keyApi => $keyModel) {
493
+ $listApi->{$keyApi} = $list->getData($keyModel);
494
+ }
495
+
496
+ $list->setData('id', $this->listIdToBudgetmailerId($listApi->id));
497
+ }
498
+
499
+ /**
500
+ * Map subscriber model to contact model
501
+ *
502
+ * @param Mage_Newsletter_Model_Subscriber $subscriber
503
+ * @param Professio_BudgetMailer_Model_Contact $contact
504
+ */
505
+ public function subscriberToModel(
506
+ Mage_Newsletter_Model_Subscriber $subscriber,
507
+ Professio_BudgetMailer_Model_Contact $contact
508
+ )
509
+ {
510
+ foreach ($this->_subscriberToModel as $keySubscriber => $keyModel) {
511
+ $contact->setData(
512
+ $keyModel, $subscriber->getData($keySubscriber)
513
+ );
514
+
515
+ $contact->status = !$contact->status;
516
+ }
517
+ }
518
+
519
+ public function prepareContactApi($contactApiNew)
520
+ {
521
+ foreach ($contactApiNew as $k => $v) {
522
+ if (is_null($v)) {
523
+ unset($contactApiNew->{$k});
524
+ }
525
+ }
526
+
527
+ if (isset($contactApiNew->tags) && (
528
+ !is_array($contactApiNew->tags) || !count($contactApiNew->tags)
529
+ || $contactApiNew->tags == '[]'
530
+ )
531
+ ) {
532
+ unset($contactApiNew->tags);
533
+ }
534
+
535
+ if (empty($contactApiNew->address)) {
536
+ unset($contactApiNew->address);
537
+ }
538
+ }
539
+ }
app/code/community/Professio/BudgetMailer/Model/Client.php ADDED
@@ -0,0 +1,826 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Professio_BudgetMailer extension
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the MIT License
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/mit-license.php
11
+ *
12
+ * @category Professio
13
+ * @package Professio_BudgetMailer
14
+ * @copyright Copyright (c) 2015
15
+ * @license http://opensource.org/licenses/mit-license.php MIT License
16
+ */
17
+
18
+ /**
19
+ * API REST-JSON client
20
+ *
21
+ * @category Professio
22
+ * @package Professio_BudgetMailer
23
+ */
24
+ class Professio_BudgetMailer_Model_Client extends Zend_Rest_Client
25
+ {
26
+ const LIMIT = 1000;
27
+
28
+ /**
29
+ * Caching of API contacts
30
+ *
31
+ * @var boolean
32
+ */
33
+ protected $_cachingEnabled = true;
34
+
35
+ /**
36
+ * Contacts memory cache
37
+ *
38
+ * @var array
39
+ */
40
+ protected $_contacts = array();
41
+
42
+ /**
43
+ * Contacts count in last result
44
+ *
45
+ * @var integer
46
+ */
47
+ protected $_contactsCount;
48
+
49
+ protected $_totalCount;
50
+ protected $_totalFail;
51
+ protected $_totalSuccess;
52
+
53
+ /**
54
+ * API salt
55
+ * @var string
56
+ */
57
+ protected $_salt;
58
+
59
+ /**
60
+ * API signature
61
+ * @var string
62
+ */
63
+ protected $_signature;
64
+
65
+ /**
66
+ * API signature (encoded)
67
+ * @var string
68
+ */
69
+ protected $_signatureEncoded;
70
+
71
+ /**
72
+ * Constructor
73
+ *
74
+ * @param string|Zend_Uri_Http $uri URI for the web service
75
+ *
76
+ * @return void
77
+ */
78
+ public function __construct($uri = null)
79
+ {
80
+ if (!empty($uri)) {
81
+ $this->setUri($uri);
82
+ } else {
83
+ $this->setUri($this->getConfigHelper()->getApiEndpoint());
84
+ }
85
+
86
+ //$this->init();
87
+ }
88
+
89
+ /**
90
+ * Override original Zend_Rest_Client::_performPost()...
91
+ * force content-type to application/json.
92
+ *
93
+ * Performs a POST or PUT request. Any data provided is set in the HTTP
94
+ * client. String data is pushed in as raw POST data; array or object data
95
+ * is pushed in as POST parameters.
96
+ *
97
+ * @param mixed $method
98
+ * @param mixed $data
99
+ *
100
+ * @return Zend_Http_Response
101
+ */
102
+ protected function _performPost($method, $data = null)
103
+ {
104
+ $client = self::getHttpClient();
105
+
106
+ if (is_string($data)) {
107
+ $client->setRawData($data);
108
+ } elseif (is_array($data) || is_object($data)) {
109
+ $client->setParameterPost((array) $data);
110
+ }
111
+
112
+ $client->setHeaders('content-type', 'application/json');
113
+
114
+ return $client->request($method);
115
+ }
116
+
117
+ /**
118
+ * Translation wrapper
119
+ *
120
+ * @param string $s
121
+ *
122
+ * @return string
123
+ */
124
+ protected function __($s)
125
+ {
126
+ return Mage::helper('budgetmailer')->__($s);
127
+ }
128
+
129
+ /**
130
+ * Get cached record
131
+ *
132
+ * @param string $id
133
+ *
134
+ * @return mixed
135
+ */
136
+ protected function getCache($id)
137
+ {
138
+ /*if (isset($this->contacts[$id]) && is_string($this->contacts[$id])) {
139
+ throw new Exception('cache fail');
140
+ }*/
141
+
142
+ return $this->isCachingEnabled()
143
+ && isset($this->_contacts[$id])
144
+ ? $this->_contacts[$id] : null;
145
+ }
146
+
147
+ /**
148
+ * Get caching enabled
149
+ *
150
+ * @return boolean
151
+ */
152
+ protected function isCachingEnabled()
153
+ {
154
+ return $this->_cachingEnabled;
155
+ }
156
+
157
+ /**
158
+ * Get config helper
159
+ *
160
+ * @return Professio_BudgetMailer_Helper_Config
161
+ */
162
+ protected function getConfigHelper()
163
+ {
164
+ return Mage::helper('budgetmailer/config');
165
+ }
166
+
167
+ /**
168
+ * Get default list
169
+ *
170
+ * @return string
171
+ */
172
+ protected function getList()
173
+ {
174
+ return $this->getConfigHelper()->getGeneralList();
175
+ }
176
+
177
+ /**
178
+ * Get / generate salt
179
+ *
180
+ * @return string
181
+ */
182
+ protected function getSalt($reinit = false)
183
+ {
184
+ if (!isset($this->salt) || $reinit) {
185
+ $this->salt = md5(microtime(true));//mt_rand();
186
+ }
187
+
188
+ return $this->salt;
189
+ }
190
+
191
+ /**
192
+ * Get / generate signature
193
+ *
194
+ * @return string
195
+ */
196
+ protected function getSignature($reinit = false)
197
+ {
198
+ if (!isset($this->_signature) || $reinit) {
199
+ $this->_signature = hash_hmac(
200
+ 'sha256',
201
+ $this->getSalt($reinit),
202
+ $this->getConfigHelper()->getApiSecret(),
203
+ true
204
+ );
205
+ }
206
+
207
+ return $this->_signature;
208
+ }
209
+
210
+ /**
211
+ * Get encoded signature
212
+ *
213
+ * @return string
214
+ */
215
+ protected function getSignatureEncoded($reinit = false)
216
+ {
217
+ if (!isset($this->_signatureEncoded) || $reinit) {
218
+ $this->_signatureEncoded = rawurlencode(
219
+ base64_encode($this->getSignature($reinit))
220
+ );
221
+ }
222
+
223
+ return $this->_signatureEncoded;
224
+ }
225
+
226
+ /**
227
+ * Initiate the http client
228
+ *
229
+ * @param boolean $reinit regenerate signature
230
+ *
231
+ * @return void
232
+ */
233
+ protected function init($reinit = false)
234
+ {
235
+ $headers = array(
236
+ 'Accept' => 'application/json',
237
+ 'apikey' => $this->getConfigHelper()->getApiKey(),
238
+ 'apisecret' => $this->getConfigHelper()->getApiSecret(),
239
+ 'Content-Type' => 'application/json',
240
+ // INFO this will regenerate salt if needed
241
+ 'signature' => $this->getSignatureEncoded($reinit),
242
+ 'salt' => $this->getSalt(),
243
+ );
244
+
245
+ $this->getHttpClient()->setHeaders($headers);
246
+
247
+ $this->log(
248
+ 'budgetmailer/client::init() re: ' . ($reinit ? 'yes' : 'no')
249
+ . ' headers: ' . json_encode($headers)
250
+ );
251
+ }
252
+
253
+ /**
254
+ * Check if memory cache is not too large
255
+ */
256
+ protected function limitCache()
257
+ {
258
+ if (count($this->_contacts) > self::LIMIT) {
259
+ $this->_contacts = array();
260
+ }
261
+ }
262
+
263
+ /**
264
+ * Custom log wrapper - log only if developer mode
265
+ *
266
+ * @param string $message
267
+ */
268
+ protected function log($message)
269
+ {
270
+ if (Mage::getIsDeveloperMode()) {
271
+ Mage::log($message);
272
+ }
273
+ }
274
+
275
+ /**
276
+ * Set cache
277
+ *
278
+ * @param mixed $data
279
+ * @param string $id
280
+ *
281
+ * @return Professio_BudgetMailer_Model_Client
282
+ */
283
+ protected function setCache($data, $id = null)
284
+ {
285
+ if (!$this->isCachingEnabled()) {
286
+ return $this;
287
+ }
288
+
289
+ if (!$id) {
290
+ $id = $data && $data->id ? $data->id : null;
291
+ }
292
+
293
+ if ($id) {
294
+ $this->limitCache();
295
+
296
+ /*if (is_string($data)) {
297
+ throw new Exception('set cache failed.');
298
+ }*/
299
+
300
+ $this->_contacts[$id] = $data;
301
+ }
302
+
303
+ return $this;
304
+ }
305
+
306
+ /**
307
+ * Delete contact API call
308
+ *
309
+ * @param string $id email or budgetmailer id
310
+ * @param null|string $list list name or id or null for default
311
+ *
312
+ * @return boolean
313
+ * @throws Professio_BudgetMailer_Exception
314
+ */
315
+ public function deleteContact($id, $list = null)
316
+ {
317
+ $this->init(true);
318
+
319
+ if (is_null($list)) {
320
+ $list = $this->getList();
321
+ }
322
+
323
+ $path = '/contacts/' . rawurlencode($list) . '/' . rawurlencode($id);
324
+
325
+ $this->log(
326
+ 'budgetmailer/client::deleteContact() path: '
327
+ . $path . ', list: ' . $list . ', id: ' . $id
328
+ );
329
+
330
+ $rs = $this->restDelete($path);
331
+
332
+ $this->log(
333
+ 'budgetmailer/client::deleteContact() result: '
334
+ . $rs->getStatus() . ', body: ' . $rs->getBody()
335
+ );
336
+
337
+ if ($rs->isError()) {
338
+ throw new Professio_BudgetMailer_Exception(
339
+ Mage::helper('budgetmailer')
340
+ ->__('Couldn\'t delete contact from BudgetMailer API.')
341
+ );
342
+ } else {
343
+ if (isset($this->_contacts[$id])) {
344
+ unset($this->_contacts[$id]);
345
+ }
346
+ }
347
+
348
+ return true;
349
+ }
350
+
351
+ /**
352
+ * Delete tag from contact API call
353
+ *
354
+ * @param string $id email or budgetmailer id
355
+ * @param string $tag tag
356
+ * @param null|string $list list name or id or null for default
357
+ *
358
+ * @return boolean
359
+ * @throws Professio_BudgetMailer_Exception
360
+ */
361
+ public function deleteTag($id, $tag, $list = null)
362
+ {
363
+ $this->init(true);
364
+
365
+ if (is_null($list)) {
366
+ $list = $this->getList();
367
+ }
368
+
369
+ $path = '/contacts/' . rawurlencode($list) . '/'
370
+ . rawurlencode($id) . '/tags/' . rawurlencode($tag);
371
+
372
+ $this->log(
373
+ 'budgetmailer/client::deleteTag() path: '
374
+ . $path . ', list: ' . $list . ', id: ' . $id
375
+ );
376
+
377
+ $rs = $this->restDelete($path);
378
+
379
+ $this->log(
380
+ 'budgetmailer/client::deleteTag() result: '
381
+ . $rs->getStatus() . ', body: ' . $rs->getBody()
382
+ );
383
+
384
+ if ($rs->isError()) {
385
+ throw new Professio_BudgetMailer_Exception(
386
+ Mage::helper('budgetmailer')
387
+ ->__('Couldn\'t delete tag from contact from BudgetMailer API.')
388
+ );
389
+ }
390
+
391
+ return true;
392
+ }
393
+
394
+ /**
395
+ * Get single contact from API
396
+ *
397
+ * @param string $id email or budgetmailer id
398
+ * @param null|string $list list name or id or null for default
399
+ *
400
+ * @return boolean|Professio_BudgetMailer_Model_Contact
401
+ * @throws Professio_BudgetMailer_Exception
402
+ */
403
+ public function getContact($id, $list = null)
404
+ {
405
+ $this->init(true);
406
+
407
+ /*try {
408
+ throw new Exception('trace');
409
+ } catch (Exception $e) {
410
+ Mage::logException($e);
411
+ }*/
412
+
413
+ $contactCache = $this->getCache($id);
414
+
415
+ if (!is_null($contactCache)) {
416
+ $this->log(
417
+ 'budgetmailer/client::getContact() cache hit, result: '
418
+ . json_encode($contactCache)
419
+ );
420
+
421
+ return $contactCache;
422
+ }
423
+
424
+ if (is_null($list)) {
425
+ $list = $this->getList();
426
+ }
427
+
428
+ $path = '/contacts/' . rawurlencode($list) . '/' . rawurlencode($id);
429
+
430
+ $this->log(
431
+ 'budgetmailer/client::getContact() path: '
432
+ . $path . ', list: ' . $list . ', id: ' . $id
433
+ );
434
+
435
+ $rs = $this->restGet($path);
436
+
437
+ $this->log(
438
+ 'budgetmailer/client::getContact() result: '
439
+ . $rs->getStatus() . ', body: ' . $rs->getBody()
440
+ );
441
+
442
+ if ($rs->isError()) {
443
+ if (404 == $rs->getStatus()) {
444
+ $contact = false;
445
+ } else {
446
+ throw new Professio_BudgetMailer_Exception(
447
+ Mage::helper('budgetmailer')
448
+ ->__('Couldn\'t get the contact from BudgetMailer API.')
449
+ );
450
+ }
451
+ } else {
452
+ $contact = json_decode($rs->getBody());
453
+ }
454
+
455
+ $this->setCache($contact, $id);
456
+
457
+ return $contact;
458
+ }
459
+
460
+ /**
461
+ * Get multiple contacts from API
462
+ *
463
+ * @param integer $offset
464
+ * @param integer $limit
465
+ * @param string $sort
466
+ * @param boolean $unsubscribed
467
+ * @param null|string $list list name or id or null for default
468
+ *
469
+ * @return boolean|array
470
+ * @throws Professio_BudgetMailer_Exception
471
+ */
472
+ public function getContacts(
473
+ $offset = 0,
474
+ $limit = 20,
475
+ $sort = 'ASC',
476
+ $unsubscribed = 'False',
477
+ $list = null
478
+ )
479
+ {
480
+ $this->init(true);
481
+
482
+ if (is_null($list)) {
483
+ $list = $this->getList();
484
+ }
485
+
486
+ $path = '/contacts/' . rawurlencode($list) . '/';
487
+
488
+ $query = array(
489
+ 'offset' => $offset,
490
+ 'limit' => $limit,
491
+ 'sort' => $sort,
492
+ 'unsubscribed' => $unsubscribed
493
+ );
494
+
495
+ foreach ($query as $k => $v) {
496
+ if (is_null($v)) {
497
+ unset($query[$k]);
498
+ }
499
+ }
500
+
501
+ $this->log(
502
+ 'budgetmailer/client::getContacts() path: '
503
+ . $path . ', list: ' . $list . ', offset: ' . $offset
504
+ . ', limit: ' . $limit . ', sort: ' . $sort . ', unsubscribed: '
505
+ . ($unsubscribed ? 'yes' : 'no')
506
+ );
507
+
508
+ $rs = $this->restGet($path, $query);
509
+
510
+ $this->log(
511
+ 'budgetmailer/client::getContacts() result: ' . $rs->getStatus()
512
+ . ', headers: ' . json_encode($rs->getHeaders())
513
+ . ', body: ' . $rs->getBody()
514
+ );
515
+
516
+ $this->_contactsCount = $rs->getHeader('X-total-count');
517
+
518
+ if ($rs->isError()) {
519
+ throw new Professio_BudgetMailer_Exception(
520
+ Mage::helper('budgetmailer')
521
+ ->__('Couldn\'t get contacts from BudgetMailer API.')
522
+ );
523
+ }
524
+
525
+ $contacts = json_decode($rs->getBody());
526
+
527
+ return $contacts;
528
+ }
529
+
530
+ /**
531
+ * Get total contacts count
532
+ *
533
+ * @return integer
534
+ */
535
+ public function getContactsCount()
536
+ {
537
+ if (!isset($this->_contactsCount)) {
538
+ $this->getContacts(0, 1);
539
+ }
540
+
541
+ return $this->_contactsCount;
542
+ }
543
+
544
+ /**
545
+ * Get budgetmailer lists API call
546
+ *
547
+ * @return boolean|array
548
+ * @throws Professio_BudgetMailer_Exception
549
+ */
550
+ public function getLists()
551
+ {
552
+ $this->init(true);
553
+
554
+ $path = '/lists';
555
+
556
+ $this->log('budgetmailer/client::getLists() path: ' . $path);
557
+
558
+ $rs = $this->restGet($path);
559
+
560
+ $this->log(
561
+ 'budgetmailer/client::getLists() result: '
562
+ . $rs->getStatus() . ', body: ' . $rs->getBody()
563
+ );
564
+
565
+ if ($rs->isError()) {
566
+ throw new Professio_BudgetMailer_Exception(
567
+ Mage::helper('budgetmailer')
568
+ ->__('Couldn\'t get the lists from BudgetMailer API.')
569
+ );
570
+ } else {
571
+ $lists = json_decode($rs->getBody());
572
+ }
573
+
574
+ return $lists;
575
+ }
576
+
577
+ /**
578
+ * Get tags for contact API call
579
+ *
580
+ * @param string $id email or budgetmailer id
581
+ * @param null|string $list list name, id, or null for default
582
+ *
583
+ * @return boolean|array
584
+ * @throws Professio_BudgetMailer_Exception
585
+ */
586
+ public function getTags($id, $list = null)
587
+ {
588
+ $this->init(true);
589
+
590
+ if (is_null($list)) {
591
+ $list = $this->getList();
592
+ }
593
+
594
+ $path = '/contacts/' . rawurlencode($list)
595
+ . '/' . rawurlencode($id) . '/tags';
596
+
597
+ $this->log(
598
+ 'budgetmailer/client::getTags() path: '
599
+ . $path . ', list: ' . $list . ', id: ' . $id
600
+ );
601
+
602
+ $rs = $this->restGet($path);
603
+
604
+ $this->log(
605
+ 'budgetmailer/client::getTags() result: '
606
+ . $rs->getStatus() . ', body: ' . $rs->getBody()
607
+ );
608
+
609
+ if ($rs->isError()) {
610
+ throw new Professio_BudgetMailer_Exception(
611
+ Mage::helper('budgetmailer')
612
+ ->__('Couldn\'t get tags from BudgetMailer API.')
613
+ );
614
+ }
615
+
616
+ return json_decode($rs->getBody());
617
+ }
618
+
619
+ /**
620
+ * Insert new contact ot API
621
+ *
622
+ * @param object $contact contact to save
623
+ * @param null|string $list list name, id, or null for default
624
+ *
625
+ * @return boolean|object false or returned record from API
626
+ * @throws Professio_BudgetMailer_Exception
627
+ */
628
+ public function postContact($contact, $list = null)
629
+ {
630
+ $this->init(true);
631
+
632
+ if (is_null($list)) {
633
+ $list = $this->getList();
634
+ }
635
+
636
+ $path = '/contacts/' . rawurlencode($list);
637
+ $data = json_encode($contact);
638
+
639
+ $this->log(
640
+ 'budgetmailer/client::postContact() path: '
641
+ . $path . ', list: ' . $list . ', contact: ' . $data
642
+ );
643
+
644
+ $rs = $this->restPost($path, $data);
645
+
646
+ $this->log(
647
+ 'budgetmailer/client::postContact() result: '
648
+ . $rs->getStatus() . ', body: ' . $rs->getBody()
649
+ );
650
+
651
+ if ($rs->isError()) {
652
+ throw new Professio_BudgetMailer_Exception(
653
+ Mage::helper('budgetmailer')
654
+ ->__('Couldn\'t subscribe contact to BudgetMailer API.')
655
+ );
656
+ }
657
+
658
+ $contact = json_decode($rs->getBody());
659
+
660
+ if ($contact) {
661
+ $this->setCache($contact);
662
+ }
663
+
664
+ return $contact;
665
+ }
666
+
667
+ /**
668
+ * Insert multiple contacts to API
669
+ *
670
+ * @param array $contacts array of contact objects
671
+ * @param null|string $list list name, id, or null for default
672
+ *
673
+ * @return boolean
674
+ * @throws Professio_BudgetMailer_Exception
675
+ */
676
+ public function postContacts($contacts, $list = null)
677
+ {
678
+ $this->init(true);
679
+
680
+ if (is_null($list)) {
681
+ $list = $this->getList();
682
+ }
683
+
684
+ $path = '/contacts/' . rawurlencode($list) . '/bulk';
685
+ $data = json_encode($contacts);
686
+
687
+ $this->log(
688
+ 'budgetmailer/client::postContacts() path: '
689
+ . $path . ', list: ' . $list . ', contact: ' . $data
690
+ );
691
+
692
+ $rs = $this->restPost($path, $data);
693
+
694
+ $this->log(
695
+ 'budgetmailer/client::postContacts() result: ' . $rs->getStatus()
696
+ . ', headers: ' . json_encode($rs->getHeaders())
697
+ . ', body: ' . $rs->getBody()
698
+ );
699
+
700
+ $this->_totalCount = $rs->getHeader('X-Total-Count');
701
+ $this->_totalFail = $rs->getHeader('X-Total-Fail');
702
+ $this->_totalSuccess = $rs->getHeader('X-Total-Success');
703
+
704
+ if ($rs->isError()) {
705
+ throw new Professio_BudgetMailer_Exception(
706
+ Mage::helper('budgetmailer')
707
+ ->__('Couldn\'t bulk subscribe contacts to BudgetMailer API.')
708
+ );
709
+ }
710
+
711
+ return array(
712
+ $this->_totalCount, $this->_totalFail, $this->_totalSuccess,
713
+ json_decode($rs->getBody())
714
+ );
715
+ }
716
+
717
+ /**
718
+ * Add tags to contact
719
+ *
720
+ * @param string $id email or budgetamiler id
721
+ * @param array $tags tags
722
+ * @param null|string $list list name, id, or null for default
723
+ *
724
+ * @return boolean
725
+ * @throws Professio_BudgetMailer_Exception
726
+ */
727
+ public function postTags($id, $tags, $list = null)
728
+ {
729
+ $this->init(true);
730
+
731
+ if (is_null($list)) {
732
+ $list = $this->getList();
733
+ }
734
+
735
+ $path = '/contacts/' . rawurlencode($list) . '/'
736
+ . rawurlencode($id) . '/tags';
737
+ $data = json_encode($tags);
738
+
739
+ $this->log(
740
+ 'budgetmailer/client::postTags() path: '
741
+ . $path . ', list: ' . $list . ', id: ' . $id . ', tags: ' . $data
742
+ );
743
+
744
+ $rs = $this->restPost($path, $data);
745
+
746
+ $this->log(
747
+ 'budgetmailer/client::postTags() result: '
748
+ . $rs->getStatus() . ', body: ' . $rs->getBody()
749
+ );
750
+
751
+ if ($rs->isError()) {
752
+ throw new Professio_BudgetMailer_Exception(
753
+ Mage::helper('budgetmailer')
754
+ ->__('Couldn\'t post tags to BudgetMailer API.')
755
+ );
756
+ }
757
+
758
+ return true;
759
+ }
760
+
761
+ /**
762
+ * Update contact in API
763
+ *
764
+ * @param string $id email or budgetamiler id
765
+ * @param object $contact contact object
766
+ * @param null|string $list list name, id, or null for default
767
+ *
768
+ * @return boolean|string
769
+ * @throws Professio_BudgetMailer_Exception
770
+ */
771
+ public function putContact($id, $contact, $list = null)
772
+ {
773
+ $this->init(true);
774
+
775
+ if (is_null($list)) {
776
+ $list = $this->getList();
777
+ }
778
+
779
+ $path = '/contacts/' . rawurlencode($list) . '/' . rawurlencode($id);
780
+ $data = json_encode($contact);
781
+
782
+ $this->log(
783
+ 'budgetmailer/client::putContact() path: '
784
+ . $path . ', list: ' . $list . ', id: ' . $id . ', contact: '
785
+ . $data
786
+ );
787
+
788
+ $rs = $this->restPut($path, $data);
789
+
790
+ $this->log(
791
+ 'budgetmailer/client::putContact() result: '
792
+ . $rs->getStatus() . ', body: ' . $rs->getBody()
793
+ );
794
+
795
+ if ($rs->isError()) {
796
+ throw new Professio_BudgetMailer_Exception(
797
+ Mage::helper('budgetmailer')
798
+ ->__('Couldn\'t update contact in BudgetMailer API.')
799
+ );
800
+ } else {
801
+ $this->setCache(json_decode($data), $id);
802
+ }
803
+
804
+ return json_decode($rs->getBody());
805
+ }
806
+
807
+ /**
808
+ * Test current API credentials
809
+ * @return boolean
810
+ */
811
+ public function testApiCredentials()
812
+ {
813
+ try {
814
+ return $this->getLists();
815
+ } catch (Exception $e) {
816
+ Mage::logException($e);
817
+
818
+ $this->log(
819
+ 'budgetmailer/client::testApiCredentials() failed '
820
+ . 'with exception: ' . $e->getMessage()
821
+ );
822
+
823
+ return false;
824
+ }
825
+ }
826
+ }
app/code/community/Professio/BudgetMailer/Model/Config/Source/Address.php ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Professio_BudgetMailer extension
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the MIT License
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/mit-license.php
11
+ *
12
+ * @category Professio
13
+ * @package Professio_BudgetMailer
14
+ * @copyright Copyright (c) 2015
15
+ * @license http://opensource.org/licenses/mit-license.php MIT License
16
+ */
17
+
18
+ /**
19
+ * Address type config source
20
+ *
21
+ * @category Professio
22
+ * @package Professio_BudgetMailer
23
+ */
24
+ class Professio_BudgetMailer_Model_Config_Source_Address
25
+ {
26
+ const BILLING = 'billing';
27
+ const SHIPPING = 'shipping';
28
+
29
+ /**
30
+ * Options getter
31
+ *
32
+ * @return array
33
+ */
34
+ public function toOptionArray()
35
+ {
36
+ return array(
37
+ array(
38
+ 'label' => Mage::helper('budgetmailer')->__('Billing Address'),
39
+ 'value' => self::BILLING
40
+ ),
41
+ array(
42
+ 'label' => Mage::helper('budgetmailer')->__('Shipping Address'),
43
+ 'value' => self::SHIPPING
44
+ ),
45
+ );
46
+ }
47
+
48
+ /**
49
+ * Get options in "key-value" format
50
+ *
51
+ * @return array
52
+ */
53
+ public function toArray()
54
+ {
55
+ return array(
56
+ self::ON_DELETE_DELEBILLINGTE =>
57
+ Mage::helper('budgetmailer')->__('Billing Address'),
58
+ self::SHIPPING =>
59
+ Mage::helper('budgetmailer')->__('Shipping Address'),
60
+ );
61
+ }
62
+ }
app/code/community/Professio/BudgetMailer/Model/Config/Source/Delete.php ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Professio_BudgetMailer extension
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the MIT License
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/mit-license.php
11
+ *
12
+ * @category Professio
13
+ * @package Professio_BudgetMailer
14
+ * @copyright Copyright (c) 2015
15
+ * @license http://opensource.org/licenses/mit-license.php MIT License
16
+ */
17
+
18
+ /**
19
+ * Delete entity config source
20
+ *
21
+ * @category Professio
22
+ * @package Professio_BudgetMailer
23
+ */
24
+ class Professio_BudgetMailer_Model_Config_Source_Delete
25
+ {
26
+ const ON_DELETE_DELETE = 'delete';
27
+ const ON_DELETE_IGNORE = 'ignore';
28
+
29
+ /**
30
+ * Options getter
31
+ *
32
+ * @return array
33
+ */
34
+ public function toOptionArray()
35
+ {
36
+ return array(
37
+ array(
38
+ 'label' => Mage::helper('budgetmailer')->__('Delete Contact'),
39
+ 'value' => self::ON_DELETE_DELETE
40
+ ),
41
+ array(
42
+ 'label' => Mage::helper('budgetmailer')->__('Do nothing'),
43
+ 'value' => self::ON_DELETE_IGNORE
44
+ ),
45
+ );
46
+ }
47
+
48
+ /**
49
+ * Get options in "key-value" format
50
+ *
51
+ * @return array
52
+ */
53
+ public function toArray()
54
+ {
55
+ return array(
56
+ self::ON_DELETE_DELETE =>
57
+ Mage::helper('budgetmailer')->__('Delete Contact'),
58
+ self::ON_DELETE_IGNORE =>
59
+ Mage::helper('budgetmailer')->__('Do nothing'),
60
+ );
61
+ }
62
+ }
app/code/community/Professio/BudgetMailer/Model/Config/Source/Delete/Address.php ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Professio_BudgetMailer extension
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the MIT License
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/mit-license.php
11
+ *
12
+ * @category Professio
13
+ * @package Professio_BudgetMailer
14
+ * @copyright Copyright (c) 2015
15
+ * @license http://opensource.org/licenses/mit-license.php MIT License
16
+ */
17
+
18
+ /**
19
+ * Delete address config source
20
+ *
21
+ * @category Professio
22
+ * @package Professio_BudgetMailer
23
+ */
24
+ class Professio_BudgetMailer_Model_Config_Source_Delete_Address
25
+ {
26
+ const ON_DELETE_USE_NEW = 'use_new';
27
+ const ON_DELETE_IGNORE = 'ignore';
28
+
29
+ /**
30
+ * Options getter
31
+ *
32
+ * @return array
33
+ */
34
+ public function toOptionArray()
35
+ {
36
+ return array(
37
+ array(
38
+ 'label' => Mage::helper('budgetmailer')
39
+ ->__('Use new primary address'),
40
+ 'value' => self::ON_DELETE_USE_NEW
41
+ ),
42
+ array(
43
+ 'label' => Mage::helper('budgetmailer')->__('Do nothing'),
44
+ 'value' => self::ON_DELETE_IGNORE
45
+ ),
46
+ );
47
+ }
48
+
49
+ /**
50
+ * Get options in "key-value" format
51
+ *
52
+ * @return array
53
+ */
54
+ public function toArray()
55
+ {
56
+ return array(
57
+ self::ON_DELETE_USE_NEW => Mage::helper('budgetmailer')
58
+ ->__('Use new primary address'),
59
+ self::ON_DELETE_IGNORE => Mage::helper('budgetmailer')
60
+ ->__('Do nothing'),
61
+ );
62
+ }
63
+ }
app/code/community/Professio/BudgetMailer/Model/Config/Source/List.php ADDED
@@ -0,0 +1,102 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Professio_BudgetMailer extension
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the MIT License
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/mit-license.php
11
+ *
12
+ * @category Professio
13
+ * @package Professio_BudgetMailer
14
+ * @copyright Copyright (c) 2015
15
+ * @license http://opensource.org/licenses/mit-license.php MIT License
16
+ */
17
+
18
+ /**
19
+ * Select list config source
20
+ *
21
+ * @category Professio
22
+ * @package Professio_BudgetMailer
23
+ */
24
+ class Professio_BudgetMailer_Model_Config_Source_List
25
+ {
26
+ protected $_lists;
27
+
28
+ public function getLists()
29
+ {
30
+ if (!isset($this->_lists)) {
31
+ try {
32
+ $collection = Mage::getModel('budgetmailer/list')
33
+ ->getCollection();
34
+
35
+ if ($collection) {
36
+ $collection->load();
37
+ $this->_lists = $collection->getIterator();
38
+ } else {
39
+ $this->_lists = array();
40
+ }
41
+ } catch (Exception $e) {
42
+ $this->_lists = array();
43
+ Mage::logException($e);
44
+ Mage::log(
45
+ 'budgetmailer/config_source_list::getLists() failed '
46
+ . ' with exception: ' . $e->getMessage()
47
+ );
48
+ }
49
+ }
50
+
51
+ return $this->_lists;
52
+ }
53
+
54
+ /**
55
+ * Options getter
56
+ *
57
+ * @return array
58
+ */
59
+ public function toOptionArray()
60
+ {
61
+ static $options;
62
+
63
+ if (!isset($options)) {
64
+ $options = array();
65
+
66
+ $options[] = array(
67
+ 'value' => 0,
68
+ 'label' => Mage::helper('budgetmailer')->__('Select List')
69
+ );
70
+
71
+ foreach ($this->getLists() as $list) {
72
+ $options[] = array(
73
+ 'value' => $list->getBudgetmailerId(),
74
+ 'label' => $list->getName()
75
+ );
76
+ }
77
+ }
78
+
79
+ return $options;
80
+ }
81
+
82
+ /**
83
+ * Get options in "key-value" format
84
+ *
85
+ * @return array
86
+ */
87
+ public function toArray()
88
+ {
89
+ static $array;
90
+
91
+ if (!isset($array)) {
92
+ $array = array();
93
+ $array[0] = Mage::helper('budgetmailer')->__('Select List');
94
+
95
+ foreach ($this->getLists() as $list) {
96
+ $array[$list->getId()] = $list->getName();
97
+ }
98
+ }
99
+
100
+ return $array;
101
+ }
102
+ }
app/code/community/Professio/BudgetMailer/Model/Config/Source/Update.php ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Professio_BudgetMailer extension
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the MIT License
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/mit-license.php
11
+ *
12
+ * @category Professio
13
+ * @package Professio_BudgetMailer
14
+ * @copyright Copyright (c) 2015
15
+ * @license http://opensource.org/licenses/mit-license.php MIT License
16
+ */
17
+
18
+ /**
19
+ * Update entity config source
20
+ *
21
+ * @category Professio
22
+ * @package Professio_BudgetMailer
23
+ */
24
+ class Professio_BudgetMailer_Model_Config_Source_Update
25
+ {
26
+ const ON_UPDATE_UPDATE = 'update';
27
+ const ON_UPDATE_IGNORE = 'ignore';
28
+
29
+ /**
30
+ * Options getter
31
+ *
32
+ * @return array
33
+ */
34
+ public function toOptionArray()
35
+ {
36
+ return array(
37
+ array(
38
+ 'label' => Mage::helper('budgetmailer')->__('Update Contact'),
39
+ 'value' => self::ON_UPDATE_UPDATE
40
+ ),
41
+ array(
42
+ 'label' => Mage::helper('budgetmailer')->__('Do nothing'),
43
+ 'value' => self::ON_UPDATE_IGNORE
44
+ ),
45
+ );
46
+ }
47
+
48
+ /**
49
+ * Get options in "key-value" format
50
+ *
51
+ * @return array
52
+ */
53
+ public function toArray()
54
+ {
55
+ return array(
56
+ self::ON_UPDATE_UPDATE =>
57
+ Mage::helper('budgetmailer')->__('Update Contact'),
58
+ self::ON_UPDATE_IGNORE =>
59
+ Mage::helper('budgetmailer')->__('Do nothing'),
60
+ );
61
+ }
62
+ }
app/code/community/Professio/BudgetMailer/Model/Contact.php ADDED
@@ -0,0 +1,558 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Professio_BudgetMailer extension
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the MIT License
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/mit-license.php
11
+ *
12
+ * @category Professio
13
+ * @package Professio_BudgetMailer
14
+ * @copyright Copyright (c) 2015
15
+ * @license http://opensource.org/licenses/mit-license.php MIT License
16
+ */
17
+
18
+ /**
19
+ * Contact model
20
+ *
21
+ * @category Professio
22
+ * @package Professio_BudgetMailer
23
+ */
24
+ class Professio_BudgetMailer_Model_Contact extends Mage_Core_Model_Abstract
25
+ {
26
+ /**
27
+ * Entity code.
28
+ * Can be used as part of method name for entity processing
29
+ */
30
+ const ENTITY = 'budgetmailer_contact';
31
+ const CACHE_TAG = 'budgetmailer_contact';
32
+
33
+ /**
34
+ * Prefix of model events names
35
+ * @var string
36
+ */
37
+ protected $_eventPrefix = 'budgetmailer_contact';
38
+
39
+ /**
40
+ * Parameter name in event
41
+ * @var string
42
+ */
43
+ protected $_eventObject = 'contact';
44
+
45
+ /**
46
+ * Current customer
47
+ * @var Mage_Customer_Model_Customer
48
+ */
49
+ protected $_customer;
50
+
51
+ /**
52
+ * Constructor
53
+ */
54
+ public function _construct()
55
+ {
56
+ parent::_construct();
57
+
58
+ $this->_init('budgetmailer/contact');
59
+ }
60
+
61
+ /**
62
+ * Before save contact
63
+ *
64
+ * @return Professio_BudgetMailer_Model_Contact
65
+ */
66
+ protected function _beforeSave()
67
+ {
68
+ parent::_beforeSave();
69
+
70
+ $now = Mage::getSingleton('core/date')->gmtDate();
71
+
72
+ if ($this->isObjectNew()) {
73
+ $this->setCreatedAt($now);
74
+ }
75
+
76
+ $this->setUpdatedAt($now);
77
+
78
+ if (!is_array($this->getTags())) {
79
+ $this->setTags(array());
80
+ }
81
+
82
+ $this->setTags(json_encode($this->getTags()));
83
+
84
+ return $this;
85
+ }
86
+
87
+ /**
88
+ * After load - decode json encoded tags to array
89
+ *
90
+ * @return Professio_BudgetMailer_Model_Contact
91
+ */
92
+ protected function _afterLoad()
93
+ {
94
+ parent::_afterLoad();
95
+
96
+ $this->setTags($this->decodeTags($this->getTags()));
97
+
98
+ return $this;
99
+ }
100
+
101
+ /**
102
+ * Retrieve parent
103
+ *
104
+ * @return null|Professio_BudgetMailer_Model_List
105
+ */
106
+ public function getParentList()
107
+ {
108
+ if (!$this->hasData('_parent_list')) {
109
+ if (!$this->getListId()) {
110
+ return null;
111
+ } else {
112
+ $list = Mage::getModel('budgetmailer/list')
113
+ ->load($this->getListId());
114
+
115
+ if ($list->getId()) {
116
+ $this->setData('_parent_list', $list);
117
+ } else {
118
+ $this->setData('_parent_list', null);
119
+ }
120
+ }
121
+ }
122
+
123
+ return $this->getData('_parent_list');
124
+ }
125
+
126
+ /**
127
+ * Get default values
128
+ *
129
+ * @return array
130
+ */
131
+ public function getDefaultValues()
132
+ {
133
+ $values = array();
134
+ $values['status'] = 1;
135
+
136
+ return $values;
137
+ }
138
+
139
+ /**
140
+ * Delete override, allows delete contact from API
141
+ *
142
+ * @param boolean $fromApi true = delete from API , false = do nothing
143
+ *
144
+ * @return Professio_BudgetMailer_Model_Contact
145
+ */
146
+ public function delete($fromApi = true)
147
+ {
148
+ // INFO ensure we have loaded email...
149
+ if (empty($this->getEmail())) {
150
+ $this->load($this->getEntityId());
151
+ }
152
+
153
+ // INFO deleting local before API
154
+ parent::delete();
155
+
156
+ if ($fromApi) {
157
+ $this->deleteFromApi();
158
+ }
159
+
160
+ return $this;
161
+ }
162
+
163
+ /**
164
+ * Load method override ... automatically loading record from API
165
+ * if doesn't exist locally
166
+ *
167
+ * @param integer $id
168
+ * @param mixed $field
169
+ */
170
+ public function load($id, $field = null)
171
+ {
172
+ Mage::log('budgetmailer/contact::load()');
173
+
174
+ parent::load($id, $field);
175
+
176
+ Mage::log(
177
+ 'budgetmailer/contact::load() orig data: '
178
+ . json_encode($this->getOrigData())
179
+ );
180
+
181
+ if (is_null($this->getOrigData())) {
182
+ Mage::log('budgetmailer/contact::load() setting original data.');
183
+ $this->setOrigData();
184
+ }
185
+
186
+ $ttl = time() - Mage::helper('budgetmailer/config')->getSyncTtl();
187
+ $updatedAt = strtotime($this->getData('updated_at'));
188
+
189
+ if (!$updatedAt || $updatedAt < $ttl) {
190
+ $this->loadFromApi();
191
+
192
+ Mage::log(
193
+ 'budgetmailer/contact::load() after load from api: '
194
+ . json_encode($this->getData())
195
+ );
196
+
197
+ if ($this->getBudgetmailerId()) {
198
+ $this->save(false);
199
+ }
200
+ } else {
201
+ Mage::log('budgetmailer/contact::load() no api load');
202
+ }
203
+ }
204
+
205
+ /**
206
+ * Save method override - allows save contact to API
207
+ *
208
+ * @param boolean $useApi true = save to API, false = don't save to API
209
+ */
210
+ public function save($useApi = true)
211
+ {
212
+ $changed = false;
213
+
214
+ if (!is_null($this->getOrigData())) {
215
+ foreach ($this->getData() as $k => $v) {
216
+ $changed = $this->dataHasChangedFor($k);
217
+
218
+ // decode orig value of tags field
219
+ if ($changed && 'tags' == $k) {
220
+ $ov = $this->getOrigData($k);
221
+ $ov = $this->decodeTags($ov);
222
+ $v = $this->decodeTags($v);
223
+
224
+ $changed = $ov != $v;
225
+ }
226
+
227
+ if ($changed) {
228
+ Mage::log(
229
+ 'budgetmailer/contact::save() changed key: '
230
+ . $k . ' orig: ' . json_encode($this->getOrigData($k))
231
+ . ', now: ' . json_encode($v)
232
+ );
233
+ break;
234
+ }
235
+ }
236
+ } else {
237
+ Mage::log('budgetmailer/contact::save() no orig data...');
238
+ // INFO i guess it's new then?!?!?!
239
+ $changed = true;
240
+ }
241
+
242
+ Mage::log(
243
+ 'budgetmailer/contact::save() changed: '
244
+ . ($changed ? 'yes' : 'no') . ', with data: '
245
+ . json_encode($this->getData())
246
+ );
247
+
248
+ if ($useApi && $changed) {
249
+ $this->saveToApi();
250
+ }
251
+
252
+ parent::save();
253
+ }
254
+
255
+ /**
256
+ * Get BudgetMailer client
257
+ *
258
+ * @return Professio_BudgetMailer_Model_Client
259
+ */
260
+ protected function getClient()
261
+ {
262
+ return Mage::getSingleton('budgetmailer/client');
263
+ }
264
+
265
+ /**
266
+ * Get customer for this contact
267
+ *
268
+ * @return Mage_Customer_Model_Customer
269
+ */
270
+ protected function getCustomer()
271
+ {
272
+ if (!isset($this->_customer)) {
273
+ if (isset($this->_data['customer_id'])) {
274
+ $this->_customer = Mage::getModel('customer/customer')
275
+ ->load($this->_data['customer_id']);
276
+ } elseif (isset($this->_data['email'])) {
277
+ $this->_customer = Mage::getModel('customer/customer')
278
+ ->loadByEmail($this->_data['email']);
279
+ }
280
+ }
281
+
282
+ return $this->_customer;
283
+ }
284
+
285
+ /**
286
+ * Get mapper
287
+ *
288
+ * @return Professio_BudgetMailer_Helper_Mapper
289
+ */
290
+ protected function getMapper()
291
+ {
292
+ return Mage::helper('budgetmailer/mapper');
293
+ }
294
+
295
+ /**
296
+ * Load contact by budgetmailer id
297
+ *
298
+ * @param string $budgetmailerId
299
+ * @param boolean $useApi true = try to load from API
300
+ *
301
+ * @return Professio_BudgetMailer_Model_Contact
302
+ */
303
+ public function loadByBudgetMailerId($budgetmailerId, $useApi = true)
304
+ {
305
+ $this->_getResource()->loadByBudgetMailerId($this, $budgetmailerId);
306
+
307
+ $ttl = time() - Mage::helper('budgetmailer/config')->getSyncTtl();
308
+
309
+ // not found try to load from API
310
+ if ($useApi && (
311
+ !$this->getData('updated_at')
312
+ || $this->getData('updated_at') < $ttl
313
+ )
314
+ ) {
315
+ $this->setBudgetmailerId($budgetmailerId);
316
+ $this->load($this->getId());
317
+ }
318
+
319
+ return $this;
320
+ }
321
+
322
+ /**
323
+ * Load by customer
324
+ *
325
+ * @param Mage_Customer_Model_Customer $customer
326
+ * @param boolean $useApi true = try to load from API
327
+ *
328
+ * @return Professio_BudgetMailer_Model_Contact
329
+ */
330
+ public function loadByCustomer($customer, $useApi = true)
331
+ {
332
+ if (!$customer->getEntityId()) {
333
+ $this->setData(array());
334
+
335
+ return $this;
336
+ }
337
+
338
+ $this->_getResource()->loadByCustomer($this, $customer);
339
+
340
+ $ttl = time() - Mage::helper('budgetmailer/config')->getSyncTtl();
341
+
342
+ // not found try to load from API
343
+ if ($useApi && (
344
+ !$this->getData('updated_at')
345
+ || $this->getData('updated_at') < $ttl
346
+ )
347
+ ) {
348
+ $this->setEmail($customer->getEmail());
349
+ $this->setCustomerId($customer->getId());
350
+ $this->load($this->getId());
351
+ }
352
+
353
+ return $this;
354
+ }
355
+
356
+ /**
357
+ * Load by email
358
+ * @param string $email
359
+ * @param boolean $useApi true = try to load from API
360
+ * @return Professio_BudgetMailer_Model_Contact
361
+ */
362
+ public function loadByEmail($email, $useApi = true)
363
+ {
364
+ $this->_getResource()->loadByEmail($this, $email);
365
+
366
+ $ttl = time() - Mage::helper('budgetmailer/config')->getSyncTtl();
367
+
368
+ // not found try to load from API
369
+ if ($useApi && (
370
+ !$this->getData('updated_at')
371
+ || $this->getData('updated_at') < $ttl
372
+ )
373
+ ) {
374
+ $this->setEmail($email);
375
+ $this->load($this->getId());
376
+ }
377
+
378
+ return $this;
379
+ }
380
+
381
+ /**
382
+ * Load by subscriber
383
+ *
384
+ * @param Mage_Newsletter_Model_Subscriber $subscriber
385
+ * @param boolean $useApi true = try to load from API
386
+ *
387
+ * @return Professio_BudgetMailer_Model_Contact
388
+ */
389
+ public function loadBySubscriber($subscriber, $useApi = true)
390
+ {
391
+ if ($subscriber->getCustomer() && $subscriber->getCustomer()->getId()) {
392
+ $this->_getResource()
393
+ ->loadByCustomer($this, $subscriber->getCustomer());
394
+ } else {
395
+ $this->_getResource()
396
+ ->loadByEmail($this, $subscriber->getEmail());
397
+ }
398
+
399
+ $ttl = time() - Mage::helper('budgetmailer/config')->getSyncTtl();
400
+
401
+ // not found try to load from API
402
+ if ($useApi && (
403
+ !$this->getData('updated_at')
404
+ || $this->getData('updated_at') < $ttl
405
+ )
406
+ ) {
407
+ $this->setEmail($subscriber->getEmail());
408
+ $this->load($this->getId());
409
+ }
410
+
411
+ return $this;
412
+ }
413
+
414
+ /**
415
+ * Delete contact from API
416
+ *
417
+ * @param null|string $email email or use current contact email
418
+ *
419
+ * @return boolean
420
+ */
421
+ public function deleteFromApi($email = null)
422
+ {
423
+ $email = is_null($email) ? $this->getEmail() : $email;
424
+ $list = $this->getList()
425
+ ? $this->getMapper()->listIdToBudgetmailerId($this->getList())
426
+ : null;
427
+
428
+ return $this->getClient()->deleteContact($email, $list);
429
+ }
430
+
431
+ /**
432
+ * Load contact from API - will use either budgetmailer id or email
433
+ *
434
+ * @return boolean
435
+ */
436
+ public function loadFromApi()
437
+ {
438
+ if ($this->getBudgetmailerId()) {
439
+ $id = $this->getBudgetmailerId();
440
+ } else if ($this->getEmail()) {
441
+ $id = $this->getEmail();
442
+ } else {
443
+ throw new Exception('Don\'t know how to load contact from API.');
444
+ }
445
+
446
+ if (!$id) {
447
+ return false;
448
+ }
449
+
450
+ $contact = $this->getClient()->getContact($id);
451
+ $this->getMapper()->contactToModel($contact, $this);
452
+
453
+ return true;
454
+ }
455
+
456
+ /**
457
+ * INFO this method expected subscribe/unsubscribed to be set from outside
458
+ *
459
+ * Save contact to API
460
+ * @return boolean
461
+ */
462
+ public function saveToApi()
463
+ {
464
+ Mage::log('budgetmailer/contact::saveToApi() start');
465
+
466
+ if ($this->getBudgetmailerId()) {
467
+ $id = $this->getBudgetmailerId();
468
+ } else {
469
+ $id = $this->getEmail();
470
+ }
471
+
472
+ if (!$id) {
473
+ return false;
474
+ }
475
+
476
+ $contactApi = $this->getClient()->getContact($id);
477
+ $contactApiNew = $this->getMapper()->contactToApi($this, $contactApi);
478
+
479
+ if (!is_object($contactApi)
480
+ || !isset($contactApi->id) || !$contactApi->id
481
+ ) {
482
+ Mage::log('budgetmailer/contact::saveToApi(): post');
483
+
484
+ $this->getMapper()->prepareContactApi($contactApiNew);
485
+
486
+ Mage::log(
487
+ 'budgetmailer/contact::saveToApi(): '
488
+ . json_encode($contactApiNew)
489
+ );
490
+
491
+ $contactApiNew = $this->getClient()->postContact($contactApiNew);
492
+ $this->getMapper()->contactToModel($contactApiNew, $this);
493
+
494
+ $this->save(false);
495
+ } else {
496
+ Mage::log('budgetmailer/contact::saveToApi(): put');
497
+
498
+ $list = $this->getList()
499
+ ? $this->getMapper()->listIdToBudgetmailerId($this->getList())
500
+ : null;
501
+
502
+ $this->getClient()->putContact($id, $contactApiNew, $list);
503
+ }
504
+
505
+ Mage::log('budgetmailer/contact::saveToApi() end');
506
+ }
507
+
508
+ /**
509
+ * Add tags to this contact, and save to API
510
+ *
511
+ * @param array $orderTags
512
+ * @param boolean $useApi true = save to API
513
+ *
514
+ * @return void
515
+ */
516
+ public function addTags($orderTags, $useApi = true)
517
+ {
518
+ if (!is_array($orderTags) || !count($orderTags)) {
519
+ return;
520
+ }
521
+
522
+ $newTags = array();
523
+ $tags = $this->getTags();
524
+
525
+ if (is_array($tags) && count($tags)) {
526
+ foreach ($orderTags as $tag) {
527
+ if (!in_array($tag, $tags)) {
528
+ $newTags = $orderTags;
529
+ }
530
+ }
531
+ } else {
532
+ $newTags = $orderTags;
533
+ }
534
+
535
+ if (count($newTags)) {
536
+ $this->setTags(array_merge($tags, $newTags));
537
+ $this->save($useApi);
538
+
539
+ $this->getClient()->postTags($this->getBudgetmailerId(), $newTags);
540
+ }
541
+ }
542
+
543
+ /**
544
+ * Make sure tags are decoded
545
+ * @param mixed $tags string or array
546
+ * @return array
547
+ */
548
+ protected function decodeTags($tags)
549
+ {
550
+ if (is_string($tags)) {
551
+ $tags = json_decode($tags);
552
+ } else if (!is_array($tags)) {
553
+ $tags = array();
554
+ }
555
+
556
+ return $tags;
557
+ }
558
+ }
app/code/community/Professio/BudgetMailer/Model/Exporter.php ADDED
@@ -0,0 +1,576 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Professio_BudgetMailer extension
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the MIT License
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/mit-license.php
11
+ *
12
+ * @category Professio
13
+ * @package Professio_BudgetMailer
14
+ * @copyright Copyright (c) 2015
15
+ * @license http://opensource.org/licenses/mit-license.php MIT License
16
+ */
17
+
18
+ /**
19
+ * Exporter model
20
+ *
21
+ * @category Professio
22
+ * @package Professio_BudgetMailer
23
+ */
24
+ class Professio_BudgetMailer_Model_Exporter
25
+ {
26
+ /**
27
+ * Customers collection
28
+ * @var Mage_Customer_Model_Resource_Customer_Collection
29
+ */
30
+ protected $_customersCollection;
31
+
32
+ /**
33
+ * Subscribers collection
34
+ * @var Mage_Newsletter_Model_Resource_Subscriber_Collection
35
+ */
36
+ protected $_subscribersCollection;
37
+
38
+ /**
39
+ * Get BudgetMailer API client
40
+ *
41
+ * @return Professio_BudgetMailer_Model_Client
42
+ */
43
+ protected function getClient()
44
+ {
45
+ return Mage::getSingleton('budgetmailer/client');
46
+ }
47
+
48
+ /**
49
+ * Get contact model
50
+ *
51
+ * @return Professio_BudgetMailer_Model_Contact
52
+ */
53
+ protected function getContact()
54
+ {
55
+ return Mage::getModel('budgetmailer/contact');
56
+ }
57
+
58
+ /**
59
+ * Prepare and get customers collection
60
+ *
61
+ * @return Mage_Customer_Model_Resource_Customer_Collection
62
+ */
63
+ protected function getCustomersCollection()
64
+ {
65
+ if (!isset($this->_customersCollection)) {
66
+ $this->_customersCollection = Mage::getModel('customer/customer')
67
+ ->getCollection();
68
+ $this->_customersCollection
69
+ ->addAttributeToSelect('*')
70
+ ->setPageSize(Professio_BudgetMailer_Model_Client::LIMIT);
71
+ }
72
+
73
+ return $this->_customersCollection;
74
+ }
75
+
76
+ /**
77
+ * Prepare and get subscribers collection
78
+ *
79
+ * @return Mage_Newsletter_Model_Resource_Subscriber_Collection
80
+ */
81
+ protected function getSubscribersCollection()
82
+ {
83
+ if (!isset($this->_subscribersCollection)) {
84
+ $this->_subscribersCollection =
85
+ Mage::getModel('newsletter/subscriber')->getCollection();
86
+ $this->_subscribersCollection
87
+ ->setPageSize(Professio_BudgetMailer_Model_Client::LIMIT);
88
+ }
89
+
90
+ return $this->_subscribersCollection;
91
+ }
92
+
93
+ /**
94
+ * Get mapper helper
95
+ * @return Professio_BudgetMailer_Helper_Mapper
96
+ */
97
+ protected function getMapper()
98
+ {
99
+ return Mage::helper('budgetmailer/mapper');
100
+ }
101
+
102
+ /**
103
+ * Get budgetmailer helper
104
+ * @return Professio_BudgetMailer_Helper_Data
105
+ */
106
+ protected function getHelper()
107
+ {
108
+ return Mage::helper('budgetmailer');
109
+ }
110
+
111
+ /**
112
+ * Export all customers to BudgetMailer API
113
+ *
114
+ * @return array
115
+ */
116
+ public function exportCustomers()
117
+ {
118
+ $this->log('budgetmailer/exporter::exportCustomers() start');
119
+
120
+ $totals = array('total' => 0, 'fail' => 0, 'success' => 0);
121
+
122
+ try {
123
+ $total = $this->getCustomersCollection()->getSize();
124
+
125
+ if ($total > 0) {
126
+
127
+ $page = 1;
128
+ $pages = ceil(
129
+ $total / Professio_BudgetMailer_Model_Client::LIMIT
130
+ );
131
+
132
+ $this->log(
133
+ 'budgetmailer/exporter::exportCustomers() customers: '
134
+ . $total . ', customer pages: ' . $pages
135
+ );
136
+
137
+ do {
138
+ $this->getCustomersCollection()->clear();
139
+ $this->getCustomersCollection()->setCurPage($page);
140
+ $this->getCustomersCollection()->load();
141
+
142
+ list($total, $fail, $success) =
143
+ $this->exportCustomersPage();
144
+
145
+ $totals['total'] += $total;
146
+ $totals['fail'] += $fail;
147
+ $totals['success'] += $success;
148
+
149
+ $page++;
150
+ } while ($page <= $pages);
151
+ } else {
152
+ $this->log(
153
+ 'budgetmailer/exporter::exportCustomers() no customers'
154
+ );
155
+ }
156
+ } catch(Exception $e) {
157
+ $this->log(
158
+ 'budgetmailer/exporter::exportCustomers() failed '
159
+ . 'with exception: ' . $e->getMessage()
160
+ );
161
+
162
+ Mage::logException($e);
163
+ }
164
+
165
+ $this->log('budgetmailer/exporter::exportCustomers() end');
166
+
167
+ return $totals;
168
+ }
169
+
170
+ /**
171
+ * Export single customers page
172
+ *
173
+ * @param boolean $subscribe (un)subscribe
174
+ * @return array
175
+ */
176
+ protected function exportCustomersPage($subscribe = true)
177
+ {
178
+ $this->log('budgetmailer/exporter::exportCustomersPage() start');
179
+
180
+ try {
181
+ $contacts = $emails = array();
182
+ $total = $fail = $success = 0;
183
+
184
+ foreach (
185
+ $this->getCustomersCollection()->getIterator() as $customer
186
+ ) {
187
+ $contact = $this->getContact();
188
+ $contact->loadByCustomer($customer, false);
189
+
190
+ if (!$contact->getEntityId()) {
191
+ $this->getMapper()->customerToModel($customer, $contact);
192
+
193
+ $address = $this->getHelper()
194
+ ->getCustomersPrimaryAddress($customer);
195
+
196
+ if ($address && $address->getEntityId()) {
197
+ $this->getMapper()->addressToModel($address, $contact);
198
+ }
199
+
200
+ $contact->setCustomerId($customer->getEntityId());
201
+ $contact->setEmail($customer->getEmail());
202
+
203
+ $contact->setUnsubscribed(!$subscribe);
204
+ $contact->setSubscribe($subscribe);
205
+ //$contact->setIsMassupdate(true);
206
+ //$contact->save(false);
207
+
208
+ $emails[] = $contact->getEmail();
209
+ }
210
+
211
+ $contacts[] = $this->getMapper()->contactToApi($contact);
212
+ }
213
+
214
+ if (count($contacts)) {
215
+ list($total, $fail, $success, $contactsNew) =
216
+ $this->getClient()->postContacts($contacts);
217
+
218
+ foreach ($contactsNew->Success as $contact) {
219
+ Mage::getModel('budgetmailer/importer')
220
+ ->importContact($contact);
221
+ }
222
+ }
223
+ } catch (Exception $e) {
224
+ $this->log(
225
+ 'budgetmailer/exporter::exportCustomersPage() failed '
226
+ . 'with exception: ' . $e->getMessage()
227
+ );
228
+
229
+ Mage::logException($e);
230
+ }
231
+
232
+ $this->log(
233
+ 'budgetmailer/exporter::exportCustomersPage() end (total: '
234
+ . $total . ', fail: ' . $fail . ', success: ' . $success
235
+ );
236
+
237
+ return array($total, $fail, $success);
238
+ }
239
+
240
+ /**
241
+ * Export all subscribers to BudgetMailer API
242
+ *
243
+ * @return array
244
+ */
245
+ public function exportSubscribers()
246
+ {
247
+ $this->log('budgetmailer/exporter::exportSubscribers() start');
248
+
249
+ $totals = array('total' => 0, 'fail' => 0, 'success' => 0);
250
+
251
+ try {
252
+ $total = $this->getSubscribersCollection()->getSize();
253
+
254
+ if ($total > 0) {
255
+ $page = 1;
256
+ $pages = ceil(
257
+ $total / Professio_BudgetMailer_Model_Client::LIMIT
258
+ );
259
+
260
+ $this->log(
261
+ 'budgetmailer/exporter::exportSubscribers() '
262
+ . 'subscribers: ' . $total . ', subscriber pages: '
263
+ . $pages
264
+ );
265
+
266
+ do {
267
+ $this->getSubscribersCollection()->clear();
268
+ $this->getSubscribersCollection()->setCurPage($page);
269
+ $this->getSubscribersCollection()->load();
270
+
271
+ list($total, $fail, $success) =
272
+ $this->exportSubscribersPage();
273
+
274
+ $totals['total'] += $total;
275
+ $totals['fail'] += $fail;
276
+ $totals['success'] += $success;
277
+
278
+ $page++;
279
+ } while ($page <= $pages);
280
+ } else {
281
+ $this->log(
282
+ 'budgetmailer/exporter::exportSubscribers() no subscribers'
283
+ );
284
+ }
285
+ } catch(Exception $e) {
286
+ $this->log(
287
+ 'budgetmailer/exporter::exportSubscribers() failed '
288
+ . 'with exception: ' . $e->getMessage()
289
+ );
290
+
291
+ Mage::logException($e);
292
+ }
293
+
294
+ $this->log('budgetmailer/exporter::exportSubscribers() end');
295
+
296
+ return $totals;
297
+ }
298
+
299
+ /**
300
+ * Export single subscribers page
301
+ *
302
+ * @param boolean $subscribe (un)subscribe
303
+ * @return array
304
+ */
305
+ protected function exportSubscribersPage($subscribe = true)
306
+ {
307
+ $this->log('budgetmailer/exporter::exportSubscribersPage() start');
308
+
309
+ try {
310
+ $contacts = array();
311
+ $total = $fail = $success = 0;
312
+
313
+ foreach (
314
+ $this->getSubscribersCollection()->getIterator() as $subscriber
315
+ ) {
316
+ $contact = $this->getContact();
317
+ $contact->loadBySubscriber($subscriber, false);
318
+
319
+ if (!$contact->getEntityId()) {
320
+ if ($subscriber->getCustomer()) {
321
+ $this->getMapper()
322
+ ->customerToModel(
323
+ $subscriber->getCustomer(),
324
+ $contact
325
+ );
326
+
327
+ $address = $this->getHelper()
328
+ ->getCustomersPrimaryAddress(
329
+ $subscriber->getCustomer()
330
+ );
331
+
332
+ if ($address && $address->getEntityId()) {
333
+ $this->getMapper()
334
+ ->addressToModel($address, $contact);
335
+ }
336
+
337
+ $contact->setCustomerId(
338
+ $subscriber->getCustomer()->getEntityId()
339
+ );
340
+ }
341
+
342
+ $contact->setEmail($subscriber->getEmail());
343
+
344
+ $contact->setUnsubscribed(!$subscribe);
345
+ $contact->setSubscribe($subscribe);
346
+ //$contact->setIsMassupdate(true);
347
+ //$contact->save(false);
348
+ }
349
+
350
+ $contacts[] = $this->getMapper()->contactToApi($contact);
351
+ }
352
+
353
+ if (count($contacts)) {
354
+ list($total, $fail, $success, $contactsNew) =
355
+ $this->getClient()->postContacts($contacts);
356
+
357
+ foreach ($contactsNew->Success as $contact) {
358
+ Mage::getModel('budgetmailer/importer')
359
+ ->importContact($contact);
360
+ }
361
+ }
362
+ } catch (Exception $e) {
363
+ $this->log(
364
+ 'budgetmailer/exporter::exportSubscribersPage() failed '
365
+ . 'with exception: ' . $e->getMessage()
366
+ );
367
+
368
+ Mage::logException($e);
369
+ }
370
+
371
+ $this->log(
372
+ 'budgetmailer/exporter::exportSubscribersPage() end (total: '
373
+ . $total . ', fail: ' . $fail . ', success: ' . $success
374
+ );
375
+
376
+ return array($total, $fail, $success);
377
+ }
378
+
379
+ /**
380
+ * Custom log wrapper - log only in developer mode
381
+ *
382
+ * @param string $message
383
+ */
384
+ protected function log($message)
385
+ {
386
+ if (Mage::getIsDeveloperMode()) {
387
+ Mage::log($message);
388
+ }
389
+ }
390
+
391
+ /**
392
+ * Mass (un)subscribe customers
393
+ * @param array $customerIds list of customer ids
394
+ * @param boolean $subscribe subscribe or not
395
+ * @return array mass actions stats as assoc array
396
+ */
397
+ public function massSubscribeCustomers($customerIds, $subscribe = true)
398
+ {
399
+ $this->log('budgetmailer/exporter::massSubscribeCustomers() start');
400
+
401
+ try {
402
+ $customerIdPages = array_chunk(
403
+ $customerIds, Professio_BudgetMailer_Model_Client::LIMIT, true
404
+ );
405
+ $totals = array('complete' => 0, 'fail' => 0, 'success' => 0);
406
+
407
+ foreach ($customerIdPages as $customerIdPage) {
408
+ $this->getCustomersCollection()->clear();
409
+ $this->getCustomersCollection()->addAttributeToFilter(
410
+ 'id', array('in' => $customerIdPage)
411
+ );
412
+ $this->getCustomersCollection()->load();
413
+
414
+ list($total, $fail, $success) = $this
415
+ ->exportCustomersPage($subscribe);
416
+
417
+ $totals['complete'] += $total;
418
+ $totals['fail'] += $fail;
419
+ $totals['success'] += $success;
420
+ }
421
+ } catch (Exception $e) {
422
+ $this->log(
423
+ 'budgetmailer/exporter::massSubscribeCustomers() failed '
424
+ . 'with exception: ' . $e->getMessage()
425
+ );
426
+
427
+ Mage::logException($e);
428
+ }
429
+
430
+ $this->log('budgetmailer/exporter::massSubscribeCustomers() end');
431
+
432
+ return $totals;
433
+ }
434
+
435
+ /**
436
+ * Mass (un)subscribe subscribers
437
+ * @param array $subscriberIds list of subscriber ids
438
+ * @param boolean $subscribe subscribe or not
439
+ * @return array mass actions stats as assoc array
440
+ */
441
+ public function massSubscribeSubscribers($subscriberIds, $subscribe = true)
442
+ {
443
+ $this->log('budgetmailer/exporter::massSubscribeSubscribers() start');
444
+
445
+ try {
446
+ $subscriberIdPages = array_chunk(
447
+ $subscriberIds, Professio_BudgetMailer_Model_Client::LIMIT, true
448
+ );
449
+ $totals = array('complete' => 0, 'fail' => 0, 'success' => 0);
450
+
451
+ foreach ($subscriberIdPages as $subscriberIdPage) {
452
+ $this->getSubscribersCollection()->clear();
453
+ $this->getSubscribersCollection()->addAttributeToFilter(
454
+ 'id', array('in' => $subscriberIdPage)
455
+ );
456
+ $this->getSubscribersCollection()->load();
457
+
458
+ list($total, $fail, $success) = $this
459
+ ->exportSubscribersPage($subscribe);
460
+
461
+ $totals['complete'] += $total;
462
+ $totals['fail'] += $fail;
463
+ $totals['success'] += $success;
464
+ }
465
+ } catch (Exception $e) {
466
+ $this->log(
467
+ 'budgetmailer/exporter::massSubscribeSubscribers() failed '
468
+ . 'with exception: ' . $e->getMessage()
469
+ );
470
+
471
+ Mage::logException($e);
472
+ }
473
+
474
+ $this->log('budgetmailer/exporter::massSubscribeSubscribers() end');
475
+
476
+ return $totals;
477
+ }
478
+
479
+ /**
480
+ * INFO unused...
481
+ * Export single customer
482
+ *
483
+ * @param Mage_Customer_Model_Customer $customer
484
+ *
485
+ * @return boolean
486
+ */
487
+ /*protected function exportCustomer($customer)
488
+ {
489
+ $this->log('budgetmailer/exporter::exportCustomer() start');
490
+
491
+ $contact = $this->getContact();
492
+ // enabled useApi param... because otherwise i can try to
493
+ // create existing contacts
494
+ $contact->loadByCustomer($customer, true);
495
+
496
+ $this->log(
497
+ 'budgetmailer/exporter::exportCustomer(): customer id: '
498
+ . $customer->getEntityId() . ', contact id: '
499
+ . $contact->getEntityId()
500
+ );
501
+
502
+ if (!$contact->getEntityId()) {
503
+ $this->getMapper()->customerToModel($customer, $contact);
504
+
505
+ $address = $this->getHelper()
506
+ ->getCustomersPrimaryAddress($customer);
507
+
508
+ if ($address && $address->getEntityId()) {
509
+ $this->getMapper()->addressToModel($address, $contact);
510
+ }
511
+
512
+ $contact->setCustomerId($customer->getEntityId());
513
+ $contact->setEmail($customer->getEmail());
514
+ $contact->setIsMassupdate(true);
515
+ $contact->save();
516
+ $rs = true;
517
+ } else {
518
+ $rs = false;
519
+ }
520
+
521
+ $this->log('budgetmailer/exporter::exportCustomer() end');
522
+
523
+ return $rs;
524
+ }*/
525
+
526
+ /**
527
+ * INFO NOT USED
528
+ * Export single subscriber
529
+ * @param Mage_Newsletter_Model_Subscriber $subscriber
530
+ * @return boolean
531
+ */
532
+ /*protected function exportSubscriber($subscriber)
533
+ {
534
+ $this->log('budgetmailer/exporter::exportSubscriber() start');
535
+
536
+ $contact = $this->getContact();
537
+ // will try to load subscribers from api
538
+ $contact->loadByEmail($subscriber->getEmail(), true);
539
+
540
+ $this->log(
541
+ 'budgetmailer/exporter::exportSubscriber(): subscriber id: '
542
+ . $subscriber->getSubscriberId() . ', contact id: '
543
+ . $contact->getEntityId()
544
+ );
545
+
546
+ if (!$contact->getEntityId()) {
547
+ if ($subscriber->getCustomer()) {
548
+ $this->getMapper()
549
+ ->customerToModel($subscriber->getCustomer(), $contact);
550
+
551
+ $address = $this->getHelper()
552
+ ->getCustomersPrimaryAddress($subscriber->getCustomer());
553
+
554
+ if ($address && $address->getEntityId()) {
555
+ $this->getMapper()->addressToModel($address, $contact);
556
+ }
557
+
558
+ $contact->setCustomerId(
559
+ $subscriber->getCustomer()->getEntityId()
560
+ );
561
+ }
562
+
563
+ $contact->setEmail($subscriber->getEmail());
564
+ $contact->setIsMassupdate(true);
565
+ $contact->save();
566
+ $rs = true;
567
+ } else {
568
+ $rs = false;
569
+ }
570
+
571
+ $this->log('budgetmailer/exporter::exportSubscriber() end');
572
+
573
+ return $rs;
574
+ }*/
575
+
576
+ }
app/code/community/Professio/BudgetMailer/Model/Importer.php ADDED
@@ -0,0 +1,480 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Professio_BudgetMailer extension
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the MIT License
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/mit-license.php
11
+ *
12
+ * @category Professio
13
+ * @package Professio_BudgetMailer
14
+ * @copyright Copyright (c) 2015
15
+ * @license http://opensource.org/licenses/mit-license.php MIT License
16
+ */
17
+
18
+ /**
19
+ * Importer model
20
+ *
21
+ * @category Professio
22
+ * @package Professio_BudgetMailer
23
+ */
24
+ class Professio_BudgetMailer_Model_Importer
25
+ {
26
+ const ACTION_INSERT = 'insert';
27
+ const ACTION_UPDATE = 'update';
28
+ const ACTION_DELETE = 'delete';
29
+
30
+ /**
31
+ * Cache of all budgetmailer ids in API
32
+ * @var array
33
+ */
34
+ protected $_budgetmailerIds;
35
+ /**
36
+ * Contacts collection
37
+ * @var Professio_BudgetMailer_Model_Resource_Contact_Collection
38
+ */
39
+ protected $_contactsCollection;
40
+
41
+ /**
42
+ * Get API client
43
+ *
44
+ * @return Professio_BudgetMailer_Model_Client
45
+ */
46
+ protected function getClient()
47
+ {
48
+ return Mage::getSingleton('budgetmailer/client');
49
+ }
50
+
51
+ /**
52
+ * Get contact
53
+ *
54
+ * @return Professio_BudgetMailer_Model_Contact
55
+ */
56
+ protected function getContact()
57
+ {
58
+ return Mage::getModel('budgetmailer/contact');
59
+ }
60
+
61
+ /**
62
+ * Get contacts collection
63
+ *
64
+ * @return array
65
+ */
66
+ protected function getContactsCollection()
67
+ {
68
+ if (!isset($this->_contactsCollection)) {
69
+ $this->_contactsCollection =
70
+ Mage::getModel('budgetmailer/contact')->getCollection();
71
+ $this->_contactsCollection
72
+ ->setPageSize(Professio_BudgetMailer_Model_Client::LIMIT);
73
+ $this->_contactsCollection->load();
74
+ }
75
+
76
+ return $this->_contactsCollection;
77
+ }
78
+
79
+ /**
80
+ * Get mapper
81
+ *
82
+ * @return Professio_BudgetMailer_Helper_Mapper
83
+ */
84
+ protected function getMapper()
85
+ {
86
+ return Mage::helper('budgetmailer/mapper');
87
+ }
88
+
89
+ /**
90
+ * Get all budgetmailer ids from API
91
+ *
92
+ * @return array
93
+ */
94
+ protected function getBudgetmailerIds()
95
+ {
96
+ if (!isset($this->_budgetmailerIds)) {
97
+ $this->log('budgetmailer/importer::getBudgetmailerIds() start');
98
+
99
+ $this->_budgetmailerIds = array();
100
+ $total = $this->getClient()->getContactsCount();
101
+
102
+ if ($total > 0) {
103
+ $page = 0;
104
+ $pages =
105
+ ceil($total / Professio_BudgetMailer_Model_Client::LIMIT);
106
+
107
+ $this->log(
108
+ 'budgetmailer/importer::deleteOrphans() contact pages: '
109
+ . $pages
110
+ );
111
+
112
+ do {
113
+ try {
114
+ $contactsApi = $this->getClient()->getContacts(
115
+ $page * Professio_BudgetMailer_Model_Client::LIMIT,
116
+ Professio_BudgetMailer_Model_Client::LIMIT,
117
+ 'ASC',
118
+ null
119
+ );
120
+
121
+ foreach ($contactsApi as $contactApi) {
122
+ $this->_budgetmailerIds[$contactApi->id] = true;
123
+ }
124
+
125
+ $page++;
126
+ } catch (Exception $e) {
127
+ Mage::logException($e);
128
+ $this->log(
129
+ 'budgetmailer/importer::getBudgetmailerIds() page: '
130
+ . $page . ', failed with exception: '
131
+ . $e->getMessage()
132
+ );
133
+ }
134
+ } while ($page < $pages);
135
+ }
136
+
137
+ $this->log(
138
+ 'budgetmailer/importer::getBudgetmailerIds() end, data: '
139
+ . json_encode(array_keys($this->_budgetmailerIds))
140
+ );
141
+ }
142
+
143
+ return $this->_budgetmailerIds;
144
+ }
145
+
146
+ /**
147
+ * Delete single orphans page
148
+ *
149
+ * @param integer $page
150
+ *
151
+ * @return array
152
+ */
153
+ protected function deleteOrphansPage()
154
+ {
155
+ $this->log('budgetmailer/importer::deleteOrphansPage() start');
156
+
157
+ $totals = array(
158
+ 'completed' => 0, 'deleted' => 0, 'failed' => 0, 'skipped' => 0
159
+ );
160
+
161
+ foreach ($this->getContactsCollection()->getIterator() as $contact) {
162
+ try {
163
+ $this->log(
164
+ 'budgetmailer/importer::deleteOrphansPage() '
165
+ . 'checking budgetmailer id: '
166
+ . $contact->getBudgetmailerId()
167
+ . ', has: '
168
+ . (
169
+ $this->hasBudgetmailerId($contact->getBudgetmailerId())
170
+ ? 'yes' : 'no'
171
+ )
172
+ );
173
+
174
+ if (!$this->hasBudgetmailerId($contact->getBudgetmailerId())) {
175
+ $contact->delete(false);
176
+
177
+ $totals['deleted']++;
178
+ $this->log(
179
+ 'budgetmailer/importer::deleteOrphansPage() '
180
+ . 'deleted: ' . $contact->getEntityId()
181
+ );
182
+ } else {
183
+ $totals['skipped']++;
184
+ $this->log(
185
+ 'budgetmailer/importer::deleteOrphansPage() '
186
+ . 'skipping: ' . $contact->getEntityId()
187
+ );
188
+ }
189
+
190
+ $totals['completed']++;
191
+ } catch(Exception $e) {
192
+ $totals['failed']++;
193
+ $this->log(
194
+ 'budgetmailer/importer::deleteOrphansPage() '
195
+ . 'failed: ' . $contact->getEntityId()
196
+ . ', with exception: ' . $e->getMessage()
197
+ );
198
+ Mage::logException($e);
199
+ }
200
+ }
201
+
202
+ $this->log('budgetmailer/importer::deleteOrphansPage() start');
203
+
204
+ return $totals;
205
+ }
206
+
207
+ /**
208
+ * Delete all orphan models from database
209
+ *
210
+ * @return array
211
+ */
212
+ public function deleteOrphans()
213
+ {
214
+ $this->log('budgetmailer/importer::deleteOrphans() start');
215
+
216
+ $total = $this->getContactsCollection()->getSize();
217
+ $totals = array(
218
+ 'completed' => 0, 'deleted' => 0, 'failed' => 0, 'skipped' => 0
219
+ );
220
+
221
+ if ($total > 0) {
222
+ $page = 0;
223
+ $pages = ceil($total / Professio_BudgetMailer_Model_Client::LIMIT);
224
+
225
+ $this->log(
226
+ 'budgetmailer/importer::deleteOrphans() local contact pages: '
227
+ . $pages
228
+ );
229
+
230
+ do {
231
+ try {
232
+ $this->getContactsCollection()->clear();
233
+ $this->getContactsCollection()->setCurPage($page);
234
+ $this->getContactsCollection()->load();
235
+
236
+ $rs = $this->deleteOrphansPage();
237
+
238
+ $totals['completed'] += $rs['completed'];
239
+ $totals['deleted'] += $rs['deleted'];
240
+ $totals['failed'] += $rs['failed'];
241
+ $totals['skipped'] += $rs['skipped'];
242
+
243
+ $page++;
244
+ } catch(Exception $e) {
245
+ $this->log(
246
+ 'budgetmailer/importer::deleteOrphans() failed page: '
247
+ . $page . ', with exception: ' . $e->getMessage()
248
+ );
249
+ Mage::logException($e);
250
+ }
251
+ } while ($page <= $pages);
252
+
253
+ } else {
254
+ $this->log(
255
+ 'budgetmailer/importer::deleteOrphans() no local contacts.'
256
+ );
257
+ }
258
+
259
+ return $totals;
260
+ }
261
+
262
+ /**
263
+ * Check if budgetmailer id exists
264
+ *
265
+ * @param string $id
266
+ *
267
+ * @return boolean
268
+ */
269
+ protected function hasBudgetmailerId($id)
270
+ {
271
+ $this->getBudgetmailerIds();
272
+
273
+ return isset($this->_budgetmailerIds[$id]);
274
+ }
275
+
276
+ /**
277
+ * Web hook implementation
278
+ *
279
+ * @param array $actions actions to process
280
+ *
281
+ * @return void
282
+ */
283
+ public function hook($actions)
284
+ {
285
+ foreach ($actions as $action) {
286
+ if (is_object($action) && isset($action->action)) {
287
+ $contact = $this->getContact();
288
+
289
+ switch($action->action) {
290
+ case self::ACTION_DELETE:
291
+ $contact->loadByBudgetmailerId($action->contact->id);
292
+ $contact->delete(false);
293
+ break;
294
+ case self::ACTION_INSERT:
295
+ $this->getMapper()
296
+ ->contactToModel($action->contact, $contact);
297
+ $contact->save(false);
298
+ break;
299
+ case self::ACTION_UPDATE:
300
+ $contact->loadByBudgetmailerId($action->contact->id);
301
+ $this->getMapper()
302
+ ->contactToModel($action->contact, $contact);
303
+ $contact->save(false);
304
+ break;
305
+ default:
306
+ throw new Professio_BudgetMailer_Exception(
307
+ 'Unexpected hook action.'
308
+ );
309
+ break;
310
+ }
311
+ } else {
312
+ throw new Professio_BudgetMailer_Exception(
313
+ 'Unexpected hook action.'
314
+ );
315
+ }
316
+ }
317
+ }
318
+
319
+ /**
320
+ * Import single contact from API
321
+ *
322
+ * @param object $contactApi
323
+ *
324
+ * @return boolean
325
+ */
326
+ public function importContact($contactApi)
327
+ {
328
+ $this->log('budgetmailer/importer::importContact() start');
329
+
330
+ // INFO we assume there is only one website
331
+ $websiteId = Mage::helper('budgetmailer')->getDefaultWebsiteId();
332
+
333
+ $contact = $this->getContact();
334
+
335
+ $this->log(
336
+ 'budgetmailer/importer::importContact() id: ' . $contactApi->id
337
+ . ' email: ' . $contactApi->email
338
+ );
339
+
340
+ if ($contactApi->email) {
341
+ $contact->loadByEmail($contactApi->email, false);
342
+ }
343
+
344
+ if (!$contact->getEntityId() && $contactApi->id) {
345
+ $contact->loadByBudgetMailerId($contactApi->id, false);
346
+ }
347
+
348
+ $this->getMapper()->contactToModel($contactApi, $contact);
349
+
350
+ if (!$contact->getCustomerId()) {
351
+ $customer = Mage::getModel('customer/customer')
352
+ ->setWebsiteId($websiteId)->loadByEmail($contactApi->email);
353
+
354
+ $this->log(
355
+ 'budgetmailer/importer::importContact() email: '
356
+ . $contactApi->email
357
+ . ', customer id: '
358
+ . $customer->getEntityId()
359
+ );
360
+
361
+ if ($customer->getEntityId()) {
362
+ $contact->setCustomerId($customer->getEntityId());
363
+ }
364
+ }
365
+
366
+ $contact->setIsMassupdate(true);
367
+ $contact->save(false);
368
+
369
+ $this->log('budgetmailer/importer::importContact() end');
370
+
371
+ return true;
372
+ }
373
+
374
+ /**
375
+ * Import single page of contacts
376
+ *
377
+ * @param integer $page
378
+ *
379
+ * @return array
380
+ */
381
+ protected function importContactsPage($page)
382
+ {
383
+ $this->log(
384
+ 'budgetmailer/importer::importContactsPage() start ('
385
+ . $page . ')'
386
+ );
387
+
388
+ $completed = $failed = 0;
389
+
390
+ $contactsApi = $this->getClient()->getContacts(
391
+ $page * Professio_BudgetMailer_Model_Client::LIMIT,
392
+ Professio_BudgetMailer_Model_Client::LIMIT,
393
+ 'ASC',
394
+ null
395
+ );
396
+
397
+ if (is_array($contactsApi) && count($contactsApi)) {
398
+ foreach ($contactsApi as $contactApi) {
399
+ try {
400
+ $this->importContact($contactApi);
401
+
402
+ $this->log(
403
+ 'budgetmailer/importer::importContactsPage() '
404
+ . 'saved contact: ' . json_encode($contactApi)
405
+ );
406
+ } catch(Exception $e) {
407
+ Mage::logException($e);
408
+ $this->log(
409
+ 'budgetmailer/importer::importContactsPage() '
410
+ . 'failed contact: ' . json_encode($contactApi)
411
+ . ', with exception: ' . $e->getMessage()
412
+ );
413
+ $failed++;
414
+ }
415
+
416
+ $completed++;
417
+ }
418
+ }
419
+
420
+ $this->log(
421
+ 'budgetmailer/importer::importContactsPage() end, completed: '
422
+ . $completed . ', failed: '. $failed
423
+ );
424
+
425
+ return array('completed' => $completed, 'failed' => $failed);
426
+ }
427
+
428
+ /**
429
+ * Import all contacts from budgetmailer list
430
+ *
431
+ * @return array
432
+ */
433
+ public function importContacts()
434
+ {
435
+ $this->log('budgetmailer/importer::importContacts() start');
436
+
437
+ $total = $this->getClient()->getContactsCount();
438
+ $totals = array('completed' => 0, 'failed' => 0);
439
+
440
+ if ($total > 0) {
441
+ $page = 0;
442
+ $pages = ceil($total / Professio_BudgetMailer_Model_Client::LIMIT);
443
+
444
+ $this->log(
445
+ 'budgetmailer/importer::importContacts() subscriber pages: '
446
+ . $pages
447
+ );
448
+
449
+ do {
450
+ $rs = $this->importContactsPage($page);
451
+
452
+ $totals['completed'] += $rs['completed'];
453
+ $totals['failed'] += $rs['failed'];
454
+
455
+ $page++;
456
+ } while ($page < $pages);
457
+ }
458
+
459
+ $this->log(
460
+ 'budgetmailer/importer::importContacts() end: '
461
+ . json_encode($totals)
462
+ );
463
+
464
+ return $totals;
465
+ }
466
+
467
+ /**
468
+ * Custom logging method - logging only if in developer mode
469
+ *
470
+ * @param string $message
471
+ *
472
+ * @return void
473
+ */
474
+ protected function log($message)
475
+ {
476
+ if (Mage::getIsDeveloperMode()) {
477
+ Mage::log($message);
478
+ }
479
+ }
480
+ }
app/code/community/Professio/BudgetMailer/Model/List.php ADDED
@@ -0,0 +1,138 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Professio_BudgetMailer extension
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the MIT License
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/mit-license.php
11
+ *
12
+ * @category Professio
13
+ * @package Professio_BudgetMailer
14
+ * @copyright Copyright (c) 2015
15
+ * @license http://opensource.org/licenses/mit-license.php MIT License
16
+ */
17
+
18
+ /**
19
+ * List model
20
+ *
21
+ * @category Professio
22
+ * @package Professio_BudgetMailer
23
+ */
24
+ class Professio_BudgetMailer_Model_List extends Mage_Core_Model_Abstract
25
+ {
26
+ /**
27
+ * Entity code.
28
+ * Can be used as part of method name for entity processing
29
+ */
30
+ const ENTITY = 'budgetmailer_list';
31
+ const CACHE_TAG = 'budgetmailer_list';
32
+
33
+ /**
34
+ * Prefix of model events names
35
+ * @var string
36
+ */
37
+ protected $_eventPrefix = 'budgetmailer_list';
38
+
39
+ /**
40
+ * Parameter name in event
41
+ * @var string
42
+ */
43
+ protected $_eventObject = 'list';
44
+
45
+ /**
46
+ * Constructor
47
+ *
48
+ * @return void
49
+ */
50
+ public function _construct()
51
+ {
52
+ parent::_construct();
53
+
54
+ $this->_init('budgetmailer/list');
55
+ }
56
+
57
+ /**
58
+ * Before save list
59
+ *
60
+ * @return Professio_BudgetMailer_Model_List
61
+ */
62
+ protected function _beforeSave()
63
+ {
64
+ parent::_beforeSave();
65
+
66
+ $now = Mage::getSingleton('core/date')->gmtDate();
67
+
68
+ if ($this->isObjectNew()) {
69
+ $this->setCreatedAt($now);
70
+ }
71
+
72
+ $this->setUpdatedAt($now);
73
+
74
+ return $this;
75
+ }
76
+
77
+ /**
78
+ * Retrieve collection
79
+ *
80
+ * @return Professio_BudgetMailer_Model_Contact_Collection
81
+ */
82
+ public function getSelectedContactsCollection()
83
+ {
84
+ if (!$this->hasData('_contact_collection')) {
85
+ if (!$this->getId()) {
86
+ return new Varien_Data_Collection();
87
+ } else {
88
+ $collection = Mage::getResourceModel(
89
+ 'budgetmailer/contact_collection'
90
+ )->addFieldToFilter('list_id', $this->getId());
91
+ $this->setData('_contact_collection', $collection);
92
+ }
93
+ }
94
+
95
+ return $this->getData('_contact_collection');
96
+ }
97
+
98
+ /**
99
+ * Get default values
100
+ *
101
+ * @return array
102
+ */
103
+ public function getDefaultValues()
104
+ {
105
+ $values = array();
106
+ $values['status'] = 1;
107
+
108
+ return $values;
109
+ }
110
+
111
+ /**
112
+ * Load list by budgetmailer id
113
+ *
114
+ * @param string $budgetmailerId
115
+ * @param boolean $useApi true = try to load from API
116
+ *
117
+ * @return Professio_BudgetMailer_Model_Contact
118
+ */
119
+ public function loadByBudgetMailerId($budgetmailerId, $useApi = true)
120
+ {
121
+ $this->_getResource()->loadByBudgetMailerId($this, $budgetmailerId);
122
+
123
+ $ttl = time() - Mage::helper('budgetmailer/config')->getSyncTtl();
124
+
125
+ // not found try to load from API
126
+ if ($useApi && (
127
+ !$this->getData('updated_at')
128
+ || $this->getData('updated_at') < $ttl
129
+ )
130
+ ) {
131
+ $this->setBudgetmailerId($budgetmailerId);
132
+ $this->load($this->getId());
133
+ }
134
+
135
+ return $this;
136
+ }
137
+
138
+ }
app/code/community/Professio/BudgetMailer/Model/Observer.php ADDED
@@ -0,0 +1,651 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Professio_BudgetMailer extension
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the MIT License
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/mit-license.php
11
+ *
12
+ * @category Professio
13
+ * @package Professio_BudgetMailer
14
+ * @copyright Copyright (c) 2015
15
+ * @license http://opensource.org/licenses/mit-license.php MIT License
16
+ */
17
+
18
+ /**
19
+ * Observer model
20
+ *
21
+ * @category Professio
22
+ * @package Professio_BudgetMailer
23
+ */
24
+ class Professio_BudgetMailer_Model_Observer
25
+ {
26
+ /**
27
+ * Get BudgetMailer API client
28
+ *
29
+ * @return Professio_BudgetMailer_Model_Client
30
+ */
31
+ protected function getClient()
32
+ {
33
+ return Mage::getSingleton('budgetmailer/client');
34
+ }
35
+
36
+ /**
37
+ * Get request
38
+ *
39
+ * @return Mage_Core_Controller_Request_Http
40
+ */
41
+ protected function getRequest()
42
+ {
43
+ return Mage::app()->getRequest();
44
+ }
45
+
46
+ /**
47
+ * Get current session
48
+ *
49
+ * @return Mage_Core_Model_Session_Abstract
50
+ */
51
+ protected function getSession()
52
+ {
53
+ if (!isset($this->session)) {
54
+ if (Mage::app()->getStore()->isAdmin()) {
55
+ $this->session = Mage::getSingleton('adminhtml/session');
56
+ } else if (Mage::getSingleton('customer/session')->isLoggedIn()) {
57
+ $this->session = Mage::getSingleton('customer/session');
58
+ } else {
59
+ $this->session = Mage::getSingleton('core/session');
60
+ }
61
+ }
62
+
63
+ return $this->session;
64
+ }
65
+
66
+ /**
67
+ * Log wrapper - loging only when in developer mode
68
+ *
69
+ * @param type $message
70
+ */
71
+ protected function log($message)
72
+ {
73
+ if (Mage::getIsDeveloperMode()) {
74
+ Mage::log($message);
75
+ }
76
+ }
77
+
78
+ /**
79
+ * Add mass actions to customer grid
80
+ *
81
+ * @param Varien_Event_Observer $observer
82
+ */
83
+ public function addMassAction($observer)
84
+ {
85
+ //$this->log('budgetmailer/observer::addMassAction() start');
86
+
87
+ try {
88
+ $block = $observer->getEvent()->getBlock();
89
+ $controller = $block->getRequest()->getControllerName();
90
+
91
+ if ('Mage_Adminhtml_Block_Widget_Grid_Massaction'
92
+ == get_class($block)
93
+ && in_array(
94
+ $controller,
95
+ array('customer', 'newsletter_subscriber')
96
+ )
97
+ ) {
98
+ $url = '*/budgetmailer/';
99
+
100
+ $block->addItem(
101
+ 'budgetmailer_subscribe',
102
+ array(
103
+ 'label' => Mage::helper('budgetmailer')
104
+ ->__('Subscribe to a BudgetMailer List'),
105
+ 'url' => Mage::helper('adminhtml')
106
+ ->getUrl($url . 'masssubscribe')
107
+ )
108
+ );
109
+
110
+ $block->addItem(
111
+ 'budgetmailer_unsubscribe',
112
+ array(
113
+ 'label' => Mage::helper('budgetmailer')
114
+ ->__('Unsubscribe from a BudgetMailer List'),
115
+ 'url' => Mage::helper('adminhtml')
116
+ ->getUrl($url . 'massunsubscribe')
117
+ )
118
+ );
119
+ }
120
+ } catch (Exception $e) {
121
+ $this->log('budgetmailer/observer::addMassAction() failed');
122
+ Mage::logException($e);
123
+ }
124
+
125
+ //$this->log('budgetmailer/observer::addMassAction() end');
126
+ }
127
+
128
+ /**
129
+ * After address delete - try to use new primary address, and update contact
130
+ *
131
+ * @param Varien_Event_Observer $observer
132
+ */
133
+ public function addressDeleteAfter($observer)
134
+ {
135
+ $this->log('budgetmailer/observer::addressDeleteAfter() start');
136
+
137
+ try {
138
+ if (Mage::helper('budgetmailer/config')
139
+ ->isAdvancedOnAddressDeleteEnabled()
140
+ ) {
141
+ $address = $observer->getCustomerAddress();
142
+ $customer = $address->getCustomer();
143
+
144
+ if ($customer && $customer->getEntityId()) {
145
+ $addressPrimary = Mage::helper('budgetmailer')
146
+ ->getCustomersPrimaryAddress($customer);
147
+
148
+ // check if primary address / map / save
149
+ if ($address && $addressPrimary
150
+ && $address->getEntityId()
151
+ == $addressPrimary->getEntityId()
152
+ ) {
153
+ $contact = Mage::getModel('budgetmailer/contact')
154
+ ->loadByCustomer($customer);
155
+ Mage::helper('budgetmailer/mapper')
156
+ ->addressToModel($address, $contact);
157
+ $contact->save();
158
+ }
159
+ } else {
160
+ $this->log(
161
+ 'budgetmailer/observer::addressDeleteAfter() '
162
+ . 'no customer'
163
+ );
164
+ }
165
+ } else {
166
+ $this->log(
167
+ 'budgetmailer/observer::addressDeleteAfter() disabled'
168
+ );
169
+ }
170
+ } catch (Exception $e) {
171
+ $this->getSession()->addError($e->getMessage());
172
+ $this->log(
173
+ 'budgetmailer/observer::addressDeleteAfter() '
174
+ . 'failed with exception: ' . $e->getMessage()
175
+ );
176
+ Mage::logException($e);
177
+ }
178
+
179
+ $this->log('budgetmailer/observer::addressDeleteAfter() end');
180
+ }
181
+
182
+ /**
183
+ * After address save, check if primary and update contact
184
+ *
185
+ * @param Varien_Event_Observer $observer
186
+ */
187
+ public function addressSaveAfter($observer)
188
+ {
189
+ $this->log('budgetmailer/observer::addressSaveAfter() start');
190
+
191
+ try {
192
+ if (Mage::helper('budgetmailer/config')
193
+ ->isAdvancedOnAddressUpdateEnabled()
194
+ ) {
195
+ $address = $observer->getCustomerAddress();
196
+ $customer = $address->getCustomer();
197
+
198
+ $this->log(
199
+ 'budgetmailer/observer::addressSaveAfter address id: '
200
+ . $address->getEntityId()
201
+ );
202
+
203
+ if ($customer && $customer->getEntityId()) {
204
+ $contact = Mage::getModel('budgetmailer/contact')
205
+ ->loadByCustomer($customer);
206
+
207
+ if ($contact && $contact->getEntityId()) {
208
+ $addressPrimary = Mage::helper('budgetmailer')
209
+ ->getCustomersPrimaryAddress($customer);
210
+
211
+ // check if primary address
212
+ if (!$addressPrimary
213
+ || ($addressPrimary->getEntityId()
214
+ == $address->getEntityId())
215
+ ) {
216
+ Mage::helper('budgetmailer/mapper')
217
+ ->addressToModel($address, $contact);
218
+
219
+ $contact->save();
220
+
221
+ $this->log(
222
+ 'budgetmailer/observer::addressSaveAfter() '
223
+ . 'updated contact'
224
+ );
225
+ } else {
226
+ $this->log(
227
+ 'budgetmailer/observer::addressSaveAfter() '
228
+ . 'DIDN\'T update contact'
229
+ );
230
+ }
231
+ } else {
232
+ $this->log(
233
+ 'budgetmailer/observer::addressSaveAfter() '
234
+ . 'no contact'
235
+ );
236
+ }
237
+ } else {
238
+ $this->log(
239
+ 'budgetmailer/observer::addressSaveAfter() no customer'
240
+ );
241
+ }
242
+ } else {
243
+ $this->log(
244
+ 'budgetmailer/observer::addressSaveAfter() disabled'
245
+ );
246
+ }
247
+ } catch (Exception $e) {
248
+ $this->getSession()->addError($e->getMessage());
249
+ $this->log(
250
+ 'budgetmailer/observer::addressSaveAfter() '
251
+ . 'failed with exception: ' . $e->getMessage()
252
+ );
253
+ Mage::logException($e);
254
+ }
255
+
256
+ $this->log('budgetmailer/observer::addressSaveAfter() end');
257
+ }
258
+
259
+ /**
260
+ * After customer delete - if enabled by config delete contact after
261
+ * deleting customer
262
+ *
263
+ * @param Varien_Event_Observer $observer
264
+ */
265
+ public function customerDeleteAfter($observer)
266
+ {
267
+ $this->log('budgetmailer/observer::customerDeleteAfter() start');
268
+
269
+ try {
270
+ if (Mage::helper('budgetmailer/config')
271
+ ->isAdvancedOnCustomerDeleteEnabled()
272
+ ) {
273
+ $customer = $observer->getCustomer();
274
+
275
+ $contact = Mage::getModel('budgetmailer/contact');
276
+ $contact->loadByCustomer($customer);
277
+
278
+ if ($contact->getEntityId()) {
279
+ $contact->delete();
280
+
281
+ $this->log(
282
+ 'budgetmailer/observer::customerDeleteAfter() '
283
+ . 'deleted contact for customer id: '
284
+ . $customer->getEntityId()
285
+ );
286
+ } else {
287
+ $this->log(
288
+ 'budgetmailer/observer::customerDeleteAfter() '
289
+ . 'contact not found for customer id: '
290
+ . $customer->getEntityId()
291
+ );
292
+ }
293
+ } else {
294
+ $this->log(
295
+ 'budgetmailer/observer::customerDeleteAfter() disabled'
296
+ );
297
+ }
298
+ } catch (Exception $e) {
299
+ $this->getSession()->addError($e->getMessage());
300
+ $this->log(
301
+ 'budgetmailer/observer::customerDeleteAfter() '
302
+ . 'failed with exception: ' . $e->getMessage()
303
+ );
304
+ Mage::logException($e);
305
+ }
306
+
307
+ $this->log('budgetmailer/observer::customerDeleteAfter() end');
308
+ }
309
+
310
+ /**
311
+ * Admin - Customer save after - if there are post data for contact
312
+ * update contact. If update customer contact config is enabled,
313
+ * update contact by customer data
314
+ *
315
+ * @param Varien_Event_Observer $observer
316
+ */
317
+ public function customerSaveAfterAdmin($observer)
318
+ {
319
+ $this->log('budgetmailer/observer::customerSaveAfterAdmin() start');
320
+
321
+ try {
322
+ $contactData = $this->getRequest()->getPost('contact');
323
+
324
+ $customer = $observer->getEvent()->getCustomer();
325
+ $contact = Mage::getModel('budgetmailer/contact')
326
+ ->loadByCustomer($customer);
327
+
328
+ $this->log(
329
+ 'budgetmailer/observer::customerSaveAfterAdmin() contact: '
330
+ . $contact->getEntityId() . ', customer: '
331
+ . $customer->getEntityId()
332
+ );
333
+
334
+ // MAP CONTACT DATA
335
+ $hasContactData = is_array($contactData) && count($contactData);
336
+
337
+ if ($hasContactData) {
338
+ $contactData['unsubscribed'] =
339
+ !isset($contactData['subscribe']);
340
+ $contact->addData($contactData);
341
+
342
+ $this->log(
343
+ 'budgetmailer/observer::customerSaveAfterAdmin() '
344
+ . 'mapped contact data to contact: '
345
+ . json_encode($contactData) . '.'
346
+ );
347
+ } else {
348
+ $this->log(
349
+ 'budgetmailer/observer::customerSaveAfterAdmin() '
350
+ . 'no contact data.'
351
+ );
352
+ }
353
+
354
+ // MAP CUSTOMER
355
+ if (Mage::helper('budgetmailer/config')
356
+ ->isAdvancedOnCustomerUpdateEnabled()
357
+ ) {
358
+ Mage::helper('budgetmailer/mapper')
359
+ ->customerToModel($customer, $contact);
360
+
361
+ $this->log(
362
+ 'budgetmailer/observer::customerSaveAfterAdmin() '
363
+ . 'mapped customer to contact.'
364
+ );
365
+ } else {
366
+ $this->log(
367
+ 'budgetmailer/observer::customerSaveAfterAdmin() '
368
+ . 'customer update disabled.'
369
+ );
370
+ }
371
+
372
+ $this->customerSaveAfterAdminAddress($contact, $customer);
373
+
374
+ if ($contact->getEntityId()
375
+ || ( $hasContactData && isset($contactData['subscribe']) )
376
+ ) {
377
+ $this->log(
378
+ 'budgetmailer/observer::customerSaveAfterAdmin() saving.'
379
+ );
380
+
381
+ $contact->save();
382
+ }
383
+ } catch (Exception $e) {
384
+ $this->getSession()->addError($e->getMessage());
385
+ $this->log(
386
+ 'budgetmailer/observer::customerSaveAfterAdmin() '
387
+ . 'failed with exception: ' . $e->getMessage()
388
+ );
389
+ Mage::logException($e);
390
+ }
391
+
392
+ $this->log('budgetmailer/observer::customerSaveAfterAdmin() end');
393
+ }
394
+
395
+ protected function customerSaveAfterAdminAddress($contact, $customer)
396
+ {
397
+ // MAP ADDRESS
398
+ if (Mage::helper('budgetmailer/config')
399
+ ->isAdvancedOnAddressUpdateEnabled()
400
+ && $customer && $customer->getEntityId()
401
+ ) {
402
+ $address = Mage::helper('budgetmailer')
403
+ ->getCustomersPrimaryAddress($customer);
404
+
405
+ if ($address && $address->getEntityId()) {
406
+ $this->log(
407
+ 'budgetmailer/observer::customerSaveAfterAdmin()'
408
+ . ' mapping address: ' . $address->getEntityId()
409
+ );
410
+
411
+ Mage::helper('budgetmailer/mapper')
412
+ ->addressToModel($address, $contact);
413
+ } else {
414
+ $this->log(
415
+ 'budgetmailer/observer::customerSaveAfterAdmin() '
416
+ . 'no address'
417
+ );
418
+ }
419
+ } else {
420
+ $this->log(
421
+ 'budgetmailer/observer::customerSaveAfterAdmin() '
422
+ . 'not mapping address'
423
+ );
424
+ }
425
+ }
426
+
427
+ /**
428
+ * Front - customer save after. If update of contact by customer is enabled
429
+ * update contact. If there is subscribed parameter after registering new
430
+ * customer, sign up the customer.
431
+ *
432
+ * @param Varien_Event_Observer $observer
433
+ */
434
+ public function customerSaveAfterFront($observer)
435
+ {
436
+ $this->log('budgetmailer/observer::customerSaveAfterFront() start');
437
+
438
+ try {
439
+ $bmIsSubscribed = $this->getRequest()->get('bm_is_subscribed');
440
+ $changed = false;
441
+
442
+ $customer = $observer->getEvent()->getCustomer();
443
+ $contact = Mage::getModel('budgetmailer/contact')
444
+ ->loadByCustomer($customer);
445
+
446
+ $this->log(
447
+ 'budgetmailer/observer::customerSaveAfterFront() contact: '
448
+ . $contact->getEntityId() . ', customer: '
449
+ . $customer->getEntityId()
450
+ );
451
+
452
+ if ($bmIsSubscribed) {
453
+ $changed = true;
454
+
455
+ $contact->setSubscribe(true);
456
+ $contact->setUnsubscribed(false);
457
+
458
+ $this->log(
459
+ 'budgetmailer/observer::customerSaveAfterFront() '
460
+ . 'subscribing.'
461
+ );
462
+ } else {
463
+ $this->log(
464
+ 'budgetmailer/observer::customerSaveAfterFront() '
465
+ . 'not subscribing.'
466
+ );
467
+ }
468
+
469
+ if (Mage::helper('budgetmailer/config')
470
+ ->isAdvancedOnCustomerUpdateEnabled()
471
+ ) {
472
+ if ($contact->getEntityId() || $bmIsSubscribed) {
473
+ $changed = true;
474
+
475
+ Mage::helper('budgetmailer/mapper')
476
+ ->customerToModel($customer, $contact);
477
+
478
+ $this->log(
479
+ 'budgetmailer/observer::customerSaveAfterFront() '
480
+ . 'mapped customer to contact.'
481
+ );
482
+ } else {
483
+ // INFO this was creating contacts without subscribing
484
+ $this->log(
485
+ 'budgetmailer/observer::customerSaveAfterFront() '
486
+ . 'DIDN\'T mapped customer to contact, because '
487
+ . 'contact doesnt\'t exist yet..'
488
+ );
489
+ }
490
+ } else {
491
+ $this->log(
492
+ 'budgetmailer/observer::customerSaveAfterFront() '
493
+ . 'customer update disabled.'
494
+ );
495
+ }
496
+
497
+ if ($changed) {
498
+ $this->log(
499
+ 'budgetmailer/observer::customerSaveAfterFront() saving.'
500
+ );
501
+ $contact->save();
502
+ } else {
503
+ $this->log(
504
+ 'budgetmailer/observer::customerSaveAfterFront() not saved.'
505
+ );
506
+ }
507
+ } catch (Exception $e) {
508
+ $this->getSession()->addError($e->getMessage());
509
+ $this->log(
510
+ 'budgetmailer/observer::customerSaveAfterFront() '
511
+ . 'failed with exception: ' . $e->getMessage()
512
+ );
513
+ Mage::logException($e);
514
+ }
515
+
516
+ $this->log('budgetmailer/observer::customerSaveAfterFront() end');
517
+ }
518
+
519
+ /**
520
+ * After place order - if enabled, update contact by tags (ordered product
521
+ * category names)
522
+ *
523
+ * @param Varien_Event_Observer $observer
524
+ */
525
+ public function salesOrderPlaceAfter($observer)
526
+ {
527
+ $this->log('budgetmailer/observer::salesOrderPlaceAfter() start');
528
+
529
+ try {
530
+ if (Mage::helper('budgetmailer/config')
531
+ ->isAdvancedOnOrderEnabled()) {
532
+ $order = $observer->getEvent()->getOrder();
533
+ $contact = Mage::getModel('budgetmailer/contact');
534
+ $customer = $order->getCustomer();
535
+
536
+ $contact->loadByCustomer($customer);
537
+
538
+ $this->log(
539
+ 'budgetmailer/observer::salesOrderPlaceAfter() order: '
540
+ . $order->getEntityId() . ', customer: '
541
+ . $customer->getEntityId() . ', contact: '
542
+ . $contact->getEntityId()
543
+ );
544
+
545
+ if ($contact->getEntityId()) {
546
+ $orderTags = Mage::helper('budgetmailer')
547
+ ->getOrderTags($order);
548
+
549
+ $this->log(
550
+ 'budgetmailer/observer::salesOrderPlaceAfter() '
551
+ . 'adding tags: ' . json_encode($orderTags)
552
+ );
553
+ $contact->addTags($orderTags, false);
554
+ }
555
+ } else {
556
+ $this->log(
557
+ 'budgetmailer/observer::salesOrderPlaceAfter() '
558
+ . 'tagging disabled'
559
+ );
560
+ }
561
+ } catch(Exception $e) {
562
+ $this->getSession()->addError($e->getMessage());
563
+ $this->log(
564
+ 'budgetmailer/observer::salesOrderPlaceAfter() '
565
+ . 'failed with exception: ' . $e->getMessage()
566
+ );
567
+ Mage::logException($e);
568
+ }
569
+
570
+ $this->log('budgetmailer/observer::salesOrderPlaceAfter() end');
571
+ }
572
+
573
+ /**
574
+ * Cron method. Periodically delete orphans, and import new contacts
575
+ */
576
+ public function cron()
577
+ {
578
+ Mage::log('budgetmailer/observer::cron() start');
579
+
580
+ if (Mage::helper('budgetmailer/config')->isSyncCronEnabled()) {
581
+ try {
582
+ $rs = Mage::getSingleton('budgetmailer/importer')
583
+ ->deleteOrphans();
584
+ $rs = Mage::getSingleton('budgetmailer/importer')
585
+ ->importContacts();
586
+
587
+ Mage::log(
588
+ ($rs
589
+ ? 'budgetmailer/observer::cron() no contacts'
590
+ : 'budgetmailer/observer::cron() completed: '
591
+ . $rs['completed'] . ', failed: ' . $rs['failed']
592
+ )
593
+ );
594
+ } catch (Exception $e) {
595
+ $this->getSession()->addError($e->getMessage());
596
+ Mage::log(
597
+ 'budgetmailer/observer::cron() failed with exception: '
598
+ . $e->getMessage()
599
+ );
600
+ Mage::logException($e);
601
+ }
602
+ }
603
+
604
+ Mage::log('budgetmailer/observer::cron() end');
605
+ }
606
+
607
+ /**
608
+ * After saving configuration of BudgetMailer - validate API key
609
+ * and secret. Initiate lists collection.
610
+ *
611
+ * @param Varien_Event_Observer $observer
612
+ */
613
+ public function afterConfigChange()
614
+ {
615
+ $this->log('budgetmailer/observer::afterConfigChange() start');
616
+
617
+ try {
618
+ $p = $this->getRequest()->getPost();
619
+ $t = $this->getClient()
620
+ ->testApiCredentials($p['groups']['api']['fields']);
621
+
622
+ if (!$t) {
623
+ $this->getSession()->addError(
624
+ Mage::helper('budgetmailer')
625
+ ->__('Invalid API endpoint, key and secret combination.')
626
+ );
627
+ } else {
628
+ // INFO this block could be in testApiCredentials
629
+ $list = Mage::getModel('budgetmailer/list');
630
+ $collection = $list->getCollection();
631
+ $collection->load(false, false, true, true);
632
+
633
+ $this->getSession()->addSuccess(
634
+ Mage::helper('budgetmailer')
635
+ ->__(
636
+ 'API endpoint, key and secret combination is valid.'
637
+ )
638
+ );
639
+ }
640
+ } catch (Exception $e) {
641
+ $this->getSession()->addError($e->getMessage());
642
+ $this->log(
643
+ 'budgetmailer/observer::afterConfigChange() failed '
644
+ . 'with exception: ' . $e->getMessage()
645
+ );
646
+ Mage::logException($e);
647
+ }
648
+
649
+ $this->log('budgetmailer/observer::afterConfigChange() end');
650
+ }
651
+ }
app/code/community/Professio/BudgetMailer/Model/Resource/Contact.php ADDED
@@ -0,0 +1,156 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Professio_BudgetMailer extension
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the MIT License
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/mit-license.php
11
+ *
12
+ * @category Professio
13
+ * @package Professio_BudgetMailer
14
+ * @copyright Copyright (c) 2015
15
+ * @license http://opensource.org/licenses/mit-license.php MIT License
16
+ */
17
+
18
+ /**
19
+ * Contact model
20
+ *
21
+ * @category Professio
22
+ * @package Professio_BudgetMailer
23
+ */
24
+ class Professio_BudgetMailer_Model_Resource_Contact
25
+ extends Mage_Core_Model_Resource_Db_Abstract
26
+ {
27
+ /**
28
+ * Constructor
29
+ */
30
+ public function _construct()
31
+ {
32
+ $this->_init('budgetmailer/contact', 'entity_id');
33
+ }
34
+
35
+ /**
36
+ * Load contact by budgetmailer id
37
+ *
38
+ * @param Professio_BudgetMailer_Model_Contact $contact
39
+ * @param string $budgetmailerId
40
+ *
41
+ * @return Mage_Customer_Model_Resource_Customer
42
+ * @throws Mage_Core_Exception
43
+ */
44
+ public function loadByBudgetMailerId(
45
+ Professio_BudgetMailer_Model_Contact $contact,
46
+ $budgetmailerId
47
+ )
48
+ {
49
+ $adapter = $this->_getReadAdapter();
50
+ $bind = array('budgetmailer_id' => $budgetmailerId);
51
+
52
+ $select = $adapter->select()
53
+ ->from(
54
+ Mage::getSingleton('core/resource')
55
+ ->getTableName('budgetmailer/contact'), array('entity_id')
56
+ )
57
+ ->where('budgetmailer_id = :budgetmailer_id');
58
+
59
+ $contactId = $adapter->fetchOne($select, $bind);
60
+
61
+ if ($contactId) {
62
+ $this->load($contact, $contactId);
63
+ $contact->setOrigData();
64
+ } else {
65
+ $contact->setData(array());
66
+ }
67
+
68
+ return $this;
69
+ }
70
+
71
+ /**
72
+ * Load contact by customer
73
+ *
74
+ * @param Professio_BudgetMailer_Model_Contact $contact
75
+ * @param Mage_Customer_Model_Customer $customer
76
+ *
77
+ * @return Mage_Customer_Model_Resource_Customer
78
+ * @throws Mage_Core_Exception
79
+ */
80
+ public function loadByCustomer(
81
+ Professio_BudgetMailer_Model_Contact $contact,
82
+ Mage_Customer_Model_Customer $customer
83
+ )
84
+ {
85
+ $adapter = $this->_getReadAdapter();
86
+ $bind = array(
87
+ 'customer_id' => $customer->getEntityId(),
88
+ 'list_id' => Mage::helper('budgetmailer/mapper')
89
+ ->listBudgetmailerIdToListId(
90
+ Mage::helper('budgetmailer/config')->getGeneralList()
91
+ )
92
+ );
93
+
94
+ $select = $adapter->select()
95
+ ->from(
96
+ Mage::getSingleton('core/resource')
97
+ ->getTableName('budgetmailer/contact'), array('entity_id')
98
+ )
99
+ ->where('customer_id = :customer_id AND list_id = :list_id');
100
+
101
+ $contactId = $adapter->fetchOne($select, $bind);
102
+
103
+ if ($contactId) {
104
+ Mage::log('resource loadByCustomer() loading');
105
+ $this->load($contact, $contactId);
106
+ $contact->setOrigData();
107
+ } else {
108
+ Mage::log('resource loadByCustomer() NOT loading');
109
+ $contact->setData(array());
110
+ }
111
+
112
+ return $this;
113
+ }
114
+
115
+ /**
116
+ * Load contact by email
117
+ *
118
+ * @param Professio_BudgetMailer_Model_Contact $contact
119
+ * @param string $email
120
+ *
121
+ * @return Mage_Customer_Model_Resource_Customer
122
+ * @throws Mage_Core_Exception
123
+ */
124
+ public function loadByEmail(
125
+ Professio_BudgetMailer_Model_Contact $contact,
126
+ $email
127
+ )
128
+ {
129
+ $adapter = $this->_getReadAdapter();
130
+ $bind = array(
131
+ 'email' => $email,
132
+ 'list_id' => Mage::helper('budgetmailer/mapper')
133
+ ->listBudgetmailerIdToListId(
134
+ Mage::helper('budgetmailer/config')->getGeneralList()
135
+ )
136
+ );
137
+
138
+ $select = $adapter->select()
139
+ ->from(
140
+ Mage::getSingleton('core/resource')
141
+ ->getTableName('budgetmailer/contact'), array('entity_id')
142
+ )
143
+ ->where('email = :email AND list_id = :list_id');
144
+
145
+ $contactId = $adapter->fetchOne($select, $bind);
146
+
147
+ if ($contactId) {
148
+ $this->load($contact, $contactId);
149
+ $contact->setOrigData();
150
+ } else {
151
+ $contact->setData(array());
152
+ }
153
+
154
+ return $this;
155
+ }
156
+ }
app/code/community/Professio/BudgetMailer/Model/Resource/Contact/Collection.php ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Professio_BudgetMailer extension
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the MIT License
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/mit-license.php
11
+ *
12
+ * @category Professio
13
+ * @package Professio_BudgetMailer
14
+ * @copyright Copyright (c) 2015
15
+ * @license http://opensource.org/licenses/mit-license.php MIT License
16
+ */
17
+
18
+ /**
19
+ * List collection resource
20
+ *
21
+ * @category Professio
22
+ * @package Professio_BudgetMailer
23
+ */
24
+ class Professio_BudgetMailer_Model_Resource_Contact_Collection
25
+ extends Mage_Core_Model_Resource_Db_Collection_Abstract
26
+ {
27
+ /**
28
+ * Something
29
+ * @var array
30
+ */
31
+ protected $_joinedFields = array();
32
+
33
+ /**
34
+ * Constructor
35
+ *
36
+ * @return void
37
+ */
38
+ protected function _construct()
39
+ {
40
+ parent::_construct();
41
+
42
+ $this->_init('budgetmailer/contact');
43
+ }
44
+
45
+ /**
46
+ * Get SQL for get record count. Extra GROUP BY strip added.
47
+ *
48
+ * @return Varien_Db_Select
49
+ */
50
+ public function getSelectCountSql()
51
+ {
52
+ $countSelect = parent::getSelectCountSql();
53
+ $countSelect->reset(Zend_Db_Select::GROUP);
54
+
55
+ return $countSelect;
56
+ }
57
+
58
+
59
+ public function idsToBudgetmailerIds($ids)
60
+ {
61
+ $this->clear();
62
+ //$this->addFieldToSelect('budgetmailer_id');
63
+ $this->addFieldToFilter(
64
+ 'entity_id',
65
+ array(
66
+ 'in' => $ids
67
+ )
68
+ );
69
+ $this->load();
70
+
71
+ $budgetmailerIds = array();
72
+
73
+ foreach ($this->getIterator() as $contact) {
74
+ $budgetmailerIds[] = $contact->getBudgetmailerId();
75
+ }
76
+
77
+ return $budgetmailerIds;
78
+ }
79
+ }
app/code/community/Professio/BudgetMailer/Model/Resource/List.php ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Professio_BudgetMailer extension
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the MIT License
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/mit-license.php
11
+ *
12
+ * @category Professio
13
+ * @package Professio_BudgetMailer
14
+ * @copyright Copyright (c) 2015
15
+ * @license http://opensource.org/licenses/mit-license.php MIT License
16
+ */
17
+
18
+ /**
19
+ * Resource for list
20
+ *
21
+ * @category Professio
22
+ * @package Professio_BudgetMailer
23
+ */
24
+ class Professio_BudgetMailer_Model_Resource_List
25
+ extends Mage_Core_Model_Resource_Db_Abstract
26
+ {
27
+ /**
28
+ * Constructor
29
+ *
30
+ * @return void
31
+ */
32
+ public function _construct()
33
+ {
34
+ $this->_init('budgetmailer/list', 'entity_id');
35
+ }
36
+
37
+ /**
38
+ * Load list by budgetmailer id
39
+ *
40
+ * @param Professio_BudgetMailer_Model_List $list
41
+ * @param string $budgetmailerId
42
+ *
43
+ * @return Mage_Customer_Model_Resource_Customer
44
+ * @throws Mage_Core_Exception
45
+ */
46
+ public function loadByBudgetMailerId(
47
+ Professio_BudgetMailer_Model_List $list,
48
+ $budgetmailerId
49
+ )
50
+ {
51
+ $adapter = $this->_getReadAdapter();
52
+ $bind = array('budgetmailer_id' => $budgetmailerId);
53
+
54
+ $select = $adapter->select()
55
+ ->from(
56
+ Mage::getSingleton('core/resource')
57
+ ->getTableName('budgetmailer/list'), array('entity_id')
58
+ )
59
+ ->where('budgetmailer_id = :budgetmailer_id');
60
+
61
+ $listId = $adapter->fetchOne($select, $bind);
62
+
63
+ if ($listId) {
64
+ $this->load($list, $listId);
65
+ $list->setOrigData();
66
+ } else {
67
+ $list->setData(array());
68
+ }
69
+
70
+ return $this;
71
+ }
72
+ }
app/code/community/Professio/BudgetMailer/Model/Resource/List/Collection.php ADDED
@@ -0,0 +1,177 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Professio_BudgetMailer extension
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the MIT License
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/mit-license.php
11
+ *
12
+ * @category Professio
13
+ * @package Professio_BudgetMailer
14
+ * @copyright Copyright (c) 2015
15
+ * @license http://opensource.org/licenses/mit-license.php MIT License
16
+ */
17
+
18
+ /**
19
+ * List collection resource
20
+ *
21
+ * @category Professio
22
+ * @package Professio_BudgetMailer
23
+ */
24
+ class Professio_BudgetMailer_Model_Resource_List_Collection
25
+ extends Mage_Core_Model_Resource_Db_Collection_Abstract
26
+ {
27
+ /**
28
+ * Something?
29
+ * @var array
30
+ */
31
+ protected $_joinedFields = array();
32
+ /**
33
+ * Load from API flag
34
+ * @var boolean
35
+ */
36
+ protected $_loadingFromApi;
37
+
38
+ /**
39
+ * Constructor
40
+ *
41
+ * @return void
42
+ */
43
+ protected function _construct()
44
+ {
45
+ parent::_construct();
46
+
47
+ $this->_init('budgetmailer/list');
48
+ }
49
+
50
+ /**
51
+ * Get SQL for get record count. Extra GROUP BY strip added.
52
+ *
53
+ * @return Varien_Db_Select
54
+ */
55
+ public function getSelectCountSql()
56
+ {
57
+ $countSelect = parent::getSelectCountSql();
58
+ $countSelect->reset(Zend_Db_Select::GROUP);
59
+
60
+ return $countSelect;
61
+ }
62
+
63
+ /**
64
+ * Get BudgetMailer client
65
+ * @return Professio_BudgetMailer_Model_Client
66
+ */
67
+ protected function getClient()
68
+ {
69
+ return Mage::getSingleton('budgetmailer/client');
70
+ }
71
+
72
+ /**
73
+ * Get mapper
74
+ * @return Professio_BudgetMailer_Helper_Mapper
75
+ */
76
+ protected function getMapper()
77
+ {
78
+ return Mage::helper('budgetmailer/mapper');
79
+ }
80
+
81
+ /**
82
+ * Load collection
83
+ *
84
+ * @param boolean $printQuery
85
+ * @param boolean $logQuery
86
+ * @param boolean $loadFromApi if true and no records -> load from api
87
+ *
88
+ * @return \Professio_BudgetMailer_Model_Resource_List_Collection
89
+ */
90
+ public function load(
91
+ $printQuery = false, $logQuery = false, $loadFromApi = true,
92
+ $forceLoadFromApi = false
93
+ )
94
+ {
95
+ parent::load($printQuery, $logQuery);
96
+
97
+ if ( ( !count($this->_items) && $loadFromApi ) || $forceLoadFromApi) {
98
+ $this->loadFromApi();
99
+ }
100
+
101
+ return $this;
102
+ }
103
+
104
+ /**
105
+ * Load list collection from API
106
+ *
107
+ * @return void
108
+ */
109
+ public function loadFromApi()
110
+ {
111
+ if ($this->isLoadingFromApi()) {
112
+ return;
113
+ }
114
+
115
+ $this->setLoadingFromApi(true);
116
+ // INFO this prevents cycling out (see mapper)
117
+ Mage::register('budgetmailer_list_initiation', true);
118
+
119
+ $lists = $this->getClient()->getLists();
120
+
121
+ foreach ($lists as $list) {
122
+ $model = Mage::getModel('budgetmailer/list');
123
+ $model->loadByBudgetmailerId($list->id, false);
124
+ $this->getMapper()->listToModel($list, $model);
125
+ $model->setIsMassupdate(true);
126
+ $model->save();
127
+ }
128
+
129
+ $this->clear();
130
+ //$this->_setIsLoaded(false);
131
+ //$this->load(false, false, false);
132
+ parent::load(false, false);
133
+
134
+ $this->setLoadingFromApi(false);
135
+ Mage::unregister('budgetmailer_list_initiation');
136
+ }
137
+
138
+ /**
139
+ * Check if loading from api is in progress.
140
+ * Avoid double API loads
141
+ *
142
+ * @return boolean
143
+ */
144
+ protected function isLoadingFromApi()
145
+ {
146
+ return $this->_loadingFromApi;
147
+ }
148
+
149
+ /**
150
+ * Set load from api flag value
151
+ *
152
+ * @param boolean $v
153
+ *
154
+ * @return Professio_BudgetMailer_Model_Resource_List_Collection
155
+ */
156
+ protected function setLoadingFromApi($v)
157
+ {
158
+ $this->_loadingFromApi = $v;
159
+
160
+ return $this;
161
+ }
162
+
163
+ /**
164
+ * Override to option hash
165
+ * @return array
166
+ */
167
+ public function toOptionHash()
168
+ {
169
+ $h = array();
170
+
171
+ foreach ($this as $item) {
172
+ $h[$item->getEntityId()] = $item->getName();
173
+ }
174
+
175
+ return $h;
176
+ }
177
+ }
app/code/community/Professio/BudgetMailer/Model/Resource/Setup.php ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Professio_BudgetMailer extension
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the MIT License
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/mit-license.php
11
+ *
12
+ * @category Professio
13
+ * @package Professio_BudgetMailer
14
+ * @copyright Copyright (c) 2015
15
+ * @license http://opensource.org/licenses/mit-license.php MIT License
16
+ */
17
+
18
+ /**
19
+ * Resource setup model
20
+ *
21
+ * @category Professio
22
+ * @package Professio_BudgetMailer
23
+ */
24
+ class Professio_BudgetMailer_Model_Resource_Setup
25
+ extends Mage_Core_Model_Resource_Setup
26
+ {
27
+
28
+ }
app/code/community/Professio/BudgetMailer/controllers/Adminhtml/Budgetmailer/ContactController.php ADDED
@@ -0,0 +1,497 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Professio_BudgetMailer extension
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the MIT License
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/mit-license.php
11
+ *
12
+ * @category Professio
13
+ * @package Professio_BudgetMailer
14
+ * @copyright Copyright (c) 2015
15
+ * @license http://opensource.org/licenses/mit-license.php MIT License
16
+ */
17
+
18
+ /**
19
+ * Implementation of backend-end contact controller (CRUD + mass actions)
20
+ *
21
+ * @category Professio
22
+ * @package Professio_BudgetMailer
23
+ */
24
+ class Professio_BudgetMailer_Adminhtml_Budgetmailer_ContactController
25
+ extends Professio_BudgetMailer_Controller_Adminhtml_BudgetMailer
26
+ {
27
+
28
+ /**
29
+ * Intiate new contact and try to load it by requested id
30
+ *
31
+ * @return Professio_BudgetMailer_Model_Contact
32
+ */
33
+ protected function _initContact()
34
+ {
35
+ $contactId = (int) $this->getRequest()->getParam('id');
36
+ $contact = Mage::getModel('budgetmailer/contact');
37
+
38
+ if ($contactId) {
39
+ $contact->load($contactId);
40
+ }
41
+
42
+ Mage::register('current_contact', $contact);
43
+
44
+ return $contact;
45
+ }
46
+
47
+ /**
48
+ * Displays list of contacts
49
+ *
50
+ * @return null
51
+ */
52
+ public function indexAction()
53
+ {
54
+ $this->loadLayout();
55
+
56
+ $this->_title(Mage::helper('budgetmailer')->__('BudgetMailer'))
57
+ ->_title(Mage::helper('budgetmailer')->__('Contacts'));
58
+
59
+ $this->renderLayout();
60
+ }
61
+
62
+ /**
63
+ * Grid action
64
+ *
65
+ * @return null
66
+ */
67
+ public function gridAction()
68
+ {
69
+ $this->loadLayout()->renderLayout();
70
+ }
71
+
72
+ /**
73
+ * Edit single contact, if doesn't exist redirect to index
74
+ *
75
+ * @return null
76
+ */
77
+ public function editAction()
78
+ {
79
+ $contactId = $this->getRequest()->getParam('id');
80
+ $contact = $this->_initContact();
81
+
82
+ if ($contactId && !$contact->getId()) {
83
+ $this->_getSession()->addError(
84
+ Mage::helper('budgetmailer')
85
+ ->__('This contact no longer exists.')
86
+ );
87
+ $this->_redirect('*/*/');
88
+
89
+ return;
90
+ }
91
+
92
+ $data = Mage::getSingleton('adminhtml/session')->getContactData(true);
93
+
94
+ if (!empty($data)) {
95
+ $contact->setData($data);
96
+ }
97
+
98
+ Mage::register('contact_data', $contact);
99
+
100
+ $this->loadLayout();
101
+ $this->_title(Mage::helper('budgetmailer')->__('BudgetMailer'))
102
+ ->_title(Mage::helper('budgetmailer')->__('Contacts'));
103
+
104
+ if ($contact->getId()) {
105
+ $this->_title($contact->getEmail());
106
+ } else {
107
+ $this->_title(Mage::helper('budgetmailer')->__('Add contact'));
108
+ }
109
+
110
+ if (Mage::getSingleton('cms/wysiwyg_config')->isEnabled()) {
111
+ $this->getLayout()->getBlock('head')->setCanLoadTinyMce(true);
112
+ }
113
+
114
+ $this->renderLayout();
115
+ }
116
+
117
+ /**
118
+ * Create new contact (same as edit)
119
+ */
120
+ public function newAction()
121
+ {
122
+ $this->_forward('edit');
123
+ }
124
+
125
+ /**
126
+ * Handle new/edit POST data and save the model
127
+ *
128
+ * @return null
129
+ */
130
+ public function saveAction()
131
+ {
132
+ if ($data = $this->getRequest()->getPost('contact')) {
133
+ try {
134
+ $data['tags'] = isset($data['tags']) && is_array($data['tags'])
135
+ && count($data['tags']) ? $data['tags'] : array();
136
+ $data['unsubscribed'] = !isset($data['subscribe']);
137
+
138
+ $contact = $this->_initContact();
139
+ $contact->addData($data);
140
+ $contact->save();
141
+ Mage::getSingleton('adminhtml/session')->addSuccess(
142
+ Mage::helper('budgetmailer')->__(
143
+ 'Contact was successfully saved'
144
+ )
145
+ );
146
+ Mage::getSingleton('adminhtml/session')->setFormData(false);
147
+
148
+ if ($this->getRequest()->getParam('back')) {
149
+ $this->_redirect(
150
+ '*/*/edit', array('id' => $contact->getId())
151
+ );
152
+
153
+ return;
154
+ }
155
+
156
+ $this->_redirect('*/*/');
157
+
158
+ return;
159
+ } catch (Mage_Core_Exception $e) {
160
+ Mage::getSingleton('adminhtml/session')
161
+ ->addError($e->getMessage());
162
+ Mage::getSingleton('adminhtml/session')
163
+ ->setContactData($data);
164
+
165
+ $this->_redirect(
166
+ '*/*/edit',
167
+ array('id' => $this->getRequest()->getParam('id'))
168
+ );
169
+
170
+ return;
171
+ } catch (Exception $e) {
172
+ Mage::logException($e);
173
+ Mage::getSingleton('adminhtml/session')
174
+ ->addError(
175
+ Mage::helper('budgetmailer')
176
+ ->__('There was a problem saving the contact.')
177
+ );
178
+ Mage::getSingleton('adminhtml/session')->setContactData($data);
179
+
180
+ $this->_redirect(
181
+ '*/*/edit',
182
+ array('id' => $this->getRequest()->getParam('id'))
183
+ );
184
+
185
+ return;
186
+ }
187
+ }
188
+
189
+ Mage::getSingleton('adminhtml/session')
190
+ ->addError(
191
+ Mage::helper('budgetmailer')
192
+ ->__('Unable to find contact to save.')
193
+ );
194
+
195
+ $this->_redirect('*/*/');
196
+ }
197
+
198
+ /**
199
+ * Delete single contact
200
+ *
201
+ * @return null
202
+ */
203
+ public function deleteAction()
204
+ {
205
+ if ($this->getRequest()->getParam('id') > 0) {
206
+ try {
207
+ $contact = Mage::getModel('budgetmailer/contact');
208
+ $contact->setId($this->getRequest()->getParam('id'))->delete();
209
+
210
+ Mage::getSingleton('adminhtml/session')->addSuccess(
211
+ Mage::helper('budgetmailer')
212
+ ->__('Contact was successfully deleted.')
213
+ );
214
+
215
+ $this->_redirect('*/*/');
216
+
217
+ return;
218
+ } catch (Mage_Core_Exception $e) {
219
+ Mage::getSingleton('adminhtml/session')->addError(
220
+ $e->getMessage()
221
+ );
222
+ $this->_redirect(
223
+ '*/*/edit',
224
+ array('id' => $this->getRequest()->getParam('id'))
225
+ );
226
+ } catch (Exception $e) {
227
+ Mage::getSingleton('adminhtml/session')
228
+ ->addError(
229
+ Mage::helper('budgetmailer')
230
+ ->__('There was an error deleting contact.')
231
+ );
232
+ $this->_redirect(
233
+ '*/*/edit',
234
+ array('id' => $this->getRequest()->getParam('id'))
235
+ );
236
+ Mage::logException($e);
237
+
238
+ return;
239
+ }
240
+ }
241
+
242
+ Mage::getSingleton('adminhtml/session')->addError(
243
+ Mage::helper('budgetmailer')
244
+ ->__('Could not find contact to delete.')
245
+ );
246
+
247
+ $this->_redirect('*/*/');
248
+ }
249
+
250
+ /**
251
+ * Delete multiple contacts as mass action
252
+ *
253
+ * @return null
254
+ */
255
+ public function massDeleteAction()
256
+ {
257
+ $contactIds = $this->getRequest()->getParam('contact');
258
+
259
+ if (!is_array($contactIds)) {
260
+ Mage::getSingleton('adminhtml/session')
261
+ ->addError(
262
+ Mage::helper('budgetmailer')
263
+ ->__('Please select contacts to delete.')
264
+ );
265
+ } else {
266
+ try {
267
+ foreach ($contactIds as $contactId) {
268
+ $contact = Mage::getModel('budgetmailer/contact');
269
+
270
+ try {
271
+ $contact->setId($contactId)->delete();
272
+ } catch(Professio_BudgetMailer_Exception $e) {
273
+ Mage::getSingleton('adminhtml/session')->addError(
274
+ Mage::helper('budgetmailer')
275
+ ->__(
276
+ 'There was an error deleting contacts '
277
+ . 'from BudgetMailer API.'
278
+ )
279
+ );
280
+ Mage::logException($e);
281
+ }
282
+ }
283
+
284
+ Mage::getSingleton('adminhtml/session')->addSuccess(
285
+ Mage::helper('budgetmailer')
286
+ ->__(
287
+ 'Total of %d contacts were successfully deleted.',
288
+ count($contactIds)
289
+ )
290
+ );
291
+ } catch (Mage_Core_Exception $e) {
292
+ Mage::getSingleton('adminhtml/session')
293
+ ->addError($e->getMessage());
294
+ } catch (Exception $e) {
295
+ Mage::getSingleton('adminhtml/session')
296
+ ->addError(
297
+ Mage::helper('budgetmailer')
298
+ ->__('There was an error deleting contacts.')
299
+ );
300
+ Mage::logException($e);
301
+ }
302
+ }
303
+
304
+ $this->_redirect('*/*/index');
305
+ }
306
+
307
+ /**
308
+ * Not used...
309
+ */
310
+ public function massStatusAction()
311
+ {
312
+ $contactIds = $this->getRequest()->getParam('contact');
313
+
314
+ if (!is_array($contactIds)) {
315
+ Mage::getSingleton('adminhtml/session')
316
+ ->addError(
317
+ Mage::helper('budgetmailer')
318
+ ->__('Please select contacts.')
319
+ );
320
+ } else {
321
+ try {
322
+ foreach ($contactIds as $contactId) {
323
+ $contact = Mage::getSingleton('budgetmailer/contact');
324
+ $contact->load($contactId)
325
+ ->setStatus($this->getRequest()->getParam('status'))
326
+ ->setIsMassupdate(true)
327
+ ->save();
328
+ }
329
+
330
+ $this->_getSession()->addSuccess(
331
+ $this->__(
332
+ 'Total of %d contacts were successfully updated.',
333
+ count($contactIds)
334
+ )
335
+ );
336
+ } catch (Mage_Core_Exception $e) {
337
+ Mage::getSingleton('adminhtml/session')
338
+ ->addError($e->getMessage());
339
+ } catch (Exception $e) {
340
+ Mage::getSingleton('adminhtml/session')
341
+ ->addError(
342
+ Mage::helper('budgetmailer')
343
+ ->__('There was an error updating contacts.')
344
+ );
345
+ Mage::logException($e);
346
+ }
347
+ }
348
+
349
+ $this->_redirect('*/*/index');
350
+ }
351
+
352
+ /**
353
+ * Mass unsubscribe contacts
354
+ *
355
+ * @return null
356
+ */
357
+ public function massUnsubscribedAction()
358
+ {
359
+ $contactIds = $this->getRequest()->getParam('contact');
360
+
361
+ if (!is_array($contactIds)) {
362
+ Mage::getSingleton('adminhtml/session')
363
+ ->addError(
364
+ Mage::helper('budgetmailer')
365
+ ->__('Please select contacts.')
366
+ );
367
+ } else {
368
+ try {
369
+ foreach ($contactIds as $contactId) {
370
+ $subscribe = !$this->getRequest()
371
+ ->getParam('flag_unsubscribed');
372
+
373
+ $contact = Mage::getSingleton('budgetmailer/contact');
374
+ $contact->load($contactId);
375
+
376
+ $contact->setUnsubscribed(!$subscribe);
377
+ $contact->setSubscribe($subscribe);
378
+
379
+ $contact->setIsMassupdate(true);
380
+ $contact->save();
381
+ }
382
+
383
+ $this->_getSession()->addSuccess(
384
+ $this->__(
385
+ 'Total of %d contacts were successfully updated.',
386
+ count($contactIds)
387
+ )
388
+ );
389
+ } catch (Mage_Core_Exception $e) {
390
+ Mage::getSingleton('adminhtml/session')
391
+ ->addError($e->getMessage());
392
+ } catch (Exception $e) {
393
+ Mage::getSingleton('adminhtml/session')
394
+ ->addError(
395
+ Mage::helper('budgetmailer')
396
+ ->__('There was an error updating contacts.')
397
+ );
398
+ Mage::logException($e);
399
+ }
400
+ }
401
+
402
+ $this->_redirect('*/*/index');
403
+ }
404
+
405
+ /**
406
+ * Not used
407
+ */
408
+ public function massListIdAction()
409
+ {
410
+ $contactIds = $this->getRequest()->getParam('contact');
411
+
412
+ if (!is_array($contactIds)) {
413
+ Mage::getSingleton('adminhtml/session')
414
+ ->addError(
415
+ Mage::helper('budgetmailer')
416
+ ->__('Please select contacts.')
417
+ );
418
+ } else {
419
+ try {
420
+ foreach ($contactIds as $contactId) {
421
+ $contact = Mage::getSingleton('budgetmailer/contact');
422
+ $contact->load($contactId)
423
+ ->setListId(
424
+ $this->getRequest()->getParam('flag_list_id')
425
+ )
426
+ ->setIsMassupdate(true)
427
+ ->save();
428
+ }
429
+
430
+ $this->_getSession()
431
+ ->addSuccess(
432
+ $this->__(
433
+ 'Total of %d contacts were successfully updated.',
434
+ count($contactIds)
435
+ )
436
+ );
437
+ } catch (Mage_Core_Exception $e) {
438
+ Mage::getSingleton('adminhtml/session')
439
+ ->addError($e->getMessage());
440
+ } catch (Exception $e) {
441
+ Mage::getSingleton('adminhtml/session')
442
+ ->addError(
443
+ Mage::helper('budgetmailer')
444
+ ->__('There was an error updating contacts.')
445
+ );
446
+ Mage::logException($e);
447
+ }
448
+ }
449
+
450
+ $this->_redirect('*/*/index');
451
+ }
452
+
453
+ /**
454
+ * Not used
455
+ */
456
+ public function exportCsvAction()
457
+ {
458
+ $fileName = 'contact.csv';
459
+ $content = $this->getLayout()
460
+ ->createBlock('budgetmailer/adminhtml_contact_grid')
461
+ ->getCsv();
462
+ $this->_prepareDownloadResponse($fileName, $content);
463
+ }
464
+
465
+ /**
466
+ * Not used
467
+ */
468
+ public function exportExcelAction()
469
+ {
470
+ $fileName = 'contact.xls';
471
+ $content = $this->getLayout()
472
+ ->createBlock('budgetmailer/adminhtml_contact_grid')
473
+ ->getExcelFile();
474
+ $this->_prepareDownloadResponse($fileName, $content);
475
+ }
476
+
477
+ /**
478
+ * Not used
479
+ */
480
+ public function exportXmlAction()
481
+ {
482
+ $fileName = 'contact.xml';
483
+ $content = $this->getLayout()
484
+ ->createBlock('budgetmailer/adminhtml_contact_grid')
485
+ ->getXml();
486
+ $this->_prepareDownloadResponse($fileName, $content);
487
+ }
488
+
489
+ /**
490
+ * Not used
491
+ */
492
+ protected function _isAllowed()
493
+ {
494
+ return Mage::getSingleton('admin/session')
495
+ ->isAllowed('budgetmailer/contact');
496
+ }
497
+ }
app/code/community/Professio/BudgetMailer/controllers/Adminhtml/BudgetmailerController.php ADDED
@@ -0,0 +1,254 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Professio_BudgetMailer extension
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the MIT License
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/mit-license.php
11
+ *
12
+ * @category Professio
13
+ * @package Professio_BudgetMailer
14
+ * @copyright Copyright (c) 2015
15
+ * @license http://opensource.org/licenses/mit-license.php MIT License
16
+ */
17
+
18
+ /**
19
+ * Back-end controller for customer and newsletter subscriber mass actions
20
+ *
21
+ * @category Professio
22
+ * @package Professio_BudgetMailer
23
+ */
24
+ class Professio_BudgetMailer_Adminhtml_BudgetmailerController
25
+ extends Mage_Adminhtml_Controller_Action
26
+ {
27
+ /**
28
+ * Check the permission to run it
29
+ *
30
+ * @return boolean
31
+ */
32
+ protected function _isAllowed()
33
+ {
34
+ return Mage::getSingleton('admin/session')
35
+ ->isAllowed('budgetmailer/manage');
36
+ }
37
+
38
+ /**
39
+ * Get importer model
40
+ * @return Professio_BudgetMailer_Model_Importer
41
+ */
42
+ protected function getImporter()
43
+ {
44
+ return Mage::getSingleton('budgetmailer/importer');
45
+ }
46
+
47
+ /**
48
+ * Get exporter model
49
+ * @return Professio_BudgetMailer_Model_Exporter
50
+ */
51
+ protected function getExporter()
52
+ {
53
+ return Mage::getSingleton('budgetmailer/exporter');
54
+ }
55
+
56
+ /**
57
+ * Get session
58
+ * @return Mage_Adminhtml_Model_Session
59
+ */
60
+ protected function getSession()
61
+ {
62
+ return Mage::getSingleton('adminhtml/session');
63
+ }
64
+
65
+ /**
66
+ * Delete orphans action
67
+ */
68
+ public function deleteorphansAction()
69
+ {
70
+ try {
71
+ $rs = $this->getImporter()->deleteOrphans();
72
+ $this->getSession()->addSuccess(
73
+ sprintf(
74
+ $this->__(
75
+ 'Delete orphan contacts finished (completed: %d, '
76
+ . 'deleted: %d, skipped: %d).'
77
+ ),
78
+ $rs['completed'], $rs['deleted'], $rs['skipped']
79
+ )
80
+ );
81
+ } catch (Exception $e) {
82
+ $this->getSession()->addError(
83
+ $this->__('Deleting of orphan contacts failed.')
84
+ );
85
+ Mage::logException($e);
86
+ }
87
+
88
+ $this->_redirect('*/budgetmailer_contact/index');
89
+ }
90
+
91
+ /**
92
+ * Export customers action
93
+ */
94
+ public function exportcustomersAction()
95
+ {
96
+ try {
97
+ $rs = $this->getExporter()->exportCustomers();
98
+ $this->getSession()->addSuccess(
99
+ sprintf(
100
+ $this->__(
101
+ 'Customers import finished (completed: %d, fail: %d, '
102
+ . 'success: %d).'
103
+ ),
104
+ $rs['total'], $rs['fail'], $rs['success']
105
+ )
106
+ );
107
+ } catch (Exception $e) {
108
+ $this->getSession()
109
+ ->addError($this->__('Customers import failed.'));
110
+ Mage::logException($e);
111
+ }
112
+
113
+ $this->_redirect('*/budgetmailer_contact/index');
114
+ }
115
+
116
+ /**
117
+ * Export subscribers action
118
+ */
119
+ public function exportsubscribersAction()
120
+ {
121
+ try {
122
+ $rs = $this->getExporter()->exportSubscribers();
123
+ $this->getSession()
124
+ ->addSuccess(
125
+ sprintf(
126
+ $this->__(
127
+ 'Newsletter Subscribers import finished '
128
+ . '(completed: %d, fail: %d, success: %d).'
129
+ ),
130
+ $rs['total'], $rs['fail'], $rs['success']
131
+ )
132
+ );
133
+ } catch (Exception $e) {
134
+ $this->getSession()
135
+ ->addError($this->__('Newsletter subscribers import failed.'));
136
+ Mage::logException($e);
137
+ }
138
+
139
+ $this->_redirect('*/budgetmailer_contact/index');
140
+ }
141
+
142
+ /**
143
+ * Import budgetmailer contacts action
144
+ */
145
+ public function importbudgetmailerAction()
146
+ {
147
+ try {
148
+ $rs = $this->getImporter()->importContacts();
149
+ $this->getSession()
150
+ ->addSuccess(
151
+ sprintf(
152
+ $this->__(
153
+ 'BudgetMailer contacts import finished (completed: '
154
+ . '%d, failed: %d).'
155
+ ),
156
+ $rs['completed'], $rs['failed']
157
+ )
158
+ );
159
+ } catch (Exception $e) {
160
+ $this->getSession()
161
+ ->addError($this->__('BudgetMailer contacts import failed.'));
162
+ Mage::logException($e);
163
+ }
164
+
165
+ $this->_redirect('*/budgetmailer_contact/index');
166
+ }
167
+
168
+ /**
169
+ * Mas subscribe customers or subscribers
170
+ */
171
+ public function masssubscribeAction()
172
+ {
173
+ try {
174
+ if ($this->getRequest()->getParam('customer')) {
175
+ $redirect = '*/customer/index';
176
+ $rs = $this->getExporter()->massSubscribeCustomers(
177
+ $this->getRequest()->getParam('customer'), true
178
+ );
179
+ $message = Mage::helper('budgetmailer')->__(
180
+ sprintf(
181
+ 'Subscribed %d customer(s) (fail: %d, success: %d).',
182
+ $rs['total'], $rs['fail'], $rs['success']
183
+ )
184
+ );
185
+ } elseif ($this->getRequest()->getParam('subscriber')) {
186
+ $redirect = '*/newsletter_subscriber/index';
187
+ $rs = $this->getExporter()->massSubscribeSubscribers(
188
+ $this->getRequest()->getParam('subscriber'), true
189
+ );
190
+ $message = Mage::helper('budgetmailer')->__(
191
+ sprintf(
192
+ 'Subscribed %d subscriber(s) (fail: %d, success: %d).',
193
+ $rs['total'], $rs['fail'], $rs['success']
194
+ )
195
+ );
196
+ } else {
197
+ $rs = false;
198
+ $message = Mage::helper('adminhtml')
199
+ ->__('Please select some item(s).');
200
+ }
201
+
202
+ if ($rs) {
203
+ Mage::getSingleton('adminhtml/session')->addSuccess($message);
204
+ } else {
205
+ Mage::getSingleton('adminhtml/session')->addError($message);
206
+ }
207
+
208
+ if (isset($redirect)) {
209
+ $this->_redirect($redirect);
210
+ }
211
+ } catch(Exception $e) {
212
+ Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
213
+ }
214
+ }
215
+
216
+ /**
217
+ * Mass unsubscribe customers or subscribers
218
+ */
219
+ public function massunsubscribeAction()
220
+ {
221
+ try {
222
+ if ($this->getRequest()->getParam('customer')) {
223
+ $redirect = '*/customer/index';
224
+ $rs = $this->getExporter()->massSubscribeCustomers(
225
+ $this->getRequest()->getParam('customer'), false
226
+ );
227
+ $message = Mage::helper('budgetmailer')->__(
228
+ sprintf(
229
+ 'Unsubscribed %d customer(s) (fail: %d, success: %d).',
230
+ $rs['total'], $rs['fail'], $rs['success']
231
+ )
232
+ );
233
+ } elseif ($this->getRequest()->getParam('subscriber')) {
234
+ $redirect = '*/newsletter_subscriber/index';
235
+ $rs = $this->getExporter()->massSubscribeSubscribers(
236
+ $this->getRequest()->getParam('subscriber'), false
237
+ );
238
+ $message = Mage::helper('budgetmailer')->__(
239
+ sprintf(
240
+ 'Unsubscribed %d subscriber(s) '
241
+ . '(fail: %d, success: %d).',
242
+ $rs['total'], $rs['fail'], $rs['success']
243
+ )
244
+ );
245
+ } else {
246
+ $rs = false;
247
+ $message = Mage::helper('adminhtml')
248
+ ->__('Please select some item(s).');
249
+ }
250
+ } catch(Exception $e) {
251
+ Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
252
+ }
253
+ }
254
+ }
app/code/community/Professio/BudgetMailer/controllers/ManageController.php ADDED
@@ -0,0 +1,157 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Professio_BudgetMailer extension
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the MIT License
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/mit-license.php
11
+ *
12
+ * @category Professio
13
+ * @package Professio_BudgetMailer
14
+ * @copyright Copyright (c) 2015
15
+ * @license http://opensource.org/licenses/mit-license.php MIT License
16
+ */
17
+
18
+ /**
19
+ * Subscription management controller
20
+ *
21
+ * @category Professio
22
+ * @package Professio_BudgetMailer
23
+ */
24
+ class Professio_BudgetMailer_ManageController
25
+ extends Mage_Core_Controller_Front_Action
26
+ {
27
+ /**
28
+ * Current contact
29
+ *
30
+ * @var Professio_BudgetMailer_Model_Contact
31
+ */
32
+ protected $_contact;
33
+
34
+ /**
35
+ * Don't dispatch if there is no customer session
36
+ */
37
+ public function preDispatch()
38
+ {
39
+ parent::preDispatch();
40
+
41
+ if (!$this->getCustomerSession()->authenticate($this)) {
42
+ $this->setFlag('', 'no-dispatch', true);
43
+ }
44
+ }
45
+
46
+ /**
47
+ * Get current contact
48
+ *
49
+ * @return Professio_BudgetMailer_Model_Contact
50
+ */
51
+ protected function getContact()
52
+ {
53
+ if (!isset($this->_contact)) {
54
+ $this->_contact = Mage::getModel('budgetmailer/contact');
55
+ $this->_contact->loadByCustomer($this->getCustomer());
56
+ }
57
+
58
+ return $this->_contact;
59
+ }
60
+
61
+ /**
62
+ * Get current customer
63
+ *
64
+ * @return Mage_Customer_Model_Customer
65
+ */
66
+ protected function getCustomer()
67
+ {
68
+ return $this->getCustomerSession()->getCustomer();
69
+ }
70
+
71
+ /**
72
+ * Get current customer session
73
+ *
74
+ * @return Mage_Customer_Model_Session
75
+ */
76
+ protected function getCustomerSession()
77
+ {
78
+ return Mage::getSingleton('customer/session');
79
+ }
80
+
81
+ /**
82
+ * Display subscription status
83
+ *
84
+ * @return null
85
+ */
86
+ public function indexAction()
87
+ {
88
+ $this->loadLayout();
89
+
90
+ $this->_initLayoutMessages('customer/session');
91
+ $this->_initLayoutMessages('catalog/session');
92
+
93
+ if ($block = $this->getLayout()->getBlock('budgetmailer_newsletter')) {
94
+ $block->setRefererUrl($this->_getRefererUrl());
95
+ }
96
+
97
+ $this->getLayout()
98
+ ->getBlock('head')
99
+ ->setTitle($this->__('Newsletter Subscription'));
100
+
101
+ $this->renderLayout();
102
+ }
103
+
104
+ /**
105
+ * Save single subscription
106
+ *
107
+ * @return null
108
+ */
109
+ public function saveAction()
110
+ {
111
+ if (!$this->_validateFormKey()) {
112
+ return $this->_redirect('customer/account/');
113
+ }
114
+
115
+ try {
116
+ $subscribe = $this->getRequest()
117
+ ->getParam('budgetmailer_subscribe', false);
118
+
119
+ if (!$this->getContact()->getEntityId()) {
120
+ Mage::helper('budgetmailer/mapper')->customerToModel(
121
+ $this->getCustomer(), $this->getContact()
122
+ );
123
+
124
+ $address = Mage::helper('budgetmailer')
125
+ ->getCustomersPrimaryAddress($this->getCustomer());
126
+
127
+ if ($address && $address->getEntityId()) {
128
+ Mage::helper('budgetmailer/mapper')->addressToModel(
129
+ $address, $this->getContact()
130
+ );
131
+ }
132
+ }
133
+
134
+ $this->getContact()->setUnsubscribed(!$subscribe);
135
+ $this->getContact()->setSubscribe($subscribe);
136
+
137
+ $this->getContact()->save();
138
+
139
+ $this->getCustomerSession()->addSuccess(
140
+ $subscribe ?
141
+ $this->__('The subscription has been saved.') :
142
+ $this->__('The subscription has been removed.')
143
+ );
144
+ } catch (Exception $e) {
145
+ $this->getCustomerSession()
146
+ ->addError(
147
+ $this->__(
148
+ 'An error occurred while saving your subscription.'
149
+ )
150
+ );
151
+
152
+ Mage::logException($e);
153
+ }
154
+
155
+ $this->_redirect('customer/account/');
156
+ }
157
+ }
app/code/community/Professio/BudgetMailer/controllers/SubscriberController.php ADDED
@@ -0,0 +1,188 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Professio_BudgetMailer extension
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the MIT License
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/mit-license.php
11
+ *
12
+ * @category Professio
13
+ * @package Professio_BudgetMailer
14
+ * @copyright Copyright (c) 2015
15
+ * @license http://opensource.org/licenses/mit-license.php MIT License
16
+ */
17
+
18
+ /**
19
+ * Implementation of front-end subscriber
20
+ *
21
+ * @category Professio
22
+ * @package Professio_BudgetMailer
23
+ */
24
+ class Professio_BudgetMailer_SubscriberController
25
+ extends Mage_Core_Controller_Front_Action
26
+ {
27
+ /**
28
+ * Current contact
29
+ * @var Professio_BudgetMailer_Model_Contact
30
+ */
31
+ protected $_contact;
32
+
33
+ /**
34
+ * Get current contact
35
+ *
36
+ * @return Professio_BudgetMailer_Model_Contact
37
+ */
38
+ protected function getContact()
39
+ {
40
+ if (!isset($this->_contact)) {
41
+ $this->_contact = Mage::getModel('budgetmailer/contact');
42
+ // $this->contact->loadByEmail($this->getEmail());
43
+ }
44
+
45
+ return $this->_contact;
46
+ }
47
+
48
+ /**
49
+ * Get current customer
50
+ *
51
+ * @return Mage_Customer_Model_Customer
52
+ */
53
+ protected function getCustomer()
54
+ {
55
+ return $this->getCustomerSession()->getCustomer();
56
+ }
57
+
58
+ /**
59
+ * Get current customer session
60
+ *
61
+ * @return Mage_Customer_Model_Session
62
+ */
63
+ protected function getCustomerSession()
64
+ {
65
+ return Mage::getSingleton('customer/session');
66
+ }
67
+
68
+ /**
69
+ * Get email input from request
70
+ *
71
+ * @return string
72
+ */
73
+ protected function getEmail()
74
+ {
75
+ return (string) $this->getRequest()->getPost('email');
76
+ }
77
+
78
+ /**
79
+ * Get mapper
80
+ *
81
+ * @return Professio_BudgetMailer_Helper_Mapper
82
+ */
83
+ protected function getMapper()
84
+ {
85
+ return Mage::helper('budgetmailer/mapper');
86
+ }
87
+
88
+ /**
89
+ * Get core session
90
+ *
91
+ * @return Mage_Core_Model_Session
92
+ */
93
+ protected function getSession()
94
+ {
95
+ return Mage::getSingleton('core/session');
96
+ }
97
+
98
+ /**
99
+ * Custom log function, logs only in developer mode.
100
+ *
101
+ * @param string $message message to log
102
+ * @return null
103
+ */
104
+ protected function log($message)
105
+ {
106
+ if (Mage::getIsDeveloperMode()) {
107
+ Mage::log($message);
108
+ }
109
+ }
110
+
111
+ /**
112
+ * INFO this doesn't allow the have single customer with multiple emails
113
+ *
114
+ * Handle single subscription
115
+ *
116
+ * @return null
117
+ */
118
+ protected function subscribe()
119
+ {
120
+ $this->log('budgetmailer/subscriber::subscribe() start');
121
+
122
+ if ($this->getCustomer()->getId()) {
123
+ $this->log('budgetmailer/subscriber::subscribe() customer');
124
+
125
+ $this->getContact()->loadByCustomer($this->getCustomer());
126
+
127
+ if (!$this->getContact()->getId()) {
128
+ $this->log(
129
+ 'budgetmailer/subscriber::subscribe() customer no contact'
130
+ );
131
+
132
+ $this->getContact()->setCustomerId(
133
+ $this->getCustomer()->getId()
134
+ );
135
+
136
+ $this->getContact()->setEmail($this->getEmail());
137
+ }
138
+
139
+ $this->getMapper()->customerToModel(
140
+ $this->getCustomer(), $this->getContact()
141
+ );
142
+ } else {
143
+ $this->log('budgetmailer/subscriber::subscribe() no customer');
144
+
145
+ $this->getContact()->loadByEmail($this->getEmail());
146
+
147
+ if (!$this->getContact()->getId()) {
148
+ $this->log(
149
+ 'budgetmailer/subscriber::subscribe() no customer no '
150
+ . 'contact'
151
+ );
152
+
153
+ $this->getContact()->setEmail($this->getEmail());
154
+ }
155
+ }
156
+
157
+ $this->getContact()->setUnsubscribed(false);
158
+ $this->getContact()->setSubscribe(true);
159
+
160
+ $this->getContact()->save();
161
+ }
162
+
163
+ /**
164
+ * Single subscription action
165
+ *
166
+ * @return null
167
+ */
168
+ public function subscribeAction()
169
+ {
170
+ if ($this->getRequest()->isPost()
171
+ && $this->getRequest()->getPost('email')) {
172
+ try {
173
+ $this->subscribe();
174
+ $this->getSession()->addSuccess(
175
+ $this->__('The subscription has been saved.')
176
+ );
177
+ } catch(Exception $e) {
178
+ $this->getSession()->addError(
179
+ $this->__(
180
+ 'An error occurred while saving your subscription.'
181
+ )
182
+ );
183
+ }
184
+ }
185
+
186
+ $this->_redirectReferer();
187
+ }
188
+ }
app/code/community/Professio/BudgetMailer/controllers/WebhookController.php ADDED
@@ -0,0 +1,145 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Professio_BudgetMailer extension
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the MIT License
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/mit-license.php
11
+ *
12
+ * @category Professio
13
+ * @package Professio_BudgetMailer
14
+ * @copyright Copyright (c) 2015
15
+ * @license http://opensource.org/licenses/mit-license.php MIT License
16
+ */
17
+
18
+ /**
19
+ * Implementation of BudgetMailer web hook
20
+ *
21
+ * @category Professio
22
+ * @package Professio_BudgetMailer
23
+ */
24
+ class Professio_BudgetMailer_WebhookController
25
+ extends Mage_Core_Controller_Front_Action
26
+ {
27
+ protected $_headers = array(
28
+ 'APIKEY', 'SALT', 'SIGNATURE'
29
+ );
30
+
31
+ /**
32
+ * Get HTTP headers
33
+ * @return array
34
+ */
35
+ protected function getHeaders()
36
+ {
37
+ $headers = array();
38
+
39
+ foreach ($this->_headers as $k) {
40
+ $headers[$k] = $this->getRequest()->getHeader($k);
41
+ }
42
+
43
+ return $headers;
44
+ }
45
+
46
+ /**
47
+ * Get budgetmailer helper
48
+ * @return Professio_BudgetMailer_Helper_Data
49
+ */
50
+ protected function getHelper()
51
+ {
52
+ return Mage::helper('budgetmailer');
53
+ }
54
+
55
+ /**
56
+ * Index action - listening for webhook calls.
57
+ *
58
+ * @return void
59
+ */
60
+ public function indexAction()
61
+ {
62
+ Mage::log(
63
+ 'budgetmailer/webhook_controller::indexAction() start'
64
+ );
65
+
66
+ try {
67
+ $body = $this->getRequest()->getRawBody();
68
+ $actions = json_decode($body);
69
+ $actions = is_object($actions) && isset($actions->data)
70
+ && is_array($actions->data) && count($actions->data)
71
+ ? $actions->data : array();
72
+ $headers = $this->getHeaders();
73
+
74
+ Mage::log(
75
+ 'budgetmailer/webhook_controller::indexAction() '
76
+ . 'body: ' . $body . ', actions: ' . json_encode($actions)
77
+ . 'headers: ' . json_encode($headers)
78
+ );
79
+
80
+ list($code, $message) = $this->processHook($actions, $headers);
81
+
82
+ $this->getResponse()->setHeader(
83
+ $this->getRequest()->getServer('SERVER_PROTOCOL'),
84
+ $code, true
85
+ );
86
+
87
+ $this->getResponse()->setBody($message);
88
+
89
+ Mage::log(
90
+ 'budgetmailer/webhook_controller::indexAction() message: '
91
+ . $message . ', status: ' . $code
92
+ );
93
+ } catch (Exception $e) {
94
+ Mage::logException($e);
95
+
96
+ $this->getResponse()->setHeader(
97
+ $this->getRequest()->getServer('SERVER_PROTOCOL'),
98
+ 500, true
99
+ );
100
+
101
+ $this->getResponse()->setBody(
102
+ Mage::helper('budgetmailer')
103
+ ->__('Unexpected error:')
104
+ . ' ' . $e->getMessage() . '.'
105
+ );
106
+
107
+ Mage::log(
108
+ 'budgetmailer/webhook_controller::indexAction() exception: '
109
+ . $e->getMessage()
110
+ );
111
+ }
112
+
113
+ Mage::log(
114
+ 'budgetmailer/webhook_controller::indexAction() end'
115
+ );
116
+ }
117
+
118
+ protected function processHook($actions, $headers)
119
+ {
120
+ // INFO HTTPS CHECK
121
+ if (false && !$this->getRequest()->isSecure()) {
122
+ $code = 500;
123
+ $message = $this->getHelper()
124
+ ->__('This URL works only with HTTPS.');
125
+ } else if (
126
+ !Mage::helper('budgetmailer/config')->isSyncWebhookEnabled()) {
127
+ $code = 503;
128
+ $message = $this->getHelper()->__('Webhook disabled.');
129
+ } else if (
130
+ !$this->getHelper()->checkSignature($headers)) {
131
+ $code = 403;
132
+ $message = $this->getHelper()->__('Invalid signature.');
133
+ } else if (!is_array($actions) || !count($actions)) {
134
+ $code = 200;
135
+ $message = $this->getHelper()->__('Nothing to do.');
136
+ } else {
137
+ $code = 200;
138
+ $message = $this->getHelper()->__('OK');
139
+ Mage::getSingleton('budgetmailer/importer')
140
+ ->hook($actions, $headers);
141
+ }
142
+
143
+ return array($code, $message);
144
+ }
145
+ }
app/code/community/Professio/BudgetMailer/etc/adminhtml.xml ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <!--
3
+ /**
4
+ * Professio_BudgetMailer extension
5
+ *
6
+ * NOTICE OF LICENSE
7
+ *
8
+ * This source file is subject to the MIT License
9
+ * that is bundled with this package in the file LICENSE.txt.
10
+ * It is also available through the world-wide-web at this URL:
11
+ * http://opensource.org/licenses/mit-license.php
12
+ *
13
+ * @category Professio
14
+ * @package Professio_BudgetMailer
15
+ * @copyright Copyright (c) 2015
16
+ * @license http://opensource.org/licenses/mit-license.php MIT License
17
+ */
18
+ -->
19
+ <config>
20
+ <acl>
21
+ <resources>
22
+ <admin>
23
+ <children>
24
+ <system>
25
+ <children>
26
+ <config>
27
+ <children>
28
+ <budgetmailer translate="title" module="budgetmailer">
29
+ <title>BudgetMailer</title>
30
+ </budgetmailer>
31
+ </children>
32
+ </config>
33
+ </children>
34
+ </system>
35
+
36
+ <professio_budgetmailer translate="title" module="budgetmailer">
37
+ <title>BudgetMailer</title>
38
+ <children>
39
+ <contact translate="title" module="budgetmailer">
40
+ <title>Contacts</title>
41
+ <sort_order>0</sort_order>
42
+ </contact>
43
+ <!--<list translate="title" module="budgetmailer">
44
+ <title>List</title>
45
+ <sort_order>10</sort_order>
46
+ </list>-->
47
+ <massactions translate="title" module="budgetmailer">
48
+ <title>Mass Actions</title>
49
+ <sort_order>0</sort_order>
50
+ </massactions>
51
+ </children>
52
+ </professio_budgetmailer>
53
+ </children>
54
+ </admin>
55
+ </resources>
56
+ </acl>
57
+
58
+ <menu>
59
+ <professio_budgetmailer translate="title" module="budgetmailer">
60
+ <title>BudgetMailer</title>
61
+ <sort_order>65</sort_order>
62
+ <children>
63
+ <contact translate="title" module="budgetmailer">
64
+ <title>Contacts</title>
65
+ <action>adminhtml/budgetmailer_contact</action>
66
+ <sort_order>0</sort_order>
67
+ </contact>
68
+ <!--<list translate="title" module="budgetmailer">
69
+ <title>List</title>
70
+ <action>adminhtml/budgetmailer_list</action>
71
+ <sort_order>10</sort_order>
72
+ </list>-->
73
+ </children>
74
+ </professio_budgetmailer>
75
+ </menu>
76
+ </config>
app/code/community/Professio/BudgetMailer/etc/config.xml ADDED
@@ -0,0 +1,258 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <!--
3
+ /**
4
+ * Professio_BudgetMailer extension
5
+ *
6
+ * NOTICE OF LICENSE
7
+ *
8
+ * This source file is subject to the MIT License
9
+ * that is bundled with this package in the file LICENSE.txt.
10
+ * It is also available through the world-wide-web at this URL:
11
+ * http://opensource.org/licenses/mit-license.php
12
+ *
13
+ * @category Professio
14
+ * @package Professio_BudgetMailer
15
+ * @copyright Copyright (c) 2015
16
+ * @license http://opensource.org/licenses/mit-license.php MIT License
17
+ */
18
+ -->
19
+ <config>
20
+
21
+ <admin>
22
+ <routers>
23
+ <adminhtml>
24
+ <args>
25
+ <modules>
26
+ <Professio_BudgetMailer before="Mage_Adminhtml">Professio_BudgetMailer_Adminhtml</Professio_BudgetMailer>
27
+ </modules>
28
+ </args>
29
+ </adminhtml>
30
+ </routers>
31
+ </admin>
32
+
33
+ <adminhtml>
34
+ <events>
35
+ <admin_system_config_changed_section_budgetmailer>
36
+ <observers>
37
+ <admin_system_config_changed_section_budgetmailer>
38
+ <class>budgetmailer/observer</class>
39
+ <method>afterConfigChange</method>
40
+ <type>singleton</type>
41
+ </admin_system_config_changed_section_budgetmailer>
42
+ </observers>
43
+ </admin_system_config_changed_section_budgetmailer>
44
+
45
+ <core_block_abstract_prepare_layout_before>
46
+ <observers>
47
+ <budgetmailer_core_block_abstract_prepare_layout_before>
48
+ <class>budgetmailer/observer</class>
49
+ <method>addMassAction</method>
50
+ <type>singleton</type>
51
+ </budgetmailer_core_block_abstract_prepare_layout_before>
52
+ </observers>
53
+ </core_block_abstract_prepare_layout_before>
54
+
55
+ <customer_save_after>
56
+ <observers>
57
+ <budgetmailer_customer_customer_save_after>
58
+ <class>budgetmailer/observer</class>
59
+ <method>customerSaveAfterAdmin</method>
60
+ <type>singleton</type>
61
+ </budgetmailer_customer_customer_save_after>
62
+ </observers>
63
+ </customer_save_after>
64
+ </events>
65
+
66
+ <layout>
67
+ <updates>
68
+ <budgetmailer>
69
+ <file>budgetmailer.xml</file>
70
+ </budgetmailer>
71
+ </updates>
72
+ </layout>
73
+
74
+ <translate>
75
+ <modules>
76
+ <translations>
77
+ <files>
78
+ <default>Professio_BudgetMailer.csv</default>
79
+ </files>
80
+ </translations>
81
+ </modules>
82
+ </translate>
83
+ </adminhtml>
84
+
85
+ <crontab>
86
+ <jobs>
87
+ <budgetmailer_cron>
88
+ <schedule><cron_expr>0 1 * * *</cron_expr></schedule>
89
+ <run><model>budgetmailer/observer::cron</model></run>
90
+ </budgetmailer_cron>
91
+ </jobs>
92
+ </crontab>
93
+
94
+ <default>
95
+ <budgetmailer>
96
+ <api>
97
+ <endpoint>https://api.budgetmailer.com/</endpoint>
98
+ <key></key>
99
+ <secret></secret>
100
+ </api>
101
+
102
+ <general>
103
+ <list></list>
104
+ </general>
105
+
106
+ <sync>
107
+ <cron>1</cron>
108
+ <webhook>1</webhook>
109
+ <ttl>86400</ttl>
110
+ </sync>
111
+
112
+ <advanced>
113
+ <address_type>billing</address_type>
114
+ <frontend>1</frontend>
115
+ <on_address_delete>use_new</on_address_delete>
116
+ <on_address_update>update</on_address_update>
117
+ <on_customer_delete>ignore</on_customer_delete>
118
+ <on_customer_update>update</on_customer_update>
119
+ <on_order>1</on_order>
120
+ </advanced>
121
+ </budgetmailer>
122
+ </default>
123
+
124
+ <frontend>
125
+ <events>
126
+ <customer_save_after>
127
+ <observers>
128
+ <budgetmailer_customer_customer_save_after>
129
+ <class>budgetmailer/observer</class>
130
+ <method>customerSaveAfterFront</method>
131
+ <type>singleton</type>
132
+ </budgetmailer_customer_customer_save_after>
133
+ </observers>
134
+ </customer_save_after>
135
+
136
+ <sales_order_place_after>
137
+ <observers>
138
+ <budgetmailer_sales_order_place_after>
139
+ <class>budgetmailer/observer</class>
140
+ <method>salesOrderPlaceAfter</method>
141
+ </budgetmailer_sales_order_place_after>
142
+ </observers>
143
+ </sales_order_place_after>
144
+ </events>
145
+
146
+ <layout>
147
+ <updates>
148
+ <budgetmailer>
149
+ <file>budgetmailer.xml</file>
150
+ </budgetmailer>
151
+ </updates>
152
+ </layout>
153
+
154
+ <routers>
155
+ <budgetmailer>
156
+ <use>standard</use>
157
+ <args>
158
+ <module>Professio_BudgetMailer</module>
159
+ <frontName>budgetmailer</frontName>
160
+ </args>
161
+ </budgetmailer>
162
+ </routers>
163
+
164
+ <translate>
165
+ <modules>
166
+ <translations>
167
+ <files>
168
+ <default>Professio_BudgetMailer.csv</default>
169
+ </files>
170
+ </translations>
171
+ </modules>
172
+ </translate>
173
+ </frontend>
174
+
175
+ <global>
176
+ <blocks>
177
+ <budgetmailer>
178
+ <class>Professio_BudgetMailer_Block</class>
179
+ </budgetmailer>
180
+ </blocks>
181
+
182
+ <helpers>
183
+ <budgetmailer>
184
+ <class>Professio_BudgetMailer_Helper</class>
185
+ </budgetmailer>
186
+ </helpers>
187
+
188
+ <events>
189
+ <customer_address_save_after>
190
+ <observers>
191
+ <budgetmailer_customer_customer_save_after>
192
+ <class>budgetmailer/observer</class>
193
+ <method>addressSaveAfter</method>
194
+ <type>singleton</type>
195
+ </budgetmailer_customer_customer_save_after>
196
+ </observers>
197
+ </customer_address_save_after>
198
+
199
+ <customer_address_delete_after>
200
+ <observers>
201
+ <budgetmailer_customer_customer_save_after>
202
+ <class>budgetmailer/observer</class>
203
+ <method>addressDeleteAfter</method>
204
+ <type>singleton</type>
205
+ </budgetmailer_customer_customer_save_after>
206
+ </observers>
207
+ </customer_address_delete_after>
208
+
209
+ <customer_delete_after>
210
+ <observers>
211
+ <budgetmailer_customer_customer_delete_after>
212
+ <class>budgetmailer/observer</class>
213
+ <method>customerDeleteAfter</method>
214
+ <type>singleton</type>
215
+ </budgetmailer_customer_customer_delete_after>
216
+ </observers>
217
+ </customer_delete_after>
218
+ </events>
219
+
220
+ <models>
221
+ <budgetmailer>
222
+ <class>Professio_BudgetMailer_Model</class>
223
+ <resourceModel>budgetmailer_resource</resourceModel>
224
+ </budgetmailer>
225
+ <budgetmailer>
226
+ <class>Professio_BudgetMailer_Model</class>
227
+ <resourceModel>budgetmailer_resource</resourceModel>
228
+ </budgetmailer>
229
+ <budgetmailer_resource>
230
+ <class>Professio_BudgetMailer_Model_Resource</class>
231
+ <entities>
232
+ <contact>
233
+ <table>budgetmailer_contact</table>
234
+ </contact>
235
+ <list>
236
+ <table>budgetmailer_list</table>
237
+ </list>
238
+ </entities>
239
+ </budgetmailer_resource>
240
+ </models>
241
+
242
+ <resources>
243
+ <budgetmailer_setup>
244
+ <setup>
245
+ <module>Professio_BudgetMailer</module>
246
+ <class>Professio_BudgetMailer_Model_Resource_Setup</class>
247
+ </setup>
248
+ </budgetmailer_setup>
249
+ </resources>
250
+ </global>
251
+
252
+ <modules>
253
+ <Professio_BudgetMailer>
254
+ <version>1.0.0</version>
255
+ </Professio_BudgetMailer>
256
+ </modules>
257
+
258
+ </config>
app/code/community/Professio/BudgetMailer/etc/system.xml ADDED
@@ -0,0 +1,223 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <!--
3
+ /**
4
+ * Professio_BudgetMailer extension
5
+ *
6
+ * NOTICE OF LICENSE
7
+ *
8
+ * This source file is subject to the MIT License
9
+ * that is bundled with this package in the file LICENSE.txt.
10
+ * It is also available through the world-wide-web at this URL:
11
+ * http://opensource.org/licenses/mit-license.php
12
+ *
13
+ * @category Professio
14
+ * @package Professio_BudgetMailer
15
+ * @copyright Copyright (c) 2015
16
+ * @license http://opensource.org/licenses/mit-license.php MIT License
17
+ */
18
+ -->
19
+ <config>
20
+ <sections>
21
+ <budgetmailer translate="label" module="budgetmailer">
22
+ <label>BudgetMailer</label>
23
+ <tab>customer</tab>
24
+ <frontend_type>text</frontend_type>
25
+ <sort_order>999</sort_order>
26
+ <show_in_default>1</show_in_default>
27
+ <show_in_website>1</show_in_website>
28
+ <show_in_store>1</show_in_store>
29
+
30
+ <groups>
31
+ <api translate="label" module="budgetmailer">
32
+ <label>API</label>
33
+ <frontend_type>text</frontend_type>
34
+ <sort_order>10</sort_order>
35
+ <show_in_default>1</show_in_default>
36
+ <show_in_website>1</show_in_website>
37
+ <show_in_store>1</show_in_store>
38
+ <fields>
39
+ <endpoint translate="label comment" module="budgetmailer">
40
+ <label>API Endpoint</label>
41
+ <frontend_type>text</frontend_type>
42
+ <sort_order>10</sort_order>
43
+ <show_in_default>1</show_in_default>
44
+ <show_in_website>1</show_in_website>
45
+ <show_in_store>1</show_in_store>
46
+ <comment>BudgetMailer API Endpoint. Please don't change the URL unless you are instructed to.</comment>
47
+ </endpoint>
48
+
49
+ <key translate="label comment" module="budgetmailer">
50
+ <label>API Key</label>
51
+ <frontend_type>text</frontend_type>
52
+ <sort_order>20</sort_order>
53
+ <show_in_default>1</show_in_default>
54
+ <show_in_website>1</show_in_website>
55
+ <show_in_store>1</show_in_store>
56
+ <comment>Insert your BudgetMailer API Key.</comment>
57
+ </key>
58
+
59
+ <secret translate="label comment" module="budgetmailer">
60
+ <label>API Secret</label>
61
+ <frontend_type>text</frontend_type>
62
+ <sort_order>30</sort_order>
63
+ <show_in_default>1</show_in_default>
64
+ <show_in_website>1</show_in_website>
65
+ <show_in_store>1</show_in_store>
66
+ <comment>Insert your BudgetMailer API Secret. </comment>
67
+ </secret>
68
+ </fields>
69
+ </api>
70
+
71
+ <general translate="label" module="budgetmailer">
72
+ <label>General</label>
73
+ <frontend_type>text</frontend_type>
74
+ <sort_order>20</sort_order>
75
+ <show_in_default>1</show_in_default>
76
+ <show_in_website>1</show_in_website>
77
+ <show_in_store>1</show_in_store>
78
+ <fields>
79
+ <list translate="label comment" module="budgetmailer">
80
+ <label>List</label>
81
+ <frontend_type>select</frontend_type>
82
+ <source_model>budgetmailer/config_source_list</source_model>
83
+ <sort_order>10</sort_order>
84
+ <show_in_default>1</show_in_default>
85
+ <show_in_website>1</show_in_website>
86
+ <show_in_store>1</show_in_store>
87
+ <comment>Please select the BudgetMailer List you want to use with Magento.</comment>
88
+ </list>
89
+ </fields>
90
+ </general>
91
+
92
+ <sync translate="label" module="budgetmailer">
93
+ <label>Synchronization</label>
94
+ <frontend_type>text</frontend_type>
95
+ <sort_order>30</sort_order>
96
+ <show_in_default>1</show_in_default>
97
+ <show_in_website>1</show_in_website>
98
+ <show_in_store>1</show_in_store>
99
+ <fields>
100
+ <cron translate="label comment" module="budgetmailer">
101
+ <label>Use Cron Job</label>
102
+ <frontend_type>select</frontend_type>
103
+ <source_model>adminhtml/system_config_source_yesno</source_model>
104
+ <sort_order>10</sort_order>
105
+ <show_in_default>1</show_in_default>
106
+ <show_in_website>1</show_in_website>
107
+ <show_in_store>1</show_in_store>
108
+ <comment>In this mode, data will be synchronized periodically, every day at 1 a.m.</comment>
109
+ </cron>
110
+
111
+ <webhook translate="label comment" module="budgetmailer">
112
+ <label>Use webhook</label>
113
+ <frontend_type>select</frontend_type>
114
+ <source_model>adminhtml/system_config_source_yesno</source_model>
115
+ <sort_order>20</sort_order>
116
+ <show_in_default>1</show_in_default>
117
+ <show_in_website>1</show_in_website>
118
+ <show_in_store>1</show_in_store>
119
+ <comment>In this mode, BudgetMailer Contacts will be synchronized by webhook in a real time. However this requires having SSL certificate installed, and Magento configured to use HTTPS protocol.</comment>
120
+ </webhook>
121
+
122
+ <ttl>
123
+ <label>Local data TTL</label>
124
+ <frontend_type>text</frontend_type>
125
+ <sort_order>30</sort_order>
126
+ <show_in_default>1</show_in_default>
127
+ <show_in_website>1</show_in_website>
128
+ <show_in_store>1</show_in_store>
129
+ <comment>Time to live of local contacts data, for both modes. Please do not change this value, unless you are understand how this works.</comment>
130
+ </ttl>
131
+ </fields>
132
+ </sync>
133
+
134
+ <advanced translate="label" module="budgetmailer">
135
+ <label>Advanced</label>
136
+ <frontend_type>text</frontend_type>
137
+ <sort_order>40</sort_order>
138
+ <show_in_default>1</show_in_default>
139
+ <show_in_website>1</show_in_website>
140
+ <show_in_store>1</show_in_store>
141
+ <fields>
142
+ <frontend translate="label comment" module="budgetmailer">
143
+ <label>Enable front-end output</label>
144
+ <frontend_type>select</frontend_type>
145
+ <source_model>adminhtml/system_config_source_yesno</source_model>
146
+ <sort_order>10</sort_order>
147
+ <show_in_default>1</show_in_default>
148
+ <show_in_website>1</show_in_website>
149
+ <show_in_store>1</show_in_store>
150
+ <comment>If disabled, this module, won't have any front-end output.</comment>
151
+ </frontend>
152
+
153
+ <address_type translate="label comment" module="budgetmailer">
154
+ <label>Use Address</label>
155
+ <frontend_type>select</frontend_type>
156
+ <source_model>budgetmailer/config_source_address</source_model>
157
+ <sort_order>20</sort_order>
158
+ <show_in_default>1</show_in_default>
159
+ <show_in_website>1</show_in_website>
160
+ <show_in_store>1</show_in_store>
161
+ <comment>Select address that will be synchronized.</comment>
162
+ </address_type>
163
+
164
+ <on_address_delete translate="label comment" module="budgetmailer">
165
+ <label>On Primary Address Delete</label>
166
+ <frontend_type>select</frontend_type>
167
+ <source_model>budgetmailer/config_source_delete_address</source_model>
168
+ <sort_order>30</sort_order>
169
+ <show_in_default>1</show_in_default>
170
+ <show_in_website>1</show_in_website>
171
+ <show_in_store>1</show_in_store>
172
+ <comment>Select address update behaviour.</comment>
173
+ </on_address_delete>
174
+
175
+ <on_address_update translate="label comment" module="budgetmailer">
176
+ <label>On Primary Address Update</label>
177
+ <frontend_type>select</frontend_type>
178
+ <source_model>budgetmailer/config_source_update</source_model>
179
+ <sort_order>40</sort_order>
180
+ <show_in_default>1</show_in_default>
181
+ <show_in_website>1</show_in_website>
182
+ <show_in_store>1</show_in_store>
183
+ <comment>Select address update behaviour.</comment>
184
+ </on_address_update>
185
+
186
+ <on_customer_delete translate="label comment" module="budgetmailer">
187
+ <label>On Customer Delete</label>
188
+ <frontend_type>select</frontend_type>
189
+ <source_model>budgetmailer/config_source_delete</source_model>
190
+ <sort_order>50</sort_order>
191
+ <show_in_default>1</show_in_default>
192
+ <show_in_website>1</show_in_website>
193
+ <show_in_store>1</show_in_store>
194
+ <comment>Select customer delete behaviour.</comment>
195
+ </on_customer_delete>
196
+
197
+ <on_customer_update translate="label comment" module="budgetmailer">
198
+ <label>On Customer Update</label>
199
+ <frontend_type>select</frontend_type>
200
+ <source_model>budgetmailer/config_source_update</source_model>
201
+ <sort_order>60</sort_order>
202
+ <show_in_default>1</show_in_default>
203
+ <show_in_website>1</show_in_website>
204
+ <show_in_store>1</show_in_store>
205
+ <comment>Select customer update behaviour.</comment>
206
+ </on_customer_update>
207
+
208
+ <on_order translate="label comment" module="budgetmailer">
209
+ <label>Tag Customers after Order</label>
210
+ <frontend_type>select</frontend_type>
211
+ <source_model>adminhtml/system_config_source_yesno</source_model>
212
+ <sort_order>70</sort_order>
213
+ <show_in_default>1</show_in_default>
214
+ <show_in_website>1</show_in_website>
215
+ <show_in_store>1</show_in_store>
216
+ <comment>If enabled, contact will be tagged by category names of ordered products.</comment>
217
+ </on_order>
218
+ </fields>
219
+ </advanced>
220
+ </groups>
221
+ </budgetmailer>
222
+ </sections>
223
+ </config>
app/code/community/Professio/BudgetMailer/sql/budgetmailer_setup/install-1.0.0.php ADDED
@@ -0,0 +1,236 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Professio_BudgetMailer extension
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the MIT License
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/mit-license.php
11
+ *
12
+ * @category Professio
13
+ * @package Professio_BudgetMailer
14
+ * @copyright Copyright (c) 2015
15
+ * @license http://opensource.org/licenses/mit-license.php MIT License
16
+ */
17
+
18
+ if (Mage::getIsDeveloperMode()) {
19
+ Mage::log('budgetmailer_setup started');
20
+ }
21
+
22
+ $this->startSetup();
23
+
24
+ $table = $this->getConnection()
25
+ ->newTable($this->getTable('budgetmailer/contact'))
26
+
27
+ ->addColumn(
28
+ 'entity_id',
29
+ Varien_Db_Ddl_Table::TYPE_INTEGER,
30
+ null,
31
+ array(
32
+ 'identity' => true,
33
+ 'nullable' => false,
34
+ 'primary' => true,
35
+ ),
36
+ 'Contact ID'
37
+ )
38
+
39
+ ->addColumn(
40
+ 'customer_id', Varien_Db_Ddl_Table::TYPE_INTEGER, null, array(
41
+ 'unsigned' => true,
42
+ 'nullable' => false,
43
+ ), 'Customer ID'
44
+ )
45
+
46
+ ->addColumn(
47
+ 'list_id', Varien_Db_Ddl_Table::TYPE_INTEGER, null, array(
48
+ 'unsigned' => true,
49
+ 'nullable' => true,
50
+ ), 'List ID'
51
+ )
52
+
53
+ ->addColumn(
54
+ 'budgetmailer_id', Varien_Db_Ddl_Table::TYPE_TEXT, 255, array(
55
+ 'nullable' => true,
56
+ ), 'Budgetmailer ID'
57
+ )
58
+
59
+ ->addColumn(
60
+ 'email', Varien_Db_Ddl_Table::TYPE_TEXT, 255, array(
61
+ 'nullable' => true,
62
+ ), 'Email'
63
+ )
64
+
65
+ ->addColumn(
66
+ 'company_name', Varien_Db_Ddl_Table::TYPE_TEXT, 255, array(
67
+ 'nullable' => true,
68
+ ), 'Company Name'
69
+ )
70
+
71
+ ->addColumn(
72
+ 'first_name', Varien_Db_Ddl_Table::TYPE_TEXT, 255, array(
73
+ 'nullable' => true,
74
+ ), 'First Name'
75
+ )
76
+
77
+ ->addColumn(
78
+ 'insertion', Varien_Db_Ddl_Table::TYPE_TEXT, 255, array(
79
+ 'nullable' => true,
80
+ ), 'Insertion'
81
+ )
82
+
83
+ ->addColumn(
84
+ 'last_name', Varien_Db_Ddl_Table::TYPE_TEXT, 255, array(
85
+ 'nullable' => true,
86
+ ), 'Last Name'
87
+ )
88
+
89
+ ->addColumn(
90
+ 'sex', Varien_Db_Ddl_Table::TYPE_SMALLINT, 255, array(
91
+ 'nullable' => true,
92
+ ), 'Sex'
93
+ )
94
+
95
+ ->addColumn(
96
+ 'address', Varien_Db_Ddl_Table::TYPE_TEXT, 255, array(
97
+ 'nullable' => false,
98
+ ), 'Address'
99
+ )
100
+
101
+ ->addColumn(
102
+ 'postal_code', Varien_Db_Ddl_Table::TYPE_TEXT, 255, array(
103
+ 'nullable' => true,
104
+ ), 'Postal Code'
105
+ )
106
+
107
+ ->addColumn(
108
+ 'city', Varien_Db_Ddl_Table::TYPE_TEXT, 255, array(
109
+ 'nullable' => true,
110
+ ), 'City'
111
+ )
112
+
113
+ ->addColumn(
114
+ 'country_code', Varien_Db_Ddl_Table::TYPE_TEXT, 255, array(
115
+ 'nullable' => true,
116
+ ), 'Country Code'
117
+ )
118
+
119
+ ->addColumn(
120
+ 'telephone', Varien_Db_Ddl_Table::TYPE_TEXT, 255, array(
121
+ 'nullable' => true,
122
+ ), 'Telephone'
123
+ )
124
+
125
+ ->addColumn(
126
+ 'mobile', Varien_Db_Ddl_Table::TYPE_TEXT, 255, array(
127
+ 'nullable' => true,
128
+ ), 'Mobile'
129
+ )
130
+
131
+ ->addColumn(
132
+ 'remarks', Varien_Db_Ddl_Table::TYPE_TEXT, '64k', array(
133
+ 'nullable' => true,
134
+ ), 'Remarks'
135
+ )
136
+
137
+ ->addColumn(
138
+ 'tags', Varien_Db_Ddl_Table::TYPE_TEXT, 255, array(
139
+ 'nullable' => true,
140
+ ), 'Tags'
141
+ )
142
+
143
+ ->addColumn(
144
+ 'unsubscribed', Varien_Db_Ddl_Table::TYPE_SMALLINT, null, array(
145
+ 'nullable' => true,
146
+ ), 'Unsubscribed'
147
+ )
148
+
149
+ ->addColumn(
150
+ 'status', Varien_Db_Ddl_Table::TYPE_INTEGER, null, array(),
151
+ 'Contact Status'
152
+ )
153
+
154
+ ->addColumn(
155
+ 'updated_at', Varien_Db_Ddl_Table::TYPE_TIMESTAMP, null, array(),
156
+ 'Contact Modification Time'
157
+ )
158
+
159
+ ->addColumn(
160
+ 'created_at', Varien_Db_Ddl_Table::TYPE_TIMESTAMP, null, array(),
161
+ 'Contact Creation Time'
162
+ )
163
+
164
+ ->addIndex(
165
+ $this->getIdxName('customer/entity', array('customer_id')),
166
+ array('customer_id')
167
+ )
168
+
169
+ ->addIndex(
170
+ $this->getIdxName('budgetmailer/list', array('list_id')),
171
+ array('list_id')
172
+ )
173
+
174
+ ->addIndex(
175
+ $this->getIdxName('budgetmailer/contact', array('email')),
176
+ array('email')
177
+ )
178
+
179
+ ->addIndex(
180
+ $this->getIdxName(
181
+ 'budgetmailer/contact', array('budgetmailer_id')
182
+ ),
183
+ array('budgetmailer_id')
184
+ )
185
+
186
+ ->setComment('Contact Table');
187
+
188
+ $this->getConnection()->createTable($table);
189
+
190
+ $table = $this->getConnection()
191
+ ->newTable($this->getTable('budgetmailer/list'))
192
+
193
+ ->addColumn(
194
+ 'entity_id', Varien_Db_Ddl_Table::TYPE_INTEGER, null, array(
195
+ 'identity' => true,
196
+ 'nullable' => false,
197
+ 'primary' => true,
198
+ ), 'List ID'
199
+ )
200
+
201
+ ->addColumn(
202
+ 'budgetmailer_id', Varien_Db_Ddl_Table::TYPE_TEXT, 255, array(
203
+ 'nullable' => false,
204
+ ), 'Budgetmailer ID'
205
+ )
206
+
207
+ ->addColumn(
208
+ 'name', Varien_Db_Ddl_Table::TYPE_TEXT, 255, array(
209
+ 'nullable' => false,
210
+ ), 'Name'
211
+ )
212
+
213
+ ->addColumn(
214
+ 'status', Varien_Db_Ddl_Table::TYPE_INTEGER, null, array(),
215
+ 'List Status'
216
+ )
217
+
218
+ ->addColumn(
219
+ 'updated_at', Varien_Db_Ddl_Table::TYPE_TIMESTAMP, null, array(),
220
+ 'List Modification Time'
221
+ )
222
+
223
+ ->addColumn(
224
+ 'created_at', Varien_Db_Ddl_Table::TYPE_TIMESTAMP, null, array(),
225
+ 'List Creation Time'
226
+ )
227
+
228
+ ->setComment('List Table');
229
+
230
+ $this->getConnection()->createTable($table);
231
+
232
+ $this->endSetup();
233
+
234
+ if (Mage::getIsDeveloperMode()) {
235
+ Mage::log('budgetmailer_setup ended');
236
+ }
app/code/community/Professio/BudgetMailer/sql/budgetmailer_setup_uninstall.sql ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * Professio_BudgetMailer extension
3
+ *
4
+ * NOTICE OF LICENSE
5
+ *
6
+ * This source file is subject to the MIT License
7
+ * that is bundled with this package in the file LICENSE.txt.
8
+ * It is also available through the world-wide-web at this URL:
9
+ * http://opensource.org/licenses/mit-license.php
10
+ *
11
+ * @category Professio
12
+ * @package Professio_BudgetMailer
13
+ * @copyright Copyright (c) 2015
14
+ * @license http://opensource.org/licenses/mit-license.php MIT License
15
+ */
16
+
17
+ DROP TABLE budgetmailer_contact;
18
+ DROP TABLE budgetmailer_list;
19
+
20
+ DELETE FROM core_config_data WHERE `path` LIKE 'budgetmailer%';
21
+ DELETE FROM core_resource WHERE code = 'budgetmailer_setup';
app/design/adminhtml/default/default/layout/budgetmailer.xml ADDED
@@ -0,0 +1,104 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <!--
3
+ /**
4
+ * Professio_BudgetMailer extension
5
+ *
6
+ * NOTICE OF LICENSE
7
+ *
8
+ * This source file is subject to the MIT License
9
+ * that is bundled with this package in the file LICENSE.txt.
10
+ * It is also available through the world-wide-web at this URL:
11
+ * http://opensource.org/licenses/mit-license.php
12
+ *
13
+ * @category Professio
14
+ * @package Professio_BudgetMailer
15
+ * @copyright Copyright (c) 2015
16
+ * @license http://opensource.org/licenses/mit-license.php MIT License
17
+ */
18
+ -->
19
+ <layout>
20
+
21
+ <adminhtml_budgetmailer_contact_index>
22
+ <reference name="menu">
23
+ <action method="setActive">
24
+ <menupath>professio_budgetmailer/contact</menupath>
25
+ </action>
26
+ </reference>
27
+
28
+ <reference name="content">
29
+ <block type="budgetmailer/adminhtml_contact" name="contact" />
30
+ </reference>
31
+ </adminhtml_budgetmailer_contact_index>
32
+
33
+ <adminhtml_budgetmailer_contact_grid>
34
+ <block type="core/text_list" name="root" output="toHtml">
35
+ <block type="budgetmailer/adminhtml_contact_grid" name="contact_grid"/>
36
+ </block>
37
+ </adminhtml_budgetmailer_contact_grid>
38
+
39
+ <!-- Contact add/edit action -->
40
+ <adminhtml_budgetmailer_contact_edit>
41
+ <update handle="editor"/>
42
+
43
+ <reference name="menu">
44
+ <action method="setActive">
45
+ <menupath>professio_budgetmailer/contact</menupath>
46
+ </action>
47
+ </reference>
48
+
49
+ <reference name="content">
50
+ <block type="budgetmailer/adminhtml_contact_edit" name="contact_edit"></block>
51
+ </reference>
52
+
53
+ <reference name="left">
54
+ <block type="budgetmailer/adminhtml_contact_edit_tabs" name="contact_tabs"></block>
55
+ </reference>
56
+ </adminhtml_budgetmailer_contact_edit>
57
+
58
+ <adminhtml_budgetmailer_list_index>
59
+ <reference name="menu">
60
+ <action method="setActive">
61
+ <menupath>professio_budgetmailer/list</menupath>
62
+ </action>
63
+ </reference>
64
+
65
+ <reference name="content">
66
+ <block type="budgetmailer/adminhtml_list" name="list" />
67
+ </reference>
68
+ </adminhtml_budgetmailer_list_index>
69
+
70
+ <adminhtml_budgetmailer_list_grid>
71
+ <block type="core/text_list" name="root" output="toHtml">
72
+ <block type="budgetmailer/adminhtml_list_grid" name="list_grid"/>
73
+ </block>
74
+ </adminhtml_budgetmailer_list_grid>
75
+
76
+ <!-- List add/edit action -->
77
+ <adminhtml_budgetmailer_list_edit>
78
+ <update handle="editor"/>
79
+
80
+ <reference name="menu">
81
+ <action method="setActive">
82
+ <menupath>professio_budgetmailer/list</menupath>
83
+ </action>
84
+ </reference>
85
+
86
+ <reference name="content">
87
+ <block type="budgetmailer/adminhtml_list_edit" name="list_edit"></block>
88
+ </reference>
89
+
90
+ <reference name="left">
91
+ <block type="budgetmailer/adminhtml_list_edit_tabs" name="list_tabs"></block>
92
+ </reference>
93
+ </adminhtml_budgetmailer_list_edit>
94
+
95
+ <adminhtml_customer_edit>
96
+ <reference name="customer_edit_tabs">
97
+ <action method="addTab">
98
+ <name>budgetmailer</name>
99
+ <block>budgetmailer/adminhtml_customer_tab</block>
100
+ </action>
101
+ </reference>
102
+ </adminhtml_customer_edit>
103
+
104
+ </layout>
app/design/adminhtml/default/default/template/budgetmailer/customer/tab.phtml ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Professio_BudgetMailer extension
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the MIT License
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/mit-license.php
11
+ *
12
+ * @category Professio
13
+ * @package Professio_BudgetMailer
14
+ * @copyright Copyright (c) 2015
15
+ * @license http://opensource.org/licenses/mit-license.php MIT License
16
+ */
17
+
18
+ /**
19
+ * BudgetMailer customer form edit tab
20
+ *
21
+ * @category Professio
22
+ * @package Professio_BudgetMailer
23
+ */
24
+ ?>
25
+ <?php echo $this->getChildHtml('budgetmailer_customer_tab_edit') ?>
app/design/frontend/base/default/layout/budgetmailer.xml ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <!--
3
+ /**
4
+ * Professio_BudgetMailer extension
5
+ *
6
+ * NOTICE OF LICENSE
7
+ *
8
+ * This source file is subject to the MIT License
9
+ * that is bundled with this package in the file LICENSE.txt.
10
+ * It is also available through the world-wide-web at this URL:
11
+ * http://opensource.org/licenses/mit-license.php
12
+ *
13
+ * @category Professio
14
+ * @package Professio_BudgetMailer
15
+ * @copyright Copyright (c) 2015
16
+ * @license http://opensource.org/licenses/mit-license.php MIT License
17
+ */
18
+ -->
19
+ <layout>
20
+
21
+ <default>
22
+ <reference name="left">
23
+ <block type="budgetmailer/subscribe" name="budgetmailer.sidebar" template="budgetmailer/subscribe.phtml"/>
24
+ </reference>
25
+ </default>
26
+
27
+ <customer_account>
28
+ <reference name="customer_account_navigation">
29
+ <action method="addLink" translate="label" module="budgetmailer" ifconfig="budgetmailer/advanced/frontend">
30
+ <name>budgetmailer</name>
31
+ <path>budgetmailer/manage/</path>
32
+ <label>Newsletter Subscriptions</label>
33
+ </action>
34
+ </reference>
35
+ <remove name="budgetmailer.sidebar"/>
36
+ </customer_account>
37
+
38
+ <customer_account_create>
39
+ <reference name="content">
40
+ <block type="budgetmailer/subscribe_mini" name="budgetmailer_subscribe_mini" template="budgetmailer/subscribe/mini.phtml" after="-">
41
+ </block>
42
+ </reference>
43
+ </customer_account_create>
44
+
45
+ <budgetmailer_manage_index translate="label">
46
+ <label>Customer My Account Newsletter Subscriptions</label>
47
+ <update handle="customer_account"/>
48
+ <reference name="my.account.wrapper">
49
+ <block type="budgetmailer/newsletter" name="budgetmailer_newsletter">
50
+ <block type="page/html_wrapper" name="budgetmailer.newsletter.form.before" as="form_before" translate="label">
51
+ <label>Newsletter Subscription Form Before</label>
52
+ <action method="setMayBeInvisible"><value>1</value></action>
53
+ </block>
54
+ </block>
55
+ </reference>
56
+ </budgetmailer_manage_index>
57
+
58
+ </layout>
app/design/frontend/base/default/template/budgetmailer/newsletter.phtml ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Professio_BudgetMailer extension
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the MIT License
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/mit-license.php
11
+ *
12
+ * @category Professio
13
+ * @package Professio_BudgetMailer
14
+ * @copyright Copyright (c) 2015
15
+ * @license http://opensource.org/licenses/mit-license.php MIT License
16
+ */
17
+
18
+ /**
19
+ * Newsletter subscription management
20
+ *
21
+ * @category Professio
22
+ * @package Professio_BudgetMailer
23
+ */
24
+
25
+ if (!$this->getConfigHelper()->isAdvancedFrontendEnabled()) {
26
+ return '';
27
+ }
28
+ ?>
29
+
30
+ <div class="page-title">
31
+ <h1><?php echo $this->__('Newsletter Subscription') ?></h1>
32
+ </div>
33
+
34
+ <?php echo $this->getMessagesBlock()->getGroupedHtml() ?>
35
+ <?php echo $this->getChildHtml('form_before')?>
36
+
37
+ <form action="<?php echo $this->getAction() ?>" method="post"
38
+ id="form-validate">
39
+ <div class="fieldset">
40
+ <?php echo $this->getBlockHtml('formkey')?>
41
+ <h2 class="legend">
42
+ <?php echo $this->__('Newsletter Subscription') ?>
43
+ </h2>
44
+ <label for="budgetmailer-subscribe">
45
+ <?php echo $this->__('Subscribed to newsletter?') ?>
46
+ <input id="budgetmailer-subscribe" name="budgetmailer_subscribe"
47
+ value="1" type="checkbox"
48
+ <?php
49
+ echo $this->isSubscribed() ? ' checked="checked"' : null
50
+ ?>/>
51
+ </label>
52
+ </div>
53
+ <div class="buttons-set">
54
+ <p class="back-link">
55
+ <a href="<?php echo $this->escapeUrl($this->getBackUrl()) ?>">
56
+ <small>&laquo; </small><?php echo $this->__('Back') ?>
57
+ </a>
58
+ </p>
59
+ <button type="submit" title="<?php echo $this->__('Save') ?>"
60
+ class="button">
61
+ <span>
62
+ <span><?php echo $this->__('Save') ?></span>
63
+ </span>
64
+ </button>
65
+ </div>
66
+ </form>
67
+ <?php /* Extensions placeholder */ ?>
68
+ <?php echo $this->getChildHtml('budgetmailer.form.newsletter.extra')?>
69
+ <script type="text/javascript">
70
+ //<![CDATA[
71
+ var dataForm = new VarienForm('form-validate', true);
72
+ //]]>
73
+ </script>
app/design/frontend/base/default/template/budgetmailer/subscribe.phtml ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Professio_BudgetMailer extension
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the MIT License
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/mit-license.php
11
+ *
12
+ * @category Professio
13
+ * @package Professio_BudgetMailer
14
+ * @copyright Copyright (c) 2015
15
+ * @license http://opensource.org/licenses/mit-license.php MIT License
16
+ */
17
+
18
+ /**
19
+ * Subscription form
20
+ *
21
+ * @category Professio
22
+ * @package Professio_BudgetMailer
23
+ */
24
+
25
+ if (!$this->getConfigHelper()->isAdvancedFrontendEnabled()) {
26
+ return '';
27
+ }
28
+ ?>
29
+
30
+ <div class="block block-budgetmailer block-subscribe
31
+ block-budgetmailer-subscribe">
32
+ <div class="block-title">
33
+ <strong><span><?php echo $this->__('Newsletter') ?></span></strong>
34
+ </div>
35
+ <form action="<?php echo $this->getFormActionUrl() ?>" method="post"
36
+ id="budgetmailer-subscribe">
37
+ <div class="block-content">
38
+ <div class="form-subscribe-header">
39
+ <label for="newsletter">
40
+ <?php echo $this->__('Sign Up for Our Newsletter:') ?>
41
+ </label>
42
+ </div>
43
+ <div class="input-box">
44
+ <input type="text" name="email" id="newsletter"
45
+ title="<?php echo $this->__('Sign up for our newsletter') ?>"
46
+ class="input-text required-entry validate-email" />
47
+ </div>
48
+ <div class="actions">
49
+ <button type="submit" title="<?php echo $this->__('Subscribe') ?>"
50
+ class="button">
51
+ <span>
52
+ <span><?php echo $this->__('Subscribe') ?></span>
53
+ </span>
54
+ </button>
55
+ </div>
56
+ </div>
57
+ </form>
58
+ <script type="text/javascript">
59
+ //<![CDATA[
60
+ var newsletterSubscriberFormDetail =
61
+ new VarienForm('budgetmailer-subscribe');
62
+ //]]>
63
+ </script>
64
+ </div>
app/design/frontend/base/default/template/budgetmailer/subscribe/mini.phtml ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Professio_BudgetMailer extension
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the MIT License
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/mit-license.php
11
+ *
12
+ * @category Professio
13
+ * @package Professio_BudgetMailer
14
+ * @copyright Copyright (c) 2015
15
+ * @license http://opensource.org/licenses/mit-license.php MIT License
16
+ */
17
+
18
+ /**
19
+ * Mini subscribe widget
20
+ *
21
+ * @category Professio
22
+ * @package Professio_BudgetMailer
23
+ */
24
+ ?>
25
+ <script type="text/javascript">
26
+ var bm_li = '<li class="control">';
27
+ bm_li += '<div class="input-box">';
28
+ bm_li += '<input type="checkbox" name="bm_is_subscribed" title="<?php echo $this->__('Sign Up for Newsletter') ?>" value="1" id="bm_is_subscribed"<?php if($this->getFormData()->getBmIsSubscribed()): ?> checked="checked"<?php endif; ?> class="checkbox" />';
29
+ bm_li += '</div>';
30
+ bm_li += '<label for="is_subscribed"><?php echo $this->__('Sign Up for Newsletter') ?></label>';
31
+ bm_li += '</li>';
32
+ // XXX add dynamically subscribe button here...
33
+ //#form-validate .form-list ... append li.control etc
34
+
35
+ var bm_list = $$('#form-validate .form-list')[0];
36
+
37
+ $(bm_list).insert(bm_li);
38
+ </script>
app/design/frontend/rwd/default/layout/budgetmailer.xml ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <!--
3
+ /**
4
+ * Professio_BudgetMailer extension
5
+ *
6
+ * NOTICE OF LICENSE
7
+ *
8
+ * This source file is subject to the MIT License
9
+ * that is bundled with this package in the file LICENSE.txt.
10
+ * It is also available through the world-wide-web at this URL:
11
+ * http://opensource.org/licenses/mit-license.php
12
+ *
13
+ * @category Professio
14
+ * @package Professio_BudgetMailer
15
+ * @copyright Copyright (c) 2015
16
+ * @license http://opensource.org/licenses/mit-license.php MIT License
17
+ */
18
+ -->
19
+ <layout>
20
+
21
+ <default>
22
+ <reference name="footer">
23
+ <block type="budgetmailer/subscribe" name="budgetmailer.footer" after="-" template="budgetmailer/subscribe.phtml"/>
24
+ </reference>
25
+ </default>
26
+
27
+ <customer_account>
28
+ <reference name="customer_account_navigation">
29
+ <action method="addLink" translate="label" module="budgetmailer" ifconfig="budgetmailer/advanced/frontend">
30
+ <name>budgetmailer</name>
31
+ <path>budgetmailer/manage/</path>
32
+ <label>Newsletter Subscriptions</label>
33
+ </action>
34
+ </reference>
35
+ <remove name="budgetmailer.sidebar"/>
36
+ </customer_account>
37
+
38
+ <customer_account_create>
39
+ <reference name="content">
40
+ <block type="budgetmailer/subscribe_mini" name="budgetmailer_subscribe_mini" template="budgetmailer/subscribe/mini.phtml" after="-">
41
+ </block>
42
+ </reference>
43
+ </customer_account_create>
44
+
45
+ <budgetmailer_manage_index translate="label">
46
+ <label>Customer My Account Newsletter Subscriptions</label>
47
+ <update handle="customer_account"/>
48
+ <reference name="my.account.wrapper">
49
+ <block type="budgetmailer/newsletter" name="budgetmailer_newsletter">
50
+ <block type="page/html_wrapper" name="budgetmailer.newsletter.form.before" as="form_before" translate="label">
51
+ <label>Newsletter Subscription Form Before</label>
52
+ </block>
53
+ </block>
54
+ </reference>
55
+ </budgetmailer_manage_index>
56
+
57
+ </layout>
app/design/frontend/rwd/default/template/budgetmailer/newsletter.phtml ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Professio_BudgetMailer extension
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the MIT License
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/mit-license.php
11
+ *
12
+ * @category Professio
13
+ * @package Professio_BudgetMailer
14
+ * @copyright Copyright (c) 2015
15
+ * @license http://opensource.org/licenses/mit-license.php MIT License
16
+ */
17
+
18
+ /**
19
+ * Newsletter management
20
+ *
21
+ * @category Professio
22
+ * @package Professio_BudgetMailer
23
+ */
24
+
25
+ if (!$this->getConfigHelper()->isAdvancedFrontendEnabled()) {
26
+ return '';
27
+ }
28
+ ?>
29
+
30
+ <div class="page-title">
31
+ <h1><?php echo $this->__('Newsletter Subscription') ?></h1>
32
+ </div>
33
+ <?php echo $this->getMessagesBlock()->getGroupedHtml() ?>
34
+ <?php echo $this->getChildHtml('form_before') ?>
35
+ <form action="<?php echo $this->getAction() ?>" method="post" id="form-validate">
36
+ <div class="fieldset">
37
+ <?php echo $this->getBlockHtml('formkey') ?>
38
+ <h2 class="legend"><?php echo $this->__('Newsletter Subscription') ?></h2>
39
+ <label for="budgetmailer-subscribe"><?php echo $this->__('Subscribed to newsletter?') ?> <input id="budgetmailer-subscribe" name="budgetmailer_subscribe" value="1" type="checkbox"<?php echo $this->isSubscribed() ? ' checked="checked"' : null ?> /></label>
40
+ </div>
41
+ <div class="buttons-set">
42
+ <p class="back-link"><a href="<?php echo $this->escapeUrl($this->getBackUrl()) ?>"><small>&laquo; </small><?php echo $this->__('Back') ?></a></p>
43
+ <button type="submit" title="<?php echo $this->__('Save') ?>" class="button"><span><span><?php echo $this->__('Save') ?></span></span></button>
44
+ </div>
45
+ </form>
46
+ <?php /* Extensions placeholder */ ?>
47
+ <?php echo $this->getChildHtml('customer.form.newsletter.extra2') ?>
48
+ <script type="text/javascript">
49
+ //<![CDATA[
50
+ var dataForm = new VarienForm('form-validate', true);
51
+ //]]>
52
+ </script>
app/design/frontend/rwd/default/template/budgetmailer/subscribe.phtml ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Professio_BudgetMailer extension
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the MIT License
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/mit-license.php
11
+ *
12
+ * @category Professio
13
+ * @package Professio_BudgetMailer
14
+ * @copyright Copyright (c) 2015
15
+ * @license http://opensource.org/licenses/mit-license.php MIT License
16
+ */
17
+
18
+ /**
19
+ * Subscription form
20
+ *
21
+ * @category Professio
22
+ * @package Professio_BudgetMailer
23
+ */
24
+ if (!$this->getConfigHelper()->isAdvancedFrontendEnabled()) {
25
+ return '';
26
+ }
27
+ ?>
28
+
29
+ <div class="block block-budgetmailer block-subscribe block-budgetmailer-subscribe">
30
+ <div class="block-title">
31
+ <strong><span><?php echo $this->__('Newsletter') ?></span></strong>
32
+ </div>
33
+ <form action="<?php echo $this->getFormActionUrl() ?>" method="post" id="budgetmailer-subscribe">
34
+ <div class="block-content">
35
+ <div class="form-subscribe-header">
36
+ <label for="newsletter"><?php echo $this->__('Sign Up for Our Newsletter:') ?></label>
37
+ </div>
38
+ <div class="input-box">
39
+ <input type="text" name="email" id="newsletter" title="<?php echo $this->__('Sign up for our newsletter') ?>" class="input-text required-entry validate-email" />
40
+ </div>
41
+ <div class="actions">
42
+ <button type="submit" title="<?php echo $this->__('Subscribe') ?>" class="button"><span><span><?php echo $this->__('Subscribe') ?></span></span></button>
43
+ </div>
44
+ </div>
45
+ </form>
46
+ <script type="text/javascript">
47
+ //<![CDATA[
48
+ var newsletterSubscriberFormDetail = new VarienForm('budgetmailer-subscribe');
49
+ //]]>
50
+ </script>
51
+ </div>
app/design/frontend/rwd/default/template/budgetmailer/subscribe/mini.phtml ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Professio_BudgetMailer extension
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the MIT License
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/mit-license.php
11
+ *
12
+ * @category Professio
13
+ * @package Professio_BudgetMailer
14
+ * @copyright Copyright (c) 2015
15
+ * @license http://opensource.org/licenses/mit-license.php MIT License
16
+ */
17
+
18
+ /**
19
+ * Subscription widget
20
+ *
21
+ * @category Professio
22
+ * @package Professio_BudgetMailer
23
+ */
24
+ ?>
25
+ <script type="text/javascript">
26
+ var bm_li = '<li class="control">';
27
+ bm_li += '<div class="input-box">';
28
+ bm_li += '<input type="checkbox" name="bm_is_subscribed" title="<?php echo $this->__('Sign Up for Newsletter') ?>" value="1" id="bm_is_subscribed"<?php if($this->getFormData()->getBmIsSubscribed()): ?> checked="checked"<?php endif; ?> class="checkbox" />';
29
+ bm_li += '</div>';
30
+ bm_li += '<label for="is_subscribed"><?php echo $this->__('Sign Up for Newsletter') ?></label>';
31
+ bm_li += '</li>';
32
+ // XXX add dynamically subscribe button here...
33
+ //#form-validate .form-list ... append li.control etc
34
+
35
+ var bm_list = $$('#form-validate .form-list')[0];
36
+
37
+ $(bm_list).insert(bm_li);
38
+ </script>
app/etc/modules/Professio_BudgetMailer.xml ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <config>
3
+ <modules>
4
+ <Professio_BudgetMailer>
5
+ <active>true</active>
6
+ <codePool>community</codePool>
7
+ <version>1.0.0</version>
8
+ </Professio_BudgetMailer>
9
+ </modules>
10
+ </config>
app/locale/en_US/Professio_BudgetMailer.csv ADDED
@@ -0,0 +1,89 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ "Contact","Contact"
2
+ "Subscribe","Subscribe"
3
+ "Save Contact","Save Contact"
4
+ "Delete Contact","Delete Contact"
5
+ "Add Contact","Add Contact"
6
+ "Delete","Delete"
7
+ "Are you sure?","Are you sure?"
8
+ "Change Unsubscribed","Change Unsubscribed"
9
+ "Unsubscribed","Unsubscribed"
10
+ "Yes","Yes"
11
+ "No","No"
12
+ "Delete Orphans","Delete Orphans"
13
+ "Export Magento Customers","Export Magento Customers"
14
+ "Export Magento Newsletter Subscribers","Export Magento Newsletter Subscribers"
15
+ "Import BudgetMailer Contacts","Import BudgetMailer Contacts"
16
+ "BudgetMailer Newsletter","BudgetMailer Newsletter"
17
+ "List","List"
18
+ "Budgetmailer ID","BudgetMailer ID"
19
+ "Name","Name"
20
+ "Status","Status"
21
+ "Enabled","Enabled"
22
+ "Disabled","Disabled"
23
+ "Save List","Save List"
24
+ "Delete List","Delete List"
25
+ "Save and Continue Edit","Save and Continue Edit"
26
+ "Add List","Add List"
27
+ "Id","Id"
28
+ "Created at","Created at"
29
+ "Updated at","Updated at"
30
+ "Action","Action"
31
+ "Edit","Edit"
32
+ "Change status","Change status"
33
+ "Remove","Remove"
34
+ "Add Tag","Add Tag"
35
+ "No Tags","No Tags"
36
+ "BudgetMailer list is not set.","BudgetMailer list is not set."
37
+ "Couldn't delete contact from BudgetMailer API.","Couldn't delete contact from BudgetMailer API."
38
+ "Couldn't delete tag from contact from BudgetMailer API.","Couldn't delete tag from contact from BudgetMailer API."
39
+ "Couldn't get the contact from BudgetMailer API.","Couldn't get the contact from BudgetMailer API."
40
+ "Couldn't get contacts from BudgetMailer API.","Couldn't get contacts from BudgetMailer API."
41
+ "Couldn't get the lists from BudgetMailer API.","Couldn't get the lists from BudgetMailer API."
42
+ "Couldn't get tags from BudgetMailer API.","Couldn't get tags from BudgetMailer API."
43
+ "Couldn't subscribe contact to BudgetMailer API.","Couldn't subscribe contact to BudgetMailer API."
44
+ "Couldn't bulk subscribe contacts to BudgetMailer API.","Couldn't bulk subscribe contacts to BudgetMailer API."
45
+ "Couldn't post tags to BudgetMailer API.","Couldn't post tags to BudgetMailer API."
46
+ "Couldn't update contact in BudgetMailer API.","Couldn't update contact in BudgetMailer API."
47
+ "Billing Address","Billing Address"
48
+ "Shipping Address","Shipping Address"
49
+ "Use new primary address","Use new primary address"
50
+ "Do nothing","Do nothing"
51
+ "Select List","Select List"
52
+ "Update Contact","Update Contact"
53
+ "Subscribe to a BudgetMailer List","Subscribe to a BudgetMailer List"
54
+ "Unsubscribe from a BudgetMailer List","Unsubscribe from a BudgetMailer List"
55
+ "Invalid API endpoint, key and secret combination.","Invalid API endpoint, key and secret combination."
56
+ "API endpoint, key and secret combination is valid.","API endpoint, key and secret combination is valid."
57
+ "Contacts","Contacts"
58
+ "This contact no longer exists.","This contact no longer exists."
59
+ "There was a problem saving the contact.","There was a problem saving the contact."
60
+ "Unable to find contact to save.","Unable to find contact to save."
61
+ "Contact was successfully deleted.","Contact was successfully deleted."
62
+ "There was an error deleting contact.","There was an error deleting contact."
63
+ "Could not find contact to delete.","Could not find contact to delete."
64
+ "Please select contacts to delete.","Please select contacts to delete."
65
+ "There was an error deleting contacts.","There was an error deleting contacts."
66
+ "Please select contacts.","Please select contacts."
67
+ "There was an error updating contacts.","There was an error updating contacts."
68
+ "Deleting of orphan contacts failed.","Deleting of orphan contacts failed."
69
+ "Customers import failed.","Customers import failed."
70
+ "Newsletter subscribers import failed.","Newsletter subscribers import failed."
71
+ "BudgetMailer contacts import failed.","BudgetMailer contacts import failed."
72
+ "Please select some item(s).","Please select some item(s)."
73
+ "Newsletter Subscription","Newsletter Subscription"
74
+ "The subscription has been saved.","The subscription has been saved."
75
+ "The subscription has been removed.","The subscription has been removed."
76
+ "Unexpected error:","Unexpected error:"
77
+ "This URL works only with HTTPS.","This URL works only with HTTPS."
78
+ "Webhook disabled.","Webhook disabled."
79
+ "Invalid signature.","Invalid signature."
80
+ "Nothing to do.","Nothing to do."
81
+ "OK","OK"
82
+ "Subscribed to newsletter?","Subscribed to newsletter?"
83
+ "Back","Back"
84
+ "Save","Save"
85
+ "Sign Up for Newsletter","Sign Up for Newsletter"
86
+ "Newsletter","Newsletter"
87
+ "Sign Up for Our Newsletter:","Sign Up for Our Newsletter:"
88
+ "Sign up for our newsletter","Sign up for our newsletter"
89
+ "Subscribe","Subscribe"
package.xml ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <package>
3
+ <name>Professio_BudgetMailer</name>
4
+ <version>1.0.0</version>
5
+ <stability>stable</stability>
6
+ <license>MIT</license>
7
+ <channel>community</channel>
8
+ <extends/>
9
+ <summary>Professio BudgetMailer Summary</summary>
10
+ <description>Professio BudgetMailer Description</description>
11
+ <notes>Professio BudgetMailer v 1.0.0 Stable</notes>
12
+ <authors><author><name>J.S. Diertens</name><user>budgetmailer</user><email>info@budgetmailer.nl</email></author></authors>
13
+ <date>2015-07-09</date>
14
+ <time>03:20:27</time>
15
+ <contents><target name="magecommunity"><dir name="Professio"><dir name="BudgetMailer"><dir name="Block"><dir name="Adminhtml"><dir name="Contact"><dir name="Edit"><file name="Form.php" hash="84ffb3e0633e9ff1ce93b3606b82a574"/><dir name="Tab"><file name="Form.php" hash="7d01ee2ed832e1f0d3814402181c9942"/></dir><file name="Tabs.php" hash="469cdd4b84c4733679bbf60f4bd9bbb9"/></dir><file name="Edit.php" hash="a58fbc229ac537be75f85a7e20ce39b2"/><file name="Grid.php" hash="90b904cf0f245800eb611a3e599d0436"/></dir><file name="Contact.php" hash="2627c926ff6a5c79fef8d25a6f45022f"/><dir name="Customer"><file name="Tab.php" hash="11e9f72548a61e33f5012ace80fea2d4"/></dir><dir name="Helper"><dir name="Column"><dir name="Renderer"><file name="Parent.php" hash="5da322afef5caea928d85e90c30c297b"/></dir></dir></dir><dir name="List"><dir name="Edit"><file name="Form.php" hash="fc11a9ee7bd6a3b8bf703569f93f558e"/><dir name="Tab"><file name="Form.php" hash="9391bc1eaa8dd3075317ebcb473d1938"/></dir><file name="Tabs.php" hash="05c4743d5c17319c41f387e8189e6e5c"/></dir><file name="Edit.php" hash="a601e5169eacf2bab6307c56af02727f"/><file name="Grid.php" hash="3cb50bb35c682a35b83f5315946ea898"/></dir><file name="List.php" hash="c805782c8aef4cbce2b8acfacec29119"/></dir><file name="Newsletter.php" hash="b9aec616529485c88bfc895c5ab37f4b"/><dir name="Subscribe"><file name="Mini.php" hash="e4c78ecfca926db483719d1a95daab72"/></dir><file name="Subscribe.php" hash="dd55cee6b835ca4292d3776319947485"/></dir><dir name="Controller"><dir name="Adminhtml"><file name="BudgetMailer.php" hash="419ac9d8824cdcf7b3d5040305d41d9b"/></dir></dir><file name="Exception.php" hash="8d5a5578baa52c32c86aab9552367871"/><dir name="Form"><dir name="Element"><file name="Tags.php" hash="1d8c68499bb5a249246c5a96f437f5af"/></dir></dir><dir name="Helper"><file name="Config.php" hash="8c3cb321e4a7f6a01cb549d43bbe68f6"/><file name="Data.php" hash="f9ecf092f21e9d57574482ed4deb49b5"/><file name="Mapper.php" hash="d5dbe387560465b3d309f2012b788d5d"/></dir><dir name="Model"><file name="Client.php" hash="2235fa30a9d6e9b1cabff4feafc9d910"/><dir name="Config"><dir name="Source"><file name="Address.php" hash="506569472173eb88859a39071a2478d9"/><dir name="Delete"><file name="Address.php" hash="d5fae5c1b2e822d025a00eaeaf6b77e5"/></dir><file name="Delete.php" hash="42c9db5f3a06898dbf9226fe73263c6e"/><file name="List.php" hash="d0ef01f39fe9d92b48ff3c555e7e26d5"/><file name="Update.php" hash="ab92b99a2bce5dc5235b1135b9d00f1e"/></dir></dir><file name="Contact.php" hash="9baea0c242396d155363db1c83e8273b"/><file name="Exporter.php" hash="5d2db19514e91a0afe83089e04f3d390"/><file name="Importer.php" hash="e300c9bc223e6c5fa4f01d2791ed4e08"/><file name="List.php" hash="bd324ba673c963d81de8345242bcc822"/><file name="Observer.php" hash="8347ddce61ad89a13b9b22b694538964"/><dir name="Resource"><dir name="Contact"><file name="Collection.php" hash="33b6a3514437d48b90d6deadd6deb05c"/></dir><file name="Contact.php" hash="0f02959a67178ab33fe903a60f4e2359"/><dir name="List"><file name="Collection.php" hash="db2d92515fe39a8ad0f1ca144550fd7d"/></dir><file name="List.php" hash="12c23fa0395209c68bdf9d273464c957"/><file name="Setup.php" hash="939f3c5bf1c38b36b9de6984befe53ac"/></dir></dir><dir name="controllers"><dir name="Adminhtml"><dir name="Budgetmailer"><file name="ContactController.php" hash="a06a5e48431bcdb81af4e929fd9bfead"/></dir><file name="BudgetmailerController.php" hash="11b0d7fb45e6cfb697d5f5a80ca62351"/></dir><file name="ManageController.php" hash="e70082c900d2b7630752787ade582122"/><file name="SubscriberController.php" hash="b9ebaa98346b04d3f19f4864c8d1ec10"/><file name="WebhookController.php" hash="d7803e2cfa237b35bbf34041571fb3dd"/></dir><dir name="etc"><file name="adminhtml.xml" hash="8f88efcafef92b874f44ffc263d1d4fa"/><file name="config.xml" hash="c279251f8309e5535ce49c256dd7a2fe"/><file name="system.xml" hash="f08411074d810feb04ea73cf4096010a"/></dir><dir name="sql"><dir name="budgetmailer_setup"><file name="install-1.0.0.php" hash="8854ecf4cce961c84b88f4b3c4cfb715"/></dir><file name="budgetmailer_setup_uninstall.sql" hash="ccf0931d13ee3baa903f77003c77c7c9"/></dir></dir></dir></target><target name="magedesign"><dir name="frontend"><dir name="base"><dir name="default"><dir name="layout"><file name="budgetmailer.xml" hash="3f8fab70b03bfc23ea2c9091b623da48"/></dir><dir name="template"><dir name="budgetmailer"><file name="newsletter.phtml" hash="4c5797708f22751e6565b6da516483a3"/><dir><dir name="subscribe"><file name="mini.phtml" hash="95d0f6924bab784c45472450745bee0e"/></dir></dir><file name="subscribe.phtml" hash="e1a47e179c4d3d69f6d46e5e03e59d97"/></dir></dir></dir></dir><dir name="rwd"><dir name="default"><dir name="layout"><file name="budgetmailer.xml" hash="5dd0fd80c51125930ae444922343c694"/></dir><dir name="template"><dir name="budgetmailer"><file name="newsletter.phtml" hash="b25b064f39666aa69eedc91ddc44c95c"/><dir><dir name="subscribe"><file name="mini.phtml" hash="ee4a6904d78db697ec84353ed62a37b0"/></dir></dir><file name="subscribe.phtml" hash="ed835b8c15de0788617e6331dd135a3d"/></dir></dir></dir></dir></dir><dir name="adminhtml"><dir name="default"><dir name="default"><dir name="layout"><file name="budgetmailer.xml" hash="a4733989973690c7ed4c5216157bd8a6"/></dir><dir name="template"><dir name="budgetmailer"><dir><dir name="customer"><file name="tab.phtml" hash="832eb6b15600c265dd86ab3d59cb7b9f"/></dir></dir></dir></dir></dir></dir></dir></target><target name="mageetc"><dir name="modules"><file name="Professio_BudgetMailer.xml" hash="0308fff9854085492da955ef2f953d8c"/></dir></target><target name="magelocale"><dir name="en_US"><file name="Professio_BudgetMailer.csv" hash="b8cba8d3594cc659087db92d5a2f645b"/></dir></target></contents>
16
+ <compatible/>
17
+ <dependencies><required><php><min>5.3.0</min><max>5.9.9</max></php></required></dependencies>
18
+ </package>