Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 494 | Rev 503 | Ir a la última revisión | 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\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\Mapper\QueryMapper;
use Laminas\Db\Sql\Select;
use LeadersLinked\Library\Functions;
use LeadersLinked\Mapper\CompanySelfEvaluationFormMapper;
use LeadersLinked\Mapper\CompanySelfEvaluationTestMapper;
use LeadersLinked\Form\CompanySelfEvaluationTestForm;
use LeadersLinked\Model\CompanySelfEvaluationForm;
use LeadersLinked\Model\CompanySelfEvaluationTest;
use LeadersLinked\Mapper\UserMapper;
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
use LeadersLinked\Library\Pdf;
use LeadersLinked\Mapper\CompanySelfEvaluationFormUserMapper;

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

            if ($isJson) {
                $search = $this->params()->fromQuery('search', []);
                $search = empty($search['value']) ? '' : filter_var($search['value'], FILTER_SANITIZE_STRING);

                $queryMapper = QueryMapper::getInstance($this->adapter);

                $select = $queryMapper->getSql()->select();
                $select->columns(['uuid', 'status', 'user_id','added_on']);
                $select->from(['test' => CompanySelfEvaluationTestMapper::_TABLE]);
                $select->join(['form' => CompanySelfEvaluationFormMapper::_TABLE], 'test.form_id = form.id', ['name', 'language']);
                $select->join(['user' => UserMapper::_TABLE], 'test.user_id = user.id', ['first_name', 'last_name'], Select::JOIN_LEFT_OUTER);

                $select->where->equalTo('form.status', CompanySelfEvaluationForm::STATUS_ACTIVE);
                $select->where->notEqualTo('test.status', CompanySelfEvaluationTest::STATUS_DRAFT);


                //Search items
                if ($search) {
                    $select->where->NEST->like('name', '%' . $search . '%')
                                    ->OR
                                    ->like('first_name', '%' . $search . '%')
                                    ->OR
                                    ->like('last_name', '%' . $search . '%')
                            ->UNNEST;
                }
                $select->order('added_on DESC');

                $records = $queryMapper->fetchAll($select);
                $items = [];

                foreach ($records as $record) {
                    switch ($record['language']) {
                        case CompanySelfEvaluationForm::LANGUAGE_ENGLISH :
                            $language = 'LABEL_ENGLISH';
                            break;

                        case CompanySelfEvaluationForm::LANGUAGE_SPANISH :
                            $language = 'LABEL_SPANISH';
                            break;

                        default :
                            $language = '';
                            break;
                    }

                    switch ($record['status']) {

                        case CompanySelfEvaluationTest::STATUS_COMPLETED :
                            $status = 'LABEL_COMPLETED';
                            break;

                        case CompanySelfEvaluationTest::STATUS_PENDING :
                            $status = 'LABEL_PENDING';
                            break;

                        case CompanySelfEvaluationTest::STATUS_REVIEW :
                            $status = 'LABEL_REVIEW';
                            break;


                        default :
                            $status = 'LABEL_AVAILABLE';
                            break;
                    }

                    $item = [
                        'name' => $record['name'],
                        'user' => $record['first_name'] . ' ' . $record['last_name'],
                        'language' => $language,
                        'status' => $status,
                        'added_on'=>$record['added_on'],
                        'actions'=>[
                            'link_edit' => $record['status'] != CompanySelfEvaluationTest::STATUS_COMPLETED ? $this->url()->fromRoute('self-evaluation/reviews/edit', ['id' => $record['uuid']]) : '',
                            'link_report' => $record['status'] == CompanySelfEvaluationTest::STATUS_COMPLETED ? $this->url()->fromRoute('self-evaluation/reviews/report', ['id' => $record['uuid']]) : '',
                        ]
                       
                    ];

                    array_push($items, $item);
                }

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

                $form = new CompanySelfEvaluationTestForm();

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

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

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

            return new JsonModel($data);
        }

        if ($request->isPost()) {
            $form = new CompanySelfEvaluationTestForm();
            $dataPost = $request->getPost()->toArray();
            $form->setData($dataPost);

            if ($form->isValid()) {

                $selfEvaluationTest = new CompanySelfEvaluationTest();
                $selfEvaluationTest->status = CompanySelfEvaluationTest::STATUS_COMPLETED;
                $selfEvaluationTest->content = $dataPost['content'];

                $result = $companySelfEvaluationTestMapper->update($selfEvaluationTest, $companySelfEvaluationTest->id);

                if ($result) {
                    $data = [
                        'success' => true,
                        'data' => 'LABEL_RECORD_UPDATED'
                    ];
                } else {
                    $data = [
                        'success' => false,
                        'data' => $companySelfEvaluationTestMapper->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();

            //get form data           
            $companySelfEvaluationFormMapper = CompanySelfEvaluationFormMapper::getInstance($this->adapter);
            $companySelfEvaluationForm = $companySelfEvaluationFormMapper->fetchOne($companySelfEvaluationTest->form_id);

            //get user data
            $CompanyUserMapper = UserMapper::getInstance($this->adapter);
            $userMapper = $CompanyUserMapper->fetchOne($companySelfEvaluationTest->user_id);

            if ($companySelfEvaluationForm && $userMapper) {

                $data = [
                    'success' => true,
                    'data' => [
                        'id' => $companySelfEvaluationTest->id,
                        'name' => $companySelfEvaluationForm->name,
                        'text' => $companySelfEvaluationForm->text,
                        'user' => $userMapper->first_name . ' ' . $userMapper->last_name,
                        'status' => $companySelfEvaluationTest->status,
                        'content' => json_decode($companySelfEvaluationTest->content),
                    ]
                ];

                return new JsonModel($data);
            } else {

                $data = [
                    'success' => false,
                    'data' => 'ERROR_METHOD_NOT_ALLOWED'
                ];

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

            return new JsonModel($data);
        }

        return new JsonModel($data);
    }


    public function reportAction() {
        $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);
        }

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

            return new JsonModel($data);
        }

        if ($request->isGet()) {
            $hydrator = new ObjectPropertyHydrator();

            //get form data           
            $companySelfEvaluationFormMapper = CompanySelfEvaluationFormMapper::getInstance($this->adapter);
            $companySelfEvaluationForm = $companySelfEvaluationFormMapper->fetchOne($companySelfEvaluationTest->form_id);

            //get user data
            $CompanyUserMapper = UserMapper::getInstance($this->adapter);
            $userMapper = $CompanyUserMapper->fetchOne($companySelfEvaluationTest->user_id);

            if ($companySelfEvaluationForm && $userMapper) {

                $data = [
                    'success' => true,
                    'data' => [
                        'id' => $companySelfEvaluationTest->id,
                        'name' => $companySelfEvaluationForm->name,
                        'text' => $companySelfEvaluationForm->text,
                        'user' => $userMapper->first_name . ' ' . $userMapper->last_name,
                        'status' => $companySelfEvaluationTest->status,
                        'content' => json_decode($companySelfEvaluationTest->content),
                    ]
                ];

                $pdf = new Pdf();
                $pdf->AliasNbPages();
        $pdf->AddPage();

                $headerFormName = utf8_decode($companySelfEvaluationForm->name);
                $headerUserName = utf8_decode('Informe de Auto-evaluación: ' .trim($userMapper->first_name . ' ' . $userMapper->last_name) . ' al ' . date("m-d-Y H:i:s", strtotime($companySelfEvaluationTest->added_on)) );





                $pdf->pageHeader($headerFormName, $headerUserName);



                $pdf->SetFont('Times','',12);
                for($i=1;$i<=40;$i++)
                    $pdf->Cell(0,10,'Printing line number '.$i,0,1);
                return $pdf->Output();

              
            } else {

                $data = [
                    'success' => false,
                    'data' => 'ERROR_METHOD_NOT_ALLOWED'
                ];

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

            return new JsonModel($data);
        }

        return new JsonModel($data);
    }

}