Proyectos de Subversion LeadersLinked - Backend

Rev

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