Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 17168 | Rev 17170 | 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
17167 ariadna 2
 
1 www 3
declare(strict_types=1);
4
 
5
namespace LeadersLinked\Controller;
6
 
7
use Laminas\Db\Adapter\AdapterInterface;
8
 
9
 
16768 efrain 10
 
1 www 11
use Laminas\Mvc\Controller\AbstractActionController;
12
use Laminas\Log\LoggerInterface;
13
 
14
use Laminas\View\Model\ViewModel;
15
use Laminas\View\Model\JsonModel;
16
use LeadersLinked\Library\Functions;
17
use LeadersLinked\Mapper\UserMapper;
18
use LeadersLinked\Mapper\UserPasswordMapper;
19
 
20
use LeadersLinked\Model\User;
21
use LeadersLinked\Form\ChangePasswordForm;
22
use LeadersLinked\Model\CompanyUser;
23
use LeadersLinked\Mapper\CompanyUserMapper;
24
use LeadersLinked\Mapper\CompanyUserRoleMapper;
25
use LeadersLinked\Mapper\RoleMapper;
26
use LeadersLinked\Model\UserType;
27
use LeadersLinked\Model\UserPassword;
28
use PhpOffice\PhpSpreadsheet\IOFactory;
17169 ariadna 29
use LeadersLinked\Form\User\UserUploadForm;
1 www 30
use LeadersLinked\Mapper\CompanyServiceMapper;
31
use LeadersLinked\Model\CompanyService;
32
use LeadersLinked\Model\Role;
33
use LeadersLinked\Mapper\CompanyRoleMapper;
34
use LeadersLinked\Model\CompanyUserRole;
35
use LeadersLinked\Model\Notification;
36
use LeadersLinked\Model\EmailTemplate;
37
use LeadersLinked\Mapper\NotificationMapper;
38
use LeadersLinked\Mapper\UserNotificationSettingMapper;
39
use LeadersLinked\Mapper\EmailTemplateMapper;
40
use LeadersLinked\Library\QueueEmail;
15351 efrain 41
use LeadersLinked\Mapper\NetworkMapper;
42
use LeadersLinked\Model\Network;
15460 efrain 43
use LeadersLinked\Mapper\CompanyMapper;
44
use LeadersLinked\Mapper\ConnectionMapper;
45
use LeadersLinked\Model\Connection;
46
use LeadersLinked\Mapper\CompanyFollowerMapper;
47
use LeadersLinked\Model\CompanyFollower;
48
use LeadersLinked\Mapper\CountryMapper;
49
use LeadersLinked\Mapper\LocationMapper;
50
use LeadersLinked\Model\Location;
16248 efrain 51
use LeadersLinked\Form\User\ChangeTypeForm;
16299 efrain 52
use LeadersLinked\Form\User\NetworkDataForm;
16768 efrain 53
use LeadersLinked\Cache\CacheInterface;
54
use LeadersLinked\Cache\CacheImpl;
1 www 55
 
