Proyectos de Subversion LeadersLinked - Backend

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
9350 eleazar 1
<?php
2
 
3
declare(strict_types=1);
4
 
5
namespace LeadersLinked\Controller;
6
 
7
use Laminas\Db\Adapter\AdapterInterface;
8
use Laminas\Cache\Storage\Adapter\AbstractAdapter;
9
use Laminas\Mvc\Controller\AbstractActionController;
10
use Laminas\Log\LoggerInterface;
11
use Laminas\View\Model\ViewModel;
12
use Laminas\View\Model\JsonModel;
13
use LeadersLinked\Library\Functions;
14
use LeadersLinked\Mapper\MyTrainerCategoriesMapper;
10117 eleazar 15
use LeadersLinked\Mapper\TopicMapper;
9356 eleazar 16
use LeadersLinked\Mapper\MyTrainerQuestionsMapper;
10893 eleazar 17
use LeadersLinked\Mapper\FeedMapper;
9350 eleazar 18
use LeadersLinked\Form\MyTrainerQuestionForm;
10893 eleazar 19
use LeadersLinked\Form\CreateFeedForm;
9369 eleazar 20
use LeadersLinked\Model\MyTrainerQuestions;
10893 eleazar 21
use LeadersLinked\Model\Feed;
9350 eleazar 22
use LeadersLinked\Mapper\CompanyMapper;
23
use LeadersLinked\Model\Company;
24
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
25
 
