Proyectos de Subversion LeadersLinked - Backend

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
8780 eleazar 1
<?php
2
 
3
declare(strict_types=1);
4
 
5
namespace LeadersLinked\Controller;
6
 
7
use Laminas\Db\Adapter\AdapterInterface;
8
use LeadersLinked\Library\InterviewPDF;
9
use Laminas\Cache\Storage\Adapter\AbstractAdapter;
10
use Laminas\Mvc\Controller\AbstractActionController;
11
use Laminas\Log\LoggerInterface;
12
use Laminas\View\Model\JsonModel;
13
use LeadersLinked\Form\RecruitmentSelectionInterviewFormForm;
14
use LeadersLinked\Form\InterviewFileForm;
15
use LeadersLinked\Form\JobDescriptionForm;
16
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
17
use LeadersLinked\Library\Functions;
18
use LeadersLinked\Mapper\CompanyMapper;
19
use LeadersLinked\Mapper\CompetencyTypeMapper;
20
use LeadersLinked\Model\JobDescription;
21
use LeadersLinked\Mapper\CompetencyMapper;
22
use LeadersLinked\Mapper\BehaviorCompetencyMapper;
23
use LeadersLinked\Model\BehaviorCompetency;
24
use LeadersLinked\Library\JobPdf;
25
use LeadersLinked\Mapper\JobDescriptionCompetencyMapper;
26
use LeadersLinked\Mapper\JobDescriptionSubordinateMapper;
27
use LeadersLinked\Model\JobDescriptionCompetency;
28
use LeadersLinked\Model\JobDescriptionBehaviorCompetency;
29
use LeadersLinked\Mapper\JobDescriptionBehaviorCompetencyMapper;
30
use LeadersLinked\Model\JobDescriptionSubordinate;
31
use LeadersLinked\Mapper\BehaviorMapper;
32
use LeadersLinked\Model\Behavior;
33
use LeadersLinked\Model\Company;
34
use LeadersLinked\Mapper\RecruitmentSelectionInterviewMapper;
35
use LeadersLinked\Mapper\RecruitmentSelectionInterviewFileMapper;
36
use LeadersLinked\Model\RecruitmentSelectionInterview;
37
use LeadersLinked\Model\RecruitmentSelectionInterviewFile;
38
use LeadersLinked\Mapper\RecruitmentSelectionVacancyMapper;
39
use LeadersLinked\Model\RecruitmentSelectionVacancy;
40
use LeadersLinked\Mapper\RecruitmentSelectionCandidateMapper;
41
use LeadersLinked\Model\RecruitmentSelectionCandidate;
42
use LeadersLinked\Mapper\JobDescriptionMapper;
43
use Laminas\View\Model\ViewModel;
44
use LeadersLinked\Form\RecruitmentSelectionCandidateFormForm;
45
use Laminas\Hydrator\ArraySerializableHydrator;
46
use Laminas\Db\ResultSet\HydratingResultSet;
47
use LeadersLinked\Mapper\QueryMapper;
48
use Laminas\Paginator\Adapter\DbSelect;
49
use Laminas\Paginator\Paginator;
50
 
