Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 260 | Rev 269 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
244 geraldo 1
<?php
2
 
3
declare(strict_types=1);
4
 
5
namespace LeadersLinked\Controller;
6
 
7
use Laminas\Db\Adapter\AdapterInterface;
8
use Laminas\Cache\Storage\Adapter\AbstractAdapter;
9
use Laminas\Mvc\Controller\AbstractActionController;
10
use Laminas\Log\LoggerInterface;
11
use Laminas\View\Model\ViewModel;
12
use Laminas\View\Model\JsonModel;
13
use LeadersLinked\Library\Functions;
14
use LeadersLinked\Mapper\CompanySelfEvaluationFormMapper;
260 geraldo 15
use LeadersLinked\Mapper\CompanySelfEvaluationTestMapper;
244 geraldo 16
use LeadersLinked\Form\CompanySelfEvaluationFormForm;
17
use LeadersLinked\Model\CompanySelfEvaluationForm;
18
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
19
use LeadersLinked\Mapper\CompanySelfEvaluationFormUserMapper;
20
 
21
class SelfEvaluationReviewController extends AbstractActionController {
22
 
23
    /**
24
     *
25
     * @var AdapterInterface
26
     */
27
    private $adapter;
28
 
29
    /**
30
     *
31
     * @var AbstractAdapter
32
     */
33
    private $cache;
34
 
35
    /**
36
     *
37
     * @var  LoggerInterface
38
     */
39
    private $logger;
40
 
41
    /**
42
     *
43
     * @var array
44
     */
45
    private $config;
46
 
47
    /**
48
     *
49
     * @param AdapterInterface $adapter
50
     * @param AbstractAdapter $cache
51
     * @param LoggerInterface $logger
52
     * @param array $config
53
     */
54
    public function __construct($adapter, $cache, $logger, $config) {
55
        $this->adapter = $adapter;
56
        $this->cache = $cache;
57
        $this->logger = $logger;
58
        $this->config = $config;
59
    }
60
 
61
    public function indexAction() {
62
        $request = $this->getRequest();
63
        $currentUserPlugin = $this->plugin('currentUserPlugin');
64
        $currentCompany = $currentUserPlugin->getCompany();
65
        $currentUser = $currentUserPlugin->getUser();
66
 
67
 
68
        $request = $this->getRequest();
69
        if ($request->isGet()) {
70
 
71
            $headers = $request->getHeaders();
72
 
73
            $isJson = false;
74
            if ($headers->has('Accept')) {
75
                $accept = $headers->get('Accept');
76
 
77
                $prioritized = $accept->getPrioritized();
78
 
79
                foreach ($prioritized as $key => $value) {
80
                    $raw = trim($value->getRaw());
81
 
82
                    if (!$isJson) {
83
                        $isJson = strpos($raw, 'json');
84
                    }
85
                }
86
            }
87
 
88
            if ($isJson) {
89
                $search = $this->params()->fromQuery('search', []);
90
                $search = empty($search['value']) ? '' : filter_var($search['value'], FILTER_SANITIZE_STRING);
91
 
92
                $page = intval($this->params()->fromQuery('start', 1), 10);
93
                $records_x_page = intval($this->params()->fromQuery('length', 10), 10);
94
                $order = $this->params()->fromQuery('order', []);
95
                $order_field = empty($order[0]['column']) ? 99 : intval($order[0]['column'], 10);
96
                $order_direction = empty($order[0]['dir']) ? 'ASC' : strtoupper(filter_var($order[0]['dir'], FILTER_SANITIZE_STRING));
97
 
98
                $fields = ['name'];
99
                $order_field = isset($fields[$order_field]) ? $fields[$order_field] : 'name';
100
 
101
                if (!in_array($order_direction, ['ASC', 'DESC'])) {
102
                    $order_direction = 'ASC';
103
                }
104
 
268 geraldo 105
                $queryMapper = QueryMapper::getInstance($this->adapter);
244 geraldo 106
 
268 geraldo 107
                $select = $queryMapper->getSql()->select();
108
                $select->columns(['uuid', 'content','status','user_id','company_id']);
109
                $select->from(['test' => CompanySelfEvaluationTestMapper::_TABLE]);
110
                $select->join(['form' => CompanySelfEvaluationFormMapper::_TABLE], 'test.form_id = form.id', []);
111
                $select->where->equalTo('f.status', CompanySelfEvaluationForm::STATUS_ACTIVE);
244 geraldo 112
 
268 geraldo 113
                if ($search) {
114
                    $select->where->NEST->like('name', '%' . $search . '%');
115
                }
116
                $select->order('name ASC, language ASC');
244 geraldo 117
 
268 geraldo 118
                $records = $queryMapper->fetchAll($select);
119
                $items = [];
120
 
121
                array_push($items, $item);
244 geraldo 122
 
123
                return new JsonModel([
124
                    'success' => true,
125
                    'data' => [
268 geraldo 126
                        'items' => $records,
244 geraldo 127
                        'total' => $paginator->getTotalItemCount(),
128
                    ]
129
                ]);
130
            } else {
131
 
132
                $form = new CompanySelfEvaluationFormForm();
133
 
134
                $this->layout()->setTemplate('layout/layout-backend');
135
                $viewModel = new ViewModel();
250 geraldo 136
                $viewModel->setTemplate('leaders-linked/self-evaluation-reviews/index.phtml');
244 geraldo 137
                $viewModel->setVariable('form', $form);
138
                return $viewModel;
139
            }
140
        } else {
141
            return new JsonModel([
142
                'success' => false,
143
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
144
            ]);
145
            ;
146
        }
147
    }
148
 
149
    public function editAction() {
150
        $request = $this->getRequest();
151
        $currentUserPlugin = $this->plugin('currentUserPlugin');
152
        $currentCompany = $currentUserPlugin->getCompany();
153
        $currentUser = $currentUserPlugin->getUser();
154
 
155
        $request = $this->getRequest();
156
        $uuid = $this->params()->fromRoute('id');
157
 
158
 
159
        if (!$uuid) {
160
            $data = [
161
                'success' => false,
162
                'data' => 'ERROR_INVALID_PARAMETER'
163
            ];
164
 
165
            return new JsonModel($data);
166
        }
167
 
168
        $companySelfEvaluationMapper = CompanySelfEvaluationFormMapper::getInstance($this->adapter);
169
        $companySelfEvaluation = $companySelfEvaluationMapper->fetchOneByUuid($uuid);
170
        if (!$companySelfEvaluation) {
171
            $data = [
172
                'success' => false,
173
                'data' => 'ERROR_RECORD_NOT_FOUND'
174
            ];
175
 
176
            return new JsonModel($data);
177
        }
178
 
179
        if ($companySelfEvaluation->company_id != $currentCompany->id) {
180
            return new JsonModel([
181
                'success' => false,
182
                'data' => 'ERROR_UNAUTHORIZED'
183
            ]);
184
        }
185
 
186
 
187
        if ($request->isPost()) {
188
            $form = new CompanySelfEvaluationFormForm();
189
            $dataPost = $request->getPost()->toArray();
190
            $dataPost['status'] = isset($dataPost['status']) ? $dataPost['status'] : CompanySelfEvaluationForm::STATUS_INACTIVE;
191
 
192
            $form->setData($dataPost);
193
 
194
            if ($form->isValid()) {
195
                $dataPost = (array) $form->getData();
196
 
197
                $hydrator = new ObjectPropertyHydrator();
198
                $hydrator->hydrate($dataPost, $companySelfEvaluation);
199
 
200
                if (!$companySelfEvaluation->status) {
201
                    $companySelfEvaluation->status = CompanySelfEvaluationForm::STATUS_INACTIVE;
202
                }
203
 
204
                $result = $companySelfEvaluationMapper->update($companySelfEvaluation);
205
 
206
                if ($result) {
207
                    $this->logger->info('Se actualizo el tamaño de empresa ' . $companySelfEvaluation->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
208
                    $data = [
209
                        'success' => true,
210
                        'id' => $companySelfEvaluation->id,
211
                        'action_edit' => $this->url()->fromRoute('self-evaluation/forms/edit', ['id' => $companySelfEvaluation->uuid]),
212
                        'data' => 'LABEL_RECORD_UPDATED'
213
                    ];
214
                } else {
215
                    $data = [
216
                        'success' => false,
217
                        'data' => $companySelfEvaluationMapper->getError()
218
                    ];
219
                }
220
 
221
                return new JsonModel($data);
222
            } else {
223
                $messages = [];
224
                $form_messages = (array) $form->getMessages();
225
                foreach ($form_messages as $fieldname => $field_messages) {
226
                    $messages[$fieldname] = array_values($field_messages);
227
                }
228
 
229
                return new JsonModel([
230
                    'success' => false,
231
                    'data' => $messages
232
                ]);
233
            }
234
        } else if ($request->isGet()) {
235
            $hydrator = new ObjectPropertyHydrator();
236
 
237
            $data = [
238
                'success' => true,
239
                'data' => [
240
                    'id' => $companySelfEvaluation->uuid,
241
                    'name' => $companySelfEvaluation->name,
242
                    'description' => $companySelfEvaluation->description,
243
                    'text' => $companySelfEvaluation->text,
244
                    'language' => $companySelfEvaluation->language,
245
                    'status' => $companySelfEvaluation->status,
246
                    'content' => $companySelfEvaluation->content ? json_decode($companySelfEvaluation->content) : [],
247
                ]
248
            ];
249
 
250
            return new JsonModel($data);
251
        } else {
252
            $data = [
253
                'success' => false,
254
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
255
            ];
256
 
257
            return new JsonModel($data);
258
        }
259
 
260
        return new JsonModel($data);
261
    }
262
 
263
 
264
 
265
}