Proyectos de Subversion LeadersLinked - Services

Rev

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