Proyectos de Subversion LeadersLinked - Backend

Rev

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