TonisOrmisson /
yii2-survey-base-models
| 1 | <?php |
||
| 2 | |||
| 3 | namespace andmemasin\surveybasemodels; |
||
| 4 | use andmemasin\myabstract\MyActiveRecord; |
||
| 5 | use yii; |
||
| 6 | |||
| 7 | /** |
||
| 8 | * |
||
| 9 | * @property integer $rejection_id |
||
| 10 | * @property integer $survey_id |
||
| 11 | * @property integer $respondent_id |
||
| 12 | * @property string $email_address |
||
| 13 | * @property string $type |
||
| 14 | * @property string $bounce |
||
| 15 | * @property string $time_rejected |
||
| 16 | * |
||
| 17 | * @property Respondent $respondent |
||
| 18 | * @property Survey $survey |
||
| 19 | */ |
||
| 20 | class Rejection extends MyActiveRecord |
||
| 21 | { |
||
| 22 | const BOUNCE_TYPE_HARD = 'hard'; |
||
| 23 | const BOUNCE_TYPE_SOFT = 'soft'; |
||
| 24 | const BOUNCE_TYPE_COMPLAINT = 'complaint'; |
||
| 25 | const BOUNCE_TYPE_ANSWERED = 'answered'; |
||
| 26 | const BOUNCE_TYPE_OTHER = 'other'; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * {@inheritdoc} |
||
| 30 | */ |
||
| 31 | public static function tableName() |
||
| 32 | { |
||
| 33 | return 'rejection'; |
||
| 34 | } |
||
| 35 | |||
| 36 | |||
| 37 | /** |
||
| 38 | * {@inheritdoc} |
||
| 39 | */ |
||
| 40 | public function rules() |
||
| 41 | { |
||
| 42 | return array_merge([ |
||
| 43 | [['survey_id', 'respondent_id'], 'integer'], |
||
| 44 | [['email_address','time_rejected'], 'required'], |
||
| 45 | [['bounce'], 'string'], |
||
| 46 | [['time_rejected'], 'safe'], |
||
| 47 | [['email_address'], 'string', 'max' => 255], |
||
| 48 | [['email_address'], 'email'], |
||
| 49 | [['type'], 'string', 'max' => 45], |
||
| 50 | [['respondent_id'], 'exist', 'skipOnError' => true, 'targetClass' => Respondent::class, 'targetAttribute' => ['respondent_id' => 'respondent_id']], |
||
| 51 | [['survey_id'], 'exist', 'skipOnError' => true, 'targetClass' => Survey::class, 'targetAttribute' => ['survey_id' => 'survey_id']], |
||
| 52 | ], parent::rules()); |
||
| 53 | } |
||
| 54 | |||
| 55 | |||
| 56 | /** |
||
| 57 | * @param string $email_address |
||
| 58 | * @return bool |
||
| 59 | */ |
||
| 60 | public static function hasBouncedHard($email_address){ |
||
| 61 | $rejection = Rejection::findByEmail($email_address); |
||
| 62 | if($rejection){ |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 63 | return true; |
||
| 64 | } |
||
| 65 | return false; |
||
| 66 | } |
||
| 67 | |||
| 68 | |||
| 69 | |||
| 70 | /** |
||
| 71 | * @param string $email_address |
||
| 72 | * @return Rejection[] |
||
| 73 | */ |
||
| 74 | public static function findHardBounces($email_address){ |
||
| 75 | return Rejection::find() |
||
| 76 | ->andWhere(['email_address'=>$email_address]) |
||
| 77 | |||
| 78 | ->all(); |
||
| 79 | } |
||
| 80 | |||
| 81 | |||
| 82 | /** |
||
| 83 | * Check whether there are rejections that match the respondent's token |
||
| 84 | * @param string $code |
||
| 85 | * @return bool |
||
| 86 | */ |
||
| 87 | public static function rejectedByCode($code){ |
||
| 88 | $respondent = Respondent::findByToken($code); |
||
| 89 | if($respondent){ |
||
|
0 ignored issues
–
show
|
|||
| 90 | $rejections = self::find() |
||
| 91 | ->andWhere(['respondent_id'=>$respondent->primaryKey]) |
||
| 92 | ->all(); |
||
| 93 | if($rejections){ |
||
| 94 | return true; |
||
| 95 | } |
||
| 96 | } |
||
| 97 | return false; |
||
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Check whether we have a bounce registered from this email |
||
| 102 | * @param string $email_address |
||
| 103 | * @param string $type |
||
| 104 | * @return bool |
||
| 105 | */ |
||
| 106 | public static function bouncedByEmailAddress($email_address,$type = self::BOUNCE_TYPE_HARD){ |
||
| 107 | $rejections = self::find() |
||
| 108 | ->andWhere('email_address=:email_address', [':email_address' => $email_address]) |
||
| 109 | ->andWhere('type=:type', [':type' => $type]) |
||
| 110 | ->all(); |
||
| 111 | if($rejections){ |
||
| 112 | return true; |
||
| 113 | } |
||
| 114 | return false; |
||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @param string $email_address |
||
| 119 | * @return Rejection |
||
| 120 | */ |
||
| 121 | public static function findByEmail($email_address){ |
||
| 122 | return Rejection::findOne(['email_address'=>$email_address]); |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @return \yii\db\ActiveQuery |
||
| 127 | */ |
||
| 128 | public function getSurvey() |
||
| 129 | { |
||
| 130 | return $this->hasOne(Survey::class, ['survey_id' => 'survey_id']); |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @return \yii\db\ActiveQuery |
||
| 135 | */ |
||
| 136 | public function getRespondent() |
||
| 137 | { |
||
| 138 | return $this->hasOne(Respondent::class, ['respondent_id' => 'respondent_id']); |
||
| 139 | } |
||
| 140 | |||
| 141 | public static function getBounceTypes(){ |
||
| 142 | return [ |
||
| 143 | self::BOUNCE_TYPE_COMPLAINT => Yii::t('app','Complaint'), |
||
| 144 | self::BOUNCE_TYPE_SOFT => Yii::t('app','Soft bounce'), |
||
| 145 | self::BOUNCE_TYPE_HARD => Yii::t('app','Hard bounce'), |
||
| 146 | self::BOUNCE_TYPE_ANSWERED => Yii::t('app','Respondent has answered already'), |
||
| 147 | self::BOUNCE_TYPE_OTHER => Yii::t('app','Other'), |
||
| 148 | ]; |
||
| 149 | } |
||
| 150 | } |
||
| 151 |