Rev 12961 | 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\FeedTopicMapper;
use LeadersLinked\Mapper\MyTrainerQuestionsMapper;
use LeadersLinked\Mapper\QueryMapper;
use LeadersLinked\Mapper\FeedMapper;
use LeadersLinked\Form\MyTrainerQuestionForm;
use LeadersLinked\Form\CreateFeedForm;
use LeadersLinked\Model\MyTrainerQuestions;
use LeadersLinked\Model\FeedTopic;
use LeadersLinked\Model\Feed;
use LeadersLinked\Model\Topic;
use LeadersLinked\Mapper\CompanyMapper;
use LeadersLinked\Model\Company;
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
class MyTrainerQuestionViewController 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();
$id = $this->params()->fromRoute('id');
$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');
}
}
}
$topicMapper = TopicMapper::getInstance($this->adapter);
$topic = $topicMapper->fetchOneByUUid($id);
$topicsMapper = FeedTopicMapper::getInstance($this->adapter);
$feedTopics = $topicsMapper->fetchAllByTopicId($topic->id);
foreach ($feedTopics as $feed) {
$questionsMapper = FeedMapper::getInstance($this->adapter);
if ($question = $questionsMapper->fetchOne($feed->feed_id)) {
$questions [] = $question;
}
}
$topics = [];
foreach ($questions as $question) {
$topicRelationships = $topicsMapper->fetchAllByFeedId($question->id);
$question->topics = [];
foreach ($topicRelationships as $rel) {
$topic = $topicMapper->fetchOne($rel->topic_id);
if ($topic) {
$question->topics []= $topic;
}
}
}
$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');
$allowAnswer = $acl->isAllowed($currentUser->usertype_id, 'my-trainer/question/answer');
$page = $this->params()->fromQuery('page', 1);
$perpage = $this->params()->fromQuery('perpage', 20);
$form = new CreateFeedForm($this->adapter);
$viewModel = new ViewModel();
$this->layout()->setTemplate('layout/layout-backend');
$viewModel->setTemplate('leaders-linked/my-trainer-question-view/index.phtml');
$viewModel->setVariables([
'form' => $form,
'id' => $id,
'page' => $page,
'perpage' => $perpage,
'questions' => $questions,
// 'categories' => $categories
]);
return $viewModel;
} else {
return new JsonModel([
'success' => false,
'data' => 'ERROR_METHOD_NOT_ALLOWED'
]);
}
}
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();
$categoriesId = $dataPost['category_id'] ?? [];
// return new JsonModel([
// 'success' => false,
// 'data' => $categoriesId
// ]);
$dataPost['category_id'] = null;
$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;
$feedMapper = FeedMapper::getInstance($this->adapter);
if($feedMapper->insert($feed)) {
$feed = $feedMapper->fetchOne($feed->id);
$category = new FeedTopic();
if(!empty($categoriesId)){
// print_r($dataPost['job_description_id']);
$categoriesMapper = TopicMapper::getInstance($this->adapter);
$feedTopicMapper = FeedTopicMapper::getInstance($this->adapter);
$ok = true;
foreach($categoriesId as $category_id) {
// echo '$jobDescriptionUuid = ' . $jobDescriptionUuid ;
$category = $categoriesMapper->fetchOneByUuidOrTitle($category_id);
if(!$category){
$category = new Topic;
$category->title = $category_id;
$category->type='myt';
$categoriesMapper->insert($category);
}
$record = new FeedTopic();
$record->topic_id = $category->id;
$record->feed_id = $feed->id;
$result = $feedTopicMapper->insert($record);
$ok = $ok && $result;
}
}
$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 deleteAction() {
$currentUserPlugin = $this->plugin('currentUserPlugin');
$currentUser = $currentUserPlugin->getUser();
$currentCompany = $currentUserPlugin->getCompany();
$request = $this->getRequest();
if($request->isPost()) {
$id = $this->params()->fromRoute('id');
$feedMapper = FeedMapper::getInstance($this->adapter);
$feed = $feedMapper->fetchOneByUuid($id);
if(!$feed) {
$response = [
'success' => false,
'data' => 'ERROR_POST_NOT_FOUND'
];
return new JsonModel($response);
}
if($feed->company_id != $currentCompany->id) {
$response = [
'success' => false,
'data' => 'ERROR_UNAUTHORIZED'
];
return new JsonModel($response);
}
$feedMapper = FeedMapper::getInstance($this->adapter);
$feed->status = Feed::STATUS_DELETED;
if($feedMapper->update($feed)) {
$response = [
'success' => true,
'data' => 'LABEL_FEED_WAS_DELETED'
];
} else {
$response = [
'success' => false,
'data' => $feedMapper->getError()
];
}
return new JsonModel($response);
} else {
$response = [
'success' => false,
'data' => 'ERROR_METHOD_NOT_ALLOWED'
];
}
return new JsonModel($response);
}
}