Proyectos de Subversion LeadersLinked - Backend

Rev

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