<?php
namespace frontend\controllers;
use Yii;
use common\models\Subscriber;
use common\models\SubscriberSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
/**
* SubscriberController implements the CRUD actions for Subscriber model.
*/
class SubscriberController extends Controller
{
/**
* Creates a new Subscriber model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionSubscribe()
{
$model = new Subscriber();
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
if ($model->sendEmail()) {
Yii::$app->session->setFlash('success', 'You have successfully subscribed My-Blog. You will get notification whenever New post is published');
return $this->goHome();
} else {
Yii::$app->session->setFlash('error', 'Sorry, we are unable to subscribe for the provided email address.');
}
}
return $this->render('create', [
'model' => $model,
]);
}
/**
* Finds the Subscriber model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param integer $id
* @return Subscriber the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/`enter code here`
}
使用以下模型:
<?php
namespace common\models;
use Yii;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\db\Expression;
/**
* This is the model class for table "subscriber".
*
* @property int $id
* @property string $email
* @property string $token
* @property int $status
* @property int $created_at
* @property int $updated_at
*/
class Subscriber extends \yii\db\ActiveRecord
{
const STATUS_DEACTIVE = 0;
const STATUS_ACTIVE = 1;
/**
* @inheritdoc
*/
public static function tableName()
{
return 'subscriber';
}
public function behaviors()
{
return [
'timestamp' => [
'class' => TimestampBehavior::className(),
'attributes' => [
ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
],
'value' => new Expression('NOW()'),
],
];
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['email'], 'required'],
[['status', 'created_at', 'updated_at'], 'integer'],
[['email'], 'string', 'max' => 60],
[['token'], 'string', 'max' => 255],
[['token'], 'unique'],
[['email'], 'unique', 'targetClass' => '\common\models\Subscriber', 'message' => 'This email has already subscribed our blog.','filter' => ['!=','status' ,0]],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'email' => 'Email',
'token' => 'Token',
'status' => 'Status',
'created_at' => 'Created At',
'updated_at' => 'Updated At',
];
}
/**
* Generates subscriber token
*/
public function generateSubscriberToken()
{
return $this->token = Yii::$app->security->generateRandomString() . '_' . time();
}
/**
* Send Email when successfully subscribe
*/
public function sendEmail()
{
$subscribers = Subscriber::find()->where(['email' => $this->email, 'status' => 0,])->one();
if(!$subscribers)
{
$this->generateSubscriberToken();
if(!$this->save())
{
return false;
}
return Yii::$app->mailer
->compose()
->setFrom(['noreply@my-blog.com' => Yii::$app->name . ' robot'])
->setTo('piyush@localhost')
->setSubject('Subscription : ' . Yii::$app->name)
->setHtmlBody('Thank you '.$this->email.' for subscribing '.Yii::$app->name.'<br /><br /> You will receive notification whenever new trick or post is published to website')
->send();
}
$subscribers->generateSubscriberToken();
$subscribers->status = 1;
if(!$subscribers->save())
{
return false;
}
return Yii::$app->mailer
->compose()
->setFrom(['noreply@my-blog.com' => Yii::$app->name . ' robot'])
->setTo('piyush@localhost')
->setSubject('Subscription : ' . Yii::$app->name)
->setHtmlBody('Welcome back '.$this->email.'Thank you for subscribing '.Yii::$app->name.'<br /><br /> You will receive notification whenever new trick or post is published to website')
->send();
}
}
此控制器和模型用于使用电子邮件进行订阅活动。我希望如果用户已取消订阅,并且在一段时间后再次想要订阅,则更新status = 1并重新生成令牌。如果sendEmail
是新订阅者,则上面status 0
工作正常,但如果它是if (!caught.includes("AreYouHuman") && !caught.includes("pfail=1") && caught.includes("Episode") && caught.includes("Anime")) {
var arraystring = caught.split("&s");
var updating = chrome.tabs.update({url: arraystring[0] + "&s=beta&pfail=1"});
}
的旧订阅者则无效。
答案 0 :(得分:0)
最重要的是,您需要替换
行$subscribers->generateSubscriberToken();
$subscribers->status = 1;
带
$subscriber->token =$this->generateSubscriberToken();
$subscribers->status = 1;
在您的功能中,您正在设置$this->token
并将其返回并更新您需要设置$subcribers->token
字段的记录。
您不应在表格中搜索包含status 0
的电子邮件,只需查询email
并在PHP
中查看status ==0
,因为只有输入新记录才能输入如果电子邮件存在,但status
您的查询无法获取记录,并且会尝试插入记录,那么电子邮件不存在,而不关心status =1
字段对您的情况有何影响而不是什么都不做。
要了解您可以尝试在两种情况下都使用var_dump(!$subscribers)
来查看它返回的内容。
此外,您正在重复发送电子邮件和令牌生成等内容,您应该将功能更改为以下内容。
public function sendEmail()
{
$subscribers = self::find()->where(['email' => $this->email])->one();
//set flag for sending email
$sendMail = false;
//email subject
$subject = '';
//generate token
$token = $this->generateSubscriberToken();
//if email found in subscribers
if ($subscribers !== null) {
//check if inactive
if ($subscribers->status !== self::STATUS_ACTIVE) {
//assign token
$subscribers->token = $token;
//set status to active
$subscribers->status = self::STATUS_ACTIVE;
//update the recrod
if (!$subscribers->save()) {
return false;
}
//set subject
$subject = 'Welcome back ' . $this->email . 'Thank you for subscribing ' . Yii::$app->name . '<br /><br /> You will receive notification whenever new trick or post is published to website';
$sendMail = true;
}
} else { //if email does not exist only then insert a new record
$this->status = 1;
if (!$this->save()) {
return false;
}
$subject = 'Thank you ' . $this->email . ' for subscribing ' . Yii::$app->name . '<br /><br /> You will receive notification whenever new trick or post is published to website';
$sendMail = true;
}
//check if send mail flag set
if ($sendMail) {
return Yii::$app->mailer
->compose()
->setFrom(['noreply@my-blog.com' => Yii::$app->name . ' robot'])
->setTo('piyush@localhost')
->setSubject('Subscription : ' . Yii::$app->name)
->setHtmlBody($subject)
->send();
}
}