Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 281 | Rev 283 | 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;
277 geraldo 20
use LeadersLinked\Model\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']);
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
 
244 geraldo 113
 
282 geraldo 114
 
244 geraldo 115
                return new JsonModel([
116
                    'success' => true,
117
                    'data' => [
268 geraldo 118
                        'items' => $records,
272 geraldo 119
                        'total' => 10,
244 geraldo 120
                    ]
121
                ]);
122
            } else {
123
 
124
                $form = new CompanySelfEvaluationFormForm();
125
 
126
                $this->layout()->setTemplate('layout/layout-backend');
127
                $viewModel = new ViewModel();
250 geraldo 128
                $viewModel->setTemplate('leaders-linked/self-evaluation-reviews/index.phtml');
244 geraldo 129
                $viewModel->setVariable('form', $form);
130
                return $viewModel;
131
            }
132
        } else {
133
            return new JsonModel([
134
                'success' => false,
135
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
136
            ]);
137
            ;
138
        }
139
    }
140
 
141
    public function editAction() {
142
        $request = $this->getRequest();
143
        $currentUserPlugin = $this->plugin('currentUserPlugin');
144
        $currentCompany = $currentUserPlugin->getCompany();
145
        $currentUser = $currentUserPlugin->getUser();
146
 
147
        $request = $this->getRequest();
148
        $uuid = $this->params()->fromRoute('id');
149
 
150
 
151
        if (!$uuid) {
152
            $data = [
153
                'success' => false,
154
                'data' => 'ERROR_INVALID_PARAMETER'
155
            ];
156
 
157
            return new JsonModel($data);
158
        }
159
 
160
        $companySelfEvaluationMapper = CompanySelfEvaluationFormMapper::getInstance($this->adapter);
161
        $companySelfEvaluation = $companySelfEvaluationMapper->fetchOneByUuid($uuid);
162
        if (!$companySelfEvaluation) {
163
            $data = [
164
                'success' => false,
165
                'data' => 'ERROR_RECORD_NOT_FOUND'
166
            ];
167
 
168
            return new JsonModel($data);
169
        }
170
 
171
        if ($companySelfEvaluation->company_id != $currentCompany->id) {
172
            return new JsonModel([
173
                'success' => false,
174
                'data' => 'ERROR_UNAUTHORIZED'
175
            ]);
176
        }
177
 
178
 
179
        if ($request->isPost()) {
180
            $form = new CompanySelfEvaluationFormForm();
181
            $dataPost = $request->getPost()->toArray();
182
            $dataPost['status'] = isset($dataPost['status']) ? $dataPost['status'] : CompanySelfEvaluationForm::STATUS_INACTIVE;
183
 
184
            $form->setData($dataPost);
185
 
186
            if ($form->isValid()) {
187
                $dataPost = (array) $form->getData();
188
 
189
                $hydrator = new ObjectPropertyHydrator();
190
                $hydrator->hydrate($dataPost, $companySelfEvaluation);
191
 
192
                if (!$companySelfEvaluation->status) {
193
                    $companySelfEvaluation->status = CompanySelfEvaluationForm::STATUS_INACTIVE;
194
                }
195
 
196
                $result = $companySelfEvaluationMapper->update($companySelfEvaluation);
197
 
198
                if ($result) {
199
                    $this->logger->info('Se actualizo el tamaño de empresa ' . $companySelfEvaluation->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
200
                    $data = [
201
                        'success' => true,
202
                        'id' => $companySelfEvaluation->id,
203
                        'action_edit' => $this->url()->fromRoute('self-evaluation/forms/edit', ['id' => $companySelfEvaluation->uuid]),
204
                        'data' => 'LABEL_RECORD_UPDATED'
205
                    ];
206
                } else {
207
                    $data = [
208
                        'success' => false,
209
                        'data' => $companySelfEvaluationMapper->getError()
210
                    ];
211
                }
212
 
213
                return new JsonModel($data);
214
            } else {
215
                $messages = [];
216
                $form_messages = (array) $form->getMessages();
217
                foreach ($form_messages as $fieldname => $field_messages) {
218
                    $messages[$fieldname] = array_values($field_messages);
219
                }
220
 
221
                return new JsonModel([
222
                    'success' => false,
223
                    'data' => $messages
224
                ]);
225
            }
226
        } else if ($request->isGet()) {
227
            $hydrator = new ObjectPropertyHydrator();
228
 
229
            $data = [
230
                'success' => true,
231
                'data' => [
232
                    'id' => $companySelfEvaluation->uuid,
233
                    'name' => $companySelfEvaluation->name,
234
                    'description' => $companySelfEvaluation->description,
235
                    'text' => $companySelfEvaluation->text,
236
                    'language' => $companySelfEvaluation->language,
237
                    'status' => $companySelfEvaluation->status,
238
                    'content' => $companySelfEvaluation->content ? json_decode($companySelfEvaluation->content) : [],
239
                ]
240
            ];
241
 
242
            return new JsonModel($data);
243
        } else {
244
            $data = [
245
                'success' => false,
246
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
247
            ];
248
 
249
            return new JsonModel($data);
250
        }
251
 
252
        return new JsonModel($data);
253
    }
254
 
255
 
256
 
257
}