Rev 10132 | AutorÃa | Ultima modificación | Ver Log |
<?php
declare(strict_types=1);
namespace LeadersLinked\Controller;
use Laminas\Db\Adapter\AdapterInterface;
use Laminas\Cache\Storage\Adapter\AbstractAdapter;
use Laminas\Mvc\Controller\AbstractActionController;
use Laminas\Log\LoggerInterface;
use Laminas\View\Model\ViewModel;
use Laminas\View\Model\JsonModel;
use LeadersLinked\Form\RecruitmentSelectionCandidateFormForm;
use LeadersLinked\Library\Functions;
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
use LeadersLinked\Mapper\RecruitmentSelectionCandidateMapper;
use LeadersLinked\Model\RecruitmentSelectionCandidate;
use LeadersLinked\Mapper\RecruitmentSelectionVacancyMapper;
use LeadersLinked\Model\RecruitmentSelectionVacancy;
use LeadersLinked\Mapper\BehaviorMapper;
use LeadersLinked\Mapper\UserMapper;
use LeadersLinked\Mapper\CompanyMapper;
use LeadersLinked\Model\Company;
use Laminas\Hydrator\ArraySerializableHydrator;
use Laminas\Db\ResultSet\HydratingResultSet;
use LeadersLinked\Mapper\QueryMapper;
use Laminas\Paginator\Adapter\DbSelect;
use Laminas\Paginator\Paginator;
class RecruitmentSelectionCandidateController extends AbstractActionController{
/**
*
* @var AdapterInterface
*/
private $adapter;
/**
*
* @var AbstractAdapter
*/
private $cache;
/**
*
* @var LoggerInterface
*/
private $logger;
/**
*
* @var array
*/
private $config;
/**
*
* @param AdapterInterface $adapter
* @param AbstractAdapter $cache
* @param LoggerInterface $logger
* @param array $config
*/
public function __construct($adapter, $cache, $logger, $config) {
$this->adapter = $adapter;
$this->cache = $cache;
$this->logger = $logger;
$this->config = $config;
}
public function indexAction() {
$currentUserPlugin = $this->plugin('currentUserPlugin');
$currentUser = $currentUserPlugin->getUser();
$currentCompany = $currentUserPlugin->getCompany();
$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) {
//$form_uuid = filter_var($this->params()->fromQuery('form_uuid'), FILTER_SANITIZE_STRING);
$vacancy_uuid = $this->params()->fromRoute('vacancy_uuid');
$data = [
'items' => [],
'total' => 0,
];
if (!$vacancy_uuid) {
return new JsonModel([
'success' => true,
'data' => $data
]);
}
$vacancyMapper = RecruitmentSelectionVacancyMapper::getInstance($this->adapter);
$vacancy = $vacancyMapper->fetchOneByUuid($vacancy_uuid);
if (! $vacancy) {
return new JsonModel([
'success' => true,
'data' => 'ERROR_FORM_NOT_FOUND'
]);
}
if ( $vacancy->company_id != $currentCompany->id) {
return new JsonModel([
'success' => true,
'data' => 'ERROR_UNAUTHORIZED'
]);
}
$search = $this->params()->fromQuery('search');
$search = empty($search) ? '' : filter_var($search, FILTER_SANITIZE_STRING);
$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(filter_var($order[0]['dir'], FILTER_SANITIZE_STRING));
$fields = ['uuid', 'first_name', 'last_name', 'email', 'status'];
$order_field = isset($fields[$order_field]) ? $fields[$order_field] : 'first_name';
if (!in_array($order_direction, ['ASC', 'DESC'])) {
$order_direction = 'ASC';
}
$acl = $this->getEvent()->getViewModel()->getVariable('acl');
$allowAdd = $acl->isAllowed($currentUser->usertype_id, 'recruitment-and-selection/candidates/add');
$allowEdit = $acl->isAllowed($currentUser->usertype_id, 'recruitment-and-selection/candidates/edit');
$allowDelete = $acl->isAllowed($currentUser->usertype_id, 'recruitment-and-selection/candidates/delete');
$allowEmail = $acl->isAllowed($currentUser->usertype_id, 'recruitment-and-selection/user-by-email');
$queryMapper = QueryMapper::getInstance($this->adapter);
$sql = $queryMapper->getSql();
$select = $sql->select();
$select->columns(['id', 'uuid', 'first_name', 'last_name', 'email', 'file', 'status']);
$select->from(['tb1' => RecruitmentSelectionCandidateMapper::_TABLE]);
$select->where->equalTo('tb1.vacancy_id', $vacancy->id);
if($search) {
$select->where->nest()
->like('first_name', '%' . $search . '%')
->or->like('last_name', '%' . $search . '%')
->or->like('email', '%' . $search . '%')
->unnest();
}
$select->order($order_field . ' ' . $order_direction);
$hydrator = new ArraySerializableHydrator();
$resultset = new HydratingResultSet($hydrator);
$adapter = new DbSelect($select, $sql, $resultset);
$paginator = new Paginator($adapter);
$paginator->setItemCountPerPage($records_x_page);
$paginator->setCurrentPageNumber($page);
$items = [];
$records = $paginator->getCurrentItems();
foreach ($records as $record) {
$params = [
'vacancy_uuid' => $vacancy->uuid,
'id' => $record['uuid'],
];
$link_delete = $this->url()->fromRoute('recruitment-and-selection/candidates/delete', $params);
$link_edit = $this->url()->fromRoute('recruitment-and-selection/candidates/edit', $params);
$item = [
'uuid' => $record['uuid'],
'first_name' => $record['first_name'],
'last_name' => $record['last_name'],
'email' => $record['email'],
'status' => $record['status'],
// 'file' => $this->url()->fromRoute('storage', ['type' => 'recruitment_selection', 'code' => $record['uuid'], 'filename' => $record['file']]),
'actions' => [
'link_edit' => $allowEdit ? $link_edit : '',
'link_delete' => $allowDelete ? $link_delete : '',
]
];
array_push($items, $item);
}
$data['items'] = $items;
$data['total'] = $paginator->getTotalItemCount();
return new JsonModel([
'success' => true,
'data' => $data
]);
} else {
$form = new RecruitmentSelectionCandidateFormForm($this->adapter, $currentCompany->id);
$vacancyMapper = RecruitmentSelectionVacancyMapper::getInstance($this->adapter);
$vacancies = $vacancyMapper->fetchAllByCompanyId($currentCompany->id);
$this->layout()->setTemplate('layout/layout-backend');
$viewModel = new ViewModel();
$viewModel->setTemplate('leaders-linked/recruitment-and-selection-candidates/index.phtml');
$viewModel->setVariables([
'form' => $form,
'vacancies' => $vacancies,
]);
return $viewModel;
}
} else {
return new JsonModel([
'success' => false,
'data' => 'ERROR_METHOD_NOT_ALLOWED'
]);
;
}
}
public function addAction()
{
$currentUserPlugin = $this->plugin('currentUserPlugin');
$currentCompany = $currentUserPlugin->getCompany();
$currentUser = $currentUserPlugin->getUser();
$request = $this->getRequest();
$vacancy_uuid = $this->params()->fromRoute('vacancy_uuid');
if($request->isPost()) {
$form = new RecruitmentSelectionCandidateFormForm($this->adapter, $currentCompany->id);
$dataPost = array_merge(
$request->getPost()->toArray(),
$request->getFiles()->toArray(),
['form_uuid' => $vacancy_uuid],
);
$form->setData($dataPost);
if($form->isValid()) {
$dataPost = (array) $form->getData();
$files = $this->getRequest()->getFiles()->toArray();
$hydrator = new ObjectPropertyHydrator();
$candidate = new RecruitmentSelectionCandidate();
$hydrator->hydrate($dataPost, $candidate);
$candidate->company_id = $currentCompany->id;
$candidate->file = null;
$vacancyMapper = RecruitmentSelectionVacancyMapper::getInstance($this->adapter);
$vacancy = $vacancyMapper->fetchOneByUuid($vacancy_uuid);
$candidate->vacancy_id = $vacancy->id;
$candidateMapper = RecruitmentSelectionCandidateMapper::getInstance($this->adapter);
if($candidateMapper->insert($candidate)) {
$candidate = $candidateMapper->fetchOne($candidate->id);
//leaderslinked.fullpath.recruitment_selection/uuid vacante/uuid candidato
$target_path = $this->config['leaderslinked.fullpath.recruitment_selection'].$vacancy->uuid.DIRECTORY_SEPARATOR.$candidate->uuid;
if(!file_exists($target_path)) {
mkdir($target_path, 0755, true);
}
$files = $this->getRequest()->getFiles()->toArray();
if(isset($files['file']) && empty($files['file']['error'])) {
$tmp_filename = $files['file']['tmp_name'];
$original_filename = trim(strtolower($files['file']['name']));
try {
$parts = explode('.', $original_filename);
$filename = 'document-' . uniqid() . '.' . $parts[ count($parts) - 1 ];
$full_filename = $target_path . DIRECTORY_SEPARATOR .$filename;
if(move_uploaded_file($tmp_filename, $full_filename)) {
$candidate->file = $filename;
$candidateMapper->update($candidate);
}
} catch(\Throwable $e) {
error_log($e->getTraceAsString());
}
}
$this->logger->info('Se agrego el candidato ' . $candidate->first_name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
$data = [
'success' => true,
'data' => 'LABEL_RECORD_ADDED'
];
} else {
$data = [
'success' => false,
'data' => $candidateMapper->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);
}
}
} else {
$data = [
'success' => false,
'data' => 'ERROR_METHOD_NOT_ALLOWED'
];
return new JsonModel($data);
}
return new JsonModel($data);
}
public function editAction() {
$request = $this->getRequest();
$currentUserPlugin = $this->plugin('currentUserPlugin');
$currentCompany = $currentUserPlugin->getCompany();
$currentUser = $currentUserPlugin->getUser();
$request = $this->getRequest();
$id = $this->params()->fromRoute('id');
if (!$id) {
$data = [
'success' => false,
'data' => 'ERROR_INVALID_PARAMETER'
];
return new JsonModel($data);
}
$recruitmentSelectionCandidateMapper = RecruitmentSelectionCandidateMapper::getInstance($this->adapter);
$candidate = $recruitmentSelectionCandidateMapper->fetchOneByUuid($id);
if (!$candidate) {
$data = [
'success' => false,
'data' => 'ERROR_RECORD_NOT_FOUND'
];
return new JsonModel($data);
}
if ($candidate->company_id != $currentCompany->id) {
return new JsonModel([
'success' => false,
'data' => 'ERROR_UNAUTHORIZED'
]);
}
$vacancyMapper = RecruitmentSelectionVacancyMapper::getInstance($this->adapter);
$vacancy = $vacancyMapper->fetchOne($candidate->vacancy_id);
if ($request->isPost()) {
$vacancy_uuid = $this->params()->fromRoute('vacancy_uuid');
$form = new RecruitmentSelectionCandidateFormForm($this->adapter, $currentCompany->id);
$dataPost = $request->getPost()->toArray();
$dataPost['status'] = isset($dataPost['status']) ? $dataPost['status'] : RecruitmentSelectionCandidate::STATUS_REJECTED;
$dataPost['form_uuid'] = $vacancy_uuid;
$form->setData($dataPost);
if ($form->isValid()) {
$dataPost = (array) $form->getData();
$hydrator = new ObjectPropertyHydrator();
$hydrator->hydrate($dataPost, $candidate);
if($recruitmentSelectionCandidateMapper->update($candidate)) {
$candidate = $recruitmentSelectionCandidateMapper->fetchOne($candidate->id);
//leaderslinked.fullpath.recruitment_selection/uuid vacante/uuid candidato
$target_path = $this->config['leaderslinked.fullpath.recruitment_selection'].$vacancy_uuid.DIRECTORY_SEPARATOR.$candidate->uuid;
if(!file_exists($target_path)) {
mkdir($target_path, 0755, true);
}
$files = $this->getRequest()->getFiles()->toArray();
if(isset($files['file']) && empty($files['file']['error'])) {
$tmp_filename = $files['file']['tmp_name'];
$original_filename = trim(strtolower($files['file']['name']));
try {
$parts = explode('.', $original_filename);
$filename = 'document-' . uniqid() . '.' . $parts[ count($parts) - 1 ];
$full_filename = $target_path . DIRECTORY_SEPARATOR .$filename;
if(move_uploaded_file($tmp_filename, $full_filename)) {
$candidate->file = $filename;
$recruitmentSelectionCandidateMapper->update($candidate);
}
} catch(\Throwable $e) {
error_log($e->getTraceAsString());
}
}
$this->logger->info('Se agrego el candidato ' . $candidate->first_name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
$data = [
'success' => true,
'data' => 'LABEL_RECORD_UPDATED'
];
} else {
$data = [
'success' => false,
'data' => $recruitmentSelectionCandidateMapper->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()) {
$hydrator = new ObjectPropertyHydrator();
$data = [
'success' => true,
'data' => [
'id' => $candidate->uuid,
'user_id' => $candidate->user_id,
'first_name' => $candidate->first_name,
'last_name' => $candidate->last_name,
'email' => $candidate->email,
'file' => $this->url()->fromRoute('storage', ['type' => 'recruitment-selection', 'code'=> $vacancy->uuid, 'code2'=> $candidate->uuid, 'filename' => $candidate->file]),
'evaluation' => $candidate->evaluation,
'coment' => strip_tags($candidate->coment),
'status' => $candidate->status,
]
];
return new JsonModel($data);
} else {
$data = [
'success' => false,
'data' => 'ERROR_METHOD_NOT_ALLOWED'
];
return new JsonModel($data);
}
return new JsonModel($data);
}
public function deleteAction() {
$request = $this->getRequest();
$currentUserPlugin = $this->plugin('currentUserPlugin');
$currentCompany = $currentUserPlugin->getCompany();
$currentUser = $currentUserPlugin->getUser();
$request = $this->getRequest();
$uuid = $this->params()->fromRoute('id');
if (!$uuid) {
$data = [
'success' => false,
'data' => 'ERROR_INVALID_PARAMETER'
];
return new JsonModel($data);
}
$recruitmentSelectionCandidateMapper = RecruitmentSelectionCandidateMapper::getInstance($this->adapter);
$candidateMapper = $recruitmentSelectionCandidateMapper->fetchOneByUuid($uuid);
if (!$candidateMapper) {
$data = [
'success' => false,
'data' => 'ERROR_RECORD_NOT_FOUND'
];
return new JsonModel($data);
}
if ($candidateMapper->company_id != $currentCompany->id) {
return new JsonModel([
'success' => false,
'data' => 'ERROR_UNAUTHORIZED'
]);
}
if ($request->isPost()) {
$result = $recruitmentSelectionCandidateMapper->delete($candidateMapper->id);
if ($result) {
$this->logger->info('Se borro el candidato ' . $candidateMapper->first_name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
$data = [
'success' => true,
'data' => 'LABEL_RECORD_DELETED'
];
} else {
$data = [
'success' => false,
'data' => $recruitmentSelectionCandidateMapper->getError()
];
return new JsonModel($data);
}
} else {
$data = [
'success' => false,
'data' => 'ERROR_METHOD_NOT_ALLOWED'
];
return new JsonModel($data);
}
return new JsonModel($data);
}
public function emailAction() {
$request = $this->getRequest();
$currentUserPlugin = $this->plugin('currentUserPlugin');
$currentCompany = $currentUserPlugin->getCompany();
$currentUser = $currentUserPlugin->getUser();
$email = $request->getQuery('email');
if(!$email){
$data = [
'success' => false,
'data' => 'ERROR_INVALID_PARAMETER'
];
return new JsonModel($data);
}
$userMapper = UserMapper::getInstance($this->adapter);
$user = $userMapper->fetchOneByEmail($email);
if (!$user) {
return new JsonModel([
'success' => false,
'data' => 'ERROR_USER_NOT_FOUND',
]);
} else if($request->isGet()){
$data = [
'success' => true,
'data' => [
'user_id' => $user->id,
'first_name' => $user->first_name,
'last_name' => $user->last_name,
]
];
return new JsonModel($data);
}else {
$data = [
'success' => false,
'data' => 'ERROR_METHOD_NOT_ALLOWED'
];
return new JsonModel($data);
}
}
}