Proyectos de Subversion LeadersLinked - Backend

Rev

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\Library\Functions;
use LeadersLinked\Mapper\RecruitCandidateMpper;
use LeadersLinked\Form\RecruitCandidateForm;
use LeadersLinked\Model\RecruitmentForm;
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
use LeadersLinked\Mapper\JobDescriptionMapper;
use LeadersLinked\Mapper\JobDescriptionCompetencyMapper;
use LeadersLinked\Mapper\CompetencyMapper;
use LeadersLinked\Mapper\CompetencyTypeMapper;
use LeadersLinked\Mapper\BehaviorCompetencyMapper;
use LeadersLinked\Mapper\BehaviorMapper;
use LeadersLinked\Mapper\JobDescriptionBehaviorCompetencyMapper;
use LeadersLinked\Mapper\CompanyMapper;
use LeadersLinked\Model\Company;

class RecruitmentSelectionFormController 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');
        $currentCompany = $currentUserPlugin->getCompany();
        $currentUser = $currentUserPlugin->getUser();


        $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');
                    }
                }
            }

            //$isJson = true;
            if ($isJson) {
                $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 = ['name'];
                $order_field = isset($fields[$order_field]) ? $fields[$order_field] : 'name';

                if (!in_array($order_direction, ['ASC', 'DESC'])) {
                    $order_direction = 'ASC';
                }

                $recruitCandidateMapper = RecruitCandidateMpper::getInstance($this->adapter);
                $paginator = $recruitCandidateMapper->fetchAllDataTableByCompanyId($currentCompany->id, $search, $page, $records_x_page, $order_field, $order_direction);

                $jobDescriptionMapper = JobDescriptionMapper::getInstance($this->adapter);

                $items = [];
                $records = $paginator->getCurrentItems();
                foreach ($records as $record) {



                    $jobDescription = $jobDescriptionMapper->fetchOne($record->job_description_id);
                    if ($jobDescription) {

                        $item = [
                            'id' => $record->id,
                            'name' => $record->name,
                            'job_description' => $jobDescription->name,
                            'status' => $record->status,
                            'actions' => [
                                'link_edit' => $this->url()->fromRoute('recruit-candidate/forms/edit', ['id' => $record->uuid]),
                                'link_delete' => $this->url()->fromRoute('recruit-candidate/forms/delete', ['id' => $record->uuid])
                            ]
                        ];
                    }

                    array_push($items, $item);
                }

                return new JsonModel([
                    'success' => true,
                    'data' => [
                        'items' => $items,
                        'total' => $paginator->getTotalItemCount(),
                    ]
                ]);
            } else {

                $form = new RecruitCandidateForm($this->adapter, $currentCompany->id);

                $jobDescriptionMapper = JobDescriptionMapper::getInstance($this->adapter);
                $jobsDescription = $jobDescriptionMapper->fetchAllByCompanyId($currentCompany->id);

                $this->layout()->setTemplate('layout/layout-backend');
                $viewModel = new ViewModel();
                $viewModel->setTemplate('leaders-linked/recruit-candidate/index.phtml');
                $viewModel->setVariable('form', $form);
                $viewModel->setVariable('jobsDescription', $jobsDescription);
                return $viewModel;
            }
        } else {
            return new JsonModel([
                'success' => false,
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
            ]);
            ;
        }
    }

    public function addAction() {
        $request = $this->getRequest();
        $currentUserPlugin = $this->plugin('currentUserPlugin');
        $currentCompany = $currentUserPlugin->getCompany();
        $currentUser = $currentUserPlugin->getUser();

        $request = $this->getRequest();


        if ($request->isPost()) {
            $form = new RecruitCandidateForm($this->adapter, $currentCompany->id);
            $dataPost = $request->getPost()->toArray();


            $dataPost['status'] = isset($dataPost['status']) ? $dataPost['status'] : RecruitmentForm::STATUS_INACTIVE;

            $form->setData($dataPost);

            if ($form->isValid()) {


                $dataPost = (array) $form->getData();

                $hydrator = new ObjectPropertyHydrator();
                $recruitmentCandidate = new RecruitmentForm();
                $hydrator->hydrate($dataPost, $recruitmentCandidate);

                if (!$recruitmentCandidate->status) {
                    $recruitmentCandidate->status = RecruitmentForm::STATUS_INACTIVE;
                }
                $recruitmentCandidate->company_id = $currentCompany->id;

                $jobDescriptionMapper = JobDescriptionMapper::getInstance($this->adapter);
                $jobDescription = $jobDescriptionMapper->fetchOneByUuid($dataPost['job_description_id']);
                $recruitmentCandidate->job_description_id = $jobDescription->id;

                $recruitCandidateMapper = RecruitCandidateMpper::getInstance($this->adapter);

                $result = $recruitCandidateMapper->insert($recruitmentCandidate);


                if ($result) {
                    $this->logger->info('Se agrego el candidato al proceso de reclutamiento' . $recruitmentCandidate->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);

                    // Get record by id
                    $record = $recruitCandidateMapper->fetchOne($recruitmentCandidate->id);

                } else {
                    $data = [
                        'success' => false,
                        'data' => $recruitCandidateMapper->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() {
        $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);
        }

        $recruitCandidateMapper = RecruitCandidateMpper::getInstance($this->adapter);
        $recruitmentCandidate = $recruitCandidateMapper->fetchOneByUuid($uuid);
        if (!$recruitmentCandidate) {
            $data = [
                'success' => false,
                'data' => 'ERROR_RECORD_NOT_FOUND'
            ];

            return new JsonModel($data);
        }

        if ($recruitmentCandidate->company_id != $currentCompany->id) {
            return new JsonModel([
                'success' => false,
                'data' => 'ERROR_UNAUTHORIZED'
            ]);
        }


        if ($request->isPost()) {
            $form = new RecruitCandidateForm($this->adapter, $currentCompany->id);
            $dataPost = $request->getPost()->toArray();
            $dataPost['status'] = isset($dataPost['status']) ? $dataPost['status'] : RecruitmentForm::STATUS_INACTIVE;

            $form->setData($dataPost);

            if ($form->isValid()) {
                $dataPost = (array) $form->getData();

                $hydrator = new ObjectPropertyHydrator();
                $hydrator->hydrate($dataPost, $recruitmentCandidate);

                if (!$recruitmentCandidate->status) {
                    $recruitmentCandidate->status = RecruitmentForm::STATUS_INACTIVE;
                }

                $jobDescriptionMapper = JobDescriptionMapper::getInstance($this->adapter);
                $jobDescription = $jobDescriptionMapper->fetchOneByUuid($dataPost['job_description_id']);
                $recruitmentCandidate->job_description_id = $jobDescription->id;

                $result = $recruitCandidateMapper->update($recruitmentCandidate);

                if ($result) {
                    $this->logger->info('Se agrego el candidato' . $recruitmentCandidate->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
                    $data = [
                        'success' => true,
                        'id' => $recruitmentCandidate->id,
                        'action_edit' => $this->url()->fromRoute('recruit-candidate/forms/edit', ['id' => $recruitmentCandidate->uuid]),
                        'data' => 'LABEL_RECORD_UPDATED'
                    ];
                } else {
                    $data = [
                        'success' => false,
                        'data' => $recruitCandidateMapper->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();

            $jobDescriptionMapper = JobDescriptionMapper::getInstance($this->adapter);
            $jobDescription = $jobDescriptionMapper->fetchOne($recruitmentCandidate->job_description_id);
            if (!$jobDescription) {
                $data = [
                    'success' => false,
                    'data' => 'ERROR_METHOD_NOT_ALLOWED'
                ];

                return new JsonModel($data);
            }

            $data = [
                'success' => true,
                'data' => [
                    'id' => $recruitmentCandidate->uuid,
                    'company'=>$recruitmentCandidate->company_id,
                    'name' => $recruitmentCandidate->name,
                    'description' => $recruitmentCandidate->description_id,
                    'category' => $recruitmentCandidate->category_id,
                    'job_description_id' => $jobDescription->uuid,
                    'text' => $recruitmentCandidate->text,
                    'location' => $recruitmentCandidate->location_id,
                    'industries' => $recruitmentCandidate->industries_id, 
                    'status' => $recruitmentCandidate->status,
                    'content' => $recruitmentCandidate->content ? json_decode($recruitmentCandidate->content) : [],
                ]
            ];

            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);
        }

        $recruitCandidateMapper = RecruitCandidateMpper::getInstance($this->adapter);
        $recruitmentCandidate = $recruitCandidateMapper->fetchOneByUuid($uuid);
        if (!$recruitmentCandidate) {
            $data = [
                'success' => false,
                'data' => 'ERROR_RECORD_NOT_FOUND'
            ];

            return new JsonModel($data);
        }

        if ($recruitmentCandidate->company_id != $currentCompany->id) {
            return new JsonModel([
                'success' => false,
                'data' => 'ERROR_UNAUTHORIZED'
            ]);
        }

        if ($request->isPost()) {


            $result = $recruitCandidateMapper->delete($recruitmentCandidate->id);
            if ($result) {
                $this->logger->info('Se borro el formulario de reclutamiento ' . $recruitmentCandidate->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);

                $data = [
                    'success' => true,
                    'data' => 'LABEL_RECORD_DELETED'
                ];
            } else {

                $data = [
                    'success' => false,
                    'data' => $recruitCandidateMapper->getError()
                ];

                return new JsonModel($data);
            }
        } else {
            $data = [
                'success' => false,
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
            ];

            return new JsonModel($data);
        }

        return new JsonModel($data);
    }

   

}