Proyectos de Subversion LeadersLinked - Backend

Rev

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