Rev 726 | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |
<?php
/**
*
* Controlador: Mis Perfiles
*
*/
declare(strict_types=1);
namespace LeadersLinked\Controller;
use Laminas\Db\Adapter\AdapterInterface;
use Laminas\Mvc\Controller\AbstractActionController;
use Laminas\Log\LoggerInterface;
use Laminas\View\Model\ViewModel;
use Laminas\View\Model\JsonModel;
use LeadersLinked\Library\Functions;
use LeadersLinked\Mapper\KnowledgeAreaCategoryMapper;
use LeadersLinked\Mapper\KnowledgeAreaCategoryUserMapper;
use LeadersLinked\Mapper\UserMapper;
use LeadersLinked\Model\KnowledgeAreaCategory;
use LeadersLinked\Model\KnowledgeAreaCategoryUser;
use LeadersLinked\Form\KnowledgeArea\KnowledgeAreaCreateForm;
use LeadersLinked\Mapper\KnowledgeAreaContentMapper;
use LeadersLinked\Model\KnowledgeAreaContent;
use LeadersLinked\Form\KnowledgeArea\KnowledgeAreaEditForm;
use LeadersLinked\Mapper\CommentMapper;
use LeadersLinked\Form\KnowledgeArea\CommentForm;
use LeadersLinked\Model\Comment;
use LeadersLinked\Mapper\ContentReactionMapper;
use LeadersLinked\Model\ContentReaction;
use LeadersLinked\Mapper\KnowledgeAreaCategoryJobDescriptionMapper;
use LeadersLinked\Mapper\OrganizationPositionMapper;
use LeadersLinked\Model\User;
use LeadersLinked\Library\Storage;
class KnowledgeAreaController extends AbstractActionController
{
/**
*
* @var \Laminas\Db\Adapter\AdapterInterface
*/
private $adapter;
/**
*
* @var \LeadersLinked\Cache\CacheInterface
*/
private $cache;
/**
*
* @var \Laminas\Log\LoggerInterface
*/
private $logger;
/**
*
* @var array
*/
private $config;
/**
*
* @var \Laminas\Mvc\I18n\Translator
*/
private $translator;
/**
*
* @param \Laminas\Db\Adapter\AdapterInterface $adapter
* @param \LeadersLinked\Cache\CacheInterface $cache
* @param \Laminas\Log\LoggerInterface LoggerInterface $logger
* @param array $config
* @param \Laminas\Mvc\I18n\Translator $translator
*/
public function __construct($adapter, $cache, $logger, $config, $translator)
{
$this->adapter = $adapter;
$this->cache = $cache;
$this->logger = $logger;
$this->config = $config;
$this->translator = $translator;
}
/**
*
* Handles the display of the knowledge area content listing.
* Fetches knowledge areas based on user permissions, network settings, and filters.
* Returns a JSON response suitable for a data table or list view.
* {@inheritDoc}
* @see \Laminas\Mvc\Controller\AbstractActionController::indexAction()
*/
public function indexAction()
{
$request = $this->getRequest();
if (!$request->isGet()) {
return new JsonModel([
'success' => false,
'data' => 'ERROR_METHOD_NOT_ALLOWED'
]);
}
// Get current user and network information from plugins.
$currentUserPlugin = $this->plugin('currentUserPlugin');
$currentUser = $currentUserPlugin->getUser();
$currentNetworkPlugin = $this->plugin('currentNetworkPlugin');
$currentNetwork = $currentNetworkPlugin->getNetwork();
// --- Query Parameters ---
$category_filter_id = Functions::sanitizeFilterString($this->params()->fromQuery('category_id'));
$search = Functions::sanitizeFilterString($this->params()->fromQuery('search'));
$page = intval($this->params()->fromQuery('start', 1), 10);
$order_field = 'added_on'; // Default order field
$order_direction = 'asc'; // Default order direction
$records_x_page = 12; // Default records per page
// --- Get accessible categories and filter by category if needed ---
$categoryData = $this->getAccessibleCategories($currentUser, $currentNetwork);
$category_ids_to_filter = $this->filterCategoriesByRequest($categoryData, $category_filter_id);
// --- Content Fetching ---
$storage = Storage::getInstance($this->config, $this->adapter);
$path = $storage->getPathKnowledgeArea();
$knowledgeAreaContentMapper = KnowledgeAreaContentMapper::getInstance($this->adapter);
$paginator = $knowledgeAreaContentMapper->fetchAllDataTableByCategoryIds(
$category_ids_to_filter,
$search,
$page,
$records_x_page,
$order_field,
$order_direction
);
// --- Response Item Construction ---
$items = $this->buildContentItems($paginator, $categoryData, $storage, $path);
// --- JSON Response ---
$image_size = $this->config['leaderslinked.image_sizes.knowledge_area'];
return new JsonModel([
'success' => true,
'data' => [
'current' => [
'items' => $items,
'page' => $paginator->getCurrentPageNumber(),
'count' => $paginator->getCurrentItemCount(),
],
'total' => [
'count' => $paginator->getTotalItemCount(),
'pages' => $paginator->getPages()->pageCount,
],
'categories' => array_values($categoryData['categories']),
'categories_with_edition' => $categoryData['categories_with_edition_uuids'],
'link_add' => $categoryData['allow_add'] ? $this->url()->fromRoute('knowledge-area/add') : '',
'image_size' => $image_size,
'content_edit' => count($categoryData['category_with_edition_ids']) > 0,
]
]);
}
/**
* Get metadata for knowledge area (categories, permissions, etc.)
* This endpoint can be called separately to get UI configuration data
*/
public function metadataAction()
{
$request = $this->getRequest();
if (!$request->isGet()) {
return new JsonModel([
'success' => false,
'data' => 'ERROR_METHOD_NOT_ALLOWED'
]);
}
$currentUserPlugin = $this->plugin('currentUserPlugin');
$currentUser = $currentUserPlugin->getUser();
$currentNetworkPlugin = $this->plugin('currentNetworkPlugin');
$currentNetwork = $currentNetworkPlugin->getNetwork();
$categoryData = $this->getAccessibleCategories($currentUser, $currentNetwork);
$image_size = $this->config['leaderslinked.image_sizes.knowledge_area'];
return new JsonModel([
'success' => true,
'data' => [
'categories' => array_values($categoryData['categories']),
'categories_with_edition' => $categoryData['categories_with_edition_uuids'],
'link_add' => $categoryData['allow_add'] ? $this->url()->fromRoute('knowledge-area/add') : '',
'image_size' => $image_size,
'content_edit' => count($categoryData['category_with_edition_ids']) > 0,
]
]);
}
/**
* Get only the content listing without metadata
* This endpoint can be called for pagination/filtering without reloading metadata
*/
public function listAction()
{
$request = $this->getRequest();
if (!$request->isGet()) {
return new JsonModel([
'success' => false,
'data' => 'ERROR_METHOD_NOT_ALLOWED'
]);
}
$currentUserPlugin = $this->plugin('currentUserPlugin');
$currentUser = $currentUserPlugin->getUser();
$currentNetworkPlugin = $this->plugin('currentNetworkPlugin');
$currentNetwork = $currentNetworkPlugin->getNetwork();
// --- Query Parameters ---
$category_filter_id = Functions::sanitizeFilterString($this->params()->fromQuery('category_id'));
$search = Functions::sanitizeFilterString($this->params()->fromQuery('search'));
$page = intval($this->params()->fromQuery('start', 1), 10);
$order_field = 'added_on';
$order_direction = 'asc';
$records_x_page = 12;
// --- Get accessible categories and filter ---
$categoryData = $this->getAccessibleCategories($currentUser, $currentNetwork);
$category_ids_to_filter = $this->filterCategoriesByRequest($categoryData, $category_filter_id);
// --- Content Fetching ---
$storage = Storage::getInstance($this->config, $this->adapter);
$path = $storage->getPathKnowledgeArea();
$knowledgeAreaContentMapper = KnowledgeAreaContentMapper::getInstance($this->adapter);
$paginator = $knowledgeAreaContentMapper->fetchAllDataTableByCategoryIds(
$category_ids_to_filter,
$search,
$page,
$records_x_page,
$order_field,
$order_direction
);
// --- Response Item Construction ---
$items = $this->buildContentItems($paginator, $categoryData, $storage, $path);
return new JsonModel([
'success' => true,
'data' => [
'current' => [
'items' => $items,
'page' => $paginator->getCurrentPageNumber(),
'count' => $paginator->getCurrentItemCount(),
],
'total' => [
'count' => $paginator->getTotalItemCount(),
'pages' => $paginator->getPages()->pageCount,
],
]
]);
}
/**
* Get accessible categories for the current user and network
*
* @param object $currentUser
* @param object $currentNetwork
* @return array
*/
private function getAccessibleCategories($currentUser, $currentNetwork)
{
// --- ACL Permissions ---
$acl = $this->getEvent()->getViewModel()->getVariable('acl');
$allowAdd = $acl->isAllowed($currentUser->usertype_id, 'knowledge-area/add') ? 1 : 0;
$allowEdit = $acl->isAllowed($currentUser->usertype_id, 'knowledge-area/edit');
$allowDelete = $acl->isAllowed($currentUser->usertype_id, 'knowledge-area/delete');
$allowView = $acl->isAllowed($currentUser->usertype_id, 'knowledge-area/view');
// --- Category Fetching and Processing ---
$category_with_edition_ids = [];
$category_with_edition_uuids = [];
$user_accessible_category_ids = [];
$categories = [];
// Instantiate mappers.
$knowledgeAreaCategoryMapper = KnowledgeAreaCategoryMapper::getInstance($this->adapter);
$knowledgeAreaCategoryUserMapper = KnowledgeAreaCategoryUserMapper::getInstance($this->adapter);
// 1. Fetch categories based on user's direct permissions
$userCategoryRecords = $knowledgeAreaCategoryUserMapper->fetchAllByUserId($currentUser->id);
foreach ($userCategoryRecords as $record) {
if ($record->role == KnowledgeAreaCategoryUser::ROLE_ADMINISTRATOR || $record->role == KnowledgeAreaCategoryUser::ROLE_USER) {
array_push($category_with_edition_ids, $record->category_id);
$categoryDetails = $knowledgeAreaCategoryMapper->fetchOne($record->category_id);
if($categoryDetails) {
array_push($category_with_edition_uuids, $categoryDetails->uuid);
}
}
array_push($user_accessible_category_ids, $record->category_id);
}
// 2. Fetch details for all directly accessible categories
if (!empty($user_accessible_category_ids)) {
$detailedUserCategories = $knowledgeAreaCategoryMapper->fetchAllByIds($user_accessible_category_ids);
foreach ($detailedUserCategories as $record) {
if (!isset($categories[$record->id])) {
$categories[$record->id] = [
'uuid' => $record->uuid,
'name' => $record->name,
];
}
}
}
// 3. Fetch all public categories within the current network
$publicCategories = $knowledgeAreaCategoryMapper->fetchAllPublicByNetworkId($currentNetwork->id);
foreach ($publicCategories as $record) {
if (!isset($categories[$record->id])) {
$categories[$record->id] = [
'uuid' => $record->uuid,
'name' => $record->name,
];
}
}
// Sort all collected categories alphabetically by name
uasort($categories, function ($a, $b) {
return $a['name'] <=> $b['name'];
});
return [
'categories' => $categories,
'category_with_edition_ids' => $category_with_edition_ids,
'categories_with_edition_uuids' => $category_with_edition_uuids,
'allow_add' => $allowAdd,
'allow_edit' => $allowEdit,
'allow_delete' => $allowDelete,
'allow_view' => $allowView,
];
}
/**
* Filter categories based on request parameter
*
* @param array $categoryData
* @param string $category_filter_id
* @return array
*/
private function filterCategoriesByRequest($categoryData, $category_filter_id)
{
$category_ids_to_filter = array_keys($categoryData['categories']);
if ($category_filter_id) {
$knowledgeAreaCategoryMapper = KnowledgeAreaCategoryMapper::getInstance($this->adapter);
$categoryFilter = $knowledgeAreaCategoryMapper->fetchOneByUuid($category_filter_id);
if ($categoryFilter && isset($categoryData['categories'][$categoryFilter->id])) {
$category_ids_to_filter = [$categoryFilter->id];
} else {
$category_ids_to_filter = [];
}
}
return $category_ids_to_filter;
}
/**
* Build content items for response
*
* @param object $paginator
* @param array $categoryData
* @param object $storage
* @param string $path
* @return array
*/
private function buildContentItems($paginator, $categoryData, $storage, $path)
{
$items = [];
$contentRecords = $paginator->getCurrentItems();
$knowledgeAreaCategoryMapper = KnowledgeAreaCategoryMapper::getInstance($this->adapter);
foreach ($contentRecords as $record) {
// Ensure category details are available for the current content item
if (!isset($categoryData['categories'][$record->category_id])) {
$category = $knowledgeAreaCategoryMapper->fetchOne($record->category_id);
if ($category) {
$categoryData['categories'][$category->id] = [
'uuid' => $category->uuid,
'name' => $category->name,
];
} else {
$categoryData['categories'][$record->category_id] = ['uuid' => null, 'name' => 'Unknown Category'];
}
}
// Strip HTML tags and truncate description for display
$description = strip_tags($record->description);
if (strlen($description) > 120) {
$description = substr($description, 0, 120) . '...';
}
// Construct the item for the JSON response
$item = [
'image' => $storage->getGenericImage($path, $record->uuid, $record->image),
'title' => $record->title,
'description' => $description,
'category' => $categoryData['categories'][$record->category_id]['name'],
'link_view' => $categoryData['allow_view'] ? $this->url()->fromRoute('knowledge-area/view', ['id' => $record->uuid]) : '',
];
// Add edit/delete links if user has permission for the content's category
if (in_array($record->category_id, $categoryData['category_with_edition_ids'])) {
$item['link_edit'] = $categoryData['allow_edit'] ? $this->url()->fromRoute('knowledge-area/edit', ['id' => $record->uuid]) : '';
$item['link_delete'] = $categoryData['allow_delete'] ? $this->url()->fromRoute('knowledge-area/delete', ['id' => $record->uuid]) : '';
}
array_push($items, $item);
}
return $items;
}
public function addAction()
{
$currentUserPlugin = $this->plugin('currentUserPlugin');
$currentUser = $currentUserPlugin->getUser();
$currentNetworkPlugin = $this->plugin('currentNetworkPlugin');
$currentNetwork = $currentNetworkPlugin->getNetwork();
$request = $this->getRequest();
if ($request->isPost()) {
$category_with_edition_ids = [];
$category_ids = [];
$categories = [];
$knowledgeAreaCategoryUserMapper = KnowledgeAreaCategoryUserMapper::getInstance($this->adapter);
$records = $knowledgeAreaCategoryUserMapper->fetchAllByUserId($currentUser->id);
foreach ($records as $record) {
if ($record->role == KnowledgeAreaCategoryUser::ROLE_ADMINISTRATOR || $record->role == KnowledgeAreaCategoryUser::ROLE_USER) {
array_push($category_with_edition_ids, $record->category_id);
}
array_push($category_ids, $record->category_id);
}
$knowledgeAreaCategoryMapper = KnowledgeAreaCategoryMapper::getInstance($this->adapter);
if ($category_ids) {
$records = $knowledgeAreaCategoryMapper->fetchAllByIds($category_ids);
foreach ($records as $record) {
if ($record->status == KnowledgeAreaCategory::STATUS_ACTIVE) {
$categories[$record->id] = [
'uuid' => $record->uuid,
'name' => $record->name,
];
}
}
}
$categories = array_values($categories);
usort($categories, function ($a, $b) {
return $a['name'] <=> $b['name'];
});
$dataPost = array_merge($request->getPost()->toArray(), $request->getFiles()->toArray());
$form = new KnowledgeAreaCreateForm($this->adapter, $category_with_edition_ids);
$form->setData($dataPost);
if ($form->isValid()) {
$dataPost = (array) $form->getData();
$knowledgeAreaCategoryMapper = KnowledgeAreaCategoryMapper::getInstance($this->adapter);
$knowledgeAreaCategory = $knowledgeAreaCategoryMapper->fetchOneByUuid($dataPost['category_id']);
$knowledgeAreaContent = new KnowledgeAreaContent();
$knowledgeAreaContent->network_id = $knowledgeAreaCategory->network_id;
$knowledgeAreaContent->company_id = $knowledgeAreaCategory->company_id;
$knowledgeAreaContent->category_id = $knowledgeAreaCategory->id;
$knowledgeAreaContent->user_id = $currentUser->id;
$knowledgeAreaContent->title = $dataPost['title'];
$knowledgeAreaContent->description = $dataPost['description'];
$knowledgeAreaContent->link = $dataPost['link'];
$knowledgeAreaContentMapper = KnowledgeAreaContentMapper::getInstance($this->adapter);
if ($knowledgeAreaContentMapper->insert($knowledgeAreaContent)) {
$knowledgeAreaContent = $knowledgeAreaContentMapper->fetchOne($knowledgeAreaContent->id);
// Se instancia la clase Storage para manejar el almacenamiento de archivos.
// La clase Image fue eliminada debido a que no se encontraba o estaba obsoleta.
$storage = Storage::getInstance($this->config, $this->adapter);
// Obtiene la ruta base para los archivos del área de conocimiento.
// $target_path = $storage->getPathKnowledgeArea(); // No longer needed directly here, composePathTo methods use storagePath internally
$storage->setFiles($request->getFiles()->toArray());
if($storage->setCurrentFilename('image')) {
$image_tmp_filename = $storage->getTmpFilename();
$image_filename = 'knowledge_image_' . $knowledgeAreaContent->uuid . '.' . $storage->getExtension();
$image_target_filename = $storage->composePathToFilename(Storage::TYPE_KNOWLEDGE_AREA, $knowledgeAreaContent->uuid, $image_filename);
list($target_width_str, $target_height_str) = explode('x', $this->config['leaderslinked.image_sizes.knowledge_area']);
$target_width = (int)$target_width_str;
$target_height = (int)$target_height_str;
if (!$storage->uploadImageResize($image_tmp_filename, $image_target_filename, $target_width, $target_height)) {
return new JsonModel([
'success' => false,
'data' => 'ERROR_IMAGE_UPLOAD'
]);
}
$knowledgeAreaContent->image = $image_filename;
}
if($storage->setCurrentFilename('attachment')) {
$attachment_tmp_filename = $storage->getTmpFilename();
$attachment_filename = 'knowledge_attachment_' . $knowledgeAreaContent->uuid . '.' . $storage->getExtension();
$attachment_target_filename = $storage->composePathToFilename(Storage::TYPE_KNOWLEDGE_AREA, $knowledgeAreaContent->uuid, $attachment_filename);
if (!$storage->moveUploadedFile($attachment_tmp_filename, $attachment_target_filename)) {
return new JsonModel([
'success' => false,
'data' => 'ERROR_IMAGE_UPLOAD'
]);
}
$knowledgeAreaContent->attachment = $attachment_filename;
}
if(!$knowledgeAreaContentMapper->update($knowledgeAreaContent)) {
return new JsonModel([
'success' => false,
'data' => $knowledgeAreaContentMapper->getError()
]);
}
$this->logger->info('Se agrego el contenido ' . $knowledgeAreaContent->title, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
$categoryData = $this->getAccessibleCategories($currentUser, $currentNetwork);
$path = $storage->getPathKnowledgeArea();
$knowledgeData = [
'uuid' => $knowledgeAreaContent->uuid,
'title' => $knowledgeAreaContent->title,
'description' => $knowledgeAreaContent->description,
'link' => $knowledgeAreaContent->link,
'image' => $storage->getGenericImage($path, $knowledgeAreaContent->uuid, $knowledgeAreaContent->image),
'attachment' => $storage->getGenericFile($path, $knowledgeAreaContent->uuid, $knowledgeAreaContent->attachment),
'category' => $knowledgeAreaCategory->name,
'link_view' => '',
'link_edit' => '',
'link_delete' => '',
];
if($categoryData['allow_view']) {
$knowledgeData['link_view'] = $this->url()->fromRoute('knowledge-area/view', ['id' => $knowledgeAreaContent->uuid]);
}
if(in_array($knowledgeAreaContent->category_id, $categoryData['category_with_edition_ids'])) {
if($categoryData['allow_edit']) {
$knowledgeData['link_edit'] = $this->url()->fromRoute('knowledge-area/edit', ['id' => $knowledgeAreaContent->uuid]);
}
if($categoryData['allow_delete']) {
$knowledgeData['link_delete'] = $this->url()->fromRoute('knowledge-area/delete', ['id' => $knowledgeAreaContent->uuid]);
}
}
$data = [
'success' => true,
'data' => [
'message' => 'LABEL_RECORD_ADDED',
'knowledge' => $knowledgeData
]
];
} else {
$data = [
'success' => false,
'data' => $knowledgeAreaContentMapper->getError()
];
}
return new JsonModel($data);
} else {
$messages = [];
$form_messages = (array) $form->getMessages();
foreach ($form_messages as $fieldname => $field_messages) {
$messages[$fieldname] = array_values($field_messages);
}
return new JsonModel([
'success' => false,
'data' => $messages
]);
}
} else {
$data = [
'success' => false,
'data' => 'ERROR_METHOD_NOT_ALLOWED'
];
return new JsonModel($data);
}
return new JsonModel($data);
}
public function deleteAction()
{
$currentUserPlugin = $this->plugin('currentUserPlugin');
$currentUser = $currentUserPlugin->getUser();
$request = $this->getRequest();
$id = $this->params()->fromRoute('id');
$knowledgeAreaContentMapper = KnowledgeAreaContentMapper::getInstance($this->adapter);
$knowledgeAreaContent = $knowledgeAreaContentMapper->fetchOneByUuid($id);
if (!$knowledgeAreaContent) {
return new JsonModel([
'success' => false,
'data' => 'ERROR_RECORD_NOT_FOUND'
]);
}
$knowledgeAreaCategoryUserMapper = KnowledgeAreaCategoryUserMapper::getInstance($this->adapter);
$knowledgeAreaCategoryUser = $knowledgeAreaCategoryUserMapper->fetchOneByCategoryIdAndUserId($knowledgeAreaContent->category_id, $currentUser->id);
$ok = false;
if ($knowledgeAreaCategoryUser) {
if ($knowledgeAreaCategoryUser->role == KnowledgeAreaCategoryUser::ROLE_EDITOR) {
$ok = $knowledgeAreaContent->user_id == $currentUser->id;
}
if ($knowledgeAreaCategoryUser->role == KnowledgeAreaCategoryUser::ROLE_ADMINISTRATOR) {
$ok = true;
}
}
if (!$ok) {
return new JsonModel([
'success' => false,
'data' => 'ERROR_KNOWLEDGE_AREA_YOU_DO_NOT_HAVE_PERMISSION'
]);
}
if ($request->isPost()) {
$result = $knowledgeAreaContentMapper->delete($knowledgeAreaContent);
if ($result) {
$this->logger->info('Se borro el cotenido : ' . $knowledgeAreaContent->title, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
$storage = Storage::getInstance($this->config, $this->adapter);
$target_path = $storage->getPathKnowledgeArea();
if($knowledgeAreaContent->attachment) {
$storage->deleteFile($target_path, $knowledgeAreaContent->uuid, $knowledgeAreaContent->attachment);
}
if ($knowledgeAreaContent->image) {
$storage->deleteFile($target_path, $knowledgeAreaContent->uuid, $knowledgeAreaContent->image);
}
$data = [
'success' => true,
'data' => 'LABEL_RECORD_DELETED'
];
} else {
$data = [
'success' => false,
'data' => $knowledgeAreaContentMapper->getError()
];
return new JsonModel($data);
}
} else {
$data = [
'success' => false,
'data' => 'ERROR_METHOD_NOT_ALLOWED'
];
return new JsonModel($data);
}
return new JsonModel($data);
}
public function editAction()
{
$currentUserPlugin = $this->plugin('currentUserPlugin');
$currentUser = $currentUserPlugin->getUser();
$request = $this->getRequest();
$id = $this->params()->fromRoute('id');
$knowledgeAreaContentMapper = KnowledgeAreaContentMapper::getInstance($this->adapter);
$knowledgeAreaContent = $knowledgeAreaContentMapper->fetchOneByUuid($id);
if (!$knowledgeAreaContent) {
return new JsonModel([
'success' => false,
'data' => 'ERROR_RECORD_NOT_FOUND'
]);
}
$knowledgeAreaCategoryUserMapper = KnowledgeAreaCategoryUserMapper::getInstance($this->adapter);
$knowledgeAreaCategoryUser = $knowledgeAreaCategoryUserMapper->fetchOneByCategoryIdAndUserId($knowledgeAreaContent->category_id, $currentUser->id);
$ok = false;
if ($knowledgeAreaCategoryUser) {
if ($knowledgeAreaCategoryUser->role == KnowledgeAreaCategoryUser::ROLE_EDITOR) {
$ok = $knowledgeAreaContent->user_id == $currentUser->id;
}
if ($knowledgeAreaCategoryUser->role == KnowledgeAreaCategoryUser::ROLE_ADMINISTRATOR) {
$ok = true;
}
}
if (!$ok) {
return new JsonModel([
'success' => false,
'data' => 'ERROR_KNOWLEDGE_AREA_YOU_DO_NOT_HAVE_PERMISSION'
]);
}
if ($request->isGet()) {
$knowledgeAreaCategoryMapper = KnowledgeAreaCategoryMapper::getInstance($this->adapter);
$knowledgeAreaCategory = $knowledgeAreaCategoryMapper->fetchOne($knowledgeAreaContent->category_id);
$data = [
'success' => true,
'data' => [
'category_id' => $knowledgeAreaCategory->uuid,
'title' => $knowledgeAreaContent->title,
'description' => $knowledgeAreaContent->description,
'link' => $knowledgeAreaContent->link,
]
];
return new JsonModel($data);
} else if ($request->isPost()) {
$category_with_edition_ids = [];
$category_ids = [];
$categories = [];
$knowledgeAreaCategoryUserMapper = KnowledgeAreaCategoryUserMapper::getInstance($this->adapter);
$records = $knowledgeAreaCategoryUserMapper->fetchAllByUserId($currentUser->id);
foreach ($records as $record) {
if ($record->role == KnowledgeAreaCategoryUser::ROLE_ADMINISTRATOR || $record->role == KnowledgeAreaCategoryUser::ROLE_USER) {
array_push($category_with_edition_ids, $record->category_id);
}
array_push($category_ids, $record->category_id);
}
$knowledgeAreaCategoryMapper = KnowledgeAreaCategoryMapper::getInstance($this->adapter);
if ($category_ids) {
$records = $knowledgeAreaCategoryMapper->fetchAllByIds($category_ids);
foreach ($records as $record) {
if ($record->status == KnowledgeAreaCategory::STATUS_ACTIVE) {
$categories[$record->id] = [
'uuid' => $record->uuid,
'name' => $record->name,
];
}
}
}
$categories = array_values($categories);
usort($categories, function ($a, $b) {
return $a['name'] <=> $b['name'];
});
$dataPost = array_merge($request->getPost()->toArray(), $request->getFiles()->toArray());
$form = new KnowledgeAreaEditForm($this->adapter, $category_with_edition_ids);
$form->setData($dataPost);
if ($form->isValid()) {
$dataPost = (array) $form->getData();
$knowledgeAreaCategoryMapper = KnowledgeAreaCategoryMapper::getInstance($this->adapter);
$knowledgeAreaCategory = $knowledgeAreaCategoryMapper->fetchOneByUuid($dataPost['category_id']);
$knowledgeAreaContent->category_id = $knowledgeAreaCategory->id;
$knowledgeAreaContent->title = $dataPost['title'];
$knowledgeAreaContent->description = $dataPost['description'];
if ($knowledgeAreaContentMapper->update($knowledgeAreaContent)) {
// Se instancia la clase Storage para manejar el almacenamiento de archivos.
$storage = Storage::getInstance($this->config, $this->adapter);
// Obtiene la ruta base para los archivos del área de conocimiento.
// $target_path = $storage->getPathKnowledgeArea(); // No longer needed directly here, composePathTo methods use storagePath internally
$files = $this->getRequest()->getFiles()->toArray();
// Manejo de la actualización de la imagen principal.
if (isset($files['image']) && empty($files['image']['error'])) {
// Si existe una imagen previa, se elimina.
if ($knowledgeAreaContent->image) {
$storage->deleteFile($target_path, $knowledgeAreaContent->uuid, $knowledgeAreaContent->image);
}
$tmp_image_name = $files['image']['tmp_name'];
$original_image_name_parts = explode('.', $files['image']['name']);
// Genera un nombre de archivo único y normalizado para la nueva imagen, forzando .png.
$final_image_filename = Functions::normalizeString(uniqid() . '-' . $original_image_name_parts[0] . '.png');
// Obtener dimensiones para el redimensionamiento de la imagen del área de conocimiento.
list($target_width_str, $target_height_str) = explode('x', $this->config['leaderslinked.image_sizes.knowledge_area']);
$target_width = (int)$target_width_str;
$target_height = (int)$target_height_str;
$uuid_path_segment = $knowledgeAreaCategory->uuid;
// Construye la ruta completa del directorio de destino.
$full_target_dir = $storage->composePathToDirectory(Storage::TYPE_KNOWLEDGE_AREA, $uuid_path_segment);
// Crea el directorio si no existe.
if (!is_dir($full_target_dir)) {
mkdir($full_target_dir, 0775, true);
}
$full_target_path_for_image = $storage->composePathToFilename(Storage::TYPE_KNOWLEDGE_AREA, $uuid_path_segment, $final_image_filename);
// Sube y redimensiona la imagen.
if ($storage->uploadImageResize($tmp_image_name, $full_target_path_for_image, $target_width, $target_height)) {
$knowledgeAreaContent->image = $final_image_filename;
$knowledgeAreaContentMapper->update($knowledgeAreaContent);
}
}
// Manejo de la actualización del archivo adjunto.
if (isset($files['attachment']) && empty($files['attachment']['error'])) {
$tmp_attachment_name = $files['attachment']['tmp_name'];
// Normaliza el nombre del nuevo archivo adjunto.
$final_attachment_filename = Functions::normalizeString($files['attachment']['name']);
// Si existe un adjunto previo, se elimina.
if ($knowledgeAreaContent->attachment) {
$storage->deleteFile($target_path, $knowledgeAreaContent->uuid, $knowledgeAreaContent->attachment);
}
// Construye la ruta completa del directorio de destino para el adjunto.
$uuid_path_segment = $knowledgeAreaCategory->uuid; // Similar al comentario de la imagen.
$full_target_dir_for_attachment = $target_path . DIRECTORY_SEPARATOR . $uuid_path_segment;
if (!is_dir($full_target_dir_for_attachment)) {
mkdir($full_target_dir_for_attachment, 0775, true);
}
$full_target_path_for_attachment = $full_target_dir_for_attachment . DIRECTORY_SEPARATOR . $final_attachment_filename;
// Mueve el nuevo archivo adjunto a su ubicación final.
if ($storage->moveUploadedFile($tmp_attachment_name, $full_target_path_for_attachment)) {
$knowledgeAreaContent->attachment = $final_attachment_filename;
$knowledgeAreaContentMapper->update($knowledgeAreaContent);
}
}
$this->logger->info('Se edito el contenido ' . $knowledgeAreaContent->title, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
$data = [
'success' => true,
'data' => 'LABEL_RECORD_UPDATED'
];
} else {
$data = [
'success' => false,
'data' => $knowledgeAreaContentMapper->getError()
];
}
return new JsonModel($data);
} else {
$messages = [];
$form_messages = (array) $form->getMessages();
foreach ($form_messages as $fieldname => $field_messages) {
$messages[$fieldname] = array_values($field_messages);
}
return new JsonModel([
'success' => false,
'data' => $messages
]);
}
} else {
$data = [
'success' => false,
'data' => 'ERROR_METHOD_NOT_ALLOWED'
];
return new JsonModel($data);
}
return new JsonModel($data);
}
public function viewAction()
{
$currentUserPlugin = $this->plugin('currentUserPlugin');
$currentUser = $currentUserPlugin->getUser();
$request = $this->getRequest();
$id = $this->params()->fromRoute('id');
$knowledgeAreaContentMapper = KnowledgeAreaContentMapper::getInstance($this->adapter);
$knowledgeAreaContent = $knowledgeAreaContentMapper->fetchOneByUuid($id);
if (!$knowledgeAreaContent) {
return new JsonModel([
'success' => false,
'data' => 'ERROR_RECORD_NOT_FOUND'
]);
}
$request = $this->getRequest();
if ($request->isGet()) {
$knowledgeAreaCategoryMapper = KnowledgeAreaCategoryMapper::getInstance($this->adapter);
$knowledgeAreaCategory = $knowledgeAreaCategoryMapper->fetchOne($knowledgeAreaContent->category_id);
$ok = false;
if ($knowledgeAreaCategory->privacy == KnowledgeAreaCategory::PRIVACY_COMPANY) {
$knowledgeAreaCategoryUserMapper = KnowledgeAreaCategoryUserMapper::getInstance($this->adapter);
$knowledgeAreaCategoryUser = $knowledgeAreaCategoryUserMapper->fetchOneByCategoryIdAndUserId($knowledgeAreaContent->category_id, $currentUser->id);
if ($knowledgeAreaCategoryUser) {
$ok = true;
}
}
if ($knowledgeAreaCategory->privacy == KnowledgeAreaCategory::PRIVACY_PUBLIC) {
$ok = true;
}
if (!$ok) {
return new JsonModel([
'success' => false,
'data' => 'ERROR_KNOWLEDGE_AREA_YOU_DO_NOT_HAVE_PERMISSION'
]);
}
$contentReactionMapper = ContentReactionMapper::getInstance($this->adapter);
$contentReaction = $contentReactionMapper->fetchOneByKnowledgeAreaIdAndUserId($knowledgeAreaContent->id, $currentUser->id);
$storage = Storage::getInstance($this->config, $this->adapter);
$path = $storage->getPathKnowledgeArea();
return new JsonModel([
'success' => true,
'data' => [
'category' => $knowledgeAreaCategory->name,
'title' => $knowledgeAreaContent->title,
'description' => $knowledgeAreaContent->description,
'link' => $knowledgeAreaContent->link,
'image' => $storage->getGenericImage($path, $knowledgeAreaContent->uuid, $knowledgeAreaContent->image),
'attachment' => $knowledgeAreaContent->attachment ? $storage->getGenericFile($path, $knowledgeAreaContent->uuid, $knowledgeAreaContent->attachment) : '',
'reaction' => $contentReaction ? $contentReaction->reaction : '',
'routeComments' => $this->url()->fromRoute('knowledge-area/comments', ['id' => $knowledgeAreaContent->uuid],['force_canonical' => true]),
'routeCommentAdd' => $this->url()->fromRoute('knowledge-area/comments/add', ['id' => $knowledgeAreaContent->uuid],['force_canonical' => true]),
'routeSaveReaction' => $this->url()->fromRoute('knowledge-area/save-reaction', ['id' => $knowledgeAreaContent->uuid],['force_canonical' => true]),
'routeDeleteReaction' => $this->url()->fromRoute('knowledge-area/delete-reaction', ['id' => $knowledgeAreaContent->uuid],['force_canonical' => true]),
'routeReactions' => $this->url()->fromRoute('knowledge-area/reactions', ['id' => $knowledgeAreaContent->uuid],['force_canonical' => true]),
]
]);
} else {
$data = [
'success' => false,
'data' => 'ERROR_METHOD_NOT_ALLOWED'
];
return new JsonModel($data);
}
return new JsonModel($data);
}
public function reactionsAction()
{
$id = $this->params()->fromRoute('id');
$request = $this->getRequest();
$request = $this->getRequest();
if ($request->isGet()) {
$currentUserPlugin = $this->plugin('currentUserPlugin');
$currentUser = $currentUserPlugin->getUser();
$knowledgeAreaContentMapper = KnowledgeAreaContentMapper::getInstance($this->adapter);
$knowledgeAreaContent = $knowledgeAreaContentMapper->fetchOneByUuid($id);
if (!$knowledgeAreaContent || $knowledgeAreaContent->network_id != $currentUser->network_id) {
return new JsonModel([
'success' => false,
'data' => 'ERROR_RECORD_NOT_FOUND'
]);
}
$userMapper = UserMapper::getInstance($this->adapter);
$items = [];
$storage = Storage::getInstance($this->config, $this->adapter);
$path = $storage->getPathKnowledgeArea();
$contentReactionMapper = ContentReactionMapper::getInstance($this->adapter);
$records = $contentReactionMapper->fetchAllByKnowledgeAreaId($knowledgeAreaContent->id);
foreach($records as $record)
{
$user = $userMapper->fetchOne($record->user_id);
if($user && $user->status == User::STATUS_ACTIVE) {
array_push($items, [
'first_name' => $user->first_name,
'last_name' => $user->last_name,
'email' => $user->email,
'image' => $storage->getGenericImage($path, $user->uuid, $user->image),
'reaction' => $record->reaction,
]);
}
}
$response = [
'success' => true,
'data' => $items
];
return new JsonModel($response);
} else {
$response = [
'success' => false,
'data' => 'ERROR_METHOD_NOT_ALLOWED'
];
return new JsonModel($response);
}
}
public function addCommentAction()
{
$currentUserPlugin = $this->plugin('currentUserPlugin');
$currentUser = $currentUserPlugin->getUser();
$id = $this->params()->fromRoute('id');
$request = $this->getRequest();
if ($request->isPost()) {
$knowledgeAreaContentMapper = KnowledgeAreaContentMapper::getInstance($this->adapter);
$knowledgeAreaContent = $knowledgeAreaContentMapper->fetchOneByUuid($id);
if (!$knowledgeAreaContent || $knowledgeAreaContent->network_id != $currentUser->network_id) {
return new JsonModel([
'success' => false,
'data' => 'ERROR_RECORD_NOT_FOUND'
]);
}
$dataPost = $request->getPost()->toArray();
$form = new CommentForm();
$form->setData($dataPost);
if ($form->isValid()) {
$dataPost = (array) $form->getData();
$comment = new Comment();
$comment->network_id = $currentUser->network_id;
$comment->comment = $dataPost['comment'];
$comment->user_id = $currentUser->id;
$comment->knowledge_area_id = $knowledgeAreaContent->id;
$comment->relational = Comment::RELATIONAL_KNOWLEDGE_AREA;
$commentMapper = CommentMapper::getInstance($this->adapter);
$now = $commentMapper->getDatebaseNow();
if ($commentMapper->insert($comment)) {
$total_comments = $commentMapper->fetchCountCommentByKnowledgeAreaId($comment->knowledge_area_id);
$knowledgeAreaContent->total_comments = $total_comments;
$knowledgeAreaContentMapper->update($knowledgeAreaContent);
$response = [
'success' => true,
'data' => $this->renderComment($comment->id, $now),
'total_comments' => $total_comments
];
return new JsonModel($response);
} else {
$response = [
'success' => false,
'data' => $commentMapper->getError()
];
return new JsonModel($response);
}
} else {
$message = '';;
$form_messages = (array) $form->getMessages();
foreach ($form_messages as $fieldname => $field_messages) {
foreach ($field_messages as $key => $value) {
$message = $value;
}
}
$response = [
'success' => false,
'data' => $message
];
return new JsonModel($response);
}
} else {
$response = [
'success' => false,
'data' => 'ERROR_METHOD_NOT_ALLOWED'
];
return new JsonModel($response);
}
}
public function deleteCommentAction()
{
$currentUserPlugin = $this->plugin('currentUserPlugin');
$currentUser = $currentUserPlugin->getUser();
$request = $this->getRequest();
if ($request->isPost()) {
$id = $this->params()->fromRoute('id');
$comment = $this->params()->fromRoute('comment');
$knowledgeAreaContentMapper = KnowledgeAreaContentMapper::getInstance($this->adapter);
$knowledgeAreaContent = $knowledgeAreaContentMapper->fetchOneByUuid($id);
if ($knowledgeAreaContent && $knowledgeAreaContent->network_id == $currentUser->network_id) {
$commentMapper = CommentMapper::getInstance($this->adapter);
$comment = $commentMapper->fetchOneByUuid($comment);
if ($comment && $comment->knowledge_area_id == $knowledgeAreaContent->id && $comment->user_id == $currentUser->id) {
$comment->status = Comment::STATUS_DELETED;
if ($commentMapper->update($comment)) {
$total_comments = $commentMapper->fetchCountCommentByKnowledgeAreaId($knowledgeAreaContent->id);
$knowledgeAreaContent->total_comments = $total_comments;
$knowledgeAreaContentMapper->update($knowledgeAreaContent);
$response = [
'success' => true,
'data' => 'LABEL_COMMENT_WAS_DELETED',
'total_comments' => $total_comments
];
} else {
$response = [
'success' => false,
'data' => $commentMapper->getError()
];
}
} else {
$response = [
'success' => false,
'data' => 'ERROR_COMMENT_NOT_FOUND'
];
}
} else {
$response = [
'success' => false,
'data' => 'ERROR_COMMENT_NOT_FOUND'
];
}
} else {
$response = [
'success' => false,
'data' => 'ERROR_METHOD_NOT_ALLOWED'
];
}
return new JsonModel($response);
}
public function saveReactionAction()
{
$currentUserPlugin = $this->plugin('currentUserPlugin');
$currentUser = $currentUserPlugin->getUser();
$id = $this->params()->fromRoute('id');
$reaction = $this->params()->fromPost('reaction');
$request = $this->getRequest();
if ($request->isPost()) {
$knowledgeAreaContentMapper = KnowledgeAreaContentMapper::getInstance($this->adapter);
$knowledgeAreaContent = $knowledgeAreaContentMapper->fetchOneByUuid($id);
if (!$knowledgeAreaContent || $knowledgeAreaContent->network_id != $currentUser->network_id) {
return new JsonModel([
'success' => false,
'data' => 'ERROR_RECORD_NOT_FOUND'
]);
}
$reactions = [
ContentReaction::REACTION_RECOMMENDED,
ContentReaction::REACTION_SUPPORT,
ContentReaction::REACTION_LOVE,
ContentReaction::REACTION_INTEREST,
ContentReaction::REACTION_FUN
];
if(!in_array($reaction, $reactions)) {
$response = [
'success' => false,
'data' => 'ERROR_REACTION_NOT_FOUND'
];
return new JsonModel($response);
}
$contentReactionMapper = ContentReactionMapper::getInstance($this->adapter);
$contentReaction = $contentReactionMapper->fetchOneByKnowledgeAreaIdAndUserId($knowledgeAreaContent->id, $currentUser->id);
if ($contentReaction) {
$contentReaction->reaction = $reaction;
$result = $contentReactionMapper->update($contentReaction);
} else {
$contentReaction = new ContentReaction();
$contentReaction->user_id = $currentUser->id;
$contentReaction->knowledge_area_id = $knowledgeAreaContent->id;
$contentReaction->relational = ContentReaction::RELATIONAL_KNOWLEDGE_AREA;
$contentReaction->reaction = $reaction;
$result = $contentReactionMapper->insert($contentReaction);
}
if ($result) {
$reactions = $contentReactionMapper->fetchCountByKnowledgeAreaId($knowledgeAreaContent->id);
$response = [
'success' => true,
'data' => [
'reactions' => $reactions
]
];
} else {
$response = [
'success' => false,
'data' => $contentReactionMapper->getError()
];
}
return new JsonModel($response);
}
$response = [
'success' => false,
'data' => 'ERROR_METHOD_NOT_ALLOWED'
];
return new JsonModel($response);
}
public function deleteReactionAction()
{
$currentUserPlugin = $this->plugin('currentUserPlugin');
$currentUser = $currentUserPlugin->getUser();
$id = $this->params()->fromRoute('id');
$request = $this->getRequest();
if ($request->isPost()) {
$knowledgeAreaContentMapper = KnowledgeAreaContentMapper::getInstance($this->adapter);
$knowledgeAreaContent = $knowledgeAreaContentMapper->fetchOneByUuid($id);
if (!$knowledgeAreaContent || $knowledgeAreaContent->network_id != $currentUser->network_id) {
return new JsonModel([
'success' => false,
'data' => 'ERROR_RECORD_NOT_FOUND'
]);
}
$contentReactionMapper = ContentReactionMapper::getInstance($this->adapter);
$contentReaction = $contentReactionMapper->fetchOneByKnowledgeAreaIdAndUserId($knowledgeAreaContent->id, $currentUser->id);
if (!$contentReaction) {
$response = [
'success' => false,
'data' => 'ERROR_DUPLICATE_ACTION'
];
return new JsonModel($response);
}
if ($contentReactionMapper->deleteByKnowledgeAreaIdAndUserId($knowledgeAreaContent->id, $currentUser->id)) {
$reactions = $contentReactionMapper->fetchCountByKnowledgeAreaId($knowledgeAreaContent->id);
$response = [
'success' => true,
'data' => [
'reactions' => $reactions
]
];
} else {
$response = [
'success' => false,
'data' => $contentReactionMapper->getError()
];
}
return new JsonModel($response);
}
$response = [
'success' => false,
'data' => 'ERROR_METHOD_NOT_ALLOWED'
];
return new JsonModel($response);
}
public function commentsAction()
{
$id = $this->params()->fromRoute('id');
$request = $this->getRequest();
if ($request->isGet()) {
$currentUserPlugin = $this->plugin('currentUserPlugin');
$currentUser = $currentUserPlugin->getUser();
$knowledgeAreaContentMapper = KnowledgeAreaContentMapper::getInstance($this->adapter);
$now = $knowledgeAreaContentMapper->getDatebaseNow();
$knowledgeAreaContent = $knowledgeAreaContentMapper->fetchOneByUuid($id);
if (!$knowledgeAreaContent || $knowledgeAreaContent->network_id != $currentUser->network_id) {
return new JsonModel([
'success' => false,
'data' => 'ERROR_RECORD_NOT_FOUND'
]);
}
$commentMapper = CommentMapper::getInstance($this->adapter);
$records = $commentMapper->fetchAllPublishedByKnowledgeAreaId($knowledgeAreaContent->id);
$comments = [];
foreach ($records as $record) {
$comment = $this->renderComment($record->id, $now);
array_push($comments, $comment);
}
$response = [
'success' => true,
'data' => $comments
];
return new JsonModel($response);
} else {
$response = [
'success' => false,
'data' => 'ERROR_METHOD_NOT_ALLOWED'
];
return new JsonModel($response);
}
}
private function renderComment($comment_id, $now)
{
$item = [];
$commentMapper = CommentMapper::getInstance($this->adapter);
$record = $commentMapper->fetchOne($comment_id);
$knowledgeAreaContentMapper = KnowledgeAreaContentMapper::getInstance($this->adapter);
$knowledgeAreaContent = $knowledgeAreaContentMapper->fetchOne($record->knowledge_area_id);
if ($record) {
$storage = Storage::getInstance($this->config, $this->adapter);
$userMapper = UserMapper::getInstance($this->adapter);
$user = $userMapper->fetchOne($record->user_id);
$item['unique'] = uniqid();
$item['user_image'] = $storage->getUserImage($user);
$item['user_url'] = $this->url()->fromRoute('profile/view', ['id' => $user->uuid]);
$item['user_name'] = $user->first_name . ' ' . $user->last_name;
$item['time_elapsed'] = Functions::timeAgo($record->added_on, $now);
$item['comment'] = $record->comment;
$item['link_delete'] = $this->url()->fromRoute('knowledge-area/comments/delete', ['id' => $knowledgeAreaContent->uuid, 'comment' => $record->uuid]);
}
return $item;
}
}