Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 1444 | Rev 1446 | 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
 
1434 eleazar 120
                $acl = $this->getEvent()->getViewModel()->getVariable('acl');
121
                $allowDelete = $acl->isAllowed($currentUser->usertype_id, 'recruitment-and-selection-vacancies/forms/delete');
122
 
123
                $allowEdit = $acl->isAllowed($currentUser->usertype_id, 'recruitment-and-selection-vacancies/forms/edit');
124
 
1386 eleazar 125
                $recruitmentSelectionVacancyMapper = RecruitmentSelectionVacancyMapper::getInstance($this->adapter);
126
                $paginator = $recruitmentSelectionVacancyMapper->fetchAllDataTableByCompanyId($currentCompany->id, $search, $page, $records_x_page, $order_field, $order_direction);
1384 efrain 127
 
128
                $jobDescriptionMapper = JobDescriptionMapper::getInstance($this->adapter);
1386 eleazar 129
                $industryMapper = IndustryMapper::getInstance($this->adapter);
1384 efrain 130
                $jobCategoryMapper = JobCategoryMapper::getInstance($this->adapter);
131
 
132
                $items = [];
133
                $records = $paginator->getCurrentItems();
134
                foreach ($records as $record) {
135
 
136
 
137
 
138
                    $jobDescription = $jobDescriptionMapper->fetchOne($record->job_description_id);
139
                    if ($jobDescription) {
140
 
141
                        $item = [
142
                            'id' => $record->id,
143
                            'name' => $record->name,
144
                            'job_description' => $jobDescription->name,
145
                            'status' => $record->status,
146
                            'actions' => [
1395 eleazar 147
                                'link_edit' => $this->url()->fromRoute('recruitment-and-selection-vacancies/forms/edit', ['id' => $record->uuid]),
148
                                'link_delete' => $this->url()->fromRoute('recruitment-and-selection-vacancies/forms/delete', ['id' => $record->uuid])
1384 efrain 149
                            ]
150
                        ];
151
                    }
152
 
153
                    array_push($items, $item);
154
                }
155
 
156
                return new JsonModel([
157
                    'success' => true,
158
                    'data' => [
159
                        'items' => $items,
160
                        'total' => $paginator->getTotalItemCount(),
161
                    ]
162
                ]);
163
            } else {
164
 
165
                $form = new RecruitForm($this->adapter, $currentCompany->id);
166
 
167
                $jobDescriptionMapper = JobDescriptionMapper::getInstance($this->adapter);
168
                $jobsDescription = $jobDescriptionMapper->fetchAllByCompanyId($currentCompany->id);
169
                $industryMapper = industryMapper::getInstance($this->adapter);
170
                $industry = $industryMapper->fetchAllActives($currentCompany->id);
171
                $jobCategoryMapper = JobCategoryMapper::getInstance($this->adapter);
172
                $jobCategory = $jobCategoryMapper->fetchAllActives($currentCompany->id);
173
 
174
                $this->layout()->setTemplate('layout/layout-backend');
175
                $viewModel = new ViewModel();
1396 eleazar 176
                $viewModel->setTemplate('leaders-linked/recruitment-and-selection-vacancies/index.phtml');
1384 efrain 177
                $viewModel->setVariable('form', $form);
178
                $viewModel->setVariable('industry', $industry);
179
                $viewModel->setVariable('jobsDescription', $jobsDescription);
180
                $viewModel->setVariable('jobCategory', $jobCategory);
181
                $viewModel->setVariable('google_map_key', $google_map_key);
182
                return $viewModel;
183
            }
184
        } else {
185
            return new JsonModel([
186
                'success' => false,
187
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
188
            ]);
189
            ;
190
        }
191
    }
192
 
