Proyectos de Subversion LeadersLinked - Services

Rev

Rev 797 | Rev 799 | 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
 
798 stevensc 383
                    // Obtener datos adicionales para formato consistente con questionsAction
384
                    $userMapper = UserMapper::getInstance($this->adapter);
385
                    $user = $userMapper->fetchOne($currentUser->id);
386
 
387
                    $contentReactionMapper = ContentReactionMapper::getInstance($this->adapter);
388
                    $commentMapper = CommentMapper::getInstance($this->adapter);
389
                    $myCoachAnswerMapper = MyCoachAnswerMapper::getInstance($this->adapter);
390
                    $myCoachQuestionViewMapper = MyCoachQuestionViewMapper::getInstance($this->adapter);
391
 
392
                    $storage = Storage::getInstance($this->config, $this->adapter);
393
 
394
                    // Obtener categorías de la pregunta
395
                    $bags_categories = [];
396
                    if(is_array($category_ids)) {
397
                        foreach($category_ids as $category_id) {
398
                            $category = $myCoachCategoryMapper->fetchOneByUuid($category_id);
399
                            if($category) {
400
                                array_push($bags_categories, $category->name);
401
                            }
402
                        }
403
                    } else {
404
                        $category = $myCoachCategoryMapper->fetchOneByUuid($category_ids);
405
                        if($category) {
406
                            array_push($bags_categories, $category->name);
407
                        }
408
                    }
409
 
410
                    // Formatear descripción (limitar a 250 caracteres)
411
                    $description = strip_tags($myCoachQuestion->description);
412
                    if (strlen($description) > 250) {
413
                        $description = substr($description, 0, 250) . '...';
414
                    }
415
 
416
                    // Formatear fechas
417
                    $dt = \DateTime::createFromFormat('Y-m-d H:i:s', $myCoachQuestion->added_on);
418
                    $added_on = $dt->format('d/m/Y H:i a');
419
 
420
                    $dt = \DateTime::createFromFormat('Y-m-d H:i:s', $myCoachQuestion->updated_on);
421
                    $updated_on = $dt->format('d/m/Y H:i a');
422
 
423
                    // Verificar permisos
424
                    $myCoachAccessControl = MyCoachAccessControl::getInstance($this->adapter);
425
                    $message_error = '';
426
                    $allowEdit = $myCoachAccessControl->hasAccessEditQuestion($currentUser->id, $myCoachQuestion->id, $currentNetwork->id, $message_error);
427
                    $allowDelete = $myCoachAccessControl->hasAccessDeleteQuestion($currentUser->id, $myCoachQuestion->id, $currentNetwork->id, $message_error);
428
 
429
                    $questionData = [
430
                        'uuid' => $myCoachQuestion->uuid,
431
                        'user_name' => trim($user->first_name . ' ' . $user->last_name),
432
                        'user_image' => $storage->getUserImage($user),
433
                        'title' => $myCoachQuestion->title,
434
                        'description' => $description,
435
                        'categories' => $bags_categories,
436
                        'views' => 0, // Nueva pregunta, sin vistas aún
437
                        'answers' => 0, // Nueva pregunta, sin respuestas aún
438
                        'reactions' => 0, // Nueva pregunta, sin reacciones aún
439
                        'comments' => 0, // Nueva pregunta, sin comentarios aún
440
                        'added_on' => $added_on,
441
                        'updated_on' => $updated_on,
442
                        'link_add_comment' => $this->url()->fromRoute('my-coach/questions/comments/add', ['id' => $myCoachQuestion->uuid],['force_canonical' => true]),
443
                        'link_view' => $this->url()->fromRoute('my-coach/questions/view', ['id' => $myCoachQuestion->uuid]),
444
                        'link_edit' => $allowEdit ? $this->url()->fromRoute('my-coach/questions/edit', ['id' => $myCoachQuestion->uuid],['force_canonical' => true]) : '',
445
                        'link_delete' => $allowDelete ? $this->url()->fromRoute('my-coach/questions/delete', ['id' => $myCoachQuestion->uuid],['force_canonical' => true]) : '',
446
                        'link_reactions' => $this->url()->fromRoute('my-coach/questions/reactions', ['id' => $myCoachQuestion->uuid],['force_canonical' => true])
447
                    ];
448
 
1 efrain 449
                    $data = [
450
                        'success'   => true,
798 stevensc 451
                        'data'      => [
452
                            'question' => $questionData,
453
                            'message' => 'LABEL_RECORD_ADDED'
454
                        ]
1 efrain 455
                    ];
456
                } else {
457
                    $data = [
458
                        'success'   => false,
459
                        'data'      => $myCoachQuestionMapper->getError()
460
                    ];
461
                }
462
 
463
                return new JsonModel($data);
464
            } else {
465
                $messages = [];
466
                $form_messages = (array) $form->getMessages();
467
                foreach ($form_messages  as $fieldname => $field_messages)
468
                {
469
                    $messages[$fieldname] = array_values($field_messages);
470
                }
471
 
472
                return new JsonModel([
473
                    'success'   => false,
474
                    'data'   => $messages
475
                ]);
476
            }
477
        } else {
478
            $data = [
479
                'success' => false,
480
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
481
            ];
482
 
483
            return new JsonModel($data);
484
        }
485
 
486
        return new JsonModel($data);
487
    }
488
 
489
 
490
 
491
    public function deleteQuestionAction()
492
    {
493
        $currentNetworkPlugin  = $this->plugin('currentNetworkPlugin');
494
        $currentNetwork        = $currentNetworkPlugin->getNetwork();
495
 
496
        $currentUserPlugin  = $this->plugin('currentUserPlugin');
497
        $currentUser        = $currentUserPlugin->getUser();
498
 
499
        $request    = $this->getRequest();
500
        $id         = $this->params()->fromRoute('id');
501
 
502
        $message_error = '';
503
        $myCoachAccessControl = MyCoachAccessControl::getInstance($this->adapter);
504
        if(!$myCoachAccessControl->hasAccessDeleteQuestion($currentUser->id, $id, $currentNetwork->id, $message_error)) {
505
            return new JsonModel([
506
                'success'   => false,
507
                'data'   => $message_error
508
            ]);
509
        }
510
 
511
 
512
        $myCoachQuestionMapper = MyCoachQuestionMapper::getInstance($this->adapter);
513
        $myCoachQuestion = $myCoachQuestionMapper->fetchOneByUuid($id);
514
 
515
 
516
        if ($request->isPost()) {
517
 
518
            $myCoachQuestionCategoryMapper = MyCoachQuestionCategoryMapper::getInstance($this->adapter);
519
            $myCoachQuestionCategoryMapper->deleteAllByQuestionId($myCoachQuestion->id);
520
 
521
            $myCoachAnswerMapper = MyCoachAnswerMapper::getInstance($this->adapter);
522
            $myCoachAnswerMapper->deleteAllByQuestionId($myCoachQuestion->id);
523
 
524
            $result =  $myCoachQuestionMapper->delete($myCoachQuestion);
525
            if ($result) {
526
                $data = [
527
                    'success' => true,
528
                    'data' => 'LABEL_RECORD_DELETED'
529
                ];
530
            } else {
531
 
532
                $data = [
533
                    'success'   => false,
534
                    'data'      => $myCoachQuestionMapper->getError()
535
                ];
536
 
537
                return new JsonModel($data);
538
            }
539
        } else {
540
            $data = [
541
                'success' => false,
542
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
543
            ];
544
 
545
            return new JsonModel($data);
546
        }
547
 
548
        return new JsonModel($data);
549
    }
