Template.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Newsletter\Model\ResourceModel;
  7. /**
  8. * Newsletter template resource model
  9. *
  10. * @author Magento Core Team <core@magentocommerce.com>
  11. *
  12. * @api
  13. * @since 100.0.2
  14. */
  15. class Template extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
  16. {
  17. /**
  18. * Date
  19. *
  20. * @var \Magento\Framework\Stdlib\DateTime\DateTime
  21. */
  22. protected $_date;
  23. /**
  24. * @param \Magento\Framework\Model\ResourceModel\Db\Context $context
  25. * @param \Magento\Framework\Stdlib\DateTime\DateTime $date
  26. * @param string $connectionName
  27. */
  28. public function __construct(
  29. \Magento\Framework\Model\ResourceModel\Db\Context $context,
  30. \Magento\Framework\Stdlib\DateTime\DateTime $date,
  31. $connectionName = null
  32. ) {
  33. parent::__construct($context, $connectionName);
  34. $this->_date = $date;
  35. }
  36. /**
  37. * Initialize connection
  38. *
  39. * @return void
  40. */
  41. protected function _construct()
  42. {
  43. $this->_init('newsletter_template', 'template_id');
  44. }
  45. /**
  46. * Check usage of template in queue
  47. *
  48. * @param \Magento\Newsletter\Model\Template $template
  49. * @return boolean
  50. */
  51. public function checkUsageInQueue(\Magento\Newsletter\Model\Template $template)
  52. {
  53. if ($template->getTemplateActual() !== 0 && !$template->getIsSystem()) {
  54. $select = $this->getConnection()->select()->from(
  55. $this->getTable('newsletter_queue'),
  56. new \Zend_Db_Expr('COUNT(queue_id)')
  57. )->where(
  58. 'template_id = :template_id'
  59. );
  60. $countOfQueue = $this->getConnection()->fetchOne($select, ['template_id' => $template->getId()]);
  61. return $countOfQueue > 0;
  62. } elseif ($template->getIsSystem()) {
  63. return false;
  64. } else {
  65. return true;
  66. }
  67. }
  68. /**
  69. * Check usage of template code in other templates
  70. *
  71. * @param \Magento\Newsletter\Model\Template $template
  72. * @return boolean
  73. */
  74. public function checkCodeUsage(\Magento\Newsletter\Model\Template $template)
  75. {
  76. if ($template->getTemplateActual() != 0 || $template->getTemplateActual() === null) {
  77. $bind = [
  78. 'template_id' => $template->getId(),
  79. 'template_code' => $template->getTemplateCode(),
  80. 'template_actual' => 1,
  81. ];
  82. $select = $this->getConnection()->select()->from(
  83. $this->getMainTable(),
  84. new \Zend_Db_Expr('COUNT(template_id)')
  85. )->where(
  86. 'template_id != :template_id'
  87. )->where(
  88. 'template_code = :template_code'
  89. )->where(
  90. 'template_actual = :template_actual'
  91. );
  92. $countOfCodes = $this->getConnection()->fetchOne($select, $bind);
  93. return $countOfCodes > 0;
  94. } else {
  95. return false;
  96. }
  97. }
  98. /**
  99. * Perform actions before object save
  100. *
  101. * @param \Magento\Framework\Model\AbstractModel $object
  102. * @return $this
  103. * @throws \Magento\Framework\Exception\LocalizedException
  104. */
  105. protected function _beforeSave(\Magento\Framework\Model\AbstractModel $object)
  106. {
  107. if ($this->checkCodeUsage($object)) {
  108. throw new \Magento\Framework\Exception\LocalizedException(__('Duplicate template code'));
  109. }
  110. if (!$object->hasTemplateActual()) {
  111. $object->setTemplateActual(1);
  112. }
  113. if (!$object->hasAddedAt()) {
  114. $object->setAddedAt($this->_date->gmtDate());
  115. }
  116. $object->setModifiedAt($this->_date->gmtDate());
  117. return parent::_beforeSave($object);
  118. }
  119. }