Rev 16790 | Ir a la última revisión | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |
<?php
declare(strict_types=1);
namespace LeadersLinked\Controller;
use Laminas\Db\Adapter\AdapterInterface;
use Laminas\Mvc\Controller\AbstractActionController;
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
use Laminas\View\Model\ViewModel;
use Laminas\View\Model\JsonModel;
use LeadersLinked\Library\Functions;
use LeadersLinked\Form\Organization\PositionForm;
use LeadersLinked\Mapper\UserMapper;
use LeadersLinked\Mapper\JobDescriptionMapper;
use LeadersLinked\Mapper\OrganizationPositionMapper;
use LeadersLinked\Model\OrganizationPosition;
use LeadersLinked\Mapper\OrganizationPositionSubordinateMapper;
use LeadersLinked\Form\Organization\DatatableForm;
class OrganizationPositionController extends AbstractActionController
{
/**
*
* @var \Laminas\Db\Adapter\AdapterInterface
*/
private $adapter;
/**
*
* @var \LeadersLinked\Cache\CacheInterface
*/
private $cache;
/**
*
* @var \Laminas\Log\LoggerInterface
*/
private $logger;
/**
*
* @var array
*/
private $config;
/**
*
* @var \Laminas\Mvc\I18n\Translator
*/
private $translator;
/**
*
* @param \Laminas\Db\Adapter\AdapterInterface $adapter
* @param \LeadersLinked\Cache\CacheInterface $cache
* @param \Laminas\Log\LoggerInterface LoggerInterface $logger
* @param array $config
* @param \Laminas\Mvc\I18n\Translator $translator
*/
public function __construct($adapter, $cache, $logger, $config, $translator)
{
$this->adapter = $adapter;
$this->cache = $cache;
$this->logger = $logger;
$this->config = $config;
$this->translator = $translator;
}
public function indexAction()
{
$currentUserPlugin = $this->plugin('currentUserPlugin');
$currentUser = $currentUserPlugin->getUser();
$currentCompany = $currentUserPlugin->getCompany();
$request = $this->getRequest();
$headers = $request->getHeaders();
$request = $this->getRequest();
if($request->isGet()) {
$headers = $request->getHeaders();
$isJson = false;
if($headers->has('Accept')) {
$accept = $headers->get('Accept');
$prioritized = $accept->getPrioritized();
foreach($prioritized as $key => $value) {
$raw = trim($value->getRaw());
if(!$isJson) {
$isJson = strpos($raw, 'json');
}
}
}
if($isJson) {
$job_description_id = Functions::sanitizeFilterString($this->params()->fromQuery('job_description_id'));
if(empty($job_description_id)) {
return new JsonModel([
'success' => false,
'data' => [
'items' => [],
'total' => 0,
'link_add' => ''
]
]);
}
$jobDescriptionMapper = JobDescriptionMapper::getInstance($this->adapter);
$jobDescription = $jobDescriptionMapper->fetchOneByUuid($job_description_id);
if(!$jobDescription) {
return new JsonModel([
'success' => false,
'data' => 'ERROR_JOB_DESCRIPTION_NOT_FOUND'
]);
}
if($jobDescription->company_id != $currentCompany->id) {
return new JsonModel([
'success' => false,
'data' => 'ERROR_UNAUTHORIZED'
]);
}
$search = $this->params()->fromQuery('search', []);
$search = empty($search['value']) ? '' : Functions::sanitizeFilterString($search['value']);
$page = intval($this->params()->fromQuery('start', 1), 10);
$records_x_page = intval($this->params()->fromQuery('length', 10), 10);
$order = $this->params()->fromQuery('order', []);
$order_field = empty($order[0]['column']) ? 99 : intval($order[0]['column'], 10);
$order_direction = empty($order[0]['dir']) ? 'ASC' : strtoupper(Functions::sanitizeFilterString($order[0]['dir']));
$fields = ['user'];
$order_field = isset($fields[$order_field]) ? $fields[$order_field] : 'user';
if(!in_array($order_direction, ['ASC', 'DESC'])) {
$order_direction = 'ASC';
}
$positionMapper = OrganizationPositionMapper::getInstance($this->adapter);
$paginator = $positionMapper->fetchAllDataTableByCompanyIdAndJobDescriptionId($jobDescription->company_id, $jobDescription->id, $search, $page, $records_x_page, $order_field, $order_direction);
$items = [];
$records = $paginator->getCurrentItems();
foreach($records as $record)
{
$item = [
'user' => $record['user'],
'status' => $record['status'],
'actions' => [
'link_edit' => $this->url()->fromRoute('organization/positions/edit', ['id' => $record['uuid'] ]),
'link_delete' => $this->url()->fromRoute('organization/positions/delete', ['id' => $record['uuid'] ]),
'link_subordinates' => $this->url()->fromRoute('organization/positions/subordinates', ['id' => $record['uuid'] ])
]
];
array_push($items, $item);
}
return new JsonModel([
'success' => true,
'data' => [
'items' => $items,
'total' => $paginator->getTotalItemCount(),
'link_add' => $this->url()->fromRoute('organization/positions/add', ['job_description_id' => $jobDescription->uuid ]),
]
]);
} else {
$formDatatable = new DatatableForm($this->adapter, $currentCompany->id);
$formPosition = new PositionForm($this->adapter, $currentCompany->id);
$this->layout()->setTemplate('layout/layout-backend');
$viewModel = new ViewModel();
$viewModel->setTemplate('leaders-linked/organization/positions');
$viewModel->setVariables([
'formDatatable' => $formDatatable,
'formPosition' => $formPosition,
]);
return $viewModel ;
}
} else {
return new JsonModel([
'success' => false,
'data' => 'ERROR_METHOD_NOT_ALLOWED'
]);;
}
}
public function addAction()
{
$currentUserPlugin = $this->plugin('currentUserPlugin');
$currentUser = $currentUserPlugin->getUser();
$currentCompany = $currentUserPlugin->getCompany();
$request = $this->getRequest();
if($request->isPost()) {
$job_description_id = $this->params()->fromRoute('job_description_id');
$jobDescriptionMapper = JobDescriptionMapper::getInstance($this->adapter);
$jobDescription = $jobDescriptionMapper->fetchOneByUuid($job_description_id);
if(!$jobDescription) {
return new JsonModel([
'success' => false,
'data' => 'JOB_DESCRIPTION_NOT_FOUND'
]);
}
if($jobDescription->company_id != $currentCompany->id) {
return new JsonModel([
'success' => false,
'data' => 'ERROR_UNAUTHORIZED'
]);
}
$form = new PositionForm($this->adapter, $currentCompany->id);
$dataPost = $request->getPost()->toArray();
$form->setData($dataPost);
if($form->isValid()) {
$dataPost = (array) $form->getData();
$dataPost['status'] = $dataPost['status'] ? $dataPost['status'] : OrganizationPosition::STATUS_INACTIVE;
$userMapper = UserMapper::getInstance($this->adapter);
$user = $userMapper->fetchOneByUuid($dataPost['user_id']);
$dataPost['user_id'] = $user->id;
$hydrator = new ObjectPropertyHydrator();
$position = new OrganizationPosition();
$position->company_id = $jobDescription->company_id;
$position->job_description_id = $jobDescription->id;
$hydrator->hydrate($dataPost, $position);
$positionMapper = OrganizationPositionMapper::getInstance($this->adapter);
$result = $positionMapper->insert($position);
if($result) {
$this->logger->info('Se agrego la posicion ' . $jobDescription->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
$data = [
'success' => true,
'data' => 'LABEL_RECORD_ADDED'
];
} else {
$data = [
'success' => false,
'data' => $positionMapper->getError()
];
}
return new JsonModel($data);
} else {
$messages = [];
$form_messages = (array) $form->getMessages();
foreach($form_messages as $fieldname => $field_messages)
{
$messages[$fieldname] = array_values($field_messages);
}
return new JsonModel([
'success' => false,
'data' => $messages
]);
}
} else {
$data = [
'success' => false,
'data' => 'ERROR_METHOD_NOT_ALLOWED'
];
return new JsonModel($data);
}
return new JsonModel($data);
}
public function editAction()
{
$currentUserPlugin = $this->plugin('currentUserPlugin');
$currentUser = $currentUserPlugin->getUser();
$currentCompany = $currentUserPlugin->getCompany();
$request = $this->getRequest();
$uuid = $this->params()->fromRoute('id');
if(!$uuid) {
$data = [
'success' => false,
'data' => 'ERROR_INVALID_PARAMETER'
];
return new JsonModel($data);
}
$positionMapper = OrganizationPositionMapper::getInstance($this->adapter);
$position = $positionMapper->fetchOneByUuid($uuid);
if(!$position) {
$data = [
'success' => false,
'data' => 'ERROR_RECORD_NOT_FOUND'
];
return new JsonModel($data);
}
if($position->company_id != $currentCompany->id) {
$data = [
'success' => false,
'data' => 'ERROR_UNAUTHORIZED'
];
return new JsonModel($data);
}
$jobDescriptionMapper = JobDescriptionMapper::getInstance($this->adapter);
$jobDescription = $jobDescriptionMapper->fetchOne($position->job_description_id);
if($request->isPost()) {
$form = new PositionForm($this->adapter, $currentCompany->id);
$dataPost = $request->getPost()->toArray();
$form->setData($dataPost);
if($form->isValid()) {
$dataPost = (array) $form->getData();
$dataPost['status'] = $dataPost['status'] ? $dataPost['status'] : OrganizationPosition::STATUS_INACTIVE;
$userMapper = UserMapper::getInstance($this->adapter);
$user = $userMapper->fetchOneByUuid($dataPost['user_id']);
$dataPost['user_id'] = $user->id;
$hydrator = new ObjectPropertyHydrator();
$hydrator->hydrate($dataPost, $position);
$result = $positionMapper->update($position);
if($result) {
$this->logger->info('Se actualizo la posición ' . $jobDescription->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
$data = [
'success' => true,
'data' => 'LABEL_RECORD_UPDATED'
];
} else {
$data = [
'success' => false,
'data' => $positionMapper->getError()
];
}
return new JsonModel($data);
} else {
$messages = [];
$form_messages = (array) $form->getMessages();
foreach($form_messages as $fieldname => $field_messages)
{
$messages[$fieldname] = array_values($field_messages);
}
return new JsonModel([
'success' => false,
'data' => $messages
]);
}
} else if ($request->isGet()) {
$userMapper = UserMapper::getInstance($this->adapter);
$user = $userMapper->fetchOne($position->user_id);
$jobDescriptionMapper = JobDescriptionMapper::getInstance($this->adapter);
$jobDescription = $jobDescriptionMapper->fetchOne($position->job_description_id);
$hydrator = new ObjectPropertyHydrator();
$data = $hydrator->extract($position);
$data['user_id'] = $user->uuid;
$data['job_description_id'] = $jobDescription->uuid;
$data['users'] = [];
$data['subordinates'] = [];
$ids = [];
$userMapper = UserMapper::getInstance($this->adapter);
$positionSubordinateMapper = OrganizationPositionSubordinateMapper::getInstance($this->adapter);
$subordinates = $positionSubordinateMapper->fetchAllByPositionId($position->id);
foreach($subordinates as $subordinate)
{
$user = $userMapper->fetchOne($subordinate->user_id);
if($user) {
array_push($data['subordinates'], $user->uuid);
}
}
/*
$jobDescriptionSubordinateMapper = JobDescriptionSubordinateMapper::getInstance($this->adapter);
$jobDescriptionSubordinates = $jobDescriptionSubordinateMapper->fetchAllByJobDescriptionIdTopLevel($jobDescription->id);
foreach($jobDescriptionSubordinates as $jobDescriptionSubordinate)
{
$positions = $positionMapper->fetchAllByJobDescriptionIdAndCompanyId($jobDescriptionSubordinate->job_description_id_low_level, $currentCompany->id);
foreach($positions as $position)
{
if(!in_array($position->user_id, $ids)) {
array_push($ids, $position->user_id);
}
}
}*/
if($ids) {
$users = $userMapper->fetchAllByIds($ids);
foreach($users as $user)
{
array_push($data['users'], [
'uuid' => $user->uuid,
'name' => trim(trim($user->first_name) . ' ' . trim($user->last_name)) . ' (' . trim($user->email) . ')',
]);
}
}
$response = [
'success' => true,
'data' => $data
];
return new JsonModel($response);
} else {
$data = [
'success' => false,
'data' => 'ERROR_METHOD_NOT_ALLOWED'
];
return new JsonModel($data);
}
return new JsonModel($data);
}
public function deleteAction()
{
$currentUserPlugin = $this->plugin('currentUserPlugin');
$currentUser = $currentUserPlugin->getUser();
$currentCompany = $currentUserPlugin->getCompany();
$request = $this->getRequest();
$uuid = $this->params()->fromRoute('id');
if(!$uuid) {
$data = [
'success' => false,
'data' => 'ERROR_INVALID_PARAMETER'
];
return new JsonModel($data);
}
$positionMapper = OrganizationPositionMapper::getInstance($this->adapter);
$position = $positionMapper->fetchOneByUuid($uuid);
if(!$position) {
$data = [
'success' => false,
'data' => 'ERROR_RECORD_NOT_FOUND'
];
return new JsonModel($data);
}
if($position->company_id != $currentCompany->id) {
$data = [
'success' => false,
'data' => 'ERROR_UNAUTHORIZED'
];
return new JsonModel($data);
}
if($request->isPost()) {
$result = $positionMapper->delete($position);
if($result) {
$jobDescriptionMapper = JobDescriptionMapper::getInstance($this->adapter);
$jobDescription = $jobDescriptionMapper->fetchOne($position->job_description_id);
$this->logger->info('Se borro la posición ' . $jobDescription->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
$data = [
'success' => true,
'data' => 'LABEL_RECORD_DELETED'
];
} else {
$data = [
'success' => false,
'data' => $positionMapper->getError()
];
return new JsonModel($data);
}
} else {
$data = [
'success' => false,
'data' => 'ERROR_METHOD_NOT_ALLOWED'
];
return new JsonModel($data);
}
return new JsonModel($data);
}
/*
public function subordinatesAction()
{
$currentUserPlugin = $this->plugin('currentUserPlugin');
$currentUser = $currentUserPlugin->getUser();
$currentCompany = $currentUserPlugin->getCompany();
$request = $this->getRequest();
$uuid = $this->params()->fromRoute('job_description_id');
if(!$uuid) {
$data = [
'success' => false,
'data' => 'ERROR_INVALID_PARAMETER'
];
return new JsonModel($data);
}
$jobDescriptionMapper = JobDescriptionMapper::getInstance($this->adapter);
$jobDescription = $jobDescriptionMapper->fetchOneByUuid($uuid);
if(!$jobDescription) {
$data = [
'success' => false,
'data' => 'ERROR_RECORD_NOT_FOUND'
];
return new JsonModel($data);
}
if($jobDescription->company_id != $currentCompany->id) {
$data = [
'success' => false,
'data' => 'ERROR_UNAUTHORIZED'
];
return new JsonModel($data);
}
if ($request->isGet()) {
$ids = [];
$userMapper = UserMapper::getInstance($this->adapter);
$positionMapper = PositionMapper::getInstance($this->adapter);
$jobDescriptionSubordinateMapper = JobDescriptionSubordinateMapper::getInstance($this->adapter);
$jobDescriptionSubordinates = $jobDescriptionSubordinateMapper->fetchAllByJobDescriptionIdTopLevel($jobDescription->id);
foreach($jobDescriptionSubordinates as $jobDescriptionSubordinate)
{
$positions = $positionMapper->fetchAllByJobDescriptionIdAndCompanyId($jobDescriptionSubordinate->job_description_id_low_level, $currentCompany->id);
foreach($positions as $position)
{
if(!in_array($position->user_id, $ids)) {
array_push($ids, $position->user_id);
}
}
}
$data = [];
if($ids) {
$users = $userMapper->fetchAllByIds($ids);
foreach($users as $user)
{
array_push($data, [
'uuid' => $user->uuid,
'name' => trim(trim($user->first_name) . ' ' . trim($user->last_name)) . ' (' . trim($user->email) . ')',
]);
}
}
$response = [
'success' => true,
'data' => $data
];
return new JsonModel($response);
} else {
$data = [
'success' => false,
'data' => 'ERROR_METHOD_NOT_ALLOWED'
];
}
return new JsonModel($data);
}
public function graphAction()
{
$currentUserPlugin = $this->plugin('currentUserPlugin');
$currentUser = $currentUserPlugin->getUser();
$currentCompany = $currentUserPlugin->getCompany();
$request = $this->getRequest();
$id = $this->params()->fromRoute('id');
if(!$id) {
$data = [
'success' => false,
'data' => 'ERROR_INVALID_PARAMETER'
];
return new JsonModel($data);
}
$positionMapper = PositionMapper::getInstance($this->adapter);
$position = $positionMapper->fetchOneByUuid($id);
if(!$position) {
$data = [
'success' => false,
'data' => 'ERROR_RECORD_NOT_FOUND'
];
return new JsonModel($data);
}
if($position->company_id != $currentCompany->id) {
$data = [
'success' => false,
'data' => 'ERROR_UNAUTHORIZED'
];
return new JsonModel($data);
}
if ($request->isGet()) {
$data = [
'success' => false,
'data' => [
'job_description_id' => $position->job_description_id,
'user_id' => $position->user_id,
'status' => $position->status,
]
];
} else {
$data = [
'success' => false,
'data' => 'ERROR_METHOD_NOT_ALLOWED'
];
}
return new JsonModel($data);
}*/
}