Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 11221 | Ir a la última revisión | | 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;
8
use Laminas\Cache\Storage\Adapter\AbstractAdapter;
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
     *
29
     * @var AbstractAdapter
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
48
     * @param AbstractAdapter $cache
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', []);
88
                $search = empty($search['value']) ? '' : filter_var($search['value'], FILTER_SANITIZE_STRING);
89
 
90
                $page = intval($this->params()->fromQuery('start', 1), 10);
91
                $records_x_page = intval($this->params()->fromQuery('length', 10), 10);
92
                $order = $this->params()->fromQuery('order', []);
93
                $order_field = empty($order[0]['column']) ? 99 : intval($order[0]['column'], 10);
94
                $order_direction = empty($order[0]['dir']) ? 'ASC' : strtoupper(filter_var($order[0]['dir'], FILTER_SANITIZE_STRING));
95
 
96
                $fields = ['name'];
97
                $order_field = isset($fields[$order_field]) ? $fields[$order_field] : 'name';
98
 
99
                if (!in_array($order_direction, ['ASC', 'DESC'])) {
100
                    $order_direction = 'ASC';
101
                }
102
 
103
                $organizationalClimateMapper = OrganizationalClimateFormMapper::getInstance($this->adapter);
104
                $paginator = $organizationalClimateMapper->fetchAllDataTableByCompanyId($currentCompany->id, $search, $page, $records_x_page, $order_field, $order_direction);
105
 
106
                $items = [];
107
                $records = $paginator->getCurrentItems();
108
 
109
                foreach ($records as $record) {
110
 
111
                    $item = [
112
                        'id' => $record->id,
113
                        'name' => $record->name,
114
                        'status' => $record->status,
115
                        'actions' => [
116
                            'link_edit' => $this->url()->fromRoute('organizational-climate/form/edit', ['id' => $record->uuid]),
117
                            'link_delete' => $this->url()->fromRoute('organizational-climate/form/delete', ['id' => $record->uuid])
118
                        ]
119
                    ];
120
 
121
                    array_push($items, $item);
122
                }
123
 
124
                return new JsonModel([
125
                    'success' => true,
126
                    'data' => [
127
                        'items' => $items,
128
                        'total' => $paginator->getTotalItemCount(),
129
                    ]
130
                ]);
131
            } else {
132
 
133
                $form = new OrganizationalClimateFormForm($this->adapter, $currentCompany->id);
134
 
135
                $this->layout()->setTemplate('layout/layout-backend');
136
                $viewModel = new ViewModel();
137
                $viewModel->setTemplate('leaders-linked/organizational-climate-form/index.phtml');
138
                $viewModel->setVariable('form', $form);
139
                return $viewModel;
140
            }
141
        } else {
142
            return new JsonModel([
143
                'success' => false,
144
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
145
            ]);
146
            ;
147
        }
148
    }
149
 
