/app/code/core/Mage/Core/Model/Resource/Email/Queue.php

https://github.com/speedupmate/Magento-CE-Mirror · PHP · 195 lines · 113 code · 12 blank · 70 comment · 4 complexity · 44746b06b986253126f6f5d26ca1afc3 MD5 · raw file

  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magento.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magento.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Core
  23. * @copyright Copyright (c) 2006-2020 Magento, Inc. (http://www.magento.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. class Mage_Core_Model_Resource_Email_Queue extends Mage_Core_Model_Resource_Db_Abstract
  27. {
  28. /**
  29. * Initialize email queue resource model
  30. *
  31. */
  32. protected function _construct()
  33. {
  34. $this->_init('core/email_queue', 'message_id');
  35. }
  36. /**
  37. * Load recipients, unserialize message parameters
  38. *
  39. * @param Mage_Core_Model_Abstract $object
  40. *
  41. * @return Mage_Core_Model_Resource_Email_Queue
  42. */
  43. protected function _afterLoad(Mage_Core_Model_Abstract $object)
  44. {
  45. $object->setRecipients($this->getRecipients($object->getId()));
  46. $object->setMessageParameters(unserialize($object->getMessageParameters()));
  47. return $this;
  48. }
  49. /**
  50. * Prepare object data for saving
  51. *
  52. * @param Mage_Core_Model_Email_Queue|Mage_Core_Model_Abstract $object
  53. * @return Mage_Core_Model_Resource_Email_Queue
  54. */
  55. protected function _beforeSave(Mage_Core_Model_Abstract $object)
  56. {
  57. if ($object->isObjectNew()) {
  58. $object->setCreatedAt($this->formatDate(true));
  59. }
  60. $object->setMessageBodyHash(md5($object->getMessageBody()));
  61. $object->setMessageParameters(serialize($object->getMessageParameters()));
  62. return parent::_beforeSave($object);
  63. }
  64. /**
  65. * Check if email was added to queue for requested recipients
  66. *
  67. * @param Mage_Core_Model_Email_Queue $queue
  68. *
  69. * @return bool
  70. */
  71. public function wasEmailQueued(Mage_Core_Model_Email_Queue $queue)
  72. {
  73. $readAdapter = $this->_getReadAdapter();
  74. $select = $readAdapter->select()
  75. ->from(
  76. array('recips' => $this->getTable('core/email_recipients')),
  77. array('recipient_email', 'recipient_name', 'email_type')
  78. )
  79. ->join(array('queue' => $this->getMainTable()), 'queue.message_id = recips.message_id', array())
  80. ->where('queue.entity_id =? ', $queue->getEntityId())
  81. ->where('queue.entity_type =? ', $queue->getEntityType())
  82. ->where('queue.event_type =? ', $queue->getEventType())
  83. ->where('queue.message_body_hash =? ', md5($queue->getMessageBody()));
  84. $existingRecipients = $readAdapter->fetchAll($select);
  85. if ($existingRecipients) {
  86. $newRecipients = $queue->getRecipients();
  87. $oldEmails = $newEmails = array();
  88. foreach ($existingRecipients as $recipient) {
  89. $oldEmails[$recipient['recipient_email']] = array(
  90. $recipient['recipient_email'], $recipient['recipient_name'], $recipient['email_type']
  91. );
  92. }
  93. unset($recipient);
  94. foreach ($newRecipients as $recipient) {
  95. list($email, $name, $type) = $recipient;
  96. $newEmails[$email] = array($email, $name, $type);
  97. }
  98. $diff = array_diff_key($newEmails, $oldEmails);
  99. if (sizeof($diff) > 0) {
  100. $queue->clearRecipients();
  101. foreach ($diff as $recipient) {
  102. list($email, $name, $type) = $recipient;
  103. $queue->addRecipients($email, $name, $type);
  104. }
  105. return false;
  106. }
  107. return true;
  108. }
  109. return false;
  110. }
  111. /**
  112. * Retrieve recipients data for specified message
  113. *
  114. * @param int $messageId
  115. *
  116. * @return array
  117. */
  118. public function getRecipients($messageId)
  119. {
  120. $readAdapter = $this->_getReadAdapter();
  121. $select = $readAdapter->select()
  122. ->from($this->getTable('core/email_recipients'), array('recipient_email', 'recipient_name', 'email_type'))
  123. ->where('message_id =? ', $messageId);
  124. $recipients = $readAdapter->fetchAll($select);
  125. $existingRecipients = array();
  126. if ($recipients) {
  127. foreach ($recipients as $recipient) {
  128. $existingRecipients[] = array(
  129. $recipient['recipient_email'],
  130. $recipient['recipient_name'],
  131. $recipient['email_type']
  132. );
  133. }
  134. }
  135. return $existingRecipients;
  136. }
  137. /**
  138. * Save message recipients
  139. *
  140. * @param int $messageId
  141. * @param array $recipients
  142. *
  143. * @throws Exception
  144. *
  145. * @return Mage_Core_Model_Resource_Email_Queue
  146. */
  147. public function saveRecipients($messageId, array $recipients)
  148. {
  149. $writeAdapter = $this->_getWriteAdapter();
  150. $recipientsTable = $this->getTable('core/email_recipients');
  151. $writeAdapter->beginTransaction();
  152. try {
  153. foreach ($recipients as $recipient) {
  154. list($email, $name, $type) = $recipient;
  155. $writeAdapter->insertOnDuplicate(
  156. $recipientsTable,
  157. array(
  158. 'message_id' => $messageId,
  159. 'recipient_email' => $email,
  160. 'recipient_name' => $name,
  161. 'email_type' => $type
  162. ),
  163. array('recipient_name')
  164. );
  165. }
  166. $writeAdapter->commit();
  167. } catch (Exception $e) {
  168. $writeAdapter->rollback();
  169. throw $e;
  170. }
  171. return $this;
  172. }
  173. /**
  174. * Remove already sent messages
  175. *
  176. * @return Mage_Core_Model_Resource_Email_Queue
  177. */
  178. public function removeSentMessages()
  179. {
  180. $this->_getWriteAdapter()->delete($this->getMainTable(), 'processed_at IS NOT NULL');
  181. return $this;
  182. }
  183. }