You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

297 lines
7.8KB

  1. <?php
  2. namespace common\models;
  3. use Yii;
  4. use yii\base\NotSupportedException;
  5. use yii\behaviors\TimestampBehavior;
  6. use yii\db\ActiveRecord;
  7. use yii\db\CDbCriteria;
  8. use yii\web\IdentityInterface;
  9. /**
  10. * User model
  11. *
  12. * @property integer $id
  13. * @property string $username
  14. * @property string $password_hash
  15. * @property string $password_reset_token
  16. * @property string $email
  17. * @property string $auth_key
  18. * @property integer $status
  19. * @property integer $created_at
  20. * @property integer $updated_at
  21. * @property string $password write-only password
  22. * @property boolean $confiance
  23. */
  24. class User extends ActiveRecord implements IdentityInterface
  25. {
  26. const STATUS_DELETED = 0;
  27. const STATUS_ACTIVE = 10;
  28. const STATUS_BOULANGER = 11;
  29. const STATUS_ADMIN = 13;
  30. /**
  31. * @inheritdoc
  32. */
  33. public static function tableName()
  34. {
  35. return '{{%user}}';
  36. }
  37. /**
  38. * @inheritdoc
  39. */
  40. public function behaviors()
  41. {
  42. return [
  43. TimestampBehavior::className(),
  44. ];
  45. }
  46. /**
  47. * @inheritdoc
  48. */
  49. public function rules()
  50. {
  51. return [
  52. ['confiance','default','value'=>0],
  53. [['no_mail','mail_prod_lundi','mail_prod_mardi','mail_prod_mercredi','mail_prod_jeudi','mail_prod_vendredi','mail_prod_samedi','mail_prod_dimanche'],'boolean'],
  54. [['nom','prenom','telephone','adresse'], 'string'],
  55. [['nom','prenom','email'],'required','message'=> 'Ce champs ne peut être vide'],
  56. ['email','email','message'=> 'Cette adresse email n\'est pas valide'],
  57. ['email','verifyEmail'],
  58. ['status', 'default', 'value' => self::STATUS_ACTIVE],
  59. ['status', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_DELETED, self::STATUS_ADMIN,self::STATUS_BOULANGER ]],
  60. ];
  61. }
  62. public function verifyEmail($attribute,$params) {
  63. $user = User::find()->where("email LIKE :email AND id != :id")->params(array(':email'=>'%'.$this->email.'%', ':id'=>$this->id))->one() ;
  64. if($user)
  65. $this->addError($attribute, 'Cette adresse email est déjà utilisée par un autre utilisateur ');
  66. }
  67. /**
  68. * @inheritdoc
  69. */
  70. public static function findIdentity($id)
  71. {
  72. return static::findOne(['id' => $id/*, 'status' => self::STATUS_ACTIVE*/]);
  73. }
  74. /**
  75. * @inheritdoc
  76. */
  77. public static function findIdentityByAccessToken($token, $type = null)
  78. {
  79. throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.');
  80. }
  81. /**
  82. * Finds user by username
  83. *
  84. * @param string $username
  85. * @return static|null
  86. */
  87. public static function findByUsername($username)
  88. {
  89. return static::findOne(['username' => $username/*, 'status' => self::STATUS_ACTIVE*/]);
  90. }
  91. public static function findByEmail($email)
  92. {
  93. return static::findOne(['email' => $email /*, 'status' => self::STATUS_ACTIVE*/]);
  94. }
  95. /**
  96. * Finds user by password reset token
  97. *
  98. * @param string $token password reset token
  99. * @return static|null
  100. */
  101. public static function findByPasswordResetToken($token)
  102. {
  103. if (!static::isPasswordResetTokenValid($token)) {
  104. return null;
  105. }
  106. return static::findOne([
  107. 'password_reset_token' => $token,
  108. ]);
  109. }
  110. /**
  111. * Finds out if password reset token is valid
  112. *
  113. * @param string $token password reset token
  114. * @return boolean
  115. */
  116. public static function isPasswordResetTokenValid($token)
  117. {
  118. if (empty($token)) {
  119. return false;
  120. }
  121. $expire = Yii::$app->params['user.passwordResetTokenExpire'];
  122. $parts = explode('_', $token);
  123. $timestamp = (int) end($parts);
  124. return $timestamp + $expire >= time();
  125. }
  126. /**
  127. * @inheritdoc
  128. */
  129. public function getId()
  130. {
  131. return $this->getPrimaryKey();
  132. }
  133. /**
  134. * @inheritdoc
  135. */
  136. public function getAuthKey()
  137. {
  138. return $this->auth_key;
  139. }
  140. /**
  141. * @inheritdoc
  142. */
  143. public function validateAuthKey($authKey)
  144. {
  145. return $this->getAuthKey() === $authKey;
  146. }
  147. /**
  148. * Validates password
  149. *
  150. * @param string $password password to validate
  151. * @return boolean if password provided is valid for current user
  152. */
  153. public function validatePassword($password)
  154. {
  155. return Yii::$app->security->validatePassword($password, $this->password_hash);
  156. }
  157. /**
  158. * Generates password hash from password and sets it to the model
  159. *
  160. * @param string $password
  161. */
  162. public function setPassword($password)
  163. {
  164. $this->password_hash = Yii::$app->security->generatePasswordHash($password);
  165. }
  166. /**
  167. * Generates "remember me" authentication key
  168. */
  169. public function generateAuthKey()
  170. {
  171. $this->auth_key = Yii::$app->security->generateRandomString();
  172. }
  173. /**
  174. * Generates new password reset token
  175. */
  176. public function generatePasswordResetToken()
  177. {
  178. $this->password_reset_token = Yii::$app->security->generateRandomString() . '_' . time();
  179. }
  180. /**
  181. * Removes password reset token
  182. */
  183. public function removePasswordResetToken()
  184. {
  185. $this->password_reset_token = null;
  186. }
  187. public function attributeLabels()
  188. {
  189. return [
  190. 'id' => 'ID',
  191. 'username' => 'Identifiant',
  192. 'password' => 'Mot de passe',
  193. 'rememberMe' => 'Se souvenir de moi',
  194. 'confiance' => 'De confiance',
  195. 'no_mail' => 'Ne pas recevoir d\'email de la part du Chat des Noisettes',
  196. 'mail_prod_lundi' => 'Lundi',
  197. 'mail_prod_mardi' => 'Mardi',
  198. 'mail_prod_mercredi' => 'Mercredi',
  199. 'mail_prod_jeudi' => 'Jeudi',
  200. 'mail_prod_vendredi' => 'Vendredi',
  201. 'mail_prod_samedi' => 'Samedi',
  202. 'mail_prod_dimanche' => 'Dimanche',
  203. ];
  204. }
  205. public function isBoulanger()
  206. {
  207. return $this->id_etablissement ;
  208. }
  209. public function getNomMagasin()
  210. {
  211. $etablissement = Etablissement::findOne($this->id_etablissement) ;
  212. return $etablissement->nom ;
  213. }
  214. public function getEtablissementsFavoris()
  215. {
  216. $etabs = (new \yii\db\Query())
  217. ->select('*')
  218. ->from(['user_etablissement', 'etablissement'])
  219. ->where('user_etablissement.id_etablissement = etablissement.id')
  220. ->andWhere(['user_etablissement.id_user' => $this->id])
  221. ->all();
  222. $arr_etabs = array() ;
  223. foreach($etabs as $e)
  224. {
  225. $etablissement = Etablissement::findOne($e['id_etablissement']) ;
  226. if($etablissement->etatPaiement() == Etablissement::PAIEMENT_OK || $etablissement->etatPaiement() == Etablissement::PAIEMENT_ESSAI)
  227. {
  228. $arr_etabs[] = $e ;
  229. }
  230. }
  231. return $arr_etabs ;
  232. }
  233. public function etatPaiementEtablissement()
  234. {
  235. $etablissement = Etablissement::findOne($this->id_etablissement) ;
  236. if($etablissement)
  237. {
  238. return $etablissement->etatPaiement() ;
  239. }
  240. }
  241. public function periodeEssai()
  242. {
  243. $etablissement = Etablissement::findOne($this->id_etablissement) ;
  244. if($etablissement)
  245. {
  246. $date_limite = strtotime($etablissement->date_creation) + 30*24*60*60 ;
  247. $date = time() ;
  248. if($date < $date_limite)
  249. {
  250. $date = $date_limite - $date ;
  251. return (int) ($date / (24*60*60)) ;
  252. }
  253. else {
  254. return 0 ;
  255. }
  256. }
  257. }
  258. }