Proyectos de Subversion LeadersLinked - Backend

Rev

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