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); } }