Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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

Rev Autor Línea Nro. Línea
1 www 1
<?php
2
declare(strict_types=1);
3
 
4
namespace LeadersLinked\Controller;
5
 
6
use Laminas\Db\Adapter\AdapterInterface;
7
use Laminas\Mvc\Controller\AbstractActionController;
8
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
9
use Laminas\Log\LoggerInterface;
10
use Laminas\View\Model\ViewModel;
11
use Laminas\View\Model\JsonModel;
12
use LeadersLinked\Mapper\UserMapper;
13
use LeadersLinked\Library\Functions;
14
use LeadersLinked\Mapper\UserPasswordMapper;
15
use LeadersLinked\Form\AccountSetting\NotificationSettingForm;
16
use LeadersLinked\Mapper\UserNotificationSettingMapper;
17
use LeadersLinked\Form\AccountSetting\ChangePasswordForm;
18
use LeadersLinked\Form\AccountSetting\ChangeImageForm;
19
use LeadersLinked\Library\Image;
20
use LeadersLinked\Form\AccountSetting\LocationForm;
21
use LeadersLinked\Model\Location;
22
use LeadersLinked\Mapper\LocationMapper;
23
use LeadersLinked\Form\AccountSetting\PrivacySettingForm;
24
use LeadersLinked\Mapper\UserProfileMapper;
25
use LeadersLinked\Form\AccountSetting\BasicForm;
26
use LeadersLinked\Form\Transaction\FundsAddForm;
27
use LeadersLinked\Mapper\UserBrowserMapper;
28
use LeadersLinked\Mapper\QueryMapper;
29
use LeadersLinked\Mapper\DeviceHistoryMapper;
30
use LeadersLinked\Mapper\DeviceMapper;
31
use Laminas\Hydrator\ArraySerializableHydrator;
32
use Laminas\Db\ResultSet\HydratingResultSet;
33
use Laminas\Paginator\Adapter\DbSelect;
34
use Laminas\Paginator\Paginator;
35
use LeadersLinked\Mapper\UserIpMapper;
36
use LeadersLinked\Model\Transaction;
37
use LeadersLinked\Model\Provider;
38
use LeadersLinked\Mapper\TransactionMapper;
39
use LeadersLinked\Mapper\UserProviderMapper;
40
use LeadersLinked\Model\UserProvider;
41
use LeadersLinked\Model\UserPassword;
1979 efrain 42
use LeadersLinked\Model\UserDeleted;
43
use LeadersLinked\Mapper\UserDeletedMapper;
44
use LeadersLinked\Model\UserType;
45
use LeadersLinked\Model\User;
46
use LeadersLinked\Library\QueueEmail;
47
use LeadersLinked\Mapper\EmailTemplateMapper;
48
use LeadersLinked\Model\EmailTemplate;
6749 efrain 49
use LeadersLinked\Cache\CacheInterface;;
1 www 50
 
