2-send_log_repository.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. namespace cx_newsletter;
  3. class send_log_repository
  4. extends repository {
  5. public function load_all(
  6. campaign $target,
  7. ?int $limit = null,
  8. ?bool $sended = null
  9. ) : array {
  10. if (!$target->has_id()) {
  11. throw $this->get_blank_id_exception();
  12. }
  13. $campaigns = $this->get_tables()->campaigns();
  14. $customers = $this->get_tables()->customers();
  15. $send_log = $this->get_tables()->send_log();
  16. $query = new \cx_appengine\string_builder();
  17. $query->push('select '.$customers.'.id as customer ');
  18. $query->push('from '.$campaigns.' ');
  19. $query->push('inner join '.$customers.' ');
  20. $query->push('left outer join '.$send_log.' ');
  21. $query->push('on '.$customers.'.id='.$send_log.'.customer ');
  22. $query->push('and '.$campaigns.'.id='.$send_log.'.campaign ');
  23. $query->push('where '.$campaigns.'.id='.$target->get_id().' ');
  24. $query->push('and '.$campaigns.'.test='.$customers.'.tester ');
  25. if ($target->group !== null and $target->group->has_id()) {
  26. $query->push('and '.$customers.'.grouped=');
  27. $query->push(strval($target->group->get_id().' '));
  28. }
  29. if ($target->type === campaign_type::email) {
  30. $query->push('and '.$customers.'.email != "" ');
  31. } elseif ($target->type === campaign_type::sms) {
  32. $query->push('and '.$customers.'.phone_number != "" ');
  33. }
  34. if ($sended === false) {
  35. $query->push('and '.$send_log.'.id is NULL ');
  36. } elseif ($sended === true) {
  37. $query->push('and '.$send_log.'.id is not NULL ');
  38. }
  39. $query->push($this->parse_limit($limit));
  40. $result = $this->get_database()->get_results($query->get(), ARRAY_A);
  41. if ($result === null) {
  42. throw $this->get_exception('load_all');
  43. }
  44. $next = [];
  45. $mapper = new customers_mapper(
  46. $this->get_database(),
  47. $this->get_tables()
  48. );
  49. foreach ($result as $row) {
  50. $customer = new customer($row['customer']);
  51. $customer = $mapper->complete($customer);
  52. array_push($next, $customer);
  53. }
  54. return $next;
  55. }
  56. public function mark_send(campaign $from, customer $who) : self {
  57. if (!$from->has_id() or !$who->has_id()) {
  58. throw $this->get_blank_id_exception();
  59. }
  60. $log = [];
  61. $log['campaign'] = $from->get_id();
  62. $log['customer'] = $who->get_id();
  63. $log['sended'] = \current_time('mysql', 1);
  64. $this->get_database()->insert($this->get_tables()->send_log(), $log);
  65. return $this;
  66. }
  67. public function count(campaign $target, ?bool $sended = null) : int {
  68. if (!$target->has_id()) {
  69. throw $this->get_blank_id_exception();
  70. }
  71. $campaigns = $this->get_tables()->campaigns();
  72. $customers = $this->get_tables()->customers();
  73. $send_log = $this->get_tables()->send_log();
  74. $query = new \cx_appengine\string_builder();
  75. $query->push('select count('.$customers.'.id) ');
  76. $query->push('from '.$campaigns.' ');
  77. $query->push('inner join '.$customers.' ');
  78. $query->push('left outer join '.$send_log.' ');
  79. $query->push('on '.$customers.'.id='.$send_log.'.customer ');
  80. $query->push('and '.$campaigns.'.id='.$send_log.'.campaign ');
  81. $query->push('where '.$campaigns.'.id='.$target->get_id().' ');
  82. $query->push('and '.$campaigns.'.test='.$customers.'.tester ');
  83. if ($target->type === campaign_type::email) {
  84. $query->push('and '.$customers.'.email != "" ');
  85. } elseif ($target->type === campaign_type::sms) {
  86. $query->push('and '.$customers.'.phone_number != "" ');
  87. }
  88. if ($sended === false) {
  89. $query->push('and '.$send_log.'.id is NULL ');
  90. } elseif ($sended === true) {
  91. $query->push('and '.$send_log.'.id is not NULL ');
  92. }
  93. if ($target->group !== null and $target->group->has_id()) {
  94. $query->push('and '.$customers.'.grouped=');
  95. $query->push(strval($target->group->get_id().' '));
  96. }
  97. $result = $this->get_database()->get_var($query->get());
  98. if ($result === null) {
  99. throw $this->get_exception('count');
  100. }
  101. return intval($result);
  102. }
  103. }