From 5946846e317d1d0164c4d86a005e75c919545579 Mon Sep 17 00:00:00 2001
From: Alex ORLUC <alex.orluc@maarch.org>
Date: Thu, 19 Sep 2019 19:51:52 +0200
Subject: [PATCH] FEAT #11271 TIME 1 add entities tree select input in indexing
 form

---
 src/frontend/app/app-routing.module.ts        |  2 +-
 .../app/indexation/indexation.component.html  |  2 +-
 .../app/indexation/indexation.component.ts    |  2 +
 .../indexing-form.component.html              |  3 +-
 .../indexing-form/indexing-form.component.ts  | 70 ++++++++++++++++---
 5 files changed, 64 insertions(+), 15 deletions(-)

diff --git a/src/frontend/app/app-routing.module.ts b/src/frontend/app/app-routing.module.ts
index e9fb98ca2b0..258bcefd814 100755
--- a/src/frontend/app/app-routing.module.ts
+++ b/src/frontend/app/app-routing.module.ts
@@ -27,7 +27,7 @@ import { IndexationComponent }              from './indexation/indexation.compon
             { path: 'saveNumericPackage', canActivate: [AppGuard], component: SaveNumericPackageComponent },
             { path: 'separators/print', canActivate: [AppGuard], component: PrintSeparatorComponent },
             { path: 'signatureBook/users/:userId/groups/:groupId/baskets/:basketId/resources/:resId', canActivate: [AppGuard],component: SignatureBookComponent },
-            { path: 'indexing/:group', canActivate: [AppGuard],component: IndexationComponent },
+            { path: 'indexing/:groupId', canActivate: [AppGuard],component: IndexationComponent },
             { path: '**',  redirectTo: 'home', pathMatch: 'full' },
         ], { useHash: true }),
     ],
diff --git a/src/frontend/app/indexation/indexation.component.html b/src/frontend/app/indexation/indexation.component.html
index 2a1371a9332..8ab476c27dd 100644
--- a/src/frontend/app/indexation/indexation.component.html
+++ b/src/frontend/app/indexation/indexation.component.html
@@ -19,7 +19,7 @@
                 comme modèle</button>
         </div>
         <div class="indexing-form-container">
-            <app-indexing-form *ngIf="currentIndexingModel.id !== undefined" #indexingForm [indexingFormId]="currentIndexingModel.id"></app-indexing-form>
+            <app-indexing-form *ngIf="currentIndexingModel.id !== undefined" #indexingForm [groupId]="currentGroupId" [indexingFormId]="currentIndexingModel.id"></app-indexing-form>
         </div>
         <div class="actions-indexing-form">
             <button mat-button class="button-form-primary" [matMenuTriggerFor]="menu" style="flex:1;margin-right:20px;">
