From 2557c753874f5bdb032a623b99f0e974a11aae33 Mon Sep 17 00:00:00 2001
From: "florian.azizian" <florian.azizian@maarch.org>
Date: Thu, 5 Dec 2019 09:46:26 +0100
Subject: [PATCH] FEAT #12509 TIME 0:45 History and check privileges in
 contacts

---
 .../contact/controllers/ContactController.php | 62 ++++++++++++++++++-
 .../ContactCustomFieldController.php          | 19 ++++--
 .../contact/models/ContactFillingModel.php    |  1 -
 src/core/lang/lang-en.php                     |  8 +++
 src/core/lang/lang-fr.php                     |  8 +++
 src/core/lang/lang-nl.php                     |  8 +++
 src/frontend/lang/lang-fr.ts                  |  2 +-
 7 files changed, 98 insertions(+), 10 deletions(-)

diff --git a/src/app/contact/controllers/ContactController.php b/src/app/contact/controllers/ContactController.php
index 0bd81cd1abc..a9053a189d6 100755
--- a/src/app/contact/controllers/ContactController.php
+++ b/src/app/contact/controllers/ContactController.php
@@ -19,13 +19,14 @@ use Contact\models\ContactFillingModel;
 use Contact\models\ContactModel;
 use Entity\models\EntityModel;
 use Group\controllers\PrivilegeController;
+use History\controllers\HistoryController;
 use Resource\controllers\ResController;
 use Resource\models\ResModel;
 use Resource\models\ResourceContactModel;
-use SrcCore\models\CoreConfigModel;
 use Respect\Validation\Validator;
 use Slim\Http\Request;
 use Slim\Http\Response;
+use SrcCore\models\CoreConfigModel;
 use SrcCore\models\TextFormatModel;
 use SrcCore\models\ValidatorModel;
 use User\models\UserModel;
@@ -96,6 +97,25 @@ class ContactController
             'external_id'           => $externalId
         ]);
 
+        $historyInfoContact = '';
+        if (!empty($body['firstname']) || !empty($body['lastname'])) {
+            $historyInfoContact .= $body['firstname'] . ' ' . $body['lastname'];
+        }
+        if (!empty($historyInfoContact) && !empty($body['company'])) {
+            $historyInfoContact .= ' (' . $body['company'] . ')';
+        } else {
+            $historyInfoContact .= $body['company'];
+        }
+
+        HistoryController::add([
+            'tableName' => 'contacts',
+            'recordId'  => $id,
+            'eventType' => 'ADD',
+            'info'      => _CONTACT_CREATION . " : " . trim($historyInfoContact),
+            'moduleId'  => 'contact',
+            'eventId'   => 'contactCreation',
+        ]);
+
         ContactController::createAdjacentData(['body' => $body, 'id' => $id]);
 
         return $response->withJson(['id' => $id]);
@@ -200,6 +220,25 @@ class ContactController
             'data'  => [$args['id']]
         ]);
 
+        $historyInfoContact = '';
+        if (!empty($body['firstname']) || !empty($body['lastname'])) {
+            $historyInfoContact .= $body['firstname'] . ' ' . $body['lastname'];
+        }
+        if (!empty($historyInfoContact) && !empty($body['company'])) {
+            $historyInfoContact .= ' (' . $body['company'] . ')';
+        } else {
+            $historyInfoContact .= $body['company'];
+        }
+
+        HistoryController::add([
+            'tableName' => 'contacts',
+            'recordId'  => $args['id'],
+            'eventType' => 'UP',
+            'info'      => _CONTACT_MODIFICATION . " : " . trim($historyInfoContact),
+            'moduleId'  => 'contact',
+            'eventId'   => 'contactModification',
+        ]);
+
         return $response->withStatus(204);
     }
 
@@ -239,7 +278,7 @@ class ContactController
             return $response->withStatus(400)->withJson(['errors' => 'Route id is not an integer']);
         }
 
-        $contact = ContactModel::getById(['id' => $args['id'], 'select' => [1]]);
+        $contact = ContactModel::getById(['id' => $args['id'], 'select' => ['lastname', 'firstname', 'company']]);
         if (empty($contact)) {
             return $response->withStatus(400)->withJson(['errors' => 'Contact does not exist']);
         }
@@ -249,6 +288,25 @@ class ContactController
             'data'  => [$args['id']]
         ]);
 
+        $historyInfoContact = '';
+        if (!empty($contact[0]['firstname']) || !empty($contact[0]['lastname'])) {
+            $historyInfoContact .= $contact[0]['firstname'] . ' ' . $contact[0]['lastname'];
+        }
+        if (!empty($historyInfoContact) && !empty($contact[0]['company'])) {
+            $historyInfoContact .= ' (' . $contact[0]['company'] . ')';
+        } else {
+            $historyInfoContact .= $contact[0]['company'];
+        }
+
+        HistoryController::add([
+            'tableName' => 'contacts',
+            'recordId'  => $args['id'],
+            'eventType' => 'DEL',
+            'info'      => _CONTACT_SUPPRESSION . " : " . trim($historyInfoContact),
+            'moduleId'  => 'contact',
+            'eventId'   => 'contactSuppression',
+        ]);
+
         return $response->withStatus(204);
     }
 