51
class AccountSettingController extends AbstractActionController
52
{
53
    /**
54
     *
55
     * @var AdapterInterface
56
     */
57
    private $adapter;
58
 
59
 
60
    /**
61
     *
6749 efrain 62
     * @var CacheInterface
1 www 63
     */
64
    private $cache;
65
 
66
    /**
67
     *
68
     * @var  LoggerInterface
69
     */
70
    private $logger;
71
 
72
    /**
73
     *
74
     * @var array
75
     */
76
    private $config;
77
 
78
 
79
 
80
 
81
    /**
82
     *
83
     * @param AdapterInterface $adapter
6749 efrain 84
     * @param CacheInterface $cache
1 www 85
     * @param LoggerInterface $logger
86
     * @param array $config
87
     */
88
    public function __construct($adapter, $cache , $logger, $config)
89
    {
90
        $this->adapter      = $adapter;
91
        $this->cache        = $cache;
92
        $this->logger       = $logger;
93
        $this->config       = $config;
94
    }
95
 
96
    public function indexAction()
97
    {
98
        $request = $this->getRequest();
99
        if($request->isGet()) {
100
 
4398 efrain 101
            $currentNetworkPlugin = $this->plugin('currentNetworkPlugin');
102
            $currentNetwork = $currentNetworkPlugin->getNetwork();
103
 
104
 
6749 efrain 105
            $tab =  Functions::sanitizeFilterString($this->params()->fromQuery('tab'));
1 www 106
            if(!in_array($tab, ['nav-basic', 'nav-notification', 'nav-password', 'nav-image', 'nav-location', 'nav-privacy', 'nav-ips', 'nav-browsers', 'nav-transactions', 'nav-social-networks'])) {
107
                $tab = 'nav-basic';
108
            }
109
 
110
            $sandbox = $this->config['leaderslinked.runmode.sandbox'];
111
            if($sandbox) {
112
                $google_map_key  = $this->config['leaderslinked.google_map.sandbox_api_key'];
113
            } else {
114
                $google_map_key  = $this->config['leaderslinked.google_map.production_api_key'];
115
            }
116
 
117
            $currentUserPlugin = $this->plugin('currentUserPlugin');
118
            $currentUser = $currentUserPlugin->getUser();
119
 
120
            $userUserNotificationSettingMapper = UserNotificationSettingMapper::getInstance($this->adapter);
121
            $userUserNotificationSetting = $userUserNotificationSettingMapper->fetchOne($currentUser->id);
122
 
123
            $formNotificationSetting = new NotificationSettingForm();
124
            $formNotificationSetting->setData((array) $userUserNotificationSetting );
125
 
126
            $formLocation = new LocationForm();
127
 
128
            if($currentUser->location_id) {
129
 
130
                $locationMapper = LocationMapper::getInstance($this->adapter);
131
                $location = $locationMapper->fetchOne($currentUser->location_id);
132
                if($location) {
133
                    $location_formatted_address = $location->formatted_address;
134
                    $formLocation->setData((array) $location);
135
                }
136
            } else {
137
                $location_formatted_address = '';
138
            }
139
 
140
            $facebook    = 0;
141
            $twitter     = 0;
142
            $google      = 0;
143
 
144
 
145
            $userProviderMapper = UserProviderMapper::getInstance($this->adapter);
146
            $userProviders = $userProviderMapper->fetchAllByUserId($currentUser->id);
147
            foreach($userProviders as $userProvider)
148
            {
149
                switch($userProvider->provider)
150
                {
151
                    case  UserProvider::PROVIDER_FACEBOOK :
152
                        $facebook  = 1;
153
                        break;
154
 
155
                    case  UserProvider::PROVIDER_TWITTER :
156
                        $twitter = 1;
157
                        break;
158
 
159
                    case  UserProvider::PROVIDER_GOOGLE :
160
                        $google  = 1;
161
                        break;
162
 
163
                }
164
            }
165
 
166
            $hydrator = new ObjectPropertyHydrator();
167
            $user_data = $hydrator->extract($currentUser);
4113 efrain 168
 
1 www 169
 
170
            $formBasic = new BasicForm();
171
            $formBasic->setData($user_data);
172
 
173
            $formChangePassword = new ChangePasswordForm();
174
            $formChangeImage = new ChangeImageForm($this->config);
175
            $formPrivacy = new PrivacySettingForm();
176
            $formPrivacy->setData([
177
                'show_in_search' => $currentUser->show_in_search,
178
            ]);
179
 
180
            $formAddFund = new FundsAddForm();
181
 
182
            $this->layout()->setTemplate('layout/layout.phtml');
183
            $viewModel = new ViewModel();
184
            $viewModel->setTemplate('leaders-linked/account-settings/index.phtml');
185
            $viewModel->setVariables([
186
                'tab' => $tab,
187
                'balance' => number_format(floatval($currentUser->balance), 2),
188
                'amounts' => [
189
                    '5' => '5 LABEL_USD',
190
                    '10' => '10 LABEL_USD',
191
                    '15' => '15 LABEL_USD',
192
                    '20' => '20 LABEL_USD',
193
                    '25' => '25 LABEL_USD',
194
                    '50' => '50 LABEL_USD',
195
                    '75' => '75 LABEL_USD',
196
                    '100' => '100 LABEL_USD',
197
                ],
198
                'usertype_id' => $currentUser->usertype_id,
199
                'image' => $this->url()->fromRoute('storage',['type' => 'user', 'code' => $currentUser->uuid, 'filename' => $currentUser->image]),
200
                'formNotificationSetting' => $formNotificationSetting,
201
                'formBasic' => $formBasic,
202
                'formChangePassword' => $formChangePassword,
203
                'formChangeImage' => $formChangeImage,
204
                'formLocation' => $formLocation,
205
                'formPrivacy' => $formPrivacy,
206
                'formAddFund' => $formAddFund,
207
                'config' => $this->config,
208
                'google_map_key' => $google_map_key,
209
                'location_formatted_address' => $location_formatted_address,
210
                'google' => $google,
211
                'facebook' => $facebook,
212
                'twitter' => $twitter,
4402 efrain 213
                'defaultNetwork' => $currentNetwork->default,
1 www 214
 
215
            ]);
216
            return $viewModel ;
217
 
218
        } else {
219
            return new JsonModel([
220
                'success' => false,
221
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
222
            ]);
223
        }
224
    }
225
 
226
    public function notificationAction()
227
    {
228
        $request = $this->getRequest();
229
 
230
        if($request->isGet()) {
231
            $hydrator = new ObjectPropertyHydrator();
232
 
233
            $currentUserPlugin = $this->plugin('currentUserPlugin');
234
            $currentUser = $currentUserPlugin->getUser();
235
 
236
            $userUserNotificationSettingMapper = UserNotificationSettingMapper::getInstance($this->adapter);
237
            $userUserNotificationSetting = $userUserNotificationSettingMapper->fetchOne($currentUser->id);
238
 
239
 
240
            return new JsonModel([
241
               'success' => true,
242
               'data' => [
243
                   'receive_connection_request' => $userUserNotificationSetting->receive_connection_request ? 1 : 0,
244
                   'accept_my_request_connection' => $userUserNotificationSetting->accept_my_request_connection ? 1 : 0,
245
 
246
                   'receive_invitation_group' => $userUserNotificationSetting->receive_invitation_group ? 1 : 0,
247
                   'accept_my_request_join_group' => $userUserNotificationSetting->accept_my_request_join_group ? 1 : 0,
248
                   'receive_request_join_my_group' => $userUserNotificationSetting->receive_request_join_my_group ? 1 : 0,
249
 
250
 
251
                   'receive_invitation_company' => $userUserNotificationSetting->receive_invitation_company ? 1 : 0,
252
 
253
                   'like_my_feed' => $userUserNotificationSetting->like_my_feed ? 1 : 0,
254
                   'comment_my_feed' => $userUserNotificationSetting->comment_my_feed ? 1 : 0,
255
                   'share_my_feed' => $userUserNotificationSetting->share_my_feed ? 1 : 0,
256
                   'receive_inmail' => $userUserNotificationSetting->receive_inmail ? 1 : 0,
257
 
258
                   'receive_invitation_meeting' => $userUserNotificationSetting->receive_invitation_meeting ? 1 : 0,
259
                   'receive_reminder_meeting' => $userUserNotificationSetting->receive_reminder_meeting ? 1 : 0,
260
                   'receive_records_available_meeting' => $userUserNotificationSetting->receive_records_available_meeting ? 1 : 0,
261
 
262
               ]
263
            ]);
264
 
265
 
266
        } else  if($request->isPost()) {
267
 
268
            $dataPost = $request->getPost()->toArray();
269
            $form = new NotificationSettingForm();
270
            $form->setData($dataPost);
271
 
272
            if($form->isValid()) {
273
                $currentUserPlugin = $this->plugin('currentUserPlugin');
274
                $currentUser = $currentUserPlugin->getUser();
275
 
276
                $dataPost = (array) $form->getData();
277
                $hydrator = new ObjectPropertyHydrator();
278
 
279
                $userUserNotificationSettingMapper = UserNotificationSettingMapper::getInstance($this->adapter);
280
                $userUserNotificationSetting = $userUserNotificationSettingMapper->fetchOne($currentUser->id);
281
                $hydrator->hydrate($dataPost, $userUserNotificationSetting);
282
 
283
                if($userUserNotificationSettingMapper->update($userUserNotificationSetting)) {
284
                    $this->logger->info('Se guardo las preferencias de notificación', ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
285
                    $data = [
286
                        'success'   => true,
287
                        'data'      => 'LABEL_NOTIFICATION_SETTINGS_UPDATE'
288
                    ];
289
                } else {
290
                    $data = [
291
                        'success'   => false,
292
                        'data'   => 'ERROR_UNKNOWN'
293
                    ];
294
                }
295
 
296
                return new JsonModel($data);
297
 
298
            } else {
299
                $messages = [];
300
 
301
 
302
 
303
                $form_messages = (array) $form->getMessages();
304
                foreach($form_messages  as $fieldname => $field_messages)
305
                {
306
 
307
                    $messages[$fieldname] = array_values($field_messages);
308
                }
309
 
310
                return new JsonModel([
311
                    'success'   => false,
312
                    'data'   => $messages
313
                ]);
314
            }
315
        }  else {
316
            $data = [
317
                'success' => false,
318
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
319
            ];
320
 
321
            return new JsonModel($data);
322
        }
323
 
324
        return new JsonModel($data);
325
 
326
    }
327
 
328
 
329
 
330
 
331
 
332
    public function passwordAction()
333
    {
334
        $request = $this->getRequest();
335
        if($request->isPost()) {
336
            $dataPost = $request->getPost()->toArray();
337
            $form = new ChangePasswordForm();
338
            $form->setData($dataPost);
339
 
340
            if($form->isValid()) {
341
                $data = (array) $form->getData();
342
                $password = $data['password'];
343
 
344
                $currentUserPlugin = $this->plugin('currentUserPlugin');
345
                $currentUser = $currentUserPlugin->getUser();
346
 
347
 
348
                $userPasswordMapper = UserPasswordMapper::getInstance($this->adapter);
349
                $userPasswords = $userPasswordMapper->fetchAllByUserId($currentUser->id);
350
 
351
                $oldPassword = false;
352
                foreach($userPasswords as $userPassword)
353
                {
354
                    if(password_verify($password, $userPassword->password) || (md5($password) == $userPassword->password))
355
                    {
356
                        $oldPassword = true;
357
                        break;
358
                    }
359
                }
360
 
361
                if($oldPassword) {
362
                    $this->logger->err('Cambio de contraseña del usuario - error contraseña ya utilizada anteriormente', ['user_id' =>  $currentUser->id, 'ip' => Functions::getUserIP()]);
363
 
364
                    return new JsonModel([
365
                        'success'   => false,
366
                        'data'      => 'ERROR_PASSWORD_HAS_ALREADY_BEEN_USED'
367
 
368
                    ]);
369
                } else {
370
                    $password_hash = password_hash($password, PASSWORD_DEFAULT);
371
 
372
                    $userMapper = UserMapper::getInstance($this->adapter);
373
                    $result = $userMapper->updatePassword($currentUser, $password_hash);
374
                    if($result) {
375
 
376
                        $userPassword = new UserPassword();
377
                        $userPassword->user_id = $currentUser->id;
378
                        $userPassword->password = $password_hash;
379
                        $userPasswordMapper->insert($userPassword);
380
 
381
                        $this->logger->info('Cambio de contraseña del usuario realizado', ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
382
 
383
 
384
                        return new JsonModel([
385
                            'success'   => true,
386
                            'data'      => 'LABEL_YOUR_PASSWORD_HAS_BEEN_UPDATED'
387
 
388
                        ]);
389
                    } else {
390
                        $this->logger->err('Cambio de contraseña del usuario - error desconocido', ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
391
 
392
                        return new JsonModel([
393
                            'success'   => true,
394
                            'data'      => 'ERROR_THERE_WAS_AN_ERROR'
395
 
396
                        ]);
397
                    }
398
                }
399
 
400
            } else {
401
                $messages = [];
402
 
403
                $form_messages = (array) $form->getMessages();
404
                foreach($form_messages  as $fieldname => $field_messages)
405
                {
406
                    $messages[$fieldname] = array_values($field_messages);
407
                }
408
 
409
                return new JsonModel([
410
                    'success'   => false,
411
                    'data'   => $messages
412
                ]);
413
            }
414
 
415
        }
416
 
417
 
418
 
419
        return new JsonModel([
420
            'success' => false,
421
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
422
        ]);
423
    }
424
 
425
    public function imageAction()
426
    {
427
        $currentUserPlugin = $this->plugin('currentUserPlugin');
428
        $currentUser = $currentUserPlugin->getUser();
429
        $operation = $this->params()->fromRoute('operation');
430
 
431
 
432
 
433
 
434
        $request = $this->getRequest();
435
        if($request->isGet()) {
436
 
437
            $currentUserPlugin = $this->plugin('currentUserPlugin');
438
            $currentUser = $currentUserPlugin->getUser();
439
 
440
            $userMapper = UserMapper::getInstance($this->adapter);
441
 
442
            $target_path = $this->config['leaderslinked.fullpath.user'] . DIRECTORY_SEPARATOR . $currentUser->uuid;
443
 
444
            return new JsonModel([
445
                'success' => true,
446
                'data' => $this->url()->fromRoute('storage', ['code' => $currentUser->uuid, 'type' => 'user', 'filename' => $currentUser->image])
447
            ]);
448
 
449
 
450
        } else  if($request->isPost()) {
451
            $target_path = $this->config['leaderslinked.fullpath.user'] . DIRECTORY_SEPARATOR . $currentUser->uuid;
452
 
453
            $userMapper = UserMapper::getInstance($this->adapter);
454
 
455
            if($operation == 'delete') {
456
                $this->logger->info('Se borro el image  del usuario ', ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
457
 
458
                if($currentUser->image) {
459
                    if(!image ::delete($target_path, $currentUser->image)) {
460
                        return new JsonModel([
461
                            'success'   => false,
462
                            'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
463
                        ]);
464
                    }
465
                }
466
 
467
                $currentUser->image = '';
468
                if(!$userMapper->update($currentUser)) {
469
                    return new JsonModel([
470
                        'success'   => false,
471
                        'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
472
                    ]);
473
                }
474
 
475
 
476
 
477
            } else {
478
                $form = new ChangeImageForm($this->config);
479
                $data 	= array_merge($request->getPost()->toArray(), $request->getFiles()->toArray());
480
 
481
                $form->setData($data);
482
 
483
                if($form->isValid()) {
484
 
485
                    $files = $request->getFiles()->toArray();
486
                    if(!empty($files['image']['error'])) {
487
 
488
                        return new JsonModel([
489
                            'success'   => false,
490
                            'data'   =>  'ERROR_UPLOAD_FILE'
491
                        ]);
492
 
493
 
494
                    }
495
 
496
                    if($currentUser->image) {
497
                        if(!Image::delete($target_path, $currentUser->image)) {
498
                            return new JsonModel([
499
                                'success'   => false,
500
                                'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
501
                            ]);
502
                        }
503
                    }
504
 
505
                    $target_filename    = 'user-' . uniqid() . '.png';
506
                    list( $target_width, $target_height ) = explode('x', $this->config['leaderslinked.image_sizes.user_size']);
507
                    $source             = $files['image']['tmp_name'];
508
                    $crop_to_dimensions = true;
509
                    if(!Image::uploadImage($source, $target_path, $target_filename, $target_width, $target_height, $crop_to_dimensions)) {
510
                        return new JsonModel([
511
                            'success'   => false,
512
                            'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
513
                        ]);
514
                    }
515
 
516
 
517
                    $currentUser->image = $target_filename;
518
                    if(!$userMapper->updateImage($currentUser)) {
519
 
520
                        return new JsonModel([
521
                            'success'   => false,
522
                            'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
523
                        ]);
524
                    } else {
3163 efrain 525
 
526
 
527
 
1 www 528
                        $userProfileMapper = UserProfileMapper::getInstance($this->adapter);
529
                        $userProfile = $userProfileMapper->fetchOnePublicByUserId($currentUser->id);
3163 efrain 530
 
531
                        if($userProfile) {
1 www 532
                            $userProfile->image = $currentUser->image;
533
                            $userProfileMapper->updateImage($userProfile);
534
                        }
3163 efrain 535
 
1 www 536
                    }
537
 
538
 
539
 
540
                    $this->logger->info('Se actualizo el image del usuario', ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
541
 
542
                } else {
543
                    $messages = [];
544
                    $form_messages = (array) $form->getMessages();
545
                    foreach($form_messages  as $fieldname => $field_messages)
546
                    {
547
                        $messages[$fieldname] = array_values($field_messages);
548
                    }
549
 
550
                    return new JsonModel([
551
                        'success'   => false,
552
                        'data'   => $messages
553
                    ]);
554
                }
555
            }
556
            return new JsonModel([
557
                'success'   => true,
558
                'data' => $this->url()->fromRoute('storage', ['type' => 'user', 'code' => $currentUser->uuid, 'filename' => $currentUser->image])
559
 
560
            ]);
561
        }
562
 
563
 
564
        $data = [
565
            'success' => false,
566
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
567
        ];
568
 
569
 
570
        return new JsonModel($data);
571
    }
572
 
4113 efrain 573
 
574
 
1 www 575
    /**
576
     * Actualización de la ubucación
577
     * @return \Laminas\View\Model\JsonModel
578
     */
579
    public function locationAction()
580
    {
581
        $currentUserPlugin = $this->plugin('currentUserPlugin');
582
        $currentUser = $currentUserPlugin->getUser();
583
 
584
        $request = $this->getRequest();
585
        if($request->isGet()) {
586
            $hydrator = new ObjectPropertyHydrator();
587
 
588
            $currentUserPlugin = $this->plugin('currentUserPlugin');
589
            $currentUser = $currentUserPlugin->getUser();
590
 
591
            $locationMapper = LocationMapper::getInstance($this->adapter);
592
            $location = $locationMapper->fetchOne($currentUser->location_id);
593
 
594
 
595
            $data = [
596
                'formatted_address' => $location ? $location->formatted_address : '',
597
                'address1' => $location ? $location->address1 : '',
598
                'address2' => $location ? $location->address2 : '',
599
                'country' => $location ? $location->country : '',
600
                'state' => $location ? $location->state : '',
601
                'city1' => $location ? $location->city1 : '',
602
                'city2' => $location ? $location->city2 : '',
603
                'postal_code' => $location ? $location->postal_code : '',
604
                'latitude' => $location ? $location->latitude : '',
605
                'longitude' => $location ? $location->longitude : '',
606
            ];
607
 
608
            return new JsonModel([
609
                'success' => true,
610
                'data' => $data
611
            ]);
612
 
613
 
614
        } else  if($request->isPost()) {
615
 
616
            $form = new LocationForm();
617
            $dataPost = $request->getPost()->toArray();
618
 
619
            $form->setData($dataPost);
620
 
621
            if($form->isValid()) {
622
 
623
 
624
                $dataPost = (array) $form->getData();
625
 
626
                $location = new Location();
627
                $hydrator = new ObjectPropertyHydrator();
628
                $hydrator->hydrate($dataPost, $location);
629
 
630
                $location->id = $currentUser->location_id;
631
 
632
                $locationMapper = LocationMapper::getInstance($this->adapter);
633
                if($currentUser->location_id) {
634
                    $result = $locationMapper->update($location);
635
                } else {
636
                    $result = $locationMapper->insert($location);
637
 
638
                    if($result) {
639
                        $currentUser->location_id = $location->id;
640
 
641
 
642
                        $userMapper = UserMapper::getInstance($this->adapter);
643
                        $userMapper->updateLocation($currentUser);
644
                    }
645
                }
646
 
647
                if($result) {
648
                    $userProfileMapper = UserProfileMapper::getInstance($this->adapter);
649
                    $userProfile = $userProfileMapper->fetchOnePublicByUserId($currentUser->id);
650
                    if($userProfile) {
651
                        $userProfile->location_id = $location->id;
652
                        $userProfileMapper->updateLocation($userProfile);
653
                    }
654
                }
655
 
656
                if($result) {
657
                    $this->logger->info('Se actualizo la ubicación del usuario ', ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
658
 
659
                    $response = [
660
                        'success'   => true,
661
                        'data' => [
662
                            'formatted_address' => $location->formatted_address,
663
                            'message' =>  'LABEL_LOCATION_UPDATED' ,
664
 
665
                        ]
666
                    ];
667
                } else {
668
                    $response = [
669
                        'success'   => false,
670
                        'data' => 'ERROR_THERE_WAS_AN_ERROR'
671
                    ];
672
                }
673
 
674
 
675
 
676
                return new JsonModel($response);
677
 
678
            } else {
679
                return new JsonModel([
680
                    'success'   => false,
681
                    'data'   =>   'ERROR_PLACED_AUTOCOMPLETE_DOES_NOT_CONTAIN_GEOMETRY'
682
                ]);
683
            }
684
        }
685
 
686
 
687
        $data = [
688
            'success' => false,
689
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
690
        ];
691
 
692
 
693
        return new JsonModel($data);
694
    }
695
 
696
    public function privacyAction()
697
    {
698
        $request = $this->getRequest();
699
 
700
        if($request->isGet()) {
701
 
702
            $currentUserPlugin = $this->plugin('currentUserPlugin');
703
            $currentUser = $currentUserPlugin->getUser();
704
 
705
            $userMapper = UserMapper::getInstance($this->adapter);
706
            $user = $userMapper->fetchOne($currentUser->id);
707
 
708
            return new JsonModel([
709
                'success' => true,
710
                'data' => [
711
                    'show_in_search' => $user->show_in_search ? 1  : 0
712
                ]
713
            ]);
714
 
715
 
716
        } else if($request->isPost()) {
717
 
718
            $dataPost = $request->getPost()->toArray();
719
            $form = new PrivacySettingForm();
720
            $form->setData($dataPost);
721
 
722
            if($form->isValid()) {
723
                $currentUserPlugin = $this->plugin('currentUserPlugin');
724
                $currentUser = $currentUserPlugin->getUser();
725
 
726
                $dataPost = (array) $form->getData();
727
                $hydrator = new ObjectPropertyHydrator();
728
 
729
 
730
                $userMapper = UserMapper::getInstance($this->adapter);
731
                $hydrator->hydrate($dataPost, $currentUser);
732
 
733
                if($userMapper->updatePrivacy($currentUser)) {
734
                    $this->logger->info('Se guardo las preferencias de privacidad', ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
735
                    $data = [
736
                        'success'   => true,
737
                        'data'      => 'LABEL_PRIVACY_UPDATE'
738
                    ];
739
                } else {
740
                    $data = [
741
                        'success'   => false,
742
                        'data'   => 'ERROR_UNKNOWN'
743
                    ];
744
                }
745
 
746
                return new JsonModel($data);
747
 
748
            } else {
749
                $messages = [];
750
 
751
 
752
 
753
                $form_messages = (array) $form->getMessages();
754
                foreach($form_messages  as $fieldname => $field_messages)
755
                {
756
 
757
                    $messages[$fieldname] = array_values($field_messages);
758
                }
759
 
760
                return new JsonModel([
761
                    'success'   => false,
762
                    'data'   => $messages
763
                ]);
764
            }
765
        }  else {
766
            $data = [
767
                'success' => false,
768
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
769
            ];
770
 
771
            return new JsonModel($data);
772
        }
773
 
774
        return new JsonModel($data);
775
 
776
    }
777
 
778
    public function basicAction()
779
    {
780
        $request = $this->getRequest();
781
 
782
        if($request->isGet()) {
783
            $currentUserPlugin = $this->plugin('currentUserPlugin');
784
            $currentUser = $currentUserPlugin->getUser();
785
 
786
            $userMapper = UserMapper::getInstance($this->adapter);
787
            $user = $userMapper->fetchOne($currentUser->id);
788
 
789
            return new JsonModel([
790
                'success' => true,
791
                'data' => [
792
                    'first_name' => $user->first_name,
793
                    'last_name' => $user->last_name,
794
                    'gender' => $user->gender ? $user->gender : '',
795
                    'phone' => $user->phone ? $user->phone : '',
796
                    'email' => $user->email,
4401 efrain 797
                    'is_adult' => $user->is_adult,
4113 efrain 798
                    'timezone' => $user->timezone,
1 www 799
                ]
800
            ]);
801
 
802
 
803
        } else if($request->isPost()) {
804
 
805
            $dataPost = $request->getPost()->toArray();
4415 efrain 806
 
4398 efrain 807
 
808
            if(empty($dataPost['is_adult'])) {
809
                $dataPost['is_adult'] = User::IS_ADULT_NO;
810
            } else {
811
                $dataPost['is_adult'] = $dataPost['is_adult'] == User::IS_ADULT_YES ? User::IS_ADULT_YES : User::IS_ADULT_NO;
812
            }
4415 efrain 813
 
4398 efrain 814
 
815
 
1 www 816
            $form = new  BasicForm();
817
            $form->setData($dataPost);
818
 
819
            if($form->isValid()) {
820
                $currentUserPlugin = $this->plugin('currentUserPlugin');
821
                $currentUser = $currentUserPlugin->getUser();
822
 
823
                $dataPost = (array) $form->getData();
824
                $hydrator = new ObjectPropertyHydrator();
825
 
826
 
827
                $userMapper = UserMapper::getInstance($this->adapter);
4409 efrain 828
                $user = $userMapper->fetchOne($currentUser->id);
1 www 829
 
4409 efrain 830
                $hydrator->hydrate($dataPost, $user);
831
 
4415 efrain 832
 
4409 efrain 833
 
834
                if($userMapper->updateBasic($user)) {
1 www 835
                    $this->logger->info('Se guardaron los datos básicos ', ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
836
                    $data = [
837
                        'success'   => true,
838
                        'data'      => 'LABEL_BASIC_UPDATE'
839
                    ];
840
                } else {
841
                    $data = [
842
                        'success'   => false,
843
                        'data'   => 'ERROR_UNKNOWN'
844
                    ];
845
                }
846
 
847
                return new JsonModel($data);
848
 
849
            } else {
850
                $messages = [];
851
 
852
 
853
 
854
                $form_messages = (array) $form->getMessages();
855
                foreach($form_messages  as $fieldname => $field_messages)
856
                {
857
 
858
                    $messages[$fieldname] = array_values($field_messages);
859
                }
860
 
861
                return new JsonModel([
862
                    'success'   => false,
863
                    'data'   => $messages
864
                ]);
865
            }
866
        }  else {
867
            $data = [
868
                'success' => false,
869
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
870
            ];
871
 
872
            return new JsonModel($data);
873
        }
874
 
875
        return new JsonModel($data);
876
 
877
    }
878
 
879
    public function browsersAction()
880
    {
881
        $request = $this->getRequest();
882
        if($request->isGet()) {
883
 
884
            $currentUserPlugin = $this->plugin('currentUserPlugin');
885
            $currentUser = $currentUserPlugin->getUser();
886
 
887
            $search = '';
888
            $page               = intval($this->params()->fromQuery('start', 1), 10);
889
            $records_x_page     = intval($this->params()->fromQuery('length', 10), 10);
890
            $order_field        = 'updated_on';
891
            $order_direction = 'DESC';
892
 
893
 
894
 
895
            $userBrowserMapper = UserBrowserMapper::getInstance($this->adapter);
896
            $paginator = $userBrowserMapper->fetchAllDataTable($currentUser->id, $search, $page, $records_x_page, $order_field, $order_direction);
897
 
898
            $items = [];
899
            $records = $paginator->getCurrentItems();
900
            foreach($records as $record)
901
            {
902
                $item = [
903
                    'id' => $record->id,
904
                    'platform' => $record->platform,
905
                    'browser' => $record->browser,
906
                    'device_type' => $record->device_type,
907
                    'version' => $record->version,
908
                    'updated_on' => $record->updated_on,
909
                ];
910
 
911
                array_push($items, $item);
912
            }
913
 
914
            return new JsonModel([
915
                'success' => true,
916
                'data' => [
917
                    'items' => $items,
918
                    'total' => $paginator->getTotalItemCount(),
919
                ]
920
            ]);
921
 
922
        } else {
923
            return new JsonModel(['success' => false, 'data' => 'ERROR_METHOD_NOT_ALLOWED' ]);
924
        }
925
    }
926
    public function devicesAction()
927
    {
928
        $request = $this->getRequest();
929
        if($request->isGet()) {
930
 
931
            $currentUserPlugin = $this->plugin('currentUserPlugin');
932
            $currentUser = $currentUserPlugin->getUser();
933
 
934
            $page               = intval($this->params()->fromPost('start', 1), 10);
935
            $records_x_page     = intval($this->params()->fromPost('length', 10), 10);
936
 
937
 
938
            /*
939
             select d.platform, d.brand, d.manufacturer, d.model, d.version,
940
             dh.ip, dh.updated_on  from tbl_device_history as dh
941
             inner join tbl_devices as d on d.id  = dh.device_id
942
             where dh.user_id = 4 order by dh.updated_on  desc
943
             */
944
 
945
            $queryMapper = QueryMapper::getInstance($this->adapter);
946
            $select = $queryMapper->getSql()->select();
947
            $select->columns(['ip', 'updated_on']);
948
            $select->from(['dh' => DeviceHistoryMapper::_TABLE]);
949
            $select->join(['d' => DeviceMapper::_TABLE], 'd.id  = dh.device_id', ['id', 'platform','brand','manufacturer','model','version']);
950
            $select->where->equalTo('dh.user_id', $currentUser->id);
951
            $select->order('updated_on desc ');
952
 
953
 
954
 
955
            $hydrator   = new ArraySerializableHydrator();
956
            $resultset  = new HydratingResultSet($hydrator);
957
 
958
            $adapter = new DbSelect($select, $queryMapper->getSql(), $resultset);
959
            $paginator = new Paginator($adapter);
960
            $paginator->setItemCountPerPage($records_x_page);
961
            $paginator->setCurrentPageNumber($page);
962
 
963
            $items = [];
964
            $records = $paginator->getCurrentItems();
965
            foreach($records as $record)
966
            {
967
                $item = [
968
                    'id' => $record['id'],
969
                    'platform' => $record['platform'],
970
                    'brand' => $record['brand'],
971
                    'manufacturer' => $record['manufacturer'],
972
                    'version' => $record['version'],
973
                    'model' => $record['model'],
974
                    'version' => $record['version'],
975
                    'ip' => $record['ip'],
976
                    'updated_on' => $record['updated_on'],
977
                ];
978
 
979
                array_push($items, $item);
980
            }
981
 
982
            return new JsonModel([
983
                'success' => true,
984
                'data' => [
985
                    'items' => $items,
986
                    'total' => $paginator->getTotalItemCount(),
987
                ]
988
            ]);
989
 
990
        } else {
991
            return new JsonModel(['success' => false, 'data' => 'ERROR_METHOD_NOT_ALLOWED' ]);
992
        }
993
    }
994
 
995
 
996
    public function ipsAction()
997
    {
998
        $request = $this->getRequest();
999
        if($request->isGet()) {
1000
 
1001
            $currentUserPlugin = $this->plugin('currentUserPlugin');
1002
            $currentUser = $currentUserPlugin->getUser();
1003
 
1004
            $search = '';
1005
            $page               = intval($this->params()->fromPost('start', 1), 10);
1006
            $records_x_page     = intval($this->params()->fromPost('length', 10), 10);
1007
            $order_field        = 'updated_on';
1008
            $order_direction = 'DESC';
1009
 
1010
 
1011
 
1012
            $userBrowserMapper = UserIpMapper::getInstance($this->adapter);
1013
            $paginator = $userBrowserMapper->fetchAllDataTable($currentUser->id, $search, $page, $records_x_page, $order_field, $order_direction);
1014
 
1015
            $items = [];
1016
            $records = $paginator->getCurrentItems();
1017
            foreach($records as $record)
1018
            {
1019
                $item = [
1020
                    'id' => $record->id,
1021
                    'ip' => $record->ip,
1022
                    'country_name' => $record->country_name,
1023
                    'state_name' => $record->state_name,
1024
                    'city' => $record->city,
1025
                    'postal_code' => $record->postal_code,
1026
                    'updated_on' => $record->updated_on,
1027
                ];
1028
 
1029
                array_push($items, $item);
1030
            }
1031
 
1032
            return new JsonModel([
1033
                'success' => true,
1034
                'data' => [
1035
                    'items' => $items,
1036
                    'total' => $paginator->getTotalItemCount(),
1037
                ]
1038
            ]);
1039
 
1040
        } else {
1041
            return new JsonModel(['success' => false, 'data' => 'ERROR_METHOD_NOT_ALLOWED' ]);
1042
        }
1043
    }
1044
 
1045
    public function transactionsAction()
1046
    {
1047
        $request = $this->getRequest();
1048
        if($request->isGet()) {
1049
 
1050
            $currentUserPlugin = $this->plugin('currentUserPlugin');
1051
            $currentUser = $currentUserPlugin->getUser();
1052
 
1053
            $search = '';
1054
            $page               = intval($this->params()->fromPost('start', 1), 10);
1055
            $records_x_page     = intval($this->params()->fromPost('length', 10), 10);
1056
            $order_field        = 'updated_on';
1057
            $order_direction = 'DESC';
1058
 
1059
            $status = [
1060
                Transaction::STATUS_CANCELLED => 'LABEL_CANCELLED',
1061
                Transaction::STATUS_PENDING => 'LABEL_PENDING',
1062
                Transaction::STATUS_PROCESSING => 'LABEL_PROCESSING',
1063
                Transaction::STATUS_REJECTED => 'LABEL_REJECTED',
1064
                Transaction::STATUS_COMPLETED => 'LABEL_COMPLETED',
1065
                Transaction::STATUS_CANCELLED => 'LABEL_CANCELLED',
1066
            ];
1067
 
1068
            $types = [
1069
                Transaction::TYPE_COUPON => 'LABEL_COUPON',
1070
                Transaction::TYPE_PAYMENT => 'LABEL_PAYMENT',
1071
                Transaction::TYPE_REVERSE => 'LABEL_REVERSE',
1072
                Transaction::TYPE_TRANSFER => 'LABEL_TRANSFER',
1073
            ];
1074
 
1075
            $providers = [
1076
                Provider::PAYPAL => 'LABEL_PAYPAL',
1077
            ];
1078
 
1079
            $transactionMapper = TransactionMapper::getInstance($this->adapter);
1080
            $paginator = $transactionMapper->fetchAllDataTable($currentUser->id, $search, $page, $records_x_page, $order_field, $order_direction);
1081
 
1082
            $items = [];
1083
            $records = $paginator->getCurrentItems();
1084
            foreach($records as $record)
1085
            {
1086
                $item = [
1087
                    'id' => $record->id,
1088
                    'description' => $record->description,
1089
                    'provider' => $providers[$record->provider],
1090
                    'type' => $types[$record->type],
1091
                    'status' => $status[$record->status],
1092
                    'previous' => $record->previous,
1093
                    'amount' => $record->amount,
1094
                    'current' => $record->current,
1095
                    'updated_on' => $record->updated_on,
1096
                ];
1097
 
1098
                array_push($items, $item);
1099
            }
1100
 
1101
            return new JsonModel([
1102
                'success' => true,
1103
                'data' => [
1104
                    'items' => $items,
1105
                    'total' => $paginator->getTotalItemCount(),
1106
                ]
1107
            ]);
1108
 
1109
        } else {
1110
            return new JsonModel(['success' => false, 'data' => 'ERROR_METHOD_NOT_ALLOWED' ]);
1111
        }
1112
    }
1113
 
1114
 
1115
 
1116
    public function addFundAction()
1117
    {
6749 efrain 1118
        /*
1 www 1119
        $request = $this->request;
1120
        if($request->isPost()) {
1121
 
1122
            $form = new FundsAddForm();
1123
            $form->setData($request->getPost()->toArray());
1124
            if($form->isValid()) {
1125
 
1126
                $currentUserPlugin = $this->plugin('currentUserPlugin');
1127
                $currentUser = $currentUserPlugin->getUser();
1128
 
1129
 
1130
 
1131
 
1132
                $dataPost = (array) $form->getData();
1133
 
1134
                $description    = $dataPost['description'];
1135
                $amount         = $dataPost['amount'];
1136
 
1137
 
1138
 
1139
                $sandbox = $this->config['leaderslinked.runmode.sandbox_paypal'];
1140
                if($sandbox) {
1141
                    //$account_id     = $this->config['leaderslinked.paypal.sandbox_account_id'];
1142
                    $client_id      = $this->config['leaderslinked.paypal.sandobx_client_id'];
1143
                    $client_secret  = $this->config['leaderslinked.paypal.sandbox_client_secret'];
1144
 
1145
 
1146
                    $environment = new SandboxEnvironment($client_id, $client_secret);
1147
 
1148
                } else {
1149
                    // $account_id     = $this->config['leaderslinked.paypal.production_account_id'];
1150
                    $client_id      = $this->config['leaderslinked.paypal.production_client_id'];
1151
                    $client_secret  = $this->config['leaderslinked.paypal.production_client_secret'];
1152
 
1153
                    $environment = new ProductionEnvironment($client_id, $client_secret);
1154
                }
1155
 
1156
                $internal_id = uniqid(Provider::PAYPAL, true);
1157
                $client = new PayPalHttpClient($environment);
1158
                $request = new OrdersCreateRequest;
1159
 
1160
 
1161
                //$request->prefer('return=representation');
1162
                $request->body = [
1163
                    'intent' => 'CAPTURE',
1164
                    'purchase_units' => [[
1165
                        'reference_id' => $internal_id,
1166
                        'description' => $description,
1167
                        'amount' => [
1168
                            'value' => number_format($amount, 2),
1169
                            'currency_code' => 'USD'
1170
                        ]
1171
                    ]],
1172
                    'application_context' => [
1173
                        'brand_name' => 'Leaders Linked',
1174
                        'locale' => 'es-UY',
1175
                        'cancel_url' => $this->url()->fromRoute('paypal/cancel', [] , ['force_canonical' => true]),
1176
                        'return_url' => $this->url()->fromRoute('paypal/success', [] , ['force_canonical' => true]),
1177
                    ]
1178
                ];
1179
 
1180
                try {
1181
                    // Call API with your client and get a response for your call
1182
                    $response = $client->execute($request);
1183
 
1184
 
1185
                    $external_id = $response->result->id;
1186
                    $approve_url = '';
1187
                    if($response->result->status == 'CREATED') {
1188
 
1189
                        $response->result->id;
1190
                        foreach($response->result->links as $link)
1191
                        {
1192
                            if($link->rel == 'approve') {
1193
                                $approve_url = $link->href;
1194
                            }
1195
                            //print "\t{$link->rel}: {$link->href}\tCall Type: {$link->method}\n";
1196
                        }
1197
 
1198
 
1199
                    }
1200
 
1201
 
1202
                    //echo json_encode($resp, JSON_PRETTY_PRINT), "\n";
1203
 
1204
 
1205
 
1206
 
1207
 
1208
                    // To toggle printing the whole response body comment/uncomment below line
1209
                    // echo json_encode($resp->result, JSON_PRETTY_PRINT), "\n";
1210
                    if($external_id && $approve_url) {
1211
 
1212
                        $transaction = new Transaction();
1213
                        $transaction->internal_id = $internal_id;
1214
                        $transaction->external_id = $external_id;
1215
                        $transaction->provider = Provider::PAYPAL;
1216
                        $transaction->user_id = $currentUser->id;
1217
                        $transaction->previous = 0;
1218
                        $transaction->amount = $amount;
1219
                        $transaction->current = 0;
1220
                        $transaction->status = Transaction::STATUS_PENDING;
1221
                        $transaction->type = Transaction::TYPE_PAYMENT;
1222
                        $transaction->description = $description;
1223
                        $transaction->request = json_encode($response, JSON_PRETTY_PRINT);
1224
 
1225
                        $requestId = Provider::PAYPAL . '-' . $external_id;
1226
 
6749 efrain 1227
                        $this->cache->add($requestId, serialize($transaction));
1 www 1228
 
1229
 
1230
 
1231
 
1232
                        return new JsonModel(['success' => true, 'data' => $approve_url]);
1233
                    } else {
1234
                        return new JsonModel(['success' => false, 'data' => 'ERROR_TRANSACTION_NOT_SAVED']);
1235
                    }
1236
 
1237
 
1238
 
1239
                } catch (HttpException $ex) {
1240
 
1241
 
1242
                    return new JsonModel(['success' => false, 'data' => $ex->getMessage()]);
1243
                }
1244
 
1245
            } else {
1246
 
1247
                $message = '';;
1248
                $form_messages = (array) $form->getMessages();
1249
                foreach($form_messages  as $fieldname => $field_messages)
1250
                {
1251
                    foreach( $field_messages as $key => $value)
1252
                    {
1253
                        $message = $value;
1254
                    }
1255
                }
1256
 
1257
                $response = [
1258
                    'success'   => false,
1259
                    'data'   => $message
1260
                ];
1261
 
1262
                return new JsonModel($response);
1263
 
1264
            }
1265
 
1266
        } else {
1267
            return new JsonModel(['success' => false, 'data' => 'ERROR_METHOD_NOT_ALLOWED' ]);
6749 efrain 1268
        }*/
1 www 1269
    }
1270
 
1271
    public function removeFacebookAction()
1272
    {
1273
        $request = $this->getRequest();
1274
        if($request->isPost()) {
1275
 
1276
            $currentUserPlugin = $this->plugin('currentUserPlugin');
1277
            $currentUser = $currentUserPlugin->getUser();
1278
 
1279
 
1280
            $userProviderMapper = UserProviderMapper::getInstance($this->adapter);
1281
            $userProvider = $userProviderMapper->fetchOneByUserIdAndProvider($currentUser->id, UserProvider::PROVIDER_FACEBOOK);
1282
 
1283
            if($userProvider) {
1284
 
1285
                if($userProviderMapper->deleteByUserIdAndProvider($currentUser->id, UserProvider::PROVIDER_FACEBOOK)) {
1286
                    return new JsonModel([
1287
                        'success' => true,
1288
                        'data' => 'LABEL_USER_PROVIDER_FACEBOOK_REMOVED'
1289
                    ]);
1290
 
1291
                } else {
1292
                    return new JsonModel([
1293
                        'success' => false,
1294
                        'data' => $userProviderMapper->getError()
1295
                    ]);
1296
                }
1297
 
1298
 
1299
            } else {
1300
                return new JsonModel([
1301
                    'success' => false,
1302
                    'data' => 'ERROR_USER_PROVIDER_FACEBOOK_NOT_FOUND'
1303
                ]);
1304
            }
1305
 
1306
 
1307
        } else {
1308
            return new JsonModel([
1309
                'success' => false,
1310
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
1311
            ]);
1312
        }
1313
    }
1314
 
1315
    public function addFacebookAction()
1316
    {
6749 efrain 1317
        /*
1 www 1318
        $request = $this->getRequest();
1319
        if($request->isGet()) {
1320
 
1321
            try {
1322
                $app_id = $this->config['leaderslinked.facebook.app_id'];
1323
                $app_password = $this->config['leaderslinked.facebook.app_password'];
1324
                $app_graph_version = $this->config['leaderslinked.facebook.app_graph_version'];
1325
                //$app_url_auth = $this->config['leaderslinked.facebook.app_url_auth'];
1326
                //$redirect_url = $this->config['leaderslinked.facebook.app_redirect_url'];
1327
 
1328
 
1329
 
1330
                $fb = new \Facebook\Facebook([
1331
                    'app_id' => $app_id,
1332
                    'app_secret' => $app_password,
1333
                    'default_graph_version' => $app_graph_version,
1334
                ]);
1335
 
1336
                $app_url_auth =  $this->url()->fromRoute('oauth/facebook', [], ['force_canonical' => true]);
1337
                $helper = $fb->getRedirectLoginHelper();
1338
                $permissions = ['email', 'public_profile']; // Optional permissions
1339
                $facebookUrl = $helper->getLoginUrl($app_url_auth, $permissions);
1340
 
1341
                return new JsonModel([
1342
                    'success' => true,
1343
                    'data' => $facebookUrl
1344
                ]);
1345
            } catch (\Throwable $e) {
1346
                return new JsonModel([
1347
                    'success' => false,
1348
                    'data' =>  'ERROR_WE_COULD_NOT_CONNECT_TO_FACEBOOK'
1349
                ]);
1350
            }
1351
 
1352
        } else {
1353
            return new JsonModel([
1354
                'success' => false,
1355
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
1356
            ]);
6749 efrain 1357
        }*/
1 www 1358
    }
1359
 
1360
    public function removeTwitterAction()
1361
    {
1362
        $request = $this->getRequest();
1363
        if($request->isPost()) {
1364
 
1365
            $currentUserPlugin = $this->plugin('currentUserPlugin');
1366
            $currentUser = $currentUserPlugin->getUser();
1367
 
1368
 
1369
            $userProviderMapper = UserProviderMapper::getInstance($this->adapter);
1370
            $userProvider = $userProviderMapper->fetchOneByUserIdAndProvider($currentUser->id, UserProvider::PROVIDER_TWITTER);
1371
 
1372
            if($userProvider) {
1373
 
1374
                if($userProviderMapper->deleteByUserIdAndProvider($currentUser->id, UserProvider::PROVIDER_TWITTER)) {
1375
                    return new JsonModel([
1376
                        'success' => true,
1377
                        'data' => 'LABEL_USER_PROVIDER_TWITTER_REMOVED'
1378
                    ]);
1379
 
1380
                } else {
1381
                    return new JsonModel([
1382
                        'success' => false,
1383
                        'data' => $userProviderMapper->getError()
1384
                    ]);
1385
                }
1386
 
1387
 
1388
            } else {
1389
                return new JsonModel([
1390
                    'success' => false,
1391
                    'data' => 'ERROR_USER_PROVIDER_TWITTER_NOT_FOUND'
1392
                ]);
1393
            }
1394
 
1395
 
1396
        } else {
1397
            return new JsonModel([
1398
                'success' => false,
1399
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
1400
            ]);
1401
        }
1402
    }
1403
 
1404
    public function addTwitterAction()
1405
    {
6749 efrain 1406
 
1 www 1407
        $request = $this->getRequest();
1408
        if($request->isGet()) {
1409
 
1410
            try {
1411
                if($this->config['leaderslinked.runmode.sandbox']) {
1412
 
1413
                    $twitter_api_key = $this->config['leaderslinked.twitter.sandbox_api_key'];
1414
                    $twitter_api_secret = $this->config['leaderslinked.twitter.sandbox_api_secret'];
1415
 
1416
                } else {
1417
                    $twitter_api_key = $this->config['leaderslinked.twitter.production_api_key'];
1418
                    $twitter_api_secret = $this->config['leaderslinked.twitter.production_api_secret'];
1419
                }
1420
 
6749 efrain 1421
 
1 www 1422
 
1423
                //Twitter
1424
                //$redirect_url =  $this->url()->fromRoute('oauth/twitter', [], ['force_canonical' => true]);
1425
                $redirect_url = $this->config['leaderslinked.twitter.app_redirect_url'];
1426
                $twitter = new \Abraham\TwitterOAuth\TwitterOAuth($twitter_api_key, $twitter_api_secret);
1427
                $request_token =  $twitter->oauth('oauth/request_token', ['oauth_callback' => $redirect_url ]);
1428
                $twitterUrl = $twitter->url('oauth/authorize', [ 'oauth_token' => $request_token['oauth_token'] ]);
1429
 
1430
                $twitterSession = new \Laminas\Session\Container('twitter');
1431
                $twitterSession->oauth_token = $request_token['oauth_token'];
1432
                $twitterSession->oauth_token_secret = $request_token['oauth_token_secret'];
1433
 
1434
                return new JsonModel([
1435
                    'success' => true,
1436
                    'data' =>  $twitterUrl
1437
                ]);
1438
            } catch (\Throwable $e) {
1439
                return new JsonModel([
1440
                    'success' => false,
1441
                    'data' =>  'ERROR_WE_COULD_NOT_CONNECT_TO_TWITTER'
1442
                ]);
1443
            }
1444
 
1445
        } else {
1446
            return new JsonModel([
1447
                'success' => false,
1448
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
1449
            ]);
1450
        }
1451
 
1452
 
1453
    }
1454
 
1455
    public function removeGoogleAction()
1456
    {
1457
        $request = $this->getRequest();
1458
        if($request->isPost()) {
1459
 
1460
            $currentUserPlugin = $this->plugin('currentUserPlugin');
1461
            $currentUser = $currentUserPlugin->getUser();
1462
 
1463
 
1464
            $userProviderMapper = UserProviderMapper::getInstance($this->adapter);
1465
            $userProvider = $userProviderMapper->fetchOneByUserIdAndProvider($currentUser->id, UserProvider::PROVIDER_GOOGLE);
1466
 
1467
            if($userProvider) {
1468
 
1469
                if($userProviderMapper->deleteByUserIdAndProvider($currentUser->id, UserProvider::PROVIDER_GOOGLE)) {
1470
                    return new JsonModel([
1471
                        'success' => true,
1472
                        'data' => 'LABEL_USER_PROVIDER_GOOGLE_REMOVED'
1473
                    ]);
1474
 
1475
                } else {
1476
                    return new JsonModel([
1477
                        'success' => false,
1478
                        'data' => $userProviderMapper->getError()
1479
                    ]);
1480
                }
1481
 
1482
 
1483
            } else {
1484
                return new JsonModel([
1485
                    'success' => false,
1486
                    'data' => 'ERROR_USER_PROVIDER_GOOGLE_NOT_FOUND'
1487
                ]);
1488
            }
1489
 
1490
 
1491
        } else {
1492
            return new JsonModel([
1493
                'success' => false,
1494
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
1495
            ]);
1496
        }
1497
    }
1498
 
1499
    public function addGoogleAction()
1500
    {
1501
        $request = $this->getRequest();
1502
        if($request->isGet()) {
1503
 
1504
            try {
1505
 
1506
 
1507
                //Google
1508
                $google = new \Google_Client();
1509
                $google->setAuthConfig('data/google/auth-leaderslinked/apps.google.com_secreto_cliente.json');
1510
                $google->setAccessType("offline");        // offline access
1511
 
1512
                $google->setIncludeGrantedScopes(true);   // incremental auth
1513
 
1514
                $google->addScope('profile');
1515
                $google->addScope('email');
1516
 
1517
                // $redirect_url =  $this->url()->fromRoute('oauth/google', [], ['force_canonical' => true]);
1518
                $redirect_url = $this->config['leaderslinked.google_auth.app_redirect_url'];
1519
 
1520
                $google->setRedirectUri($redirect_url);
1521
                $googleUrl = $google->createAuthUrl();
1522
 
1523
                return new JsonModel([
1524
                    'success' => true,
1525
                    'data' =>  $googleUrl
1526
                ]);
1527
            } catch (\Throwable $e) {
1528
                return new JsonModel([
1529
                    'success' => false,
1530
                    'data' =>  'ERROR_WE_COULD_NOT_CONNECT_TO_GOOGLE'
1531
                ]);
1532
            }
1533
 
1534
        } else {
1535
            return new JsonModel([
1536
                'success' => false,
1537
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
1538
            ]);
1539
        }
1540
    }
1979 efrain 1541
 
1542
    public function deleteAccountAction()
1543
    {
1544
 
1545
 
1546
        $currentUserPlugin = $this->plugin('currentUserPlugin');
1547
        $user = $currentUserPlugin->getUser();
1548
 
1549
 
1550
 
1551
        $request = $this->getRequest();
1552
 
1553
        if($request->isGet()) {
1554
 
1555
            $this->sendEmailDeleteAccountKey($user);
1556
 
1557
 
1558
            return new JsonModel([
1559
                'success' => true,
1560
                'data' => [
1561
                    'message' => 'LABEL_DELETE_ACCOUNT_WE_HAVE_SENT_A_CONFIRMATION_CODE'
1562
                ]
1563
            ]);
1564
 
1565
        } else  if($request->isPost()) {
1566
 
1567
            $code = $this->params()->fromPost('code');
2013 efrain 1568
            if(empty($code) || $code != $user->delete_account_key) {
1979 efrain 1569
 
1570
                $this->sendEmailDeleteAccountKey($user);
1571
 
1572
                return new JsonModel([
1573
                    'success' => false,
1574
                    'data' => [
1575
                        'message' => 'ERROR_DELETE_ACCOUNT_CONFIRMATION_CODE_IS_WRONG'
1576
                    ]
1577
                ]);
1578
            }
1579
 
1580
            $delete_account_generated_on = strtotime($user->delete_account_generated_on);
1581
            $expiry_time = $delete_account_generated_on + $this->config['leaderslinked.security.delete_account_expired'];
1582
 
1583
 
1584
            if (time() > $expiry_time) {
1585
 
1586
                $this->sendEmailDeleteAccountKey($user) ;
1587
 
1588
                return new JsonModel([
1589
                    'success' => false,
1590
                    'data' => [
1591
                        'message' => 'ERROR_DELETE_ACCOUNT_CONFIRMATION_CODE_EXPIRED'
1592
                    ]
1593
                ]);
1594
 
1595
 
1596
            }
1597
 
1598
            $userDeleted  = new UserDeleted();
1599
            $userDeleted->user_id = $user->id;
1600
            $userDeleted->first_name = $user->first_name;
1601
            $userDeleted->last_name = $user->last_name;
1602
            $userDeleted->email = $user->email;
1603
            $userDeleted->image = $user->image;
1604
            $userDeleted->phone = $user->phone;
1605
            $userDeleted->pending = UserDeleted::PENDING_YES;
1606
 
1607
 
1608
            $userDeletedMapper = UserDeletedMapper::getInstance($this->adapter);
1609
            if ($userDeletedMapper->insert($userDeleted)) {
1610
 
2019 efrain 1611
                $this->sendEmailDeleteAccountCompleted($user);
1612
 
1979 efrain 1613
                $user->first_name = 'LABEL_DELETE_ACCOUNT_FIRST_NAME';
1614
                $user->last_name = 'LABEL_DELETE_ACCOUNT_LAST_NAME';
1984 efrain 1615
                $user->email = 'user-deleted-' . uniqid() . '@leaderslinked.com';
1979 efrain 1616
                $user->image = '';
1617
                $user->usertype_id = UserType::USER_DELETED;
1618
                $user->status = User::STATUS_DELETED;
1619
                $user->delete_account_key = '';
1620
                $user->delete_account_generated_on = '';
1621
 
1622
                $userMapper = UserMapper::getInstance($this->adapter);
1623
                if($userMapper->update($user)) {
1624
 
1625
 
2019 efrain 1626
 
1979 efrain 1627
                    return new JsonModel([
1628
                        'success' => true,
1629
                        'data' => [
1630
                            'message' => 'LABEL_DELETE_ACCOUNT_WE_HAVE_STARTED_DELETING_YOUR_DATA',
1631
                            'redirect_url' => $this->url()->fromRoute('signout'),
1632
                        ]
1633
                    ]);
1634
 
1635
 
1636
                } else {
1637
                    return new JsonModel([
1638
                        'success' => false,
1639
                        'data' => [
1640
                            'message' => $userDeletedMapper->getError()
1641
                        ]
1642
                    ]);
1643
                }
1644
 
1645
 
1646
 
1647
            } else {
1648
                return new JsonModel([
1649
                    'success' => false,
1650
                    'data' => [
1651
                        'message' => $userDeletedMapper->getError()
1652
                    ]
1653
                ]);
1654
            }
1655
 
1656
 
1657
 
1658
 
1659
 
1660
        }
1661
 
1662
 
1663
            return new JsonModel([
1664
                'success' => false,
1665
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
1666
            ]);
1667
    }
1668
 
4398 efrain 1669
 
4113 efrain 1670
 
1671
 
1979 efrain 1672
    private function sendEmailDeleteAccountKey($user)
1673
    {
1674
        $delete_account_key = Functions::generatePassword(8);
1675
 
1676
        $userMapper = UserMapper::getInstance($this->adapter);
1677
        $userMapper->updateDeleteAccountKey($user->id, $delete_account_key);
1678
 
1679
        $emailTemplateMapper = EmailTemplateMapper::getInstance($this->adapter);
3712 efrain 1680
        $emailTemplate = $emailTemplateMapper->fetchOneByCodeAndNetworkId(EmailTemplate::CODE_DELETE_ACCOUNT_CODE, $user->network_id);
1979 efrain 1681
        if($emailTemplate) {
1682
            $arrayCont = [
1683
                'firstname' => $user->first_name,
1684
                'lastname'  => $user->last_name,
1685
                'code'      => $delete_account_key,
1686
                'link'      => ''
1687
            ];
1688
 
1689
            $email = new QueueEmail($this->adapter);
1690
            $email->processEmailTemplate($emailTemplate, $arrayCont, $user->email, trim($user->first_name . ' ' . $user->last_name));
1691
        }
1692
    }
1693
 
1694
 
1695
    private function sendEmailDeleteAccountCompleted($user)
1696
    {
1697
 
1698
        $emailTemplateMapper = EmailTemplateMapper::getInstance($this->adapter);
3712 efrain 1699
        $emailTemplate = $emailTemplateMapper->fetchOneByCodeAndNetworkId(EmailTemplate::CODE_DELETE_ACCOUNT_COMPLETED, $user->network_id);
1979 efrain 1700
        if($emailTemplate) {
1701
            $arrayCont = [
1702
                'firstname' => $user->first_name,
1703
                'lastname'  => $user->last_name,
1704
                'code'      => '',
1705
                'link'      => ''
1706
            ];
1707
 
1708
            $email = new QueueEmail($this->adapter);
1709
            $email->processEmailTemplate($emailTemplate, $arrayCont, $user->email, trim($user->first_name . ' ' . $user->last_name));
1710
        }
1711
    }
1 www 1712
 
1713
}