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
|
|
|
* @property \stdClass $bounceObject The bounce as object |
20
|
|
|
* @property string $bounceReason |
21
|
|
|
*/ |
22
|
|
|
class Rejection extends MyActiveRecord |
23
|
|
|
{ |
24
|
|
|
const BOUNCE_TYPE_HARD = 'hard'; |
25
|
|
|
const BOUNCE_TYPE_SOFT = 'soft'; |
26
|
|
|
const BOUNCE_TYPE_COMPLAINT = 'complaint'; |
27
|
|
|
const BOUNCE_TYPE_ANSWERED = 'answered'; |
28
|
|
|
const BOUNCE_TYPE_OTHER = 'other'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* {@inheritdoc} |
32
|
|
|
*/ |
33
|
|
|
public static function tableName() |
34
|
|
|
{ |
35
|
|
|
return 'rejection'; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritdoc} |
41
|
|
|
*/ |
42
|
|
|
public function rules() |
43
|
|
|
{ |
44
|
|
|
return array_merge([ |
45
|
|
|
[['survey_id', 'respondent_id'], 'integer'], |
46
|
|
|
[['email_address','time_rejected'], 'required'], |
47
|
|
|
[['bounce'], 'string'], |
48
|
|
|
[['time_rejected'], 'safe'], |
49
|
|
|
[['email_address'], 'string', 'max' => 255], |
50
|
|
|
[['email_address'], 'email'], |
51
|
|
|
[['type'], 'string', 'max' => 45], |
52
|
|
|
[['respondent_id'], 'exist', 'skipOnError' => true, 'targetClass' => Respondent::class, 'targetAttribute' => ['respondent_id' => 'respondent_id']], |
53
|
|
|
[['survey_id'], 'exist', 'skipOnError' => true, 'targetClass' => Survey::class, 'targetAttribute' => ['survey_id' => 'survey_id']], |
54
|
|
|
], parent::rules()); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param string $email_address |
60
|
|
|
* @return bool |
61
|
|
|
*/ |
62
|
|
|
public static function hasBouncedHard($email_address){ |
63
|
|
|
return (!empty(self::findHardBounces($email_address))); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
|
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param string $email_address |
70
|
|
|
* @return Rejection[] |
71
|
|
|
*/ |
72
|
|
|
public static function findHardBounces($email_address){ |
73
|
|
|
return Rejection::find() |
74
|
|
|
->andWhere(['email_address'=>$email_address, 'type' => self::BOUNCE_TYPE_HARD]) |
75
|
|
|
->all(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Check whether there are rejections that match the respondent's token |
81
|
|
|
* @param string $code |
82
|
|
|
* @return bool |
83
|
|
|
*/ |
84
|
|
|
public static function rejectedByCode($code){ |
85
|
|
|
$respondent = Respondent::findByToken($code); |
86
|
|
|
if($respondent){ |
|
|
|
|
87
|
|
|
$rejections = self::find() |
88
|
|
|
->andWhere(['respondent_id'=>$respondent->primaryKey]) |
89
|
|
|
->all(); |
90
|
|
|
if($rejections){ |
|
|
|
|
91
|
|
|
return true; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
return false; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Check whether we have a bounce registered from this email |
99
|
|
|
* @param string $email_address |
100
|
|
|
* @param string $type |
101
|
|
|
* @return bool |
102
|
|
|
*/ |
103
|
|
|
public static function bouncedByEmailAddress($email_address,$type = self::BOUNCE_TYPE_HARD){ |
104
|
|
|
$rejections = self::find() |
105
|
|
|
->andWhere('email_address=:email_address', [':email_address' => $email_address]) |
106
|
|
|
->andWhere('type=:type', [':type' => $type]) |
107
|
|
|
->all(); |
108
|
|
|
if($rejections){ |
|
|
|
|
109
|
|
|
return true; |
110
|
|
|
} |
111
|
|
|
return false; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param string $email_address |
116
|
|
|
* @return Rejection |
117
|
|
|
*/ |
118
|
|
|
public static function findByEmail($email_address){ |
119
|
|
|
return Rejection::findOne(['email_address'=>$email_address]); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @return \yii\db\ActiveQuery |
124
|
|
|
*/ |
125
|
|
|
public function getSurvey() |
126
|
|
|
{ |
127
|
|
|
return $this->hasOne(Survey::class, ['survey_id' => 'survey_id']); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @return \yii\db\ActiveQuery |
132
|
|
|
*/ |
133
|
|
|
public function getRespondent() |
134
|
|
|
{ |
135
|
|
|
return $this->hasOne(Respondent::class, ['respondent_id' => 'respondent_id']); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public static function getBounceTypes(){ |
139
|
|
|
return [ |
140
|
|
|
self::BOUNCE_TYPE_COMPLAINT => Yii::t('app','Complaint'), |
141
|
|
|
self::BOUNCE_TYPE_SOFT => Yii::t('app','Soft bounce'), |
142
|
|
|
self::BOUNCE_TYPE_HARD => Yii::t('app','Hard bounce'), |
143
|
|
|
self::BOUNCE_TYPE_ANSWERED => Yii::t('app','Respondent has answered already'), |
144
|
|
|
self::BOUNCE_TYPE_OTHER => Yii::t('app','Other'), |
145
|
|
|
]; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function getBounceObject() |
149
|
|
|
{ |
150
|
|
|
$object = json_decode($this->bounce); |
151
|
|
|
if (!empty($object)) { |
152
|
|
|
return $object; |
153
|
|
|
} |
154
|
|
|
return null; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
public function getBounceReason() |
158
|
|
|
{ |
159
|
|
|
if (!empty($this->bounceObject)) { |
160
|
|
|
if(isset($this->bounceObject->diagnosticcode)) { |
161
|
|
|
return $this->bounceObject->diagnosticcode; |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
return null; |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|