From 1068eef1581450615177e56eeb389b308826ddc2 Mon Sep 17 00:00:00 2001
From: Alex ORLUC <alex.orluc@maarch.org>
Date: Fri, 21 May 2021 16:53:50 +0200
Subject: [PATCH] FIX #16992 TIME 2:30 add merge var control + add merge far in
 cursor pos

---
 angular.json                                  |  3 +-
 lang/fr.json                                  |  4 +-
 .../app/administration/otp/otp.component.html |  4 +-
 .../app/administration/otp/otp.component.ts   | 75 ++++++++++++-------
 4 files changed, 56 insertions(+), 30 deletions(-)

diff --git a/angular.json b/angular.json
index 3b43cc652b..9bc188fb30 100755
--- a/angular.json
+++ b/angular.json
@@ -48,7 +48,8 @@
               },
               { "glob": "**/*", "input": "node_modules/tinymce/skins", "output": "/tinymce/skins/" },
               { "glob": "**/*", "input": "node_modules/tinymce/themes", "output": "/tinymce/themes/" },
-              { "glob": "**/*", "input": "node_modules/tinymce/plugins", "output": "/tinymce/plugins/" }
+              { "glob": "**/*", "input": "node_modules/tinymce/plugins", "output": "/tinymce/plugins/" },
+              { "glob": "**/*", "input": "node_modules/tinymce/icons", "output": "/tinymce/icons/" }
             ],
             "styles": [
               {
diff --git a/lang/fr.json b/lang/fr.json
index 3fd4c965f7..d36a8b41ea 100755
--- a/lang/fr.json
+++ b/lang/fr.json
@@ -509,6 +509,8 @@
 		"mergedVariablesMsg": "Utiliser la balise",
 		"mergedVariablesMsg2": "pour afficher le code de sécurité généré.",
 		"mergedVariablesMsgEmail": "Vous pouvez utiliser les différentes balises ci-dessous pour enrichir votre e-mail",
-		"updateOtp": "Modifier l'utilisateur externe"
+		"updateOtp": "Modifier l'utilisateur externe",
+		"smsCodeTagMandatory": "La balise <b>code</b> est requis pour envoie du code de sécurité par SMS",
+		"emailLinkTagMandatory": "La balise <b>lien d'accès</b> est requis pour la notification email"
 	}
 }
diff --git a/src/frontend/app/administration/otp/otp.component.html b/src/frontend/app/administration/otp/otp.component.html
index d1b77955eb..a332eb00eb 100644
--- a/src/frontend/app/administration/otp/otp.component.html
+++ b/src/frontend/app/administration/otp/otp.component.html
@@ -7,7 +7,7 @@
         <ion-title>{{title}}</ion-title>
     </ion-toolbar>
 </ion-header>
-<form *ngIf="!loading" style="display: contents;" id="adminForm" (ngSubmit)="onSubmit()" #adminForm="ngForm">
+<form style="display: contents;" id="adminForm" (ngSubmit)="onSubmit()" #adminForm="ngForm">
     <ion-content>
         <ion-item>
             <ion-label color="secondary" position="floating">{{'lang.label' | translate}} *</ion-label>
@@ -81,7 +81,7 @@
                 </ion-label>
             </ion-list-header>
             <ion-item lines="none">
-                <textarea style="padding-top: 10px;width: 100%;" name="email_message" id="email_message" [(ngModel)]="connector.message.notification.body"></textarea>
+                <textarea style="padding-top: 10px;width: 100%;visibility: hidden;" name="email_message" id="email_message" [(ngModel)]="connector.message.notification.body"></textarea>
             </ion-item>
             <ion-item lines="none">
                 <ion-note style="align-items: center;display:flex;">
diff --git a/src/frontend/app/administration/otp/otp.component.ts b/src/frontend/app/administration/otp/otp.component.ts
index a5d055b0e1..07b533076b 100644
--- a/src/frontend/app/administration/otp/otp.component.ts
+++ b/src/frontend/app/administration/otp/otp.component.ts
@@ -46,24 +46,7 @@ export class OtpComponent implements OnInit, OnDestroy {
         'email'
     ];
 
-    tags = [
-        {
-            id: '<span class="mceNonEditable"><tag data-tag-name="url" data-tag-type="button" data-tag-title="Accédez aux documents">accessLink</tag></span>',
-            label : 'lang.accessLink'
-        },
-        {
-            id: '<span class="mceNonEditable"><tag data-tag-name="recipient.lastname" data-tag-type="string">recipientLastname</tag></span>',
-            label : 'lang.recipientLastname'
-        },
-        {
-            id: '<span class="mceNonEditable"><tag data-tag-name="recipient.firstname" data-tag-type="string">recipientFirstname</tag></span>',
-            label : 'lang.recipientFirstname'
-        },
-        {
-            id: '<span class="mceNonEditable"><tag data-tag-name="procedure.expiresAt" data-tag-type="date" data-tag-date-format="SHORT" data-tag-time-format="NONE" data-tag-locale="fr_FR">expiresAt</tag></span>',
-            label : 'lang.expiresAt'
-        }
-    ];
+    tags = [];
 
     connectorTypes: any[] = [];
     nbCurrentWorkflow: number;
