Rev 16768 | 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 Laminas\Log\LoggerInterface;
use Laminas\View\Model\JsonModel;
use LeadersLinked\Form\InterviewFileForm;
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
use LeadersLinked\Library\Functions;
use LeadersLinked\Mapper\RecruitmentSelectionApplicationMapper;
use LeadersLinked\Mapper\RecruitmentSelectionFileMapper;
use LeadersLinked\Mapper\RecruitmentSelectionInterviewMapper;
use LeadersLinked\Mapper\RecruitmentSelectionVacancyMapper;
use LeadersLinked\Mapper\RecruitmentSelectionCandidateMapper;
use LeadersLinked\Model\RecruitmentSelectionApplication;
use Laminas\View\Model\ViewModel;
use LeadersLinked\Form\RecruitmentSelection\RecruitmentSelectionApplicationFileForm;
use LeadersLinked\Model\RecruitmentSelectionCandidate;
use LeadersLinked\Model\RecruitmentSelectionFile;
use LeadersLinked\Model\RecruitmentSelectionVacancy;
class RecruitmentSelectionFileController 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()
{
$data = [
'success' => false,
'data' => 'ERROR_METHOD_NOT_ALLOWED'
];
return new JsonModel($data);
}
public function addAction()
{
$request = $this->getRequest();
$currentUserPlugin = $this->plugin('currentUserPlugin');
$currentCompany = $currentUserPlugin->getCompany();
$currentUser = $currentUserPlugin->getUser();
$request = $this->getRequest();
$application_id = $this->params()->fromRoute('application_id');
$vacancy_uuid = $this->params()->fromRoute('vacancy_id');
$vacancyMapper = RecruitmentSelectionVacancyMapper::getInstance($this->adapter);
$vacancy = $vacancyMapper->fetchOneByUuid($vacancy_uuid);
if(!$vacancy) {
$data = [
'success' => false,
'data' => 'ERROR_VACANCY_NOT_FOUND'
];
return new JsonModel($data);
}
if($vacancy->company_id != $currentCompany->id) {
$data = [
'success' => false,
'data' => 'ERROR_VACANCY_IS_OTHER_COMPANY',
];
return new JsonModel($data);
}
$applicationMapper = RecruitmentSelectionApplicationMapper::getInstance($this->adapter);
$application = $applicationMapper->fetchOneByUuid($application_id);
if (!$application) {
$data = [
'success' => false,
'data' => 'ERROR_RECORD_NOT_FOUND'
];
return new JsonModel($data);
}
if ($application->vacancy_id != $vacancy->id) {
return new JsonModel([
'success' => false,
'data' => 'ERROR_UNAUTHORIZED'
]);
}
if($request->isPost()) {
$form = new RecruitmentSelectionApplicationFileForm();
$dataPost = array_merge(
$request->getPost()->toArray(),
$request->getFiles()->toArray(),
);
$form->setData($dataPost);
if($form->isValid()) {
$dataPost = (array) $form->getData();
$files = $this->getRequest()->getFiles()->toArray();
//if(!isset($files['file']) && empty($files['file']['error'])) {
$original_filename = Functions::normalizeStringFilename($files['file']['name']);
$file = new RecruitmentSelectionFile();
$file->name = $dataPost['name'];
$file->company_id = $currentCompany->id;
$file->application_id = $application->id;
$file->vacancy_id = $vacancy->id;
$file->file = $original_filename;
$fileMapper = RecruitmentSelectionFileMapper::getInstance($this->adapter);
if($fileMapper->insert($file)) {
$target_path = $this->config['leaderslinked.fullpath.recruitment_selection'].$application->uuid;
if(!file_exists($target_path)) {
mkdir($target_path, 0755, true);
}
$target_filename = $target_path . DIRECTORY_SEPARATOR . $original_filename;
if(!move_uploaded_file($files['file']['tmp_name'], $target_filename)) {
$fileMapper->delete($file->id);
$data = [
'success' => false,
'data' => 'ERROR_UPLOAD_FILE'
];
} else {
$this->logger->info('Se agrego el archivo : ' . $file->name . ' (' . $original_filename . ')', ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
$data = [
'success' => true,
'data' => [
'message' => 'LABEL_RECORD_ADDED',
'files' => $this->renderFiles($vacancy, $application)
],
];
}
} 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 {
$data = [
'success' => false,
'data' => 'ERROR_METHOD_NOT_ALLOWED'
];
return new JsonModel($data);
}
}
public function deleteAction()
{
$request = $this->getRequest();
$currentUserPlugin = $this->plugin('currentUserPlugin');
$currentCompany = $currentUserPlugin->getCompany();
$currentUser = $currentUserPlugin->getUser();
$request = $this->getRequest();
$application_id = $this->params()->fromRoute('application_id');
$vacancy_uuid = $this->params()->fromRoute('vacancy_id');
$id = $this->params()->fromRoute('id');
$vacancyMapper = RecruitmentSelectionVacancyMapper::getInstance($this->adapter);
$vacancy = $vacancyMapper->fetchOneByUuid($vacancy_uuid);
if(!$vacancy) {
$data = [
'success' => false,
'data' => 'ERROR_VACANCY_NOT_FOUND'
];
return new JsonModel($data);
}
if($vacancy->company_id != $currentCompany->id) {
$data = [
'success' => false,
'data' => 'ERROR_VACANCY_IS_OTHER_COMPANY',
];
return new JsonModel($data);
}
$applicationMapper = RecruitmentSelectionApplicationMapper::getInstance($this->adapter);
$application = $applicationMapper->fetchOneByUuid($application_id);
if (!$application) {
$data = [
'success' => false,
'data' => 'ERROR_RECORD_NOT_FOUND'
];
return new JsonModel($data);
}
if ($application->vacancy_id != $vacancy->id) {
return new JsonModel([
'success' => false,
'data' => 'ERROR_UNAUTHORIZED'
]);
}
$fileMapper = RecruitmentSelectionFileMapper::getInstance($this->adapter);
$file = $fileMapper->fetchOneByUuid($id);
if (!$file) {
$data = [
'success' => false,
'data' => 'ERROR_FILE_NOT_FOUND'
];
return new JsonModel($data);
}
if ($file->application_id != $application->id) {
return new JsonModel([
'success' => false,
'data' => 'ERROR_UNAUTHORIZED'
]);
}
if ($request->isPost()) {
$target_path = $this->config['leaderslinked.fullpath.recruitment_selection'].$application->uuid;
if(!file_exists($target_path)) {
mkdir($target_path, 0755, true);
}
Functions::deleteFilename($target_path, $file->file);
$result = $fileMapper->delete($file->id);
if ($result) {
$this->logger->info('Se borro el archivo : ' . $file->name , ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
$data = [
'success' => true,
'data' => [
'message' => 'LABEL_FILE_DELETED',
'files' => $this->renderFiles($vacancy, $application),
]
];
} 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);
}
public function viewAction()
{
$request = $this->getRequest();
$currentUserPlugin = $this->plugin('currentUserPlugin');
$currentCompany = $currentUserPlugin->getCompany();
$currentUser = $currentUserPlugin->getUser();
$request = $this->getRequest();
$application_id = $this->params()->fromRoute('application_id');
$vacancy_uuid = $this->params()->fromRoute('vacancy_id');
$id = $this->params()->fromRoute('id');
$vacancyMapper = RecruitmentSelectionVacancyMapper::getInstance($this->adapter);
$vacancy = $vacancyMapper->fetchOneByUuid($vacancy_uuid);
if(!$vacancy) {
$data = [
'success' => false,
'data' => 'ERROR_VACANCY_NOT_FOUND'
];
return new JsonModel($data);
}
if($vacancy->company_id != $currentCompany->id) {
$data = [
'success' => false,
'data' => 'ERROR_VACANCY_IS_OTHER_COMPANY',
];
return new JsonModel($data);
}
$applicationMapper = RecruitmentSelectionApplicationMapper::getInstance($this->adapter);
$application = $applicationMapper->fetchOneByUuid($application_id);
if (!$application) {
$data = [
'success' => false,
'data' => 'ERROR_RECORD_NOT_FOUND'
];
return new JsonModel($data);
}
if ($application->vacancy_id != $vacancy->id) {
return new JsonModel([
'success' => false,
'data' => 'ERROR_UNAUTHORIZED'
]);
}
$fileMapper = RecruitmentSelectionFileMapper::getInstance($this->adapter);
$file = $fileMapper->fetchOneByUuid($id);
if (!$file) {
$data = [
'success' => false,
'data' => 'ERROR_FILE_NOT_FOUND'
];
return new JsonModel($data);
}
if ($file->application_id != $application->id) {
return new JsonModel([
'success' => false,
'data' => 'ERROR_UNAUTHORIZED'
]);
}
if($request->isGet()) {
$target_path = $this->config['leaderslinked.fullpath.recruitment_selection'].$application->uuid;
if(!file_exists($target_path)) {
mkdir($target_path, 0755, true);
}
$target_filename = $target_path . DIRECTORY_SEPARATOR . $file->file;
$filename = $file->file;
$content = base64_encode(file_get_contents($target_filename));
$data = [
'success' => true,
'data' => [
'basename' => $filename,
'content' => $content
]
];
return new JsonModel($data);
} else {
return new JsonModel([
'success' => false,
'data' => 'ERROR_METHOD_NOT_ALLOWED'
]);
}
}
/**
*
* @param RecruitmentSelectionVacancy $vacany
* @param RecruitmentSelectionApplication $application
* @return array
*/
private function renderFiles($vacancy, $application)
{
$currentUserPlugin = $this->plugin('currentUserPlugin');
$currentCompany = $currentUserPlugin->getCompany();
$currentUser = $currentUserPlugin->getUser();
$acl = $this->getEvent()->getViewModel()->getVariable('acl');
$allowFileDelete = $acl->isAllowed($currentUser->usertype_id, 'recruitment-and-selection/applications/files/delete');
$allowFileView = $acl->isAllowed($currentUser->usertype_id, 'recruitment-and-selection/applications/files/view');
$recruitmentSelectionFileMapper = RecruitmentSelectionFileMapper::getInstance($this->adapter);
$data = [];
$records = $recruitmentSelectionFileMapper->fetchAllByVacancyId($vacancy->id);
foreach($records as $record)
{
array_push($data, [
'name' => $record->name,
'filename' => basename($record->file),
'link_view' => $allowFileView ? $this->url()->fromRoute('recruitment-and-selection/applications/files/view', ['vacancy_id' => $vacancy->uuid, 'application_id' => $application->uuid, 'id' => $record->uuid]) : '',
'link_delete' => $allowFileDelete ? $this->url()->fromRoute('recruitment-and-selection/applications/files/delete', ['vacancy_id' => $vacancy->uuid, 'application_id' => $application->uuid, 'id' => $record->uuid]) : '',
]);
}
return $data;
}
}