Information.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. namespace Dotdigitalgroup\Email\Block\Adminhtml\Dashboard;
  3. /**
  4. * Dashboard information block
  5. *
  6. * @api
  7. */
  8. class Information extends \Magento\Backend\Block\Template
  9. {
  10. /**
  11. * Helper.
  12. *
  13. * @var \Dotdigitalgroup\Email\Helper\Data
  14. */
  15. private $helper;
  16. /**
  17. * Test class.
  18. * @var \Dotdigitalgroup\Email\Model\Apiconnector\Test
  19. */
  20. private $test;
  21. /**
  22. * @var \Magento\Framework\App\ProductMetadata
  23. */
  24. private $productMetadata;
  25. /*
  26. * @var \Dotdigitalgroup\Email\Model\ResourceModel\FailedAuth\Collection
  27. */
  28. private $failedAuthCollectionFactory;
  29. /**
  30. * @var int
  31. */
  32. private $storeIdFromParam = 1;
  33. /**
  34. * Information constructor.
  35. * @param \Magento\Backend\Block\Template\Context $context
  36. * @param \Dotdigitalgroup\Email\Model\Apiconnector\Test $test
  37. * @param \Dotdigitalgroup\Email\Helper\Data $helper
  38. * @param \Magento\Framework\App\ProductMetadataFactory $productMetadata
  39. * @param \Dotdigitalgroup\Email\Model\ResourceModel\FailedAuth\CollectionFactory $failedAuthCollectionFactory
  40. * @param array $data
  41. * @throws \Magento\Framework\Exception\LocalizedException
  42. */
  43. public function __construct(
  44. \Magento\Backend\Block\Template\Context $context,
  45. \Dotdigitalgroup\Email\Model\Apiconnector\Test $test,
  46. \Dotdigitalgroup\Email\Helper\Data $helper,
  47. \Magento\Framework\App\ProductMetadataFactory $productMetadata,
  48. \Dotdigitalgroup\Email\Model\ResourceModel\FailedAuth\CollectionFactory $failedAuthCollectionFactory,
  49. array $data = []
  50. ) {
  51. $this->productMetadata = $productMetadata->create();
  52. $this->test = $test;
  53. $this->helper = $helper;
  54. $this->failedAuthCollectionFactory = $failedAuthCollectionFactory;
  55. parent::__construct($context, $data);
  56. $this->getStoreIdParam();
  57. }
  58. /**
  59. * @return string
  60. */
  61. public function getPhpVersion()
  62. {
  63. return __('v. %1', PHP_VERSION);
  64. }
  65. /**
  66. * @return string
  67. */
  68. public function getPhpMaxExecutionTime()
  69. {
  70. return $this->escapeHtml(ini_get('max_execution_time') . ' sec.');
  71. }
  72. /**
  73. * @return string
  74. */
  75. public function getDeveloperMode()
  76. {
  77. return $this->escapeHtml($this->_appState->getMode());
  78. }
  79. /**
  80. * Magento version
  81. * @return \Magento\Framework\Phrase | string
  82. */
  83. public function getMagentoVersion()
  84. {
  85. $productMetadata = $this->productMetadata;
  86. return $this->escapeHtml(__('ver. %1', $productMetadata->getVersion()));
  87. }
  88. /**
  89. * @return string
  90. */
  91. public function getConnectorVersion()
  92. {
  93. return $this->escapeHtml(__('v. %1', $this->helper->getConnectorVersion()));
  94. }
  95. /**
  96. * Get the api creds are valid.
  97. * @return string
  98. */
  99. public function getApiValid()
  100. {
  101. $apiUsername = $this->helper->getApiUsername();
  102. $apiPassword = $this->helper->getApiPassword();
  103. $result = $this->test->validate($apiUsername, $apiPassword);
  104. return ($result)? '<span class="message message-success">Valid</span>' :
  105. '<span class="message message-error">Not Valid</span>';
  106. }
  107. /**
  108. * Get the last successful execution for import.
  109. *
  110. * @return string
  111. */
  112. public function getCronLastExecution()
  113. {
  114. $date = $this->escapeHtml($this->helper->getDateLastCronRun('ddg_automation_importer'));
  115. if (! $date) {
  116. $date = '<span class="message message-error">No cron found</span>';
  117. }
  118. return $date;
  119. }
  120. /**
  121. * Get the passcode used for DC.
  122. *
  123. * @return string
  124. */
  125. public function getDynamicContentPasscode()
  126. {
  127. return $this->helper->getPasscode();
  128. }
  129. /**
  130. * Abandoned cart limit.
  131. *
  132. * @return string
  133. */
  134. public function getAbandonedCartLimit()
  135. {
  136. return ($this->helper->getAbandonedCartLimit()) ? __('%1 h', $this->helper->getAbandonedCartLimit()) :
  137. __('No limit');
  138. }
  139. /**
  140. * @return \Magento\Framework\Phrase|string]
  141. */
  142. public function getAuthStatus()
  143. {
  144. $collection = $this->failedAuthCollectionFactory->create()
  145. ->loadByStoreId($this->storeIdFromParam);
  146. $failedAuth = $collection->getFirstItem();
  147. //check if the failed auth is set for the store
  148. if ($failedAuth->getId()) {
  149. return ($failedAuth->isLocked())? __('Locked.'): __('Not Locked.');
  150. } else {
  151. return __('Not Locked.');
  152. }
  153. }
  154. /**
  155. * @return \Magento\Framework\Phrase
  156. */
  157. public function getLastFailedAuth()
  158. {
  159. $collection = $this->failedAuthCollectionFactory->create()
  160. ->loadByStoreId($this->storeIdFromParam);
  161. $failedAuth = $collection->getFirstItem();
  162. if ($failedAuth->getId()) {
  163. return $this->formatTime($failedAuth->getLastAttemptDate(), \IntlDateFormatter::LONG);
  164. }
  165. }
  166. /**
  167. * @throws \Magento\Framework\Exception\LocalizedException
  168. */
  169. private function getStoreIdParam()
  170. {
  171. $storeCode = $this->getRequest()->getParam('store');
  172. $websiteCode = $this->getRequest()->getParam('website');
  173. //store level
  174. if ($storeCode) {
  175. $this->storeIdFromParam = $this->_storeManager->getStore($storeCode)->getId();
  176. } else {
  177. //website level
  178. $this->storeIdFromParam = $this->_storeManager->getWebsite($websiteCode)->getDefaultStore()->getId();
  179. }
  180. }
  181. }