Proyectos de Subversion LeadersLinked - Backend

Rev

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