16248 |
efrain |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
declare(strict_types=1);
|
|
|
4 |
|
|
|
5 |
namespace LeadersLinked\Controller;
|
|
|
6 |
|
|
|
7 |
use Laminas\Db\Adapter\AdapterInterface;
|
16768 |
efrain |
8 |
|
16248 |
efrain |
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\KnowledgeAreaCategoryMapper;
|
|
|
15 |
use LeadersLinked\Model\KnowledgeAreaCategory;
|
|
|
16 |
use LeadersLinked\Mapper\UserMapper;
|
|
|
17 |
use LeadersLinked\Mapper\KnowledgeAreaCategoryUserMapper;
|
|
|
18 |
use LeadersLinked\Form\KnowledgeArea\KnowledgeAreaCategoryUserDataForm;
|
|
|
19 |
use LeadersLinked\Form\KnowledgeArea\KnowledgeAreaCategoryUserForm;
|
|
|
20 |
use LeadersLinked\Mapper\QueryMapper;
|
|
|
21 |
use Laminas\Paginator\Adapter\DbSelect;
|
|
|
22 |
use Laminas\Paginator\Paginator;
|
|
|
23 |
use LeadersLinked\Model\KnowledgeAreaCategoryUser;
|
|
|
24 |
|
|
|
25 |
|
|
|
26 |
class KnowledgeAreaCategoryUserController extends AbstractActionController {
|
|
|
27 |
|
|
|
28 |
/**
|
|
|
29 |
*
|
16769 |
efrain |
30 |
* @var \Laminas\Db\Adapter\AdapterInterface
|
16248 |
efrain |
31 |
*/
|
|
|
32 |
private $adapter;
|
16768 |
efrain |
33 |
|
16248 |
efrain |
34 |
/**
|
|
|
35 |
*
|
16769 |
efrain |
36 |
* @var \LeadersLinked\Cache\CacheInterface
|
16248 |
efrain |
37 |
*/
|
16769 |
efrain |
38 |
private $cache;
|
|
|
39 |
|
|
|
40 |
|
|
|
41 |
/**
|
|
|
42 |
*
|
|
|
43 |
* @var \Laminas\Log\LoggerInterface
|
|
|
44 |
*/
|
16248 |
efrain |
45 |
private $logger;
|
16768 |
efrain |
46 |
|
16248 |
efrain |
47 |
/**
|
|
|
48 |
*
|
|
|
49 |
* @var array
|
|
|
50 |
*/
|
|
|
51 |
private $config;
|
16768 |
efrain |
52 |
|
16769 |
efrain |
53 |
|
16248 |
efrain |
54 |
/**
|
|
|
55 |
*
|
16769 |
efrain |
56 |
* @var \Laminas\Mvc\I18n\Translator
|
|
|
57 |
*/
|
|
|
58 |
private $translator;
|
|
|
59 |
|
|
|
60 |
|
|
|
61 |
/**
|
|
|
62 |
*
|
|
|
63 |
* @param \Laminas\Db\Adapter\AdapterInterface $adapter
|
|
|
64 |
* @param \LeadersLinked\Cache\CacheInterface $cache
|
|
|
65 |
* @param \Laminas\Log\LoggerInterface LoggerInterface $logger
|
16248 |
efrain |
66 |
* @param array $config
|
16769 |
efrain |
67 |
* @param \Laminas\Mvc\I18n\Translator $translator
|
16248 |
efrain |
68 |
*/
|
16769 |
efrain |
69 |
public function __construct($adapter, $cache, $logger, $config, $translator)
|
16768 |
efrain |
70 |
{
|
16769 |
efrain |
71 |
$this->adapter = $adapter;
|
|
|
72 |
$this->cache = $cache;
|
|
|
73 |
$this->logger = $logger;
|
|
|
74 |
$this->config = $config;
|
|
|
75 |
$this->translator = $translator;
|
16248 |
efrain |
76 |
}
|
|
|
77 |
|
|
|
78 |
public function indexAction() {
|
|
|
79 |
$request = $this->getRequest();
|
|
|
80 |
$currentUserPlugin = $this->plugin('currentUserPlugin');
|
|
|
81 |
$currentCompany = $currentUserPlugin->getCompany();
|
|
|
82 |
$currentUser = $currentUserPlugin->getUser();
|
|
|
83 |
|
|
|
84 |
|
|
|
85 |
$request = $this->getRequest();
|
|
|
86 |
if ($request->isGet()) {
|
|
|
87 |
|
|
|
88 |
$headers = $request->getHeaders();
|
|
|
89 |
|
|
|
90 |
$isJson = false;
|
|
|
91 |
if ($headers->has('Accept')) {
|
|
|
92 |
$accept = $headers->get('Accept');
|
|
|
93 |
|
|
|
94 |
$prioritized = $accept->getPrioritized();
|
|
|
95 |
|
|
|
96 |
foreach ($prioritized as $key => $value) {
|
|
|
97 |
$raw = trim($value->getRaw());
|
|
|
98 |
|
|
|
99 |
if (!$isJson) {
|
|
|
100 |
$isJson = strpos($raw, 'json');
|
|
|
101 |
}
|
|
|
102 |
}
|
|
|
103 |
}
|
|
|
104 |
|
|
|
105 |
//$isJson = true;
|
|
|
106 |
if ($isJson) {
|
|
|
107 |
|
16766 |
efrain |
108 |
$category_uuid = Functions::sanitizeFilterString($this->params()->fromQuery('category_id'));
|
|
|
109 |
|
16248 |
efrain |
110 |
if(!$category_uuid) {
|
|
|
111 |
return new JsonModel([
|
|
|
112 |
'success' => true,
|
|
|
113 |
'data' => [
|
|
|
114 |
'total' => 0,
|
|
|
115 |
'items' => [],
|
|
|
116 |
'link_add' => '',
|
|
|
117 |
'link_upload' => '',
|
|
|
118 |
]
|
|
|
119 |
]);
|
|
|
120 |
|
|
|
121 |
}
|
|
|
122 |
|
|
|
123 |
|
|
|
124 |
$knowledgeAreaCategoryMapper = KnowledgeAreaCategoryMapper::getInstance($this->adapter);
|
|
|
125 |
$knowledgeAreaCategory = $knowledgeAreaCategoryMapper->fetchOneByUuid($category_uuid);
|
|
|
126 |
|
|
|
127 |
if(!$knowledgeAreaCategory) {
|
|
|
128 |
return new JsonModel([
|
|
|
129 |
'success' => false,
|
|
|
130 |
'data' => 'ERROR_KNOWLEDGE_AREA_CATEGORY_NOT_FOUND'
|
|
|
131 |
]);
|
|
|
132 |
}
|
|
|
133 |
|
|
|
134 |
if($knowledgeAreaCategory->company_id != $currentCompany->id) {
|
|
|
135 |
return new JsonModel([
|
|
|
136 |
'success' => false,
|
|
|
137 |
'data' => 'ERROR_KNOWLEDGE_AREA_CATEGORY_IS_OTHER_COMPANY'
|
|
|
138 |
]);
|
|
|
139 |
}
|
|
|
140 |
|
|
|
141 |
|
|
|
142 |
|
|
|
143 |
|
|
|
144 |
$search = $this->params()->fromQuery('search', []);
|
16766 |
efrain |
145 |
$search = empty($search['value']) ? '' : Functions::sanitizeFilterString($search['value']);
|
16248 |
efrain |
146 |
|
|
|
147 |
$start = intval($this->params()->fromQuery('start', 0), 10);
|
|
|
148 |
$records_x_page = intval($this->params()->fromQuery('length', 10), 10);
|
|
|
149 |
$page = intval($start / $records_x_page);
|
|
|
150 |
$page++;
|
|
|
151 |
|
|
|
152 |
$order = $this->params()->fromQuery('order', []);
|
|
|
153 |
$order_field = empty($order[0]['column']) ? 99 : intval($order[0]['column'], 10);
|
16766 |
efrain |
154 |
$order_direction = empty($order[0]['dir']) ? 'ASC' : Functions::sanitizeFilterString(filter_var($order[0]['dir']));
|
16248 |
efrain |
155 |
|
|
|
156 |
$fields = ['first_name', 'last_name', 'email'];
|
|
|
157 |
$order_field = isset($fields[$order_field]) ? $fields[$order_field] : 'first_name';
|
|
|
158 |
|
|
|
159 |
if (!in_array($order_direction, ['ASC', 'DESC'])) {
|
|
|
160 |
$order_direction = 'ASC';
|
|
|
161 |
}
|
|
|
162 |
|
|
|
163 |
|
|
|
164 |
|
|
|
165 |
$acl = $this->getEvent()->getViewModel()->getVariable('acl');
|
|
|
166 |
$allowAdd = $acl->isAllowed($currentUser->usertype_id, 'knowledge-area/categories/users/add');
|
|
|
167 |
$allowEdit = $acl->isAllowed($currentUser->usertype_id, 'knowledge-area/categories/users/edit');
|
|
|
168 |
$allowDelete = $acl->isAllowed($currentUser->usertype_id, 'knowledge-area/categories/users/delete');
|
|
|
169 |
$allowUpload = $acl->isAllowed($currentUser->usertype_id, 'knowledge-area/categories/users/upload');
|
|
|
170 |
|
|
|
171 |
|
|
|
172 |
$items = [];
|
|
|
173 |
|
|
|
174 |
$queryMapper = QueryMapper::getInstance($this->adapter);
|
|
|
175 |
$select = $queryMapper->getSql()->select();
|
|
|
176 |
$select->columns(['role']);
|
|
|
177 |
$select->from(['cu' => KnowledgeAreaCategoryUserMapper::_TABLE]);
|
|
|
178 |
$select->join(['u' => UserMapper::_TABLE], 'cu.user_id = u.id', ['uuid', 'first_name', 'last_name', 'email']);
|
|
|
179 |
$select->where->equalTo('cu.category_id', $knowledgeAreaCategory->id);
|
|
|
180 |
|
|
|
181 |
|
|
|
182 |
if($search) {
|
|
|
183 |
$select->where->nest()->like('first_name', '%' . $search . '%')
|
|
|
184 |
->or->like('last_name', '%' . $search . '%')
|
|
|
185 |
->or->like('email', '%' . $search . '%')->unnest();
|
|
|
186 |
}
|
|
|
187 |
|
|
|
188 |
$select->order($order_field . ' ' . $order_direction);
|
|
|
189 |
|
|
|
190 |
//echo $select->getSqlString($this->adapter->platform); exit;
|
|
|
191 |
|
|
|
192 |
|
|
|
193 |
$paginatorAdapter = new DbSelect($select, $this->adapter);
|
|
|
194 |
$paginator = new Paginator($paginatorAdapter);
|
|
|
195 |
$paginator->setItemCountPerPage($records_x_page);
|
|
|
196 |
$paginator->setCurrentPageNumber($page);
|
|
|
197 |
|
|
|
198 |
$records = $paginator->getCurrentItems();
|
|
|
199 |
|
|
|
200 |
foreach ($records as $record) {
|
|
|
201 |
|
|
|
202 |
switch($record['role'])
|
|
|
203 |
{
|
|
|
204 |
|
|
|
205 |
case KnowledgeAreaCategoryUser::ROLE_ADMINISTRATOR :
|
|
|
206 |
$role = 'LABEL_ADMINISTRATOR';
|
|
|
207 |
break;
|
|
|
208 |
|
|
|
209 |
case KnowledgeAreaCategoryUser::ROLE_EDITOR :
|
|
|
210 |
$role = 'LABEL_EDITOR';
|
|
|
211 |
break;
|
|
|
212 |
|
|
|
213 |
case KnowledgeAreaCategoryUser::ROLE_USER :
|
|
|
214 |
$role = 'LABEL_USER';
|
|
|
215 |
break;
|
|
|
216 |
|
|
|
217 |
default :
|
|
|
218 |
$role = 'LABEL_UNKNOWN';
|
|
|
219 |
break;
|
|
|
220 |
}
|
|
|
221 |
|
|
|
222 |
|
|
|
223 |
|
|
|
224 |
|
|
|
225 |
|
|
|
226 |
|
|
|
227 |
$item = [
|
|
|
228 |
'first_name' => $record['first_name'],
|
|
|
229 |
'last_name' => $record['first_name'],
|
|
|
230 |
'email' => $record['email'],
|
|
|
231 |
'role' => $role,
|
|
|
232 |
'actions' => [
|
|
|
233 |
'link_edit' => $allowEdit ? $this->url()->fromRoute('knowledge-area/categories/users/edit', ['id' => $knowledgeAreaCategory->uuid, 'user_id' => $record['uuid'] ]) : '',
|
|
|
234 |
'link_delete' => $allowDelete ? $this->url()->fromRoute('knowledge-area/categories/users/delete', ['id' => $knowledgeAreaCategory->uuid, 'user_id' => $record['uuid'] ]) : '',
|
|
|
235 |
]
|
|
|
236 |
];
|
|
|
237 |
|
|
|
238 |
array_push($items, $item);
|
|
|
239 |
}
|
|
|
240 |
|
|
|
241 |
|
|
|
242 |
if($knowledgeAreaCategory->privacy == KnowledgeAreaCategory::PRIVACY_COMPANY) {
|
|
|
243 |
$roles = [
|
|
|
244 |
KnowledgeAreaCategoryUser::ROLE_USER => 'LABEL_USER',
|
|
|
245 |
KnowledgeAreaCategoryUser::ROLE_EDITOR => 'LABEL_EDITOR',
|
|
|
246 |
KnowledgeAreaCategoryUser::ROLE_ADMINISTRATOR => 'LABEL_ADMINISTRATOR',
|
|
|
247 |
];
|
|
|
248 |
} else {
|
|
|
249 |
$roles = [
|
|
|
250 |
KnowledgeAreaCategoryUser::ROLE_EDITOR => 'LABEL_EDITOR',
|
|
|
251 |
KnowledgeAreaCategoryUser::ROLE_ADMINISTRATOR => 'LABEL_ADMINISTRATOR',
|
|
|
252 |
];
|
|
|
253 |
}
|
|
|
254 |
|
|
|
255 |
|
|
|
256 |
|
|
|
257 |
return new JsonModel([
|
|
|
258 |
'success' => true,
|
|
|
259 |
'data' => [
|
|
|
260 |
'total' => $paginator->getTotalItemCount(),
|
|
|
261 |
'items' => $items,
|
|
|
262 |
'link_add' => $allowAdd ? $this->url()->fromRoute('knowledge-area/categories/users/add', ['id' => $knowledgeAreaCategory->uuid ] ) : '',
|
|
|
263 |
'link_upload' => $allowUpload ? $this->url()->fromRoute('knowledge-area/categories/users/upload', ['id' => $knowledgeAreaCategory->uuid ] ) : '',
|
|
|
264 |
'roles' => $roles,
|
|
|
265 |
]
|
|
|
266 |
]);
|
|
|
267 |
} else {
|
|
|
268 |
|
|
|
269 |
$form = new KnowledgeAreaCategoryUserForm($this->adapter, $currentCompany->id, KnowledgeAreaCategory::PRIVACY_COMPANY);
|
|
|
270 |
$formFilter = new KnowledgeAreaCategoryUserDataForm($this->adapter, $currentCompany->id);
|
|
|
271 |
|
|
|
272 |
|
|
|
273 |
$this->layout()->setTemplate('layout/layout-backend');
|
|
|
274 |
$viewModel = new ViewModel();
|
|
|
275 |
$viewModel->setTemplate('leaders-linked/knowledge-area-category-users/index.phtml');
|
|
|
276 |
$viewModel->setVariables([
|
|
|
277 |
'form' => $form,
|
|
|
278 |
'formFilter' => $formFilter,
|
|
|
279 |
]);
|
|
|
280 |
return $viewModel;
|
|
|
281 |
}
|
|
|
282 |
} else {
|
|
|
283 |
return new JsonModel([
|
|
|
284 |
'success' => false,
|
|
|
285 |
'data' => 'ERROR_METHOD_NOT_ALLOWED'
|
|
|
286 |
]);
|
|
|
287 |
}
|
|
|
288 |
|
|
|
289 |
}
|
|
|
290 |
|
|
|
291 |
public function addAction()
|
|
|
292 |
{
|
|
|
293 |
$currentUserPlugin = $this->plugin('currentUserPlugin');
|
|
|
294 |
$currentCompany = $currentUserPlugin->getCompany();
|
|
|
295 |
$currentUser = $currentUserPlugin->getUser();
|
|
|
296 |
$request = $this->getRequest();
|
|
|
297 |
|
|
|
298 |
if($request->isPost()) {
|
|
|
299 |
|
16766 |
efrain |
300 |
$category_uuid = Functions::sanitizeFilterString($this->params()->fromRoute('id'));
|
16248 |
efrain |
301 |
|
|
|
302 |
$knowledgeAreaCategoryMapper = KnowledgeAreaCategoryMapper::getInstance($this->adapter);
|
|
|
303 |
$knowledgeAreaCategory = $knowledgeAreaCategoryMapper->fetchOneByUuid($category_uuid);
|
|
|
304 |
|
|
|
305 |
if(!$knowledgeAreaCategory) {
|
|
|
306 |
return new JsonModel([
|
|
|
307 |
'success' => false,
|
|
|
308 |
'data' => 'ERROR_KNOWLEDGE_AREA_CATEGORY_NOT_FOUND'
|
|
|
309 |
]);
|
|
|
310 |
}
|
|
|
311 |
|
|
|
312 |
if($knowledgeAreaCategory->company_id != $currentCompany->id) {
|
|
|
313 |
return new JsonModel([
|
|
|
314 |
'success' => false,
|
|
|
315 |
'data' => 'ERROR_KNOWLEDGE_AREA_CATEGORY_IS_OTHER_COMPANY'
|
|
|
316 |
]);
|
|
|
317 |
}
|
|
|
318 |
|
|
|
319 |
$dataPost = $request->getPost()->toArray();
|
|
|
320 |
|
|
|
321 |
$form = new KnowledgeAreaCategoryUserForm($this->adapter, $currentCompany->id, $knowledgeAreaCategory->privacy);
|
|
|
322 |
$form->setData($dataPost);
|
|
|
323 |
|
|
|
324 |
if($form->isValid()) {
|
|
|
325 |
|
|
|
326 |
$dataPost = (array) $form->getData();
|
|
|
327 |
|
|
|
328 |
|
|
|
329 |
$userMapper = UserMapper::getInstance($this->adapter);
|
|
|
330 |
$user = $userMapper->fetchOneByUuid($dataPost['user_id']);
|
|
|
331 |
|
|
|
332 |
$knowledgeAreaCategoryUserMapper = KnowledgeAreaCategoryUserMapper::getInstance($this->adapter);
|
|
|
333 |
$knowledgeAreaCategoryUser = $knowledgeAreaCategoryUserMapper->fetchOneByCategoryIdAndUserId($knowledgeAreaCategory->id, $user->id);
|
|
|
334 |
|
|
|
335 |
if($knowledgeAreaCategoryUser) {
|
|
|
336 |
return new JsonModel([
|
|
|
337 |
'success' => false,
|
|
|
338 |
'data' => 'ERROR_KNOWLEDGE_AREA_CATEGORY_USER_ALREADY_FOUND'
|
|
|
339 |
]);
|
|
|
340 |
}
|
|
|
341 |
|
|
|
342 |
|
|
|
343 |
$knowledgeAreaCategoryUser = new KnowledgeAreaCategoryUser();
|
|
|
344 |
$knowledgeAreaCategoryUser->category_id = $knowledgeAreaCategory->id;
|
|
|
345 |
$knowledgeAreaCategoryUser->user_id = $user->id;
|
|
|
346 |
$knowledgeAreaCategoryUser->role = $dataPost['role'];
|
|
|
347 |
$knowledgeAreaCategoryUserMapper = KnowledgeAreaCategoryUserMapper::getInstance($this->adapter);
|
|
|
348 |
|
|
|
349 |
|
|
|
350 |
if($knowledgeAreaCategoryUserMapper->insert($knowledgeAreaCategoryUser)) {
|
|
|
351 |
|
|
|
352 |
|
|
|
353 |
$this->logger->info('Se agrego el usuario ' . $user->first_name . ' ' . $user->last_name . ' (' . $user->email . ') la categoria ' . $knowledgeAreaCategory->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
|
|
|
354 |
|
|
|
355 |
$data = [
|
|
|
356 |
'success' => true,
|
|
|
357 |
'data' => 'LABEL_RECORD_ADDED'
|
|
|
358 |
];
|
|
|
359 |
} else {
|
|
|
360 |
$data = [
|
|
|
361 |
'success' => false,
|
|
|
362 |
'data' => $knowledgeAreaCategoryUserMapper->getError()
|
|
|
363 |
];
|
|
|
364 |
|
|
|
365 |
}
|
|
|
366 |
|
|
|
367 |
return new JsonModel($data);
|
|
|
368 |
|
|
|
369 |
} else {
|
|
|
370 |
$messages = [];
|
|
|
371 |
$form_messages = (array) $form->getMessages();
|
|
|
372 |
foreach ($form_messages as $fieldname => $field_messages) {
|
|
|
373 |
|
|
|
374 |
$messages[$fieldname] = array_values($field_messages);
|
|
|
375 |
}
|
|
|
376 |
|
|
|
377 |
return new JsonModel([
|
|
|
378 |
'success' => false,
|
|
|
379 |
'data' => $messages
|
|
|
380 |
]);
|
|
|
381 |
|
|
|
382 |
}
|
|
|
383 |
|
|
|
384 |
} else {
|
|
|
385 |
$data = [
|
|
|
386 |
'success' => false,
|
|
|
387 |
'data' => 'ERROR_METHOD_NOT_ALLOWED'
|
|
|
388 |
];
|
|
|
389 |
|
|
|
390 |
return new JsonModel($data);
|
|
|
391 |
}
|
|
|
392 |
|
|
|
393 |
return new JsonModel($data);
|
|
|
394 |
|
|
|
395 |
|
|
|
396 |
}
|
|
|
397 |
|
|
|
398 |
public function editAction()
|
|
|
399 |
{
|
|
|
400 |
$request = $this->getRequest();
|
|
|
401 |
$currentUserPlugin = $this->plugin('currentUserPlugin');
|
|
|
402 |
$currentCompany = $currentUserPlugin->getCompany();
|
|
|
403 |
$currentUser = $currentUserPlugin->getUser();
|
|
|
404 |
|
|
|
405 |
$request = $this->getRequest();
|
|
|
406 |
$uuid = $this->params()->fromRoute('id');
|
|
|
407 |
$user_uuid = $this->params()->fromRoute('user_id');
|
|
|
408 |
|
|
|
409 |
$knowledgeAreaCategoryMapper = KnowledgeAreaCategoryMapper::getInstance($this->adapter);
|
|
|
410 |
$knowledgeAreaCategory = $knowledgeAreaCategoryMapper->fetchOneByUuid($uuid);
|
|
|
411 |
|
|
|
412 |
if(!$knowledgeAreaCategory) {
|
|
|
413 |
return new JsonModel([
|
|
|
414 |
'success' => false,
|
|
|
415 |
'data' => 'ERROR_KNOWLEDGE_AREA_CATEGORY_NOT_FOUND'
|
|
|
416 |
]);
|
|
|
417 |
}
|
|
|
418 |
|
|
|
419 |
if($knowledgeAreaCategory->company_id != $currentCompany->id) {
|
|
|
420 |
return new JsonModel([
|
|
|
421 |
'success' => false,
|
|
|
422 |
'data' => 'ERROR_KNOWLEDGE_AREA_CATEGORY_IS_OTHER_COMPANY'
|
|
|
423 |
]);
|
|
|
424 |
}
|
|
|
425 |
|
|
|
426 |
$userMapper = UserMapper::getInstance($this->adapter);
|
|
|
427 |
$user = $userMapper->fetchOneByUuid($user_uuid);
|
|
|
428 |
|
|
|
429 |
if(!$user) {
|
|
|
430 |
return new JsonModel([
|
|
|
431 |
'success' => false,
|
|
|
432 |
'data' => 'ERROR_USER_NOT_FOUND'
|
|
|
433 |
]);
|
|
|
434 |
|
|
|
435 |
}
|
|
|
436 |
|
|
|
437 |
$knowledgeAreaCategoryUserMapper = KnowledgeAreaCategoryUserMapper::getInstance($this->adapter);
|
|
|
438 |
$knowledgeAreaCategoryUser = $knowledgeAreaCategoryUserMapper->fetchOneByCategoryIdAndUserId($knowledgeAreaCategory->id, $user->id);
|
|
|
439 |
|
|
|
440 |
if(!$knowledgeAreaCategoryUser) {
|
|
|
441 |
return new JsonModel([
|
|
|
442 |
'success' => false,
|
|
|
443 |
'data' => 'ERROR_KNOWLEDGE_AREA_CATEGORY_USER_NOT_FOUND'
|
|
|
444 |
]);
|
|
|
445 |
}
|
|
|
446 |
|
|
|
447 |
|
|
|
448 |
|
|
|
449 |
if ($request->isPost()) {
|
|
|
450 |
$dataPost = $request->getPost()->toArray();
|
|
|
451 |
|
|
|
452 |
$form = new KnowledgeAreaCategoryUserForm($this->adapter, $currentCompany->id, $knowledgeAreaCategory->privacy);
|
|
|
453 |
$form->setData($dataPost);
|
|
|
454 |
|
|
|
455 |
|
|
|
456 |
|
|
|
457 |
if ($form->isValid()) {
|
|
|
458 |
$dataPost = (array) $form->getData();
|
|
|
459 |
|
|
|
460 |
$knowledgeAreaCategoryUser->role = $dataPost['role'];
|
|
|
461 |
|
|
|
462 |
|
|
|
463 |
if($knowledgeAreaCategoryUserMapper->update($knowledgeAreaCategoryUser)) {
|
|
|
464 |
|
|
|
465 |
$this->logger->info('Se actualizo el usuario ' . $user->first_name . ' ' . $user->last_name . ' (' . $user->email . ') la categoria ' . $knowledgeAreaCategory->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
|
|
|
466 |
|
|
|
467 |
$data = [
|
|
|
468 |
'success' => true,
|
|
|
469 |
'data' => 'LABEL_RECORD_UPDATED'
|
|
|
470 |
];
|
|
|
471 |
} else {
|
|
|
472 |
$data = [
|
|
|
473 |
'success' => false,
|
|
|
474 |
'data' => $knowledgeAreaCategoryUserMapper->getError()
|
|
|
475 |
];
|
|
|
476 |
|
|
|
477 |
}
|
|
|
478 |
|
|
|
479 |
return new JsonModel($data);
|
|
|
480 |
} else {
|
|
|
481 |
$messages = [];
|
|
|
482 |
$form_messages = (array) $form->getMessages();
|
|
|
483 |
foreach ($form_messages as $fieldname => $field_messages) {
|
|
|
484 |
$messages[$fieldname] = array_values($field_messages);
|
|
|
485 |
}
|
|
|
486 |
|
|
|
487 |
return new JsonModel([
|
|
|
488 |
'success' => false,
|
|
|
489 |
'data' => $messages
|
|
|
490 |
]);
|
|
|
491 |
}
|
|
|
492 |
} else if ($request->isGet()) {
|
|
|
493 |
|
|
|
494 |
|
|
|
495 |
$data = [
|
|
|
496 |
'success' => true,
|
|
|
497 |
'data' => [
|
|
|
498 |
'user_id' => $user->uuid,
|
|
|
499 |
'role' => $knowledgeAreaCategoryUser->role,
|
|
|
500 |
]
|
|
|
501 |
];
|
|
|
502 |
|
|
|
503 |
return new JsonModel($data);
|
|
|
504 |
}
|
|
|
505 |
|
|
|
506 |
|
|
|
507 |
$data = [
|
|
|
508 |
'success' => false,
|
|
|
509 |
'data' => 'ERROR_METHOD_NOT_ALLOWED'
|
|
|
510 |
];
|
|
|
511 |
|
|
|
512 |
return new JsonModel($data);
|
|
|
513 |
|
|
|
514 |
}
|
|
|
515 |
|
|
|
516 |
public function deleteAction()
|
|
|
517 |
{
|
|
|
518 |
$request = $this->getRequest();
|
|
|
519 |
$currentUserPlugin = $this->plugin('currentUserPlugin');
|
|
|
520 |
$currentCompany = $currentUserPlugin->getCompany();
|
|
|
521 |
$currentUser = $currentUserPlugin->getUser();
|
|
|
522 |
|
|
|
523 |
$request = $this->getRequest();
|
|
|
524 |
$uuid = $this->params()->fromRoute('id');
|
|
|
525 |
$user_id = $this->params()->fromRoute('user_id');
|
|
|
526 |
|
|
|
527 |
|
|
|
528 |
$knowledgeAreaCategoryMapper = KnowledgeAreaCategoryMapper::getInstance($this->adapter);
|
|
|
529 |
$knowledgeAreaCategory = $knowledgeAreaCategoryMapper->fetchOneByUuid($uuid);
|
|
|
530 |
|
|
|
531 |
if(!$knowledgeAreaCategory) {
|
|
|
532 |
return new JsonModel([
|
|
|
533 |
'success' => false,
|
|
|
534 |
'data' => 'ERROR_KNOWLEDGE_AREA_CATEGORY_NOT_FOUND'
|
|
|
535 |
]);
|
|
|
536 |
}
|
|
|
537 |
|
|
|
538 |
if($knowledgeAreaCategory->company_id != $currentCompany->id) {
|
|
|
539 |
return new JsonModel([
|
|
|
540 |
'success' => false,
|
|
|
541 |
'data' => 'ERROR_KNOWLEDGE_AREA_CATEGORY_IS_OTHER_COMPANY'
|
|
|
542 |
]);
|
|
|
543 |
}
|
|
|
544 |
|
|
|
545 |
$userMapper = UserMapper::getInstance($this->adapter);
|
|
|
546 |
$user = $userMapper->fetchOneByUuid($user_id);
|
|
|
547 |
|
|
|
548 |
if(!$user) {
|
|
|
549 |
return new JsonModel([
|
|
|
550 |
'success' => false,
|
|
|
551 |
'data' => 'ERROR_USER_NOT_FOUND'
|
|
|
552 |
]);
|
|
|
553 |
|
|
|
554 |
}
|
|
|
555 |
|
|
|
556 |
$knowledgeAreaCategoryUserMapper = KnowledgeAreaCategoryUserMapper::getInstance($this->adapter);
|
|
|
557 |
$knowledgeAreaCategoryUser = $knowledgeAreaCategoryUserMapper->fetchOneByCategoryIdAndUserId($knowledgeAreaCategory->id, $user->id);
|
|
|
558 |
|
|
|
559 |
if(!$knowledgeAreaCategoryUser) {
|
|
|
560 |
return new JsonModel([
|
|
|
561 |
'success' => false,
|
|
|
562 |
'data' => 'ERROR_KNOWLEDGE_AREA_CATEGORY_USER_NOT_FOUND'
|
|
|
563 |
]);
|
|
|
564 |
}
|
|
|
565 |
|
|
|
566 |
|
|
|
567 |
|
|
|
568 |
if ($request->isPost()) {
|
|
|
569 |
|
|
|
570 |
if ($knowledgeAreaCategoryUserMapper->deleteOneByCategoryIdAndUserId($knowledgeAreaCategory->id, $user->id)) {
|
|
|
571 |
$this->logger->info('Se borro el usuario ' . $user->first_name . ' ' . $user->last_name . ' (' . $user->email . ') la categoria ' . $knowledgeAreaCategory->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
|
|
|
572 |
|
|
|
573 |
$data = [
|
|
|
574 |
'success' => true,
|
|
|
575 |
'data' => 'LABEL_RECORD_DELETED'
|
|
|
576 |
];
|
|
|
577 |
} else {
|
|
|
578 |
|
|
|
579 |
$data = [
|
|
|
580 |
'success' => false,
|
|
|
581 |
'data' => $knowledgeAreaCategoryUserMapper->getError()
|
|
|
582 |
];
|
|
|
583 |
|
|
|
584 |
|
|
|
585 |
}
|
|
|
586 |
} else {
|
|
|
587 |
$data = [
|
|
|
588 |
'success' => false,
|
|
|
589 |
'data' => 'ERROR_METHOD_NOT_ALLOWED'
|
|
|
590 |
];
|
|
|
591 |
|
|
|
592 |
|
|
|
593 |
}
|
|
|
594 |
|
|
|
595 |
|
|
|
596 |
return new JsonModel($data);
|
|
|
597 |
|
|
|
598 |
}
|
|
|
599 |
}
|