Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 1683 | Rev 6849 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

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