Proyectos de Subversion LeadersLinked - Services

Rev

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