diff --git a/apps/maarch_entreprise/xml/m2m_config.xml b/apps/maarch_entreprise/xml/m2m_config.xml index e2da65f57995b292c333531d36cbfb61cde24be3..eecd51118c844dec015563bd63f7510c92a4e51a 100755 --- a/apps/maarch_entreprise/xml/m2m_config.xml +++ b/apps/maarch_entreprise/xml/m2m_config.xml @@ -8,12 +8,6 @@ <res_attachments> <attachment_type>simple_attachment</attachment_type> </res_attachments> - <contacts_v2> - <contact_type>100</contact_type> - </contacts_v2> - <contact_addresses> - <contact_purpose_id>1</contact_purpose_id> - </contact_addresses> <basketRedirection_afterUpload>NumericBasket</basketRedirection_afterUpload><!--basketId--> <indexingModelId>1</indexingModelId> <m2m_communication>https://cchaplin:maarch@demo.maarchcourrier.com</m2m_communication><!--moyen de communication de l'instance : email ou uri --> diff --git a/migration/19.12/migrateM2MConfiguration.php b/migration/19.12/migrateM2MConfiguration.php index 529c3d8420254f5432a08bf079353c1b26fcc628..94dd0479eee14f90808bd0264fcebd5aadef65e9 100644 --- a/migration/19.12/migrateM2MConfiguration.php +++ b/migration/19.12/migrateM2MConfiguration.php @@ -18,6 +18,8 @@ foreach ($customs as $custom) { if ($xmlfile) { $xmlfile->indexingModelId = 1; + unset($xmlfile->contacts_v2); + unset($xmlfile->contact_addresses); $res = $xmlfile->asXML(); $fp = fopen($path, "w+"); diff --git a/modules/sendmail/Controllers/ReceiveMessageExchangeController.php b/modules/sendmail/Controllers/ReceiveMessageExchangeController.php index 1db6a68175295cfd8d685a423e899a99d1382f58..e0ed77125ee53f0e4c854dcec444db12d63f8e28 100755 --- a/modules/sendmail/Controllers/ReceiveMessageExchangeController.php +++ b/modules/sendmail/Controllers/ReceiveMessageExchangeController.php @@ -15,18 +15,19 @@ namespace Sendmail\Controllers; +use Basket\models\BasketModel; +use Contact\models\ContactModel; +use Entity\models\EntityModel; use Group\controllers\PrivilegeController; +use History\controllers\HistoryController; +use Note\models\NoteModel; use Resource\controllers\StoreController; +use Resource\models\ResModel; +use Resource\models\ResourceContactModel; use Slim\Http\Request; use Slim\Http\Response; -use User\models\UserModel; use SrcCore\models\CoreConfigModel; -use Entity\models\EntityModel; -use Basket\models\BasketModel; -use Resource\models\ResModel; -use Note\models\NoteModel; -use History\controllers\HistoryController; -use Contact\models\ContactModel; +use User\models\UserModel; require_once 'modules/export_seda/Controllers/ReceiveMessage.php'; require_once 'modules/export_seda/Controllers/SendMessage.php'; @@ -240,8 +241,6 @@ class ReceiveMessageExchangeController $destUser = UserModel::getByEmail(['mail' => $email]); } - $contact = $aArgs['contact']; - $dataValue = []; $user = UserModel::getByLogin(['login' => 'superadmin', 'select' => ['id']]); $entityId = EntityModel::getByEntityId(['entityId' => $destination[0]['entity_id'], 'select' => ['id']]); @@ -256,9 +255,6 @@ class ReceiveMessageExchangeController array_push($dataValue, ['priority' => $defaultConfig['priority']]); array_push($dataValue, ['confidentiality' => false]); array_push($dataValue, ['chrono' => true]); - // TODO CONTACT - // array_push($dataValue, ['exp_contact_id' => $contact['contactId']]); - // array_push($dataValue, ['address_id' => $contact['addressId']]); $date = new \DateTime(); array_push($dataValue, ['arrivalDate' => $date->format('d-m-Y H:i')]); array_push($dataValue, ['encodedFile' => $documentMetaData->Attachment->value]); @@ -266,58 +262,69 @@ class ReceiveMessageExchangeController array_push($dataValue, ['status' => $defaultConfig['status']]); array_push($dataValue, ['modelId' => $defaultConfig['indexingModelId']]); - return StoreController::storeResource($dataValue); + $storeResource = StoreController::storeResource($dataValue); + if (!empty($storeResource['errors'])) { + ResourceContactModel::create(['res_id' => $storeResource, 'item_id' => $aArgs['contact']['id'], 'type' => 'contact', 'mode' => 'sender']); + } + + return $storeResource; } protected static function saveContact($aArgs = []) { $dataObject = $aArgs['dataObject']; - $defaultConfigContacts = $aArgs['defaultConfig']['contacts_v2']; - $defaultConfigAddress = $aArgs['defaultConfig']['contact_addresses']; $transferringAgency = $dataObject->TransferringAgency; $transferringAgencyMetadata = $transferringAgency->OrganizationDescriptiveMetadata; - $aDataContact = []; - array_push($aDataContact, ['column' => 'contact_type', 'value' => $defaultConfigContacts['contact_type'], 'type' => 'integer', 'table' => 'contacts_v2']); - array_push($aDataContact, ['column' => 'society', 'value' => $transferringAgencyMetadata->LegalClassification, 'type' => 'string', 'table' => 'contacts_v2']); - array_push($aDataContact, ['column' => 'is_corporate_person', 'value' => 'Y', 'type' => 'string', 'table' => 'contacts_v2']); - array_push($aDataContact, ['column' => 'is_external_contact', 'value' => 'Y', 'type' => 'string', 'table' => 'contacts_v2']); - - array_push($aDataContact, ['column' => 'contact_purpose_id', 'value' => $defaultConfigAddress['contact_purpose_id'], 'type' => 'integer', 'table' => 'contact_addresses']); - array_push($aDataContact, ['column' => 'external_id', 'value' => $transferringAgency->Identifier->value, 'type' => 'string', 'table' => 'contact_addresses']); - array_push($aDataContact, ['column' => 'departement', 'value' => $transferringAgencyMetadata->Name, 'type' => 'string', 'table' => 'contact_addresses']); + if (strrpos($transferringAgencyMetadata->Communication[0]->value, "/rest/") !== false) { + $contactCommunicationValue = substr($transferringAgencyMetadata->Communication[0]->value, 0, strrpos($transferringAgencyMetadata->Communication[0]->value, "/rest/")+1); + } else { + $contactCommunicationValue = $transferringAgencyMetadata->Communication[0]->value; + } + + if (filter_var($contactCommunicationValue, FILTER_VALIDATE_EMAIL)) { + $aCommunicationMeans['email'] = $contactCommunicationValue; + $whereAlreadyExist = "communication_means->>'email' = ?"; + } elseif (filter_var($contactCommunicationValue, FILTER_VALIDATE_URL)) { + $aCommunicationMeans['url'] = $contactCommunicationValue; + $whereAlreadyExist = "communication_means->>'url' = ?"; + } + $dataAlreadyExist = $contactCommunicationValue; $contactAlreadyCreated = ContactModel::get([ - 'select' => ['contact_id', 'ca_id'], - 'where' => ["external_id->>'m2m' = ?"], - 'data' => [$transferringAgency->Identifier->value], + 'select' => ['id', 'communication_means'], + 'where' => ["external_id->>'m2m' = ?", $whereAlreadyExist], + 'data' => [$transferringAgency->Identifier->value, $dataAlreadyExist], 'limit' => 1 ]); - if (!empty($contactAlreadyCreated)) { - $contact['contactId'] = $contactAlreadyCreated[0]['contact_id']; - $contact['addressId'] = $contactAlreadyCreated[0]['ca_id']; - } else { - $contact = ContactModel::createContactM2M(['data' => $aDataContact, 'contactCommunication' => $transferringAgencyMetadata->Communication[0]->value]); - } - $contactCommunicationExisted = ContactModel::getContactCommunication([ - "contactId" => $contact['contactId'] - ]); - $contactCommunication = $transferringAgencyMetadata->Communication; - if (empty($contactCommunicationExisted) && !empty($contactCommunication)) { - foreach ($contactCommunication as $value) { - if (strrpos($value->value, "/rest/") !== false) { - $contactCommunicationValue = substr($value->value, 0, strrpos($value->value, "/rest/")+1); - } else { - $contactCommunicationValue = $value->value; - } - ContactModel::createContactCommunication([ - "contactId" => $contact['contactId'], - "type" => $value->Channel, - "value" => $contactCommunicationValue - ]); + if (!empty($contactAlreadyCreated[0]['id'])) { + $contact = [ + 'id' => $contactAlreadyCreated[0]['id'], + 'returnCode' => (int) 0 + ]; + } else { + $aDataContact = [ + 'company' => $transferringAgencyMetadata->LegalClassification, + 'external_id' => json_encode(['m2m' => $transferringAgency->Identifier->value]), + 'department' => $transferringAgencyMetadata->Name, + 'communication_means' => json_encode($aCommunicationMeans) + ]; + + $contactId = ContactModel::create(['data' => $aDataContact]); + if (empty($contactId)) { + $contact = [ + 'returnCode' => (int) -1, + 'error' => 'Contact creation error', + ]; + } else { + $contact = [ + 'id' => $contactId, + 'returnCode' => (int) 0 + ]; } } + return $contact; } diff --git a/src/app/contact/models/ContactModel.php b/src/app/contact/models/ContactModel.php index 57e1eb073fb26d71fd5df0d1e02c2f5be8124fae..c2f231e757d4c53bff9f3611bf97184afca965b1 100755 --- a/src/app/contact/models/ContactModel.php +++ b/src/app/contact/models/ContactModel.php @@ -124,41 +124,6 @@ class ContactModel } } - public static function getContactIdByCommunicationValue(array $aArgs) - { - ValidatorModel::notEmpty($aArgs, ['communicationValue']); - - $aReturn = DatabaseModel::select([ - 'select' => ['*'], - 'table' => ['contact_communication'], - 'where' => ['value = ?'], - 'data' => [$aArgs['communicationValue']], - ]); - - if (empty($aReturn)) { - return ''; - } else { - return $aReturn[0]; - } - } - - public static function createContactCommunication(array $aArgs) - { - ValidatorModel::notEmpty($aArgs, ['contactId', 'type', 'value']); - ValidatorModel::intVal($aArgs, ['contactId']); - - DatabaseModel::insert([ - 'table' => 'contact_communication', - 'columnsValues' => [ - 'contact_id' => $aArgs['contactId'], - 'type' => $aArgs['type'], - 'value' => trim(trim($aArgs['value']), '/') - ] - ]); - - return true; - } - public static function getLabelledContactWithAddress(array $aArgs) { ValidatorModel::notEmpty($aArgs, ['contactId', 'addressId']); @@ -265,129 +230,4 @@ class ContactModel return ''; } - - public static function createContactM2M(array $aArgs) - { - ValidatorModel::notEmpty($aArgs, ['data', 'contactCommunication']); - - $currentContactId = "0"; - $currentAddressId = "0"; - $formatedDataContact = []; - $formatedDataAddress = []; - - $contact_exists = false; - foreach ($aArgs['data'] as $key => $value) { - // On regarde si le contact existe déjà - if (strtoupper($value['column']) == strtoupper('external_id') && ($value['value'] <> "" || $value['value'] <> null)) { - try { - $res = DatabaseModel::select([ - 'select' => ['contact_id', 'ca_id'], - 'table' => ['contacts'], - 'where' => ["external_id->>'m2m' = ?", 'enabled = ?'], - 'data' => [$value['value'], 'Y'], - ]); - - $res = $res[0]; - if (!empty($res['ca_id'])) { - $contact_exists = true; - $currentContactId = $res['contact_id']; - $currentAddressId = $res['ca_id']; - } else { - $contact_exists = false; - } - } catch (\Exception $e) { - $returnResArray = [ - 'returnCode' => (int) -1, - 'contactId' => '', - 'addressId' => '', - 'contactInfo' => '', - 'error' => 'unknown error: ' . $e->getMessage() - ]; - return $returnResArray; - } - } - - $aArgs['data'][$key]['column'] = strtolower($value['column']); - - if ($value['column'] == 'external_id') { - $formatedDataAddress[$value['column']] = json_encode(['m2m' => $value['value']]); - } elseif ($value['table'] == "contacts_v2") { - $formatedDataContact[$value['column']] = $value['value']; - } elseif ($value['table'] == "contact_addresses") { - $formatedDataAddress[$value['column']] = $value['value']; - } - } - - // Si le contact n'existe pas, on le créé - if (!$contact_exists) { - $contactInfo = ContactModel::getContactIdByCommunicationValue(['communicationValue' => $aArgs['contactCommunication']]); - if (!empty($contactInfo)) { - $currentContactId = $contactInfo['contact_id']; - } else { - try { - $currentContactId = DatabaseModel::getNextSequenceValue(['sequenceId' => 'contact_v2_id_seq']); - $formatedDataContact['user_id'] = 'superadmin'; - $formatedDataContact['entity_id'] = 'SUPERADMIN'; - $formatedDataContact['creation_date'] = 'CURRENT_TIMESTAMP'; - $formatedDataContact['contact_id'] = $currentContactId; - - DatabaseModel::insert([ - 'table' => 'contacts_v2', - 'columnsValues' => $formatedDataContact - ]); - } catch (\Exception $e) { - $returnResArray = [ - 'returnCode' => (int) -1, - 'contactId' => 'ERROR', - 'addressId' => 'ERROR', - 'contactInfo' => '', - 'error' => 'contact creation error : '. $e->getMessage(), - ]; - - return $returnResArray; - } - } - try { - $currentAddressId = DatabaseModel::getNextSequenceValue(['sequenceId' => 'contact_addresses_id_seq']); - $formatedDataAddress['user_id'] = 'superadmin'; - $formatedDataAddress['entity_id'] = 'SUPERADMIN'; - $formatedDataAddress['contact_id'] = $currentContactId; - $formatedDataAddress['id'] = $currentAddressId; - - DatabaseModel::insert([ - 'table' => 'contact_addresses', - 'columnsValues' => $formatedDataAddress - ]); - } catch (\Exception $e) { - $returnResArray = [ - 'returnCode' => (int) -1, - 'contactId' => $currentContactId, - 'addressId' => 'ERROR', - 'contactInfo' => '', - 'error' => 'address creation error : '. $e->getMessage(), - ]; - - return $returnResArray; - } - $returnResArray = [ - 'returnCode' => (int) 0, - 'contactId' => $currentContactId, - 'addressId' => $currentAddressId, - 'contactInfo' => 'contact created and attached to doc ... ', - 'error' => '' - ]; - - return $returnResArray; - } else { - $returnResArray = [ - 'returnCode' => (int) 0, - 'contactId' => $currentContactId, - 'addressId' => $currentAddressId, - 'contactInfo' => 'contact already exist, attached to doc ... ', - 'error' => '' - ]; - - return $returnResArray; - } - } } diff --git a/src/app/resource/models/ResourceContactModel.php b/src/app/resource/models/ResourceContactModel.php index 75abaa8a76e8dd1f03571435c6953e2630571e70..41481fca2044306a577abb2985607a22168962a1 100755 --- a/src/app/resource/models/ResourceContactModel.php +++ b/src/app/resource/models/ResourceContactModel.php @@ -14,12 +14,8 @@ namespace Resource\models; -use Contact\models\ContactModel; -use Entity\models\EntityModel; -use SrcCore\controllers\AutoCompleteController; use SrcCore\models\DatabaseModel; use SrcCore\models\ValidatorModel; -use User\models\UserModel; class ResourceContactModel {