|
- <?php
-
-
-
- namespace backend\controllers;
-
- use common\models\User ;
- use common\models\Producer ;
- use backend\models\MailForm ;
-
-
- class UserController extends BackendController
- {
-
- public function behaviors()
- {
- return [
- 'verbs' => [
- 'class' => VerbFilter::className(),
- 'actions' => [
- ],
- ],
- 'access' => [
- 'class' => AccessControl::className(),
- 'rules' => [
- [
- 'allow' => true,
- 'roles' => ['@'],
- 'matchCallback' => function ($rule, $action) {
- return User::hasAccessBackend();
- }
- ]
- ],
- ],
- ];
- }
-
-
-
- public function actionIndex(
- $idPointSale = 0, $sectionInactiveUsers = false)
- {
- $searchModel = new UserSearch ;
- $dataProvider = $searchModel->search([
- 'UserSearch' => array_merge(
- [
- 'id_point_sale' => $idPointSale,
- 'inactive' => (int) $sectionInactiveUsers
- ],
- isset(Yii::$app->request->queryParams['UserSearch']) ?
- Yii::$app->request->queryParams['UserSearch'] :
- []
- )
- ]) ;
-
- $producer = Producer::searchOne([
- 'id' => Producer::getId()
- ]) ;
-
- $pointsSaleArray = PointSale::searchAll() ;
-
- return $this->render('index', [
- 'searchModel' => $searchModel,
- 'dataProvider' => $dataProvider,
- 'producer' => $producer,
- 'idPointSaleActive' => $idPointSale,
- 'pointsSaleArray' => $pointsSaleArray,
- 'sectionInactiveUsers' => $sectionInactiveUsers,
- ]);
- }
-
-
-
- public function actionCreate()
- {
- $model = new User();
-
- if ($model->load(Yii::$app->request->post()) && $model->validate() && YII_ENV != 'demo') {
-
-
- $password = Password::generate();
- $model->setPassword($password);
- $model->generateAuthKey();
- $model->username = $model->email;
- if (!strlen($model->email)) {
- $model->username = 'inconnu@laboiteapain.net';
- }
-
- $model->save();
-
-
- $useProducer = new UserProducer();
- $useProducer->id_user = $model->id;
- $useProducer->id_producer = Producer::getId();
- $useProducer->credit = 0;
- $useProducer->active = 1;
- $useProducer->save();
-
- $model->sendMailWelcome($password) ;
-
- return $this->redirect(['index']);
- } else {
- return $this->render('create', [
- 'model' => $model,
- ]);
- }
- }
-
-
-
- public function actionUpdate($id)
- {
- $model = $this->findModel($id);
-
- $previousMail = $model->email ;
-
- $user = User::find()->with('userProducer')->where(['id' => $model['id']])->one();
- $userBelongToProducer = UserProducer::findOne(['id_user' => $id, 'id_producer' => Producer::getId()]);
- if (($userBelongToProducer && count($user->userProducer) == 1) || User::getCurrentStatus() == USER::STATUS_ADMIN) {
- if ($model->load(Yii::$app->request->post()) && $model->save()) {
-
-
- if(!strlen($previousMail) && strlen($model->email)) {
- $password = Password::generate();
- $model->setPassword($password);
- $model->username = $model->email;
- $model->sendMailWelcome($password) ;
- }
-
- return $this->redirect(['index']);
- } else {
- return $this->render('update', [
- 'model' => $model,
- ]);
- }
- } else {
- throw new UserException("Vous ne pouvez pas modifier cet utilisateur, soit parce qu'il appartient à plusieurs établissements, soit parce qu'il n'est pas lié au votre.");
- }
- }
-
-
-
- public function actionDelete($id)
- {
- $userProducer = UserProducer::findOne([
- 'id_user' => $id,
- 'id_producer' => Producer::getId()
- ]) ;
- if($userProducer) {
- $userProducer->active = 0 ;
- $userProducer->bookmark = 0 ;
- $userProducer->save() ;
- }
- else {
- throw new \yii\web\NotFoundHttpException('L\'enregistrement UserProducer est introuvable', 404) ;
- }
-
- $params = Yii::$app->getRequest()->getQueryParams() ;
- unset($params['id']) ;
-
- $this->redirect(array_merge(['index'],$params));
- }
-
-
-
- public function actionMail($idPointSale = 0)
- {
- $users = User::findBy([
- 'id_producer' => Producer::getId(),
- 'id_point_sale' => $idPointSale
- ])->all() ;
-
- $usersArray = [];
- foreach ($users as $user) {
- if (isset($user['email']) && strlen($user['email']))
- $usersArray[] = $user['email'];
- }
-
- $pointsSaleArray = PointSale::find()->where(['id_producer' => Producer::getId()])->all() ;
-
- $pointSale = null ;
- if($idPointSale) {
- $pointSale = PointSale::findOne(['id' => $idPointSale]) ;
- }
-
- $mailForm = new MailForm() ;
- if ($mailForm->load(Yii::$app->request->post()) && $mailForm->validate()) {
- $resultSendEmail = $mailForm->sendEmail($users) ;
- if($resultSendEmail) {
- Yii::$app->getSession()->setFlash('success', 'Votre email a bien été envoyé.');
- }
- else {
- Yii::$app->getSession()->setFlash('error', 'Un problème est survenu lors de l\'envoi de votre email.');
- }
-
- return $this->redirect(['mail','idPointSale' => $idPointSale]);
- }
-
- return $this->render('emails', [
- 'usersArray' => $usersArray,
- 'pointsSaleArray' => $pointsSaleArray,
- 'pointSale' => $pointSale,
- 'mailForm' => $mailForm,
- ]);
- }
-
-
-
- public function actionCredit($id)
- {
- $user = User::find()->with('userProducer')->where(['id' => $id])->one();
- $userProducer = UserProducer::findOne(['id_user' => $id, 'id_producer' => Producer::getId()]);
-
- if (($userProducer) || User::getCurrentStatus() == User::STATUS_ADMIN) {
-
- $creditForm = new CreditForm;
- if ($creditForm->load(Yii::$app->request->post()) && $creditForm->validate()) {
- $creditForm->id_user = $id ;
- $creditForm->save();
- $creditForm = new CreditForm;
- }
-
- $history = CreditHistory::find()
- ->with(['order', 'userAction'])
- ->where([
- 'id_user' => $user->id,
- 'id_producer' => Producer::getId(),
- ])
- ->orderBy('date DESC')
- ->all();
-
- return $this->render('credit', [
- 'user' => $user,
- 'creditForm' => $creditForm,
- 'history' => $history
- ]);
- }
- else {
- throw new UserException("Vous ne pouvez pas créditer un utilisateur qui n'est pas associé à votre boulangerie.");
- }
- }
-
-
-
- public function actionOrders($id)
- {
- $user = User::findOne($id);
-
- $ordersArray = Order::searchAll([
- 'id_user' => $id
- ], ['orderby' => 'distribution.date DESC']) ;
-
- return $this->render('orders', [
- 'ordersArray' => $ordersArray,
- 'user' => $user
- ]);
- }
-
-
-
- protected function findModel($id)
- {
- if (($model = User::findOne($id)) !== null) {
- return $model;
- } else {
- throw new NotFoundHttpException('The requested page does not exist.');
- }
- }
-
- }
|