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 Auto Complete Controller
* @author dev@maarch.org
*/
namespace SrcCore\controllers;
use Slim\Http\Request;
use Slim\Http\Response;
use Entity\models\EntityModel;
use Status\models\StatusModel;
use User\models\UserModel;
class AutoCompleteController
{
public static function getContacts(Request $request, Response $response)
{
$data = $request->getQueryParams();
$check = Validator::stringType()->notEmpty()->validate($data['search']);
if (!$check) {
return $response->withStatus(400)->withJson(['errors' => 'Bad Request']);
}
$searchItems = explode(' ', $data['search']);
$fields = '(contact_firstname ilike ? OR contact_lastname ilike ? OR firstname ilike ? OR lastname ilike ? OR society ilike ?
OR address_num ilike ? OR address_street ilike ? OR address_town ilike ? OR address_postal_code ilike ?)';
if (strlen($item) >= 2) {
$where[] = $fields;
for ($i = 0; $i < 9; $i++) {
$color = (!empty($data['color']) && $data['color'] == 'true');
$autocompleteData = [];
$autocompleteData[] = AutoCompleteController::getFormattedContact(['contact' => $contact, 'color' => $color])['contact'];
return $response->withJson($autocompleteData);
public static function getUsers(Request $request, Response $response)
{
$check = Validator::stringType()->notEmpty()->validate($data['search']);
if (!$check) {
return $response->withStatus(400)->withJson(['errors' => 'Bad Request']);
}
$requestData = AutoCompleteController::getDataForRequest([
'search' => $data['search'],
'fields' => '(firstname ilike ? OR lastname ilike ?)',
'where' => ['enabled = ?', 'status != ?', 'user_id not in (?)'],
'data' => ['Y', 'DEL', $excludedUsers],
'fieldsNumber' => 2,
]);
$users = UserModel::get([
'select' => ['user_id', 'firstname', 'lastname'],
'where' => $requestData['where'],
'data' => $requestData['data'],
'orderBy' => ['lastname'],
'limit' => self::LIMIT
$data[] = [
'type' => 'user',
'id' => $value['user_id'],
'idToDisplay' => "{$value['firstname']} {$value['lastname']}",
];
}
return $response->withJson($data);
}
public static function getContactsAndUsers(Request $request, Response $response)
{
$data = $request->getQueryParams();
$check = Validator::stringType()->notEmpty()->validate($data['search']);
if (!$check) {
return $response->withStatus(400)->withJson(['errors' => 'Bad Request']);
}
$searchItems = explode(' ', $data['search']);
$fields = '(contact_firstname ilike ? OR contact_lastname ilike ? OR firstname ilike ? OR lastname ilike ? OR society ilike ?
OR address_num ilike ? OR address_street ilike ? OR address_town ilike ? OR address_postal_code ilike ?)';
$where = [];
$requestData = [];
foreach ($searchItems as $item) {
if (strlen($item) >= 2) {
$where[] = $fields;
for ($i = 0; $i < 9; $i++) {
$requestData[] = "%{$item}%";
}
}
}
$contacts = ContactModel::getOnView([
'where' => $where,
'data' => $requestData,
$color = (!empty($data['color']) && $data['color'] == 'true');
$onlyContacts = [];
$autocompleteData = [];
foreach ($contacts as $contact) {
if (!empty($data['onlyContacts']) && $data['onlyContacts'] == 'true' && !in_array($contact['contact_id'], $onlyContacts)) {
$autocompleteData[] = AutoCompleteController::getFormattedOnlyContact(['contact' => $contact])['contact'];
$onlyContacts[] = $contact['contact_id'];
}
$autocompleteData[] = AutoCompleteController::getFormattedContact(['contact' => $contact, 'color' => $color])['contact'];
}
$excludedUsers = ['superadmin'];
$requestData = AutoCompleteController::getDataForRequest([
'search' => $data['search'],
'fields' => '(firstname ilike ? OR lastname ilike ?)',
'where' => ['enabled = ?', 'status != ?', 'user_id not in (?)'],
'data' => ['Y', 'DEL', $excludedUsers],
'fieldsNumber' => 2,
]);
$users = UserModel::get([
'select' => ['id', 'user_id', 'firstname', 'lastname'],
'where' => $requestData['where'],
'data' => $requestData['data'],
'orderBy' => ['lastname'],
]);
foreach ($users as $value) {
$autocompleteData[] = [
'type' => 'user',
'id' => $value['id'],
'idToDisplay' => "{$value['firstname']} {$value['lastname']}",
'otherInfo' => "{$value['firstname']} {$value['lastname']}"
];
}
return $response->withJson($autocompleteData);
}
public static function getUsersForAdministration(Request $request, Response $response)
{
$data = $request->getQueryParams();
$check = Validator::stringType()->notEmpty()->validate($data['search']);
if (!$check) {
return $response->withStatus(400)->withJson(['errors' => 'Bad Request']);
}
$excludedUsers = ['superadmin'];
if ($GLOBALS['userId'] != 'superadmin') {
$entities = EntityModel::getAllEntitiesByUserId(['userId' => $GLOBALS['userId']]);
$requestData = AutoCompleteController::getDataForRequest([
'search' => $data['search'],
'fields' => '(users.firstname ilike ? OR users.lastname ilike ?)',
'where' => [
'users.user_id = users_entities.user_id',
'users_entities.entity_id in (?)',
'users.status != ?',
'users.enabled = ?'
],
'data' => [$entities, 'DEL', 'Y'],
'fieldsNumber' => 2,
]);
$users = DatabaseModel::select([
'select' => ['DISTINCT users.user_id', 'users.id', 'users.firstname', 'users.lastname'],
'table' => ['users, users_entities'],
'where' => $requestData['where'],
'data' => $requestData['data'],
'limit' => self::LIMIT
$requestData = AutoCompleteController::getDataForRequest([
'search' => $data['search'],
'fields' => '(users.firstname ilike ? OR users.lastname ilike ?)',
'where' => [
'users_entities IS NULL',
'users.user_id not in (?)',
'users.status != ?',
'users.enabled = ?'
],
'data' => [$excludedUsers, 'DEL', 'Y'],
'fieldsNumber' => 2,
]);
$usersNoEntities = DatabaseModel::select([
'select' => ['users.id', 'users.user_id', 'users.firstname', 'users.lastname'],
'table' => ['users', 'users_entities'],
'left_join' => ['users.user_id = users_entities.user_id'],
'where' => $requestData['where'],
'data' => $requestData['data'],
'limit' => (self::LIMIT - count($users))
]);
$users = array_merge($users, $usersNoEntities);
}
$requestData = AutoCompleteController::getDataForRequest([
'search' => $data['search'],
'fields' => '(firstname ilike ? OR lastname ilike ?)',
'where' => ['enabled = ?', 'status != ?', 'user_id not in (?)'],
'data' => ['Y', 'DEL', $excludedUsers],
'fieldsNumber' => 2,
]);
$users = UserModel::get([
'select' => ['id', 'user_id', 'firstname', 'lastname'],
'where' => $requestData['where'],
'data' => $requestData['data'],
'orderBy' => ['lastname'],
'limit' => self::LIMIT
]);
}
$data = [];
$data[] = [
'type' => 'user',
'id' => $value['id'],
'idToDisplay' => "{$value['firstname']} {$value['lastname']}",
'otherInfo' => $value['user_id']
];
}
return $response->withJson($data);
}
public static function getUsersForVisa(Request $request, Response $response)
{
$data = $request->getQueryParams();
$check = Validator::stringType()->notEmpty()->validate($data['search']);
if (!$check) {
return $response->withStatus(400)->withJson(['errors' => 'Bad Request']);
}
$requestData = AutoCompleteController::getDataForRequest([
'search' => $data['search'],
'fields' => '(users.firstname ilike ? OR users.lastname ilike ?)',
'where' => [
'usergroup_content.group_id = usergroups_services.group_id',
'usergroup_content.user_id = users.user_id',
'usergroups_services.service_id in (?)',
'users.user_id not in (?)',
'users.enabled = ?',
'users.status != ?'
],
'data' => [['visa_documents', 'sign_document'], $excludedUsers, 'Y', 'DEL'],
'fieldsNumber' => 2,
]);
$users = DatabaseModel::select([
'select' => ['DISTINCT users.user_id', 'users.firstname', 'users.lastname'],
'table' => ['users, usergroup_content, usergroups_services'],
'where' => $requestData['where'],
'data' => $requestData['data'],
'order_by' => ['users.lastname'],
'limit' => self::LIMIT
foreach ($users as $key => $value) {
$data[] = [
'type' => 'user',
'id' => $value['user_id'],
'idToDisplay' => "{$value['firstname']} {$value['lastname']}",
'otherInfo' => ''
];
}
return $response->withJson($data);
}
public static function getEntities(Request $request, Response $response)
{
$data = $request->getQueryParams();
$check = Validator::stringType()->notEmpty()->validate($data['search']);
if (!$check) {
return $response->withStatus(400)->withJson(['errors' => 'Bad Request']);
}
$requestData = AutoCompleteController::getDataForRequest([
'search' => $data['search'],
'fields' => '(entity_label ilike ?)',
'where' => ['enabled = ?'],
'data' => ['Y'],
'fieldsNumber' => 1,
]);
'select' => ['id', 'entity_id', 'entity_label', 'short_label'],
'where' => $requestData['where'],
'data' => $requestData['data'],
'orderBy' => ['entity_label'],
'limit' => self::LIMIT
$data[] = [
'type' => 'entity',
'id' => $value['entity_id'],
'idToDisplay' => $value['entity_label'],
'otherInfo' => $value['short_label']
];
}
return $response->withJson($data);
}
public static function getStatuses(Request $request, Response $response)
{
$statuses = StatusModel::get(['select' => ['id', 'label_status', 'img_filename']]);
$data[] = [
'type' => 'status',
'id' => $value['id'],
'idToDisplay' => $value['label_status'],
'otherInfo' => $value['img_filename']
];
}
return $response->withJson($data);
}
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
public static function getContactsForGroups(Request $request, Response $response)
{
$data = $request->getQueryParams();
$check = Validator::stringType()->notEmpty()->validate($data['search']);
$check = $check && Validator::stringType()->notEmpty()->validate($data['type']);
if (!$check) {
return $response->withStatus(400)->withJson(['errors' => 'Bad Request']);
}
$searchItems = explode(' ', $data['search']);
$fields = '(contact_firstname ilike ? OR contact_lastname ilike ? OR firstname ilike ? OR lastname ilike ? OR society ilike ?
OR address_num ilike ? OR address_street ilike ? OR address_town ilike ? OR address_postal_code ilike ?)';
$where = [];
$requestData = [];
if ($data['type'] != 'all') {
$where = ['contact_type = ?'];
$requestData = [$data['type']];
}
foreach ($searchItems as $item) {
if (strlen($item) >= 2) {
$where[] = $fields;
for ($i = 0; $i < 9; $i++) {
$requestData[] = "%{$item}%";
}
}
}
$contacts = ContactModel::getOnView([
'select' => [
'ca_id', 'firstname', 'lastname', 'contact_lastname', 'contact_firstname', 'society', 'address_num',
'address_street', 'address_town', 'address_postal_code', 'is_corporate_person'
],
'where' => $where,
'data' => $requestData,
'limit' => 1000
]);
$data = [];
foreach ($contacts as $contact) {
$data[] = ContactGroupController::getFormattedContact(['contact' => $contact])['contact'];
}
return $response->withJson($data);
}
public static function getBanAddresses(Request $request, Response $response)
{
$data = $request->getQueryParams();
$check = Validator::stringType()->notEmpty()->validate($data['address']);
$check = $check && Validator::stringType()->notEmpty()->validate($data['department']);
if (!$check) {
return $response->withStatus(400)->withJson(['errors' => 'Bad Request']);
}
$customId = CoreConfigModel::getCustomId();
if (is_dir("custom/{$customId}/referential/ban/indexes/{$data['department']}")) {
$path = "custom/{$customId}/referential/ban/indexes/{$data['department']}";
} elseif (is_dir('referential/ban/indexes/' . $data['department'])) {
$path = 'referential/ban/indexes/' . $data['department'];
} else {
return $response->withStatus(400)->withJson(['errors' => 'Department indexes do not exist']);
}
\Zend_Search_Lucene_Analysis_Analyzer::setDefault(new \Zend_Search_Lucene_Analysis_Analyzer_Common_Utf8Num_CaseInsensitive());
\Zend_Search_Lucene_Search_QueryParser::setDefaultOperator(\Zend_Search_Lucene_Search_QueryParser::B_AND);
\Zend_Search_Lucene_Search_QueryParser::setDefaultEncoding('utf-8');
$index = \Zend_Search_Lucene::open($path);
\Zend_Search_Lucene::setResultSetLimit(100);
$data['address'] = str_replace(['*', '~', '-', '\''], ' ', $data['address']);
$aAddress = explode(' ', $data['address']);
foreach ($aAddress as $key => $value) {
unset($aAddress[$key]);
continue;
}
if (strlen($value) >= 3 && $value != 'rue' && $value != 'avenue' && $value != 'boulevard') {
$aAddress[$key] .= '*';
}
}
$data['address'] = implode(' ', $aAddress);
if (empty($data['address'])) {
return $response->withJson([]);
}
$hits = $index->find(TextFormatModel::normalize(['string' => $data['address']]));
'banId' => $hit->banId,
'number' => $hit->streetNumber,
'afnorName' => $hit->afnorName,
'postalCode' => $hit->postalCode,
'city' => $hit->city,
'address' => "{$hit->streetNumber} {$hit->afnorName}, {$hit->city} ({$hit->postalCode})"
private static function getDataForRequest(array $aArgs)
{
ValidatorModel::notEmpty($aArgs, ['search', 'fields', 'where', 'data', 'fieldsNumber']);
ValidatorModel::stringType($aArgs, ['search', 'fields']);
ValidatorModel::arrayType($aArgs, ['where', 'data']);
ValidatorModel::intType($aArgs, ['fieldsNumber']);
$searchItems = explode(' ', $aArgs['search']);
foreach ($searchItems as $item) {
if (strlen($item) >= 2) {
$aArgs['where'][] = $aArgs['fields'];
for ($i = 0; $i < $aArgs['fieldsNumber']; $i++) {
$aArgs['data'][] = "%{$item}%";
}
}
}
return ['where' => $aArgs['where'], 'data' => $aArgs['data']];
}
public static function getFormattedContact(array $aArgs)
{
ValidatorModel::notEmpty($aArgs, ['contact']);
ValidatorModel::arrayType($aArgs, ['contact']);
ValidatorModel::boolType($aArgs, ['color']);
if (!empty($aArgs['color'])) {
$rate = ContactController::getFillingRate(['contact' => $aArgs['contact']]);
}
$rateColor = empty($rate['color']) ? '' : $rate['color'];
$address = '';
if ($aArgs['contact']['is_corporate_person'] == 'Y') {
$address.= $aArgs['contact']['firstname'];
$address.= (empty($address) ? $aArgs['contact']['lastname'] : " {$aArgs['contact']['lastname']}");
if (!empty($address)) {
$address.= ', ';
}
if (!empty($aArgs['contact']['address_num'])) {
$address.= $aArgs['contact']['address_num'] . ' ';
}
if (!empty($aArgs['contact']['address_street'])) {
$address.= $aArgs['contact']['address_street'] . ' ';
}
if (!empty($aArgs['contact']['address_town'])) {
$address.= $aArgs['contact']['address_town'] . ' ';
}
if (!empty($aArgs['contact']['address_postal_code'])) {
$address.= $aArgs['contact']['address_postal_code'] . ' ';
}
$otherInfo = empty($address) ? "{$aArgs['contact']['society']}" : "{$aArgs['contact']['society']} - {$address}";
$contact = [
'type' => 'contact',
'id' => $aArgs['contact']['ca_id'],
'contact' => $aArgs['contact']['society'],
'address' => $address,
'idToDisplay' => "{$aArgs['contact']['society']}<br/>{$address}",
];
} else {
if (!empty($aArgs['contact']['address_num'])) {
$address.= $aArgs['contact']['address_num'] . ' ';
}
if (!empty($aArgs['contact']['address_street'])) {
$address.= $aArgs['contact']['address_street'] . ' ';
}
if (!empty($aArgs['contact']['address_town'])) {
$address.= $aArgs['contact']['address_town'] . ' ';
}
if (!empty($aArgs['contact']['address_postal_code'])) {
$address.= $aArgs['contact']['address_postal_code'] . ' ';
}
$contactToDisplay = "{$aArgs['contact']['contact_firstname']} {$aArgs['contact']['contact_lastname']}";
if (!empty($aArgs['contact']['society'])) {
$contactToDisplay .= " ({$aArgs['contact']['society']})";
$otherInfo = empty($address) ? "{$contactToDisplay}" : "{$contactToDisplay} - {$address}";
$contact = [
'type' => 'contact',
'id' => $aArgs['contact']['ca_id'],
'idToDisplay' => "{$contactToDisplay}<br/>{$address}",
];
}
return ['contact' => $contact];
}
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
public static function getFormattedOnlyContact(array $aArgs)
{
ValidatorModel::notEmpty($aArgs, ['contact']);
ValidatorModel::arrayType($aArgs, ['contact']);
if ($aArgs['contact']['is_corporate_person'] == 'Y') {
$contact = [
'type' => 'onlyContact',
'id' => $aArgs['contact']['contact_id'],
'idToDisplay' => $aArgs['contact']['society'],
'otherInfo' => $aArgs['contact']['society'],
'rateColor' => ''
];
} else {
$contactToDisplay = "{$aArgs['contact']['contact_firstname']} {$aArgs['contact']['contact_lastname']}";
if (!empty($aArgs['contact']['society'])) {
$contactToDisplay .= " ({$aArgs['contact']['society']})";
}
$contact = [
'type' => 'onlyContact',
'id' => $aArgs['contact']['contact_id'],
'idToDisplay' => $contactToDisplay,
'otherInfo' => $contactToDisplay,
'rateColor' => ''
];
}
return ['contact' => $contact];
}