150
    public function addAction() {
151
        $request = $this->getRequest();
152
        $currentUserPlugin = $this->plugin('currentUserPlugin');
153
        $currentCompany = $currentUserPlugin->getCompany();
154
        $currentUser = $currentUserPlugin->getUser();
155
 
156
        $request = $this->getRequest();
157
 
158
        if ($request->isPost()) {
159
 
160
            $form = new OrganizationalClimateFormForm($this->adapter, $currentCompany->id);
161
 
162
            $dataPost = $request->getPost()->toArray();
163
            $dataPost['status'] = isset($dataPost['status']) ? $dataPost['status'] : OrganizationalClimateForm::STATUS_INACTIVE;
164
 
165
 
166
 
167
 
168
            $form->setData($dataPost);
169
 
170
            if ($form->isValid()) {
171
                $dataPost = (array) $form->getData();
172
 
173
                $hydrator = new ObjectPropertyHydrator();
174
                $organizationalClimate = new OrganizationalClimateForm();
175
                $hydrator->hydrate($dataPost, $organizationalClimate);
176
 
177
                if (!$organizationalClimate->status) {
178
                    $organizationalClimate->status = OrganizationalClimateForm::STATUS_INACTIVE;
179
                }
180
                $organizationalClimate->company_id = $currentCompany->id;
181
 
182
 
183
                $organizationalClimateMapper = OrganizationalClimateFormMapper::getInstance($this->adapter);
184
                $result = $organizationalClimateMapper->insert($organizationalClimate);
185
 
186
                if ($result) {
187
                    $this->logger->info('Se agrego el formulario' . $organizationalClimate->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
188
 
189
                    // Get record by id
190
                    $record = $organizationalClimateMapper->fetchOne($organizationalClimate->id);
191
 
192
                    if ($record) {
193
 
194
                        $data = [
195
                            'success' => true,
196
                            'id' => $record->id,
197
                            'data' => 'LABEL_RECORD_ADDED'
198
                        ];
199
                    } else {
200
 
201
                        $data = [
202
                            'success' => false,
203
                            'data' => 'ERROR_RECORD_NOT_FOUND'
204
                        ];
205
                    }
206
                } else {
207
                    $data = [
208
                        'success' => false,
209
                        'data' => $organizationalClimateMapper->getError()
210
                    ];
211
                }
212
 
213
                return new JsonModel($data);
214
            } else {
215
                $messages = [];
216
                $form_messages = (array) $form->getMessages();
217
                foreach ($form_messages as $fieldname => $field_messages) {
218
 
219
                    $messages[$fieldname] = array_values($field_messages);
220
                }
221
 
222
                return new JsonModel([
223
                    'success' => false,
224
                    'data' => $messages
225
                ]);
226
            }
227
        } else {
228
            $data = [
229
                'success' => false,
230
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
231
            ];
232
 
233
            return new JsonModel($data);
234
        }
235
 
236
        return new JsonModel($data);
237
    }
238
 
239
    public function editAction() {
240
        $request = $this->getRequest();
241
        $currentUserPlugin = $this->plugin('currentUserPlugin');
242
        $currentCompany = $currentUserPlugin->getCompany();
243
        $currentUser = $currentUserPlugin->getUser();
244
 
245
        $request = $this->getRequest();
246
        $uuid = $this->params()->fromRoute('id');
247
 
248
 
249
        if (!$uuid) {
250
            $data = [
251
                'success' => false,
252
                'data' => 'ERROR_INVALID_PARAMETER'
253
            ];
254
 
255
            return new JsonModel($data);
256
        }
257
 
258
        $organizationalClimateMapper = OrganizationalClimateFormMapper::getInstance($this->adapter);
259
        $organizationalClimate = $organizationalClimateMapper->fetchOneByUuid($uuid);
260
 
261
        if (!$organizationalClimate) {
262
            $data = [
263
                'success' => false,
264
                'data' => 'ERROR_RECORD_NOT_FOUND'
265
            ];
266
 
267
            return new JsonModel($data);
268
        }
269
 
270
        if ($organizationalClimate->company_id != $currentCompany->id) {
271
            return new JsonModel([
272
                'success' => false,
273
                'data' => 'ERROR_UNAUTHORIZED'
274
            ]);
275
        }
276
 
277
 
278
        if ($request->isPost()) {
279
            $form = new OrganizationalClimateFormForm($this->adapter, $currentCompany->id);
280
            $dataPost = $request->getPost()->toArray();
281
            $dataPost['status'] = isset($dataPost['status']) ? $dataPost['status'] : OrganizationalClimateForm::STATUS_INACTIVE;
282
 
283
            $form->setData($dataPost);
284
 
285
            if ($form->isValid()) {
286
                $dataPost = (array) $form->getData();
287
 
288
                $hydrator = new ObjectPropertyHydrator();
289
                $hydrator->hydrate($dataPost, $organizationalClimate);
290
 
291
                if (!$organizationalClimate->status) {
292
                    $organizationalClimate->status = OrganizationalClimateForm::STATUS_INACTIVE;
293
                }
294
 
295
                $result = $organizationalClimateMapper->update($organizationalClimate);
296
 
297
                if ($result) {
298
                    $this->logger->info('Se edito el formulario ' . $organizationalClimate->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
299
                    $data = [
300
                        'success' => true,
301
                        'id' => $organizationalClimate->id,
302
                        'action_edit' => $this->url()->fromRoute('organizational-climate/form/edit', ['id' => $organizationalClimate->uuid]),
303
                        'data' => 'LABEL_RECORD_UPDATED'
304
                    ];
305
                } else {
306
                    $data = [
307
                        'success' => false,
308
                        'data' => $organizationalClimateMapper->getError()
309
                    ];
310
                }
311
 
312
                return new JsonModel($data);
313
            } else {
314
                $messages = [];
315
                $form_messages = (array) $form->getMessages();
316
                foreach ($form_messages as $fieldname => $field_messages) {
317
                    $messages[$fieldname] = array_values($field_messages);
318
                }
319
 
320
                return new JsonModel([
321
                    'success' => false,
322
                    'data' => $messages
323
                ]);
324
            }
325
        } else if ($request->isGet()) {
326
            $hydrator = new ObjectPropertyHydrator();
327
 
328
            $data = [
329
                'success' => true,
330
                'data' => [
331
                    'id' => $organizationalClimate->uuid,
332
                    'name' => $organizationalClimate->name,
333
                    'description' => $organizationalClimate->description,
334
                    'text' => $organizationalClimate->text,
335
                    'status' => $organizationalClimate->status,
336
                    'content' => $organizationalClimate->content ? json_decode($organizationalClimate->content) : [],
337
                ]
338
            ];
339
 
340
            return new JsonModel($data);
341
        } else {
342
            $data = [
343
                'success' => false,
344
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
345
            ];
346
 
347
            return new JsonModel($data);
348
        }
349
 
350
        return new JsonModel($data);
351
    }
352
 
353
    public function deleteAction() {
354
        $request = $this->getRequest();
355
        $currentUserPlugin = $this->plugin('currentUserPlugin');
356
        $currentCompany = $currentUserPlugin->getCompany();
357
        $currentUser = $currentUserPlugin->getUser();
358
 
359
        $request = $this->getRequest();
360
        $uuid = $this->params()->fromRoute('id');
361
 
362
        if (!$uuid) {
363
            $data = [
364
                'success' => false,
365
                'data' => 'ERROR_INVALID_PARAMETER'
366
            ];
367
 
368
            return new JsonModel($data);
369
        }
370
 
371
        $organizationalClimateMapper = OrganizationalClimateFormMapper::getInstance($this->adapter);
372
        $organizationalClimate = $organizationalClimateMapper->fetchOneByUuid($uuid);
373
        if (!$organizationalClimate) {
374
            $data = [
375
                'success' => false,
376
                'data' => 'ERROR_RECORD_NOT_FOUND'
377
            ];
378
 
379
            return new JsonModel($data);
380
        }
381
 
382
        if ($organizationalClimate->company_id != $currentCompany->id) {
383
            return new JsonModel([
384
                'success' => false,
385
                'data' => 'ERROR_UNAUTHORIZED'
386
            ]);
387
        }
388
 
389
        if ($request->isPost()) {
390
 
391
            $result = $organizationalClimateMapper->delete($organizationalClimate->id);
392
            if ($result) {
393
                $this->logger->info('Se borro el formulario ' . $organizationalClimate->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
394
 
395
                $data = [
396
                    'success' => true,
397
                    'data' => 'LABEL_RECORD_DELETED'
398
                ];
399
            } else {
400
 
401
                $data = [
402
                    'success' => false,
403
                    'data' => $organizationalClimateMapper->getError()
404
                ];
405
 
406
                return new JsonModel($data);
407
            }
408
        } else {
409
            $data = [
410
                'success' => false,
411
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
412
            ];
413
 
414
            return new JsonModel($data);
415
        }
416
 
417
        return new JsonModel($data);
418
    }
419
 
420
}