Proyectos de Subversion LeadersLinked - Backend

Rev

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