Proyectos de Subversion LeadersLinked - Services

Rev

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

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
declare(strict_types=1);
3
 
4
namespace LeadersLinked\Controller;
5
 
6
use Laminas\Db\Adapter\AdapterInterface;
7
 
8
use Laminas\Mvc\Controller\AbstractActionController;
9
use Laminas\Log\LoggerInterface;
10
use Laminas\View\Model\ViewModel;
11
use Laminas\View\Model\JsonModel;
12
use LeadersLinked\Mapper\MyCoachCategoryMapper;
13
 
14
use LeadersLinked\Form\MyCoach\MyCoachQuestionForm;
15
use LeadersLinked\Library\MyCoachAccessControl;
16
use LeadersLinked\Form\MyCoach\MyCoachAnswerForm;
17
use LeadersLinked\Mapper\MyCoachQuestionMapper;
18
use LeadersLinked\Mapper\MyCoachQuestionCategoryMapper;
19
use LeadersLinked\Model\MyCoachQuestionCategory;
20
use LeadersLinked\Mapper\QueryMapper;
21
use LeadersLinked\Model\MyCoachQuestion;
22
use Laminas\Db\ResultSet\HydratingResultSet;
23
use Laminas\Paginator\Adapter\DbSelect;
24
use Laminas\Paginator\Paginator;
25
use LeadersLinked\Mapper\UserMapper;
26
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
27
use LeadersLinked\Library\Functions;
28
use LeadersLinked\Mapper\MyCoachAnswerMapper;
29
use LeadersLinked\Mapper\CommentMapper;
30
use LeadersLinked\Model\MyCoachAnswer;
31
use LeadersLinked\Form\MyCoach\CommentForm;
32
use LeadersLinked\Model\Comment;
33
use LeadersLinked\Mapper\ContentReactionMapper;
34
use LeadersLinked\Model\ContentReaction;
35
use LeadersLinked\Mapper\MyCoachQuestionViewMapper;
36
use LeadersLinked\Model\MyCoachQuestionView;
37
 
38
 