26
class MyTrainerQuestionController extends AbstractActionController {
27
 
28
    /**
29
     *
30
     * @var AdapterInterface
31
     */
32
    private $adapter;
33
 
34
    /**
35
     *
36
     * @var AbstractAdapter
37
     */
38
    private $cache;
39
 
40
    /**
41
     *
42
     * @var  LoggerInterface
43
     */
44
    private $logger;
45
 
46
    /**
47
     *
48
     * @var array
49
     */
50
    private $config;
51
 
52
    /**
53
     *
54
     * @param AdapterInterface $adapter
55
     * @param AbstractAdapter $cache
56
     * @param LoggerInterface $logger
57
     * @param array $config
58
     */
59
    public function __construct($adapter, $cache, $logger, $config) {
60
        $this->adapter = $adapter;
61
        $this->cache = $cache;
62
        $this->logger = $logger;
63
        $this->config = $config;
64
    }
65
 
66
    public function indexAction() {
67
        $request = $this->getRequest();
68
        $currentUserPlugin = $this->plugin('currentUserPlugin');
69
        $currentCompany = $currentUserPlugin->getCompany();
70
        $currentUser = $currentUserPlugin->getUser();
71
 
72
        try{
73
        $request = $this->getRequest();
11148 eleazar 74
 
11146 eleazar 75
 
11148 eleazar 76
        if($request->isGet()) {
77
            $headers  = $request->getHeaders();
11146 eleazar 78
 
9350 eleazar 79
            $isJson = false;
11148 eleazar 80
            if($headers->has('Accept')) {
9350 eleazar 81
                $accept = $headers->get('Accept');
82
 
83
                $prioritized = $accept->getPrioritized();
84
 
11148 eleazar 85
                foreach($prioritized as $key => $value) {
9350 eleazar 86
                    $raw = trim($value->getRaw());
87
 
11148 eleazar 88
                    if(!$isJson) {
9350 eleazar 89
                        $isJson = strpos($raw, 'json');
90
                    }
11148 eleazar 91
 
9350 eleazar 92
                }
93
            }
94
 
95
                $acl = $this->getEvent()->getViewModel()->getVariable('acl');
96
                $allowAdd = $acl->isAllowed($currentUser->usertype_id, 'my-trainer/question/add');
97
                $allowEdit = $acl->isAllowed($currentUser->usertype_id, 'my-trainer/question/edit');
98
                $allowDelete = $acl->isAllowed($currentUser->usertype_id, 'my-trainer/question/delete');
11148 eleazar 99
                $page = $this->params()->fromQuery('page', 1);
100
                $perpage = $this->params()->fromQuery('perpage', 20);
101
 
102
                $form = new CreateFeedForm($this->adapter);
10912 eleazar 103
                $questionsMapper = FeedMapper::getInstance($this->adapter);
11149 eleazar 104
                $questions = $questionsMapper->fetchAllByMytQuestion();
11148 eleazar 105
 
106
                // $posts;
107
 
9350 eleazar 108
                $viewModel = new ViewModel();
11153 eleazar 109
                $this->layout()->setTemplate('layout/layout-backend');
9350 eleazar 110
                $viewModel->setTemplate('leaders-linked/my-trainer-question/index.phtml');
11151 eleazar 111
                $viewModel->setVariables([
11148 eleazar 112
                    'form' => $form,
113
                    'page' => $page,
114
                    'perpage' => $perpage,
115
                    'questions' => $questions,
116
                ]);
9350 eleazar 117
                return $viewModel;
118
        } else {
119
            return new JsonModel([
120
                'success' => false,
121
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
122
            ]);
123
        }
124
    } catch (\Throwable $e) {
125
            $e->getMessage();
126
            return new JsonModel([
127
                'success' => false,
128
                'data' => $e
129
            ]);
130
        }
131
    }
132
 
133
    public function addAction() {
134
        $currentUserPlugin  = $this->plugin('currentUserPlugin');
135
        $currentCompany     = $currentUserPlugin->getCompany();
136
        $currentUser        = $currentUserPlugin->getUser();
137
 
10180 eleazar 138
        $request    = $this->getRequest();
10238 eleazar 139
        try{
9350 eleazar 140
        if($request->isPost()) {
10892 eleazar 141
            $form = new  CreateFeedForm($this->adapter);
9365 eleazar 142
            $dataPost = $request->getPost()->toArray();
9373 eleazar 143
            $form->setData($dataPost);
10909 eleazar 144
 
9350 eleazar 145
            if($form->isValid()) {
10198 eleazar 146
 
10908 eleazar 147
                $dataPost = (array) $form->getData();
10899 eleazar 148
                $hydrator = new ObjectPropertyHydrator();
149
                $feed = new Feed();
150
                $hydrator->hydrate($dataPost, $feed);
151
 
10892 eleazar 152
                $feed->user_id = $currentUser->id;
153
                $feed->company_id = $currentCompany->id;
154
                $feed->user_id          = $currentUser->id;
11001 eleazar 155
                $feed->type             = Feed::TYPE_MYT_QUESTION;
10892 eleazar 156
                $feed->posted_or_shared = Feed::POSTED;
157
                $feed->shared_with      = Feed::SHARE_WITH_PUBLIC;
158
                $feed->total_comments   = 0;
159
                $feed->total_shared     = 0;
9377 eleazar 160
 
10117 eleazar 161
                $categoriesMapper = TopicMapper::getInstance($this->adapter);
9363 eleazar 162
                $category = $categoriesMapper->fetchOneByUuid($dataPost['category_id']);
10892 eleazar 163
                $feed->topic_id = $category->id;
9363 eleazar 164
 
10892 eleazar 165
                $feedMapper = FeedMapper::getInstance($this->adapter);
9350 eleazar 166
 
10892 eleazar 167
                if($feedMapper->insert($feed)) {
168
                     $feed = $feedMapper->fetchOne($feed->id);
9350 eleazar 169
 
10892 eleazar 170
                     $feed->myt_id = $feed->id;
171
                     $feed->related_feed = $feed->id;
172
                     $feedMapper->update($feed);
173
                    $this->logger->info('Se agrego la pregunta ' . $feed->title, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
9350 eleazar 174
 
175
                    $data = [
176
                        'success'   => true,
177
                        'data'   => 'LABEL_RECORD_ADDED'
178
                    ];
179
                } else {
180
                    $data = [
181
                        'success'   => false,
10892 eleazar 182
                        'data'      => $feedMapper->getError()
9350 eleazar 183
                    ];
184
 
185
                }
186
 
187
                return new JsonModel($data);
188
 
189
            } else {
190
                $messages = [];
191
                $form_messages = (array) $form->getMessages();
192
                foreach($form_messages  as $fieldname => $field_messages)
193
                {
194
 
195
                    $messages[$fieldname] = array_values($field_messages);
196
                }
197
 
198
            }
199
 
200
        } else {
201
            $data = [
202
                'success' => false,
203
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
204
            ];
205
 
206
            return new JsonModel($data);
207
        }
208
 
10238 eleazar 209
    } catch (\Throwable $e) {
10892 eleazar 210
        $e->getMessage();
211
        return new JsonModel([
212
            'success' => false,
213
            'data' => $e
214
        ]);
9350 eleazar 215
    }
10238 eleazar 216
    }
9350 eleazar 217
 
218
    public function editAction() {
219
        $request = $this->getRequest();
220
        $currentUserPlugin = $this->plugin('currentUserPlugin');
221
        $currentCompany = $currentUserPlugin->getCompany();
222
        $currentUser = $currentUserPlugin->getUser();
223
 
224
        $request = $this->getRequest();
11014 eleazar 225
        $myt_id = $this->params()->fromRoute('id');
9350 eleazar 226
        try{
11015 eleazar 227
        if (!$myt_id) {
9350 eleazar 228
            $data = [
229
                'success' => false,
230
                'data' => 'ERROR_INVALID_PARAMETER'
231
            ];
232
 
233
            return new JsonModel($data);
234
        }
235
 
11014 eleazar 236
        $feedMapper = FeedMapper::getInstance($this->adapter);
11016 eleazar 237
        $feed = $feedMapper->fetchOneByUuid($myt_id);
9350 eleazar 238
 
11014 eleazar 239
        if (!$feed) {
9350 eleazar 240
            $data = [
241
                'success' => false,
242
                'data' => 'ERROR_RECORD_NOT_FOUND'
243
            ];
244
 
245
            return new JsonModel($data);
246
        }
247
 
11014 eleazar 248
        if ($feed->company_id != $currentCompany->id) {
9350 eleazar 249
            return new JsonModel([
250
                'success' => false,
251
                'data' => 'ERROR_UNAUTHORIZED'
252
            ]);
253
        }
254
 
255
 
256
        if ($request->isPost()) {
11014 eleazar 257
            $form = new CreateFeedForm($this->adapter);
9350 eleazar 258
            $dataPost = $request->getPost()->toArray();
259
 
260
            $form->setData($dataPost);
261
 
262
            if ($form->isValid()) {
263
                $dataPost = (array) $form->getData();
264
 
265
                $hydrator = new ObjectPropertyHydrator();
11018 eleazar 266
                $hydrator->hydrate($dataPost, $feed);
10250 eleazar 267
                $categoriesMapper = TopicMapper::getInstance($this->adapter);
268
                $category = $categoriesMapper->fetchOneByUuid($dataPost['category_id']);
11014 eleazar 269
                $feed->topic_id = $category->id;
9350 eleazar 270
 
11014 eleazar 271
                if($feedMapper->update($feed)) {
272
                    $feed = $feedMapper->fetchOne($feed->id);
9350 eleazar 273
 
11014 eleazar 274
                   $this->logger->info('Se modifico la pregunta ' . $feed->title, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
9350 eleazar 275
 
276
                   $data = [
277
                       'success'   => true,
278
                       'data'   => 'LABEL_RECORD_UPDATED'
279
                   ];
280
               } else {
281
                   $data = [
282
                       'success'   => false,
11014 eleazar 283
                       'data'      => $feedMapper->getError()
9350 eleazar 284
                   ];
285
 
286
               }
287
 
288
                return new JsonModel($data);
289
            } else {
290
                $messages = [];
291
                $form_messages = (array) $form->getMessages();
292
                foreach ($form_messages as $fieldname => $field_messages) {
293
                    $messages[$fieldname] = array_values($field_messages);
294
                }
295
 
296
                return new JsonModel([
297
                    'success' => false,
298
                    'data' => $messages
299
                ]);
300
            }
301
        } else if ($request->isGet()) {
302
 
303
            $hydrator = new ObjectPropertyHydrator();
10252 eleazar 304
            $categoriesMapper = TopicMapper::getInstance($this->adapter);
11014 eleazar 305
            $category = $categoriesMapper->fetchOne($feed->topic_id);
9350 eleazar 306
 
307
            $data = [
308
                'success' => true,
309
                'data' => [
11014 eleazar 310
                    'id' => $feed->uuid,
10252 eleazar 311
                    'category_id' => $category->uuid,
11014 eleazar 312
                    'title' => $feed->title,
11017 eleazar 313
                    'description' => $feed->description,
9350 eleazar 314
                ]
315
            ];
316
 
317
            return new JsonModel($data);
318
        } else {
319
            $data = [
320
                'success' => false,
321
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
322
            ];
323
 
324
            return new JsonModel($data);
325
        }
326
 
327
        return new JsonModel($data);
328
    } catch (\Throwable $e) {
329
        $e->getMessage();
330
        return new JsonModel([
331
            'success' => false,
332
            'data' => $e
333
        ]);
334
    }
335
    }
336
 
337
    public function deleteAction() {
338
        $request = $this->getRequest();
339
        $currentUserPlugin = $this->plugin('currentUserPlugin');
340
        $currentCompany = $currentUserPlugin->getCompany();
341
        $currentUser = $currentUserPlugin->getUser();
342
 
343
        $request = $this->getRequest();
344
        $uuid = $this->params()->fromRoute('id');
345
 
346
        if (!$uuid) {
347
            $data = [
348
                'success' => false,
349
                'data' => 'ERROR_INVALID_PARAMETER'
350
            ];
351
 
352
            return new JsonModel($data);
353
        }
354
 
11019 eleazar 355
        $feedMapper = FeedMapper::getInstance($this->adapter);
356
        $feed = $feedMapper->fetchOneByUuid($uuid);
357
        if (!$feed) {
9350 eleazar 358
            $data = [
359
                'success' => false,
360
                'data' => 'ERROR_RECORD_NOT_FOUND'
361
            ];
362
 
363
            return new JsonModel($data);
364
        }
365
 
11019 eleazar 366
        if ($feed->company_id != $currentCompany->id) {
9350 eleazar 367
            return new JsonModel([
368
                'success' => false,
369
                'data' => 'ERROR_UNAUTHORIZED'
370
            ]);
371
        }
372
 
373
        if ($request->isPost()) {
374
 
375
 
11019 eleazar 376
            $result = $feedMapper->delete($feed->id);
9350 eleazar 377
            if ($result) {
11019 eleazar 378
                $this->logger->info('Se borro la pregunta ' . $feed->title, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
9350 eleazar 379
 
380
                $data = [
381
                    'success' => true,
382
                    'data' => 'LABEL_RECORD_DELETED'
383
                ];
384
            } else {
385
 
386
                $data = [
387
                    'success' => false,
11019 eleazar 388
                    'data' => $feedMapper->getError()
9350 eleazar 389
                ];
390
 
391
                return new JsonModel($data);
392
            }
393
        } else {
394
            $data = [
395
                'success' => false,
396
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
397
            ];
398
 
399
            return new JsonModel($data);
400
        }
401
 
402
        return new JsonModel($data);
403
    }
404
}