Proyectos de Subversion LeadersLinked - Backend

Rev

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