@@ -102,6 +85,7 @@ export class OtpComponent implements OnInit, OnDestroy {
 
     ngOnInit(): void {
         this.route.params.subscribe(async (params: any) => {
+            this.initTags();
             if (params['id'] === undefined) {
                 this.creationMode = true;
                 this.title = this.translate.instant('lang.otpConnectorCreation');
@@ -109,7 +93,7 @@ export class OtpComponent implements OnInit, OnDestroy {
                 this.connectorClone = JSON.parse(JSON.stringify(this.connector));
                 setTimeout(() => {
                     this.initMce();
-                }, 100);
+                }, 0);
             } else {
                 this.creationMode = false;
                 await this.getConfig(params['id']);
@@ -124,7 +108,7 @@ export class OtpComponent implements OnInit, OnDestroy {
     initMce() {
         const param = {
             selector: '#email_message',
-            base_url: '../../../node_modules/tinymce/',
+            base_url: '../../tinymce/',
             height: '200',
             suffix: '.min',
             extended_valid_elements : 'tag,class',
@@ -153,6 +137,27 @@ export class OtpComponent implements OnInit, OnDestroy {
         tinymce.init(param);
     }
 
+    initTags() {
+        this.tags = [
+            {
+                id: `<span class="mceNonEditable"><tag data-tag-name="url" data-tag-type="button" data-tag-title="Accédez aux documents">${this.translate.instant('lang.accessLink')}</tag></span>`,
+                label : 'lang.accessLink'
+            },
+            {
+                id: `<span class="mceNonEditable"><tag data-tag-name="recipient.lastname" data-tag-type="string">${this.translate.instant('lang.recipientLastname')}</tag></span>`,
+                label : 'lang.recipientLastname'
+            },
+            {
+                id: `<span class="mceNonEditable"><tag data-tag-name="recipient.firstname" data-tag-type="string">${this.translate.instant('lang.recipientFirstname')}</tag></span>`,
+                label : 'lang.recipientFirstname'
+            },
+            {
+                id: `<span class="mceNonEditable"><tag data-tag-name="procedure.expiresAt" data-tag-type="date" data-tag-date-format="SHORT" data-tag-time-format="NONE" data-tag-locale="fr_FR">${this.translate.instant('lang.expiresAt')}</tag></span>`,
+                label : 'lang.expiresAt'
+            }
+        ];
+    }
+
     getConfig(id: any) {
         return new Promise((resolve) => {
             this.http.get('../rest/connectors/' + id)
@@ -164,7 +169,7 @@ export class OtpComponent implements OnInit, OnDestroy {
                         this.title = this.connector.label;
                         setTimeout(() => {
                             this.initMce();
-                        }, 100);
+                        }, 0);
                     }),
                     finalize(() => {
                         this.loading = false;
@@ -190,6 +195,7 @@ export class OtpComponent implements OnInit, OnDestroy {
     }
 
     canValidate() {
+
         if (this.connector.label === this.connectorClone.label) {
             return false;
         } else {
@@ -198,14 +204,31 @@ export class OtpComponent implements OnInit, OnDestroy {
     }
 
     onSubmit() {
+
         this.connector.message.otp_sms = this.connector.securityModes.indexOf('sms') === -1 ? '' : this.connector.message.otp_sms;
 
         this.connector.message.notification.body = tinymce.get('email_message').getContent();
 
-        if (this.creationMode) {
-            this.createconnector();
-        } else {
-            this.modifyconnector();
+        const regex = /data-tag-name=\"url\"/g;
+
+        let error = false;
+
+        if (tinymce.get('email_message').getContent().match(regex) === null) {
+            this.notificationService.error(this.translate.instant('lang.emailLinkTagMandatory'));
+            error = true;
+        }
+
+        const regex2 = /\{\{code\}\}/g;
+        if (this.connector.securityModes.indexOf('sms') > -1 && this.connector.message.otp_sms.match(regex2) === null) {
+            this.notificationService.error(this.translate.instant('lang.smsCodeTagMandatory'));
+            error = true;
+        }
+        if (!error) {
+            if (this.creationMode) {
+                this.createconnector();
+            } else {
+                this.modifyconnector();
+            }
         }
     }
 
@@ -310,6 +333,6 @@ export class OtpComponent implements OnInit, OnDestroy {
     }
 
     addEmailVariable(code: string) {
-        tinymce.get('email_message').setContent(tinymce.get('email_message').getContent() + ' ' + code);
+        tinymce.get('email_message').execCommand('mceInsertContent', false, ' ' + code + ' ');
     }
 }
-- 
GitLab