This repository has been archived by the owner on Feb 1, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Data.php
178 lines (169 loc) · 7.18 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
<?php
/**
* Ebizmarts_MAgeMonkey Magento JS component
*
* @category Ebizmarts
* @package Ebizmarts_MageMonkey
* @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)
*/
namespace Ebizmarts\MageMonkey\Helper;
use Magento\Store\Model\Store;
class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
const XML_PATH_ACTIVE = 'magemonkey/general/active';
const XML_PATH_APIKEY = 'magemonkey/general/apikey';
const XML_PATH_MAXLISTAMOUNT = 'magemonkey/general/maxlistamount';
const XML_PATH_LIST = 'magemonkey/general/list';
const XML_PATH_LOG = 'magemonkey/general/log';
const XML_PATH_MAPPING = 'magemonkey/general/mapping';
const XML_PATH_CONFIRMATION_FLAG = 'newsletter/subscription/confirm';
protected $_storeManager;
protected $_mlogger;
protected $_groupRepositoryInterface;
/**
* @param \Magento\Framework\App\Helper\Context $context
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param \Ebizmarts\MageMonkey\Model\Logger\Magemonkey $logger
* @param \Magento\Customer\Api\GroupRepositoryInterface $groupRepositoryInterface
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
*/
public function __construct(
\Magento\Framework\App\Helper\Context $context,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Ebizmarts\MageMonkey\Model\Logger\Magemonkey $logger,
\Magento\Customer\Api\GroupRepositoryInterface $groupRepositoryInterface
)
{
$this->_storeManager = $storeManager;
$this->_mlogger = $logger;
$this->_groupRepositoryInterface = $groupRepositoryInterface;
parent::__construct($context);
}
public function isMonkeyEnabled($store = null)
{
return $this->scopeConfig->getValue(self::XML_PATH_ACTIVE, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $store);
}
public function isDoubleOptInEnabled($store = null)
{
return $this->scopeConfig->getValue(self::XML_PATH_CONFIRMATION_FLAG, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $store);
}
public function getApiKey($store = null)
{
return $this->scopeConfig->getValue(self::XML_PATH_APIKEY, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $store);
}
public function getDefaultList($store = null)
{
return $this->scopeConfig->getValue(self::XML_PATH_LIST, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $store);
}
public function getLogger()
{
return $this->_logger;
}
public function log($message,$store=null)
{
if($this->scopeConfig->getValue(self::XML_PATH_LOG, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $store))
{
$this->_mlogger->info($message);
}
}
public function getMergeVars($customer,$store = null)
{
$merge_vars = array();
$mergeVars = unserialize($this->scopeConfig->getValue(self::XML_PATH_MAPPING, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $store));
foreach($mergeVars as $map)
{
array_merge($merge_vars,$this->_getMergeVarsValue($map, $customer));
}
return $merge_vars;
}
protected function _getMergeVarsValue($map, $customer)
{
$merge_vars = array();
$customAtt = $map['magento'];
$chimpTag = $map['mailchimp'];
if($chimpTag && $customAtt) {
$key = strtoupper($chimpTag);
switch ($customAtt) {
case 'fname':
$val = $customer->getFirstname();
$this->log($val);
$merge_vars[$key] = $val;
break;
case 'lname':
$val = $customer->getLastname();
$this->log($val);
$merge_vars[$key] = $val;
break;
case 'gender':
$val = (int)$customer->getData(strtolower($customAtt));
if ($val == 1) {
$merge_vars[$key] = 'Male';
} elseif ($val == 2) {
$merge_vars[$key] = 'Female';
}
break;
case 'dob':
$dob = (string)$customer->getData(strtolower($customAtt));
if ($dob) {
$merge_vars[$key] = (substr($dob, 5, 2) . '/' . substr($dob, 8, 2));
}
break;
case 'billing_address':
case 'shipping_address':
$addr = explode('_', $customAtt);
array_merge($merge_vars,$this->_updateMergeVars($key,ucfirst($addr[0]),$customer));
break;
case 'telephone':
if ($address = $customer->{'getDefaultBillingAddress'}()) {
$telephone = $address->getTelephone();
if ($telephone) {
$merge_vars['TELEPHONE'] = $telephone;
}
}
break;
case 'company':
if ($address = $customer->{'getDefaultBillingAddress'}()) {
$company = $address->getCompany();
if ($company) {
$merge_vars['COMPANY'] = $company;
}
}
break;
case 'group_id':
// $group_id = (int)$customer->getData(strtolower($customAtt));
// $customerGroup = $this->_groupRepositoryInterface->getList('');
// $this->log(print_r($customerGroup));
//// $customerGroup = Mage::helper('customer')->getGroups()->toOptionHash();
//// if($group_id == 0){
//// $merge_vars[$key] = 'NOT LOGGED IN';
//// }else{
//// $merge_vars[$key] = $customerGroup[$group_id];
//// }
break;
default:
if (($value = (string)$customer->getData(strtolower($customAtt)))) {
$merge_vars[$key] = (string)$customer->getData(strtolower($customAtt));
}
break;
}
return $merge_vars;
}
}
protected function _updateMergeVars($key,$type,$customer)
{
$merge_vars = array();
if ($address = $customer->{'getDefault' . $type . 'Address'}()) {
$merge_vars[$key] = array(
'addr1' => $address->getStreetLine(1),
'addr2' => $address->getStreetLine(2),
'city' => $address->getCity(),
'state' => (!$address->getRegion() ? $address->getCity() : $address->getRegion()),
'zip' => $address->getPostcode(),
'country' => $address->getCountryId()
);
}
return $merge_vars;
}
}