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.

преди 8 години
преди 8 години
преди 8 години
преди 8 години
преди 8 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. 'status' => self::STATUS_ACTIVE,
  109. ]);
  110. }
  111. /**
  112. * Finds out if password reset token is valid
  113. *
  114. * @param string $token password reset token
  115. * @return boolean
  116. */
  117. public static function isPasswordResetTokenValid($token)
  118. {
  119. if (empty($token)) {
  120. return false;
  121. }
  122. $expire = Yii::$app->params['user.passwordResetTokenExpire'];
  123. $parts = explode('_', $token);
  124. $timestamp = (int) end($parts);
  125. return $timestamp + $expire >= time();
  126. }
  127. /**
  128. * @inheritdoc
  129. */
  130. public function getId()
  131. {
  132. return $this->getPrimaryKey();
  133. }
  134. /**
  135. * @inheritdoc
  136. */
  137. public function getAuthKey()
  138. {
  139. return $this->auth_key;
  140. }
  141. /**
  142. * @inheritdoc
  143. */
  144. public function validateAuthKey($authKey)
  145. {
  146. return $this->getAuthKey() === $authKey;
  147. }
  148. /**
  149. * Validates password
  150. *
  151. * @param string $password password to validate
  152. * @return boolean if password provided is valid for current user
  153. */
  154. public function validatePassword($password)
  155. {
  156. return Yii::$app->security->validatePassword($password, $this->password_hash);
  157. }
  158. /**
  159. * Generates password hash from password and sets it to the model
  160. *
  161. * @param string $password
  162. */
  163. public function setPassword($password)
  164. {
  165. $this->password_hash = Yii::$app->security->generatePasswordHash($password);
  166. }
  167. /**
  168. * Generates "remember me" authentication key
  169. */
  170. public function generateAuthKey()
  171. {
  172. $this->auth_key = Yii::$app->security->generateRandomString();
  173. }
  174. /**
  175. * Generates new password reset token
  176. */
  177. public function generatePasswordResetToken()
  178. {
  179. $this->password_reset_token = Yii::$app->security->generateRandomString() . '_' . time();
  180. }
  181. /**
  182. * Removes password reset token
  183. */
  184. public function removePasswordResetToken()
  185. {
  186. $this->password_reset_token = null;
  187. }
  188. public function attributeLabels()
  189. {
  190. return [
  191. 'id' => 'ID',
  192. 'username' => 'Identifiant',
  193. 'password' => 'Mot de passe',
  194. 'rememberMe' => 'Se souvenir de moi',
  195. 'confiance' => 'De confiance',
  196. 'no_mail' => 'Ne pas recevoir d\'email de la part du Chat des Noisettes',
  197. 'mail_prod_lundi' => 'Lundi',
  198. 'mail_prod_mardi' => 'Mardi',
  199. 'mail_prod_mercredi' => 'Mercredi',
  200. 'mail_prod_jeudi' => 'Jeudi',
  201. 'mail_prod_vendredi' => 'Vendredi',
  202. 'mail_prod_samedi' => 'Samedi',
  203. 'mail_prod_dimanche' => 'Dimanche',
  204. ];
  205. }
  206. public function isBoulanger()
  207. {
  208. return $this->id_etablissement ;
  209. }
  210. public function getNomMagasin()
  211. {
  212. $etablissement = Etablissement::findOne($this->id_etablissement) ;
  213. return $etablissement->nom ;
  214. }
  215. public function getEtablissementsFavoris()
  216. {
  217. $etabs = (new \yii\db\Query())
  218. ->select('*')
  219. ->from(['user_etablissement', 'etablissement'])
  220. ->where('user_etablissement.id_etablissement = etablissement.id')
  221. ->andWhere(['user_etablissement.id_user' => $this->id])
  222. ->all();
  223. $arr_etabs = array() ;
  224. foreach($etabs as $e)
  225. {
  226. $etablissement = Etablissement::findOne($e['id_etablissement']) ;
  227. if($etablissement->etatPaiement() == Etablissement::PAIEMENT_OK || $etablissement->etatPaiement() == Etablissement::PAIEMENT_ESSAI)
  228. {
  229. $arr_etabs[] = $e ;
  230. }
  231. }
  232. return $arr_etabs ;
  233. }
  234. public function etatPaiementEtablissement()
  235. {
  236. $etablissement = Etablissement::findOne($this->id_etablissement) ;
  237. if($etablissement)
  238. {
  239. return $etablissement->etatPaiement() ;
  240. }
  241. }
  242. public function periodeEssai()
  243. {
  244. $etablissement = Etablissement::findOne($this->id_etablissement) ;
  245. if($etablissement)
  246. {
  247. $date_limite = strtotime($etablissement->date_creation) + 30*24*60*60 ;
  248. $date = time() ;
  249. if($date < $date_limite)
  250. {
  251. $date = $date_limite - $date ;
  252. return (int) ($date / (24*60*60)) ;
  253. }
  254. else {
  255. return 0 ;
  256. }
  257. }
  258. }
  259. }