Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 16769 | | Comparar con el anterior | Ultima modificación | Ver Log |

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