Location: PHPKode > projects > Php4dvd - movie database > php4dvd/lib/db/MovieDataManager.class.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/>.
**/

include_once($loc."/lib/objects/Movie.class.php");

class MovieDataManager extends DataManager { 
	// Add a new movie
	function add($Movie) {
		// Query
		$query  = "INSERT INTO `movies` (imdbid, name, aka, year, duration, rating, languages, country, genres, director, writer, producer, music, cast, taglines, plotoutline, plots, format, own, seen, notes, loaned, loandate, loanname) VALUES ";
		$query .= "('".addslashes($Movie->imdbid)."', ";
		$query .= "'".addslashes($Movie->name)."', ";
		$query .= "'".addslashes($Movie->aka)."', ";
		$query .= "'".addslashes($Movie->year)."', ";
		$query .= "'".addslashes($Movie->duration)."', ";
		$query .= "'".addslashes($Movie->rating)."', ";
		$query .= "'".addslashes($Movie->languages)."', ";
		$query .= "'".addslashes($Movie->country)."', ";
		$query .= "'".addslashes($Movie->genres)."', ";
		$query .= "'".addslashes($Movie->director)."', ";
		$query .= "'".addslashes($Movie->writer)."', ";
		$query .= "'".addslashes($Movie->producer)."', ";
		$query .= "'".addslashes($Movie->music)."', ";
		$query .= "'".addslashes($Movie->cast)."', ";
		$query .= "'".addslashes($Movie->taglines)."', ";
		$query .= "'".addslashes($Movie->plotoutline)."', ";
		$query .= "'".addslashes($Movie->plots)."', ";
		$query .= "'".addslashes($Movie->format)."', ";
		$query .= "'".addslashes($Movie->own)."', ";
		$query .= "'".addslashes($Movie->seen)."', ";
		$query .= "'".addslashes($Movie->notes)."', ";
		$query .= "'".addslashes($Movie->loaned)."', ";
		$query .= "'".addslashes($Movie->loandate)."', ";
		$query .= "'".addslashes($Movie->loanname)."')";
		
		$this->db->insert($query);
		$Movie->id = $this->db->insertId();

		// Update the movie
		$Movie->update();

		return $Movie->id;
	}

	// Remove a movie
	function remove($Movie, $photopath = "", $coverpath = "") {
		// Query
		$query = "DELETE FROM `movies` WHERE id = '".addslashes($Movie->id)."'";
		
		$this->db->delete($query);
		
		// Remove its photo
		if($photopath == "") {
			$Movie->removePhoto();
		} else {
			$Movie->removePhoto($photopath);
		}
		// Remove its cover
		if($coverpath == "") {
			$Movie->removeCover();
		} else {
			$Movie->removeCover($coverpath);
		}
	}
	
	// Search for movies
	function searchMovies($search, $sort = "name", $page = 0, $amount = -1) {
		// List to fill and return
		$return = array();
		
		// Words
		$words = split(" ", $search);
		
		// Sort
		/*switch($sort) {
			case "year":	$sort = "year,name"; 	break;
			case "rating":	$sort = "rating,name"; 	break;
			case "format":	$sort = "format,name"; 	break;
			case "seen":	$sort = "seen,name"; 	break;
			case "own":		$sort = "own,name"; 	break;
		}*/
		
		// Query
		$query  = "SELECT SQL_CALC_FOUND_ROWS * FROM `movies` WHERE ";
		for($i = 0; $i < count($words); $i++) {
			$word = $words[$i];
			
			$query .= "(";
			$query .= "name LIKE '%".addslashes($word)."%' OR ";
			$query .= "aka LIKE '%".addslashes($word)."%'";
			//$query .= "director LIKE '%".addslashes($word)."%' OR ";
			//$query .= "writer LIKE '%".addslashes($word)."%' OR ";
			//$query .= "producer LIKE '%".addslashes($word)."%' OR ";
			//$query .= "cast LIKE '%".addslashes($word)."%'";
			$query .= ")";
			
			// Next word
			if($i + 1 < count($words))
				$query .= " AND ";
		}
		$query .= " ORDER BY ".addslashes($sort).", name";
		if($amount != -1)
			$query .= " LIMIT ".addslashes($page).",".addslashes($amount);
		
		if($rs = $this->db->select($query)) {
			// Create all movies
			while($row = $rs->getNextRow()) {
				$Movie = new Movie();
				$Movie = Factory::FillObject($Movie, $row);
				$return[] = $Movie;
			}
		}
		
		// Return
		return $return;
	}
	
	// Get the amount of rows when doing the search limited
	function getFoundRows() {
		$query  = "SELECT FOUND_ROWS()";
		if($rs = $this->db->select($query)) {
			if($row = $rs->getNextRow())
				return $row[0];
		}
		return 0;
	}

	// Get a movie by its id
	function getMovie($id) {
		// Query
		$query = "SELECT * FROM `movies` WHERE id = '".addslashes($id)."'";

		if($rs = $this->db->select($query)) {
			if($row = $rs->getNextRow()) {
				$Movie = new Movie();
				$Movie = Factory::FillObject($Movie, $row);
				return $Movie;
			}
		}
		
		// Nothing found
		return false;
	}
	
	// Get a movie by its Imdb id
	function getMovieByImdb($imdbid) {
		// Query
		$query = "SELECT * FROM `movies` WHERE imdbid = '".addslashes($imdbid)."'";

		if($rs = $this->db->select($query)) {
			if($row = $rs->getNextRow()) {
				$Movie = new Movie();
				$Movie = Factory::FillObject($Movie, $row);
				return $Movie;
			}
		}
		
		// Nothing found
		return false;
	}
}

?>
Return current item: Php4dvd - movie database