56
class UserController extends AbstractActionController
57
{
58
    /**
59
     *
16769 efrain 60
     * @var \Laminas\Db\Adapter\AdapterInterface
1 www 61
     */
62
    private $adapter;
17167 ariadna 63
 
1 www 64
    /**
65
     *
16769 efrain 66
     * @var \LeadersLinked\Cache\CacheInterface
1 www 67
     */
16769 efrain 68
    private $cache;
17167 ariadna 69
 
70
 
16769 efrain 71
    /**
72
     *
73
     * @var \Laminas\Log\LoggerInterface
74
     */
1 www 75
    private $logger;
17167 ariadna 76
 
1 www 77
    /**
78
     *
79
     * @var array
80
     */
81
    private $config;
17167 ariadna 82
 
83
 
16768 efrain 84
    /**
16769 efrain 85
     *
86
     * @var \Laminas\Mvc\I18n\Translator
16768 efrain 87
     */
16769 efrain 88
    private $translator;
17167 ariadna 89
 
90
 
1 www 91
    /**
92
     *
16769 efrain 93
     * @param \Laminas\Db\Adapter\AdapterInterface $adapter
94
     * @param \LeadersLinked\Cache\CacheInterface $cache
95
     * @param \Laminas\Log\LoggerInterface LoggerInterface $logger
1 www 96
     * @param array $config
16769 efrain 97
     * @param \Laminas\Mvc\I18n\Translator $translator
1 www 98
     */
16769 efrain 99
    public function __construct($adapter, $cache, $logger, $config, $translator)
1 www 100
    {
16769 efrain 101
        $this->adapter      = $adapter;
102
        $this->cache        = $cache;
103
        $this->logger       = $logger;
104
        $this->config       = $config;
105
        $this->translator   = $translator;
1 www 106
    }
17167 ariadna 107
 
1 www 108
    public function indexAction()
109
    {
110
        $currentUserPlugin = $this->plugin('currentUserPlugin');
111
        $currentUser = $currentUserPlugin->getUser();
112
        $currentCompany = $currentUserPlugin->getCompany();
17167 ariadna 113
 
15351 efrain 114
        $currentNetworkPlugin = $this->plugin('currentNetworkPlugin');
16299 efrain 115
        $currentNetwork = $currentNetworkPlugin->getNetwork();
17167 ariadna 116
 
117
 
1 www 118
        $request = $this->getRequest();
17167 ariadna 119
 
120
        if ($request->isGet())
121
 
122
 
1 www 123
            $headers  = $request->getHeaders();
17167 ariadna 124
 
125
        $isJson = false;
126
        if ($headers->has('Accept')) {
127
            $accept = $headers->get('Accept');
128
 
129
            $prioritized = $accept->getPrioritized();
130
 
131
            foreach ($prioritized as $key => $value) {
132
                $raw = trim($value->getRaw());
133
 
134
                if (!$isJson) {
135
                    $isJson = strpos($raw, 'json');
1 www 136
                }
137
            }
17167 ariadna 138
        }
139
 
140
        if ($isJson) { {
16299 efrain 141
                $network_id = $this->params()->fromQuery('network_id');
17167 ariadna 142
                if ($currentUser->is_super_user == User::IS_SUPER_USER_YES) {
143
 
16299 efrain 144
                    $networkMapper = NetworkMapper::getInstance($this->adapter);
145
                    $network = $networkMapper->fetchOneByUuid($network_id);
17167 ariadna 146
                    if (!$network) {
16299 efrain 147
                        $network = $currentNetwork;
148
                    }
149
                } else {
150
                    $network = $currentNetwork;
151
                }
15351 efrain 152
 
17167 ariadna 153
 
154
 
1 www 155
                $search = $this->params()->fromQuery('search', []);
16766 efrain 156
                $search = empty($search['value']) ? '' :  Functions::sanitizeFilterString($search['value']);
17167 ariadna 157
 
3343 efrain 158
                //$page               = intval($this->params()->fromQuery('start', 1), 10);
159
                //$records_x_page     = intval($this->params()->fromQuery('length', 10), 10);
17167 ariadna 160
 
1 www 161
                $records_x_page     = intval($this->params()->fromQuery('length', 10), 10);
17167 ariadna 162
                $page               = (intval($this->params()->fromQuery('start', 1), 10) / $records_x_page) + 1;
163
 
1 www 164
                $order =  $this->params()->fromQuery('order', []);
165
                $order_field        = empty($order[0]['column']) ? 99 :  intval($order[0]['column'], 10);
16766 efrain 166
                $order_direction    = empty($order[0]['dir']) ? 'ASC' : strtoupper(Functions::sanitizeFilterString($order[0]['dir']));
17167 ariadna 167
 
1 www 168
                $fields =  ['first_name', 'last_name', 'email'];
169
                $order_field = isset($fields[$order_field]) ? $fields[$order_field] : 'first_name';
17167 ariadna 170
 
171
                if (!in_array($order_direction, ['ASC', 'DESC'])) {
1 www 172
                    $order_direction = 'ASC';
173
                }
174
 
17167 ariadna 175
 
176
                if ($currentCompany) {
1 www 177
                    $companyUserRoleMapper = CompanyUserRoleMapper::getInstance($this->adapter);
17167 ariadna 178
 
179
                    $roles = [];
1 www 180
                    $roleMapper = RoleMapper::getInstance($this->adapter);
17167 ariadna 181
                    $records = $roleMapper->fetchAll();
182
                    foreach ($records as $record) {
183
                        $roles[$record->id] = $record->name;
1 www 184
                    }
17167 ariadna 185
 
186
 
1 www 187
                    //Usuarios cuando el nivel es empresa
17167 ariadna 188
 
1 www 189
                    $acl = $this->getEvent()->getViewModel()->getVariable('acl');
190
                    $allowUnblock = $acl->isAllowed($currentUser->usertype_id, 'users/unblock');
191
                    $allowChangePassword = $acl->isAllowed($currentUser->usertype_id, 'users/change-password');
17167 ariadna 192
 
1 www 193
                    $allowAccept    = $acl->isAllowed($currentUser->usertype_id, 'users/accept') ? 1 : 0;
194
                    $allowCancel    = $acl->isAllowed($currentUser->usertype_id, 'users/cancel') ? 1 : 0;
195
                    $allowReject    = $acl->isAllowed($currentUser->usertype_id, 'users/reject') ? 1 : 0;
196
                    $allowEdit     = $acl->isAllowed($currentUser->usertype_id, 'users/edit') ? 1 : 0;
16248 efrain 197
 
17167 ariadna 198
 
199
 
16766 efrain 200
                    $status = Functions::sanitizeFilterString($this->params()->fromQuery('status'));
17167 ariadna 201
                    if (!in_array($status, [
1 www 202
                        CompanyUser::STATUS_ACCEPTED,
203
                        CompanyUser::STATUS_ADMIN_WILL_ADD,
204
                        CompanyUser::STATUS_CANCELLED,
205
                        CompanyUser::STATUS_PENDING,
206
                        CompanyUser::STATUS_REJECTED,
207
                        CompanyUser::STATUS_SENT,
17167 ariadna 208
                    ])) {
1 www 209
                        $status = '';
210
                    }
17167 ariadna 211
 
212
 
213
 
1 www 214
                    $userMapper = UserMapper::getInstance($this->adapter);
215
                    $companyUserMapper = CompanyUserMapper::getInstance($this->adapter);
17167 ariadna 216
 
16767 efrain 217
                    $paginator = $userMapper->fetchAllDataTableByCompanyId($currentCompany->id, $search, $status,   $page, $records_x_page, $order_field, $order_direction);
17167 ariadna 218
 
1 www 219
                    $items = [];
220
                    $records = $paginator->getCurrentItems();
17167 ariadna 221
 
222
                    foreach ($records as $record) {
223
 
224
                        $actions = [];
225
                        $actions['link_profile'] = 'https://' . $network->main_hostname . '/profile/view/' . $record['uuid'];
226
 
227
 
228
 
229
 
230
                        $details = [];
231
                        switch ($record['status']) {
232
 
233
                            case CompanyUser::STATUS_PENDING:
1 www 234
                                $details['status']  = 'LABEL_PENDING';
235
                                $actions['link_accept'] = $allowAccept ? $this->url()->fromRoute('users/accept', ['id' => $record['uuid']]) : '';
236
                                $actions['link_reject'] = $allowReject ? $this->url()->fromRoute('users/reject', ['id' => $record['uuid']]) : '';
237
                                break;
17167 ariadna 238
 
239
                            case CompanyUser::STATUS_ACCEPTED:
1 www 240
                                $details['status']  = 'LABEL_ACCEPTED';
17167 ariadna 241
                                if ($record['creator'] == CompanyUser::CREATOR_NO) {
1 www 242
                                    $actions['link_edit'] = $allowEdit ? $this->url()->fromRoute('users/edit', ['id' => $record['uuid']]) : '';
17167 ariadna 243
 
244
 
245
 
1 www 246
                                    $actions['link_cancel'] = $allowCancel ? $this->url()->fromRoute('users/cancel', ['id' => $record['uuid']]) : '';
17167 ariadna 247
                                }
248
 
1 www 249
                                break;
17167 ariadna 250
 
251
                            case CompanyUser::STATUS_ADMIN_WILL_ADD:
1 www 252
                                $details['status']  = 'LABEL_ADMIN_WILL_ADD';
253
                                $actions['link_cancel'] = $allowCancel ? $this->url()->fromRoute('users/cancel', ['id' => $record['uuid']]) : '';
254
                                $actions['link_edit'] = $allowEdit ? $this->url()->fromRoute('users/edit', ['id' => $record['uuid']]) : '';
17167 ariadna 255
 
1 www 256
                                break;
17167 ariadna 257
 
258
                            case CompanyUser::STATUS_SENT:
1 www 259
                                $details['status']  = 'LABEL_INVITED';
260
                                $actions['link_accept'] = $allowAccept ? $this->url()->fromRoute('users/accept', ['id' => $record['uuid']]) : '';
261
                                $actions['link_reject'] = $allowReject ? $this->url()->fromRoute('users/reject', ['id' => $record['uuid']]) : '';
262
                                break;
17167 ariadna 263
 
264
                            case CompanyUser::STATUS_REJECTED:
1 www 265
                                $actions['link_accept'] = $allowAccept ? $this->url()->fromRoute('users/accept', ['id' => $record['uuid']]) : '';
266
                                $details['status']  = 'LABEL_REJECTED';
267
                                break;
17167 ariadna 268
 
269
                            case CompanyUser::STATUS_CANCELLED:
1 www 270
                                $actions['link_accept'] = $allowAccept ? $this->url()->fromRoute('users/accept', ['id' => $record['uuid']]) : '';
271
                                $details['status']  = 'LABEL_CANCELLED';
272
                                break;
17167 ariadna 273
 
274
                            default:
1 www 275
                                $details['status']  = '';
276
                                break;
277
                        }
17167 ariadna 278
 
1 www 279
                        $totalOtherCompanies = $companyUserMapper->fetchCountOtherCompaniesByCompanyIdAndUserId($currentCompany->id, $record['id']);
17167 ariadna 280
                        if (!$totalOtherCompanies) {
281
 
282
                            $actions['link_change_password'] = $allowChangePassword ? $this->url()->fromRoute('users/change-password', ['id' => $record['uuid']]) : '';
283
                            if ($record['blocked'] == User::BLOCKED_YES) {
284
                                $actions['link_unblock'] = $allowUnblock ? $this->url()->fromRoute('users/unblock', ['id' => $record['uuid']]) : '';
285
                            }
1 www 286
                        }
17167 ariadna 287
 
288
                        if ($record['blocked'] == User::BLOCKED_YES) {
1 www 289
                            $details['blocked'] = 'LABEL_YES';
17167 ariadna 290
                        } else if ($record['blocked'] == User::BLOCKED_NO) {
1 www 291
                            $details['blocked'] = 'LABEL_NO';
292
                        }
17167 ariadna 293
                        if ($record['email_verified'] == User::EMAIL_VERIFIED_YES) {
1 www 294
                            $details['email_verified'] = 'LABEL_YES';
17167 ariadna 295
                        } else if ($record['email_verified'] == User::EMAIL_VERIFIED_NO) {
1 www 296
                            $details['email_verified'] = 'LABEL_NO';
297
                        }
298
                        $details['login_attempt'] = $record['login_attempt'];
17167 ariadna 299
 
300
 
1 www 301
                        $company_user_roles = $companyUserRoleMapper->fetchAllByCompanyIdAndUserId($currentCompany->id, $record['id']);
17167 ariadna 302
 
1 www 303
                        $details['roles'] = [];
17167 ariadna 304
 
305
                        if ($record['creator'] == CompanyUser::CREATOR_YES) {
1 www 306
                            $details['roles'][] = 'LABEL_ALL_PERMITS';
17167 ariadna 307
                            $details['creator'] = 'LABEL_YES';
308
                        } else {
309
                            $details['creator'] = 'LABEL_NO';
310
                            foreach ($company_user_roles as $company_user_role) {
311
                                $role = $roles[$company_user_role->role_id];
1 www 312
                                $details['roles'][] = $role;
313
                            }
17167 ariadna 314
                        }
315
 
316
 
1 www 317
                        $details['backend'] = $record['backend'] == CompanyUser::BACKEND_YES ? 'LABEL_YES' : 'LABEL_NO';
318
 
17167 ariadna 319
 
320
 
321
 
322
 
323
 
1 www 324
                        $item = [
17167 ariadna 325
                            'first_name' => $record['first_name'],
1 www 326
                            'last_name' => $record['last_name'],
327
                            'email' => $record['email'],
328
                            'details' => $details,
17167 ariadna 329
                            'actions' =>  $actions,
330
                        ];
331
 
1 www 332
                        array_push($items, $item);
333
                    }
334
                } else {
335
                    //Usuario cuando el nivel es administrador
17167 ariadna 336
 
337
 
338
 
1 www 339
                    $acl = $this->getEvent()->getViewModel()->getVariable('acl');
340
                    $allowUnblock = $acl->isAllowed($currentUser->usertype_id, 'users/unblock');
341
                    $allowChangePassword = $acl->isAllowed($currentUser->usertype_id, 'users/change-password');
16248 efrain 342
                    $allowChangeType   = $acl->isAllowed($currentUser->usertype_id, 'users/change-type') ? 1 : 0;
17167 ariadna 343
 
344
 
345
 
1 www 346
                    $userMapper = UserMapper::getInstance($this->adapter);
15455 efrain 347
                    $paginator = $userMapper->fetchAllDataTableByNetworkId($network->id, $search, $page, $records_x_page, $order_field, $order_direction);
17167 ariadna 348
 
1 www 349
                    $items = [];
350
                    $records = $paginator->getCurrentItems();
17167 ariadna 351
 
352
                    foreach ($records as $record) {
1 www 353
                        $actions = [];
17167 ariadna 354
 
355
                        if ($currentNetwork->id == $record->network_id) {
356
 
357
                            $actions['link_profile'] = 'https://' . $network->main_hostname . '/profile/view/' . $record->uuid;
16299 efrain 358
                        } else {
359
                            $actions['link_profile'] = '';
360
                        }
17167 ariadna 361
 
362
                        $details = [];
363
                        if ($record->status == User::STATUS_ACTIVE) {
1 www 364
                            $details['status'] = 'LABEL_ACTIVE';
17167 ariadna 365
                        } else if ($record->status == User::STATUS_INACTIVE) {
366
                            $details['status'] = 'LABEL_INACTIVE';
1 www 367
                        }
17167 ariadna 368
                        if ($record->blocked == User::BLOCKED_YES) {
1 www 369
                            $details['blocked'] = 'LABEL_YES';
17167 ariadna 370
                        } else if ($record->blocked == User::BLOCKED_NO) {
1 www 371
                            $details['blocked'] = 'LABEL_NO';
372
                        }
17167 ariadna 373
                        if ($record->email_verified == User::EMAIL_VERIFIED_YES) {
1 www 374
                            $details['email_verified'] = 'LABEL_YES';
17167 ariadna 375
                        } else if ($record->email_verified == User::EMAIL_VERIFIED_NO) {
1 www 376
                            $details['email_verified'] = 'LABEL_NO';
377
                        }
17167 ariadna 378
 
379
                        if ($record->usertype_id == UserType::ADMIN) {
16248 efrain 380
                            $details['user_type'] = 'LABEL_ADMINISTRATOR';
17167 ariadna 381
                        } else   if ($record->usertype_id == UserType::USER) {
16248 efrain 382
                            $details['user_type'] = 'LABEL_USER';
383
                        }
17167 ariadna 384
 
1 www 385
                        $details['login_attempt'] = $record->login_attempt;
17167 ariadna 386
 
387
 
388
                        $actions['link_change_password'] = $allowChangePassword ? $this->url()->fromRoute('users/change-password', ['id' => $record->uuid]) : '';
389
                        $actions['link_unblock'] = $allowUnblock && $record->blocked == User::BLOCKED_YES ? $this->url()->fromRoute('users/unblock', ['id' => $record->uuid]) : '';
390
                        $actions['link_change_type'] =  $record->is_super_user == User::IS_SUPER_USER_NO && $allowChangeType ? $this->url()->fromRoute('users/change-type', ['id' => $record->uuid]) : '';
391
 
392
 
1 www 393
                        $item = [
394
                            'first_name' => $record->first_name,
395
                            'last_name' => $record->last_name,
396
                            'email' => $record->email,
397
                            'details' => $details,
398
                            'actions' => $actions
17167 ariadna 399
                        ];
400
 
1 www 401
                        array_push($items, $item);
402
                    }
403
                }
404
            }
17167 ariadna 405
 
1 www 406
            return new JsonModel([
407
                'success' => true,
408
                'data' => [
409
                    'items' => $items,
410
                    'total' => $paginator->getTotalItemCount(),
411
                ]
412
            ]);
17167 ariadna 413
        } else if ($request->isGet()) {
1 www 414
            $this->layout()->setTemplate('layout/layout-backend');
415
            $viewModel = new ViewModel();
17167 ariadna 416
 
417
 
16299 efrain 418
            $networks = [];
419
            $networkMapper = NetworkMapper::getInstance($this->adapter);
17167 ariadna 420
 
421
            if ($currentUser->is_super_user == User::IS_SUPER_USER_YES) {
16299 efrain 422
                $records = $networkMapper->fetchAll();
17167 ariadna 423
                foreach ($records as $record) {
424
                    $networks[$record->uuid] = $record->name;
16299 efrain 425
                }
426
            } else {
17167 ariadna 427
                $networks[$currentNetwork->uuid] = $currentNetwork->name;
16299 efrain 428
            }
17167 ariadna 429
 
430
 
16299 efrain 431
            $formFilter = new NetworkDataForm($networks);
15460 efrain 432
            $formUploadUsers = new UserUploadForm();
1 www 433
            $formChangePassword = new ChangePasswordForm();
16248 efrain 434
            $formChangeType = new ChangeTypeForm();
17167 ariadna 435
 
1 www 436
            $company = $currentUserPlugin->getCompany();
17167 ariadna 437
            if ($company) {
1 www 438
                $viewModel->setTemplate('leaders-linked/users/company.phtml');
17167 ariadna 439
            } else {
1 www 440
                $viewModel->setTemplate('leaders-linked/users/index.phtml');
441
            }
15460 efrain 442
            $viewModel->setVariables([
443
                'formUploadUsers' => $formUploadUsers,
444
                'formChangePassword' => $formChangePassword,
16248 efrain 445
                'formChangeType' => $formChangeType,
16299 efrain 446
                'formFilter' => $formFilter
17167 ariadna 447
            ]);
448
            return $viewModel;
1 www 449
        } else {
450
            return new JsonModel([
451
                'success' => false,
452
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
453
            ]);;
454
        }
455
    }
456
    /*
457
    public function addAction()
458
    {
459
        $currentUserPlugin = $this->plugin('currentUserPlugin');
460
        $currentUser = $currentUserPlugin->getUser();
461
 
462
        $request = $this->getRequest();
463
 
464
 
465
        if($request->isPost()) {
466
            $form = new  AddForm($this->adapter);
467
            $dataPost = $request->getPost()->toArray();
468
 
469
            $form->setData($dataPost);
470
 
471
            if($form->isValid()) {
472
                $dataPost = (array) $form->getData();
473
 
474
                $hydrator = new ObjectPropertyHydrator();
475
                $user = new User();
476
                $hydrator->hydrate($dataPost, $user);
477
 
478
 
479
                $userMapper = UserMapper::getInstance($this->adapter);
480
                $result = $userMapper->insert($user);
481
 
482
                if($result) {
483
                    $this->logger->info('Se agrego el usuario ' . $user->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
484
 
485
                    $data = [
486
                        'success'   => true,
487
                        'data'   => 'LABEL_RECORD_ADDED'
488
                    ];
489
                } else {
490
                    $data = [
491
                        'success'   => false,
492
                        'data'      => $userMapper->getError()
493
                    ];
494
 
495
                }
496
 
497
                return new JsonModel($data);
498
 
499
            } else {
500
                $messages = [];
501
                $form_messages = (array) $form->getMessages();
502
                foreach($form_messages  as $fieldname => $field_messages)
503
                {
504
 
505
                    $messages[$fieldname] = array_values($field_messages);
506
                }
507
 
508
                return new JsonModel([
509
                    'success'   => false,
510
                    'data'   => $messages
511
                ]);
512
            }
513
 
514
        } else {
515
            $data = [
516
                'success' => false,
517
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
518
            ];
519
 
520
            return new JsonModel($data);
521
        }
522
 
523
        return new JsonModel($data);
524
    }
525
 
526
    public function editAction()
527
    {
528
        $currentUserPlugin = $this->plugin('currentUserPlugin');
529
        $currentUser = $currentUserPlugin->getUser();
530
 
531
        $request = $this->getRequest();
532
        $id = $this->params()->fromRoute('id');
533
 
534
 
535
        if(!$id) {
536
            $data = [
537
                'success'   => false,
538
                'data'   => 'ERROR_INVALID_PARAMETER'
539
            ];
540
 
541
            return new JsonModel($data);
542
        }
543
 
544
        $userMapper = UserMapper::getInstance($this->adapter);
545
        $user = $userMapper->fetchOne($id);
546
        if(!$user) {
547
            $data = [
548
                'success'   => false,
549
                'data'   => 'ERROR_RECORD_NOT_FOUND'
550
            ];
551
 
552
            return new JsonModel($data);
553
        }
554
 
555
        if($request->isPost()) {
556
            $form = new  EditForm($this->adapter);
557
            $dataPost = $request->getPost()->toArray();
558
 
559
            $form->setData($dataPost);
560
 
561
            if($form->isValid()) {
562
                $dataPost = (array) $form->getData();
563
 
564
                $hydrator = new ObjectPropertyHydrator();
565
                $hydrator->hydrate($dataPost, $user);
566
                $result = $userMapper->update($user);
567
 
568
                if($result) {
569
                    $this->logger->info('Se actualizo el usuario ' . $user->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
570
 
571
                    $data = [
572
                        'success' => true,
573
                        'data' => 'LABEL_RECORD_UPDATED'
574
                    ];
575
                } else {
576
                    $data = [
577
                        'success'   => false,
578
                        'data'      => $userMapper->getError()
579
                    ];
580
                }
581
 
582
                return new JsonModel($data);
583
 
584
            } else {
585
                $messages = [];
586
                $form_messages = (array) $form->getMessages();
587
                foreach($form_messages  as $fieldname => $field_messages)
588
                {
589
                    $messages[$fieldname] = array_values($field_messages);
590
                }
591
 
592
                return new JsonModel([
593
                    'success'   => false,
594
                    'data'   => $messages
595
                ]);
596
            }
597
        } else if ($request->isGet()) {
598
            $hydrator = new ObjectPropertyHydrator();
599
 
600
            $data = [
601
                'success' => true,
602
                'data' => $hydrator->extract($user)
603
            ];
604
 
605
            return new JsonModel($data);
606
        } else {
607
            $data = [
608
                'success' => false,
609
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
610
            ];
611
 
612
            return new JsonModel($data);
613
        }
614
 
615
        return new JsonModel($data);
616
    }
617
 
618
    public function deleteAction()
619
    {
620
        $currentUserPlugin = $this->plugin('currentUserPlugin');
621
        $currentUser = $currentUserPlugin->getUser();
622
 
623
        $request = $this->getRequest();
624
        $id = $this->params()->fromRoute('id');
625
 
626
        if(!$id) {
627
            $data = [
628
                'success'   => false,
629
                'data'   => 'ERROR_INVALID_PARAMETER'
630
            ];
631
 
632
            return new JsonModel($data);
633
        }
634
 
635
 
636
        $userMapper = UserMapper::getInstance($this->adapter);
637
        $user = $userMapper->fetchOne($id);
638
        if(!$user) {
639
            $data = [
640
                'success'   => false,
641
                'data'   => 'ERROR_RECORD_NOT_FOUND'
642
            ];
643
 
644
            return new JsonModel($data);
645
        }
646
 
647
        if($request->isPost()) {
648
            $result = $userMapper->delete($user);
649
            if($result) {
650
                $this->logger->info('Se borro el usuario ' . $user->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
651
 
652
                $data = [
653
                    'success' => true,
654
                    'data' => 'LABEL_RECORD_DELETED'
655
                ];
656
            } else {
657
 
658
                $data = [
659
                    'success'   => false,
660
                    'data'      => $userMapper->getError()
661
                ];
662
 
663
                return new JsonModel($data);
664
            }
665
 
666
        } else {
667
            $data = [
668
                'success' => false,
669
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
670
            ];
671
 
672
            return new JsonModel($data);
673
        }
674
 
675
        return new JsonModel($data);
676
    }*/
17167 ariadna 677
 
1 www 678
    public function unblockAction()
679
    {
680
        $currentUserPlugin = $this->plugin('currentUserPlugin');
681
        $currentUser = $currentUserPlugin->getUser();
682
        $request = $this->getRequest();
683
 
17167 ariadna 684
 
685
        if ($request->isPost()) {
686
 
1 www 687
            $uuid = $this->params()->fromRoute('id');
17167 ariadna 688
            if (!$uuid) {
1 www 689
                return new JsonModel([
690
                    'success'   => false,
691
                    'data'      => 'ERROR_INVALID_PARAMETER'
692
                ]);
693
            }
17167 ariadna 694
 
1 www 695
            $userMapper = UserMapper::getInstance($this->adapter);
696
            $user = $userMapper->fetchOneByUuid($uuid);
17167 ariadna 697
 
698
            if (!$user) {
1 www 699
                return new JsonModel([
700
                    'success'   => false,
701
                    'data'      => 'ERROR_USER_NOT_FOUND'
702
                ]);
703
            }
17167 ariadna 704
 
705
            if ($user->blocked == User::BLOCKED_NO) {
1 www 706
                return new JsonModel([
707
                    'success'   => false,
708
                    'data'      => 'ERROR_USER_IS_NOT_BLOCKED'
709
                ]);
710
            }
711
 
712
 
17167 ariadna 713
 
1 www 714
            $result = $userMapper->unblock($user);
17167 ariadna 715
            if ($result) {
1 www 716
                $this->logger->info('El usuario : ' . $user->email . ' ha sido desbloqueado ', ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
17167 ariadna 717
 
1 www 718
                return new JsonModel([
719
                    'success'   => true,
720
                    'data'      => 'LABEL_USER_HAS_BEEN_UNBLOCKED',
721
                ]);
17167 ariadna 722
            } else {
723
 
1 www 724
                return new JsonModel([
725
                    'success'   => false,
726
                    'data'      => $userMapper->getError()
727
                ]);
728
            }
729
        }
17167 ariadna 730
 
731
 
732
 
1 www 733
        return new JsonModel([
734
            'success' => false,
735
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
736
        ]);
737
    }
17167 ariadna 738
 
129 efrain 739
    public function emailVerifyAction()
740
    {
741
        $currentUserPlugin = $this->plugin('currentUserPlugin');
742
        $currentUser = $currentUserPlugin->getUser();
743
        $request = $this->getRequest();
17167 ariadna 744
 
745
 
746
        if ($request->isPost()) {
747
 
129 efrain 748
            $uuid = $this->params()->fromRoute('id');
17167 ariadna 749
            if (!$uuid) {
129 efrain 750
                return new JsonModel([
751
                    'success'   => false,
752
                    'data'      => 'ERROR_INVALID_PARAMETER'
753
                ]);
754
            }
17167 ariadna 755
 
129 efrain 756
            $userMapper = UserMapper::getInstance($this->adapter);
757
            $user = $userMapper->fetchOneByUuid($uuid);
17167 ariadna 758
 
759
            if (!$user) {
129 efrain 760
                return new JsonModel([
761
                    'success'   => false,
762
                    'data'      => 'ERROR_USER_NOT_FOUND'
763
                ]);
764
            }
17167 ariadna 765
 
766
            if ($user->blocked == User::BLOCKED_NO) {
129 efrain 767
                return new JsonModel([
768
                    'success'   => false,
769
                    'data'      => 'ERROR_USER_IS_NOT_PENDING_FOR_EMAIL_VERIFY'
770
                ]);
771
            }
17167 ariadna 772
 
773
 
774
 
129 efrain 775
            $result = $userMapper->emailVerifyAndActive($user);
17167 ariadna 776
            if ($result) {
129 efrain 777
                $this->logger->info('El usuario : ' . $user->email . ' ha sido desbloqueado ', ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
17167 ariadna 778
 
129 efrain 779
                return new JsonModel([
780
                    'success'   => true,
781
                    'data'      => 'LABEL_USER_HAS_BEEN_UNBLOCKED',
782
                ]);
17167 ariadna 783
            } else {
784
 
129 efrain 785
                return new JsonModel([
786
                    'success'   => false,
787
                    'data'      => $userMapper->getError()
788
                ]);
789
            }
790
        }
17167 ariadna 791
 
792
 
793
 
129 efrain 794
        return new JsonModel([
795
            'success' => false,
796
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
797
        ]);
798
    }
17167 ariadna 799
 
1 www 800
    public function changePasswordAction()
801
    {
802
        $currentUserPlugin = $this->plugin('currentUserPlugin');
803
        $currentUser = $currentUserPlugin->getUser();
17167 ariadna 804
 
1 www 805
        $request = $this->getRequest();
806
 
17167 ariadna 807
        if ($request->isGet()) {
1 www 808
            $uuid = $this->params()->fromRoute('id');
17167 ariadna 809
            if (!$uuid) {
1 www 810
                return new JsonModel([
811
                    'success'   => false,
812
                    'data'      => 'ERROR_INVALID_PARAMETER'
813
                ]);
814
            }
17167 ariadna 815
 
1 www 816
            $userMapper = UserMapper::getInstance($this->adapter);
817
            $user = $userMapper->fetchOneByUuid($uuid);
17167 ariadna 818
 
819
 
820
 
821
            if ($user) {
1 www 822
                return new JsonModel([
823
                    'success'   => true,
824
                    'data'      => [
825
                        'first_name' => $user->first_name,
826
                        'last_name' => $user->last_name,
827
                        'email' => $user->email,
828
                    ]
829
                ]);
830
            } else {
831
                return new JsonModel([
832
                    'success'   => false,
833
                    'data'      => 'ERROR_USER_NOT_FOUND'
834
                ]);
835
            }
836
        }
17167 ariadna 837
 
838
        if ($request->isPost()) {
839
 
1 www 840
            $uuid = $this->params()->fromRoute('id');
17167 ariadna 841
            if (!$uuid) {
1 www 842
                return new JsonModel([
843
                    'success'   => false,
844
                    'data'      => 'ERROR_INVALID_PARAMETER'
845
                ]);
846
            }
17167 ariadna 847
 
1 www 848
            $userMapper = UserMapper::getInstance($this->adapter);
849
            $user = $userMapper->fetchOneByUuid($uuid);
17167 ariadna 850
 
851
            if (!$user) {
1 www 852
                return new JsonModel([
853
                    'success'   => false,
854
                    'data'      => 'ERROR_USER_NOT_FOUND'
855
                ]);
856
            }
17167 ariadna 857
 
858
 
1 www 859
            $dataPost = $request->getPost()->toArray();
860
            $form = new ChangePasswordForm();
861
            $form->setData($dataPost);
17167 ariadna 862
 
863
            if ($form->isValid()) {
864
 
865
 
866
 
1 www 867
                $data = (array) $form->getData();
868
                $password = $data['password'];
869
 
17167 ariadna 870
 
871
 
1 www 872
                $userPasswordMapper = UserPasswordMapper::getInstance($this->adapter);
873
                $userPasswords = $userPasswordMapper->fetchAllByUserId($user->id);
17167 ariadna 874
 
1 www 875
                $oldPassword = false;
17167 ariadna 876
                foreach ($userPasswords as $userPassword) {
877
                    if (password_verify($password, $userPassword->password) || (md5($password) == $userPassword->password)) {
1 www 878
                        $oldPassword = true;
879
                        break;
880
                    }
881
                }
17167 ariadna 882
 
883
                if ($oldPassword) {
1 www 884
                    $this->logger->err('Cambio de contraseña del usuario - error contraseña ya utilizada anteriormente', ['user_id' =>  $currentUser->id, 'ip' => Functions::getUserIP()]);
17167 ariadna 885
 
1 www 886
                    return new JsonModel([
887
                        'success'   => false,
888
                        'data'      => 'ERROR_PASSWORD_HAS_ALREADY_BEEN_USED'
17167 ariadna 889
 
1 www 890
                    ]);
891
                } else {
892
                    $password_hash = password_hash($password, PASSWORD_DEFAULT);
17167 ariadna 893
 
894
 
1 www 895
                    $result = $userMapper->updatePassword($user, $password_hash);
17167 ariadna 896
                    if ($result) {
1 www 897
                        $this->logger->info('Cambio de contraseña del usuario realizado', ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
17167 ariadna 898
 
899
 
1 www 900
                        return new JsonModel([
901
                            'success'   => true,
902
                            'data'      => 'LABEL_YOUR_PASSWORD_HAS_BEEN_UPDATED'
17167 ariadna 903
 
1 www 904
                        ]);
905
                    } else {
906
                        $this->logger->err('Cambio de contraseña del usuario - error desconocido', ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
17167 ariadna 907
 
1 www 908
                        return new JsonModel([
909
                            'success'   => true,
910
                            'data'      => 'ERROR_THERE_WAS_AN_ERROR'
17167 ariadna 911
 
1 www 912
                        ]);
913
                    }
914
                }
915
            } else {
916
                $messages = [];
17167 ariadna 917
 
1 www 918
                $form_messages = (array) $form->getMessages();
17167 ariadna 919
                foreach ($form_messages  as $fieldname => $field_messages) {
1 www 920
                    $messages[$fieldname] = array_values($field_messages);
921
                }
17167 ariadna 922
 
1 www 923
                return new JsonModel([
924
                    'success'   => false,
925
                    'data'   => $messages
926
                ]);
927
            }
928
        }
17167 ariadna 929
 
930
 
931
 
1 www 932
        return new JsonModel([
933
            'success' => false,
934
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
935
        ]);
936
    }
17167 ariadna 937
 
1 www 938
    public function cancelAction()
939
    {
940
        $currentUserPlugin = $this->plugin('currentUserPlugin');
941
        $currentUser = $currentUserPlugin->getUser();
17167 ariadna 942
 
943
        $currentCompany = $currentUserPlugin->getCompany();
944
 
1 www 945
        $request = $this->getRequest();
17167 ariadna 946
 
947
 
948
        if ($request->isPost()) {
949
 
1 www 950
            $uuid = $this->params()->fromRoute('id');
17167 ariadna 951
            if (!$uuid) {
1 www 952
                return new JsonModel([
953
                    'success'   => false,
954
                    'data'      => 'ERROR_INVALID_PARAMETER'
955
                ]);
956
            }
17167 ariadna 957
 
1 www 958
            $userMapper = UserMapper::getInstance($this->adapter);
959
            $user = $userMapper->fetchOneByUuid($uuid);
17167 ariadna 960
 
961
            if (!$user) {
1 www 962
                return new JsonModel([
963
                    'success'   => false,
964
                    'data'      => 'ERROR_USER_COMPANY_NOT_FOUND'
965
                ]);
966
            }
17167 ariadna 967
 
1 www 968
            $companyUserMapper = CompanyUserMapper::getInstance($this->adapter);
17167 ariadna 969
            $companyUser = $companyUserMapper->fetchOneByCompanyIdAndUserId($currentCompany->id, $user->id);
970
 
971
            if (!$companyUser) {
1 www 972
                return new JsonModel([
973
                    'success'   => false,
974
                    'data'      => 'ERROR_USER_NOT_FOUND'
975
                ]);
976
            }
17167 ariadna 977
 
978
 
979
            if (!in_array($companyUser->status, [CompanyUser::STATUS_ADMIN_WILL_ADD, CompanyUser::STATUS_ACCEPTED])) {
1 www 980
                return new JsonModel([
981
                    'success'   => false,
982
                    'data'      => 'ERROR_USER_COMPANY_WRONG_STATUS'
983
                ]);
984
            }
17167 ariadna 985
 
1 www 986
            $companyUser->status = CompanyUser::STATUS_CANCELLED;
987
            $result = $companyUserMapper->update($companyUser);
17167 ariadna 988
            if ($result) {
1 www 989
                $this->logger->info('La relación del usuario : ' . $user->email . ' con la empresa : ' . $currentCompany->name  . ' ha sido cancelada ', ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
17167 ariadna 990
 
1 www 991
                return new JsonModel([
992
                    'success'   => true,
993
                    'data'      => 'LABEL_USER_COMPANY_HAS_BEEN_CANCELLED',
994
                ]);
17167 ariadna 995
            } else {
996
 
1 www 997
                return new JsonModel([
998
                    'success'   => false,
999
                    'data'      => $userMapper->getError()
1000
                ]);
1001
            }
1002
        }
17167 ariadna 1003
 
1004
 
1005
 
1 www 1006
        return new JsonModel([
1007
            'success' => false,
1008
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
1009
        ]);
1010
    }
17167 ariadna 1011
 
1 www 1012
    public function acceptAction()
1013
    {
1014
        $currentUserPlugin = $this->plugin('currentUserPlugin');
1015
        $currentUser = $currentUserPlugin->getUser();
17167 ariadna 1016
 
1 www 1017
        $currentCompany = $currentUserPlugin->getCompany();
17167 ariadna 1018
 
1 www 1019
        $request = $this->getRequest();
17167 ariadna 1020
 
1021
 
1022
        if ($request->isPost()) {
1023
 
1 www 1024
            $uuid = $this->params()->fromRoute('id');
17167 ariadna 1025
            if (!$uuid) {
1 www 1026
                return new JsonModel([
1027
                    'success'   => false,
1028
                    'data'      => 'ERROR_INVALID_PARAMETER'
1029
                ]);
1030
            }
17167 ariadna 1031
 
1 www 1032
            $userMapper = UserMapper::getInstance($this->adapter);
1033
            $user = $userMapper->fetchOneByUuid($uuid);
17167 ariadna 1034
 
1035
            if (!$user) {
1 www 1036
                return new JsonModel([
1037
                    'success'   => false,
1038
                    'data'      => 'ERROR_USER_NOT_FOUND'
1039
                ]);
1040
            }
17167 ariadna 1041
 
1 www 1042
            $companyUserMapper = CompanyUserMapper::getInstance($this->adapter);
1043
            $companyUser = $companyUserMapper->fetchOneByCompanyIdAndUserId($currentCompany->id, $user->id);
17167 ariadna 1044
 
1045
            if (!$companyUser) {
1 www 1046
                return new JsonModel([
1047
                    'success'   => false,
1048
                    'data'      => 'ERROR_USER_NOT_FOUND'
1049
                ]);
1050
            }
17167 ariadna 1051
 
1052
            if (
1053
                $companyUser->status != CompanyUser::STATUS_PENDING
1 www 1054
                && $companyUser->status != CompanyUser::STATUS_SENT
1055
                && $companyUser->status != CompanyUser::STATUS_CANCELLED
17167 ariadna 1056
                && $companyUser->status != CompanyUser::STATUS_REJECTED
1057
            ) {
1 www 1058
                return new JsonModel([
1059
                    'success'   => false,
1060
                    'data'      => 'ERROR_USER_COMPANY_WRONG_STATUS'
1061
                ]);
1062
            }
17167 ariadna 1063
 
1 www 1064
            $companyUser->status = CompanyUser::STATUS_ACCEPTED;
1065
            $result = $companyUserMapper->update($companyUser);
17167 ariadna 1066
            if ($result) {
1 www 1067
                $this->logger->info('La relación del usuario : ' . $user->email . ' con la empresa : ' . $currentCompany->name  . ' ha sido aceptada ', ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
17167 ariadna 1068
 
1 www 1069
                return new JsonModel([
1070
                    'success'   => true,
1071
                    'data'      => 'LABEL_USER_COMPANY_HAS_BEEN_ACCEPTED'
1072
                ]);
17167 ariadna 1073
            } else {
1074
 
1 www 1075
                return new JsonModel([
1076
                    'success'   => false,
1077
                    'data'      => $userMapper->getError()
1078
                ]);
1079
            }
1080
        }
17167 ariadna 1081
 
1082
 
1083
 
1 www 1084
        return new JsonModel([
1085
            'success' => false,
1086
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
1087
        ]);
1088
    }
17167 ariadna 1089
 
1 www 1090
    public function rejectAction()
1091
    {
1092
        $currentUserPlugin = $this->plugin('currentUserPlugin');
1093
        $currentUser = $currentUserPlugin->getUser();
17167 ariadna 1094
 
1 www 1095
        $currentCompany = $currentUserPlugin->getCompany();
17167 ariadna 1096
 
1 www 1097
        $request = $this->getRequest();
17167 ariadna 1098
 
1099
 
1100
        if ($request->isPost()) {
1101
 
1 www 1102
            $uuid = $this->params()->fromRoute('id');
17167 ariadna 1103
            if (!$uuid) {
1 www 1104
                return new JsonModel([
1105
                    'success'   => false,
1106
                    'data'      => 'ERROR_INVALID_PARAMETER'
1107
                ]);
1108
            }
17167 ariadna 1109
 
1 www 1110
            $userMapper = UserMapper::getInstance($this->adapter);
1111
            $user = $userMapper->fetchOneByUuid($uuid);
17167 ariadna 1112
 
1113
            if (!$user) {
1 www 1114
                return new JsonModel([
1115
                    'success'   => false,
1116
                    'data'      => 'ERROR_USER_NOT_FOUND'
1117
                ]);
1118
            }
17167 ariadna 1119
 
1 www 1120
            $companyUserMapper = CompanyUserMapper::getInstance($this->adapter);
1121
            $companyUser = $companyUserMapper->fetchOneByCompanyIdAndUserId($currentCompany->id, $user->id);
17167 ariadna 1122
 
1123
            if (!$companyUser) {
1 www 1124
                return new JsonModel([
1125
                    'success'   => false,
1126
                    'data'      => 'ERROR_USER_COMPANY_NOT_FOUND'
1127
                ]);
1128
            }
17167 ariadna 1129
 
1130
            if (
1131
                $companyUser->status != CompanyUser::STATUS_PENDING
1132
                && $companyUser->status != CompanyUser::STATUS_SENT
1133
            ) {
1 www 1134
                return new JsonModel([
1135
                    'success'   => false,
1136
                    'data'      => 'ERROR_USER_COMPANY_WRONG_STATUS'
1137
                ]);
1138
            }
17167 ariadna 1139
 
1 www 1140
            $companyUser->status = CompanyUser::STATUS_REJECTED;
1141
            $result = $companyUserMapper->update($companyUser);
17167 ariadna 1142
            if ($result) {
1 www 1143
                $this->logger->info('La relación del usuario : ' . $user->email . ' con la empresa : ' . $currentCompany->name  . ' ha sido rechazada ', ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
17167 ariadna 1144
 
1 www 1145
                return new JsonModel([
1146
                    'success'   => true,
1147
                    'data'      => 'LABEL_USER_COMPANY_HAS_BEEN_REJECTED',
1148
                ]);
17167 ariadna 1149
            } else {
1150
 
1 www 1151
                return new JsonModel([
1152
                    'success'   => false,
1153
                    'data'      => $userMapper->getError()
1154
                ]);
1155
            }
1156
        }
17167 ariadna 1157
 
1158
 
1159
 
1 www 1160
        return new JsonModel([
1161
            'success' => false,
1162
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
1163
        ]);
1164
    }
17167 ariadna 1165
 
1 www 1166
    public function inviteAction()
1167
    {
1168
        $currentUserPlugin = $this->plugin('currentUserPlugin');
1169
        $currentUser = $currentUserPlugin->getUser();
17167 ariadna 1170
 
1171
 
1172
 
1 www 1173
        $currentCompany = $currentUserPlugin->getCompany();
17167 ariadna 1174
 
1 www 1175
        $request = $this->getRequest();
17167 ariadna 1176
 
1177
        if ($request->isGet()) {
1178
 
1179
            $search = Functions::sanitizeFilterString($this->params()->fromQuery('search'));
1180
            if (strlen($search) >= 3) {
1181
 
1 www 1182
                $userMapper = UserMapper::getInstance($this->adapter);
1183
                $records  = $userMapper->fetchAllSuggestForInvitationByCompanyId($currentCompany->id, $search);
17167 ariadna 1184
 
1185
                $users = [];
1186
                foreach ($records as $record) {
1 www 1187
                    array_push($users, [
17167 ariadna 1188
                        'value' => $record->uuid,
1189
                        'text' => trim($record->first_name . ' ' . $record->last_name) . ' (' . $record->email . ')'
1190
 
1 www 1191
                    ]);
1192
                }
17167 ariadna 1193
 
1 www 1194
                return new JsonModel([
1195
                    'success' => true,
1196
                    'data' => $users
1197
                ]);
1198
            } else {
1199
                return new JsonModel([
1200
                    'success' => true,
17167 ariadna 1201
                    'data' => []
1202
                ]);
1203
            }
1204
        } else if ($request->isPost()) {
1205
 
1 www 1206
            $uuid = $this->params()->fromPost('id');
17167 ariadna 1207
            if (!$uuid) {
1 www 1208
                return new JsonModel([
1209
                    'success'   => false,
1210
                    'data'      => 'ERROR_INVALID_PARAMETER'
1211
                ]);
1212
            }
17167 ariadna 1213
 
1 www 1214
            $userMapper = UserMapper::getInstance($this->adapter);
1215
            $user = $userMapper->fetchOneByUuid($uuid);
17167 ariadna 1216
 
1217
            if (!$user) {
1 www 1218
                return new JsonModel([
1219
                    'success'   => false,
1220
                    'data'      => 'ERROR_USER_NOT_FOUND'
1221
                ]);
1222
            }
17167 ariadna 1223
 
1224
            if ($user->status != User::STATUS_ACTIVE) {
1 www 1225
                return new JsonModel([
1226
                    'success'   => false,
1227
                    'data'      => 'ERROR_USER_IS_INACTIVE'
1228
                ]);
1229
            }
17167 ariadna 1230
 
1231
 
1232
 
1233
 
1234
 
1 www 1235
            $companyUserMapper = CompanyUserMapper::getInstance($this->adapter);
1236
            $companyUser = $companyUserMapper->fetchOneByCompanyIdAndUserId($currentCompany->id, $user->id);
17167 ariadna 1237
 
1238
            if ($companyUser && $companyUser->status == CompanyUser::STATUS_ACCEPTED) {
1 www 1239
                return new JsonModel([
1240
                    'success'   => false,
1241
                    'data'      => 'ERROR_USER_COMPANY_FOUND'
1242
                ]);
1243
            }
15351 efrain 1244
            $networkMapper = NetworkMapper::getInstance($this->adapter);
1245
            $network = $networkMapper->fetchOne($currentUser->network_id);
1 www 1246
 
17167 ariadna 1247
 
1248
            if ($companyUser) {
1249
 
1250
 
1251
                if ($network->default == Network::DEFAULT_YES) {
15351 efrain 1252
                    $companyUser->status = CompanyUser::STATUS_ADMIN_WILL_ADD;
1253
                } else {
1254
                    $companyUser->status = CompanyUser::STATUS_ACCEPTED;
1255
                }
1 www 1256
                $result = $companyUserMapper->update($companyUser);
1257
            } else {
15351 efrain 1258
 
17167 ariadna 1259
 
1 www 1260
                $companyUser = new CompanyUser();
1261
                $companyUser->company_id = $currentCompany->id;
1262
                $companyUser->backend = CompanyUser::BACKEND_NO;
1263
                $companyUser->creator = CompanyUser::CREATOR_NO;
1264
                $companyUser->owner = CompanyUser::OWNER_NO;
17167 ariadna 1265
 
1266
                if ($network->default == Network::DEFAULT_YES) {
15351 efrain 1267
                    $companyUser->status = CompanyUser::STATUS_ADMIN_WILL_ADD;
1268
                } else {
1269
                    $companyUser->status = CompanyUser::STATUS_ACCEPTED;
1270
                }
17167 ariadna 1271
 
1272
 
1 www 1273
                $companyUser->status = CompanyUser::STATUS_ADMIN_WILL_ADD;
1274
                $companyUser->user_id = $user->id;
17167 ariadna 1275
 
1276
 
1277
 
1 www 1278
                $result = $companyUserMapper->insert($companyUser);
1279
            }
1280
 
17167 ariadna 1281
 
1282
 
1283
            if ($result) {
1284
 
1285
 
1 www 1286
                $notification = new Notification();
1287
                $notification->type     = Notification::TYPE_RECEIVE_INVITATION_COMPANY;
1288
                $notification->read     = Notification::NO;
1289
                $notification->user_id  = $user->id;
1290
                $notification->company_id = $currentCompany->id;
1291
                $notification->message  = 'LABEL_NOTIFICATION_RECEIVE_INVITATION_COMPANY';
1292
                $notification->url      = 'company/view/' . $currentCompany->uuid;
17167 ariadna 1293
 
1 www 1294
                $notificationMapper = NotificationMapper::getInstance($this->adapter);
1295
                $notificationMapper->insert($notification);
17167 ariadna 1296
 
1 www 1297
                $userNotificationMapper = UserNotificationSettingMapper::getInstance($this->adapter);
1298
                $userNotification = $userNotificationMapper->fetchOne($user->id);
17167 ariadna 1299
 
1300
                if ($userNotification && $userNotification->receive_invitation_company) {
1 www 1301
                    $emailTemplateMapper = EmailTemplateMapper::getInstance($this->adapter);
15351 efrain 1302
                    $emailTemplate = $emailTemplateMapper->fetchOneByCodeAndNetworkId(EmailTemplate::CODE_RECEIVE_INVITATION_COMPANY, $currentUser->network_id);
17167 ariadna 1303
 
1304
                    if ($emailTemplate) {
1305
                        $company_profile_url = 'https://' . $network->main_hostname . '/company/view/' . $currentCompany->uuid;
1306
 
1307
 
1 www 1308
                        $arrayCont = [
1309
                            'firstname'             => $currentUser->first_name,
1310
                            'lastname'              => $currentUser->last_name,
1311
                            'other_user_firstname'  => $user->first_name,
1312
                            'other_user_lastname'   => $user->last_name,
1313
                            'company_name'          => $currentCompany->name,
1314
                            'group_name'            => '',
1315
                            'content'               => '',
1316
                            'code'                  => '',
1317
                            'link'                  => $company_profile_url,
1318
                        ];
17167 ariadna 1319
 
1 www 1320
                        $email = new QueueEmail($this->adapter);
1321
                        $email->processEmailTemplate($emailTemplate, $arrayCont, $user->email, trim($user->first_name . ' ' . $user->last_name));
1322
                    }
1323
                }
17167 ariadna 1324
 
1325
 
1 www 1326
                $this->logger->info('La empresa : ' . $currentCompany->name . ' envio al usuario : ' . $user->email . ' una invitación ', ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
17167 ariadna 1327
 
1328
                if ($network->default == Network::DEFAULT_YES) {
15351 efrain 1329
                    return new JsonModel([
1330
                        'success'   => true,
1331
                        'data'      => 'LABEL_USER_COMPANY_HAS_BEEN_CREATED',
1332
                    ]);
1333
                } else {
1334
                    return new JsonModel([
1335
                        'success'   => true,
1336
                        'data'      => 'LABEL_USER_COMPANY_HAS_BEEN_INVITED_SENT',
1337
                    ]);
1338
                }
17167 ariadna 1339
            } else {
1340
 
1 www 1341
                return new JsonModel([
1342
                    'success'   => false,
1343
                    'data'      => $userMapper->getError()
1344
                ]);
1345
            }
1346
        }
17167 ariadna 1347
 
1348
 
1349
 
1 www 1350
        return new JsonModel([
1351
            'success' => false,
1352
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
1353
        ]);
1354
    }
17167 ariadna 1355
 
1356
 
1 www 1357
    public function deleteAction()
1358
    {
1359
        $currentUserPlugin = $this->plugin('currentUserPlugin');
1360
        $currentUser = $currentUserPlugin->getUser();
17167 ariadna 1361
 
1 www 1362
        $currentCompany = $currentUserPlugin->getCompany();
17167 ariadna 1363
 
1 www 1364
        $request = $this->getRequest();
17167 ariadna 1365
 
1366
 
1367
        if ($request->isPost()) {
1368
 
1 www 1369
            $uuid = $this->params()->fromRoute('id');
17167 ariadna 1370
            if (!$uuid) {
1 www 1371
                return new JsonModel([
1372
                    'success'   => false,
1373
                    'data'      => 'ERROR_INVALID_PARAMETER'
1374
                ]);
1375
            }
17167 ariadna 1376
 
1 www 1377
            $userMapper = UserMapper::getInstance($this->adapter);
1378
            $user = $userMapper->fetchOneByUuid($uuid);
17167 ariadna 1379
 
1380
            if (!$user) {
1 www 1381
                return new JsonModel([
1382
                    'success'   => false,
1383
                    'data'      => 'ERROR_USER_NOT_FOUND'
17167 ariadna 1384
 
1 www 1385
                ]);
1386
            }
17167 ariadna 1387
 
1 www 1388
            $companyUserMapper = CompanyUserMapper::getInstance($this->adapter);
1389
            $companyUser = $companyUserMapper->fetchOneByCompanyIdAndUserId($currentCompany->id, $user->id);
17167 ariadna 1390
 
1391
            if ($companyUser) {
1 www 1392
                return new JsonModel([
1393
                    'success'   => false,
1394
                    'data'      => 'ERROR_USER_COMPANY_NOT_FOUND'
1395
                ]);
1396
            }
17167 ariadna 1397
 
1398
 
1399
            if (!$currentCompany->internal) {
1 www 1400
                return new JsonModel([
1401
                    'success'   => false,
1402
                    'data'      => 'ERROR_INTERNAL_COMPANY_ONLY'
1403
                ]);
1404
            }
1405
 
1406
 
17167 ariadna 1407
 
1 www 1408
            $result = $companyUserMapper->delete($companyUser->id);
17167 ariadna 1409
            if ($result) {
1 www 1410
                $this->logger->info('La relación del usuario : ' . $user->email . ' con la empresa : ' . $currentCompany->name  . ' ha sido eliminada ', ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
17167 ariadna 1411
 
1 www 1412
                return new JsonModel([
1413
                    'success'   => true,
1414
                    'data'      => 'LABEL_USER_COMPANY_HAS_BEEN_DELETED',
1415
                ]);
17167 ariadna 1416
            } else {
1417
 
1 www 1418
                return new JsonModel([
1419
                    'success'   => false,
1420
                    'data'      => $userMapper->getError()
1421
                ]);
1422
            }
1423
        }
17167 ariadna 1424
 
1425
 
1426
 
1 www 1427
        return new JsonModel([
1428
            'success' => false,
1429
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
1430
        ]);
1431
    }
17167 ariadna 1432
 
1433
 
1 www 1434
    public function uploadAction()
1435
    {
1436
        $request = $this->getRequest();
17167 ariadna 1437
 
15459 efrain 1438
        $currentNetworkPlugin = $this->plugin('currentNetworkPlugin');
1439
        $currentNetwork    = $currentNetworkPlugin->getNetwork();
17167 ariadna 1440
 
1 www 1441
        $currentUserPlugin = $this->plugin('currentUserPlugin');
1442
        $currentUser    = $currentUserPlugin->getUser();
1443
        $currentCompany = $currentUserPlugin->getCompany();
17167 ariadna 1444
 
1 www 1445
        $request    = $this->getRequest();
1446
 
17167 ariadna 1447
        if ($request->isPost()) {
1448
 
16766 efrain 1449
            $step = Functions::sanitizeFilterString($this->params()->fromPost('step'));
17167 ariadna 1450
            if ($step == 'validation') {
1 www 1451
                $userMapper = UserMapper::getInstance($this->adapter);
1452
                $companyUserMapper = CompanyUserMapper::getInstance($this->adapter);
17167 ariadna 1453
 
1 www 1454
                $form = new  UserUploadForm();
1455
                $dataPost = array_merge($request->getPost()->toArray(), $request->getFiles()->toArray());
17167 ariadna 1456
 
1 www 1457
                $form->setData($dataPost);
17167 ariadna 1458
 
1459
                if ($form->isValid()) {
1460
 
1 www 1461
                    $file = $_FILES['file'];
1462
                    $tmp_filename = $file['tmp_name'];
1463
                    $final_filename =  'data/' . $file['name'];
17167 ariadna 1464
 
1465
                    if (!move_uploaded_file($tmp_filename, $final_filename)) {
1 www 1466
                        return new JsonModel([
1467
                            'success' => false,
1468
                            'data' => 'ERROR_UPLOAD_FILE'
1469
                        ]);
1470
                    }
17167 ariadna 1471
 
1472
 
1 www 1473
                    $users = [];
17167 ariadna 1474
 
1475
 
1 www 1476
                    $spreadsheet = IOFactory::load($final_filename);
1477
                    $records = $spreadsheet->getActiveSheet()->toArray(null, true, true, true);
17167 ariadna 1478
 
1 www 1479
                    $emails = [];
17167 ariadna 1480
 
1481
                    foreach ($records as $record) {
1 www 1482
                        //A = Nombre 	B = Apellidos	C = Email 	D = contraseña
17167 ariadna 1483
 
1484
 
16766 efrain 1485
                        $first_name = Functions::sanitizeFilterString($record['A']);
1486
                        $last_name = Functions::sanitizeFilterString($record['B']);
15460 efrain 1487
                        $email = trim(filter_var($record['C'], FILTER_SANITIZE_EMAIL));
17167 ariadna 1488
 
16766 efrain 1489
                        $password = Functions::sanitizeFilterString($record['D']);
1490
                        $isAdult = strtolower(Functions::sanitizeFilterString($record['E']));
1491
                        $country = strtolower(Functions::sanitizeFilterString($record['F']));
15460 efrain 1492
 
17167 ariadna 1493
 
1494
                        if (empty($first_name) || empty($last_name) || !filter_var($email, FILTER_VALIDATE_EMAIL) ||  empty($password)) {
1 www 1495
                            continue;
1496
                        }
17167 ariadna 1497
 
1498
                        if (!in_array($email, $emails)) {
1499
 
1 www 1500
                            $user = $userMapper->fetchOneByEmail($email);
17167 ariadna 1501
 
1 www 1502
                            array_push($emails, $email);
1503
                            array_push($users, [
1504
                                'first_name' => $first_name,
1505
                                'last_name' => $last_name,
1506
                                'password'  => $password,
1507
                                'email' => $email,
15460 efrain 1508
                                'is_adult' => $isAdult,
1509
                                'country' => $country,
1510
 
1 www 1511
                            ]);
1512
                        }
1513
                    }
17167 ariadna 1514
 
1 www 1515
                    $key = md5($currentUser->id . '-' . microtime(true));
1516
                    $this->cache->setItem($key, serialize($users));
17167 ariadna 1517
 
1 www 1518
                    return new JsonModel([
1519
                        'success' => true,
1520
                        'data' => [
1521
                            'key' => $key,
1522
                            'items' => $users,
1523
                        ]
1524
                    ]);
17167 ariadna 1525
 
16954 efrain 1526
                    @unlink($final_filename);
1 www 1527
                } else {
1528
                    $messages = [];
1529
                    $form_messages = (array) $form->getMessages();
17167 ariadna 1530
                    foreach ($form_messages  as $fieldname => $field_messages) {
1531
 
1 www 1532
                        $messages[$fieldname] = array_values($field_messages);
1533
                    }
17167 ariadna 1534
 
1 www 1535
                    return new JsonModel([
1536
                        'success'   => false,
1537
                        'data'   => $messages
1538
                    ]);
1539
                }
17167 ariadna 1540
            } else if ($step == 'process') {
1541
 
16766 efrain 1542
                $key = Functions::sanitizeFilterString($this->params()->fromPost('key'));
17167 ariadna 1543
                if (!$key) {
1 www 1544
                    return new JsonModel([
1545
                        'success' => false,
1546
                        'data' => 'ERROR_CACHE_KEY_EMPTY'
1547
                    ]);
1548
                }
17167 ariadna 1549
 
1 www 1550
                $value = $this->cache->getItem($key);
17167 ariadna 1551
                if (!$value) {
1552
 
1 www 1553
                    return new JsonModel([
1554
                        'success' => false,
1555
                        'data' => 'ERROR_CACHE_NOT_FOUND'
1556
                    ]);
1557
                }
17167 ariadna 1558
 
1 www 1559
                $records = unserialize($value);
17167 ariadna 1560
                if (!$records) {
1 www 1561
                    return new JsonModel([
1562
                        'success' => false,
1563
                        'data' => 'ERROR_CACHE_INVALID'
1564
                    ]);
1565
                }
17167 ariadna 1566
 
15460 efrain 1567
                $locationMapper = LocationMapper::getInstance($this->adapter);
1568
                $countryMapper = CountryMapper::getInstance($this->adapter);
17167 ariadna 1569
 
1570
 
15460 efrain 1571
                $networkMapper = NetworkMapper::getInstance($this->adapter);
1572
                $networkDefault = $networkMapper->fetchOneByDefault();
17167 ariadna 1573
 
1 www 1574
                $userMapper = UserMapper::getInstance($this->adapter);
1575
                $userPasswordMapper = UserPasswordMapper::getInstance($this->adapter);
1576
 
17167 ariadna 1577
 
15460 efrain 1578
                $user_ids_in_default_network = [];
17167 ariadna 1579
 
1580
 
1 www 1581
                $users_created = 0;
1582
                $user_ids = [];
17167 ariadna 1583
                foreach ($records as $record) {
1 www 1584
                    $first_name = $record['first_name'];
1585
                    $last_name = $record['last_name'];
1586
                    $password = $record['password'];
1587
                    $email = $record['email'];
17167 ariadna 1588
 
1589
 
16286 efrain 1590
                    $user = $userMapper->fetchOneByEmailAndNetworkId($email, $currentNetwork->id);
17167 ariadna 1591
                    if (!$user) {
1 www 1592
                        $password_hash = password_hash($password, PASSWORD_DEFAULT);
17167 ariadna 1593
 
1 www 1594
                        $user = new User();
15459 efrain 1595
                        $user->network_id = $currentNetwork->id;
1 www 1596
                        $user->blocked = User::BLOCKED_NO;
1597
                        $user->email_verified = User::EMAIL_VERIFIED_YES;
1598
                        $user->email = $email;
1599
                        $user->first_name = $first_name;
1600
                        $user->last_name = $last_name;
1601
                        $user->password = $password_hash;
1602
                        $user->login_attempt = 0;
1603
                        $user->usertype_id = UserType::USER;
1604
                        $user->status = User::STATUS_ACTIVE;
15460 efrain 1605
                        $user->is_adult = $record['is_adult'] == 'y' ? User::IS_ADULT_YES : User::IS_ADULT_NO;
17167 ariadna 1606
 
1 www 1607
                        $result = $userMapper->insert($user);
17167 ariadna 1608
                        if ($result) {
15460 efrain 1609
                            $users_created++;
17167 ariadna 1610
 
16286 efrain 1611
                            $country_code = trim($record['country']);
17167 ariadna 1612
 
1613
                            if ($country_code) {
16286 efrain 1614
                                $country = $countryMapper->fetchOneByCodeOrCountry($record['country']);
17167 ariadna 1615
                                if ($country) {
1616
 
16286 efrain 1617
                                    $location = new Location();
1618
                                    $location->formatted_address = $country->country;
1619
                                    $location->country = $country->country;
17167 ariadna 1620
                                    if ($locationMapper->insert($location)) {
1621
 
16286 efrain 1622
                                        $user->location_id = $location->id;
1623
                                        $userMapper->updateLocation($user);
1624
                                    }
15460 efrain 1625
                                }
16286 efrain 1626
                            } else {
1627
                                $country_code = '';
15460 efrain 1628
                            }
17167 ariadna 1629
 
1 www 1630
                            $userPassword = new UserPassword();
1631
                            $userPassword->user_id = $user->id;
1632
                            $userPassword->password = $password_hash;
1633
                            $userPasswordMapper->insert($userPassword);
17167 ariadna 1634
 
1635
 
1636
                            if ($currentNetwork->default == Network::DEFAULT_YES) {
15460 efrain 1637
                                array_push($user_ids_in_default_network, $user->id);
1638
                            } else {
17167 ariadna 1639
 
1640
 
1641
 
1642
                                if ($user->is_adult == User::IS_ADULT_YES) {
1643
 
15460 efrain 1644
                                    $userInDefaultNetwork = $userMapper->fetchOneByEmailAndNetworkId($user->email, $networkDefault->id);
17167 ariadna 1645
                                    if ($userInDefaultNetwork) {
1646
 
15460 efrain 1647
                                        array_push($user_ids_in_default_network, $userInDefaultNetwork->id);
17167 ariadna 1648
 
1649
                                        if ($userInDefaultNetwork->email_verified == User::EMAIL_VERIFIED_NO || $userInDefaultNetwork->status != User::STATUS_ACTIVE) {
15460 efrain 1650
                                            $userInDefaultNetwork->email_verified = User::EMAIL_VERIFIED_YES;
1651
                                            $userInDefaultNetwork->status != User::STATUS_ACTIVE;
17167 ariadna 1652
 
1653
                                            if (!$userMapper->update($userInDefaultNetwork)) {
15460 efrain 1654
                                                continue;
1655
                                            }
1656
                                        }
1657
                                    } else {
1658
                                        $userInDefaultNetwork = new User();
1659
                                        $userInDefaultNetwork->network_id = $networkDefault->id;
1660
                                        $userInDefaultNetwork->blocked = User::BLOCKED_NO;
1661
                                        $userInDefaultNetwork->email_verified = User::EMAIL_VERIFIED_YES;
1662
                                        $userInDefaultNetwork->email = $email;
1663
                                        $userInDefaultNetwork->first_name = $first_name;
1664
                                        $userInDefaultNetwork->last_name = $last_name;
1665
                                        $userInDefaultNetwork->password = $password_hash;
1666
                                        $userInDefaultNetwork->login_attempt = 0;
1667
                                        $userInDefaultNetwork->usertype_id = UserType::USER;
1668
                                        $userInDefaultNetwork->status = User::STATUS_ACTIVE;
1669
                                        $userInDefaultNetwork->is_adult = User::IS_ADULT_YES;
1670
                                        $result = $userMapper->insert($userInDefaultNetwork);
17167 ariadna 1671
                                        if ($result) {
15460 efrain 1672
                                            array_push($user_ids_in_default_network, $userInDefaultNetwork->id);
17167 ariadna 1673
 
1674
                                            if ($country) {
1675
 
15460 efrain 1676
                                                $location = new Location();
1677
                                                $location->formatted_address = $country->country;
1678
                                                $location->country = $country->country;
17167 ariadna 1679
                                                if ($locationMapper->insert($location)) {
1680
 
15460 efrain 1681
                                                    $userInDefaultNetwork->location_id = $location->id;
1682
                                                    $userMapper->updateLocation($userInDefaultNetwork);
1683
                                                }
1684
                                            }
17167 ariadna 1685
 
1686
 
15460 efrain 1687
                                            $userPassword = new UserPassword();
1688
                                            $userPassword->user_id = $userInDefaultNetwork->id;
1689
                                            $userPassword->password = $password_hash;
1690
                                            $userPasswordMapper->insert($userPassword);
1691
                                        }
1692
                                    }
1693
                                }
1694
                            }
1 www 1695
                        } else {
1696
                            continue;
1697
                        }
129 efrain 1698
                    } else {
17167 ariadna 1699
                        if ($user->email_verified == User::EMAIL_VERIFIED_NO || $user->status != User::STATUS_ACTIVE) {
129 efrain 1700
                            $user->email_verified = User::EMAIL_VERIFIED_YES;
1701
                            $user->status != User::STATUS_ACTIVE;
17167 ariadna 1702
 
1703
                            if (!$userMapper->update($user)) {
129 efrain 1704
                                continue;
1705
                            }
1706
                        }
1 www 1707
                    }
17167 ariadna 1708
 
15460 efrain 1709
                    array_push($user_ids, $user->id);
1710
                }
17167 ariadna 1711
 
1712
                if ($currentCompany) {
1713
 
15460 efrain 1714
                    $companyUserMapper = CompanyUserMapper::getInstance($this->adapter);
17167 ariadna 1715
 
15460 efrain 1716
                    $company_users_created = 0;
17167 ariadna 1717
 
1718
                    foreach ($user_ids as $user_id) {
15460 efrain 1719
                        $companyUser = $companyUserMapper->fetchOneByCompanyIdAndUserId($currentCompany->id, $user_id);
17167 ariadna 1720
                        if (!$companyUser) {
1721
 
1722
                            $companyUser = new CompanyUser();
1723
                            $companyUser->company_id = $currentCompany->id;
1724
                            $companyUser->user_id = $user_id;
15460 efrain 1725
                            $companyUser->backend = CompanyUser::BACKEND_NO;
1726
                            $companyUser->creator = CompanyUser::CREATOR_NO;
17167 ariadna 1727
                            $companyUser->owner = CompanyUser::OWNER_NO;
1728
 
1729
                            if ($currentNetwork->default == Network::DEFAULT_YES) {
15460 efrain 1730
                                $companyUser->status = CompanyUser::STATUS_ADMIN_WILL_ADD;
1731
                            } else {
1732
                                $companyUser->status = CompanyUser::STATUS_ACCEPTED;
1733
                            }
17167 ariadna 1734
 
1735
                            if ($companyUserMapper->insert($companyUser)) {
15460 efrain 1736
                                $company_users_created++;
1737
                            }
1738
                        }
1 www 1739
                    }
17167 ariadna 1740
 
15460 efrain 1741
                    $this->logger->info('Se agregaron ' . $users_created . '  usuarios  la empresa ' . $currentCompany->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
1742
                } else {
17167 ariadna 1743
 
15460 efrain 1744
                    $this->logger->info('Se agregaron ' . $users_created . ' usuarios a la red', ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
1 www 1745
                }
17167 ariadna 1746
 
1747
                if ($user_ids_in_default_network) {
15460 efrain 1748
                    $companyMapper = CompanyMapper::getInstance($this->adapter);
1749
                    $companyToFollower = $companyMapper->fetchOneDefaultForFollowers();
17167 ariadna 1750
 
15460 efrain 1751
                    $companyFollowerMapper = CompanyFollowerMapper::getInstance($this->adapter);
17167 ariadna 1752
 
15460 efrain 1753
                    $userToConnection = $userMapper->fetchOneDefaultForConnection();
17167 ariadna 1754
 
1755
 
15460 efrain 1756
                    $connectionMapper = ConnectionMapper::getInstance($this->adapter);
17167 ariadna 1757
 
1758
                    foreach ($user_ids_in_default_network as $user_id) {
1759
                        if ($userToConnection) {
15460 efrain 1760
                            $connection = $connectionMapper->fetchOneByUserId1AndUserId2($user_id, $userToConnection->id);
17167 ariadna 1761
                            if (!$connection) {
15460 efrain 1762
                                $connection = new Connection();
1763
                                $connection->request_from = $user_id;
1764
                                $connection->request_to = $userToConnection->id;
1765
                                $connection->status = Connection::STATUS_ACCEPTED;
17167 ariadna 1766
 
15460 efrain 1767
                                $connectionMapper->insert($connection);
1768
                            } else {
17167 ariadna 1769
                                if ($connection->status == Connection::STATUS_SENT) {
15460 efrain 1770
                                    $connectionMapper->approve($connection);
1771
                                }
1772
                            }
1773
                        }
17167 ariadna 1774
 
1775
                        if ($companyToFollower) {
15460 efrain 1776
                            $companyFollower = $companyFollowerMapper->fetchOneByCompanyIdAndUserId($companyToFollower->id, $user_id);
17167 ariadna 1777
                            if (!$companyFollower) {
15460 efrain 1778
                                $companyFollower = new CompanyFollower();
17167 ariadna 1779
                                $companyFollower->company_id = $companyToFollower->id;
15460 efrain 1780
                                $companyFollower->follower_id = $user_id;
17167 ariadna 1781
 
15460 efrain 1782
                                $companyFollowerMapper->insert($companyFollower);
1783
                            }
1 www 1784
                        }
1785
                    }
1786
                }
17167 ariadna 1787
 
1788
 
1789
 
1 www 1790
                return new JsonModel([
1791
                    'success' => true,
1792
                    'data' => [
1793
                        'users_created' => $users_created
1794
                    ]
1795
                ]);
1796
            } else {
1797
                return new JsonModel([
1798
                    'success' => false,
1799
                    'data' => 'ERROR_PARAMETERS_ARE_INVALID'
1800
                ]);
1801
            }
1802
        }
17167 ariadna 1803
 
1 www 1804
        return new JsonModel([
1805
            'success' => false,
1806
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
1807
        ]);
1808
    }
17167 ariadna 1809
 
1810
 
1 www 1811
    public function editAction()
1812
    {
1813
        $currentUserPlugin = $this->plugin('currentUserPlugin');
1814
        $currentUser = $currentUserPlugin->getUser();
1815
        $currentCompany = $currentUserPlugin->getCompany();
17167 ariadna 1816
 
1 www 1817
        $request = $this->getRequest();
1818
        $uuid = $this->params()->fromRoute('id');
17167 ariadna 1819
 
1820
 
1821
        if (!$uuid) {
1 www 1822
            $data = [
1823
                'success'   => false,
1824
                'data'   => 'ERROR_INVALID_PARAMETER'
1825
            ];
17167 ariadna 1826
 
1 www 1827
            return new JsonModel($data);
1828
        }
17167 ariadna 1829
 
1 www 1830
        $userMapper = UserMapper::getInstance($this->adapter);
1831
        $user = $userMapper->fetchOneByUuid($uuid);
17167 ariadna 1832
        if (!$user) {
1 www 1833
            $data = [
1834
                'success'   => false,
1835
                'data'   => 'ERROR_COMPANY_NOT_FOUND'
1836
            ];
17167 ariadna 1837
 
1 www 1838
            return new JsonModel($data);
1839
        }
17167 ariadna 1840
 
1 www 1841
        $companyUserMapper = CompanyUserMapper::getInstance($this->adapter);
1842
        $companyUser = $companyUserMapper->fetchOneByCompanyIdAndUserId($currentCompany->id, $user->id);
17167 ariadna 1843
        if (!$companyUser) {
1 www 1844
            return new JsonModel([
1845
                'success'   => false,
1846
                'data'   => 'ERROR_COMPANY_USER_NOT_FOUND'
1847
            ]);
1848
        }
17167 ariadna 1849
 
1850
        if ($companyUser->status != CompanyUser::STATUS_ACCEPTED && $companyUser->status != CompanyUser::STATUS_ADMIN_WILL_ADD) {
1 www 1851
            return new JsonModel([
1852
                'success'   => false,
1853
                'data'   => 'ERROR_COMPANY_USER_IS_NOT_ACTIVE'
1854
            ]);
1855
        }
17167 ariadna 1856
 
1857
 
1858
 
1859
 
1860
        if ($request->isPost()) {
1861
 
1 www 1862
            $companyRoleMapper = CompanyRoleMapper::getInstance($this->adapter);
1863
            $companyUserMapper = CompanyUserMapper::getInstance($this->adapter);
1864
            $companyUserRoleMapper = CompanyUserRoleMapper::getInstance($this->adapter);
17167 ariadna 1865
 
1866
            $backend = Functions::sanitizeFilterString($this->params()->fromPost('backend'));
1867
            $companyUser->backend = $backend == CompanyUser::BACKEND_YES ? CompanyUser::BACKEND_YES : CompanyUser::BACKEND_NO;
1 www 1868
            $companyUserMapper->update($companyUser);
17167 ariadna 1869
 
1870
 
1 www 1871
            $roleMapper = RoleMapper::getInstance($this->adapter);
1872
            $roles = $roleMapper->fetchAll();
17167 ariadna 1873
 
1874
 
1875
            foreach ($roles as $role) {
1 www 1876
                $companyRole = $companyRoleMapper->fetchOneByCompanyIdAndRoleId($currentCompany->id, $role->id);
17167 ariadna 1877
                if (!$companyRole) {
1878
                    $companyUserRoleMapper->deleteByCompanyIdAndRoleId($currentCompany->id, $role->id);
1 www 1879
                    continue;
17167 ariadna 1880
                }
1881
 
1882
                $checked     = filter_var($this->params()->fromPost('checked' . $role->id), FILTER_SANITIZE_NUMBER_INT);
1883
 
1884
 
1885
 
1886
                if ($checked) {
1887
 
1 www 1888
                    $companyUserRole = $companyUserRoleMapper->fetchOneByCompanyIdAndUserIdAndRoleId($currentCompany->id, $user->id, $role->id);
17167 ariadna 1889
                    if (!$companyUserRole) {
1890
 
1891
                        $companyUserRole = new CompanyUserRole();
1 www 1892
                        $companyUserRole->company_id = $currentCompany->id;
1893
                        $companyUserRole->role_id = $role->id;
1894
                        $companyUserRole->user_id = $user->id;
17167 ariadna 1895
 
1 www 1896
                        $companyUserRoleMapper->insert($companyUserRole);
1897
                    }
1898
                } else {
17167 ariadna 1899
 
1 www 1900
                    $companyUserRoleMapper->deleteByCompanyIdAndUserIdAndRoleId($currentCompany->id, $user->id, $role->id);
17167 ariadna 1901
                }
1 www 1902
            }
17167 ariadna 1903
 
1 www 1904
            $this->logger->info('Se actualizo los roles del usuario : ' . $user->email . ' en la empresa ' . $currentCompany->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
17167 ariadna 1905
 
1 www 1906
            return new JsonModel([
1907
                'success' => true,
1908
                'data' => 'LABEL_RECORD_UPDATED'
1909
            ]);
1910
        } else if ($request->isGet()) {
1911
            $companyServiceMapper = CompanyServiceMapper::getInstance($this->adapter);
17167 ariadna 1912
 
1 www 1913
            $roleMapper = RoleMapper::getInstance($this->adapter);
1914
            $records = $roleMapper->fetchAll();
4 efrain 1915
 
17167 ariadna 1916
 
1917
 
1 www 1918
            $companyRoleMapper = CompanyRoleMapper::getInstance($this->adapter);
1919
            $companyUserRoleMapper = CompanyUserRoleMapper::getInstance($this->adapter);
17167 ariadna 1920
 
1 www 1921
            $roles = [];
17167 ariadna 1922
            foreach ($records as $record) {
1923
                if ($record->creator == Role::CREATOR_YES) {
1 www 1924
                    continue;
1925
                }
17167 ariadna 1926
 
1927
                if ($record->service_id) {
1 www 1928
                    $companyService = $companyServiceMapper->fetchOneByCompanyIdAndServiceId($currentCompany->id, $record->service_id);
17167 ariadna 1929
                    if (!$companyService || $companyService->status == CompanyService::INACTIVE) {
1 www 1930
                        continue;
1931
                    }
1932
                }
17167 ariadna 1933
 
1934
 
1935
 
1 www 1936
                $companyRole = $companyRoleMapper->fetchOneByCompanyIdAndRoleId($currentCompany->id, $record->id);
17167 ariadna 1937
                if (!$companyRole) {
1 www 1938
                    continue;
1939
                }
17167 ariadna 1940
 
1 www 1941
                $companyUserRole  = $companyUserRoleMapper->fetchOneByCompanyIdAndUserIdAndRoleId($currentCompany->id, $user->id, $record->id);
17167 ariadna 1942
 
1943
 
1944
                $roles[$record->id] = [
1 www 1945
                    'id' => $record->id,
1946
                    'name' => $record->name,
1947
                    'fixed' => $record->creator == Role::CREATOR_YES ? true : false,
1948
                    'checked' => $companyUserRole ? true : false,
1949
                ];
1950
            }
1951
 
17167 ariadna 1952
 
1953
 
1 www 1954
            $data = [
1955
                'success' => true,
1956
                'data' => [
17167 ariadna 1957
                    'backend' => $companyUser->backend == CompanyUser::BACKEND_YES ? 1 : 0,
1958
                    'roles' => $roles,
1959
                ],
1 www 1960
            ];
17167 ariadna 1961
 
1962
 
1963
 
1 www 1964
            return new JsonModel($data);
1965
        } else {
1966
            $data = [
1967
                'success' => false,
1968
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
1969
            ];
17167 ariadna 1970
 
1 www 1971
            return new JsonModel($data);
1972
        }
17167 ariadna 1973
 
1 www 1974
        return new JsonModel($data);
1975
    }
17167 ariadna 1976
 
1977
 
16248 efrain 1978
    public function changeTypeAction()
1979
    {
1980
        $currentUserPlugin = $this->plugin('currentUserPlugin');
1981
        $currentUser = $currentUserPlugin->getUser();
17167 ariadna 1982
 
16248 efrain 1983
        $request = $this->getRequest();
17167 ariadna 1984
 
1985
        if ($request->isGet()) {
16248 efrain 1986
            $uuid = $this->params()->fromRoute('id');
17167 ariadna 1987
            if (!$uuid) {
16248 efrain 1988
                return new JsonModel([
1989
                    'success'   => false,
1990
                    'data'      => 'ERROR_INVALID_PARAMETER'
1991
                ]);
1992
            }
17167 ariadna 1993
 
16248 efrain 1994
            $userMapper = UserMapper::getInstance($this->adapter);
1995
            $user = $userMapper->fetchOneByUuid($uuid);
17167 ariadna 1996
 
1997
 
1998
 
1999
            if ($user) {
16248 efrain 2000
                return new JsonModel([
2001
                    'success'   => true,
2002
                    'data'      => [
2003
                        'usertype_id' => $user->usertype_id,
2004
                    ]
2005
                ]);
2006
            } else {
2007
                return new JsonModel([
2008
                    'success'   => false,
2009
                    'data'      => 'ERROR_USER_NOT_FOUND'
2010
                ]);
2011
            }
2012
        }
17167 ariadna 2013
 
2014
        if ($request->isPost()) {
2015
 
16248 efrain 2016
            $uuid = $this->params()->fromRoute('id');
17167 ariadna 2017
            if (!$uuid) {
16248 efrain 2018
                return new JsonModel([
2019
                    'success'   => false,
2020
                    'data'      => 'ERROR_INVALID_PARAMETER'
2021
                ]);
2022
            }
17167 ariadna 2023
 
16248 efrain 2024
            $userMapper = UserMapper::getInstance($this->adapter);
2025
            $user = $userMapper->fetchOneByUuid($uuid);
17167 ariadna 2026
 
2027
            if (!$user) {
16248 efrain 2028
                return new JsonModel([
2029
                    'success'   => false,
2030
                    'data'      => 'ERROR_USER_NOT_FOUND'
2031
                ]);
2032
            }
17167 ariadna 2033
 
2034
 
16248 efrain 2035
            $dataPost = $request->getPost()->toArray();
2036
            $form = new ChangeTypeForm();
2037
            $form->setData($dataPost);
17167 ariadna 2038
 
2039
            if ($form->isValid()) {
16248 efrain 2040
                $dataPost = (array) $form->getData();
17167 ariadna 2041
 
16248 efrain 2042
                $result = $userMapper->updateUserTypeId($user, $dataPost['usertype_id']);
17167 ariadna 2043
                if ($result) {
16248 efrain 2044
                    $this->logger->info('Cambio del tipo de usuario realizado por realizado', ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
17167 ariadna 2045
 
2046
 
16248 efrain 2047
                    return new JsonModel([
2048
                        'success'   => true,
2049
                        'data'      => 'LABEL_USER_CHANGE_TYPE_HAS_BEEN_UPDATED'
17167 ariadna 2050
 
16248 efrain 2051
                    ]);
2052
                } else {
2053
                    $this->logger->err('Cambio del tipo de usuario - error desconocido', ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
17167 ariadna 2054
 
16248 efrain 2055
                    return new JsonModel([
2056
                        'success'   => true,
2057
                        'data'      => 'ERROR_THERE_WAS_AN_ERROR'
17167 ariadna 2058
 
16248 efrain 2059
                    ]);
2060
                }
2061
            } else {
2062
                $messages = [];
17167 ariadna 2063
 
16248 efrain 2064
                $form_messages = (array) $form->getMessages();
17167 ariadna 2065
                foreach ($form_messages  as $fieldname => $field_messages) {
16248 efrain 2066
                    $messages[$fieldname] = array_values($field_messages);
2067
                }
17167 ariadna 2068
 
16248 efrain 2069
                return new JsonModel([
2070
                    'success'   => false,
2071
                    'data'   => $messages
2072
                ]);
2073
            }
2074
        }
17167 ariadna 2075
 
2076
 
2077
 
16248 efrain 2078
        return new JsonModel([
2079
            'success' => false,
2080
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
2081
        ]);
2082
    }
1 www 2083
}