Proyectos de Subversion LeadersLinked - Backend

Rev

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