Rev 14740 | 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\Mapper\CompanyMapper;
use LeadersLinked\Mapper\CompanyUserMapper;
use LeadersLinked\Form\CreateFeedForm;
use LeadersLinked\Form\CommentForm;
use LeadersLinked\Form\CommentAnswerForm;
use LeadersLinked\Model\Comment;
use LeadersLinked\Mapper\CommentMapper;
use LeadersLinked\Mapper\FeedMapper;
use LeadersLinked\Mapper\GroupMapper;
use LeadersLinked\Model\Feed;
use LeadersLinked\Mapper\QueryMapper;
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
use LeadersLinked\Library\Image;
use LeadersLinked\Model\VideoConvert;
use LeadersLinked\Mapper\VideoConvertMapper;
use LeadersLinked\Mapper\LikeMapper;
use Laminas\Paginator\Adapter\DbSelect;
use Laminas\Paginator\Paginator;
use LeadersLinked\Mapper\UserMapper;
use LeadersLinked\Library\Functions;
use LeadersLinked\Model\Company;
use LeadersLinked\Model\UserType;
use Laminas\Authentication\AuthenticationService;
use Laminas\Authentication\Result as AuthResult;
use Laminas\Mvc\I18n\Translator;
use LeadersLinked\Mapper\MyTrainerQuestionsMapper;
use LeadersLinked\Mapper\FeedTopicMapper;
use LeadersLinked\Mapper\TopicMapper;
use LeadersLinked\Form\MyTrainerQuestionForm;
use LeadersLinked\Model\MyTrainerQuestions;
use LeadersLinked\Model\FeedTopic;
use LeadersLinked\Form\TopicForm;
use LeadersLinked\Mapper\UtilMapper;
class MyTrainerAnswerController 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()
{
$currentUserPlugin = $this->plugin('currentUserPlugin');
$currentUser = $currentUserPlugin->getUser();
$currentCompany = $currentUserPlugin->getCompany();
$id = $this->params()->fromRoute('id');
$feedMapper = FeedMapper::getInstance($this->adapter);
$title = $feedMapper->fetchOneByUuid($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');
}
}
}
$form = new CreateFeedForm($this->adapter);
$tagsMapper = TopicMapper::getInstance($this->adapter);
$topicsMapper = FeedTopicMapper::getInstance($this->adapter);
$feedTopic = $topicsMapper->fetchOneByFeedId($title->id);
$answer = $feedMapper->fetchAllByMytAnswer($title->related_feed);
$topicRelationships = $topicsMapper->fetchAllByTopicId($feedTopic->topic_id);
$tags = $topicsMapper->fetchAllByFeedId($title->id);
foreach ($topicRelationships as $rel) {
if ($question = $feedMapper->fetchOne($rel->feed_id)) {
$questions [] = $question;
}
}
foreach($tags as $tel){
$badge = $tagsMapper->fetchOne($tel->topic_id);
$badges [] = $badge;
}
$this->layout()->setTemplate('layout/layout-backend');
$viewModel = new ViewModel();
$viewModel->setTemplate('leaders-linked/my-trainer-answer/index.phtml');
$viewModel->setVariables([
'form' => $form,
'id' => $id,
'title_title' => $title->title,
'title_description' => $title->description,
'questions' => $questions,
'badges' => $badges,
'date' => $title->added_on,
'answer' => $answer,
]);
return $viewModel ;
} else {
return new JsonModel([
'success' => false,
'data' => 'ERROR_METHOD_NOT_ALLOWED'
]);
}
}
public function timelineAction(){
$currentUserPlugin = $this->plugin('currentUserPlugin');
$currentUser = $currentUserPlugin->getUser();
$currentCompany = $currentUserPlugin->getCompany();
$request = $this->getRequest();
if($request->isGet()) {
$feed_uuid = $this->params()->fromRoute('id');
if(!isset($feed_uuid)){
$data = [
'success' => false,
'data' => 'ERROR_INVALID_PARAMETER'
];
return new JsonModel($data);
}
$items = [];
$feedMapper = FeedMapper::getInstance($this->adapter);
$feed = $feedMapper->fetchOneByUuid($feed_uuid);
$feeds = $feedMapper->fetchAllByMytAnswer($feed->related_feed);
if (!$feeds) {
$data = [
'success' => false,
'data' => 'ERROR_RECORD_NOT_FOUND'
];
return new JsonModel($data);
}
$items = [];
$utilMapper = UtilMapper::getInstance($this->adapter);
$now = $utilMapper->getDatebaseNow();
foreach($feeds as $solution)
{
$items = $this->renderFeed($solution->id, $now);
}
$response = [
'success' => true,
'data' => [
'items' =>$items,
'feed_title'=>$feed->title,
'topic_title'=>$feed->description
]
];
return new JsonModel($response);
}
}
public function feedAction() {
$currentUserPlugin = $this->plugin('currentUserPlugin');
$currentUser = $currentUserPlugin->getUser();
$currentCompany = $currentUserPlugin->getCompany();
$request = $this->getRequest();
if($request->isGet()) {
$page = (int) $this->params()->fromQuery('page');
$myt_id = $this->params()->fromRoute('id');
$feedMapper = FeedMapper::getInstance($this->adapter);
$myt = $feedMapper->fetchOneByUuid($myt_id);
$queryMapper = QueryMapper::getInstance($this->adapter);
$select = $queryMapper->getSql()->select(FeedMapper::_TABLE);
$select->columns(['id']);
$select->where->equalTo('status', Feed::STATUS_PUBLISHED);
$select->where->equalTo('company_id',$currentCompany->id);
$select->where->equalTo('related_feed', $myt->id);
$select->where->and->equalTo('type', Feed::TYPE_MYT_ANSWER);
$select->order('added_on desc');
$dbSelect = new DbSelect($select, $this->adapter);
$paginator = new Paginator($dbSelect);
$paginator->setCurrentPageNumber($page ? $page : 1);
$paginator->setItemCountPerPage(10);
$items = [];
$feeds = $paginator->getCurrentItems();
$utilMapper = UtilMapper::getInstance($this->adapter);
$now = $utilMapper->getDatebaseNow();
foreach($feeds as $feed)
{
$items[] = $this->renderFeed($feed->id, $now);
}
$response = [
'success' => true,
'data' => [
'total' => [
'count' => $paginator->getTotalItemCount(),
'pages' => $paginator->getPages()->pageCount,
],
'current' => [
'items' => $items,
'page' => $paginator->getCurrentPageNumber(),
'count' => $paginator->getCurrentItemCount(),
]
]
];
return new JsonModel($response);
}
$response = [
'success' => false,
'data' => 'ERROR_METHOD_NOT_ALLOWED'
];
return new JsonModel($response);
}
public function addAction() {
$currentUserPlugin = $this->plugin('currentUserPlugin');
$currentCompany = $currentUserPlugin->getCompany();
$currentUser = $currentUserPlugin->getUser();
$id = $this->params()->fromRoute('id');
$questionsMapper = FeedMapper::getInstance($this->adapter);
$question = $questionsMapper->fetchOneByUuid($id);
$request = $this->getRequest();
try{
if($request->isPost()) {
$form = new CreateFeedForm($this->adapter);
$dataPost = array_merge($request->getPost()->toArray(), $request->getFiles()->toArray());
$categoriesId = $dataPost['category_id'] ?? [];
$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_ANSWER;
$feed->posted_or_shared = Feed::POSTED;
$feed->shared_with = Feed::SHARE_WITH_PUBLIC;
$feed->related_feed = $question->id;
$feed->total_comments = 0;
$feed->total_shared = 0;
$files = $this->getRequest()->getFiles()->toArray();
$file_type = '';
if(isset($files['file']) && empty($files['file']['error'])) {
$feed_tmp_filename = $files['file']['tmp_name'];
$feed_filename = $this->normalizeString($files['file']['name']);
$mime_type = mime_content_type($feed_tmp_filename);
if($mime_type == 'image/jpg' || $mime_type == 'image/jpeg' || $mime_type == 'image/png') {
$file_type = Feed::FILE_TYPE_IMAGE;
} else if($mime_type == 'video/webm' || $mime_type == 'video/mpeg' || $mime_type == 'video/mpg' || $mime_type == 'video/mp4') {
$file_type = Feed::FILE_TYPE_VIDEO;
} else if($mime_type == 'application/pdf') {
$file_type = Feed::FILE_TYPE_DOCUMENT;
}
}
$feedMapper = FeedMapper::getInstance($this->adapter);
if($feedMapper->insert($feed)) {
$feed = $feedMapper->fetchOne($feed->id);
$category = new FeedTopic();
$feedMapper->update($feed);
$feed = $feedMapper->fetchOne($feed->id);
if( $file_type == Feed::FILE_TYPE_DOCUMENT) {
try {
$target_path = $this->config['leaderslinked.fullpath.feed'] . $feed->uuid;
if(!file_exists($target_path)) {
mkdir($target_path, 0755);
}
$full_filename = $target_path . DIRECTORY_SEPARATOR .$feed_filename;
$result = move_uploaded_file($feed_tmp_filename , $full_filename);
if($result) {
$feed->file_type = $file_type;
$feed->file_name = basename($feed_filename);
$feedMapper->update($feed);
} else {
error_log('no se pudo mover o renombrar el documento : ' . $feed_tmp_filename . ' al directorio : ' . $full_filename );
}
} catch(\Throwable $e) {
error_log($e->getTraceAsString());
}
}
if( $file_type == Feed::FILE_TYPE_IMAGE) {
try {
$target_path = $this->config['leaderslinked.fullpath.feed'] . $feed->uuid;
if(!file_exists($target_path)) {
mkdir($target_path, 0755);
}
list($target_width, $target_height) = explode('x', $this->config['leaderslinked.image_sizes.feed_image_size']);
$feed_filename = substr($feed_filename, 0, strrpos($feed_filename, '.')) . '.png';
$crop_to_dimensions = false;
if(Image::uploadImage($feed_tmp_filename, $target_path, $feed_filename, $target_width, $target_height, $crop_to_dimensions )) {
$feed->file_type = $file_type;
$feed->file_name = basename($feed_filename);
$feedMapper->update($feed);
}
} catch(\Throwable $e) {
error_log($e->getTraceAsString());
}
}
if( $file_type == Feed::FILE_TYPE_VIDEO) {
try {
$target_path = $this->config['leaderslinked.fullpath.feed'] . $feed->uuid;
if(!file_exists($target_path)) {
mkdir($target_path, 0755);
}
$full_filename = $target_path . DIRECTORY_SEPARATOR . $feed_filename;
$result = move_uploaded_file($feed_tmp_filename , $full_filename);
if($result) {
$size = $this->config['leaderslinked.image_sizes.feed_image_size'];
$getFromSecound = 2;
//extracción del cover
$generateFileName = substr($feed_filename, 0, strrpos($feed_filename, '.'));
$generateFile = $target_path . DIRECTORY_SEPARATOR . $generateFileName . '.png';
$cmd = "/usr/bin/ffmpeg -i $full_filename -an -ss $getFromSecound -s $size $generateFile";
exec($cmd);
$feed->file_type = $file_type;
$feed->file_name = basename($feed_filename);
$feed->file_image_preview = basename($generateFile);
$feedMapper->update($feed);
$videoConvert = new VideoConvert();
$videoConvert->filename = $full_filename;
$videoConvert->type = VideoConvert::TYPE_FEED;
$videoConvertMapper = VideoConvertMapper::getInstance($this->adapter);
$videoConvertMapper->insert($videoConvert);
} else {
error_log('no se pudo mover o renombrar el documento : ' . $feed_tmp_filename . ' al directorio : ' . $full_filename );
}
} catch(\Throwable $e) {
error_log($e->getTraceAsString());
}
}
$response = [
'success' => true,
'data' => 'success'
];
} else {
$messages = [];
$form_messages = (array) $form->getMessages();
foreach($form_messages as $fieldname => $field_messages)
{
$messages[$fieldname] = array_values($field_messages);
}
}
} else {
$response = [
'success' => false,
'data' => 'ERROR_METHOD_NOT_ALLOWED'
];
}
return new JsonModel($response);
}
} catch (\Throwable $e) {
$e->getMessage();
return new JsonModel([
'success' => false,
'data' => $e
]);
}
}
/**
*
* @param string $str
* @return string
*/
private function normalizeString ($str = ''){
$basename = substr($str, 0, strrpos($str, '.'));
$basename = str_replace('.', '-', $basename);
$extension = substr($str, strrpos($str, '.'));
$str = $basename . $extension;
$str = strip_tags($str);
$str = preg_replace('/[\r\n\t ]+/', ' ', $str);
$str = preg_replace('/[\"\*\/\:\<\>\?\'\|\,]+/', ' ', $str);
$str = strtolower($str);
$str = html_entity_decode( $str, ENT_QUOTES, "utf-8" );
$str = htmlentities($str, ENT_QUOTES, "utf-8");
$str = preg_replace("/(&)([a-z])([a-z]+;)/i", '$2', $str);
$str = str_replace(' ', '-', $str);
$str = rawurlencode($str);
$str = str_replace('%', '-', $str);
return trim(strtolower($str));
}
private function renderFeed($id , $now){
$currentNetworkPlugin = $this->plugin('currentNetworkPlugin');
$network = $currentNetworkPlugin->getNetwork();
$currentUserPlugin = $this->plugin('currentUserPlugin');
$currentUser = $currentUserPlugin->getUser();
$currentCompany = $currentUserPlugin->getCompany();
$companyMapper = CompanyMapper::getInstance($this->adapter);
$company = $companyMapper->fetchOne($currentCompany->id);
$companyUserMapper = CompanyUserMapper::getInstance($this->adapter);
$owner = $companyUserMapper->fetchOwnerByCompanyId($currentCompany->id);
$acl = $this->getEvent()->getViewModel()->getVariable('acl');
$feedMapper = FeedMapper::getInstance($this->adapter);
$feed = $feedMapper->fetchOne($id);
$params = [
'id' => $feed->uuid,
];
$item = [
'feed_unique' => uniqid(),
'feed_uuid' => $feed->uuid,
'feed_description' => $feed->description,
'feed_delete_url' => $this->url()->fromRoute('my-trainer/question/answer/delete', $params),
'feed_content_type' => $feed->file_type ? $feed->file_type : '',
'feed_priority'=> $feed->priority,
];
$userMapper = UserMapper::getInstance($this->adapter);
$user = $userMapper->fetchOne($feed->user_id);
$item['feed_delete_url'] = $this->url()->fromRoute('my-trainer/question/answer/delete', $params);
$item['owner_url'] = 'https://'. $network->main_hostname . '/company/view/' .$company->uuid;
$item['owner_image'] = $this->url()->fromRoute('storage', ['code' => $company->uuid, 'type' => 'user', 'filename' => $company->image]);
$item['owner_name'] = $company->name;
$item['owner_time_elapse'] = Functions::timeAgo( $feed->added_on, $now);
if($feed->file_type == Feed::FILE_TYPE_IMAGE) {
$item['owner_file_image'] = $this->url()->fromRoute('storage', ['code' => $feed->uuid, 'type' => 'feed', 'filename' => $feed->file_name]);
}
if($feed->file_type == Feed::FILE_TYPE_DOCUMENT) {
$item['owner_file_document'] = $this->url()->fromRoute('storage', ['code' => $feed->uuid, 'type' => 'feed', 'filename' => $feed->file_name]);
}
if($feed->file_type == Feed::FILE_TYPE_VIDEO) {
$item['owner_file_image_preview'] = $this->url()->fromRoute('storage', ['code' => $feed->uuid, 'type' => 'feed', 'filename' => $feed->file_image_preview]);
$item['owner_file_video'] = $this->url()->fromRoute('storage', ['code' => $feed->uuid, 'type' => 'feed', 'filename' => $feed->file_name]);
}
if($feed->file_type == Feed::FILE_TYPE_MEETING) {
$dt = \DateTime::createFromFormat('Y-m-d', $feed->date);
$dt2 = \DateTime::createFromFormat('H:i:s', $feed->time);
$item['owner_meeting'] = true;
$item['owner_meeting_date'] = $dt->format('d/m/Y');
$item['owner_meeting_time'] = $dt2->format('h:i a');
$item['owner_meeting_link_type'] = $feed->link_type;
$item['owner_meeting_link_name'] = $feed->link_name;
}
$commentMapper = CommentMapper::getInstance($this->adapter);
$records = $commentMapper->fetchAllPublishedByFeedId($feed->id);
$comments = [];
$comment_count=0;
foreach($records as $record)
{
$user = $userMapper->fetchOne($record->user_id);
$comment = [];
$comment['unique'] = uniqid();
$comment_count++;
$comment['comment_index']=$comment_count;
$user = $userMapper->fetchOne($record->user_id);
if($user->id == $owner->user_id) {
$comment['user_image'] = $this->url()->fromRoute('storage', ['type' => 'company', 'code' => $company->uuid, 'filename' => $company->image ]);
$comment['user_url'] = 'https://'. $network->main_hostname . '/company/view/' . $company->uuid;
$comment['user_name'] = $company->name;
} else {
$comment['user_image'] = $this->url()->fromRoute('storage', ['type' => 'user', 'code' => $user->uuid, 'filename' => $user->image ]);
$comment['user_url'] = 'https://'. $network->main_hostname . '/profile/view/' . $user->uuid;
$comment['user_name'] = $user->first_name . ' ' . $user->last_name;
}
$comment['link_delete'] = $this->url()->fromRoute('my-trainer/question/answer/comments/delete',['id' => $feed->uuid, 'comment' => $record->uuid ]);
$comment['link_answer_add'] = $this->url()->fromRoute('my-trainer/question/answer/comments/answer',['id' => $feed->uuid, 'comment' => $record->uuid ]);
$comment['time_elapsed'] = Functions::timeAgo($record->added_on, $now);
$comment['comment'] = $record->comment;
$records2 = $commentMapper->fetchAllPublishedByCommentId($record->id);
$answers = [];
$contador=0;
foreach($records2 as $record2)
{
$user = $userMapper->fetchOne($record2->user_id);
$answer = [];
$answer['unique'] = uniqid();
$user = $userMapper->fetchOne($record2->user_id);
if($user->id == $owner->user_id) {
$answer['user_image'] = $this->url()->fromRoute('storage', ['type' => 'company', 'code' => $company->uuid, 'filename' => $company->image ]);
$answer['user_url'] = 'https://'. $network->main_hostname . '/company/view/' . $company->uuid;
$answer['user_name'] = $company->name;
} else {
$answer['user_image'] = $this->url()->fromRoute('storage', ['type' => 'user', 'code' => $user->uuid, 'filename' => $user->image ]);
$answer['user_url'] = 'https://'. $network->main_hostname . '/profile/view/' .$user->uuid;
$answer['user_name'] = $user->first_name . ' ' . $user->last_name;
}
$answer['link_delete'] = $this->url()->fromRoute('my-trainer/question/answer/comments/delete',['id' => $feed->uuid, 'comment' => $record2->uuid ]);
$answer['time_elapsed'] = Functions::timeAgo($record2->added_on, $now);
$answer['comment'] = $record2->comment;
$records2 = $commentMapper->fetchAllPublishedByCommentId($record2->id);
$contador++;
array_push($answers, $answer);
}
$comment['number_answers']=$contador;
$comment['answers'] = $answers;
array_push($comments, $comment);
}
$item['comment_add_url'] = $this->url()->fromRoute('my-trainer/question/answer/comments', ['id' => $feed->uuid]);
$item['comments'] = $comments;
return $item;
}
public function commentAction()
{
$currentUserPlugin = $this->plugin('currentUserPlugin');
$currentUser = $currentUserPlugin->getUser();
$currentCompany = $currentUserPlugin->getCompany();
$id = $this->params()->fromRoute('id');
$request = $this->getRequest();
if($request->isPost()) {
$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);
}
$dataPost = $request->getPost()->toArray();
$form = new CommentForm();
$form->setData($dataPost);
if($form->isValid()) {
$companyUserMapper = CompanyUserMapper::getInstance($this->adapter);
$owner = $companyUserMapper->fetchOwnerByCompanyId($currentCompany->id);
$dataPost = (array) $form->getData();
$comment = new Comment();
$comment->comment = $dataPost['comment'];
$comment->feed_id = $feed->id;
$comment->user_id = $currentCompany->id;
$commentMapper = CommentMapper::getInstance($this->adapter);
if($commentMapper->insert($comment)) {
$utilMapper = UtilMapper::getInstance($this->adapter);
$now = $utilMapper->getDatebaseNow();
$total_comments = $commentMapper->fetchCountCommentByFeedId($comment->feed_id);
$feed->total_comments = $total_comments;
$feedMapper->update($feed);
$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 answerAction()
{
$currentUserPlugin = $this->plugin('currentUserPlugin');
$currentUser = $currentUserPlugin->getUser();
$currentCompany = $currentUserPlugin->getCompany();
$id = $this->params()->fromRoute('id');
$comment_uuid = $this->params()->fromRoute('comment');
$request = $this->getRequest();
if($request->isPost()) {
$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);
}
$dataPost = $request->getPost()->toArray();
$form = new CommentAnswerForm();
$form->setData($dataPost);
if($form->isValid()) {
$companyUserMapper = CompanyUserMapper::getInstance($this->adapter);
$owner = $companyUserMapper->fetchOwnerByCompanyId($currentCompany->id);
$dataPost = (array) $form->getData();
$answer = new Comment();
$answer->comment = $dataPost['answer'];
$answer->feed_id = $feed->id;
$answer->user_id = $owner->user_id;
$commentMapper = CommentMapper::getInstance($this->adapter);
$comment=$commentMapper->fetchOneByUuid($comment_uuid);
$answer->parent_id = $comment->id;
if($commentMapper->insert($answer)) {
$utilMapper = UtilMapper::getInstance($this->adapter);
$now = $utilMapper->getDatebaseNow();
$total_comments = $commentMapper->fetchCountCommentByFeedId($comment->feed_id);
$feed->total_comments = $total_comments;
$feedMapper->update($feed);
$response = [
'success' => true,
'data' => $this->renderComment($answer->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 commentDeleteAction()
{
$currentUserPlugin = $this->plugin('currentUserPlugin');
$currentUser = $currentUserPlugin->getUser();
$currentCompany = $currentUserPlugin->getCompany();
$request = $this->getRequest();
if($request->isPost()) {
$currentUserPlugin = $this->plugin('currentUserPlugin');
$currentUser = $currentUserPlugin->getUser();
$id = $this->params()->fromRoute('id');
$comment = $this->params()->fromRoute('comment');
$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);
}
$commentMapper = CommentMapper::getInstance($this->adapter);
$comment = $commentMapper->fetchOneByUuid($comment);
if($comment && $comment->feed_id == $feed->id) {
$comment->status = Comment::STATUS_DELETED;
if($commentMapper->update($comment)) {
$total_comments = $commentMapper->fetchCountCommentByFeedId($comment->feed_id);
$feed = $feedMapper->fetchOne($comment->feed_id);
$feed->total_comments = $total_comments;
$feedMapper->update($feed);
$response = [
'success' => true,
'data' => [
'message' => '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_METHOD_NOT_ALLOWED'
];
}
return new JsonModel($response);
}
private function renderComment($comment_id, $now)
{
$currentUserPlugin = $this->plugin('currentUserPlugin');
$currentUser = $currentUserPlugin->getUser();
$currentCompany = $currentUserPlugin->getCompany();
$companyMapper = CompanyMapper::getInstance($this->adapter);
$company = $companyMapper->fetchOne($currentCompany->id);
$companyUserMapper = CompanyUserMapper::getInstance($this->adapter);
$owner = $companyUserMapper->fetchOwnerByCompanyId($currentCompany->id);
$currentNetworkPlugin = $this->plugin('currentNetworkPlugin');
$network = $currentNetworkPlugin->getNetwork();
$item = [];
$commentMapper = CommentMapper::getInstance($this->adapter);
$record = $commentMapper->fetchOne($comment_id);
$feedMapper = FeedMapper::getInstance($this->adapter);
$feed = $feedMapper->fetchOne($record->feed_id);
if($record) {
$userMapper = UserMapper::getInstance($this->adapter);
$item = [];
$item['unique'] = uniqid();
$user = $userMapper->fetchOne($record->user_id);
if($user->id == $owner->user_id) {
$item['user_image'] = $this->url()->fromRoute('storage', ['type' => 'company', 'code' => $company->uuid, 'filename' => $company->image ]);
$item['user_url'] = 'https://'. $network->main_hostname . '/company/view/' . $company->uuid;
$item['user_name'] = $company->name;
} else {
$item['user_image'] = $this->url()->fromRoute('storage', ['type' => 'user', 'code' => $user->uuid, 'filename' => $user->image ]);
$item['user_url'] = 'https://'. $network->main_hostname . '/profile/view/' . $user->uuid;
$item['user_name'] = $user->first_name . ' ' . $user->last_name;
}
$item['link_answer_add'] = $this->url()->fromRoute('my-trainer/question/answer/comments/answer',['id' => $feed->uuid, 'comment' => $record->uuid ]);
$item['link_delete'] = $this->url()->fromRoute('my-trainer/question/answer/comments/delete',['id' => $feed->uuid, 'comment' => $record->uuid ]);
$item['time_elapsed'] = Functions::timeAgo($record->added_on, $now);
$item['comment'] = $record->comment;
}
return $item;
}
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);
}
}