diff --git a/src/app/contact/controllers/ContactCustomFieldController.php b/src/app/contact/controllers/ContactCustomFieldController.php
index 584ef0fa536..f27ede076cb 100644
--- a/src/app/contact/controllers/ContactCustomFieldController.php
+++ b/src/app/contact/controllers/ContactCustomFieldController.php
@@ -15,6 +15,7 @@ namespace Contact\controllers;
 
 use Contact\models\ContactCustomFieldListModel;
 use Contact\models\ContactCustomFieldModel;
+use Group\controllers\PrivilegeController;
 use History\controllers\HistoryController;
 use Respect\Validation\Validator;
 use Slim\Http\Request;
@@ -35,7 +36,9 @@ class ContactCustomFieldController
 
     public function create(Request $request, Response $response)
     {
-        //TODO privileges
+        if (!PrivilegeController::hasPrivilege(['privilegeId' => 'admin_contacts', 'userId' => $GLOBALS['id']])) {
+            return $response->withStatus(403)->withJson(['errors' => 'Service forbidden']);
+        }
 
         $body = $request->getParsedBody();
 
@@ -62,7 +65,7 @@ class ContactCustomFieldController
             'tableName' => 'contacts_custom_fields_list',
             'recordId'  => $id,
             'eventType' => 'ADD',
-            'info'      => _CUSTOMFIELDS_CREATION . " : {$body['label']}",
+            'info'      => _CONTACT_CUSTOMFIELDS_CREATION . " : {$body['label']}",
             'moduleId'  => 'contactCustomFieldList',
             'eventId'   => 'contactCustomFieldListCreation',
         ]);
@@ -72,7 +75,9 @@ class ContactCustomFieldController
 
     public function update(Request $request, Response $response, array $args)
     {
-        //TODO privileges
+        if (!PrivilegeController::hasPrivilege(['privilegeId' => 'admin_contacts', 'userId' => $GLOBALS['id']])) {
+            return $response->withStatus(403)->withJson(['errors' => 'Service forbidden']);
+        }
 
         if (!Validator::intVal()->notEmpty()->validate($args['id'])) {
             return $response->withStatus(400)->withJson(['errors' => 'Param id is empty or not an integer']);
@@ -113,7 +118,7 @@ class ContactCustomFieldController
             'tableName' => 'contacts_custom_fields_list',
             'recordId'  => $args['id'],
             'eventType' => 'UP',
-            'info'      => _CUSTOMFIELDS_MODIFICATION . " : {$body['label']}",
+            'info'      => _CONTACT_CUSTOMFIELDS_MODIFICATION . " : {$body['label']}",
             'moduleId'  => 'contactCustomFieldList',
             'eventId'   => 'contactCustomFieldListModification',
         ]);
@@ -123,7 +128,9 @@ class ContactCustomFieldController
 
     public function delete(Request $request, Response $response, array $args)
     {
-        //TODO privileges
+        if (!PrivilegeController::hasPrivilege(['privilegeId' => 'admin_contacts', 'userId' => $GLOBALS['id']])) {
+            return $response->withStatus(403)->withJson(['errors' => 'Service forbidden']);
+        }
 
         if (!Validator::intVal()->notEmpty()->validate($args['id'])) {
             return $response->withStatus(400)->withJson(['errors' => 'Param id is empty or not an integer']);
@@ -142,7 +149,7 @@ class ContactCustomFieldController
             'tableName' => 'contacts_custom_fields_list',
             'recordId'  => $args['id'],
             'eventType' => 'DEL',
-            'info'      => _CUSTOMFIELDS_SUPPRESSION . " : {$field['label']}",
+            'info'      => _CONTACT_CUSTOMFIELDS_SUPPRESSION . " : {$field['label']}",
             'moduleId'  => 'contactCustomFieldList',
             'eventId'   => 'contactCustomFieldListSuppression',
         ]);
diff --git a/src/app/contact/models/ContactFillingModel.php b/src/app/contact/models/ContactFillingModel.php
index 58614e9d2c1..9d505538b3b 100755
--- a/src/app/contact/models/ContactFillingModel.php
+++ b/src/app/contact/models/ContactFillingModel.php
@@ -15,7 +15,6 @@
 
 namespace Contact\models;
 
-
 use SrcCore\models\DatabaseModel;
 use SrcCore\models\ValidatorModel;
 
diff --git a/src/core/lang/lang-en.php b/src/core/lang/lang-en.php
index 37bc7f9c05a..258c10da7fe 100755
--- a/src/core/lang/lang-en.php
+++ b/src/core/lang/lang-en.php
@@ -407,6 +407,14 @@ define("_CUSTOMFIELDS_CREATION", "Custom field creation");
 define("_CUSTOMFIELDS_MODIFICATION", "Custom field modification");
 define("_CUSTOMFIELDS_SUPPRESSION", "Custom field suppression");
 
