wikipoints/ground/controllers/ArticleController.php

241 lines
6.7 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
class ArticleController extends Controller
{
static function actionIndex()
{
self::$layout = 'layoutPage.php';
$articleId = (int) App::getParam('id');
if ($articleId) {
self::addScript('/js/jquery-2.1.0.min.js');
self::addScript('/js/jquery-ui-1.10.4.custom.min.js');
self::addScript('/js/bootstrap.min.js');
self::addScript('/js/app.js?ver=' . App::getConfig('version'));
self::addScript('http://cdnjs.cloudflare.com/ajax/libs/fotorama/4.6.3/fotorama.js');
self::addStyle('/css/style.css?ver=' . App::getConfig('version'));
self::addStyle('/css/pageStyle.css?ver=' . App::getConfig('version'));
self::addStyle('/css/bootstrap.min.css');
self::addStyle('/css/bootstrap-theme.min.css');
self::addStyle('http://cdnjs.cloudflare.com/ajax/libs/fotorama/4.6.3/fotorama.css');
$article = Article::model()->getByPK($articleId);
if ($article->id == NULL or $article->author == 0) {
App::error404();
}
if ($article->status == 'del') {
App::error404();
}
if ($article->status == 'draft' && (!App::$user || App::$user->id != $article->author)) {
App::error404();
}
App::setConfig('title', $article->title . ' wikipoints.ru');
$article->html = Helper::replaceLinks($article->html, 'html');
$article->html = Helper::replaceImgs($article->html, 'html');
$randomPoint = Point::model()->getRandom();
if ($article->photoId) {
$photo = ArticlePhotos::model()->getByPK($article->photoId);
$background = '/photos/articles/middle/' . $photo->name;
} else {
$background = $randomPoint->getImg('middle');
}
self::render('article.php', array(
'article' => $article,
'randomPoint' => $randomPoint,
'og' => Helper::buildArticleOG($article),
'background' => $background,
// 'user' => App::$user,
));
} else {
App::error404();
}
}
static function actionNew()
{
self::actionEdit();
}
static function actionEdit()
{
if (!App::$user) {
App::error404();
}
$articleId = (int) App::getParam('id');
self::$layout = 'layoutClear.php';
self::addScript('/js/jquery-2.1.0.min.js');
self::addScript('/js/jquery-ui-1.10.4.custom.min.js');
self::addScript('/js/bootstrap.min.js');
self::addScript('/js/app.js?ver=' . App::getConfig('version'));
self::addScript('/js/articleEdit.js?ver=' . App::getConfig('version'));
self::addScript('http://cdnjs.cloudflare.com/ajax/libs/fotorama/4.6.3/fotorama.js');
self::addStyle('/css/style.css?ver=' . App::getConfig('version'));
self::addStyle('/css/pageStyle.css?ver=' . App::getConfig('version'));
self::addStyle('/css/bootstrap.min.css');
self::addStyle('/css/bootstrap-theme.min.css');
self::addStyle('http://cdnjs.cloudflare.com/ajax/libs/fotorama/4.6.3/fotorama.css');
self::addStyle('/css/articleEdit.css?ver=' . App::getConfig('version'));
$article = Article::model()->getByPK($articleId);
if ($articleId > 0 && (!$article || $article->author != App::$user->id)) {
App::error404();
}
App::setConfig('title', 'Редактирование статьи ' . $article->title . ' wikipoints.ru');
self::render('articles/edit.php', array(
'article' => $article,
'randomPoint' => Point::model()->getRandom(),
));
}
static function actionSave()
{
if (!App::$user) {
App::error404();
}
if (!(isset($_POST['id']) && isset($_POST['title']) && isset($_POST['text']))) {
App::error404();
}
$id = (int) $_POST['id'];
$title = strip_tags($_POST['title']);
$text = str_replace(array('<','>'), array('&lt;','&gt;'), $_POST['text']);
$html = strip_tags($_POST['text']);
$html = str_replace(array('<','>',"'"), array('&lt;','&gt;','&prime;'), $html);
$html = Markdown::processing($html);
$html = nl2br($html);
$allowComments = (int) isset($_POST['allowComments']);
$article = $id ? Article::model()->getByPK($id) : new Article();
if (!$article) {
App::error404();
}
if ($id == 0) {
$article->author = App::$user->id;
}
if (isset($_POST['save']) && $_POST['save'] == 'published') {
if ($article->status != 'published' || $article->pubDate == 0) {
$article->pubDate = time();
}
$article->status = 'published';
} else {
$article->status = 'draft';
}
$article->title = $title ? $title : 'Без названия';
$article->text = $text;
$article->html = $html;
$article->status = (isset($_POST['save']) && $_POST['save'] == 'published') ? 'published' : 'draft';
$article->photoId = Helper::getFirstImg($text);
$article->allowComments = $allowComments;
$article->save();
preg_match_all("((\[#(\d+)[>\|](.*?)\])|(\[#(\d+)\]))", $text, $links);
$links = isset($links[0]) ? $links[0] : FALSE;
ArticlePoints::model()->deleteByArticle($article->id);
if ($links) {
foreach ($links as $link) {
$idAndLink = mb_substr($link, 2, -1);
$delimiter = strpos($idAndLink, '|') === FALSE ? '>' : '|';
$idAndLink = explode($delimiter, $idAndLink);
$linkId = $idAndLink[0];
if (Point::model()->getByPK($linkId)) {
ArticlePoints::model()->add(array(
'articleId' => $article->id,
'pointId' => $linkId,
));
}
}
}
App::redirect('/article/edit/id/'.$article->id);
}
static function actionDel()
{
if (!App::$user) {
App::error404();
}
$articleId = (int) App::getParam('id');
$article = Article::model()->getByPK($articleId);
if ($articleId == 0 || $article->id == 0 || $article->author != App::$user->id) {
App::error404();
}
$article->status = 'del';
$article->save();
App::redirect('/user/');
}
static function actionAddComment()
{
if (!App::$user ||
!isset($_POST['parentType']) ||
!isset($_POST['parentId']) ||
!isset($_POST['answerTo']) ||
!isset($_POST['text']))
{
App::error404();
}
$text = str_replace(array('<','>'), array('&lt;','&gt;'), $_POST['text']);
$html = strip_tags($_POST['text']);
$html = str_replace(array('<','>',"'"), array('&lt;','&gt;','&prime;'), $html);
$html = Markdown::liteProcessing($html);
$html = nl2br($html);
$comment = new Comment(array(
'userId' => App::$user->id,
'parentType' => ($_POST['parentType'] == 'article') ? 'article' : 'point',
'parentId' => (int)$_POST['parentId'],
'answerTo' => (int)$_POST['answerTo'],
'addDate' => time(),
'text' => $text,
'html' => $html,
));
$comment->save();
App::redirect('/'.$_POST['parentType'].'/'.$_POST['parentId'].'#comment'.$comment->id);
}
static function actionMarkdown()
{
if (isset($_POST['text']) ){
$html = strip_tags($_POST['text']);
$html = str_replace(array('<','>',"'"), array('&lt;','&gt;','&prime;'), $html);
$html = Markdown::processing($html);
$html = nl2br($html);
$html = Helper::replaceLinks($html, 'html');
$html = Helper::replaceImgs($html, 'html', 'small');
print $html;
}
die;
}
}