Proyectos de Subversion LeadersLinked - Backend

Rev

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