From fdb8a8b5352556d8aff19dd853d7ae876b0fa8a4 Mon Sep 17 00:00:00 2001
From: "florian.azizian" <florian.azizian@maarch.org>
Date: Thu, 20 Feb 2020 12:29:05 +0100
Subject: [PATCH] FEAT #12982 TIME 2:15 make action in signatureBook

---
 apps/maarch_entreprise/actions/setSession.php |  3 -
 apps/maarch_entreprise/js/angularFunctions.js | 48 +---------
 apps/maarch_entreprise/js/functions.js        | 12 +--
 modules/notes/js/functions.js                 |  2 +-
 package.json                                  |  2 +-
 src/frontend/app/process/process.component.ts |  1 -
 src/frontend/app/signature-book.component.ts  | 89 ++++++++++++-------
 7 files changed, 65 insertions(+), 92 deletions(-)
 delete mode 100755 apps/maarch_entreprise/actions/setSession.php

diff --git a/apps/maarch_entreprise/actions/setSession.php b/apps/maarch_entreprise/actions/setSession.php
deleted file mode 100755
index bbe64fc19d1..00000000000
--- a/apps/maarch_entreprise/actions/setSession.php
+++ /dev/null
@@ -1,3 +0,0 @@
-<?php
-
-$_SESSION['doc_id'] = $_REQUEST['resId'];
diff --git a/apps/maarch_entreprise/js/angularFunctions.js b/apps/maarch_entreprise/js/angularFunctions.js
index 4890936f45c..0dd0b1c96cc 100755
--- a/apps/maarch_entreprise/js/angularFunctions.js
+++ b/apps/maarch_entreprise/js/angularFunctions.js
@@ -67,41 +67,6 @@ function changeLocationToAngular(locationToGo) {
     location.href = locationToGo;
 }
 
