Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 1432 | Rev 1434 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1384 efrain 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\ViewModel;
12
use Laminas\View\Model\JsonModel;
1387 eleazar 13
use LeadersLinked\Form\RecruitForm;
1384 efrain 14
use LeadersLinked\Library\Functions;
1386 eleazar 15
use LeadersLinked\Mapper\RecruitmentSelectionVacancyMapper;
1387 eleazar 16
use LeadersLinked\Model\RecruitmentSelectionVacancy;
1384 efrain 17
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
18
use LeadersLinked\Model\Location;
19
use LeadersLinked\Mapper\LocationMapper;
20
use LeadersLinked\Mapper\IndustryMapper;
21
use LeadersLinked\Mapper\JobDescriptionMapper;
22
use LeadersLinked\Mapper\BehaviorMapper;
23
use LeadersLinked\Mapper\JobDescriptionBehaviorCompetencyMapper;
24
use LeadersLinked\Mapper\CompanyMapper;
25
use LeadersLinked\Model\Company;
26
use LeadersLinked\Mapper\JobCategoryMapper;
27
 
28
class RecruitmentSelectionVacancyController extends AbstractActionController {
29
 
30
    /**
31
     *
32
     * @var AdapterInterface
33
     */
34
    private $adapter;
35
 
36
    /**
37
     *
38
     * @var AbstractAdapter
39
     */
40
    private $cache;
41
 
42
    /**
43
     *
44
     * @var  LoggerInterface
45
     */
46
    private $logger;
47
 
48
    /**
49
     *
50
     * @var array
51
     */
52
    private $config;
53
 
54
    /**
55
     *
56
     * @param AdapterInterface $adapter
57
     * @param AbstractAdapter $cache
58
     * @param LoggerInterface $logger
59
     * @param array $config
60
     */
61
    public function __construct($adapter, $cache, $logger, $config) {
62
        $this->adapter = $adapter;
63
        $this->cache = $cache;
64
        $this->logger = $logger;
65
        $this->config = $config;
66
    }
67
 
68
    public function indexAction() {
69
        $request = $this->getRequest();
70
        $currentUserPlugin = $this->plugin('currentUserPlugin');
71
        $currentCompany = $currentUserPlugin->getCompany();
72
        $currentUser = $currentUserPlugin->getUser();
73
 
74
 
75
        $request = $this->getRequest();
76
        if ($request->isGet()) {
77
            $sandbox = $this->config['leaderslinked.runmode.sandbox'];
78
            if($sandbox) {
79
                $google_map_key  = $this->config['leaderslinked.google_map.sandbox_api_key'];
80
            } else {
81
                $google_map_key  = $this->config['leaderslinked.google_map.production_api_key'];
82
            }
83
 
84
 
85
            $headers = $request->getHeaders();
86
 
87
            $isJson = false;
88
            if ($headers->has('Accept')) {
89
                $accept = $headers->get('Accept');
90
 
91
                $prioritized = $accept->getPrioritized();
92
 
93
                foreach ($prioritized as $key => $value) {
94
                    $raw = trim($value->getRaw());
95
 
96
                    if (!$isJson) {
97
                        $isJson = strpos($raw, 'json');
98
                    }
99
                }
100
            }
101
 
102
            //$isJson = true;
103
            if ($isJson) {
104
                $search = $this->params()->fromQuery('search', []);
105
                $search = empty($search['value']) ? '' : filter_var($search['value'], FILTER_SANITIZE_STRING);
106
 
107
                $page = intval($this->params()->fromQuery('start', 1), 10);
108
                $records_x_page = intval($this->params()->fromQuery('length', 10), 10);
109
                $order = $this->params()->fromQuery('order', []);
110
                $order_field = empty($order[0]['column']) ? 99 : intval($order[0]['column'], 10);
111
                $order_direction = empty($order[0]['dir']) ? 'ASC' : strtoupper(filter_var($order[0]['dir'], FILTER_SANITIZE_STRING));
112
 
113
                $fields = ['name'];
114
                $order_field = isset($fields[$order_field]) ? $fields[$order_field] : 'name';
115
 
116
                if (!in_array($order_direction, ['ASC', 'DESC'])) {
117
                    $order_direction = 'ASC';
118
                }
119
 
1386 eleazar 120
                $recruitmentSelectionVacancyMapper = RecruitmentSelectionVacancyMapper::getInstance($this->adapter);
121
                $paginator = $recruitmentSelectionVacancyMapper->fetchAllDataTableByCompanyId($currentCompany->id, $search, $page, $records_x_page, $order_field, $order_direction);
1384 efrain 122
 
123
                $jobDescriptionMapper = JobDescriptionMapper::getInstance($this->adapter);
1386 eleazar 124
                $industryMapper = IndustryMapper::getInstance($this->adapter);
1384 efrain 125
                $jobCategoryMapper = JobCategoryMapper::getInstance($this->adapter);
126
 
127
                $items = [];
128
                $records = $paginator->getCurrentItems();
129
                foreach ($records as $record) {
130
 
131
 
132
 
133
                    $jobDescription = $jobDescriptionMapper->fetchOne($record->job_description_id);
134
                    if ($jobDescription) {
135
 
136
                        $item = [
137
                            'id' => $record->id,
138
                            'name' => $record->name,
139
                            'job_description' => $jobDescription->name,
140
                            'status' => $record->status,
141
                            'actions' => [
1395 eleazar 142
                                'link_edit' => $this->url()->fromRoute('recruitment-and-selection-vacancies/forms/edit', ['id' => $record->uuid]),
143
                                'link_delete' => $this->url()->fromRoute('recruitment-and-selection-vacancies/forms/delete', ['id' => $record->uuid])
1384 efrain 144
                            ]
145
                        ];
146
                    }
147
 
148
                    array_push($items, $item);
149
                }
150
 
151
                return new JsonModel([
152
                    'success' => true,
153
                    'data' => [
154
                        'items' => $items,
155
                        'total' => $paginator->getTotalItemCount(),
156
                    ]
157
                ]);
158
            } else {
159
 
160
                $form = new RecruitForm($this->adapter, $currentCompany->id);
161
 
162
                $jobDescriptionMapper = JobDescriptionMapper::getInstance($this->adapter);
163
                $jobsDescription = $jobDescriptionMapper->fetchAllByCompanyId($currentCompany->id);
164
                $industryMapper = industryMapper::getInstance($this->adapter);
165
                $industry = $industryMapper->fetchAllActives($currentCompany->id);
166
                $jobCategoryMapper = JobCategoryMapper::getInstance($this->adapter);
167
                $jobCategory = $jobCategoryMapper->fetchAllActives($currentCompany->id);
168
 
169
                $this->layout()->setTemplate('layout/layout-backend');
170
                $viewModel = new ViewModel();
1396 eleazar 171
                $viewModel->setTemplate('leaders-linked/recruitment-and-selection-vacancies/index.phtml');
1384 efrain 172
                $viewModel->setVariable('form', $form);
173
                $viewModel->setVariable('industry', $industry);
174
                $viewModel->setVariable('jobsDescription', $jobsDescription);
175
                $viewModel->setVariable('jobCategory', $jobCategory);
176
                $viewModel->setVariable('google_map_key', $google_map_key);
177
                return $viewModel;
178
            }
179
        } else {
180
            return new JsonModel([
181
                'success' => false,
182
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
183
            ]);
184
            ;
185
        }
186
    }
187
 
188
    public function addAction() {
189
        $request = $this->getRequest();
190
        $currentUserPlugin = $this->plugin('currentUserPlugin');
191
        $currentCompany = $currentUserPlugin->getCompany();
192
        $currentUser = $currentUserPlugin->getUser();
193
 
194
        $request = $this->getRequest();
195
 
196
 
197
        if ($request->isPost()) {
198
            $form = new RecruitForm($this->adapter, $currentCompany->id);
199
            $dataPost = $request->getPost()->toArray();
200
 
201
 
1387 eleazar 202
            $dataPost['status'] = isset($dataPost['status']) ? $dataPost['status'] : RecruitmentSelectionVacancy::STATUS_INACTIVE;
1384 efrain 203
 
204
            $form->setData($dataPost);
205
 
206
            if ($form->isValid()) {
207
 
208
 
209
                $dataPost = (array) $form->getData();
210
 
211
                $hydrator = new ObjectPropertyHydrator();
212
 
1417 eleazar 213
                $location = new Location();
214
                $hydrator->hydrate($dataPost, $location);
215
 
216
                $locationMapper= LocationMapper::getInstance($this->adapter);
217
                $result = $locationMapper->insert($location);
218
 
1418 eleazar 219
                if (!$result) {
220
                    return new JsonModel([
221
                        'success'   => false,
222
                        'data'   => [
223
                            'success'   => false,
1425 eleazar 224
                            'data' => 'ERROR_THERE_WAS_AN_ERROR'
1418 eleazar 225
                        ]
226
                    ]);
227
                }
1417 eleazar 228
 
1427 eleazar 229
                $recruitmentForm = new RecruitmentSelectionVacancy();
230
                $hydrator->hydrate($dataPost, $recruitmentForm);
231
 
1418 eleazar 232
                $recruitmentForm->location_id = $location->id;
1384 efrain 233
                $recruitmentForm->company_id = $currentCompany->id;
234
 
1427 eleazar 235
                $jobDescriptionMapper = JobDescriptionMapper::getInstance($this->adapter);
236
                $jobDescription = $jobDescriptionMapper->fetchOneByUuid($dataPost['job_description_id']);
237
                $recruitmentForm->job_description_id = $jobDescription->id;
238
 
239
                $jobCategoryMapper = JobCategoryMapper::getInstance($this->adapter);
240
                $jobCategory = $jobCategoryMapper->fetchOneByUuid( $dataPost['job_category_id']);
241
                $recruitmentForm->job_category_id = $jobCategory->id;
242
 
1428 eleazar 243
                $industryMapper = IndustryMapper::getInstance($this->adapter);
244
                $industry = $industryMapper->fetchOneByUuid($dataPost['industry_id']);
245
                $recruitmentForm->industry_id = $industry->id;
1425 eleazar 246
 
1428 eleazar 247
                $recruitmentForm->description = $dataPost['description'];
248
                $recruitmentForm->last_date = $dataPost['last_date'];
1433 eleazar 249
 
250
                $dt = \DateTime::createFromFormat('d/m/Y', $recruitmentForm->last_date);
251
                if($dt) {
252
                    $recruitmentForm->last_date = $dt->format('Y-m-d');
253
                }
1428 eleazar 254
 
1427 eleazar 255
                $recruitmentSelectionVacancyMapper = RecruitmentSelectionVacancyMapper::getInstance($this->adapter);
1430 eleazar 256
 
1386 eleazar 257
                $result = $recruitmentSelectionVacancyMapper->insert($recruitmentForm);
1384 efrain 258
 
259
                if ($result) {
1385 eleazar 260
                    $this->logger->info('Se agrego el proceso de reclutamiento' . $recruitmentForm->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
1384 efrain 261
 
262
                    $data = [
263
                        'success' => true,
264
                        'data' => 'LABEL_RECORD_ADDED'
265
                    ];
266
 
267
                } else {
268
                    $data = [
269
                        'success' => false,
1412 eleazar 270
                        'data' => $recruitmentSelectionVacancyMapper->getError()
1384 efrain 271
                    ];
272
                }
273
 
274
                return new JsonModel($data);
275
            } else {
276
                $messages = [];
277
                $form_messages = (array) $form->getMessages();
278
                foreach ($form_messages as $fieldname => $field_messages) {
279
 
280
                    $messages[$fieldname] = array_values($field_messages);
281
                }
282
 
283
                return new JsonModel([
284
                    'success' => false,
285
                    'data' => $messages
286
                ]);
287
            }
288
        } else {
289
            $data = [
290
                'success' => false,
291
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
292
            ];
293
 
294
            return new JsonModel($data);
295
        }
296
 
297
        return new JsonModel($data);
298
    }
299
 
300
    public function editAction() {
301
        $request = $this->getRequest();
302
        $currentUserPlugin = $this->plugin('currentUserPlugin');
303
        $currentCompany = $currentUserPlugin->getCompany();
304
        $currentUser = $currentUserPlugin->getUser();
305
 
306
        $request = $this->getRequest();
307
        $uuid = $this->params()->fromRoute('id');
308
 
309
 
310
        if (!$uuid) {
311
            $data = [
312
                'success' => false,
313
                'data' => 'ERROR_INVALID_PARAMETER'
314
            ];
315
 
316
            return new JsonModel($data);
317
        }
318
 
1386 eleazar 319
        $recruitmentSelectionVacancyMapper = RecruitmentSelectionVacancyMapper::getInstance($this->adapter);
320
        $recruitmentCandidate = $recruitmentSelectionVacancyMapper->fetchOneByUuid($uuid);
1384 efrain 321
        if (!$recruitmentCandidate) {
322
            $data = [
323
                'success' => false,
324
                'data' => 'ERROR_RECORD_NOT_FOUND'
325
            ];
326
 
327
            return new JsonModel($data);
328
        }
329
 
330
        if ($recruitmentCandidate->company_id != $currentCompany->id) {
331
            return new JsonModel([
332
                'success' => false,
333
                'data' => 'ERROR_UNAUTHORIZED'
334
            ]);
335
        }
336
 
337
 
338
        if ($request->isPost()) {
339
            $form = new RecruitForm($this->adapter, $currentCompany->id);
340
            $dataPost = $request->getPost()->toArray();
1387 eleazar 341
            $dataPost['status'] = isset($dataPost['status']) ? $dataPost['status'] : RecruitmentSelectionVacancy::STATUS_INACTIVE;
1384 efrain 342
 
343
            $form->setData($dataPost);
344
 
345
            if ($form->isValid()) {
346
                $dataPost = (array) $form->getData();
347
 
348
                $hydrator = new ObjectPropertyHydrator();
349
                $hydrator->hydrate($dataPost, $recruitmentCandidate);
350
 
351
                if (!$recruitmentCandidate->status) {
1387 eleazar 352
                    $recruitmentCandidate->status = RecruitmentSelectionVacancy::STATUS_INACTIVE;
1384 efrain 353
                }
354
 
355
                $jobDescriptionMapper = JobDescriptionMapper::getInstance($this->adapter);
356
                $jobDescription = $jobDescriptionMapper->fetchOneByUuid($dataPost['job_description_id']);
357
                $recruitmentCandidate->job_description_id = $jobDescription->id;
358
 
359
                $recruitmentCandidate->job_description_id = $jobDescription->id;
360
 
1386 eleazar 361
                $result = $recruitmentSelectionVacancyMapper->update($recruitmentCandidate);
1384 efrain 362
 
363
                if ($result) {
364
                    $this->logger->info('Se agrego el candidato' . $recruitmentCandidate->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
365
                    $data = [
366
                        'success' => true,
367
                        'id' => $recruitmentCandidate->id,
1395 eleazar 368
                        'action_edit' => $this->url()->fromRoute('recruitment-and-selection-vacancies/forms/edit', ['id' => $recruitmentCandidate->uuid]),
1384 efrain 369
                        'data' => 'LABEL_RECORD_UPDATED'
370
                    ];
371
                } else {
372
                    $data = [
373
                        'success' => false,
1386 eleazar 374
                        'data' => $recruitmentSelectionVacancyMapper->getError()
1384 efrain 375
                    ];
376
                }
377
 
378
                return new JsonModel($data);
379
            } else {
380
                $messages = [];
381
                $form_messages = (array) $form->getMessages();
382
                foreach ($form_messages as $fieldname => $field_messages) {
383
                    $messages[$fieldname] = array_values($field_messages);
384
                }
385
 
386
                return new JsonModel([
387
                    'success' => false,
388
                    'data' => $messages
389
                ]);
390
            }
391
        } else if ($request->isGet()) {
392
            $hydrator = new ObjectPropertyHydrator();
393
 
394
            $jobDescriptionMapper = JobDescriptionMapper::getInstance($this->adapter);
395
            $jobDescription = $jobDescriptionMapper->fetchOne($recruitmentCandidate->job_description_id);
396
            if (!$jobDescription) {
397
                $data = [
398
                    'success' => false,
399
                    'data' => 'ERROR_METHOD_NOT_ALLOWED'
400
                ];
401
 
402
                return new JsonModel($data);
403
            }
404
 
405
            $data = [
406
                'success' => true,
407
                'data' => [
408
                    'id' => $recruitmentCandidate->uuid,
409
                    'name' => $recruitmentCandidate->name,
1402 eleazar 410
                    'company'=>$currentCompany->id,
411
                    'job_description_id' => $jobDescription->job_description_id,
1411 eleazar 412
                    'location_search' => $recruitmentCandidate->location_id,
1402 eleazar 413
                    'job_category_id' => $recruitmentCandidate->job_category_id,
1417 eleazar 414
                    'description' => $recruitmentCandidate->description,
1384 efrain 415
                    'industry' => $recruitmentCandidate->industry_id,
1417 eleazar 416
                    'last_date' => $recruitmentCandidate->last_date,
1384 efrain 417
                    'status' => $recruitmentCandidate->status,
418
                    'content' => $recruitmentCandidate->content ? json_decode($recruitmentCandidate->content) : [],
419
                ]
420
            ];
421
 
422
            return new JsonModel($data);
423
        } else {
424
            $data = [
425
                'success' => false,
426
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
427
            ];
428
 
429
            return new JsonModel($data);
430
        }
431
 
432
        return new JsonModel($data);
433
    }
434
 
435
    public function deleteAction() {
436
        $request = $this->getRequest();
437
        $currentUserPlugin = $this->plugin('currentUserPlugin');
438
        $currentCompany = $currentUserPlugin->getCompany();
439
        $currentUser = $currentUserPlugin->getUser();
440
 
441
        $request = $this->getRequest();
442
        $uuid = $this->params()->fromRoute('id');
443
 
444
        if (!$uuid) {
445
            $data = [
446
                'success' => false,
447
                'data' => 'ERROR_INVALID_PARAMETER'
448
            ];
449
 
450
            return new JsonModel($data);
451
        }
452
 
1386 eleazar 453
        $recruitmentSelectionVacancyMapper = RecruitmentSelectionVacancyMapper::getInstance($this->adapter);
454
        $recruitmentCandidate = $recruitmentSelectionVacancyMapper->fetchOneByUuid($uuid);
1384 efrain 455
        if (!$recruitmentCandidate) {
456
            $data = [
457
                'success' => false,
458
                'data' => 'ERROR_RECORD_NOT_FOUND'
459
            ];
460
 
461
            return new JsonModel($data);
462
        }
463
 
464
        if ($recruitmentCandidate->company_id != $currentCompany->id) {
465
            return new JsonModel([
466
                'success' => false,
467
                'data' => 'ERROR_UNAUTHORIZED'
468
            ]);
469
        }
470
 
471
        if ($request->isPost()) {
472
 
473
 
1386 eleazar 474
            $result = $recruitmentSelectionVacancyMapper->delete($recruitmentCandidate->id);
1384 efrain 475
            if ($result) {
476
                $this->logger->info('Se borro el formulario de reclutamiento ' . $recruitmentCandidate->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
477
 
478
                $data = [
479
                    'success' => true,
480
                    'data' => 'LABEL_RECORD_DELETED'
481
                ];
482
            } else {
483
 
484
                $data = [
485
                    'success' => false,
1386 eleazar 486
                    'data' => $recruitmentSelectionVacancyMapper->getError()
1384 efrain 487
                ];
488
 
489
                return new JsonModel($data);
490
            }
491
        } else {
492
            $data = [
493
                'success' => false,
494
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
495
            ];
496
 
497
            return new JsonModel($data);
498
        }
499
 
500
        return new JsonModel($data);
501
    }
502
 
503
 
504
 
505
}