Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 16769 | | Comparar con el anterior | Ultima modificación | Ver Log |

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