| 244 | geraldo | 1 | <?php
 | 
        
           |  |  | 2 |   | 
        
           |  |  | 3 | declare(strict_types=1);
 | 
        
           |  |  | 4 |   | 
        
           |  |  | 5 | namespace LeadersLinked\Controller;
 | 
        
           |  |  | 6 |   | 
        
           |  |  | 7 | use Laminas\Db\Adapter\AdapterInterface;
 | 
        
           |  |  | 8 | use Laminas\Cache\Storage\Adapter\AbstractAdapter;
 | 
        
           |  |  | 9 | use Laminas\Mvc\Controller\AbstractActionController;
 | 
        
           |  |  | 10 | use Laminas\Log\LoggerInterface;
 | 
        
           |  |  | 11 | use Laminas\View\Model\ViewModel;
 | 
        
           |  |  | 12 | use Laminas\View\Model\JsonModel;
 | 
        
           | 270 | geraldo | 13 | use LeadersLinked\Mapper\QueryMapper;
 | 
        
           | 280 | geraldo | 14 | use Laminas\Db\Sql\Select;
 | 
        
           | 244 | geraldo | 15 | use LeadersLinked\Library\Functions;
 | 
        
           |  |  | 16 | use LeadersLinked\Mapper\CompanySelfEvaluationFormMapper;
 | 
        
           | 260 | geraldo | 17 | use LeadersLinked\Mapper\CompanySelfEvaluationTestMapper;
 | 
        
           | 244 | geraldo | 18 | use LeadersLinked\Form\CompanySelfEvaluationFormForm;
 | 
        
           |  |  | 19 | use LeadersLinked\Model\CompanySelfEvaluationForm;
 | 
        
           | 287 | geraldo | 20 | use LeadersLinked\Model\CompanySelfEvaluationTest;
 | 
        
           | 284 | geraldo | 21 | use LeadersLinked\Mapper\UserMapper;
 | 
        
           | 244 | geraldo | 22 | use LeadersLinked\Hydrator\ObjectPropertyHydrator;
 | 
        
           |  |  | 23 | use LeadersLinked\Mapper\CompanySelfEvaluationFormUserMapper;
 | 
        
           |  |  | 24 |   | 
        
           |  |  | 25 | class SelfEvaluationReviewController extends AbstractActionController {
 | 
        
           |  |  | 26 |   | 
        
           |  |  | 27 |     /**
 | 
        
           |  |  | 28 |      *
 | 
        
           |  |  | 29 |      * @var AdapterInterface
 | 
        
           |  |  | 30 |      */
 | 
        
           |  |  | 31 |     private $adapter;
 | 
        
           |  |  | 32 |   | 
        
           |  |  | 33 |     /**
 | 
        
           |  |  | 34 |      *
 | 
        
           |  |  | 35 |      * @var AbstractAdapter
 | 
        
           |  |  | 36 |      */
 | 
        
           |  |  | 37 |     private $cache;
 | 
        
           |  |  | 38 |   | 
        
           |  |  | 39 |     /**
 | 
        
           |  |  | 40 |      *
 | 
        
           |  |  | 41 |      * @var  LoggerInterface
 | 
        
           |  |  | 42 |      */
 | 
        
           |  |  | 43 |     private $logger;
 | 
        
           |  |  | 44 |   | 
        
           |  |  | 45 |     /**
 | 
        
           |  |  | 46 |      *
 | 
        
           |  |  | 47 |      * @var array
 | 
        
           |  |  | 48 |      */
 | 
        
           |  |  | 49 |     private $config;
 | 
        
           |  |  | 50 |   | 
        
           |  |  | 51 |     /**
 | 
        
           |  |  | 52 |      *
 | 
        
           |  |  | 53 |      * @param AdapterInterface $adapter
 | 
        
           |  |  | 54 |      * @param AbstractAdapter $cache
 | 
        
           |  |  | 55 |      * @param LoggerInterface $logger
 | 
        
           |  |  | 56 |      * @param array $config
 | 
        
           |  |  | 57 |      */
 | 
        
           |  |  | 58 |     public function __construct($adapter, $cache, $logger, $config) {
 | 
        
           |  |  | 59 |         $this->adapter = $adapter;
 | 
        
           |  |  | 60 |         $this->cache = $cache;
 | 
        
           |  |  | 61 |         $this->logger = $logger;
 | 
        
           |  |  | 62 |         $this->config = $config;
 | 
        
           |  |  | 63 |     }
 | 
        
           |  |  | 64 |   | 
        
           |  |  | 65 |     public function indexAction() {
 | 
        
           |  |  | 66 |         $request = $this->getRequest();
 | 
        
           |  |  | 67 |         $currentUserPlugin = $this->plugin('currentUserPlugin');
 | 
        
           |  |  | 68 |         $currentCompany = $currentUserPlugin->getCompany();
 | 
        
           |  |  | 69 |         $currentUser = $currentUserPlugin->getUser();
 | 
        
           |  |  | 70 |   | 
        
           |  |  | 71 |   | 
        
           |  |  | 72 |         $request = $this->getRequest();
 | 
        
           |  |  | 73 |         if ($request->isGet()) {
 | 
        
           |  |  | 74 |   | 
        
           |  |  | 75 |             $headers = $request->getHeaders();
 | 
        
           |  |  | 76 |   | 
        
           |  |  | 77 |             $isJson = false;
 | 
        
           |  |  | 78 |             if ($headers->has('Accept')) {
 | 
        
           |  |  | 79 |                 $accept = $headers->get('Accept');
 | 
        
           |  |  | 80 |   | 
        
           |  |  | 81 |                 $prioritized = $accept->getPrioritized();
 | 
        
           |  |  | 82 |   | 
        
           |  |  | 83 |                 foreach ($prioritized as $key => $value) {
 | 
        
           |  |  | 84 |                     $raw = trim($value->getRaw());
 | 
        
           |  |  | 85 |   | 
        
           |  |  | 86 |                     if (!$isJson) {
 | 
        
           |  |  | 87 |                         $isJson = strpos($raw, 'json');
 | 
        
           |  |  | 88 |                     }
 | 
        
           |  |  | 89 |                 }
 | 
        
           |  |  | 90 |             }
 | 
        
           |  |  | 91 |   | 
        
           |  |  | 92 |             if ($isJson) {
 | 
        
           |  |  | 93 |                 $search = $this->params()->fromQuery('search', []);
 | 
        
           |  |  | 94 |                 $search = empty($search['value']) ? '' : filter_var($search['value'], FILTER_SANITIZE_STRING);
 | 
        
           |  |  | 95 |   | 
        
           | 268 | geraldo | 96 |                 $queryMapper = QueryMapper::getInstance($this->adapter);
 | 
        
           | 244 | geraldo | 97 |   | 
        
           | 268 | geraldo | 98 |                 $select = $queryMapper->getSql()->select();
 | 
        
           | 286 | geraldo | 99 |                 $select->columns(['uuid','status','user_id']);
 | 
        
           | 268 | geraldo | 100 |                 $select->from(['test' => CompanySelfEvaluationTestMapper::_TABLE]);
 | 
        
           | 277 | geraldo | 101 |                 $select->join(['form' => CompanySelfEvaluationFormMapper::_TABLE], 'test.form_id = form.id', ['name','language']);
 | 
        
           | 286 | geraldo | 102 |                 $select->join(['user' => UserMapper::_TABLE], 'test.user_id = user.id', ['first_name','last_name'], Select::JOIN_LEFT_OUTER);
 | 
        
           | 277 | geraldo | 103 |   | 
        
           | 273 | geraldo | 104 |                 $select->where->equalTo('form.status', CompanySelfEvaluationForm::STATUS_ACTIVE);
 | 
        
           | 244 | geraldo | 105 |   | 
        
           | 268 | geraldo | 106 |                 if ($search) {
 | 
        
           |  |  | 107 |                     $select->where->NEST->like('name', '%' . $search . '%');
 | 
        
           | 295 | geraldo | 108 |                     $select->where->NEST->like('first_name', '%' . $search . '%');
 | 
        
           |  |  | 109 |                     $select->where->NEST->like('last_name', '%' . $search . '%');
 | 
        
           | 268 | geraldo | 110 |                 }
 | 
        
           |  |  | 111 |                 $select->order('name ASC, language ASC');
 | 
        
           | 244 | geraldo | 112 |   | 
        
           | 268 | geraldo | 113 |                 $records = $queryMapper->fetchAll($select);
 | 
        
           |  |  | 114 |                 $items = [];
 | 
        
           | 276 | geraldo | 115 |   | 
        
           | 286 | geraldo | 116 |                 foreach ($records as $record) {
 | 
        
           |  |  | 117 |                     switch ($record['language']) {
 | 
        
           |  |  | 118 |                         case CompanySelfEvaluationForm::LANGUAGE_ENGLISH :
 | 
        
           |  |  | 119 |                             $language = 'LABEL_ENGLISH';
 | 
        
           |  |  | 120 |                             break;
 | 
        
           | 244 | geraldo | 121 |   | 
        
           | 286 | geraldo | 122 |                         case CompanySelfEvaluationForm::LANGUAGE_SPANISH :
 | 
        
           |  |  | 123 |                             $language = 'LABEL_SPANISH';
 | 
        
           |  |  | 124 |                             break;
 | 
        
           |  |  | 125 |   | 
        
           |  |  | 126 |                         default :
 | 
        
           |  |  | 127 |                             $language = '';
 | 
        
           |  |  | 128 |                             break;
 | 
        
           |  |  | 129 |                     }
 | 
        
           |  |  | 130 |   | 
        
           |  |  | 131 |                     switch ($record['status']) {
 | 
        
           |  |  | 132 |   | 
        
           |  |  | 133 |                         case CompanySelfEvaluationTest::STATUS_DRAFT :
 | 
        
           |  |  | 134 |                             $status = 'LABEL_DRAFT';
 | 
        
           |  |  | 135 |                             break;
 | 
        
           |  |  | 136 |   | 
        
           |  |  | 137 |                         case CompanySelfEvaluationTest::STATUS_COMPLETED :
 | 
        
           |  |  | 138 |                             $status = 'LABEL_COMPLETED';
 | 
        
           |  |  | 139 |                             break;
 | 
        
           |  |  | 140 |   | 
        
           |  |  | 141 |                         case CompanySelfEvaluationTest::STATUS_PENDING :
 | 
        
           |  |  | 142 |                             $status = 'LABEL_PENDING';
 | 
        
           |  |  | 143 |                             break;
 | 
        
           |  |  | 144 |   | 
        
           |  |  | 145 |                         case CompanySelfEvaluationTest::STATUS_REVIEW :
 | 
        
           |  |  | 146 |                             $status = 'LABEL_REVIEW';
 | 
        
           |  |  | 147 |                             break;
 | 
        
           |  |  | 148 |   | 
        
           |  |  | 149 |   | 
        
           |  |  | 150 |                         default :
 | 
        
           |  |  | 151 |                             $status = 'LABEL_AVAILABLE';
 | 
        
           |  |  | 152 |                             break;
 | 
        
           |  |  | 153 |                     }
 | 
        
           |  |  | 154 |   | 
        
           |  |  | 155 |   | 
        
           |  |  | 156 |                     $item = [
 | 
        
           |  |  | 157 |                         'name' => $record['name'],
 | 
        
           |  |  | 158 |                         'user' => $record['first_name'].' '.$record['last_name'],
 | 
        
           |  |  | 159 |                         'language' => $language,
 | 
        
           | 289 | geraldo | 160 |                         'status' => $status,
 | 
        
           |  |  | 161 |                         'link_edit' => $this->url()->fromRoute('self-evaluation/reviews/edit', ['id' => $record['uuid']]),
 | 
        
           | 286 | geraldo | 162 |                     ];
 | 
        
           |  |  | 163 |   | 
        
           |  |  | 164 |                     array_push($items, $item);
 | 
        
           |  |  | 165 |                 }
 | 
        
           |  |  | 166 |   | 
        
           |  |  | 167 |   | 
        
           |  |  | 168 |   | 
        
           | 244 | geraldo | 169 |                 return new JsonModel([
 | 
        
           |  |  | 170 |                     'success' => true,
 | 
        
           |  |  | 171 |                     'data' => [
 | 
        
           | 286 | geraldo | 172 |                         'items' => $items,
 | 
        
           | 291 | geraldo | 173 |                         'total' => count($items),
 | 
        
           | 244 | geraldo | 174 |                     ]
 | 
        
           |  |  | 175 |                 ]);
 | 
        
           |  |  | 176 |             } else {
 | 
        
           |  |  | 177 |   | 
        
           |  |  | 178 |                 $form = new CompanySelfEvaluationFormForm();
 | 
        
           |  |  | 179 |   | 
        
           |  |  | 180 |                 $this->layout()->setTemplate('layout/layout-backend');
 | 
        
           |  |  | 181 |                 $viewModel = new ViewModel();
 | 
        
           | 250 | geraldo | 182 |                 $viewModel->setTemplate('leaders-linked/self-evaluation-reviews/index.phtml');
 | 
        
           | 244 | geraldo | 183 |                 $viewModel->setVariable('form', $form);
 | 
        
           |  |  | 184 |                 return $viewModel;
 | 
        
           |  |  | 185 |             }
 | 
        
           |  |  | 186 |         } else {
 | 
        
           |  |  | 187 |             return new JsonModel([
 | 
        
           |  |  | 188 |                 'success' => false,
 | 
        
           |  |  | 189 |                 'data' => 'ERROR_METHOD_NOT_ALLOWED'
 | 
        
           |  |  | 190 |             ]);
 | 
        
           |  |  | 191 |             ;
 | 
        
           |  |  | 192 |         }
 | 
        
           |  |  | 193 |     }
 | 
        
           |  |  | 194 |   | 
        
           |  |  | 195 |     public function editAction() {
 | 
        
           |  |  | 196 |         $request = $this->getRequest();
 | 
        
           |  |  | 197 |         $currentUserPlugin = $this->plugin('currentUserPlugin');
 | 
        
           |  |  | 198 |         $currentCompany = $currentUserPlugin->getCompany();
 | 
        
           |  |  | 199 |         $currentUser = $currentUserPlugin->getUser();
 | 
        
           |  |  | 200 |   | 
        
           |  |  | 201 |         $request = $this->getRequest();
 | 
        
           |  |  | 202 |         $uuid = $this->params()->fromRoute('id');
 | 
        
           |  |  | 203 |   | 
        
           |  |  | 204 |   | 
        
           |  |  | 205 |         if (!$uuid) {
 | 
        
           |  |  | 206 |             $data = [
 | 
        
           |  |  | 207 |                 'success' => false,
 | 
        
           |  |  | 208 |                 'data' => 'ERROR_INVALID_PARAMETER'
 | 
        
           |  |  | 209 |             ];
 | 
        
           |  |  | 210 |   | 
        
           |  |  | 211 |             return new JsonModel($data);
 | 
        
           |  |  | 212 |         }
 | 
        
           |  |  | 213 |   | 
        
           |  |  | 214 |         $companySelfEvaluationMapper = CompanySelfEvaluationFormMapper::getInstance($this->adapter);
 | 
        
           |  |  | 215 |         $companySelfEvaluation = $companySelfEvaluationMapper->fetchOneByUuid($uuid);
 | 
        
           |  |  | 216 |         if (!$companySelfEvaluation) {
 | 
        
           |  |  | 217 |             $data = [
 | 
        
           |  |  | 218 |                 'success' => false,
 | 
        
           |  |  | 219 |                 'data' => 'ERROR_RECORD_NOT_FOUND'
 | 
        
           |  |  | 220 |             ];
 | 
        
           |  |  | 221 |   | 
        
           |  |  | 222 |             return new JsonModel($data);
 | 
        
           |  |  | 223 |         }
 | 
        
           |  |  | 224 |   | 
        
           |  |  | 225 |         if ($companySelfEvaluation->company_id != $currentCompany->id) {
 | 
        
           |  |  | 226 |             return new JsonModel([
 | 
        
           |  |  | 227 |                 'success' => false,
 | 
        
           |  |  | 228 |                 'data' => 'ERROR_UNAUTHORIZED'
 | 
        
           |  |  | 229 |             ]);
 | 
        
           |  |  | 230 |         }
 | 
        
           |  |  | 231 |   | 
        
           |  |  | 232 |   | 
        
           |  |  | 233 |         if ($request->isPost()) {
 | 
        
           |  |  | 234 |             $form = new CompanySelfEvaluationFormForm();
 | 
        
           |  |  | 235 |             $dataPost = $request->getPost()->toArray();
 | 
        
           |  |  | 236 |             $dataPost['status'] = isset($dataPost['status']) ? $dataPost['status'] : CompanySelfEvaluationForm::STATUS_INACTIVE;
 | 
        
           |  |  | 237 |   | 
        
           |  |  | 238 |             $form->setData($dataPost);
 | 
        
           |  |  | 239 |   | 
        
           |  |  | 240 |             if ($form->isValid()) {
 | 
        
           |  |  | 241 |                 $dataPost = (array) $form->getData();
 | 
        
           |  |  | 242 |   | 
        
           |  |  | 243 |                 $hydrator = new ObjectPropertyHydrator();
 | 
        
           |  |  | 244 |                 $hydrator->hydrate($dataPost, $companySelfEvaluation);
 | 
        
           |  |  | 245 |   | 
        
           |  |  | 246 |                 if (!$companySelfEvaluation->status) {
 | 
        
           |  |  | 247 |                     $companySelfEvaluation->status = CompanySelfEvaluationForm::STATUS_INACTIVE;
 | 
        
           |  |  | 248 |                 }
 | 
        
           |  |  | 249 |   | 
        
           |  |  | 250 |                 $result = $companySelfEvaluationMapper->update($companySelfEvaluation);
 | 
        
           |  |  | 251 |   | 
        
           |  |  | 252 |                 if ($result) {
 | 
        
           |  |  | 253 |                     $this->logger->info('Se actualizo el tamaño de empresa ' . $companySelfEvaluation->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
 | 
        
           |  |  | 254 |                     $data = [
 | 
        
           |  |  | 255 |                         'success' => true,
 | 
        
           |  |  | 256 |                         'id' => $companySelfEvaluation->id,
 | 
        
           |  |  | 257 |                         'action_edit' => $this->url()->fromRoute('self-evaluation/forms/edit', ['id' => $companySelfEvaluation->uuid]),
 | 
        
           |  |  | 258 |                         'data' => 'LABEL_RECORD_UPDATED'
 | 
        
           |  |  | 259 |                     ];
 | 
        
           |  |  | 260 |                 } else {
 | 
        
           |  |  | 261 |                     $data = [
 | 
        
           |  |  | 262 |                         'success' => false,
 | 
        
           |  |  | 263 |                         'data' => $companySelfEvaluationMapper->getError()
 | 
        
           |  |  | 264 |                     ];
 | 
        
           |  |  | 265 |                 }
 | 
        
           |  |  | 266 |   | 
        
           |  |  | 267 |                 return new JsonModel($data);
 | 
        
           |  |  | 268 |             } else {
 | 
        
           |  |  | 269 |                 $messages = [];
 | 
        
           |  |  | 270 |                 $form_messages = (array) $form->getMessages();
 | 
        
           |  |  | 271 |                 foreach ($form_messages as $fieldname => $field_messages) {
 | 
        
           |  |  | 272 |                     $messages[$fieldname] = array_values($field_messages);
 | 
        
           |  |  | 273 |                 }
 | 
        
           |  |  | 274 |   | 
        
           |  |  | 275 |                 return new JsonModel([
 | 
        
           |  |  | 276 |                     'success' => false,
 | 
        
           |  |  | 277 |                     'data' => $messages
 | 
        
           |  |  | 278 |                 ]);
 | 
        
           |  |  | 279 |             }
 | 
        
           |  |  | 280 |         } else if ($request->isGet()) {
 | 
        
           |  |  | 281 |             $hydrator = new ObjectPropertyHydrator();
 | 
        
           |  |  | 282 |   | 
        
           |  |  | 283 |             $data = [
 | 
        
           |  |  | 284 |                 'success' => true,
 | 
        
           |  |  | 285 |                 'data' => [
 | 
        
           |  |  | 286 |                     'id' => $companySelfEvaluation->uuid,
 | 
        
           |  |  | 287 |                     'name' => $companySelfEvaluation->name,
 | 
        
           |  |  | 288 |                     'description' => $companySelfEvaluation->description,
 | 
        
           |  |  | 289 |                     'text' => $companySelfEvaluation->text,
 | 
        
           |  |  | 290 |                     'language' => $companySelfEvaluation->language,
 | 
        
           |  |  | 291 |                     'status' => $companySelfEvaluation->status,
 | 
        
           |  |  | 292 |                     'content' => $companySelfEvaluation->content ? json_decode($companySelfEvaluation->content) : [],
 | 
        
           |  |  | 293 |                 ]
 | 
        
           |  |  | 294 |             ];
 | 
        
           |  |  | 295 |   | 
        
           |  |  | 296 |             return new JsonModel($data);
 | 
        
           |  |  | 297 |         } else {
 | 
        
           |  |  | 298 |             $data = [
 | 
        
           |  |  | 299 |                 'success' => false,
 | 
        
           |  |  | 300 |                 'data' => 'ERROR_METHOD_NOT_ALLOWED'
 | 
        
           |  |  | 301 |             ];
 | 
        
           |  |  | 302 |   | 
        
           |  |  | 303 |             return new JsonModel($data);
 | 
        
           |  |  | 304 |         }
 | 
        
           |  |  | 305 |   | 
        
           |  |  | 306 |         return new JsonModel($data);
 | 
        
           |  |  | 307 |     }
 | 
        
           |  |  | 308 |   | 
        
           |  |  | 309 |   | 
        
           |  |  | 310 |   | 
        
           |  |  | 311 | }
 |