wp = WPFunctions::get(); } public function subscribers() { return $this->hasManyThrough( __NAMESPACE__ . '\Subscriber', __NAMESPACE__ . '\ScheduledTaskSubscriber', 'task_id', 'subscriber_id' ); } public function pause() { $this->set('status', self::STATUS_PAUSED); $this->save(); return ($this->getErrors() === false && $this->id() > 0); } public static function pauseAllByNewsletter(Newsletter $newsletter) { ScheduledTask::rawExecute( 'UPDATE `' . ScheduledTask::$_table . '` t ' . 'JOIN `' . SendingQueue::$_table . '` q ON t.`id` = q.`task_id` ' . 'SET t.`status` = "' . self::STATUS_PAUSED . '" ' . 'WHERE ' . 'q.`newsletter_id` = ' . $newsletter->id() . ' AND t.`status` = "' . self::STATUS_SCHEDULED . '" ' ); } public function resume() { $this->setExpr('status', 'NULL'); $this->save(); return ($this->getErrors() === false && $this->id() > 0); } public static function setScheduledAllByNewsletter(Newsletter $newsletter) { ScheduledTask::rawExecute( 'UPDATE `' . ScheduledTask::$_table . '` t ' . 'JOIN `' . SendingQueue::$_table . '` q ON t.`id` = q.`task_id` ' . 'SET t.`status` = "' . self::STATUS_SCHEDULED . '" ' . 'WHERE ' . 'q.`newsletter_id` = ' . $newsletter->id() . ' AND t.`status` = "' . self::STATUS_PAUSED . '" ' . ' AND t.`scheduled_at` > CURDATE() - INTERVAL 30 DAY' ); } public function complete() { $this->processedAt = $this->wp->currentTime('mysql'); $this->set('status', self::STATUS_COMPLETED); $this->save(); return ($this->getErrors() === false && $this->id() > 0); } public function save() { // set the default priority to medium if (!$this->priority) { $this->priority = self::PRIORITY_MEDIUM; } if (!is_null($this->meta) && !Helpers::isJson($this->meta)) { $this->set( 'meta', (string)json_encode($this->meta) ); } parent::save(); return $this; } public function asArray() { $model = parent::asArray(); $model['meta'] = $this->getMeta(); return $model; } public function getMeta() { $meta = (Helpers::isJson($this->meta) && is_string($this->meta)) ? json_decode($this->meta, true) : $this->meta; return !empty($meta) ? (array)$meta : []; } public function delete() { try { ORM::get_db()->beginTransaction(); ScheduledTaskSubscriber::where('task_id', $this->id)->deleteMany(); parent::delete(); ORM::get_db()->commit(); } catch (\Exception $error) { ORM::get_db()->rollBack(); throw $error; } return null; } /** * @return ScheduledTask|null */ public static function findOneScheduledByNewsletterIdAndSubscriberId($newsletterId, $subscriberId) { return ScheduledTask::tableAlias('tasks') ->select('tasks.*') ->innerJoin(SendingQueue::$_table, 'queues.task_id = tasks.id', 'queues') ->innerJoin(ScheduledTaskSubscriber::$_table, 'task_subscribers.task_id = tasks.id', 'task_subscribers') ->where('queues.newsletter_id', $newsletterId) ->where('tasks.status', ScheduledTask::STATUS_SCHEDULED) ->where('task_subscribers.subscriber_id', $subscriberId) ->whereNull('queues.deleted_at') ->whereNull('tasks.deleted_at') ->findOne() ?: null; } }