Template.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Email\Model\ResourceModel;
  7. use Magento\Framework\Model\AbstractModel;
  8. /**
  9. * Template db resource
  10. *
  11. * @api
  12. * @since 100.0.2
  13. */
  14. class Template extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
  15. {
  16. /**
  17. * @param \Magento\Framework\Model\ResourceModel\Db\Context $context
  18. * @param string $connectionName
  19. */
  20. public function __construct(
  21. \Magento\Framework\Model\ResourceModel\Db\Context $context,
  22. $connectionName = null
  23. ) {
  24. parent::__construct($context, $connectionName);
  25. }
  26. /**
  27. * Initialize email template resource model
  28. *
  29. * @return void
  30. */
  31. protected function _construct()
  32. {
  33. $this->_init('email_template', 'template_id');
  34. }
  35. /**
  36. * Check usage of template code in other templates
  37. *
  38. * @param \Magento\Email\Model\Template $template
  39. * @return bool
  40. */
  41. public function checkCodeUsage(\Magento\Email\Model\Template $template)
  42. {
  43. if ($template->getTemplateActual() != 0 || $template->getTemplateActual() === null) {
  44. $select = $this->getConnection()->select()->from(
  45. $this->getMainTable(),
  46. 'COUNT(*)'
  47. )->where(
  48. 'template_code = :template_code'
  49. );
  50. $bind = ['template_code' => $template->getTemplateCode()];
  51. $templateId = $template->getId();
  52. if ($templateId) {
  53. $select->where('template_id != :template_id');
  54. $bind['template_id'] = $templateId;
  55. }
  56. $result = $this->getConnection()->fetchOne($select, $bind);
  57. if ($result) {
  58. return true;
  59. }
  60. }
  61. return false;
  62. }
  63. /**
  64. * Set template type, added at and modified at time
  65. *
  66. * @param \Magento\Framework\Model\AbstractModel $object
  67. * @return $this
  68. */
  69. protected function _beforeSave(AbstractModel $object)
  70. {
  71. $object->setTemplateType((int)$object->getTemplateType());
  72. return parent::_beforeSave($object);
  73. }
  74. /**
  75. * Retrieve config scope and scope id of specified email template by email paths
  76. *
  77. * @param array $paths
  78. * @param int|string $templateId
  79. * @return array
  80. */
  81. public function getSystemConfigByPathsAndTemplateId($paths, $templateId)
  82. {
  83. $orWhere = [];
  84. $pathsCounter = 1;
  85. $bind = [];
  86. foreach ($paths as $path) {
  87. $pathAlias = 'path_' . $pathsCounter;
  88. $orWhere[] = 'path = :' . $pathAlias;
  89. $bind[$pathAlias] = $path;
  90. $pathsCounter++;
  91. }
  92. $bind['template_id'] = $templateId;
  93. $select = $this->getConnection()->select()->from(
  94. $this->getTable('core_config_data'),
  95. ['scope', 'scope_id', 'path']
  96. )->where(
  97. 'value LIKE :template_id'
  98. )->where(
  99. join(' OR ', $orWhere)
  100. );
  101. return $this->getConnection()->fetchAll($select, $bind);
  102. }
  103. }