Proyectos de Subversion LeadersLinked - Backend

Rev

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

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