-function lockDocument(resId) {
-    $j.ajax({
-        url: 'index.php?display=true&dir=actions&page=docLocker',
-        type: 'POST',
-        data: {
-            AJAX_CALL: true,
-            lock: true,
-            res_id: resId
-        },
-        success: function (result) {}
-    });
-}
-
-function intervalLockDocument(resId){
-    lockInterval = setInterval(function() {lockDocument(resId);}, 50000);
-}
-
-function unlockDocument(resId) {
-    $j.ajax({
-        url: 'index.php?display=true&dir=actions&page=docLocker',
-        type: 'POST',
-        data: {
-            AJAX_CALL: true,
-            unlock: true,
-            res_id: resId
-        },
-        success: function (result) {}
-    });
-}
-
-function intervalUnlockDocument(resId){
-    clearInterval(lockInterval);
-    unlockDocument(resId);
-}
-
 function islockForSignatureBook(resId, basketId, groupId) {
     $j.ajax({
         url: 'index.php?display=true&dir=actions&page=docLocker',
@@ -144,7 +109,7 @@ function setAttachmentInSignatureBook(id) {
         },
         success: function (answer) {
             if (typeof window.parent['angularSignatureBookComponent'] !== "undefined") {
-                window.parent.angularSignatureBookComponent.componentAfterAttach("left");
+                // window.parent.angularSignatureBookComponent.componentAfterAttach("left");
             }
         },
         error: function (err) {
@@ -153,17 +118,6 @@ function setAttachmentInSignatureBook(id) {
     });
 }
 
-function setSessionForSignatureBook(resId) {
-    $j.ajax({
-        url: 'index.php?display=true&dir=actions&page=setSession',
-        type: 'POST',
-        data: {
-            resId: resId
-        },
-        success: function (result) {}
-    });
-}
-
 function displayThumbnail(resId) {
     $j('#thumb_' + resId).html('<img src="../../rest/resources/' + resId + '/thumbnail">');
 }
diff --git a/apps/maarch_entreprise/js/functions.js b/apps/maarch_entreprise/js/functions.js
index d5424b1f6ff..fc4e1e3b179 100755
--- a/apps/maarch_entreprise/js/functions.js
+++ b/apps/maarch_entreprise/js/functions.js
@@ -847,7 +847,7 @@ function close_action(id_action, page, path_manage_script, mode_req, res_id_valu
         } else {
             if (page != '' && page != NaN && page && page != null) {
                 if (typeof window['angularSignatureBookComponent'] != "undefined") {
-                    window.angularSignatureBookComponent.componentAfterAction();
+                    // window.angularSignatureBookComponent.componentAfterAction();
                 } else {
                     do_nothing = false;
                     window.top.location.href = page;
@@ -855,7 +855,7 @@ function close_action(id_action, page, path_manage_script, mode_req, res_id_valu
 
             } else if (do_nothing == false) {
                 if (typeof window['angularSignatureBookComponent'] != "undefined") {
-                    window.angularSignatureBookComponent.componentAfterAction();
+                    // window.angularSignatureBookComponent.componentAfterAction();
                 } else {
                     window.top.location.hash = "";
                     window.top.location.reload();
@@ -1281,13 +1281,13 @@ function action_change_status(path_manage_script, mode_req, res_id_values, table
                     var cur_url = window.top.location.href;
                     if (cur_url.indexOf("&directLinkToAction") != -1) {
                         if (typeof window['angularSignatureBookComponent'] != "undefined") {
-                            window.angularSignatureBookComponent.componentAfterAction();
+                            // window.angularSignatureBookComponent.componentAfterAction();
                         } else {
                             window.top.location = cur_url.replace("&directLinkToAction", "");
                         }
                     } else {
                         if (typeof window['angularSignatureBookComponent'] != "undefined") {
-                            window.angularSignatureBookComponent.componentAfterAction();
+                            // window.angularSignatureBookComponent.componentAfterAction();
                         } else {
                             
                             var arr = window.top.location.href.split('&');
@@ -1779,7 +1779,7 @@ function addLinks(path_manage_script, child, parent, action, tableHist) {
                 eval("response = " + answer.responseText);
                 if (response.status == 0 || response.status == 1) {
                     if (typeof window.parent['angularSignatureBookComponent'] != "undefined") {
-                        window.parent.angularSignatureBookComponent.componentAfterLinks();
+                        // window.parent.angularSignatureBookComponent.componentAfterLinks();
                     }
                     if (response.status == 0) {
                         $(divName).innerHTML = response.links;
@@ -2842,7 +2842,7 @@ function setSendAttachment(id) {
         },
         success: function (answer) {
             if (typeof window.parent['angularSignatureBookComponent'] !== "undefined") {
-                window.parent.angularSignatureBookComponent.componentAfterAttach("left");
+                // window.parent.angularSignatureBookComponent.componentAfterAttach("left");
             }
         },
         error: function (err) {
diff --git a/modules/notes/js/functions.js b/modules/notes/js/functions.js
index 0ce0d024f68..434517a222d 100755
--- a/modules/notes/js/functions.js
+++ b/modules/notes/js/functions.js
@@ -40,7 +40,7 @@ function validNotesForm(path, form_id) {
             eval("response = "+answer.responseText);
             if(response.status == 0){
                 if (typeof window.parent['angularSignatureBookComponent'] != "undefined") {
-                    window.parent.angularSignatureBookComponent.componentAfterNotes();
+                    // window.parent.angularSignatureBookComponent.componentAfterNotes();
                 }
                 destroyModal('form_notes');
                 var modInfo = $j('<div class="info" id="main_info" onclick="this.remove();">'+response.msg_result+'</div>');
diff --git a/package.json b/package.json
index 346870bb82b..11b2d60b6a9 100755
--- a/package.json
+++ b/package.json
@@ -26,7 +26,7 @@
     "jstree-bootstrap-theme": "^1.0.1",
     "ng2-pdf-viewer": "^5.3.4",
     "ngx-cookie-service": "^2.1.0",
-    "ngx-pipes": "^2.6.0",
+    "ngx-pipes": "^2.7.3",
     "pdfjs-dist": "2.2.228",
     "photoswipe": "^4.1.3",
     "tinymce": "^5.1.6",
diff --git a/src/frontend/app/process/process.component.ts b/src/frontend/app/process/process.component.ts
index b2d2b5420cd..19d393972b8 100755
--- a/src/frontend/app/process/process.component.ts
+++ b/src/frontend/app/process/process.component.ts
@@ -194,7 +194,6 @@ export class ProcessComponent implements OnInit {
             } else {
                 this.initProcessPage(params);
             }
-            console.log(params);
         }, (err: any) => {
             this.notify.handleErrors(err);
         });
diff --git a/src/frontend/app/signature-book.component.ts b/src/frontend/app/signature-book.component.ts
index fc341c6b244..c3df638f3a6 100755
--- a/src/frontend/app/signature-book.component.ts
+++ b/src/frontend/app/signature-book.component.ts
@@ -5,19 +5,16 @@ import { Router, ActivatedRoute } from '@angular/router';
 import { LANG } from './translate.component';
 import { NotificationService } from './notification.service';
 import { tap, catchError, filter } from 'rxjs/operators';
-import { of } from 'rxjs';
+import { of, Subscription } from 'rxjs';
 import { PrivilegeService } from '../service/privileges.service';
 import { MatDialogRef, MatDialog } from '@angular/material';
 import { AttachmentCreateComponent } from './attachments/attachment-create/attachment-create.component';
 import { FunctionsService } from '../service/functions.service';
 import { AttachmentPageComponent } from './attachments/attachments-page/attachment-page.component';
 import { VisaWorkflowComponent } from './visa/visa-workflow.component';
+import { ActionsService } from './actions/actions.service';
 
-declare function lockDocument(resId: number) : void;
-declare function unlockDocument(resId: number) : void;
-declare function valid_action_form(a1: string, a2: string, a3: string, a4: number, a5: string, a6: string, a7: string, a8: string, a9: boolean, a10: any) : void;
 declare function $j(selector: string) : any;
-declare function setSessionForSignatureBook(resId: any) : void;
 
 declare var angularGlobals : any;
 
@@ -68,6 +65,9 @@ export class SignatureBookComponent implements OnInit {
     loading                     : boolean   = false;
     loadingSign                 : boolean   = false;
 
+    subscription: Subscription;
+    currentResourceLock: any = null;
+
     leftContentWidth            : string    = "44%";
     rightContentWidth           : string    = "44%";
     dialogRef: MatDialogRef<any>;
@@ -109,13 +109,16 @@ export class SignatureBookComponent implements OnInit {
         private notify: NotificationService,
         public privilegeService: PrivilegeService,
         public dialog: MatDialog,
-        public functions: FunctionsService
+        public functions: FunctionsService,
+        public actionService: ActionsService
     ) {
-        window['angularSignatureBookComponent'] = {
-            componentAfterAttach: (value: string) => this.processAfterAttach(value),
-            componentAfterAction: () => this.processAfterAction()
-        };
         (<any>window).pdfWorkerSrc = '../../node_modules/pdfjs-dist/build/pdf.worker.min.js';
+
+        // Event after process action 
+        this.subscription = this.actionService.catchAction().subscribe(message => {
+            clearInterval(this.currentResourceLock);
+            this.processAfterAction();
+        });
     }
 
     ngOnInit() : void {
@@ -130,8 +133,7 @@ export class SignatureBookComponent implements OnInit {
             this.userId    = params['userId'];
 
             this.signatureBook.resList = []; // This line is added because of manage action behaviour (processAfterAction is called twice)
-            lockDocument(this.resId);
-            setInterval(() => {lockDocument(this.resId)}, 50000);
+            this.lockResource();
             this.http.get("../../rest/signatureBook/users/" + this.userId + "/groups/" + this.groupId + "/baskets/" + this.basketId + "/resources/" + this.resId)
                 .subscribe((data : any) => {
                     if (data.error) {
@@ -189,6 +191,35 @@ export class SignatureBookComponent implements OnInit {
                 });
         });
     }
+
+    lockResource() {
+        this.http.put(`../../rest/resourcesList/users/${this.userId}/groups/${this.groupId}/baskets/${this.basketId}/lock`, { resources: [this.resId] }).pipe(
+            catchError((err: any) => {
+                this.notify.handleErrors(err);
+                return of(false);
+            })
+        ).subscribe();
+
+        this.currentResourceLock = setInterval(() => {
+            this.http.put(`../../rest/resourcesList/users/${this.userId}/groups/${this.groupId}/baskets/${this.basketId}/lock`, { resources: [this.resId] }).pipe(
+                catchError((err: any) => {
+                    this.notify.handleErrors(err);
+                    return of(false);
+                })
+            ).subscribe();
+        }, 50000);
+    }
+
+    unlockResource() {
+        clearInterval(this.currentResourceLock);
+
+        this.http.put(`../../rest/resourcesList/users/${this.userId}/groups/${this.groupId}/baskets/${this.basketId}/unlock`, { resources: [this.resId] }).pipe(
+            catchError((err: any) => {
+                this.notify.handleErrors(err);
+                return of(false);
+            })
+        ).subscribe();
+    }
     
     loadActions() {
         this.http.get("../../rest/resourcesList/users/" + this.userId + "/groups/" + this.groupId + "/baskets/" + this.basketId + "/actions?resId=" + this.resId)
@@ -199,10 +230,6 @@ export class SignatureBookComponent implements OnInit {
         });
     }
 
-    ngOnDestroy() : void {
-        delete window['angularSignatureBookComponent'];
-    }
-
     processAfterAttach(mode: string) {
         this.zone.run(() => this.refreshAttachments(mode));
     }
@@ -461,28 +488,24 @@ export class SignatureBookComponent implements OnInit {
 
     validForm() {
         if ($j("#signatureBookActions option:selected")[0].value != "") {
-            this.sendActionForm();
+            this.processAction();
         } else {
             alert("Aucune action choisie");
         }
     }
 
-    sendActionForm() {
-        unlockDocument(this.resId);
-
-        setSessionForSignatureBook(this.resId);
-        valid_action_form(
-            'empty',
-            'index.php?display=true&page=manage_action&module=core',
-            this.signatureBook.currentAction.id,
-            this.resId,
-            'res_letterbox',
-            'null',
-            'letterbox_coll',
-            'page',
-            false,
-            [$j("#signatureBookActions option:selected")[0].value]
-        );
+    processAction() {
+        this.http.get(`../../rest/resources/${this.resId}?light=true`).pipe(
+            tap((data: any) => {
+                let actionId = $j("#signatureBookActions option:selected")[0].value;
+                let selectedAction = this.signatureBook.actions.filter((action: any) => action.id == actionId)[0];
+                this.actionService.launchAction(selectedAction, this.userId, this.groupId, this.basketId, [this.resId], data, false);
+            }),
+            catchError((err: any) => {
+                this.notify.handleErrors(err);
+                return of(false);
+            })
+        ).subscribe();
     }
 
     refreshBadge(nbRres: any, id: string) {
-- 
GitLab