|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499 |
- <?php
-
-
-
- namespace backend\controllers;
-
- use common\helpers\GlobalParam;
- use common\models\User;
- use common\models\Producer;
- use common\models\Distribution;
- use backend\models\MailForm;
- use common\models\UserProducer;
- use common\models\UserPointSale;
- use common\models\PointSale;
-
-
- 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, $sectionSubscribers = false, $sectionInactiveUsers = false)
- {
- $searchModel = new UserSearch;
- $dataProvider = $searchModel->search([
- 'UserSearch' => array_merge(
- [
- 'id_point_sale' => $idPointSale,
- 'inactive' => (int)$sectionInactiveUsers,
- 'subscribers' => (int)$sectionSubscribers
- ],
- isset(Yii::$app->request->queryParams['UserSearch']) ?
- Yii::$app->request->queryParams['UserSearch'] :
- []
- )
- ]);
-
- $producer = Producer::searchOne([
- 'id' => GlobalParam::getCurrentProducerId()
- ]);
-
- $pointsSaleArray = PointSale::searchAll();
-
- return $this->render('index', [
- 'searchModel' => $searchModel,
- 'dataProvider' => $dataProvider,
- 'producer' => $producer,
- 'idPointSaleActive' => $idPointSale,
- 'pointsSaleArray' => $pointsSaleArray,
- 'sectionInactiveUsers' => $sectionInactiveUsers,
- 'sectionSubscribers' => $sectionSubscribers,
- ]);
- }
-
- public function initForm($model)
- {
-
- if ($model->id) {
- $userPointSaleArray = UserPointSale::searchAll([
- 'id_user' => $model->id
- ]);
- if ($userPointSaleArray && count($userPointSaleArray) > 0) {
- foreach ($userPointSaleArray as $userPointSaleArray) {
- $model->points_sale[] = $userPointSaleArray->id_point_sale;
- }
- }
- }
-
-
- $pointsSaleArray = PointSale::find()
- ->where([
- 'id_producer' => GlobalParam::getCurrentProducerId(),
- ])
- ->joinWith(['userPointSale' => function ($query) use ($model) {
- if ($model->id) {
- $query->andOnCondition('user_point_sale.id_user = ' . $model->id);
- }
- }])
- ->all();
-
- return [
- 'pointsSaleArray' => $pointsSaleArray
- ];
- }
-
-
-
- public function actionCreate()
- {
- $model = new User();
-
- $userExist = false ;
- $posts = Yii::$app->request->post() ;
-
- if($posts && isset($posts['User']['email']) && strlen($posts['User']['email']) > 0) {
- $userExist = User::searchOne([
- 'email' => $posts['User']['email']
- ]);
- }
-
- if ($userExist) {
- Producer::addUser($userExist->id, GlobalParam::getCurrentProducerId()) ;
- $this->processLinkPointSale($userExist);
- Yii::$app->getSession()->setFlash('success', "L'utilisateur que vous souhaitez créer possède déjà un compte sur la plateforme. Il vient d'être lié à votre établissement.");
- }
- else {
- if ($model->load(Yii::$app->request->post()) && $model->validate() && YII_ENV != 'demo') {
-
- $password = Password::generate();
- $model->id_producer = 0;
- $model->setPassword($password);
- $model->generateAuthKey();
- $model->username = $model->email;
- if (!strlen($model->email)) {
- $model->username = 'inconnu@opendistrib.net';
- }
-
- $model->save();
-
-
- $useProducer = new UserProducer();
- $useProducer->id_user = $model->id;
- $useProducer->id_producer = GlobalParam::getCurrentProducerId();
- $useProducer->credit = 0;
- $useProducer->active = 1;
- $useProducer->save();
-
- $model->sendMailWelcome($password);
- $this->processLinkPointSale($model);
-
- Yii::$app->getSession()->setFlash('success', 'Utilisateur créé.');
- $model = new User();
- }
- }
-
- return $this->render('create', array_merge($this->initForm($model), [
- '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' => GlobalParam::getCurrentProducerId()]);
- if ($userBelongToProducer) {
- 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);
- }
- $this->processLinkPointSale($model);
- Yii::$app->getSession()->setFlash('success', 'Utilisateur modifié.');
- }
- } else {
- throw new UserException("Vous ne pouvez pas modifier cet utilisateur.");
- }
-
-
- $newPassword = Yii::$app->request->post('submit_new_password');
- if ($newPassword) {
- $password = Password::generate();
- $model->setPassword($password);
- $model->save();
-
- $producer = GlobalParam::getCurrentProducer();
- Yii::$app->mailer->compose();
- $mail = Yii::$app->mailer->compose(
- ['html' => 'newPasswordUserAdmin-html', 'text' => 'newPasswordUserAdmin-text'], ['user' => $model, 'producer' => $producer, 'password' => $password]
- )
- ->setTo($model->email)
- ->setFrom(['contact@opendistrib.net' => 'Opendistrib'])
- ->setSubject('[Opendistrib] Nouveau mot de passe')
- ->send();
- Yii::$app->getSession()->setFlash('success', 'Nouveau mot de passe envoyé.');
- }
-
- return $this->render('update', array_merge($this->initForm($model), [
- 'model' => $model,
- ]));
- }
-
-
-
- public function processLinkPointSale($modelUser)
- {
- $posts = Yii::$app->request->post();
- UserPointSale::deleteAll([
- 'id_user' => $modelUser->id
- ]);
- if (is_array($modelUser->points_sale) && count($modelUser->points_sale) > 0) {
- foreach ($modelUser->points_sale as $pointSaleId) {
- $userPointSale = UserPointSale::searchOne([
- 'id_user' => $modelUser->id,
- 'id_point_sale' => $pointSaleId
- ]);
- if (!$userPointSale) {
- $userPointSale = new UserPointSale;
- $userPointSale->id_user = $modelUser->id;
- $userPointSale->id_point_sale = $pointSaleId;
- $userPointSale->comment = isset($posts['User']['comment_point_sale_' . $pointSaleId]) ? $posts['User']['comment_point_sale_' . $pointSaleId] : '';
- $userPointSale->save();
- }
- }
- }
- }
-
-
-
- public function actionDelete($id)
- {
- $userProducer = UserProducer::findOne([
- 'id_user' => $id,
- 'id_producer' => GlobalParam::getCurrentProducerId()
- ]);
- if ($userProducer) {
- $userProducer->active = 0;
- $userProducer->bookmark = 0;
- $userProducer->save();
- Yii::$app->getSession()->setFlash('success', 'L\'utilisateur a bien été supprimé de votre établissement.');
- } 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,
- $sectionSubscribers = 0,
- $sectionInactiveUsers = 0,
- $usersPointSaleLink = 0,
- $usersPointSaleHasOrder = 0)
- {
- if ($idPointSale && !$usersPointSaleLink && !$usersPointSaleHasOrder) {
- $usersPointSaleLink = 1;
- }
-
- $users = User::findBy([
- 'id_producer' => GlobalParam::getCurrentProducerId(),
- 'id_point_sale' => $idPointSale,
- 'users_point_sale_link' => $usersPointSaleLink,
- 'users_point_sale_has_order' => $usersPointSaleHasOrder,
- 'subscribers' => $sectionSubscribers,
- 'inactive' => $sectionInactiveUsers,
- ])->all();
-
- $usersArray = [];
- foreach ($users as $user) {
- if (isset($user['email']) && strlen($user['email']))
- $usersArray[] = $user['email'];
- }
-
- $pointsSaleArray = PointSale::find()->where(['id_producer' => GlobalParam::getCurrentProducerId()])->all();
-
- $pointSale = null;
- if ($idPointSale) {
- $pointSale = PointSale::findOne(['id' => $idPointSale]);
- }
-
- $mailForm = new MailForm();
- if ($mailForm->load(Yii::$app->request->post()) && $mailForm->validate()) {
- $responseSendMail = $mailForm->sendEmail($users);
- if ($responseSendMail->success()) {
- Yii::$app->getSession()->setFlash('success', 'Votre email a bien été envoyé.');
- } else {
- $bodyResponseSendMail = $responseSendMail->getBody();
- $emailsErrorArray = [];
- foreach ($bodyResponseSendMail['Messages'] as $message) {
- if ($message['Status'] != 'success') {
- $emailsErrorArray[] = $message['To']['Email'];
- }
- }
- $messageError = 'Un problème est survenu lors de l\'envoi de votre email.';
- if (count($emailsErrorArray) > 0) {
- $messageError .= '<br />Problème détecté sur les adresses suivantes : ' . implode(',', $emailsErrorArray);
- }
- Yii::$app->getSession()->setFlash('error', $messageError);
- }
-
- return $this->redirect(['mail', 'idPointSale' => $idPointSale]);
- }
-
- $incomingDistributions = Distribution::getIncomingDistributions();
- $incomingDistributionsArray = ['0' => '--'];
- foreach ($incomingDistributions as $distribution) {
- $incomingDistributionsArray[$distribution->id] = strftime('%A %d %B %Y', strtotime($distribution->date));
- }
-
- return $this->render('emails', [
- 'usersArray' => $usersArray,
- 'pointsSaleArray' => $pointsSaleArray,
- 'pointSale' => $pointSale,
- 'mailForm' => $mailForm,
- 'idPointSaleActive' => $idPointSale,
- 'incomingDistributionsArray' => $incomingDistributionsArray,
- 'sectionSubscribers' => $sectionSubscribers,
- 'sectionInactiveUsers' => $sectionInactiveUsers,
- 'usersPointSaleLink' => $usersPointSaleLink,
- 'usersPointSaleHasOrder' => $usersPointSaleHasOrder,
- ]);
- }
-
-
-
- public function actionCredit($id)
- {
- $user = User::find()->with('userProducer')->where(['id' => $id])->one();
- $userProducer = UserProducer::findOne(['id_user' => $id, 'id_producer' => GlobalParam::getCurrentProducerId()]);
-
- 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' => GlobalParam::getCurrentProducerId(),
- ])
- ->orderBy('date DESC')
- ->all();
-
- return $this->render('credit', [
- 'user' => $user,
- 'userProducer' => $userProducer,
- 'creditForm' => $creditForm,
- 'history' => $history
- ]);
- } else {
- throw new UserException("Vous ne pouvez pas créditer un utilisateur qui n'est pas associé à votre établissement.");
- }
- }
-
-
-
- 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
- ]);
- }
-
-
-
- public function actionStateCredit($idUser, $state)
- {
- $userProducer = UserProducer::searchOne([
- 'id_user' => $idUser
- ]);
-
- if ($userProducer) {
- $userProducer->credit_active = $state;
- $userProducer->save();
- }
-
- return $this->redirect(['user/credit', 'id' => $idUser]);
- }
-
-
-
- protected function findModel($id)
- {
- if (($model = User::findOne($id)) !== null) {
- return $model;
- } else {
- throw new NotFoundHttpException('The requested page does not exist.');
- }
- }
-
- }
|