Proyectos de Subversion LeadersLinked - Backend

Rev

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

Rev Autor Línea Nro. Línea
15397 efrain 1
<?php
15623 anderson 2
 
15397 efrain 3
declare(strict_types=1);
4
 
5
namespace LeadersLinked\Controller;
6
 
7
use Laminas\Db\Adapter\AdapterInterface;
8
use Laminas\Cache\Storage\Adapter\AbstractAdapter;
9
 
10
use Laminas\Mvc\Controller\AbstractActionController;
11
use Laminas\Log\LoggerInterface;
12
 
13
use Laminas\View\Model\ViewModel;
14
use Laminas\View\Model\JsonModel;
15
use LeadersLinked\Library\Functions;
16
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
17
use LeadersLinked\Model\DiscoveryContact;
18
use LeadersLinked\Mapper\DiscoveryContactMapper;
19
use LeadersLinked\Form\DiscoveryContact\ContactForm;
20
use LeadersLinked\Model\DiscoveryContactLog;
21
use LeadersLinked\Mapper\DiscoveryContactLogMapper;
22
use LeadersLinked\Form\DiscoveryContact\InteractionForm;
15546 efrain 23
use PhpOffice\PhpSpreadsheet\IOFactory;
24
use LeadersLinked\Form\DiscoveryContact\ContactUploadForm;
25
use LeadersLinked\Mapper\DiscoveryContactInteractionMapper;
26
use LeadersLinked\Mapper\DiscoveryContactInteractionTypeMapper;
27
use LeadersLinked\Model\DiscoveryContactInteraction;
15397 efrain 28
 
29
 
