<?php

require_once 'DatabaseConnector.php';

class Videos extends DatabaseConnector
{

   /**
    * Pobierz filmy
    * @access remote
    * @returns tablica z filmami
    */
   function getVideos($categoryID=-1, $videoID=-1)
   {
      if ($categoryID == -1 || strlen($categoryID) == 0)
      {
         trigger_error("Aby otrzymać listę filmów podaj numer identyfikacyjny kategorii");
      }

      $vidSQL = "";
      $rows = array();

      // Czy szukamy konkretnego filmu?
      if ($videoID != -1 && strlen($videoID) != 0)
      {
         $vidSQL = " AND v.id=" . $videoID;
      }

      $link = $this->getConnection();

      $query = sprintf("SELECT
        v.name,
        v.src,
        v.id as 'vid', 
        c.name as 'cat', 
        v.description,
        v.count
        FROM categories c, videos v
        WHERE v.catId=c.id
        AND v.catId=%d
        AND v.active=1 " . $vidSQL, $categoryID);

      $result = mysql_query($query, $link);

      if (mysql_num_rows($result) == 0)
      {
         trigger_error("Nie znaleziono żadnych filmów");
         return false;
      }

      while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
      {
         array_push($rows, $row);
      }

      return $rows;
   }

   public function updatePlayCount($vid)
   {
      $link = $this->getConnection();

      $query = sprintf("SELECT count FROM videos WHERE id=%d", $vid);
      $result = mysql_query($query, $link);

      if (mysql_num_rows($result) > 0)
      {
         $row = mysql_fetch_row($result);

         $count = $row[0];
         $count++;

         $queryUpdate = sprintf("UPDATE videos SET count=%d WHERE id=%d", $count, $vid);
         mysql_query($queryUpdate, $link);

         return array('count' => $count);

      }
      return false;
   }

   private function deleteVideo($vid)
   {
      $vid;
      // Tutaj znajdzie się obsługa usuwania filmów
   }
	
}

?>
