1 | <?php |
||||
2 | |||||
3 | namespace andmemasin\surveybasemodels; |
||||
4 | |||||
5 | use andmemasin\myabstract\MyActiveRecord; |
||||
6 | use PascalDeVink\ShortUuid\ShortUuid; |
||||
7 | use Ramsey\Uuid\Uuid; |
||||
8 | use yii; |
||||
9 | |||||
10 | /** |
||||
11 | * This is the model class for a generic Respondent. |
||||
12 | * |
||||
13 | * @property int $respondent_id |
||||
14 | * @property int $survey_id |
||||
15 | * @property string $token |
||||
16 | * @property Survey $survey |
||||
17 | * @property string $email_address |
||||
18 | * @property string $alternative_email_addresses Inserted as CSV, stored as JSON |
||||
19 | * @property string $phone_number |
||||
20 | * @property string $alternative_phone_numbers Inserted as CSV, stored as JSON |
||||
21 | * |
||||
22 | * @property boolean $isRejected |
||||
23 | * @property string $shortToken If the token is uuid, then short-uuid will be returned |
||||
24 | */ |
||||
25 | class Respondent extends MyActiveRecord |
||||
26 | { |
||||
27 | const MAX_ALTERNATIVE_CONTACTS = 20; |
||||
28 | /** @var bool $checkDSNForEmails whether email validation also used DSN records to check if domain exists */ |
||||
29 | public static $checkDSNForEmails = true; |
||||
30 | |||||
31 | /** |
||||
32 | * @var array $surveyIdentifyingColumns names of the columns that identify a respondent as unique inside a survey |
||||
33 | */ |
||||
34 | public static $surveyIdentifyingColumns = ['phone_number', 'email_address']; |
||||
35 | |||||
36 | public function init() |
||||
37 | { |
||||
38 | parent::init(); |
||||
39 | } |
||||
40 | |||||
41 | |||||
42 | /** |
||||
43 | * {@inheritdoc} |
||||
44 | */ |
||||
45 | public function rules() |
||||
46 | { |
||||
47 | return array_merge([ |
||||
48 | [['survey_id', 'token'], 'required'], |
||||
49 | [['survey_id'], 'integer'], |
||||
50 | [['email_address'], 'validateEmail'], |
||||
51 | // email addresses always lowercase |
||||
52 | [['email_address', 'email_address'], 'trim'], |
||||
53 | ['email_address', 'filter', 'filter' => 'strtolower'], |
||||
54 | [['alternative_email_addresses'], 'string'], |
||||
55 | [['alternative_email_addresses'], 'validateMultipleEmails'], |
||||
56 | [['phone_number', 'email_address'], 'trim'], |
||||
57 | ['phone_number', 'filter', 'filter' => 'strtolower'], |
||||
58 | [['phone_number'], 'string'], |
||||
59 | [['phone_number'], 'validatePhoneNumber'], |
||||
60 | [['alternative_phone_numbers'], 'string'], |
||||
61 | [['alternative_phone_numbers'], 'validateMultiplePhoneNumbers'], |
||||
62 | [['token'], 'unique'], |
||||
63 | ], parent::rules()); |
||||
64 | } |
||||
65 | |||||
66 | |||||
67 | /** |
||||
68 | * @param string $attribute |
||||
69 | * @param string $address |
||||
70 | * @return bool |
||||
71 | */ |
||||
72 | public function validateEmail($attribute, $address = null) |
||||
73 | { |
||||
74 | |||||
75 | if ($this->validateEmailFormat($attribute, $address) |
||||
76 | && !$this->isSameAsMainAddress($attribute, $address) |
||||
77 | && !$this->isEmailSurveyDuplicate($attribute, $address)) { |
||||
78 | return true; |
||||
79 | } |
||||
80 | return false; |
||||
81 | } |
||||
82 | |||||
83 | /** |
||||
84 | * @param string $attribute |
||||
85 | * @param string $address |
||||
86 | * @return bool |
||||
87 | */ |
||||
88 | private function validateEmailFormat($attribute, $address = null) |
||||
89 | { |
||||
90 | $validator = new yii\validators\EmailValidator(); |
||||
91 | $validator->checkDNS = static::$checkDSNForEmails; |
||||
92 | if (!$validator->validate($address)) { |
||||
93 | $this->addError($attribute, |
||||
94 | Yii::t('app', |
||||
95 | 'Invalid email address "{0}"', [$address] |
||||
96 | ) . ' ' . Yii::t('app', 'Reason: {0}', [Yii::t('app', 'Invalid email format')]) |
||||
97 | ); |
||||
98 | return false; |
||||
99 | } |
||||
100 | return true; |
||||
101 | } |
||||
102 | |||||
103 | /** |
||||
104 | * @param string $attribute |
||||
105 | * @param string $address |
||||
106 | * @return bool |
||||
107 | */ |
||||
108 | private function isSameAsMainAddress($attribute, $address = null) |
||||
109 | { |
||||
110 | if (empty($address)) { |
||||
111 | return false; |
||||
112 | } |
||||
113 | |||||
114 | if ($attribute == 'email_address' ? false : $address == $this->email_address) { |
||||
115 | $this->addError($attribute, |
||||
116 | Yii::t('app', |
||||
117 | 'Invalid email address "{0}"', [$address] |
||||
118 | ) . ' ' . Yii::t('app', 'Reason: {0}', [Yii::t('app', $attribute . ' duplicates main address')]) |
||||
119 | ); |
||||
120 | return false; |
||||
121 | } |
||||
122 | return true; |
||||
123 | } |
||||
124 | |||||
125 | |||||
126 | public function validateMultipleEmails($attribute) |
||||
127 | { |
||||
128 | $cleanAddresses = []; |
||||
129 | $addresses = yii\helpers\Json::decode($this->alternative_email_addresses); |
||||
130 | if (!empty($addresses)) { |
||||
131 | $i = 0; |
||||
132 | foreach ($addresses as $key => $address) { |
||||
133 | $i++; |
||||
134 | // check the alternative numbers of that model for duplicates |
||||
135 | $checkItems = $addresses; |
||||
136 | unset($checkItems[$key]); |
||||
137 | if (in_array($address, $checkItems)) { |
||||
138 | $this->addError($attribute, Yii::t('app', 'Duplicate email in alternative email addresses')); |
||||
139 | } |
||||
140 | |||||
141 | if ($i >= static::MAX_ALTERNATIVE_CONTACTS) { |
||||
142 | $this->addError($attribute, Yii::t('app', 'Maximum alternative addresses limit ({0}) reached for {1}', [static::MAX_ALTERNATIVE_CONTACTS, $this->email_address])); |
||||
143 | } |
||||
144 | $address = strtolower(trim($address)); |
||||
145 | if ($this->validateEmail($attribute, $address)) { |
||||
146 | $cleanAddresses[] = $address; |
||||
147 | } |
||||
148 | } |
||||
149 | } |
||||
150 | $this->alternative_email_addresses = yii\helpers\Json::encode($cleanAddresses); |
||||
151 | } |
||||
152 | |||||
153 | |||||
154 | public function validatePhoneNumber($attribute, $phone_number = null) |
||||
155 | { |
||||
156 | $this->validateSameAsMainNumber($attribute, $phone_number); |
||||
157 | // TODO |
||||
158 | $isValidFormat = true; |
||||
0 ignored issues
–
show
Unused Code
introduced
by
![]() |
|||||
159 | $this->validatePhoneSurveyDuplicate($attribute, $phone_number); |
||||
160 | } |
||||
161 | |||||
162 | |||||
163 | public function validateMultiplePhoneNumbers($attribute) |
||||
164 | { |
||||
165 | $cleanItems = []; |
||||
166 | $items = yii\helpers\Json::decode($this->alternative_phone_numbers); |
||||
167 | if (!empty($items)) { |
||||
168 | $i = 0; |
||||
169 | foreach ($items as $key => $item) { |
||||
170 | $i++; |
||||
171 | $item = strtolower(trim($item)); |
||||
172 | $this->validateAlternativePhoneNumberInternalDuplicates($attribute, $item, $key); |
||||
173 | |||||
174 | if ($i >= static::MAX_ALTERNATIVE_CONTACTS) { |
||||
175 | $this->addError($attribute, Yii::t('app', 'Maximum alternative phone numbers limit ({0}) reached for {1}', [static::MAX_ALTERNATIVE_CONTACTS, $this->phone_number])); |
||||
176 | } |
||||
177 | $this->validatePhoneNumber($attribute, $item); |
||||
178 | } |
||||
179 | } |
||||
180 | $this->alternative_phone_numbers = yii\helpers\Json::encode($cleanItems); |
||||
181 | } |
||||
182 | |||||
183 | private function validateAlternativePhoneNumberInternalDuplicates($attribute, $number, $key) |
||||
184 | { |
||||
185 | $items = yii\helpers\Json::decode($this->alternative_phone_numbers); |
||||
186 | $checkItems = $items; |
||||
187 | unset($checkItems[$key]); |
||||
188 | if (in_array($number, $checkItems)) { |
||||
0 ignored issues
–
show
It seems like
$checkItems can also be of type null ; however, parameter $haystack of in_array() does only seem to accept array , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
189 | $this->addError($attribute, Yii::t('app', 'Duplicate number in alternative phone numbers')); |
||||
190 | } |
||||
191 | } |
||||
192 | |||||
193 | |||||
194 | /** |
||||
195 | * @param string $attribute |
||||
196 | * @param string $number |
||||
197 | * @return null |
||||
198 | */ |
||||
199 | private function validateSameAsMainNumber($attribute, $number = null) |
||||
200 | { |
||||
201 | if (!$number or empty($number)) { |
||||
202 | return null; |
||||
203 | } |
||||
204 | |||||
205 | $isSame = ($attribute == 'phone_number' ? false : $number == $this->phone_number); |
||||
206 | |||||
207 | if ($isSame) { |
||||
208 | $this->addError($attribute, |
||||
209 | Yii::t('app', |
||||
210 | 'Invalid phone number "{0}"', [$number] |
||||
211 | ) . ' ' . Yii::t('app', 'Reason: {0}', [Yii::t('app', $attribute . ' duplicates main phone number')]) |
||||
212 | ); |
||||
213 | } |
||||
214 | return null; |
||||
215 | } |
||||
216 | |||||
217 | |||||
218 | /** |
||||
219 | * @param string $attribute |
||||
220 | * @param string $phone_number Phone number to check duplicates for |
||||
221 | */ |
||||
222 | private function validatePhoneSurveyDuplicate($attribute, $phone_number) |
||||
223 | { |
||||
224 | $query = static::find(); |
||||
225 | // check only this survey |
||||
226 | $query->andWhere(['survey_id' => $this->survey_id]); |
||||
227 | |||||
228 | if ($this->respondent_id) { |
||||
229 | // not itself |
||||
230 | $query->andWhere(['!=', 'respondent_id', $this->respondent_id]); |
||||
231 | } |
||||
232 | |||||
233 | $condition = ['or', |
||||
234 | '`phone_number`=:phone_number', |
||||
235 | '`alternative_phone_numbers` LIKE :phone_number2', |
||||
236 | ]; |
||||
237 | |||||
238 | $query->andWhere($condition, [':phone_number' => $phone_number, ':phone_number2' => '%\"' . $phone_number . '\"%']); |
||||
239 | |||||
240 | if ($query->count() > 0) { |
||||
241 | $this->addError($attribute, |
||||
242 | Yii::t('app', |
||||
243 | 'Invalid phone number "{0}"', [$phone_number] |
||||
244 | ) . ' ' . Yii::t('app', 'Reason: {0}', [Yii::t('app', 'Duplicate phone number')]) |
||||
245 | ); |
||||
246 | } |
||||
247 | } |
||||
248 | |||||
249 | |||||
250 | /** |
||||
251 | * @param string $email_address Email address to check duplicates for |
||||
252 | * @return bool |
||||
253 | */ |
||||
254 | public function isEmailSurveyDuplicate($attribute, $email_address) |
||||
255 | { |
||||
256 | $query = static::find(); |
||||
257 | // check only this survey |
||||
258 | $query->andWhere(['survey_id' => $this->survey_id]); |
||||
259 | // not itself |
||||
260 | if ($this->respondent_id) { |
||||
261 | // not itself |
||||
262 | $query->andWhere(['!=', 'respondent_id', $this->respondent_id]); |
||||
263 | } |
||||
264 | |||||
265 | $email_condition = ['or', |
||||
266 | '`email_address`=:email_address', |
||||
267 | '`alternative_email_addresses` LIKE :email_address2', |
||||
268 | ]; |
||||
269 | $query->andWhere($email_condition, [':email_address' => $email_address, ':email_address2' => '%\"' . $email_address . '\"%']); |
||||
270 | if ($query->count() > 0) { |
||||
271 | return true; |
||||
272 | } |
||||
273 | |||||
274 | $this->addError($attribute, |
||||
275 | Yii::t('app', |
||||
276 | 'Invalid email address "{0}"', [$email_address] |
||||
277 | ) . ' ' . Yii::t('app', 'Reason: {0}', [Yii::t('app', 'Duplicates some other address')]) |
||||
278 | ); |
||||
279 | |||||
280 | return false; |
||||
281 | } |
||||
282 | |||||
283 | /** |
||||
284 | * {@inheritdoc} |
||||
285 | */ |
||||
286 | public function attributeLabels() |
||||
287 | { |
||||
288 | return [ |
||||
289 | 'token' => Yii::t('app', 'Unique Token'), |
||||
290 | ]; |
||||
291 | } |
||||
292 | |||||
293 | /** |
||||
294 | * @param string $token Respondents token |
||||
295 | * @return static |
||||
296 | */ |
||||
297 | public static function findByToken($token = null) |
||||
298 | { |
||||
299 | if ($token) { |
||||
300 | $models = self::findByTokens([$token]); |
||||
301 | if (!empty($models)) { |
||||
302 | return $models[0]; |
||||
303 | } |
||||
304 | } |
||||
305 | return null; |
||||
306 | } |
||||
307 | |||||
308 | /** |
||||
309 | * @param string[] $tokens Respondents tokens |
||||
310 | * @return static[] |
||||
311 | */ |
||||
312 | public static function findByTokens($tokens) |
||||
313 | { |
||||
314 | /** @var static[] $model */ |
||||
315 | $models = static::find() |
||||
316 | ->andWhere(['in', 'token', $tokens]) |
||||
317 | ->all(); |
||||
318 | return $models; |
||||
319 | } |
||||
320 | |||||
321 | /** |
||||
322 | * Check whether respondent has rejected this specific survey or has a hard bounce on e_mail address |
||||
323 | * @return bool |
||||
324 | */ |
||||
325 | public function getIsRejected() |
||||
326 | { |
||||
327 | if (Rejection::rejectedByCode($this->token)) { |
||||
328 | return true; |
||||
329 | } |
||||
330 | |||||
331 | if (Rejection::bouncedByEmailAddress($this->email_address)) { |
||||
332 | return true; |
||||
333 | } |
||||
334 | |||||
335 | return false; |
||||
336 | } |
||||
337 | |||||
338 | /** |
||||
339 | * Get the last respondent based on Email-address |
||||
340 | * @param string $email_address |
||||
341 | * @return static |
||||
342 | */ |
||||
343 | public static function getLatestByEmail($email_address) |
||||
344 | { |
||||
345 | /** @var static $model */ |
||||
346 | $model = static::find() |
||||
347 | ->andWhere(['email_address' => $email_address]) |
||||
348 | ->orderBy([static::primaryKey()[0] => SORT_DESC]) |
||||
349 | ->one(); |
||||
350 | return $model; |
||||
351 | } |
||||
352 | |||||
353 | /** |
||||
354 | * @return string |
||||
355 | */ |
||||
356 | public function getShortToken() |
||||
357 | { |
||||
358 | if (Uuid::isValid($this->token)) { |
||||
359 | $uuid = Uuid::fromString($this->token); |
||||
360 | $shotUuid = new ShortUuid(); |
||||
361 | return $shotUuid->encode($uuid); |
||||
362 | } |
||||
363 | return $this->token; |
||||
364 | } |
||||
365 | |||||
366 | |||||
367 | } |