<?php

class PageController extends Controller
{

	static function actionPoints()
	{
		self::$layout = 'layoutPage.php';
		self::addStyle('/css/style.css?ver=' . App::getConfig('version'));
		self::addStyle('/css/pageStyle.css?ver=' . App::getConfig('version'));
		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::addStyle('/css/bootstrap.min.css');
		self::addStyle('/css/bootstrap-theme.min.css');

		$notModeratedPoints = $result = array();

		// On moderation
		if (App::$user && App::$user->isAdmin) {
			$param['order'] = 'id DESC';
			$param['where'] = '`moderate` = 0';
			$points = Point::model()->getAll($param);
			$categories = Category::model()->getAll(array('order' => 'ord ASC'));

			foreach ($points as $point) {
				$id = $point->id;
				$result[$id]['id'] = $point->id;
				$result[$id]['name'] = $point->name;
				$result[$id]['lat'] = $point->lat;
				$result[$id]['lng'] = $point->lng;
				$result[$id]['author'] = $point->authorUser->nick;
				$result[$id]['categoryId'] = $point->categoryId;
				$result[$id]['categoryIcon'] = $categories[$point->categoryId]->icon;
			}
			$notModeratedPoints = $result;
		}
		// ------------------------------------------

		$param = array();
		$param['order'] = 'id DESC';
		if (App::getParam('category')) {
			$param['where'] = 'categoryId = ' . (int) App::getParam('category') . ' AND `moderate` = 1';
		} else {
			$param['where'] = '`moderate` = 1';
			if (!(App::$user && App::$user->isAdmin)) {
				$param['limit'] = 100;
			}
		}
		$points = Point::model()->getAll($param);
		$categories = Category::model()->getAll(array('order' => 'ord ASC'));

		$result = array();

		foreach ($points as $point) {
			$id = $point->id;
			$result[$id]['id'] = $point->id;
			$result[$id]['name'] = $point->name;
			$result[$id]['lat'] = $point->lat;
			$result[$id]['lng'] = $point->lng;
			$result[$id]['author'] = $point->authorUser->nick;
			$result[$id]['categoryId'] = $point->categoryId;
			$result[$id]['categoryIcon'] = $categories[$point->categoryId]->icon;
		}

		$res = App::DB()->query("SELECT COUNT(`id`) pointsCount FROM `points`")->fetch(PDO::FETCH_ASSOC);
		App::setConfig('title', $res['pointsCount'] . ' или даже больше поводов не сидеть дома');

		self::render('pointsTemplate.php', array(
			'categories' => $categories,
			'user' => App::$user,
			'points' => $result,
			'notModeratedPoints' => $notModeratedPoints,
			'randomPoint' => Point::model()->getRandom(),
		));
	}

