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