Proyectos de Subversion LeadersLinked - Backend

Rev

| Ultima modificación | Ver Log |

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