/** * Validation Rule * Add error if the SerialNumber is already linked with a person */ public function verifyUnicitySerialNumber($attribute, $params) { $verifIdentity = VerifIdentity::model()->findByPk($this->serialNumber); if ($verifIdentity != null) { if (!($verifIdentity->idUser === null)) { $this->addError('serialNumber', 'This serial Number is already used.'); } } }
public function actionDeleteCoin() { if (isset($_POST['serialNumber'])) { $verifIdentity = VerifIdentity::model()->findByPk($_POST['serialNumber']); //Verify that the person who deletes the coin is its owner if ($verifIdentity->idUser == Yii::app()->user->getId()) { //User can not delete coin if only one registered if (VerifIdentity::model()->count('idUser = :idUser', array(':idUser' => Yii::app()->user->getId())) > 1) { $verifIdentity->idUser = null; if ($verifIdentity->save()) { $user = User::model()->findByPk(Yii::app()->user->getId()); Yii::app()->user->setState('userLevel', $user->getLevel()); return; } } echo "You need at least one coin!"; return; } } echo "ERROR"; return; }
/** * Create a new user * @return 1 if success 0 else */ public function createUser() { $result = 1; $this->registrationDate = date('Y-m-d'); $this->validation = uniqid(); $transaction = $this->dbConnection->beginTransaction(); try { if ($this->save()) { //Get the idUser of the new User and specify it in the table verifIdentity $idUser = Yii::app()->db->getLastInsertId(); $verifIdentity = VerifIdentity::model()->findByPk($this->serialNumber); $verifIdentity->idUser = $idUser; if (!$verifIdentity->save()) { throw new CDbException(null); } //Upload of Profile Picture if ($this->profilePicture !== "default") { $result = $this->addPicture($this->profilePicture, $this->profilePictureExtension); if ($result !== 1) { $this->addError('pictureUploader', $result); throw new CException(null); } } $transaction->commit(); } else { $result = 0; } } catch (Exception $e) { if ($e !== null) { $this->addError('pictureUploader', "Problem during create process"); } $transaction->rollBack(); $result = 0; } return $result; }