BxDolEmailQueue.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaCore UNA Core
  7. * @{
  8. */
  9. class BxDolEmailQueue extends BxDolFactory implements iBxDolSingleton
  10. {
  11. protected $_oQuery;
  12. protected function __construct()
  13. {
  14. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  15. trigger_error ('Multiple instances are not allowed for the class: ' . get_class($this), E_USER_ERROR);
  16. parent::__construct();
  17. $this->_oQuery = new BxDolEmailQueueQuery();
  18. }
  19. /**
  20. * Prevent cloning the instance
  21. */
  22. public function __clone()
  23. {
  24. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  25. trigger_error('Clone is not allowed for the class: ' . get_class($this), E_USER_ERROR);
  26. }
  27. /**
  28. * Get singleton instance of the class
  29. */
  30. static function getInstance()
  31. {
  32. if(!isset($GLOBALS['bxDolClasses'][__CLASS__]))
  33. $GLOBALS['bxDolClasses'][__CLASS__] = new BxDolEmailQueue();
  34. return $GLOBALS['bxDolClasses'][__CLASS__];
  35. }
  36. /**
  37. * Add message to mail queue
  38. *
  39. * @param string $sEmail - recipient email
  40. * @param string $sSubject - message subject
  41. * @param string $sBody - message body
  42. * @return true on success, false on error
  43. */
  44. public function add($sEmail, $sSubject, $sBody, $sHeaders = '', $sParams = '')
  45. {
  46. return (int)$this->_oQuery->insertItem(array(
  47. 'email' => $sEmail,
  48. 'subject' => $sSubject,
  49. 'body' => $sBody,
  50. 'headers' => $sHeaders,
  51. 'params' => $sParams
  52. )) > 0;
  53. }
  54. /**
  55. * Send some number of email form mail queue
  56. *
  57. * @param int $iLimit - number of emails to send
  58. * @return real number of sent emails
  59. */
  60. function send($iLimit = 1)
  61. {
  62. $aSent = array();
  63. $aMails = $this->_oQuery->getItems(array('type' => 'to_send', 'start' => 0, 'per_page' => $iLimit));
  64. foreach($aMails as $iId => $aMail)
  65. if(call_user_func_array('mail', array_slice($aMail, 1)))
  66. $aSent[] = $iId;
  67. if(!empty($aSent) && is_array($aSent))
  68. $this->_oQuery->deleteItem($aSent);
  69. return count($aSent);
  70. }
  71. }
  72. /** @} */