Proyectos de Subversion LeadersLinked - Backend

Rev

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

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