<?php

class PointController extends Controller
{

	static function actionIndex()
	{
		self::$layout = 'layouts/page.php';

		$pointId = (int) App::getParam('id');
		if (!$pointId) {
			App::error404();
		}

		$point = Point::model()->getByPK($pointId);
		if (!$point->id) {
			App::error404();
		}

		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.js');
		self::addScript('/js/api-maps-yandex-2.0.js');
		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('/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/leaflet.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');
		}

		if ($point->status != Point::statusPublished) {
			if (!App::$user) {
				App::error404();
			} else if(!App::$user->isAdmin && $point->author != App::$user->id) {
				App::error404();
			}
		}

		$point->descriptionHtml = Markdown::liteProcessing($point->description);
		$point->descriptionHtml = nl2br($point->descriptionHtml);
		$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]{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));

		$categoryId = 0;
		if (array_search($point->categoryId, Category::$paidIds) !== FALSE) {
			$categoryId = $point->categoryId;
		}

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

		$articlePoints = ArticlePoints::model()->getAll(array('where' => '`pointId` = ' . $pointId));
		$articleIds = array();
		$articles = array();
		if ($articlePoints) {
			foreach ($articlePoints as $articlePoint) {
				$articleIds[] = (int) $articlePoint->articleId;
			}
			$articles = Article::model()->getAll(array('where' => '`id` IN (' . implode(',', $articleIds) . ')'));
		}

		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,
			'articles' => $articles,
			'extraParams' => $point->getExtraParams(),
			'usersAirport' => Airports::getNearest(App::$userGeo['lat'], App::$userGeo['lng']),
			'pointsAirport' => Airports::getNearest($point->lat, $point->lng),
		));
	}

}
