From 74a77718be69c17d043ad4001d59ce8fd1dbfd2c Mon Sep 17 00:00:00 2001
From: Damien Burel <damien.burel@maarch.org>
Date: Wed, 11 Oct 2017 12:36:49 +0200
Subject: [PATCH] FEAT #6052 [develop] Baskets redirection

---
 .../groups-administration.component.js        |  1 -
 .../js/angular/app/profile.component.js       |  1 +
 .../js/angular/app/profile.component.ts       |  1 +
 core/Controllers/UserController.php           | 18 ++++++++--
 .../basket/Models/BasketsModelAbstract.php    | 36 +++++++++++++++++++
 modules/basket/display_basket_list.php        |  1 +
 rest/index.php                                |  4 ++-
 7 files changed, 58 insertions(+), 4 deletions(-)

diff --git a/apps/maarch_entreprise/js/angular/app/administration/groups-administration.component.js b/apps/maarch_entreprise/js/angular/app/administration/groups-administration.component.js
index 8a488836082..766bf7b17aa 100644
--- a/apps/maarch_entreprise/js/angular/app/administration/groups-administration.component.js
+++ b/apps/maarch_entreprise/js/angular/app/administration/groups-administration.component.js
@@ -56,7 +56,6 @@ var GroupsAdministrationComponent = (function () {
                 });
             }
         }
-        console.log(this.groupsForAssign);
     };
     GroupsAdministrationComponent.prototype.reassignUsers = function (group, groupId) {
         var _this = this;
diff --git a/apps/maarch_entreprise/js/angular/app/profile.component.js b/apps/maarch_entreprise/js/angular/app/profile.component.js
index a23a4590305..b6883115697 100755
--- a/apps/maarch_entreprise/js/angular/app/profile.component.js
+++ b/apps/maarch_entreprise/js/angular/app/profile.component.js
@@ -40,6 +40,7 @@ var ProfileComponent = (function () {
             title: "",
         };
         this.userAbsenceModel = [];
+        this.basketsToRedirect = [];
         this.showPassword = false;
         this.selectedSignature = -1;
         this.selectedSignatureLabel = "";
diff --git a/apps/maarch_entreprise/js/angular/app/profile.component.ts b/apps/maarch_entreprise/js/angular/app/profile.component.ts
index a6240c5d9c5..c475cd84fc0 100755
--- a/apps/maarch_entreprise/js/angular/app/profile.component.ts
+++ b/apps/maarch_entreprise/js/angular/app/profile.component.ts
@@ -41,6 +41,7 @@ export class ProfileComponent implements OnInit {
         title                   : "",
     };
     userAbsenceModel            : any[]     = [];
+    basketsToRedirect           : string[]  = [];
 
     showPassword                : boolean   = false;
     selectedSignature           : number    = -1;
diff --git a/core/Controllers/UserController.php b/core/Controllers/UserController.php
index 8ff46c8411d..25b7f955362 100755
--- a/core/Controllers/UserController.php
+++ b/core/Controllers/UserController.php
@@ -42,7 +42,8 @@ class UserController
         $user['groups'] = UserModel::getGroupsByUserId(['userId' => $_SESSION['user']['UserId']]);
         $user['entities'] = UserModel::getEntitiesById(['userId' => $_SESSION['user']['UserId']]);
         $user['baskets'] = BasketsModel::getBasketsByUserId(['userId' => $_SESSION['user']['UserId']]);
-        
+        $user['redirectedBaskets'] = BasketsModel::getBasketsRedirectedByUserId(['userId' => $_SESSION['user']['UserId']]);
+
         return $response->withJson($user);
     }
 
@@ -214,7 +215,7 @@ class UserController
         return $response->withJson(['success' => _UPDATED_PASSWORD]);
     }
 
-    public function setBasketsRedirectionForAbsence(RequestInterface $request, ResponseInterface $response, $aArgs) {
+    public function setRedirectedBaskets(RequestInterface $request, ResponseInterface $response, $aArgs) {
         $error = $this->hasUsersRights(['id' => $aArgs['id'], 'himself' => true]);
         if (!empty($error['error'])) {
             return $response->withStatus($error['status'])->withJson(['errors' => $error['error']]);
@@ -254,6 +255,19 @@ class UserController
         ]);
     }
 