30
class DiscoveryContactController extends AbstractActionController
31
{
32
    /**
33
     *
34
     * @var AdapterInterface
35
     */
36
    private $adapter;
15623 anderson 37
 
38
 
15397 efrain 39
    /**
40
     *
41
     * @var AbstractAdapter
42
     */
43
    private $cache;
15623 anderson 44
 
15397 efrain 45
    /**
46
     *
47
     * @var  LoggerInterface
48
     */
49
    private $logger;
15623 anderson 50
 
15397 efrain 51
    /**
52
     *
53
     * @var array
54
     */
55
    private $config;
15623 anderson 56
 
15397 efrain 57
    /**
58
     *
59
     * @param AdapterInterface $adapter
60
     * @param AbstractAdapter $cache
61
     * @param LoggerInterface $logger
62
     * @param array $config
63
     */
15623 anderson 64
    public function __construct($adapter, $cache, $logger, $config)
15397 efrain 65
    {
66
        $this->adapter      = $adapter;
67
        $this->cache        = $cache;
68
        $this->logger       = $logger;
69
        $this->config       = $config;
70
    }
15623 anderson 71
 
15397 efrain 72
    public function indexAction()
73
    {
74
        $currentUserPlugin = $this->plugin('currentUserPlugin');
75
        $currentUser = $currentUserPlugin->getUser();
76
        $currentCompany = $currentUserPlugin->getCompany();
15623 anderson 77
 
15397 efrain 78
        $request = $this->getRequest();
15623 anderson 79
        if ($request->isGet()) {
80
 
81
 
15397 efrain 82
            $headers  = $request->getHeaders();
15623 anderson 83
 
15397 efrain 84
            $isJson = false;
15623 anderson 85
            if ($headers->has('Accept')) {
15397 efrain 86
                $accept = $headers->get('Accept');
15623 anderson 87
 
15397 efrain 88
                $prioritized = $accept->getPrioritized();
15623 anderson 89
 
90
                foreach ($prioritized as $key => $value) {
15397 efrain 91
                    $raw = trim($value->getRaw());
15623 anderson 92
 
93
                    if (!$isJson) {
15397 efrain 94
                        $isJson = strpos($raw, 'json');
95
                    }
96
                }
97
            }
15623 anderson 98
 
99
            if ($isJson) {
15397 efrain 100
                $search = $this->params()->fromQuery('search');
15623 anderson 101
                $search = empty($search['value']) ? '' : filter_var($search['value'], FILTER_SANITIZE_STRING);
15546 efrain 102
 
15623 anderson 103
 
104
 
15397 efrain 105
                $page               = intval($this->params()->fromQuery('start', 1), 10);
106
                $records_x_page     = intval($this->params()->fromQuery('length', 10), 10);
107
                $order =  $this->params()->fromQuery('order', []);
108
                $order_field        = empty($order[0]['column']) ? 99 :  intval($order[0]['column'], 10);
15623 anderson 109
                $order_direction    = empty($order[0]['dir']) ? 'ASC' : strtoupper(filter_var($order[0]['dir'], FILTER_SANITIZE_STRING));
110
 
15628 anderson 111
                $fields =  ['first_name', 'last_name', 'corporate_email', 'company', 'country', 'sector'];
15397 efrain 112
                $order_field = isset($fields[$order_field]) ? $fields[$order_field] : 'first_name';
15623 anderson 113
 
114
                if (!in_array($order_direction, ['ASC', 'DESC'])) {
15397 efrain 115
                    $order_direction = 'ASC';
116
                }
117
 
15623 anderson 118
 
119
 
15397 efrain 120
                $discoveryContactMapper = DiscoveryContactMapper::getInstance($this->adapter);
121
                $paginator = $discoveryContactMapper->fetchAllDataTableForCompanyId($search, $currentCompany->id, $page, $records_x_page, $order_field, $order_direction);
15623 anderson 122
 
15397 efrain 123
                $items = [];
124
                $records = $paginator->getCurrentItems();
15623 anderson 125
                foreach ($records as $record) {
15397 efrain 126
 
15623 anderson 127
 
15397 efrain 128
                    $item = [
129
                        'first_name' => $record->first_name,
130
                        'last_name' => $record->last_name,
131
                        'corporate_email' => $record->corporate_email,
15623 anderson 132
                        'company' => $record->company,
133
                        'country' => $record->country,
15628 anderson 134
                        'sector' => $record->sector,
15397 efrain 135
                        'actions' => [
15623 anderson 136
                            'link_edit' => $this->url()->fromRoute('discovery-contacts/edit', ['id' => $record->uuid]),
137
                            'link_delete' => $this->url()->fromRoute('discovery-contacts/delete', ['id' => $record->uuid]),
138
                            'link_view' => $this->url()->fromRoute('discovery-contacts/view', ['id' => $record->uuid]),
139
                        ],
15397 efrain 140
                    ];
15623 anderson 141
 
15397 efrain 142
                    array_push($items, $item);
143
                }
15623 anderson 144
 
15397 efrain 145
                return new JsonModel([
146
                    'success' => true,
147
                    'data' => [
148
                        'items' => $items,
149
                        'total' => $paginator->getTotalItemCount(),
150
                    ]
151
                ]);
152
            } else {
15750 anderson 153
                $exclude_id = 0;
154
                $form = new ContactForm($this->adapter, $currentCompany->id, $exclude_id);
155
                $formInteraction = new InteractionForm($this->adapter, $currentCompany->id);
156
                $contactUploadForm = new ContactUploadForm();
15623 anderson 157
 
158
 
15750 anderson 159
                $this->layout()->setTemplate('layout/layout-backend');
160
                $viewModel = new ViewModel();
161
                $viewModel->setTemplate('leaders-linked/discovery-contacts/index.phtml');
162
                $viewModel->setVariables([
163
                    'form' => $form,
164
                    'formInteraction' => $formInteraction,
165
                    'contactUploadForm' => $contactUploadForm,
166
                ]);
167
                return $viewModel;
15623 anderson 168
            }
15397 efrain 169
        } else {
170
            return new JsonModel([
171
                'success' => false,
172
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
173
            ]);;
174
        }
175
    }
15623 anderson 176
 
15397 efrain 177
    public function addAction()
178
    {
179
        $currentUserPlugin = $this->plugin('currentUserPlugin');
180
        $currentUser = $currentUserPlugin->getUser();
181
        $currentCompany = $currentUserPlugin->getCompany();
15623 anderson 182
 
15397 efrain 183
        $request = $this->getRequest();
15623 anderson 184
        if ($request->isPost()) {
15397 efrain 185
            $exclude_id = 0;
186
            $form = new ContactForm($this->adapter, $currentCompany->id, $exclude_id);
187
            $dataPost = $request->getPost()->toArray();
15623 anderson 188
 
15397 efrain 189
            $form->setData($dataPost);
15623 anderson 190
 
191
            if ($form->isValid()) {
15397 efrain 192
                $dataPost = (array) $form->getData();
193
 
194
                $hydrator = new ObjectPropertyHydrator();
195
                $discoveryContact = new DiscoveryContact();
196
                $hydrator->hydrate($dataPost, $discoveryContact);
15623 anderson 197
 
198
 
15397 efrain 199
                $discoveryContact->company_id = $currentCompany->id;
200
 
201
 
15623 anderson 202
 
15397 efrain 203
                $discoveryContactMapper = DiscoveryContactMapper::getInstance($this->adapter);
204
                $result = $discoveryContactMapper->insert($discoveryContact);
15623 anderson 205
 
206
                if ($result) {
207
                    $this->logger->info('Se agrego el Contacto : ' . $discoveryContact->first_name . ' ' . $discoveryContact->last_name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
208
 
15397 efrain 209
                    $discoveryContactLog = new DiscoveryContactLog();
210
                    $discoveryContactLog->company_id = $currentCompany->id;
211
                    $discoveryContactLog->contact_id = $discoveryContact->id;
212
                    $discoveryContactLog->user_id = $currentUser->id;
213
                    $discoveryContactLog->activity =  'LABEL_RECORD_CONTACT_ADDED';
15623 anderson 214
                    $discoveryContactLog->details = 'LABEL_FIRST_NAME : ' . $discoveryContact->first_name  . PHP_EOL .
215
                        'LABEL_LAST_NAME : ' . $discoveryContact->last_name  . PHP_EOL .
216
                        'LABEL_CORPORATE_EMAIL : ' . $discoveryContact->corporate_email  . PHP_EOL .
217
                        'LABEL_COMPANY : ' . $discoveryContact->company  . PHP_EOL .
218
                        'LABEL_POSITION : ' . $discoveryContact->position  . PHP_EOL .
219
                        'LABEL_COUNTRY : ' . $discoveryContact->country  . PHP_EOL;
220
 
221
 
222
                    if ($discoveryContact->state) {
15397 efrain 223
                        $discoveryContactLog->details .= 'LABEL_STATE : ' . $discoveryContact->state  . PHP_EOL;
224
                    }
15623 anderson 225
                    if ($discoveryContact->city) {
15397 efrain 226
                        $discoveryContactLog->details .= 'LABEL_CITY : ' . $discoveryContact->city  . PHP_EOL;
227
                    }
15623 anderson 228
                    if ($discoveryContact->phone) {
15397 efrain 229
                        $discoveryContactLog->details .= 'LABEL_PHONE : ' . $discoveryContact->phone  . PHP_EOL;
230
                    }
15623 anderson 231
                    if ($discoveryContact->phone_extension) {
15397 efrain 232
                        $discoveryContactLog->details .= 'LABEL_PHONE_EXTENSION : ' . $discoveryContact->phone_extension  . PHP_EOL;
233
                    }
15623 anderson 234
                    if ($discoveryContact->personal_email) {
15397 efrain 235
                        $discoveryContactLog->details .= 'LABEL_PERSONAL_EMAIL : ' . $discoveryContact->personal_email  . PHP_EOL;
236
                    }
15623 anderson 237
                    if ($discoveryContact->celular) {
15397 efrain 238
                        $discoveryContactLog->details .= 'LABEL_CELULAR : ' . $discoveryContact->celular  . PHP_EOL;
239
                    }
15623 anderson 240
                    if ($discoveryContact->whatsapp) {
15397 efrain 241
                        $discoveryContactLog->details .= 'LABEL_WHATSAPP : ' . $discoveryContact->whatsapp  . PHP_EOL;
242
                    }
15623 anderson 243
                    if ($discoveryContact->linkedin) {
15397 efrain 244
                        $discoveryContactLog->details .= 'LABEL_LINKEDIN : ' . $discoveryContact->linkedin  . PHP_EOL;
245
                    }
15623 anderson 246
 
15397 efrain 247
                    $discoveryContactLogMapper = DiscoveryContactLogMapper::getInstance($this->adapter);
248
                    $discoveryContactLogMapper->insert($discoveryContactLog);
15623 anderson 249
 
15397 efrain 250
                    $data = [
251
                        'success'   => true,
252
                        'data'   => 'LABEL_RECORD_ADDED'
253
                    ];
254
                } else {
255
                    $data = [
256
                        'success'   => false,
257
                        'data'      => $discoveryContactMapper->getError()
258
                    ];
259
                }
15623 anderson 260
 
15397 efrain 261
                return new JsonModel($data);
262
            } else {
263
                $messages = [];
264
                $form_messages = (array) $form->getMessages();
15623 anderson 265
                foreach ($form_messages  as $fieldname => $field_messages) {
266
 
15397 efrain 267
                    $messages[$fieldname] = array_values($field_messages);
268
                }
15623 anderson 269
 
15397 efrain 270
                return new JsonModel([
271
                    'success'   => false,
272
                    'data'   => $messages
273
                ]);
274
            }
275
        } else {
276
            $data = [
277
                'success' => false,
278
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
279
            ];
15623 anderson 280
 
15397 efrain 281
            return new JsonModel($data);
282
        }
15623 anderson 283
 
15397 efrain 284
        return new JsonModel($data);
285
    }
15623 anderson 286
 
15397 efrain 287
    public function editAction()
288
    {
289
        $currentUserPlugin = $this->plugin('currentUserPlugin');
290
        $currentUser = $currentUserPlugin->getUser();
291
        $currentCompany = $currentUserPlugin->getCompany();
15623 anderson 292
 
15397 efrain 293
        $request = $this->getRequest();
294
        $uuid = $this->params()->fromRoute('id');
295
 
15623 anderson 296
 
297
        if (!$uuid) {
15397 efrain 298
            $data = [
299
                'success'   => false,
300
                'data'   => 'ERROR_INVALID_PARAMETER'
301
            ];
15623 anderson 302
 
15397 efrain 303
            return new JsonModel($data);
304
        }
305
 
306
        $discoveryContactMapper = DiscoveryContactMapper::getInstance($this->adapter);
307
        $discoveryContact = $discoveryContactMapper->fetchOneByUuid($uuid);
15623 anderson 308
        if (!$discoveryContact) {
15397 efrain 309
            $data = [
310
                'success'   => false,
311
                'data'   => 'ERROR_RECORD_NOT_FOUND'
312
            ];
15623 anderson 313
 
15397 efrain 314
            return new JsonModel($data);
315
        }
15623 anderson 316
 
15397 efrain 317
        $discoveryContactOld =  clone $discoveryContact;
318
 
15623 anderson 319
        if ($request->isPost()) {
320
 
15397 efrain 321
            $form = new ContactForm($this->adapter, $currentCompany->id, $discoveryContact->id);
322
            $dataPost = $request->getPost()->toArray();
323
 
324
            $form->setData($dataPost);
15623 anderson 325
 
326
            if ($form->isValid()) {
15397 efrain 327
                $dataPost = (array) $form->getData();
15623 anderson 328
 
15397 efrain 329
                $hydrator = new ObjectPropertyHydrator();
330
                $hydrator->hydrate($dataPost, $discoveryContact);
331
                $result = $discoveryContactMapper->update($discoveryContact);
15623 anderson 332
 
333
                if ($result) {
15397 efrain 334
                    $this->logger->info('Se actualizo el Contacto ' . $discoveryContact->first_name . ' ' . $discoveryContact->last_name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
15623 anderson 335
 
336
 
15397 efrain 337
                    $discoveryContactLog = new DiscoveryContactLog();
338
                    $discoveryContactLog->company_id = $currentCompany->id;
339
                    $discoveryContactLog->contact_id = $discoveryContact->id;
340
                    $discoveryContactLog->user_id = $currentUser->id;
341
                    $discoveryContactLog->activity =  'LABEL_RECORD_CONTACT_UPDATED';
342
                    $discoveryContactLog->details = '';
15623 anderson 343
 
344
                    if ($discoveryContactOld->first_name  != $discoveryContact->first_name) {
15397 efrain 345
                        $discoveryContactLog->details .= 'LABEL_FIRST_NAME : ' . $discoveryContactOld->first_name . ' / ' . $discoveryContact->first_name  . PHP_EOL;
346
                    }
15623 anderson 347
                    if ($discoveryContactOld->last_name  !=  $discoveryContact->last_name) {
348
                        $discoveryContactLog->details .= 'LABEL_LAST_NAME : ' . $discoveryContactOld->last_name . ' / ' . $discoveryContact->last_name  . PHP_EOL;
15397 efrain 349
                    }
15623 anderson 350
                    if ($discoveryContactOld->corporate_email != $discoveryContact->corporate_email) {
351
                        $discoveryContactLog->details .= 'LABEL_CORPORATE_EMAIL : ' . $discoveryContactOld->corporate_email . ' / ' . $discoveryContact->corporate_email  . PHP_EOL;
15397 efrain 352
                    }
15623 anderson 353
                    if ($discoveryContactOld->company != $discoveryContact->company) {
354
                        $discoveryContactLog->details .= 'LABEL_COMPANY : ' . $discoveryContactOld->company . ' / ' . $discoveryContact->company  . PHP_EOL;
15397 efrain 355
                    }
15623 anderson 356
                    if ($discoveryContactOld->position != $discoveryContact->position) {
357
                        $discoveryContactLog->details .= 'LABEL_POSITION : ' . $discoveryContactOld->position . ' / ' . $discoveryContact->position  . PHP_EOL;
15397 efrain 358
                    }
15623 anderson 359
                    if ($discoveryContactOld->country != $discoveryContact->country) {
15397 efrain 360
                        $discoveryContactLog->details .= 'LABEL_COUNTRY : ' . $discoveryContactOld->country . ' / ' . $discoveryContact->country  . PHP_EOL;
361
                    }
15623 anderson 362
                    if ($discoveryContactOld->state != $discoveryContact->state) {
15397 efrain 363
                        $discoveryContactLog->details .= 'LABEL_STATE : ' . $discoveryContactOld->state . ' / ' . $discoveryContact->state  . PHP_EOL;
364
                    }
15623 anderson 365
                    if ($discoveryContactOld->city != $discoveryContact->city) {
15397 efrain 366
                        $discoveryContactLog->details .= 'LABEL_CITY : ' . $discoveryContactOld->city . ' / ' . $discoveryContact->city  . PHP_EOL;
367
                    }
15623 anderson 368
                    if ($discoveryContactOld->phone != $discoveryContact->phone) {
369
                        $discoveryContactLog->details .= 'LABEL_PHONE : ' . $discoveryContactOld->phone . ' / ' . $discoveryContact->phone  . PHP_EOL;
15397 efrain 370
                    }
15623 anderson 371
 
372
                    if ($discoveryContactOld->phone_extension != $discoveryContact->phone_extension) {
15397 efrain 373
                        $discoveryContactLog->details .= 'LABEL_PHONE_EXTENSION : ' . $discoveryContactOld->phone_extension . ' / ' . $discoveryContact->phone_extension  . PHP_EOL;
374
                    }
15623 anderson 375
                    if ($discoveryContactOld->personal_email != $discoveryContact->personal_email) {
15397 efrain 376
                        $discoveryContactLog->details .= 'LABEL_PERSONAL_EMAIL : ' . $discoveryContactOld->personal_email . ' / ' . $discoveryContact->personal_email  . PHP_EOL;
377
                    }
15623 anderson 378
                    if ($discoveryContactOld->celular  !=  $discoveryContact->celular) {
15397 efrain 379
                        $discoveryContactLog->details .= 'LABEL_CELULAR : ' . $discoveryContactOld->celular . ' / ' . $discoveryContact->celular  . PHP_EOL;
380
                    }
15623 anderson 381
                    if ($discoveryContactOld->whatsapp != $discoveryContact->whatsapp) {
382
                        $discoveryContactLog->details .= 'LABEL_WHATSAPP : ' . $discoveryContactOld->whatsapp . ' / ' . $discoveryContact->whatsapp  . PHP_EOL;
15397 efrain 383
                    }
15623 anderson 384
                    if ($discoveryContactOld->linkedin != $discoveryContact->linkedin) {
15397 efrain 385
                        $discoveryContactLog->details .= 'LABEL_LINKEDIN : ' . $discoveryContactOld->linkedin . ' / ' . $discoveryContact->linkedin  . PHP_EOL;
386
                    }
387
                    $discoveryContactLogMapper = DiscoveryContactLogMapper::getInstance($this->adapter);
388
                    $discoveryContactLogMapper->insert($discoveryContactLog);
15623 anderson 389
 
390
 
15397 efrain 391
                    $data = [
392
                        'success' => true,
393
                        'data' => 'LABEL_RECORD_UPDATED'
394
                    ];
395
                } else {
396
                    $data = [
397
                        'success'   => false,
398
                        'data'      => $discoveryContactMapper->getError()
399
                    ];
400
                }
15623 anderson 401
 
15397 efrain 402
                return new JsonModel($data);
403
            } else {
404
                $messages = [];
405
                $form_messages = (array) $form->getMessages();
15623 anderson 406
                foreach ($form_messages  as $fieldname => $field_messages) {
15397 efrain 407
                    $messages[$fieldname] = array_values($field_messages);
408
                }
15623 anderson 409
 
15397 efrain 410
                return new JsonModel([
411
                    'success'   => false,
412
                    'data'   => $messages
413
                ]);
414
            }
415
        } else if ($request->isGet()) {
416
 
15623 anderson 417
 
15397 efrain 418
            $hydrator = new ObjectPropertyHydrator();
419
            $data = $hydrator->extract($discoveryContact);
15623 anderson 420
 
15397 efrain 421
            $data['first_name'] = $data['first_name'] ? trim($data['first_name']) : '';
422
            $data['last_name'] = $data['last_name'] ? trim($data['last_name']) : '';
423
            $data['corporate_email'] = $data['corporate_email'] ? trim($data['corporate_email']) : '';
424
            $data['company'] = $data['company'] ? trim($data['company']) : '';
425
            $data['position'] = $data['position'] ? trim($data['position']) : '';
426
            $data['country'] = $data['country'] ? trim($data['country']) : '';
427
            $data['state'] = $data['state'] ? trim($data['state']) : '';
428
            $data['city'] = $data['city'] ? trim($data['city']) : '';
429
            $data['phone'] = $data['phone'] ? trim($data['phone']) : '';
430
            $data['phone_extension'] = $data['phone_extension'] ? trim($data['phone_extension']) : '';
431
            $data['personal_email'] = $data['personal_email'] ? trim($data['personal_email']) : '';
432
            $data['celular'] = $data['celular'] ? trim($data['celular']) : '';
433
            $data['whatsapp'] = $data['whatsapp'] ? trim($data['whatsapp']) : '';
434
            $data['linkedin'] = $data['linkedin'] ? trim($data['linkedin']) : '';
15623 anderson 435
 
436
 
437
 
438
 
15397 efrain 439
            $result = [
440
                'success' => true,
441
                'data' =>  $data
442
            ];
15623 anderson 443
 
15397 efrain 444
            return new JsonModel($result);
445
        } else {
446
            $data = [
447
                'success' => false,
448
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
449
            ];
15623 anderson 450
 
15397 efrain 451
            return new JsonModel($data);
452
        }
15623 anderson 453
 
15397 efrain 454
        return new JsonModel($data);
455
    }
15623 anderson 456
 
15397 efrain 457
    public function deleteAction()
458
    {
459
        $currentUserPlugin = $this->plugin('currentUserPlugin');
460
        $currentUser = $currentUserPlugin->getUser();
15623 anderson 461
 
15397 efrain 462
        $request = $this->getRequest();
463
        $uuid = $this->params()->fromRoute('id');
15623 anderson 464
 
465
        if (!$uuid) {
15397 efrain 466
            $data = [
467
                'success'   => false,
468
                'data'   => 'ERROR_INVALID_PARAMETER'
469
            ];
15623 anderson 470
 
15397 efrain 471
            return new JsonModel($data);
472
        }
473
 
474
        $discoveryContactMapper = DiscoveryContactMapper::getInstance($this->adapter);
475
        $discoveryContact = $discoveryContactMapper->fetchOneByUuid($uuid);
15623 anderson 476
        if (!$discoveryContact) {
15397 efrain 477
            $data = [
478
                'success'   => false,
479
                'data'   => 'ERROR_RECORD_NOT_FOUND'
480
            ];
15623 anderson 481
 
15397 efrain 482
            return new JsonModel($data);
483
        }
15623 anderson 484
 
485
        if ($request->isPost()) {
15397 efrain 486
            $result = $discoveryContactMapper->delete($discoveryContact);
15623 anderson 487
            if ($result) {
15397 efrain 488
                $this->logger->info('Se borro el Contacto : ' . $discoveryContact->first_name . ' ' . $discoveryContact->last_name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
15623 anderson 489
 
15397 efrain 490
                $data = [
491
                    'success' => true,
492
                    'data' => 'LABEL_RECORD_DELETED'
493
                ];
494
            } else {
495
 
496
                $data = [
497
                    'success'   => false,
498
                    'data'      => $discoveryContactMapper->getError()
499
                ];
500
 
501
                return new JsonModel($data);
502
            }
503
        } else {
504
            $data = [
505
                'success' => false,
506
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
507
            ];
15623 anderson 508
 
15397 efrain 509
            return new JsonModel($data);
510
        }
15623 anderson 511
 
15397 efrain 512
        return new JsonModel($data);
513
    }
15623 anderson 514
 
15397 efrain 515
    public function viewAction()
516
    {
517
        $currentUserPlugin = $this->plugin('currentUserPlugin');
518
        $currentUser = $currentUserPlugin->getUser();
519
        $currentCompany = $currentUserPlugin->getCompany();
15623 anderson 520
 
15397 efrain 521
        $request = $this->getRequest();
522
        $uuid = $this->params()->fromRoute('id');
15623 anderson 523
 
524
 
525
        if (!$uuid) {
15397 efrain 526
            $data = [
527
                'success'   => false,
528
                'data'   => 'ERROR_INVALID_PARAMETER'
529
            ];
15623 anderson 530
 
15397 efrain 531
            return new JsonModel($data);
532
        }
15623 anderson 533
 
15397 efrain 534
        $discoveryContactMapper = DiscoveryContactMapper::getInstance($this->adapter);
535
        $discoveryContact = $discoveryContactMapper->fetchOneByUuid($uuid);
15623 anderson 536
        if (!$discoveryContact) {
15397 efrain 537
            $data = [
538
                'success'   => false,
539
                'data'   => 'ERROR_RECORD_NOT_FOUND'
540
            ];
15623 anderson 541
 
15397 efrain 542
            return new JsonModel($data);
543
        }
544
 
15623 anderson 545
 
546
 
15397 efrain 547
        if ($request->isGet()) {
15623 anderson 548
 
549
 
15397 efrain 550
            $hydrator = new ObjectPropertyHydrator();
551
            $data = $hydrator->extract($discoveryContact);
15623 anderson 552
 
15397 efrain 553
            $data['first_name'] = $data['first_name'] ? trim($data['first_name']) : '';
554
            $data['last_name'] = $data['last_name'] ? trim($data['last_name']) : '';
555
            $data['corporate_email'] = $data['corporate_email'] ? trim($data['corporate_email']) : '';
556
            $data['company'] = $data['company'] ? trim($data['company']) : '';
557
            $data['position'] = $data['position'] ? trim($data['position']) : '';
558
            $data['country'] = $data['country'] ? trim($data['country']) : '';
559
            $data['state'] = $data['state'] ? trim($data['state']) : '';
560
            $data['city'] = $data['city'] ? trim($data['city']) : '';
561
            $data['phone'] = $data['phone'] ? trim($data['phone']) : '';
562
            $data['phone_extension'] = $data['phone_extension'] ? trim($data['phone_extension']) : '';
563
            $data['personal_email'] = $data['personal_email'] ? trim($data['personal_email']) : '';
564
            $data['celular'] = $data['celular'] ? trim($data['celular']) : '';
565
            $data['whatsapp'] = $data['whatsapp'] ? trim($data['whatsapp']) : '';
566
            $data['linkedin'] = $data['linkedin'] ? trim($data['linkedin']) : '';
15623 anderson 567
 
568
 
15397 efrain 569
            $data['link_interactions'] = $this->url()->fromRoute('discovery-contacts/interactions', ['id' => $discoveryContact->uuid]);
570
            $data['link_interactions_add'] = $this->url()->fromRoute('discovery-contacts/interactions/add', ['id' => $discoveryContact->uuid]);
571
            $data['link_logs'] = $this->url()->fromRoute('discovery-contacts/logs', ['id' => $discoveryContact->uuid]);
572
 
15623 anderson 573
 
574
 
15397 efrain 575
            $result = [
576
                'success' => true,
577
                'data' =>  $data
578
            ];
15623 anderson 579
 
15397 efrain 580
            return new JsonModel($result);
581
        } else {
582
            $data = [
583
                'success' => false,
584
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
585
            ];
15623 anderson 586
 
15397 efrain 587
            return new JsonModel($data);
588
        }
15623 anderson 589
 
15397 efrain 590
        return new JsonModel($data);
591
    }
15623 anderson 592
 
593
 
15546 efrain 594
    public function uploadAction()
595
    {
596
        $request = $this->getRequest();
15623 anderson 597
 
15546 efrain 598
        $currentUserPlugin = $this->plugin('currentUserPlugin');
599
        $currentUser    = $currentUserPlugin->getUser();
600
        $currentCompany = $currentUserPlugin->getCompany();
15623 anderson 601
 
15546 efrain 602
        $currentNetworkPlugin = $this->plugin('currentNetworkPlugin');
603
        $currentNetwork = $currentNetworkPlugin->getNetwork();
15623 anderson 604
 
15546 efrain 605
        $request    = $this->getRequest();
606
 
15623 anderson 607
 
608
 
609
 
610
        if ($request->isPost()) {
611
 
612
            $step = filter_var($this->params()->fromPost('step'), FILTER_SANITIZE_STRING);
613
            if ($step == 'validation') {
614
 
615
 
616
 
15546 efrain 617
                $form = new  ContactUploadForm();
618
                $dataPost = array_merge($request->getPost()->toArray(), $request->getFiles()->toArray());
15623 anderson 619
 
15546 efrain 620
                $form->setData($dataPost);
15623 anderson 621
 
622
                if ($form->isValid()) {
623
 
15546 efrain 624
                    $file = $_FILES['file'];
625
                    $tmp_filename = $file['tmp_name'];
626
                    $final_filename =  'data/' . $file['name'];
15623 anderson 627
 
628
                    if (!move_uploaded_file($tmp_filename, $final_filename)) {
15546 efrain 629
                        return new JsonModel([
630
                            'success' => false,
631
                            'data' => 'ERROR_UPLOAD_FILE'
632
                        ]);
633
                    }
15623 anderson 634
 
635
 
15546 efrain 636
                    $emails = [];
637
                    $contacts = [];
638
                    $spreadsheet = IOFactory::load($final_filename);
639
                    $records = $spreadsheet->getActiveSheet()->toArray(null, true, true, true);
15623 anderson 640
 
641
                    foreach ($records as $record) {
15546 efrain 642
                        /*
643
                        A = Nombre
644
                        B = Apellido
645
                        C = Correo Personal
646
                        D = Correo Trabajo
647
                        E = Empresa
648
                        F = Puesto
649
                        G = Pais
650
                        H = Estado
651
                        I = Ciudad
652
                        J = Telefono
653
                        K = Extensión
654
                        L = Celular
655
                        M = Whatsapp
656
                        N = Linkedin
15831 efrain 657
                        O = Sector
15546 efrain 658
 
659
                         */
15623 anderson 660
 
661
 
15546 efrain 662
                        $first_name = trim(filter_var($record['A'], FILTER_SANITIZE_STRING));
663
                        $last_name = trim(filter_var($record['B'], FILTER_SANITIZE_STRING));
664
                        $email_personal = trim(filter_var($record['C'], FILTER_SANITIZE_EMAIL));
665
                        $email_company = trim(filter_var($record['D'], FILTER_SANITIZE_EMAIL));
15623 anderson 666
 
15546 efrain 667
                        $company =  isset($record['E']) ? trim(filter_var($record['E'], FILTER_SANITIZE_STRING)) : '';
668
                        $position = isset($record['F']) ? trim(filter_var($record['F'], FILTER_SANITIZE_STRING)) : '';
669
                        $country = isset($record['G']) ? trim(filter_var($record['G'], FILTER_SANITIZE_STRING)) : '';
670
                        $state = isset($record['H']) ? trim(filter_var($record['H'], FILTER_SANITIZE_STRING)) : '';
671
                        $city = isset($record['I']) ? trim(filter_var($record['I'], FILTER_SANITIZE_STRING)) : '';
15623 anderson 672
 
673
 
15546 efrain 674
                        $phone = isset($record['J']) ? trim(filter_var($record['J'], FILTER_SANITIZE_STRING)) : '';
15623 anderson 675
                        $phone = preg_replace('/[^0-9]/', '', $phone);
676
 
15546 efrain 677
                        $extension = isset($record['K']) ? trim(filter_var($record['K'], FILTER_SANITIZE_STRING)) : '';
15623 anderson 678
                        $extension = preg_replace('/[^0-9]/', '', $extension);
679
 
15546 efrain 680
                        $movil = isset($record['L']) ? trim(filter_var($record['L'], FILTER_SANITIZE_STRING)) : '';
15623 anderson 681
                        $movil = preg_replace('/[^0-9]/', '', $movil);
682
 
15546 efrain 683
                        $whatsapp = isset($record['M']) ? trim(filter_var($record['M'], FILTER_SANITIZE_STRING)) : '';
15623 anderson 684
                        $whatsapp = preg_replace('/[^0-9]/', '', $whatsapp);
685
 
686
 
15546 efrain 687
                        $linkedin = isset($record['N']) ? trim(filter_var($record['N'], FILTER_SANITIZE_URL)) : '';
15831 efrain 688
                        $sector = isset($record['O']) ? trim(filter_var($record['O'], FILTER_SANITIZE_STRING)) : '';
15546 efrain 689
 
690
                        //||  empty($password)
15623 anderson 691
                        if (empty($first_name) || empty($last_name) || !filter_var($email_company, FILTER_VALIDATE_EMAIL)) {
15546 efrain 692
                            continue;
693
                        }
15623 anderson 694
 
695
                        if (!in_array($email_company, $emails)) {
696
 
15546 efrain 697
                            array_push($emails, $email_company);
15623 anderson 698
 
15546 efrain 699
                            array_push($contacts, [
15623 anderson 700
                                'first_name' =>   $first_name,
15546 efrain 701
                                'last_name' => $last_name,
702
                                'email_personal' => $email_personal,
703
                                'email_company' => $email_company,
15623 anderson 704
                                'company' => $company,
15546 efrain 705
                                'position' => $position,
706
                                'country' => $country,
707
                                'state' => $state,
708
                                'city' => $city,
709
                                'phone' => $phone,
710
                                'extension' => $extension,
711
                                'movil' => $movil,
712
                                'whatsapp' =>  $whatsapp,
713
                                'linkedin' => $linkedin,
15831 efrain 714
                                'sector' => $sector,
15546 efrain 715
                            ]);
716
                        }
717
                    }
15623 anderson 718
 
719
 
720
                    $key = md5($currentUser->id . '-discovery-contacts-' . uniqid());
15546 efrain 721
                    $this->cache->setItem($key, serialize($contacts));
15623 anderson 722
 
15546 efrain 723
                    return new JsonModel([
724
                        'success' => true,
725
                        'data' => [
726
                            'key' => $key,
727
                            'items' => $contacts,
728
                        ]
729
                    ]);
730
                } else {
731
                    $messages = [];
732
                    $form_messages = (array) $form->getMessages();
15623 anderson 733
                    foreach ($form_messages  as $fieldname => $field_messages) {
734
 
15546 efrain 735
                        $messages[$fieldname] = array_values($field_messages);
736
                    }
15623 anderson 737
 
15546 efrain 738
                    return new JsonModel([
739
                        'success'   => false,
740
                        'data'   => $messages
741
                    ]);
742
                }
15623 anderson 743
            } else if ($step == 'process') {
744
 
745
                $key = filter_var($this->params()->fromPost('key'), FILTER_SANITIZE_STRING);
746
                if (!$key) {
15546 efrain 747
                    return new JsonModel([
748
                        'success' => false,
749
                        'data' => 'ERROR_CACHE_KEY_EMPTY'
750
                    ]);
751
                }
15623 anderson 752
 
15546 efrain 753
                $value = $this->cache->getItem($key);
15623 anderson 754
                if (!$value) {
755
 
15546 efrain 756
                    return new JsonModel([
757
                        'success' => false,
758
                        'data' => 'ERROR_CACHE_NOT_FOUND'
759
                    ]);
760
                }
15623 anderson 761
 
15546 efrain 762
                $records = unserialize($value);
15623 anderson 763
                if (!$records) {
15546 efrain 764
                    return new JsonModel([
765
                        'success' => false,
766
                        'data' => 'ERROR_CACHE_INVALID'
767
                    ]);
768
                }
15623 anderson 769
 
770
 
771
                $discoveryContactMapper = DiscoveryContactMapper::getInstance($this->adapter);
772
                $discoveryContactInteractionTypeMapper = DiscoveryContactInteractionTypeMapper::getInstance($this->adapter);
15546 efrain 773
                $discoveryContactInteractionType = $discoveryContactInteractionTypeMapper->fetchOneFirstActiveByCompanyId($currentCompany->id);
15623 anderson 774
 
775
                $discoveryContactInteractionMapper = DiscoveryContactInteractionMapper::getInstance($this->adapter);
776
 
777
 
778
 
15546 efrain 779
                $new_contacts  = 0;
780
                $duplicate_contacts = 0;
781
                $error_contacts = 0;
15623 anderson 782
                foreach ($records as $record) {
15546 efrain 783
                    $first_name =   $record['first_name'];
784
                    $last_name = $record['last_name'];
785
                    $email_personal = $record['email_personal'];
786
                    $email_company = $record['email_company'];
787
                    $company = $record['company'];
788
                    $position = $record['position'];
789
                    $country = $record['country'];
790
                    $state = $record['state'];
791
                    $city = $record['city'];
792
                    $phone = $record['phone'];
793
                    $extension = $record['extension'];
794
                    $movil = $record['movil'];
795
                    $whatsapp =  $record['whatsapp'];
796
                    $linkedin = $record['linkedin'];
15831 efrain 797
                    $sector = $record['sector'];
798
 
799
 
15546 efrain 800
                    $discoveryContact = $discoveryContactMapper->fetchOneByCorporateEmail($email_company);
15623 anderson 801
                    if ($discoveryContact) {
15546 efrain 802
                        $duplicate_contacts++;
803
                    } else {
15623 anderson 804
 
15546 efrain 805
                        $discoveryContact = new DiscoveryContact();
806
                        $discoveryContact->company_id = $currentCompany->id;
807
                        $discoveryContact->first_name = $first_name;
808
                        $discoveryContact->last_name = $last_name;
809
                        $discoveryContact->corporate_email = $email_company;
810
                        $discoveryContact->personal_email = $email_personal;
811
                        $discoveryContact->company = $company;
812
                        $discoveryContact->position = $position;
15831 efrain 813
                        $discoveryContact->sector = $sector;
15546 efrain 814
                        $discoveryContact->country = $country;
815
                        $discoveryContact->state = $state;
816
                        $discoveryContact->city = $city;
817
                        $discoveryContact->phone = empty($phone) ? '' : '+' . $phone;
818
                        $discoveryContact->phone_extension = $extension;
819
                        $discoveryContact->celular = empty($movil) ? '' : '+' . $movil;
820
                        $discoveryContact->whatsapp = empty($whatsapp) ? '' : '+' . $whatsapp;
821
                        $discoveryContact->linkedin = $linkedin;
15831 efrain 822
 
823
 
824
                        if($discoveryContactMapper->insert($discoveryContact)) {
825
                            $new_contacts++;
826
 
827
                            if($discoveryContactInteractionType) {
828
 
15546 efrain 829
                                $discoveryContactInteraction = new DiscoveryContactInteraction();
830
                                $discoveryContactInteraction->company_id = $currentCompany->id;
831
                                $discoveryContactInteraction->contact_id = $discoveryContact->id;
832
                                $discoveryContactInteraction->interaction_type_id = $discoveryContactInteractionType->id;
833
                                $discoveryContactInteraction->user_id = $currentUser->id;
15623 anderson 834
 
15546 efrain 835
                                $discoveryContactInteractionMapper->insert($discoveryContactInteraction);
836
                            }
837
                        } else {
838
                            $error_contacts++;
839
                        }
840
                    }
841
                }
842
 
15623 anderson 843
 
15546 efrain 844
                return new JsonModel([
845
                    'success' => true,
846
                    'data' => [
847
                        'new_contacts' => $new_contacts,
848
                        'error_contacts' => $error_contacts,
849
                        'duplicate_contacts' => $duplicate_contacts,
850
                    ]
851
                ]);
852
            } else {
853
                return new JsonModel([
854
                    'success' => false,
855
                    'data' => 'ERROR_PARAMETERS_ARE_INVALID'
856
                ]);
857
            }
858
        }
15623 anderson 859
 
15546 efrain 860
        return new JsonModel([
861
            'success' => false,
862
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
863
        ]);
864
    }
15397 efrain 865
}