Rev 11151 | AutorÃa | Ultima modificación | Ver Log |
<?php
declare(strict_types=1);
namespace LeadersLinked\Controller;
use Laminas\Db\Adapter\AdapterInterface;
use Laminas\Cache\Storage\Adapter\AbstractAdapter;
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\MyTrainerCategoriesMapper;
use LeadersLinked\Mapper\TopicMapper;
use LeadersLinked\Mapper\MyTrainerQuestionsMapper;
use LeadersLinked\Mapper\FeedMapper;
use LeadersLinked\Form\MyTrainerQuestionForm;
use LeadersLinked\Form\CreateFeedForm;
use LeadersLinked\Model\MyTrainerQuestions;
use LeadersLinked\Model\Feed;
use LeadersLinked\Mapper\CompanyMapper;
use LeadersLinked\Model\Company;
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
class MyTrainerQuestionController extends AbstractActionController {
/**
*
* @var AdapterInterface
*/
private $adapter;
/**
*
* @var AbstractAdapter
*/
private $cache;
/**
*
* @var LoggerInterface
*/
private $logger;
/**
*
* @var array
*/
private $config;
/**
*
* @param AdapterInterface $adapter
* @param AbstractAdapter $cache
* @param LoggerInterface $logger
* @param array $config
*/
public function __construct($adapter, $cache, $logger, $config) {
$this->adapter = $adapter;
$this->cache = $cache;
$this->logger = $logger;
$this->config = $config;
}
public function indexAction() {
$request = $this->getRequest();
$currentUserPlugin = $this->plugin('currentUserPlugin');
$currentCompany = $currentUserPlugin->getCompany();
$currentUser = $currentUserPlugin->getUser();
try{
$request = $this->getRequest();
if($request->isGet()) {
$headers = $request->getHeaders();
$isJson = false;
if($headers->has('Accept')) {
$accept = $headers->get('Accept');
$prioritized = $accept->getPrioritized();
foreach($prioritized as $key => $value) {
$raw = trim($value->getRaw());
if(!$isJson) {
$isJson = strpos($raw, 'json');
}
}
}
$acl = $this->getEvent()->getViewModel()->getVariable('acl');
$allowAdd = $acl->isAllowed($currentUser->usertype_id, 'my-trainer/question/add');
$allowEdit = $acl->isAllowed($currentUser->usertype_id, 'my-trainer/question/edit');
$allowDelete = $acl->isAllowed($currentUser->usertype_id, 'my-trainer/question/delete');
$page = $this->params()->fromQuery('page', 1);
$perpage = $this->params()->fromQuery('perpage', 20);
$form = new CreateFeedForm($this->adapter);
$questionsMapper = FeedMapper::getInstance($this->adapter);
$questions = $questionsMapper->fetchAllByMytQuestion();
// $posts;
$viewModel = new ViewModel();
$this->layout()->setTemplate('layout/layout-backend');
$viewModel->setTemplate('leaders-linked/my-trainer-question/index.phtml');
$viewModel->setVariables([
'form' => $form,
'page' => $page,
'perpage' => $perpage,
'questions' => $questions,
]);
return $viewModel;
} else {
return new JsonModel([
'success' => false,
'data' => 'ERROR_METHOD_NOT_ALLOWED'
]);
}
} catch (\Throwable $e) {
$e->getMessage();
return new JsonModel([
'success' => false,
'data' => $e
]);
}
}
public function addAction() {
$currentUserPlugin = $this->plugin('currentUserPlugin');
$currentCompany = $currentUserPlugin->getCompany();
$currentUser = $currentUserPlugin->getUser();
$request = $this->getRequest();
try{
if($request->isPost()) {
$form = new CreateFeedForm($this->adapter);
$dataPost = $request->getPost()->toArray();
$form->setData($dataPost);
if($form->isValid()) {
$dataPost = (array) $form->getData();
$hydrator = new ObjectPropertyHydrator();
$feed = new Feed();
$hydrator->hydrate($dataPost, $feed);
$feed->user_id = $currentUser->id;
$feed->company_id = $currentCompany->id;
$feed->user_id = $currentUser->id;
$feed->type = Feed::TYPE_MYT_QUESTION;
$feed->posted_or_shared = Feed::POSTED;
$feed->shared_with = Feed::SHARE_WITH_PUBLIC;
$feed->total_comments = 0;
$feed->total_shared = 0;
$categoriesMapper = TopicMapper::getInstance($this->adapter);
$category = $categoriesMapper->fetchOneByUuid($dataPost['category_id']);
$feed->topic_id = $category->id;
$feedMapper = FeedMapper::getInstance($this->adapter);
if($feedMapper->insert($feed)) {
$feed = $feedMapper->fetchOne($feed->id);
$feed->myt_id = $feed->id;
$feed->related_feed = $feed->id;
$feedMapper->update($feed);
$this->logger->info('Se agrego la pregunta ' . $feed->title, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
$data = [
'success' => true,
'data' => 'LABEL_RECORD_ADDED'
];
} else {
$data = [
'success' => false,
'data' => $feedMapper->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);
}
}
} else {
$data = [
'success' => false,
'data' => 'ERROR_METHOD_NOT_ALLOWED'
];
return new JsonModel($data);
}
} catch (\Throwable $e) {
$e->getMessage();
return new JsonModel([
'success' => false,
'data' => $e
]);
}
}
public function editAction() {
$request = $this->getRequest();
$currentUserPlugin = $this->plugin('currentUserPlugin');
$currentCompany = $currentUserPlugin->getCompany();
$currentUser = $currentUserPlugin->getUser();
$request = $this->getRequest();
$myt_id = $this->params()->fromRoute('id');
try{
if (!$myt_id) {
$data = [
'success' => false,
'data' => 'ERROR_INVALID_PARAMETER'
];
return new JsonModel($data);
}
$feedMapper = FeedMapper::getInstance($this->adapter);
$feed = $feedMapper->fetchOneByUuid($myt_id);
if (!$feed) {
$data = [
'success' => false,
'data' => 'ERROR_RECORD_NOT_FOUND'
];
return new JsonModel($data);
}
if ($feed->company_id != $currentCompany->id) {
return new JsonModel([
'success' => false,
'data' => 'ERROR_UNAUTHORIZED'
]);
}
if ($request->isPost()) {
$form = new CreateFeedForm($this->adapter);
$dataPost = $request->getPost()->toArray();
$form->setData($dataPost);
if ($form->isValid()) {
$dataPost = (array) $form->getData();
$hydrator = new ObjectPropertyHydrator();
$hydrator->hydrate($dataPost, $feed);
$categoriesMapper = TopicMapper::getInstance($this->adapter);
$category = $categoriesMapper->fetchOneByUuid($dataPost['category_id']);
$feed->topic_id = $category->id;
if($feedMapper->update($feed)) {
$feed = $feedMapper->fetchOne($feed->id);
$this->logger->info('Se modifico la pregunta ' . $feed->title, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
$data = [
'success' => true,
'data' => 'LABEL_RECORD_UPDATED'
];
} else {
$data = [
'success' => false,
'data' => $feedMapper->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 if ($request->isGet()) {
$hydrator = new ObjectPropertyHydrator();
$categoriesMapper = TopicMapper::getInstance($this->adapter);
$category = $categoriesMapper->fetchOne($feed->topic_id);
$data = [
'success' => true,
'data' => [
'id' => $feed->uuid,
'category_id' => $category->uuid,
'title' => $feed->title,
'description' => $feed->description,
]
];
return new JsonModel($data);
} else {
$data = [
'success' => false,
'data' => 'ERROR_METHOD_NOT_ALLOWED'
];
return new JsonModel($data);
}
return new JsonModel($data);
} catch (\Throwable $e) {
$e->getMessage();
return new JsonModel([
'success' => false,
'data' => $e
]);
}
}
public function deleteAction() {
$request = $this->getRequest();
$currentUserPlugin = $this->plugin('currentUserPlugin');
$currentCompany = $currentUserPlugin->getCompany();
$currentUser = $currentUserPlugin->getUser();
$request = $this->getRequest();
$uuid = $this->params()->fromRoute('id');
if (!$uuid) {
$data = [
'success' => false,
'data' => 'ERROR_INVALID_PARAMETER'
];
return new JsonModel($data);
}
$feedMapper = FeedMapper::getInstance($this->adapter);
$feed = $feedMapper->fetchOneByUuid($uuid);
if (!$feed) {
$data = [
'success' => false,
'data' => 'ERROR_RECORD_NOT_FOUND'
];
return new JsonModel($data);
}
if ($feed->company_id != $currentCompany->id) {
return new JsonModel([
'success' => false,
'data' => 'ERROR_UNAUTHORIZED'
]);
}
if ($request->isPost()) {
$result = $feedMapper->delete($feed->id);
if ($result) {
$this->logger->info('Se borro la pregunta ' . $feed->title, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
$data = [
'success' => true,
'data' => 'LABEL_RECORD_DELETED'
];
} else {
$data = [
'success' => false,
'data' => $feedMapper->getError()
];
return new JsonModel($data);
}
} else {
$data = [
'success' => false,
'data' => 'ERROR_METHOD_NOT_ALLOWED'
];
return new JsonModel($data);
}
return new JsonModel($data);
}
}