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