PageRenderTime 52ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/app/code/local/Ebizmarts/MailchimpPro/Helper/Data.php

https://github.com/arush/fancydress
PHP | 389 lines | 314 code | 75 blank | 0 comment | 91 complexity | c2552152952d21ea8c8497d38e152e23 MD5 | raw file
  1. <?php
  2. class Ebizmarts_MailchimpPro_Helper_Data extends Mage_Core_Helper_Abstract{
  3. public function getGeneralConfig($field,$store=null){
  4. return Mage::getStoreConfig('mailchimpPro/general/'.$field,$store);
  5. }
  6. public function getSubscribeConfig($field,$store=null){
  7. return Mage::getStoreConfig('mailchimpPro/subscribe/'.$field,$store);
  8. }
  9. public function getUnSubscribeConfig($field,$store=null){
  10. return Mage::getStoreConfig('mailchimpPro/unsubscribe/'.$field,$store);
  11. }
  12. private function F91B2E37D34E5DC4FFC59C324BDC1157C($store){
  13. $R8409EAA6EC0CE2EA307354B2E150F8C2 = str_replace('www.', '', $_SERVER['HTTP_HOST']); $REBBCEB7D5CE9F8309DCC3226F5DAC53B = $this->getGeneralConfig('sid',$store); $R1A634B62E7FB6CBC3AD8309D17FDC73C = substr(strrev($R8409EAA6EC0CE2EA307354B2E150F8C2), 0, strlen($R8409EAA6EC0CE2EA307354B2E150F8C2)); $R7BCAA4FB61D5AD641E1B67637D894EC1 = crypt($R8409EAA6EC0CE2EA307354B2E150F8C2 . 'Ebizmarts_MailChimp', $R1A634B62E7FB6CBC3AD8309D17FDC73C); $R835CC35CB400C713B188267E7C10C798 = ($R7BCAA4FB61D5AD641E1B67637D894EC1 === $REBBCEB7D5CE9F8309DCC3226F5DAC53B); return $R835CC35CB400C713B188267E7C10C798;
  14. }
  15. public function addAddons($store){
  16. if($this->mailChimpProEnabled($store) && !(bool)$this->F91B2E37D34E5DC4FFC59C324BDC1157C($store)){
  17. return true;
  18. }
  19. return false;
  20. }
  21. public function newMailChimpHost($apikey,$store){
  22. if(substr($apikey, -4) != '-us1' && substr($apikey, -4) != '-us2'){
  23. Mage::getSingleton('adminhtml/session')->addError('The API key is not well formed');
  24. return false;
  25. }else{
  26. list($key, $dc) = explode('-',$apikey,2);
  27. if (!$dc) $dc = 'us1';
  28. list($aux, $host) = explode('http://',$this->getGeneralConfig('url',$store));
  29. $api_host = 'http://'.$dc.'.'.$host;
  30. return $api_host;
  31. }
  32. }
  33. public function mailChimpProEnabled($store){
  34. if((bool)$this->getGeneralConfig('active',$store) == true && $this->getGeneralConfig('apikey',$store)
  35. && $this->getGeneralConfig('general',$store) && $this->getGeneralConfig('url',$store)){
  36. return true;
  37. }
  38. return false;
  39. }
  40. public function getAdditionalEnabled($store){
  41. return (bool)$this->getGeneralConfig('intgr',$store);
  42. }
  43. public function checkSubscribe(){
  44. $store = Mage::app()->getStore()->getStoreId();
  45. if($this->mailChimpProEnabled($store)){
  46. $return = ((bool)$this->getSubscribeConfig('double_optin',$store))? false : true;
  47. }else{
  48. $return = true;
  49. }
  50. return $return;
  51. }
  52. public function checkSendSubscribe(){
  53. $store = Mage::app()->getStore()->getStoreId();
  54. if($this->mailChimpProEnabled($store)){
  55. $return = ((bool)$this->getSubscribeConfig('double_optin',$store) || (bool)$this->getSubscribeConfig('send_welcome',$store))? false : true;
  56. }else{
  57. $return = true;
  58. }
  59. return $return;
  60. }
  61. public function checkSendUnsubscribe(){
  62. $store = Mage::app()->getStore()->getStoreId();
  63. if($this->mailChimpProEnabled($store)){
  64. $return = ((bool)$this->getUnSubscribeConfig('send_notify',$store) || (bool)$this->getUnSubscribeConfig('send_goodbye',$store))? false : true;
  65. }else{
  66. $return = true;
  67. }
  68. return $return;
  69. }
  70. public function isSubscribed($email){
  71. return Mage::getModel('newsletter/subscriber')
  72. ->loadByEmail($email)
  73. ->isSubscribed();
  74. }
  75. public function isMailchimpSubscribed($subscriber,$guest = false,$list){
  76. $col = Mage::getModel('mailchimpPro/subscripter')->getCollection()
  77. ->addFieldToFilter('store_id',Mage::app()->getStore()->getStoreId())
  78. ->addFieldToFilter('customer_id',$subscriber->getCustomerId())
  79. ->addFieldToFilter('list_id',$list);
  80. if($guest){
  81. $col->addFieldToFilter('current_email',$subscriber->getSubscriberEmail());
  82. }else{
  83. if(!count($col)){
  84. $subscripter = Mage::getModel('mailchimpPro/subscripter')->getCollection()
  85. ->addFieldToFilter('store_id',Mage::app()->getStore()->getStoreId())
  86. ->addFieldToFilter('current_email',$subscriber->getSubscriberEmail())
  87. ->addFieldToFilter('list_id',$list)
  88. ->getFirstItem();
  89. return $subscripter;
  90. }
  91. }
  92. return $col->getFirstItem();
  93. }
  94. public function getAvailableLists($currentStore){
  95. if($this->mailChimpProEnabled($currentStore)){
  96. $avlists = explode(',',$this->getGeneralConfig('listid',$currentStore));
  97. $lists = array();
  98. foreach($avlists as $list){
  99. $id = substr($list,0,strpos($list,'['));
  100. if($id != $this->getGeneralConfig('general',$currentStore)){
  101. $lists[$id] = substr($list,strpos($list,'[')+1,-1);
  102. }
  103. }
  104. return $lists;
  105. }
  106. return false;
  107. }
  108. public function getMergeVars($customer,$flag){
  109. $merge_vars = array();
  110. $maps = explode('<',$this->getSubscribeConfig('mapping_fields',Mage::app()->getStore()->getStoreId()));
  111. foreach($maps as $map){
  112. if($map){
  113. $aux = substr(strstr($map,"customer='"),10);
  114. $customAtt = (string)substr($aux,0,strpos($aux,"'"));
  115. $aux = substr(strstr($map,"mailchimp='"),11);
  116. $chimpTag = (string)substr($aux,0,strpos($aux,"'"));
  117. if($chimpTag && $customAtt){
  118. if($customAtt == 'address'){
  119. $address = $customer->getAddress();
  120. $merge_vars[strtoupper($chimpTag)] = array('addr1'=>$address['street'],
  121. 'addr2'=>'',
  122. 'city'=>$address['city'],
  123. 'state'=>$address['region'],
  124. 'zip'=>$address['postcode'],
  125. 'country'=>$address['country_id']);
  126. }else{
  127. if($value = (string)$customer->getData(strtolower($customAtt))) $merge_vars[strtoupper($chimpTag)] = $value;
  128. }
  129. }
  130. }
  131. }
  132. if($flag) $merge_vars['EMAIL'] = $customer->getEmail();
  133. $groups = $customer->getListGroups();
  134. $interests = $grpToSend = array();
  135. if(is_array($groups) && count($groups)){
  136. $groupings = array();
  137. $grpName = '';
  138. $count = 0;
  139. foreach($groups as $option){
  140. $parts = explode(']',$option);
  141. if(substr($parts[0],1) == $customer->getListId() && count($parts) == 5){
  142. if($count == 0 || $grpName != substr($parts[1],1)){
  143. $grpName = substr($parts[1],1);
  144. $currentInterests = $currentInterest = array();
  145. if($count) $grpToSend[] = end($groupings);
  146. }
  147. $count++;
  148. if(substr($parts[3],1) != '') $interests[] = substr($parts[3],1);
  149. $currentInterest[] = substr($parts[3],1);
  150. $currentInterests = implode(", ", $currentInterest);
  151. $groupings[] = array('id'=>substr($parts[2],1),
  152. 'name'=>$grpName,
  153. 'groups'=>$currentInterests);
  154. if($count == count($groups)) $grpToSend[] = end($groupings);
  155. }
  156. }
  157. }
  158. $merge_vars['GROUPINGS'] = $grpToSend;
  159. $merge_vars['INTERESTS'] = ($interests)? implode(", ", $interests) : '';
  160. return $merge_vars;
  161. }
  162. private function getAllListGroups($params,$currentStore){
  163. $allGrps = array();
  164. if($this->getGeneralConfig('intgr',$currentStore)){
  165. $params = (is_array($params))? $params : $allGrps;
  166. if(isset($params['group']) || isset($params['allgroups'])){
  167. $flag = false;
  168. if(isset($params['group'])){
  169. foreach($params['group'] as $list=>$group){
  170. if($group){
  171. $flag = true;
  172. $arrayCheck = array();
  173. }
  174. }
  175. }
  176. $groups = ($flag)? $params['group'] : $params['allgroups'];
  177. foreach($groups as $list=>$group){
  178. if(is_array($group)){
  179. foreach($group as $checkbox){
  180. if(is_array($checkbox)){
  181. foreach($checkbox as $item){
  182. $allGrps[] = '['.$list.']'.str_replace('|','][',$item);
  183. $arrayCheck[] = $item;
  184. }
  185. }else{
  186. if($checkbox){
  187. if(strstr($checkbox,']')){
  188. $allGrps[] = '['.$list.']'.$checkbox;
  189. }else{
  190. $allGrps[] = '['.$list.']'.str_replace('|','][',$checkbox).'][]';
  191. }
  192. }
  193. }
  194. }
  195. }else{
  196. if($group && strstr($group,'][')){
  197. $allGrps[] = '['.$list.']'.str_replace('|','][',$group);
  198. }else{
  199. $allGrps[] = '['.$list.']'.str_replace('|','][',$params['allgroups'][$list]).'[]';
  200. }
  201. }
  202. }
  203. if($flag && isset($params['allgroups'])){
  204. $missingGrps = explode("]",str_replace("[","",$params['allgroups'][$list]));
  205. foreach($missingGrps as $fixing){
  206. if($fixing && !in_array('['.$fixing.'][]',$arrayCheck)){
  207. $allGrps[] = '['.$list.']['.str_replace('|','][',$fixing).'][]';
  208. }
  209. }
  210. }
  211. }
  212. }
  213. return $allGrps;
  214. }
  215. public function mailChimpProFilter($subscriber,$params){
  216. $store = Mage::app()->getStore()->getStoreId();
  217. if($this->mailChimpProEnabled($store)){
  218. if($store == '0') $subscriber->setStoreId(Mage::app()->getDefaultStoreView()->getStoreId());
  219. $allGrps = $this->getAllListGroups($params,$store);
  220. $subscriber->setListGroups($allGrps);
  221. $cusId = ($subscriber->getCustomerId())? $subscriber->getCustomerId() : $subscriber->getEntityId();
  222. if($cusId){
  223. $customer = Mage::getSingleton('customer/customer')->load($cusId)->setSoreId($store);
  224. foreach($customer->getData() as $k=>$v) $subscriber->setData($k,$v);
  225. if($address = $customer->getDefaultBillingAddress()){
  226. $addressArray = array();
  227. foreach($address->getData() as $k=>$v){
  228. $addressArray[$k] = $v;
  229. }
  230. $subscriber->setAddress($addressArray);
  231. }
  232. if(!isset($params['is_general'])) $params['is_subscribed'] = true;
  233. }else{
  234. $billing = Mage::getSingleton('checkout/session')->getQuote()->getBillingAddress();
  235. if((string)$billing->getEmail() == (string)$subscriber->getEmail()){
  236. $address = array();
  237. foreach($billing->getData() as $k=>$v){
  238. $subscriber->setData($k,$v);
  239. $address[$k] = $v;
  240. }
  241. $subscriber->setAddress($address);
  242. }else{
  243. $subscriber->setFirstname('GUEST')
  244. ->setLastname('GUEST');
  245. }
  246. $params['is_subscribed'] = true;
  247. }
  248. $list = (isset($params['list']))? $params['list'] : array();
  249. if(!count($list)) $list[$this->getGeneralConfig('general',$store)] = 1;
  250. foreach($list as $k=>$v) $this->doTheAction($subscriber,$params,$k);
  251. if(!isset($params['is_general'])){
  252. $allLists = $this->getAvailableLists($store);
  253. foreach($allLists as $k=>$v){
  254. if($k && !array_key_exists($k,$list)){
  255. $params['is_subscribed'] = false;
  256. $this->doTheAction($subscriber,$params,$k);
  257. }
  258. }
  259. }
  260. }
  261. return true;
  262. }
  263. private function doTheAction($customer,$params,$list){
  264. if($customer->getCustomerId() || $customer->getEntityId()){
  265. $subscription = $this->isMailchimpSubscribed($customer,false,$list);
  266. }else{
  267. $subscription = $this->isMailchimpSubscribed($customer,true,$list);
  268. }
  269. $customer->setListId($list);
  270. $mainModel = Mage::getModel('mailchimpPro/mailchimpPro');
  271. $action = $mainModel->getStatus($params, $customer);
  272. $customer->setAction($action);
  273. $mainModel->mainAction($customer,$subscription);
  274. return true;
  275. }
  276. public function webHookFilter($data){
  277. $currentStore = Mage::app()->getDefaultStoreView()->getStoreId();
  278. if($this->mailChimpProEnabled($currentStore)){
  279. $lists = $this->getAvailablelists($currentStore);
  280. $lists = (is_array($lists))? $lists : array();
  281. $generalList = $this->getGeneralConfig('general',$currentStore);
  282. $lists[$generalList] = $generalList;
  283. if(array_key_exists($data['data']['list_id'],$lists)){
  284. $customer = Mage::getModel('mailchimpPro/subscripter')->prepareCustomer($data);
  285. if($customer->getMailchimpproId() || $customer->getAction() == 'subscribe'){
  286. Mage::getModel('mailchimpPro/subscripter')->registerInfo($customer);
  287. }
  288. $email = (isset($data['data']['email']))? $data['data']['email'] : $data['data']['old_email'] ;
  289. if($customer->getAction() == 'subscribe' && !$this->isSubscribed($email) && $data['data']['list_id'] == $generalList){
  290. Mage::getModel('newsletter/subscriber')->subscribe($email,true);
  291. }elseif(($customer->getAction() == 'unsubscribe' || $customer->getAction() == 'cleaned' ) && $data['data']['list_id'] == $generalList && $this->isSubscribed($email)){
  292. Mage::getModel('newsletter/subscriber')->loadByEmail($email)->unsubscribe();
  293. }
  294. }
  295. }
  296. }
  297. public function addException(Exception $e){
  298. $currentStore = Mage::app()->getStore()->getStoreId();
  299. foreach(explode("\n", $e->getMessage()) as $message) {
  300. if ($currentStore == 0){
  301. Mage::getSingleton('adminhtml/session')->addError($this->__('Mailchimp General Error: ').$message);
  302. }else{
  303. Mage::getSingleton('customer/session')->addError($this->__('An error occurred while saving your subscription.'));
  304. }
  305. $this->logInfo($e, $this->__('Mailchimp General Error: ').$message);
  306. }
  307. }
  308. private function logInfo(Exception $exception, $alternativeText){
  309. $message = sprintf('Exception code: '.$exception->getCode().' \n Exception message: '.$exception->getMessage().' \n Trace: '.$exception->getTraceAsString());
  310. Mage::log($message, Zend_Log::DEBUG, 'mailChimpPro_Exceptions.log');
  311. return $this;
  312. }
  313. }
  314. ?>