Proyectos de Subversion LeadersLinked - Backend

Rev

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