From 84fd7500e46cb39b2cd871e6dd572d10d313c5b5 Mon Sep 17 00:00:00 2001 From: Damien <damien.burel@maarch.org> Date: Tue, 3 Dec 2019 18:04:41 +0100 Subject: [PATCH] FEAT #12510 TIME 1:00 Save contacts when indexing --- sql/structure.sql | 22 ++++++ .../resource/controllers/ResController.php | 67 +++++++++++++++++++ .../resource/models/ResourceContactModel.php | 26 +++---- 3 files changed, 102 insertions(+), 13 deletions(-) diff --git a/sql/structure.sql b/sql/structure.sql index 4ece2f8a647..ba4efe2d2f0 100755 --- a/sql/structure.sql +++ b/sql/structure.sql @@ -1619,3 +1619,25 @@ CREATE TABLE indexing_models_fields CONSTRAINT indexing_models_fields_pkey PRIMARY KEY (id) ) WITH (OIDS=FALSE); + +CREATE TABLE contacts_custom_fields_list +( + id serial NOT NULL, + label character varying(256) NOT NULL, + type character varying(256) NOT NULL, + values jsonb, + CONSTRAINT contacts_custom_fields_list_pkey PRIMARY KEY (id), + CONSTRAINT contacts_custom_fields_list_unique_key UNIQUE (label) +) +WITH (OIDS=FALSE); + +CREATE TABLE contacts_custom_fields +( + id serial NOT NULL, + contact_id INTEGER NOT NULL, + custom_field_id INTEGER NOT NULL, + value jsonb NOT NULL, + CONSTRAINT contacts_custom_fields_pkey PRIMARY KEY (id), + CONSTRAINT contacts_custom_fields_unique_key UNIQUE (contact_id, custom_field_id) +) +WITH (OIDS=FALSE); diff --git a/src/app/resource/controllers/ResController.php b/src/app/resource/controllers/ResController.php index 48bcc7dbeec..52d3054b4f2 100755 --- a/src/app/resource/controllers/ResController.php +++ b/src/app/resource/controllers/ResController.php @@ -18,6 +18,7 @@ use AcknowledgementReceipt\models\AcknowledgementReceiptModel; use Basket\models\BasketModel; use Basket\models\GroupBasketModel; use Basket\models\RedirectBasketModel; +use Contact\models\ContactModel; use Convert\controllers\ConvertPdfController; use Convert\controllers\ConvertThumbnailController; use Convert\models\AdrModel; @@ -41,6 +42,7 @@ use IndexingModel\models\IndexingModelModel; use Note\models\NoteModel; use Priority\models\PriorityModel; use Resource\models\ResModel; +use Resource\models\ResourceContactModel; use Respect\Validation\Validator; use setasign\Fpdi\Tcpdf\Fpdi; use Slim\Http\Request; @@ -870,6 +872,16 @@ class ResController TagResModel::create(['res_id' => $args['resId'], 'tag_id' => $tag]); } } + if (!empty($body['senders'])) { + foreach ($body['senders'] as $sender) { + ResourceContactModel::create(['res_id' => $args['resId'], 'item_id' => $sender['id'], 'type' => $sender['type'], 'mode' => 'sender']); + } + } + if (!empty($body['recipients'])) { + foreach ($body['recipients'] as $recipient) { + ResourceContactModel::create(['res_id' => $args['resId'], 'item_id' => $recipient['id'], 'type' => $recipient['type'], 'mode' => 'recipient']); + } + } return true; } @@ -932,6 +944,17 @@ class ResController TagResModel::create(['res_id' => $args['resId'], 'tag_id' => $tag]); } } + ResourceContactModel::delete(['where' => ['res_id = ?'], 'data' => [$args['resId']]]); + if (!empty($body['senders'])) { + foreach ($body['senders'] as $sender) { + ResourceContactModel::create(['res_id' => $args['resId'], 'item_id' => $sender['id'], 'type' => $sender['type'], 'mode' => 'sender']); + } + } + if (!empty($body['recipients'])) { + foreach ($body['recipients'] as $recipient) { + ResourceContactModel::create(['res_id' => $args['resId'], 'item_id' => $recipient['id'], 'type' => $recipient['type'], 'mode' => 'recipient']); + } + } return true; } @@ -1121,6 +1144,50 @@ class ResController return ['errors' => 'Body tags : One or more tags do not exist']; } } + if (!empty($body['senders'])) { + if (!Validator::arrayType()->notEmpty()->validate($body['senders'])) { + return ['errors' => 'Body senders is not an array']; + } + foreach ($body['senders'] as $key => $sender) { + if (!Validator::arrayType()->notEmpty()->validate($sender)) { + return ['errors' => "Body senders[{$key}] is not an array"]; + } + if ($sender['type'] == 'contact') { + $senderItem = ContactModel::getById(['id' => $sender['id'], 'select' => [1]]); + } elseif ($sender['type'] == 'user') { + $senderItem = UserModel::getById(['id' => $sender['id'], 'select' => [1]]); + } elseif ($sender['type'] == 'entity') { + $senderItem = EntityModel::getById(['id' => $sender['id'], 'select' => [1]]); + } else { + return ['errors' => "Body senders[{$key}] type is not valid"]; + } + if (empty($senderItem)) { + return ['errors' => "Body senders[{$key}] id does not exist"]; + } + } + } + if (!empty($body['recipients'])) { + if (!Validator::arrayType()->notEmpty()->validate($body['recipients'])) { + return ['errors' => 'Body recipients is not an array']; + } + foreach ($body['recipients'] as $key => $recipient) { + if (!Validator::arrayType()->notEmpty()->validate($recipient)) { + return ['errors' => "Body recipients[{$key}] is not an array"]; + } + if ($recipient['type'] == 'contact') { + $recipientItem = ContactModel::getById(['id' => $recipient['id'], 'select' => [1]]); + } elseif ($recipient['type'] == 'user') { + $recipientItem = UserModel::getById(['id' => $recipient['id'], 'select' => [1]]); + } elseif ($recipient['type'] == 'entity') { + $recipientItem = EntityModel::getById(['id' => $recipient['id'], 'select' => [1]]); + } else { + return ['errors' => "Body recipients[{$key}] type is not valid"]; + } + if (empty($recipientItem)) { + return ['errors' => "Body recipients[{$key}] id does not exist"]; + } + } + } if (!empty($body['diffusionList'])) { if (!Validator::arrayType()->notEmpty()->validate($body['diffusionList'])) { return ['errors' => 'Body diffusionList is not an array']; diff --git a/src/app/resource/models/ResourceContactModel.php b/src/app/resource/models/ResourceContactModel.php index 6e3af9fe173..5732d2c701f 100755 --- a/src/app/resource/models/ResourceContactModel.php +++ b/src/app/resource/models/ResourceContactModel.php @@ -96,34 +96,34 @@ class ResourceContactModel return $aContacts; } - public static function create(array $aArgs) + public static function create(array $args) { - ValidatorModel::notEmpty($aArgs, ['res_id', 'item_id', 'type', 'mode']); - ValidatorModel::intVal($aArgs, ['res_id', 'item_id']); - ValidatorModel::stringType($aArgs, ['type', 'mode']); + ValidatorModel::notEmpty($args, ['res_id', 'item_id', 'type', 'mode']); + ValidatorModel::intVal($args, ['res_id', 'item_id']); + ValidatorModel::stringType($args, ['type', 'mode']); DatabaseModel::insert([ 'table' => 'resource_contacts', 'columnsValues' => [ - 'res_id' => $aArgs['res_id'], - 'item_id' => $aArgs['item_id'], - 'type' => $aArgs['type'], - 'mode' => $aArgs['mode'] + 'res_id' => $args['res_id'], + 'item_id' => $args['item_id'], + 'type' => $args['type'], + 'mode' => $args['mode'] ] ]); return true; } - public static function delete(array $aArgs) + public static function delete(array $args) { - ValidatorModel::notEmpty($aArgs, ['where', 'data']); - ValidatorModel::arrayType($aArgs, ['where', 'data']); + ValidatorModel::notEmpty($args, ['where', 'data']); + ValidatorModel::arrayType($args, ['where', 'data']); DatabaseModel::delete([ 'table' => 'resource_contacts', - 'where' => $aArgs['where'], - 'data' => $aArgs['data'] + 'where' => $args['where'], + 'data' => $args['data'] ]); return true; -- GitLab