protected function doJob($arg) { $models = Newsletter::model()->findAll(array('condition' => 't.remain_subscribers IS NOT NULL AND length(t.remain_subscribers) > 0 AND t.send_time <= NOW()', 'order' => 't.id ASC')); foreach ($models as $model) { $mail_models = ProNewsletterMail::model()->findAll(array('condition' => 't.newsletter_id = ' . $model->id, 'order' => 't.id ASC')); if (count($mail_models)) { $receivers = explode(',', $model->remain_subscribers); $subscriber_count = 0; foreach ($mail_models as $key => $k) { $revei = array_shift($receivers); // need update this field $s = Subscriber::model()->getSubscriberByEmail($k->email); if (empty($s)) { continue; } if ($s) { if ($s->status == 0) { continue; } } // add by Nguyen Dung $url = Yii::app()->setting->getItem('server_name') . '/site/track_newsletter?newsletter_id=' . $model->id . '&subscriber_email=' . $s->email; $img_track_read_email = '<img src="' . $url . '" alt="" height="1" width="1"/>'; $r = array('subject' => $model->subject, 'params' => array('content' => $model->content . $img_track_read_email, 'newsletterName' => Yii::app()->params['title'], 'unsubscribe' => Yii::app()->setting->getItem('server_name') . '/site/unsubscribe?id=' . $s->id . '&code=' . md5($s->id . $s->email)), 'view' => 'newsletter', 'to' => $s->email, 'from' => Yii::app()->params['autoEmail']); $this->data[] = $r; //Delete record in newsletter mail ProNewsletterMail::model()->deleteByPk($k->id); $subscriber_count++; //count subscriber is served for current newsletter job $this->index++; //count email is sent for current cron job if ($this->index >= $this->max) { break; } } $model->total_sent = $model->total_sent + $subscriber_count; // track amount mail sent $model->remain_subscribers = implode(',', $receivers); $model->update(array('remain_subscribers', 'total_sent')); } } //when sent all subscriber of a newsletter job but the // if($this->index < $this->max) // $this->doJob($arg); }
public function buildListSubscriberAdd($mNewsletter) { $newsletter_id = $mNewsletter->id; $send_time = $mNewsletter->send_time; $criteria = new CDbCriteria(); $criteria->compare('t.status', 1); $listIdSubsciber = ''; $totalSubscriber = 0; // if not select group => we get all subscriber => don't exist if (!isset($_POST['Newsletter']['newsletter_group_subscriber']) || count($_POST['Newsletter']['newsletter_group_subscriber']) < 1) { $subscribers = Subscriber::model()->findAll($criteria); foreach ($subscribers as $s) { $listIdSubsciber .= $s->id . ','; } $totalSubscriber = count($subscribers); } else { // if select group => we get all subscriber of this group $criteria->addInCondition('t.subscriber_group_id', $_POST['Newsletter']['newsletter_group_subscriber']); $subscribers = Subscriber::model()->findAll($criteria); $aRowInsert = array(); $tableName = ProNewsletterMail::model()->tableName(); foreach ($subscribers as $s) { $listIdSubsciber .= $s->id . ','; $new_model = new ProNewsletterMail(); $new_model->name = $s->name; $new_model->email = $s->email; $new_model->subscriber_group_id = $s->subscriber_group_id; $new_model->newsletter_id = $newsletter_id; $new_model->send_time = $send_time; $aRowInsert[] = "('{$new_model->name}',\n '{$new_model->email}',\n '{$new_model->subscriber_group_id}',\n '{$new_model->newsletter_id}',\n '{$new_model->send_time}'\n )"; } $totalSubscriber = count($subscribers); if (count($aRowInsert)) { $sql = "insert into {$tableName} (name,\n email,\n subscriber_group_id,\n newsletter_id,\n send_time\n ) values " . implode(',', $aRowInsert); ProNewsletterMail::model()->deleteAll("newsletter_id={$mNewsletter->id}"); Yii::app()->db->createCommand($sql)->execute(); } } return array('listIdSubsciber' => $listIdSubsciber, 'totalSubscriber' => $totalSubscriber); }