Proyectos de Subversion LeadersLinked - Services

Rev

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