Proyectos de Subversion LeadersLinked - Backend

Rev

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