Proyectos de Subversion LeadersLinked - Services

Rev

Rev 60 | Ir a la última revisión | | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
/**
3
 *
4
 * Controlador: Mis Perfiles
5
 *
6
 */
7
declare(strict_types=1);
8
 
9
namespace LeadersLinked\Controller;
10
 
11
use Laminas\Db\Adapter\AdapterInterface;
12
 
13
use Laminas\Mvc\Controller\AbstractActionController;
14
use Laminas\Log\LoggerInterface;
15
use Laminas\View\Model\ViewModel;
16
use Laminas\View\Model\JsonModel;
17
 
18
use LeadersLinked\Library\Functions;
19
use LeadersLinked\Mapper\CompanyUserMapper;
20
use LeadersLinked\Model\Job;
21
use LeadersLinked\Mapper\LocationMapper;
22
use LeadersLinked\Mapper\JobMapper;
23
use LeadersLinked\Mapper\CompanyMapper;
24
use LeadersLinked\Mapper\JobCategoryMapper;
25
use LeadersLinked\Mapper\JobSkillMapper;
26
use LeadersLinked\Mapper\JobDegreeMapper;
27
use LeadersLinked\Mapper\JobLanguageMapper;
28
use LeadersLinked\Mapper\DegreeMapper;
29
use LeadersLinked\Mapper\LanguageMapper;
30
use LeadersLinked\Mapper\SkillMapper;
31
use LeadersLinked\Mapper\IndustryMapper;
32
use LeadersLinked\Mapper\CompanySizeMapper;
33
use LeadersLinked\Mapper\CompanyLocationMapper;
34
use LeadersLinked\Mapper\JobApplicationMapper;
35
use LeadersLinked\Form\Job\ApplyForm;
36
use LeadersLinked\Model\JobApplication;
37
use LeadersLinked\Mapper\JobSaveMapper;
38
use LeadersLinked\Model\JobSave;
39
use LeadersLinked\Mapper\QueryMapper;
40
use LeadersLinked\Mapper\UserProfileMapper;
41
 
42
 
