Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 16769 | | 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
 
16768 efrain 8
 
15397 efrain 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 Laminas\Mvc\I18n\Translator;
16
use LeadersLinked\Mapper\DiscoveryContactInteractionMapper;
17
use LeadersLinked\Mapper\DiscoveryContactInteractionTypeMapper;
18
use LeadersLinked\Model\DiscoveryContactLog;
19
use LeadersLinked\Mapper\DiscoveryContactMapper;
20
use LeadersLinked\Mapper\QueryMapper;
21
use LeadersLinked\Mapper\UserMapper;
22
use Laminas\Paginator\Adapter\DbSelect;
23
use Laminas\Paginator\Paginator;
24
use LeadersLinked\Form\DiscoveryContact\InteractionForm;
25
use LeadersLinked\Model\DiscoveryContactInteraction;
26
use LeadersLinked\Mapper\DiscoveryContactLogMapper;
27
 
28
 
29
class DiscoveryContactInteractionController extends AbstractActionController
30
{
31
    /**
32
     *
16769 efrain 33
     * @var \Laminas\Db\Adapter\AdapterInterface
15397 efrain 34
     */
35
    private $adapter;
36
 
37
    /**
38
     *
16769 efrain 39
     * @var \LeadersLinked\Cache\CacheInterface
15397 efrain 40
     */
16769 efrain 41
    private $cache;
42
 
43
 
44
    /**
45
     *
46
     * @var \Laminas\Log\LoggerInterface
47
     */
15397 efrain 48
    private $logger;
49
 
50
    /**
51
     *
52
     * @var array
53
     */
54
    private $config;
55
 
16769 efrain 56
 
15397 efrain 57
    /**
16769 efrain 58
     *
59
     * @var \Laminas\Mvc\I18n\Translator
15397 efrain 60
     */
61
    private $translator;
62
 
16769 efrain 63
 
15397 efrain 64
    /**
65
     *
16769 efrain 66
     * @param \Laminas\Db\Adapter\AdapterInterface $adapter
67
     * @param \LeadersLinked\Cache\CacheInterface $cache
68
     * @param \Laminas\Log\LoggerInterface LoggerInterface $logger
15397 efrain 69
     * @param array $config
16769 efrain 70
     * @param \Laminas\Mvc\I18n\Translator $translator
15397 efrain 71
     */
16769 efrain 72
    public function __construct($adapter, $cache, $logger, $config, $translator)
15397 efrain 73
    {
74
        $this->adapter      = $adapter;
16769 efrain 75
        $this->cache        = $cache;
15397 efrain 76
        $this->logger       = $logger;
77
        $this->config       = $config;
78
        $this->translator   = $translator;
79
    }
80
 
81
    public function indexAction()
82
    {
83
 
84
        $request = $this->getRequest();
85
        if($request->isGet()) {
86
 
87
            $request = $this->getRequest();
88
            $uuid = $this->params()->fromRoute('id');
89
 
90
 
91
            if(!$uuid) {
92
                $data = [
93
                    'success'   => false,
94
                    'data'   => 'ERROR_INVALID_PARAMETER'
95
                ];
96
 
97
                return new JsonModel($data);
98
            }
99
 
100
            $discoveryContactMapper = DiscoveryContactMapper::getInstance($this->adapter);
101
            $discoveryContact = $discoveryContactMapper->fetchOneByUuid($uuid);
102
            if(!$discoveryContact) {
103
                $data = [
104
                    'success'   => false,
105
                    'data'   => 'ERROR_RECORD_NOT_FOUND'
106
                ];
107
 
108
                return new JsonModel($data);
109
            }
110
 
111
            $search = $this->params()->fromQuery('search');
16778 efrain 112
            $search = empty($search['value']) ? '' : Functions::sanitizeFilterString($search['value']);
15397 efrain 113
 
114
            $page               = intval($this->params()->fromQuery('start', 1), 10);
115
            $records_x_page     = intval($this->params()->fromQuery('length', 10), 10);
116
 
117
 
118
            $queryMapper = QueryMapper::getInstance($this->adapter);
119
            $select = $queryMapper->getSql()->select();
120
            $select->from([ 'tb1' => DiscoveryContactInteractionMapper::_TABLE ]);
121
            $select->columns(['uuid', 'notes', 'added_on']);
122
            $select->join(['tb2' => DiscoveryContactInteractionTypeMapper::_TABLE], 'tb2.id = tb1.interaction_type_id', ['name']);
123
            $select->join(['tb3' => UserMapper::_TABLE], 'tb1.user_id = tb3.id', ['first_name' , 'last_name', 'email']);
124
            $select->where->equalTo('tb1.company_id', $discoveryContact->company_id);
125
            $select->where->equalTo('tb1.contact_id', $discoveryContact->id);
126
            if($search) {
127
                $select->where->nest()
128
                ->like('first_name', '%' . $search . '%')
129
                ->or->like('last_name', '%' . $search . '%')
130
                ->or->like('email', '%' . $search . '%')
131
                ->or->like('name', '%' . $search . '%')
132
                ->unnest();
133
            }
134
 
135
            $select->order('added_on DESC');
136
 
137
            $page               = intval($this->params()->fromQuery('start', 1), 10);
138
 
139
            $adapter = new DbSelect($select, $queryMapper->getSql());
140
            $paginator = new Paginator($adapter);
141
            $paginator->setItemCountPerPage($records_x_page);
142
            $paginator->setCurrentPageNumber($page);
143
 
144
 
145
            $items = [];
146
            $records = $paginator->getCurrentItems();
147
            foreach($records as $record)
148
            {
149
                $dt = \DateTime::createFromFormat('Y-m-d H:i:s', $record['added_on'] );
150
 
151
                $item = [
152
                    'first_name' => $record['first_name'],
153
                    'last_name' => $record['last_name'],
154
                    'email' => $record['email'],
155
                    'name' => $record['name'],
156
                    'notes' => $record['notes'],
157
                    'added_on' => $dt->format('d/m/Y H:i a'),
158
                    'link_delete' => $this->url()->fromRoute('discovery-contacts/interactions/delete', ['id' => $discoveryContact->uuid, 'interaction' => $record['uuid'] ]),
159
                ];
160
 
161
                array_push($items, $item);
162
            }
163
 
164
            $total_items = $paginator->getTotalItemCount();
165
            if($total_items <= 10) {
166
                $total_pages = 1;
167
            } else {
168
 
169
                $total_pages = (  $total_items / 10);
170
 
171
 
172
                if(($total_pages * 10) <  $total_items) {
173
                    $total_pages++;
174
                }
175
            }
176
 
177
 
178
            return new JsonModel([
179
                'success' => true,
180
                'data' => [
181
                    'current' => [
182
                        'items' => $items,
183
                        'page' => $paginator->getCurrentPageNumber()
184
 
185
                    ],
186
                    'total' => [
187
 
188
                        'items' => $total_items,
189
                        'pages' => $total_pages
190
                    ],
191
                ]
192
            ]);
193
 
194
        } else {
195
            return new JsonModel([
196
                'success' => false,
197
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
198
            ]);;
199
        }
200
    }
201
 
202
    public function addAction()
203
    {
204
        $currentUserPlugin = $this->plugin('currentUserPlugin');
205
        $currentUser = $currentUserPlugin->getUser();
206
        $currentCompany = $currentUserPlugin->getCompany();
207
 
208
        $request = $this->getRequest();
209
        $uuid = $this->params()->fromRoute('id');
210
 
211
 
212
        if(!$uuid) {
213
            $data = [
214
                'success'   => false,
215
                'data'   => 'ERROR_INVALID_PARAMETER'
216
            ];
217
 
218
            return new JsonModel($data);
219
        }
220
 
221
        $discoveryContactMapper = DiscoveryContactMapper::getInstance($this->adapter);
222
        $discoveryContact = $discoveryContactMapper->fetchOneByUuid($uuid);
223
        if(!$discoveryContact) {
224
            $data = [
225
                'success'   => false,
226
                'data'   => 'ERROR_RECORD_NOT_FOUND'
227
            ];
228
 
229
            return new JsonModel($data);
230
        }
231
 
232
 
233
 
234
        if($discoveryContact->company_id != $currentCompany->id) {
235
            return new JsonModel([
236
                'success'   => false,
237
                'data'   => 'ERROR_UNAUTHORIZED'
238
            ]);
239
        }
240
 
241
 
242
        $request = $this->getRequest();
243
        if($request->isPost()) {
244
            $form = new InteractionForm($this->adapter, $currentCompany->id);
245
            $dataPost = $request->getPost()->toArray();
246
 
247
 
248
            $form->setData($dataPost);
249
 
250
 
251
            if($form->isValid()) {
252
 
253
 
254
                $dataPost = (array) $form->getData();
255
                $notes = $dataPost['notes'];
256
                $interaction_type_id = $dataPost['interaction_type_id'];
257
 
258
 
259
 
260
                $discoveryContactInteractionTypeMapper = DiscoveryContactInteractionTypeMapper::getInstance($this->adapter);
261
                $discoveryContactInteractionType = $discoveryContactInteractionTypeMapper->fetchOneByUuid($interaction_type_id);
262
 
263
 
264
                $discoveryContactInteraction = new DiscoveryContactInteraction();
265
                $discoveryContactInteraction->user_id = $currentUser->id;
266
                $discoveryContactInteraction->company_id  = $discoveryContact->company_id;
267
                $discoveryContactInteraction->contact_id  = $discoveryContact->id;
268
                $discoveryContactInteraction->interaction_type_id =  $discoveryContactInteractionType->id;
269
                $discoveryContactInteraction->notes = $notes;
270
 
271
 
272
                $discoveryContactInteractionMapper = DiscoveryContactInteractionMapper::getInstance($this->adapter);
273
                $result = $discoveryContactInteractionMapper->insert($discoveryContactInteraction);
274
 
275
                if($result) {
276
 
277
                    $this->logger->info('Se agrego la interacción del relevamiento de contacto : ' . $discoveryContact->first_name . ' ' . $discoveryContact->last_name . '(' . $discoveryContact->corporate_email . ')', ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
278
 
279
 
280
                    $discoveryContactLog = new DiscoveryContactLog();
281
                    $discoveryContactLog->company_id    = $discoveryContact->company_id;
282
                    $discoveryContactLog->contact_id    = $discoveryContact->id;
283
                    $discoveryContactLog->user_id       = $currentUser->id;
284
                    $discoveryContactLog->activity      = 'LABEL_RECORD_INTERACTION_ADDED';
285
                    $discoveryContactLog->details       = 'LABEL_TYPE : ' . $discoveryContactInteractionType->name  . PHP_EOL .
286
                        'LABEL_NOTES : ' . $notes . PHP_EOL;
287
 
288
 
289
                    $discoveryContactLogMapper = DiscoveryContactLogMapper::getInstance($this->adapter);
290
                    $discoveryContactLogMapper->insert($discoveryContactLog);
291
 
292
                    $data = [
293
                        'success'   => true,
294
                        'data'   => 'LABEL_RECORD_ADDED'
295
                    ];
296
                } else {
297
                    $data = [
298
                        'success'   => false,
299
                        'data'      => $discoveryContactInteractionMapper->getError()
300
                    ];
301
 
302
                }
303
 
304
                return new JsonModel($data);
305
 
306
            } else {
307
                $messages = [];
308
                $form_messages = (array) $form->getMessages();
309
                foreach($form_messages  as $fieldname => $field_messages)
310
                {
311
 
312
                    $messages[$fieldname] = array_values($field_messages);
313
                }
314
 
315
                return new JsonModel([
316
                    'success'   => false,
317
                    'data'   => $messages
318
                ]);
319
            }
320
 
321
        } else {
322
            $data = [
323
                'success' => false,
324
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
325
            ];
326
 
327
            return new JsonModel($data);
328
        }
329
 
330
        return new JsonModel($data);
331
    }
332
 
333
 
334
    public function deleteAction()
335
    {
336
        $currentUserPlugin = $this->plugin('currentUserPlugin');
337
        $currentUser = $currentUserPlugin->getUser();
338
        $currentCompany = $currentUserPlugin->getCompany();
339
 
340
        $request = $this->getRequest();
341
        $uuid = $this->params()->fromRoute('id');
342
 
343
 
344
        if(!$uuid) {
345
            $data = [
346
                'success'   => false,
347
                'data'   => 'ERROR_INVALID_PARAMETER'
348
            ];
349
 
350
            return new JsonModel($data);
351
        }
352
 
353
        $discoveryContactMapper = DiscoveryContactMapper::getInstance($this->adapter);
354
        $discoveryContact = $discoveryContactMapper->fetchOneByUuid($uuid);
355
        if(!$discoveryContact) {
356
            $data = [
357
                'success'   => false,
358
                'data'   => 'ERROR_RECORD_NOT_FOUND'
359
            ];
360
 
361
            return new JsonModel($data);
362
        }
363
 
364
 
365
        if($discoveryContact->company_id != $currentCompany->id) {
366
            return new JsonModel([
367
                'success'   => false,
368
                'data'   => 'ERROR_UNAUTHORIZED'
369
            ]);
370
        }
371
 
372
        $uuid = $this->params()->fromRoute('interaction');
373
        $discoveryContactInteractionMapper = DiscoveryContactInteractionMapper::getInstance($this->adapter);
374
        $discoveryContactInteraction = $discoveryContactInteractionMapper->fetchOneByUuid($uuid);
375
        if(!$discoveryContactInteraction) {
376
            $data = [
377
                'success'   => false,
378
                'data'   => 'ERROR_RECORD_NOT_FOUND'
379
            ];
380
 
381
            return new JsonModel($data);
382
        }
383
 
384
 
385
 
386
        if($discoveryContact->id != $discoveryContactInteraction->contact_id) {
387
            return new JsonModel([
388
                'success'   => false,
389
                'data'   => 'ERROR_UNAUTHORIZED'
390
            ]);
391
        }
392
 
393
 
394
        if($request->isPost()) {
395
            $discoveryContactInteractionTypeMapper = DiscoveryContactInteractionTypeMapper::getInstance($this->adapter);
396
            $discoveryContactInteractionType = $discoveryContactInteractionTypeMapper->fetchOne($discoveryContactInteraction->interaction_type_id);
397
 
398
 
399
            $result = $discoveryContactInteractionMapper->delete($discoveryContactInteraction);
400
            if($result) {
401
                $this->logger->info('Se borro la interacción del relevamiento de contacto ' . $discoveryContact->first_name . ' ' . $discoveryContact->last_name . '(' . $discoveryContact->corporate_email . ')', ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
402
 
403
                $discoveryContactLog = new DiscoveryContactLog();
404
                $discoveryContactLog->company_id    = $discoveryContact->company_id;
405
                $discoveryContactLog->contact_id    = $discoveryContact->id;
406
                $discoveryContactLog->user_id       = $currentUser->id;
407
                $discoveryContactLog->activity      = 'LABEL_RECORD_INTERACTION_DELETED';
408
                $discoveryContactLog->details       = 'LABEL_TYPE : ' . $discoveryContactInteractionType->name  . PHP_EOL .
409
                'LABEL_NOTES : ' . $discoveryContactInteraction->notes . PHP_EOL;
410
 
411
 
412
                $discoveryContactLogMapper = DiscoveryContactLogMapper::getInstance($this->adapter);
413
                $discoveryContactLogMapper->insert($discoveryContactLog);
414
 
415
                $data = [
416
                    'success' => true,
417
                    'data' => 'LABEL_RECORD_DELETED'
418
                ];
419
            } else {
420
 
421
                $data = [
422
                    'success'   => false,
423
                    'data'      => $discoveryContactInteractionMapper->getError()
424
                ];
425
 
426
                return new JsonModel($data);
427
            }
428
 
429
        } else {
430
            $data = [
431
                'success' => false,
432
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
433
            ];
434
 
435
            return new JsonModel($data);
436
        }
437
 
438
        return new JsonModel($data);
439
    }
440
}