diff --git a/src/frontend/app/indexation/indexation.component.ts b/src/frontend/app/indexation/indexation.component.ts
index 674c5c60f09..27fbc5bbf56 100644
--- a/src/frontend/app/indexation/indexation.component.ts
+++ b/src/frontend/app/indexation/indexation.component.ts
@@ -38,6 +38,7 @@ export class IndexationComponent implements OnInit {
 
     indexingModels: any[] = [];
     currentIndexingModel: any = {};
+    currentGroupId: number;
 
     constructor(
         private route: ActivatedRoute,
@@ -56,6 +57,7 @@ export class IndexationComponent implements OnInit {
         this.headerService.setHeader("Enregistrement d'un courrier");
 
         this.route.params.subscribe(params => {
+            this.currentGroupId = params['groupId'];
             this.http.get("../../rest/indexingModels").pipe(
                 tap((data: any) => {
                     //
diff --git a/src/frontend/app/indexation/indexing-form/indexing-form.component.html b/src/frontend/app/indexation/indexing-form/indexing-form.component.html
index 99241786163..aa795c9b879 100644
--- a/src/frontend/app/indexation/indexing-form/indexing-form.component.html
+++ b/src/frontend/app/indexation/indexing-form/indexing-form.component.html
@@ -59,8 +59,7 @@
                                     <mat-select [formControl]="arrFormControl[field.identifier]"
                                         [placeholder]="field.system ? lang[field.type + 'Input'] : lang.defaultValue">
                                         <mat-option *ngIf="adminMode"></mat-option>
-                                        <mat-option *ngFor="let value of field.values" [value]="value.id" [title]="value.label" [disabled]="value.isTitle" [class.opt-group]="value.isTitle" [style.color]="value.color">
-                                            {{value.label}}
+                                        <mat-option *ngFor="let value of field.values" [value]="value.id" [title]="value.title !== undefined ? value.title : value.label" [disabled]="value.disabled" [class.opt-group]="value.isTitle" [style.color]="value.color" [innerHTML]="value.label">
                                         </mat-option>
                                     </mat-select>
                                 </mat-form-field>
diff --git a/src/frontend/app/indexation/indexing-form/indexing-form.component.ts b/src/frontend/app/indexation/indexing-form/indexing-form.component.ts
index c6dfa7eaf5f..11202f986ce 100644
--- a/src/frontend/app/indexation/indexing-form/indexing-form.component.ts
+++ b/src/frontend/app/indexation/indexing-form/indexing-form.component.ts
@@ -25,6 +25,7 @@ export class IndexingFormComponent implements OnInit {
     loading: boolean = true;
 
     @Input('indexingFormId') indexingFormId: number;
+    @Input('groupId') groupId: number;
     @Input('admin') adminMode: boolean;
 
     fieldCategories: any[] = ['mail', 'contact', 'process', 'classement'];
@@ -291,16 +292,60 @@ export class IndexingFormComponent implements OnInit {
                     elem.endDate = '_TODAY';
                 } else
                     if (elem.identifier === 'destination') {
-                        elem.values = [
-                            {
-                                label : 'test',
-                                id: 1
-                            },
-                            {
-                                label : 'plus',
-                                id: 2
-                            }
-                        ];
+                        if (this.adminMode) {
+                            this.http.get("../../rest/indexing/entities").pipe(
+                                tap((data: any) => {
+                                    let title = '';
+                                    elem.values = data.entities.map((entity: any) => {
+                                        title = entity.entity_label;
+
+                                        for (let index = 0; index < entity.level; index++) {
+                                            entity.entity_label = '&nbsp;&nbsp;&nbsp;&nbsp;' + entity.entity_label;
+                                        }
+                                        return {
+                                            id: entity.id,
+                                            title: title,
+                                            label: entity.entity_label,
+                                            disabled: false
+                                        }
+                                    });
+                                }),
+                                finalize(() => this.loading = false),
+                                catchError((err: any) => {
+                                    this.notify.handleErrors(err);
+                                    return of(false);
+                                })
+                            ).subscribe();
+                        } else {
+                            this.http.get("../../rest/indexing/" + this.groupId + "/entities").pipe(
+                                tap((data: any) => {
+                                    let title = '';
+
+                                    let defaultVal = data.entities.filter((entity: any) => entity.enabled === true && entity.id === elem.default_value);
+                                    elem.default_value = defaultVal.length > 0 ? defaultVal[0].id : '';
+                                    this.arrFormControl[elem.identifier].setValue(defaultVal.length > 0 ? defaultVal[0].id : '');
+
+                                    elem.values = data.entities.map((entity: any) => {
+                                        title = entity.entity_label;
+
+                                        for (let index = 0; index < entity.level; index++) {
+                                            entity.entity_label = '&nbsp;&nbsp;&nbsp;&nbsp;' + entity.entity_label;
+                                        }
+                                        return {
+                                            id: entity.id,
+                                            title: title,
+                                            label: entity.entity_label,
+                                            disabled: !entity.enabled
+                                        }
+                                    });
+                                }),
+                                finalize(() => this.loading = false),
+                                catchError((err: any) => {
+                                    this.notify.handleErrors(err);
+                                    return of(false);
+                                })
+                            ).subscribe();
+                        }
                     } else
                         if (elem.identifier === 'arrivalDate') {
                             elem.startDate = 'docDate';
@@ -351,6 +396,7 @@ export class IndexingFormComponent implements OnInit {
                                                             arrValues.push({
                                                                 id: doctype.doctypes_first_level_id,
                                                                 label: doctype.doctypes_first_level_label,
+                                                                disabled: true,
                                                                 isTitle: true,
                                                                 color: doctype.css_style
                                                             });
@@ -358,6 +404,7 @@ export class IndexingFormComponent implements OnInit {
                                                             arrValues.push({
                                                                 id: doctype.doctypes_second_level_id,
                                                                 label: doctype.doctypes_second_level_label,
+                                                                disabled: true,
                                                                 isTitle: true,
                                                                 color: doctype.css_style
                                                             });
@@ -366,7 +413,8 @@ export class IndexingFormComponent implements OnInit {
                                                                 return {
                                                                     id: info.type_id,
                                                                     label: info.description,
-                                                                    isTitle: false
+                                                                    disabled: false,
+                                                                    isTitle: false,
                                                                 }
                                                             }));
                                                         }
-- 
GitLab