Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 17315 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 www 1
<?php
5992 stevensc 2
 
1 www 3
/**
4
 *
5
 * Controlador: Mis Perfiles
6
 *
7
 */
5992 stevensc 8
 
1 www 9
declare(strict_types=1);
10
 
11
namespace LeadersLinked\Controller;
12
 
13
use Laminas\Db\Adapter\AdapterInterface;
16768 efrain 14
 
1 www 15
use Laminas\Mvc\Controller\AbstractActionController;
16
use Laminas\Log\LoggerInterface;
17
use Laminas\View\Model\ViewModel;
18
use Laminas\View\Model\JsonModel;
19
 
20
use LeadersLinked\Library\Functions;
21
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
22
use LeadersLinked\Library\Image;
23
 
24
use LeadersLinked\Mapper\CompanyLocationMapper;
17002 efrain 25
use LeadersLinked\Form\CompanyProfile\ProfileSocialNetworkForm;
26
use LeadersLinked\Form\CompanyProfile\ProfileLocationForm;
27
use LeadersLinked\Form\CompanyProfile\ProfileExtendedForm;
28
use LeadersLinked\Form\CompanyProfile\ProfileImageForm;
29
use LeadersLinked\Form\CompanyProfile\ProfileCoverForm;
30
use LeadersLinked\Form\CompanyProfile\ProfileHeaderForm;
31
use LeadersLinked\Form\CompanyProfile\ProfileFooterForm;
1 www 32
 
17002 efrain 33
use LeadersLinked\Form\CompanyProfile\ProfileIndustryForm;
34
use LeadersLinked\Form\CompanyProfile\ProfileCompanySizeForm;
35
use LeadersLinked\Form\CompanyProfile\ProfileFoundationYearForm;
36
use LeadersLinked\Form\CompanyProfile\ProfileWebsiteForm;
1 www 37
use LeadersLinked\Model\Location;
38
use LeadersLinked\Model\CompanyLocation;
39
use LeadersLinked\Model\CompanyUser;
40
 
41
use LeadersLinked\Mapper\LocationMapper;
42
use LeadersLinked\Mapper\CompanyFollowerMapper;
43
use LeadersLinked\Mapper\CompanyUserMapper;
44
use LeadersLinked\Mapper\CompanyMapper;
45
use LeadersLinked\Mapper\IndustryMapper;
46
use LeadersLinked\Mapper\CompanySizeMapper;
16766 efrain 47
use LeadersLinked\Mapper\UserMapper;
17002 efrain 48
use LeadersLinked\Library\Storage;
1 www 49
 
