@@ -31,7 +31,7 @@ discard block |
||
| 31 | 31 | /** |
| 32 | 32 | * @var array $surveyIdentifyingColumns names of the columns that identify a respondent as unique inside a survey |
| 33 | 33 | */ |
| 34 | - public static $surveyIdentifyingColumns = ['phone_number','email_address']; |
|
| 34 | + public static $surveyIdentifyingColumns = ['phone_number', 'email_address']; |
|
| 35 | 35 | |
| 36 | 36 | public function init() |
| 37 | 37 | { |
@@ -45,20 +45,20 @@ discard block |
||
| 45 | 45 | public function rules() |
| 46 | 46 | { |
| 47 | 47 | return array_merge([ |
| 48 | - [['survey_id','token'], 'required'], |
|
| 48 | + [['survey_id', 'token'], 'required'], |
|
| 49 | 49 | [['survey_id'], 'integer'], |
| 50 | 50 | [['email_address'], 'validateEmail'], |
| 51 | 51 | // email addresses always lowercase |
| 52 | - [['email_address','email_address'],'trim'], |
|
| 53 | - ['email_address','filter', 'filter' => 'strtolower'], |
|
| 52 | + [['email_address', 'email_address'], 'trim'], |
|
| 53 | + ['email_address', 'filter', 'filter' => 'strtolower'], |
|
| 54 | 54 | [['alternative_email_addresses'], 'string'], |
| 55 | 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'], |
|
| 56 | + [['phone_number', 'email_address'], 'trim'], |
|
| 57 | + ['phone_number', 'filter', 'filter' => 'strtolower'], |
|
| 58 | + [['phone_number'], 'string'], |
|
| 59 | + [['phone_number'], 'validatePhoneNumber'], |
|
| 60 | 60 | [['alternative_phone_numbers'], 'string'], |
| 61 | - [['alternative_phone_numbers'],'validateMultiplePhoneNumbers'], |
|
| 61 | + [['alternative_phone_numbers'], 'validateMultiplePhoneNumbers'], |
|
| 62 | 62 | [['token'], 'unique'], |
| 63 | 63 | ], parent::rules()); |
| 64 | 64 | } |
@@ -70,11 +70,11 @@ discard block |
||
| 70 | 70 | * @param string $address |
| 71 | 71 | * @return bool |
| 72 | 72 | */ |
| 73 | - public function validateEmail($attribute,$address = null){ |
|
| 73 | + public function validateEmail($attribute, $address = null) { |
|
| 74 | 74 | |
| 75 | - if($this->validateEmailFormat($attribute, $address) |
|
| 75 | + if ($this->validateEmailFormat($attribute, $address) |
|
| 76 | 76 | && !$this->isSameAsMainAddress($attribute, $address) |
| 77 | - && !$this->isEmailSurveyDuplicate($attribute, $address)){ |
|
| 77 | + && !$this->isEmailSurveyDuplicate($attribute, $address)) { |
|
| 78 | 78 | return true; |
| 79 | 79 | } |
| 80 | 80 | return false; |
@@ -92,8 +92,8 @@ discard block |
||
| 92 | 92 | if (!$validator->validate($address)) { |
| 93 | 93 | $this->addError($attribute, |
| 94 | 94 | Yii::t('app', |
| 95 | - 'Invalid email address "{0}"',[$address] |
|
| 96 | - ).' '.Yii::t('app','Reason: {0}',[Yii::t('app','Invalid email format')]) |
|
| 95 | + 'Invalid email address "{0}"', [$address] |
|
| 96 | + ).' '.Yii::t('app', 'Reason: {0}', [Yii::t('app', 'Invalid email format')]) |
|
| 97 | 97 | ); |
| 98 | 98 | return false; |
| 99 | 99 | } |
@@ -107,48 +107,48 @@ discard block |
||
| 107 | 107 | */ |
| 108 | 108 | private function isSameAsMainAddress($attribute, $address = null) |
| 109 | 109 | { |
| 110 | - if(!$address or empty($address)){ |
|
| 110 | + if (!$address or empty($address)) { |
|
| 111 | 111 | return false; |
| 112 | 112 | } |
| 113 | - $isSame = ($attribute=='email_address' ? false : $address == $this->email_address); |
|
| 113 | + $isSame = ($attribute == 'email_address' ? false : $address == $this->email_address); |
|
| 114 | 114 | if ($isSame) { |
| 115 | 115 | $this->addError($attribute, |
| 116 | 116 | Yii::t('app', |
| 117 | - 'Invalid email address "{0}"',[$address] |
|
| 118 | - ).' '.Yii::t('app','Reason: {0}',[Yii::t('app',$attribute. ' duplicates main address')]) |
|
| 117 | + 'Invalid email address "{0}"', [$address] |
|
| 118 | + ).' '.Yii::t('app', 'Reason: {0}', [Yii::t('app', $attribute.' duplicates main address')]) |
|
| 119 | 119 | ); |
| 120 | 120 | } |
| 121 | 121 | return $isSame; |
| 122 | 122 | } |
| 123 | 123 | |
| 124 | 124 | |
| 125 | - public function validateMultipleEmails($attribute){ |
|
| 125 | + public function validateMultipleEmails($attribute) { |
|
| 126 | 126 | $addresses = yii\helpers\Json::decode($this->alternative_email_addresses); |
| 127 | - if($this->alternative_email_addresses && !empty($addresses)){ |
|
| 127 | + if ($this->alternative_email_addresses && !empty($addresses)) { |
|
| 128 | 128 | $cleanAddresses = []; |
| 129 | - if(!empty($addresses)){ |
|
| 130 | - $i=0; |
|
| 131 | - foreach ($addresses as $key => $address){ |
|
| 132 | - if($address <> ""){ |
|
| 129 | + if (!empty($addresses)) { |
|
| 130 | + $i = 0; |
|
| 131 | + foreach ($addresses as $key => $address) { |
|
| 132 | + if ($address <> "") { |
|
| 133 | 133 | // check the alternative numbers of that model for duplicates |
| 134 | 134 | $checkItems = $addresses; |
| 135 | 135 | unset($checkItems[$key]); |
| 136 | - if(in_array($address,$checkItems)){ |
|
| 137 | - $this->addError($attribute,Yii::t('app','Duplicate email in alternative email addresses')); |
|
| 136 | + if (in_array($address, $checkItems)) { |
|
| 137 | + $this->addError($attribute, Yii::t('app', 'Duplicate email in alternative email addresses')); |
|
| 138 | 138 | } |
| 139 | 139 | |
| 140 | 140 | $i++; |
| 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])); |
|
| 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 | 143 | } |
| 144 | 144 | $address = strtolower(trim($address)); |
| 145 | - if($this->validateEmail($attribute,$address)){ |
|
| 146 | - $cleanAddresses[]=$address; |
|
| 145 | + if ($this->validateEmail($attribute, $address)) { |
|
| 146 | + $cleanAddresses[] = $address; |
|
| 147 | 147 | } |
| 148 | 148 | } |
| 149 | 149 | |
| 150 | 150 | } |
| 151 | - if(!empty($cleanAddresses)){ |
|
| 151 | + if (!empty($cleanAddresses)) { |
|
| 152 | 152 | $this->alternative_email_addresses = yii\helpers\Json::encode($cleanAddresses); |
| 153 | 153 | } else { |
| 154 | 154 | $this->alternative_email_addresses = null; |
@@ -158,68 +158,68 @@ discard block |
||
| 158 | 158 | } |
| 159 | 159 | |
| 160 | 160 | |
| 161 | - public function validatePhoneNumber($attribute, $phone_number = null){ |
|
| 162 | - if(!$phone_number or empty($phone_number)){ |
|
| 161 | + public function validatePhoneNumber($attribute, $phone_number = null) { |
|
| 162 | + if (!$phone_number or empty($phone_number)) { |
|
| 163 | 163 | $phone_number = $this->phone_number; |
| 164 | 164 | $isSameAsMain = false; |
| 165 | - }else{ |
|
| 166 | - $isSameAsMain = ($attribute=='phone_number' ? false : $phone_number == $this->phone_number); |
|
| 165 | + } else { |
|
| 166 | + $isSameAsMain = ($attribute == 'phone_number' ? false : $phone_number == $this->phone_number); |
|
| 167 | 167 | } |
| 168 | 168 | // TODO |
| 169 | 169 | $isValidFormat = true; |
| 170 | 170 | |
| 171 | 171 | $isDuplicate = $this->isPhoneSurveyDuplicate($phone_number); |
| 172 | 172 | |
| 173 | - if($isValidFormat && !$isSameAsMain && !$isDuplicate){ |
|
| 173 | + if ($isValidFormat && !$isSameAsMain && !$isDuplicate) { |
|
| 174 | 174 | return true; |
| 175 | - }else{ |
|
| 175 | + } else { |
|
| 176 | 176 | $reason = ''; |
| 177 | - if(!$isValidFormat){ |
|
| 178 | - $reason = Yii::t('app','Invalid phone number format'); |
|
| 179 | - } else if($isSameAsMain){ |
|
| 180 | - $reason = Yii::t('app',$attribute. ' duplicates main phone number'); |
|
| 181 | - } else if($isDuplicate) { |
|
| 182 | - $reason = Yii::t('app','Duplicate phone number'); |
|
| 177 | + if (!$isValidFormat) { |
|
| 178 | + $reason = Yii::t('app', 'Invalid phone number format'); |
|
| 179 | + } else if ($isSameAsMain) { |
|
| 180 | + $reason = Yii::t('app', $attribute.' duplicates main phone number'); |
|
| 181 | + } else if ($isDuplicate) { |
|
| 182 | + $reason = Yii::t('app', 'Duplicate phone number'); |
|
| 183 | 183 | } |
| 184 | 184 | |
| 185 | 185 | $this->addError($attribute, |
| 186 | 186 | Yii::t('app', |
| 187 | - 'Invalid phone number "{0}"',[$phone_number] |
|
| 188 | - ).' '.Yii::t('app','Reason: {0}',[$reason]) |
|
| 187 | + 'Invalid phone number "{0}"', [$phone_number] |
|
| 188 | + ).' '.Yii::t('app', 'Reason: {0}', [$reason]) |
|
| 189 | 189 | ); |
| 190 | 190 | } |
| 191 | 191 | return false; |
| 192 | 192 | } |
| 193 | 193 | |
| 194 | - public function validateMultiplePhoneNumbers($attribute){ |
|
| 195 | - if($this->alternative_phone_numbers){ |
|
| 194 | + public function validateMultiplePhoneNumbers($attribute) { |
|
| 195 | + if ($this->alternative_phone_numbers) { |
|
| 196 | 196 | $cleanItems = []; |
| 197 | 197 | $items = yii\helpers\Json::decode($this->alternative_phone_numbers); |
| 198 | 198 | |
| 199 | - if(!empty($items)){ |
|
| 200 | - $i=0; |
|
| 201 | - foreach ($items as $key=> $item){ |
|
| 199 | + if (!empty($items)) { |
|
| 200 | + $i = 0; |
|
| 201 | + foreach ($items as $key=> $item) { |
|
| 202 | 202 | $item = strtolower(trim($item)); |
| 203 | - if($item <> ''){ |
|
| 203 | + if ($item <> '') { |
|
| 204 | 204 | $i++; |
| 205 | 205 | // check the alternative numbers of that model for duplicates |
| 206 | 206 | $checkItems = $items; |
| 207 | 207 | unset($checkItems[$key]); |
| 208 | - if(in_array($item,$checkItems)){ |
|
| 209 | - $this->addError($attribute,Yii::t('app','Duplicate number in alternative phone numbers')); |
|
| 208 | + if (in_array($item, $checkItems)) { |
|
| 209 | + $this->addError($attribute, Yii::t('app', 'Duplicate number in alternative phone numbers')); |
|
| 210 | 210 | } |
| 211 | 211 | |
| 212 | 212 | |
| 213 | - if($i>=static::MAX_ALTERNATIVE_CONTACTS){ |
|
| 214 | - $this->addError($attribute,Yii::t('app','Maximum alternative phone numbers limit ({0}) reached for {1}',[static::MAX_ALTERNATIVE_CONTACTS,$this->phone_number])); |
|
| 213 | + if ($i >= static::MAX_ALTERNATIVE_CONTACTS) { |
|
| 214 | + $this->addError($attribute, Yii::t('app', 'Maximum alternative phone numbers limit ({0}) reached for {1}', [static::MAX_ALTERNATIVE_CONTACTS, $this->phone_number])); |
|
| 215 | 215 | } |
| 216 | - if($this->validatePhoneNumber($attribute,$item)){ |
|
| 217 | - $cleanItems[]=$item; |
|
| 216 | + if ($this->validatePhoneNumber($attribute, $item)) { |
|
| 217 | + $cleanItems[] = $item; |
|
| 218 | 218 | } |
| 219 | 219 | |
| 220 | 220 | } |
| 221 | 221 | } |
| 222 | - if(!empty($cleanItems)){ |
|
| 222 | + if (!empty($cleanItems)) { |
|
| 223 | 223 | $this->alternative_phone_numbers = yii\helpers\Json::encode($cleanItems); |
| 224 | 224 | } else { |
| 225 | 225 | $this->alternative_phone_numbers = null; |
@@ -234,22 +234,22 @@ discard block |
||
| 234 | 234 | * @param string $phone_number Phone number to check duplicates for |
| 235 | 235 | * @return bool |
| 236 | 236 | */ |
| 237 | - public function isPhoneSurveyDuplicate($phone_number){ |
|
| 237 | + public function isPhoneSurveyDuplicate($phone_number) { |
|
| 238 | 238 | $query = static::find(); |
| 239 | 239 | // check only this survey |
| 240 | 240 | $query->andWhere(['survey_id'=>$this->survey_id]); |
| 241 | 241 | |
| 242 | - if($this->respondent_id){ |
|
| 242 | + if ($this->respondent_id) { |
|
| 243 | 243 | // not itself |
| 244 | - $query->andWhere(['!=','respondent_id',$this->respondent_id]); |
|
| 244 | + $query->andWhere(['!=', 'respondent_id', $this->respondent_id]); |
|
| 245 | 245 | } |
| 246 | 246 | |
| 247 | 247 | $condition = ['or', |
| 248 | 248 | '`phone_number`=:phone_number', |
| 249 | 249 | '`alternative_phone_numbers` LIKE :phone_number2', |
| 250 | 250 | ]; |
| 251 | - $query->andWhere($condition,[':phone_number'=>$phone_number,':phone_number2'=>'%\"'.$phone_number.'\"%']); |
|
| 252 | - if($query->count() > 0){ |
|
| 251 | + $query->andWhere($condition, [':phone_number'=>$phone_number, ':phone_number2'=>'%\"'.$phone_number.'\"%']); |
|
| 252 | + if ($query->count() > 0) { |
|
| 253 | 253 | return true; |
| 254 | 254 | } |
| 255 | 255 | return false; |
@@ -260,29 +260,29 @@ discard block |
||
| 260 | 260 | * @param string $email_address Email address to check duplicates for |
| 261 | 261 | * @return bool |
| 262 | 262 | */ |
| 263 | - public function isEmailSurveyDuplicate($attribute, $email_address){ |
|
| 263 | + public function isEmailSurveyDuplicate($attribute, $email_address) { |
|
| 264 | 264 | $query = static::find(); |
| 265 | 265 | // check only this survey |
| 266 | 266 | $query->andWhere(['survey_id'=>$this->survey_id]); |
| 267 | 267 | // not itself |
| 268 | - if($this->respondent_id){ |
|
| 268 | + if ($this->respondent_id) { |
|
| 269 | 269 | // not itself |
| 270 | - $query->andWhere(['!=','respondent_id',$this->respondent_id]); |
|
| 270 | + $query->andWhere(['!=', 'respondent_id', $this->respondent_id]); |
|
| 271 | 271 | } |
| 272 | 272 | |
| 273 | 273 | $email_condition = ['or', |
| 274 | 274 | '`email_address`=:email_address', |
| 275 | 275 | '`alternative_email_addresses` LIKE :email_address2', |
| 276 | 276 | ]; |
| 277 | - $query->andWhere($email_condition,[':email_address'=>$email_address,':email_address2'=>'%\"'.$email_address.'\"%']); |
|
| 278 | - if($query->count() > 0){ |
|
| 277 | + $query->andWhere($email_condition, [':email_address'=>$email_address, ':email_address2'=>'%\"'.$email_address.'\"%']); |
|
| 278 | + if ($query->count() > 0) { |
|
| 279 | 279 | return true; |
| 280 | 280 | } |
| 281 | 281 | |
| 282 | 282 | $this->addError($attribute, |
| 283 | 283 | Yii::t('app', |
| 284 | - 'Invalid email address "{0}"',[$email_address] |
|
| 285 | - ).' '.Yii::t('app','Reason: {0}',[Yii::t('app','Duplicates some other address')]) |
|
| 284 | + 'Invalid email address "{0}"', [$email_address] |
|
| 285 | + ).' '.Yii::t('app', 'Reason: {0}', [Yii::t('app', 'Duplicates some other address')]) |
|
| 286 | 286 | ); |
| 287 | 287 | |
| 288 | 288 | return false; |
@@ -302,10 +302,10 @@ discard block |
||
| 302 | 302 | * @param string $token Respondents token |
| 303 | 303 | * @return static |
| 304 | 304 | */ |
| 305 | - public static function findByToken($token = null){ |
|
| 306 | - if($token){ |
|
| 305 | + public static function findByToken($token = null) { |
|
| 306 | + if ($token) { |
|
| 307 | 307 | $models = self::findByTokens([$token]); |
| 308 | - if (!empty($models)){ |
|
| 308 | + if (!empty($models)) { |
|
| 309 | 309 | return $models[0]; |
| 310 | 310 | } |
| 311 | 311 | } |
@@ -316,10 +316,10 @@ discard block |
||
| 316 | 316 | * @param string[] $tokens Respondents tokens |
| 317 | 317 | * @return static[] |
| 318 | 318 | */ |
| 319 | - public static function findByTokens($tokens){ |
|
| 319 | + public static function findByTokens($tokens) { |
|
| 320 | 320 | /** @var static[] $model */ |
| 321 | 321 | $models = static::find() |
| 322 | - ->andWhere(['in','token', $tokens]) |
|
| 322 | + ->andWhere(['in', 'token', $tokens]) |
|
| 323 | 323 | ->all(); |
| 324 | 324 | return $models; |
| 325 | 325 | } |
@@ -328,11 +328,11 @@ discard block |
||
| 328 | 328 | * Check whether respondent has rejected this specific survey or has a hard bounce on e_mail address |
| 329 | 329 | * @return bool |
| 330 | 330 | */ |
| 331 | - public function getIsRejected(){ |
|
| 332 | - if(Rejection::rejectedByCode($this->token)){ |
|
| 331 | + public function getIsRejected() { |
|
| 332 | + if (Rejection::rejectedByCode($this->token)) { |
|
| 333 | 333 | return true; |
| 334 | 334 | } |
| 335 | - if(Rejection::bouncedByEmailAddress($this->email_address)){ |
|
| 335 | + if (Rejection::bouncedByEmailAddress($this->email_address)) { |
|
| 336 | 336 | return true; |
| 337 | 337 | } |
| 338 | 338 | |
@@ -344,7 +344,7 @@ discard block |
||
| 344 | 344 | * @param string $email_address |
| 345 | 345 | * @return static |
| 346 | 346 | */ |
| 347 | - public static function getLatestByEmail($email_address){ |
|
| 347 | + public static function getLatestByEmail($email_address) { |
|
| 348 | 348 | /** @var static $model */ |
| 349 | 349 | $model = static::find() |
| 350 | 350 | ->andWhere(['email_address'=>$email_address]) |
@@ -356,8 +356,8 @@ discard block |
||
| 356 | 356 | /** |
| 357 | 357 | * @return string |
| 358 | 358 | */ |
| 359 | - public function getShortToken(){ |
|
| 360 | - if(Uuid::isValid($this->token)){ |
|
| 359 | + public function getShortToken() { |
|
| 360 | + if (Uuid::isValid($this->token)) { |
|
| 361 | 361 | $uuid = Uuid::fromString($this->token); |
| 362 | 362 | $shotUuid = new ShortUuid(); |
| 363 | 363 | return $shotUuid->encode($uuid); |
@@ -162,7 +162,7 @@ discard block |
||
| 162 | 162 | if(!$phone_number or empty($phone_number)){ |
| 163 | 163 | $phone_number = $this->phone_number; |
| 164 | 164 | $isSameAsMain = false; |
| 165 | - }else{ |
|
| 165 | + } else{ |
|
| 166 | 166 | $isSameAsMain = ($attribute=='phone_number' ? false : $phone_number == $this->phone_number); |
| 167 | 167 | } |
| 168 | 168 | // TODO |
@@ -172,7 +172,7 @@ discard block |
||
| 172 | 172 | |
| 173 | 173 | if($isValidFormat && !$isSameAsMain && !$isDuplicate){ |
| 174 | 174 | return true; |
| 175 | - }else{ |
|
| 175 | + } else{ |
|
| 176 | 176 | $reason = ''; |
| 177 | 177 | if(!$isValidFormat){ |
| 178 | 178 | $reason = Yii::t('app','Invalid phone number format'); |