Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

306 rindas
8.1KB

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