43
class JobController extends AbstractActionController
44
{
45
    /**
46
     *
47
     * @var \Laminas\Db\Adapter\AdapterInterface
48
     */
49
    private $adapter;
50
 
51
    /**
52
     *
53
     * @var \LeadersLinked\Cache\CacheInterface
54
     */
55
    private $cache;
56
 
57
 
58
    /**
59
     *
60
     * @var \Laminas\Log\LoggerInterface
61
     */
62
    private $logger;
63
 
64
    /**
65
     *
66
     * @var array
67
     */
68
    private $config;
69
 
70
 
71
    /**
72
     *
73
     * @var \Laminas\Mvc\I18n\Translator
74
     */
75
    private $translator;
76
 
77
 
78
    /**
79
     *
80
     * @param \Laminas\Db\Adapter\AdapterInterface $adapter
81
     * @param \LeadersLinked\Cache\CacheInterface $cache
82
     * @param \Laminas\Log\LoggerInterface LoggerInterface $logger
83
     * @param array $config
84
     * @param \Laminas\Mvc\I18n\Translator $translator
85
     */
86
    public function __construct($adapter, $cache, $logger, $config, $translator)
87
    {
88
        $this->adapter      = $adapter;
89
        $this->cache        = $cache;
90
        $this->logger       = $logger;
91
        $this->config       = $config;
92
        $this->translator   = $translator;
93
    }
94
 
95
    /**
96
     *
97
     * Generación del listado de perfiles
98
     * {@inheritDoc}
99
     * @see \Laminas\Mvc\Controller\AbstractActionController::indexAction()
100
     */
101
    public function indexAction()
102
    {
103
 
104
        return new JsonModel([
105
            'success' => false,
106
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
107
        ]);
108
    }
109
 
110
    public function viewAction()
111
    {
112
        $currentUserPlugin = $this->plugin('currentUserPlugin');
113
        $currentUser = $currentUserPlugin->getUser();
114
 
115
        $flashMessenger = $this->plugin('FlashMessenger');
116
        $request = $this->getRequest();
117
        $id = $this->params()->fromRoute('id');
118
 
119
        if(!$id) {
120
            $flashMessenger->addErrorMessage('ERROR_INVALID_PARAMETER');
121
            return $this->redirect()->toRoute('dashboard');
122
        }
123
 
124
        $jobMapper = JobMapper::getInstance($this->adapter);
125
        $job = $jobMapper->fetchOneByUuidAndNetworkId($id, $currentUser->network_id);
126
        if(!$job) {
127
            $flashMessenger->addErrorMessage('ERROR_RECORD_NOT_FOUND');
128
            return $this->redirect()->toRoute('dashboard');
129
        }
130
 
131
        if($job->status != Job::STATUS_ACTIVE) {
132
            $flashMessenger->addErrorMessage('ERROR_UNAUTHORIZED');
133
            return $this->redirect()->toRoute('dashboard');
134
        }
135
 
136
        $companyMapper = CompanyMapper::getInstance($this->adapter);
137
        $company = $companyMapper->fetchOne($job->company_id);
138
        if(!$company) {
139
            $flashMessenger->addErrorMessage('ERROR_RECORD_NOT_FOUND');
140
            return $this->redirect()->toRoute('dashboard');
141
        }
142
 
143
 
144
 
145
 
146
 
147
        if($request->isGet()) {
148
            $currentUserPlugin = $this->plugin('currentUserPlugin');
149
            $currentUser = $currentUserPlugin->getUser();
150
 
151
            $companyUserMapper = CompanyUserMapper::getInstance($this->adapter);
152
            $now = $companyUserMapper->getDatebaseNow();
153
 
154
            $companyUser = $companyUserMapper->fetchOneByCompanyIdAndUserId($company->id, $currentUser->id);
155
 
156
            $jobApplicationMapper = JobApplicationMapper::getInstance($this->adapter);
157
            $total_applications = $jobApplicationMapper->fetchTotalApplicationsByJobId($job->id);
158
 
159
 
160
            $job->visits++;
161
            $jobMapper->updateVisits($job);
162
 
163
 
164
            //if($companyUser) {
165
            //   $job_apply_operation = '';
166
            //} else {
167
                $jobApplication = $jobApplicationMapper->fetchOneByJobIdAndUserId($job->id, $currentUser->id);
168
                if($jobApplication) {
169
                    $job_apply_operation = 'remove-apply';
170
                } else {
171
                    $job_apply_operation = 'apply';
172
                }
173
 
174
            //}
175
 
176
            $jobSaveMapper = JobSaveMapper::getInstance($this->adapter);
177
            $jobSave = $jobSaveMapper->fetchOneByJobIdAndUserId($job->id, $currentUser->id);
178
            if($jobSave) {
179
                $job_save_operation = 'remove-job-saved';
180
            } else {
181
                $job_save_operation = 'job-save';
182
            }
183
 
184
 
185
            $months = [
186
                1 => 'LABEL_MONTH_JANUARY',
187
                2 => 'LABEL_MONTH_FEBRUARY',
188
                3 => 'LABEL_MONTH_MARCH',
189
                4 => 'LABEL_MONTH_APRIL',
190
                5 => 'LABEL_MONTH_MAY',
191
                6 => 'LABEL_MONTH_JUNE',
192
                7 => 'LABEL_MONTH_JULY',
193
                8 => 'LABEL_MONTH_AUGUST',
194
                9 => 'LABEL_MONTH_SEPTEMBER',
195
                10 => 'LABEL_MONTH_OCTOBER',
196
                11 => 'LABEL_MONTH_NOVEMBER',
197
                12 => 'LABEL_MONTH_DECEMBER',
198
            ];
199
 
200
            $dt = \dateTime::createFromFormat('Y-m-d', $job->last_date_of_application);
201
            $last_date_of_application = $dt->format('d') . ' de ' . $months[$dt->format('n')] . ' de ' . $dt->format('Y');
202
 
203
            $locationMapper = LocationMapper::getInstance($this->adapter);
204
            $location = $locationMapper->fetchOne($job->location_id);
205
 
206
            $jobCategoryMapper = JobCategoryMapper::getInstance($this->adapter);
207
            $jobCategory = $jobCategoryMapper->fetchOne($job->job_category_id);
208
 
209
            switch($job->employment_type)
210
            {
211
                case Job::EMPLOYMENT_TYPE_FULL_TIME :
212
                    $employment_type = 'LABEL_EMPLOYMENT_TYPE_FULL_TIME';
213
                    break;
214
                case Job::EMPLOYMENT_TYPE_PART_TIME :
215
                    $employment_type = 'LABEL_EMPLOYMENT_TYPE_PART_TIME';
216
                    break;
217
 
218
                case Job::EMPLOYMENT_TYPE_CONTRACT :
219
                    $employment_type = 'LABEL_EMPLOYMENT_TYPE_CONTRACT';
220
                    break;
221
 
222
                case Job::EMPLOYMENT_TYPE_TEMPORARY :
223
                    $employment_type = 'LABEL_EMPLOYMENT_TYPE_TEMPORARY';
224
                    break;
225
 
226
                default :
227
                    $employment_type = '';
228
                    break;
229
            }
230
 
231
            $degrees = [];
232
            $degreeMapper = DegreeMapper::getInstance($this->adapter);
233
            $records = $degreeMapper->fetchAllActive();
234
            foreach($records as $record)
235
            {
236
                $degrees[$record->uuid] = $record->name;
237
            }
238
 
239
            $jobDegreeMapper = JobDegreeMapper::getInstance($this->adapter);
240
            $records = $jobDegreeMapper->fetchAllByJobId($job->id);
241
 
242
            $job_degrees = [];
243
            foreach($records as $record)
244
            {
245
                $degree = $degreeMapper->fetchOne($record->degree_id);
246
                array_push($job_degrees, $degree->name);
247
            }
248
 
249
            $languages = [];
250
            $languageMapper = LanguageMapper::getInstance($this->adapter);
251
            $records = $languageMapper->fetchAllActive();
252
            foreach($records as $record)
253
            {
254
                $languages[$record->id] = $record->name;
255
            }
256
 
257
            $jobLanguageMapper = JobLanguageMapper::getInstance($this->adapter);
258
            $records = $jobLanguageMapper->fetchAllByJobId($job->id);
259
 
260
 
261
            $job_languages = [];
262
            foreach($records as $record)
263
            {
264
                $language = $languageMapper->fetchOne($record->language_id);
265
                array_push($job_languages, $language->name);
266
            }
267
 
268
            $skills = [];
269
            $skillMapper = SkillMapper::getInstance($this->adapter);
270
 
271
            $records = $skillMapper->fetchAllActive();
272
            foreach($records as $record)
273
            {
274
                $skills[$record->uuid] = $record->name;
275
            }
276
 
277
 
278
            $jobSkillMapper = JobSkillMapper::getInstance($this->adapter);
279
            $records = $jobSkillMapper->fetchAllByJobId($job->id);
280
 
281
            $job_skills = [];
282
            foreach($records as $record)
283
            {
284
                $skill = $skillMapper->fetchOne($record->skill_id);
285
                array_push($job_skills, $skill->name);
286
            }
287
 
288
 
289
            $industryMapper = IndustryMapper::getInstance($this->adapter);
290
            $industry = $industryMapper->fetchOne($company->industry_id);
291
 
292
            $companySizeMapper = CompanySizeMapper::getInstance($this->adapter);
293
            $companySize = $companySizeMapper->fetchOne($company->company_size_id);
294
 
295
            $companyLocationMapper = CompanyLocationMapper::getInstance($this->adapter);
296
            $companyLocation = $companyLocationMapper->fetchOneMainLocationByCompanyId($company->id);
297
 
298
 
299
            $company_address = '';
300
            if($companyLocation) {
301
                $mainLocation = $locationMapper->fetchOne($companyLocation->location_id);
302
                if($mainLocation) {
303
                    $company_address = $mainLocation->formatted_address;
304
                }
305
            }
306
 
307
            $user_profiles = [];
308
            $userProfileMapper = UserProfileMapper::getInstance($this->adapter);
309
            $records = $userProfileMapper->fetchAllByUserId($currentUser->id);
310
            foreach($records as $record)
311
            {
312
                $user_profiles[$record->uuid] = $record->name;
313
            }
314
 
315
 
316
            $timeElapsed = Functions::timeAgo($job->updated_on, $now);
317
 
318
 
319
                return new JsonModel([
320
                    'company_uuid'  => $company->uuid,
321
                    'company_image'         => $company->image,
322
                    'job_uuid'              => $job->uuid,
323
                    'job_title'             => $job->title,
324
                    'job_description'       => $job->description,
325
                    'total_applications'    => $total_applications,
326
                    'location'              => $location->formatted_address,
327
                    'employment_type'       => $employment_type,
328
                    'last_date_of_application'  => $last_date_of_application,
329
                    'job_category'              => $jobCategory->name,
330
                    'timeElapsed'               => $timeElapsed,
331
                    'experience'                => $job->experience_visible ? $job->experience_min . '-' . $job->experience_max : '',
332
                    'salary'                    => $job->salary_visible ? $job->salary_min . '-' . $job->salary_max . ' (' . $job->salary_currency . ')' : '',
333
                    'job_degrees'               => $job_degrees,
334
                    'job_languages'             => $job_languages,
335
                    'job_skills'                => $job_skills,
336
                    'job_visits'                => $job->visits,
337
                    'job_apply_operation'       => $job_apply_operation,
338
                    'job_save_operation'        => $job_save_operation,
339
                    'company_name'              => $company->name,
340
                    'company_foundation_year'   => $company->foundation_year,
341
                    'company_website'           => $company->website,
342
                    'company_industry'          => $industry->name,
343
                    'company_size'              => $companySize->minimum_no_of_employee . '-' . $companySize->maximum_no_of_employee,
344
                    'company_address'           => $company_address,
345
                    'user_profiles'             => $user_profiles,
346
                    'skills'                    => $skills,
347
                    'languages'                 => $languages,
348
                    'degrees'                   => $degrees,
349
                ]);
350
 
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 applyJobAction()
365
    {
366
        $currentUserPlugin = $this->plugin('currentUserPlugin');
367
        $currentUser = $currentUserPlugin->getUser();
368
 
369
 
370
        $request = $this->getRequest();
371
        $id = $this->params()->fromRoute('id');
372
 
373
        if(!$id) {
374
            $data = [
375
                'success' => false,
376
                'data' => 'ERROR_INVALID_PARAMETER'
377
            ];
378
 
379
            return new JsonModel($data);
380
        }
381
 
382
        $jobMapper = JobMapper::getInstance($this->adapter);
383
        $job = $jobMapper->fetchOneByUuidAndNetworkId($id, $currentUser->network_id);
384
        if(!$job) {
385
            $data = [
386
                'success' => false,
387
                'data' => 'ERROR_RECORD_NOT_FOUND'
388
            ];
389
 
390
            return new JsonModel($data);
391
        }
392
 
393
        if($job->status != Job::STATUS_ACTIVE) {
394
            $data = [
395
                'success' => false,
396
                'data' => 'ERROR_UNAUTHORIZED'
397
            ];
398
 
399
            return new JsonModel($data);
400
        }
401
 
402
        $companyMapper = CompanyMapper::getInstance($this->adapter);
403
        $company = $companyMapper->fetchOne($job->company_id);
404
        if(!$company) {
405
            $data = [
406
                'success' => false,
407
                'data' => 'ERROR_RECORD_NOT_FOUND'
408
            ];
409
 
410
            return new JsonModel($data);
411
        }
412
 
413
 
414
        if($request->isPost()) {
415
 
416
            $form = new ApplyForm($this->adapter, $currentUser->id);
417
            $dataPost = $request->getPost()->toArray();
418
 
419
            $form->setData($dataPost);
420
 
421
            if($form->isValid()) {
422
                $dataPost = (array) $form->getData();
423
 
424
                $userProfileMapper = UserProfileMapper::getInstance($this->adapter);
425
                $userProfile = $userProfileMapper->fetchOneByUuid($dataPost['user_profile_id']);
426
 
427
                $jobApplicationMapper = JobApplicationMapper::getInstance($this->adapter);
428
                $jobApplication = $jobApplicationMapper->fetchOneByJobIdAndUserId($job->id, $currentUser->id);
429
 
430
                $result = true;
431
 
432
                if(!$jobApplication) {
433
                    $jobApplication = new JobApplication();
434
                    $jobApplication->job_id = $job->id;
435
                    $jobApplication->user_id = $currentUser->id;
436
                    $jobApplication->user_profile_id = $userProfile->id;
437
 
438
                    $result = $jobApplicationMapper->insert($jobApplication);
439
                    if($result) {
440
                        $this->logger->info('El usuario  ' . $currentUser->first_name .  ' ' . $currentUser->last_name . ' aplico al trabajo ' . $job->title, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
441
                    }
442
                }
443
 
444
                if($result) {
445
 
446
                    $total = $jobApplicationMapper->fetchTotalApplicationsByJobId($job->id);
447
 
448
                    $data = [
449
                        'success'   => true,
450
                        'data'      => 'LABEL_SUCCESSFUL_APPLICATION',
451
                        'total'     => $total
452
                    ];
453
 
454
 
455
 
456
                } else {
457
                    $data = [
458
                        'success'   => false,
459
                        'data'      => $jobApplicationMapper->getError()
460
                    ];
461
                }
462
 
463
 
464
                return new JsonModel($data);
465
 
466
            } else {
467
                $messages = [];
468
                $form_messages = (array) $form->getMessages();
469
                foreach($form_messages  as $fieldname => $field_messages)
470
                {
471
                    $messages[$fieldname] = array_values($field_messages);
472
                }
473
 
474
                return new JsonModel([
475
                    'success'   => false,
476
                    'data'   => $messages
477
                ]);
478
            }
479
 
480
        } else {
481
            $data = [
482
                'success' => false,
483
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
484
            ];
485
 
486
            return new JsonModel($data);
487
 
488
        }
489
    }
490
 
491
    public function removeApplyJobAction()
492
    {
493
        $currentUserPlugin = $this->plugin('currentUserPlugin');
494
        $currentUser = $currentUserPlugin->getUser();
495
 
496
        $request = $this->getRequest();
497
        $id = $this->params()->fromRoute('id');
498
 
499
        if(!$id) {
500
            $data = [
501
                'success' => false,
502
                'data' => 'ERROR_INVALID_PARAMETER'
503
            ];
504
 
505
            return new JsonModel($data);
506
        }
507
 
508
        $jobMapper = JobMapper::getInstance($this->adapter);
509
        $job = $jobMapper->fetchOneByUuidAndNetworkId($id, $currentUser->network_id);
510
        if(!$job) {
511
            $data = [
512
                'success' => false,
513
                'data' => 'ERROR_RECORD_NOT_FOUND'
514
            ];
515
 
516
            return new JsonModel($data);
517
        }
518
 
519
        if($job->status != Job::STATUS_ACTIVE) {
520
            $data = [
521
                'success' => false,
522
                'data' => 'ERROR_UNAUTHORIZED'
523
            ];
524
 
525
            return new JsonModel($data);
526
        }
527
 
528
        $companyMapper = CompanyMapper::getInstance($this->adapter);
529
        $company = $companyMapper->fetchOne($job->company_id);
530
        if(!$company) {
531
            $data = [
532
                'success' => false,
533
                'data' => 'ERROR_RECORD_NOT_FOUND'
534
            ];
535
 
536
            return new JsonModel($data);
537
        }
538
 
539
 
540
        if($request->isPost()) {
541
 
542
            $jobApplicationMapper = JobApplicationMapper::getInstance($this->adapter);
543
            $result = $jobApplicationMapper->deleteByJobIdAndUserId($job->id, $currentUser->id);
544
 
545
            if($result) {
546
                $this->logger->info('El usuario  ' . $currentUser->first_name .  ' ' . $currentUser->last_name . ' removio su aplicación al trabajo ' . $job->title, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
547
 
548
                $total = $jobApplicationMapper->fetchTotalApplicationsByJobId($job->id);
549
                $data = [
550
                    'success'   => true,
551
                    'data'      => 'LABEL_APPLICATION_WAS_REMOVED',
552
                    'total'     => $total
553
                ];
554
            } else {
555
                $data = [
556
                    'success'   => false,
557
                    'data'      => $jobApplicationMapper->getError()
558
                ];
559
 
560
 
561
            }
562
 
563
            return new JsonModel($data);
564
 
565
        } else {
566
            $data = [
567
                'success' => false,
568
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
569
            ];
570
 
571
            return new JsonModel($data);
572
 
573
        }
574
    }
575
 
576
    public function removeSaveJobAction()
577
    {
578
        $currentUserPlugin = $this->plugin('currentUserPlugin');
579
        $currentUser = $currentUserPlugin->getUser();
580
 
581
        $request = $this->getRequest();
582
        $id = $this->params()->fromRoute('id');
583
 
584
        if(!$id) {
585
            $data = [
586
                'success' => false,
587
                'data' => 'ERROR_INVALID_PARAMETER'
588
            ];
589
 
590
            return new JsonModel($data);
591
        }
592
 
593
        $jobMapper = JobMapper::getInstance($this->adapter);
594
        $job = $jobMapper->fetchOneByUuidAndNetworkId($id, $currentUser->network_id);
595
        if(!$job) {
596
            $data = [
597
                'success' => false,
598
                'data' => 'ERROR_RECORD_NOT_FOUND'
599
            ];
600
 
601
            return new JsonModel($data);
602
        }
603
 
604
        if($job->status != Job::STATUS_ACTIVE) {
605
            $data = [
606
                'success' => false,
607
                'data' => 'ERROR_UNAUTHORIZED'
608
            ];
609
 
610
            return new JsonModel($data);
611
        }
612
 
613
        $companyMapper = CompanyMapper::getInstance($this->adapter);
614
        $company = $companyMapper->fetchOne($job->company_id);
615
        if(!$company) {
616
            $data = [
617
                'success' => false,
618
                'data' => 'ERROR_RECORD_NOT_FOUND'
619
            ];
620
 
621
            return new JsonModel($data);
622
        }
623
 
624
 
625
        if($request->isPost()) {
626
 
627
            $jobSaveMapper = JobSaveMapper::getInstance($this->adapter);
628
            $result = $jobSaveMapper->deleteByJobIdAndUserId($job->id, $currentUser->id);
629
 
630
            if($result) {
631
                $this->logger->info('El usuario  ' . $currentUser->first_name .  ' ' . $currentUser->last_name . ' removio este trabajo salvado ' . $job->title, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
632
 
633
                $data = [
634
                    'success'   => true,
635
                    ''
636
                ];
637
            } else {
638
                $data = [
639
                    'success'   => false,
640
                    'data'      => $jobSaveMapper->getError()
641
                ];
642
 
643
 
644
            }
645
 
646
            return new JsonModel($data);
647
 
648
        } else {
649
            $data = [
650
                'success' => false,
651
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
652
            ];
653
 
654
            return new JsonModel($data);
655
 
656
        }
657
    }
658
 
659
 
660
    public function saveJobAction()
661
    {
662
        $currentUserPlugin = $this->plugin('currentUserPlugin');
663
        $currentUser = $currentUserPlugin->getUser();
664
 
665
        $request = $this->getRequest();
666
        $id = $this->params()->fromRoute('id');
667
 
668
        if(!$id) {
669
            $data = [
670
                'success' => false,
671
                'data' => 'ERROR_INVALID_PARAMETER'
672
            ];
673
 
674
            return new JsonModel($data);
675
        }
676
 
677
        $jobMapper = JobMapper::getInstance($this->adapter);
678
        $job = $jobMapper->fetchOneByUuidAndNetworkId($id, $currentUser->network_id);
679
        if(!$job) {
680
            $data = [
681
                'success' => false,
682
                'data' => 'ERROR_RECORD_NOT_FOUND'
683
            ];
684
 
685
            return new JsonModel($data);
686
        }
687
 
688
        if($job->status != Job::STATUS_ACTIVE) {
689
            $data = [
690
                'success' => false,
691
                'data' => 'ERROR_UNAUTHORIZED'
692
            ];
693
 
694
            return new JsonModel($data);
695
        }
696
 
697
        $companyMapper = CompanyMapper::getInstance($this->adapter);
698
        $company = $companyMapper->fetchOne($job->company_id);
699
        if(!$company) {
700
            $data = [
701
                'success' => false,
702
                'data' => 'ERROR_RECORD_NOT_FOUND'
703
            ];
704
 
705
            return new JsonModel($data);
706
        }
707
 
708
 
709
        if($request->isPost()) {
710
 
711
            $jobSaveMapper = JobSaveMapper::getInstance($this->adapter);
712
            $jobSave = $jobSaveMapper->fetchOneByJobIdAndUserId($job->id, $currentUser->id);
713
 
714
            if($jobSave) {
715
                $result = true;
716
            } else {
717
                $jobSave = new JobSave();
718
                $jobSave->job_id = $job->id;
719
                $jobSave->user_id = $currentUser->id;
720
 
721
                $result = $jobSaveMapper->insert($jobSave);
722
            }
723
 
724
            if($result) {
725
                $this->logger->info('El usuario  ' . $currentUser->first_name .  ' ' . $currentUser->last_name . ' salvo este trabajo ' . $job->title, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
726
 
727
                $data = [
728
                    'success'   => true,
729
                ];
730
            } else {
731
                $data = [
732
                    'success'   => false,
733
                    'data'      => $jobSaveMapper->getError()
734
                ];
735
 
736
 
737
            }
738
 
739
            return new JsonModel($data);
740
 
741
        } else {
742
            $data = [
743
                'success' => false,
744
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
745
            ];
746
 
747
            return new JsonModel($data);
748
 
749
        }
750
    }
751
 
752
    public function appliedJobsAction()
753
    {
754
        $currentUserPlugin = $this->plugin('currentUserPlugin');
755
        $currentUser = $currentUserPlugin->getUser();
756
 
757
        $request = $this->getRequest();
758
        if($request->isGet()) {
759
 
760
 
761
                $employment_types = [
762
                    job::EMPLOYMENT_TYPE_FULL_TIME => 'LABEL_EMPLOYMENT_TYPE_FULL_TIME',
763
                    Job::EMPLOYMENT_TYPE_PART_TIME  => 'LABEL_EMPLOYMENT_TYPE_PART_TIME',
764
                    Job::EMPLOYMENT_TYPE_TEMPORARY => 'LABEL_EMPLOYMENT_TYPE_TEMPORARY',
765
                    Job::EMPLOYMENT_TYPE_CONTRACT =>  'LABEL_EMPLOYMENT_TYPE_CONTRACT',
766
                ];
767
 
768
                $search = Functions::sanitizeFilterString($this->params()->fromQuery('search', ''));
769
 
770
                $queryMapper = QueryMapper::getInstance($this->adapter);
771
 
772
                $select = $queryMapper->getSql()->select();
773
                $select->columns(['id', 'uuid', 'title', 'employment_type', 'last_date_of_application']);
774
                $select->from(['j' => JobMapper::_TABLE]);
775
                $select->join(['ja' => JobApplicationMapper::_TABLE], 'j.id = ja.job_id');
776
                $select->where->equalTo('j.network_id', $currentUser->network_id);
777
                $select->where->equalTo('j.status', Job::STATUS_ACTIVE);
778
                $select->where->equalTo('ja.user_id', $currentUser->id);
779
 
780
                if($search) {
781
                    $select->where->like('title', '%' . $search . '%');
782
                }
783
 
784
                $select->order('last_date_of_application DESC');
785
 
786
                $records = $queryMapper->fetchAll($select);
787
 
788
 
789
 
790
 
791
                $items = [];
792
                foreach($records as $record)
793
                {
794
 
795
                    $item = [
796
                        'id' => $record['uuid'],
797
                        'title' => $record['title'],
798
                        'employment_type' => $employment_types[$record['employment_type']],
799
                        'last_date_of_application' => $record['last_date_of_application'],
800
                        'link_remove' => $this->url()->fromRoute('job/remove-apply-job', ['id' => $record['uuid']  ]),
801
                        'link_view' => $this->url()->fromRoute('job/view', ['id' => $record['uuid'] ]),
802
                    ];
803
 
804
                    array_push($items, $item);
805
                }
806
 
807
 
808
 
809
                $response = [
810
                    'success' => true,
811
                    'data' => $items
812
                ];
813
 
814
                return new JsonModel($response);
815
 
816
 
817
        } else {
818
            return new JsonModel([
819
                'success' => false,
820
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
821
            ]);
822
        }
823
    }
824
 
825
    public function savedJobsAction()
826
    {
827
        $currentUserPlugin = $this->plugin('currentUserPlugin');
828
        $currentUser = $currentUserPlugin->getUser();
829
 
830
        $request = $this->getRequest();
831
        if($request->isGet()) {
832
 
833
                $employment_types = [
834
                    job::EMPLOYMENT_TYPE_FULL_TIME => 'LABEL_EMPLOYMENT_TYPE_FULL_TIME',
835
                    Job::EMPLOYMENT_TYPE_PART_TIME  => 'LABEL_EMPLOYMENT_TYPE_PART_TIME',
836
                    Job::EMPLOYMENT_TYPE_TEMPORARY => 'LABEL_EMPLOYMENT_TYPE_TEMPORARY',
837
                ];
838
 
839
                $search = Functions::sanitizeFilterString($this->params()->fromQuery('search', ''));
840
 
841
                $queryMapper = QueryMapper::getInstance($this->adapter);
842
 
843
                $select = $queryMapper->getSql()->select();
844
                $select->columns(['id', 'uuid', 'title', 'employment_type', 'last_date_of_application']);
845
                $select->from(['j' => JobMapper::_TABLE]);
846
                $select->join(['js' => JobSaveMapper::_TABLE], 'j.id = js.job_id');
847
                $select->join(['c' => CompanyMapper::_TABLE], 'j.company_id = c.id',['image', 'comp_uuid'=>'uuid']);
848
                $select->where->equalTo('j.network_id', $currentUser->network_id);
849
                $select->where->equalTo('j.status', Job::STATUS_ACTIVE);
850
                $select->where->equalTo('js.user_id', $currentUser->id);
851
 
852
                if($search) {
853
                    $select->where->like('title', '%' . $search . '%');
854
                }
855
 
856
                $select->order('last_date_of_application DESC');
857
 
858
                // $selectString = $queryMapper->getSql()->buildSqlString($select);
859
 
860
                // print_r(
861
                //     $selectString
862
                // );
863
 
864
 
865
                $records = $queryMapper->fetchAll($select);
866
 
867
                // print_r($records);
868
                $items = [];
869
                foreach($records as $record)
870
                {
871
 
872
                    $item = [
873
                        'id' => $record['uuid'],
874
                        'title' => $record['title'],
875
                        'employment_type' => $employment_types[$record['employment_type']],
876
                        'last_date_of_application' => $record['last_date_of_application'],
877
                        'image' => $this->url()->fromRoute('storage', ['type' => 'company', 'code' => $record['comp_uuid'], 'filename' => $record['image']]),
878
                        'link_remove' => $this->url()->fromRoute('job/remove-save-job', ['id' => $record['uuid']  ]),
879
                        'link_view' => $this->url()->fromRoute('job/view', ['id' => $record['uuid'] ]),
880
                    ];
881
 
882
                    array_push($items, $item);
883
                }
884
 
885
 
886
 
887
                $response = [
888
                    'success' => true,
889
                    'data' => $items
890
                ];
891
 
892
                return new JsonModel($response);
893
 
894
 
895
        } else {
896
            return new JsonModel([
897
                'success' => false,
898
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
899
            ]);
900
        }
901
    }
902
 
903
}