<?php

class UserController extends Controller
{
	function __construct()
	{
		self::$layout = 'layouts/page.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/fotorama.js');
		self::addScript('/js/app.js?ver=' . App::getConfig('version'));

		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');
	}

	function actionIndex()
	{
		if (!App::getParam('id')) {
			if (App::$user) {
				if (Notifications::getUnreadedCount() > 0) {
					App::redirect('/user/notifications/');
				} else {
					App::redirect('/user/'.App::$user->id);
				}
			} else {
				App::redirect('/');
			}
		}

		self::addStyle('/css/user.css?ver=' . App::getConfig('version'));

		$id = (int) App::getParam('id');
		$user = User::model()->getByPK($id);

		if (!$user) {
			App::error404();
		}

		if (App::$user && App::$user->id == $id) {
			$articles = Article::model()->getAll(array('where'=>'`author`='.(int)$user->id.' AND `status` != "del"', 'order' => '`pubDate` DESC'));
		} else {
			$articles = Article::model()->getAll(array('where'=>'`author`='.(int)$user->id . ' AND `status` = "published" ', 'order' => '`pubDate` DESC'));
		}

		$randomPoint = Point::model()->getRandom();
		$randomPoint->description = mb_substr($randomPoint->description, 0, 400, 'UTF-8');
		$randomPoint->description = Helper::replaceLinks($randomPoint->description);

//		$photos = ArticlePhotos::model()->getByUser($id, 5);
		$photos = NULL; // Скрыл фотоленту, так как туда могут попадать фотки неопубликованных статей

		$pointsIds = App::DB()->query('SELECT `id` FROM  `points` WHERE  `author` ='.$id.' AND  `status` =  \'published\' ORDER BY `ord` DESC LIMIT 0 , 5')->fetchAll(PDO::FETCH_ASSOC);
		$points = array();
		if (!empty($pointsIds)) {
			foreach ($pointsIds as $pointsId) {
				$points[] = Point::model()->getByPK($pointsId['id']);
			}
		}

		self::render('user/index.php', array(
			'user' => $user,
			'articles' => $articles,
			'photos' => $photos,
			'points' => $points,
			'imAuthor' => App::$user && App::$user->id == $id,
			'randomPoint' => $randomPoint,
		));
	}

	function actionNotifications()
	{
		if (!App::$user) {
			App::redirect('/');
		}

		$notifications = Notifications::model()->getAll(array(
			'where' => '`userId` = '.App::$user->id,
			'order' => 'readed ASC, date DESC'
		));
		Notifications::model()->setAllReaded();

		$randomPoint = Point::model()->getRandom();
		$randomPoint->description = mb_substr($randomPoint->description, 0, 400, 'UTF-8');
		$randomPoint->description = Helper::replaceLinks($randomPoint->description);

		self::render('user/notifications.php', array(
			'notifications' => $notifications,
			'randomPoint' => $randomPoint,
		));
	}

	function actionFavorites()
	{
		if (!App::$user) {
			App::redirect('/');
		}

		$randomPoint = Point::model()->getRandom();
		$randomPoint->description = mb_substr($randomPoint->description, 0, 400, 'UTF-8');
		$randomPoint->description = Helper::replaceLinks($randomPoint->description);

		self::render('user/favorites.php', array(
			'favorites' => Favorite::model()->getAllMy(),
			'randomPoint' => $randomPoint,
		));
	}
}
