<?php

class ArticleController extends Controller
{
	static function actionIndex()
	{
		self::$layout = 'layouts/page.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('/js/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('/css/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 = 'layouts/clear.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/jquery.cookie.js');
		self::addScript('/js/articleEdit.js?ver=' . App::getConfig('version'));
		self::addScript('/js/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('/css/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();
		}

		if (!$article->id) {
			$article->status = 'draft';
		}

		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;
			$article->status = 'draft';
		}

		if (isset($_POST['save']) && $_POST['save'] == 'published') {
			if ($article->status != 'published' || $article->pubDate == 0) {
				$article->pubDate = time();
			}
			$article->status = 'published';
//			SMS::send('Статья: ' . $title . ' :: ' . App::$user->nick);
			Alarmer::send('новая статья "' . $title . '" от ' . App::$user->nick);
		}

		$article->title = $title ? $title : 'Без названия';
		$article->text = $text;
		$article->html = $html;
		$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 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, 'editor');
			$html = Helper::replaceImgs($html, 'html', 'small');

			print $html;
		}

		die;
	}

}
