<?php
/*
   +----------------------------------------------------------------------+
   | Index Page                                                           |
   | Author: Adam Goldberg                                                |
   +----------------------------------------------------------------------+
   | Copyright (c) 2008-2009 Adam J Goldberg                              |
   | This file is part of Lifebox.                                        |
   |                                                                      |
   | Lifebox is free software: you can redistribute it and/or modify      |
   | it under the terms of the GNU General Public License as published by |
   | the Free Software Foundation, either version 3 of the License, or    |
   | (at your option) any later version.                                  |
   |                                                                      |
   | Lifebox is distributed in the hope that it will be useful,           |
   | but WITHOUT ANY WARRANTY; without even the implied warranty of       |
   | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        |
   | GNU General Public License for more details.                         |
   |                                                                      |
   | You should have received a copy of the GNU General Public License    |
   | along with Lifebox.  If not, see <http://www.gnu.org/licenses/>.     |
   +----------------------------------------------------------------------+
   | This is the homepage                                                 |
   +----------------------------------------------------------------------+
*/

require_once 'config/global.inc.php';
require_once 'Album.inc.php';
require_once 'Photo.inc.php';
require_once 'Comment.inc.php';

#DISPLAY A PHOTO
if (!empty($a) AND !empty($s)) {

	$p = new Photo ($a, $s);
	$photo_info = $p->getAll();

	#Check -- if an invalid photo, or if the user doesn't
	#have access, redirect back to the homepage
	if (empty($photo_info)) {
		header('Location: /');
		exit(0);
	}



	#IF POSTED NEW ALBUM
	if ($_POST['album_title']){
		if ($_POST['album_title'] === 'album title') {
			header('Location: '.$_SERVER['REQUEST_URI']);
			exit(0);
		}

		$album = new Album();
		$data = Array ('title'	=> $_POST['album_title'],
			       'type'	=> '2' );
		if ($album->setInfo($data)) {
			if ($album->commit()) {
				header('Location: '.$_SERVER['REQUEST_URI']);
			}
		}
	}

	#IF POSTED LEVEL CHANGE
	if ($_POST['level']){
		$p->changeLevel($_POST['level']);
		header('Location: '.$_SERVER['REQUEST_URI']);
		exit(0);
	}

	#IF POSTED COMMENT
	if ($_POST['comment_text']) {
		$c = new Comment();
		$data = Array ('photo_id'	=>	$photo_info['photo_id'],
			       'text'		=>	$_POST['comment_text']);
		if ($c->setInfo($data)) {
			$c->commit();
			header('Location: '.$_SERVER['REQUEST_URI']);
			exit(0);
		}
	}
	

	#Get a list of custom albums
	$custom_albums = Album::getCustomAlbums();
	$smarty->assign('custom_albums',$custom_albums);

	$smarty->assign('photo_info',$photo_info);

	#Update views
	$p->updateViews();

	#Get albums for this photo
	$photo_albums = $p->getAlbums();
	$smarty->assign('photo_albums',$photo_albums);

	#Array of album IDS in which this photo appears.
	# Used for administrative purposes.
	$photo_albums_ids = $p->getAllAlbumsArray();
	$smarty->assign('photo_albums_ids',$photo_albums_ids);

	if ($_POST['album_association']) {
	
		foreach ($custom_albums as $ca) {
			
			if (in_array($ca['album_id'],$photo_albums_ids) AND !$_POST[$ca['album_id']] ) {
				$p->deleteFromAlbum($ca['album_id']);
			}

			if (!in_array($ca['album_id'],$photo_albums_ids) AND $_POST[$ca['album_id']] ) {
				$p->addToAlbum($ca['album_id']);
			}

		}

			header('Location: '.$_SERVER['REQUEST_URI']);
			exit(0);
	}
	
	#Micro thumbs
	$photo_list = Photo::getPhotoList($a,true);
        $smarty->assign('photo_list',$photo_list);

	
	$album = new Album($a);
	$album_info = $album->getAll();
	if (empty($album_info)) {
		header ('Location: /');
		exit(0);
	}

	#If a Highlight ID change has been posted
	if ($_POST['highlight']){
		$album->setHighlight($photo_info['photo_id']);
		header('Location: '.$_SERVER['REQUEST_URI']);
                exit(0);
        }

	$smarty->assign('album_info',$album_info);

#LIST ALBUM/EVENT CONTENTS
} elseif (!empty($a)) {

	$album = new Album($a);
        $album_info = $album->getAll();
        if (empty($album_info)) {
                header ('Location: /');
                exit(0);
        }
	$smarty->assign('album_info',$album_info);

	$album->updateViews();

        $photo_list = Photo::getPhotoList($a);

	#If there are no photos to be displayed, either because
	#the album is empty or the user does not have access,
	#redirect back to the homepage
	if (empty($photo_list)) {
		header('Location: /');
		exit(0);
	}

        $smarty->assign('photo_list',$photo_list);

# MAIN PAGE
} else {

	#Get Featured Photo
	$featured_photo = Photo::getFeaturedPhoto();
	$smarty->assign('featured_photo',$featured_photo);

	#Get 6 Latest Albums
	$album_list = Album::getAlbumList(2,6,0,7);
	$smarty->assign('album_list',$album_list);

	#Get 10 Latest Events
	$event_list = Album::getAlbumList(1,10);
	$smarty->assign('event_list',$event_list);

	#Get 3 Latest Comments
	$comment_list = Comment::getLatestComments(3);
	$smarty->assign('comment_list',$comment_list);

	#Get 3 Latest Hot Photos
	$popular_photo_list = Photo::getPopularPhotos(1,3);
	$smarty->assign('popular_photo_list',$popular_photo_list);

	#Get 3 Latest Hot Videos
	$popular_video_list = Photo::getPopularPhotos(2,3);
	$smarty->assign('popular_video_list',$popular_video_list);

	#Determine if this is a fresh install
	if ($featured_photo OR $album_list OR $event_list OR
	    $comment_list OR $popular_photo_list OR $popular_video_list) {
		$new_site = false;
	} else {
		$new_site = true;
	}
	$smarty->assign('new_site',$new_site);

	$smarty->assign('ERROR',$ERROR);
	$smarty->display($CONFIG['smarty_templates'].'/index.tpl');

}

?>
