Proyectos de Subversion LeadersLinked - Backend

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
9334 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;
9735 eleazar 15
use LeadersLinked\Mapper\TopicMapper;
16
use LeadersLinked\Mapper\QueryMapper;
9739 eleazar 17
use Laminas\Paginator\Adapter\DbSelect;
18
use Laminas\Paginator\Paginator;
9334 eleazar 19
use LeadersLinked\Form\MyTrainerCategoriesForm;
9736 eleazar 20
use LeadersLinked\Form\TopicForm;
9334 eleazar 21
use LeadersLinked\Model\MyTrainerCategories;
9738 eleazar 22
use Laminas\Hydrator\ArraySerializableHydrator;
23
use Laminas\Db\ResultSet\HydratingResultSet;
9735 eleazar 24
use LeadersLinked\Model\Topic;
9334 eleazar 25
use LeadersLinked\Mapper\CompanyMapper;
26
use LeadersLinked\Model\Company;
27
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
28
 
29
class MyTrainerCategoriesController extends AbstractActionController {
30
 
31
    /**
32
     *
33
     * @var AdapterInterface
34
     */
35
    private $adapter;
36
 
37
    /**
38
     *
39
     * @var AbstractAdapter
40
     */
41
    private $cache;
42
 
43
    /**
44
     *
45
     * @var  LoggerInterface
46
     */
47
    private $logger;
48
 
49
    /**
50
     *
51
     * @var array
52
     */
53
    private $config;
54
 
55
    /**
56
     *
57
     * @param AdapterInterface $adapter
58
     * @param AbstractAdapter $cache
59
     * @param LoggerInterface $logger
60
     * @param array $config
61
     */
62
    public function __construct($adapter, $cache, $logger, $config) {
63
        $this->adapter = $adapter;
64
        $this->cache = $cache;
65
        $this->logger = $logger;
66
        $this->config = $config;
67
    }
68
 
69
    public function indexAction() {
70
        $request = $this->getRequest();
71
        $currentUserPlugin = $this->plugin('currentUserPlugin');
72
        $currentCompany = $currentUserPlugin->getCompany();
73
        $currentUser = $currentUserPlugin->getUser();
74
 
9339 eleazar 75
        try{
9334 eleazar 76
        $request = $this->getRequest();
77
        if ($request->isGet()) {
78
 
79
            $headers = $request->getHeaders();
80
 
81
            $isJson = false;
82
            if ($headers->has('Accept')) {
83
                $accept = $headers->get('Accept');
84
 
85
                $prioritized = $accept->getPrioritized();
86
 
87
                foreach ($prioritized as $key => $value) {
88
                    $raw = trim($value->getRaw());
89
 
90
                    if (!$isJson) {
91
                        $isJson = strpos($raw, 'json');
92
                    }
93
                }
94
            }
95
 
96
            if ($isJson) {
97
                $search = $this->params()->fromQuery('search', []);
98
                $search = empty($search['value']) ? '' : filter_var($search['value'], FILTER_SANITIZE_STRING);
99
 
15371 efrain 100
                $start = intval($this->params()->fromQuery('start', 0), 10);
9334 eleazar 101
                $records_x_page = intval($this->params()->fromQuery('length', 10), 10);
15371 efrain 102
                $page =  intval($start / $records_x_page);
103
                $page++;
104
 
9334 eleazar 105
                $order = $this->params()->fromQuery('order', []);
106
                $order_field = empty($order[0]['column']) ? 99 : intval($order[0]['column'], 10);
107
                $order_direction = empty($order[0]['dir']) ? 'ASC' : strtoupper(filter_var($order[0]['dir'], FILTER_SANITIZE_STRING));
108
 
109
                $fields = ['title'];
110
                $order_field = isset($fields[$order_field]) ? $fields[$order_field] : 'title';
111
 
112
                if (!in_array($order_direction, ['ASC', 'DESC'])) {
113
                    $order_direction = 'ASC';
114
                }
115
 
116
                $acl = $this->getEvent()->getViewModel()->getVariable('acl');
117
                $allowAdd = $acl->isAllowed($currentUser->usertype_id, 'my-trainer/category/add');
118
                $allowEdit = $acl->isAllowed($currentUser->usertype_id, 'my-trainer/category/edit');
119
                $allowDelete = $acl->isAllowed($currentUser->usertype_id, 'my-trainer/category/delete');
120
 
121
                $items = [];
12361 eleazar 122
                $topicMapper = TopicMapper::getInstance($this->adapter);
123
 
124
                $paginator = $topicMapper->fetchAllMyTrainer();
125
 
9334 eleazar 126
 
12361 eleazar 127
                foreach ($paginator as $record) {
9334 eleazar 128
 
129
                    $item = [
12361 eleazar 130
                        'id' => $record->id,
131
                        'title' => $record->title,
132
                        'description' => $record->description,
133
                        'status' => $record->status,
9334 eleazar 134
                        'actions' => [
12361 eleazar 135
                            'link_edit' => $this->url()->fromRoute('my-trainer/category/edit', ['id' => $record->uuid]),
136
                            'link_delete' => $this->url()->fromRoute('my-trainer/category/delete', ['id' => $record->uuid]),
137
                            'link_view' => $this->url()->fromRoute('my-trainer/question/view', ['id' => $record->uuid])
9334 eleazar 138
                        ]
139
                    ];
140
 
141
                    array_push($items, $item);
142
                }
143
 
144
                return new JsonModel([
145
                    'success' => true,
146
                    'data' => [
147
                        'items' => $items,
148
                    ]
149
                ]);
150
            } else {
151
 
9735 eleazar 152
                $form = new TopicForm($this->adapter, $currentCompany->id);
9334 eleazar 153
 
154
                $this->layout()->setTemplate('layout/layout-backend');
155
                $viewModel = new ViewModel();
156
                $viewModel->setTemplate('leaders-linked/my-trainer-categories/index.phtml');
157
                $viewModel->setVariable('form', $form);
158
                return $viewModel;
159
            }
160
        } else {
161
            return new JsonModel([
162
                'success' => false,
163
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
164
            ]);
165
        }
9339 eleazar 166
    } catch (\Throwable $e) {
167
            $e->getMessage();
168
            return new JsonModel([
169
                'success' => false,
170
                'data' => $e
171
            ]);
172
        }
9334 eleazar 173
    }
174
 
175
    public function addAction() {
176
        $currentUserPlugin  = $this->plugin('currentUserPlugin');
177
        $currentCompany     = $currentUserPlugin->getCompany();
178
        $currentUser        = $currentUserPlugin->getUser();
179
 
180
        $request    = $this->getRequest();
181
 
9341 eleazar 182
        try{
9334 eleazar 183
        if($request->isPost()) {
9735 eleazar 184
            $form = new  TopicForm($this->adapter, $currentCompany->id);
9349 eleazar 185
 
9334 eleazar 186
            $dataPost = $request->getPost()->toArray();
187
 
188
            $form->setData($dataPost);
10208 eleazar 189
 
9334 eleazar 190
            if($form->isValid()) {
10209 eleazar 191
 
9334 eleazar 192
                $dataPost = (array) $form->getData();
193
 
194
                $hydrator = new ObjectPropertyHydrator();
9735 eleazar 195
                $categories = new Topic();
9334 eleazar 196
                $hydrator->hydrate($dataPost, $categories);
9735 eleazar 197
 
9744 eleazar 198
                $categories->type='myt';
9745 eleazar 199
               // echo $select->getSqlString($this->adapter->platform);
9735 eleazar 200
                $categoriesMapper = TopicMapper::getInstance($this->adapter);
9334 eleazar 201
 
202
                if($categoriesMapper->insert($categories)) {
203
                     $categories = $categoriesMapper->fetchOne($categories->id);
204
 
9343 eleazar 205
                    $this->logger->info('Se agrego la categoria ' . $categories->title, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
9334 eleazar 206
 
207
                    $data = [
208
                        'success'   => true,
209
                        'data'   => 'LABEL_RECORD_ADDED'
210
                    ];
211
                } else {
212
                    $data = [
213
                        'success'   => false,
214
                        'data'      => $categoriesMapper->getError()
215
                    ];
216
 
217
                }
218
 
219
                return new JsonModel($data);
220
 
221
            } else {
222
                $messages = [];
223
                $form_messages = (array) $form->getMessages();
224
                foreach($form_messages  as $fieldname => $field_messages)
225
                {
226
 
227
                    $messages[$fieldname] = array_values($field_messages);
228
                }
229
 
230
            }
231
 
232
        } else {
233
            $data = [
234
                'success' => false,
235
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
236
            ];
237
 
238
            return new JsonModel($data);
239
        }
240
 
241
        return new JsonModel($data);
9341 eleazar 242
    } catch (\Throwable $e) {
243
        $e->getMessage();
244
        return new JsonModel([
245
            'success' => false,
246
            'data' => $e
247
        ]);
248
    }
9334 eleazar 249
 
250
    }
251
 
252
    public function editAction() {
253
        $request = $this->getRequest();
254
        $currentUserPlugin = $this->plugin('currentUserPlugin');
255
        $currentCompany = $currentUserPlugin->getCompany();
256
        $currentUser = $currentUserPlugin->getUser();
257
 
258
        $request = $this->getRequest();
259
        $uuid = $this->params()->fromRoute('id');
9345 eleazar 260
        try{
9344 eleazar 261
        if (!$uuid) {
9334 eleazar 262
            $data = [
263
                'success' => false,
264
                'data' => 'ERROR_INVALID_PARAMETER'
265
            ];
266
 
267
            return new JsonModel($data);
268
        }
269
 
9746 eleazar 270
        $categoriesMapper = TopicMapper::getInstance($this->adapter);
9334 eleazar 271
        $categories = $categoriesMapper->fetchOneByUuid($uuid);
272
 
273
        if (!$categories) {
274
            $data = [
275
                'success' => false,
276
                'data' => 'ERROR_RECORD_NOT_FOUND'
277
            ];
278
 
279
            return new JsonModel($data);
280
        }
281
 
282
 
283
        if ($request->isPost()) {
9747 eleazar 284
            $form = new TopicForm();
9334 eleazar 285
            $dataPost = $request->getPost()->toArray();
286
 
287
            $form->setData($dataPost);
288
 
289
            if ($form->isValid()) {
290
                $dataPost = (array) $form->getData();
291
 
292
                $hydrator = new ObjectPropertyHydrator();
293
                $hydrator->hydrate($dataPost, $categories);
294
 
295
                if($categoriesMapper->update($categories)) {
296
                    $categories = $categoriesMapper->fetchOne($categories->id);
297
 
298
                   $this->logger->info('Se modifico la categoria ' . $categories->title, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
299
 
300
                   $data = [
301
                       'success'   => true,
302
                       'data'   => 'LABEL_RECORD_UPDATED'
303
                   ];
304
               } else {
305
                   $data = [
306
                       'success'   => false,
307
                       'data'      => $categoriesMapper->getError()
308
                   ];
309
 
310
               }
311
 
312
                return new JsonModel($data);
313
            } else {
314
                $messages = [];
315
                $form_messages = (array) $form->getMessages();
316
                foreach ($form_messages as $fieldname => $field_messages) {
317
                    $messages[$fieldname] = array_values($field_messages);
318
                }
319
 
320
                return new JsonModel([
321
                    'success' => false,
322
                    'data' => $messages
323
                ]);
324
            }
325
        } else if ($request->isGet()) {
326
 
327
            $hydrator = new ObjectPropertyHydrator();
328
 
329
            $data = [
330
                'success' => true,
331
                'data' => [
332
                    'id' => $categories->uuid,
333
                    'title' => $categories->title,
9748 eleazar 334
                    'description' => $categories->description,
9334 eleazar 335
                ]
336
            ];
337
 
338
            return new JsonModel($data);
339
        } else {
340
            $data = [
341
                'success' => false,
342
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
343
            ];
344
 
345
            return new JsonModel($data);
346
        }
347
 
348
        return new JsonModel($data);
9345 eleazar 349
    } catch (\Throwable $e) {
350
        $e->getMessage();
351
        return new JsonModel([
352
            'success' => false,
353
            'data' => $e
354
        ]);
9334 eleazar 355
    }
9345 eleazar 356
    }
9334 eleazar 357
 
358
    public function deleteAction() {
359
        $request = $this->getRequest();
360
        $currentUserPlugin = $this->plugin('currentUserPlugin');
361
        $currentCompany = $currentUserPlugin->getCompany();
362
        $currentUser = $currentUserPlugin->getUser();
363
 
364
        $request = $this->getRequest();
365
        $uuid = $this->params()->fromRoute('id');
366
 
367
        if (!$uuid) {
368
            $data = [
369
                'success' => false,
370
                'data' => 'ERROR_INVALID_PARAMETER'
371
            ];
372
 
373
            return new JsonModel($data);
374
        }
375
 
9746 eleazar 376
        $categoriesMapper = TopicMapper::getInstance($this->adapter);
9334 eleazar 377
        $categories = $categoriesMapper->fetchOneByUuid($uuid);
378
        if (!$categories) {
379
            $data = [
380
                'success' => false,
381
                'data' => 'ERROR_RECORD_NOT_FOUND'
382
            ];
383
 
384
            return new JsonModel($data);
385
        }
386
 
387
        if ($request->isPost()) {
388
 
389
 
390
            $result = $categoriesMapper->delete($categories->id);
391
            if ($result) {
392
                $this->logger->info('Se borro la categoria ' . $categories->title, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
393
 
394
                $data = [
395
                    'success' => true,
396
                    'data' => 'LABEL_RECORD_DELETED'
397
                ];
398
            } else {
399
 
400
                $data = [
401
                    'success' => false,
402
                    'data' => $categoriesMapper->getError()
403
                ];
404
 
405
                return new JsonModel($data);
406
            }
407
        } else {
408
            $data = [
409
                'success' => false,
410
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
411
            ];
412
 
413
            return new JsonModel($data);
414
        }
415
 
416
        return new JsonModel($data);
417
    }
418
}