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\EmailTemplateMapper;
17002 efrain 14
use LeadersLinked\Form\EmailTemplate\EmailTemplateForm;
1 www 15
use LeadersLinked\Model\EmailTemplate;
16
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
17
 
17002 efrain 18
 
1 www 19
class EmailTemplateController extends AbstractActionController
20
{
21
    /**
22
     *
16769 efrain 23
     * @var \Laminas\Db\Adapter\AdapterInterface
1 www 24
     */
25
    private $adapter;
26
 
27
    /**
28
     *
16769 efrain 29
     * @var \LeadersLinked\Cache\CacheInterface
1 www 30
     */
16769 efrain 31
    private $cache;
32
 
33
 
34
    /**
35
     *
36
     * @var \Laminas\Log\LoggerInterface
37
     */
1 www 38
    private $logger;
39
 
40
    /**
41
     *
42
     * @var array
43
     */
44
    private $config;
16768 efrain 45
 
16769 efrain 46
 
1 www 47
    /**
48
     *
16769 efrain 49
     * @var \Laminas\Mvc\I18n\Translator
50
     */
51
    private $translator;
52
 
53
 
54
    /**
55
     *
56
     * @param \Laminas\Db\Adapter\AdapterInterface $adapter
57
     * @param \LeadersLinked\Cache\CacheInterface $cache
58
     * @param \Laminas\Log\LoggerInterface LoggerInterface $logger
1 www 59
     * @param array $config
16769 efrain 60
     * @param \Laminas\Mvc\I18n\Translator $translator
1 www 61
     */
16769 efrain 62
    public function __construct($adapter, $cache, $logger, $config, $translator)
1 www 63
    {
16769 efrain 64
        $this->adapter      = $adapter;
65
        $this->cache        = $cache;
66
        $this->logger       = $logger;
67
        $this->config       = $config;
68
        $this->translator   = $translator;
1 www 69
    }
70
 
71
    public function indexAction()
72
    {
15338 efrain 73
        $currentNetworkPlugin = $this->plugin('currentNetworkPlugin');
74
        $currentNetwork = $currentNetworkPlugin->getNetwork();
1 www 75
 
76
        $request = $this->getRequest();
77
        if($request->isGet()) {
78
 
79
 
80
            $headers  = $request->getHeaders();
81
 
82
            $isJson = false;
83
            if($headers->has('Accept')) {
84
                $accept = $headers->get('Accept');
85
 
86
                $prioritized = $accept->getPrioritized();
87
 
88
                foreach($prioritized as $key => $value) {
89
                    $raw = trim($value->getRaw());
90
 
91
                    if(!$isJson) {
92
                        $isJson = strpos($raw, 'json');
93
                    }
94
 
95
                }
96
            }
97
 
98
            if($isJson) {
99
                $search = $this->params()->fromQuery('search', []);
16766 efrain 100
                $search = empty($search['value']) ? '' :  Functions::sanitizeFilterString($search['value']);
1 www 101
 
102
                $page               = intval($this->params()->fromQuery('start', 1), 10);
103
                $records_x_page     = intval($this->params()->fromQuery('length', 10), 10);
104
                $order =  $this->params()->fromQuery('order', []);
105
                $order_field        = empty($order[0]['column']) ? 99 :  intval($order[0]['column'], 10);
16766 efrain 106
                $order_direction    = empty($order[0]['dir']) ? 'ASC' : strtoupper(Functions::sanitizeFilterString($order[0]['dir']));
1 www 107
 
108
                $fields =  ['subject'];
109
                $order_field = isset($fields[$order_field]) ? $fields[$order_field] : 'subject';
110
 
111
                if(!in_array($order_direction, ['ASC', 'DESC'])) {
112
                    $order_direction = 'ASC';
113
                }
114
 
115
                $emailTemplateMapper = EmailTemplateMapper::getInstance($this->adapter);
15457 efrain 116
                $paginator = $emailTemplateMapper->fetchAllDataTableForNetwork($search, $currentNetwork->id, $page, $records_x_page, $order_field, $order_direction);
1 www 117
 
118
                $items = [];
119
                $records = $paginator->getCurrentItems();
120
                foreach($records as $record)
121
                {
122
                    $item = [
123
                        'subject' => $record->subject,
124
                        'status' => $record->status,
125
                        'actions' => [
126
                            'link_edit' => $this->url()->fromRoute('settings/email-templates/edit', ['id' => $record->id ]),
127
                        ]
128
                    ];
129
 
130
                    array_push($items, $item);
131
                }
132
 
133
                return new JsonModel([
134
                    'success' => true,
135
                    'data' => [
136
                        'items' => $items,
137
                        'total' => $paginator->getTotalItemCount(),
138
                    ]
139
                ]);
140
            } else  {
141
                $form = new EmailTemplateForm();
142
 
143
                $this->layout()->setTemplate('layout/layout-backend');
144
                $viewModel = new ViewModel();
145
                $viewModel->setTemplate('leaders-linked/email-templates/index.phtml');
146
                $viewModel->setVariable('form', $form);
147
                return $viewModel ;
148
            }
149
        } else {
150
            return new JsonModel([
151
                'success' => false,
152
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
153
            ]);;
154
        }
155
    }
156
 
157
 
158
 
159
    public function editAction()
160
    {
161
        $currentUserPlugin = $this->plugin('currentUserPlugin');
162
        $currentUser = $currentUserPlugin->getUser();
163
 
164
        $request = $this->getRequest();
165
        $id = $this->params()->fromRoute('id');
166
 
167
 
168
        if(!$id) {
169
            $data = [
170
                'success'   => false,
171
                'data'   => 'ERROR_INVALID_PARAMETER'
172
            ];
173
 
174
            return new JsonModel($data);
175
        }
176
 
177
        $emailTemplateMapper = EmailTemplateMapper::getInstance($this->adapter);
178
        $emailTemplate = $emailTemplateMapper->fetchOne($id);
179
        if(!$emailTemplate) {
180
            $data = [
181
                'success'   => false,
182
                'data'   => 'ERROR_RECORD_NOT_FOUND'
183
            ];
184
 
185
            return new JsonModel($data);
186
        }
187
 
188
        if($request->isPost()) {
189
            $form = new  EmailTemplateForm();
190
            $dataPost = $request->getPost()->toArray();
191
            $dataPost['status'] =  isset($dataPost['status']) ? $dataPost['status'] : EmailTemplate::STATUS_INACTIVE;
192
 
193
            $form->setData($dataPost);
194
 
195
            if($form->isValid()) {
196
                $dataPost = (array) $form->getData();
197
 
198
                $hydrator = new ObjectPropertyHydrator();
199
                $hydrator->hydrate($dataPost, $emailTemplate);
200
                $result = $emailTemplateMapper->update($emailTemplate);
201
 
202
                if($result) {
203
                    $this->logger->info('Se actualizo el modelo del correo electrónico ' . $emailTemplate->id, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
204
 
205
                    $data = [
206
                        'success' => true,
207
                        'data' => 'LABEL_RECORD_UPDATED'
208
                    ];
209
                } else {
210
                    $data = [
211
                        'success'   => false,
212
                        'data'      => $emailTemplateMapper->getError()
213
                    ];
214
                }
215
 
216
                return new JsonModel($data);
217
 
218
            } else {
219
                $messages = [];
220
                $form_messages = (array) $form->getMessages();
221
                foreach($form_messages  as $fieldname => $field_messages)
222
                {
223
                    $messages[$fieldname] = array_values($field_messages);
224
                }
225
 
226
                return new JsonModel([
227
                    'success'   => false,
228
                    'data'   => $messages
229
                ]);
230
            }
231
        } else if ($request->isGet()) {
232
            $hydrator = new ObjectPropertyHydrator();
233
 
234
            $data = [
235
                'success' => true,
236
                'data' => $hydrator->extract($emailTemplate)
237
            ];
238
 
239
            return new JsonModel($data);
240
        } else {
241
            $data = [
242
                'success' => false,
243
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
244
            ];
245
 
246
            return new JsonModel($data);
247
        }
248
 
249
        return new JsonModel($data);
250
    }
251
 
252
 
253
}