Proyectos de Subversion LeadersLinked - Backend

Rev

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