+    public function deleteRedirectedBaskets(RequestInterface $request, ResponseInterface $response, $aArgs) {
+        $error = $this->hasUsersRights(['id' => $aArgs['id'], 'himself' => true]);
+        if (!empty($error['error'])) {
+            return $response->withStatus($error['status'])->withJson(['errors' => $error['error']]);
+        }
+
+        $user = UserModel::getById(['id' => $aArgs['id'], 'select' => ['user_id']]);
+
+        BasketsModel::deleteBasketRedirection(['userId' => $user['user_id'], 'basketId' => $aArgs['basketId']]);
+
+        return $response->withJson(['redirectedBaskets' => BasketsModel::getRedirectedBasketsByUserId(['userId' => $user['user_id']])]);
+    }
+
     public function updateStatus(RequestInterface $request, ResponseInterface $response, $aArgs) {
         $error = $this->hasUsersRights(['id' => $aArgs['id'], 'himself' => true]);
         if (!empty($error['error'])) {
diff --git a/modules/basket/Models/BasketsModelAbstract.php b/modules/basket/Models/BasketsModelAbstract.php
index 7d1f2edc4d1..59098a19ff1 100755
--- a/modules/basket/Models/BasketsModelAbstract.php
+++ b/modules/basket/Models/BasketsModelAbstract.php
@@ -168,4 +168,40 @@ class BasketsModelAbstract
 
         return true;
     }
+
+    public static function deleteBasketRedirection(array $aArgs)
+    {
+        ValidatorModel::notEmpty($aArgs, ['userId', 'basketId']);
+        ValidatorModel::stringType($aArgs, ['userId', 'basketId']);
+
+        DatabaseModel::delete([
+            'table' => 'user_abs',
+            'where' => ['(user_abs = ? OR basket_owner = ?)', 'basket_id = ?'],
+            'data'  => [$aArgs['userId'], $aArgs['userId'], $aArgs['basketId']]
+        ]);
+
+        return true;
+    }
+
+    public static function getRedirectedBasketsByUserId(array $aArgs) {
+        ValidatorModel::notEmpty($aArgs, ['userId']);
+        ValidatorModel::stringType($aArgs, ['userId']);
+
+        $aBaskets = DatabaseModel::select([
+            'select' => ['ba.basket_id', 'ba.basket_name', 'ua.new_user', 'ua.basket_owner'],
+            'table' => ['baskets ba, user_abs ua'],
+            'where' => ['ua.user_abs = ?', 'ua.basket_id = ba.basket_id'],
+            'data' => [$aArgs['userId']],
+            'order_by' => 'ua.system_id'
+        ]);
+
+        foreach ($aBaskets as $key => $value) {
+            $user = UserModel::getById(['userId' => $value['new_user'], 'select' => ['firstname', 'lastname']]);
+            $aBaskets[$key]['userToDisplay'] = "{$user['firstname']} {$user['lastname']} ({$value['new_user']})";
+            $aBaskets[$key]['user'] = "{$user['firstname']} {$user['lastname']}";
+        }
+
+        return $aBaskets;
+    }
+
 }
\ No newline at end of file
diff --git a/modules/basket/display_basket_list.php b/modules/basket/display_basket_list.php
index 54b3487193c..06dad9cc627 100755
--- a/modules/basket/display_basket_list.php
+++ b/modules/basket/display_basket_list.php
@@ -75,6 +75,7 @@ if ($core_tools->test_service('display_basket_list','basket', false)) {
                 <h2><?php echo _MY_BASKETS;?> : </h2>
             
             <?php
+            $redirectedBaskets = \Baskets\Models\BasketsModel::getRedirectedBasketsByUserId(['userId' => $_SESSION['user']['UserId']]);
             $countColl = count($collWithUserBaskets);
             $currentGroup = '';
             for ($cpt=0;$cpt<$countColl;$cpt++) {
diff --git a/rest/index.php b/rest/index.php
index 85089f5e5dc..b8bf83a3586 100755
--- a/rest/index.php
+++ b/rest/index.php
@@ -189,7 +189,9 @@ $app->put('/users/{id}/status', \Core\Controllers\UserController::class . ':upda
 $app->post('/users/{id}/signatures', \Core\Controllers\UserController::class . ':addSignature');
 $app->put('/users/{id}/signatures/{signatureId}', \Core\Controllers\UserController::class . ':updateSignature');
 $app->delete('/users/{id}/signatures/{signatureId}', \Core\Controllers\UserController::class . ':deleteSignature');
-$app->post('/users/{id}/baskets/absence', \Core\Controllers\UserController::class . ':setBasketsRedirectionForAbsence');
+$app->post('/users/{id}/baskets/absence', \Core\Controllers\UserController::class . ':setRedirectedBaskets'); //TODO penser à une meilleure route
+$app->delete('/users/{id}/baskets/{basketId}/absence', \Core\Controllers\UserController::class . ':deleteRedirectedBaskets'); //TODO penser à une meilleure route
+
 
 //CurrentUser
 $app->put('/currentUser/password', \Core\Controllers\UserController::class . ':updateCurrentUserPassword');
-- 
GitLab