Proyectos de Subversion LeadersLinked - Backend

Rev

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