+define("_CONTACT_CUSTOMFIELDS_CREATION", "Contact custom field creation");
+define("_CONTACT_CUSTOMFIELDS_MODIFICATION", "Contact custom field modification");
+define("_CONTACT_CUSTOMFIELDS_SUPPRESSION", "Contact custom field suppression");
+
+define("_CONTACT_CREATION", "Contact creation");
+define("_CONTACT_MODIFICATION", "Contact modification");
+define("_CONTACT_SUPPRESSION", "Contact suppression");
+
 define("_INDEXINGMODEL_CREATION", "Indexing model creation");
 define("_INDEXINGMODEL_MODIFICATION", "Indexing model modification");
 define("_INDEXINGMODEL_SUPPRESSION", "Indexing model suppression");
diff --git a/src/core/lang/lang-fr.php b/src/core/lang/lang-fr.php
index 13451a2ee03..7b2be6c8db0 100755
--- a/src/core/lang/lang-fr.php
+++ b/src/core/lang/lang-fr.php
@@ -406,6 +406,14 @@ define("_CUSTOMFIELDS_CREATION", "Champ personnalisé créé");
 define("_CUSTOMFIELDS_MODIFICATION", "Champ personnalisé modifié");
 define("_CUSTOMFIELDS_SUPPRESSION", "Champ personnalisé supprimé");
 
+define("_CONTACT_CUSTOMFIELDS_CREATION", "Champ personnalisé de contact créé");
+define("_CONTACT_CUSTOMFIELDS_MODIFICATION", "Champ personnalisé de contact modifié");
+define("_CONTACT_CUSTOMFIELDS_SUPPRESSION", "Champ personnalisé de contact supprimé");
+
+define("_CONTACT_CREATION", "Contact créé");
+define("_CONTACT_MODIFICATION", "Contact modifié");
+define("_CONTACT_SUPPRESSION", "Contact supprimé");
+
 define("_INDEXINGMODEL_CREATION", "Modèle d'enregistrement créé");
 define("_INDEXINGMODEL_MODIFICATION", "Modèle d'enregistrement modifié");
 define("_INDEXINGMODEL_SUPPRESSION", "Modèle d'enregistrement supprimé");
diff --git a/src/core/lang/lang-nl.php b/src/core/lang/lang-nl.php
index e16cecaa76c..3a26a60a607 100755
--- a/src/core/lang/lang-nl.php
+++ b/src/core/lang/lang-nl.php
@@ -412,6 +412,14 @@ define("_CUSTOMFIELDS_CREATION", "Custom field creation_TO_TRANSLATE");
 define("_CUSTOMFIELDS_MODIFICATION", "Custom field modification_TO_TRANSLATE");
 define("_CUSTOMFIELDS_SUPPRESSION", "Custom field suppression_TO_TRANSLATE");
 
+define("_CONTACT_CUSTOMFIELDS_CREATION", "Contact custom field creation_TO_TRANSLATE");
+define("_CONTACT_CUSTOMFIELDS_MODIFICATION", "Contact custom field modification_TO_TRANSLATE");
+define("_CONTACT_CUSTOMFIELDS_SUPPRESSION", "Contact custom field suppression_TO_TRANSLATE");
+
+define("_CONTACT_CREATION", "Contact creation_TO_TRANSLATE");
+define("_CONTACT_MODIFICATION", "Contact modification_TO_TRANSLATE");
+define("_CONTACT_SUPPRESSION", "Contact suppression_TO_TRANSLATE");
+
 define("_INDEXINGMODEL_CREATION", "Indexing model creation_TO_TRANSLATE");
 define("_INDEXINGMODEL_MODIFICATION", "Indexing model modification_TO_TRANSLATE");
 define("_INDEXINGMODEL_SUPPRESSION", "Indexing model suppression_TO_TRANSLATE");
diff --git a/src/frontend/lang/lang-fr.ts b/src/frontend/lang/lang-fr.ts
index b0d5e3f8516..c9dd9c62d68 100755
--- a/src/frontend/lang/lang-fr.ts
+++ b/src/frontend/lang/lang-fr.ts
@@ -1353,7 +1353,7 @@ export const LANG_FR = {
     "personalDataMsg": "Les données personnelles de l'utilisateur sont : ",
     "notVisible": "Non visibles",
     "enableGroupMsg": "Ce groupe pourra potentiellement avoir accès à l'ensemble des fonctionnalités de l'application.",
-    "sendActivationNotification": "Envoyer à nouveau le courriel d'activation",
+    "sendActivationNotification": "Renvoyer le courriel d'activation",
     "activationNotificationSend": "Le courriel d'activation a été envoyé",
     "tabProcessPosition": "Se positionner sur l'onglet",
     "saveModifiedData": "Voulez-vous sauvegarder les modifications ?",
-- 
GitLab