ComunicAPI/classes/components/movies.php

110 lines
2.5 KiB
PHP
Raw Normal View History

2018-01-03 08:53:10 +00:00
<?php
/**
* Users personnal movies
*
* @author Pierre HUBERT
*/
class Movies {
/**
* Movies table name
*/
const MOVIES_TABLE = "galerie_video";
2018-01-05 14:12:42 +00:00
/**
* Get the entire list of movies of a user
*
* @param int $userID The ID of the user to get
* @return array The list of movie of the user
*/
public function get_list(int $userID) : array {
//Perform a request on the database
$conditions = "WHERE ID_user = ? ORDER BY ID DESC";
$values = array($userID);
$results = CS::get()->db->select($this::MOVIES_TABLE, $conditions, $values);
$movies = array();
foreach($results as $row)
2018-04-22 06:53:12 +00:00
$movies[] = $this->dbToMovie($row);
2018-01-05 14:12:42 +00:00
return $movies;
}
2018-01-03 08:53:10 +00:00
/**
* Get informations about a movie
*
* @param int $movieID The ID of the target movie
2018-04-22 06:53:12 +00:00
* @return Movie Information about the movie (empty in case of failure)
2018-01-03 08:53:10 +00:00
*/
2018-04-22 06:53:12 +00:00
public function get_infos(int $movieID) : Movie {
2018-01-03 08:53:10 +00:00
//Perform a request in the database
$condition = "WHERE ID = ?";
$condValues = array($movieID);
$result = CS::get()->db->select($this::MOVIES_TABLE, $condition, $condValues);
//Check if we got a response
if(count($result) == 0)
return array();
2018-04-22 06:53:12 +00:00
return $this->dbToMovie($result[0]);
2018-01-03 08:53:10 +00:00
}
2018-01-06 18:03:26 +00:00
/**
* Check whether a movie specified by its ID exists or not
*
* @param int $movieID The ID of the movie to check
* @return bool TRUE if the movie exists / false else
*/
public function exist(int $movieID) : bool {
2018-01-06 18:03:26 +00:00
return CS::get()->db->count($this::MOVIES_TABLE, "WHERE ID = ?", array($movieID)) > 0;
}
/**
* Get the ID of the owner of a movie
*
* @param int $movieID The ID of the target movie
* @return int The ID of the owner of the movie / 0 if none found
*/
public function get_owner(int $movieID) : int {
//Get infos about the movie
2018-04-22 06:53:12 +00:00
$movie = $this->get_infos($movieID);
if(!$movie->isValid())
return 0;
//Return the ID of the owner of the movie
2018-04-22 06:53:12 +00:00
return $movie->get_userID();;
}
2018-01-03 08:53:10 +00:00
/**
2018-04-22 06:53:12 +00:00
* Convert database entry into movie object
2018-01-03 08:53:10 +00:00
*
2018-04-22 06:53:12 +00:00
* @param array $entry The database entry
* @return Movie Generated object
2018-01-03 08:53:10 +00:00
*/
2018-04-22 06:53:12 +00:00
private function dbToMovie(array $entry) : Movie {
2018-01-03 08:53:10 +00:00
2018-04-22 06:53:12 +00:00
$movie = new Movie();
2018-01-03 08:53:10 +00:00
2018-04-22 06:53:12 +00:00
$movie->set_id($entry["ID"]);
$movie->set_uri($entry["URL"]);
$movie->set_url(path_user_data($movie->get_uri()));
$movie->set_userID($entry["ID_user"]);
$movie->set_name($entry["nom_video"]);
$movie->set_file_type($entry["file_type"]);
$movie->set_size($entry["size"]);
2018-01-03 08:53:10 +00:00
2018-04-22 06:53:12 +00:00
return $movie;
2018-01-03 08:53:10 +00:00
}
}
//Register component
Components::register("movies", new Movies());