public function actionIndex() { echo "Sending letters to subscribers begin...\n"; $currentDayTime = time() - 60 * 60 * 24; $currentDay = date("Y-m-d H:i:s", $currentDayTime); $importantPosts = Post::find()->where(['is_public' => 1, 'is_index' => 1])->andWhere(['>', 'created_at', $currentDay])->orderBy(['created_at' => SORT_DESC])->limit(3)->all(); $ids = []; foreach ($importantPosts as $post) { $ids[] = $post->id; } $maxCommentsPosts = Post::find()->where(['is_public' => 1])->andWhere(['>', 'created_at', $currentDay])->andWhere(['not in', "id", $ids])->orderBy(['id' => SORT_DESC])->limit(3)->all(); $posts = array_merge($importantPosts, $maxCommentsPosts); // sending $subscribings = Subscribing::find()->all(); $count = 0; foreach ($subscribings as $subscribing) { if (!filter_var($subscribing->email, FILTER_VALIDATE_EMAIL)) { echo "Email is not correct: " . $subscribing->email . "\n"; $subscribing->delete(); continue; } $unsubscribeKey = md5($subscribing->id . $subscribing->email); $message = Yii::$app->mailer->compose('subscribe-view-html', compact('posts', 'unsubscribeKey'))->setTo($subscribing->email)->setSubject('Новости Динамо'); $send = $message->send(); if ($send) { $count++; } } echo "Posted {$count} letters. \n"; echo "Sending letters to subscribers end.\n"; }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Subscribing::find(); $dataProvider = new ActiveDataProvider(['query' => $query, 'sort' => ['defaultOrder' => ['created_at' => SORT_DESC]]]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to return any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'created_at' => $this->created_at, 'updated_at' => $this->updated_at]); $query->andFilterWhere(['like', 'email', $this->email]); return $dataProvider; }
/** * Finds the Subscribing model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Subscribing the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Subscribing::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
/** * Return true if user is subscribed * @return boolean */ public function getUnsubscribeKey() { $subscribing = \common\models\Subscribing::find()->where(['email' => $this->email])->one(); if (isset($subscribing->id)) { return md5($subscribing->id . $subscribing->email); } return ''; }
/** * Get a subscribing form * @return array Data */ public static function getSubscribingForm() { $model = new Subscribing(); if ($model->load(Yii::$app->request->post()) && $model->validate()) { if (!$model->save()) { $errors = $model->getErrors(); $errorMessage = array_shift($errors); Yii::$app->getSession()->setFlash('error-subscribe', 'Произошла ошибка: ' . $errorMessage); } Yii::$app->getSession()->setFlash('success-subscribe', 'Вы успешно подписались на новостную рассылку от dynamomania.com'); return Yii::$app->getResponse()->redirect(Url::to('/')); } $block = ['view' => '@frontend/views/forms/subscribing_form', 'data' => compact('model')]; return $block; }
/** * Url: /unsubscribe/{$key} * @param $key string * @return mixed */ public function actionUnsubscribe($key) { $subscribings = Subscribing::find()->all(); foreach ($subscribings as $subscribing) { if (md5($subscribing->id . $subscribing->email) === $key) { Yii::$app->session->setFlash("success-unsubscribe", "Ваш email " . $subscribing->email . " успешно отписан от рассылки новостей."); $subscribing->delete(); } } return $this->redirect(Url::to('/')); }