<?php

class Queue {
   private $id = 0;
   private $type = "";
   private $name = "";
   private $active = false;
   private $length = 0;
   private $songcount = 0;

   public function setID($id = 0) {
      if ( $id != intval($id) ) return false;
      $this->id = intval($id);
      return true;
   }

   public function getID() {
      return $this->id;
   }

   public function setType($type = "") {
      if ( !in_array($type, VALID_QUEUE_TYPES) ) return false;
      $this->type = $type;
      return true;
   }

   public function getType() {
      return $this->type;
   }

   public function isAutomatic() {
      return (($this->type == QUEUETYPE_AUTO ) ? true : false);
   }

   public function setName($name = "") {
      $name = "" . $name;
      if ( $name == "" ) return false;
      $this->name = $name;
      return true;
   }

   public function getName($flag = 0) {
      switch ($flag) {
         case HTMLSAFE:
            return htmlspecialchars($this->name);
            break;
         case HTMLFORMSAFE:
            return htmlspecialchars($this->name, ENT_QUOTES);
            break;
         default:
            return $this->name;
            break;
      }
   }

   public function setActive($active = null) {
      if ( is_null($active) ) return false;
      if ( !is_bool($active) ) $active = ($active == "true") ? true : false;
      switch ($active) {
         case true:
            $this->active = true;
            break;
         case false:
            $this->active = false;
            break;
         default:
            return false;
            break;
      }
      return true;
   }

   public function getActive($flag = 0) {
      switch ($flag) {
         case BOOLEANDB:
            return ($this->active) ? "true" : "false";
            break;
         default:
            return $this->active;
            break;
      }
   }

   public function getLength($format = 0, $flag = 0) {
      global $globaldbh;
      if ( $this->getID() == 0 ) return 0;
      if ( ($this->length == 0) or ($flag == FORCE) ) {
         $query = "SELECT SUM(length) AS total FROM " . QUEUECONTENTSTABLE . " AS qc LEFT JOIN " . SONGSTABLE . " AS s ON qc.songid=s.id WHERE qid=:qid";
         $sth = $globaldbh->prepare($query);
         $fields = array();
         $fields[':qid'] = $this->getID();
         $sth->execute($fields);
         $length = 0;
         if ( $row = $sth->fetch() ) {
            $length = $row['total'];
         }
         $this->length = $length;
      }
      if ( $format == PRETTYLENGTH ) {
         $hours = floor($this->length / 3600);
         $minutes = floor(($this->length - ($hours * 3600)) / 60);
         if ( $hours > 0 ) {
            return $hours . "h:" . $minutes . "m";
         } else {
            return $minutes . "m";
         }
      } else {
         return $this->length;
      }
   }

   public function getSongCount($flag = 0) {
      global $globaldbh;
      if ( $this->getID() == 0 ) return 0;
      if ( ($this->songcount == 0) or ($flag == FORCE) ) {
         $query = "SELECT COUNT(songid) AS songcount FROM " . QUEUECONTENTSTABLE . " WHERE qid=:qid";
         $sth = $globaldbh->prepare($query);
         $fields = array();
         $fields[':qid'] = $this->getID();
         $sth->execute($fields);
         $row = $sth->fetch();
         $this->songcount = $row['songcount'];
      }
      return $this->songcount;
   }

   public static function changeActiveQueue($qid = 0) {
      global $globaldbh;
      $qid = intval($qid);
      if ( $qid <= 0 ) return false;
      $query = "SELECT id FROM " . QUEUESTABLE . " WHERE id=:id";
      $sth = $globaldbh->prepare($query);
      $fields = array();
      $fields[':id'] = $qid;
      $sth->execute($fields);
      if ( $row = $sth->fetch() ) {
         $query = "UPDATE " . QUEUESTABLE . " SET active='false'";
         $sth = $globaldbh->prepare($query);
         $sth->execute();
         $query = "UPDATE " . QUEUESTABLE . " SET active='true' WHERE id=:id";
         $sth = $globaldbh->prepare($query);
         $sth->execute($fields);
         return true;
      } else {
         return false;
      }
   }

   public static function getList($flag = 0) {
      global $globaldbh;
      $query = "SELECT id FROM " . QUEUESTABLE;
      if ( $flag == AUTOQUEUES ) $query .= " WHERE type='" . QUEUETYPE_AUTO . "'";
      $query .= " ORDER BY name";
      $sth = $globaldbh->prepare($query);
      $thelist = array();
      $sth->execute();
      while ( $row = $sth->fetch() ) {
         $thelist[] = new Queue($row['id']);
      }
      return $thelist;
   }

   public function save() {
      global $globaldbh;
      $query = "INSERT INTO " . QUEUESTABLE . " (name) VALUES(:name) ON DUPLICATE KEY UPDATE name=:name";
      $fields = array();
      $fields[':name'] = $this->getName();
      $sth = $globaldbh->prepare($query);
      $sth->execute($fields);
   }

   public function __construct($id = 0) {
      global $globaldbh;
      $query = "SELECT id, type, name, active FROM " . QUEUESTABLE . " WHERE id=:id";
      $fields = array();
      $fields[':id'] = intval($id);
      $sth = $globaldbh->prepare($query);
      $sth->execute($fields);
      if ( $row = $sth->fetch() ) {
         $this->setID($row['id']);
         $this->setType($row['type']);
         $this->setName($row['name']);
         $this->setActive($row['active']);
      }
   }
}

?>