Proyectos de Subversion LeadersLinked - Backend

Rev

| Ultima modificación | Ver Log |

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