	static function actionPoint()
	{
		self::$layout = 'layoutPage.php';

		$pointId = (int) App::getParam('id');
		if ($pointId) {
			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/leaflet-0.7.3.js');
			self::addScript('http://api-maps.yandex.ru/2.0/?load=package.map&lang=ru-RU');
			self::addScript('/js/leaflet.markercluster.js');

			self::addScript('/js/app.js?ver=' . App::getConfig('version'));
			self::addScript('/js/mapPoint.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/leaflet-0.7.3.css');
			self::addStyle('/css/MarkerCluster.css');
			self::addStyle('/css/MarkerCluster.Default.css');

			if (App::$user && App::$user->id) {
				self::addVar('user', App::$user->id);
			} else {
				self::addVar('user', '0');
			}

			$point = Point::model()->getByPK($pointId);

			if ($point->id == NULL) {
				App::error404();
			}

			$point->descriptionHtml = nl2br($point->description);
			$point->name = htmlspecialchars($point->name);
			App::setConfig('title', $point->name . ' - wikipoints.ru');
			self::addVar('pointLat', $point->lat);
			self::addVar('pointLng', $point->lng);
			self::addVar('pointId', $point->id);

			$categories = Category::model()->getAll(array('order' => 'ord ASC', 'asArray' => true));

			$point->categoryIcon = $categories[$point->categoryId]['icon'];
			$point->categoryName = $categories[$point->categoryId]['name'];
			$point->descriptionHtml = Helper::replaceLinks($point->descriptionHtml, 'html');
//			preg_match_all("/.*?(([.?!](?:\s|$))|$)/s", $point->descriptionHtml, $items);
			preg_match_all("/.*?((([^.?!\s]{3,}?)[.?!](?:\s|$))|$)/s", $point->descriptionHtml, $items);
			$point->inFavorites = Favorite::model()->checkIsMy($pointId);
			$point->iLike = Like::model()->checkIsMy($pointId);


			$photoLinks = PhotoLinks::model()->getAll(array('where' => '`pointId` = ' . $pointId));
			$photoLinksRes = array();
			if ($photoLinks) {
				foreach ($photoLinks as $photoLink) {
					if ($photoLink->host) {
						$photoLinksRes[strtolower($photoLink->host)] = 1;
					}
				}
			}
			$photoLinksRes = implode('; ', array_keys($photoLinksRes));

			$similarPoints = Point::model()->getAll(array('where' => '`moderate` = 1 AND categoryId = ' . $point->categoryId . ' AND `id` <> ' . $point->id . ' ORDER BY RAND() LIMIT 6;'));
			$closestPoints = Point::model()->getClosestPoints($pointId);
			$randomPoint = Point::model()->getRandom();
			$randomPoint->description = mb_substr($randomPoint->description, 0, 400, 'UTF-8');
			$randomPoint->description = Helper::replaceLinks($randomPoint->description);

			self::render('point.php', array(
				'point' => $point,
				'text' => $items[0],
				'randomPoint' => $randomPoint,
				'similarPoints' => $similarPoints,
				'closestPoints' => $closestPoints,
				'categories' => $categories,
				'photos' => $point->photos,
				'countInFavs' => Favorite::model()->countInFavs($pointId),
				'countLikes' => Like::model()->countLikes($pointId),
				'og' => Helper::buildOG($point),
				'user' => App::$user,
				'photoLinks' => $photoLinksRes,
			));
		} else {
			App::error404();
		}
	}

	static function actionArticle()
	{
		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) {
				App::error404();
			}

			App::setConfig('title', $article->title . ' - wikipoints.ru');
			$article->html = Helper::replaceLinks($article->html, 'html');
			$article->html = Helper::replaceImgs($article->html, 'html');

			self::render('article.php', array(
				'article' => $article,
				'randomPoint' => Point::model()->getRandom(),
				'og' => Helper::buildArticleOG($article),
//				'user' => App::$user,
			));
		} else {
			App::error404();
		}
	}

	static function actionMyprofile()
	{
		$userId = App::$user ? App::$user->id : 0;

		die($userId);
	}

//	static function actionProfile()
//	{
//		$userId = (int) App::getParam('user');
//
//	}

	static function actionVkpoint()
	{
		$pointId = (int) App::getParam('id');
		if (App::$user && App::$user->isAdmin == 1 && $pointId) {
			$point = Point::model()->getByPK($pointId);

			if ($point->id == NULL) {
				App::error404();
			}

			$point->descriptionHtml = nl2br($point->description);
			$point->name = htmlspecialchars($point->name);

			$categories = Category::model()->getAll(array('order' => 'ord ASC', 'asArray' => true));

			$point->description = Helper::replaceLinks($point->description);

			self::renderPartial('vkPoint.php', array(
				'point' => $point,
				'photos' => $point->photos,
			));
		} else {
			App::error404();
		}
	}

	static function action404($message)
	{
		self::renderPartial('error404.php', array(
			'point' => Point::model()->getRandom(),
		));
	}

}
