Proyectos de Subversion LeadersLinked - Backend

Rev

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

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