<?php namespace Redozote\Controllers;
use Redozote\Models\Forums;
use Redozote\Models\ForumCategories;
use Redozote\Models\ForumTopics;
use Redozote\Models\ForumPosts;
use Redozote\Models\Communities;
use Redozote\Models\CommunityUsers;
use Redozote\Forms\Forums\CommentForm;
use Redozote\Forms\Forums\CreateTopicForm;
class ForumsController extends BaseController {
public function initialize() {
$this->access->set('createTopic', function ($di, $cat, $comm, $user) {
return $this->auth->check();
});
$this->access->set('posting', function ($di, $topic, $comm, $user) {
return $this->auth->check();
});
$this->access->set('deletePost', function ($di, $post, $comm, $user) {
$admitted = array('creator', 'admin', 'moder');
if ($user && in_array($user->level, $admitted)) {
return true;
} else {
return false;
}
});
$this->access->set('editPost', function ($di, $post, $comm, $user) {
$admitted = array('creator', 'admin');
if ($comm && $user && in_array($user->level, $admitted)) {
return true;
} else if ($comm && $user && $user->user_id == $post->user_id) {
return true;
} else if ($comm && !$user && $di->auth->getId() == $post->user_id) {
return true;
} else if (!$comm && $user && $user->id == $post->user_id) {
return true;
} else {
return false;
}
});
$this->access->set('replyPost', function ($di, $post, $comm, $user) {
if ($comm && $user && $user->user_id != $post->user_id) {
return true;
} else if ($comm && !$user && $di->auth->check() && $di->auth->getId() != $post->user_id) {
return true;
} else if (!$comm && $user && $user->id != $post->user_id) {
return true;
} else {
return false;
}
});
}
public function forumAction() {
$id = $this->dispatcher->getParam('forum');
$forum = Forums::findFirst($id);
if ($forum == false)
$this->showFatalError('Форум не найден');
$comm = $forum->comm;
$user = $comm == false
? $this->auth->getId()
: CommunityUsers::findFirst(array(
'user_id = ?0 AND comm_id = ?0',
'bind' => array($this->auth->getId(), $comm->id)
));
$title = $comm == false
? 'Форум / Категории'
: $this->escaper->escapeHtml($comm->name).' / Форум';
$this->tag->setTitle($title);
$this->view->setVars(array(
'user' => $user,
'comm' => $comm,
'forum' => $forum,
));
}
public function catAction() {
$id = $this->dispatcher->getParam('cat');
$cat = ForumCategories::findFirst($id);
if ($cat == false)
$this->showFatalError('Категория не найдена');
$forum = $cat->forum;
$comm = $forum->comm;
$user = $comm == false
? $this->auth->getId()
: CommunityUsers::findFirst(array(
'user_id = ?0 AND comm_id = ?0',
'bind' => array($this->auth->getId(), $comm->id)
));
$title = $comm == false
? 'Форум / '.$cat->name
: $this->escaper->escapeHtml($comm->name).' / Форум / '.$this->escaper->escapeHtml($cat->name);
$this->tag->setTitle($title);
$this->view->setVars(array(
'cat' => $cat,
'user' => $user,
'comm' => $comm,
'forum' => $forum,
));
}
public function topicAction() {
$id = $this->dispatcher->getParam('topic');
$topic = ForumTopics::findFirst($id);
$page = (int) $this->dispatcher->getParam('page');
if ($topic == false)
$this->showFatalError('Тема не найдена');
$forum = $topic->forum;
$cat = $topic->category;
$comm = $forum->comm;
$user = $comm == false
? $this->auth->getId()
: CommunityUsers::findFirst(array(
'user_id = ?0 AND comm_id = ?0',
'bind' => array($this->auth->getId(), $comm->id)
));
$title = $comm == false
? 'Форум / '.$cat->name
: $this->escaper->escapeHtml($comm->name).' / Форум / '.$this->escaper->escapeHtml($cat->name).' / '.$this->escaper->escapeHtml($topic->title);
$form = new CommentForm;
if ($this->request->isPost()) {
if ($this->access->posting()) {
if ($form->isValid($this->request->getPost())) {
$post = new ForumPosts();
$post->forum_id = $forum->id;
$post->cat_id = $cat->id;
$post->topic_id = $topic->id;
$post->user_id = $this->auth->getId();
$post->message = $this->request->getPost('message');
if ($post->save()) {
$this->flash->success('Ваш комментарий успешно добавлен!');
$this->response->redirect('forums/topic-'.$topic->id);
$this->view->disable();
return true;
} else {
foreach ($post->getMessages() as $message) {
$this->flash->error($message);
}
}
} else {
foreach ($form->getMessages() as $message) {
$this->flash->error($message);
}
}
} else {
$this->flash->error('Вы не можете комментировать эту запись!');
$this->response->redirect('forums/topic-'.$topic->id);
$this->view->disable();
return false;
}
}
$paginator = new \Phalcon\Paginator\Adapter\Model(array(
'data' => $topic->posts,
'limit' => 5,
'page' => $page
));
$this->view->form = $form;
$this->tag->setTitle($title);
$this->view->setVars(array(
'cat' => $cat,
'user' => $user,
'page' => $paginator->getPaginate(),
'comm' => $comm,
'topic' => $topic,
'forum' => $forum,
));
}
public function createTopicAction() {
$id = $this->dispatcher->getParam('cat');
$cat = ForumCategories::findFirst($id);
if ($cat == false)
$this->showFatalError('Категория не найдена');
$forum = $cat->forum;
$comm = $forum->comm;
$user = $comm == false
? $this->auth->getId()
: CommunityUsers::findFirst(array(
'user_id = ?0 AND comm_id = ?0',
'bind' => array($this->auth->getId(), $comm->id)
));
if (!$this->access->createTopic($cat, $comm, $user)) {
$this->flash->error('У вас нет прав на создание темы');
$this->response->redirect('forums/forum-'.$forum->id);
$this->view->disable();
return false;
}
$title = $comm == false
? 'Форум / '.$cat->name
: $this->escaper->escapeHtml($comm->name).' / Форум / '.$this->escaper->escapeHtml($cat->name).' / Новая тема';
$form = new CreateTopicForm();
if ($this->request->isPost()) {
if ($form->isValid($this->request->getPost())) {
$topic = new ForumTopics();
$topic->forum_id = $forum->id;
$topic->cat_id = $cat->id;
$topic->user_id = $this->auth->getId();
$topic->title = $this->request->getPost('title');
$topic->message = $this->request->getPost('message');
if ($topic->save()) {
if ($comm && $user) {
$user->posts++;
$user->save();
}
$this->flash->success('Тема успешно создана!');
$this->response->redirect('forums/topic-'.$topic->id);
$this->view->disable();
return true;
} else {
foreach ($topic->getMessages() as $message) {
$this->flash->error($message);
}
}
} else {
foreach ($form->getMessages() as $message) {
$this->flash->error($message);
}
}
}
$this->view->form = $form;
$this->view->pick('forums/create-topic');
$this->tag->setTitle($title);
$this->view->setVar('cat', $cat);
$this->view->setVar('comm', $comm);
$this->view->setVar('user', $user);
$this->view->setVar('forum', $forum);
}
public function editPostAction() {
$id = $this->dispatcher->getParam('post');
$post = ForumPosts::findFirst($id);
if ($post == false)
$this->showFatalError('Пост не наден');
$forum = $post->forum;
$comm = $forum->comm;
$user = $comm == false
? $this->auth->user()
: CommunityUsers::findFirst(array(
'user_id = ?0 AND comm_id = ?0',
'bind' => array($this->auth->getId(), $comm->id)
));
$this->tag->setTitle($comm->name);
}
}