Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

8 лет назад
8 лет назад
8 лет назад
8 лет назад
8 лет назад
8 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. public function getUserEtablissement() {
  68. return $this->hasMany(UserEtablissement::className(), ['id_user'=>'id']) ;
  69. }
  70. /**
  71. * @inheritdoc
  72. */
  73. public static function findIdentity($id)
  74. {
  75. return static::findOne(['id' => $id/*, 'status' => self::STATUS_ACTIVE*/]);
  76. }
  77. /**
  78. * @inheritdoc
  79. */
  80. public static function findIdentityByAccessToken($token, $type = null)
  81. {
  82. throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.');
  83. }
  84. /**
  85. * Finds user by username
  86. *
  87. * @param string $username
  88. * @return static|null
  89. */
  90. public static function findByUsername($username)
  91. {
  92. return static::findOne(['username' => $username/*, 'status' => self::STATUS_ACTIVE*/]);
  93. }
  94. public static function findByEmail($email)
  95. {
  96. return static::findOne(['email' => $email /*, 'status' => self::STATUS_ACTIVE*/]);
  97. }
  98. /**
  99. * Finds user by password reset token
  100. *
  101. * @param string $token password reset token
  102. * @return static|null
  103. */
  104. public static function findByPasswordResetToken($token)
  105. {
  106. if (!static::isPasswordResetTokenValid($token)) {
  107. return null;
  108. }
  109. return static::findOne([
  110. 'password_reset_token' => $token,
  111. ]);
  112. }
  113. /**
  114. * Finds out if password reset token is valid
  115. *
  116. * @param string $token password reset token
  117. * @return boolean
  118. */
  119. public static function isPasswordResetTokenValid($token)
  120. {
  121. if (empty($token)) {
  122. return false;
  123. }
  124. $expire = Yii::$app->params['user.passwordResetTokenExpire'];
  125. $parts = explode('_', $token);
  126. $timestamp = (int) end($parts);
  127. return $timestamp + $expire >= time();
  128. }
  129. /**
  130. * @inheritdoc
  131. */
  132. public function getId()
  133. {
  134. return $this->getPrimaryKey();
  135. }
  136. /**
  137. * @inheritdoc
  138. */
  139. public function getAuthKey()
  140. {
  141. return $this->auth_key;
  142. }
  143. /**
  144. * @inheritdoc
  145. */
  146. public function validateAuthKey($authKey)
  147. {
  148. return $this->getAuthKey() === $authKey;
  149. }
  150. /**
  151. * Validates password
  152. *
  153. * @param string $password password to validate
  154. * @return boolean if password provided is valid for current user
  155. */
  156. public function validatePassword($password)
  157. {
  158. return Yii::$app->security->validatePassword($password, $this->password_hash);
  159. }
  160. /**
  161. * Generates password hash from password and sets it to the model
  162. *
  163. * @param string $password
  164. */
  165. public function setPassword($password)
  166. {
  167. $this->password_hash = Yii::$app->security->generatePasswordHash($password);
  168. }
  169. /**
  170. * Generates "remember me" authentication key
  171. */
  172. public function generateAuthKey()
  173. {
  174. $this->auth_key = Yii::$app->security->generateRandomString();
  175. }
  176. /**
  177. * Generates new password reset token
  178. */
  179. public function generatePasswordResetToken()
  180. {
  181. $this->password_reset_token = Yii::$app->security->generateRandomString() . '_' . time();
  182. }
  183. /**
  184. * Removes password reset token
  185. */
  186. public function removePasswordResetToken()
  187. {
  188. $this->password_reset_token = null;
  189. }
  190. public function attributeLabels()
  191. {
  192. return [
  193. 'id' => 'ID',
  194. 'username' => 'Identifiant',
  195. 'password' => 'Mot de passe',
  196. 'rememberMe' => 'Se souvenir de moi',
  197. 'confiance' => 'De confiance',
  198. 'no_mail' => 'Ne pas recevoir d\'email de la part du Chat des Noisettes',
  199. 'mail_prod_lundi' => 'Lundi',
  200. 'mail_prod_mardi' => 'Mardi',
  201. 'mail_prod_mercredi' => 'Mercredi',
  202. 'mail_prod_jeudi' => 'Jeudi',
  203. 'mail_prod_vendredi' => 'Vendredi',
  204. 'mail_prod_samedi' => 'Samedi',
  205. 'mail_prod_dimanche' => 'Dimanche',
  206. ];
  207. }
  208. public function isBoulanger()
  209. {
  210. return ($this->status == User::STATUS_ADMIN || $this->status == User::STATUS_BOULANGER) && $this->id_etablissement ;
  211. }
  212. public function getNomMagasin()
  213. {
  214. $etablissement = Etablissement::findOne($this->id_etablissement) ;
  215. return $etablissement->nom ;
  216. }
  217. public function getEtablissementsFavoris()
  218. {
  219. $etabs = (new \yii\db\Query())
  220. ->select('*')
  221. ->from(['user_etablissement', 'etablissement'])
  222. ->where('user_etablissement.id_etablissement = etablissement.id')
  223. ->andWhere(['user_etablissement.id_user' => $this->id])
  224. ->andWhere(['user_etablissement.actif' => 1])
  225. ->all();
  226. return $etabs ;
  227. }
  228. public function etatPaiementEtablissement()
  229. {
  230. $etablissement = Etablissement::findOne($this->id_etablissement) ;
  231. if($etablissement)
  232. {
  233. return $etablissement->etatPaiement() ;
  234. }
  235. }
  236. public function periodeEssai()
  237. {
  238. $etablissement = Etablissement::findOne($this->id_etablissement) ;
  239. if($etablissement)
  240. {
  241. $date_limite = strtotime($etablissement->date_creation) + 30*24*60*60 ;
  242. $date = time() ;
  243. if($date < $date_limite)
  244. {
  245. $date = $date_limite - $date ;
  246. return (int) ($date / (24*60*60)) ;
  247. }
  248. else {
  249. return 0 ;
  250. }
  251. }
  252. }
  253. public function getCredit($id_etablissement)
  254. {
  255. $user_etablissement = UserEtablissement::find()
  256. ->where([
  257. 'id_user' => $this->id,
  258. 'id_etablissement' => $id_etablissement
  259. ])
  260. ->one() ;
  261. if($user_etablissement)
  262. {
  263. return $user_etablissement->credit ;
  264. }
  265. return 0 ;
  266. }
  267. }