Data.php 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967
  1. <?php
  2. namespace Dotdigitalgroup\Email\Helper;
  3. use Dotdigitalgroup\Email\Helper\Config as EmailConfig;
  4. use Dotdigitalgroup\Email\Model\Config\Json;
  5. use \Magento\Framework\App\Config\ScopeConfigInterface;
  6. /**
  7. * General most used helper to work with config data, saving updating and generating.
  8. *
  9. * @SuppressWarnings(PHPMD.ExcessivePublicCount)
  10. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  11. * @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
  12. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  13. * @SuppressWarnings(PHPMD.TooManyFields)
  14. */
  15. class Data extends \Magento\Framework\App\Helper\AbstractHelper
  16. {
  17. const MODULE_NAME = 'Dotdigitalgroup_Email';
  18. const DM_FIELD_LIMIT = 250;
  19. /**
  20. * @var \Magento\Config\Model\ResourceModel\Config
  21. */
  22. public $resourceConfig;
  23. /**
  24. * @var \Magento\Store\Model\StoreManagerInterface
  25. */
  26. public $storeManager;
  27. /**
  28. * @var \Dotdigitalgroup\Email\Model\ContactFactory
  29. */
  30. public $contactFactory;
  31. /**
  32. * @var \Magento\Framework\App\ProductMetadata
  33. */
  34. public $productMetadata;
  35. /**
  36. * @var \Magento\Framework\App\ResourceConnection
  37. */
  38. public $adapter;
  39. /**
  40. * @var \Magento\Store\Model\Store
  41. */
  42. public $store;
  43. /**
  44. * @var \Magento\Framework\Module\ModuleListInterface
  45. */
  46. public $fullModuleList;
  47. /**
  48. * @var \Magento\Customer\Model\CustomerFactory
  49. */
  50. public $customerFactory;
  51. /**
  52. * @var File
  53. */
  54. public $fileHelper;
  55. /**
  56. * @var \Magento\Framework\App\Config\Storage\Writer
  57. */
  58. public $writer;
  59. /**
  60. * @var \Dotdigitalgroup\Email\Model\Apiconnector\ClientFactory
  61. */
  62. public $clientFactory;
  63. /**
  64. * @var \Dotdigitalgroup\Email\Helper\ConfigFactory ConfigFactory
  65. */
  66. public $configHelperFactory;
  67. /**
  68. * @var \Magento\Framework\Stdlib\DateTime\DateTime
  69. */
  70. public $datetime;
  71. /**
  72. * @var Json
  73. */
  74. public $serializer;
  75. /**
  76. * @var \Dotdigitalgroup\Email\Model\ResourceModel\Contact
  77. */
  78. public $contactResource;
  79. /**
  80. * @var \Magento\Quote\Model\ResourceModel\Quote
  81. */
  82. private $quoteResource;
  83. /**
  84. * @var \Magento\Quote\Model\QuoteFactory
  85. */
  86. private $quoteFactory;
  87. /**
  88. * @var \Magento\User\Model\ResourceModel\User
  89. */
  90. private $userResource;
  91. /**
  92. * @var \Magento\Framework\Encryption\EncryptorInterface
  93. */
  94. public $encryptor;
  95. /**
  96. * Data constructor.
  97. * @param \Magento\Framework\App\ProductMetadata $productMetadata
  98. * @param \Dotdigitalgroup\Email\Model\ContactFactory $contactFactory
  99. * @param \Dotdigitalgroup\Email\Model\ResourceModel\Contact $contactResource
  100. * @param File $fileHelper
  101. * @param \Magento\Config\Model\ResourceModel\Config $resourceConfig
  102. * @param \Magento\Framework\App\ResourceConnection $adapter
  103. * @param \Magento\Framework\App\Helper\Context $context
  104. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  105. * @param \Magento\Customer\Model\CustomerFactory $customerFactory
  106. * @param \Magento\Framework\Module\ModuleListInterface $moduleListInterface
  107. * @param \Magento\Store\Model\Store $store
  108. * @param \Magento\Framework\App\Config\Storage\Writer $writer
  109. * @param \Dotdigitalgroup\Email\Model\Apiconnector\ClientFactory $clientFactory
  110. * @param ConfigFactory $configHelperFactory
  111. * @param Json $serilizer
  112. * @param \Magento\Framework\Stdlib\DateTime\DateTime $dateTime
  113. * @param \Magento\Quote\Model\ResourceModel\Quote $quoteResource
  114. * @param \Magento\Quote\Model\QuoteFactory $quoteFactory
  115. * @param \Magento\User\Model\ResourceModel\User $userResource
  116. * @var \Magento\Framework\Encryption\EncryptorInterface $encryptor
  117. */
  118. public function __construct(
  119. \Magento\Framework\App\ProductMetadata $productMetadata,
  120. \Dotdigitalgroup\Email\Model\ContactFactory $contactFactory,
  121. \Dotdigitalgroup\Email\Model\ResourceModel\Contact $contactResource,
  122. \Dotdigitalgroup\Email\Helper\File $fileHelper,
  123. \Magento\Config\Model\ResourceModel\Config $resourceConfig,
  124. \Magento\Framework\App\ResourceConnection $adapter,
  125. \Magento\Framework\App\Helper\Context $context,
  126. \Magento\Store\Model\StoreManagerInterface $storeManager,
  127. \Magento\Customer\Model\CustomerFactory $customerFactory,
  128. \Magento\Framework\Module\ModuleListInterface $moduleListInterface,
  129. \Magento\Store\Model\Store $store,
  130. \Magento\Framework\App\Config\Storage\Writer $writer,
  131. \Dotdigitalgroup\Email\Model\Apiconnector\ClientFactory $clientFactory,
  132. \Dotdigitalgroup\Email\Helper\ConfigFactory $configHelperFactory,
  133. \Dotdigitalgroup\Email\Model\Config\Json $serilizer,
  134. \Magento\Framework\Stdlib\DateTime\DateTime $dateTime,
  135. \Magento\Quote\Model\ResourceModel\Quote $quoteResource,
  136. \Magento\Quote\Model\QuoteFactory $quoteFactory,
  137. \Magento\User\Model\ResourceModel\User $userResource,
  138. \Magento\Framework\Encryption\EncryptorInterface $encryptor
  139. ) {
  140. $this->serializer = $serilizer;
  141. $this->adapter = $adapter;
  142. $this->productMetadata = $productMetadata;
  143. $this->contactFactory = $contactFactory;
  144. $this->resourceConfig = $resourceConfig;
  145. $this->storeManager = $storeManager;
  146. $this->customerFactory = $customerFactory;
  147. $this->fullModuleList = $moduleListInterface;
  148. $this->store = $store;
  149. $this->writer = $writer;
  150. $this->clientFactory = $clientFactory;
  151. $this->configHelperFactory = $configHelperFactory;
  152. $this->datetime = $dateTime;
  153. $this->quoteResource = $quoteResource;
  154. $this->quoteFactory = $quoteFactory;
  155. $this->userResource = $userResource;
  156. $this->contactResource = $contactResource;
  157. $this->encryptor = $encryptor;
  158. parent::__construct($context);
  159. $this->fileHelper = $fileHelper;
  160. }
  161. /**
  162. * Get api creadentials enabled.
  163. *
  164. * @param int $website
  165. *
  166. * @return bool
  167. */
  168. public function isEnabled($website = 0)
  169. {
  170. $website = $this->storeManager->getWebsite($website);
  171. $enabled = $this->scopeConfig->isSetFlag(
  172. \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_API_ENABLED,
  173. \Magento\Store\Model\ScopeInterface::SCOPE_WEBSITE,
  174. $website
  175. );
  176. $apiUsername = $this->getApiUsername($website);
  177. $apiPassword = $this->getApiPassword($website);
  178. if (! $apiUsername || ! $apiPassword || ! $enabled) {
  179. return false;
  180. }
  181. return true;
  182. }
  183. /**
  184. * @param int $storeId
  185. *
  186. * @return bool
  187. */
  188. public function isStoreEnabled($storeId)
  189. {
  190. return $this->scopeConfig->isSetFlag(
  191. \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_API_ENABLED,
  192. \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
  193. $storeId
  194. );
  195. }
  196. /**
  197. * Get all stores.
  198. *
  199. * @param bool|false $default
  200. *
  201. * @return \Magento\Store\Api\Data\StoreInterface[]
  202. */
  203. public function getStores($default = false)
  204. {
  205. return $this->storeManager->getStores($default);
  206. }
  207. /**
  208. * Passcode for dynamic content liks.
  209. *
  210. * @param string $authRequest
  211. *
  212. * @return bool
  213. */
  214. public function auth($authRequest)
  215. {
  216. if ($authRequest != $this->scopeConfig->getValue(
  217. Config::XML_PATH_CONNECTOR_DYNAMIC_CONTENT_PASSCODE
  218. )
  219. ) {
  220. return false;
  221. }
  222. return true;
  223. }
  224. /**
  225. * Check for IP address to match the ones from config.
  226. *
  227. * @return bool
  228. */
  229. public function isIpAllowed()
  230. {
  231. if ($ipString = $this->getConfigValue(
  232. \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_IP_RESTRICTION_ADDRESSES,
  233. 'default'
  234. )
  235. ) {
  236. //string to array
  237. $ipArray = explode(',', $ipString);
  238. //remove white spaces
  239. foreach ($ipArray as $key => $ip) {
  240. $ipArray[$key] = trim($ip);
  241. }
  242. //ip address
  243. $ipAddress = $this->_remoteAddress->getRemoteAddress();
  244. if (in_array($ipAddress, $ipArray)) {
  245. return true;
  246. }
  247. } else {
  248. //empty ip list from configuration will ignore the validation
  249. return true;
  250. }
  251. $this->log(sprintf("Failed to authenticate IP address - %s", $ipAddress));
  252. return false;
  253. }
  254. /**
  255. * Get config scope value.
  256. *
  257. * @param string $path
  258. * @param string $contextScope
  259. * @param null $contextScopeId
  260. *
  261. * @return int|float|string|boolean
  262. */
  263. private function getConfigValue(
  264. $path,
  265. $contextScope = 'default',
  266. $contextScopeId = null
  267. ) {
  268. $config = $this->scopeConfig->getValue(
  269. $path,
  270. $contextScope,
  271. $contextScopeId
  272. );
  273. return $config;
  274. }
  275. /**
  276. * Customer id datafield.
  277. *
  278. * @return string
  279. */
  280. public function getMappedCustomerId()
  281. {
  282. return $this->getConfigValue(
  283. \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_MAPPING_CUSTOMER_ID,
  284. 'default'
  285. );
  286. }
  287. /**
  288. * Order id datafield.
  289. *
  290. * @return string
  291. */
  292. public function getMappedOrderId()
  293. {
  294. return $this->getConfigValue(
  295. \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_MAPPING_LAST_ORDER_ID,
  296. 'default'
  297. );
  298. }
  299. /**
  300. * Get website selected in admin.
  301. *
  302. * @return \Magento\Store\Api\Data\WebsiteInterface
  303. */
  304. public function getWebsite()
  305. {
  306. $websiteId = $this->_request->getParam('website', false);
  307. if ($websiteId) {
  308. return $this->storeManager->getWebsite($websiteId);
  309. }
  310. return $this->storeManager->getWebsite();
  311. }
  312. /**
  313. * Get website for selected scope in admin
  314. *
  315. * @return \Magento\Store\Api\Data\WebsiteInterface
  316. */
  317. public function getWebsiteForSelectedScopeInAdmin()
  318. {
  319. //If website param does not exist then default value returned 0 "default scope"
  320. //This is because there is no website param in default scope
  321. $websiteId = $this->_request->getParam('website', 0);
  322. return $this->storeManager->getWebsite($websiteId);
  323. }
  324. /**
  325. * Get passcode from config.
  326. *
  327. * @return string
  328. */
  329. public function getPasscode()
  330. {
  331. $websiteId = (int) $this->_request->getParam('website', false);
  332. $scope = 'default';
  333. $scopeId = '0';
  334. if ($websiteId) {
  335. $scope = 'website';
  336. $scopeId = $websiteId;
  337. }
  338. $passcode = $this->getConfigValue(
  339. \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_DYNAMIC_CONTENT_PASSCODE,
  340. $scope,
  341. $scopeId
  342. );
  343. return $passcode;
  344. }
  345. /**
  346. * Save config data.
  347. *
  348. * @param string $path
  349. * @param string $value
  350. * @param string $scope
  351. * @param int $scopeId
  352. *
  353. * @return null
  354. */
  355. public function saveConfigData($path, $value, $scope, $scopeId)
  356. {
  357. $this->resourceConfig->saveConfig(
  358. $path,
  359. $value,
  360. $scope,
  361. $scopeId
  362. );
  363. }
  364. /**
  365. * @param string $path
  366. * @param string $scope
  367. * @param int $scopeId
  368. */
  369. public function deleteConfigData($path, $scope, $scopeId)
  370. {
  371. $this->resourceConfig->deleteConfig(
  372. $path,
  373. $scope,
  374. $scopeId
  375. );
  376. }
  377. /**
  378. * Disable wishlist sync.
  379. *
  380. * @param string $scope
  381. * @param int $scopeId
  382. *
  383. * @return null
  384. */
  385. public function disableTransactionalDataConfig($scope, $scopeId)
  386. {
  387. $this->resourceConfig->saveConfig(
  388. \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_SYNC_WISHLIST_ENABLED,
  389. 0,
  390. $scope,
  391. $scopeId
  392. );
  393. }
  394. /**
  395. * Last order id datafield.
  396. *
  397. * @return string
  398. */
  399. public function getLastOrderId()
  400. {
  401. return $this->getConfigValue(
  402. \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_CUSTOMER_LAST_ORDER_ID,
  403. 'default'
  404. );
  405. }
  406. /**
  407. * Log data into the connector file.
  408. * @param string $data
  409. *
  410. * @return null
  411. */
  412. public function log($data)
  413. {
  414. $this->fileHelper->info($data);
  415. }
  416. /**
  417. *
  418. * @param string $message
  419. * @param array $extra
  420. *
  421. * @return null
  422. */
  423. public function debug($message, $extra)
  424. {
  425. $this->fileHelper->debug($message, $extra);
  426. }
  427. /**
  428. *
  429. * @param string $message
  430. * @param array $extra
  431. *
  432. * @return null
  433. */
  434. public function error($message, $extra)
  435. {
  436. $this->debug($message, $extra);
  437. }
  438. /**
  439. * Get if the log is enabled for connector.
  440. *
  441. * @return bool
  442. */
  443. public function isDebugEnabled()
  444. {
  445. return $this->scopeConfig->isSetFlag(
  446. \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_ADVANCED_DEBUG_ENABLED
  447. );
  448. }
  449. /**
  450. * Is the page tracking enabled.
  451. *
  452. * @return bool
  453. */
  454. public function isPageTrackingEnabled()
  455. {
  456. return $this->scopeConfig->isSetFlag(
  457. \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_PAGE_TRACKING_ENABLED
  458. );
  459. }
  460. /**
  461. * Is the Roi page tracking enabled.
  462. *
  463. * @return bool
  464. */
  465. public function isRoiTrackingEnabled()
  466. {
  467. return (bool)$this->scopeConfig->isSetFlag(
  468. \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_ROI_TRACKING_ENABLED
  469. );
  470. }
  471. /**
  472. * Store name datafield.
  473. *
  474. * @param \Magento\Store\Model\Website $website
  475. *
  476. * @return boolean|string
  477. */
  478. public function getMappedStoreName(\Magento\Store\Model\Website $website)
  479. {
  480. $mapped = $website->getConfig(
  481. \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_MAPPING_CUSTOMER_STORENAME
  482. );
  483. $storeName = ($mapped) ? $mapped : '';
  484. return $storeName;
  485. }
  486. /**
  487. * Get the contact id for the customer based on website id.
  488. *
  489. * @param string $email
  490. * @param int $websiteId
  491. *
  492. * @return bool|string
  493. */
  494. public function getContactId($email, $websiteId)
  495. {
  496. if (! $this->isEnabled($websiteId)) {
  497. return false;
  498. }
  499. $contactFromTable = $this->getContactByEmail($email, $websiteId);
  500. if ($contactId = $contactFromTable->getContactId()) {
  501. return $contactId;
  502. }
  503. $contact = $this->getContact($email, $websiteId, $contactFromTable);
  504. if ($contact && isset($contact->id)) {
  505. return $contact->id;
  506. }
  507. return false;
  508. }
  509. /**
  510. * @param string $email
  511. * @param int $websiteId
  512. * @param boolean $contactFromTable
  513. *
  514. * @return bool|object
  515. */
  516. public function getContact($email, $websiteId, $contactFromTable = false)
  517. {
  518. if (! $this->isEnabled($websiteId)) {
  519. return false;
  520. }
  521. if ($contactFromTable) {
  522. $contact = $contactFromTable;
  523. } else {
  524. $contact = $this->contactFactory->create()
  525. ->loadByCustomerEmail($email, $websiteId);
  526. }
  527. $client = $this->getWebsiteApiClient($websiteId);
  528. $response = $client->getContactByEmail($email);
  529. if (! isset($response->id)) {
  530. $response = $client->postContacts($email);
  531. }
  532. if (isset($response->message)) {
  533. $contact->setEmailImported(1);
  534. if ($response->message == \Dotdigitalgroup\Email\Model\Apiconnector\Client::API_ERROR_CONTACT_SUPPRESSED) {
  535. $contact->setSuppressed(1);
  536. }
  537. $this->saveContact($contact);
  538. return false;
  539. }
  540. //save contact id
  541. if (isset($response->id)) {
  542. $contact->setContactId($response->id);
  543. $this->saveContact($contact);
  544. } else {
  545. //curl operation timeout
  546. return false;
  547. }
  548. return $response;
  549. }
  550. /**
  551. * Api client by website.
  552. *
  553. * @param int $website
  554. * @param string $username
  555. * @param string $password
  556. *
  557. *
  558. * @return \Dotdigitalgroup\Email\Model\Apiconnector\Client
  559. */
  560. public function getWebsiteApiClient($website = 0, $username = '', $password = '')
  561. {
  562. if ($username && $password) {
  563. $apiUsername = $username;
  564. $apiPassword = $password;
  565. } else {
  566. $apiUsername = $this->getApiUsername($website);
  567. $apiPassword = $this->getApiPassword($website);
  568. }
  569. $client = $this->clientFactory->create();
  570. $client->setApiUsername($apiUsername)
  571. ->setApiPassword($apiPassword);
  572. $websiteId = $this->storeManager->getWebsite($website)->getId();
  573. //Get api endpoint
  574. $apiEndpoint = $this->getApiEndpoint($websiteId, $client);
  575. //Set api endpoint on client
  576. if ($apiEndpoint) {
  577. $client->setApiEndpoint($apiEndpoint);
  578. }
  579. return $client;
  580. }
  581. /**
  582. * Get Api endPoint
  583. *
  584. * @param int $websiteId
  585. * @param \Dotdigitalgroup\Email\Model\Apiconnector\Client $client
  586. *
  587. * @return string|
  588. */
  589. public function getApiEndpoint($websiteId, $client)
  590. {
  591. //Get from DB
  592. $apiEndpoint = $this->getApiEndPointFromConfig($websiteId);
  593. //Nothing from DB then fetch from api
  594. if (!$apiEndpoint) {
  595. $apiEndpoint = $this->getApiEndPointFromApi($client);
  596. //Save it in DB
  597. if ($apiEndpoint) {
  598. $this->saveApiEndpoint($apiEndpoint, $websiteId);
  599. }
  600. }
  601. return $apiEndpoint;
  602. }
  603. /**
  604. * Get api end point from api
  605. *
  606. * @param \Dotdigitalgroup\Email\Model\Apiconnector\Client $client
  607. *
  608. * @return string|boolean
  609. */
  610. public function getApiEndPointFromApi($client)
  611. {
  612. $accountInfo = $client->getAccountInfo();
  613. $apiEndpoint = false;
  614. if (is_object($accountInfo) && !isset($accountInfo->message)) {
  615. //save endpoint for account
  616. foreach ($accountInfo->properties as $property) {
  617. if ($property->name == 'ApiEndpoint' && !empty($property->value)) {
  618. $apiEndpoint = $property->value;
  619. break;
  620. }
  621. }
  622. }
  623. return $apiEndpoint;
  624. }
  625. /**
  626. * Get api end point for given website
  627. *
  628. * @param int $websiteId
  629. *
  630. * @return string|boolean
  631. */
  632. public function getApiEndPointFromConfig($websiteId)
  633. {
  634. if ($websiteId > 0) {
  635. $apiEndpoint = $this->getWebsiteConfig(
  636. \Dotdigitalgroup\Email\Helper\Config::PATH_FOR_API_ENDPOINT,
  637. $websiteId
  638. );
  639. } else {
  640. $apiEndpoint = $this->getWebsiteConfig(
  641. \Dotdigitalgroup\Email\Helper\Config::PATH_FOR_API_ENDPOINT,
  642. $websiteId,
  643. ScopeConfigInterface::SCOPE_TYPE_DEFAULT
  644. );
  645. }
  646. return $apiEndpoint;
  647. }
  648. /**
  649. * Save api endpoint into config.
  650. *
  651. * @param string $apiEndpoint
  652. * @param int $websiteId
  653. *
  654. * @return null
  655. */
  656. public function saveApiEndpoint($apiEndpoint, $websiteId)
  657. {
  658. if ($websiteId > 0) {
  659. $scope = \Magento\Store\Model\ScopeInterface::SCOPE_WEBSITE;
  660. } else {
  661. $scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT;
  662. }
  663. $this->writer->save(
  664. \Dotdigitalgroup\Email\Helper\Config::PATH_FOR_API_ENDPOINT,
  665. $apiEndpoint,
  666. $scope,
  667. $websiteId
  668. );
  669. }
  670. /**
  671. * @param int $website
  672. *
  673. * @return string|boolean
  674. */
  675. public function getApiUsername($website = 0)
  676. {
  677. return $this->getWebsiteConfig(
  678. \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_API_USERNAME,
  679. $website
  680. );
  681. }
  682. /**
  683. * @param int $website
  684. *
  685. * @return string|boolean
  686. */
  687. public function getApiPassword($website = 0)
  688. {
  689. $value = $this->getWebsiteConfig(
  690. \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_API_PASSWORD,
  691. $website
  692. );
  693. return $this->encryptor->decrypt($value);
  694. }
  695. /**
  696. * Get the addres book for customer.
  697. *
  698. * @param int $website
  699. *
  700. * @return string
  701. */
  702. public function getCustomerAddressBook($website = 0)
  703. {
  704. $website = $this->storeManager->getWebsite($website);
  705. return $this->scopeConfig->getValue(
  706. \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_CUSTOMERS_ADDRESS_BOOK_ID,
  707. \Magento\Store\Model\ScopeInterface::SCOPE_WEBSITE,
  708. $website
  709. );
  710. }
  711. /**
  712. * Subscriber address book.
  713. *
  714. * @param \Magento\Store\Api\Data\WebsiteInterface|int $website
  715. *
  716. * @return string|boolean
  717. */
  718. public function getSubscriberAddressBook($website)
  719. {
  720. $website = $this->storeManager->getWebsite($website);
  721. return $this->scopeConfig->getValue(
  722. \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_SUBSCRIBERS_ADDRESS_BOOK_ID,
  723. \Magento\Store\Model\ScopeInterface::SCOPE_WEBSITE,
  724. $website->getId()
  725. );
  726. }
  727. /**
  728. * Guest address book.
  729. *
  730. * @param \Magento\Store\Api\Data\WebsiteInterface|int $website
  731. *
  732. * @return string|boolean
  733. */
  734. public function getGuestAddressBook($website)
  735. {
  736. $website = $this->storeManager->getWebsite($website);
  737. return $this->scopeConfig->getValue(
  738. \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_GUEST_ADDRESS_BOOK_ID,
  739. \Magento\Store\Model\ScopeInterface::SCOPE_WEBSITE,
  740. $website->getid()
  741. );
  742. }
  743. /**
  744. * Get all websites.
  745. *
  746. * @param bool|false $default
  747. *
  748. * @return \Magento\Store\Api\Data\WebsiteInterface[]
  749. */
  750. public function getWebsites($default = false)
  751. {
  752. return $this->storeManager->getWebsites($default);
  753. }
  754. /**
  755. * Get custom datafield mapped.
  756. *
  757. * @param int $website
  758. *
  759. * @return array|mixed
  760. */
  761. public function getCustomAttributes($website = 0)
  762. {
  763. $attr = $this->scopeConfig->getValue(
  764. \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_MAPPING_CUSTOM_DATAFIELDS,
  765. \Magento\Store\Model\ScopeInterface::SCOPE_WEBSITE,
  766. $website->getId()
  767. );
  768. if (!$attr) {
  769. return [];
  770. }
  771. return $this->serializer->unserialize($attr);
  772. }
  773. /**
  774. * Get callback authorization link.
  775. *
  776. * @return string
  777. */
  778. public function getRedirectUri()
  779. {
  780. $callback = $this->configHelperFactory->create()
  781. ->getCallbackUrl();
  782. return $callback;
  783. }
  784. /**
  785. * Order status config value.
  786. *
  787. * @param int $website
  788. *
  789. * @return array|bool
  790. */
  791. public function getConfigSelectedStatus($website = 0)
  792. {
  793. $status = $this->getWebsiteConfig(
  794. \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_SYNC_ORDER_STATUS,
  795. $website
  796. );
  797. if ($status) {
  798. return explode(',', $status);
  799. } else {
  800. return false;
  801. }
  802. }
  803. /**
  804. * Get website config.
  805. *
  806. * @param string $path
  807. * @param int $website
  808. * @param string $scope
  809. *
  810. * @return string|boolean
  811. */
  812. public function getWebsiteConfig($path, $website = 0, $scope = \Magento\Store\Model\ScopeInterface::SCOPE_WEBSITE)
  813. {
  814. return $this->scopeConfig->getValue(
  815. $path,
  816. $scope,
  817. $website
  818. );
  819. }
  820. /**
  821. * Get array of custom attributes for orders from config.
  822. *
  823. * @param int $website
  824. *
  825. * @return array|bool
  826. */
  827. public function getConfigSelectedCustomOrderAttributes($website = 0)
  828. {
  829. $customAttributes = $this->getWebsiteConfig(
  830. \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_CUSTOM_ORDER_ATTRIBUTES,
  831. $website
  832. );
  833. if ($customAttributes) {
  834. return explode(',', $customAttributes);
  835. } else {
  836. return false;
  837. }
  838. }
  839. /**
  840. * Mark contact for reimport.
  841. *
  842. * @param int $customerId
  843. *
  844. * @return null
  845. */
  846. public function setConnectorContactToReImport($customerId)
  847. {
  848. $contactModel = $this->contactFactory->create();
  849. $contactModel->loadByCustomerId($customerId)
  850. ->setEmailImported(
  851. \Dotdigitalgroup\Email\Model\Contact::EMAIL_CONTACT_NOT_IMPORTED
  852. );
  853. $this->contactResource->save($contactModel);
  854. }
  855. /**
  856. * Disable website config when the request is made admin area only!
  857. *
  858. * @param string $path
  859. *
  860. * @return null
  861. */
  862. public function disableConfigForWebsite($path)
  863. {
  864. $scopeId = 0;
  865. if ($website = $this->_request->getParam('website')) {
  866. $scope = 'websites';
  867. $scopeId = $this->storeManager->getWebsite($website)->getId();
  868. } else {
  869. $scope = 'default';
  870. }
  871. $this->resourceConfig->saveConfig(
  872. $path,
  873. 0,
  874. $scope,
  875. $scopeId
  876. );
  877. }
  878. /**
  879. * Number of customers with duplicate emails, emails as total number.
  880. *
  881. * @return \Magento\Customer\Model\ResourceModel\Customer\Collection
  882. */
  883. public function getCustomersWithDuplicateEmails()
  884. {
  885. $customers = $this->customerFactory->create()
  886. ->getCollection();
  887. //duplicate emails
  888. $customers->getSelect()
  889. ->columns(['emails' => 'COUNT(e.entity_id)'])
  890. ->group('email')
  891. ->having('emails > ?', 1);
  892. return $customers;
  893. }
  894. /**
  895. * Generate the baseurl for the default store
  896. * dynamic content will be displayed.
  897. *
  898. * @return string
  899. */
  900. public function generateDynamicUrl()
  901. {
  902. $website = $this->_request->getParam('website', false);
  903. //set website url for the default store id
  904. $website = ($website) ? $this->storeManager->getWebsite($website) : 0;
  905. $defaultGroup = $this->storeManager->getWebsite($website)
  906. ->getDefaultGroup();
  907. if (!$defaultGroup) {
  908. return $this->storeManager->getStore()->getBaseUrl(
  909. \Magento\Framework\UrlInterface::URL_TYPE_WEB
  910. );
  911. }
  912. //base url
  913. $baseUrl = $this->storeManager->getStore(
  914. $defaultGroup->getDefaultStore()
  915. )->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_LINK);
  916. return $baseUrl;
  917. }
  918. /**
  919. * get sales_flat_order table description.
  920. *
  921. * @return array
  922. */
  923. public function getOrderTableDescription()
  924. {
  925. $salesTable = $this->adapter->getTableName('sales_order');
  926. $adapter = $this->adapter->getConnection('sales');
  927. $columns = $adapter->describeTable($salesTable);
  928. return $columns;
  929. }
  930. /**
  931. * Is email capture enabled.
  932. *
  933. * @return bool
  934. */
  935. public function isEasyEmailCaptureEnabled()
  936. {
  937. return $this->scopeConfig->isSetFlag(
  938. \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_EMAIL_CAPTURE
  939. );
  940. }
  941. /**
  942. * Is email capture for newsletter enabled.
  943. *
  944. * @return bool
  945. */
  946. public function isEasyEmailCaptureForNewsletterEnabled()
  947. {
  948. return $this->scopeConfig->isSetFlag(
  949. \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_EMAIL_CAPTURE_NEWSLETTER
  950. );
  951. }
  952. /**
  953. * Get feefo logon config value.
  954. *
  955. * @return string
  956. */
  957. public function getFeefoLogon()
  958. {
  959. return $this->getWebsiteConfig(
  960. \Dotdigitalgroup\Email\Helper\Config::XML_PATH_REVIEWS_FEEFO_LOGON
  961. );
  962. }
  963. /**
  964. * Get feefo reviews limit config value.
  965. *
  966. * @return string
  967. */
  968. public function getFeefoReviewsPerProduct()
  969. {
  970. return $this->getWebsiteConfig(
  971. \Dotdigitalgroup\Email\Helper\Config::XML_PATH_REVIEWS_FEEFO_REVIEWS
  972. );
  973. }
  974. /**
  975. * Get feefo logo template config value.
  976. *
  977. * @return string
  978. */
  979. public function getFeefoLogoTemplate()
  980. {
  981. return $this->getWebsiteConfig(
  982. \Dotdigitalgroup\Email\Helper\Config::XML_PATH_REVIEWS_FEEFO_TEMPLATE
  983. );
  984. }
  985. /**
  986. * Update data fields.
  987. *
  988. * @param string $email
  989. * @param \Magento\Store\Api\Data\WebsiteInterface $website
  990. * @param string $storeName
  991. *
  992. * @return null
  993. */
  994. public function updateDataFields($email, $website, $storeName)
  995. {
  996. $data = [];
  997. if ($storeNameKey = $website->getConfig(
  998. \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_CUSTOMER_STORE_NAME
  999. )
  1000. ) {
  1001. $data[] = [
  1002. 'Key' => $storeNameKey,
  1003. 'Value' => $storeName,
  1004. ];
  1005. }
  1006. if ($websiteName = $website->getConfig(
  1007. \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_CUSTOMER_WEBSITE_NAME
  1008. )
  1009. ) {
  1010. $data[] = [
  1011. 'Key' => $websiteName,
  1012. 'Value' => $website->getName(),
  1013. ];
  1014. }
  1015. if (!empty($data)) {
  1016. //update data fields
  1017. if ($this->isEnabled($website)) {
  1018. $client = $this->getWebsiteApiClient($website);
  1019. $client->updateContactDatafieldsByEmail($email, $data);
  1020. }
  1021. }
  1022. }
  1023. /**
  1024. * Update last quote id datafield.
  1025. *
  1026. * @param int $quoteId
  1027. * @param string $email
  1028. * @param int $websiteId
  1029. *
  1030. * @return null
  1031. */
  1032. public function updateLastQuoteId($quoteId, $email, $websiteId)
  1033. {
  1034. if ($this->isEnabled($websiteId)) {
  1035. $client = $this->getWebsiteApiClient($websiteId);
  1036. //last quote id config data mapped
  1037. $quoteIdField = $this->getLastQuoteId();
  1038. $data[] = [
  1039. 'Key' => $quoteIdField,
  1040. 'Value' => $quoteId,
  1041. ];
  1042. //update datafields for conctact
  1043. $client->updateContactDatafieldsByEmail($email, $data);
  1044. }
  1045. }
  1046. /**
  1047. * Get last quote id datafield.
  1048. *
  1049. * @return string
  1050. */
  1051. public function getLastQuoteId()
  1052. {
  1053. return $this->getConfigValue(
  1054. \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_MAPPING_LAST_QUOTE_ID,
  1055. 'default'
  1056. );
  1057. }
  1058. /**
  1059. * Get order sync enabled value from configuration.
  1060. *
  1061. * @param int $websiteId
  1062. *
  1063. * @return bool
  1064. */
  1065. public function isOrderSyncEnabled($websiteId = 0)
  1066. {
  1067. return $this->scopeConfig->isSetFlag(
  1068. \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_SYNC_ORDER_ENABLED,
  1069. \Magento\Store\Model\ScopeInterface::SCOPE_WEBSITE,
  1070. $websiteId
  1071. );
  1072. }
  1073. /**
  1074. * Get the catalog sync enabled value from config.
  1075. *
  1076. * @param int $websiteId
  1077. *
  1078. * @return bool
  1079. */
  1080. public function isCatalogSyncEnabled($websiteId = 0)
  1081. {
  1082. return $this->scopeConfig->isSetFlag(
  1083. \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_SYNC_CATALOG_ENABLED,
  1084. \Magento\Store\Model\ScopeInterface::SCOPE_WEBSITE,
  1085. $websiteId
  1086. );
  1087. }
  1088. /**
  1089. * Customer sync enabled.
  1090. *
  1091. * @param int $website
  1092. *
  1093. * @return bool
  1094. */
  1095. public function isCustomerSyncEnabled($website = 0)
  1096. {
  1097. $website = $this->storeManager->getWebsite($website);
  1098. return $this->scopeConfig->isSetFlag(
  1099. \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_SYNC_CUSTOMER_ENABLED,
  1100. \Magento\Store\Model\ScopeInterface::SCOPE_WEBSITE,
  1101. $website
  1102. );
  1103. }
  1104. /**
  1105. * Customer sync size limit.
  1106. *
  1107. * @param int $website
  1108. *
  1109. * @return string|boolean
  1110. */
  1111. public function getSyncLimit($website = 0)
  1112. {
  1113. $website = $this->storeManager->getWebsite($website);
  1114. return $this->scopeConfig->getValue(
  1115. \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_SYNC_LIMIT,
  1116. \Magento\Store\Model\ScopeInterface::SCOPE_WEBSITE,
  1117. $website
  1118. );
  1119. }
  1120. /**
  1121. * Get the guest sync enabled value.
  1122. *
  1123. * @param int $websiteId
  1124. *
  1125. * @return bool
  1126. */
  1127. public function isGuestSyncEnabled($websiteId = 0)
  1128. {
  1129. return $this->scopeConfig->isSetFlag(
  1130. \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_SYNC_GUEST_ENABLED,
  1131. \Magento\Store\Model\ScopeInterface::SCOPE_WEBSITE,
  1132. $websiteId
  1133. );
  1134. }
  1135. /**
  1136. * Is subscriber sync enabled.
  1137. *
  1138. * @param int $websiteId
  1139. *
  1140. * @return bool
  1141. */
  1142. public function isSubscriberSyncEnabled($websiteId = 0)
  1143. {
  1144. return $this->scopeConfig->getValue(
  1145. \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_SYNC_SUBSCRIBER_ENABLED,
  1146. \Magento\Store\Model\ScopeInterface::SCOPE_WEBSITE,
  1147. $websiteId
  1148. );
  1149. }
  1150. /**
  1151. * Get customer datafields mapped - exclude custom attributes.
  1152. *
  1153. * @param \Magento\Store\Api\Data\WebsiteInterface $website
  1154. *
  1155. * @return array|boolean
  1156. */
  1157. public function getWebsiteCustomerMappingDatafields($website)
  1158. {
  1159. //customer mapped data
  1160. $store = $website->getDefaultStore();
  1161. $mappedData = $this->scopeConfig->getValue(
  1162. 'connector_data_mapping/customer_data',
  1163. \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
  1164. $store->getId()
  1165. );
  1166. unset($mappedData['custom_attributes'], $mappedData['abandoned_prod_name']);
  1167. //skip non mapped customer datafields
  1168. foreach ($mappedData as $key => $value) {
  1169. if (!$value) {
  1170. unset($mappedData[$key]);
  1171. }
  1172. }
  1173. return $mappedData;
  1174. }
  1175. /**
  1176. * Get the config id by the automation type.
  1177. *
  1178. * @param string $automationType
  1179. * @param int $storeId
  1180. *
  1181. * @return string|boolean
  1182. */
  1183. public function getAutomationIdByType($automationType, $storeId = 0)
  1184. {
  1185. $path = constant(EmailConfig::class . '::' . $automationType);
  1186. $automationCampaignId = $this->scopeConfig->getValue(
  1187. $path,
  1188. \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
  1189. $storeId
  1190. );
  1191. return $automationCampaignId;
  1192. }
  1193. /**
  1194. * Api- update the product name most expensive.
  1195. *
  1196. * @param string $name
  1197. * @param string $email
  1198. * @param int $websiteId
  1199. *
  1200. * @return null
  1201. */
  1202. public function updateAbandonedProductName($name, $email, $websiteId)
  1203. {
  1204. if ($this->isEnabled($websiteId)) {
  1205. $client = $this->getWebsiteApiClient($websiteId);
  1206. // id config data mapped
  1207. $field = $this->getAbandonedProductName();
  1208. if ($field) {
  1209. $data[] = [
  1210. 'Key' => $field,
  1211. 'Value' => $name,
  1212. ];
  1213. //update data field for contact
  1214. $client->updateContactDatafieldsByEmail($email, $data);
  1215. }
  1216. }
  1217. }
  1218. /**
  1219. * Get mapped product name.
  1220. *
  1221. * @return boolean|string
  1222. */
  1223. public function getAbandonedProductName()
  1224. {
  1225. return $this->scopeConfig->getValue(
  1226. \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_ABANDONED_PRODUCT_NAME
  1227. );
  1228. }
  1229. /**
  1230. * Trigger log for api calls longer then config value.
  1231. *
  1232. * @param int $websiteId
  1233. *
  1234. * @return boolean|string
  1235. */
  1236. public function getApiResponseTimeLimit($websiteId = 0)
  1237. {
  1238. $website = $this->storeManager->getWebsite($websiteId);
  1239. $limit = $website->getConfig(
  1240. \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_DEBUG_API_REQUEST_LIMIT
  1241. );
  1242. return $limit;
  1243. }
  1244. /**
  1245. * Get url for email capture.
  1246. *
  1247. * @return string
  1248. */
  1249. public function getEmailCaptureUrl()
  1250. {
  1251. return $this->storeManager->getStore()->getUrl(
  1252. 'connector/ajax/emailcapture',
  1253. ['_secure' => $this->storeManager->getStore()->isCurrentlySecure()]
  1254. );
  1255. }
  1256. /**
  1257. * Product review from config to link the product link.
  1258. *
  1259. * @param int $website
  1260. *
  1261. * @return boolean|string
  1262. */
  1263. public function getReviewReminderAnchor($website)
  1264. {
  1265. return $this->getWebsiteConfig(
  1266. \Dotdigitalgroup\Email\Helper\Config::XML_PATH_AUTOMATION_REVIEW_ANCHOR,
  1267. $website
  1268. );
  1269. }
  1270. /**
  1271. * Dynamic styles from config.
  1272. *
  1273. * @return array
  1274. */
  1275. public function getDynamicStyles()
  1276. {
  1277. return [
  1278. 'nameStyle' => explode(
  1279. ',',
  1280. $this->getConfigValue(
  1281. \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_DYNAMIC_NAME_STYLE
  1282. )
  1283. ),
  1284. 'priceStyle' => explode(
  1285. ',',
  1286. $this->getConfigValue(
  1287. \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_DYNAMIC_PRICE_STYLE
  1288. )
  1289. ),
  1290. 'linkStyle' => explode(
  1291. ',',
  1292. $this->getConfigValue(
  1293. \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_DYNAMIC_LINK_STYLE
  1294. )
  1295. ),
  1296. 'otherStyle' => explode(
  1297. ',',
  1298. $this->getConfigValue(
  1299. \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_DYNAMIC_OTHER_STYLE
  1300. )
  1301. ),
  1302. 'nameColor' => $this->getConfigValue(
  1303. \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_DYNAMIC_NAME_COLOR
  1304. ),
  1305. 'fontSize' => $this->getConfigValue(
  1306. \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_DYNAMIC_NAME_FONT_SIZE
  1307. ),
  1308. 'priceColor' => $this->getConfigValue(
  1309. \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_DYNAMIC_PRICE_COLOR
  1310. ),
  1311. 'priceFontSize' => $this->getConfigValue(
  1312. \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_DYNAMIC_PRICE_FONT_SIZE
  1313. ),
  1314. 'urlColor' => $this->getConfigValue(
  1315. \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_DYNAMIC_LINK_COLOR
  1316. ),
  1317. 'urlFontSize' => $this->getConfigValue(
  1318. \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_DYNAMIC_LINK_FONT_SIZE
  1319. ),
  1320. 'otherColor' => $this->getConfigValue(
  1321. \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_DYNAMIC_OTHER_COLOR
  1322. ),
  1323. 'otherFontSize' => $this->getConfigValue(
  1324. \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_DYNAMIC_OTHER_FONT_SIZE
  1325. ),
  1326. 'docFont' => $this->getConfigValue(
  1327. \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_DYNAMIC_DOC_FONT
  1328. ),
  1329. 'docBackgroundColor' => $this->getConfigValue(
  1330. \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_DYNAMIC_DOC_BG_COLOR
  1331. ),
  1332. 'dynamicStyling' => $this->getConfigValue(
  1333. \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_DYNAMIC_STYLING
  1334. ),
  1335. ];
  1336. }
  1337. /**
  1338. * Get display type for review product.
  1339. *
  1340. * @param int $website
  1341. *
  1342. * @return boolean|string
  1343. */
  1344. public function getReviewDisplayType($website)
  1345. {
  1346. return $this->getWebsiteConfig(
  1347. \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_DYNAMIC_CONTENT_REVIEW_DISPLAY_TYPE,
  1348. $website
  1349. );
  1350. }
  1351. /**
  1352. * Get config value on website level.
  1353. *
  1354. * @param string $path
  1355. * @param int $website
  1356. *
  1357. * @return boolean|string
  1358. */
  1359. public function getReviewWebsiteSettings($path, $website)
  1360. {
  1361. return $this->getWebsiteConfig($path, $website);
  1362. }
  1363. /**
  1364. * @param int $website
  1365. *
  1366. * @return string
  1367. */
  1368. public function getOrderStatus($website)
  1369. {
  1370. return $this->getReviewWebsiteSettings(\Dotdigitalgroup\Email\Helper\Config::XML_PATH_REVIEW_STATUS, $website);
  1371. }
  1372. /**
  1373. * Get review setting delay time.
  1374. *
  1375. * @param int $website
  1376. *
  1377. * @return int
  1378. */
  1379. public function getDelay($website)
  1380. {
  1381. return $this->getReviewWebsiteSettings(\Dotdigitalgroup\Email\Helper\Config::XML_PATH_REVIEW_DELAY, $website);
  1382. }
  1383. /**
  1384. * Is the review new product enabled.
  1385. *
  1386. * @param int $website
  1387. *
  1388. * @return bool
  1389. */
  1390. public function isNewProductOnly($website)
  1391. {
  1392. return $this->getReviewWebsiteSettings(
  1393. \Dotdigitalgroup\Email\Helper\Config::XML_PATH_REVIEW_NEW_PRODUCT,
  1394. $website
  1395. );
  1396. }
  1397. /**
  1398. * Get review campaign for automation review.
  1399. *
  1400. * @param int $website
  1401. *
  1402. * @return int
  1403. */
  1404. public function getCampaign($website)
  1405. {
  1406. return $this->getReviewWebsiteSettings(
  1407. \Dotdigitalgroup\Email\Helper\Config::XML_PATH_REVIEW_CAMPAIGN,
  1408. $website
  1409. );
  1410. }
  1411. /**
  1412. * Get review anchor value.
  1413. *
  1414. * @param int $website
  1415. *
  1416. * @return string
  1417. */
  1418. public function getAnchor($website)
  1419. {
  1420. return $this->getReviewWebsiteSettings(\Dotdigitalgroup\Email\Helper\Config::XML_PATH_REVIEW_ANCHOR, $website);
  1421. }
  1422. /**
  1423. * Get review display type.
  1424. *
  1425. * @param int $website
  1426. *
  1427. * @return string
  1428. */
  1429. public function getDisplayType($website)
  1430. {
  1431. return $this->getReviewWebsiteSettings(
  1432. \Dotdigitalgroup\Email\Helper\Config::XML_PATH_REVIEW_DISPLAY_TYPE,
  1433. $website
  1434. );
  1435. }
  1436. /**
  1437. * check if both frotnend and backend secure(HTTPS).
  1438. *
  1439. * @return bool
  1440. */
  1441. public function isFrontendAdminSecure()
  1442. {
  1443. $frontend = $this->store->isFrontUrlSecure();
  1444. $admin = $this->getWebsiteConfig(\Magento\Store\Model\Store::XML_PATH_SECURE_IN_ADMINHTML);
  1445. $current = $this->store->isCurrentlySecure();
  1446. if ($frontend && $admin && $current) {
  1447. return true;
  1448. }
  1449. return false;
  1450. }
  1451. /**
  1452. * Get current connector version.
  1453. *
  1454. * @return string
  1455. */
  1456. public function getConnectorVersion()
  1457. {
  1458. return $this->fullModuleList->getOne(self::MODULE_NAME)['setup_version'];
  1459. }
  1460. /**
  1461. * Get the abandoned cart limit.
  1462. *
  1463. * @return boolean|string
  1464. */
  1465. public function getAbandonedCartLimit()
  1466. {
  1467. $cartLimit = $this->scopeConfig->getValue(
  1468. \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_ABANDONED_CART_LIMIT
  1469. );
  1470. return $cartLimit;
  1471. }
  1472. /**
  1473. * @param string $cronJob
  1474. * @return boolean|string
  1475. */
  1476. public function getDateLastCronRun($cronJob)
  1477. {
  1478. return $this->contactResource->getDateLastCronRun($cronJob);
  1479. }
  1480. /**
  1481. * Get website datafields for subscriber
  1482. *
  1483. * @param \Magento\Store\Model\Website $website
  1484. * @return array
  1485. */
  1486. public function getWebsiteSalesDataFields($website)
  1487. {
  1488. $subscriberDataFileds = [
  1489. 'website_name' => '',
  1490. 'store_name' => '',
  1491. 'number_of_orders' => '',
  1492. 'average_order_value' => '',
  1493. 'total_spend' => '',
  1494. 'last_order_date' => '',
  1495. 'last_increment_id' => '',
  1496. 'most_pur_category' => '',
  1497. 'most_pur_brand' => '',
  1498. 'most_freq_pur_day' => '',
  1499. 'most_freq_pur_mon' => '',
  1500. 'first_category_pur' => '',
  1501. 'last_category_pur' => '',
  1502. 'first_brand_pur' => '',
  1503. 'last_brand_pur' => ''
  1504. ];
  1505. $store = $website->getDefaultStore();
  1506. $mappedData = $this->scopeConfig->getValue(
  1507. 'connector_data_mapping/customer_data',
  1508. \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
  1509. $store->getId()
  1510. );
  1511. $mappedData = array_intersect_key($mappedData, $subscriberDataFileds);
  1512. foreach ($mappedData as $key => $value) {
  1513. if (!$value) {
  1514. unset($mappedData[$key]);
  1515. }
  1516. }
  1517. return $mappedData;
  1518. }
  1519. /**
  1520. * Validate date range
  1521. *
  1522. * @param string $dateFrom
  1523. * @param string $dateTo
  1524. * @return bool|string
  1525. */
  1526. public function validateDateRange($dateFrom, $dateTo)
  1527. {
  1528. if (!$this->validateDate($dateFrom) || !$this->validateDate($dateTo)) {
  1529. return 'From or To date is not a valid date.';
  1530. }
  1531. if (strtotime($dateFrom) > strtotime($dateTo)) {
  1532. return 'To Date cannot be earlier then From Date.';
  1533. }
  1534. return false;
  1535. }
  1536. /**
  1537. * @param string $date
  1538. * @return bool|\DateTime|false
  1539. */
  1540. public function validateDate($date)
  1541. {
  1542. try {
  1543. return date_create($date);
  1544. } catch (\Exception $e) {
  1545. return false;
  1546. }
  1547. }
  1548. /**
  1549. * Get difference between dates
  1550. *
  1551. * @param string $created
  1552. * @return false|int
  1553. */
  1554. public function getDateDifference($created)
  1555. {
  1556. $now = $this->datetime->gmtDate();
  1557. return strtotime($now) - strtotime($created);
  1558. }
  1559. /**
  1560. * Validate code
  1561. *
  1562. * @param string $code
  1563. * @return bool
  1564. */
  1565. public function isCodeValid($code)
  1566. {
  1567. $codeFromConfig = $this->getWebsiteConfig(
  1568. \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_DYNAMIC_CONTENT_PASSCODE,
  1569. $this->getWebsite()
  1570. );
  1571. return $codeFromConfig === $code;
  1572. }
  1573. /**
  1574. * @param \Magento\User\Model\User $adminUser
  1575. * @param string $token
  1576. *
  1577. * @return null
  1578. */
  1579. public function setRefreshTokenForUser($adminUser, $token)
  1580. {
  1581. $adminUser = $adminUser->setRefreshToken($token);
  1582. $this->userResource->save($adminUser);
  1583. }
  1584. /**
  1585. * @param int $quoteId
  1586. * @return array
  1587. */
  1588. public function getQuoteAllItemsFor($quoteId)
  1589. {
  1590. $quoteModel = $this->quoteFactory->create();
  1591. $this->quoteResource->load($quoteModel, $quoteId);
  1592. $quoteItems = $quoteModel->getAllItems();
  1593. return $quoteItems;
  1594. }
  1595. /** Get brand attribute selected from config by website id
  1596. *
  1597. * @param int $websiteId
  1598. * @return string|boolean
  1599. */
  1600. public function getBrandAttributeByWebsiteId($websiteId)
  1601. {
  1602. return $this->getWebsiteConfig(
  1603. \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_SYNC_DATA_FIELDS_BRAND_ATTRIBUTE,
  1604. $websiteId
  1605. );
  1606. }
  1607. /**
  1608. * Create data fields in account by type.
  1609. *
  1610. * @param int $website
  1611. * @param string $datafield
  1612. * @param string $type
  1613. * @param string $visibility
  1614. * @param int|boolean|string $default
  1615. * @return object
  1616. */
  1617. public function createDatafield($website, $datafield, $type, $visibility = 'Private', $default = 'String')
  1618. {
  1619. $client = $this->getWebsiteApiClient($website);
  1620. switch ($type) {
  1621. case 'Numeric':
  1622. $default = (int)$default;
  1623. break;
  1624. case 'Date':
  1625. $default = $this->datetime->date(\Zend_Date::ISO_8601, $default);
  1626. break;
  1627. case 'Boolean':
  1628. $default = (bool)$default;
  1629. break;
  1630. default:
  1631. $default = (string)$default;
  1632. }
  1633. $response = $client->postDataFields($datafield, $type, $visibility, $default);
  1634. return $response;
  1635. }
  1636. /**
  1637. * Can show additional books?
  1638. *
  1639. * @param \Magento\Store\Model\Website|int $website
  1640. * @return string|boolean
  1641. */
  1642. public function getCanShowAdditionalSubscriptions($website)
  1643. {
  1644. return $this->getWebsiteConfig(
  1645. \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_ADDRESSBOOK_PREF_CAN_CHANGE_BOOKS,
  1646. $website
  1647. );
  1648. }
  1649. /**
  1650. * Can show data fields?
  1651. *
  1652. * @param \Magento\Store\Model\Website|int $website
  1653. * @return boolean|string
  1654. */
  1655. public function getCanShowDataFields($website)
  1656. {
  1657. return $this->getWebsiteConfig(
  1658. \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_ADDRESSBOOK_PREF_CAN_SHOW_FIELDS,
  1659. $website
  1660. );
  1661. }
  1662. /**
  1663. * Address book ids to display
  1664. *
  1665. * @param \Magento\Store\Model\Website $website
  1666. * @return array
  1667. */
  1668. public function getAddressBookIdsToShow($website)
  1669. {
  1670. $bookIds = $this->getWebsiteConfig(
  1671. \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_ADDRESSBOOK_PREF_SHOW_BOOKS,
  1672. $website
  1673. );
  1674. if (empty($bookIds)) {
  1675. return [];
  1676. }
  1677. $additionalFromConfig = explode(',', $bookIds);
  1678. //unset the default option - for multi select
  1679. if ($additionalFromConfig[0] == '0') {
  1680. unset($additionalFromConfig[0]);
  1681. }
  1682. return $additionalFromConfig;
  1683. }
  1684. /**
  1685. * @return string
  1686. */
  1687. public function getRegionPrefix()
  1688. {
  1689. $websiteId = $this->getWebsite()->getId();
  1690. $apiEndpoint = $this->getApiEndPointFromConfig($websiteId);
  1691. if (empty($apiEndpoint)) {
  1692. return '';
  1693. }
  1694. preg_match("/https:\/\/(.*)api.dotmailer.com/", $apiEndpoint, $matches);
  1695. return isset($matches[1]) ? $matches[1] : '';
  1696. }
  1697. /**
  1698. * @return string
  1699. */
  1700. public function getPageTrackingUrl()
  1701. {
  1702. return '//' . $this->getRegionPrefix() . 't.trackedlink.net/_dmpt';
  1703. }
  1704. /**
  1705. * @return string
  1706. */
  1707. public function getPageTrackingUrlForSuccessPage()
  1708. {
  1709. return '//' . $this->getRegionPrefix() . 't.trackedlink.net/_dmmpt';
  1710. }
  1711. /**
  1712. * @param int $storeId
  1713. *
  1714. * @return bool
  1715. */
  1716. public function isOnlySubscribersForAC($storeId)
  1717. {
  1718. $value = $this->scopeConfig->isSetFlag(
  1719. \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_CONTENT_ALLOW_NON_SUBSCRIBERS,
  1720. \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
  1721. $storeId
  1722. );
  1723. return ($value) ? false : true;
  1724. }
  1725. /**
  1726. * @param int $websiteId
  1727. *
  1728. * @return bool
  1729. */
  1730. public function isOnlySubscribersForReview($websiteId)
  1731. {
  1732. $value = $this->scopeConfig->isSetFlag(
  1733. \Dotdigitalgroup\Email\Helper\Config::XML_PATH_REVIEW_ALLOW_NON_SUBSCRIBERS,
  1734. \Magento\Store\Model\ScopeInterface::SCOPE_WEBSITE,
  1735. $websiteId
  1736. );
  1737. return ($value) ? false : true;
  1738. }
  1739. /**
  1740. * @param int $websiteId
  1741. *
  1742. * @return bool
  1743. */
  1744. public function isOnlySubscribersForContactSync($websiteId)
  1745. {
  1746. $value = $this->scopeConfig->isSetFlag(
  1747. \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_SYNC_ALLOW_NON_SUBSCRIBERS,
  1748. \Magento\Store\Model\ScopeInterface::SCOPE_WEBSITE,
  1749. $websiteId
  1750. );
  1751. return ($value) ? false : true;
  1752. }
  1753. /**
  1754. * @return ScopeConfigInterface
  1755. */
  1756. public function getScopeConfig()
  1757. {
  1758. return $this->scopeConfig;
  1759. }
  1760. /**
  1761. * @param $email
  1762. * @param $websiteId
  1763. *
  1764. * @return \Dotdigitalgroup\Email\Model\Contact
  1765. */
  1766. public function getContactByEmail($email, $websiteId)
  1767. {
  1768. $contact = $this->contactFactory->create()
  1769. ->loadByCustomerEmail($email, $websiteId);
  1770. return $contact;
  1771. }
  1772. /**
  1773. * @param \Dotdigitalgroup\Email\Model\Contact $contact
  1774. */
  1775. public function saveContact($contact)
  1776. {
  1777. $this->contactResource->save($contact);
  1778. }
  1779. }