Location: PHPKode > projects > Php4dvd - movie database > php4dvd/includes/movies.inc.php
<?php
/**
================================================================================ 
 LISENCE
================================================================================

    This file is part of php4dvd.

    php4dvd 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.

    php4dvd 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 php4dvd. If not, see <http://www.gnu.org/licenses/>.
**/

/**
 * Some important variables for other users to work with in code or templates:
 * - 'photopath' is the path to the movie photo
 * - 'coverpath' is the path to the cover of the movie
 * - 'movies' are all movies
 * - 'movie' is the requested movie
 */
// Datamanagers
require_once($loc."/lib/db/MovieDataManager.class.php");
$moviedm = new MovieDataManager($db, $settings);

// Photo and cover path
$photopath = $settings["foto"]["movies"];
$w->assign("photopath", $photopath);
$coverpath = $settings["foto"]["covers"];
$w->assign("coverpath", $coverpath);

// If the user logged in or when a guest user is allowed to view movies, show them
if($loggedin || $guestview) {
	/***********************************************************
	 * Retrieve movie information for the viewer
	 ***********************************************************/
	
	// Search for movies
	if(isset($_GET["search"])) {
		$search = $_GET["search"];
		// Vars
		isset($_GET["sort"]) 		? $sort = $_GET["sort"] 			: $sort = "name";
		isset($_GET["amount"]) 		? $amount = (int)$_GET["amount"] 	: $amount = -1;
		isset($_GET["page"]) 		? $page = (int)$_GET["page"] 		: $page = 0;
		
		// Search the database for one more movie
		$movies = $moviedm->searchMovies($search, $sort, $page*$amount, $amount);
		
		// If there are no movies found, reload
		while($page > 0 && count($movies) == 0) {
			$page--;
			$movies = $moviedm->searchMovies($search, $sort, $page*$amount, $amount);
		}
		
		$w->assign("movies", $movies);
		
		// Get the total amount of rows
		$totalmovies = $moviedm->getFoundRows();
		$w->assign("totalmovies", $totalmovies);
		$pages = ceil($totalmovies/$amount);
		$page++;
		$w->assign("page", $page);
		
		// Navigation
		$w->assign("pages", $pages);
		$w->assign("next", $page < $pages);
		$w->assign("previous", $page != 1);
		
		// The amount of pages shown (N before current and N after current)
		$N = 2;
		
		// Define the start value of the pages
		// Start at $page-N (or 1)
		$startAt = $page - $N;
		if($startAt < 1)
			$startAt = 1;
		// Stop at $startAt+2*$N (or $pages)
		$stopAt = $startAt+2*$N;
		if($stopAt > $pages) {
			$stopAt = $pages;
			// Recalculate $stopAt-2*$N
			$startAt = $stopAt-2*$N;
			if($startAt < 1)
				$startAt = 1; 

		}
		$w->assign("startAt", $startAt);
		$w->assign("stopAt", $stopAt);
		
		// Statistics
		$numberdvds = 0;
		$numberbrs = 0;
		$numberowned = 0;
		$numberseen = 0;
		foreach($movies as $m) {
			if($m->format == "DVD")
				$numberdvds++;
			if($m->format == "Blu-ray")
				$numberbrs++;
			if($m->own) 
				$numberowned++;
			if($m->seen) 
				$numberseen++;
		}
		$w->assign("numberdvds", $numberdvds);
		$w->assign("numberbrs", $numberbrs);
		$w->assign("numberowned", $numberowned);
		$w->assign("numberseen", $numberseen);
	}
	
	// Retrieve a movie
	if(isset($_GET["id"])) {
		$movie = $moviedm->getMovie($_GET["id"]);
		if($movie) {
			// Show movie
			$w->assign("movie", $movie);
		} else {
			// Wrong movie, go back
			unset($movie);
			goBack();
		}
	}
	
	// Download cover
	if(isset($_GET["cover"]) && isset($movie) && file_exists($settings["foto"]["covers"].$movie->id.".jpg")) {
		$body = implode('', file($settings["foto"]["covers"].$movie->id.".jpg"));
	
		// Creat the correct header
		$name = preg_replace("/[^\s\.a-zA-Z0-9_-]/", "", $movie->name);
		header("Content-Disposition: attachment; filename=\"".addslashes($name).".jpg\"");
		header("Content-type: application/force-download");
		header("Pragma: cache");
		header("Cache-Control: public, must-revalidate, max-age=0");
		header("Connection: close");
		header("Expires: ".date("r", time()+60*60));
		header("Last-Modified: ".date("r", time()));
		header("Content-length: ".strlen($body)."\r\n");
		echo $body;
		exit();
	}
	
	/***********************************************************
	 * Add new movie
	 ***********************************************************/
	
	// Adding is only possible if the user is logged in and editor
	if(isset($User) && $User->isEditor()) {
		// Search all movies on IMDb with this name
		if(isset($_GET["imdbsearch"])) {
			// What is the search term?
			$imdbsearch = trim($_GET["imdbsearch"]);
			$w->assign("imdbsearch", stripslashes($imdbsearch));
			if($imdbsearch != "") {
				// IMDB engine
				require ($loc."/lib/util/imdbphp/imdb.class.php");
				
				// Search IMDb for the movie
				$imdb = new imdbsearch();
				$imdb->setsearchname($imdbsearch);
				$results = $imdb->results();
				
				// Check if any of these results are allready added to our database
				$temp = array();
				foreach($results as $result) {
					$imdbmovie = $moviedm->getMovieByImdb($result->imdbid());
					if($imdbmovie)
						$result->known = true;
					else
						$result->known = false;
					$temp[] = $result;
				}
				$results = $temp;
				
				$w->assign("results", $results);
			}
		}
		
		// Find movie information on IMDb
		if(isset($_GET["imdbid"])) {
			// If no name, go back
			$imdbid = trim($_GET["imdbid"]);
			if($imdbid == "")
				goBack();
			
			// Already added to the database?
			$imdbmovie = $moviedm->getMovieByImdb($imdbid);
			if($imdbmovie)
				$w->assign("known", true);
			
			// IMDB engine
			require ($loc."/lib/util/imdbphp/imdb.class.php");
			
			// Search at IMDB by id
			$movie = new imdb($imdbid);	
			$w->assign("movie", $movie);
		}
		
		// Add a movie
		if(isset($_POST["add"]) && $_POST["add"] == 1) {
			$Movie = new Movie();
			$Movie = Factory::FillObject($Movie, $_POST);
			if(!$Movie->loaned) {
				$Movie->loandate = "";
				$Movie->loanname = "";
			}
			$Movie->id = $moviedm->add($Movie);
			
			// Save its image
			if(isset($Movie->image)) {
				// IMDB engine
				require ($loc."/lib/util/imdbphp/imdb.class.php");
				$movie = new imdb($Movie->imdbid);
				$movie->savephoto($photopath.$Movie->id.".jpg");
			}
			// Save its cover
			if(isset($_FILES["cover"]) && isset($_FILES["cover"]["size"]) && $_FILES["cover"]["size"] > 0) {
				$Movie->addCover("cover", $coverpath);
			}
			
			// Go to add an other movie
			header("Location: ./?go=add");
			exit();
		}
	}
	
	/***********************************************************
	 * Update movie information
	 ***********************************************************/
	
	// Updating is only possible if the user is logged in and editor
	if(isset($User) && $User->isEditor()) {
		// Remove a movie
		if(isset($_GET["delete"])) {
			$movie = $moviedm->getMovie($_GET["delete"]);
			if($movie) {
				$moviedm->remove($movie, $photopath, $coverpath);
			}
		}
		
		// Update movie
		if(isset($movie) && isset($_POST["update"]) && $_POST["update"] == 1 && isset($_GET["go"]) && $_GET["go"] == "edit") {
			$movie = Factory::FillObject($movie, $_POST);
			if(!$movie->loaned) {
				$movie->loandate = "";
				$movie->loanname = "";
			}
			$movie->update();
			
			// Save its cover
			if(isset($_FILES["cover"]) && isset($_FILES["cover"]["size"]) && $_FILES["cover"]["size"] > 0) {
				$movie->addCover("cover", $coverpath);
			}
			
			// Go back
			goBack();
		}
		
		// Change the seen state with ajax
		if(isset($movie) && isset($_GET["seen"])) {
			$movie->seen = $_GET["seen"];
			$movie->update();
			// Ajax call, no more parsing
			exit();
		}
		
		// Change the own state with ajax
		if(isset($movie) && isset($_GET["own"])) {
			$movie->own = $_GET["own"];
			$movie->update();
			// Ajax call, no more parsing
			exit();
		}
		
		// Remove cover
		if(isset($movie) && isset($_GET["removecover"]) && $_GET["removecover"] == 1 && $movie->hasCover($coverpath)) {
			$movie->removeCover($coverpath);
			
			// Go back
			goBack();
		}
	}
}
?>
Return current item: Php4dvd - movie database