| 5287 | eleazar | 1 | <?php
 | 
        
           |  |  | 2 |   | 
        
           |  |  | 3 | declare(strict_types=1);
 | 
        
           |  |  | 4 |   | 
        
           |  |  | 5 | namespace LeadersLinked\Controller;
 | 
        
           |  |  | 6 |   | 
        
           |  |  | 7 | use Laminas\Cache\Storage\Adapter\AbstractAdapter;
 | 
        
           |  |  | 8 | use Laminas\Mvc\Controller\AbstractActionController;
 | 
        
           |  |  | 9 | use Laminas\Log\LoggerInterface;
 | 
        
           |  |  | 10 | use Laminas\View\Model\ViewModel;
 | 
        
           |  |  | 11 | use Laminas\View\Model\JsonModel;
 | 
        
           |  |  | 12 | use LeadersLinked\Library\Functions;
 | 
        
           | 5572 | eleazar | 13 | use LeadersLinked\Hydrator\ObjectPropertyHydrator;
 | 
        
           |  |  | 14 | use LeadersLinked\Mapper\UserMapper;
 | 
        
           |  |  | 15 | use LeadersLinked\Mapper\CompanyMapper;
 | 
        
           |  |  | 16 | use LeadersLinked\Model\Company;
 | 
        
           |  |  | 17 | use Laminas\Hydrator\ArraySerializableHydrator;
 | 
        
           |  |  | 18 | use Laminas\Db\ResultSet\HydratingResultSet;
 | 
        
           |  |  | 19 | use LeadersLinked\Mapper\QueryMapper;
 | 
        
           |  |  | 20 | use Laminas\Paginator\Adapter\DbSelect;
 | 
        
           |  |  | 21 | use Laminas\Paginator\Paginator;
 | 
        
           |  |  | 22 | use Laminas\Db\Adapter\AdapterInterface;
 | 
        
           | 5287 | eleazar | 23 | use LeadersLinked\Mapper\SurveyTestMapper;
 | 
        
           | 5557 | eleazar | 24 | use LeadersLinked\Mapper\SurveyMapper;
 | 
        
           |  |  | 25 | use LeadersLinked\Mapper\SurveyFormMapper;
 | 
        
           | 5287 | eleazar | 26 | use LeadersLinked\Form\SurveyTestForm;
 | 
        
           |  |  | 27 | use LeadersLinked\Model\SurveyTest;
 | 
        
           |  |  | 28 | class SurveyTestController extends AbstractActionController {
 | 
        
           |  |  | 29 |   | 
        
           |  |  | 30 |     /**
 | 
        
           |  |  | 31 |      *
 | 
        
           |  |  | 32 |      * @var AdapterInterface
 | 
        
           |  |  | 33 |      */
 | 
        
           |  |  | 34 |     private $adapter;
 | 
        
           |  |  | 35 |   | 
        
           |  |  | 36 |     /**
 | 
        
           |  |  | 37 |      *
 | 
        
           |  |  | 38 |      * @var AbstractAdapter
 | 
        
           |  |  | 39 |      */
 | 
        
           |  |  | 40 |     private $cache;
 | 
        
           |  |  | 41 |   | 
        
           |  |  | 42 |     /**
 | 
        
           |  |  | 43 |      *
 | 
        
           |  |  | 44 |      * @var  LoggerInterface
 | 
        
           |  |  | 45 |      */
 | 
        
           |  |  | 46 |     private $logger;
 | 
        
           |  |  | 47 |   | 
        
           |  |  | 48 |     /**
 | 
        
           |  |  | 49 |      *
 | 
        
           |  |  | 50 |      * @var array
 | 
        
           |  |  | 51 |      */
 | 
        
           |  |  | 52 |     private $config;
 | 
        
           |  |  | 53 |   | 
        
           |  |  | 54 |     /**
 | 
        
           |  |  | 55 |      *
 | 
        
           |  |  | 56 |      * @param AdapterInterface $adapter
 | 
        
           |  |  | 57 |      * @param AbstractAdapter $cache
 | 
        
           |  |  | 58 |      * @param LoggerInterface $logger
 | 
        
           |  |  | 59 |      * @param array $config
 | 
        
           |  |  | 60 |      */
 | 
        
           |  |  | 61 |     public function __construct($adapter, $cache, $logger, $config) {
 | 
        
           |  |  | 62 |         $this->adapter = $adapter;
 | 
        
           |  |  | 63 |         $this->cache = $cache;
 | 
        
           |  |  | 64 |         $this->logger = $logger;
 | 
        
           |  |  | 65 |         $this->config = $config;
 | 
        
           |  |  | 66 |     }
 | 
        
           |  |  | 67 |   | 
        
           |  |  | 68 |     public function indexAction() {
 | 
        
           | 5889 | eleazar | 69 |   | 
        
           | 5287 | eleazar | 70 |         $request = $this->getRequest();
 | 
        
           |  |  | 71 |         $currentUserPlugin = $this->plugin('currentUserPlugin');
 | 
        
           |  |  | 72 |         $currentCompany = $currentUserPlugin->getCompany();
 | 
        
           |  |  | 73 |         $currentUser = $currentUserPlugin->getUser();
 | 
        
           |  |  | 74 |   | 
        
           | 5887 | eleazar | 75 |         try{
 | 
        
           | 5287 | eleazar | 76 |         $request = $this->getRequest();
 | 
        
           |  |  | 77 |         if ($request->isGet()) {
 | 
        
           |  |  | 78 |   | 
        
           |  |  | 79 |             $headers = $request->getHeaders();
 | 
        
           |  |  | 80 |   | 
        
           |  |  | 81 |             $isJson = false;
 | 
        
           |  |  | 82 |             if ($headers->has('Accept')) {
 | 
        
           |  |  | 83 |                 $accept = $headers->get('Accept');
 | 
        
           |  |  | 84 |   | 
        
           |  |  | 85 |                 $prioritized = $accept->getPrioritized();
 | 
        
           |  |  | 86 |   | 
        
           |  |  | 87 |                 foreach ($prioritized as $key => $value) {
 | 
        
           |  |  | 88 |                     $raw = trim($value->getRaw());
 | 
        
           |  |  | 89 |   | 
        
           |  |  | 90 |                     if (!$isJson) {
 | 
        
           |  |  | 91 |                         $isJson = strpos($raw, 'json');
 | 
        
           |  |  | 92 |                     }
 | 
        
           |  |  | 93 |                 }
 | 
        
           |  |  | 94 |             }
 | 
        
           |  |  | 95 |   | 
        
           |  |  | 96 |             if ($isJson) {
 | 
        
           | 5885 | eleazar | 97 |                 $survey_uuid = $this->params()->fromRoute('survey_id');
 | 
        
           |  |  | 98 |   | 
        
           |  |  | 99 |                 $surveyMapper = SurveyMapper::getInstance($this->adapter);
 | 
        
           |  |  | 100 |                 $survey = $surveyMapper->fetchOneByUuid($survey_uuid);
 | 
        
           |  |  | 101 |   | 
        
           | 5607 | eleazar | 102 |                 $search = $this->params()->fromQuery('search', []);
 | 
        
           | 5287 | eleazar | 103 |                 $search = empty($search['value']) ? '' : filter_var($search['value'], FILTER_SANITIZE_STRING);
 | 
        
           |  |  | 104 |   | 
        
           |  |  | 105 |                 $page = intval($this->params()->fromQuery('start', 1), 10);
 | 
        
           |  |  | 106 |                 $records_x_page = intval($this->params()->fromQuery('length', 10), 10);
 | 
        
           |  |  | 107 |                 $order = $this->params()->fromQuery('order', []);
 | 
        
           |  |  | 108 |                 $order_field = empty($order[0]['column']) ? 99 : intval($order[0]['column'], 10);
 | 
        
           |  |  | 109 |                 $order_direction = empty($order[0]['dir']) ? 'ASC' : strtoupper(filter_var($order[0]['dir'], FILTER_SANITIZE_STRING));
 | 
        
           |  |  | 110 |   | 
        
           | 5613 | eleazar | 111 |                 $fields = ['first_name'];
 | 
        
           |  |  | 112 |                 $order_field = isset($fields[$order_field]) ? $fields[$order_field] : 'first_name';
 | 
        
           |  |  | 113 |   | 
        
           | 5614 | eleazar | 114 |                 if (!in_array($order_direction, ['ASC', 'DESC'])) {
 | 
        
           |  |  | 115 |                     $order_direction = 'ASC';
 | 
        
           |  |  | 116 |                 }
 | 
        
           |  |  | 117 |   | 
        
           | 5620 | eleazar | 118 |                 $surveyTestMapper = SurveyTestMapper::getInstance($this->adapter);
 | 
        
           | 6038 | eleazar | 119 |                 $paginator = $surveyTestMapper->fetchAllDataTableBySurveyId($survey->id, $search, $page, $records_x_page, $order_field, $order_direction);
 | 
        
           | 5619 | eleazar | 120 |   | 
        
           | 5287 | eleazar | 121 |                 $items = [];
 | 
        
           |  |  | 122 |                 $records = $paginator->getCurrentItems();
 | 
        
           | 5566 | eleazar | 123 |   | 
        
           | 5287 | eleazar | 124 |                 foreach ($records as $record) {
 | 
        
           | 5569 | eleazar | 125 |                     $params = [
 | 
        
           | 5897 | eleazar | 126 |                         'survey_id' => $survey->uuid,
 | 
        
           |  |  | 127 |                         'id' => $record->uuid,
 | 
        
           | 5569 | eleazar | 128 |                     ];
 | 
        
           |  |  | 129 |   | 
        
           | 5287 | eleazar | 130 |                     $item = [
 | 
        
           |  |  | 131 |                         'id' => $record->id,
 | 
        
           | 5557 | eleazar | 132 |                         'first_name' => $record->first_name,
 | 
        
           | 5907 | eleazar | 133 |                         'form' => $survey->name,
 | 
        
           | 5288 | eleazar | 134 |   | 
        
           | 5287 | eleazar | 135 |                         'actions' => [
 | 
        
           |  |  | 136 |                             'link_delete' => $this->url()->fromRoute('survey/test/delete', ['id' => $record->uuid])
 | 
        
           |  |  | 137 |                         ]
 | 
        
           |  |  | 138 |                     ];
 | 
        
           |  |  | 139 |   | 
        
           |  |  | 140 |                     array_push($items, $item);
 | 
        
           |  |  | 141 |                 }
 | 
        
           |  |  | 142 |   | 
        
           |  |  | 143 |                 return new JsonModel([
 | 
        
           |  |  | 144 |                     'success' => true,
 | 
        
           |  |  | 145 |                     'data' => [
 | 
        
           |  |  | 146 |                         'items' => $items,
 | 
        
           |  |  | 147 |                         'total' => $paginator->getTotalItemCount(),
 | 
        
           |  |  | 148 |                     ]
 | 
        
           |  |  | 149 |                 ]);
 | 
        
           |  |  | 150 |             } else {
 | 
        
           | 5564 | eleazar | 151 |                 $surveyMapper = SurveyMapper::getInstance($this->adapter);
 | 
        
           |  |  | 152 |                 $survies = $surveyMapper->fetchAllByCompanyId($currentCompany->id);
 | 
        
           |  |  | 153 |   | 
        
           | 5287 | eleazar | 154 |                 $form = new SurveyTestForm($this->adapter, $currentCompany->id);
 | 
        
           |  |  | 155 |   | 
        
           |  |  | 156 |                 $this->layout()->setTemplate('layout/layout-backend');
 | 
        
           |  |  | 157 |                 $viewModel = new ViewModel();
 | 
        
           |  |  | 158 |                 $viewModel->setTemplate('leaders-linked/survey-test/index.phtml');
 | 
        
           | 5564 | eleazar | 159 |                 $viewModel->setVariables([
 | 
        
           |  |  | 160 |                     'form'      => $form,
 | 
        
           |  |  | 161 |                     'survies' => $survies
 | 
        
           |  |  | 162 |                 ]);
 | 
        
           | 5287 | eleazar | 163 |                 return $viewModel;
 | 
        
           |  |  | 164 |             }
 | 
        
           |  |  | 165 |         } else {
 | 
        
           |  |  | 166 |             return new JsonModel([
 | 
        
           |  |  | 167 |                 'success' => false,
 | 
        
           |  |  | 168 |                 'data' => 'ERROR_METHOD_NOT_ALLOWED'
 | 
        
           |  |  | 169 |             ]);
 | 
        
           | 5663 | eleazar | 170 |   | 
        
           | 5287 | eleazar | 171 |         }
 | 
        
           | 5894 | eleazar | 172 |         } catch (\Throwable $e) {
 | 
        
           | 5887 | eleazar | 173 |             $e->getMessage();
 | 
        
           | 5888 | eleazar | 174 |             return new JsonModel([
 | 
        
           |  |  | 175 |                 'success' => false,
 | 
        
           |  |  | 176 |                 'data' => $e
 | 
        
           |  |  | 177 |             ]);
 | 
        
           | 5887 | eleazar | 178 |         }
 | 
        
           | 5287 | eleazar | 179 |     }
 | 
        
           |  |  | 180 |   | 
        
           |  |  | 181 |     public function addAction() {
 | 
        
           |  |  | 182 |         $request = $this->getRequest();
 | 
        
           |  |  | 183 |         $currentUserPlugin = $this->plugin('currentUserPlugin');
 | 
        
           |  |  | 184 |         $currentCompany = $currentUserPlugin->getCompany();
 | 
        
           |  |  | 185 |         $currentUser = $currentUserPlugin->getUser();
 | 
        
           |  |  | 186 |   | 
        
           |  |  | 187 |         $request = $this->getRequest();
 | 
        
           |  |  | 188 |   | 
        
           | 5732 | eleazar | 189 |         $survey_id = $this->params()->fromRoute('survey_id');
 | 
        
           | 5823 | eleazar | 190 |   | 
        
           | 5452 | eleazar | 191 |         if ($request->isGet()) {
 | 
        
           | 5649 | eleazar | 192 |             $surveyMapper = SurveyMapper::getInstance($this->adapter);
 | 
        
           | 5732 | eleazar | 193 |             $survey = $surveyMapper->fetchOneByUuid($survey_id);
 | 
        
           | 5570 | eleazar | 194 |   | 
        
           | 5452 | eleazar | 195 |             $surveyFormMapper = SurveyFormMapper::getInstance($this->adapter);
 | 
        
           |  |  | 196 |             $surveyForm = $surveyFormMapper->fetchOne($survey->form_id);
 | 
        
           | 5570 | eleazar | 197 |   | 
        
           | 5452 | eleazar | 198 |             $data = [
 | 
        
           |  |  | 199 |                 'survey' => [
 | 
        
           |  |  | 200 |                     'id' => $survey->uuid,
 | 
        
           |  |  | 201 |                     'content' => $surveyForm->content ? json_decode($surveyForm->content) : [],
 | 
        
           |  |  | 202 |                     'description' => $surveyForm->description,
 | 
        
           |  |  | 203 |                     'text' => $surveyForm->text,
 | 
        
           |  |  | 204 |                 ],
 | 
        
           |  |  | 205 |             ];
 | 
        
           | 5663 | eleazar | 206 |             return new JsonModel([
 | 
        
           |  |  | 207 |                 'success' => true,
 | 
        
           |  |  | 208 |                 'data' => $data,
 | 
        
           |  |  | 209 |             ]);
 | 
        
           | 5563 | eleazar | 210 |   | 
        
           | 5452 | eleazar | 211 |   | 
        
           |  |  | 212 |         } else if ($request->isPost()) {
 | 
        
           | 5287 | eleazar | 213 |   | 
        
           |  |  | 214 |             $form = new SurveyTestForm($this->adapter, $currentCompany->id);
 | 
        
           |  |  | 215 |   | 
        
           |  |  | 216 |             $dataPost = $request->getPost()->toArray();
 | 
        
           |  |  | 217 |   | 
        
           |  |  | 218 |             $form->setData($dataPost);
 | 
        
           |  |  | 219 |   | 
        
           |  |  | 220 |             if ($form->isValid()) {
 | 
        
           |  |  | 221 |                 $dataPost = (array) $form->getData();
 | 
        
           |  |  | 222 |   | 
        
           |  |  | 223 |                 $hydrator = new ObjectPropertyHydrator();
 | 
        
           |  |  | 224 |                 $survey = new SurveyTest();
 | 
        
           |  |  | 225 |                 $hydrator->hydrate($dataPost, $survey);
 | 
        
           |  |  | 226 |   | 
        
           | 5831 | eleazar | 227 |                 $surveyDoneMapper = SurveyMapper::getInstance($this->adapter);
 | 
        
           |  |  | 228 |                 $surveyDone = $surveyDoneMapper->fetchOneByUuid($survey_id);
 | 
        
           |  |  | 229 |   | 
        
           | 5287 | eleazar | 230 |                 $survey->company_id = $currentCompany->id;
 | 
        
           | 5831 | eleazar | 231 |                 $survey->survey_id = $surveyDone->id;
 | 
        
           | 5287 | eleazar | 232 |   | 
        
           |  |  | 233 |                 $surveyMapper = SurveyTestMapper::getInstance($this->adapter);
 | 
        
           |  |  | 234 |                 $result = $surveyMapper->insert($survey);
 | 
        
           |  |  | 235 |   | 
        
           |  |  | 236 |                 if ($result) {
 | 
        
           | 5832 | eleazar | 237 |                     $this->logger->info('Se agrego el formulario' . $survey->first_name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
 | 
        
           | 5287 | eleazar | 238 |   | 
        
           |  |  | 239 |                     // Get record by id
 | 
        
           |  |  | 240 |                     $record = $surveyMapper->fetchOne($survey->id);
 | 
        
           |  |  | 241 |   | 
        
           |  |  | 242 |                     if ($record) {
 | 
        
           |  |  | 243 |   | 
        
           |  |  | 244 |                         $data = [
 | 
        
           |  |  | 245 |                             'success' => true,
 | 
        
           |  |  | 246 |                             'id' => $record->id,
 | 
        
           |  |  | 247 |                             'data' => 'LABEL_RECORD_ADDED'
 | 
        
           |  |  | 248 |                         ];
 | 
        
           |  |  | 249 |                     } else {
 | 
        
           |  |  | 250 |   | 
        
           |  |  | 251 |                         $data = [
 | 
        
           |  |  | 252 |                             'success' => false,
 | 
        
           |  |  | 253 |                             'data' => 'ERROR_RECORD_NOT_FOUND'
 | 
        
           |  |  | 254 |                         ];
 | 
        
           |  |  | 255 |                     }
 | 
        
           |  |  | 256 |                 } else {
 | 
        
           |  |  | 257 |                     $data = [
 | 
        
           |  |  | 258 |                         'success' => false,
 | 
        
           |  |  | 259 |                         'data' => $surveyMapper->getError()
 | 
        
           |  |  | 260 |                     ];
 | 
        
           |  |  | 261 |                 }
 | 
        
           |  |  | 262 |   | 
        
           |  |  | 263 |                 return new JsonModel($data);
 | 
        
           |  |  | 264 |             } else {
 | 
        
           |  |  | 265 |                 $messages = [];
 | 
        
           |  |  | 266 |                 $form_messages = (array) $form->getMessages();
 | 
        
           |  |  | 267 |                 foreach ($form_messages as $fieldname => $field_messages) {
 | 
        
           |  |  | 268 |   | 
        
           |  |  | 269 |                     $messages[$fieldname] = array_values($field_messages);
 | 
        
           |  |  | 270 |                 }
 | 
        
           |  |  | 271 |   | 
        
           |  |  | 272 |                 return new JsonModel([
 | 
        
           |  |  | 273 |                     'success' => false,
 | 
        
           |  |  | 274 |                     'data' => $messages
 | 
        
           |  |  | 275 |                 ]);
 | 
        
           |  |  | 276 |             }
 | 
        
           |  |  | 277 |         } else {
 | 
        
           |  |  | 278 |             $data = [
 | 
        
           |  |  | 279 |                 'success' => false,
 | 
        
           |  |  | 280 |                 'data' => 'ERROR_METHOD_NOT_ALLOWED'
 | 
        
           |  |  | 281 |             ];
 | 
        
           |  |  | 282 |   | 
        
           |  |  | 283 |             return new JsonModel($data);
 | 
        
           |  |  | 284 |         }
 | 
        
           |  |  | 285 |   | 
        
           |  |  | 286 |         return new JsonModel($data);
 | 
        
           |  |  | 287 |     }
 | 
        
           |  |  | 288 |   | 
        
           |  |  | 289 |     public function deleteAction() {
 | 
        
           |  |  | 290 |         $request = $this->getRequest();
 | 
        
           |  |  | 291 |         $currentUserPlugin = $this->plugin('currentUserPlugin');
 | 
        
           |  |  | 292 |         $currentCompany = $currentUserPlugin->getCompany();
 | 
        
           |  |  | 293 |         $currentUser = $currentUserPlugin->getUser();
 | 
        
           |  |  | 294 |   | 
        
           |  |  | 295 |         $request = $this->getRequest();
 | 
        
           |  |  | 296 |         $uuid = $this->params()->fromRoute('id');
 | 
        
           |  |  | 297 |   | 
        
           |  |  | 298 |         if (!$uuid) {
 | 
        
           |  |  | 299 |             $data = [
 | 
        
           |  |  | 300 |                 'success' => false,
 | 
        
           |  |  | 301 |                 'data' => 'ERROR_INVALID_PARAMETER'
 | 
        
           |  |  | 302 |             ];
 | 
        
           |  |  | 303 |   | 
        
           |  |  | 304 |             return new JsonModel($data);
 | 
        
           |  |  | 305 |         }
 | 
        
           |  |  | 306 |   | 
        
           |  |  | 307 |         $surveyMapper = SurveyFormMapper::getInstance($this->adapter);
 | 
        
           |  |  | 308 |         $survey = $surveyMapper->fetchOneByUuid($uuid);
 | 
        
           |  |  | 309 |         if (!$survey) {
 | 
        
           |  |  | 310 |             $data = [
 | 
        
           |  |  | 311 |                 'success' => false,
 | 
        
           |  |  | 312 |                 'data' => 'ERROR_RECORD_NOT_FOUND'
 | 
        
           |  |  | 313 |             ];
 | 
        
           |  |  | 314 |   | 
        
           |  |  | 315 |             return new JsonModel($data);
 | 
        
           |  |  | 316 |         }
 | 
        
           |  |  | 317 |   | 
        
           |  |  | 318 |         if ($survey->company_id != $currentCompany->id) {
 | 
        
           |  |  | 319 |             return new JsonModel([
 | 
        
           |  |  | 320 |                 'success' => false,
 | 
        
           |  |  | 321 |                 'data' => 'ERROR_UNAUTHORIZED'
 | 
        
           |  |  | 322 |             ]);
 | 
        
           |  |  | 323 |         }
 | 
        
           |  |  | 324 |   | 
        
           |  |  | 325 |         if ($request->isPost()) {
 | 
        
           |  |  | 326 |   | 
        
           |  |  | 327 |             $result = $surveyMapper->delete($survey->id);
 | 
        
           |  |  | 328 |             if ($result) {
 | 
        
           |  |  | 329 |                 $this->logger->info('Se borro el formulario ' . $survey->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
 | 
        
           |  |  | 330 |   | 
        
           |  |  | 331 |                 $data = [
 | 
        
           |  |  | 332 |                     'success' => true,
 | 
        
           |  |  | 333 |                     'data' => 'LABEL_RECORD_DELETED'
 | 
        
           |  |  | 334 |                 ];
 | 
        
           |  |  | 335 |             } else {
 | 
        
           |  |  | 336 |   | 
        
           |  |  | 337 |                 $data = [
 | 
        
           |  |  | 338 |                     'success' => false,
 | 
        
           |  |  | 339 |                     'data' => $surveyMapper->getError()
 | 
        
           |  |  | 340 |                 ];
 | 
        
           |  |  | 341 |   | 
        
           |  |  | 342 |                 return new JsonModel($data);
 | 
        
           |  |  | 343 |             }
 | 
        
           |  |  | 344 |         } else {
 | 
        
           |  |  | 345 |             $data = [
 | 
        
           |  |  | 346 |                 'success' => false,
 | 
        
           |  |  | 347 |                 'data' => 'ERROR_METHOD_NOT_ALLOWED'
 | 
        
           |  |  | 348 |             ];
 | 
        
           |  |  | 349 |   | 
        
           |  |  | 350 |             return new JsonModel($data);
 | 
        
           |  |  | 351 |         }
 | 
        
           |  |  | 352 |   | 
        
           |  |  | 353 |         return new JsonModel($data);
 | 
        
           |  |  | 354 |     }
 | 
        
           |  |  | 355 |   | 
        
           |  |  | 356 | }
 |