Proyectos de Subversion LeadersLinked - Backend

Rev

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