Rev 8877 | AutorÃa | Ultima modificación | Ver Log |
<?phpdeclare(strict_types=1);namespace LeadersLinked\Controller;use Laminas\Db\Adapter\AdapterInterface;use LeadersLinked\Library\InterviewPDF;use Laminas\Cache\Storage\Adapter\AbstractAdapter;use Laminas\Mvc\Controller\AbstractActionController;use Laminas\Log\LoggerInterface;use Laminas\View\Model\JsonModel;use LeadersLinked\Form\RecruitmentSelectionInterviewFormForm;use LeadersLinked\Form\InterviewFileForm;use LeadersLinked\Form\JobDescriptionForm;use LeadersLinked\Hydrator\ObjectPropertyHydrator;use LeadersLinked\Library\Functions;use LeadersLinked\Mapper\CompanyMapper;use LeadersLinked\Mapper\CompetencyTypeMapper;use LeadersLinked\Model\JobDescription;use LeadersLinked\Mapper\CompetencyMapper;use LeadersLinked\Mapper\BehaviorCompetencyMapper;use LeadersLinked\Model\BehaviorCompetency;use LeadersLinked\Library\JobPdf;use LeadersLinked\Mapper\JobDescriptionCompetencyMapper;use LeadersLinked\Mapper\JobDescriptionSubordinateMapper;use LeadersLinked\Model\JobDescriptionCompetency;use LeadersLinked\Model\JobDescriptionBehaviorCompetency;use LeadersLinked\Mapper\JobDescriptionBehaviorCompetencyMapper;use LeadersLinked\Model\JobDescriptionSubordinate;use LeadersLinked\Mapper\BehaviorMapper;use LeadersLinked\Model\Behavior;use LeadersLinked\Model\Company;use LeadersLinked\Mapper\RecruitmentSelectionInterviewMapper;use LeadersLinked\Mapper\RecruitmentSelectionInterviewFileMapper;use LeadersLinked\Model\RecruitmentSelectionInterview;use LeadersLinked\Model\RecruitmentSelectionInterviewFile;use LeadersLinked\Mapper\RecruitmentSelectionVacancyMapper;use LeadersLinked\Model\RecruitmentSelectionVacancy;use LeadersLinked\Mapper\RecruitmentSelectionCandidateMapper;use LeadersLinked\Model\RecruitmentSelectionCandidate;use LeadersLinked\Mapper\JobDescriptionMapper;use Laminas\View\Model\ViewModel;use LeadersLinked\Form\RecruitmentSelectionCandidateFormForm;use Laminas\Hydrator\ArraySerializableHydrator;use Laminas\Db\ResultSet\HydratingResultSet;use LeadersLinked\Mapper\QueryMapper;use Laminas\Paginator\Adapter\DbSelect;use Laminas\Paginator\Paginator;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');}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['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);}}