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
                $allowAdd = $acl->isAllowed($currentUser->usertype_id, 'development-and-content/category/add');
116
                $allowEdit = $acl->isAllowed($currentUser->usertype_id, 'development-and-content/category/edit');
117
                $allowDelete = $acl->isAllowed($currentUser->usertype_id, 'development-and-content/category/delete');
118
 
119
                $items = [];
120
                $topicMapper = TopicMapper::getInstance($this->adapter);
121
 
13303 eleazar 122
                $paginator = $topicMapper->fetchAllDevelopment();
13287 eleazar 123
 
124
 
125
                foreach ($paginator as $record) {
126
 
127
                    $item = [
128
                        'id' => $record->id,
129
                        'title' => $record->title,
130
                        'description' => $record->description,
131
                        'status' => $record->status,
132
                        'actions' => [
15037 kerby 133
                            'link_edit' => ($allowEdit)? $this->url()->fromRoute('development-and-content/category/edit', ['id' => $record->uuid]):'',
134
                            'link_delete' =>($allowDelete)? $this->url()->fromRoute('development-and-content/category/delete', ['id' => $record->uuid]):'',
13287 eleazar 135
                        ]
136
                    ];
137
 
138
                    array_push($items, $item);
139
                }
140
 
141
                return new JsonModel([
142
                    'success' => true,
143
                    'data' => [
144
                        'items' => $items,
145
                    ]
146
                ]);
147
            } else {
148
 
149
                $form = new TopicForm($this->adapter, $currentCompany->id);
150
 
151
                $this->layout()->setTemplate('layout/layout-backend');
152
                $viewModel = new ViewModel();
13304 eleazar 153
                $viewModel->setTemplate('leaders-linked/development-content-category/index.phtml');
13287 eleazar 154
                $viewModel->setVariable('form', $form);
155
                return $viewModel;
156
            }
157
        } else {
158
            return new JsonModel([
159
                'success' => false,
160
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
161
            ]);
162
        }
15034 kerby 163
 
13287 eleazar 164
    }
165
 
