| 7290 | 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;
 | 
        
           |  |  | 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;
 | 
        
           |  |  | 23 | use LeadersLinked\Mapper\OrganizationalClimateTestMapper;
 | 
        
           |  |  | 24 | use LeadersLinked\Mapper\OrganizationalClimateMapper;
 | 
        
           |  |  | 25 | use LeadersLinked\Mapper\OrganizationalClimateFormMapper;
 | 
        
           |  |  | 26 | use LeadersLinked\Form\OrganizationalClimateTestForm;
 | 
        
           |  |  | 27 | use LeadersLinked\Model\OrganizationalClimateTest;
 | 
        
           | 7533 | eleazar | 28 | use LeadersLinked\Library\UniqueSurveyReport;
 | 
        
           | 7290 | eleazar | 29 |   | 
        
           |  |  | 30 | class OrganizationalClimateTestController extends AbstractActionController {
 | 
        
           |  |  | 31 |   | 
        
           |  |  | 32 |     /**
 | 
        
           |  |  | 33 |      *
 | 
        
           |  |  | 34 |      * @var AdapterInterface
 | 
        
           |  |  | 35 |      */
 | 
        
           |  |  | 36 |     private $adapter;
 | 
        
           |  |  | 37 |   | 
        
           |  |  | 38 |     /**
 | 
        
           |  |  | 39 |      *
 | 
        
           |  |  | 40 |      * @var AbstractAdapter
 | 
        
           |  |  | 41 |      */
 | 
        
           |  |  | 42 |     private $cache;
 | 
        
           |  |  | 43 |   | 
        
           |  |  | 44 |     /**
 | 
        
           |  |  | 45 |      *
 | 
        
           |  |  | 46 |      * @var  LoggerInterface
 | 
        
           |  |  | 47 |      */
 | 
        
           |  |  | 48 |     private $logger;
 | 
        
           |  |  | 49 |   | 
        
           |  |  | 50 |     /**
 | 
        
           |  |  | 51 |      *
 | 
        
           |  |  | 52 |      * @var array
 | 
        
           |  |  | 53 |      */
 | 
        
           |  |  | 54 |     private $config;
 | 
        
           |  |  | 55 |   | 
        
           |  |  | 56 |     /**
 | 
        
           |  |  | 57 |      *
 | 
        
           |  |  | 58 |      * @param AdapterInterface $adapter
 | 
        
           |  |  | 59 |      * @param AbstractAdapter $cache
 | 
        
           |  |  | 60 |      * @param LoggerInterface $logger
 | 
        
           |  |  | 61 |      * @param array $config
 | 
        
           |  |  | 62 |      */
 | 
        
           |  |  | 63 |     public function __construct($adapter, $cache, $logger, $config) {
 | 
        
           |  |  | 64 |         $this->adapter = $adapter;
 | 
        
           |  |  | 65 |         $this->cache = $cache;
 | 
        
           |  |  | 66 |         $this->logger = $logger;
 | 
        
           |  |  | 67 |         $this->config = $config;
 | 
        
           |  |  | 68 |     }
 | 
        
           |  |  | 69 |   | 
        
           |  |  | 70 |     public function indexAction() {
 | 
        
           |  |  | 71 |   | 
        
           |  |  | 72 |         $request = $this->getRequest();
 | 
        
           |  |  | 73 |         $currentUserPlugin = $this->plugin('currentUserPlugin');
 | 
        
           |  |  | 74 |         $currentCompany = $currentUserPlugin->getCompany();
 | 
        
           |  |  | 75 |         $currentUser = $currentUserPlugin->getUser();
 | 
        
           |  |  | 76 |   | 
        
           | 7395 | eleazar | 77 |   | 
        
           | 7290 | eleazar | 78 |         $request = $this->getRequest();
 | 
        
           |  |  | 79 |         if ($request->isGet()) {
 | 
        
           |  |  | 80 |   | 
        
           |  |  | 81 |             $headers = $request->getHeaders();
 | 
        
           |  |  | 82 |   | 
        
           |  |  | 83 |             $isJson = false;
 | 
        
           |  |  | 84 |             if ($headers->has('Accept')) {
 | 
        
           |  |  | 85 |                 $accept = $headers->get('Accept');
 | 
        
           |  |  | 86 |   | 
        
           |  |  | 87 |                 $prioritized = $accept->getPrioritized();
 | 
        
           |  |  | 88 |   | 
        
           |  |  | 89 |                 foreach ($prioritized as $key => $value) {
 | 
        
           |  |  | 90 |                     $raw = trim($value->getRaw());
 | 
        
           |  |  | 91 |   | 
        
           |  |  | 92 |                     if (!$isJson) {
 | 
        
           |  |  | 93 |                         $isJson = strpos($raw, 'json');
 | 
        
           |  |  | 94 |                     }
 | 
        
           |  |  | 95 |                 }
 | 
        
           |  |  | 96 |             }
 | 
        
           |  |  | 97 |   | 
        
           |  |  | 98 |             if ($isJson) {
 | 
        
           |  |  | 99 |                 $organizationalClimate_uuid = $this->params()->fromRoute('organizationalClimate_id');
 | 
        
           |  |  | 100 |   | 
        
           |  |  | 101 |                 if(!$organizationalClimate_uuid){
 | 
        
           |  |  | 102 |                     return new JsonModel([
 | 
        
           |  |  | 103 |                         'success' => true,
 | 
        
           |  |  | 104 |                         'data' => [
 | 
        
           |  |  | 105 |                             'items' => [],
 | 
        
           |  |  | 106 |                             'total' => 0
 | 
        
           |  |  | 107 |                         ]
 | 
        
           |  |  | 108 |                     ]);
 | 
        
           |  |  | 109 |                 }
 | 
        
           |  |  | 110 |   | 
        
           |  |  | 111 |                 $organizationalClimateMapper = OrganizationalClimateMapper::getInstance($this->adapter);
 | 
        
           |  |  | 112 |                 $organizationalClimate = $organizationalClimateMapper->fetchOneByUuid($organizationalClimate_uuid);
 | 
        
           | 7393 | eleazar | 113 |   | 
        
           | 7290 | eleazar | 114 |                 $search = $this->params()->fromQuery('search', []);
 | 
        
           |  |  | 115 |                 $search = empty($search['value']) ? '' : filter_var($search['value'], FILTER_SANITIZE_STRING);
 | 
        
           |  |  | 116 |   | 
        
           |  |  | 117 |                 $page = intval($this->params()->fromQuery('start', 1), 10);
 | 
        
           |  |  | 118 |                 $records_x_page = intval($this->params()->fromQuery('length', 10), 10);
 | 
        
           |  |  | 119 |                 $order = $this->params()->fromQuery('order', []);
 | 
        
           |  |  | 120 |                 $order_field = empty($order[0]['column']) ? 99 : intval($order[0]['column'], 10);
 | 
        
           |  |  | 121 |                 $order_direction = empty($order[0]['dir']) ? 'ASC' : strtoupper(filter_var($order[0]['dir'], FILTER_SANITIZE_STRING));
 | 
        
           |  |  | 122 |   | 
        
           |  |  | 123 |                 $fields = ['first_name'];
 | 
        
           |  |  | 124 |                 $order_field = isset($fields[$order_field]) ? $fields[$order_field] : 'first_name';
 | 
        
           |  |  | 125 |   | 
        
           |  |  | 126 |                 if (!in_array($order_direction, ['ASC', 'DESC'])) {
 | 
        
           |  |  | 127 |                     $order_direction = 'ASC';
 | 
        
           |  |  | 128 |                 }
 | 
        
           |  |  | 129 |   | 
        
           |  |  | 130 |                 $organizationalClimateTestMapper = OrganizationalClimateTestMapper::getInstance($this->adapter);
 | 
        
           | 7394 | eleazar | 131 |                 $paginator = $organizationalClimateTestMapper->fetchAllDataTableBySurveyId($organizationalClimate->id, $search, $page, $records_x_page, $order_field, $order_direction);
 | 
        
           | 7290 | eleazar | 132 |   | 
        
           |  |  | 133 |                 $items = [];
 | 
        
           |  |  | 134 |                 $records = $paginator->getCurrentItems();
 | 
        
           |  |  | 135 |   | 
        
           |  |  | 136 |                 foreach ($records as $record) {
 | 
        
           |  |  | 137 |   | 
        
           |  |  | 138 |                     $item = [
 | 
        
           |  |  | 139 |                         'id' => $record->id,
 | 
        
           |  |  | 140 |                         'first_name' => $record->first_name,
 | 
        
           |  |  | 141 |                         'date' => $record->added_on,
 | 
        
           |  |  | 142 |   | 
        
           |  |  | 143 |                         'actions' => [
 | 
        
           |  |  | 144 |                             'link_delete' => $this->url()->fromRoute('organizational-climate/test/delete', ['id' => $record->id, 'organizationalClimate_id' => $organizationalClimate->uuid]),
 | 
        
           |  |  | 145 |                             'link_report' => $this->url()->fromRoute('organizational-climate/test/report', ['uuid' => $record->uuid, 'organizationalClimate_id' => $organizationalClimate->uuid])
 | 
        
           |  |  | 146 |                         ]
 | 
        
           |  |  | 147 |                     ];
 | 
        
           |  |  | 148 |   | 
        
           |  |  | 149 |                     array_push($items, $item);
 | 
        
           |  |  | 150 |                 }
 | 
        
           |  |  | 151 |   | 
        
           |  |  | 152 |                 return new JsonModel([
 | 
        
           |  |  | 153 |                     'success' => true,
 | 
        
           |  |  | 154 |                     'data' => [
 | 
        
           |  |  | 155 |                         'items' => $items,
 | 
        
           |  |  | 156 |                         'total' => $paginator->getTotalItemCount(),
 | 
        
           |  |  | 157 |                     ]
 | 
        
           |  |  | 158 |                 ]);
 | 
        
           |  |  | 159 |             } else {
 | 
        
           |  |  | 160 |                 $organizationalClimateMapper = OrganizationalClimateMapper::getInstance($this->adapter);
 | 
        
           |  |  | 161 |                 $organizationalClimate = $organizationalClimateMapper->fetchAllByCompanyId($currentCompany->id);
 | 
        
           |  |  | 162 |   | 
        
           |  |  | 163 |                 $form = new OrganizationalClimateTestForm($this->adapter, $currentCompany->id);
 | 
        
           |  |  | 164 |   | 
        
           |  |  | 165 |                 $this->layout()->setTemplate('layout/layout-backend');
 | 
        
           |  |  | 166 |                 $viewModel = new ViewModel();
 | 
        
           |  |  | 167 |                 $viewModel->setTemplate('leaders-linked/organizational-climate-test/index.phtml');
 | 
        
           |  |  | 168 |                 $viewModel->setVariables([
 | 
        
           |  |  | 169 |                     'form'      => $form,
 | 
        
           |  |  | 170 |                     'organizationalClimate' => $organizationalClimate
 | 
        
           |  |  | 171 |                 ]);
 | 
        
           |  |  | 172 |                 return $viewModel;
 | 
        
           |  |  | 173 |             }
 | 
        
           |  |  | 174 |         } else {
 | 
        
           |  |  | 175 |             return new JsonModel([
 | 
        
           |  |  | 176 |                 'success' => false,
 | 
        
           |  |  | 177 |                 'data' => 'ERROR_METHOD_NOT_ALLOWED'
 | 
        
           |  |  | 178 |             ]);
 | 
        
           |  |  | 179 |   | 
        
           |  |  | 180 |         }
 | 
        
           | 7395 | eleazar | 181 |   | 
        
           | 7290 | eleazar | 182 |     }
 | 
        
           |  |  | 183 |   | 
        
           |  |  | 184 |     public function addAction() {
 | 
        
           |  |  | 185 |         $request = $this->getRequest();
 | 
        
           |  |  | 186 |         $currentUserPlugin = $this->plugin('currentUserPlugin');
 | 
        
           |  |  | 187 |         $currentCompany = $currentUserPlugin->getCompany();
 | 
        
           |  |  | 188 |         $currentUser = $currentUserPlugin->getUser();
 | 
        
           |  |  | 189 |   | 
        
           |  |  | 190 |         $request = $this->getRequest();
 | 
        
           |  |  | 191 |   | 
        
           |  |  | 192 |         $organizationalClimate_id = $this->params()->fromRoute('organizationalClimate_id');
 | 
        
           |  |  | 193 |   | 
        
           |  |  | 194 |         if ($request->isGet()) {
 | 
        
           |  |  | 195 |             $organizationalClimateMapper = OrganizationalClimateMapper::getInstance($this->adapter);
 | 
        
           |  |  | 196 |             $organizationalClimate = $organizationalClimateMapper->fetchOneByUuid($organizationalClimate_id);
 | 
        
           |  |  | 197 |   | 
        
           |  |  | 198 |             $organizationalClimateFormMapper = OrganizationalClimateFormMapper::getInstance($this->adapter);
 | 
        
           |  |  | 199 |             $organizationalClimateForm = $organizationalClimateFormMapper->fetchOne($organizationalClimate->form_id);
 | 
        
           |  |  | 200 |   | 
        
           |  |  | 201 |             $data = [
 | 
        
           |  |  | 202 |                 'organizationalClimate' => [
 | 
        
           |  |  | 203 |                     'id' => $organizationalClimate->uuid,
 | 
        
           |  |  | 204 |                     'content' => $organizationalClimateForm->content ? json_decode($organizationalClimateForm->content) : [],
 | 
        
           |  |  | 205 |                     'description' => $organizationalClimateForm->description,
 | 
        
           |  |  | 206 |                     'text' => $organizationalClimateForm->text,
 | 
        
           |  |  | 207 |                 ],
 | 
        
           |  |  | 208 |             ];
 | 
        
           |  |  | 209 |   | 
        
           |  |  | 210 |         } else if ($request->isPost()) {
 | 
        
           |  |  | 211 |   | 
        
           |  |  | 212 |             $form = new OrganizationalClimateTestForm($this->adapter, $currentCompany->id);
 | 
        
           |  |  | 213 |   | 
        
           |  |  | 214 |             $dataPost = $request->getPost()->toArray();
 | 
        
           |  |  | 215 |   | 
        
           |  |  | 216 |             $form->setData($dataPost);
 | 
        
           |  |  | 217 |   | 
        
           |  |  | 218 |             if ($form->isValid()) {
 | 
        
           |  |  | 219 |                 $dataPost = (array) $form->getData();
 | 
        
           |  |  | 220 |   | 
        
           |  |  | 221 |                 $hydrator = new ObjectPropertyHydrator();
 | 
        
           |  |  | 222 |                 $organizationalClimate = new OrganizationalClimateTest();
 | 
        
           |  |  | 223 |                 $hydrator->hydrate($dataPost, $organizationalClimate);
 | 
        
           |  |  | 224 |   | 
        
           |  |  | 225 |                 $organizationalClimateDoneMapper = OrganizationalClimateMapper::getInstance($this->adapter);
 | 
        
           |  |  | 226 |                 $organizationalClimateDone = $organizationalClimateDoneMapper->fetchOneByUuid($organizationalClimate_id);
 | 
        
           |  |  | 227 |   | 
        
           |  |  | 228 |                 $organizationalClimate->company_id = $currentCompany->id;
 | 
        
           | 7525 | eleazar | 229 |                 $organizationalClimate->organizationalClimate_id = $organizationalClimateDone->id;
 | 
        
           | 7290 | eleazar | 230 |   | 
        
           |  |  | 231 |                 $organizationalClimateMapper = OrganizationalClimateTestMapper::getInstance($this->adapter);
 | 
        
           |  |  | 232 |                 $result = $organizationalClimateMapper->insert($organizationalClimate);
 | 
        
           |  |  | 233 |   | 
        
           |  |  | 234 |                 if ($result) {
 | 
        
           |  |  | 235 |                     $this->logger->info('Se agrego el formulario' . $organizationalClimate->first_name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
 | 
        
           |  |  | 236 |   | 
        
           |  |  | 237 |                     // Get record by id
 | 
        
           |  |  | 238 |                     $record = $organizationalClimateMapper->fetchOne($organizationalClimate->id);
 | 
        
           |  |  | 239 |   | 
        
           |  |  | 240 |                     if ($record) {
 | 
        
           |  |  | 241 |   | 
        
           |  |  | 242 |                         $data = [
 | 
        
           |  |  | 243 |                             'success' => true,
 | 
        
           |  |  | 244 |                             'id' => $record->id,
 | 
        
           |  |  | 245 |                             'data' => 'LABEL_RECORD_ADDED'
 | 
        
           |  |  | 246 |                         ];
 | 
        
           |  |  | 247 |                     } else {
 | 
        
           |  |  | 248 |   | 
        
           |  |  | 249 |                         $data = [
 | 
        
           |  |  | 250 |                             'success' => false,
 | 
        
           |  |  | 251 |                             'data' => 'ERROR_RECORD_NOT_FOUND'
 | 
        
           |  |  | 252 |                         ];
 | 
        
           |  |  | 253 |                     }
 | 
        
           |  |  | 254 |                 } else {
 | 
        
           |  |  | 255 |                     $data = [
 | 
        
           |  |  | 256 |                         'success' => false,
 | 
        
           |  |  | 257 |                         'data' => $organizationalClimateMapper->getError()
 | 
        
           |  |  | 258 |                     ];
 | 
        
           |  |  | 259 |                 }
 | 
        
           |  |  | 260 |   | 
        
           |  |  | 261 |                 return new JsonModel($data);
 | 
        
           |  |  | 262 |             } else {
 | 
        
           |  |  | 263 |                 $messages = [];
 | 
        
           |  |  | 264 |                 $form_messages = (array) $form->getMessages();
 | 
        
           |  |  | 265 |                 foreach ($form_messages as $fieldname => $field_messages) {
 | 
        
           |  |  | 266 |   | 
        
           |  |  | 267 |                     $messages[$fieldname] = array_values($field_messages);
 | 
        
           |  |  | 268 |                 }
 | 
        
           |  |  | 269 |   | 
        
           |  |  | 270 |                 return new JsonModel([
 | 
        
           |  |  | 271 |                     'success' => false,
 | 
        
           |  |  | 272 |                     'data' => $messages
 | 
        
           |  |  | 273 |                 ]);
 | 
        
           |  |  | 274 |             }
 | 
        
           |  |  | 275 |         } else {
 | 
        
           |  |  | 276 |             $data = [
 | 
        
           |  |  | 277 |                 'success' => false,
 | 
        
           |  |  | 278 |                 'data' => 'ERROR_METHOD_NOT_ALLOWED'
 | 
        
           |  |  | 279 |             ];
 | 
        
           |  |  | 280 |   | 
        
           |  |  | 281 |             return new JsonModel($data);
 | 
        
           |  |  | 282 |         }
 | 
        
           |  |  | 283 |   | 
        
           |  |  | 284 |         return new JsonModel($data);
 | 
        
           |  |  | 285 |     }
 | 
        
           |  |  | 286 |   | 
        
           |  |  | 287 |     public function deleteAction() {
 | 
        
           |  |  | 288 |         $request = $this->getRequest();
 | 
        
           |  |  | 289 |         $currentUserPlugin = $this->plugin('currentUserPlugin');
 | 
        
           |  |  | 290 |         $currentCompany = $currentUserPlugin->getCompany();
 | 
        
           |  |  | 291 |         $currentUser = $currentUserPlugin->getUser();
 | 
        
           |  |  | 292 |         try{
 | 
        
           |  |  | 293 |         $request = $this->getRequest();
 | 
        
           |  |  | 294 |         $id = $this->params()->fromRoute('id');
 | 
        
           |  |  | 295 |   | 
        
           |  |  | 296 |         if (!$id) {
 | 
        
           |  |  | 297 |             $data = [
 | 
        
           |  |  | 298 |                 'success' => false,
 | 
        
           |  |  | 299 |                 'data' => 'ERROR_INVALID_PARAMETER'
 | 
        
           |  |  | 300 |             ];
 | 
        
           |  |  | 301 |   | 
        
           |  |  | 302 |             return new JsonModel($data);
 | 
        
           |  |  | 303 |         }
 | 
        
           |  |  | 304 |   | 
        
           |  |  | 305 |         $organizationalClimateTestMapper = OrganizationalClimateTestMapper::getInstance($this->adapter);
 | 
        
           |  |  | 306 |         $organizationalClimate = $organizationalClimateTestMapper->fetchOne($id);
 | 
        
           |  |  | 307 |         if (!$organizationalClimate) {
 | 
        
           |  |  | 308 |             $data = [
 | 
        
           |  |  | 309 |                 'success' => false,
 | 
        
           |  |  | 310 |                 'data' => 'ERROR_RECORD_NOT_FOUND'
 | 
        
           |  |  | 311 |             ];
 | 
        
           |  |  | 312 |   | 
        
           |  |  | 313 |             return new JsonModel($data);
 | 
        
           |  |  | 314 |         }
 | 
        
           |  |  | 315 |   | 
        
           |  |  | 316 |         if ($organizationalClimate->company_id != $currentCompany->id) {
 | 
        
           |  |  | 317 |             return new JsonModel([
 | 
        
           |  |  | 318 |                 'success' => false,
 | 
        
           |  |  | 319 |                 'data' => 'ERROR_UNAUTHORIZED'
 | 
        
           |  |  | 320 |             ]);
 | 
        
           |  |  | 321 |         }
 | 
        
           |  |  | 322 |   | 
        
           |  |  | 323 |         if ($request->isPost()) {
 | 
        
           |  |  | 324 |   | 
        
           |  |  | 325 |             $result = $organizationalClimateTestMapper->delete($organizationalClimate->id);
 | 
        
           |  |  | 326 |             if ($result) {
 | 
        
           |  |  | 327 |                // $this->logger->info('Se borro el formulario ' . $organizationalClimate->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
 | 
        
           |  |  | 328 |   | 
        
           |  |  | 329 |                 $data = [
 | 
        
           |  |  | 330 |                     'success' => true,
 | 
        
           |  |  | 331 |                     'data' => 'LABEL_RECORD_DELETED'
 | 
        
           |  |  | 332 |                 ];
 | 
        
           |  |  | 333 |             } else {
 | 
        
           |  |  | 334 |   | 
        
           |  |  | 335 |                 $data = [
 | 
        
           |  |  | 336 |                     'success' => false,
 | 
        
           |  |  | 337 |                     'data' => $organizationalClimateTestMapper->getError()
 | 
        
           |  |  | 338 |                 ];
 | 
        
           |  |  | 339 |   | 
        
           |  |  | 340 |                 return new JsonModel($data);
 | 
        
           |  |  | 341 |             }
 | 
        
           |  |  | 342 |         } else {
 | 
        
           |  |  | 343 |             $data = [
 | 
        
           |  |  | 344 |                 'success' => false,
 | 
        
           |  |  | 345 |                 'data' => 'ERROR_METHOD_NOT_ALLOWED'
 | 
        
           |  |  | 346 |             ];
 | 
        
           |  |  | 347 |   | 
        
           |  |  | 348 |             return new JsonModel($data);
 | 
        
           |  |  | 349 |         }
 | 
        
           |  |  | 350 |   | 
        
           |  |  | 351 |         return new JsonModel($data);
 | 
        
           |  |  | 352 |         } catch (\Throwable $e) {
 | 
        
           |  |  | 353 |             $e->getMessage();
 | 
        
           |  |  | 354 |             return new JsonModel([
 | 
        
           |  |  | 355 |                 'success' => false,
 | 
        
           |  |  | 356 |                 'data' => $e
 | 
        
           |  |  | 357 |             ]);
 | 
        
           |  |  | 358 |         }
 | 
        
           |  |  | 359 |   | 
        
           |  |  | 360 |     }
 | 
        
           |  |  | 361 |   | 
        
           |  |  | 362 |     public function reportAction() {
 | 
        
           |  |  | 363 |         $request = $this->getRequest();
 | 
        
           |  |  | 364 |         $currentUserPlugin = $this->plugin('currentUserPlugin');
 | 
        
           |  |  | 365 |         $currentCompany = $currentUserPlugin->getCompany();
 | 
        
           |  |  | 366 |         $currentUser = $currentUserPlugin->getUser();
 | 
        
           |  |  | 367 |   | 
        
           |  |  | 368 |         $request = $this->getRequest();
 | 
        
           |  |  | 369 |         $uuid = $this->params()->fromRoute('uuid');
 | 
        
           |  |  | 370 |   | 
        
           |  |  | 371 |   | 
        
           |  |  | 372 |   | 
        
           |  |  | 373 |         if (!$uuid) {
 | 
        
           |  |  | 374 |             $data = [
 | 
        
           |  |  | 375 |                 'success' => false,
 | 
        
           |  |  | 376 |                 'data' => 'ERROR_INVALID_PARAMETER'
 | 
        
           |  |  | 377 |             ];
 | 
        
           |  |  | 378 |   | 
        
           |  |  | 379 |             return new JsonModel($data);
 | 
        
           |  |  | 380 |         }
 | 
        
           |  |  | 381 |   | 
        
           |  |  | 382 |         $organizationalClimateTestMapper = OrganizationalClimateTestMapper::getInstance($this->adapter);
 | 
        
           |  |  | 383 |         $organizationalClimateTest = $organizationalClimateTestMapper->fetchOneByuuid($uuid);
 | 
        
           |  |  | 384 |   | 
        
           |  |  | 385 |   | 
        
           |  |  | 386 |         $organizationalClimateMapper = OrganizationalClimateMapper::getInstance($this->adapter);
 | 
        
           |  |  | 387 |         $organizationalClimate = $organizationalClimateMapper->fetchOne($organizationalClimateTest->organizationalClimate_id);
 | 
        
           |  |  | 388 |   | 
        
           |  |  | 389 |         if (!$organizationalClimateTest) {
 | 
        
           |  |  | 390 |             $data = [
 | 
        
           |  |  | 391 |                 'success' => false,
 | 
        
           |  |  | 392 |                 'data' => 'ERROR_RECORD_NOT_FOUND'
 | 
        
           |  |  | 393 |             ];
 | 
        
           |  |  | 394 |   | 
        
           |  |  | 395 |             return new JsonModel($data);
 | 
        
           |  |  | 396 |         }
 | 
        
           |  |  | 397 |   | 
        
           |  |  | 398 |   | 
        
           |  |  | 399 |         if ($request->isGet()) {
 | 
        
           |  |  | 400 |             $hydrator = new ObjectPropertyHydrator();
 | 
        
           |  |  | 401 |   | 
        
           |  |  | 402 |             //get form data
 | 
        
           |  |  | 403 |             $organizationalClimateFormMapper = OrganizationalClimateFormMapper::getInstance($this->adapter);
 | 
        
           |  |  | 404 |             $organizationalClimateForm = $organizationalClimateFormMapper->fetchOne($organizationalClimate->form_id);
 | 
        
           |  |  | 405 |   | 
        
           |  |  | 406 |             if ($organizationalClimateForm) {
 | 
        
           |  |  | 407 |   | 
        
           |  |  | 408 |                 return $this->renderPDF($organizationalClimateForm, $organizationalClimateTest, $organizationalClimate);
 | 
        
           |  |  | 409 |             } else {
 | 
        
           |  |  | 410 |   | 
        
           |  |  | 411 |                 $data = [
 | 
        
           |  |  | 412 |                     'success' => false,
 | 
        
           |  |  | 413 |                     'data' => 'ERROR_METHOD_NOT_ALLOWED'
 | 
        
           |  |  | 414 |                 ];
 | 
        
           |  |  | 415 |   | 
        
           |  |  | 416 |                 return new JsonModel($data);
 | 
        
           |  |  | 417 |             }
 | 
        
           |  |  | 418 |         } else {
 | 
        
           |  |  | 419 |             $data = [
 | 
        
           |  |  | 420 |                 'success' => false,
 | 
        
           |  |  | 421 |                 'data' => 'ERROR_METHOD_NOT_ALLOWED'
 | 
        
           |  |  | 422 |             ];
 | 
        
           |  |  | 423 |   | 
        
           |  |  | 424 |             return new JsonModel($data);
 | 
        
           |  |  | 425 |         }
 | 
        
           |  |  | 426 |   | 
        
           |  |  | 427 |         return new JsonModel($data);
 | 
        
           |  |  | 428 |     }
 | 
        
           |  |  | 429 |   | 
        
           |  |  | 430 |     public function renderPDF($organizationalClimateForm, $organizationalClimateTest, $organizationalClimate) {
 | 
        
           |  |  | 431 |   | 
        
           |  |  | 432 |         $target_path = $this->config['leaderslinked.fullpath.company'] . DIRECTORY_SEPARATOR . $organizationalClimate->uuid;
 | 
        
           |  |  | 433 |   | 
        
           |  |  | 434 |   | 
        
           |  |  | 435 |         if(file_exists($target_path)) {
 | 
        
           |  |  | 436 |             Functions::deleteFiles($target_path);
 | 
        
           |  |  | 437 |         } else {
 | 
        
           |  |  | 438 |             @mkdir($target_path, 0755, true);
 | 
        
           |  |  | 439 |         }
 | 
        
           |  |  | 440 |   | 
        
           |  |  | 441 |         // Set Data
 | 
        
           |  |  | 442 |         $headerFormName = utf8_decode($organizationalClimateForm->name);
 | 
        
           |  |  | 443 |         $headerOrganizationalClimateName = utf8_decode('Informe de Encuesta: ' . trim($organizationalClimate->name) . ' al ' . date("m-d-Y H:i:s", strtotime($organizationalClimate->added_on)));
 | 
        
           |  |  | 444 |         $sections = json_decode($organizationalClimateForm->content, true);
 | 
        
           |  |  | 445 |   | 
        
           |  |  | 446 |         $test = json_decode($organizationalClimateTest->content, true);
 | 
        
           |  |  | 447 |   | 
        
           |  |  | 448 |         //Generate New PDF
 | 
        
           |  |  | 449 |         $pdf = new UniqueSurveyReport();
 | 
        
           |  |  | 450 |   | 
        
           |  |  | 451 |         $pdf->AliasNbPages();
 | 
        
           |  |  | 452 |         $pdf->AddPage();
 | 
        
           |  |  | 453 |   | 
        
           |  |  | 454 |         // Set header secundary
 | 
        
           |  |  | 455 |         $pdf->customHeader($headerFormName, $headerOrganizationalClimateName);
 | 
        
           |  |  | 456 |   | 
        
           |  |  | 457 |         $countSection = 0;
 | 
        
           |  |  | 458 |   | 
        
           |  |  | 459 |         for ($i = 0; $i < count($sections); $i++) {
 | 
        
           |  |  | 460 |             if ($countSection > 1) {
 | 
        
           |  |  | 461 |                 $countSection = 0;
 | 
        
           |  |  | 462 |                 $pdf->AddPage();
 | 
        
           |  |  | 463 |                 $pdf->customHeader($headerFormName);
 | 
        
           |  |  | 464 |             }
 | 
        
           |  |  | 465 |             $section = $sections[$i];
 | 
        
           |  |  | 466 |   | 
        
           |  |  | 467 |             foreach ($section['questions'] as $question) {
 | 
        
           |  |  | 468 |   | 
        
           |  |  | 469 |                     switch ($question['type']) {
 | 
        
           |  |  | 470 |                         case 'simple':
 | 
        
           |  |  | 471 |                             $options = [];
 | 
        
           |  |  | 472 |   | 
        
           |  |  | 473 |                             foreach($question['options'] as $option) {
 | 
        
           |  |  | 474 |                                 $options []= [
 | 
        
           |  |  | 475 |                                     'text' => strip_tags($option['text']),
 | 
        
           |  |  | 476 |                                     'selected' => $option['slug_option'] == $test[$question['slug_question']],
 | 
        
           |  |  | 477 |                                 ];
 | 
        
           |  |  | 478 |                             }
 | 
        
           |  |  | 479 |   | 
        
           |  |  | 480 |                             $pdf->optionTable(strip_tags($question['text']), $options);
 | 
        
           |  |  | 481 |   | 
        
           |  |  | 482 |                             break;
 | 
        
           |  |  | 483 |   | 
        
           |  |  | 484 |                         case 'multiple':
 | 
        
           |  |  | 485 |                             $options = [];
 | 
        
           |  |  | 486 |   | 
        
           |  |  | 487 |                             foreach($question['options'] as $option) {
 | 
        
           |  |  | 488 |                                 $options []= [
 | 
        
           |  |  | 489 |                                     'text' => strip_tags($option['text']),
 | 
        
           |  |  | 490 |                                     'selected' => in_array($option['slug_option'], $test[$question['slug_question']]),
 | 
        
           |  |  | 491 |                                 ];
 | 
        
           |  |  | 492 |                             }
 | 
        
           |  |  | 493 |   | 
        
           |  |  | 494 |                             $pdf->optionTable(strip_tags($question['text']), $options);
 | 
        
           |  |  | 495 |   | 
        
           |  |  | 496 |                             break;
 | 
        
           |  |  | 497 |                         case 'open':
 | 
        
           |  |  | 498 |                             $pdf->borderTable(strip_tags($question['text']), [['content' => $test[$question['slug_question']]]]);
 | 
        
           |  |  | 499 |   | 
        
           |  |  | 500 |   | 
        
           |  |  | 501 |                             break;
 | 
        
           |  |  | 502 |                     }
 | 
        
           |  |  | 503 |                 $countSection++;
 | 
        
           |  |  | 504 |             }
 | 
        
           |  |  | 505 |   | 
        
           |  |  | 506 |   | 
        
           |  |  | 507 |         }
 | 
        
           |  |  | 508 |   | 
        
           |  |  | 509 |         return $pdf->Output();
 | 
        
           |  |  | 510 |     }
 | 
        
           |  |  | 511 |   | 
        
           |  |  | 512 |   | 
        
           |  |  | 513 | }
 |