50
class ProfileController extends AbstractActionController
51
{
52
    /**
53
     *
16769 efrain 54
     * @var \Laminas\Db\Adapter\AdapterInterface
1 www 55
     */
56
    private $adapter;
17291 ariadna 57
 
1 www 58
    /**
59
     *
16769 efrain 60
     * @var \LeadersLinked\Cache\CacheInterface
1 www 61
     */
16769 efrain 62
    private $cache;
17291 ariadna 63
 
64
 
16769 efrain 65
    /**
66
     *
67
     * @var \Laminas\Log\LoggerInterface
68
     */
1 www 69
    private $logger;
17291 ariadna 70
 
1 www 71
    /**
72
     *
73
     * @var array
74
     */
75
    private $config;
17291 ariadna 76
 
77
 
1 www 78
    /**
79
     *
16769 efrain 80
     * @var \Laminas\Mvc\I18n\Translator
81
     */
82
    private $translator;
17291 ariadna 83
 
84
 
16769 efrain 85
    /**
86
     *
87
     * @param \Laminas\Db\Adapter\AdapterInterface $adapter
88
     * @param \LeadersLinked\Cache\CacheInterface $cache
89
     * @param \Laminas\Log\LoggerInterface LoggerInterface $logger
1 www 90
     * @param array $config
16769 efrain 91
     * @param \Laminas\Mvc\I18n\Translator $translator
1 www 92
     */
16769 efrain 93
    public function __construct($adapter, $cache, $logger, $config, $translator)
1 www 94
    {
16769 efrain 95
        $this->adapter      = $adapter;
96
        $this->cache        = $cache;
97
        $this->logger       = $logger;
98
        $this->config       = $config;
99
        $this->translator   = $translator;
1 www 100
    }
101
 
5992 stevensc 102
 
103
 
1 www 104
    /**
105
     * Presenta el perfil con las opciónes de edición de cada sección
106
     * @return \Laminas\Http\Response|\Laminas\View\Model\ViewModel|\Laminas\View\Model\JsonModel
107
     */
108
    public function indexAction()
109
    {
110
        $currentUserPlugin = $this->plugin('currentUserPlugin');
111
        $currentUser = $currentUserPlugin->getUser();
112
        $currentCompany = $currentUserPlugin->getCompany();
113
 
5992 stevensc 114
 
1 www 115
        $request = $this->getRequest();
5992 stevensc 116
        if ($request->isGet()) {
117
 
1 www 118
            $sandbox = $this->config['leaderslinked.runmode.sandbox'];
5992 stevensc 119
            if ($sandbox) {
1 www 120
                $google_map_key  = $this->config['leaderslinked.google_map.sandbox_api_key'];
121
            } else {
122
                $google_map_key  = $this->config['leaderslinked.google_map.production_api_key'];
123
            }
5992 stevensc 124
 
1 www 125
            $companyMapper = CompanyMapper::getInstance($this->adapter);
126
            $company = $companyMapper->fetchOne($currentCompany->id);
127
 
5992 stevensc 128
 
1 www 129
            $companyLocationMapper = CompanyLocationMapper::getInstance($this->adapter);
130
            $records = $companyLocationMapper->fetchAllLocationByCompanyId($company->id);
131
 
132
 
133
 
5992 stevensc 134
 
1 www 135
            $locations = [];
5992 stevensc 136
            foreach ($records as $record) {
1 www 137
                $location =  [
138
                    'formatted_address'  => $record['formatted_address'],
139
                    'country' => $record['country'],
140
                    'is_main' => $record['is_main'],
5992 stevensc 141
                    'link_edit' => $this->url()->fromRoute('profile/location', ['operation' => 'edit', 'id' =>  $record['company_location_uuid']]),
142
                    'link_delete' => $this->url()->fromRoute('profile/location', ['operation' => 'delete', 'id' => $record['company_location_uuid']])
143
 
1 www 144
                ];
5992 stevensc 145
 
1 www 146
                array_push($locations, $location);
147
            }
5992 stevensc 148
 
1 www 149
            $industryMapper = IndustryMapper::getInstance($this->adapter);
150
            $companySizeMapper = CompanySizeMapper::getInstance($this->adapter);
151
 
17002 efrain 152
            $formSocialNetwork = new ProfileSocialNetworkForm();
153
            $formLocation = new ProfileLocationForm();
154
            $formExtended = new ProfileExtendedForm();
155
            $formFoundationYear = new ProfileFoundationYearForm();
156
            $formWebsite = new ProfileWebsiteForm();
157
            $formImage = new ProfileImageForm($this->config);
158
            $formCover = new ProfileCoverForm($this->config);
159
            $formHeader = new ProfileHeaderForm($this->config);
160
            $formFooter = new ProfileFooterForm($this->config);
768 geraldo 161
 
162
 
163
 
17002 efrain 164
            $formCompanySize = new ProfileCompanySizeForm($this->adapter);
165
            $formIndustry = new ProfileIndustryForm($this->adapter);
5992 stevensc 166
 
1 www 167
            $companyFollowerMapper = CompanyFollowerMapper::getInstance($this->adapter);
168
            $follower = $companyFollowerMapper->getCountFollowers($currentCompany->id);
169
 
170
            $image_size_cover = $this->config['leaderslinked.image_sizes.company_cover_upload'];
171
            $image_size_profile = $this->config['leaderslinked.image_sizes.company_image_upload'];
1101 geraldo 172
            $image_size_header = $this->config['leaderslinked.image_sizes.company_header_pdf_upload'];
5992 stevensc 173
            $image_size_footer = $this->config['leaderslinked.image_sizes.company_footer_pdf_upload'];
174
 
175
 
1 www 176
            $industry = $industryMapper->fetchOne($company->industry_id);
177
            $companySize = $companySizeMapper->fetchOne($company->company_size_id);
5992 stevensc 178
 
179
 
1 www 180
            $this->layout()->setTemplate('layout/layout-backend');
181
            $viewModel = new ViewModel();
182
            $viewModel->setTemplate('leaders-linked/profile/index.phtml');
183
            $viewModel->setVariables([
184
                'google_map_key'        => $google_map_key,
185
                'follower'              => $follower,
186
                'company_id'            => $currentCompany->id,
187
                'company_name'          => $company->name,
188
                'company_uuid'          => $company->uuid,
189
                'name'                  => trim($company->name),
190
                'image'                 => $company->image,
191
                'cover'                 => $company->cover,
782 geraldo 192
                'header'                 => $company->header,
193
                'footer'                 => $company->footer,
5992 stevensc 194
                'overview'              => strip_tags($company->description, 'p'),
1 www 195
                'website'               => $company->website,
196
                'foundation_year'       => $company->foundation_year,
197
                'facebook'              => $company->facebook,
198
                'instagram'             => $company->instagram,
199
                'twitter'               => $company->twitter,
200
                'locations'             => $locations,
201
                'industry'              => $industry->name,
5992 stevensc 202
                'company_size'          => $companySize->name . ' (' . $companySize->minimum_no_of_employee . '-' . $companySize->maximum_no_of_employee . ')',
1 www 203
                'formLocation'          => $formLocation,
204
                'formSocialNetwork'     => $formSocialNetwork,
205
                'formExtended'          => $formExtended,
206
                'formImage'             => $formImage,
207
                'formCover'             => $formCover,
768 geraldo 208
                'formHeader'             => $formHeader,
209
                'formFooter'             => $formFooter,
1 www 210
                'formFoundationYear'    => $formFoundationYear,
211
                'formWebsite'           => $formWebsite,
212
                'formIndustry'          => $formIndustry,
213
                'formCompanySize'       => $formCompanySize,
214
                'image_size_cover'      => $image_size_cover,
215
                'image_size_profile'    => $image_size_profile,
781 geraldo 216
                'image_size_header'    => $image_size_header,
217
                'image_size_footer'    => $image_size_footer,
1 www 218
            ]);
5992 stevensc 219
            return $viewModel;
1 www 220
        } else {
221
            $data = [
222
                'success' => false,
223
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
224
            ];
5992 stevensc 225
 
1 www 226
            return new JsonModel($data);
227
        }
5992 stevensc 228
 
1 www 229
        return new JsonModel($data);
230
    }
5992 stevensc 231
 
232
 
1 www 233
    /**
234
     *
235
     * Borrar un perfil excepto el público
236
     * @return \Laminas\View\Model\JsonModel
237
     */
238
    public function deleteAction()
239
    {
240
 
241
        $currentUserPlugin = $this->plugin('currentUserPlugin');
242
        $currentUser = $currentUserPlugin->getUser();
243
        $currentCompany = $currentUserPlugin->getCompany();
5992 stevensc 244
 
1 www 245
        $companyUserMapper = CompanyUserMapper::getInstance($this->adapter);
246
        $companyUser = $companyUserMapper->fetchOneByCompanyIdAndUserId($currentCompany->id, $currentUser->id);
5992 stevensc 247
 
248
        if (!$companyUser || $companyUser->type != CompanyUser::CREATOR_YES) {
1 www 249
            $response = [
250
                'success' => false,
251
                'data' => 'ERROR_UNAUTHORIZED'
252
            ];
5992 stevensc 253
 
1 www 254
            return new JsonModel($response);
255
        }
5992 stevensc 256
 
257
 
258
        if ($request->isPost()) {
1 www 259
            $companyMapper = CompanyMapper::getInstance($this->adapter);
260
            $result = $companyMapper->delete($companyUser->id);
5992 stevensc 261
            if ($result) {
1 www 262
                $this->logger->info('Se borro la empresa : ' .  $currentCompany->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
5992 stevensc 263
 
1 www 264
                $data = [
265
                    'success' => true,
266
                    'data' => 'LABEL_RECORD_DELETED'
267
                ];
268
            } else {
5992 stevensc 269
 
1 www 270
                $data = [
271
                    'success'   => false,
272
                    'data'      => $companyUserMapper->getError()
273
                ];
5992 stevensc 274
 
1 www 275
                return new JsonModel($data);
276
            }
277
        } else {
278
            $data = [
279
                'success' => false,
280
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
281
            ];
5992 stevensc 282
 
1 www 283
            return new JsonModel($data);
284
        }
5992 stevensc 285
 
1 www 286
        return new JsonModel($data);
287
    }
5992 stevensc 288
 
289
 
290
 
1 www 291
    /**
292
     * Actualización de la descripción y cualquier otro campo extendido del perfil a futuro
293
     * @return \Laminas\View\Model\JsonModel
294
     */
295
    public function extendedAction()
296
    {
297
 
298
        $currentUserPlugin = $this->plugin('currentUserPlugin');
299
        $currentUser = $currentUserPlugin->getUser();
300
        $currentCompany = $currentUserPlugin->getCompany();
301
 
302
        $request = $this->getRequest();
5992 stevensc 303
        if ($request->isGet()) {
1 www 304
            $companyMapper = CompanyMapper::getInstance($this->adapter);
305
            $company = $companyMapper->fetchOne($currentCompany->id);
5992 stevensc 306
 
1 www 307
            $data = [
308
                'success' => true,
309
                'data' => [
310
                    'description' => $company->description,
311
                ]
312
            ];
5992 stevensc 313
 
1 www 314
            return new JsonModel($data);
5992 stevensc 315
        } else if ($request->isPost()) {
17002 efrain 316
            $form = new ProfileExtendedForm();
1 www 317
            $dataPost = $request->getPost()->toArray();
5992 stevensc 318
 
1 www 319
            $form->setData($dataPost);
5992 stevensc 320
 
321
            if ($form->isValid()) {
1 www 322
                $companyMapper = CompanyMapper::getInstance($this->adapter);
323
                $company = $companyMapper->fetchOne($currentCompany->id);
5992 stevensc 324
 
1 www 325
                $dataPost = (array) $form->getData();
5992 stevensc 326
 
1 www 327
                $hydrator = new ObjectPropertyHydrator();
328
                $hydrator->hydrate($dataPost, $company);
5992 stevensc 329
 
1 www 330
                $companyMapper->updateExtended($company);
5992 stevensc 331
 
1 www 332
                $this->logger->info('Se actualizo las descripción de la empresa ' . $company->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
5992 stevensc 333
 
1 www 334
                return new JsonModel([
335
                    'success'   => true,
336
                    'data' => [
6602 stevensc 337
                        'description' => strip_tags($company->description, 'p')
1 www 338
                    ]
339
                ]);
340
            } else {
341
                $messages = [];
342
                $form_messages = (array) $form->getMessages();
5992 stevensc 343
                foreach ($form_messages  as $fieldname => $field_messages) {
1 www 344
                    $messages[$fieldname] = array_values($field_messages);
345
                }
5992 stevensc 346
 
1 www 347
                return new JsonModel([
348
                    'success'   => false,
349
                    'data'   => $messages
350
                ]);
351
            }
352
        }
5992 stevensc 353
 
354
 
1 www 355
        $data = [
356
            'success' => false,
357
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
358
        ];
5992 stevensc 359
 
360
 
1 www 361
        return new JsonModel($data);
362
    }
5992 stevensc 363
 
1 www 364
    /**
365
     * Actualización de la descripción y cualquier otro campo extendido del perfil a futuro
366
     * @return \Laminas\View\Model\JsonModel
367
     */
368
    public function websiteAction()
369
    {
370
        $currentUserPlugin = $this->plugin('currentUserPlugin');
371
        $currentUser = $currentUserPlugin->getUser();
372
        $currentCompany = $currentUserPlugin->getCompany();
373
 
374
        $request = $this->getRequest();
5992 stevensc 375
        if ($request->isGet()) {
1 www 376
            $companyMapper = CompanyMapper::getInstance($this->adapter);
377
            $company = $companyMapper->fetchOne($currentCompany->id);
5992 stevensc 378
 
1 www 379
            $data = [
380
                'success' => true,
381
                'data' => [
382
                    'website' => $company->website,
383
                ]
384
            ];
5992 stevensc 385
 
1 www 386
            return new JsonModel($data);
5992 stevensc 387
        } else if ($request->isPost()) {
388
 
389
 
17002 efrain 390
            $form = new ProfileWebsiteForm();
1 www 391
            $dataPost = $request->getPost()->toArray();
5992 stevensc 392
 
1 www 393
            $form->setData($dataPost);
5992 stevensc 394
 
395
            if ($form->isValid()) {
1 www 396
                $companyMapper = CompanyMapper::getInstance($this->adapter);
397
                $company = $companyMapper->fetchOne($currentCompany->id);
5992 stevensc 398
 
1 www 399
                $dataPost = (array) $form->getData();
5992 stevensc 400
 
1 www 401
                $hydrator = new ObjectPropertyHydrator();
402
                $hydrator->hydrate($dataPost, $company);
5992 stevensc 403
 
1 www 404
                $companyMapper->updateWebsite($company);
5992 stevensc 405
 
1 www 406
                $this->logger->info('Se actualizo el website de la empresa ' . $company->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
5992 stevensc 407
 
1 www 408
                return new JsonModel([
409
                    'success'   => true,
410
                    'data' => [
411
                        'website' => $company->website,
412
                    ]
413
                ]);
414
            } else {
415
                $messages = [];
416
                $form_messages = (array) $form->getMessages();
5992 stevensc 417
                foreach ($form_messages  as $fieldname => $field_messages) {
1 www 418
                    $messages[$fieldname] = array_values($field_messages);
419
                }
5992 stevensc 420
 
1 www 421
                return new JsonModel([
422
                    'success'   => false,
423
                    'data'   => $messages
424
                ]);
425
            }
426
        }
5992 stevensc 427
 
428
 
1 www 429
        $data = [
430
            'success' => false,
431
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
432
        ];
5992 stevensc 433
 
434
 
1 www 435
        return new JsonModel($data);
436
    }
5992 stevensc 437
 
1 www 438
    /**
439
     * Actualización de la descripción y cualquier otro campo extendido del perfil a futuro
440
     * @return \Laminas\View\Model\JsonModel
441
     */
442
    public function foundationYearAction()
443
    {
5992 stevensc 444
 
1 www 445
        $currentUserPlugin = $this->plugin('currentUserPlugin');
446
        $currentUser = $currentUserPlugin->getUser();
447
        $currentCompany = $currentUserPlugin->getCompany();
448
 
5992 stevensc 449
 
450
 
1 www 451
        $request = $this->getRequest();
5992 stevensc 452
        if ($request->isGet()) {
1 www 453
            $companyMapper = CompanyMapper::getInstance($this->adapter);
454
            $company = $companyMapper->fetchOne($currentCompany->id);
5992 stevensc 455
 
1 www 456
            $data = [
457
                'success' => true,
458
                'data' => [
459
                    'foundation_year' => $company->foundation_year,
460
                ]
5992 stevensc 461
 
1 www 462
            ];
5992 stevensc 463
 
1 www 464
            return new JsonModel($data);
5992 stevensc 465
        } else if ($request->isPost()) {
466
 
467
 
17002 efrain 468
            $form = new ProfileFoundationYearForm();
1 www 469
            $dataPost = $request->getPost()->toArray();
5992 stevensc 470
 
1 www 471
            $form->setData($dataPost);
5992 stevensc 472
 
473
            if ($form->isValid()) {
1 www 474
                $companyMapper = CompanyMapper::getInstance($this->adapter);
475
                $company = $companyMapper->fetchOne($currentCompany->id);
5992 stevensc 476
 
1 www 477
                $dataPost = (array) $form->getData();
5992 stevensc 478
 
1 www 479
                $hydrator = new ObjectPropertyHydrator();
480
                $hydrator->hydrate($dataPost, $company);
5992 stevensc 481
 
1 www 482
                $companyMapper->updateFoundationYear($company);
5992 stevensc 483
 
1 www 484
                $this->logger->info('Se actualizo el año de fundación de la empresa ' . $company->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
5992 stevensc 485
 
1 www 486
                return new JsonModel([
487
                    'success'   => true,
488
                    'data' => $company->foundation_year,
489
                ]);
490
            } else {
491
                $messages = [];
492
                $form_messages = (array) $form->getMessages();
5992 stevensc 493
                foreach ($form_messages  as $fieldname => $field_messages) {
1 www 494
                    $messages[$fieldname] = array_values($field_messages);
495
                }
5992 stevensc 496
 
1 www 497
                return new JsonModel([
498
                    'success'   => false,
499
                    'data'   => $messages
500
                ]);
501
            }
502
        }
5992 stevensc 503
 
504
 
1 www 505
        $data = [
506
            'success' => false,
507
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
508
        ];
5992 stevensc 509
 
510
 
1 www 511
        return new JsonModel($data);
512
    }
5992 stevensc 513
 
514
 
1 www 515
    /**
516
     * Actualización de la ubucación
517
     * @return \Laminas\View\Model\JsonModel
518
     */
519
    public function locationAction()
520
    {
521
        $currentUserPlugin = $this->plugin('currentUserPlugin');
522
        $currentUser = $currentUserPlugin->getUser();
523
        $currentCompany = $currentUserPlugin->getCompany();
5992 stevensc 524
 
1 www 525
        $operation = $this->params()->fromRoute('operation');
5992 stevensc 526
        if ($operation == 'edit' || $operation == 'delete') {
1 www 527
            $id = $this->params()->fromRoute('id');
5992 stevensc 528
            if (!$id) {
1 www 529
                $response = [
530
                    'success' => false,
531
                    'data' => 'ERROR_INVALID_PARAMETER'
532
                ];
5992 stevensc 533
 
1 www 534
                return new JsonModel($response);
535
            }
536
        } else {
537
            $id = '';
538
        }
539
 
540
        $request = $this->getRequest();
5992 stevensc 541
        if ($request->isPost()) {
1 www 542
            $companyMapper = CompanyMapper::getInstance($this->adapter);
543
            $company = $companyMapper->fetchOne($currentCompany->id);
5992 stevensc 544
 
1 www 545
            $companyLocationMapper = CompanyLocationMapper::getInstance($this->adapter);
5992 stevensc 546
 
547
 
1 www 548
            $companyLocation = null;
5992 stevensc 549
            if ($id) {
1 www 550
                $companyLocation = $companyLocationMapper->fetchOneByUuid($id);
5992 stevensc 551
                if (!$companyLocation) {
1 www 552
                    $response = [
553
                        'success' => false,
554
                        'data' => 'ERROR_RECORD_NOT_FOUND'
555
                    ];
5992 stevensc 556
 
1 www 557
                    return new JsonModel($response);
558
                }
5992 stevensc 559
 
560
                if ($companyLocation->company_id != $company->id) {
1 www 561
                    $response = [
562
                        'success' => false,
563
                        'data' => 'ERROR_UNAUTHORIZED'
564
                    ];
565
                    return new JsonModel($response);
566
                }
567
            }
5992 stevensc 568
 
1 www 569
            $locationMapper = LocationMapper::getInstance($this->adapter);
570
            $response = [];
571
 
572
 
5992 stevensc 573
 
574
            if ($operation == 'delete' && $companyLocation) {
575
 
576
                if ($companyLocationMapper->delete($companyLocation->id)) {
1 www 577
                    $this->logger->info('Se borrar una ubicación de la empresa: ' . $company->name, ['user_id' => $currentUser->id,  'ip' => Functions::getUserIP()]);
578
                    $locationMapper->delete($companyLocation->location_id);
579
                } else {
580
                    $response = [
581
                        'success'   => false,
582
                        'data' => 'ERROR_THERE_WAS_AN_ERROR'
583
                    ];
584
                }
585
            } else {
17002 efrain 586
                $form = new ProfileLocationForm();
1 www 587
                $dataPost = $request->getPost()->toArray();
5992 stevensc 588
 
589
 
1 www 590
                $dataPost['is_main'] = isset($dataPost['is_main']) ? $dataPost['is_main'] : CompanyLocation::IS_MAIN_NO;
591
 
5992 stevensc 592
 
1 www 593
                $form->setData($dataPost);
5992 stevensc 594
 
595
                if ($form->isValid()) {
596
 
597
                    if ($operation == 'edit') {
1 www 598
                        $location = $locationMapper->fetchOne($companyLocation->location_id);
599
                    } else {
600
                        $location = new Location();
601
                    }
5992 stevensc 602
 
1 www 603
                    $dataPost = (array) $form->getData();
604
                    $hydrator = new ObjectPropertyHydrator();
605
                    $hydrator->hydrate($dataPost, $location);
5992 stevensc 606
 
607
                    if ($operation == 'edit') {
608
 
609
 
1 www 610
                        $companyLocation->is_main = $dataPost['is_main'];
611
                        $companyLocationMapper->update($companyLocation);
5992 stevensc 612
 
613
 
1 www 614
                        $location = $locationMapper->update($location);
5992 stevensc 615
                        $this->logger->info('Se actualizo una ubicación de la empresa: ' . $company->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
1 www 616
                    } else {
5992 stevensc 617
 
1 www 618
                        $result = $locationMapper->insert($location);
5992 stevensc 619
 
620
                        if ($result) {
1 www 621
                            $companyLocation = new CompanyLocation();
622
                            $companyLocation->company_id = $currentCompany->id;
623
                            $companyLocation->location_id = $location->id;
624
                            $companyLocation->is_main = $dataPost['is_main'];
5992 stevensc 625
 
1 www 626
                            $companyLocationMapper->insert($companyLocation);
5992 stevensc 627
                            $this->logger->info('Se agrego una ubicación a la empresa: ' . $company->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
1 www 628
                        } else {
629
                            $response = [
630
                                'success'   => false,
631
                                'data' => 'ERROR_THERE_WAS_AN_ERROR'
632
                            ];
633
                        }
634
                    }
635
                } else {
636
                    return new JsonModel([
637
                        'success'   => false,
638
                        'data'   =>   'ERROR_PLACED_AUTOCOMPLETE_DOES_NOT_CONTAIN_GEOMETRY'
639
                    ]);
640
                }
641
            }
5992 stevensc 642
 
643
            if (!$response) {
1 www 644
                $records = $companyLocationMapper->fetchAllLocationByCompanyId($currentCompany->id);
5992 stevensc 645
 
646
 
1 www 647
                $locations = [];
5992 stevensc 648
                foreach ($records as $record) {
1 www 649
                    $location =  [
650
                        'formatted_address'  => $record['formatted_address'],
651
                        'country' => $record['country'],
652
                        'is_main' => $record['is_main'],
5992 stevensc 653
                        'link_edit' => $this->url()->fromRoute('profile/location', ['id' => $record['company_location_uuid'], 'operation' => 'edit']),
4278 efrain 654
                        'link_delete' => $this->url()->fromRoute('profile/location', ['id' => $record['company_location_uuid'], 'operation' => 'delete'])
5992 stevensc 655
 
1 www 656
                    ];
5992 stevensc 657
 
1 www 658
                    array_push($locations, $location);
659
                }
5992 stevensc 660
 
1 www 661
                $response = [
662
                    'success' => true,
663
                    'data' => $locations
664
                ];
665
            }
5992 stevensc 666
 
1 www 667
            return new JsonModel($response);
5992 stevensc 668
        } else if ($request->isGet() && $operation == 'edit') {
1 www 669
 
5992 stevensc 670
 
671
 
1 www 672
            $companyLocationMapper = CompanyLocationMapper::getInstance($this->adapter);
673
            $companyLocation = $companyLocationMapper->fetchOneByUuid($id);
674
 
5992 stevensc 675
 
676
 
677
            if (!$companyLocation) {
1 www 678
                $response = [
679
                    'success' => false,
680
                    'data' => 'ERROR_RECORD_NOT_FOUND'
681
                ];
5992 stevensc 682
 
1 www 683
                return new JsonModel($response);
684
            }
5992 stevensc 685
 
686
            if ($companyLocation->company_id != $currentCompany->id) {
1 www 687
                $response = [
688
                    'success' => false,
689
                    'data' => 'ERROR_UNAUTHORIZED'
690
                ];
691
                return new JsonModel($response);
692
            }
5992 stevensc 693
 
1 www 694
            $locationMapper = LocationMapper::getInstance($this->adapter);
695
            $location = $locationMapper->fetchOne($companyLocation->location_id);
696
 
697
            $hydrator = new ObjectPropertyHydrator();
698
            $data = $hydrator->extract($location);
5992 stevensc 699
 
1 www 700
            $data['is_main'] = $companyLocation->is_main;
5992 stevensc 701
 
1 www 702
            $response = [
703
                'success' => true,
704
                'data' =>  $data
705
            ];
5992 stevensc 706
 
1 www 707
            return new JsonModel($response);
708
        }
5992 stevensc 709
 
1 www 710
        $response = [
711
            'success' => false,
712
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
713
        ];
5992 stevensc 714
 
715
 
1 www 716
        return new JsonModel($response);
717
    }
5992 stevensc 718
 
1 www 719
    /**
720
     * Actualización de las redes sociales
721
     * @return \Laminas\View\Model\JsonModel
722
     */
723
    public function socialNetworkAction()
724
    {
5992 stevensc 725
 
726
 
1 www 727
        $currentUserPlugin = $this->plugin('currentUserPlugin');
728
        $currentUser = $currentUserPlugin->getUser();
729
        $currentCompany = $currentUserPlugin->getCompany();
730
 
5992 stevensc 731
 
732
 
1 www 733
        $request = $this->getRequest();
5992 stevensc 734
        if ($request->isGet()) {
1 www 735
            $companyMapper = CompanyMapper::getInstance($this->adapter);
736
            $company = $companyMapper->fetchOne($currentCompany->id);
5992 stevensc 737
 
1 www 738
            $data = [
739
                'success' => true,
740
                'data' => [
741
                    'facebook' => $company->facebook,
742
                    'instagram' => $company->instagram,
743
                    'twitter' => $company->twitter
744
                ]
745
            ];
5992 stevensc 746
 
1 www 747
            return new JsonModel($data);
5992 stevensc 748
        } else if ($request->isPost()) {
749
 
17002 efrain 750
            $form = new ProfileSocialNetworkForm();
1 www 751
            $dataPost = $request->getPost()->toArray();
5992 stevensc 752
 
1 www 753
            $form->setData($dataPost);
5992 stevensc 754
 
755
            if ($form->isValid()) {
1 www 756
                $companyMapper = CompanyMapper::getInstance($this->adapter);
757
                $company = $companyMapper->fetchOne($currentCompany->id);
5992 stevensc 758
 
759
                $this->logger->info('Se actualizaron las redes sociales de la empresa: ' . $company->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
760
 
1 www 761
                $dataPost = (array) $form->getData();
5992 stevensc 762
 
1 www 763
                $hydrator = new ObjectPropertyHydrator();
764
                $hydrator->hydrate($dataPost, $company);
5992 stevensc 765
 
1 www 766
                $companyMapper->updateSocialNetwork($company);
767
                return new JsonModel([
768
                    'success'   => true,
769
                    'data' => [
770
                        'facebook' => $company->facebook,
771
                        'instagram' => $company->instagram,
772
                        'twitter' => $company->twitter
773
                    ]
774
                ]);
775
            } else {
776
                $messages = [];
777
                $form_messages = (array) $form->getMessages();
5992 stevensc 778
                foreach ($form_messages  as $fieldname => $field_messages) {
1 www 779
                    $messages[$fieldname] = array_values($field_messages);
780
                }
5992 stevensc 781
 
1 www 782
                return new JsonModel([
783
                    'success'   => false,
784
                    'data'   => $messages
785
                ]);
786
            }
787
        }
5992 stevensc 788
 
789
 
1 www 790
        $data = [
791
            'success' => false,
792
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
793
        ];
5992 stevensc 794
 
795
 
1 www 796
        return new JsonModel($data);
797
    }
5992 stevensc 798
 
799
 
800
 
1 www 801
    /**
17291 ariadna 802
     * Maneja las operaciones de imagen para un perfil de empresa, incluyendo carga y eliminación.
803
     *
1 www 804
     * @return \Laminas\View\Model\JsonModel
805
     */
806
    public function imageAction()
807
    {
17305 ariadna 808
        // Obtener el objeto de solicitud
809
        $request = $this->getRequest();
810
 
17291 ariadna 811
        // Recuperar la información del usuario y la empresa actual
1 www 812
        $currentUserPlugin = $this->plugin('currentUserPlugin');
813
        $currentUser = $currentUserPlugin->getUser();
814
        $currentCompany = $currentUserPlugin->getCompany();
5992 stevensc 815
 
17291 ariadna 816
        // Determinar el tipo de operación (carga o eliminación)
1 www 817
        $operation  = $this->params()->fromRoute('operation');
818
 
17305 ariadna 819
        if (!$request->isPost()) {
820
            return new JsonModel([
821
                'success'   => false,
822
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
823
            ]);
824
        }
1 www 825
 
17305 ariadna 826
        // Inicializar almacenamiento y rutas para imágenes de empresa y usuario
827
        $storage = Storage::getInstance($this->config, $this->adapter);
828
        $target_company_path = $storage->getPathCompany();
829
        $target_user_path = $storage->getPathUser();
5992 stevensc 830
 
17305 ariadna 831
        // Obtener los detalles de la empresa actual
832
        $companyMapper = CompanyMapper::getInstance($this->adapter);
833
        $company = $companyMapper->fetchOne($currentCompany->id);
5992 stevensc 834
 
17305 ariadna 835
        // Manejar la operación de eliminación
836
        if ($operation == 'delete') {
837
            $this->logger->info('Se borro la imagen de la empresa: ' . $company->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
5992 stevensc 838
 
17305 ariadna 839
            // Verificar si la empresa tiene una imagen e intentar eliminarla
840
            if ($company->image) {
5992 stevensc 841
 
17305 ariadna 842
                // Si la eliminación falla, devolver una respuesta de error
843
                if (!$storage->deleteFile($target_company_path, $company->uuid, $company->image)) {
1 www 844
                    return new JsonModel([
845
                        'success'   => false,
846
                        'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
847
                    ]);
848
                }
17305 ariadna 849
            }
17291 ariadna 850
 
17305 ariadna 851
            // Limpiar el campo de imagen en el registro de la empresa
852
            $company->image = '';
853
            if (!$companyMapper->updateImage($company)) {
854
                return new JsonModel([
855
                    'success'   => false,
856
                    'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
857
                ]);
858
            }
17291 ariadna 859
 
17305 ariadna 860
            // Obtener el propietario de la empresa y actualizar su imagen si es necesario
861
            $companyUserMapper = CompanyUserMapper::getInstance($this->adapter);
862
            $companyUser = $companyUserMapper->fetchOwnerByCompanyId($company->id);
17002 efrain 863
 
17305 ariadna 864
            if ($companyUser) {
865
                $userMapper = UserMapper::getInstance($this->adapter);
866
                $user = $userMapper->fetchOne($companyUser->user_id);
867
                if ($user) {
17291 ariadna 868
 
17305 ariadna 869
                    // Eliminar la imagen del usuario si existe
870
                    if ($user->image) {
871
                        $storage->deleteFile($target_user_path, $user->uuid, $user->image);
16766 efrain 872
                    }
1 www 873
 
17305 ariadna 874
                    // Limpiar el campo de imagen del usuario
875
                    $user->image = '';
876
                    $userMapper->updateImage($user);
5992 stevensc 877
 
17305 ariadna 878
                    // Actualizar el nombre del usuario si difiere del nombre de la empresa
879
                    if ($company->name != $user->first_name) {
880
                        $user->first_name = $company->name;
881
                        $user->last_name = '';
882
                        $userMapper->updateFirstNameAndLastName($user);
1 www 883
                    }
17301 ariadna 884
                }
17305 ariadna 885
            }
5992 stevensc 886
 
17305 ariadna 887
            return new JsonModel([
888
                'success'   => true,
889
                'data' => $storage->getCompanyImage($company)
1 www 890
 
17305 ariadna 891
            ]);
892
        }
17291 ariadna 893
 
17305 ariadna 894
        // Manejar la operación de carga de imagen
895
        $form = new ProfileImageForm($this->config);
896
        $files = $request->getFiles()->toArray();
17291 ariadna 897
 
17305 ariadna 898
        // Establecer los datos del formulario y los archivos
899
        $form->setData($files);
900
        $storage->setFiles($files);
5992 stevensc 901
 
17305 ariadna 902
        // Validar los datos del formulario
903
        if (!$form->isValid()) {
904
            $messages = [];
905
            $form_messages = (array) $form->getMessages();
906
            foreach ($form_messages  as $fieldname => $field_messages) {
907
                $messages[$fieldname] = array_values($field_messages);
908
            }
909
            return new JsonModel([
910
                'success'   => false,
911
                'data'   => $messages
912
            ]);
913
        }
5992 stevensc 914
 
17305 ariadna 915
        // Verificar y establecer el nombre de archivo actual
916
        if (!$storage->setCurrentFilename('image')) {
917
            return new JsonModel([
918
                'success'   => false,
919
                'data'   => 'ERROR_UPLOAD_FILE'
920
            ]);
921
        }
17291 ariadna 922
 
17307 ariadna 923
        // Obtener dimensiones objetivo para el redimensionamiento
17305 ariadna 924
        list($target_width_str, $target_height_str) = explode('x', $this->config['leaderslinked.image_sizes.company_image_size']);
925
        $target_width = (int)$target_width_str;
17307 ariadna 926
        $target_height = (int)$target_height_str;
17291 ariadna 927
 
17305 ariadna 928
        // Generar nombres de archivo únicos
929
        $source = $storage->getTmpFilename();
930
        $filename = 'company-' . uniqid() . '.png';
931
        $target_filename = $storage->composePathToFilename(Storage::TYPE_COMPANY, $company->uuid, $filename);
17291 ariadna 932
 
17315 ariadna 933
        // Subir y redimensionar la imagen
17307 ariadna 934
        if (!$storage->uploadImageResize($source, $target_filename, $target_width, $target_height)) {
17315 ariadna 935
            return new JsonModel([
936
                'success'   => false,
937
                'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
938
            ]);
17305 ariadna 939
        }
5992 stevensc 940
 
17305 ariadna 941
        // Eliminar la imagen anterior si existe
942
        if ($company->image) {
943
            if (!$storage->deleteFile($target_company_path, $company->uuid, $company->image)) {
944
                return $this->_createSimpleErrorResponse('ERROR_THERE_WAS_AN_ERROR');
945
            }
1 www 946
        }
947
 
17305 ariadna 948
        // Actualizar el perfil con la nueva imagen
949
        $company->image = $filename;
950
        if (!$companyMapper->update($company)) {
951
            return $this->_createSimpleErrorResponse('ERROR_THERE_WAS_AN_ERROR');
952
        }
5992 stevensc 953
 
17307 ariadna 954
        // Actualizar la imagen del propietario si es necesario
17311 ariadna 955
        $companyUserMapper = CompanyUserMapper::getInstance($this->adapter);
17307 ariadna 956
        $companyUser = $companyUserMapper->fetchOwnerByCompanyId($company->id);
957
 
958
        if ($companyUser) {
959
            $userMapper = UserMapper::getInstance($this->adapter);
960
            $user = $userMapper->fetchOne($companyUser->user_id);
961
 
962
            if ($user) {
17313 ariadna 963
                // Generar nombre de archivo para la imagen del usuario
17307 ariadna 964
                $user_filename = 'user-' . uniqid() . '.png';
965
                $target_filename_user = $storage->composePathToFilename(Storage::TYPE_USER, $user->uuid, $user_filename);
966
 
17313 ariadna 967
                // Copiar la imagen de la empresa en la imagen del usuario
17312 ariadna 968
                if (!$storage->copyFile($target_filename, $target_filename_user)) {
17307 ariadna 969
                    return $this->_createSimpleErrorResponse('ERROR_THERE_WAS_AN_ERROR');
970
                }
971
 
972
                // Eliminar la imagen existente del usuario si está presente
973
                if ($user->image) {
974
                    $storage->deleteFile($target_user_path, $user->uuid, $user->image);
975
                }
976
 
17313 ariadna 977
                // Actualizar la imagen del usuario
17307 ariadna 978
                $user->image = $user_filename;
979
                if (!$userMapper->update($user)) {
980
                    return $this->_createSimpleErrorResponse('ERROR_THERE_WAS_AN_ERROR');
981
                }
982
            }
17311 ariadna 983
        }
17307 ariadna 984
 
17305 ariadna 985
        // Registrar la actualización de la imagen
986
        $this->logger->info('Se actualizo la imagen de la empresa: ' . $company->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
987
 
988
        return new JsonModel([
989
            'success'   => true,
17306 ariadna 990
            'data' => $storage->getCompanyImage($company)
17305 ariadna 991
        ]);
1 www 992
    }
5992 stevensc 993
 
1 www 994
    /**
995
     * Cambio de la imagen de fondo superior (cover) del perfil
996
     * @return \Laminas\View\Model\JsonModel
997
     */
998
    public function coverAction()
999
    {
1000
        $operation          = $this->params()->fromRoute('operation');
1001
 
1002
        $currentUserPlugin = $this->plugin('currentUserPlugin');
1003
        $currentUser = $currentUserPlugin->getUser();
1004
        $currentCompany = $currentUserPlugin->getCompany();
1005
 
17314 ariadna 1006
        $request = $this->getRequest();
5992 stevensc 1007
 
1008
        if ($request->isPost()) {
17291 ariadna 1009
 
17314 ariadna 1010
            $storage = Storage::getInstance($this->config, $this->adapter);
1011
            $target_company_path = $storage->getPathCompany();
1012
            $target_user_path = $storage->getPathUser();
5992 stevensc 1013
 
1 www 1014
            $companyMapper = CompanyMapper::getInstance($this->adapter);
1015
            $company = $companyMapper->fetchOne($currentCompany->id);
5992 stevensc 1016
 
1 www 1017
            $target_path = $this->config['leaderslinked.fullpath.company'] . DIRECTORY_SEPARATOR . $company->uuid;
1018
 
5992 stevensc 1019
            if ($operation == 'delete') {
1020
                if ($company->cover) {
1021
 
17314 ariadna 1022
                    if (!$storage->deleteFile($target_company_path, $company->uuid, $company->cover)) {
1 www 1023
                        return new JsonModel([
1024
                            'success'   => false,
1025
                            'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
1026
                        ]);
1027
                    }
1028
                }
5992 stevensc 1029
 
1 www 1030
                $this->logger->info('Se borro el cover de la empresa ' . $company->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
17314 ariadna 1031
 
1 www 1032
                $company->cover = '';
5992 stevensc 1033
                if (!$companyMapper->updateCover($company)) {
1 www 1034
                    return new JsonModel([
1035
                        'success'   => false,
1036
                        'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
1037
                    ]);
1038
                }
1039
            } else {
17002 efrain 1040
                $form = new ProfileCoverForm($this->config);
17314 ariadna 1041
                $files = $request->getFiles()->toArray();
5992 stevensc 1042
 
17316 ariadna 1043
                // Debug file upload data
1044
                $this->logger->info('Cover upload attempt', [
1045
                    'files' => $files,
1046
                    'has_cover' => isset($files['cover']),
1047
                    'error_code' => isset($files['cover']) ? $files['cover']['error'] : 'no cover field'
1048
                ]);
1049
 
17314 ariadna 1050
                $form->setData($files);
1051
                $storage->setFiles($files);
1 www 1052
 
5992 stevensc 1053
                if ($form->isValid()) {
1054
                    if (!empty($files['cover']['error'])) {
17316 ariadna 1055
                        $this->logger->error('Cover upload error', ['error_code' => $files['cover']['error']]);
1 www 1056
                        return new JsonModel([
1057
                            'success'   => false,
1058
                            'data'   =>  'ERROR_UPLOAD_FILE'
1059
                        ]);
1060
                    }
5992 stevensc 1061
 
17314 ariadna 1062
                    // Verificar y establecer el nombre de archivo actual
17316 ariadna 1063
                    if (!$storage->setCurrentFilename('cover')) {
1064
                        $this->logger->error('Failed to set current filename for cover');
17314 ariadna 1065
                        return new JsonModel([
1066
                            'success'   => false,
1067
                            'data'   => 'ERROR_UPLOAD_FILE'
1068
                        ]);
1 www 1069
                    }
5992 stevensc 1070
 
17314 ariadna 1071
                    // Obtener dimensiones objetivo para el redimensionamiento
1072
                    list($target_width_str, $target_height_str) = explode('x', $this->config['leaderslinked.image_sizes.company_cover_size']);
1073
                    $target_width = (int)$target_width_str;
1074
                    $target_height = (int)$target_height_str;
17291 ariadna 1075
 
17316 ariadna 1076
                    // Debug resize parameters
1077
                    $this->logger->info('Cover resize parameters', [
1078
                        'target_width' => $target_width,
1079
                        'target_height' => $target_height
1080
                    ]);
1081
 
17314 ariadna 1082
                    // Generar nombres de archivo únicos
1083
                    $source = $storage->getTmpFilename();
1084
                    $filename = 'company-cover-' . uniqid() . '.png';
1085
                    $target_filename = $storage->composePathToFilename(Storage::TYPE_COMPANY, $company->uuid, $filename);
1086
 
17316 ariadna 1087
                    // Debug file paths
1088
                    $this->logger->info('Cover file paths', [
1089
                        'source' => $source,
1090
                        'target' => $target_filename
1091
                    ]);
1092
 
17314 ariadna 1093
                    // Subir y redimensionar la imagen
17315 ariadna 1094
                    if (!$storage->uploadImageResize($source, $target_filename, $target_width, $target_height)) {
17316 ariadna 1095
                        $this->logger->error('Failed to resize and upload cover image');
1 www 1096
                        return new JsonModel([
1097
                            'success'   => false,
1098
                            'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
1099
                        ]);
1100
                    }
5992 stevensc 1101
 
17314 ariadna 1102
                    // Eliminar la imagen anterior si existe
1103
                    if ($company->cover) {
17316 ariadna 1104
                        if (!$storage->deleteFile($target_company_path, $company->uuid, $company->cover)) {
1105
                            $this->logger->error('Failed to delete old cover image');
17314 ariadna 1106
                        }
1107
                    }
5992 stevensc 1108
 
17316 ariadna 1109
                    $company->cover = $filename;
5992 stevensc 1110
                    if (!$companyMapper->updateCover($company)) {
17316 ariadna 1111
                        $this->logger->error('Failed to update company cover in database');
1112
                        return new JsonModel([
1113
                            'success'   => false,
1114
                            'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
1115
                        ]);
1 www 1116
                    }
1117
 
17316 ariadna 1118
                    $this->logger->info('Cover image updated successfully', [
1119
                        'company' => $company->name,
1120
                        'filename' => $filename
1121
                    ]);
1 www 1122
                } else {
1123
                    $messages = [];
1124
                    $form_messages = (array) $form->getMessages();
5992 stevensc 1125
                    foreach ($form_messages  as $fieldname => $field_messages) {
1 www 1126
                        $messages[$fieldname] = array_values($field_messages);
1127
                    }
5992 stevensc 1128
 
1 www 1129
                    return new JsonModel([
1130
                        'success'   => false,
1131
                        'data'   => $messages
1132
                    ]);
1133
                }
1134
            }
17291 ariadna 1135
 
1 www 1136
            return new JsonModel([
1137
                'success'   => true,
17002 efrain 1138
                'data' => $storage->getCompanyCover($company)
5992 stevensc 1139
 
1 www 1140
            ]);
1141
        }
5992 stevensc 1142
 
1143
 
1 www 1144
        $data = [
1145
            'success' => false,
1146
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
1147
        ];
5992 stevensc 1148
 
1149
 
1 www 1150
        return new JsonModel($data);
1151
    }
768 geraldo 1152
 
1153
 
1154
 
5992 stevensc 1155
    /**
768 geraldo 1156
     * Cambio de la imagen de de la cabecera de los reportes
1157
     * @return \Laminas\View\Model\JsonModel
1158
     */
1159
    public function headerAction()
1160
    {
1161
        $operation = $this->params()->fromRoute('operation');
1162
 
5992 stevensc 1163
 
768 geraldo 1164
        $currentUserPlugin = $this->plugin('currentUserPlugin');
1165
        $currentUser = $currentUserPlugin->getUser();
1166
        $currentCompany = $currentUserPlugin->getCompany();
1167
 
5992 stevensc 1168
 
1169
 
1170
 
768 geraldo 1171
        $request = $this->getRequest();
5992 stevensc 1172
        if ($request->isPost()) {
1173
 
768 geraldo 1174
            $companyMapper = CompanyMapper::getInstance($this->adapter);
1175
            $company = $companyMapper->fetchOne($currentCompany->id);
5992 stevensc 1176
 
17002 efrain 1177
            $image = Image::getInstance($this->config);
1178
            $target_path = $image->getStorage()->getPathCompany();
768 geraldo 1179
 
5992 stevensc 1180
 
1181
 
1182
            if ($operation == 'delete') {
1183
                if ($company->header) {
1184
 
17002 efrain 1185
                    if (!$image->getStorage()->deleteFile($target_path, $company->uuid, $company->header)) {
768 geraldo 1186
                        return new JsonModel([
1187
                            'success'   => false,
1188
                            'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
1189
                        ]);
1190
                    }
1191
                }
5992 stevensc 1192
 
768 geraldo 1193
                $this->logger->info('Se borro el header  de la empresa ' . $company->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
1194
                $company->header = '';
5992 stevensc 1195
                if (!$companyMapper->updateHeader($company)) {
768 geraldo 1196
                    return new JsonModel([
1197
                        'success'   => false,
1198
                        'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
1199
                    ]);
1200
                }
1201
            } else {
17002 efrain 1202
                $form = new ProfileHeaderForm($this->config);
768 geraldo 1203
                $data   = array_merge($request->getPost()->toArray(), $request->getFiles()->toArray());
5992 stevensc 1204
 
768 geraldo 1205
                $form->setData($data);
1206
 
5992 stevensc 1207
                if ($form->isValid()) {
1208
 
1209
 
768 geraldo 1210
                    $files = $request->getFiles()->toArray();
5992 stevensc 1211
                    if (!empty($files['header']['error'])) {
1212
 
768 geraldo 1213
                        return new JsonModel([
1214
                            'success'   => false,
1215
                            'data'   =>  'ERROR_UPLOAD_FILE'
1216
                        ]);
1217
                    }
5992 stevensc 1218
 
1219
                    if ($company->header) {
1220
 
17002 efrain 1221
                        if (!$image->getStorage()->deleteFile($target_path, $company->uuid, $company->header)) {
768 geraldo 1222
                            return new JsonModel([
1223
                                'success'   => false,
1224
                                'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
1225
                            ]);
1226
                        }
1227
                    }
5992 stevensc 1228
 
1229
                    list($target_width, $target_height) = explode('x', $this->config['leaderslinked.image_sizes.company_header_pdf_size']);
768 geraldo 1230
                    $source             = $files['header']['tmp_name'];
1231
                    $target_filename    = 'company-header-' . uniqid() . '.png';
1232
                    $crop_to_dimensions = false;
17002 efrain 1233
                    $unlink_source = true;
17291 ariadna 1234
 
1235
                    if ($image->uploadProcessChangeSize($source, $target_path, $company->uuid, $target_filename, $target_width, $target_height, $crop_to_dimensions, $unlink_source)) {
768 geraldo 1236
                        return new JsonModel([
1237
                            'success'   => false,
1238
                            'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
1239
                        ]);
1240
                    }
5992 stevensc 1241
 
1242
 
768 geraldo 1243
                    $company->header = $target_filename;
5992 stevensc 1244
                    if (!$companyMapper->updateHeader($company)) {
768 geraldo 1245
                        return new JsonModel([
1246
                            'success'   => false,
1247
                            'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
1248
                        ]);
1249
                    }
5992 stevensc 1250
 
1251
 
768 geraldo 1252
                    $this->logger->info('Se actualizo el header  de la empresa ' . $company->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
1253
                } else {
1254
                    $messages = [];
1255
                    $form_messages = (array) $form->getMessages();
5992 stevensc 1256
                    foreach ($form_messages  as $fieldname => $field_messages) {
768 geraldo 1257
                        $messages[$fieldname] = array_values($field_messages);
1258
                    }
5992 stevensc 1259
 
768 geraldo 1260
                    return new JsonModel([
1261
                        'success'   => false,
1262
                        'data'   => $messages
1263
                    ]);
1264
                }
1265
            }
17291 ariadna 1266
 
17018 efrain 1267
            $storage = Storage::getInstance($this->config, $this->adapter);
17002 efrain 1268
            $path = $storage->getPathCompany();
17291 ariadna 1269
 
768 geraldo 1270
            return new JsonModel([
1271
                'success'   => true,
17002 efrain 1272
                'data' => $company->header ? $storage->getGenericImage($path, $company->uuid, $company->header) : '',
5992 stevensc 1273
 
768 geraldo 1274
            ]);
1275
        }
5992 stevensc 1276
 
1277
 
768 geraldo 1278
        $data = [
1279
            'success' => false,
1280
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
1281
        ];
5992 stevensc 1282
 
1283
 
768 geraldo 1284
        return new JsonModel($data);
1285
    }
1286
 
1287
 
5992 stevensc 1288
    /**
768 geraldo 1289
     * Cambio de la imagen de de la cabecera de los reportes
1290
     * @return \Laminas\View\Model\JsonModel
1291
     */
1292
    public function footerAction()
1293
    {
1294
        $operation          = $this->params()->fromRoute('operation');
1295
 
5992 stevensc 1296
 
768 geraldo 1297
        $currentUserPlugin = $this->plugin('currentUserPlugin');
1298
        $currentUser = $currentUserPlugin->getUser();
1299
        $currentCompany = $currentUserPlugin->getCompany();
1300
 
5992 stevensc 1301
 
1302
 
1303
 
768 geraldo 1304
        $request = $this->getRequest();
5992 stevensc 1305
        if ($request->isPost()) {
1306
 
768 geraldo 1307
            $companyMapper = CompanyMapper::getInstance($this->adapter);
1308
            $company = $companyMapper->fetchOne($currentCompany->id);
5992 stevensc 1309
 
17002 efrain 1310
            $image = Image::getInstance($this->config);
1311
            $target_path = $image->getStorage()->getPathCompany();
768 geraldo 1312
 
5992 stevensc 1313
 
1314
 
1315
            if ($operation == 'delete') {
1316
                if ($company->footer) {
1317
 
17002 efrain 1318
                    if (!$image->getStorage()->deleteFile($target_path, $company->uuid, $company->footer)) {
768 geraldo 1319
                        return new JsonModel([
1320
                            'success'   => false,
1321
                            'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
1322
                        ]);
1323
                    }
1324
                }
5992 stevensc 1325
 
768 geraldo 1326
                $this->logger->info('Se borro el footer  de la empresa ' . $company->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
1327
                $company->footer = '';
5992 stevensc 1328
                if (!$companyMapper->updateFooter($company)) {
768 geraldo 1329
                    return new JsonModel([
1330
                        'success'   => false,
1331
                        'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
1332
                    ]);
1333
                }
1334
            } else {
17002 efrain 1335
                $form = new ProfileFooterForm($this->config);
768 geraldo 1336
                $data   = array_merge($request->getPost()->toArray(), $request->getFiles()->toArray());
5992 stevensc 1337
 
768 geraldo 1338
                $form->setData($data);
1339
 
5992 stevensc 1340
                if ($form->isValid()) {
1341
 
1342
 
768 geraldo 1343
                    $files = $request->getFiles()->toArray();
5992 stevensc 1344
                    if (!empty($files['footer']['error'])) {
1345
 
768 geraldo 1346
                        return new JsonModel([
1347
                            'success'   => false,
1348
                            'data'   =>  'ERROR_UPLOAD_FILE'
1349
                        ]);
1350
                    }
5992 stevensc 1351
 
1352
                    if ($company->footer) {
1353
 
17002 efrain 1354
                        if (!$image->getStorage()->deleteFile($target_path, $company->uuid, $company->footer)) {
768 geraldo 1355
                            return new JsonModel([
1356
                                'success'   => false,
1357
                                'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
1358
                            ]);
1359
                        }
1360
                    }
5992 stevensc 1361
 
1362
                    list($target_width, $target_height) = explode('x', $this->config['leaderslinked.image_sizes.company_footer_pdf_size']);
768 geraldo 1363
                    $source             = $files['footer']['tmp_name'];
1364
                    $target_filename    = 'company-footer-' . uniqid() . '.png';
1365
                    $crop_to_dimensions = false;
17002 efrain 1366
                    $unlink_source = true;
17291 ariadna 1367
 
1368
                    if ($image->uploadProcessChangeSize($source, $target_path, $company->uuid, $target_filename, $target_width, $target_height, $crop_to_dimensions, $unlink_source)) {
768 geraldo 1369
                        return new JsonModel([
1370
                            'success'   => false,
1371
                            'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
1372
                        ]);
1373
                    }
5992 stevensc 1374
 
1375
 
768 geraldo 1376
                    $company->footer = $target_filename;
5992 stevensc 1377
                    if (!$companyMapper->updateFooter($company)) {
768 geraldo 1378
                        return new JsonModel([
1379
                            'success'   => false,
1380
                            'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
1381
                        ]);
1382
                    }
1383
 
1384
 
5992 stevensc 1385
 
1386
 
768 geraldo 1387
                    $this->logger->info('Se actualizo el footer  de la empresa ' . $company->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
1388
                } else {
1389
                    $messages = [];
1390
                    $form_messages = (array) $form->getMessages();
5992 stevensc 1391
                    foreach ($form_messages  as $fieldname => $field_messages) {
768 geraldo 1392
                        $messages[$fieldname] = array_values($field_messages);
1393
                    }
5992 stevensc 1394
 
768 geraldo 1395
                    return new JsonModel([
1396
                        'success'   => false,
1397
                        'data'   => $messages
1398
                    ]);
1399
                }
1400
            }
17291 ariadna 1401
 
17018 efrain 1402
            $storage = Storage::getInstance($this->config, $this->adapter);
17002 efrain 1403
            $path = $storage->getPathCompany();
17291 ariadna 1404
 
768 geraldo 1405
            return new JsonModel([
1406
                'success'   => true,
17002 efrain 1407
                'data' => $company->footer ? $storage->getGenericImage($path, $company->uuid, $company->footer) : '',
5992 stevensc 1408
 
768 geraldo 1409
            ]);
1410
        }
5992 stevensc 1411
 
1412
 
768 geraldo 1413
        $data = [
1414
            'success' => false,
1415
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
1416
        ];
5992 stevensc 1417
 
1418
 
768 geraldo 1419
        return new JsonModel($data);
1420
    }
5992 stevensc 1421
 
1 www 1422
    /**
1423
     * Actualización de la descripción y cualquier otro campo extendido del perfil a futuro
1424
     * @return \Laminas\View\Model\JsonModel
1425
     */
1426
    public function industryAction()
1427
    {
5992 stevensc 1428
 
1 www 1429
        $currentUserPlugin = $this->plugin('currentUserPlugin');
1430
        $currentUser = $currentUserPlugin->getUser();
1431
        $currentCompany = $currentUserPlugin->getCompany();
1432
 
5992 stevensc 1433
 
1434
 
1435
 
1436
 
1437
 
1 www 1438
        $request = $this->getRequest();
5992 stevensc 1439
        if ($request->isGet()) {
1 www 1440
            $companyMapper = CompanyMapper::getInstance($this->adapter);
1441
            $company = $companyMapper->fetchOne($currentCompany->id);
5992 stevensc 1442
 
8367 efrain 1443
            $industries = [];
1 www 1444
            $industryMapper = IndustryMapper::getInstance($this->adapter);
15087 efrain 1445
            $records = $industryMapper->fetchAllActive();
17291 ariadna 1446
 
1447
            foreach ($records as $record) {
8367 efrain 1448
                $industries[$record->uuid] = $record->name;
1449
            }
17291 ariadna 1450
 
1 www 1451
            $industry = $industryMapper->fetchOne($company->industry_id);
5992 stevensc 1452
 
17291 ariadna 1453
 
1454
 
1455
 
1 www 1456
            $data = [
1457
                'success' => true,
1458
                'data' => [
1459
                    'industry_id' => $industry->uuid,
8367 efrain 1460
                    'industries' => $industries
1 www 1461
                ]
1462
            ];
5992 stevensc 1463
 
1 www 1464
            return new JsonModel($data);
5992 stevensc 1465
        } else if ($request->isPost()) {
1466
 
1467
 
17291 ariadna 1468
 
17002 efrain 1469
            $form = new ProfileIndustryForm($this->adapter);
1 www 1470
            $dataPost = $request->getPost()->toArray();
5992 stevensc 1471
 
1 www 1472
            $form->setData($dataPost);
5992 stevensc 1473
 
1474
            if ($form->isValid()) {
1 www 1475
                $companyMapper = CompanyMapper::getInstance($this->adapter);
1476
                $company = $companyMapper->fetchOne($currentCompany->id);
1477
 
1478
                $dataPost = (array) $form->getData();
5992 stevensc 1479
 
1 www 1480
                $industryMapper = IndustryMapper::getInstance($this->adapter);
1481
                $industry = $industryMapper->fetchOneByUuid($dataPost['industry_id']);
5992 stevensc 1482
 
1 www 1483
                $company->industry_id = $industry->id;
1484
                $companyMapper->updateIndustry($company);
5992 stevensc 1485
 
1 www 1486
                $this->logger->info('Se actualizo la industria de la empresa ' . $company->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
5992 stevensc 1487
 
1 www 1488
                $industryMapper = IndustryMapper::getInstance($this->adapter);
1489
                $industry = $industryMapper->fetchOne($company->industry_id);
5992 stevensc 1490
 
1 www 1491
                return new JsonModel([
1492
                    'success'   => true,
1493
                    'data' =>  $industry->name,
5992 stevensc 1494
 
1 www 1495
                ]);
1496
            } else {
17291 ariadna 1497
 
1 www 1498
                $messages = [];
1499
                $form_messages = (array) $form->getMessages();
5992 stevensc 1500
                foreach ($form_messages  as $fieldname => $field_messages) {
1 www 1501
                    $messages[$fieldname] = array_values($field_messages);
1502
                }
5992 stevensc 1503
 
1 www 1504
                return new JsonModel([
1505
                    'success'   => false,
1506
                    'data'   => $messages
1507
                ]);
1508
            }
1509
        }
5992 stevensc 1510
 
1511
 
1 www 1512
        $data = [
1513
            'success' => false,
1514
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
1515
        ];
5992 stevensc 1516
 
1517
 
1 www 1518
        return new JsonModel($data);
1519
    }
5992 stevensc 1520
 
1 www 1521
    /**
1522
     * Actualización de la descripción y cualquier otro campo extendido del perfil a futuro
1523
     * @return \Laminas\View\Model\JsonModel
1524
     */
1525
    public function companySizeAction()
1526
    {
1527
 
1528
        $currentUserPlugin = $this->plugin('currentUserPlugin');
1529
        $currentUser = $currentUserPlugin->getUser();
1530
        $currentCompany = $currentUserPlugin->getCompany();
1531
 
1532
 
5992 stevensc 1533
 
1534
 
1 www 1535
        $request = $this->getRequest();
5992 stevensc 1536
        if ($request->isGet()) {
1 www 1537
            $companyMapper = CompanyMapper::getInstance($this->adapter);
1538
            $company = $companyMapper->fetchOne($currentCompany->id);
5992 stevensc 1539
 
8367 efrain 1540
            $companySizes = [];
17291 ariadna 1541
 
1 www 1542
            $companySizeMapper = CompanySizeMapper::getInstance($this->adapter);
15087 efrain 1543
            $records = $companySizeMapper->fetchAllActive();
17291 ariadna 1544
 
1545
            foreach ($records as $record) {
8367 efrain 1546
                $companySizes[$record->uuid] = $record->name . ' (' . $record->minimum_no_of_employee . '-' . $record->maximum_no_of_employee . ')';
1547
            }
17291 ariadna 1548
 
1549
 
1 www 1550
            $companySize = $companySizeMapper->fetchOne($company->company_size_id);
5992 stevensc 1551
 
1 www 1552
            $data = [
1553
                'success' => true,
1554
                'data' => [
8367 efrain 1555
                    'company_size_id' => $companySize->uuid,
1556
                    'company_sizes' => $companySizes
1 www 1557
                ]
1558
            ];
5992 stevensc 1559
 
1 www 1560
            return new JsonModel($data);
5992 stevensc 1561
        } else if ($request->isPost()) {
1562
 
1563
 
17002 efrain 1564
            $form = new ProfileCompanySizeForm($this->adapter);
1 www 1565
            $dataPost = $request->getPost()->toArray();
5992 stevensc 1566
 
1 www 1567
            $form->setData($dataPost);
5992 stevensc 1568
 
1569
            if ($form->isValid()) {
1 www 1570
                $companyMapper = CompanyMapper::getInstance($this->adapter);
1571
                $company = $companyMapper->fetchOne($currentCompany->id);
5992 stevensc 1572
 
1 www 1573
                $dataPost = (array) $form->getData();
5992 stevensc 1574
 
1 www 1575
                $companySizeMapper = CompanySizeMapper::getInstance($this->adapter);
1576
                $companySize = $companySizeMapper->fetchOneByUuid($dataPost['company_size_id']);
5992 stevensc 1577
 
1 www 1578
                $company->company_size_id = $companySize->id;
14816 efrain 1579
                $companyMapper->updateCompanySize($company);
5992 stevensc 1580
 
1 www 1581
                $this->logger->info('Se actualizo la industria de la empresa ' . $company->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
5992 stevensc 1582
 
1 www 1583
                $companySizeMapper = CompanySizeMapper::getInstance($this->adapter);
1584
                $companySize = $companySizeMapper->fetchOne($company->company_size_id);
5992 stevensc 1585
 
1 www 1586
                return new JsonModel([
1587
                    'success'   => true,
5992 stevensc 1588
                    'data' => $companySize->name . ' (' . $companySize->minimum_no_of_employee . '-' . $companySize->maximum_no_of_employee . ')',
1589
 
1 www 1590
                ]);
1591
            } else {
1592
                $messages = [];
1593
                $form_messages = (array) $form->getMessages();
5992 stevensc 1594
                foreach ($form_messages  as $fieldname => $field_messages) {
1 www 1595
                    $messages[$fieldname] = array_values($field_messages);
1596
                }
5992 stevensc 1597
 
1 www 1598
                return new JsonModel([
1599
                    'success'   => false,
1600
                    'data'   => $messages
1601
                ]);
1602
            }
1603
        }
5992 stevensc 1604
 
1605
 
1 www 1606
        $data = [
1607
            'success' => false,
1608
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
1609
        ];
5992 stevensc 1610
 
1611
 
1 www 1612
        return new JsonModel($data);
1613
    }
17291 ariadna 1614
 
1615
    private function _createSimpleErrorResponse($errorMessageKey)
1616
    {
1617
        return new JsonModel([
1618
            'success' => false,
1619
            'data'    => $errorMessageKey
1620
        ]);
1621
    }
1 www 1622
}