39
class MyCoachController extends AbstractActionController
40
{
41
    /**
42
     *
43
     * @var \Laminas\Db\Adapter\AdapterInterface
44
     */
45
    private $adapter;
46
 
47
    /**
48
     *
49
     * @var \LeadersLinked\Cache\CacheInterface
50
     */
51
    private $cache;
52
 
53
 
54
    /**
55
     *
56
     * @var \Laminas\Log\LoggerInterface
57
     */
58
    private $logger;
59
 
60
    /**
61
     *
62
     * @var array
63
     */
64
    private $config;
65
 
66
 
67
    /**
68
     *
69
     * @var \Laminas\Mvc\I18n\Translator
70
     */
71
    private $translator;
72
 
73
 
74
    /**
75
     *
76
     * @param \Laminas\Db\Adapter\AdapterInterface $adapter
77
     * @param \LeadersLinked\Cache\CacheInterface $cache
78
     * @param \Laminas\Log\LoggerInterface LoggerInterface $logger
79
     * @param array $config
80
     * @param \Laminas\Mvc\I18n\Translator $translator
81
     */
82
    public function __construct($adapter, $cache, $logger, $config, $translator)
83
    {
84
        $this->adapter      = $adapter;
85
        $this->cache        = $cache;
86
        $this->logger       = $logger;
87
        $this->config       = $config;
88
        $this->translator   = $translator;
89
    }
90
 
91
    /**
92
     *
93
     * Generación del listado de perfiles
94
     * {@inheritDoc}
95
     * @see \Laminas\Mvc\Controller\AbstractActionController::indexAction()
96
     */
97
    public function indexAction()
98
    {
99
        $currentUserPlugin = $this->plugin('currentUserPlugin');
100
        $currentUser = $currentUserPlugin->getUser();
101
 
102
        $currentNetworkPlugin = $this->plugin('currentNetworkPlugin');
103
        $currentNetwork = $currentNetworkPlugin->getNetwork();
104
 
105
        $request = $this->getRequest();
106
        if ($request->isGet()) {
107
 
108
                $myCoachAccesControl = MyCoachAccessControl::getInstance($this->adapter);
109
                $categories = $myCoachAccesControl->getCategoriesWithAccessToFormSelect($currentUser->id, $currentNetwork->id);
110
 
111
                return new JsonModel([
112
                    'success' => true,
113
                    'data' => [
114
                        'categories' => $categories,
115
                    ]
116
                ]);
117
 
118
 
119
 
120
        } else {
121
            return new JsonModel([
122
                'success' => false,
123
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
124
            ]);
125
        }
126
    }
127
 
128
    public function questionsAction()
129
    {
130
        $currentUserPlugin = $this->plugin('currentUserPlugin');
131
        $currentUser = $currentUserPlugin->getUser();
132
 
133
        $currentNetworkPlugin = $this->plugin('currentNetworkPlugin');
134
        $currentNetwork = $currentNetworkPlugin->getNetwork();
135
 
136
 
137
        $request = $this->getRequest();
138
        if ($request->isGet()) {
139
            $userMapper                     = UserMapper::getInstance($this->adapter);
140
            $myCoachCategoryMapper          = MyCoachCategoryMapper::getInstance($this->adapter);
141
            $myCoachQuestionCategoryMapper  = MyCoachQuestionCategoryMapper::getInstance($this->adapter);
142
            $myCoachAccessControl           = MyCoachAccessControl::getInstance($this->adapter);
143
 
144
            $category_filter_id = Functions::sanitizeFilterString($this->params()->fromQuery('category_id'));
145
            $search = Functions::sanitizeFilterString($this->params()->fromQuery('search'));
146
            $page   = intval($this->params()->fromQuery('start', 1), 10);
147
 
148
            $order_field        = 'added_on';
149
            $order_direction    = 'asc';
150
            $records_x_page     = 12;
151
 
152
 
153
            if ($category_filter_id) {
154
 
155
                $message_error = '';
156
                if(!$myCoachAccessControl->hasAccessForCategory($currentUser->id, $category_filter_id, $currentNetwork->id, $message_error)) {
157
 
158
                    return new JsonModel([
159
                        'success' => true,
160
                        'data' => $message_error,
161
                    ]);
162
 
163
                }
164
 
165
                $categoryFilter = $myCoachCategoryMapper->fetchOneByUuid($category_filter_id);
166
                $category_ids = [$categoryFilter->id];
167
            } else {
168
                $category_ids = $myCoachAccessControl->getCategoryIdsWithAccess($currentUser->id, $currentNetwork->id);
169
            }
170
 
171
            $category_ids_with_edition = $myCoachAccessControl->getCategoryIdsWithEdition($currentUser->id);
172
 
173
            $queryMapper = QueryMapper::getInstance($this->adapter);
174
 
175
            $prototype = new MyCoachQuestion();
176
            $selectIn = $queryMapper->getSql()->select(MyCoachQuestionCategoryMapper::_TABLE);
177
            $selectIn->columns(['question_id']);
178
            $selectIn->where->in('category_id', $category_ids);
179
 
180
 
181
 
182
            $select = $queryMapper->getSql()->select(MyCoachQuestionMapper::_TABLE);
183
            $select->where->in('id', $selectIn);
184
 
185
            if($search) {
186
                $select->where->nest()
187
                -> like('title', '%' . $search . '%')->or->like('description', '%' . $search . '%')
188
                ->unnest();
189
            }
190
            $select->order($order_field . ' ' . $order_direction);
191
 
192
            //echo $select->getSqlString($this->adapter->platform);
193
            //exit;
194
 
195
 
196
            $users = [];
197
            $categories = [];
198
            //echo $select->getSqlString($this->adapter->platform); exit;
199
 
200
            $hydrator   = new ObjectPropertyHydrator();
201
            $resultset  = new HydratingResultSet($hydrator, $prototype);
202
 
203
            $adapter = new DbSelect($select, $queryMapper->getSql(), $resultset);
204
            $paginator = new Paginator($adapter);
205
            $paginator->setItemCountPerPage($records_x_page);
206
            $paginator->setCurrentPageNumber($page);
207
 
208
 
209
            $contentReactionMapper = ContentReactionMapper::getInstance($this->adapter);
210
            $commentMapper = CommentMapper::getInstance($this->adapter);
211
            $myCoachAnswerMapper = MyCoachAnswerMapper::getInstance($this->adapter);
212
            $myCoachQuestionViewMapper = MyCoachQuestionViewMapper::getInstance($this->adapter);
213
 
214
            $items = [];
215
            $records = $paginator->getCurrentItems();
216
            foreach ($records as $record)
217
            {
218
                $bags_categories = [];
219
 
220
                $questionCategories = $myCoachQuestionCategoryMapper->fetchAllByQuestionId($record->id);
221
 
222
 
223
 
224
 
225
                $allowEdit   = $myCoachAccessControl->hasAccessEditQuestion($currentUser->id, $record->id, $currentNetwork->id, $message_error);
226
                $allowDelete = $myCoachAccessControl->hasAccessDeleteQuestion($currentUser->id, $record->id, $currentNetwork->id, $message_error);
227
                foreach($questionCategories as $questionCategory)
228
                {
229
                    if(in_array($questionCategory->category_id, $category_ids_with_edition)) {
230
 
231
                        $allowDelete = true;
232
                    }
233
 
234
                    if (!isset($categories[$questionCategory->category_id])) {
235
                        $category = $myCoachCategoryMapper->fetchOne($questionCategory->category_id);
236
                        if ($category) {
237
                            $categories[$category->id] = $category->name;
238
 
239
                        }
240
                    }
241
 
242
                    array_push($bags_categories,['category' => $categories[ $questionCategory->category_id ] ]);
243
                }
244
 
245
                if(isset($users[$record->user_id])) {
246
                    $user = $users[ $record->user_id ];
247
                } else {
248
                    $user = $userMapper->fetchOne($record->user_id);
249
                    $users[ $record->user_id ] = $user;
250
                }
251
 
252
 
253
                $description = strip_tags($record->description);
254
                if (strlen($description) > 250) {
255
                    $description = substr($description, 0, 250) . '...';
256
                }
257
 
258
 
259
                $dt = \DateTime::createFromFormat('Y-m-d H:i:s', $record->added_on);
260
                $added_on = $dt->format('d/m/Y H:i a');
261
 
262
                $dt = \DateTime::createFromFormat('Y-m-d H:i:s', $record->updated_on);
263
                $updated_on = $dt->format('d/m/Y H:i a');
264
 
265
                $item = [
266
                    'uuid' => $record->uuid,
267
                    'user_name' => trim($user->first_name . ' ' . $user->last_name),
60 efrain 268
                    'user_image' => $this->url()->fromRoute('storage', ['type' => 'user', 'code' => $user->uuid, 'filename' => $user->image],['force_canonical' => true]),
1 efrain 269
                    'title' => $record->title,
270
                    'description' => $description,
271
                    'categories' => $bags_categories,
272
                    'views' => intval($myCoachQuestionViewMapper->fetchCountByQuestionId($record->id), 10),
273
                    'answers' =>  intval($myCoachAnswerMapper->fetchCountByMyCoachQuestionId($record->id), 10),
274
                    'reactions' => intval($contentReactionMapper->fetchCountByMyCoachQuestionId($record->id), 10),
275
                    'comments' =>  intval($commentMapper->fetchCountByMyCoachQuestionId($record->id), 10),
276
                    'added_on' => $added_on,
277
                    'updated_on' => $updated_on,
278
                    'link_add_comment' => $this->url()->fromRoute('my-coach/questions/comments/add', ['id' => $record->uuid]),
279
                    'link_view' => $this->url()->fromRoute('my-coach/questions/view', ['id' => $record->uuid]),
280
                    'link_edit' => $allowEdit ?  $this->url()->fromRoute('my-coach/questions/edit', ['id' => $record->uuid]) : '',
281
                    'link_delete' => $allowDelete ? $this->url()->fromRoute('my-coach/questions/delete', ['id' =>  $record->uuid]) : '',
282
                ];
283
 
284
                array_push($items, $item);
285
            }
286
 
287
            return new JsonModel([
288
                'success' => true,
289
                'data' => [
290
                    'items' => $items,
291
                    'total' => $paginator->getTotalItemCount(),
292
                    'page' => $paginator->getCurrentPageNumber(),
293
                    'total_pages' => $paginator->getPageRange()
294
                ]
295
            ]);
296
 
297
 
298
        } else {
299
            return new JsonModel([
300
                'success' => false,
301
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
302
            ]);
303
        }
304
    }
305
 
306
    public function addQuestionAction()
307
    {
308
        $currentNetworkPlugin  = $this->plugin('currentNetworkPlugin');
309
        $currentNetwork        = $currentNetworkPlugin->getNetwork();
310
 
311
        $currentUserPlugin  = $this->plugin('currentUserPlugin');
312
        $currentUser        = $currentUserPlugin->getUser();
313
 
314
        $request            = $this->getRequest();
315
 
316
        if ($request->isPost()) {
317
 
318
            $dataPost = array_merge($request->getPost()->toArray(), $request->getFiles()->toArray());
319
 
320
 
321
            $myCoachAccessControl = MyCoachAccessControl::getInstance($this->adapter);
322
            $categories = $myCoachAccessControl->getCategoriesWithAccessToFormSelect($currentUser->id, $currentNetwork->id);
323
 
324
            $form = new MyCoachQuestionForm($categories);
325
            $form->setData($dataPost);
326
 
327
            if ($form->isValid()) {
328
                $dataPost = (array) $form->getData();
329
 
330
                $myCoachQuestion = new MyCoachQuestion();
331
                $myCoachQuestion->network_id    = $currentNetwork ->id;
332
                $myCoachQuestion->user_id       = $currentUser->id;
333
                $myCoachQuestion->title         = $dataPost['title'];
334
                $myCoachQuestion->description   = $dataPost['description'];
335
 
336
 
337
                $myCoachQuestionMapper = MyCoachQuestionMapper::getInstance($this->adapter);
338
                if ($myCoachQuestionMapper->insert($myCoachQuestion)) {
339
                    $myCoachCategoryMapper = MyCoachCategoryMapper::getInstance($this->adapter);
340
                    $myCoachQuestionCategoryMapper = MyCoachQuestionCategoryMapper::getInstance($this->adapter);
341
 
342
                    $category_ids = $dataPost['category_id'];
343
                    foreach($category_ids as $category_id)
344
                    {
345
                        $myCoachCategory = $myCoachCategoryMapper->fetchOneByUuid($category_id);
346
                        if($myCoachCategory) {
347
                            $myCoachQuestionCategory = new MyCoachQuestionCategory();
348
                            $myCoachQuestionCategory->category_id = $myCoachCategory->id;
349
                            $myCoachQuestionCategory->question_id = $myCoachQuestion->id;
350
 
351
                            $myCoachQuestionCategoryMapper->insert($myCoachQuestionCategory);
352
                        }
353
                    }
354
 
355
 
356
                    $this->logger->info('Se agrego la pregunta ' . $myCoachQuestion->title, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
357
 
358
                    $data = [
359
                        'success'   => true,
360
                        'data'   => 'LABEL_RECORD_ADDED'
361
                    ];
362
                } else {
363
                    $data = [
364
                        'success'   => false,
365
                        'data'      => $myCoachQuestionMapper->getError()
366
                    ];
367
                }
368
 
369
                return new JsonModel($data);
370
            } else {
371
                $messages = [];
372
                $form_messages = (array) $form->getMessages();
373
                foreach ($form_messages  as $fieldname => $field_messages)
374
                {
375
                    $messages[$fieldname] = array_values($field_messages);
376
                }
377
 
378
                return new JsonModel([
379
                    'success'   => false,
380
                    'data'   => $messages
381
                ]);
382
            }
383
        } else {
384
            $data = [
385
                'success' => false,
386
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
387
            ];
388
 
389
            return new JsonModel($data);
390
        }
391
 
392
        return new JsonModel($data);
393
    }
394
 
395
 
396
 
397
    public function deleteQuestionAction()
398
    {
399
        $currentNetworkPlugin  = $this->plugin('currentNetworkPlugin');
400
        $currentNetwork        = $currentNetworkPlugin->getNetwork();
401
 
402
        $currentUserPlugin  = $this->plugin('currentUserPlugin');
403
        $currentUser        = $currentUserPlugin->getUser();
404
 
405
        $request    = $this->getRequest();
406
        $id         = $this->params()->fromRoute('id');
407
 
408
        $message_error = '';
409
        $myCoachAccessControl = MyCoachAccessControl::getInstance($this->adapter);
410
        if(!$myCoachAccessControl->hasAccessDeleteQuestion($currentUser->id, $id, $currentNetwork->id, $message_error)) {
411
            return new JsonModel([
412
                'success'   => false,
413
                'data'   => $message_error
414
            ]);
415
        }
416
 
417
 
418
        $myCoachQuestionMapper = MyCoachQuestionMapper::getInstance($this->adapter);
419
        $myCoachQuestion = $myCoachQuestionMapper->fetchOneByUuid($id);
420
 
421
 
422
        if ($request->isPost()) {
423
 
424
            $myCoachQuestionCategoryMapper = MyCoachQuestionCategoryMapper::getInstance($this->adapter);
425
            $myCoachQuestionCategoryMapper->deleteAllByQuestionId($myCoachQuestion->id);
426
 
427
            $myCoachAnswerMapper = MyCoachAnswerMapper::getInstance($this->adapter);
428
            $myCoachAnswerMapper->deleteAllByQuestionId($myCoachQuestion->id);
429
 
430
            $result =  $myCoachQuestionMapper->delete($myCoachQuestion);
431
            if ($result) {
432
                $data = [
433
                    'success' => true,
434
                    'data' => 'LABEL_RECORD_DELETED'
435
                ];
436
            } else {
437
 
438
                $data = [
439
                    'success'   => false,
440
                    'data'      => $myCoachQuestionMapper->getError()
441
                ];
442
 
443
                return new JsonModel($data);
444
            }
445
        } else {
446
            $data = [
447
                'success' => false,
448
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
449
            ];
450
 
451
            return new JsonModel($data);
452
        }
453
 
454
        return new JsonModel($data);
455
    }
456
 
457
 
458
    public function editQuestionAction()
459
    {
460
        $currentNetworkPlugin  = $this->plugin('currentNetworkPlugin');
461
        $currentNetwork        = $currentNetworkPlugin->getNetwork();
462
 
463
        $currentUserPlugin  = $this->plugin('currentUserPlugin');
464
        $currentUser        = $currentUserPlugin->getUser();
465
 
466
        $request    = $this->getRequest();
467
        $id    = $this->params()->fromRoute('id');
468
 
469
        $message_error = '';
470
        $myCoachAccessControl = MyCoachAccessControl::getInstance($this->adapter);
471
        if(!$myCoachAccessControl->hasAccessEditQuestion($currentUser->id, $id, $currentNetwork->id, $message_error)) {
472
            return new JsonModel([
473
                'success'   => false,
474
                'data'   => $message_error
475
            ]);
476
        }
477
 
478
 
479
        $myCoachQuestionMapper = MyCoachQuestionMapper::getInstance($this->adapter);
480
        $myCoachQuestion = $myCoachQuestionMapper->fetchOneByUuid($id);
481
 
482
 
483
        if ($request->isGet()) {
484
 
485
            $category_ids = [];
486
            $myCoachCategoryMapper = MyCoachCategoryMapper::getInstance($this->adapter);
487
 
488
            $myCoachQuestionCategoryMapper = MyCoachQuestionCategoryMapper::getInstance($this->adapter);
489
            $records = $myCoachQuestionCategoryMapper->fetchAllByQuestionId($myCoachQuestion->id);
490
            foreach($records as $record)
491
            {
492
                $myCoachCategory = $myCoachCategoryMapper->fetchOne($record->category_id);
493
                if($myCoachCategory) {
494
                    array_push($category_ids,$myCoachCategory->uuid);
495
                }
496
            }
497
 
498
 
499
 
500
            $data = [
501
                'success' => true,
502
                'data' => [
503
                    'category_id' => $category_ids,
504
                    'title' => $myCoachQuestion->title,
505
                    'description' => $myCoachQuestion->description,
506
                ]
507
            ];
508
 
509
            return new JsonModel($data);
510
        } else if ($request->isPost()) {
511
 
512
            $categories = $myCoachAccessControl->getCategoriesWithAccessToFormSelect($myCoachQuestion->user_id, $currentNetwork->id);
513
            $dataPost = array_merge($request->getPost()->toArray(), $request->getFiles()->toArray());
514
 
515
            $form = new MyCoachQuestionForm($categories);
516
            $form->setData($dataPost);
517
 
518
            if ($form->isValid()) {
519
                $dataPost = (array) $form->getData();
520
 
521
 
522
 
523
                $myCoachQuestion->title = $dataPost['title'];
524
                $myCoachQuestion->description = $dataPost['description'];
525
 
526
 
527
                if ($myCoachQuestionMapper->update($myCoachQuestion)) {
528
 
529
 
530
                    $myCoachCategoryMapper = MyCoachCategoryMapper::getInstance($this->adapter);
531
 
532
                    $myCoachQuestionCategoryMapper = MyCoachQuestionCategoryMapper::getInstance($this->adapter);
533
                    $myCoachQuestionCategoryMapper->deleteAllByQuestionId($myCoachQuestion->id);
534
 
535
                    $category_ids = $dataPost['category_id'];
536
                    foreach($category_ids as $category_id)
537
                    {
538
                        $myCoachCategory = $myCoachCategoryMapper->fetchOneByUuid($category_id);
539
                        if($myCoachCategory) {
540
                            $myCoachQuestionCategory = new MyCoachQuestionCategory();
541
                            $myCoachQuestionCategory->category_id = $myCoachCategory->id;
542
                            $myCoachQuestionCategory->question_id = $myCoachQuestion->id;
543
 
544
                            $myCoachQuestionCategoryMapper->insert($myCoachQuestionCategory);
545
                        }
546
                    }
547
 
548
 
549
                    $this->logger->info('Se edito la pregunta ' . $myCoachQuestion->title, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
550
 
551
                    $data = [
552
                        'success'   => true,
553
                        'data'   => 'LABEL_RECORD_UPDATED'
554
                    ];
555
                } else {
556
                    $data = [
557
                        'success'   => false,
558
                        'data'      => $myCoachQuestionMapper->getError()
559
                    ];
560
                }
561
 
562
                return new JsonModel($data);
563
            } else {
564
                $messages = [];
565
                $form_messages = (array) $form->getMessages();
566
                foreach ($form_messages  as $fieldname => $field_messages) {
567
 
568
                    $messages[$fieldname] = array_values($field_messages);
569
                }
570
 
571
                return new JsonModel([
572
                    'success'   => false,
573
                    'data'   => $messages
574
                ]);
575
            }
576
        } else {
577
            $data = [
578
                'success' => false,
579
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
580
            ];
581
 
582
            return new JsonModel($data);
583
        }
584
 
585
        return new JsonModel($data);
586
    }
587
 
588
 
589
    public function viewQuestionAction()
590
    {
591
        $currentUserPlugin = $this->plugin('currentUserPlugin');
592
        $currentUser = $currentUserPlugin->getUser();
593
 
594
        $currentNetworkPlugin = $this->plugin('currentNetworkPlugin');
595
        $currentNetwork = $currentNetworkPlugin->getNetwork();
596
 
597
        $request    = $this->getRequest();
598
        $id    = $this->params()->fromRoute('id');
599
 
600
        $message_error = '';
601
        $myCoachAccessControl = MyCoachAccessControl::getInstance($this->adapter);
602
        if(!$myCoachAccessControl->hasAccessViewQuestion($currentUser->id, $id, $currentNetwork->id, $message_error)) {
603
            return new JsonModel([
604
                'success'   => false,
605
                'data'   => $message_error
606
            ]);
607
        }
608
 
609
 
610
        $myCoachQuestionMapper = MyCoachQuestionMapper::getInstance($this->adapter);
611
        $myCoachQuestion = $myCoachQuestionMapper->fetchOneByUuid($id);
612
 
613
        $contentReactionMapper = ContentReactionMapper::getInstance($this->adapter);
614
        $commentMapper = CommentMapper::getInstance($this->adapter);
615
        $myCoachQuestionViewMapper = MyCoachQuestionViewMapper::getInstance($this->adapter);
616
        $myCoachAnswerMapper = MyCoachAnswerMapper::getInstance($this->adapter);
617
 
618
 
619
        $request = $this->getRequest();
620
        if ($request->isGet()) {
621
 
622
            $categories = [];
623
            $users = [];
624
 
625
 
626
 
627
            $bags_categories = [];
628
 
629
            $myCoachQuestionViewMapper = MyCoachQuestionViewMapper::getInstance($this->adapter);
630
            $myCoachQuestionView = $myCoachQuestionViewMapper->fetchOneByQuestionIdAndUserId($myCoachQuestion->id, $currentUser->id);
631
            if(!$myCoachQuestionView) {
632
                $myCoachQuestionView = new MyCoachQuestionView();
633
                $myCoachQuestionView->question_id = $myCoachQuestion->id;
634
                $myCoachQuestionView->user_id = $currentUser->id;
635
 
636
                $myCoachQuestionViewMapper->insert($myCoachQuestionView);
637
 
638
            }
639
 
640
 
641
 
642
            $myCoachCategoryMapper = MyCoachCategoryMapper::getInstance($this->adapter);
643
            $myCoachQuestionCategoryMapper = MyCoachQuestionCategoryMapper::getInstance($this->adapter);
644
 
645
            $questionCategories = $myCoachQuestionCategoryMapper->fetchAllByQuestionId($myCoachQuestion->id);
646
            foreach($questionCategories as $questionCategory)
647
            {
648
 
649
                if (!isset($categories[$questionCategory->category_id])) {
650
                    $category = $myCoachCategoryMapper->fetchOne($questionCategory->category_id);
651
                    if ($category) {
652
                        $categories[$category->id] = $category->name;
653
                    }
654
                }
655
 
656
                array_push($bags_categories,['category' => $categories[ $questionCategory->category_id ] ]);
657
            }
658
 
659
            $users = [];
660
            $userMapper = UserMapper::getInstance($this->adapter);
661
            if(isset($users[$myCoachQuestion->user_id])) {
662
                $user = $users[$myCoachQuestion->user_id];
663
            } else {
664
                $user = $userMapper->fetchOne( $myCoachQuestion->user_id );
665
                $users[ $myCoachQuestion->user_id ] = $user;
666
            }
667
 
668
 
669
 
670
            $dt = \DateTime::createFromFormat('Y-m-d H:i:s', $myCoachQuestion->added_on);
671
            $added_on = $dt->format('d/m/Y H:i a');
672
 
673
            $dt = \DateTime::createFromFormat('Y-m-d H:i:s', $myCoachQuestion->updated_on);
674
            $updated_on = $dt->format('d/m/Y H:i a');
675
 
676
 
677
 
678
            $allowDelete = $myCoachAccessControl->hasAccessDeleteQuestion($currentUser->id, $myCoachQuestion->id, $currentNetwork->id, $message_error);
679
            if($allowDelete) {
680
                $link_delete = $this->url()->fromRoute('my-coach/questions/delete', ['id' => $myCoachQuestion->uuid]);
681
            } else {
682
                $link_delete = '';
683
            }
684
 
685
            $allowAnswerAdd = $myCoachAccessControl->hasAccessAnswerQuestion($currentUser->id, $myCoachQuestion->id, $currentNetwork->id, $message_error);
686
            if ($allowAnswerAdd) {
687
                $link_answers_add = $this->url()->fromRoute('my-coach/questions/answers/add', ['id' => $myCoachQuestion->uuid]);
688
            } else {
689
                $link_answers_add = '';
690
            }
691
 
692
 
693
 
694
            $myCoachLastAnswer = $myCoachAnswerMapper->fetchOneLastAnswerByQuestionId($myCoachQuestion->id);
695
 
696
            if( $myCoachLastAnswer) {
697
                $dt = \DateTime::createFromFormat('Y-m-d H:i:s', $myCoachLastAnswer->added_on);
698
                $last_answer_on = $dt->format('d/m/Y H:i a');
699
 
700
 
701
 
702
            } else {
703
                $last_answer_on = '';
704
            }
705
 
706
 
707
            return new JsonModel([
708
                'success' => true,
709
                'data' => [
710
                    'uuid' => $myCoachQuestion->uuid,
711
                    'user_name' => trim($user->first_name . ' ' . $user->last_name),
60 efrain 712
                    'user_image' => $this->url()->fromRoute('storage', ['type' => 'user', 'code' => $user->uuid, 'filename' => $user->image],['force_canonical' => true]),
1 efrain 713
                    'title' => $myCoachQuestion->title,
714
                    'description' => $myCoachQuestion->description,
715
                    'categories' => $bags_categories,
716
                    'views' => intval($myCoachQuestionViewMapper->fetchCountByQuestionId($myCoachQuestion->id) , 10),
717
                    'answers' => intval($myCoachAnswerMapper->fetchCountByMyCoachQuestionId($myCoachQuestion->id) , 10),
718
                    'reactions' => intval($contentReactionMapper->fetchCountByMyCoachQuestionId($myCoachQuestion->id), 10),
719
                    'comments' => intval($commentMapper->fetchCountByMyCoachQuestionId($myCoachQuestion->id), 10),
720
                    'added_on' => $added_on,
721
                    'updated_on' => $updated_on,
722
                    'last_answer_on' => $last_answer_on,
723
                    'link_answers' => $this->url()->fromRoute('my-coach/questions/answers', ['id' => $myCoachQuestion->uuid]),
724
                    'link_answers_add' => $link_answers_add,
725
                    'link_delete' => $link_delete,
726
                ]
727
 
728
            ]);
729
 
730
 
731
        } else {
732
            return new JsonModel([
733
                'success' => false,
734
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
735
            ]);
736
        }
737
 
738
 
739
 
740
 
741
 
742
    }
743
 
744
    /**
745
     *
746
     * {@inheritDoc}
747
     * @see \Laminas\Mvc\Controller\AbstractActionController::indexAction()
748
     */
749
    public function answersAction()
750
    {
751
        $currentUserPlugin = $this->plugin('currentUserPlugin');
752
        $currentUser = $currentUserPlugin->getUser();
753
 
754
        $currentNetworkPlugin = $this->plugin('currentNetworkPlugin');
755
        $currentNetwork = $currentNetworkPlugin->getNetwork();
756
 
757
        $request    = $this->getRequest();
758
        $id    = $this->params()->fromRoute('id');
759
 
760
        $message_error = '';
761
        $myCoachAccessControl = MyCoachAccessControl::getInstance($this->adapter);
762
        if(!$myCoachAccessControl->hasAccessViewQuestion($currentUser->id, $id, $currentNetwork->id, $message_error)) {
763
            return new JsonModel([
764
                'success'   => false,
765
                'data'   => $message_error
766
            ]);
767
        }
768
 
769
 
770
 
771
        $myCoachQuestionMapper = MyCoachQuestionMapper::getInstance($this->adapter);
772
        $myCoachQuestion = $myCoachQuestionMapper->fetchOneByUuid($id);
773
 
774
        $request = $this->getRequest();
775
        if ($request->isGet()) {
776
 
777
 
778
            $myCoachAnswerMapper = MyCoachAnswerMapper::getInstance($this->adapter);
779
            $now = $myCoachAnswerMapper->getDatebaseNow();
780
 
781
            $records = $myCoachAnswerMapper->fetchAllByQuestionId($myCoachQuestion->id);
782
 
783
 
784
            $items = [];
785
 
786
            foreach($records as $record)
787
            {
788
                $items[] = $this->renderAnswer($record->id, $currentUser->id, $now);
789
            }
790
 
791
 
792
 
793
            return new JsonModel([
794
                'success' => true,
795
                'data' => [
796
                    'items' => $items
797
                ]
798
            ]);
799
 
800
 
801
        } else {
802
            return new JsonModel([
803
                'success' => false,
804
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
805
            ]);
806
        }
807
    }
808
 
809
    public function deleteAnswerAction()
810
    {
811
        $currentNetworkPlugin  = $this->plugin('currentNetworkPlugin');
812
        $currentNetwork        = $currentNetworkPlugin->getNetwork();
813
 
814
        $currentUserPlugin  = $this->plugin('currentUserPlugin');
815
        $currentUser        = $currentUserPlugin->getUser();
816
 
817
        $request    = $this->getRequest();
818
        $answer     = $this->params()->fromRoute('answer');
819
 
820
        $message_error = '';
821
        $myCoachAccessControl = MyCoachAccessControl::getInstance($this->adapter);
822
        if(!$myCoachAccessControl->hasAccessEditOrDeleteAnswer($currentUser->id, $answer, $currentNetwork->id, $message_error)) {
823
            return new JsonModel([
824
                'success'   => false,
825
                'data'   => $message_error
826
            ]);
827
        }
828
 
829
 
830
        $myCoachAnswerMapper = MyCoachAnswerMapper::getInstance($this->adapter);
831
        $myCoachAnswer = $myCoachAnswerMapper->fetchOneByUuid($answer);
832
 
833
        if ($request->isPost()) {
834
 
835
            if($myCoachAnswerMapper->delete($myCoachAnswer)) {
836
 
837
                $contentReactionMapper = ContentReactionMapper::getInstance($this->adapter);
838
                $commentMapper = CommentMapper::getInstance($this->adapter);
839
 
840
 
841
                $this->logger->info('Se borro la respuesta ' . $myCoachAnswer->text, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
842
 
843
                $data = [
844
                    'success'   => true,
845
                    'data'   => [
846
                        'total_comments' => intval($commentMapper->fetchCountByMyCoachQuestionId($myCoachAnswer->question_id), 10),
847
                        'total_answers' => intval($myCoachAnswerMapper->fetchCountByMyCoachQuestionId($myCoachAnswer->question_id), 10),
848
                        'total_reactions' => intval($contentReactionMapper->fetchCountByMyCoachQuestionId($myCoachAnswer->question_id), 10)
849
 
850
                    ]
851
                ];
852
            } else {
853
                $data = [
854
                    'success'   => false,
855
                    'data'      => $myCoachAnswerMapper->getError()
856
                ];
857
            }
858
        } else {
859
            $data = [
860
                'success' => false,
861
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
862
            ];
863
 
864
 
865
        }
866
        return new JsonModel($data);
867
 
868
 
869
    }
870
 
871
 
872
    public function editAnswerAction()
873
    {
874
        $currentNetworkPlugin  = $this->plugin('currentNetworkPlugin');
875
        $currentNetwork        = $currentNetworkPlugin->getNetwork();
876
 
877
        $currentUserPlugin  = $this->plugin('currentUserPlugin');
878
        $currentUser        = $currentUserPlugin->getUser();
879
 
880
        $request    = $this->getRequest();
881
        $answer     = $this->params()->fromRoute('answer');
882
 
883
        $message_error = '';
884
        $myCoachAccessControl = MyCoachAccessControl::getInstance($this->adapter);
885
        if(!$myCoachAccessControl->hasAccessEditOrDeleteAnswer($currentUser->id, $answer, $currentNetwork->id, $message_error)) {
886
            return new JsonModel([
887
                'success'   => false,
888
                'data'   => $message_error
889
            ]);
890
        }
891
 
892
 
893
        $myCoachAnswerMapper = MyCoachAnswerMapper::getInstance($this->adapter);
894
        $myCoachAnswer = $myCoachAnswerMapper->fetchOneByUuid($answer);
895
 
896
        if ($request->isPost()) {
897
 
898
            $dataPost = array_merge($request->getPost()->toArray(), $request->getFiles()->toArray());
899
            $form = new MyCoachAnswerForm();
900
            $form->setData($dataPost);
901
 
902
            if ($form->isValid()) {
903
                $myCoachAnswer->text        = $dataPost['description'];
904
 
905
                if($myCoachAnswerMapper->update($myCoachAnswer)) {
906
 
907
                    $this->logger->info('Se edito la respuesta ' . $myCoachAnswer->text, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
908
 
909
                    $data = [
910
                        'success'   => true,
911
                        'data'   => [
912
                            'description' => $myCoachAnswer->text
913
                        ]
914
                    ];
915
                } else {
916
                    $data = [
917
                        'success'   => false,
918
                        'data'      => $myCoachAnswerMapper->getError()
919
                    ];
920
                }
921
 
922
                return new JsonModel($data);
923
            } else {
924
                $messages = [];
925
                $form_messages = (array) $form->getMessages();
926
                foreach ($form_messages  as $fieldname => $field_messages)
927
                {
928
                    $messages[$fieldname] = array_values($field_messages);
929
                }
930
 
931
                return new JsonModel([
932
                    'success'   => false,
933
                    'data'   => $messages
934
                ]);
935
            }
936
        } else  if ($request->isGet()) {
937
            return new JsonModel([
938
                'success'   => true,
939
                'data'   => [
940
                    'description' => $myCoachAnswer->text
941
                ]
942
            ]);
943
        } else {
944
            $data = [
945
                'success' => false,
946
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
947
            ];
948
 
949
            return new JsonModel($data);
950
        }
951
 
952
 
953
    }
954
 
955
 
956
 
957
 
958
 
959
    public function addAnswerAction()
960
    {
961
        $currentNetworkPlugin  = $this->plugin('currentNetworkPlugin');
962
        $currentNetwork        = $currentNetworkPlugin->getNetwork();
963
 
964
        $currentUserPlugin  = $this->plugin('currentUserPlugin');
965
        $currentUser        = $currentUserPlugin->getUser();
966
 
967
        $request    = $this->getRequest();
968
        $id    = $this->params()->fromRoute('id');
969
 
970
        $message_error = '';
971
        $myCoachAccessControl = MyCoachAccessControl::getInstance($this->adapter);
972
        if(!$myCoachAccessControl->hasAccessAnswerQuestion($currentUser->id, $id, $currentNetwork->id, $message_error)) {
973
            return new JsonModel([
974
                'success'   => false,
975
                'data'   => $message_error
976
            ]);
977
        }
978
 
979
 
980
        $myCoachQuestionMapper = MyCoachQuestionMapper::getInstance($this->adapter);
981
        $myCoachQuestion = $myCoachQuestionMapper->fetchOneByUuid($id);
982
 
983
        if ($request->isPost()) {
984
 
985
            $dataPost = array_merge($request->getPost()->toArray(), $request->getFiles()->toArray());
986
            $form = new MyCoachAnswerForm();
987
            $form->setData($dataPost);
988
 
989
            if ($form->isValid()) {
990
 
991
                $myCoachAnswer = new MyCoachAnswer();
992
                $myCoachAnswer->question_id = $myCoachQuestion->id;
993
                $myCoachAnswer->text        = $dataPost['description'];
994
                $myCoachAnswer->user_id     = $currentUser->id;
995
 
996
 
997
                $myCoachAnswerMapper = MyCoachAnswerMapper::getInstance($this->adapter);
998
                if($myCoachAnswerMapper->insert($myCoachAnswer)) {
999
                    $now = $myCoachAnswerMapper->getDatebaseNow();
1000
 
1001
                    $this->logger->info('Se agrego la respuesta ' . $myCoachAnswer->text, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
1002
 
1003
                    $data = [
1004
                        'success'   => true,
1005
                        'data'   => [
1006
                            'answers' => $myCoachAnswerMapper->fetchCountByMyCoachQuestionId($myCoachQuestion->id),
1007
                            'item' => $this->renderAnswer($myCoachAnswer->id, $currentUser->id, $now)
1008
                        ]
1009
                    ];
1010
                } else {
1011
                    $data = [
1012
                        'success'   => false,
1013
                        'data'      => $myCoachQuestionMapper->getError()
1014
                    ];
1015
                }
1016
 
1017
                return new JsonModel($data);
1018
            } else {
1019
                $messages = [];
1020
                $form_messages = (array) $form->getMessages();
1021
                foreach ($form_messages  as $fieldname => $field_messages)
1022
                {
1023
                    $messages[$fieldname] = array_values($field_messages);
1024
                }
1025
 
1026
                return new JsonModel([
1027
                    'success'   => false,
1028
                    'data'   => $messages
1029
                ]);
1030
            }
1031
 
1032
        } else {
1033
            $data = [
1034
                'success' => false,
1035
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
1036
            ];
1037
 
1038
            return new JsonModel($data);
1039
        }
1040
 
1041
 
1042
    }
60 efrain 1043
 
1 efrain 1044
 
1045
 
1046
    public function addCommentAction()
1047
    {
1048
        $currentNetworkPlugin  = $this->plugin('currentNetworkPlugin');
1049
        $currentNetwork        = $currentNetworkPlugin->getNetwork();
1050
 
1051
        $currentUserPlugin  = $this->plugin('currentUserPlugin');
1052
        $currentUser        = $currentUserPlugin->getUser();
1053
 
1054
        $request    = $this->getRequest();
1055
        $id    = $this->params()->fromRoute('id');
1056
 
1057
 
1058
        $request = $this->getRequest();
1059
        if ($request->isPost()) {
1060
 
1061
            $message_error = '';
1062
            $myCoachAccessControl = MyCoachAccessControl::getInstance($this->adapter);
1063
            if(!$myCoachAccessControl->hasAccessViewAnswer($currentUser->id, $id, $currentNetwork->id, $message_error)) {
1064
                return new JsonModel([
1065
                    'success'   => false,
1066
                    'data'   => $message_error
1067
                ]);
1068
            }
1069
 
1070
            $myCoachAnswerMapper = MyCoachAnswerMapper::getInstance($this->adapter);
1071
            $myCoachAnswer = $myCoachAnswerMapper->fetchOneByUuid($id);
1072
 
1073
            $dataPost = $request->getPost()->toArray();
1074
            $form = new CommentForm();
1075
            $form->setData($dataPost);
1076
 
1077
            if ($form->isValid()) {
1078
 
1079
 
1080
                $currentUserPlugin = $this->plugin('currentUserPlugin');
1081
                $currentUser = $currentUserPlugin->getUser();
1082
 
1083
                $dataPost = (array) $form->getData();
1084
 
1085
 
1086
 
1087
                $comment = new Comment();
1088
                $comment->network_id = $currentUser->network_id;
1089
                $comment->comment = $dataPost['comment'];
1090
                $comment->user_id = $currentUser->id;
1091
                $comment->my_coach_answer_id = $myCoachAnswer->id;
1092
                $comment->relational = Comment::RELATIONAL_MY_COACH;
1093
 
1094
                $commentMapper = CommentMapper::getInstance($this->adapter);
1095
                if ($commentMapper->insert($comment)) {
1096
                    $now = $commentMapper->getDatebaseNow();
1097
 
1098
                    $response = [
1099
                        'success'           => true,
1100
                        'data'              => [
1101
                            'item'  => $this->renderComment($comment->id, $now),
1102
                            'total_comments_answer' => intval( $commentMapper->fetchCountByMyCoachAnswerId($myCoachAnswer->id), 10),
1103
                            'total_comments_question' => intval( $commentMapper->fetchCountByMyCoachQuestionId($myCoachAnswer->question_id), 10),
1104
                        ]
1105
                    ];
1106
 
1107
                    return new JsonModel($response);
1108
                } else {
1109
 
1110
                    $response = [
1111
                        'success'   => false,
1112
                        'data'   => $commentMapper->getError()
1113
                    ];
1114
 
1115
                    return new JsonModel($response);
1116
                }
1117
            } else {
1118
                $message = '';;
1119
                $form_messages = (array) $form->getMessages();
1120
                foreach ($form_messages  as $fieldname => $field_messages) {
1121
                    foreach ($field_messages as $key => $value) {
1122
                        $message = $value;
1123
                    }
1124
                }
1125
 
1126
                $response = [
1127
                    'success'   => false,
1128
                    'data'   => $message
1129
                ];
1130
 
1131
                return new JsonModel($response);
1132
            }
1133
        } else {
1134
            $response = [
1135
                'success' => false,
1136
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
1137
            ];
1138
 
1139
            return new JsonModel($response);
1140
        }
1141
    }
1142
 
1143
 
1144
 
1145
    public function deleteCommentAction()
1146
    {
1147
        $currentNetworkPlugin  = $this->plugin('currentNetworkPlugin');
1148
        $currentNetwork        = $currentNetworkPlugin->getNetwork();
1149
 
1150
        $currentUserPlugin      = $this->plugin('currentUserPlugin');
1151
        $currentUser            = $currentUserPlugin->getUser();
1152
 
1153
        $request                = $this->getRequest();
1154
        $id                     = $this->params()->fromRoute('id');
1155
        $comment                = $this->params()->fromRoute('comment');
1156
 
1157
 
1158
        $request = $this->getRequest();
1159
        if ($request->isPost()) {
1160
 
1161
 
1162
 
1163
 
1164
            $message_error = '';
1165
            $myCoachAccessControl = MyCoachAccessControl::getInstance($this->adapter);
1166
            if(!$myCoachAccessControl->hasAccessViewAnswer($currentUser->id, $id, $currentNetwork->id, $message_error)) {
1167
                return new JsonModel([
1168
                    'success'   => false,
1169
                    'data'   => $message_error
1170
                ]);
1171
            }
1172
 
1173
            $myCoachAnswerMapper = MyCoachAnswerMapper::getInstance($this->adapter);
1174
            $myCoachAnswer = $myCoachAnswerMapper->fetchOneByUuid($id);
1175
 
1176
            $commentMapper = CommentMapper::getInstance($this->adapter);
1177
            $comment = $commentMapper->fetchOneByUuid($comment);
1178
 
1179
 
1180
            if ($comment && $comment->my_coach_answer_id == $myCoachAnswer->id && $comment->user_id == $currentUser->id) {
1181
 
1182
                $comment->status = Comment::STATUS_DELETED;
1183
 
1184
                if ($commentMapper->update($comment)) {
1185
 
1186
                    $response = [
1187
                        'success' => true,
1188
                        'data' => [
1189
                            'message' => 'LABEL_COMMENT_WAS_DELETED',
1190
                            'total_comments_answer' => intval( $commentMapper->fetchCountByMyCoachAnswerId($myCoachAnswer->id), 10),
1191
                            'total_comments_question' => intval( $commentMapper->fetchCountByMyCoachQuestionId($myCoachAnswer->question_id), 10),
1192
                        ]
1193
                    ];
1194
                } else {
1195
                    $response = [
1196
                        'success' => false,
1197
                        'data' => $commentMapper->getError()
1198
                    ];
1199
 
1200
                }
1201
            } else {
1202
                $response = [
1203
                    'success' => false,
1204
                    'data' => 'ERROR_COMMENT_NOT_FOUND'
1205
                ];
1206
            }
1207
        } else {
1208
            $response = [
1209
                'success' => false,
1210
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
1211
            ];
1212
        }
1213
 
1214
        return new JsonModel($response);
1215
    }
1216
 
1217
    public function reactionsAction()
1218
    {
1219
        return new JsonModel([
1220
            'success' => false,
1221
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
1222
        ]);
1223
 
1224
    }
1225
 
1226
 
1227
 
1228
    public function saveReactionAction()
1229
    {
1230
        $currentNetworkPlugin  = $this->plugin('currentNetworkPlugin');
1231
        $currentNetwork        = $currentNetworkPlugin->getNetwork();
1232
 
1233
        $currentUserPlugin  = $this->plugin('currentUserPlugin');
1234
        $currentUser        = $currentUserPlugin->getUser();
1235
 
1236
        $request    = $this->getRequest();
1237
 
1238
        $id = $this->params()->fromRoute('id');
1239
        $reaction  = $this->params()->fromPost('reaction');
1240
 
1241
        $request = $this->getRequest();
1242
        if ($request->isPost()) {
1243
 
1244
 
1245
            $message_error = '';
1246
            $myCoachAccessControl = MyCoachAccessControl::getInstance($this->adapter);
1247
            if(!$myCoachAccessControl->hasAccessViewAnswer($currentUser->id, $id, $currentNetwork->id, $message_error)) {
1248
                return new JsonModel([
1249
                    'success'   => false,
1250
                    'data'   => $message_error
1251
                ]);
1252
            }
1253
 
1254
            $reactions = [
1255
                ContentReaction::REACTION_RECOMMENDED,
1256
                ContentReaction::REACTION_SUPPORT,
1257
                ContentReaction::REACTION_LOVE,
1258
                ContentReaction::REACTION_INTEREST,
1259
                ContentReaction::REACTION_FUN
1260
 
1261
            ];
1262
            if(!in_array($reaction, $reactions)) {
1263
                $response = [
1264
                    'success' => false,
1265
                    'data' => 'ERROR_REACTION_NOT_FOUND'
1266
                ];
1267
                return new JsonModel($response);
1268
            }
1269
 
1270
            $myCoachAnswerMapper = MyCoachAnswerMapper::getInstance($this->adapter);
1271
            $myCoachAnswer = $myCoachAnswerMapper->fetchOneByUuid($id);
1272
 
1273
            $contentReactionMapper = ContentReactionMapper::getInstance($this->adapter);
1274
            $contentReaction = $contentReactionMapper->fetchOneByMyCoachAnswerIdAndUserId($myCoachAnswer->id, $currentUser->id);
1275
 
1276
            if ($contentReaction) {
1277
                $contentReaction->reaction = $reaction;
1278
 
1279
                $result = $contentReactionMapper->update($contentReaction);
1280
            } else {
1281
                $contentReaction = new ContentReaction();
1282
                $contentReaction->user_id = $currentUser->id;
1283
                $contentReaction->my_coach_answer_id = $myCoachAnswer->id;
1284
                $contentReaction->relational = ContentReaction::RELATIONAL_MY_COACH;
1285
                $contentReaction->reaction = $reaction;
1286
 
1287
                $result = $contentReactionMapper->insert($contentReaction);
1288
            }
1289
 
1290
 
1291
 
1292
            if ($result) {
1293
 
1294
                $total_reactions_question = intval($contentReactionMapper->fetchCountByMyCoachQuestionId($myCoachAnswer->question_id), 10);
1295
                $total_reactions_answer = intval($contentReactionMapper->fetchCountByMyCoachAnswerId($myCoachAnswer->id), 10);
1296
                $response = [
1297
                    'success' => true,
1298
                    'data' => [
1299
                        'reaction' => $reaction,
1300
                        'total_reactions_question' => $total_reactions_question,
1301
                        'total_reactions_answer' => $total_reactions_answer
1302
                    ]
1303
                ];
1304
            } else {
1305
                $response = [
1306
                    'success' => false,
1307
                    'data' => $contentReactionMapper->getError()
1308
                ];
1309
            }
1310
            return new JsonModel($response);
1311
        }
1312
 
1313
        $response = [
1314
            'success' => false,
1315
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
1316
        ];
1317
        return new JsonModel($response);
1318
    }
1319
 
1320
    public function deleteReactionAction()
1321
    {
1322
        $currentNetworkPlugin  = $this->plugin('currentNetworkPlugin');
1323
        $currentNetwork        = $currentNetworkPlugin->getNetwork();
1324
 
1325
        $currentUserPlugin  = $this->plugin('currentUserPlugin');
1326
        $currentUser        = $currentUserPlugin->getUser();
1327
 
1328
        $request    = $this->getRequest();
1329
 
1330
        $id = $this->params()->fromRoute('id');
1331
 
1332
        $request = $this->getRequest();
1333
        if ($request->isPost()) {
1334
 
1335
            $message_error = '';
1336
            $myCoachAccessControl = MyCoachAccessControl::getInstance($this->adapter);
1337
            if(!$myCoachAccessControl->hasAccessViewAnswer($currentUser->id, $id, $currentNetwork->id, $message_error)) {
1338
                return new JsonModel([
1339
                    'success'   => false,
1340
                    'data'   => $message_error
1341
                ]);
1342
            }
1343
 
1344
            $myCoachAnswerMapper = MyCoachAnswerMapper::getInstance($this->adapter);
1345
            $myCoachAnswer = $myCoachAnswerMapper->fetchOneByUuid($id);
1346
 
1347
 
1348
            $contentReactionMapper = ContentReactionMapper::getInstance($this->adapter);
1349
            $contentReaction = $contentReactionMapper->fetchOneByMyCoachAnswerIdAndUserId($myCoachAnswer->id, $currentUser->id);
1350
 
1351
            if ($contentReaction) {
1352
                if ($contentReactionMapper->deleteByByMyCoachAnswerId($myCoachAnswer->id, $currentUser->id)) {
1353
                    $total_reactions_question = intval($contentReactionMapper->fetchCountByMyCoachQuestionId($myCoachAnswer->question_id), 10);
1354
                    $total_reactions_answer = intval($contentReactionMapper->fetchCountByMyCoachAnswerId($myCoachAnswer->id), 10);
1355
 
1356
                    $response = [
1357
                        'success' => true,
1358
                        'data' => [
1359
                            'total_reactions_question' => $total_reactions_question,
1360
                            'total_reactions_answer' => $total_reactions_answer
1361
                        ]
1362
                    ];
1363
                } else {
1364
                    $response = [
1365
                        'success' => false,
1366
                        'data' => $contentReactionMapper->getError()
1367
                    ];
1368
                }
1369
            } else {
1370
                $total_reactions_question = $contentReactionMapper->fetchCountByMyCoachQuestionId($myCoachAnswer->question_id);
1371
                $total_reactions_answer = $contentReactionMapper->fetchCountByMyCoachAnswerId($myCoachAnswer->id);
1372
 
1373
                $response = [
1374
                    'success' => true,
1375
                    'data' => [
1376
                        'total_reactions_question' => $total_reactions_question,
1377
                        'total_reactions_answer' => $total_reactions_answer
1378
                    ]
1379
                ];
1380
 
1381
 
1382
            }
1383
 
1384
 
1385
 
1386
            return new JsonModel($response);
1387
        }
1388
 
1389
        $response = [
1390
            'success' => false,
1391
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
1392
        ];
1393
        return new JsonModel($response);
1394
    }
1395
 
1396
    public function commentsAction()
1397
    {
1398
        $currentNetworkPlugin  = $this->plugin('currentNetworkPlugin');
1399
        $currentNetwork        = $currentNetworkPlugin->getNetwork();
1400
 
1401
        $currentUserPlugin  = $this->plugin('currentUserPlugin');
1402
        $currentUser        = $currentUserPlugin->getUser();
1403
 
1404
 
1405
        $id = $this->params()->fromRoute('id');
1406
 
1407
        $request = $this->getRequest();
1408
        if ($request->isGet()) {
1409
            $message_error = '';
1410
            $myCoachAccessControl = MyCoachAccessControl::getInstance($this->adapter);
1411
            if(!$myCoachAccessControl->hasAccessViewAnswer($currentUser->id, $id, $currentNetwork->id, $message_error)) {
1412
                return new JsonModel([
1413
                    'success'   => false,
1414
                    'data'   => $message_error
1415
                ]);
1416
            }
1417
 
1418
            $myCoachAnswerMapper = MyCoachAnswerMapper::getInstance($this->adapter);
1419
            $myCoachAnswer = $myCoachAnswerMapper->fetchOneByUuid($id);
1420
            $now = $myCoachAnswerMapper->getDatebaseNow();
1421
 
1422
            $commentMapper = CommentMapper::getInstance($this->adapter);
1423
            $records = $commentMapper->fetchAllPublishedByMyCoachAnswerId($myCoachAnswer->id);
1424
 
1425
            $comments = [];
1426
            foreach ($records as $record) {
1427
                $comment = $this->renderComment($record->id, $now);
1428
                array_push($comments, $comment);
1429
            }
1430
 
1431
            $response = [
1432
                'success' => true,
1433
                'data' => $comments
1434
            ];
1435
 
1436
            return new JsonModel($response);
1437
        } else {
1438
 
1439
 
1440
 
1441
            $response = [
1442
                'success' => false,
1443
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
1444
            ];
1445
 
1446
 
1447
            return new JsonModel($response);
1448
        }
1449
    }
1450
 
1451
 
1452
    private function renderAnswer($answer_id, $current_user_id, $now)
1453
    {
1454
        $currentNetworkPlugin  = $this->plugin('currentNetworkPlugin');
1455
        $currentNetwork        = $currentNetworkPlugin->getNetwork();
1456
        $message_error = '';
1457
 
1458
        $myCoachAnswerMapper = MyCoachAnswerMapper::getInstance($this->adapter);
1459
        $myCoachAnswer =  $myCoachAnswerMapper->fetchOne($answer_id);
1460
 
1461
        $myCoachQuestionMapper = MyCoachQuestionMapper::getInstance($this->adapter);
1462
        $myCoachQuestion = $myCoachQuestionMapper->fetchOne($myCoachAnswer->question_id);
1463
 
1464
 
1465
        $myCoachAccessControl = MyCoachAccessControl::getInstance($this->adapter);
1466
        $hasAccessEditOrDeleteAnswer = $myCoachAccessControl->hasAccessEditOrDeleteAnswer($current_user_id, $answer_id, $currentNetwork->id, $message_error);
1467
        if($hasAccessEditOrDeleteAnswer) {
1468
 
1469
 
1470
            $link_edit = $this->url()->fromRoute('my-coach/questions/answers/edit',['id' => $myCoachQuestion->uuid, 'answer' => $myCoachAnswer->uuid]);
1471
            $link_delete = $this->url()->fromRoute('my-coach/questions/answers/delete',['id' => $myCoachQuestion->uuid, 'answer' => $myCoachAnswer->uuid]);
1472
 
1473
        } else {
1474
            $link_edit = '';
1475
            $link_delete = '';
1476
        }
1477
 
1478
        $userMapper = UserMapper::getInstance($this->adapter);
1479
        $user = $userMapper->fetchOne($myCoachAnswer->user_id);
1480
 
1481
        $contentReactionMapper = ContentReactionMapper::getInstance($this->adapter);
1482
        $contentReaction = $contentReactionMapper->fetchOneByMyCoachAnswerIdAndUserId($myCoachAnswer->id, $current_user_id);
1483
        $total_reactions = intval($contentReactionMapper->fetchCountByMyCoachAnswerId($myCoachAnswer->id), 10);
1484
 
1485
 
1486
 
1487
 
1488
        $comments = [];
1489
        $commentMapper = CommentMapper::getInstance($this->adapter);
1490
        $total_comments = $commentMapper->fetchCountByMyCoachAnswerId($myCoachAnswer->id);
1491
        $records = $commentMapper->fetchAllPublishedByMyCoachAnswerId($myCoachAnswer->id);
1492
        foreach($records as $record)
1493
        {
1494
            $comments[] = $this->renderComment($record->id, $now);
1495
        }
1496
 
1497
 
1498
        $item = [
1499
            'unique' => uniqid(),
1500
            'uuid' => $myCoachAnswer->uuid,
1501
            'question_uuid' => $myCoachQuestion->uuid,
60 efrain 1502
            'user_image' => $this->url()->fromRoute('storage', ['type' => 'user',  'code' => $user->uuid, 'filename' =>  $user->image],['force_canonical' => true]),
1 efrain 1503
            'user_url' => $this->url()->fromRoute('profile/view', ['id' => $user->uuid]),
1504
            'user_name' => $user->first_name . ' ' . $user->last_name,
1505
            'time_elapsed' => Functions::timeAgo($myCoachAnswer->added_on, $now),
1506
            'text' => $myCoachAnswer->text,
1507
            'reaction' => $contentReaction ? $contentReaction->reaction : '',
1508
            'total_comments' => $total_comments,
1509
            'total_reactions' => $total_reactions,
1510
            'comments' => $comments,
1511
            'link_edit' => $link_edit,
1512
            'link_delete' => $link_delete,
1513
            'link_reaction_delete' => $this->url()->fromRoute('my-coach/questions/reaction/delete', ['id' => $myCoachAnswer->uuid]),
1514
            'link_save_reaction' => $this->url()->fromRoute('my-coach/questions/reaction/save', ['id' => $myCoachAnswer->uuid]),
1515
            'link_add_comment' => $this->url()->fromRoute('my-coach/questions/comments/add', ['id' => $myCoachAnswer->uuid]),
1516
        ];
1517
 
1518
        return $item;
1519
 
1520
 
1521
 
1522
    }
1523
 
1524
 
1525
 
1526
    private function renderComment($comment_id, $now)
1527
    {
1528
        $item = [];
1529
 
1530
        $commentMapper = CommentMapper::getInstance($this->adapter);
1531
        $record = $commentMapper->fetchOne($comment_id);
1532
 
1533
        $myCoachAnswerMapper = MyCoachAnswerMapper::getInstance($this->adapter);
1534
        $myCoachAnswer = $myCoachAnswerMapper->fetchOne($record->my_coach_answer_id);
1535
 
1536
 
1537
        if ($record) {
1538
            $userMapper = UserMapper::getInstance($this->adapter);
1539
 
1540
            $user = $userMapper->fetchOne($record->user_id);
1541
 
1542
            $item['unique'] = uniqid();
1543
            $item['answer_uuid'] = $myCoachAnswer->uuid;
60 efrain 1544
            $item['user_image'] = $this->url()->fromRoute('storage', ['type' => 'user',  'code' => $user->uuid, 'filename' =>  $user->image],['force_canonical' => true]);
1 efrain 1545
            $item['user_url'] = $this->url()->fromRoute('profile/view', ['id' => $user->uuid]);
1546
            $item['user_name'] = $user->first_name . ' ' . $user->last_name;
1547
            $item['time_elapsed'] = Functions::timeAgo($record->added_on, $now);
1548
            $item['comment'] = $record->comment;
1549
            $item['link_delete'] = $this->url()->fromRoute('my-coach/questions/comments/delete', ['id' => $myCoachAnswer->uuid, 'comment' => $record->uuid]);
1550
        }
1551
        return $item;
1552
    }
1553
 
1554
}