Something went wrong on our end
AutoCompleteController.php 10.31 KiB
<?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 Contact\models\ContactModel;
use Group\models\ServiceModel;
use Respect\Validation\Validator;
use Slim\Http\Request;
use Slim\Http\Response;
use Entity\models\EntityModel;
use SrcCore\models\CoreConfigModel;
use SrcCore\models\TextFormatModel;
use Status\models\StatusModel;
use User\models\UserEntityModel;
use User\models\UserModel;
class AutoCompleteController
{
public static function getContacts(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 = ['contact_type = ?'];
$data = [$data['type']];
foreach ($searchItems as $item) {
$where[] = $fields;
for ($i = 0; $i < 9; $i++) {
$data[] = $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' => $data,
'limit' => 25000
]);
$data = [];
foreach ($contacts as $contact) {
if ($contact['is_corporate_person'] == 'Y') {
$arr = [
'addressId' => $contact['ca_id'],
'contact' => $contact['society'],
'address' => "{$contact['firstname']} {$contact['lastname']}, {$contact['address_num']} {$contact['address_street']} {$contact['address_town']} {$contact['address_postal_code']}",
];
} else {
$arr = [
'addressId' => $contact['ca_id'],
'contact' => "{$contact['contact_firstname']} {$contact['contact_lastname']} {$contact['society']}",
'address' => "{$contact['address_num']} {$contact['address_street']} {$contact['address_town']} {$contact['address_postal_code']}",
];
}
$data[] = $arr;
}
return $response->withJson($data);
}
public static function getUsers(Request $request, Response $response)
{
$excludedUsers = ['superadmin'];
$users = UserModel::get([
'select' => ['user_id', 'firstname', 'lastname'],
'where' => ['enabled = ?', 'status != ?', 'user_id not in (?)'],
'data' => ['Y', 'DEL', $excludedUsers],
'orderBy' => ['lastname']
]);
$data = [];
foreach ($users as $value) {
$primaryEntity = UserModel::getPrimaryEntityByUserId(['userId' => $value['user_id']]);
$data[] = [
'type' => 'user',
'id' => $value['user_id'],
'idToDisplay' => "{$value['firstname']} {$value['lastname']}",
'otherInfo' => $primaryEntity['entity_label']
];
}
return $response->withJson($data);
}
public static function getUsersForAdministration(Request $request, Response $response)
{
if ($GLOBALS['userId'] != 'superadmin') {
$entities = EntityModel::getAllEntitiesByUserId(['userId' => $GLOBALS['userId']]);
$users = UserEntityModel::getUsersByEntities([
'select' => ['DISTINCT users.user_id', 'users.id', 'users.firstname', 'users.lastname'],
'entities' => $entities
]);
$usersNoEntities = UserEntityModel::getUsersWithoutEntities(['select' => ['users.id', 'users.user_id', 'users.firstname', 'users.lastname']]);
$users = array_merge($users, $usersNoEntities);
} else {
$excludedUsers = ['superadmin'];
$users = UserModel::get([
'select' => ['id', 'user_id', 'firstname', 'lastname'],
'where' => ['enabled = ?', 'status != ?', 'user_id not in (?)'],
'data' => ['Y', 'DEL', $excludedUsers],
'orderBy' => ['lastname']
]);
}
$data = [];
foreach ($users as $value) {
$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)
{
$excludedUsers = ['superadmin'];
$users = UserModel::get([
'select' => ['user_id', 'firstname', 'lastname'],
'where' => ['enabled = ?', 'status != ?', 'user_id not in (?)'],
'data' => ['Y', 'DEL', $excludedUsers],
'orderBy' => ['lastname']
]);
$data = [];
foreach ($users as $value) {
if (ServiceModel::hasService(['id' => 'visa_documents', 'userId' => $value['user_id'], 'location' => 'visa', 'type' => 'use'])
|| ServiceModel::hasService(['id' => 'sign_document', 'userId' => $value['user_id'], 'location' => 'visa', 'type' => 'use'])) {
$primaryEntity = UserModel::getPrimaryEntityByUserId(['userId' => $value['user_id']]);
$data[] = [
'type' => 'user',
'id' => $value['user_id'],
'idToDisplay' => "{$value['firstname']} {$value['lastname']}",
'otherInfo' => $primaryEntity['entity_label']
];
}
}
return $response->withJson($data);
}
public static function getEntities(Request $request, Response $response)
{
$entities = EntityModel::get([
'select' => ['entity_id', 'entity_label', 'short_label'],
'where' => ['enabled = ?'],
'data' => ['Y'],
'orderBy' => ['entity_label']
]);
$data = [];
foreach ($entities as $value) {
$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 = [];
foreach ($statuses as $value) {
$data[] = [
'type' => 'status',
'id' => $value['id'],
'idToDisplay' => $value['label_status'],
'otherInfo' => $value['img_filename']
];
}
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) {
if (strlen($value) <= 2 && !is_numeric($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']]));
$addresses = [];
foreach ($hits as $key => $hit) {
$addresses[] = [
'banId' => $hit->banId,
'number' => $hit->streetNumber,
'afnorName' => $hit->afnorName,
'postalCode' => $hit->postalCode,
'city' => $hit->city,
'address' => "{$hit->streetNumber} {$hit->afnorName}, {$hit->city} ({$hit->postalCode})"
];
}
return $response->withJson($addresses);
}
}