Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 16768 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
15461 efrain 1
<?php
2
 
3
declare(strict_types=1);
4
 
5
namespace LeadersLinked\Controller;
6
 
7
use Laminas\Db\Adapter\AdapterInterface;
16768 efrain 8
 
15461 efrain 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\RecruitmentSelectionApplicationMapper;
16
use LeadersLinked\Mapper\RecruitmentSelectionFileMapper;
17
use LeadersLinked\Mapper\RecruitmentSelectionInterviewMapper;
18
use LeadersLinked\Mapper\RecruitmentSelectionVacancyMapper;
19
use LeadersLinked\Mapper\RecruitmentSelectionCandidateMapper;
20
use LeadersLinked\Model\RecruitmentSelectionApplication;
21
use Laminas\View\Model\ViewModel;
22
use LeadersLinked\Form\RecruitmentSelection\RecruitmentSelectionApplicationFileForm;
23
use LeadersLinked\Model\RecruitmentSelectionCandidate;
24
use LeadersLinked\Model\RecruitmentSelectionFile;
25
use LeadersLinked\Model\RecruitmentSelectionVacancy;
26
 
27
class RecruitmentSelectionFileController extends AbstractActionController {
28
 
29
    /**
30
     *
16769 efrain 31
     * @var \Laminas\Db\Adapter\AdapterInterface
15461 efrain 32
     */
33
    private $adapter;
16768 efrain 34
 
15461 efrain 35
    /**
36
     *
16769 efrain 37
     * @var \LeadersLinked\Cache\CacheInterface
15461 efrain 38
     */
16769 efrain 39
    private $cache;
40
 
41
 
42
    /**
43
     *
44
     * @var \Laminas\Log\LoggerInterface
45
     */
15461 efrain 46
    private $logger;
16768 efrain 47
 
15461 efrain 48
    /**
49
     *
50
     * @var array
51
     */
52
    private $config;
16768 efrain 53
 
16769 efrain 54
 
15461 efrain 55
    /**
56
     *
16769 efrain 57
     * @var \Laminas\Mvc\I18n\Translator
58
     */
59
    private $translator;
60
 
61
 
62
    /**
63
     *
64
     * @param \Laminas\Db\Adapter\AdapterInterface $adapter
65
     * @param \LeadersLinked\Cache\CacheInterface $cache
66
     * @param \Laminas\Log\LoggerInterface LoggerInterface $logger
15461 efrain 67
     * @param array $config
16769 efrain 68
     * @param \Laminas\Mvc\I18n\Translator $translator
15461 efrain 69
     */
16769 efrain 70
    public function __construct($adapter, $cache, $logger, $config, $translator)
16768 efrain 71
    {
16769 efrain 72
        $this->adapter      = $adapter;
73
        $this->cache        = $cache;
74
        $this->logger       = $logger;
75
        $this->config       = $config;
76
        $this->translator   = $translator;
15461 efrain 77
    }
78
 
79
    public function indexAction()
80
    {
81
            $data = [
82
                'success' => false,
83
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
84
            ];
85
 
86
            return new JsonModel($data);
87
    }
88
 
89
    public function addAction()
90
    {
91
        $request = $this->getRequest();
92
        $currentUserPlugin = $this->plugin('currentUserPlugin');
93
        $currentCompany = $currentUserPlugin->getCompany();
94
        $currentUser = $currentUserPlugin->getUser();
95
 
96
        $request = $this->getRequest();
97
        $application_id = $this->params()->fromRoute('application_id');
98
 
99
        $vacancy_uuid = $this->params()->fromRoute('vacancy_id');
100
 
101
        $vacancyMapper = RecruitmentSelectionVacancyMapper::getInstance($this->adapter);
102
        $vacancy = $vacancyMapper->fetchOneByUuid($vacancy_uuid);
103
 
104
        if(!$vacancy) {
105
            $data = [
106
                'success' => false,
107
                'data' => 'ERROR_VACANCY_NOT_FOUND'
108
            ];
109
 
110
            return new JsonModel($data);
111
        }
112
 
113
 
114
        if($vacancy->company_id != $currentCompany->id) {
115
            $data = [
116
                'success' => false,
117
                'data' => 'ERROR_VACANCY_IS_OTHER_COMPANY',
118
            ];
119
 
120
            return new JsonModel($data);
121
        }
122
 
123
 
124
 
125
        $applicationMapper = RecruitmentSelectionApplicationMapper::getInstance($this->adapter);
126
        $application = $applicationMapper->fetchOneByUuid($application_id);
127
 
128
 
129
        if (!$application) {
130
            $data = [
131
                'success' => false,
132
                'data' => 'ERROR_RECORD_NOT_FOUND'
133
            ];
134
 
135
            return new JsonModel($data);
136
        }
137
 
138
 
139
        if ($application->vacancy_id != $vacancy->id) {
140
            return new JsonModel([
141
                'success' => false,
142
                'data' => 'ERROR_UNAUTHORIZED'
143
            ]);
144
        }
145
 
146
        if($request->isPost()) {
147
            $form = new  RecruitmentSelectionApplicationFileForm();
148
 
149
            $dataPost = array_merge(
150
                $request->getPost()->toArray(),
151
                $request->getFiles()->toArray(),
152
            );
153
 
154
            $form->setData($dataPost);
155
 
156
            if($form->isValid()) {
157
 
158
                $dataPost = (array) $form->getData();
159
                $files = $this->getRequest()->getFiles()->toArray();
160
 
161
                //if(!isset($files['file']) && empty($files['file']['error'])) {
162
 
15464 efrain 163
                $original_filename = Functions::normalizeStringFilename($files['file']['name']);
15461 efrain 164
 
165
                $file = new RecruitmentSelectionFile();
166
                $file->name = $dataPost['name'];
167
                $file->company_id = $currentCompany->id;
168
                $file->application_id = $application->id;
169
                $file->vacancy_id = $vacancy->id;
170
                $file->file = $original_filename;
171
 
172
                $fileMapper = RecruitmentSelectionFileMapper::getInstance($this->adapter);
173
 
174
                if($fileMapper->insert($file)) {
175
 
176
                     $target_path = $this->config['leaderslinked.fullpath.recruitment_selection'].$application->uuid;
177
                     if(!file_exists($target_path)) {
178
                         mkdir($target_path, 0755, true);
179
                     }
180
 
181
                     $target_filename = $target_path . DIRECTORY_SEPARATOR . $original_filename;
182
 
183
 
184
                     if(!move_uploaded_file($files['file']['tmp_name'], $target_filename)) {
185
 
186
                         $fileMapper->delete($file->id);
187
 
188
 
189
                         $data = [
190
                             'success'   => false,
191
                             'data'      =>  'ERROR_UPLOAD_FILE'
192
                         ];
193
 
194
                     } else {
195
 
196
 
197
                        $this->logger->info('Se agrego el archivo : '  . $file->name . ' (' . $original_filename . ')', ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
198
 
199
                        $data = [
200
                            'success'   => true,
201
                            'data'   => [
202
                                'message' => 'LABEL_RECORD_ADDED',
203
                                'files' => $this->renderFiles($vacancy, $application)
204
                             ],
205
                        ];
206
                     }
207
                } else {
208
                    $data = [
209
                        'success'   => false,
210
                        'data'      => $fileMapper->getError()
211
                    ];
212
 
213
                }
214
 
215
                return new JsonModel($data);
216
 
217
            } else {
218
                $messages = [];
219
                $form_messages = (array) $form->getMessages();
220
                foreach ($form_messages as $fieldname => $field_messages) {
221
                    $messages[$fieldname] = array_values($field_messages);
222
                }
223
 
224
                return new JsonModel([
225
                    'success' => false,
226
                    'data' => $messages
227
                ]);
228
 
229
            }
230
 
231
        } else {
232
            $data = [
233
                'success' => false,
234
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
235
            ];
236
 
237
            return new JsonModel($data);
238
        }
239
 
240
    }
241
 
242
 
243
 
244
 
245
 
246
    public function deleteAction()
247
    {
248
        $request = $this->getRequest();
249
        $currentUserPlugin = $this->plugin('currentUserPlugin');
250
        $currentCompany = $currentUserPlugin->getCompany();
251
        $currentUser = $currentUserPlugin->getUser();
252
 
253
        $request = $this->getRequest();
254
        $application_id = $this->params()->fromRoute('application_id');
255
        $vacancy_uuid   = $this->params()->fromRoute('vacancy_id');
256
        $id             = $this->params()->fromRoute('id');
257
 
258
        $vacancyMapper = RecruitmentSelectionVacancyMapper::getInstance($this->adapter);
259
        $vacancy = $vacancyMapper->fetchOneByUuid($vacancy_uuid);
260
 
261
        if(!$vacancy) {
262
            $data = [
263
                'success' => false,
264
                'data' => 'ERROR_VACANCY_NOT_FOUND'
265
            ];
266
 
267
            return new JsonModel($data);
268
        }
269
 
270
 
271
        if($vacancy->company_id != $currentCompany->id) {
272
            $data = [
273
                'success' => false,
274
                'data' => 'ERROR_VACANCY_IS_OTHER_COMPANY',
275
            ];
276
 
277
            return new JsonModel($data);
278
        }
279
 
280
 
281
 
282
        $applicationMapper = RecruitmentSelectionApplicationMapper::getInstance($this->adapter);
283
        $application = $applicationMapper->fetchOneByUuid($application_id);
284
 
285
 
286
        if (!$application) {
287
            $data = [
288
                'success' => false,
289
                'data' => 'ERROR_RECORD_NOT_FOUND'
290
            ];
291
 
292
            return new JsonModel($data);
293
        }
294
 
295
 
296
        if ($application->vacancy_id != $vacancy->id) {
297
            return new JsonModel([
298
                'success' => false,
299
                'data' => 'ERROR_UNAUTHORIZED'
300
            ]);
301
        }
302
 
303
 
304
        $fileMapper = RecruitmentSelectionFileMapper::getInstance($this->adapter);
305
        $file = $fileMapper->fetchOneByUuid($id);
306
        if (!$file) {
307
            $data = [
308
                'success' => false,
309
                'data' => 'ERROR_FILE_NOT_FOUND'
310
            ];
311
 
312
            return new JsonModel($data);
313
        }
314
 
315
        if ($file->application_id != $application->id) {
316
            return new JsonModel([
317
                'success' => false,
318
                'data' => 'ERROR_UNAUTHORIZED'
319
            ]);
320
        }
321
 
322
        if ($request->isPost()) {
323
 
324
            $target_path = $this->config['leaderslinked.fullpath.recruitment_selection'].$application->uuid;
325
            if(!file_exists($target_path)) {
326
                mkdir($target_path, 0755, true);
327
            }
328
 
329
            Functions::deleteFilename($target_path, $file->file);
330
 
331
 
332
            $result = $fileMapper->delete($file->id);
333
            if ($result) {
334
                $this->logger->info('Se borro el archivo  : '  . $file->name , ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
335
 
336
                $data = [
337
                    'success' => true,
338
                    'data' => [
339
                        'message' => 'LABEL_FILE_DELETED',
340
                        'files' => $this->renderFiles($vacancy, $application),
341
                     ]
342
                ];
343
            } else {
344
 
345
                $data = [
346
                    'success' => false,
347
                    'data' => $fileMapper->getError()
348
                ];
349
 
350
                return new JsonModel($data);
351
            }
352
        } else {
353
            $data = [
354
                'success' => false,
355
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
356
            ];
357
 
358
            return new JsonModel($data);
359
        }
360
 
361
        return new JsonModel($data);
362
    }
363
 
364
    public function viewAction()
365
    {
366
        $request = $this->getRequest();
367
        $currentUserPlugin = $this->plugin('currentUserPlugin');
368
        $currentCompany = $currentUserPlugin->getCompany();
369
        $currentUser = $currentUserPlugin->getUser();
370
 
371
        $request = $this->getRequest();
372
        $application_id = $this->params()->fromRoute('application_id');
373
        $vacancy_uuid   = $this->params()->fromRoute('vacancy_id');
374
        $id             = $this->params()->fromRoute('id');
375
 
376
        $vacancyMapper = RecruitmentSelectionVacancyMapper::getInstance($this->adapter);
377
        $vacancy = $vacancyMapper->fetchOneByUuid($vacancy_uuid);
378
 
379
        if(!$vacancy) {
380
            $data = [
381
                'success' => false,
382
                'data' => 'ERROR_VACANCY_NOT_FOUND'
383
            ];
384
 
385
            return new JsonModel($data);
386
        }
387
 
388
 
389
        if($vacancy->company_id != $currentCompany->id) {
390
            $data = [
391
                'success' => false,
392
                'data' => 'ERROR_VACANCY_IS_OTHER_COMPANY',
393
            ];
394
 
395
            return new JsonModel($data);
396
        }
397
 
398
 
399
 
400
        $applicationMapper = RecruitmentSelectionApplicationMapper::getInstance($this->adapter);
401
        $application = $applicationMapper->fetchOneByUuid($application_id);
402
 
403
 
404
        if (!$application) {
405
            $data = [
406
                'success' => false,
407
                'data' => 'ERROR_RECORD_NOT_FOUND'
408
            ];
409
 
410
            return new JsonModel($data);
411
        }
412
 
413
 
414
        if ($application->vacancy_id != $vacancy->id) {
415
            return new JsonModel([
416
                'success' => false,
417
                'data' => 'ERROR_UNAUTHORIZED'
418
            ]);
419
        }
420
 
421
 
422
        $fileMapper = RecruitmentSelectionFileMapper::getInstance($this->adapter);
423
        $file = $fileMapper->fetchOneByUuid($id);
424
        if (!$file) {
425
            $data = [
426
                'success' => false,
427
                'data' => 'ERROR_FILE_NOT_FOUND'
428
            ];
429
 
430
            return new JsonModel($data);
431
        }
432
 
433
        if ($file->application_id != $application->id) {
434
            return new JsonModel([
435
                'success' => false,
436
                'data' => 'ERROR_UNAUTHORIZED'
437
            ]);
438
        }
439
 
440
 
15464 efrain 441
        if($request->isGet()) {
15461 efrain 442
            $target_path = $this->config['leaderslinked.fullpath.recruitment_selection'].$application->uuid;
443
            if(!file_exists($target_path)) {
444
                mkdir($target_path, 0755, true);
445
            }
446
 
15464 efrain 447
            $target_filename = $target_path . DIRECTORY_SEPARATOR . $file->file;
15461 efrain 448
 
449
 
15464 efrain 450
            $filename = $file->file;
15461 efrain 451
            $content = base64_encode(file_get_contents($target_filename));
452
 
453
            $data = [
454
                'success' => true,
455
                'data' => [
456
                    'basename' => $filename,
457
                    'content' => $content
458
                ]
459
            ];
460
 
461
            return new JsonModel($data);
462
 
463
        } else {
464
            return new JsonModel([
465
                'success' => false,
466
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
467
            ]);
468
        }
469
    }
470
 
471
 
472
    /**
473
     *
474
     * @param RecruitmentSelectionVacancy $vacany
475
     * @param RecruitmentSelectionApplication $application
476
     * @return array
477
     */
478
    private function renderFiles($vacancy, $application)
479
    {
480
        $currentUserPlugin = $this->plugin('currentUserPlugin');
481
        $currentCompany = $currentUserPlugin->getCompany();
482
        $currentUser = $currentUserPlugin->getUser();
483
 
484
        $acl = $this->getEvent()->getViewModel()->getVariable('acl');
485
        $allowFileDelete = $acl->isAllowed($currentUser->usertype_id, 'recruitment-and-selection/applications/files/delete');
486
        $allowFileView = $acl->isAllowed($currentUser->usertype_id, 'recruitment-and-selection/applications/files/view');
487
 
488
 
489
 
490
        $recruitmentSelectionFileMapper = RecruitmentSelectionFileMapper::getInstance($this->adapter);
491
 
492
 
493
        $data = [];
494
        $records = $recruitmentSelectionFileMapper->fetchAllByVacancyId($vacancy->id);
495
        foreach($records as $record)
496
        {
497
            array_push($data, [
498
                'name' => $record->name,
499
                'filename' => basename($record->file),
500
                'link_view' => $allowFileView ? $this->url()->fromRoute('recruitment-and-selection/applications/files/view', ['vacancy_id' => $vacancy->uuid, 'application_id' => $application->uuid, 'id' => $record->uuid]) : '',
501
                'link_delete' => $allowFileDelete ? $this->url()->fromRoute('recruitment-and-selection/applications/files/delete', ['vacancy_id' => $vacancy->uuid, 'application_id' => $application->uuid, 'id' => $record->uuid]) : '',
502
 
503
            ]);
504
        }
505
 
506
        return $data;
507
    }
508
}