Proyectos de Subversion LeadersLinked - Backend

Rev

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