550
 
551
 
552
    public function editQuestionAction()
553
    {
554
        $currentNetworkPlugin  = $this->plugin('currentNetworkPlugin');
555
        $currentNetwork        = $currentNetworkPlugin->getNetwork();
556
 
557
        $currentUserPlugin  = $this->plugin('currentUserPlugin');
558
        $currentUser        = $currentUserPlugin->getUser();
559
 
560
        $request    = $this->getRequest();
561
        $id    = $this->params()->fromRoute('id');
562
 
563
        $message_error = '';
564
        $myCoachAccessControl = MyCoachAccessControl::getInstance($this->adapter);
565
        if(!$myCoachAccessControl->hasAccessEditQuestion($currentUser->id, $id, $currentNetwork->id, $message_error)) {
566
            return new JsonModel([
567
                'success'   => false,
568
                'data'   => $message_error
569
            ]);
570
        }
571
 
572
 
573
        $myCoachQuestionMapper = MyCoachQuestionMapper::getInstance($this->adapter);
574
        $myCoachQuestion = $myCoachQuestionMapper->fetchOneByUuid($id);
575
 
576
 
577
        if ($request->isGet()) {
578
 
579
            $category_ids = [];
580
            $myCoachCategoryMapper = MyCoachCategoryMapper::getInstance($this->adapter);
581
 
582
            $myCoachQuestionCategoryMapper = MyCoachQuestionCategoryMapper::getInstance($this->adapter);
583
            $records = $myCoachQuestionCategoryMapper->fetchAllByQuestionId($myCoachQuestion->id);
584
            foreach($records as $record)
585
            {
586
                $myCoachCategory = $myCoachCategoryMapper->fetchOne($record->category_id);
587
                if($myCoachCategory) {
588
                    array_push($category_ids,$myCoachCategory->uuid);
589
                }
590
            }
591
 
592
 
593
 
594
            $data = [
595
                'success' => true,
596
                'data' => [
597
                    'category_id' => $category_ids,
598
                    'title' => $myCoachQuestion->title,
599
                    'description' => $myCoachQuestion->description,
600
                ]
601
            ];
602
 
603
            return new JsonModel($data);
604
        } else if ($request->isPost()) {
605
 
606
            $categories = $myCoachAccessControl->getCategoriesWithAccessToFormSelect($myCoachQuestion->user_id, $currentNetwork->id);
607
            $dataPost = array_merge($request->getPost()->toArray(), $request->getFiles()->toArray());
608
 
609
            $form = new MyCoachQuestionForm($categories);
610
            $form->setData($dataPost);
611
 
612
            if ($form->isValid()) {
613
                $dataPost = (array) $form->getData();
614
 
615
 
616
 
617
                $myCoachQuestion->title = $dataPost['title'];
618
                $myCoachQuestion->description = $dataPost['description'];
619
 
620
 
621
                if ($myCoachQuestionMapper->update($myCoachQuestion)) {
622
 
623
 
624
                    $myCoachCategoryMapper = MyCoachCategoryMapper::getInstance($this->adapter);
625
 
626
                    $myCoachQuestionCategoryMapper = MyCoachQuestionCategoryMapper::getInstance($this->adapter);
627
                    $myCoachQuestionCategoryMapper->deleteAllByQuestionId($myCoachQuestion->id);
628
 
629
                    $category_ids = $dataPost['category_id'];
137 efrain 630
                    $category_ids = $dataPost['category_id'];
631
                    if(is_array($category_ids)) {
632
 
633
 
634
                        foreach($category_ids as $category_id)
635
                        {
636
                            $myCoachCategory = $myCoachCategoryMapper->fetchOneByUuid($category_id);
637
                            if($myCoachCategory) {
638
                                $myCoachQuestionCategory = new MyCoachQuestionCategory();
639
                                $myCoachQuestionCategory->category_id = $myCoachCategory->id;
640
                                $myCoachQuestionCategory->question_id = $myCoachQuestion->id;
641
 
642
                                $myCoachQuestionCategoryMapper->insert($myCoachQuestionCategory);
643
                            }
644
                        }
645
                    } else {
646
                        $myCoachCategory = $myCoachCategoryMapper->fetchOneByUuid($category_ids);
1 efrain 647
                        if($myCoachCategory) {
648
                            $myCoachQuestionCategory = new MyCoachQuestionCategory();
649
                            $myCoachQuestionCategory->category_id = $myCoachCategory->id;
650
                            $myCoachQuestionCategory->question_id = $myCoachQuestion->id;
651
 
652
                            $myCoachQuestionCategoryMapper->insert($myCoachQuestionCategory);
653
                        }
654
                    }
655
 
656
 
137 efrain 657
 
1 efrain 658
                    $this->logger->info('Se edito la pregunta ' . $myCoachQuestion->title, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
659
 
660
                    $data = [
661
                        'success'   => true,
662
                        'data'   => 'LABEL_RECORD_UPDATED'
663
                    ];
664
                } else {
665
                    $data = [
666
                        'success'   => false,
667
                        'data'      => $myCoachQuestionMapper->getError()
668
                    ];
669
                }
670
 
671
                return new JsonModel($data);
672
            } else {
673
                $messages = [];
674
                $form_messages = (array) $form->getMessages();
675
                foreach ($form_messages  as $fieldname => $field_messages) {
676
 
677
                    $messages[$fieldname] = array_values($field_messages);
678
                }
679
 
680
                return new JsonModel([
681
                    'success'   => false,
682
                    'data'   => $messages
683
                ]);
684
            }
685
        } else {
686
            $data = [
687
                'success' => false,
688
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
689
            ];
690
 
691
            return new JsonModel($data);
692
        }
693
 
694
        return new JsonModel($data);
695
    }
696
 
697
 
698
    public function viewQuestionAction()
699
    {
700
        $currentUserPlugin = $this->plugin('currentUserPlugin');
701
        $currentUser = $currentUserPlugin->getUser();
702
 
703
        $currentNetworkPlugin = $this->plugin('currentNetworkPlugin');
704
        $currentNetwork = $currentNetworkPlugin->getNetwork();
705
 
706
        $request    = $this->getRequest();
707
        $id    = $this->params()->fromRoute('id');
708
 
709
        $message_error = '';
710
        $myCoachAccessControl = MyCoachAccessControl::getInstance($this->adapter);
711
        if(!$myCoachAccessControl->hasAccessViewQuestion($currentUser->id, $id, $currentNetwork->id, $message_error)) {
712
            return new JsonModel([
713
                'success'   => false,
714
                'data'   => $message_error
715
            ]);
716
        }
717
 
718
 
719
        $myCoachQuestionMapper = MyCoachQuestionMapper::getInstance($this->adapter);
720
        $myCoachQuestion = $myCoachQuestionMapper->fetchOneByUuid($id);
721
 
722
        $contentReactionMapper = ContentReactionMapper::getInstance($this->adapter);
723
        $commentMapper = CommentMapper::getInstance($this->adapter);
724
        $myCoachQuestionViewMapper = MyCoachQuestionViewMapper::getInstance($this->adapter);
725
        $myCoachAnswerMapper = MyCoachAnswerMapper::getInstance($this->adapter);
726
 
727
 
728
        $request = $this->getRequest();
729
        if ($request->isGet()) {
730
 
731
            $categories = [];
732
            $users = [];
733
 
734
 
735
 
736
            $bags_categories = [];
737
 
738
            $myCoachQuestionViewMapper = MyCoachQuestionViewMapper::getInstance($this->adapter);
739
            $myCoachQuestionView = $myCoachQuestionViewMapper->fetchOneByQuestionIdAndUserId($myCoachQuestion->id, $currentUser->id);
740
            if(!$myCoachQuestionView) {
741
                $myCoachQuestionView = new MyCoachQuestionView();
742
                $myCoachQuestionView->question_id = $myCoachQuestion->id;
743
                $myCoachQuestionView->user_id = $currentUser->id;
744
 
745
                $myCoachQuestionViewMapper->insert($myCoachQuestionView);
746
 
747
            }
748
 
749
 
750
 
751
            $myCoachCategoryMapper = MyCoachCategoryMapper::getInstance($this->adapter);
752
            $myCoachQuestionCategoryMapper = MyCoachQuestionCategoryMapper::getInstance($this->adapter);
753
 
754
            $questionCategories = $myCoachQuestionCategoryMapper->fetchAllByQuestionId($myCoachQuestion->id);
755
            foreach($questionCategories as $questionCategory)
756
            {
757
 
758
                if (!isset($categories[$questionCategory->category_id])) {
759
                    $category = $myCoachCategoryMapper->fetchOne($questionCategory->category_id);
760
                    if ($category) {
761
                        $categories[$category->id] = $category->name;
762
                    }
763
                }
764
 
765
                array_push($bags_categories,['category' => $categories[ $questionCategory->category_id ] ]);
766
            }
767
 
768
            $users = [];
769
            $userMapper = UserMapper::getInstance($this->adapter);
770
            if(isset($users[$myCoachQuestion->user_id])) {
771
                $user = $users[$myCoachQuestion->user_id];
772
            } else {
773
                $user = $userMapper->fetchOne( $myCoachQuestion->user_id );
774
                $users[ $myCoachQuestion->user_id ] = $user;
775
            }
776
 
777
 
778
 
779
            $dt = \DateTime::createFromFormat('Y-m-d H:i:s', $myCoachQuestion->added_on);
780
            $added_on = $dt->format('d/m/Y H:i a');
781
 
782
            $dt = \DateTime::createFromFormat('Y-m-d H:i:s', $myCoachQuestion->updated_on);
783
            $updated_on = $dt->format('d/m/Y H:i a');
784
 
785
 
786
 
787
            $allowDelete = $myCoachAccessControl->hasAccessDeleteQuestion($currentUser->id, $myCoachQuestion->id, $currentNetwork->id, $message_error);
788
            if($allowDelete) {
789
                $link_delete = $this->url()->fromRoute('my-coach/questions/delete', ['id' => $myCoachQuestion->uuid]);
790
            } else {
791
                $link_delete = '';
792
            }
793
 
794
            $allowAnswerAdd = $myCoachAccessControl->hasAccessAnswerQuestion($currentUser->id, $myCoachQuestion->id, $currentNetwork->id, $message_error);
795
            if ($allowAnswerAdd) {
796
                $link_answers_add = $this->url()->fromRoute('my-coach/questions/answers/add', ['id' => $myCoachQuestion->uuid]);
797
            } else {
798
                $link_answers_add = '';
799
            }
800
 
801
 
802
 
803
            $myCoachLastAnswer = $myCoachAnswerMapper->fetchOneLastAnswerByQuestionId($myCoachQuestion->id);
804
 
805
            if( $myCoachLastAnswer) {
806
                $dt = \DateTime::createFromFormat('Y-m-d H:i:s', $myCoachLastAnswer->added_on);
807
                $last_answer_on = $dt->format('d/m/Y H:i a');
808
 
809
 
810
 
811
            } else {
812
                $last_answer_on = '';
813
            }
814
 
333 www 815
            $storage = Storage::getInstance($this->config, $this->adapter);
1 efrain 816
 
817
            return new JsonModel([
818
                'success' => true,
819
                'data' => [
820
                    'uuid' => $myCoachQuestion->uuid,
821
                    'user_name' => trim($user->first_name . ' ' . $user->last_name),
283 www 822
                    'user_image' => $storage->getUserImage($user),
1 efrain 823
                    'title' => $myCoachQuestion->title,
824
                    'description' => $myCoachQuestion->description,
825
                    'categories' => $bags_categories,
826
                    'views' => intval($myCoachQuestionViewMapper->fetchCountByQuestionId($myCoachQuestion->id) , 10),
827
                    'answers' => intval($myCoachAnswerMapper->fetchCountByMyCoachQuestionId($myCoachQuestion->id) , 10),
828
                    'reactions' => intval($contentReactionMapper->fetchCountByMyCoachQuestionId($myCoachQuestion->id), 10),
829
                    'comments' => intval($commentMapper->fetchCountByMyCoachQuestionId($myCoachQuestion->id), 10),
830
                    'added_on' => $added_on,
831
                    'updated_on' => $updated_on,
832
                    'last_answer_on' => $last_answer_on,
833
                    'link_answers' => $this->url()->fromRoute('my-coach/questions/answers', ['id' => $myCoachQuestion->uuid]),
834
                    'link_answers_add' => $link_answers_add,
835
                    'link_delete' => $link_delete,
836
                ]
837
 
838
            ]);
839
 
840
 
841
        } else {
842
            return new JsonModel([
843
                'success' => false,
844
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
845
            ]);
846
        }
847
 
848
 
849
 
850
 
851
 
852
    }
853
 
854
    /**
855
     *
856
     * {@inheritDoc}
857
     * @see \Laminas\Mvc\Controller\AbstractActionController::indexAction()
858
     */
859
    public function answersAction()
860
    {
861
        $currentUserPlugin = $this->plugin('currentUserPlugin');
862
        $currentUser = $currentUserPlugin->getUser();
863
 
864
        $currentNetworkPlugin = $this->plugin('currentNetworkPlugin');
865
        $currentNetwork = $currentNetworkPlugin->getNetwork();
866
 
867
        $request    = $this->getRequest();
868
        $id    = $this->params()->fromRoute('id');
869
 
870
        $message_error = '';
871
        $myCoachAccessControl = MyCoachAccessControl::getInstance($this->adapter);
872
        if(!$myCoachAccessControl->hasAccessViewQuestion($currentUser->id, $id, $currentNetwork->id, $message_error)) {
873
            return new JsonModel([
874
                'success'   => false,
875
                'data'   => $message_error
876
            ]);
877
        }
878
 
879
 
880
 
881
        $myCoachQuestionMapper = MyCoachQuestionMapper::getInstance($this->adapter);
882
        $myCoachQuestion = $myCoachQuestionMapper->fetchOneByUuid($id);
883
 
884
        $request = $this->getRequest();
885
        if ($request->isGet()) {
886
 
887
 
888
            $myCoachAnswerMapper = MyCoachAnswerMapper::getInstance($this->adapter);
889
            $now = $myCoachAnswerMapper->getDatebaseNow();
890
 
891
            $records = $myCoachAnswerMapper->fetchAllByQuestionId($myCoachQuestion->id);
892
 
893
 
894
            $items = [];
895
 
896
            foreach($records as $record)
897
            {
898
                $items[] = $this->renderAnswer($record->id, $currentUser->id, $now);
899
            }
900
 
901
 
902
 
903
            return new JsonModel([
904
                'success' => true,
905
                'data' => [
906
                    'items' => $items
907
                ]
908
            ]);
909
 
910
 
911
        } else {
912
            return new JsonModel([
913
                'success' => false,
914
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
915
            ]);
916
        }
917
    }
918
 
919
    public function deleteAnswerAction()
920
    {
921
        $currentNetworkPlugin  = $this->plugin('currentNetworkPlugin');
922
        $currentNetwork        = $currentNetworkPlugin->getNetwork();
923
 
924
        $currentUserPlugin  = $this->plugin('currentUserPlugin');
925
        $currentUser        = $currentUserPlugin->getUser();
926
 
927
        $request    = $this->getRequest();
928
        $answer     = $this->params()->fromRoute('answer');
929
 
930
        $message_error = '';
931
        $myCoachAccessControl = MyCoachAccessControl::getInstance($this->adapter);
932
        if(!$myCoachAccessControl->hasAccessEditOrDeleteAnswer($currentUser->id, $answer, $currentNetwork->id, $message_error)) {
933
            return new JsonModel([
934
                'success'   => false,
935
                'data'   => $message_error
936
            ]);
937
        }
938
 
939
 
940
        $myCoachAnswerMapper = MyCoachAnswerMapper::getInstance($this->adapter);
941
        $myCoachAnswer = $myCoachAnswerMapper->fetchOneByUuid($answer);
942
 
943
        if ($request->isPost()) {
944
 
945
            if($myCoachAnswerMapper->delete($myCoachAnswer)) {
946
 
947
                $contentReactionMapper = ContentReactionMapper::getInstance($this->adapter);
948
                $commentMapper = CommentMapper::getInstance($this->adapter);
949
 
950
 
951
                $this->logger->info('Se borro la respuesta ' . $myCoachAnswer->text, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
952
 
953
                $data = [
954
                    'success'   => true,
955
                    'data'   => [
956
                        'total_comments' => intval($commentMapper->fetchCountByMyCoachQuestionId($myCoachAnswer->question_id), 10),
957
                        'total_answers' => intval($myCoachAnswerMapper->fetchCountByMyCoachQuestionId($myCoachAnswer->question_id), 10),
958
                        'total_reactions' => intval($contentReactionMapper->fetchCountByMyCoachQuestionId($myCoachAnswer->question_id), 10)
959
 
960
                    ]
961
                ];
962
            } else {
963
                $data = [
964
                    'success'   => false,
965
                    'data'      => $myCoachAnswerMapper->getError()
966
                ];
967
            }
968
        } else {
969
            $data = [
970
                'success' => false,
971
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
972
            ];
973
 
974
 
975
        }
976
        return new JsonModel($data);
977
 
978
 
979
    }
980
 
981
 
982
    public function editAnswerAction()
983
    {
984
        $currentNetworkPlugin  = $this->plugin('currentNetworkPlugin');
985
        $currentNetwork        = $currentNetworkPlugin->getNetwork();
986
 
987
        $currentUserPlugin  = $this->plugin('currentUserPlugin');
988
        $currentUser        = $currentUserPlugin->getUser();
989
 
990
        $request    = $this->getRequest();
991
        $answer     = $this->params()->fromRoute('answer');
992
 
993
        $message_error = '';
994
        $myCoachAccessControl = MyCoachAccessControl::getInstance($this->adapter);
995
        if(!$myCoachAccessControl->hasAccessEditOrDeleteAnswer($currentUser->id, $answer, $currentNetwork->id, $message_error)) {
996
            return new JsonModel([
997
                'success'   => false,
998
                'data'   => $message_error
999
            ]);
1000
        }
1001
 
1002
 
1003
        $myCoachAnswerMapper = MyCoachAnswerMapper::getInstance($this->adapter);
1004
        $myCoachAnswer = $myCoachAnswerMapper->fetchOneByUuid($answer);
1005
 
1006
        if ($request->isPost()) {
1007
 
1008
            $dataPost = array_merge($request->getPost()->toArray(), $request->getFiles()->toArray());
1009
            $form = new MyCoachAnswerForm();
1010
            $form->setData($dataPost);
1011
 
1012
            if ($form->isValid()) {
1013
                $myCoachAnswer->text        = $dataPost['description'];
1014
 
1015
                if($myCoachAnswerMapper->update($myCoachAnswer)) {
1016
 
1017
                    $this->logger->info('Se edito la respuesta ' . $myCoachAnswer->text, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
1018
 
1019
                    $data = [
1020
                        'success'   => true,
1021
                        'data'   => [
1022
                            'description' => $myCoachAnswer->text
1023
                        ]
1024
                    ];
1025
                } else {
1026
                    $data = [
1027
                        'success'   => false,
1028
                        'data'      => $myCoachAnswerMapper->getError()
1029
                    ];
1030
                }
1031
 
1032
                return new JsonModel($data);
1033
            } else {
1034
                $messages = [];
1035
                $form_messages = (array) $form->getMessages();
1036
                foreach ($form_messages  as $fieldname => $field_messages)
1037
                {
1038
                    $messages[$fieldname] = array_values($field_messages);
1039
                }
1040
 
1041
                return new JsonModel([
1042
                    'success'   => false,
1043
                    'data'   => $messages
1044
                ]);
1045
            }
1046
        } else  if ($request->isGet()) {
1047
            return new JsonModel([
1048
                'success'   => true,
1049
                'data'   => [
1050
                    'description' => $myCoachAnswer->text
1051
                ]
1052
            ]);
1053
        } else {
1054
            $data = [
1055
                'success' => false,
1056
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
1057
            ];
1058
 
1059
            return new JsonModel($data);
1060
        }
1061
 
1062
 
1063
    }
1064
 
1065
 
1066
 
1067
 
1068
 
1069
    public function addAnswerAction()
1070
    {
1071
        $currentNetworkPlugin  = $this->plugin('currentNetworkPlugin');
1072
        $currentNetwork        = $currentNetworkPlugin->getNetwork();
1073
 
1074
        $currentUserPlugin  = $this->plugin('currentUserPlugin');
1075
        $currentUser        = $currentUserPlugin->getUser();
1076
 
1077
        $request    = $this->getRequest();
1078
        $id    = $this->params()->fromRoute('id');
1079
 
1080
        $message_error = '';
1081
        $myCoachAccessControl = MyCoachAccessControl::getInstance($this->adapter);
1082
        if(!$myCoachAccessControl->hasAccessAnswerQuestion($currentUser->id, $id, $currentNetwork->id, $message_error)) {
1083
            return new JsonModel([
1084
                'success'   => false,
1085
                'data'   => $message_error
1086
            ]);
1087
        }
1088
 
1089
 
1090
        $myCoachQuestionMapper = MyCoachQuestionMapper::getInstance($this->adapter);
1091
        $myCoachQuestion = $myCoachQuestionMapper->fetchOneByUuid($id);
1092
 
1093
        if ($request->isPost()) {
1094
 
1095
            $dataPost = array_merge($request->getPost()->toArray(), $request->getFiles()->toArray());
1096
            $form = new MyCoachAnswerForm();
1097
            $form->setData($dataPost);
1098
 
1099
            if ($form->isValid()) {
1100
 
1101
                $myCoachAnswer = new MyCoachAnswer();
1102
                $myCoachAnswer->question_id = $myCoachQuestion->id;
1103
                $myCoachAnswer->text        = $dataPost['description'];
1104
                $myCoachAnswer->user_id     = $currentUser->id;
1105
 
1106
 
1107
                $myCoachAnswerMapper = MyCoachAnswerMapper::getInstance($this->adapter);
1108
                if($myCoachAnswerMapper->insert($myCoachAnswer)) {
1109
                    $now = $myCoachAnswerMapper->getDatebaseNow();
1110
 
1111
                    $this->logger->info('Se agrego la respuesta ' . $myCoachAnswer->text, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
1112
 
1113
                    $data = [
1114
                        'success'   => true,
1115
                        'data'   => [
1116
                            'answers' => $myCoachAnswerMapper->fetchCountByMyCoachQuestionId($myCoachQuestion->id),
1117
                            'item' => $this->renderAnswer($myCoachAnswer->id, $currentUser->id, $now)
1118
                        ]
1119
                    ];
1120
                } else {
1121
                    $data = [
1122
                        'success'   => false,
1123
                        'data'      => $myCoachQuestionMapper->getError()
1124
                    ];
1125
                }
1126
 
1127
                return new JsonModel($data);
1128
            } else {
1129
                $messages = [];
1130
                $form_messages = (array) $form->getMessages();
1131
                foreach ($form_messages  as $fieldname => $field_messages)
1132
                {
1133
                    $messages[$fieldname] = array_values($field_messages);
1134
                }
1135
 
1136
                return new JsonModel([
1137
                    'success'   => false,
1138
                    'data'   => $messages
1139
                ]);
1140
            }
1141
 
1142
        } else {
1143
            $data = [
1144
                'success' => false,
1145
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
1146
            ];
1147
 
1148
            return new JsonModel($data);
1149
        }
1150
 
1151
 
1152
    }
60 efrain 1153
 
1 efrain 1154
 
1155
 
1156
    public function addCommentAction()
1157
    {
1158
        $currentNetworkPlugin  = $this->plugin('currentNetworkPlugin');
1159
        $currentNetwork        = $currentNetworkPlugin->getNetwork();
1160
 
1161
        $currentUserPlugin  = $this->plugin('currentUserPlugin');
1162
        $currentUser        = $currentUserPlugin->getUser();
1163
 
1164
        $request    = $this->getRequest();
1165
        $id    = $this->params()->fromRoute('id');
1166
 
1167
 
1168
        $request = $this->getRequest();
1169
        if ($request->isPost()) {
1170
 
1171
            $message_error = '';
1172
            $myCoachAccessControl = MyCoachAccessControl::getInstance($this->adapter);
1173
            if(!$myCoachAccessControl->hasAccessViewAnswer($currentUser->id, $id, $currentNetwork->id, $message_error)) {
1174
                return new JsonModel([
1175
                    'success'   => false,
1176
                    'data'   => $message_error
1177
                ]);
1178
            }
1179
 
1180
            $myCoachAnswerMapper = MyCoachAnswerMapper::getInstance($this->adapter);
1181
            $myCoachAnswer = $myCoachAnswerMapper->fetchOneByUuid($id);
1182
 
1183
            $dataPost = $request->getPost()->toArray();
1184
            $form = new CommentForm();
1185
            $form->setData($dataPost);
1186
 
1187
            if ($form->isValid()) {
1188
 
1189
 
1190
                $currentUserPlugin = $this->plugin('currentUserPlugin');
1191
                $currentUser = $currentUserPlugin->getUser();
1192
 
1193
                $dataPost = (array) $form->getData();
1194
 
1195
 
1196
 
1197
                $comment = new Comment();
1198
                $comment->network_id = $currentUser->network_id;
1199
                $comment->comment = $dataPost['comment'];
1200
                $comment->user_id = $currentUser->id;
1201
                $comment->my_coach_answer_id = $myCoachAnswer->id;
1202
                $comment->relational = Comment::RELATIONAL_MY_COACH;
1203
 
1204
                $commentMapper = CommentMapper::getInstance($this->adapter);
1205
                if ($commentMapper->insert($comment)) {
1206
                    $now = $commentMapper->getDatebaseNow();
1207
 
1208
                    $response = [
1209
                        'success'           => true,
1210
                        'data'              => [
1211
                            'item'  => $this->renderComment($comment->id, $now),
1212
                            'total_comments_answer' => intval( $commentMapper->fetchCountByMyCoachAnswerId($myCoachAnswer->id), 10),
1213
                            'total_comments_question' => intval( $commentMapper->fetchCountByMyCoachQuestionId($myCoachAnswer->question_id), 10),
1214
                        ]
1215
                    ];
1216
 
1217
                    return new JsonModel($response);
1218
                } else {
1219
 
1220
                    $response = [
1221
                        'success'   => false,
1222
                        'data'   => $commentMapper->getError()
1223
                    ];
1224
 
1225
                    return new JsonModel($response);
1226
                }
1227
            } else {
1228
                $message = '';;
1229
                $form_messages = (array) $form->getMessages();
1230
                foreach ($form_messages  as $fieldname => $field_messages) {
1231
                    foreach ($field_messages as $key => $value) {
1232
                        $message = $value;
1233
                    }
1234
                }
1235
 
1236
                $response = [
1237
                    'success'   => false,
1238
                    'data'   => $message
1239
                ];
1240
 
1241
                return new JsonModel($response);
1242
            }
1243
        } else {
1244
            $response = [
1245
                'success' => false,
1246
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
1247
            ];
1248
 
1249
            return new JsonModel($response);
1250
        }
1251
    }
1252
 
1253
 
1254
 
1255
    public function deleteCommentAction()
1256
    {
1257
        $currentNetworkPlugin  = $this->plugin('currentNetworkPlugin');
1258
        $currentNetwork        = $currentNetworkPlugin->getNetwork();
1259
 
1260
        $currentUserPlugin      = $this->plugin('currentUserPlugin');
1261
        $currentUser            = $currentUserPlugin->getUser();
1262
 
1263
        $request                = $this->getRequest();
1264
        $id                     = $this->params()->fromRoute('id');
1265
        $comment                = $this->params()->fromRoute('comment');
1266
 
1267
 
1268
        $request = $this->getRequest();
1269
        if ($request->isPost()) {
1270
 
1271
 
1272
 
1273
 
1274
            $message_error = '';
1275
            $myCoachAccessControl = MyCoachAccessControl::getInstance($this->adapter);
1276
            if(!$myCoachAccessControl->hasAccessViewAnswer($currentUser->id, $id, $currentNetwork->id, $message_error)) {
1277
                return new JsonModel([
1278
                    'success'   => false,
1279
                    'data'   => $message_error
1280
                ]);
1281
            }
1282
 
1283
            $myCoachAnswerMapper = MyCoachAnswerMapper::getInstance($this->adapter);
1284
            $myCoachAnswer = $myCoachAnswerMapper->fetchOneByUuid($id);
1285
 
1286
            $commentMapper = CommentMapper::getInstance($this->adapter);
1287
            $comment = $commentMapper->fetchOneByUuid($comment);
1288
 
1289
 
1290
            if ($comment && $comment->my_coach_answer_id == $myCoachAnswer->id && $comment->user_id == $currentUser->id) {
1291
 
1292
                $comment->status = Comment::STATUS_DELETED;
1293
 
1294
                if ($commentMapper->update($comment)) {
1295
 
1296
                    $response = [
1297
                        'success' => true,
1298
                        'data' => [
1299
                            'message' => 'LABEL_COMMENT_WAS_DELETED',
1300
                            'total_comments_answer' => intval( $commentMapper->fetchCountByMyCoachAnswerId($myCoachAnswer->id), 10),
1301
                            'total_comments_question' => intval( $commentMapper->fetchCountByMyCoachQuestionId($myCoachAnswer->question_id), 10),
1302
                        ]
1303
                    ];
1304
                } else {
1305
                    $response = [
1306
                        'success' => false,
1307
                        'data' => $commentMapper->getError()
1308
                    ];
1309
 
1310
                }
1311
            } else {
1312
                $response = [
1313
                    'success' => false,
1314
                    'data' => 'ERROR_COMMENT_NOT_FOUND'
1315
                ];
1316
            }
1317
        } else {
1318
            $response = [
1319
                'success' => false,
1320
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
1321
            ];
1322
        }
1323
 
1324
        return new JsonModel($response);
1325
    }
1326
 
242 efrain 1327
    public function reactionAction()
1 efrain 1328
    {
1329
        return new JsonModel([
1330
            'success' => false,
1331
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
1332
        ]);
1333
 
1334
    }
1335
 
242 efrain 1336
    public function reactionsAction()
1337
    {
1338
        $id = $this->params()->fromRoute('id');
1339
 
1340
        $request = $this->getRequest();
1341
        if ($request->isGet()) {
1342
 
1343
            $currentNetworkPlugin  = $this->plugin('currentNetworkPlugin');
1344
            $currentNetwork        = $currentNetworkPlugin->getNetwork();
1345
 
1346
            $currentUserPlugin  = $this->plugin('currentUserPlugin');
1347
            $currentUser        = $currentUserPlugin->getUser();
1348
 
1349
            $message_error = '';
1350
            $myCoachAccessControl = MyCoachAccessControl::getInstance($this->adapter);
1351
            if(!$myCoachAccessControl->hasAccessViewQuestion($currentUser->id, $id, $currentNetwork->id, $message_error)) {
1352
                return new JsonModel([
1353
                    'success'   => false,
1354
                    'data'   => $message_error
1355
                ]);
1356
            }
1357
 
1358
            $myCoachQuestionMapper = MyCoachQuestionMapper::getInstance($this->adapter);
1359
            $myCoachQuestion = $myCoachQuestionMapper->fetchOneByUuid($id);
1360
 
1361
 
1362
            $userMapper = UserMapper::getInstance($this->adapter);
1363
 
1364
            $items = [];
1365
 
1366
            $contentReactionMapper = ContentReactionMapper::getInstance($this->adapter);
1367
            $records = $contentReactionMapper->fetchAllByMyCoachQuestionId($myCoachQuestion->id);
1368
 
333 www 1369
            $storage = Storage::getInstance($this->config, $this->adapter);
283 www 1370
 
1371
 
242 efrain 1372
            foreach($records as $record)
1373
            {
1374
                $user = $userMapper->fetchOne($record->user_id);
1375
                if($user && $user->status == User::STATUS_ACTIVE) {
1376
 
1377
                    array_push($items, [
1378
                        'first_name' => $user->first_name,
1379
                        'last_name' => $user->last_name,
1380
                        'email' => $user->email,
283 www 1381
                        'image' => $storage->getUserImage($user),
242 efrain 1382
                        'reaction' => $record->reaction,
1383
                    ]);
1384
                }
1385
            }
1386
 
1387
            $response = [
1388
                'success' => true,
1389
                'data' => $items
1390
            ];
1391
 
1392
            return new JsonModel($response);
1393
 
1394
 
1395
 
1396
 
1397
        } else {
1398
            $response = [
1399
                'success' => false,
1400
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
1401
            ];
1402
 
1403
            return new JsonModel($response);
1404
        }
1405
    }
1 efrain 1406
 
1407
 
242 efrain 1408
 
1 efrain 1409
    public function saveReactionAction()
1410
    {
1411
        $currentNetworkPlugin  = $this->plugin('currentNetworkPlugin');
1412
        $currentNetwork        = $currentNetworkPlugin->getNetwork();
1413
 
1414
        $currentUserPlugin  = $this->plugin('currentUserPlugin');
1415
        $currentUser        = $currentUserPlugin->getUser();
1416
 
1417
        $request    = $this->getRequest();
1418
 
1419
        $id = $this->params()->fromRoute('id');
1420
        $reaction  = $this->params()->fromPost('reaction');
1421
 
1422
        $request = $this->getRequest();
1423
        if ($request->isPost()) {
1424
 
1425
 
1426
            $message_error = '';
1427
            $myCoachAccessControl = MyCoachAccessControl::getInstance($this->adapter);
1428
            if(!$myCoachAccessControl->hasAccessViewAnswer($currentUser->id, $id, $currentNetwork->id, $message_error)) {
1429
                return new JsonModel([
1430
                    'success'   => false,
1431
                    'data'   => $message_error
1432
                ]);
1433
            }
1434
 
1435
            $reactions = [
1436
                ContentReaction::REACTION_RECOMMENDED,
1437
                ContentReaction::REACTION_SUPPORT,
1438
                ContentReaction::REACTION_LOVE,
1439
                ContentReaction::REACTION_INTEREST,
1440
                ContentReaction::REACTION_FUN
1441
 
1442
            ];
1443
            if(!in_array($reaction, $reactions)) {
1444
                $response = [
1445
                    'success' => false,
1446
                    'data' => 'ERROR_REACTION_NOT_FOUND'
1447
                ];
1448
                return new JsonModel($response);
1449
            }
1450
 
1451
            $myCoachAnswerMapper = MyCoachAnswerMapper::getInstance($this->adapter);
1452
            $myCoachAnswer = $myCoachAnswerMapper->fetchOneByUuid($id);
1453
 
1454
            $contentReactionMapper = ContentReactionMapper::getInstance($this->adapter);
1455
            $contentReaction = $contentReactionMapper->fetchOneByMyCoachAnswerIdAndUserId($myCoachAnswer->id, $currentUser->id);
1456
 
1457
            if ($contentReaction) {
1458
                $contentReaction->reaction = $reaction;
1459
 
1460
                $result = $contentReactionMapper->update($contentReaction);
1461
            } else {
1462
                $contentReaction = new ContentReaction();
1463
                $contentReaction->user_id = $currentUser->id;
1464
                $contentReaction->my_coach_answer_id = $myCoachAnswer->id;
1465
                $contentReaction->relational = ContentReaction::RELATIONAL_MY_COACH;
1466
                $contentReaction->reaction = $reaction;
1467
 
1468
                $result = $contentReactionMapper->insert($contentReaction);
1469
            }
1470
 
1471
 
1472
 
1473
            if ($result) {
1474
 
1475
                $total_reactions_question = intval($contentReactionMapper->fetchCountByMyCoachQuestionId($myCoachAnswer->question_id), 10);
1476
                $total_reactions_answer = intval($contentReactionMapper->fetchCountByMyCoachAnswerId($myCoachAnswer->id), 10);
1477
                $response = [
1478
                    'success' => true,
1479
                    'data' => [
1480
                        'reaction' => $reaction,
1481
                        'total_reactions_question' => $total_reactions_question,
1482
                        'total_reactions_answer' => $total_reactions_answer
1483
                    ]
1484
                ];
1485
            } else {
1486
                $response = [
1487
                    'success' => false,
1488
                    'data' => $contentReactionMapper->getError()
1489
                ];
1490
            }
1491
            return new JsonModel($response);
1492
        }
1493
 
1494
        $response = [
1495
            'success' => false,
1496
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
1497
        ];
1498
        return new JsonModel($response);
1499
    }
1500
 
1501
    public function deleteReactionAction()
1502
    {
1503
        $currentNetworkPlugin  = $this->plugin('currentNetworkPlugin');
1504
        $currentNetwork        = $currentNetworkPlugin->getNetwork();
1505
 
1506
        $currentUserPlugin  = $this->plugin('currentUserPlugin');
1507
        $currentUser        = $currentUserPlugin->getUser();
1508
 
1509
        $request    = $this->getRequest();
1510
 
1511
        $id = $this->params()->fromRoute('id');
1512
 
1513
        $request = $this->getRequest();
1514
        if ($request->isPost()) {
1515
 
1516
            $message_error = '';
1517
            $myCoachAccessControl = MyCoachAccessControl::getInstance($this->adapter);
1518
            if(!$myCoachAccessControl->hasAccessViewAnswer($currentUser->id, $id, $currentNetwork->id, $message_error)) {
1519
                return new JsonModel([
1520
                    'success'   => false,
1521
                    'data'   => $message_error
1522
                ]);
1523
            }
1524
 
1525
            $myCoachAnswerMapper = MyCoachAnswerMapper::getInstance($this->adapter);
1526
            $myCoachAnswer = $myCoachAnswerMapper->fetchOneByUuid($id);
1527
 
1528
 
1529
            $contentReactionMapper = ContentReactionMapper::getInstance($this->adapter);
1530
            $contentReaction = $contentReactionMapper->fetchOneByMyCoachAnswerIdAndUserId($myCoachAnswer->id, $currentUser->id);
1531
 
1532
            if ($contentReaction) {
1533
                if ($contentReactionMapper->deleteByByMyCoachAnswerId($myCoachAnswer->id, $currentUser->id)) {
1534
                    $total_reactions_question = intval($contentReactionMapper->fetchCountByMyCoachQuestionId($myCoachAnswer->question_id), 10);
1535
                    $total_reactions_answer = intval($contentReactionMapper->fetchCountByMyCoachAnswerId($myCoachAnswer->id), 10);
1536
 
1537
                    $response = [
1538
                        'success' => true,
1539
                        'data' => [
1540
                            'total_reactions_question' => $total_reactions_question,
1541
                            'total_reactions_answer' => $total_reactions_answer
1542
                        ]
1543
                    ];
1544
                } else {
1545
                    $response = [
1546
                        'success' => false,
1547
                        'data' => $contentReactionMapper->getError()
1548
                    ];
1549
                }
1550
            } else {
1551
                $total_reactions_question = $contentReactionMapper->fetchCountByMyCoachQuestionId($myCoachAnswer->question_id);
1552
                $total_reactions_answer = $contentReactionMapper->fetchCountByMyCoachAnswerId($myCoachAnswer->id);
1553
 
1554
                $response = [
1555
                    'success' => true,
1556
                    'data' => [
1557
                        'total_reactions_question' => $total_reactions_question,
1558
                        'total_reactions_answer' => $total_reactions_answer
1559
                    ]
1560
                ];
1561
 
1562
 
1563
            }
1564
 
1565
 
1566
 
1567
            return new JsonModel($response);
1568
        }
1569
 
1570
        $response = [
1571
            'success' => false,
1572
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
1573
        ];
1574
        return new JsonModel($response);
1575
    }
1576
 
1577
    public function commentsAction()
1578
    {
1579
        $currentNetworkPlugin  = $this->plugin('currentNetworkPlugin');
1580
        $currentNetwork        = $currentNetworkPlugin->getNetwork();
1581
 
1582
        $currentUserPlugin  = $this->plugin('currentUserPlugin');
1583
        $currentUser        = $currentUserPlugin->getUser();
1584
 
1585
 
1586
        $id = $this->params()->fromRoute('id');
1587
 
1588
        $request = $this->getRequest();
1589
        if ($request->isGet()) {
1590
            $message_error = '';
1591
            $myCoachAccessControl = MyCoachAccessControl::getInstance($this->adapter);
1592
            if(!$myCoachAccessControl->hasAccessViewAnswer($currentUser->id, $id, $currentNetwork->id, $message_error)) {
1593
                return new JsonModel([
1594
                    'success'   => false,
1595
                    'data'   => $message_error
1596
                ]);
1597
            }
1598
 
1599
            $myCoachAnswerMapper = MyCoachAnswerMapper::getInstance($this->adapter);
1600
            $myCoachAnswer = $myCoachAnswerMapper->fetchOneByUuid($id);
1601
            $now = $myCoachAnswerMapper->getDatebaseNow();
1602
 
1603
            $commentMapper = CommentMapper::getInstance($this->adapter);
1604
            $records = $commentMapper->fetchAllPublishedByMyCoachAnswerId($myCoachAnswer->id);
1605
 
1606
            $comments = [];
1607
            foreach ($records as $record) {
1608
                $comment = $this->renderComment($record->id, $now);
1609
                array_push($comments, $comment);
1610
            }
1611
 
1612
            $response = [
1613
                'success' => true,
1614
                'data' => $comments
1615
            ];
1616
 
1617
            return new JsonModel($response);
1618
        } else {
1619
 
1620
 
1621
 
1622
            $response = [
1623
                'success' => false,
1624
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
1625
            ];
1626
 
1627
 
1628
            return new JsonModel($response);
1629
        }
1630
    }
1631
 
1632
 
1633
    private function renderAnswer($answer_id, $current_user_id, $now)
1634
    {
1635
        $currentNetworkPlugin  = $this->plugin('currentNetworkPlugin');
1636
        $currentNetwork        = $currentNetworkPlugin->getNetwork();
1637
        $message_error = '';
1638
 
1639
        $myCoachAnswerMapper = MyCoachAnswerMapper::getInstance($this->adapter);
1640
        $myCoachAnswer =  $myCoachAnswerMapper->fetchOne($answer_id);
1641
 
1642
        $myCoachQuestionMapper = MyCoachQuestionMapper::getInstance($this->adapter);
1643
        $myCoachQuestion = $myCoachQuestionMapper->fetchOne($myCoachAnswer->question_id);
1644
 
1645
 
1646
        $myCoachAccessControl = MyCoachAccessControl::getInstance($this->adapter);
1647
        $hasAccessEditOrDeleteAnswer = $myCoachAccessControl->hasAccessEditOrDeleteAnswer($current_user_id, $answer_id, $currentNetwork->id, $message_error);
1648
        if($hasAccessEditOrDeleteAnswer) {
1649
 
1650
 
1651
            $link_edit = $this->url()->fromRoute('my-coach/questions/answers/edit',['id' => $myCoachQuestion->uuid, 'answer' => $myCoachAnswer->uuid]);
1652
            $link_delete = $this->url()->fromRoute('my-coach/questions/answers/delete',['id' => $myCoachQuestion->uuid, 'answer' => $myCoachAnswer->uuid]);
1653
 
1654
        } else {
1655
            $link_edit = '';
1656
            $link_delete = '';
1657
        }
1658
 
1659
        $userMapper = UserMapper::getInstance($this->adapter);
1660
        $user = $userMapper->fetchOne($myCoachAnswer->user_id);
1661
 
1662
        $contentReactionMapper = ContentReactionMapper::getInstance($this->adapter);
1663
        $contentReaction = $contentReactionMapper->fetchOneByMyCoachAnswerIdAndUserId($myCoachAnswer->id, $current_user_id);
1664
        $total_reactions = intval($contentReactionMapper->fetchCountByMyCoachAnswerId($myCoachAnswer->id), 10);
1665
 
1666
 
1667
 
1668
 
1669
        $comments = [];
1670
        $commentMapper = CommentMapper::getInstance($this->adapter);
1671
        $total_comments = $commentMapper->fetchCountByMyCoachAnswerId($myCoachAnswer->id);
1672
        $records = $commentMapper->fetchAllPublishedByMyCoachAnswerId($myCoachAnswer->id);
283 www 1673
 
1 efrain 1674
        foreach($records as $record)
1675
        {
1676
            $comments[] = $this->renderComment($record->id, $now);
1677
        }
1678
 
333 www 1679
        $storage = Storage::getInstance($this->config, $this->adapter);
1 efrain 1680
 
1681
        $item = [
1682
            'unique' => uniqid(),
1683
            'uuid' => $myCoachAnswer->uuid,
1684
            'question_uuid' => $myCoachQuestion->uuid,
283 www 1685
            'user_image' => $storage->getUserImage($user),
1 efrain 1686
            'user_url' => $this->url()->fromRoute('profile/view', ['id' => $user->uuid]),
1687
            'user_name' => $user->first_name . ' ' . $user->last_name,
1688
            'time_elapsed' => Functions::timeAgo($myCoachAnswer->added_on, $now),
1689
            'text' => $myCoachAnswer->text,
1690
            'reaction' => $contentReaction ? $contentReaction->reaction : '',
1691
            'total_comments' => $total_comments,
1692
            'total_reactions' => $total_reactions,
1693
            'comments' => $comments,
1694
            'link_edit' => $link_edit,
1695
            'link_delete' => $link_delete,
1696
            'link_reaction_delete' => $this->url()->fromRoute('my-coach/questions/reaction/delete', ['id' => $myCoachAnswer->uuid]),
1697
            'link_save_reaction' => $this->url()->fromRoute('my-coach/questions/reaction/save', ['id' => $myCoachAnswer->uuid]),
1698
            'link_add_comment' => $this->url()->fromRoute('my-coach/questions/comments/add', ['id' => $myCoachAnswer->uuid]),
1699
        ];
1700
 
1701
        return $item;
1702
 
1703
 
1704
 
1705
    }
1706
 
1707
 
1708
 
1709
    private function renderComment($comment_id, $now)
1710
    {
1711
        $item = [];
1712
 
1713
        $commentMapper = CommentMapper::getInstance($this->adapter);
1714
        $record = $commentMapper->fetchOne($comment_id);
1715
 
1716
        $myCoachAnswerMapper = MyCoachAnswerMapper::getInstance($this->adapter);
1717
        $myCoachAnswer = $myCoachAnswerMapper->fetchOne($record->my_coach_answer_id);
1718
 
1719
 
1720
        if ($record) {
1721
            $userMapper = UserMapper::getInstance($this->adapter);
1722
 
1723
            $user = $userMapper->fetchOne($record->user_id);
1724
 
333 www 1725
            $storage = Storage::getInstance($this->config, $this->adapter);
283 www 1726
 
1 efrain 1727
            $item['unique'] = uniqid();
1728
            $item['answer_uuid'] = $myCoachAnswer->uuid;
283 www 1729
            $item['user_image'] = $storage->getUserImage($user);
1 efrain 1730
            $item['user_url'] = $this->url()->fromRoute('profile/view', ['id' => $user->uuid]);
1731
            $item['user_name'] = $user->first_name . ' ' . $user->last_name;
1732
            $item['time_elapsed'] = Functions::timeAgo($record->added_on, $now);
1733
            $item['comment'] = $record->comment;
1734
            $item['link_delete'] = $this->url()->fromRoute('my-coach/questions/comments/delete', ['id' => $myCoachAnswer->uuid, 'comment' => $record->uuid]);
1735
        }
1736
        return $item;
1737
    }
1738
 
1739
}