Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 15392 | Rev 16768 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

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