166
    public function addAction() {
167
        $currentUserPlugin  = $this->plugin('currentUserPlugin');
168
        $currentCompany     = $currentUserPlugin->getCompany();
169
        $currentUser        = $currentUserPlugin->getUser();
170
 
171
        $request    = $this->getRequest();
172
 
15034 kerby 173
 
13287 eleazar 174
        if($request->isPost()) {
175
            $form = new  TopicForm($this->adapter, $currentCompany->id);
176
 
177
            $dataPost = $request->getPost()->toArray();
178
 
179
            $form->setData($dataPost);
180
 
181
            if($form->isValid()) {
182
 
183
                $dataPost = (array) $form->getData();
184
 
185
                $hydrator = new ObjectPropertyHydrator();
186
                $categories = new Topic();
187
                $hydrator->hydrate($dataPost, $categories);
188
 
189
                $categories->type='dc';
15038 kerby 190
 
13287 eleazar 191
                $categoriesMapper = TopicMapper::getInstance($this->adapter);
192
 
193
                if($categoriesMapper->insert($categories)) {
194
                     $categories = $categoriesMapper->fetchOne($categories->id);
195
 
196
                    $this->logger->info('Se agrego la categoria ' . $categories->title, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
197
 
198
                    $data = [
199
                        'success'   => true,
200
                        'data'   => 'LABEL_RECORD_ADDED'
201
                    ];
202
                } else {
203
                    $data = [
204
                        'success'   => false,
205
                        'data'      => $categoriesMapper->getError()
206
                    ];
207
 
208
                }
209
 
210
                return new JsonModel($data);
211
 
212
            } else {
213
                $messages = [];
214
                $form_messages = (array) $form->getMessages();
215
                foreach($form_messages  as $fieldname => $field_messages)
216
                {
217
 
218
                    $messages[$fieldname] = array_values($field_messages);
219
                }
220
 
221
            }
222
 
223
        } else {
224
            $data = [
225
                'success' => false,
226
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
227
            ];
228
 
229
            return new JsonModel($data);
230
        }
231
 
232
        return new JsonModel($data);
233
 
234
    }
235
 
236
    public function editAction() {
237
        $request = $this->getRequest();
238
        $currentUserPlugin = $this->plugin('currentUserPlugin');
239
        $currentUser = $currentUserPlugin->getUser();
240
 
241
        $request = $this->getRequest();
242
        $uuid = $this->params()->fromRoute('id');
15034 kerby 243
 
13287 eleazar 244
        if (!$uuid) {
245
            $data = [
246
                'success' => false,
247
                'data' => 'ERROR_INVALID_PARAMETER'
248
            ];
249
 
250
            return new JsonModel($data);
251
        }
252
 
253
        $categoriesMapper = TopicMapper::getInstance($this->adapter);
254
        $categories = $categoriesMapper->fetchOneByUuid($uuid);
255
 
256
        if (!$categories) {
257
            $data = [
258
                'success' => false,
259
                'data' => 'ERROR_RECORD_NOT_FOUND'
260
            ];
261
 
262
            return new JsonModel($data);
263
        }
264
 
265
 
266
        if ($request->isPost()) {
267
            $form = new TopicForm();
268
            $dataPost = $request->getPost()->toArray();
269
 
270
            $form->setData($dataPost);
271
 
272
            if ($form->isValid()) {
273
                $dataPost = (array) $form->getData();
274
 
275
                $hydrator = new ObjectPropertyHydrator();
276
                $hydrator->hydrate($dataPost, $categories);
277
 
278
                if($categoriesMapper->update($categories)) {
279
                    $categories = $categoriesMapper->fetchOne($categories->id);
280
 
281
                   $this->logger->info('Se modifico la categoria ' . $categories->title, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
282
 
283
                   $data = [
284
                       'success'   => true,
285
                       'data'   => 'LABEL_RECORD_UPDATED'
286
                   ];
287
               } else {
288
                   $data = [
289
                       'success'   => false,
290
                       'data'      => $categoriesMapper->getError()
291
                   ];
292
 
293
               }
294
 
295
                return new JsonModel($data);
296
            } else {
297
                $messages = [];
298
                $form_messages = (array) $form->getMessages();
299
                foreach ($form_messages as $fieldname => $field_messages) {
300
                    $messages[$fieldname] = array_values($field_messages);
301
                }
302
 
303
                return new JsonModel([
304
                    'success' => false,
305
                    'data' => $messages
306
                ]);
307
            }
308
        } else if ($request->isGet()) {
309
 
310
            $hydrator = new ObjectPropertyHydrator();
311
 
312
            $data = [
313
                'success' => true,
314
                'data' => [
315
                    'id' => $categories->uuid,
316
                    'title' => $categories->title,
317
                    'description' => $categories->description,
14554 kerby 318
                    'status' => $categories->status
13287 eleazar 319
                ]
320
            ];
321
 
322
            return new JsonModel($data);
323
        } else {
324
            $data = [
325
                'success' => false,
326
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
327
            ];
328
 
329
            return new JsonModel($data);
330
        }
331
 
14564 kerby 332
            return new JsonModel($data);
15034 kerby 333
 
13287 eleazar 334
    }
335
 
336
    public function deleteAction() {
337
        $request = $this->getRequest();
338
        $currentUserPlugin = $this->plugin('currentUserPlugin');
339
        $currentUser = $currentUserPlugin->getUser();
340
 
341
        $request = $this->getRequest();
342
        $uuid = $this->params()->fromRoute('id');
343
 
344
        if (!$uuid) {
345
            $data = [
346
                'success' => false,
347
                'data' => 'ERROR_INVALID_PARAMETER'
348
            ];
349
 
350
            return new JsonModel($data);
351
        }
352
 
353
        $categoriesMapper = TopicMapper::getInstance($this->adapter);
354
        $categories = $categoriesMapper->fetchOneByUuid($uuid);
355
        if (!$categories) {
356
            $data = [
357
                'success' => false,
358
                'data' => 'ERROR_RECORD_NOT_FOUND'
359
            ];
360
 
361
            return new JsonModel($data);
362
        }
363
 
364
        if ($request->isPost()) {
365
 
14958 kerby 366
            $feedMapper = FeedMapper::getInstance($this->adapter);
14960 kerby 367
            $feeds = $feedMapper->fetchAllByDevelopAndCategoryIdCount($categories->id);
368
            if($feeds > 0){
14962 kerby 369
 
370
                $data = [
371
                    'success' => false,
372
                    'data' => 'Esta categoria posee contenidos asociados no puede ser eliminada'
373
                ];
374
                return new JsonModel($data);
375
            }else{
14958 kerby 376
                $result = $categoriesMapper->delete($categories->id);
377
                if ($result) {
378
                    $this->logger->info('Se borro la categoria ' . $categories->title, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
13287 eleazar 379
 
14958 kerby 380
                    $data = [
381
                        'success' => true,
382
                        'data' => 'LABEL_RECORD_DELETED'
383
                    ];
384
                } else {
13287 eleazar 385
 
14958 kerby 386
                    $data = [
387
                        'success' => false,
388
                        'data' => $categoriesMapper->getError()
389
                    ];
390
 
391
                    return new JsonModel($data);
392
                }
393
            }
13287 eleazar 394
 
14958 kerby 395
 
13287 eleazar 396
        } else {
397
            $data = [
398
                'success' => false,
399
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
400
            ];
401
 
402
            return new JsonModel($data);
403
        }
404
 
405
        return new JsonModel($data);
406
    }
407
}