-
Notifications
You must be signed in to change notification settings - Fork 94
/
Data.php
4259 lines (3757 loc) · 127 KB
/
Data.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* MailChimp For Magento
*
* @category Ebizmarts_MailChimp
* @author Ebizmarts Team <info@ebizmarts.com>
* @copyright Ebizmarts (http://ebizmarts.com)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @date: 4/29/16 3:55 PM
* @file: Data.php
*/
class Ebizmarts_MailChimp_Helper_Data extends Mage_Core_Helper_Abstract
{
const DEFAULT_SIZE = '0';
const SMALL_SIZE = '1';
const THUMBNAIL_SIZE = '2';
const ORIGINAL_SIZE = '3';
const SUB_MOD = "SubscriberModified";
const SUB_NEW = "SubscriberNew";
const PRO_MOD = "ProductModified";
const PRO_NEW = "ProductNew";
const CUS_MOD = "CustomerModified";
const CUS_NEW = "CustomerNew";
const ORD_MOD = "OrderModified";
const ORD_NEW = "OrderNew";
const QUO_MOD = "QuoteModified";
const QUO_NEW = "QuoteNew";
const DATA_NOT_SENT_TO_MAILCHIMP = 'NOT SENT';
const DATA_SENT_TO_MAILCHIMP = 'SENT';
const BATCH_STATUS_LOG = 'Mailchimp_Batch_Status.log';
const BATCH_CANCELED = 'canceled';
const BATCH_PENDING = 'pending';
protected $_countersSendBatch = array();
protected $_countersSubscribers = array();
protected $_countersGetResponseBatch = array();
/**
* All MailChimp available language codes
*
* @var array
*/
public static $LANGUAGES = array(
'en', // English
'ar', // Arabic
'af', // Afrikaans
'be', // Belarusian
'bg', // Bulgarian
'ca', // Catalan
'zh', // Chinese
'hr', // Croatian
'cs', // Czech
'da', // Danish
'nl', // Dutch
'et', // Estonian
'fa', // Farsi
'fi', // Finnish
'fr', // French (France)
'fr_CA', // French (Canada)
'de', // German
'el', // Greek
'he', // Hebrew
'hi', // Hindi
'hu', // Hungarian
'is', // Icelandic
'id', // Indonesian
'ga', // Irish
'it', // Italian
'ja', // Japanese
'km', // Khmer
'ko', // Korean
'lv', // Latvian
'lt', // Lithuanian
'mt', // Maltese
'ms', // Malay
'mk', // Macedonian
'no', // Norwegian
'pl', // Polish
'pt', // Portuguese (Brazil)
'pt_PT', // Portuguese (Portugal)
'ro', // Romanian
'ru', // Russian
'sr', // Serbian
'sk', // Slovak
'sl', // Slovenian
'es', // Spanish (Mexico)
'es_ES', // Spanish (Spain)
'sw', // Swahili
'sv', // Swedish
'ta', // Tamil
'th', // Thai
'tr', // Turkish
'uk', // Ukrainian
'vi', // Vietnamese
);
/**
* Get Config value for certain scope.
*
* @param $path
* @param $scopeId
* @param null $scope
* @return mixed
* @throws Mage_Core_Exception
*/
public function getConfigValueForScope($path, $scopeId, $scope = null)
{
if ($scope == 'websites') {
$configValue = $this->getMageApp()->getWebsite($scopeId)->getConfig($path);
} else {
$configValue = Mage::getStoreConfig($path, $scopeId);
}
return $configValue;
}
/**
* Get storeId and/or websiteId if scope selected on back end
*
* @param null $storeId
* @param null $websiteId
* @return array
*/
public function getConfigScopeId($storeId = null, $websiteId = null)
{
$scopeArray = array();
if ($code = Mage::getSingleton('adminhtml/config_data')->getStore()) {
// store level
$storeId = Mage::getModel('core/store')->load($code)->getId();
} elseif ($code = Mage::getSingleton('adminhtml/config_data')->getWebsite()) {
// website level
$websiteId = $this->getCoreWebsite()->load($code)->getId();
$storeId = $this->getMageApp()->getWebsite($websiteId)->getDefaultStore()->getId();
}
$scopeArray['websiteId'] = $websiteId;
$scopeArray['storeId'] = $storeId;
return $scopeArray;
}
/**
* Create string for current scope with format scope-scopeId.
*
* @return array
*/
public function getCurrentScope()
{
$scopeIdArray = $this->getConfigScopeId();
$scopeArray = array();
if (isset($scopeIdArray['websiteId'])) {
$scopeArray['scope'] = 'websites';
$scopeArray['scope_id'] = $scopeIdArray['websiteId'];
} elseif (isset($scopeIdArray['storeId'])) {
$scopeArray['scope'] = 'stores';
$scopeArray['scope_id'] = $scopeIdArray['storeId'];
} else {
$scopeArray['scope'] = 'default';
$scopeArray['scope_id'] = 0;
}
return $scopeArray;
}
/**
* @param $scopeArray
* @return false|string
* @throws Mage_Core_Exception
* @throws Mage_Core_Model_Store_Exception
*/
public function getScopeName($scopeArray)
{
$storeName = false;
if (isset($scopeArray['scope'])) {
switch ($scopeArray['scope']) {
case 'stores':
$store = $this->getMageApp()->getStore($scopeArray['scope_id']);
$storeName = $store->getName();
break;
case 'websites':
$website = $this->getMageApp()->getWebsite($scopeArray['scope_id']);
$storeName = $website->getName();
break;
case 'default':
$storeName = 'Default Config';
break;
}
}
return $storeName;
}
/**
* @param $scopeId
* @param $scope
* @return bool
* @throws Mage_Core_Exception
*/
public function isUsingConfigStoreName($scopeId, $scope)
{
$storeName = $this->getConfigValueForScope(
Mage_Core_Model_Store::XML_PATH_STORE_STORE_NAME,
$scopeId,
$scope
);
if ($storeName == '') {
$usingConfigName = false;
} else {
$usingConfigName = true;
}
return $usingConfigName;
}
/**
* @param $scopeId
* @param $scope
* @return mixed|string
* @throws Mage_Core_Exception
* @throws Mage_Core_Model_Store_Exception
*/
public function getStoreDomain($scopeId, $scope)
{
if ($scope == 'stores') {
$domain = $this->getMageApp()->getStore($scopeId)->getBaseUrl();
} elseif ($scope == 'websites') {
$website = Mage::getModel('core/website')->load($scopeId);
$websiteCode = $website->getCode();
$domain = (string)Mage::getConfig()->getNode('web/unsecure/base_url', 'website', $websiteCode);
} else {
$domain = $this->getConfigValueForScope(
Mage_Core_Model_Store::XML_PATH_UNSECURE_BASE_LINK_URL,
$scopeId,
$scope
);
}
return $domain;
}
/**
* Get local store_id value of the MC store.
*
* @return array
* @throws Mage_Core_Exception
*/
public function getStoreRelation()
{
$stores = $this->getMageApp()->getStores();
$storeRelation = array();
foreach ($stores as $storeId => $store) {
if ($this->isEcomSyncDataEnabled($storeId)) {
$mcStoreId = $this->getMCStoreId($storeId);
if ($mcStoreId) {
if (!array_key_exists($mcStoreId, $storeRelation)) {
$storeRelation[$mcStoreId] = array();
}
$storeRelation[$mcStoreId][] = $storeId;
}
}
}
return $storeRelation;
}
/**
* Get all Magento stores associated to the MailChimp store configured for the given scope.
*
* @param $scopeId
* @param $scope
* @return array|mixed
* @throws Mage_Core_Exception
*/
public function getMagentoStoresForMCStoreIdByScope($scopeId, $scope)
{
$ret = array();
$storeRelation = $this->getStoreRelation();
$mailchimpStoreIdForScope = $this->getMCStoreId($scopeId, $scope);
$isThereAnyStore = array_key_exists($mailchimpStoreIdForScope, $storeRelation);
if ($mailchimpStoreIdForScope && $isThereAnyStore) {
$ret = $storeRelation[$mailchimpStoreIdForScope];
}
return $ret;
}
/**
* Validate if api key exists, could still be incorrect
*
* @param $scopeId
* @param null $scope
* @return mixed
*/
public function validateApiKey($scopeId, $scope = null)
{
$apiKey = $this->getApiKey($scopeId, $scope);
$isApiKeyValid = $apiKey !== null && $apiKey != "";
return $isApiKeyValid;
}
/**
* Return if module is enabled for given scope.
*
* @param $scopeId
* @param null $scope
* @return mixed
*/
public function isMailChimpEnabled($scopeId, $scope = null)
{
return $this->getConfigValueForScope(Ebizmarts_MailChimp_Model_Config::GENERAL_ACTIVE, $scopeId, $scope);
}
/**
* @param $scopeId
* @return bool | returns true if useMagentoEmails is enabled
*/
public function isUseMagentoEmailsEnabled($scopeId)
{
return (int)$this->getConfigValueForScope(Ebizmarts_MailChimp_Model_Config::GENERAL_MAGENTO_MAIL, $scopeId);
}
/**
* Return if module is enabled and list selected for given scope.
*
* @param $scopeId
* @param null $scope
* @return mixed
*/
public function isSubscriptionEnabled($scopeId, $scope = null)
{
$apiKeyValid = $this->validateApiKey($scopeId, $scope);
$mailChimpEnabled = $this->isMailChimpEnabled($scopeId, $scope);
$generalList = $this->getGeneralList($scopeId, $scope);
return $apiKeyValid && $mailChimpEnabled && $generalList;
}
/**
* Return Api Key if exists for given scope.
*
* @param $scopeId
* @param null $scope
* @return mixed
* @throws Mage_Core_Exception
*/
public function getApiKey($scopeId, $scope = null)
{
$apiKey = $this->getConfigValueForScope(
Ebizmarts_MailChimp_Model_Config::GENERAL_APIKEY,
$scopeId,
$scope
);
return $this->decryptData($apiKey);
}
/**
* Return decrypted data.
*
* @param $data
* @return mixed
*/
public function decryptData($data)
{
return Mage::helper('core')->decrypt($data);
}
/**
* Return encrypted data.
*
* @param $data
* @return mixed
*/
public function encryptData($data)
{
return Mage::helper('core')->encrypt($data);
}
/**
* Get local store_id value of the MC store for given scope.
*
* @param $scopeId
* @param null $scope
* @return mixed
* @throws Mage_Core_Exception
*/
public function getMCStoreId($scopeId, $scope = null)
{
return $this->getConfigValueForScope(
Ebizmarts_MailChimp_Model_Config::GENERAL_MCSTOREID,
$scopeId,
$scope
);
}
/**
* Delete all data related to the configured store in a given scope.
*
* @param $scopeId
* @param string $scope
*/
public function deletePreviousConfiguredMCStoreLocalData($mailchimpStoreId, $scopeId, $scope = 'stores')
{
$config = $this->getConfig();
if ($mailchimpStoreId !== null && $mailchimpStoreId !== '') {
foreach ($this->getAllStoresForScope($scopeId, $scope) as $storeId) {
$config->deleteConfig(
Ebizmarts_MailChimp_Model_Config::GENERAL_MCISSYNCING . "_$mailchimpStoreId",
'stores',
$storeId
);
}
$config->deleteConfig(
Ebizmarts_MailChimp_Model_Config::GENERAL_MCISSYNCING . "_$mailchimpStoreId",
$scope,
$scopeId
);
}
$config->deleteConfig(Ebizmarts_MailChimp_Model_Config::ECOMMERCE_CUSTOMER_LAST_ID, $scope, $scopeId);
$config->deleteConfig(Ebizmarts_MailChimp_Model_Config::ECOMMERCE_PRODUCT_LAST_ID, $scope, $scopeId);
$config->deleteConfig(Ebizmarts_MailChimp_Model_Config::ECOMMERCE_ORDER_LAST_ID, $scope, $scopeId);
$config->deleteConfig(Ebizmarts_MailChimp_Model_Config::ECOMMERCE_CART_LAST_ID, $scope, $scopeId);
$config->deleteConfig(Ebizmarts_MailChimp_Model_Config::ECOMMERCE_PCD_LAST_ID, $scope, $scopeId);
$config->deleteConfig(Ebizmarts_MailChimp_Model_Config::ECOMMERCE_RESEND_ENABLED, $scope, $scopeId);
$config->deleteConfig(Ebizmarts_MailChimp_Model_Config::ECOMMERCE_RESEND_TURN, $scope, $scopeId);
$config->cleanCache();
$resource = $this->getCoreResource();
$connection = $resource->getConnection('core_write');
$tableName = $resource->getTableName('mailchimp/synchbatches');
$where = $connection->quoteInto("status = 'pending' AND store_id = ?", $mailchimpStoreId);
$connection->update($tableName, array('status' => 'canceled'), $where);
}
/**
* Delete all data related to the configured store in a given scope.
*
* @param $scopeId
* @param string $scope
*/
public function deleteAllConfiguredMCStoreLocalData($mailchimpStoreId, $scopeId, $scope = 'stores')
{
$configValues = array(array(Ebizmarts_MailChimp_Model_Config::ECOMMERCE_ACTIVE, 0));
$this->saveMailchimpConfig($configValues, $scopeId, $scope, false);
$config = $this->getConfig();
$config->deleteConfig(Ebizmarts_MailChimp_Model_Config::GENERAL_MCSTOREID, $scope, $scopeId);
$this->deletePreviousConfiguredMCStoreLocalData($mailchimpStoreId, $scopeId, $scope = 'stores');
}
/**
* @return Ebizmarts_MailChimp_Model_Resource_SynchBatches
*/
protected function getSyncBatchesResource()
{
return Mage::getResourceModel('mailchimp/synchbatches');
}
public function deleteAllMCStoreData($mailchimpStoreId)
{
//Delete default configurations for this store.
$config = $this->getConfig();
$config->deleteConfig(
Ebizmarts_MailChimp_Model_Config::ECOMMERCE_SYNC_DATE . "_$mailchimpStoreId", 'default', 0
);
$config->deleteConfig(
Ebizmarts_MailChimp_Model_Config::ECOMMERCE_MC_JS_URL . "_$mailchimpStoreId", 'default', 0
);
//Delete local ecommerce data and errors for this store.
$this->removeEcommerceSyncDataByMCStore($mailchimpStoreId);
$this->clearErrorGridByMCStore($mailchimpStoreId);
//Delete particular scopes configuraion flags for this store
$scopeArrayIfExist = $this->getScopeByMailChimpStoreId($mailchimpStoreId);
if ($scopeArrayIfExist !== false) {
$this->deleteAllConfiguredMCStoreLocalData(
$mailchimpStoreId,
$scopeArrayIfExist['scope_id'],
$scopeArrayIfExist['scope']
);
}
}
/**
* Return if Ecommerce configuration is enabled for given scope.
*
* @param $scopeId
* @param null $scope
* @return mixed
* @throws Mage_Core_Exception
*/
public function isEcommerceEnabled($scopeId, $scope = null)
{
return $this->getConfigValueForScope(
Ebizmarts_MailChimp_Model_Config::ECOMMERCE_ACTIVE,
$scopeId,
$scope
);
}
/**
* Get general list configured for the given scope.
*
* @param $scopeId
* @param null $scope
* @return mixed
* @throws Mage_Core_Exception
*/
public function getGeneralList($scopeId, $scope = null)
{
return $this->getConfigValueForScope(
Ebizmarts_MailChimp_Model_Config::GENERAL_LIST,
$scopeId,
$scope
);
}
/**
* Get map fields configured for the given scope.
*
* @param $scopeId
* @param null $scope
* @return mixed
* @throws Mage_Core_Exception
*/
public function getMapFields($scopeId, $scope = null)
{
return $this->getConfigValueForScope(
Ebizmarts_MailChimp_Model_Config::GENERAL_MAP_FIELDS,
$scopeId,
$scope
);
}
/**
* Get custom merge fields configured for the given scope.
*
* @param $scopeId
* @param null $scope
* @return mixed
* @throws Mage_Core_Exception
*/
public function getCustomMergeFieldsSerialized($scopeId, $scope = null)
{
return $this->getConfigValueForScope(
Ebizmarts_MailChimp_Model_Config::GENERAL_CUSTOM_MAP_FIELDS, $scopeId, $scope
);
}
/**
* Get if Abandoned Cart module is enabled.
*
* @param $scopeId
* @param null $scope
* @return mixed
* @throws Mage_Core_Exception
*/
public function isAbandonedCartEnabled($scopeId, $scope = null)
{
return $this->getConfigValueForScope(
Ebizmarts_MailChimp_Model_Config::ABANDONEDCART_ACTIVE,
$scopeId,
$scope
);
}
/**
* Get date configured for carts to be sent for the given scope.
*
* @param $scopeId
* @param null $scope
* @return mixed
* @throws Mage_Core_Exception
*/
public function getAbandonedCartFirstDate($scopeId, $scope = null)
{
return $this->getConfigValueForScope(
Ebizmarts_MailChimp_Model_Config::ABANDONEDCART_FIRSTDATE,
$scopeId,
$scope
);
}
/**
* Get date configured for ecommerce data to be sent for the given scope.
*
* @param $scopeId
* @param null $scope
* @return mixed
* @throws Mage_Core_Exception
*/
public function getEcommerceFirstDate($scopeId, $scope = null)
{
return $this->getConfigValueForScope(
Ebizmarts_MailChimp_Model_Config::ECOMMERCE_FIRSTDATE,
$scopeId,
$scope
);
}
/**
* Get local is_syncing value of the MC store.
* If data was saved in the old way get it from the scope and update it to the new way.
*
* @param $mailchimpStoreId
* @param int $scopeId
* @param string $scope
* @return mixed|null
* @throws Mage_Core_Exception
*/
public function getMCIsSyncing($mailchimpStoreId, $scopeId = 0, $scope = 'stores')
{
$oldSyncingFlag = $this->getConfigValueForScope(
Ebizmarts_MailChimp_Model_Config::GENERAL_MCISSYNCING,
$scopeId,
$scope
);
$syncingFlag = $this->getConfigValueForScope(
Ebizmarts_MailChimp_Model_Config::GENERAL_MCISSYNCING . "_$mailchimpStoreId",
$scopeId, $scope
);
//Save old value in new place.
if ($syncingFlag === null && $oldSyncingFlag !== null) {
$configValue = array(
array(Ebizmarts_MailChimp_Model_Config::GENERAL_MCISSYNCING . "_$mailchimpStoreId", $oldSyncingFlag)
);
$this->saveMailchimpConfig($configValue, $scopeId, $scope);
}
//Delete old entry if exists particularly in this scope.
if ($oldSyncingFlag !== null && $this->getIfConfigExistsForScope(
Ebizmarts_MailChimp_Model_Config::GENERAL_MCISSYNCING,
$scopeId,
$scope
)
) {
$config = $this->getConfig();
$config->deleteConfig(Ebizmarts_MailChimp_Model_Config::GENERAL_MCISSYNCING, $scope, $scopeId);
$config->cleanCache();
}
return ($syncingFlag !== null) ? $syncingFlag : $oldSyncingFlag;
}
/**
* Get if logs are enabled.
* Logs can only be enabled in default scope.
*
* @return mixed
* @throws Mage_Core_Exception
*/
public function getLogsEnabled()
{
return $this->getConfigValueForScope(Ebizmarts_MailChimp_Model_Config::GENERAL_LOG, 0);
}
/**
* Get if two way sync is enabled for given scope.
*
* @param int $scopeId
* @param null $scope
* @return mixed
* @throws Mage_Core_Exception
*/
public function getTwoWaySyncEnabled($scopeId = 0, $scope = null)
{
return $this->getConfigValueForScope(
Ebizmarts_MailChimp_Model_Config::GENERAL_TWO_WAY_SYNC,
$scopeId,
$scope
);
}
/**
* Get if monkey should be displayed in order grid.
*
* @param int $scopeId
* @param null $scope
* @return mixed
* @throws Mage_Core_Exception
*/
public function getMonkeyInGrid($scopeId = 0, $scope = null)
{
return $this->getConfigValueForScope(
Ebizmarts_MailChimp_Model_Config::GENERAL_ORDER_GRID,
$scopeId,
$scope
);
}
/**
* Get if Email Catcher popup is enabled for given scope.
*
* @param int $scopeId
* @param null $scope
* @return mixed
* @throws Mage_Core_Exception
*/
public function isEmailCatcherEnabled($scopeId = 0, $scope = null)
{
return $this->getConfigValueForScope(Ebizmarts_MailChimp_Model_Config::ENABLE_POPUP, $scopeId, $scope);
}
/**
* @param int $scopeId
* @param null $scope
* @return mixed
* @throws Mage_Core_Exception
*/
public function getDateSyncFinishByStoreId($scopeId = 0, $scope = null)
{
$mailchimpStoreId = $this->getMCStoreId($scopeId, $scope);
return $this->getConfigValueForScope(
Ebizmarts_MailChimp_Model_Config::ECOMMERCE_SYNC_DATE . "_$mailchimpStoreId",
0,
'default'
);
}
/**
* @param $mailchimpStoreId
* @return mixed
* @throws Mage_Core_Exception
*/
public function getDateSyncFinishByMailChimpStoreId($mailchimpStoreId)
{
return $this->getConfigValueForScope(
Ebizmarts_MailChimp_Model_Config::ECOMMERCE_SYNC_DATE . "_$mailchimpStoreId",
0,
'default'
);
}
/**
* Set the values to send all the items again.
*
* @param $scopeId
* @param $scope
* @param null $filters
* @throws Mage_Core_Exception
*/
public function resendMCEcommerceData($scopeId, $scope, $filters = null)
{
if ($this->getMCStoreId($scopeId, $scope) && $this->getMCStoreId($scopeId, $scope) != "") {
if (!$this->getResendEnabled($scopeId, $scope)) {
$this->saveLastItemsSent($scopeId, $scope, $filters);
}
$this->removeEcommerceSyncData($scopeId, $scope, false, $filters);
$this->clearErrorGrid($scopeId, $scope, true, $filters);
if ($filters !== null && in_array(Ebizmarts_MailChimp_Model_Config::IS_PRODUCT, $filters)) {
$this->deleteFlushMagentoCacheFlag();
}
}
}
/**
* Remove items from mailchimp_ecommerce_sync_data table to allow them to be sent.
* If scopeId is 0 remova from all scopes.
*
* @param $scopeId
* @param $scope
* @param bool $deleteErrorsOnly
* @param null $filters
* @throws Mage_Core_Exception
*/
public function removeEcommerceSyncData($scopeId, $scope, $deleteErrorsOnly = false, $filters = null)
{
if ($scopeId == 0 && $deleteErrorsOnly) {
$this->removeAllEcommerceSyncDataErrors($filters);
} else {
$mailchimpStoreId = $this->getMCStoreId($scopeId, $scope);
$this->removeEcommerceSyncDataByMCStore($mailchimpStoreId, $deleteErrorsOnly, $filters);
}
}
/**
* @param null $filters
* @throws Mage_Core_Exception
*/
public function removeAllEcommerceSyncDataErrors($filters = null)
{
$resource = $this->getCoreResource();
$connection = $resource->getConnection('core_write');
$tableName = $resource->getTableName('mailchimp/ecommercesyncdata');
$where = array();
$where[] = "mailchimp_sync_error != ''";
if ($filters !== null) {
$where[] = $connection->quoteInto('type IN (?)', $filters);
}
try {
$connection->delete($tableName, $where);
} catch (Exception $e) {
$this->logError($e->getMessage());
}
}
/**
* @param $mailchimpStoreId
* @param bool $deleteErrorsOnly
* @param null $filters
*/
public function removeEcommerceSyncDataByMCStore($mailchimpStoreId, $deleteErrorsOnly = false, $filters = null)
{
$resource = $this->getCoreResource();
$connection = $resource->getConnection('core_write');
$tableName = $resource->getTableName('mailchimp/ecommercesyncdata');
$where = array();
if ($deleteErrorsOnly) {
$where[] = $connection->quoteInto(
"mailchimp_store_id = ? AND mailchimp_sync_error != ''",
$mailchimpStoreId
);
} else {
$where[] = $connection->quoteInto("mailchimp_store_id = ?", $mailchimpStoreId);
}
if ($filters !== null) {
$where[] = $connection->quoteInto('type IN (?)', $filters);
}
try {
$connection->delete($tableName, $where);
} catch (Exception $e) {
$this->logError($e->getMessage());
}
}
/**
* Check if Ecommerce data is configured to be sent.
*
* @param $scopeId
* @param null $scope
* @param bool $isStoreCreation
* @return bool
* @throws Mage_Core_Exception
*/
public function isEcomSyncDataEnabled($scopeId, $scope = null, $isStoreCreation = false)
{
//If store id does not exist return false. (For deleted stores i.e.: order grid synced status)
if ($scopeId === null) {
$ret = false;
} else {
$subscriptionEnabled = $this->isSubscriptionEnabled($scopeId, $scope);
$ecommerceEnabled = $this->isEcommerceEnabled($scopeId, $scope);
$mailchimpStoreId = $this->getMCStoreId($scopeId, $scope);
$ret = ($mailchimpStoreId !== null || $isStoreCreation)
&& $subscriptionEnabled && $ecommerceEnabled;
}
return $ret;
}
/**
* Check if Ecommerce data is configured to be sent in any scope.
*
* @return bool
*/
public function isEcomSyncDataEnabledInAnyScope()
{
$stores = $this->getMageApp()->getStores();
foreach ($stores as $storeId => $store) {
$ecomEnabled = $this->isEcomSyncDataEnabled($storeId);
if ($ecomEnabled) {
return true;
}
}
return false;
}
/**
* Save error response from MailChimp's API in "MailChimp_Error.log" file.
*
* @param $message
*/
public function logError($message)
{
if ($this->isErrorLogEnabled()) {
Mage::log($message, null, 'MailChimp_Errors.log', true);
}
}
/**
* Save the message errors for the data sent
* succesfully or not to Mailchimp
* in the file "Mailchimp_Batch_Status.log"
*
* @param $message
* @throws Mage_Core_Exception
*/
public function logBatchStatus($message)
{
if ($this->isRequestLogEnabled()) {
Mage::log($message . "\n", null, self::BATCH_STATUS_LOG, true);
}
}
/**
* Save how many data was sent to Mailchimp,
* how many data was successfully and not sent to Mailchimp
* in the file "Mailchimp_Batch_Status.log"
*
* @param $message
* @throws Mage_Core_Exception
*/
public function logBatchQuantity($message)
{
if ($this->isRequestLogEnabled()) {
Mage::log($message, null, self::BATCH_STATUS_LOG, true);
}
}
/**
* Save request made to MailChimp's API in "MailChimp_Requests.log" file.
*
* @param $message
* @param null $batchId
*/
public function logRequest($message, $batchId = null)
{
$logRequestEnabled = $this->isRequestLogEnabled();
$logErrorEnabled = $this->isErrorLogEnabled();
if (!$batchId && ($logRequestEnabled || $logErrorEnabled)) {
Mage::log($message, null, 'MailChimp_Failing_Requests.log', true);
} elseif ($logRequestEnabled) {
$logDir = Mage::getBaseDir('var') . DS . 'log';
$fileHelper = $this->getFileHelper();
$fileHelper->open(array('path'=>$logDir));
if (!$fileHelper->fileExists($logDir, false)) {
$fileHelper->mkDir($logDir, 0750);
}
$logDir .= DS . 'MailChimp_Requests';
if (!$fileHelper->fileExists($logDir, false)) {
$fileHelper->mkDir($logDir, 0750);
}
$fileName = $logDir . DS . $batchId . '.Request.log';
$oldPermission = umask(0033);
$fileHelper->filePutContent($fileName, $message);
umask($oldPermission);
}
}
/**
* @return bool
* @throws Mage_Core_Exception
*/
protected function isRequestLogEnabled()
{
$logEnabled = false;
$logConfig = $this->getLogsEnabled();
if ($logConfig == Ebizmarts_MailChimp_Model_System_Config_Source_Log::REQUEST_LOG
|| $logConfig == Ebizmarts_MailChimp_Model_System_Config_Source_Log::BOTH
) {
$logEnabled = true;
}
return $logEnabled;
}
/**
* @return bool
* @throws Mage_Core_Exception
*/
public function isErrorLogEnabled()
{
$logEnabled = false;
$logConfig = $this->getLogsEnabled();