8798 eleazar 51
class RecruitmentSelectionInterviewFileController extends AbstractActionController {
8780 eleazar 52
 
53
    /**
54
     *
55
     * @var AdapterInterface
56
     */
57
    private $adapter;
58
 
59
    /**
60
     *
61
     * @var AbstractAdapter
62
     */
63
    private $cache;
64
 
65
    /**
66
     *
67
     * @var  LoggerInterface
68
     */
69
    private $logger;
70
 
71
    /**
72
     *
73
     * @var array
74
     */
75
    private $config;
76
 
77
    /**
78
     *
79
     * @param AdapterInterface $adapter
80
     * @param AbstractAdapter $cache
81
     * @param LoggerInterface $logger
82
     * @param array $config
83
     */
84
    public function __construct($adapter, $cache, $logger, $config) {
85
        $this->adapter = $adapter;
86
        $this->cache = $cache;
87
        $this->logger = $logger;
88
        $this->config = $config;
89
    }
90
 
91
    public function indexAction()
92
    {
8847 eleazar 93
 
8780 eleazar 94
        $request            = $this->getRequest();
95
        $currentUserPlugin  = $this->plugin('currentUserPlugin');
96
        $currentUser        = $currentUserPlugin->getUser();
97
        $currentCompany     = $currentUserPlugin->getCompany();
8802 eleazar 98
        $interview_uuid       = $this->params()->fromRoute('interview_uuid');
8850 eleazar 99
 
100
 
101
        $flashMessenger     = $this->plugin('FlashMessenger');
8852 eleazar 102
 
103
        $interviewMapper = RecruitmentSelectionInterviewMapper::getInstance($this->adapter);
104
        $interview = $interviewMapper->fetchOneByUuid($interview_uuid);
8854 eleazar 105
 
8780 eleazar 106
 
8811 eleazar 107
        if(!$interview) {
108
            $flashMessenger->addErrorMessage('ERROR_FORM_NOT_FOUND');
109
            return $this->redirect()->toRoute('dashboard');
110
        }
8812 eleazar 111
 
112
        if($interview->company_id != $currentCompany->id) {
113
            $flashMessenger->addErrorMessage('ERROR_UNAUTHORIZED');
114
            return $this->redirect()->toRoute('dashboard');
115
        }
8813 eleazar 116
 
8863 eleazar 117
        try{
8780 eleazar 118
        $request = $this->getRequest();
119
        if($request->isGet()) {
120
            $headers  = $request->getHeaders();
8814 eleazar 121
 
8780 eleazar 122
            $isJson = false;
123
            if($headers->has('Accept')) {
124
                $accept = $headers->get('Accept');
8815 eleazar 125
 
126
                $prioritized = $accept->getPrioritized();
8816 eleazar 127
 
8780 eleazar 128
                foreach($prioritized as $key => $value) {
129
                    $raw = trim($value->getRaw());
130
 
131
                    if(!$isJson) {
132
                        $isJson = strpos($raw, 'json');
133
                    }
134
 
135
                }
136
            }
8817 eleazar 137
 
8780 eleazar 138
            if($isJson) {
139
                $acl = $this->getEvent()->getViewModel()->getVariable('acl');
8787 eleazar 140
                $allowAdd  = $acl->isAllowed($currentUser->usertype_id, 'recruitment-and-selection/interview/file/add');
141
                $allowEdit  = $acl->isAllowed($currentUser->usertype_id, 'recruitment-and-selection/interview/file/edit');
142
                $allowDelete = $acl->isAllowed($currentUser->usertype_id, 'recruitment-and-selection/interview/file/delete');
8818 eleazar 143
 
8780 eleazar 144
 
145
                $search = $this->params()->fromQuery('search', []);
146
                $search = empty($search['value']) ? '' : filter_var($search['value'], FILTER_SANITIZE_STRING);
147
 
148
                $page               = intval($this->params()->fromQuery('start', 1), 10);
149
                $records_x_page     = intval($this->params()->fromQuery('length', 10), 10);
150
                $order =  $this->params()->fromQuery('order', []);
151
                $order_field        = empty($order[0]['column']) ? 99 :  intval($order[0]['column'], 10);
152
                $order_direction    = empty($order[0]['dir']) ? 'ASC' : strtoupper(filter_var( $order[0]['dir'], FILTER_SANITIZE_STRING));
153
 
154
                $fields =  ['title'];
8805 eleazar 155
                $order_field = isset($fields[$order_field]) ? $fields[$order_field] : 'title';
8780 eleazar 156
 
157
                if(!in_array($order_direction, ['ASC', 'DESC'])) {
158
                    $order_direction = 'ASC';
8819 eleazar 159
                }
8780 eleazar 160
 
8819 eleazar 161
                $fileMapper = RecruitmentSelectionInterviewFileMapper::getInstance($this->adapter);
162
                $paginator = $fileMapper->fetchAllDataTableByInterviewId($interview->id, $search, $page, $records_x_page, $order_field, $order_direction);
8859 eleazar 163
 
164
                $items = [];
165
                $records = $paginator->getCurrentItems();
8862 eleazar 166
 
8859 eleazar 167
                foreach($records as $record)
8780 eleazar 168
                {
169
                    $params = [
8844 eleazar 170
                        'interview_uuid' => $interview->uuid,
8780 eleazar 171
                        'id' => $record->uuid,
172
                    ];
173
 
174
                    $item = [
8871 eleazar 175
                        'interview_uuid' => $interview->uuid,
176
                        'id' => $record->uuid,
8780 eleazar 177
                        'title' => $record->title,
178
                        'coment' => $record->coment,
179
                        'actions' => [
8787 eleazar 180
                            'link_edit'   => $allowEdit ? $this->url()->fromRoute('recruitment-and-selection/interview/file/edit', $params) : '',
181
                            'link_delete' => $allowDelete ? $this->url()->fromRoute('recruitment-and-selection/interview/file/delete', $params) : '',
8780 eleazar 182
                        ],
183
                    ];
184
                    array_push($items, $item);
185
                }
186
 
8871 eleazar 187
 
188
                $data = [];
189
                $data['items'] = $items;
190
                $data['total'] = $paginator->getTotalItemCount();
8875 eleazar 191
 
8780 eleazar 192
                $response = [
193
                    'success' => true,
194
                    'data' => $data
195
                ];
196
 
197
                return new JsonModel($response);
198
 
199
 
200
            } else {
8801 eleazar 201
                $form = new InterviewFileForm($this->adapter, $currentCompany ? $currentCompany->id : null);
8780 eleazar 202
 
203
                $this->layout()->setTemplate('layout/layout-backend.phtml');
204
                $viewModel = new ViewModel();
205
                $viewModel->setTemplate('leaders-linked/recruitment-and-selection-interview-file/index.phtml');
206
                $viewModel->setVariables([
8865 eleazar 207
                    'interview_uuid' => $interview->uuid,
8780 eleazar 208
                    'form' => $form,
209
                ]);
210
                return $viewModel ;
211
            }
212
 
213
        } else {
214
            return new JsonModel([
215
                'success' => false,
216
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
217
            ]);
218
        }
8863 eleazar 219
    } catch (\Throwable $e) {
220
        $e->getMessage();
221
        return new JsonModel([
222
            'success' => false,
223
            'data' => $e
224
        ]);
225
    }
8847 eleazar 226
 
8780 eleazar 227
    }
228
 
229
    public function addAction()
230
    {
231
        $currentUserPlugin  = $this->plugin('currentUserPlugin');
232
        $currentCompany     = $currentUserPlugin->getCompany();
233
        $currentUser        = $currentUserPlugin->getUser();
234
 
235
        $request    = $this->getRequest();
236
 
237
        $interview_id = $this->params()->fromRoute('interview_uuid');
8838 eleazar 238
        $recruitmentSelectionInterviewMapper = RecruitmentSelectionInterviewMapper::getInstance($this->adapter);
239
        $interview = $recruitmentSelectionInterviewMapper->fetchOneByUuid($interview_id);
8837 eleazar 240
     try{
8780 eleazar 241
        if($request->isPost()) {
242
            $form = new  InterviewFileForm($this->adapter, $currentCompany->id);
8831 eleazar 243
 
244
            $dataPost = array_merge(
245
                $request->getPost()->toArray(),
246
                $request->getFiles()->toArray(),
247
            );
8833 eleazar 248
 
249
            $form->setData($dataPost);
8834 eleazar 250
 
8780 eleazar 251
            if($form->isValid()) {
8835 eleazar 252
 
253
                $dataPost = (array) $form->getData();
8837 eleazar 254
 
8780 eleazar 255
                $files = $this->getRequest()->getFiles()->toArray();
256
 
257
                $hydrator = new ObjectPropertyHydrator();
258
                $file = new RecruitmentSelectionInterviewFile();
259
                $hydrator->hydrate($dataPost, $file);
260
 
261
                $file->company_id = $currentCompany->id;
8839 eleazar 262
                $file->interview_id = $interview->id;
8780 eleazar 263
                $file->file = null;
264
 
8828 eleazar 265
                $fileMapper = RecruitmentSelectionInterviewFileMapper::getInstance($this->adapter);
8780 eleazar 266
 
267
                if($fileMapper->insert($file)) {
268
                     $file = $fileMapper->fetchOne($file->id);
269
 
270
                    //leaderslinked.fullpath.recruitment_selection/uuid vacante/uuid candidato
271
 
272
                     $target_path = $this->config['leaderslinked.fullpath.recruitment_selection'].$interview_id;
273
                     if(!file_exists($target_path)) {
274
                         mkdir($target_path, 0755, true);
275
                     }
276
                    $files = $this->getRequest()->getFiles()->toArray();
277
                     if(isset($files['file']) && empty($files['file']['error'])) {
278
                         $tmp_filename  = $files['file']['tmp_name'];
279
                         $original_filename  = trim(strtolower($files['file']['name']));
280
                        try {
281
                            $parts = explode('.', $original_filename);
282
                            $filename = 'document-' . uniqid() . '.' . $parts[ count($parts) - 1 ];
283
 
284
                            $full_filename = $target_path  . DIRECTORY_SEPARATOR .$filename;
285
                            if(move_uploaded_file($tmp_filename, $full_filename)) {
286
                                 $file->file = $filename;
287
                                 $fileMapper->update($file);
288
                             }
289
                         } catch(\Throwable $e) {
290
                             error_log($e->getTraceAsString());
291
                         }
292
                     }
293
 
294
                    $this->logger->info('Se agrego el candidato ' . $file->title, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
295
 
296
                    $data = [
297
                        'success'   => true,
298
                        'data'   => 'LABEL_RECORD_ADDED'
299
                    ];
300
                } else {
301
                    $data = [
302
                        'success'   => false,
303
                        'data'      => $fileMapper->getError()
304
                    ];
305
 
306
                }
307
 
308
                return new JsonModel($data);
309
 
310
            } else {
311
                $messages = [];
312
                $form_messages = (array) $form->getMessages();
313
                foreach($form_messages  as $fieldname => $field_messages)
314
                {
315
 
316
                    $messages[$fieldname] = array_values($field_messages);
317
                }
318
 
319
            }
320
 
321
        } else {
322
            $data = [
323
                'success' => false,
324
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
325
            ];
326
 
327
            return new JsonModel($data);
328
        }
8837 eleazar 329
      } catch (\Throwable $e) {
330
          $e->getMessage();
331
          return new JsonModel([
332
              'success' => false,
333
              'data' => $e
334
          ]);
335
      }
8780 eleazar 336
    }
337
 
338
    public function editAction() {
339
        $request = $this->getRequest();
340
        $currentUserPlugin = $this->plugin('currentUserPlugin');
341
        $currentCompany = $currentUserPlugin->getCompany();
342
        $currentUser = $currentUserPlugin->getUser();
343
 
344
        $request = $this->getRequest();
345
        $id = $this->params()->fromRoute('id');
346
        $interview_id = $this->params()->fromRoute('interview_uuid');
347
 
348
        if (!$id) {
349
            $data = [
350
                'success' => false,
351
                'data' => 'ERROR_INVALID_PARAMETER'
352
            ];
353
 
354
            return new JsonModel($data);
355
        }
356
 
357
        $fileMapper = RecruitmentSelectionInterviewFileMapper::getInstance($this->adapter);
8876 eleazar 358
        $file = $fileMapper->fetchOneByUuid($id);
8780 eleazar 359
 
360
        if (!$file) {
361
            $data = [
362
                'success' => false,
363
                'data' => 'ERROR_RECORD_NOT_FOUND'
364
            ];
365
 
366
            return new JsonModel($data);
367
        }
368
 
369
        if ($file->company_id != $currentCompany->id) {
370
            return new JsonModel([
371
                'success' => false,
372
                'data' => 'ERROR_UNAUTHORIZED'
373
            ]);
374
        }
375
 
376
        if ($request->isPost()) {
377
            $form = new InterviewFileForm($this->adapter, $currentCompany->id);
378
            $dataPost = $request->getPost()->toArray();
379
 
380
            $form->setData($dataPost);
381
 
382
            if ($form->isValid()) {
383
                $dataPost = (array) $form->getData();
384
 
385
                $hydrator = new ObjectPropertyHydrator();
386
                $hydrator->hydrate($dataPost, $file);
387
 
388
                if($fileMapper->update($file)) {
389
                    $file = $fileMapper->fetchOne($file->id);
390
 
391
                   //leaderslinked.fullpath.recruitment_selection/uuid vacante/uuid candidato
392
 
393
                    $target_path = $this->config['leaderslinked.fullpath.recruitment_selection'].$interview_id;
394
                    if(!file_exists($target_path)) {
395
                        mkdir($target_path, 0755, true);
396
                    }
397
 
398
                    $files = $this->getRequest()->getFiles()->toArray();
399
 
400
                    if(isset($files['file']) && empty($files['file']['error'])) {
401
                        $tmp_filename  = $files['file']['tmp_name'];
402
                        $original_filename  = trim(strtolower($files['file']['name']));
403
 
404
                        try {
405
                            $parts = explode('.', $original_filename);
406
                            $filename = 'document-' . uniqid() . '.' . $parts[ count($parts) - 1 ];
407
 
408
                            $full_filename = $target_path  . DIRECTORY_SEPARATOR .$filename;
409
                            if(move_uploaded_file($tmp_filename, $full_filename)) {
410
                                $file->file = $filename;
411
                                $fileMapper->update($file);
412
                            }
413
                        } catch(\Throwable $e) {
414
                            error_log($e->getTraceAsString());
415
                        }
416
                    }
417
 
418
                   $this->logger->info('Se agrego el archivo ' . $file->title, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
419
 
420
                   $data = [
421
                       'success'   => true,
422
                       'data'   => 'LABEL_RECORD_UPDATED'
423
                   ];
424
               } else {
425
                   $data = [
426
                       'success'   => false,
427
                       'data'      => $fileMapper->getError()
428
                   ];
429
 
430
               }
431
 
432
                return new JsonModel($data);
433
            } else {
434
                $messages = [];
435
                $form_messages = (array) $form->getMessages();
436
                foreach ($form_messages as $fieldname => $field_messages) {
437
                    $messages[$fieldname] = array_values($field_messages);
438
                }
439
 
440
                return new JsonModel([
441
                    'success' => false,
442
                    'data' => $messages
443
                ]);
444
            }
445
        } else if ($request->isGet()) {
446
 
447
            $hydrator = new ObjectPropertyHydrator();
448
 
449
            $data = [
450
                'success' => true,
451
                'data' => [
452
                    'id' => $file->uuid,
453
                    'title' => $file->title,
8896 eleazar 454
                    'coment' => strip_tags($file->coment),
8877 eleazar 455
                    'file' => $this->url()->fromRoute('storage', ['type' => 'recruitment-selection', 'code'=> $interview_id, 'filename' => $file->file]),
8780 eleazar 456
                ]
457
            ];
458
 
459
            return new JsonModel($data);
460
        } else {
461
            $data = [
462
                'success' => false,
463
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
464
            ];
465
 
466
            return new JsonModel($data);
467
        }
468
 
469
        return new JsonModel($data);
470
    }
471
 
472
    public function deleteAction() {
473
        $request = $this->getRequest();
474
        $currentUserPlugin = $this->plugin('currentUserPlugin');
475
        $currentCompany = $currentUserPlugin->getCompany();
476
        $currentUser = $currentUserPlugin->getUser();
477
 
478
        $request = $this->getRequest();
8873 eleazar 479
        $id = $this->params()->fromRoute('id');
8780 eleazar 480
 
8873 eleazar 481
        if (!$id) {
8780 eleazar 482
            $data = [
483
                'success' => false,
484
                'data' => 'ERROR_INVALID_PARAMETER'
485
            ];
486
 
487
            return new JsonModel($data);
488
        }
489
 
490
        $fileMapper = RecruitmentSelectionInterviewFileMapper::getInstance($this->adapter);
8873 eleazar 491
        $file = $fileMapper->fetchOneByUuid($id);
8780 eleazar 492
        if (!$file) {
493
            $data = [
494
                'success' => false,
495
                'data' => 'ERROR_RECORD_NOT_FOUND'
496
            ];
497
 
498
            return new JsonModel($data);
499
        }
500
 
501
        if ($file->company_id != $currentCompany->id) {
502
            return new JsonModel([
503
                'success' => false,
504
                'data' => 'ERROR_UNAUTHORIZED'
505
            ]);
506
        }
507
 
508
        if ($request->isPost()) {
509
 
510
 
511
            $result = $fileMapper->delete($file->id);
512
            if ($result) {
513
                $this->logger->info('Se borro el archivo ' . $file->title, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
514
 
515
                $data = [
516
                    'success' => true,
517
                    'data' => 'LABEL_RECORD_DELETED'
518
                ];
519
            } else {
520
 
521
                $data = [
522
                    'success' => false,
523
                    'data' => $fileMapper->getError()
524
                ];
525
 
526
                return new JsonModel($data);
527
            }
528
        } else {
529
            $data = [
530
                'success' => false,
531
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
532
            ];
533
 
534
            return new JsonModel($data);
535
        }
536
 
537
        return new JsonModel($data);
538
    }
539
}