1 |
efrain |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/**
|
|
|
4 |
*
|
|
|
5 |
* Controlador: Mis Perfiles
|
|
|
6 |
*
|
|
|
7 |
*/
|
|
|
8 |
|
|
|
9 |
declare(strict_types=1);
|
|
|
10 |
|
|
|
11 |
namespace LeadersLinked\Controller;
|
|
|
12 |
|
|
|
13 |
use Laminas\Db\Adapter\AdapterInterface;
|
|
|
14 |
|
|
|
15 |
use Laminas\Mvc\Controller\AbstractActionController;
|
|
|
16 |
use Laminas\Log\LoggerInterface;
|
|
|
17 |
use Laminas\View\Model\ViewModel;
|
|
|
18 |
use Laminas\View\Model\JsonModel;
|
|
|
19 |
use LeadersLinked\Library\Functions;
|
|
|
20 |
use LeadersLinked\Mapper\KnowledgeAreaCategoryMapper;
|
|
|
21 |
use LeadersLinked\Mapper\KnowledgeAreaCategoryUserMapper;
|
|
|
22 |
use LeadersLinked\Mapper\UserMapper;
|
|
|
23 |
use LeadersLinked\Model\KnowledgeAreaCategory;
|
|
|
24 |
use LeadersLinked\Model\KnowledgeAreaCategoryUser;
|
|
|
25 |
use LeadersLinked\Form\KnowledgeArea\KnowledgeAreaCreateForm;
|
|
|
26 |
use LeadersLinked\Mapper\KnowledgeAreaContentMapper;
|
|
|
27 |
use LeadersLinked\Model\KnowledgeAreaContent;
|
|
|
28 |
use LeadersLinked\Form\KnowledgeArea\KnowledgeAreaEditForm;
|
|
|
29 |
use LeadersLinked\Mapper\CommentMapper;
|
|
|
30 |
use LeadersLinked\Form\KnowledgeArea\CommentForm;
|
|
|
31 |
use LeadersLinked\Model\Comment;
|
|
|
32 |
use LeadersLinked\Mapper\ContentReactionMapper;
|
|
|
33 |
use LeadersLinked\Model\ContentReaction;
|
|
|
34 |
use LeadersLinked\Mapper\KnowledgeAreaCategoryJobDescriptionMapper;
|
|
|
35 |
use LeadersLinked\Mapper\OrganizationPositionMapper;
|
242 |
efrain |
36 |
use LeadersLinked\Model\User;
|
283 |
www |
37 |
use LeadersLinked\Library\Storage;
|
1 |
efrain |
38 |
|
|
|
39 |
class KnowledgeAreaController extends AbstractActionController
|
|
|
40 |
{
|
|
|
41 |
/**
|
|
|
42 |
*
|
|
|
43 |
* @var \Laminas\Db\Adapter\AdapterInterface
|
|
|
44 |
*/
|
|
|
45 |
private $adapter;
|
|
|
46 |
|
|
|
47 |
/**
|
|
|
48 |
*
|
|
|
49 |
* @var \LeadersLinked\Cache\CacheInterface
|
|
|
50 |
*/
|
|
|
51 |
private $cache;
|
|
|
52 |
|
|
|
53 |
|
|
|
54 |
/**
|
|
|
55 |
*
|
|
|
56 |
* @var \Laminas\Log\LoggerInterface
|
|
|
57 |
*/
|
|
|
58 |
private $logger;
|
|
|
59 |
|
|
|
60 |
/**
|
|
|
61 |
*
|
|
|
62 |
* @var array
|
|
|
63 |
*/
|
|
|
64 |
private $config;
|
|
|
65 |
|
|
|
66 |
|
|
|
67 |
/**
|
|
|
68 |
*
|
|
|
69 |
* @var \Laminas\Mvc\I18n\Translator
|
|
|
70 |
*/
|
|
|
71 |
private $translator;
|
|
|
72 |
|
|
|
73 |
|
|
|
74 |
/**
|
|
|
75 |
*
|
|
|
76 |
* @param \Laminas\Db\Adapter\AdapterInterface $adapter
|
|
|
77 |
* @param \LeadersLinked\Cache\CacheInterface $cache
|
|
|
78 |
* @param \Laminas\Log\LoggerInterface LoggerInterface $logger
|
|
|
79 |
* @param array $config
|
|
|
80 |
* @param \Laminas\Mvc\I18n\Translator $translator
|
|
|
81 |
*/
|
|
|
82 |
public function __construct($adapter, $cache, $logger, $config, $translator)
|
|
|
83 |
{
|
|
|
84 |
$this->adapter = $adapter;
|
|
|
85 |
$this->cache = $cache;
|
|
|
86 |
$this->logger = $logger;
|
|
|
87 |
$this->config = $config;
|
|
|
88 |
$this->translator = $translator;
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
/**
|
|
|
92 |
*
|
562 |
stevensc |
93 |
* Handles the display of the knowledge area content listing.
|
|
|
94 |
* Fetches knowledge areas based on user permissions, network settings, and filters.
|
|
|
95 |
* Returns a JSON response suitable for a data table or list view.
|
1 |
efrain |
96 |
* {@inheritDoc}
|
|
|
97 |
* @see \Laminas\Mvc\Controller\AbstractActionController::indexAction()
|
|
|
98 |
*/
|
|
|
99 |
public function indexAction()
|
|
|
100 |
{
|
721 |
stevensc |
101 |
$request = $this->getRequest();
|
|
|
102 |
if (!$request->isGet()) {
|
|
|
103 |
return new JsonModel([
|
|
|
104 |
'success' => false,
|
|
|
105 |
'data' => 'ERROR_METHOD_NOT_ALLOWED'
|
|
|
106 |
]);
|
|
|
107 |
}
|
|
|
108 |
|
562 |
stevensc |
109 |
// Get current user and network information from plugins.
|
1 |
efrain |
110 |
$currentUserPlugin = $this->plugin('currentUserPlugin');
|
|
|
111 |
$currentUser = $currentUserPlugin->getUser();
|
|
|
112 |
|
|
|
113 |
$currentNetworkPlugin = $this->plugin('currentNetworkPlugin');
|
|
|
114 |
$currentNetwork = $currentNetworkPlugin->getNetwork();
|
|
|
115 |
|
721 |
stevensc |
116 |
// --- Query Parameters ---
|
|
|
117 |
$category_filter_id = Functions::sanitizeFilterString($this->params()->fromQuery('category_id'));
|
|
|
118 |
$search = Functions::sanitizeFilterString($this->params()->fromQuery('search'));
|
|
|
119 |
$page = intval($this->params()->fromQuery('start', 1), 10);
|
|
|
120 |
$order_field = 'added_on'; // Default order field
|
|
|
121 |
$order_direction = 'asc'; // Default order direction
|
|
|
122 |
$records_x_page = 12; // Default records per page
|
|
|
123 |
|
|
|
124 |
// --- Get accessible categories and filter by category if needed ---
|
|
|
125 |
$categoryData = $this->getAccessibleCategories($currentUser, $currentNetwork);
|
|
|
126 |
$category_ids_to_filter = $this->filterCategoriesByRequest($categoryData, $category_filter_id);
|
|
|
127 |
|
|
|
128 |
// --- Content Fetching ---
|
|
|
129 |
$storage = Storage::getInstance($this->config, $this->adapter);
|
|
|
130 |
$path = $storage->getPathKnowledgeArea();
|
|
|
131 |
|
|
|
132 |
$knowledgeAreaContentMapper = KnowledgeAreaContentMapper::getInstance($this->adapter);
|
|
|
133 |
$paginator = $knowledgeAreaContentMapper->fetchAllDataTableByCategoryIds(
|
|
|
134 |
$category_ids_to_filter,
|
|
|
135 |
$search,
|
|
|
136 |
$page,
|
|
|
137 |
$records_x_page,
|
|
|
138 |
$order_field,
|
|
|
139 |
$order_direction
|
|
|
140 |
);
|
|
|
141 |
|
|
|
142 |
// --- Response Item Construction ---
|
|
|
143 |
$items = $this->buildContentItems($paginator, $categoryData, $storage, $path);
|
|
|
144 |
|
|
|
145 |
// --- JSON Response ---
|
|
|
146 |
$image_size = $this->config['leaderslinked.image_sizes.knowledge_area'];
|
|
|
147 |
|
|
|
148 |
return new JsonModel([
|
|
|
149 |
'success' => true,
|
|
|
150 |
'data' => [
|
|
|
151 |
'current' => [
|
|
|
152 |
'items' => $items,
|
|
|
153 |
'page' => $paginator->getCurrentPageNumber(),
|
|
|
154 |
'count' => $paginator->getCurrentItemCount(),
|
|
|
155 |
],
|
|
|
156 |
'total' => [
|
|
|
157 |
'count' => $paginator->getTotalItemCount(),
|
|
|
158 |
'pages' => $paginator->getPages()->pageCount,
|
|
|
159 |
],
|
|
|
160 |
'categories' => array_values($categoryData['categories']),
|
|
|
161 |
'categories_with_edition' => $categoryData['categories_with_edition_uuids'],
|
|
|
162 |
'link_add' => $categoryData['allow_add'] ? $this->url()->fromRoute('knowledge-area/add') : '',
|
|
|
163 |
'image_size' => $image_size,
|
|
|
164 |
'content_edit' => count($categoryData['category_with_edition_ids']) > 0,
|
|
|
165 |
]
|
|
|
166 |
]);
|
|
|
167 |
}
|
|
|
168 |
|
|
|
169 |
/**
|
|
|
170 |
* Get metadata for knowledge area (categories, permissions, etc.)
|
|
|
171 |
* This endpoint can be called separately to get UI configuration data
|
|
|
172 |
*/
|
|
|
173 |
public function metadataAction()
|
|
|
174 |
{
|
1 |
efrain |
175 |
$request = $this->getRequest();
|
721 |
stevensc |
176 |
if (!$request->isGet()) {
|
|
|
177 |
return new JsonModel([
|
|
|
178 |
'success' => false,
|
|
|
179 |
'data' => 'ERROR_METHOD_NOT_ALLOWED'
|
|
|
180 |
]);
|
|
|
181 |
}
|
1 |
efrain |
182 |
|
721 |
stevensc |
183 |
$currentUserPlugin = $this->plugin('currentUserPlugin');
|
|
|
184 |
$currentUser = $currentUserPlugin->getUser();
|
1 |
efrain |
185 |
|
721 |
stevensc |
186 |
$currentNetworkPlugin = $this->plugin('currentNetworkPlugin');
|
|
|
187 |
$currentNetwork = $currentNetworkPlugin->getNetwork();
|
1 |
efrain |
188 |
|
721 |
stevensc |
189 |
$categoryData = $this->getAccessibleCategories($currentUser, $currentNetwork);
|
|
|
190 |
$image_size = $this->config['leaderslinked.image_sizes.knowledge_area'];
|
1 |
efrain |
191 |
|
721 |
stevensc |
192 |
return new JsonModel([
|
|
|
193 |
'success' => true,
|
|
|
194 |
'data' => [
|
|
|
195 |
'categories' => array_values($categoryData['categories']),
|
|
|
196 |
'categories_with_edition' => $categoryData['categories_with_edition_uuids'],
|
|
|
197 |
'link_add' => $categoryData['allow_add'] ? $this->url()->fromRoute('knowledge-area/add') : '',
|
|
|
198 |
'image_size' => $image_size,
|
|
|
199 |
'content_edit' => count($categoryData['category_with_edition_ids']) > 0,
|
|
|
200 |
]
|
|
|
201 |
]);
|
|
|
202 |
}
|
1 |
efrain |
203 |
|
721 |
stevensc |
204 |
/**
|
|
|
205 |
* Get only the content listing without metadata
|
|
|
206 |
* This endpoint can be called for pagination/filtering without reloading metadata
|
|
|
207 |
*/
|
|
|
208 |
public function listAction()
|
|
|
209 |
{
|
|
|
210 |
$request = $this->getRequest();
|
|
|
211 |
if (!$request->isGet()) {
|
|
|
212 |
return new JsonModel([
|
|
|
213 |
'success' => false,
|
|
|
214 |
'data' => 'ERROR_METHOD_NOT_ALLOWED'
|
|
|
215 |
]);
|
|
|
216 |
}
|
|
|
217 |
|
|
|
218 |
$currentUserPlugin = $this->plugin('currentUserPlugin');
|
|
|
219 |
$currentUser = $currentUserPlugin->getUser();
|
|
|
220 |
|
|
|
221 |
$currentNetworkPlugin = $this->plugin('currentNetworkPlugin');
|
|
|
222 |
$currentNetwork = $currentNetworkPlugin->getNetwork();
|
|
|
223 |
|
|
|
224 |
// --- Query Parameters ---
|
|
|
225 |
$category_filter_id = Functions::sanitizeFilterString($this->params()->fromQuery('category_id'));
|
|
|
226 |
$search = Functions::sanitizeFilterString($this->params()->fromQuery('search'));
|
|
|
227 |
$page = intval($this->params()->fromQuery('start', 1), 10);
|
|
|
228 |
$order_field = 'added_on';
|
|
|
229 |
$order_direction = 'asc';
|
|
|
230 |
$records_x_page = 12;
|
|
|
231 |
|
|
|
232 |
// --- Get accessible categories and filter ---
|
|
|
233 |
$categoryData = $this->getAccessibleCategories($currentUser, $currentNetwork);
|
|
|
234 |
$category_ids_to_filter = $this->filterCategoriesByRequest($categoryData, $category_filter_id);
|
|
|
235 |
|
|
|
236 |
// --- Content Fetching ---
|
|
|
237 |
$storage = Storage::getInstance($this->config, $this->adapter);
|
|
|
238 |
$path = $storage->getPathKnowledgeArea();
|
|
|
239 |
|
|
|
240 |
$knowledgeAreaContentMapper = KnowledgeAreaContentMapper::getInstance($this->adapter);
|
|
|
241 |
$paginator = $knowledgeAreaContentMapper->fetchAllDataTableByCategoryIds(
|
|
|
242 |
$category_ids_to_filter,
|
|
|
243 |
$search,
|
|
|
244 |
$page,
|
|
|
245 |
$records_x_page,
|
|
|
246 |
$order_field,
|
|
|
247 |
$order_direction
|
|
|
248 |
);
|
|
|
249 |
|
|
|
250 |
// --- Response Item Construction ---
|
|
|
251 |
$items = $this->buildContentItems($paginator, $categoryData, $storage, $path);
|
|
|
252 |
|
|
|
253 |
return new JsonModel([
|
|
|
254 |
'success' => true,
|
|
|
255 |
'data' => [
|
|
|
256 |
'current' => [
|
|
|
257 |
'items' => $items,
|
|
|
258 |
'page' => $paginator->getCurrentPageNumber(),
|
|
|
259 |
'count' => $paginator->getCurrentItemCount(),
|
|
|
260 |
],
|
|
|
261 |
'total' => [
|
|
|
262 |
'count' => $paginator->getTotalItemCount(),
|
|
|
263 |
'pages' => $paginator->getPages()->pageCount,
|
|
|
264 |
],
|
|
|
265 |
]
|
|
|
266 |
]);
|
|
|
267 |
}
|
|
|
268 |
|
|
|
269 |
/**
|
|
|
270 |
* Get accessible categories for the current user and network
|
|
|
271 |
*
|
|
|
272 |
* @param object $currentUser
|
|
|
273 |
* @param object $currentNetwork
|
|
|
274 |
* @return array
|
|
|
275 |
*/
|
|
|
276 |
private function getAccessibleCategories($currentUser, $currentNetwork)
|
|
|
277 |
{
|
|
|
278 |
// --- ACL Permissions ---
|
|
|
279 |
$acl = $this->getEvent()->getViewModel()->getVariable('acl');
|
|
|
280 |
$allowAdd = $acl->isAllowed($currentUser->usertype_id, 'knowledge-area/add') ? 1 : 0;
|
|
|
281 |
$allowEdit = $acl->isAllowed($currentUser->usertype_id, 'knowledge-area/edit');
|
|
|
282 |
$allowDelete = $acl->isAllowed($currentUser->usertype_id, 'knowledge-area/delete');
|
|
|
283 |
$allowView = $acl->isAllowed($currentUser->usertype_id, 'knowledge-area/view');
|
|
|
284 |
|
|
|
285 |
// --- Category Fetching and Processing ---
|
|
|
286 |
$category_with_edition_ids = [];
|
|
|
287 |
$category_with_edition_uuids = [];
|
|
|
288 |
$user_accessible_category_ids = [];
|
|
|
289 |
$categories = [];
|
|
|
290 |
|
|
|
291 |
// Instantiate mappers.
|
|
|
292 |
$knowledgeAreaCategoryMapper = KnowledgeAreaCategoryMapper::getInstance($this->adapter);
|
|
|
293 |
$knowledgeAreaCategoryUserMapper = KnowledgeAreaCategoryUserMapper::getInstance($this->adapter);
|
|
|
294 |
|
|
|
295 |
// 1. Fetch categories based on user's direct permissions
|
|
|
296 |
$userCategoryRecords = $knowledgeAreaCategoryUserMapper->fetchAllByUserId($currentUser->id);
|
|
|
297 |
foreach ($userCategoryRecords as $record) {
|
|
|
298 |
if ($record->role == KnowledgeAreaCategoryUser::ROLE_ADMINISTRATOR || $record->role == KnowledgeAreaCategoryUser::ROLE_USER) {
|
|
|
299 |
array_push($category_with_edition_ids, $record->category_id);
|
|
|
300 |
$categoryDetails = $knowledgeAreaCategoryMapper->fetchOne($record->category_id);
|
|
|
301 |
if($categoryDetails) {
|
|
|
302 |
array_push($category_with_edition_uuids, $categoryDetails->uuid);
|
1 |
efrain |
303 |
}
|
562 |
stevensc |
304 |
}
|
721 |
stevensc |
305 |
array_push($user_accessible_category_ids, $record->category_id);
|
|
|
306 |
}
|
1 |
efrain |
307 |
|
721 |
stevensc |
308 |
// 2. Fetch details for all directly accessible categories
|
|
|
309 |
if (!empty($user_accessible_category_ids)) {
|
|
|
310 |
$detailedUserCategories = $knowledgeAreaCategoryMapper->fetchAllByIds($user_accessible_category_ids);
|
|
|
311 |
foreach ($detailedUserCategories as $record) {
|
|
|
312 |
if (!isset($categories[$record->id])) {
|
562 |
stevensc |
313 |
$categories[$record->id] = [
|
|
|
314 |
'uuid' => $record->uuid,
|
|
|
315 |
'name' => $record->name,
|
|
|
316 |
];
|
|
|
317 |
}
|
|
|
318 |
}
|
721 |
stevensc |
319 |
}
|
562 |
stevensc |
320 |
|
721 |
stevensc |
321 |
// 3. Fetch all public categories within the current network
|
|
|
322 |
$publicCategories = $knowledgeAreaCategoryMapper->fetchAllPublicByNetworkId($currentNetwork->id);
|
|
|
323 |
foreach ($publicCategories as $record) {
|
|
|
324 |
if (!isset($categories[$record->id])) {
|
|
|
325 |
$categories[$record->id] = [
|
|
|
326 |
'uuid' => $record->uuid,
|
|
|
327 |
'name' => $record->name,
|
|
|
328 |
];
|
|
|
329 |
}
|
|
|
330 |
}
|
|
|
331 |
|
|
|
332 |
// Sort all collected categories alphabetically by name
|
|
|
333 |
uasort($categories, function ($a, $b) {
|
|
|
334 |
return $a['name'] <=> $b['name'];
|
|
|
335 |
});
|
1 |
efrain |
336 |
|
721 |
stevensc |
337 |
return [
|
|
|
338 |
'categories' => $categories,
|
|
|
339 |
'category_with_edition_ids' => $category_with_edition_ids,
|
|
|
340 |
'categories_with_edition_uuids' => $category_with_edition_uuids,
|
|
|
341 |
'allow_add' => $allowAdd,
|
|
|
342 |
'allow_edit' => $allowEdit,
|
|
|
343 |
'allow_delete' => $allowDelete,
|
|
|
344 |
'allow_view' => $allowView,
|
|
|
345 |
];
|
|
|
346 |
}
|
|
|
347 |
|
|
|
348 |
/**
|
|
|
349 |
* Filter categories based on request parameter
|
|
|
350 |
*
|
|
|
351 |
* @param array $categoryData
|
|
|
352 |
* @param string $category_filter_id
|
|
|
353 |
* @return array
|
|
|
354 |
*/
|
|
|
355 |
private function filterCategoriesByRequest($categoryData, $category_filter_id)
|
|
|
356 |
{
|
|
|
357 |
$category_ids_to_filter = array_keys($categoryData['categories']);
|
|
|
358 |
|
|
|
359 |
if ($category_filter_id) {
|
|
|
360 |
$knowledgeAreaCategoryMapper = KnowledgeAreaCategoryMapper::getInstance($this->adapter);
|
|
|
361 |
$categoryFilter = $knowledgeAreaCategoryMapper->fetchOneByUuid($category_filter_id);
|
|
|
362 |
if ($categoryFilter && isset($categoryData['categories'][$categoryFilter->id])) {
|
|
|
363 |
$category_ids_to_filter = [$categoryFilter->id];
|
|
|
364 |
} else {
|
|
|
365 |
$category_ids_to_filter = [];
|
|
|
366 |
}
|
|
|
367 |
}
|
|
|
368 |
|
|
|
369 |
return $category_ids_to_filter;
|
|
|
370 |
}
|
|
|
371 |
|
|
|
372 |
/**
|
|
|
373 |
* Build content items for response
|
|
|
374 |
*
|
|
|
375 |
* @param object $paginator
|
|
|
376 |
* @param array $categoryData
|
|
|
377 |
* @param object $storage
|
|
|
378 |
* @param string $path
|
|
|
379 |
* @return array
|
|
|
380 |
*/
|
|
|
381 |
private function buildContentItems($paginator, $categoryData, $storage, $path)
|
|
|
382 |
{
|
|
|
383 |
$items = [];
|
|
|
384 |
$contentRecords = $paginator->getCurrentItems();
|
|
|
385 |
$knowledgeAreaCategoryMapper = KnowledgeAreaCategoryMapper::getInstance($this->adapter);
|
|
|
386 |
|
|
|
387 |
foreach ($contentRecords as $record) {
|
|
|
388 |
// Ensure category details are available for the current content item
|
|
|
389 |
if (!isset($categoryData['categories'][$record->category_id])) {
|
|
|
390 |
$category = $knowledgeAreaCategoryMapper->fetchOne($record->category_id);
|
|
|
391 |
if ($category) {
|
|
|
392 |
$categoryData['categories'][$category->id] = [
|
|
|
393 |
'uuid' => $category->uuid,
|
|
|
394 |
'name' => $category->name,
|
|
|
395 |
];
|
562 |
stevensc |
396 |
} else {
|
721 |
stevensc |
397 |
$categoryData['categories'][$record->category_id] = ['uuid' => null, 'name' => 'Unknown Category'];
|
562 |
stevensc |
398 |
}
|
|
|
399 |
}
|
|
|
400 |
|
721 |
stevensc |
401 |
// Strip HTML tags and truncate description for display
|
|
|
402 |
$description = strip_tags($record->description);
|
|
|
403 |
if (strlen($description) > 120) {
|
|
|
404 |
$description = substr($description, 0, 120) . '...';
|
|
|
405 |
}
|
1 |
efrain |
406 |
|
721 |
stevensc |
407 |
// Construct the item for the JSON response
|
|
|
408 |
$item = [
|
724 |
stevensc |
409 |
'image' => $storage->getGenericImage($path, $record->uuid, $record->image),
|
721 |
stevensc |
410 |
'title' => $record->title,
|
|
|
411 |
'description' => $description,
|
|
|
412 |
'category' => $categoryData['categories'][$record->category_id]['name'],
|
|
|
413 |
'link_view' => $categoryData['allow_view'] ? $this->url()->fromRoute('knowledge-area/view', ['id' => $record->uuid]) : '',
|
|
|
414 |
];
|
1 |
efrain |
415 |
|
721 |
stevensc |
416 |
// Add edit/delete links if user has permission for the content's category
|
|
|
417 |
if (in_array($record->category_id, $categoryData['category_with_edition_ids'])) {
|
|
|
418 |
$item['link_edit'] = $categoryData['allow_edit'] ? $this->url()->fromRoute('knowledge-area/edit', ['id' => $record->uuid]) : '';
|
|
|
419 |
$item['link_delete'] = $categoryData['allow_delete'] ? $this->url()->fromRoute('knowledge-area/delete', ['id' => $record->uuid]) : '';
|
562 |
stevensc |
420 |
}
|
721 |
stevensc |
421 |
array_push($items, $item);
|
1 |
efrain |
422 |
}
|
721 |
stevensc |
423 |
|
|
|
424 |
return $items;
|
1 |
efrain |
425 |
}
|
|
|
426 |
|
|
|
427 |
public function addAction()
|
|
|
428 |
{
|
|
|
429 |
$currentUserPlugin = $this->plugin('currentUserPlugin');
|
|
|
430 |
$currentUser = $currentUserPlugin->getUser();
|
|
|
431 |
|
726 |
stevensc |
432 |
$currentNetworkPlugin = $this->plugin('currentNetworkPlugin');
|
|
|
433 |
$currentNetwork = $currentNetworkPlugin->getNetwork();
|
|
|
434 |
|
1 |
efrain |
435 |
$request = $this->getRequest();
|
|
|
436 |
|
|
|
437 |
if ($request->isPost()) {
|
|
|
438 |
$category_with_edition_ids = [];
|
|
|
439 |
$category_ids = [];
|
|
|
440 |
|
|
|
441 |
|
|
|
442 |
$categories = [];
|
|
|
443 |
|
|
|
444 |
|
|
|
445 |
$knowledgeAreaCategoryUserMapper = KnowledgeAreaCategoryUserMapper::getInstance($this->adapter);
|
|
|
446 |
$records = $knowledgeAreaCategoryUserMapper->fetchAllByUserId($currentUser->id);
|
|
|
447 |
foreach ($records as $record) {
|
|
|
448 |
if ($record->role == KnowledgeAreaCategoryUser::ROLE_ADMINISTRATOR || $record->role == KnowledgeAreaCategoryUser::ROLE_USER) {
|
|
|
449 |
|
|
|
450 |
array_push($category_with_edition_ids, $record->category_id);
|
|
|
451 |
}
|
|
|
452 |
|
|
|
453 |
array_push($category_ids, $record->category_id);
|
|
|
454 |
}
|
|
|
455 |
|
|
|
456 |
$knowledgeAreaCategoryMapper = KnowledgeAreaCategoryMapper::getInstance($this->adapter);
|
|
|
457 |
|
|
|
458 |
if ($category_ids) {
|
|
|
459 |
$records = $knowledgeAreaCategoryMapper->fetchAllByIds($category_ids);
|
|
|
460 |
foreach ($records as $record) {
|
|
|
461 |
if ($record->status == KnowledgeAreaCategory::STATUS_ACTIVE) {
|
|
|
462 |
|
|
|
463 |
$categories[$record->id] = [
|
|
|
464 |
'uuid' => $record->uuid,
|
|
|
465 |
'name' => $record->name,
|
|
|
466 |
];
|
|
|
467 |
}
|
|
|
468 |
}
|
|
|
469 |
}
|
|
|
470 |
|
|
|
471 |
|
|
|
472 |
|
|
|
473 |
$categories = array_values($categories);
|
|
|
474 |
usort($categories, function ($a, $b) {
|
|
|
475 |
return $a['name'] <=> $b['name'];
|
|
|
476 |
});
|
|
|
477 |
|
|
|
478 |
|
|
|
479 |
$dataPost = array_merge($request->getPost()->toArray(), $request->getFiles()->toArray());
|
|
|
480 |
$form = new KnowledgeAreaCreateForm($this->adapter, $category_with_edition_ids);
|
|
|
481 |
|
|
|
482 |
$form->setData($dataPost);
|
|
|
483 |
|
|
|
484 |
if ($form->isValid()) {
|
|
|
485 |
$dataPost = (array) $form->getData();
|
|
|
486 |
|
|
|
487 |
$knowledgeAreaCategoryMapper = KnowledgeAreaCategoryMapper::getInstance($this->adapter);
|
|
|
488 |
$knowledgeAreaCategory = $knowledgeAreaCategoryMapper->fetchOneByUuid($dataPost['category_id']);
|
|
|
489 |
|
|
|
490 |
|
|
|
491 |
$knowledgeAreaContent = new KnowledgeAreaContent();
|
|
|
492 |
$knowledgeAreaContent->network_id = $knowledgeAreaCategory->network_id;
|
|
|
493 |
$knowledgeAreaContent->company_id = $knowledgeAreaCategory->company_id;
|
|
|
494 |
$knowledgeAreaContent->category_id = $knowledgeAreaCategory->id;
|
|
|
495 |
$knowledgeAreaContent->user_id = $currentUser->id;
|
|
|
496 |
$knowledgeAreaContent->title = $dataPost['title'];
|
|
|
497 |
$knowledgeAreaContent->description = $dataPost['description'];
|
|
|
498 |
$knowledgeAreaContent->link = $dataPost['link'];
|
|
|
499 |
|
|
|
500 |
$knowledgeAreaContentMapper = KnowledgeAreaContentMapper::getInstance($this->adapter);
|
|
|
501 |
if ($knowledgeAreaContentMapper->insert($knowledgeAreaContent)) {
|
|
|
502 |
$knowledgeAreaContent = $knowledgeAreaContentMapper->fetchOne($knowledgeAreaContent->id);
|
|
|
503 |
|
561 |
stevensc |
504 |
// Se instancia la clase Storage para manejar el almacenamiento de archivos.
|
|
|
505 |
// La clase Image fue eliminada debido a que no se encontraba o estaba obsoleta.
|
334 |
www |
506 |
$storage = Storage::getInstance($this->config, $this->adapter);
|
561 |
stevensc |
507 |
// Obtiene la ruta base para los archivos del área de conocimiento.
|
|
|
508 |
// $target_path = $storage->getPathKnowledgeArea(); // No longer needed directly here, composePathTo methods use storagePath internally
|
1 |
efrain |
509 |
|
721 |
stevensc |
510 |
$storage->setFiles($request->getFiles()->toArray());
|
1 |
efrain |
511 |
|
721 |
stevensc |
512 |
if($storage->setCurrentFilename('image')) {
|
727 |
stevensc |
513 |
$image_tmp_filename = $storage->getTmpFilename();
|
|
|
514 |
$image_filename = 'knowledge_image_' . $knowledgeAreaContent->uuid . '.' . $storage->getExtension();
|
|
|
515 |
$image_target_filename = $storage->composePathToFilename(Storage::TYPE_KNOWLEDGE_AREA, $knowledgeAreaContent->uuid, $image_filename);
|
721 |
stevensc |
516 |
|
561 |
stevensc |
517 |
list($target_width_str, $target_height_str) = explode('x', $this->config['leaderslinked.image_sizes.knowledge_area']);
|
|
|
518 |
$target_width = (int)$target_width_str;
|
|
|
519 |
$target_height = (int)$target_height_str;
|
|
|
520 |
|
727 |
stevensc |
521 |
if (!$storage->uploadImageResize($image_tmp_filename, $image_target_filename, $target_width, $target_height)) {
|
721 |
stevensc |
522 |
return new JsonModel([
|
|
|
523 |
'success' => false,
|
|
|
524 |
'data' => 'ERROR_IMAGE_UPLOAD'
|
|
|
525 |
]);
|
560 |
stevensc |
526 |
}
|
334 |
www |
527 |
|
727 |
stevensc |
528 |
$knowledgeAreaContent->image = $image_filename;
|
1 |
efrain |
529 |
}
|
|
|
530 |
|
721 |
stevensc |
531 |
if($storage->setCurrentFilename('attachment')) {
|
727 |
stevensc |
532 |
$attachment_tmp_filename = $storage->getTmpFilename();
|
|
|
533 |
$attachment_filename = 'knowledge_attachment_' . $knowledgeAreaContent->uuid . '.' . $storage->getExtension();
|
|
|
534 |
$attachment_target_filename = $storage->composePathToFilename(Storage::TYPE_KNOWLEDGE_AREA, $knowledgeAreaContent->uuid, $attachment_filename);
|
721 |
stevensc |
535 |
|
727 |
stevensc |
536 |
if (!$storage->moveUploadedFile($attachment_tmp_filename, $attachment_target_filename)) {
|
721 |
stevensc |
537 |
return new JsonModel([
|
|
|
538 |
'success' => false,
|
|
|
539 |
'data' => 'ERROR_IMAGE_UPLOAD'
|
|
|
540 |
]);
|
560 |
stevensc |
541 |
}
|
|
|
542 |
|
727 |
stevensc |
543 |
$knowledgeAreaContent->attachment = $attachment_filename;
|
1 |
efrain |
544 |
}
|
|
|
545 |
|
721 |
stevensc |
546 |
if(!$knowledgeAreaContentMapper->update($knowledgeAreaContent)) {
|
|
|
547 |
return new JsonModel([
|
|
|
548 |
'success' => false,
|
|
|
549 |
'data' => $knowledgeAreaContentMapper->getError()
|
|
|
550 |
]);
|
|
|
551 |
}
|
|
|
552 |
|
1 |
efrain |
553 |
$this->logger->info('Se agrego el contenido ' . $knowledgeAreaContent->title, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
|
|
|
554 |
|
726 |
stevensc |
555 |
$categoryData = $this->getAccessibleCategories($currentUser, $currentNetwork);
|
|
|
556 |
$path = $storage->getPathKnowledgeArea();
|
|
|
557 |
|
|
|
558 |
$knowledgeData = [
|
|
|
559 |
'uuid' => $knowledgeAreaContent->uuid,
|
|
|
560 |
'title' => $knowledgeAreaContent->title,
|
|
|
561 |
'description' => $knowledgeAreaContent->description,
|
|
|
562 |
'link' => $knowledgeAreaContent->link,
|
|
|
563 |
'image' => $storage->getGenericImage($path, $knowledgeAreaContent->uuid, $knowledgeAreaContent->image),
|
727 |
stevensc |
564 |
'attachment' => $storage->getGenericFile($path, $knowledgeAreaContent->uuid, $knowledgeAreaContent->attachment),
|
726 |
stevensc |
565 |
'category' => $knowledgeAreaCategory->name,
|
|
|
566 |
'link_view' => '',
|
|
|
567 |
'link_edit' => '',
|
|
|
568 |
'link_delete' => '',
|
|
|
569 |
];
|
|
|
570 |
|
|
|
571 |
if($categoryData['allow_view']) {
|
|
|
572 |
$knowledgeData['link_view'] = $this->url()->fromRoute('knowledge-area/view', ['id' => $knowledgeAreaContent->uuid]);
|
|
|
573 |
}
|
|
|
574 |
|
|
|
575 |
if(in_array($knowledgeAreaContent->category_id, $categoryData['category_with_edition_ids'])) {
|
|
|
576 |
if($categoryData['allow_edit']) {
|
|
|
577 |
$knowledgeData['link_edit'] = $this->url()->fromRoute('knowledge-area/edit', ['id' => $knowledgeAreaContent->uuid]);
|
|
|
578 |
}
|
|
|
579 |
if($categoryData['allow_delete']) {
|
|
|
580 |
$knowledgeData['link_delete'] = $this->url()->fromRoute('knowledge-area/delete', ['id' => $knowledgeAreaContent->uuid]);
|
|
|
581 |
}
|
|
|
582 |
}
|
|
|
583 |
|
1 |
efrain |
584 |
$data = [
|
|
|
585 |
'success' => true,
|
721 |
stevensc |
586 |
'data' => [
|
|
|
587 |
'message' => 'LABEL_RECORD_ADDED',
|
726 |
stevensc |
588 |
'knowledge' => $knowledgeData
|
721 |
stevensc |
589 |
]
|
1 |
efrain |
590 |
];
|
|
|
591 |
} else {
|
|
|
592 |
$data = [
|
|
|
593 |
'success' => false,
|
|
|
594 |
'data' => $knowledgeAreaContentMapper->getError()
|
|
|
595 |
];
|
|
|
596 |
}
|
|
|
597 |
|
|
|
598 |
return new JsonModel($data);
|
|
|
599 |
} else {
|
|
|
600 |
$messages = [];
|
|
|
601 |
$form_messages = (array) $form->getMessages();
|
|
|
602 |
foreach ($form_messages as $fieldname => $field_messages) {
|
|
|
603 |
|
|
|
604 |
$messages[$fieldname] = array_values($field_messages);
|
|
|
605 |
}
|
|
|
606 |
|
|
|
607 |
return new JsonModel([
|
|
|
608 |
'success' => false,
|
|
|
609 |
'data' => $messages
|
|
|
610 |
]);
|
|
|
611 |
}
|
|
|
612 |
} else {
|
|
|
613 |
$data = [
|
|
|
614 |
'success' => false,
|
|
|
615 |
'data' => 'ERROR_METHOD_NOT_ALLOWED'
|
|
|
616 |
];
|
|
|
617 |
|
|
|
618 |
return new JsonModel($data);
|
|
|
619 |
}
|
|
|
620 |
|
|
|
621 |
return new JsonModel($data);
|
|
|
622 |
}
|
|
|
623 |
|
|
|
624 |
public function deleteAction()
|
|
|
625 |
{
|
|
|
626 |
$currentUserPlugin = $this->plugin('currentUserPlugin');
|
|
|
627 |
$currentUser = $currentUserPlugin->getUser();
|
|
|
628 |
|
|
|
629 |
|
|
|
630 |
$request = $this->getRequest();
|
|
|
631 |
$id = $this->params()->fromRoute('id');
|
|
|
632 |
|
|
|
633 |
|
|
|
634 |
|
|
|
635 |
$knowledgeAreaContentMapper = KnowledgeAreaContentMapper::getInstance($this->adapter);
|
|
|
636 |
$knowledgeAreaContent = $knowledgeAreaContentMapper->fetchOneByUuid($id);
|
|
|
637 |
if (!$knowledgeAreaContent) {
|
|
|
638 |
return new JsonModel([
|
|
|
639 |
'success' => false,
|
|
|
640 |
'data' => 'ERROR_RECORD_NOT_FOUND'
|
|
|
641 |
]);
|
|
|
642 |
}
|
|
|
643 |
|
|
|
644 |
|
|
|
645 |
|
|
|
646 |
$knowledgeAreaCategoryUserMapper = KnowledgeAreaCategoryUserMapper::getInstance($this->adapter);
|
|
|
647 |
$knowledgeAreaCategoryUser = $knowledgeAreaCategoryUserMapper->fetchOneByCategoryIdAndUserId($knowledgeAreaContent->category_id, $currentUser->id);
|
|
|
648 |
|
|
|
649 |
$ok = false;
|
|
|
650 |
if ($knowledgeAreaCategoryUser) {
|
|
|
651 |
|
|
|
652 |
if ($knowledgeAreaCategoryUser->role == KnowledgeAreaCategoryUser::ROLE_EDITOR) {
|
|
|
653 |
$ok = $knowledgeAreaContent->user_id == $currentUser->id;
|
|
|
654 |
}
|
|
|
655 |
|
|
|
656 |
if ($knowledgeAreaCategoryUser->role == KnowledgeAreaCategoryUser::ROLE_ADMINISTRATOR) {
|
|
|
657 |
$ok = true;
|
|
|
658 |
}
|
|
|
659 |
}
|
|
|
660 |
|
|
|
661 |
if (!$ok) {
|
|
|
662 |
return new JsonModel([
|
|
|
663 |
'success' => false,
|
|
|
664 |
'data' => 'ERROR_KNOWLEDGE_AREA_YOU_DO_NOT_HAVE_PERMISSION'
|
|
|
665 |
]);
|
|
|
666 |
}
|
|
|
667 |
|
|
|
668 |
if ($request->isPost()) {
|
|
|
669 |
|
|
|
670 |
$result = $knowledgeAreaContentMapper->delete($knowledgeAreaContent);
|
|
|
671 |
if ($result) {
|
|
|
672 |
$this->logger->info('Se borro el cotenido : ' . $knowledgeAreaContent->title, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
|
|
|
673 |
|
283 |
www |
674 |
|
333 |
www |
675 |
$storage = Storage::getInstance($this->config, $this->adapter);
|
283 |
www |
676 |
$target_path = $storage->getPathKnowledgeArea();
|
|
|
677 |
|
|
|
678 |
|
|
|
679 |
if($knowledgeAreaContent->attachment) {
|
|
|
680 |
$storage->deleteFile($target_path, $knowledgeAreaContent->uuid, $knowledgeAreaContent->attachment);
|
|
|
681 |
}
|
|
|
682 |
|
|
|
683 |
|
|
|
684 |
|
1 |
efrain |
685 |
if ($knowledgeAreaContent->image) {
|
283 |
www |
686 |
$storage->deleteFile($target_path, $knowledgeAreaContent->uuid, $knowledgeAreaContent->image);
|
1 |
efrain |
687 |
}
|
|
|
688 |
|
|
|
689 |
$data = [
|
|
|
690 |
'success' => true,
|
|
|
691 |
'data' => 'LABEL_RECORD_DELETED'
|
|
|
692 |
];
|
|
|
693 |
} else {
|
|
|
694 |
|
|
|
695 |
$data = [
|
|
|
696 |
'success' => false,
|
|
|
697 |
'data' => $knowledgeAreaContentMapper->getError()
|
|
|
698 |
];
|
|
|
699 |
|
|
|
700 |
return new JsonModel($data);
|
|
|
701 |
}
|
|
|
702 |
} else {
|
|
|
703 |
$data = [
|
|
|
704 |
'success' => false,
|
|
|
705 |
'data' => 'ERROR_METHOD_NOT_ALLOWED'
|
|
|
706 |
];
|
|
|
707 |
|
|
|
708 |
return new JsonModel($data);
|
|
|
709 |
}
|
|
|
710 |
|
|
|
711 |
return new JsonModel($data);
|
|
|
712 |
}
|
|
|
713 |
|
|
|
714 |
|
|
|
715 |
public function editAction()
|
|
|
716 |
{
|
|
|
717 |
$currentUserPlugin = $this->plugin('currentUserPlugin');
|
|
|
718 |
$currentUser = $currentUserPlugin->getUser();
|
|
|
719 |
|
|
|
720 |
$request = $this->getRequest();
|
|
|
721 |
$id = $this->params()->fromRoute('id');
|
|
|
722 |
|
|
|
723 |
$knowledgeAreaContentMapper = KnowledgeAreaContentMapper::getInstance($this->adapter);
|
|
|
724 |
$knowledgeAreaContent = $knowledgeAreaContentMapper->fetchOneByUuid($id);
|
|
|
725 |
if (!$knowledgeAreaContent) {
|
|
|
726 |
return new JsonModel([
|
|
|
727 |
'success' => false,
|
|
|
728 |
'data' => 'ERROR_RECORD_NOT_FOUND'
|
|
|
729 |
]);
|
|
|
730 |
}
|
|
|
731 |
|
|
|
732 |
|
|
|
733 |
|
|
|
734 |
$knowledgeAreaCategoryUserMapper = KnowledgeAreaCategoryUserMapper::getInstance($this->adapter);
|
|
|
735 |
$knowledgeAreaCategoryUser = $knowledgeAreaCategoryUserMapper->fetchOneByCategoryIdAndUserId($knowledgeAreaContent->category_id, $currentUser->id);
|
|
|
736 |
|
|
|
737 |
$ok = false;
|
|
|
738 |
if ($knowledgeAreaCategoryUser) {
|
|
|
739 |
|
|
|
740 |
if ($knowledgeAreaCategoryUser->role == KnowledgeAreaCategoryUser::ROLE_EDITOR) {
|
|
|
741 |
$ok = $knowledgeAreaContent->user_id == $currentUser->id;
|
|
|
742 |
}
|
|
|
743 |
|
|
|
744 |
if ($knowledgeAreaCategoryUser->role == KnowledgeAreaCategoryUser::ROLE_ADMINISTRATOR) {
|
|
|
745 |
$ok = true;
|
|
|
746 |
}
|
|
|
747 |
}
|
|
|
748 |
|
|
|
749 |
if (!$ok) {
|
|
|
750 |
return new JsonModel([
|
|
|
751 |
'success' => false,
|
|
|
752 |
'data' => 'ERROR_KNOWLEDGE_AREA_YOU_DO_NOT_HAVE_PERMISSION'
|
|
|
753 |
]);
|
|
|
754 |
}
|
|
|
755 |
|
|
|
756 |
if ($request->isGet()) {
|
|
|
757 |
|
|
|
758 |
|
|
|
759 |
$knowledgeAreaCategoryMapper = KnowledgeAreaCategoryMapper::getInstance($this->adapter);
|
|
|
760 |
$knowledgeAreaCategory = $knowledgeAreaCategoryMapper->fetchOne($knowledgeAreaContent->category_id);
|
|
|
761 |
|
|
|
762 |
|
|
|
763 |
|
|
|
764 |
$data = [
|
|
|
765 |
'success' => true,
|
|
|
766 |
'data' => [
|
|
|
767 |
'category_id' => $knowledgeAreaCategory->uuid,
|
|
|
768 |
'title' => $knowledgeAreaContent->title,
|
|
|
769 |
'description' => $knowledgeAreaContent->description,
|
|
|
770 |
'link' => $knowledgeAreaContent->link,
|
|
|
771 |
]
|
|
|
772 |
];
|
|
|
773 |
|
|
|
774 |
return new JsonModel($data);
|
|
|
775 |
} else if ($request->isPost()) {
|
|
|
776 |
$category_with_edition_ids = [];
|
|
|
777 |
$category_ids = [];
|
|
|
778 |
|
|
|
779 |
|
|
|
780 |
$categories = [];
|
|
|
781 |
|
|
|
782 |
|
|
|
783 |
$knowledgeAreaCategoryUserMapper = KnowledgeAreaCategoryUserMapper::getInstance($this->adapter);
|
|
|
784 |
$records = $knowledgeAreaCategoryUserMapper->fetchAllByUserId($currentUser->id);
|
|
|
785 |
foreach ($records as $record) {
|
|
|
786 |
if ($record->role == KnowledgeAreaCategoryUser::ROLE_ADMINISTRATOR || $record->role == KnowledgeAreaCategoryUser::ROLE_USER) {
|
|
|
787 |
|
|
|
788 |
array_push($category_with_edition_ids, $record->category_id);
|
|
|
789 |
}
|
|
|
790 |
|
|
|
791 |
array_push($category_ids, $record->category_id);
|
|
|
792 |
}
|
|
|
793 |
|
|
|
794 |
$knowledgeAreaCategoryMapper = KnowledgeAreaCategoryMapper::getInstance($this->adapter);
|
|
|
795 |
|
|
|
796 |
if ($category_ids) {
|
|
|
797 |
$records = $knowledgeAreaCategoryMapper->fetchAllByIds($category_ids);
|
|
|
798 |
foreach ($records as $record) {
|
|
|
799 |
if ($record->status == KnowledgeAreaCategory::STATUS_ACTIVE) {
|
|
|
800 |
|
|
|
801 |
$categories[$record->id] = [
|
|
|
802 |
'uuid' => $record->uuid,
|
|
|
803 |
'name' => $record->name,
|
|
|
804 |
];
|
|
|
805 |
}
|
|
|
806 |
}
|
|
|
807 |
}
|
|
|
808 |
|
|
|
809 |
|
|
|
810 |
|
|
|
811 |
$categories = array_values($categories);
|
|
|
812 |
usort($categories, function ($a, $b) {
|
|
|
813 |
return $a['name'] <=> $b['name'];
|
|
|
814 |
});
|
|
|
815 |
|
|
|
816 |
|
|
|
817 |
$dataPost = array_merge($request->getPost()->toArray(), $request->getFiles()->toArray());
|
|
|
818 |
$form = new KnowledgeAreaEditForm($this->adapter, $category_with_edition_ids);
|
|
|
819 |
$form->setData($dataPost);
|
|
|
820 |
|
|
|
821 |
if ($form->isValid()) {
|
|
|
822 |
$dataPost = (array) $form->getData();
|
|
|
823 |
|
|
|
824 |
|
|
|
825 |
$knowledgeAreaCategoryMapper = KnowledgeAreaCategoryMapper::getInstance($this->adapter);
|
|
|
826 |
$knowledgeAreaCategory = $knowledgeAreaCategoryMapper->fetchOneByUuid($dataPost['category_id']);
|
|
|
827 |
|
|
|
828 |
|
|
|
829 |
|
|
|
830 |
$knowledgeAreaContent->category_id = $knowledgeAreaCategory->id;
|
|
|
831 |
$knowledgeAreaContent->title = $dataPost['title'];
|
|
|
832 |
$knowledgeAreaContent->description = $dataPost['description'];
|
|
|
833 |
|
|
|
834 |
|
|
|
835 |
if ($knowledgeAreaContentMapper->update($knowledgeAreaContent)) {
|
|
|
836 |
|
561 |
stevensc |
837 |
// Se instancia la clase Storage para manejar el almacenamiento de archivos.
|
334 |
www |
838 |
$storage = Storage::getInstance($this->config, $this->adapter);
|
561 |
stevensc |
839 |
// Obtiene la ruta base para los archivos del área de conocimiento.
|
|
|
840 |
// $target_path = $storage->getPathKnowledgeArea(); // No longer needed directly here, composePathTo methods use storagePath internally
|
1 |
efrain |
841 |
|
|
|
842 |
$files = $this->getRequest()->getFiles()->toArray();
|
|
|
843 |
|
561 |
stevensc |
844 |
// Manejo de la actualización de la imagen principal.
|
1 |
efrain |
845 |
if (isset($files['image']) && empty($files['image']['error'])) {
|
283 |
www |
846 |
|
561 |
stevensc |
847 |
// Si existe una imagen previa, se elimina.
|
1 |
efrain |
848 |
if ($knowledgeAreaContent->image) {
|
560 |
stevensc |
849 |
$storage->deleteFile($target_path, $knowledgeAreaContent->uuid, $knowledgeAreaContent->image);
|
1 |
efrain |
850 |
}
|
|
|
851 |
|
560 |
stevensc |
852 |
$tmp_image_name = $files['image']['tmp_name'];
|
|
|
853 |
$original_image_name_parts = explode('.', $files['image']['name']);
|
561 |
stevensc |
854 |
// Genera un nombre de archivo único y normalizado para la nueva imagen, forzando .png.
|
560 |
stevensc |
855 |
$final_image_filename = Functions::normalizeString(uniqid() . '-' . $original_image_name_parts[0] . '.png');
|
334 |
www |
856 |
|
561 |
stevensc |
857 |
// Obtener dimensiones para el redimensionamiento de la imagen del área de conocimiento.
|
|
|
858 |
list($target_width_str, $target_height_str) = explode('x', $this->config['leaderslinked.image_sizes.knowledge_area']);
|
|
|
859 |
$target_width = (int)$target_width_str;
|
|
|
860 |
$target_height = (int)$target_height_str;
|
|
|
861 |
|
|
|
862 |
$uuid_path_segment = $knowledgeAreaCategory->uuid;
|
283 |
www |
863 |
|
561 |
stevensc |
864 |
// Construye la ruta completa del directorio de destino.
|
|
|
865 |
$full_target_dir = $storage->composePathToDirectory(Storage::TYPE_KNOWLEDGE_AREA, $uuid_path_segment);
|
|
|
866 |
// Crea el directorio si no existe.
|
|
|
867 |
if (!is_dir($full_target_dir)) {
|
|
|
868 |
mkdir($full_target_dir, 0775, true);
|
560 |
stevensc |
869 |
}
|
561 |
stevensc |
870 |
$full_target_path_for_image = $storage->composePathToFilename(Storage::TYPE_KNOWLEDGE_AREA, $uuid_path_segment, $final_image_filename);
|
334 |
www |
871 |
|
561 |
stevensc |
872 |
// Sube y redimensiona la imagen.
|
|
|
873 |
if ($storage->uploadImageResize($tmp_image_name, $full_target_path_for_image, $target_width, $target_height)) {
|
560 |
stevensc |
874 |
$knowledgeAreaContent->image = $final_image_filename;
|
|
|
875 |
$knowledgeAreaContentMapper->update($knowledgeAreaContent);
|
1 |
efrain |
876 |
}
|
334 |
www |
877 |
|
1 |
efrain |
878 |
}
|
|
|
879 |
|
561 |
stevensc |
880 |
// Manejo de la actualización del archivo adjunto.
|
1 |
efrain |
881 |
if (isset($files['attachment']) && empty($files['attachment']['error'])) {
|
560 |
stevensc |
882 |
$tmp_attachment_name = $files['attachment']['tmp_name'];
|
561 |
stevensc |
883 |
// Normaliza el nombre del nuevo archivo adjunto.
|
560 |
stevensc |
884 |
$final_attachment_filename = Functions::normalizeString($files['attachment']['name']);
|
283 |
www |
885 |
|
561 |
stevensc |
886 |
// Si existe un adjunto previo, se elimina.
|
1 |
efrain |
887 |
if ($knowledgeAreaContent->attachment) {
|
560 |
stevensc |
888 |
$storage->deleteFile($target_path, $knowledgeAreaContent->uuid, $knowledgeAreaContent->attachment);
|
1 |
efrain |
889 |
}
|
561 |
stevensc |
890 |
|
|
|
891 |
// Construye la ruta completa del directorio de destino para el adjunto.
|
|
|
892 |
$uuid_path_segment = $knowledgeAreaCategory->uuid; // Similar al comentario de la imagen.
|
560 |
stevensc |
893 |
$full_target_dir_for_attachment = $target_path . DIRECTORY_SEPARATOR . $uuid_path_segment;
|
|
|
894 |
if (!is_dir($full_target_dir_for_attachment)) {
|
|
|
895 |
mkdir($full_target_dir_for_attachment, 0775, true);
|
|
|
896 |
}
|
|
|
897 |
$full_target_path_for_attachment = $full_target_dir_for_attachment . DIRECTORY_SEPARATOR . $final_attachment_filename;
|
|
|
898 |
|
561 |
stevensc |
899 |
// Mueve el nuevo archivo adjunto a su ubicación final.
|
560 |
stevensc |
900 |
if ($storage->moveUploadedFile($tmp_attachment_name, $full_target_path_for_attachment)) {
|
|
|
901 |
$knowledgeAreaContent->attachment = $final_attachment_filename;
|
1 |
efrain |
902 |
$knowledgeAreaContentMapper->update($knowledgeAreaContent);
|
|
|
903 |
}
|
|
|
904 |
}
|
|
|
905 |
|
|
|
906 |
$this->logger->info('Se edito el contenido ' . $knowledgeAreaContent->title, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
|
|
|
907 |
|
|
|
908 |
$data = [
|
|
|
909 |
'success' => true,
|
|
|
910 |
'data' => 'LABEL_RECORD_UPDATED'
|
|
|
911 |
];
|
|
|
912 |
} else {
|
|
|
913 |
$data = [
|
|
|
914 |
'success' => false,
|
|
|
915 |
'data' => $knowledgeAreaContentMapper->getError()
|
|
|
916 |
];
|
|
|
917 |
}
|
|
|
918 |
|
|
|
919 |
return new JsonModel($data);
|
|
|
920 |
} else {
|
|
|
921 |
$messages = [];
|
|
|
922 |
$form_messages = (array) $form->getMessages();
|
|
|
923 |
foreach ($form_messages as $fieldname => $field_messages) {
|
|
|
924 |
|
|
|
925 |
$messages[$fieldname] = array_values($field_messages);
|
|
|
926 |
}
|
|
|
927 |
|
|
|
928 |
return new JsonModel([
|
|
|
929 |
'success' => false,
|
|
|
930 |
'data' => $messages
|
|
|
931 |
]);
|
|
|
932 |
}
|
|
|
933 |
} else {
|
|
|
934 |
$data = [
|
|
|
935 |
'success' => false,
|
|
|
936 |
'data' => 'ERROR_METHOD_NOT_ALLOWED'
|
|
|
937 |
];
|
|
|
938 |
|
|
|
939 |
return new JsonModel($data);
|
|
|
940 |
}
|
|
|
941 |
|
|
|
942 |
return new JsonModel($data);
|
|
|
943 |
}
|
|
|
944 |
|
|
|
945 |
|
|
|
946 |
public function viewAction()
|
|
|
947 |
{
|
|
|
948 |
$currentUserPlugin = $this->plugin('currentUserPlugin');
|
|
|
949 |
$currentUser = $currentUserPlugin->getUser();
|
|
|
950 |
|
|
|
951 |
$request = $this->getRequest();
|
|
|
952 |
$id = $this->params()->fromRoute('id');
|
|
|
953 |
|
|
|
954 |
$knowledgeAreaContentMapper = KnowledgeAreaContentMapper::getInstance($this->adapter);
|
|
|
955 |
$knowledgeAreaContent = $knowledgeAreaContentMapper->fetchOneByUuid($id);
|
|
|
956 |
if (!$knowledgeAreaContent) {
|
|
|
957 |
return new JsonModel([
|
|
|
958 |
'success' => false,
|
|
|
959 |
'data' => 'ERROR_RECORD_NOT_FOUND'
|
|
|
960 |
]);
|
|
|
961 |
}
|
|
|
962 |
|
|
|
963 |
|
|
|
964 |
|
|
|
965 |
$request = $this->getRequest();
|
|
|
966 |
if ($request->isGet()) {
|
|
|
967 |
|
|
|
968 |
|
|
|
969 |
$knowledgeAreaCategoryMapper = KnowledgeAreaCategoryMapper::getInstance($this->adapter);
|
|
|
970 |
$knowledgeAreaCategory = $knowledgeAreaCategoryMapper->fetchOne($knowledgeAreaContent->category_id);
|
|
|
971 |
|
|
|
972 |
|
|
|
973 |
|
|
|
974 |
$ok = false;
|
|
|
975 |
if ($knowledgeAreaCategory->privacy == KnowledgeAreaCategory::PRIVACY_COMPANY) {
|
|
|
976 |
|
|
|
977 |
$knowledgeAreaCategoryUserMapper = KnowledgeAreaCategoryUserMapper::getInstance($this->adapter);
|
|
|
978 |
$knowledgeAreaCategoryUser = $knowledgeAreaCategoryUserMapper->fetchOneByCategoryIdAndUserId($knowledgeAreaContent->category_id, $currentUser->id);
|
|
|
979 |
|
|
|
980 |
|
|
|
981 |
if ($knowledgeAreaCategoryUser) {
|
|
|
982 |
$ok = true;
|
|
|
983 |
}
|
|
|
984 |
}
|
|
|
985 |
if ($knowledgeAreaCategory->privacy == KnowledgeAreaCategory::PRIVACY_PUBLIC) {
|
|
|
986 |
$ok = true;
|
|
|
987 |
}
|
|
|
988 |
|
|
|
989 |
if (!$ok) {
|
|
|
990 |
return new JsonModel([
|
|
|
991 |
'success' => false,
|
|
|
992 |
'data' => 'ERROR_KNOWLEDGE_AREA_YOU_DO_NOT_HAVE_PERMISSION'
|
|
|
993 |
]);
|
|
|
994 |
}
|
|
|
995 |
|
|
|
996 |
$contentReactionMapper = ContentReactionMapper::getInstance($this->adapter);
|
|
|
997 |
$contentReaction = $contentReactionMapper->fetchOneByKnowledgeAreaIdAndUserId($knowledgeAreaContent->id, $currentUser->id);
|
|
|
998 |
|
|
|
999 |
|
333 |
www |
1000 |
$storage = Storage::getInstance($this->config, $this->adapter);
|
283 |
www |
1001 |
$path = $storage->getPathKnowledgeArea();
|
1 |
efrain |
1002 |
|
|
|
1003 |
return new JsonModel([
|
|
|
1004 |
'success' => true,
|
|
|
1005 |
'data' => [
|
|
|
1006 |
'category' => $knowledgeAreaCategory->name,
|
|
|
1007 |
'title' => $knowledgeAreaContent->title,
|
|
|
1008 |
'description' => $knowledgeAreaContent->description,
|
|
|
1009 |
'link' => $knowledgeAreaContent->link,
|
283 |
www |
1010 |
'image' => $storage->getGenericImage($path, $knowledgeAreaContent->uuid, $knowledgeAreaContent->image),
|
|
|
1011 |
'attachment' => $knowledgeAreaContent->attachment ? $storage->getGenericFile($path, $knowledgeAreaContent->uuid, $knowledgeAreaContent->attachment) : '',
|
1 |
efrain |
1012 |
'reaction' => $contentReaction ? $contentReaction->reaction : '',
|
242 |
efrain |
1013 |
'routeComments' => $this->url()->fromRoute('knowledge-area/comments', ['id' => $knowledgeAreaContent->uuid],['force_canonical' => true]),
|
|
|
1014 |
'routeCommentAdd' => $this->url()->fromRoute('knowledge-area/comments/add', ['id' => $knowledgeAreaContent->uuid],['force_canonical' => true]),
|
|
|
1015 |
'routeSaveReaction' => $this->url()->fromRoute('knowledge-area/save-reaction', ['id' => $knowledgeAreaContent->uuid],['force_canonical' => true]),
|
|
|
1016 |
'routeDeleteReaction' => $this->url()->fromRoute('knowledge-area/delete-reaction', ['id' => $knowledgeAreaContent->uuid],['force_canonical' => true]),
|
|
|
1017 |
'routeReactions' => $this->url()->fromRoute('knowledge-area/reactions', ['id' => $knowledgeAreaContent->uuid],['force_canonical' => true]),
|
1 |
efrain |
1018 |
]
|
|
|
1019 |
]);
|
|
|
1020 |
|
|
|
1021 |
} else {
|
|
|
1022 |
$data = [
|
|
|
1023 |
'success' => false,
|
|
|
1024 |
'data' => 'ERROR_METHOD_NOT_ALLOWED'
|
|
|
1025 |
];
|
|
|
1026 |
|
|
|
1027 |
return new JsonModel($data);
|
|
|
1028 |
}
|
|
|
1029 |
|
|
|
1030 |
return new JsonModel($data);
|
|
|
1031 |
}
|
242 |
efrain |
1032 |
|
|
|
1033 |
public function reactionsAction()
|
|
|
1034 |
{
|
|
|
1035 |
$id = $this->params()->fromRoute('id');
|
|
|
1036 |
|
|
|
1037 |
$request = $this->getRequest();
|
|
|
1038 |
$request = $this->getRequest();
|
|
|
1039 |
if ($request->isGet()) {
|
|
|
1040 |
|
|
|
1041 |
$currentUserPlugin = $this->plugin('currentUserPlugin');
|
|
|
1042 |
$currentUser = $currentUserPlugin->getUser();
|
|
|
1043 |
|
|
|
1044 |
$knowledgeAreaContentMapper = KnowledgeAreaContentMapper::getInstance($this->adapter);
|
|
|
1045 |
$knowledgeAreaContent = $knowledgeAreaContentMapper->fetchOneByUuid($id);
|
|
|
1046 |
if (!$knowledgeAreaContent || $knowledgeAreaContent->network_id != $currentUser->network_id) {
|
|
|
1047 |
return new JsonModel([
|
|
|
1048 |
'success' => false,
|
|
|
1049 |
'data' => 'ERROR_RECORD_NOT_FOUND'
|
|
|
1050 |
]);
|
|
|
1051 |
}
|
|
|
1052 |
|
|
|
1053 |
|
|
|
1054 |
|
|
|
1055 |
$userMapper = UserMapper::getInstance($this->adapter);
|
|
|
1056 |
|
|
|
1057 |
$items = [];
|
|
|
1058 |
|
333 |
www |
1059 |
$storage = Storage::getInstance($this->config, $this->adapter);
|
283 |
www |
1060 |
$path = $storage->getPathKnowledgeArea();
|
|
|
1061 |
|
242 |
efrain |
1062 |
$contentReactionMapper = ContentReactionMapper::getInstance($this->adapter);
|
|
|
1063 |
$records = $contentReactionMapper->fetchAllByKnowledgeAreaId($knowledgeAreaContent->id);
|
|
|
1064 |
|
|
|
1065 |
foreach($records as $record)
|
|
|
1066 |
{
|
|
|
1067 |
$user = $userMapper->fetchOne($record->user_id);
|
|
|
1068 |
if($user && $user->status == User::STATUS_ACTIVE) {
|
|
|
1069 |
|
|
|
1070 |
array_push($items, [
|
|
|
1071 |
'first_name' => $user->first_name,
|
|
|
1072 |
'last_name' => $user->last_name,
|
|
|
1073 |
'email' => $user->email,
|
283 |
www |
1074 |
'image' => $storage->getGenericImage($path, $user->uuid, $user->image),
|
242 |
efrain |
1075 |
'reaction' => $record->reaction,
|
|
|
1076 |
]);
|
|
|
1077 |
}
|
|
|
1078 |
}
|
|
|
1079 |
|
|
|
1080 |
$response = [
|
|
|
1081 |
'success' => true,
|
|
|
1082 |
'data' => $items
|
|
|
1083 |
];
|
|
|
1084 |
|
|
|
1085 |
return new JsonModel($response);
|
|
|
1086 |
|
|
|
1087 |
|
|
|
1088 |
|
|
|
1089 |
|
|
|
1090 |
} else {
|
|
|
1091 |
$response = [
|
|
|
1092 |
'success' => false,
|
|
|
1093 |
'data' => 'ERROR_METHOD_NOT_ALLOWED'
|
|
|
1094 |
];
|
|
|
1095 |
|
|
|
1096 |
return new JsonModel($response);
|
|
|
1097 |
}
|
|
|
1098 |
}
|
1 |
efrain |
1099 |
|
|
|
1100 |
|
|
|
1101 |
public function addCommentAction()
|
|
|
1102 |
{
|
|
|
1103 |
$currentUserPlugin = $this->plugin('currentUserPlugin');
|
|
|
1104 |
$currentUser = $currentUserPlugin->getUser();
|
|
|
1105 |
|
|
|
1106 |
$id = $this->params()->fromRoute('id');
|
|
|
1107 |
|
|
|
1108 |
$request = $this->getRequest();
|
|
|
1109 |
if ($request->isPost()) {
|
|
|
1110 |
|
|
|
1111 |
$knowledgeAreaContentMapper = KnowledgeAreaContentMapper::getInstance($this->adapter);
|
|
|
1112 |
$knowledgeAreaContent = $knowledgeAreaContentMapper->fetchOneByUuid($id);
|
|
|
1113 |
if (!$knowledgeAreaContent || $knowledgeAreaContent->network_id != $currentUser->network_id) {
|
|
|
1114 |
return new JsonModel([
|
|
|
1115 |
'success' => false,
|
|
|
1116 |
'data' => 'ERROR_RECORD_NOT_FOUND'
|
|
|
1117 |
]);
|
|
|
1118 |
}
|
|
|
1119 |
|
|
|
1120 |
|
|
|
1121 |
|
|
|
1122 |
|
|
|
1123 |
$dataPost = $request->getPost()->toArray();
|
|
|
1124 |
$form = new CommentForm();
|
|
|
1125 |
$form->setData($dataPost);
|
|
|
1126 |
|
|
|
1127 |
if ($form->isValid()) {
|
242 |
efrain |
1128 |
|
1 |
efrain |
1129 |
$dataPost = (array) $form->getData();
|
|
|
1130 |
|
|
|
1131 |
$comment = new Comment();
|
|
|
1132 |
$comment->network_id = $currentUser->network_id;
|
|
|
1133 |
$comment->comment = $dataPost['comment'];
|
|
|
1134 |
$comment->user_id = $currentUser->id;
|
|
|
1135 |
$comment->knowledge_area_id = $knowledgeAreaContent->id;
|
|
|
1136 |
$comment->relational = Comment::RELATIONAL_KNOWLEDGE_AREA;
|
|
|
1137 |
|
|
|
1138 |
$commentMapper = CommentMapper::getInstance($this->adapter);
|
|
|
1139 |
$now = $commentMapper->getDatebaseNow();
|
|
|
1140 |
|
|
|
1141 |
if ($commentMapper->insert($comment)) {
|
|
|
1142 |
|
|
|
1143 |
$total_comments = $commentMapper->fetchCountCommentByKnowledgeAreaId($comment->knowledge_area_id);
|
|
|
1144 |
|
|
|
1145 |
$knowledgeAreaContent->total_comments = $total_comments;
|
|
|
1146 |
$knowledgeAreaContentMapper->update($knowledgeAreaContent);
|
|
|
1147 |
|
|
|
1148 |
$response = [
|
|
|
1149 |
'success' => true,
|
|
|
1150 |
'data' => $this->renderComment($comment->id, $now),
|
|
|
1151 |
'total_comments' => $total_comments
|
|
|
1152 |
];
|
|
|
1153 |
|
|
|
1154 |
return new JsonModel($response);
|
|
|
1155 |
} else {
|
|
|
1156 |
|
|
|
1157 |
$response = [
|
|
|
1158 |
'success' => false,
|
|
|
1159 |
'data' => $commentMapper->getError()
|
|
|
1160 |
];
|
|
|
1161 |
|
|
|
1162 |
return new JsonModel($response);
|
|
|
1163 |
}
|
|
|
1164 |
} else {
|
|
|
1165 |
$message = '';;
|
|
|
1166 |
$form_messages = (array) $form->getMessages();
|
|
|
1167 |
foreach ($form_messages as $fieldname => $field_messages) {
|
|
|
1168 |
foreach ($field_messages as $key => $value) {
|
|
|
1169 |
$message = $value;
|
|
|
1170 |
}
|
|
|
1171 |
}
|
|
|
1172 |
|
|
|
1173 |
$response = [
|
|
|
1174 |
'success' => false,
|
|
|
1175 |
'data' => $message
|
|
|
1176 |
];
|
|
|
1177 |
|
|
|
1178 |
return new JsonModel($response);
|
|
|
1179 |
}
|
|
|
1180 |
} else {
|
|
|
1181 |
$response = [
|
|
|
1182 |
'success' => false,
|
|
|
1183 |
'data' => 'ERROR_METHOD_NOT_ALLOWED'
|
|
|
1184 |
];
|
|
|
1185 |
|
|
|
1186 |
return new JsonModel($response);
|
|
|
1187 |
}
|
|
|
1188 |
}
|
|
|
1189 |
|
|
|
1190 |
|
|
|
1191 |
|
|
|
1192 |
public function deleteCommentAction()
|
|
|
1193 |
{
|
|
|
1194 |
$currentUserPlugin = $this->plugin('currentUserPlugin');
|
|
|
1195 |
$currentUser = $currentUserPlugin->getUser();
|
|
|
1196 |
|
|
|
1197 |
$request = $this->getRequest();
|
|
|
1198 |
if ($request->isPost()) {
|
|
|
1199 |
|
|
|
1200 |
$id = $this->params()->fromRoute('id');
|
|
|
1201 |
$comment = $this->params()->fromRoute('comment');
|
|
|
1202 |
|
|
|
1203 |
|
|
|
1204 |
$knowledgeAreaContentMapper = KnowledgeAreaContentMapper::getInstance($this->adapter);
|
|
|
1205 |
$knowledgeAreaContent = $knowledgeAreaContentMapper->fetchOneByUuid($id);
|
|
|
1206 |
|
|
|
1207 |
|
|
|
1208 |
|
|
|
1209 |
if ($knowledgeAreaContent && $knowledgeAreaContent->network_id == $currentUser->network_id) {
|
|
|
1210 |
|
|
|
1211 |
$commentMapper = CommentMapper::getInstance($this->adapter);
|
|
|
1212 |
$comment = $commentMapper->fetchOneByUuid($comment);
|
|
|
1213 |
|
|
|
1214 |
|
|
|
1215 |
if ($comment && $comment->knowledge_area_id == $knowledgeAreaContent->id && $comment->user_id == $currentUser->id) {
|
|
|
1216 |
|
|
|
1217 |
$comment->status = Comment::STATUS_DELETED;
|
|
|
1218 |
|
|
|
1219 |
if ($commentMapper->update($comment)) {
|
|
|
1220 |
|
|
|
1221 |
$total_comments = $commentMapper->fetchCountCommentByKnowledgeAreaId($knowledgeAreaContent->id);
|
|
|
1222 |
$knowledgeAreaContent->total_comments = $total_comments;
|
|
|
1223 |
$knowledgeAreaContentMapper->update($knowledgeAreaContent);
|
|
|
1224 |
|
|
|
1225 |
$response = [
|
|
|
1226 |
'success' => true,
|
|
|
1227 |
'data' => 'LABEL_COMMENT_WAS_DELETED',
|
|
|
1228 |
'total_comments' => $total_comments
|
|
|
1229 |
];
|
|
|
1230 |
} else {
|
|
|
1231 |
$response = [
|
|
|
1232 |
'success' => false,
|
|
|
1233 |
'data' => $commentMapper->getError()
|
|
|
1234 |
];
|
|
|
1235 |
}
|
|
|
1236 |
} else {
|
|
|
1237 |
$response = [
|
|
|
1238 |
'success' => false,
|
|
|
1239 |
'data' => 'ERROR_COMMENT_NOT_FOUND'
|
|
|
1240 |
];
|
|
|
1241 |
}
|
|
|
1242 |
} else {
|
|
|
1243 |
$response = [
|
|
|
1244 |
'success' => false,
|
|
|
1245 |
'data' => 'ERROR_COMMENT_NOT_FOUND'
|
|
|
1246 |
];
|
|
|
1247 |
}
|
|
|
1248 |
} else {
|
|
|
1249 |
$response = [
|
|
|
1250 |
'success' => false,
|
|
|
1251 |
'data' => 'ERROR_METHOD_NOT_ALLOWED'
|
|
|
1252 |
];
|
|
|
1253 |
}
|
|
|
1254 |
|
|
|
1255 |
return new JsonModel($response);
|
|
|
1256 |
}
|
|
|
1257 |
|
|
|
1258 |
|
|
|
1259 |
|
|
|
1260 |
public function saveReactionAction()
|
|
|
1261 |
{
|
|
|
1262 |
|
|
|
1263 |
$currentUserPlugin = $this->plugin('currentUserPlugin');
|
|
|
1264 |
$currentUser = $currentUserPlugin->getUser();
|
|
|
1265 |
|
|
|
1266 |
$id = $this->params()->fromRoute('id');
|
|
|
1267 |
$reaction = $this->params()->fromPost('reaction');
|
|
|
1268 |
|
|
|
1269 |
$request = $this->getRequest();
|
|
|
1270 |
if ($request->isPost()) {
|
|
|
1271 |
|
|
|
1272 |
$knowledgeAreaContentMapper = KnowledgeAreaContentMapper::getInstance($this->adapter);
|
|
|
1273 |
$knowledgeAreaContent = $knowledgeAreaContentMapper->fetchOneByUuid($id);
|
|
|
1274 |
if (!$knowledgeAreaContent || $knowledgeAreaContent->network_id != $currentUser->network_id) {
|
|
|
1275 |
return new JsonModel([
|
|
|
1276 |
'success' => false,
|
|
|
1277 |
'data' => 'ERROR_RECORD_NOT_FOUND'
|
|
|
1278 |
]);
|
|
|
1279 |
}
|
|
|
1280 |
|
|
|
1281 |
$reactions = [
|
|
|
1282 |
ContentReaction::REACTION_RECOMMENDED,
|
|
|
1283 |
ContentReaction::REACTION_SUPPORT,
|
|
|
1284 |
ContentReaction::REACTION_LOVE,
|
|
|
1285 |
ContentReaction::REACTION_INTEREST,
|
|
|
1286 |
ContentReaction::REACTION_FUN
|
|
|
1287 |
|
|
|
1288 |
];
|
|
|
1289 |
if(!in_array($reaction, $reactions)) {
|
|
|
1290 |
$response = [
|
|
|
1291 |
'success' => false,
|
|
|
1292 |
'data' => 'ERROR_REACTION_NOT_FOUND'
|
|
|
1293 |
];
|
|
|
1294 |
return new JsonModel($response);
|
|
|
1295 |
}
|
|
|
1296 |
|
|
|
1297 |
$contentReactionMapper = ContentReactionMapper::getInstance($this->adapter);
|
|
|
1298 |
$contentReaction = $contentReactionMapper->fetchOneByKnowledgeAreaIdAndUserId($knowledgeAreaContent->id, $currentUser->id);
|
|
|
1299 |
|
|
|
1300 |
if ($contentReaction) {
|
|
|
1301 |
$contentReaction->reaction = $reaction;
|
|
|
1302 |
|
|
|
1303 |
$result = $contentReactionMapper->update($contentReaction);
|
|
|
1304 |
} else {
|
|
|
1305 |
$contentReaction = new ContentReaction();
|
|
|
1306 |
$contentReaction->user_id = $currentUser->id;
|
|
|
1307 |
$contentReaction->knowledge_area_id = $knowledgeAreaContent->id;
|
|
|
1308 |
$contentReaction->relational = ContentReaction::RELATIONAL_KNOWLEDGE_AREA;
|
|
|
1309 |
$contentReaction->reaction = $reaction;
|
|
|
1310 |
|
|
|
1311 |
$result = $contentReactionMapper->insert($contentReaction);
|
|
|
1312 |
}
|
|
|
1313 |
|
|
|
1314 |
|
|
|
1315 |
|
|
|
1316 |
if ($result) {
|
|
|
1317 |
|
|
|
1318 |
$reactions = $contentReactionMapper->fetchCountByKnowledgeAreaId($knowledgeAreaContent->id);
|
|
|
1319 |
$response = [
|
|
|
1320 |
'success' => true,
|
|
|
1321 |
'data' => [
|
|
|
1322 |
'reactions' => $reactions
|
|
|
1323 |
]
|
|
|
1324 |
];
|
|
|
1325 |
} else {
|
|
|
1326 |
$response = [
|
|
|
1327 |
'success' => false,
|
|
|
1328 |
'data' => $contentReactionMapper->getError()
|
|
|
1329 |
];
|
|
|
1330 |
}
|
|
|
1331 |
return new JsonModel($response);
|
|
|
1332 |
}
|
|
|
1333 |
|
|
|
1334 |
$response = [
|
|
|
1335 |
'success' => false,
|
|
|
1336 |
'data' => 'ERROR_METHOD_NOT_ALLOWED'
|
|
|
1337 |
];
|
|
|
1338 |
return new JsonModel($response);
|
|
|
1339 |
}
|
|
|
1340 |
|
|
|
1341 |
public function deleteReactionAction()
|
|
|
1342 |
{
|
|
|
1343 |
$currentUserPlugin = $this->plugin('currentUserPlugin');
|
|
|
1344 |
$currentUser = $currentUserPlugin->getUser();
|
|
|
1345 |
|
|
|
1346 |
$id = $this->params()->fromRoute('id');
|
|
|
1347 |
|
|
|
1348 |
$request = $this->getRequest();
|
|
|
1349 |
if ($request->isPost()) {
|
|
|
1350 |
|
|
|
1351 |
|
|
|
1352 |
$knowledgeAreaContentMapper = KnowledgeAreaContentMapper::getInstance($this->adapter);
|
|
|
1353 |
$knowledgeAreaContent = $knowledgeAreaContentMapper->fetchOneByUuid($id);
|
|
|
1354 |
if (!$knowledgeAreaContent || $knowledgeAreaContent->network_id != $currentUser->network_id) {
|
|
|
1355 |
return new JsonModel([
|
|
|
1356 |
'success' => false,
|
|
|
1357 |
'data' => 'ERROR_RECORD_NOT_FOUND'
|
|
|
1358 |
]);
|
|
|
1359 |
}
|
|
|
1360 |
|
|
|
1361 |
$contentReactionMapper = ContentReactionMapper::getInstance($this->adapter);
|
|
|
1362 |
$contentReaction = $contentReactionMapper->fetchOneByKnowledgeAreaIdAndUserId($knowledgeAreaContent->id, $currentUser->id);
|
|
|
1363 |
|
|
|
1364 |
if (!$contentReaction) {
|
|
|
1365 |
$response = [
|
|
|
1366 |
'success' => false,
|
|
|
1367 |
'data' => 'ERROR_DUPLICATE_ACTION'
|
|
|
1368 |
];
|
|
|
1369 |
return new JsonModel($response);
|
|
|
1370 |
}
|
|
|
1371 |
|
|
|
1372 |
if ($contentReactionMapper->deleteByKnowledgeAreaIdAndUserId($knowledgeAreaContent->id, $currentUser->id)) {
|
|
|
1373 |
$reactions = $contentReactionMapper->fetchCountByKnowledgeAreaId($knowledgeAreaContent->id);
|
|
|
1374 |
|
|
|
1375 |
$response = [
|
|
|
1376 |
'success' => true,
|
|
|
1377 |
'data' => [
|
|
|
1378 |
'reactions' => $reactions
|
|
|
1379 |
]
|
|
|
1380 |
];
|
|
|
1381 |
} else {
|
|
|
1382 |
$response = [
|
|
|
1383 |
'success' => false,
|
|
|
1384 |
'data' => $contentReactionMapper->getError()
|
|
|
1385 |
];
|
|
|
1386 |
}
|
|
|
1387 |
return new JsonModel($response);
|
|
|
1388 |
}
|
|
|
1389 |
|
|
|
1390 |
$response = [
|
|
|
1391 |
'success' => false,
|
|
|
1392 |
'data' => 'ERROR_METHOD_NOT_ALLOWED'
|
|
|
1393 |
];
|
|
|
1394 |
return new JsonModel($response);
|
|
|
1395 |
}
|
|
|
1396 |
|
|
|
1397 |
public function commentsAction()
|
|
|
1398 |
{
|
|
|
1399 |
$id = $this->params()->fromRoute('id');
|
|
|
1400 |
|
|
|
1401 |
$request = $this->getRequest();
|
|
|
1402 |
if ($request->isGet()) {
|
|
|
1403 |
$currentUserPlugin = $this->plugin('currentUserPlugin');
|
|
|
1404 |
$currentUser = $currentUserPlugin->getUser();
|
|
|
1405 |
|
|
|
1406 |
$knowledgeAreaContentMapper = KnowledgeAreaContentMapper::getInstance($this->adapter);
|
|
|
1407 |
$now = $knowledgeAreaContentMapper->getDatebaseNow();
|
|
|
1408 |
|
|
|
1409 |
$knowledgeAreaContent = $knowledgeAreaContentMapper->fetchOneByUuid($id);
|
|
|
1410 |
if (!$knowledgeAreaContent || $knowledgeAreaContent->network_id != $currentUser->network_id) {
|
|
|
1411 |
return new JsonModel([
|
|
|
1412 |
'success' => false,
|
|
|
1413 |
'data' => 'ERROR_RECORD_NOT_FOUND'
|
|
|
1414 |
]);
|
|
|
1415 |
}
|
|
|
1416 |
|
|
|
1417 |
$commentMapper = CommentMapper::getInstance($this->adapter);
|
|
|
1418 |
$records = $commentMapper->fetchAllPublishedByKnowledgeAreaId($knowledgeAreaContent->id);
|
|
|
1419 |
|
|
|
1420 |
$comments = [];
|
|
|
1421 |
foreach ($records as $record) {
|
|
|
1422 |
$comment = $this->renderComment($record->id, $now);
|
|
|
1423 |
array_push($comments, $comment);
|
|
|
1424 |
}
|
|
|
1425 |
|
|
|
1426 |
$response = [
|
|
|
1427 |
'success' => true,
|
|
|
1428 |
'data' => $comments
|
|
|
1429 |
];
|
|
|
1430 |
|
|
|
1431 |
return new JsonModel($response);
|
|
|
1432 |
} else {
|
|
|
1433 |
|
|
|
1434 |
|
|
|
1435 |
|
|
|
1436 |
$response = [
|
|
|
1437 |
'success' => false,
|
|
|
1438 |
'data' => 'ERROR_METHOD_NOT_ALLOWED'
|
|
|
1439 |
];
|
|
|
1440 |
|
|
|
1441 |
|
|
|
1442 |
return new JsonModel($response);
|
|
|
1443 |
}
|
|
|
1444 |
}
|
|
|
1445 |
|
|
|
1446 |
|
|
|
1447 |
|
|
|
1448 |
|
|
|
1449 |
private function renderComment($comment_id, $now)
|
|
|
1450 |
{
|
|
|
1451 |
$item = [];
|
|
|
1452 |
|
|
|
1453 |
$commentMapper = CommentMapper::getInstance($this->adapter);
|
|
|
1454 |
$record = $commentMapper->fetchOne($comment_id);
|
|
|
1455 |
|
|
|
1456 |
$knowledgeAreaContentMapper = KnowledgeAreaContentMapper::getInstance($this->adapter);
|
|
|
1457 |
$knowledgeAreaContent = $knowledgeAreaContentMapper->fetchOne($record->knowledge_area_id);
|
|
|
1458 |
|
283 |
www |
1459 |
|
|
|
1460 |
|
1 |
efrain |
1461 |
if ($record) {
|
333 |
www |
1462 |
$storage = Storage::getInstance($this->config, $this->adapter);
|
283 |
www |
1463 |
|
|
|
1464 |
|
1 |
efrain |
1465 |
$userMapper = UserMapper::getInstance($this->adapter);
|
|
|
1466 |
|
|
|
1467 |
$user = $userMapper->fetchOne($record->user_id);
|
|
|
1468 |
|
|
|
1469 |
$item['unique'] = uniqid();
|
283 |
www |
1470 |
$item['user_image'] = $storage->getUserImage($user);
|
1 |
efrain |
1471 |
$item['user_url'] = $this->url()->fromRoute('profile/view', ['id' => $user->uuid]);
|
|
|
1472 |
$item['user_name'] = $user->first_name . ' ' . $user->last_name;
|
|
|
1473 |
$item['time_elapsed'] = Functions::timeAgo($record->added_on, $now);
|
|
|
1474 |
$item['comment'] = $record->comment;
|
|
|
1475 |
$item['link_delete'] = $this->url()->fromRoute('knowledge-area/comments/delete', ['id' => $knowledgeAreaContent->uuid, 'comment' => $record->uuid]);
|
|
|
1476 |
}
|
|
|
1477 |
return $item;
|
|
|
1478 |
}
|
|
|
1479 |
}
|