Rev 14918 | 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\JsonModel;
use LeadersLinked\Form\InterviewFileForm;
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
use LeadersLinked\Library\Functions;
use LeadersLinked\Mapper\RecruitmentSelectionInterviewMapper;
use LeadersLinked\Mapper\RecruitmentSelectionInterviewFileMapper;
use LeadersLinked\Model\RecruitmentSelectionInterviewFile;
use LeadersLinked\Mapper\RecruitmentSelectionVacancyMapper;
use LeadersLinked\Mapper\RecruitmentSelectionCandidateMapper;
use Laminas\View\Model\ViewModel;
class RecruitmentSelectionInterviewFileController 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()
{
$request = $this->getRequest();
$currentUserPlugin = $this->plugin('currentUserPlugin');
$currentUser = $currentUserPlugin->getUser();
$currentCompany = $currentUserPlugin->getCompany();
$interview_uuid = $this->params()->fromRoute('interview_uuid');
$flashMessenger = $this->plugin('FlashMessenger');
$interviewMapper = RecruitmentSelectionInterviewMapper::getInstance($this->adapter);
$interview = $interviewMapper->fetchOneByUuid($interview_uuid);
if(!$interview) {
$flashMessenger->addErrorMessage('ERROR_FORM_NOT_FOUND');
return $this->redirect()->toRoute('dashboard');
}
if($interview->company_id != $currentCompany->id) {
$flashMessenger->addErrorMessage('ERROR_UNAUTHORIZED');
return $this->redirect()->toRoute('dashboard');
}
$vacancyMapper = RecruitmentSelectionVacancyMapper::getInstance($this->adapter);
$vacancy = $vacancyMapper->fetchOne($interview->vacancy_id);
$candidateMapper = RecruitmentSelectionCandidateMapper::getInstance($this->adapter);
$candidate = $candidateMapper->fetchOne($interview->candidate_id);
try{
$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) {
$acl = $this->getEvent()->getViewModel()->getVariable('acl');
$allowAdd = $acl->isAllowed($currentUser->usertype_id, 'recruitment-and-selection/interview/file/add');
$allowEdit = $acl->isAllowed($currentUser->usertype_id, 'recruitment-and-selection/interview/file/edit');
$allowDelete = $acl->isAllowed($currentUser->usertype_id, 'recruitment-and-selection/interview/file/delete');
$search = $this->params()->fromQuery('search', []);
$search = empty($search['value']) ? '' : filter_var($search['value'], 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 = ['title'];
$order_field = isset($fields[$order_field]) ? $fields[$order_field] : 'title';
if(!in_array($order_direction, ['ASC', 'DESC'])) {
$order_direction = 'ASC';
}
$fileMapper = RecruitmentSelectionInterviewFileMapper::getInstance($this->adapter);
$paginator = $fileMapper->fetchAllDataTableByInterviewId($interview->id, $search, $page, $records_x_page, $order_field, $order_direction);
$items = [];
$records = $paginator->getCurrentItems();
foreach($records as $record)
{
$params = [
'interview_uuid' => $interview->uuid,
'id' => $record->uuid,
];
$item = [
'interview_uuid' => $interview->uuid,
'id' => $record->uuid,
'title' => $record->title,
'coment' => $record->coment,
'actions' => [
'link_edit' => $allowEdit ? $this->url()->fromRoute('recruitment-and-selection/interview/file/edit', $params) : '',
'link_delete' => $allowDelete ? $this->url()->fromRoute('recruitment-and-selection/interview/file/delete', $params) : '',
],
];
array_push($items, $item);
}
$data = [];
$data['vacancy'] = [
'name' => $vacancy->name
];
$data['candidate'] = [
'full_name' => trim(trim($candidate->first_name) . ' ' . trim($candidate->last_name)),
'email' => $candidate->email,
];
$candidateMapper = RecruitmentSelectionCandidateMapper::getInstance($this->adapter);
$candidate = $candidateMapper->fetchOne($vacancy->id);
$data['items'] = $items;
$data['total'] = $paginator->getTotalItemCount();
$response = [
'success' => true,
'data' => $data
];
return new JsonModel($response);
} else {
$form = new InterviewFileForm($this->adapter, $currentCompany ? $currentCompany->id : null);
$this->layout()->setTemplate('layout/layout-backend.phtml');
$viewModel = new ViewModel();
$viewModel->setTemplate('leaders-linked/recruitment-and-selection-interview-file/index.phtml');
$viewModel->setVariables([
'interview_uuid' => $interview->uuid,
'form' => $form,
]);
return $viewModel ;
}
} else {
return new JsonModel([
'success' => false,
'data' => 'ERROR_METHOD_NOT_ALLOWED'
]);
}
} catch (\Throwable $e) {
$e->getMessage();
return new JsonModel([
'success' => false,
'data' => $e
]);
}
}
public function addAction()
{
$currentUserPlugin = $this->plugin('currentUserPlugin');
$currentCompany = $currentUserPlugin->getCompany();
$currentUser = $currentUserPlugin->getUser();
$request = $this->getRequest();
$interview_id = $this->params()->fromRoute('interview_uuid');
$recruitmentSelectionInterviewMapper = RecruitmentSelectionInterviewMapper::getInstance($this->adapter);
$interview = $recruitmentSelectionInterviewMapper->fetchOneByUuid($interview_id);
try{
if($request->isPost()) {
$form = new InterviewFileForm($this->adapter, $currentCompany->id);
$dataPost = array_merge(
$request->getPost()->toArray(),
$request->getFiles()->toArray(),
);
$form->setData($dataPost);
if($form->isValid()) {
$dataPost = (array) $form->getData();
$files = $this->getRequest()->getFiles()->toArray();
$hydrator = new ObjectPropertyHydrator();
$file = new RecruitmentSelectionInterviewFile();
$hydrator->hydrate($dataPost, $file);
$file->company_id = $currentCompany->id;
$file->interview_id = $interview->id;
$file->file = null;
$fileMapper = RecruitmentSelectionInterviewFileMapper::getInstance($this->adapter);
if($fileMapper->insert($file)) {
$file = $fileMapper->fetchOne($file->id);
//leaderslinked.fullpath.recruitment_selection/uuid vacante/uuid candidato
$target_path = $this->config['leaderslinked.fullpath.recruitment_selection'].$interview_id;
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)) {
$file->file = $filename;
$fileMapper->update($file);
}
} catch(\Throwable $e) {
error_log($e->getTraceAsString());
}
}
$this->logger->info('Se agrego el candidato ' . $file->title, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
$data = [
'success' => true,
'data' => 'LABEL_RECORD_ADDED'
];
} else {
$data = [
'success' => false,
'data' => $fileMapper->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);
}
} catch (\Throwable $e) {
$e->getMessage();
return new JsonModel([
'success' => false,
'data' => $e
]);
}
}
public function editAction() {
$request = $this->getRequest();
$currentUserPlugin = $this->plugin('currentUserPlugin');
$currentCompany = $currentUserPlugin->getCompany();
$currentUser = $currentUserPlugin->getUser();
$request = $this->getRequest();
$id = $this->params()->fromRoute('id');
$interview_id = $this->params()->fromRoute('interview_uuid');
if (!$id) {
$data = [
'success' => false,
'data' => 'ERROR_INVALID_PARAMETER'
];
return new JsonModel($data);
}
$fileMapper = RecruitmentSelectionInterviewFileMapper::getInstance($this->adapter);
$file = $fileMapper->fetchOneByUuid($id);
if (!$file) {
$data = [
'success' => false,
'data' => 'ERROR_RECORD_NOT_FOUND'
];
return new JsonModel($data);
}
if ($file->company_id != $currentCompany->id) {
return new JsonModel([
'success' => false,
'data' => 'ERROR_UNAUTHORIZED'
]);
}
if ($request->isPost()) {
$form = new InterviewFileForm($this->adapter, $currentCompany->id);
$dataPost = $request->getPost()->toArray();
$form->setData($dataPost);
if ($form->isValid()) {
$dataPost = (array) $form->getData();
$hydrator = new ObjectPropertyHydrator();
$hydrator->hydrate($dataPost, $file);
if($fileMapper->update($file)) {
$file = $fileMapper->fetchOne($file->id);
//leaderslinked.fullpath.recruitment_selection/uuid vacante/uuid candidato
$target_path = $this->config['leaderslinked.fullpath.recruitment_selection'].$interview_id;
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)) {
$file->file = $filename;
$fileMapper->update($file);
}
} catch(\Throwable $e) {
error_log($e->getTraceAsString());
}
}
$this->logger->info('Se agrego el archivo ' . $file->title, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
$data = [
'success' => true,
'data' => 'LABEL_RECORD_UPDATED'
];
} else {
$data = [
'success' => false,
'data' => $fileMapper->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' => $file->uuid,
'title' => $file->title,
'coment' => strip_tags($file->coment),
'file' => $this->url()->fromRoute('storage', ['type' => 'recruitment-selection', 'code'=> $interview_id, 'filename' => $file->file]),
]
];
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();
$id = $this->params()->fromRoute('id');
if (!$id) {
$data = [
'success' => false,
'data' => 'ERROR_INVALID_PARAMETER'
];
return new JsonModel($data);
}
$fileMapper = RecruitmentSelectionInterviewFileMapper::getInstance($this->adapter);
$file = $fileMapper->fetchOneByUuid($id);
if (!$file) {
$data = [
'success' => false,
'data' => 'ERROR_RECORD_NOT_FOUND'
];
return new JsonModel($data);
}
if ($file->company_id != $currentCompany->id) {
return new JsonModel([
'success' => false,
'data' => 'ERROR_UNAUTHORIZED'
]);
}
if ($request->isPost()) {
$result = $fileMapper->delete($file->id);
if ($result) {
$this->logger->info('Se borro el archivo ' . $file->title, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
$data = [
'success' => true,
'data' => 'LABEL_RECORD_DELETED'
];
} else {
$data = [
'success' => false,
'data' => $fileMapper->getError()
];
return new JsonModel($data);
}
} else {
$data = [
'success' => false,
'data' => 'ERROR_METHOD_NOT_ALLOWED'
];
return new JsonModel($data);
}
return new JsonModel($data);
}
}