193
    public function addAction() {
194
        $request = $this->getRequest();
195
        $currentUserPlugin = $this->plugin('currentUserPlugin');
196
        $currentCompany = $currentUserPlugin->getCompany();
197
        $currentUser = $currentUserPlugin->getUser();
198
 
199
        $request = $this->getRequest();
200
 
201
 
202
        if ($request->isPost()) {
203
            $form = new RecruitForm($this->adapter, $currentCompany->id);
204
            $dataPost = $request->getPost()->toArray();
205
 
206
 
1387 eleazar 207
            $dataPost['status'] = isset($dataPost['status']) ? $dataPost['status'] : RecruitmentSelectionVacancy::STATUS_INACTIVE;
1384 efrain 208
 
209
            $form->setData($dataPost);
210
 
211
            if ($form->isValid()) {
212
 
213
 
214
                $dataPost = (array) $form->getData();
215
 
216
                $hydrator = new ObjectPropertyHydrator();
217
 
1417 eleazar 218
                $location = new Location();
219
                $hydrator->hydrate($dataPost, $location);
220
 
221
                $locationMapper= LocationMapper::getInstance($this->adapter);
222
                $result = $locationMapper->insert($location);
223
 
1418 eleazar 224
                if (!$result) {
225
                    return new JsonModel([
226
                        'success'   => false,
227
                        'data'   => [
228
                            'success'   => false,
1425 eleazar 229
                            'data' => 'ERROR_THERE_WAS_AN_ERROR'
1418 eleazar 230
                        ]
231
                    ]);
232
                }
1417 eleazar 233
 
1427 eleazar 234
                $recruitmentForm = new RecruitmentSelectionVacancy();
235
                $hydrator->hydrate($dataPost, $recruitmentForm);
236
 
1444 eleazar 237
                $recruitmentForm->location_id = $location->id;
238
                $recruitmentForm->company_id = $currentCompany->id;
1384 efrain 239
 
1427 eleazar 240
                $jobDescriptionMapper = JobDescriptionMapper::getInstance($this->adapter);
241
                $jobDescription = $jobDescriptionMapper->fetchOneByUuid($dataPost['job_description_id']);
242
                $recruitmentForm->job_description_id = $jobDescription->id;
243
 
244
                $jobCategoryMapper = JobCategoryMapper::getInstance($this->adapter);
245
                $jobCategory = $jobCategoryMapper->fetchOneByUuid( $dataPost['job_category_id']);
246
                $recruitmentForm->job_category_id = $jobCategory->id;
247
 
1428 eleazar 248
                $industryMapper = IndustryMapper::getInstance($this->adapter);
249
                $industry = $industryMapper->fetchOneByUuid($dataPost['industry_id']);
250
                $recruitmentForm->industry_id = $industry->id;
1425 eleazar 251
 
1428 eleazar 252
                $recruitmentForm->description = $dataPost['description'];
253
                $recruitmentForm->last_date = $dataPost['last_date'];
1433 eleazar 254
 
255
                $dt = \DateTime::createFromFormat('d/m/Y', $recruitmentForm->last_date);
256
                if($dt) {
257
                    $recruitmentForm->last_date = $dt->format('Y-m-d');
258
                }
1428 eleazar 259
 
1427 eleazar 260
                $recruitmentSelectionVacancyMapper = RecruitmentSelectionVacancyMapper::getInstance($this->adapter);
1430 eleazar 261
 
1386 eleazar 262
                $result = $recruitmentSelectionVacancyMapper->insert($recruitmentForm);
1384 efrain 263
 
264
                if ($result) {
1385 eleazar 265
                    $this->logger->info('Se agrego el proceso de reclutamiento' . $recruitmentForm->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
1384 efrain 266
 
267
                    $data = [
268
                        'success' => true,
269
                        'data' => 'LABEL_RECORD_ADDED'
270
                    ];
271
 
272
                } else {
273
                    $data = [
274
                        'success' => false,
1412 eleazar 275
                        'data' => $recruitmentSelectionVacancyMapper->getError()
1384 efrain 276
                    ];
277
                }
278
 
279
                return new JsonModel($data);
280
            } else {
281
                $messages = [];
282
                $form_messages = (array) $form->getMessages();
283
                foreach ($form_messages as $fieldname => $field_messages) {
284
 
285
                    $messages[$fieldname] = array_values($field_messages);
286
                }
287
 
288
                return new JsonModel([
289
                    'success' => false,
290
                    'data' => $messages
291
                ]);
292
            }
293
        } else {
294
            $data = [
295
                'success' => false,
296
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
297
            ];
298
 
299
            return new JsonModel($data);
300
        }
301
 
302
        return new JsonModel($data);
303
    }
304
 
305
    public function editAction() {
306
        $request = $this->getRequest();
307
        $currentUserPlugin = $this->plugin('currentUserPlugin');
308
        $currentCompany = $currentUserPlugin->getCompany();
309
        $currentUser = $currentUserPlugin->getUser();
310
 
311
        $request = $this->getRequest();
312
        $uuid = $this->params()->fromRoute('id');
313
 
314
 
315
        if (!$uuid) {
316
            $data = [
317
                'success' => false,
318
                'data' => 'ERROR_INVALID_PARAMETER'
319
            ];
320
 
321
            return new JsonModel($data);
322
        }
323
 
1386 eleazar 324
        $recruitmentSelectionVacancyMapper = RecruitmentSelectionVacancyMapper::getInstance($this->adapter);
325
        $recruitmentCandidate = $recruitmentSelectionVacancyMapper->fetchOneByUuid($uuid);
1384 efrain 326
        if (!$recruitmentCandidate) {
327
            $data = [
328
                'success' => false,
329
                'data' => 'ERROR_RECORD_NOT_FOUND'
330
            ];
331
 
332
            return new JsonModel($data);
333
        }
334
 
335
        if ($recruitmentCandidate->company_id != $currentCompany->id) {
336
            return new JsonModel([
337
                'success' => false,
338
                'data' => 'ERROR_UNAUTHORIZED'
339
            ]);
340
        }
341
 
342
 
343
        if ($request->isPost()) {
344
            $form = new RecruitForm($this->adapter, $currentCompany->id);
345
            $dataPost = $request->getPost()->toArray();
1387 eleazar 346
            $dataPost['status'] = isset($dataPost['status']) ? $dataPost['status'] : RecruitmentSelectionVacancy::STATUS_INACTIVE;
1384 efrain 347
 
348
            $form->setData($dataPost);
349
 
350
            if ($form->isValid()) {
351
                $dataPost = (array) $form->getData();
352
 
353
                $hydrator = new ObjectPropertyHydrator();
354
                $hydrator->hydrate($dataPost, $recruitmentCandidate);
355
 
356
                if (!$recruitmentCandidate->status) {
1387 eleazar 357
                    $recruitmentCandidate->status = RecruitmentSelectionVacancy::STATUS_INACTIVE;
1384 efrain 358
                }
359
 
360
                $jobDescriptionMapper = JobDescriptionMapper::getInstance($this->adapter);
361
                $jobDescription = $jobDescriptionMapper->fetchOneByUuid($dataPost['job_description_id']);
1443 eleazar 362
                $recruitmentCandidate->job_description_id = $jobDescription->id;
1384 efrain 363
 
1439 eleazar 364
                $jobCategoryMapper = JobCategoryMapper::getInstance($this->adapter);
365
                $jobCategory = $jobCategoryMapper->fetchOneByUuid( $dataPost['job_category_id']);
1443 eleazar 366
                $recruitmentForm->job_category_id = $jobCategory->id;
1384 efrain 367
 
1439 eleazar 368
                $industryMapper = IndustryMapper::getInstance($this->adapter);
369
                $industry = $industryMapper->fetchOneByUuid($dataPost['industry_id']);
1443 eleazar 370
                $recruitmentForm->industry_id = $industry->id;
1439 eleazar 371
 
1443 eleazar 372
                $recruitmentCandidate->job_description_id = $jobDescription->id;
373
                $recruitmentCandidate->job_category_id = $jobCategory->id;
374
                $recruitmentCandidate->industry = $industry->id;
1439 eleazar 375
 
1386 eleazar 376
                $result = $recruitmentSelectionVacancyMapper->update($recruitmentCandidate);
1384 efrain 377
 
378
                if ($result) {
379
                    $this->logger->info('Se agrego el candidato' . $recruitmentCandidate->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
380
                    $data = [
381
                        'success' => true,
382
                        'id' => $recruitmentCandidate->id,
1395 eleazar 383
                        'action_edit' => $this->url()->fromRoute('recruitment-and-selection-vacancies/forms/edit', ['id' => $recruitmentCandidate->uuid]),
1384 efrain 384
                        'data' => 'LABEL_RECORD_UPDATED'
385
                    ];
386
                } else {
387
                    $data = [
388
                        'success' => false,
1386 eleazar 389
                        'data' => $recruitmentSelectionVacancyMapper->getError()
1384 efrain 390
                    ];
391
                }
392
 
393
                return new JsonModel($data);
394
            } else {
395
                $messages = [];
396
                $form_messages = (array) $form->getMessages();
397
                foreach ($form_messages as $fieldname => $field_messages) {
398
                    $messages[$fieldname] = array_values($field_messages);
399
                }
400
 
401
                return new JsonModel([
402
                    'success' => false,
403
                    'data' => $messages
404
                ]);
405
            }
406
        } else if ($request->isGet()) {
407
            $hydrator = new ObjectPropertyHydrator();
408
 
1441 eleazar 409
            $locationMapper = LocationMapper::getInstance($this->adapter);
410
            $location = $locationMapper->fetchOne($recruitmentCandidate->location_id);
411
 
1384 efrain 412
            $jobDescriptionMapper = JobDescriptionMapper::getInstance($this->adapter);
1445 eleazar 413
            $jobDescription = $jobDescriptionMapper->fetchOneByUuid($recruitmentCandidate->job_description_id);
414
 
415
            $jobCategoryMapper = JobCategoryMapper::getInstance($this->adapter);
416
            $jobCategory = $jobCategoryMapper->fetchOneByUuid($recruitmentCandidate->job_category_id);
417
 
418
            $industryMapper = IndustryMapper::getInstance($this->adapter);
419
            $industry = $industryMapper->fetchOneByUuid($recruitmentCandidate->industry_id);
420
 
421
 
1384 efrain 422
            if (!$jobDescription) {
423
                $data = [
424
                    'success' => false,
425
                    'data' => 'ERROR_METHOD_NOT_ALLOWED'
426
                ];
427
 
428
                return new JsonModel($data);
429
            }
430
 
431
            $data = [
432
                'success' => true,
433
                'data' => [
434
                    'id' => $recruitmentCandidate->uuid,
435
                    'name' => $recruitmentCandidate->name,
1402 eleazar 436
                    'company'=>$currentCompany->id,
1436 eleazar 437
                    'job_description_id' => $recruitmentCandidate->job_description_id,
1441 eleazar 438
                    'location_search' => $location->formatted_address,
1402 eleazar 439
                    'job_category_id' => $recruitmentCandidate->job_category_id,
1417 eleazar 440
                    'description' => $recruitmentCandidate->description,
1441 eleazar 441
                    'industry_id' => $recruitmentCandidate->industry_id,
1417 eleazar 442
                    'last_date' => $recruitmentCandidate->last_date,
1384 efrain 443
                    'status' => $recruitmentCandidate->status,
1437 eleazar 444
                   // 'content' => $recruitmentCandidate->content ? json_decode($recruitmentCandidate->content) : [],
1384 efrain 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
 
461
    public function deleteAction() {
462
        $request = $this->getRequest();
463
        $currentUserPlugin = $this->plugin('currentUserPlugin');
464
        $currentCompany = $currentUserPlugin->getCompany();
465
        $currentUser = $currentUserPlugin->getUser();
466
 
467
        $request = $this->getRequest();
468
        $uuid = $this->params()->fromRoute('id');
469
 
470
        if (!$uuid) {
471
            $data = [
472
                'success' => false,
473
                'data' => 'ERROR_INVALID_PARAMETER'
474
            ];
475
 
476
            return new JsonModel($data);
477
        }
478
 
1386 eleazar 479
        $recruitmentSelectionVacancyMapper = RecruitmentSelectionVacancyMapper::getInstance($this->adapter);
480
        $recruitmentCandidate = $recruitmentSelectionVacancyMapper->fetchOneByUuid($uuid);
1384 efrain 481
        if (!$recruitmentCandidate) {
482
            $data = [
483
                'success' => false,
484
                'data' => 'ERROR_RECORD_NOT_FOUND'
485
            ];
486
 
487
            return new JsonModel($data);
488
        }
489
 
490
        if ($recruitmentCandidate->company_id != $currentCompany->id) {
491
            return new JsonModel([
492
                'success' => false,
493
                'data' => 'ERROR_UNAUTHORIZED'
494
            ]);
495
        }
496
 
497
        if ($request->isPost()) {
498
 
499
 
1386 eleazar 500
            $result = $recruitmentSelectionVacancyMapper->delete($recruitmentCandidate->id);
1384 efrain 501
            if ($result) {
502
                $this->logger->info('Se borro el formulario de reclutamiento ' . $recruitmentCandidate->name, ['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,
1386 eleazar 512
                    'data' => $recruitmentSelectionVacancyMapper->getError()
1384 efrain 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
 
529
 
530
 
531
}