Newer
Older
<?php
/**
* Copyright Maarch since 2008 under licence GPLv3.
* See LICENCE.txt file at the root folder for more details.
* This file is part of Maarch software.
*/
/**
* @brief Contact Controller
* @author dev@maarch.org
*/
use AcknowledgementReceipt\models\AcknowledgementReceiptModel;
use Attachment\models\AttachmentModel;
use Contact\models\ContactCustomFieldListModel;

Florian Azizian
committed
use Contact\models\ContactGroupModel;
use Contact\models\ContactParameterModel;
use Entity\models\EntityModel;
use Group\controllers\PrivilegeController;
use History\controllers\HistoryController;
use MessageExchange\controllers\AnnuaryController;
use Parameter\models\ParameterModel;
use Resource\controllers\ResController;
use Resource\models\ResModel;
use Resource\models\ResourceContactModel;
use Slim\Http\Request;
use Slim\Http\Response;
use SrcCore\controllers\AutoCompleteController;
use SrcCore\models\CoreConfigModel;
use User\models\UserModel;
const MAPPING_FIELDS = [
'civility' => 'civility',
'firstname' => 'firstname',
'lastname' => 'lastname',
'company' => 'company',
'department' => 'department',
'function' => 'function',
'addressNumber' => 'address_number',
'addressStreet' => 'address_street',
'addressAdditional1' => 'address_additional1',
'addressAdditional2' => 'address_additional2',
'addressPostcode' => 'address_postcode',
'addressTown' => 'address_town',
'addressCountry' => 'address_country',
'email' => 'email',
'phone' => 'phone',
'notes' => 'notes'
];
public function get(Request $request, Response $response)
if (!PrivilegeController::hasPrivilege(['privilegeId' => 'admin_contacts', 'userId' => $GLOBALS['id']])) {
return $response->withStatus(403)->withJson(['errors' => 'Service forbidden']);
}
$queryParams = $request->getQueryParams();
$queryParams['offset'] = (empty($queryParams['offset']) || !is_numeric($queryParams['offset']) ? 0 : (int)$queryParams['offset']);
$queryParams['limit'] = (empty($queryParams['limit']) || !is_numeric($queryParams['limit']) ? 25 : (int)$queryParams['limit']);
$order = !in_array($queryParams['order'], ['asc', 'desc']) ? '' : $queryParams['order'];
$orderBy = !in_array($queryParams['orderBy'], ['firstname', 'lastname', 'company']) ? ['id'] : ["{$queryParams['orderBy']} {$order}", 'id'];
if (!empty($queryParams['search'])) {
$fields = ['firstname', 'lastname', 'company', 'address_number', 'address_street', 'address_additional1', 'address_additional2', 'address_postcode', 'address_town', 'address_country'];
$fieldsNumber = count($fields);
$fields = AutoCompleteController::getUnsensitiveFieldsForRequest(['fields' => $fields]);
$requestData = AutoCompleteController::getDataForRequest([
'search' => $queryParams['search'],
'fields' => $fields,
'where' => [],
'data' => [],
'fieldsNumber' => $fieldsNumber
]);
}
$contacts = ContactModel::get([
'select' => [
'id', 'firstname', 'lastname', 'company', 'address_number as "addressNumber"', 'address_street as "addressStreet"',
'address_additional1 as "addressAdditional1"', 'address_additional2 as "addressAdditional2"', 'address_postcode as "addressPostcode"',
'address_town as "addressTown"', 'address_country as "addressCountry"', 'enabled', 'count(1) OVER()'
],
'where' => $requestData['where'] ?? null,
'data' => $requestData['data'] ?? null,
'offset' => $queryParams['offset'],
'limit' => $queryParams['limit']
]);
$count = $contacts[0]['count'] ?? 0;
foreach ($contacts as $key => $contact) {
unset($contacts[$key]['count']);
$filling = ContactController::getFillingRate(['contactId' => $contact['id']]);
$contacts[$key]['isUsed'] = ContactController::isContactUsed(['id' => $contact['id']]);
$contacts[$key]['filling'] = $filling;
if ($queryParams['orderBy'] == 'filling') {
usort($contacts, function ($a, $b) {
return $a['filling']['rate'] <=> $b['filling']['rate'];
});
if ($queryParams['order'] == 'desc') {
$contacts = array_reverse($contacts);
}
}
return $response->withJson(['contacts' => $contacts, 'count' => $count]);
public function create(Request $request, Response $response)
{
if (!PrivilegeController::hasPrivilege(['privilegeId' => 'create_contacts', 'userId' => $GLOBALS['id']])
&& !PrivilegeController::hasPrivilege(['privilegeId' => 'admin_contacts', 'userId' => $GLOBALS['id']])) {
return $response->withStatus(403)->withJson(['errors' => 'Service forbidden']);
}
$control = ContactController::controlContact(['body' => $body]);
if (!empty($control['errors'])) {
return $response->withStatus(400)->withJson(['errors' => $control['errors']]);
if (!empty($body['email'])) {
$contact = ContactModel::get(['select' => ['id'], 'where' => ['email = ?'], 'data' => [$body['email']]]);
if (!empty($contact[0]['id'])) {
return $response->withJson(['id' => $contact[0]['id']]);
}
if (!empty($body['communicationMeans'])) {
if (filter_var($body['communicationMeans'], FILTER_VALIDATE_EMAIL)) {
$body['communicationMeans'] = ['email' => $body['communicationMeans']];
} elseif (filter_var($body['communicationMeans'], FILTER_VALIDATE_URL)) {
$body['communicationMeans'] = ['url' => $body['communicationMeans']];
} else {
return $response->withStatus(400)->withJson(['errors' => _COMMUNICATION_MEANS_VALIDATOR]);
$annuaryReturn = ContactController::addContactToM2MAnnuary(['body' => $body]);
$body = $annuaryReturn['body'];
if (!empty($body['externalId']) && is_array($body['externalId'])) {
$externalId = json_encode($body['externalId']);
} else {
$externalId = '{}';
}
if (!empty($body['customFields'])) {
foreach ($body['customFields'] as $key => $value) {
$customField = ContactCustomFieldListModel::getById(['id' => $key, 'select' => ['type']]);
if ($customField['type'] == 'date') {
$date = new \DateTime($value);
$value = $date->format('Y-m-d');
$body['customFields'][$key] = $value;
}
}
}
$id = ContactModel::create([
'civility' => $body['civility'] ?? null,
'firstname' => $body['firstname'] ?? null,
'lastname' => $body['lastname'] ?? null,
'company' => $body['company'] ?? null,
'department' => $body['department'] ?? null,
'function' => $body['function'] ?? null,
'address_number' => $body['addressNumber'] ?? null,
'address_street' => $body['addressStreet'] ?? null,
'address_additional1' => $body['addressAdditional1'] ?? null,
'address_additional2' => $body['addressAdditional2'] ?? null,
'address_postcode' => $body['addressPostcode'] ?? null,
'address_town' => $body['addressTown'] ?? null,
'address_country' => $body['addressCountry'] ?? null,
'email' => $body['email'] ?? null,
'phone' => $body['phone'] ?? null,
'communication_means' => !empty($body['communicationMeans']) ? json_encode($body['communicationMeans']) : null,
'notes' => $body['notes'] ?? null,
'creator' => $GLOBALS['id'],
'enabled' => 'true',
'custom_fields' => !empty($body['customFields']) ? json_encode($body['customFields']) : null,
$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',
]);
return $response->withJson(['id' => $id, 'warning' => $annuaryReturn['warning']]);
public function getById(Request $request, Response $response, array $args)
{
if (!Validator::intVal()->validate($args['id'])) {
return $response->withStatus(400)->withJson(['errors' => 'Route id is not an integer']);
}
$rawContact = ContactModel::getById(['id' => $args['id'], 'select' => ['*']]);
if (empty($rawContact)) {
return $response->withStatus(400)->withJson(['errors' => 'Contact does not exist']);
$contact = [
'id' => $rawContact['id'],
'civility' => null,
'firstname' => $rawContact['firstname'],
'lastname' => $rawContact['lastname'],
'company' => $rawContact['company'],
'department' => $rawContact['department'],
'function' => $rawContact['function'],
'addressNumber' => $rawContact['address_number'],
'addressStreet' => $rawContact['address_street'],
'addressAdditional1' => $rawContact['address_additional1'],
'addressAdditional2' => $rawContact['address_additional2'],
'addressPostcode' => $rawContact['address_postcode'],
'addressTown' => $rawContact['address_town'],
'addressCountry' => $rawContact['address_country'],
'email' => $rawContact['email'],
'phone' => $rawContact['phone'],
'notes' => $rawContact['notes'],
'creator' => $rawContact['creator'],
'creatorLabel' => UserModel::getLabelledUserById(['id' => $rawContact['creator']]),
'enabled' => $rawContact['enabled'],
'creationDate' => $rawContact['creation_date'],
'modificationDate' => $rawContact['modification_date'],
'customFields' => !empty($rawContact['custom_fields']) ? json_decode($rawContact['custom_fields'], true) : null,
'externalId' => json_decode($rawContact['external_id'], true)
];
if (!empty($rawContact['civility'])) {
$civilities = ContactModel::getCivilities();
$contact['civility'] = [
'id' => $rawContact['civility'],
'label' => $civilities[$rawContact['civility']]['label'],
'abbreviation' => $civilities[$rawContact['civility']]['abbreviation']
];
}
if (!empty($rawContact['communication_means'])) {
$communicationMeans = json_decode($rawContact['communication_means'], true);
$contact['communicationMeans'] = $communicationMeans['url'] ?? $communicationMeans['email'];
}
$filling = ContactController::getFillingRate(['contactId' => $rawContact['id']]);
$contact['fillingRate'] = empty($filling) ? null : $filling;
public function update(Request $request, Response $response, array $args)
if (!PrivilegeController::hasPrivilege(['privilegeId' => 'update_contacts', 'userId' => $GLOBALS['id']])
&& !PrivilegeController::hasPrivilege(['privilegeId' => 'admin_contacts', 'userId' => $GLOBALS['id']])) {
return $response->withStatus(403)->withJson(['errors' => 'Service forbidden']);
}
if (!Validator::intVal()->validate($args['id'])) {
return $response->withStatus(400)->withJson(['errors' => 'Route id is not an integer']);
}
$body = $request->getParsedBody();
$control = ContactController::controlContact(['body' => $body]);
if (!empty($control['errors'])) {
return $response->withStatus(400)->withJson(['errors' => $control['errors']]);
}
$contact = ContactModel::getById(['id' => $args['id'], 'select' => [1]]);
if (empty($contact)) {
return $response->withStatus(400)->withJson(['errors' => 'Contact does not exist']);
}
if (!empty($body['communicationMeans'])) {
if (filter_var($body['communicationMeans'], FILTER_VALIDATE_EMAIL)) {
$body['communicationMeans'] = ['email' => $body['communicationMeans']];
} elseif (filter_var($body['communicationMeans'], FILTER_VALIDATE_URL)) {
$body['communicationMeans'] = ['url' => $body['communicationMeans']];
}
}
$annuaryReturn = ContactController::addContactToM2MAnnuary(['body' => $body]);
$body = $annuaryReturn['body'];
if (!empty($body['externalId']) && is_array($body['externalId'])) {
$externalId = json_encode($body['externalId']);
} else {
$externalId = '{}';
}
ContactModel::update([
'set' => [
'civility' => $body['civility'] ?? null,
'firstname' => $body['firstname'] ?? null,
'lastname' => $body['lastname'] ?? null,
'company' => $body['company'] ?? null,
'department' => $body['department'] ?? null,
'function' => $body['function'] ?? null,
'address_number' => $body['addressNumber'] ?? null,
'address_street' => $body['addressStreet'] ?? null,
'address_additional1' => $body['addressAdditional1'] ?? null,
'address_additional2' => $body['addressAdditional2'] ?? null,
'address_postcode' => $body['addressPostcode'] ?? null,
'address_town' => $body['addressTown'] ?? null,
'address_country' => $body['addressCountry'] ?? null,
'email' => $body['email'] ?? null,
'phone' => $body['phone'] ?? null,
'communication_means' => !empty($body['communicationMeans']) ? json_encode($body['communicationMeans']) : null,
'notes' => $body['notes'] ?? null,
'modification_date' => 'CURRENT_TIMESTAMP',
'custom_fields' => !empty($body['customFields']) ? json_encode($body['customFields']) : null,
'external_id' => $externalId
],
'where' => ['id = ?'],
'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',
]);
if (!empty($annuaryReturn['warning'])) {
return $response->withJson(['warning' => $annuaryReturn['warning']]);
}
public function addContactToM2MAnnuary($args = [])
{
$warning = '';
$body = $args['body'];
if (!empty($body['externalId']['m2m']) && !empty($body['company']) && empty($body['externalId']['m2m_annuary_id'])) {
if (empty($body['company']) || (empty($body['communicationMeans']['email']) && empty($body['communicationMeans']['url'])) || empty($body['department'])) {
$warning = _CANNOT_SYNCHRONIZE_M2M_ANNUARY;
} else {
$annuaryInfo = AnnuaryController::addContact([
'ouName' => $body['company'],
'communicationValue' => $body['communicationMeans']['email'] ?? $body['communicationMeans']['url'],
'serviceName' => $body['department'],
'm2mId' => $body['externalId']['m2m']
]);
if (!empty($annuaryInfo['errors'])) {
$warning = $annuaryInfo['errors'];
} else {
$body['externalId']['m2m_annuary_id'] = $annuaryInfo['entryUUID'];
}
}
}
return ['body' => $body, 'warning' => $warning];
}
public function updateActivation(Request $request, Response $response, array $args)
{
if (!PrivilegeController::hasPrivilege(['privilegeId' => 'admin_contacts', 'userId' => $GLOBALS['id']])) {
return $response->withStatus(403)->withJson(['errors' => 'Service forbidden']);
}
if (!Validator::intVal()->validate($args['id'])) {
return $response->withStatus(400)->withJson(['errors' => 'Route id is not an integer']);
}
$contact = ContactModel::getById(['id' => $args['id'], 'select' => [1]]);
if (empty($contact)) {
return $response->withStatus(400)->withJson(['errors' => 'Contact does not exist']);
}
$body = $request->getParsedBody();
ContactModel::update([
'set' => ['enabled' => empty($body['enabled']) ? 'false' : 'true'],
'where' => ['id = ?'],
'data' => [$args['id']]
]);
return $response->withStatus(204);
}
public function delete(Request $request, Response $response, array $args)
if (!PrivilegeController::hasPrivilege(['privilegeId' => 'admin_contacts', 'userId' => $GLOBALS['id']])) {
return $response->withStatus(403)->withJson(['errors' => 'Service forbidden']);
}
if (!Validator::intVal()->validate($args['id'])) {
return $response->withStatus(400)->withJson(['errors' => 'Route id is not an integer']);
}
$contact = ContactModel::getById(['id' => $args['id'], 'select' => ['lastname', 'firstname', 'company']]);
if (empty($contact)) {
return $response->withStatus(400)->withJson(['errors' => 'Contact does not exist']);
}
$queryParams = $request->getQueryParams();
if (!empty($queryParams['redirect'])) {
if (!Validator::intVal()->validate($queryParams['redirect'])) {
return $response->withStatus(400)->withJson(['errors' => 'Query param redirect is not an integer']);
} elseif ($queryParams['redirect'] == $args['id']) {
return $response->withStatus(400)->withJson(['errors' => 'Cannot redirect to contact you are deleting']);
}
$contactRedirect = ContactModel::getById(['id' => $queryParams['redirect'], 'select' => [1]]);
if (empty($contactRedirect)) {
return $response->withStatus(400)->withJson(['errors' => 'Contact does not exist']);
}
$resourcesContacts = ResourceContactModel::get([
'select' => ['res_id', 'mode'],
'where' => ['item_id = ?', "type = 'contact'"],
'data' => [$args['id']]
]);
ResourceContactModel::update([
'set' => ['item_id' => $queryParams['redirect']],
'where' => ['item_id = ?', 'type = ?'],
'data' => [$args['id'], 'contact']
]);
// Delete duplicates if needed
$toDelete = [];
foreach ($resourcesContacts as $resourcesContact) {
$resContact = ResourceContactModel::get([
'select' => ['id'],
'where' => ['res_id = ?', 'item_id = ?', 'mode = ?', "type = 'contact'"],
'data' => [$resourcesContact['res_id'], $queryParams['redirect'], $resourcesContact['mode']],
'orderBy' => ['id desc']
]);
$toDelete[] = $resContact[0]['id'];
}
if (!empty($toDelete)) {
ResourceContactModel::delete([
'where' => ['id in (?)'],
'data' => [$toDelete]
]);
}
AcknowledgementReceiptModel::update([
'set' => ['contact_id' => $queryParams['redirect']],
'where' => ['contact_id = ?'],
'data' => [$args['id']]
]);
AttachmentModel::update([
'set' => ['recipient_id' => $queryParams['redirect']],
'where' => ['recipient_id = ?', "recipient_type = 'contact'"],
'data' => [$args['id']]
]);
}
AttachmentModel::update([
'set' => ['recipient_id' => null, 'recipient_type' => null],
'where' => ['recipient_id = ?', "recipient_type = 'contact'"],
'data' => [$args['id']]
]);
ResourceContactModel::delete([
'where' => ['item_id = ?', "type = 'contact'"],
]);
ContactModel::delete([
'where' => ['id = ?'],
'data' => [$args['id']]
]);

Florian Azizian
committed
ContactGroupModel::deleteByContactId(['contactId' => $args['id']]);
$historyInfoContact = '';
if (!empty($contact['firstname']) || !empty($contact['lastname'])) {
$historyInfoContact .= $contact['firstname'] . ' ' . $contact['lastname'];
if (!empty($historyInfoContact) && !empty($contact['company'])) {
$historyInfoContact .= ' (' . $contact['company'] . ')';
} else {
$historyInfoContact .= $contact['company'];
}
HistoryController::add([
'tableName' => 'contacts',
'recordId' => $args['id'],
'eventType' => 'DEL',
'info' => _CONTACT_SUPPRESSION . " : " . trim($historyInfoContact),
'moduleId' => 'contact',
'eventId' => 'contactSuppression',
]);
public function getContactsParameters(Request $request, Response $response)
{
$contactsFilling = ContactFillingModel::get();
$contactParameters = ContactParameterModel::get([
'select' => ['*'],
'orderBy' => ['identifier=\'civility\' desc, identifier=\'firstname\' desc, identifier=\'lastname\' desc, identifier=\'company\' desc, identifier=\'department\' desc,
identifier=\'function\' desc, identifier=\'address_number\' desc, identifier=\'address_street\' desc, identifier=\'address_additional1\' desc, identifier=\'address_additional2\' desc,
identifier=\'address_postcode\' desc, identifier=\'address_town\' desc, identifier=\'address_country\' desc, identifier=\'email\' desc, identifier=\'phone\' desc']
]);
foreach ($contactParameters as $key => $parameter) {
if (strpos($parameter['identifier'], 'contactCustomField_') !== false) {
$contactCustomId = str_replace("contactCustomField_", "", $parameter['identifier']);
$customField = ContactCustomFieldListModel::getById(['select' => ['label'], 'id' => $contactCustomId]);
$contactParameters[$key]['label'] = $customField['label'];
} else {
$contactParameters[$key]['label'] = null;
}
}
return $response->withJson(['contactsFilling' => $contactsFilling, 'contactsParameters' => $contactParameters]);
public function updateContactsParameters(Request $request, Response $response)
if (!PrivilegeController::hasPrivilege(['privilegeId' => 'admin_contacts', 'userId' => $GLOBALS['id']])) {
return $response->withStatus(403)->withJson(['errors' => 'Service forbidden']);
}
$data = $request->getParams();
$check = Validator::arrayType()->validate($data['contactsParameters']);
$check = $check && Validator::arrayType()->validate($data['contactsFilling']);
$check = $check && Validator::boolType()->validate($data['contactsFilling']['enable']);
$check = $check && Validator::intVal()->notEmpty()->validate($data['contactsFilling']['first_threshold']) && $data['contactsFilling']['first_threshold'] > 0 && $data['contactsFilling']['first_threshold'] < 99;
$check = $check && Validator::intVal()->notEmpty()->validate($data['contactsFilling']['second_threshold']) && $data['contactsFilling']['second_threshold'] > 1 && $data['contactsFilling']['second_threshold'] < 100;
$check = $check && $data['contactsFilling']['first_threshold'] < $data['contactsFilling']['second_threshold'];
if (!$check) {
return $response->withStatus(400)->withJson(['errors' => 'Bad Request']);
}
foreach ($data['contactsParameters'] as $contactParameter) {
unset($contactParameter['label']);
ContactParameterModel::update([
'set' => [
'mandatory' => empty($contactParameter['mandatory']) ? 'false' : 'true',
'filling' => empty($contactParameter['filling']) ? 'false' : 'true',
'searchable' => empty($contactParameter['searchable']) ? 'false' : 'true',
'displayable' => empty($contactParameter['displayable']) ? 'false' : 'true',
],
'where' => ['id = ?'],
'data' => [$contactParameter['id']]
]);
}
ContactFillingModel::update($data['contactsFilling']);
return $response->withJson(['success' => 'success']);
}
public function getByResId(Request $request, Response $response, array $args)
if (!Validator::intVal()->validate($args['resId']) || !ResController::hasRightByResId(['resId' => [$args['resId']], 'userId' => $GLOBALS['id']])) {
return $response->withStatus(403)->withJson(['errors' => 'Document out of perimeter']);
}
$resource = ResModel::getById(['select' => ['res_id'], 'resId' => $args['resId']]);
if (empty($resource)) {
return $response->withStatus(404)->withJson(['errors' => 'Document does not exist']);
}
$queryParams = $request->getQueryParams();
$contacts = [];
if ($queryParams['type'] == 'senders') {
$contacts = ContactController::getParsedContacts(['resId' => $resource['res_id'], 'mode' => 'sender']);
} elseif ($queryParams['type'] == 'recipients') {
$contacts = ContactController::getParsedContacts(['resId' => $resource['res_id'], 'mode' => 'recipient']);
}
return $response->withJson(['contacts' => $contacts]);
}
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
public static function getLightFormattedContact(Request $request, Response $response, array $args)
{
if (!Validator::intVal()->notEmpty()->validate($args['id'])) {
return $response->withStatus(400)->withJson(['errors' => 'Query params id is not an integer']);
}
if ($args['type'] == 'contact') {
$contact = ContactModel::getById([
'select' => [
'firstname', 'lastname', 'company', 'address_number as "addressNumber"', 'address_street as "addressStreet"',
'address_postcode as "addressPostcode"', 'address_town as "addressTown"', 'address_country as "addressCountry"'],
'id' => $args['id']
]);
} elseif ($args['type'] == 'user') {
$contact = UserModel::getById(['id' => $args['id'], 'select' => ['firstname', 'lastname']]);
} elseif ($args['type'] == 'entity') {
$contact = EntityModel::getById(['id' => $args['id'], 'select' => ['entity_label as label']]);
}
if (empty($contact)) {
return $response->withStatus(400)->withJson(['errors' => 'Contact does not exist']);
}
return $response->withJson(['contact' => $contact]);
}
public static function getCivilities(Request $request, Response $response)
{
$civilities = ContactModel::getCivilities();
return $response->withJson(['civilities' => $civilities]);
}
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
public static function getFormattedContactsForSearchV1(Request $request, Response $response)
{
$data = $request->getParsedBody();
$return = '';
if (!isset($data['resId']) && !isset($data['mode'])) {
$status = 1;
$return .= '<td colspan="6" style="background-color: red;">';
$return .= '<p style="padding: 10px; color: black;">';
$return .= 'Erreur lors du chargement des contacts';
$return .= '</p>';
$return .= '</td>';
return $response->withJson(['status' => $status, 'toShow' => $return]);
}
$status = 0;
$return .= '<td>';
$return .= '<div align="center">';
$return .= '<table width="100%">';
$resourceContacts = ResourceContactModel::get([
'where' => ['res_id = ?', 'mode = ?'],
'data' => [$data['resId'], $data['mode']]
]);
$mode = '';
if ($data['mode'] == 'sender') {
$mode = _SENDER;
} elseif ($data['mode'] == 'recipient') {
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
$mode = _RECIPIENT;
}
foreach ($resourceContacts as $resourceContact) {
$return .= '<tr>';
$return .= '<td style="background: transparent; border: 0px dashed rgb(200, 200, 200);">';
$return .= '<div style="text-align: left; background-color: rgb(230, 230, 230); padding: 3px; margin-left: 20px; margin-top: -6px;">';
if ($resourceContact['type'] == 'contact') {
$contactRaw = ContactModel::getById([
'select' => ['*'],
'id' => $resourceContact['item_id']
]);
$contactToDisplay = ContactController::getFormattedContactWithAddress(['contact' => $contactRaw]);
$return .= '<span style="font-size:10px;color:#135F7F;">' . $mode . '</span> - ';
$return .= $contactToDisplay['contact']['otherInfo'];
} elseif ($resourceContact['type'] == 'user') {
$return .= '<span style="font-size:10px;color:#135F7F;">' . $mode . ' (interne)</span> - ';
$return .= UserModel::getLabelledUserById(['id' => $resourceContact['item_id']]);
} elseif ($resourceContact['type'] == 'entity') {
$return .= '<span style="font-size:10px;color:#135F7F;">' . $mode . ' (interne)</span> - ';
$entity = EntityModel::getById(['id' => $resourceContact['item_id'], 'select' => ['entity_label']]);
$return .= $entity['entity_label'];
}
$return .= '</div>';
$return .= '</td>';
$return .= '</tr>';
}
$return .= '</table>';
$return .= '<br />';
$return .= '</div>';
$return .= '</td>';
return $response->withJson(['status' => $status, 'toShow' => $return]);
}
public static function getFillingRate(array $aArgs)
{
ValidatorModel::notEmpty($aArgs, ['contactId']);
ValidatorModel::intVal($aArgs, ['contactId']);
$contactsFilling = ContactFillingModel::get();
$contactsParameters = ContactParameterModel::get(['select' => ['identifier'], 'where' => ['filling = ?'], 'data' => ['true']]);
if ($contactsFilling['enable'] && !empty($contactsParameters)) {
$contactRaw = ContactModel::getById([
'select' => [
'civility', 'firstname', 'lastname', 'company', 'department', 'function', 'address_number as "addressNumber"', 'address_street as "addressStreet"',
'address_additional1 as "addressAdditional1"', 'address_additional2 as "addressAdditional2"', 'address_postcode as "addressPostcode"',
'address_town as "addressTown"', 'address_country as "addressCountry"', 'email', 'phone', 'custom_fields'
],
'id' => $aArgs['contactId']
]);
$customFields = json_decode($contactRaw['custom_fields'], true);
foreach ($contactsParameters as $ratingColumn) {
if (strpos($ratingColumn['identifier'], 'contactCustomField_') !== false && !empty($customFields[str_replace("contactCustomField_", "", $ratingColumn['identifier'])])) {
$percent++;
} elseif (!empty($contactRaw[$ratingColumn['identifier']])) {
$percent = $percent * 100 / count($contactsParameters);
if ($percent <= $contactsFilling['first_threshold']) {
$thresholdLevel = 'first';
} elseif ($percent <= $contactsFilling['second_threshold']) {
$thresholdLevel = 'second';
$thresholdLevel = 'third';
return ['rate' => round($percent, 2), 'thresholdLevel' => $thresholdLevel];
public static function getContactAfnor(array $args)
{
$afnorAddress = ['Afnor',
'',
'',
'',
'',
'',
''
];
if (!empty($args['company'])) {
$afnorAddress[1] = trim(substr($args['company'], 0, 38));
// Ligne 2
if (!empty($args['civility']) || !empty($args['firstname']) || !empty($args['lastname'])) {
$afnorAddress[2] = ContactController::controlLengthNameAfnor([
'civility' => $args['civility'],
'fullName' => $args['firstname'].' '.$args['lastname'],
'strMaxLength' => 38
]);
$afnorAddress[2] = trim($afnorAddress[2]);
if (!empty($args['address_additional1'])) {
$afnorAddress[3] = trim(substr($args['address_additional1'], 0, 38));
if (!empty($args['address_number'])) {
$args['address_number'] = TextFormatModel::normalize(['string' => $args['address_number']]);
$args['address_number'] = preg_replace('/[^\w]/s', ' ', $args['address_number']);
$args['address_number'] = strtoupper($args['address_number']);
if (!empty($args['address_street'])) {
$args['address_street'] = TextFormatModel::normalize(['string' => $args['address_street']]);
$args['address_street'] = preg_replace('/[^\w]/s', ' ', $args['address_street']);
$args['address_street'] = strtoupper($args['address_street']);
$afnorAddress[4] = trim(substr($args['address_number'].' '.$args['address_street'], 0, 38));
if (!empty($args['address_additional2'])) {
$afnorAddress[5] = trim(substr($args['address_additional2'], 0, 38));
$args['address_postcode'] = strtoupper($args['address_postcode']);
$args['address_town'] = strtoupper($args['address_town']);
$afnorAddress[6] = trim(substr($args['address_postcode'].' '.$args['address_town'], 0, 38));
public static function controlLengthNameAfnor(array $args)
if (strlen($args['civility'].' '.$args['fullName']) > $args['strMaxLength']) {
$args['civility'] = $aCivility[$args['civility']]['abbreviation'];
$args['civility'] = $aCivility[$args['civility']]['label'];
return substr($args['civility'].' '.$args['fullName'], 0, $args['strMaxLength']);
public function getAvailableDepartments(Request $request, Response $response)
$customId = CoreConfigModel::getCustomId();

Alex ORLUC
committed
$referentialDirectory = 'referential/ban/indexes';
if (is_dir("custom/{$customId}/".$referentialDirectory)) {
$customFilesDepartments = scandir("custom/{$customId}/".$referentialDirectory);
if (is_dir($referentialDirectory)) {
$filesDepartments = scandir($referentialDirectory);
$departments = [];
if (!empty($customFilesDepartments)) {
foreach ($customFilesDepartments as $value) {

Alex ORLUC
committed
if ($value != '.' && $value != '..' && is_writable("custom/{$customId}/".$referentialDirectory.'/'.$value)) {
if (!empty($filesDepartments)) {
foreach ($filesDepartments as $value) {

Alex ORLUC
committed
if ($value != '.' && $value != '..' && !in_array($value, $departments) && is_writable($referentialDirectory.'/'.$value)) {
if (empty($departments)) {
return $response->withJson(['departments' => []]);
sort($departments, SORT_NUMERIC);
$defaultDepartment = ParameterModel::getById(['id' => 'defaultDepartment', 'select' => ['param_value_int']]);
return $response->withJson(['departments' => $departments, 'default' => empty($defaultDepartment['param_value_int']) ? null : $defaultDepartment['param_value_int']]);
public static function getParsedContacts(array $args)
ValidatorModel::notEmpty($args, ['resId', 'mode']);
ValidatorModel::intVal($args, ['resId']);
ValidatorModel::stringType($args, ['mode']);
$contacts = [];
$resourceContacts = ResourceContactModel::get([
'where' => ['res_id = ?', 'mode = ?'],
'data' => [$args['resId'], $args['mode']]
]);
foreach ($resourceContacts as $resourceContact) {
$contact = [];
if ($resourceContact['type'] == 'contact') {
$contactRaw = ContactModel::getById([
'select' => ['*'],
'id' => $resourceContact['item_id']
]);
$civilities = ContactModel::getCivilities();
$xmlCivility = $civilities[$contactRaw['civility']];
$civility = [
'id' => $contactRaw['civility'],
'label' => $xmlCivility['label'],
'abbreviation' => $xmlCivility['abbreviation']
];
'type' => 'contact',
'civility' => $civility,
'firstname' => $contactRaw['firstname'],
'lastname' => $contactRaw['lastname'],
'company' => $contactRaw['company'],
'department' => $contactRaw['department'],
'function' => $contactRaw['function'],
'addressNumber' => $contactRaw['address_number'],
'addressStreet' => $contactRaw['address_street'],
'addressAdditional1' => $contactRaw['address_additional1'],
'addressAdditional2' => $contactRaw['address_additional2'],
'addressPostcode' => $contactRaw['address_postcode'],
'addressTown' => $contactRaw['address_town'],
'addressCountry' => $contactRaw['address_country'],
'email' => $contactRaw['email'],
'phone' => $contactRaw['phone'],
'communicationMeans' => !empty($contactRaw['communication_means']) ? json_decode($contactRaw['communication_means']) : null,
'notes' => $contactRaw['notes'],
'creator' => $contactRaw['creator'],
'creatorLabel' => UserModel::getLabelledUserById(['id' => $contactRaw['creator']]),
'enabled' => $contactRaw['enabled'],
'creationDate' => $contactRaw['creation_date'],
'modificationDate' => $contactRaw['modification_date'],
'customFields' => !empty($contactRaw['custom_fields']) ? json_decode($contactRaw['custom_fields'], true) : null,
'externalId' => json_decode($contactRaw['external_id'], true)
$filling = ContactController::getFillingRate(['contactId' => $resourceContact['item_id']]);
} elseif ($resourceContact['type'] == 'user') {
$user = UserModel::getById(['id' => $resourceContact['item_id']]);
$phone = '';
if (!empty($phone) && ($user['id'] == $GLOBALS['id']
|| PrivilegeController::hasPrivilege(['privilegeId' => 'view_personal_data', 'userId' => $GLOBALS['id']]))) {
$phone = $user['phone'];
}
$primaryEntity = UserModel::getPrimaryEntityById(['select' => ['entity_label'], 'id' => $user['id']]);
$userEntities = UserModel::getNonPrimaryEntitiesById(['id' => $user['id']]);
$userEntities = array_column($userEntities, 'entity_label');
$nonPrimaryEntities = implode(', ', $userEntities);
$contact = [
'type' => 'user',
'firstname' => $user['firstname'],
'lastname' => $user['lastname'],
'company' => null,
'department' => $primaryEntity['entity_label'],
'function' => null,
'addressNumber' => null,
'addressStreet' => null,
'addressAdditional1' => $nonPrimaryEntities,
'addressAdditional2' => null,
'addressPostcode' => null,
'addressTown' => null,
'addressCountry' => null,
'email' => $user['mail'],
'phone' => $phone,
'communicationMeans' => null,
'notes' => null,
'creator' => null,
'creatorLabel' => null,
'enabled' => null,
'creationDate' => null,
'modificationDate' => null,
'customFields' => null,
'externalId' => null
} elseif ($resourceContact['type'] == 'entity') {
$entity = EntityModel::getById(['id' => $resourceContact['item_id'], 'select' => ['entity_label', 'email']]);
$contact = [
'type' => 'entity',
'firstname' => null,
'lastname' => $entity['entity_label'],
'company' => null,
'department' => null,
'function' => null,
'addressNumber' => null,
'addressStreet' => null,
'addressAdditional1' => null,
'addressAdditional2' => null,
'addressPostcode' => null,
'addressTown' => null,
'addressCountry' => null,
'email' => $entity['email'],
'phone' => null,
'communicationMeans' => null,
'notes' => null,
'creator' => null,
'creatorLabel' => null,
'enabled' => null,
'creationDate' => null,
'modificationDate' => null,
'customFields' => null,
'externalId' => null