<?php
/*
   +----------------------------------------------------------------------+
   | Photo/Video View 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/>.     |
   +----------------------------------------------------------------------+
   | Display a photo or video, and its statistics and comments            |
   +----------------------------------------------------------------------+
*/

require_once 'config/global.inc.php';
require_once 'Comment.inc.php';
require_once 'Album.inc.php';
require_once 'AlbumCategory.inc.php';
require_once 'Pagination.inc.php';

$p = new Photo ($a, $s,0,$_GET['sort']);
$photo_info = $p->getAll();

#Auto flush the tag queue
$p->autoFlushTagQueue();

$album = new Album($a);
$album_info = $album->getAll();
$smarty->assign('album_info',$album_info);

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

// AJAX REQUESTS
if( isset($_POST['action']) && !empty($_POST['action']) ) {

		if ($_POST['action'] == 'delete'){
			$p->deleteTag(stripslashes($_POST['tag']));
                        exit();
                }

                if ($_POST['action'] == 'autocomplete_tags'){
			$tags = $p->getAllTags();
			foreach ($tags as $tag) {
				print $tag['title'].',';
			}
                        exit();
                }
                if ($_POST['action'] == 'request_date'){
			$tag_list = $p->getTagArray();
			foreach ($tag_list as $tag) {
				if (($tag['x1'] OR $tag['x2'] OR $tag['y1'] OR $tag['y2'])) {
					echo '(x1:'.$tag['x1'].',y1:'.$tag['y1'].',x2:'.$tag['x2'].',y2:'.$tag['y2'].',tag:'.$tag['title'].")\n";
				}
			}
			exit(0);
		}
}

#If a comment was submitted for deletion
if (isset($_POST['comment_id'])) {
	$comment = new Comment($_POST['comment_id']);
	if ($comment->delete($photo_info['photo_id'])) {
		header('Location: '.$_SERVER['REQUEST_URI']);
		exit(0);
	}
}
	
// AJAX EDITED INFORMATION
//------------------------------------------------------------
#ALBUM TITLE UPDATED
if (isset($_POST['id']) AND $_POST['id'] === 'photo_title') {

	#Protect against empty submits
	if ($_POST['value'] === $CONFIG['text_title']) {
		print $CONFIG['text_title'];
		exit(0);
	}

	$data = Array('title'	=>	$_POST['value'],
		      'title_url_previous' 	=>	$s);
	$p->setInfo($data);
	if ($p->commit()) {
		print stripslashes($_POST['value']);
	} else {
		if ($ERROR){
			foreach ($ERROR as $e) {
				print $e;
			}
		}
	}

	exit(0);
}

if (isset($_POST['tag']) AND $_POST['tag']) {
        $p->addTag(stripslashes($_POST['tag']),$_POST['x1'],$_POST['y1'],$_POST['x2'],$_POST['y2'],$_POST['email']);
	exit(0);
}

#PHOTO DESCRIPTION UPDATED
if (isset($_POST['id']) AND $_POST['id'] === 'photo_text') {

	#Protect against empty submits
        if ($_POST['value'] === $CONFIG['text_description']) {
                print $CONFIG['text_description'];
                exit(0);
        }

        $data = Array('text'   =>      $_POST['value']);
        $p->setInfo($data);
        if ($p->commit()) {
                print strip_tags(stripslashes($_POST['value']));
        } else {
                print "An error has occurred";
        }

        exit(0);
}

#TAG LIST UPDATED
if (isset($_POST['id']) AND $_POST['id'] === 'photo_tags') {

	#Protect against empty submits
        if ($_POST['value'] === $CONFIG['text_tags']) {
                print $CONFIG['text_tags'];
                exit(0);
        }

	$p->updateTagString($_POST['value']);
	$tags = $p->getTagString();
	print $tags;
	exit(0);
}

// ADMINISTRATION FUNCTIONS
//------------------------------------------------------------
#IF POSTED NEW ALBUM
if (isset($_POST['album_title']) AND $_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'   	=> $_POST['type'],
		       'location' 	=> $_POST['location'] );
	if ($album->setInfo($data)) {
		if ($album->commit()) {
			header('Location: '.$_SERVER['REQUEST_URI']);
		}
	}
	exit(0);
}

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

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

#ALBUM ASSOCIATIONS

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

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

if (isset($_POST['album_association']) AND $_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']);
                }

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

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

#Get a list of album types
$album_type_list = AlbumCategory::getCategoryList('norolls');
$smarty->assign('album_type_list',$album_type_list);

//------------------------------------------------------------

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

#Update views
$p->updateViews();

#Get Roll
$roll_info = Album::getRoll($photo_info['photo_id']);
$smarty->assign('roll_info',$roll_info);

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

//------------------------------------------------------------
#Get other videos
//------------------------------------------------------------
#First, calculate how many micro thumbs we're going to display.
	$num_similar_media = floor($photo_info['preview_height'] / ($CONFIG['micro_height']));
	if ($num_similar_media == 0) { $num_similar_media = 10; } ;
	
	$num_similar_videos = floor((.5 * $num_similar_media));
	$similar_videos = $p->getSimilarMedia($album_info['album_id'],$photo_info['photo_id'],$_GET['sort'],$media='video',$num_similar_videos);

	$num_videos = count($similar_videos);
	$num_similar_photos = $num_similar_media - $num_videos;

	$similar_photos = $p->getSimilarMedia($album_info['album_id'],$photo_info['photo_id'],$_GET['sort'],$media='photo',$num_similar_photos);

	$similar_media = array_merge($similar_videos,$similar_photos);
	$smarty->assign('similar_media',$similar_media);
//------------------------------------------------------------


#Count the number of albums in which this photo appears
$album_count=0;
foreach ($photo_albums as $album) {
	if ($album['type'] > 1) {
		$album_count++;
	}
}
if (isset($album_count)) {
	$smarty->assign('album_count',$album_count);
}

#Get Comments
$comment_list = Comment::getLatestComments(10000,$photo_info['photo_id']);
$smarty->assign('comment_list',$comment_list);

#Get Tags
$tag_string = $p->getTagString();
$smarty->assign('tag_string',$tag_string);

$smarty->display($CONFIG['smarty_templates'].'/view.tpl');
?>
