| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- <?php
- namespace cx_newsletter;
- class send_log_repository
- extends repository {
- public function load_all(
- campaign $target,
- ?int $limit = null,
- ?bool $sended = null
- ) : array {
- if (!$target->has_id()) {
- throw $this->get_blank_id_exception();
- }
- $campaigns = $this->get_tables()->campaigns();
- $customers = $this->get_tables()->customers();
- $send_log = $this->get_tables()->send_log();
- $query = new \cx_appengine\string_builder();
- $query->push('select '.$customers.'.id as customer ');
- $query->push('from '.$campaigns.' ');
- $query->push('inner join '.$customers.' ');
- $query->push('left outer join '.$send_log.' ');
- $query->push('on '.$customers.'.id='.$send_log.'.customer ');
- $query->push('and '.$campaigns.'.id='.$send_log.'.campaign ');
- $query->push('where '.$campaigns.'.id='.$target->get_id().' ');
- $query->push('and '.$campaigns.'.test='.$customers.'.tester ');
- if ($target->group !== null and $target->group->has_id()) {
- $query->push('and '.$customers.'.grouped=');
- $query->push(strval($target->group->get_id().' '));
- }
- if ($target->type === campaign_type::email) {
- $query->push('and '.$customers.'.email != "" ');
- } elseif ($target->type === campaign_type::sms) {
- $query->push('and '.$customers.'.phone_number != "" ');
- }
-
- if ($sended === false) {
- $query->push('and '.$send_log.'.id is NULL ');
- } elseif ($sended === true) {
- $query->push('and '.$send_log.'.id is not NULL ');
- }
- $query->push($this->parse_limit($limit));
- $result = $this->get_database()->get_results($query->get(), ARRAY_A);
- if ($result === null) {
- throw $this->get_exception('load_all');
- }
- $next = [];
- $mapper = new customers_mapper(
- $this->get_database(),
- $this->get_tables()
- );
- foreach ($result as $row) {
- $customer = new customer($row['customer']);
- $customer = $mapper->complete($customer);
- array_push($next, $customer);
- }
- return $next;
- }
- public function mark_send(campaign $from, customer $who) : self {
- if (!$from->has_id() or !$who->has_id()) {
- throw $this->get_blank_id_exception();
- }
- $log = [];
- $log['campaign'] = $from->get_id();
- $log['customer'] = $who->get_id();
- $log['sended'] = \current_time('mysql', 1);
- $this->get_database()->insert($this->get_tables()->send_log(), $log);
-
- return $this;
- }
- public function count(campaign $target, ?bool $sended = null) : int {
- if (!$target->has_id()) {
- throw $this->get_blank_id_exception();
- }
- $campaigns = $this->get_tables()->campaigns();
- $customers = $this->get_tables()->customers();
- $send_log = $this->get_tables()->send_log();
- $query = new \cx_appengine\string_builder();
- $query->push('select count('.$customers.'.id) ');
- $query->push('from '.$campaigns.' ');
- $query->push('inner join '.$customers.' ');
- $query->push('left outer join '.$send_log.' ');
- $query->push('on '.$customers.'.id='.$send_log.'.customer ');
- $query->push('and '.$campaigns.'.id='.$send_log.'.campaign ');
- $query->push('where '.$campaigns.'.id='.$target->get_id().' ');
- $query->push('and '.$campaigns.'.test='.$customers.'.tester ');
-
- if ($target->type === campaign_type::email) {
- $query->push('and '.$customers.'.email != "" ');
- } elseif ($target->type === campaign_type::sms) {
- $query->push('and '.$customers.'.phone_number != "" ');
- }
-
- if ($sended === false) {
- $query->push('and '.$send_log.'.id is NULL ');
- } elseif ($sended === true) {
- $query->push('and '.$send_log.'.id is not NULL ');
- }
- if ($target->group !== null and $target->group->has_id()) {
- $query->push('and '.$customers.'.grouped=');
- $query->push(strval($target->group->get_id().' '));
- }
- $result = $this->get_database()->get_var($query->get());
-
- if ($result === null) {
- throw $this->get_exception('count');
- }
-
- return intval($result);
- }
- }
|