Proyectos de Subversion LeadersLinked - Services

Rev

| 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
 
7
use Laminas\Db\Adapter\AdapterInterface;
8
 
9
use Laminas\Mvc\Controller\AbstractActionController;
10
use Laminas\Log\LoggerInterface;
11
use Laminas\View\Model\JsonModel;
283 www 12
use LeadersLinked\Mapper\MicrolearningCapsuleMapper;
13
use LeadersLinked\Mapper\MicrolearningCapsuleUserMapper;
14
use LeadersLinked\Mapper\MicrolearningCapsuleCommentMapper;
1 efrain 15
use LeadersLinked\Form\Service\CapsuleCommentForm;
283 www 16
use LeadersLinked\Model\MicrolearningCapsuleComment;
1 efrain 17
use LeadersLinked\Mapper\UserMapper;
283 www 18
use LeadersLinked\Library\Storage;
1 efrain 19
 
20
 
21
class ServiceCapsuleCommentsController extends AbstractActionController
22
{
23
    /**
24
     *
25
     * @var \Laminas\Db\Adapter\AdapterInterface
26
     */
27
    private $adapter;
28
 
29
    /**
30
     *
31
     * @var \LeadersLinked\Cache\CacheInterface
32
     */
33
    private $cache;
34
 
35
 
36
    /**
37
     *
38
     * @var \Laminas\Log\LoggerInterface
39
     */
40
    private $logger;
41
 
42
    /**
43
     *
44
     * @var array
45
     */
46
    private $config;
47
 
48
 
49
    /**
50
     *
51
     * @var \Laminas\Mvc\I18n\Translator
52
     */
53
    private $translator;
54
 
55
 
56
    /**
57
     *
58
     * @param \Laminas\Db\Adapter\AdapterInterface $adapter
59
     * @param \LeadersLinked\Cache\CacheInterface $cache
60
     * @param \Laminas\Log\LoggerInterface LoggerInterface $logger
61
     * @param array $config
62
     * @param \Laminas\Mvc\I18n\Translator $translator
63
     */
64
    public function __construct($adapter, $cache, $logger, $config, $translator)
65
    {
66
        $this->adapter      = $adapter;
67
        $this->cache        = $cache;
68
        $this->logger       = $logger;
69
        $this->config       = $config;
70
        $this->translator   = $translator;
71
    }
72
 
73
    public function indexAction()
74
    {
75
 
76
        $request = $this->getRequest();
77
 
78
        if($request->isGet()) {
79
            $serviceDatetimeFormat = $this->config['leaderslinked.services.datetime'];
80
 
81
            $currentUserPlugin = $this->plugin('currentUserPlugin');
82
            $currentUser = $currentUserPlugin->getUser();
83
 
84
            $capsule_id = $this->params()->fromRoute('capsule_id');
85
 
283 www 86
            $capsuleMapper = MicrolearningCapsuleMapper::getInstance($this->adapter);
1 efrain 87
            $capsule = $capsuleMapper->fetchOneByUuid($capsule_id);
88
 
89
            if(!$capsule) {
90
                return new JsonModel([
91
                    'success' => false,
92
                    'data' => 'ERROR_CAPSULE_NOT_FOUND'
93
 
94
                ]);
95
 
96
            }
97
 
283 www 98
            $capsuleUserMapper = MicrolearningCapsuleUserMapper::getInstance($this->adapter);
1 efrain 99
            $capsuleUser = $capsuleUserMapper->fetchOneByUserIdAndCapsuleId($currentUser->id, $capsule->id);
100
            if(! $capsuleUser) {
101
                return new JsonModel([
102
                    'success' => false,
103
                    'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS_TO_THIS_CAPSULE',
104
                ]);
105
            }
106
 
107
            $userMapper = UserMapper::getInstance($this->adapter);
108
            $users = [];
109
 
283 www 110
            $capsuleCommentMapper = MicrolearningCapsuleCommentMapper::getInstance($this->adapter);
1 efrain 111
            $records = $capsuleCommentMapper->fetchAllByCapsuleId($capsule->id);
112
 
283 www 113
            $storage = Storage::getInstance($this->config);
114
 
1 efrain 115
            $comments = [];
116
            foreach($records as $record)
117
            {
118
 
119
                if(isset($users[$record->user_id])) {
120
 
121
                    $user = $users[$record->user_id];
122
 
123
                } else {
124
 
125
                    $user = $userMapper->fetchOne($record->user_id);
126
                    if(!$user) {
127
                        continue;
128
                    }
129
 
130
                    $users[$record->user_id] = $user;
131
 
132
 
133
                }
134
 
135
 
136
                $dt = \DateTime::createFromFormat('Y-m-d H:i:s', $record->added_on);
137
 
138
                array_push($comments, [
139
                    'date' => $dt->format($serviceDatetimeFormat),
283 www 140
                    'image' =>  $storage->getUserImage($user),
1 efrain 141
                    'fullname' => trim(trim($user->first_name) . ' ' . trim($user->last_name)),
142
                    'rating' => strval($record->rating),
143
                    'comment' => $record->comment,
144
                    'link_delete' => $record->user_id == $currentUser->id ? $this->url()->fromRoute('services/microlearning/capsules/comments/delete', ['capsule_id' => $capsule->uuid, 'comment_id' => $record->uuid], ['force_canonical' => true]) : '',
145
                ]);
146
            }
147
 
148
            $dataCountrAndRatingAverage = $capsuleCommentMapper->fetchCountAndRatingAverage($capsule->company_id, $capsule->topic_id, $capsule->id);
149
 
150
            return new JsonModel([
151
                'success' => true,
152
                'data' => [
153
                    'comments' => $comments,
154
                    'capsule' => [
155
                        'total_comments' => strval($dataCountrAndRatingAverage['total_comments']),
156
                            'total_rating' => strval($dataCountrAndRatingAverage['total_rating'])
157
                    ]
158
                ]
159
 
160
            ]);
161
 
162
        }
163
 
164
        return new JsonModel([
165
            'success' => false,
166
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
167
        ]);
168
    }
169
 
170
 
171
 
172
    public function deleteAction()
173
    {
174
 
175
        $request = $this->getRequest();
176
 
177
        if($request->isPost()) {
178
 
179
            $currentUserPlugin = $this->plugin('currentUserPlugin');
180
            $currentUser = $currentUserPlugin->getUser();
181
 
182
            $capsule_id = $this->params()->fromRoute('capsule_id');
183
            $comment_id = $this->params()->fromRoute('comment_id');
184
 
283 www 185
            $capsuleMapper = MicrolearningCapsuleMapper::getInstance($this->adapter);
1 efrain 186
            $capsule = $capsuleMapper->fetchOneByUuid($capsule_id);
187
 
188
            if(!$capsule) {
189
                return new JsonModel([
190
                    'success' => false,
191
                    'data' => 'ERROR_CAPSULE_NOT_FOUND'
192
 
193
                ]);
194
 
195
            }
196
 
283 www 197
            $capsuleUserMapper = MicrolearningCapsuleUserMapper::getInstance($this->adapter);
1 efrain 198
            $capsuleUser = $capsuleUserMapper->fetchOneByUserIdAndCapsuleId($currentUser->id, $capsule->id);
199
            if(! $capsuleUser) {
200
                return new JsonModel([
201
                    'success' => false,
202
                    'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS_TO_THIS_CAPSULE',
203
                ]);
204
            }
205
 
283 www 206
            $capsuleCommentMapper = MicrolearningCapsuleCommentMapper::getInstance($this->adapter);
1 efrain 207
            $capsuleComment = $capsuleCommentMapper->fetchOneByUuid($comment_id);
208
 
209
            if(!$capsuleComment) {
210
                return new JsonModel([
211
                    'success' => false,
212
                    'data' => 'ERROR_CAPSULE_COMMENT_NOT_FOUND',
213
                ]);
214
            }
215
 
216
            if($capsuleComment->capsule_id != $capsule->id || $capsuleComment->user_id != $currentUser->id) {
217
                return new JsonModel([
218
                    'success' => false,
219
                    'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS_TO_THIS_CAPSULE_COMMENT',
220
                ]);
221
            }
222
 
223
 
224
            $result = $capsuleCommentMapper->delete($capsuleComment->id);
225
            if($result) {
226
                $dataCountrAndRatingAverage = $capsuleCommentMapper->fetchCountAndRatingAverage($capsule->company_id, $capsule->topic_id, $capsule->id);
227
 
228
 
229
 
230
 
231
                return new JsonModel([
232
                    'success' => true,
233
                    'data' => [
234
                        'message' => 'LABEL_CAPSULE_COMMENT_HAVE_BEEN_SUCCESSFULLY_DELETE',
235
                        'capsule' => [
236
                            'total_comments' => strval($dataCountrAndRatingAverage['total_comments']),
237
                                'total_rating' => strval($dataCountrAndRatingAverage['total_rating'])
238
                        ]
239
                    ],
240
 
241
                ]);
242
            } else {
243
                return new JsonModel([
244
                    'success' => false,
245
                    'data' => $capsuleCommentMapper->getError()
246
 
247
                ]);
248
            }
249
 
250
 
251
 
252
        }
253
 
254
        return new JsonModel([
255
            'success' => false,
256
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
257
        ]);
258
    }
259
 
260
    public function addAction()
261
    {
262
 
263
        $request = $this->getRequest();
264
 
265
        if($request->isPost()) {
266
            $serviceDatetimeFormat = $this->config['leaderslinked.services.datetime'];
267
 
268
 
269
 
270
            $currentUserPlugin = $this->plugin('currentUserPlugin');
271
            $currentUser = $currentUserPlugin->getUser();
272
 
273
            $capsule_id = $this->params()->fromRoute('capsule_id');
274
 
275
 
283 www 276
            $capsuleMapper = MicrolearningCapsuleMapper::getInstance($this->adapter);
1 efrain 277
            $capsule = $capsuleMapper->fetchOneByUuid($capsule_id);
278
 
279
            if(!$capsule) {
280
                return new JsonModel([
281
                    'success' => false,
282
                    'data' => 'ERROR_CAPSULE_NOT_FOUND'
283
 
284
                ]);
285
 
286
            }
287
 
283 www 288
            $capsuleUserMapper = MicrolearningCapsuleUserMapper::getInstance($this->adapter);
1 efrain 289
            $capsuleUser = $capsuleUserMapper->fetchOneByUserIdAndCapsuleId($currentUser->id, $capsule->id);
290
            if(! $capsuleUser) {
291
                return new JsonModel([
292
                    'success' => false,
293
                    'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS_TO_THIS_CAPSULE',
294
                ]);
295
            }
296
 
297
            //$rawdata = file_get_contents("php://input");
298
           //  error_log('$rawdata = ' . $rawdata );
299
 
300
 
301
            $form = new  CapsuleCommentForm();
302
            $dataPost = $request->getPost()->toArray();
303
 
304
 
305
            if(!empty($dataPost['added_on'])) {
306
 
307
                $dt = \DateTime::createFromFormat($serviceDatetimeFormat, $dataPost['added_on']);
308
                if(!$dt) {
309
                    $dataPost['added_on'] = '';
310
                }
311
 
312
 
313
            }
314
 
315
 
316
 
317
            $form->setData($dataPost);
318
 
319
            if($form->isValid()) {
320
                $dataPost = (array) $form->getData();
321
                $serviceDatetimeFormat = $this->config['leaderslinked.services.datetime'];
322
                $dt = \DateTime::createFromFormat($serviceDatetimeFormat, $dataPost['added_on']);
323
 
324
 
283 www 325
                $capsuleComment = new MicrolearningCapsuleComment();
1 efrain 326
                $capsuleComment->company_id = $capsule->company_id;
327
                $capsuleComment->topic_id = $capsule->topic_id;
328
                $capsuleComment->capsule_id = $capsule->id;
329
                $capsuleComment->user_id = $currentUser->id;
330
                $capsuleComment->comment = $dataPost['comment'];
331
                $capsuleComment->rating = strval($dataPost['rating']);
332
                $capsuleComment->added_on =  $dt->format('Y-m-d H:i:s');
333
 
334
 
283 www 335
                $capsuleCommentMapper = MicrolearningCapsuleCommentMapper::getInstance($this->adapter);
1 efrain 336
                $result = $capsuleCommentMapper->insert($capsuleComment);
337
                if($result) {
338
 
339
                    $capsuleComment = $capsuleCommentMapper->fetchOne($capsuleComment->id);
340
 
341
                    $dataCountrAndRatingAverage = $capsuleCommentMapper->fetchCountAndRatingAverage($capsule->company_id, $capsule->topic_id, $capsule->id);
342
 
343
 
344
 
345
                    return new JsonModel([
346
                        'success' => true,
347
                        'data' => [
348
                            'message' =>'LABEL_CAPSULE_COMMENT_HAVE_BEEN_SUCCESSFULLY_ADDED',
349
 
350
                            'comment' => [
351
                                'comment' => $capsuleComment->comment,
352
                                'rating' => $capsuleComment->rating,
353
                                'link_delete' => $this->url()->fromRoute('services/microlearning/capsules/comments/delete', ['capsule_id' => $capsule->uuid, 'comment_id' => $capsuleComment->uuid], ['force_canonical' => true])
354
                            ],
355
                            'capsule' => [
356
                                'total_comments' => strval($dataCountrAndRatingAverage['total_comments']),
357
                                    'total_rating' => strval($dataCountrAndRatingAverage['total_rating'])
358
                            ]
359
                        ]
360
 
361
                    ]);
362
                } else {
363
                    return new JsonModel([
364
                        'success' => false,
365
                        'data' => $capsuleCommentMapper->getError()
366
 
367
                    ]);
368
                }
369
 
370
 
371
            } else {
372
                $messages = [];
373
                $form_messages = (array) $form->getMessages();
374
                foreach($form_messages  as $fieldname => $field_messages)
375
                {
376
 
377
                    $messages[$fieldname] = array_values($field_messages);
378
                }
379
 
380
                return new JsonModel([
381
                    'success'   => false,
382
                    'data'   => $messages
383
                ]);
384
            }
385
 
386
 
387
 
388
 
389
        }
390
 
391
        return new JsonModel([
392
            'success' => false,
393
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
394
        ]);
395
    }
396
 
397
 
398
 
399
 
400
 
401
 
402
}
403