Proyectos de Subversion LeadersLinked - Backend

Rev

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