<?php

class Song {
   private $id = 0;
   private $path = "";
   private $song = "";
   private $timesplayed = 0;
   private $genre = "";
   private $title = "";
   private $artist = "";
   private $album = "";
   private $year = "";
   private $length = 0;
   private $artfile = "";
   private $parsed = false;

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

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

   public function getSong($flag = 0) {
      switch ($flag) {
         case HTMLSAFE:
            return htmlspecialchars($this->song);
            break;
         case HTMLFORMSAFE:
            return htmlspecialchars($this->song, ENT_QUOTES);
            break;
         case NOEXT:
            return htmlspecialchars(substr($this->song, 0, -4));
            break;
         default:
            return $this->song;
            break;
      }
   }

   public function getTimesPlayed() {
      return intval($this->timesplayed);
   }

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

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

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

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

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

   public function getLength() {
      return intval($this->length);
   }

   public function getArtFile($flag = 0) {
      global $artbygenre;
      switch ($flag) {
         case ARTURL:
            if ( $this->artfile == "" ) {
               if ( array_key_exists($this->getGenre(), $artbygenre) ) {
                  $ret = "img/" . $artbygenre[$this->getGenre()];
               } else {
                  $ret = "img/no_art.jpg";
               }
            } else {
               $ret = "art/" . $this->artfile;
            }
            return $ret;
            break;
         case HTMLSAFE:
            return htmlspecialchars($this->artfile);
            break;
         case HTMLFORMSAFE:
            return htmlspecialchars($this->artfile, ENT_QUOTES);
            break;
         default:
            return $this->artfile;
            break;
      }
   }

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

   public static function getSearchList($searchfor = "" ) {
      global $globaldbh;
      $thelist = array();
      $searchfor = trim($searchfor);
      $searchfor = str_replace('%', "", $searchfor);
      $terms = explode(" ", $searchfor);
      $likematch = "%";
      foreach ( $terms as $term ) {
         $likematch .= $term . "%";
      }
      //$likematch = "%" . $searchfor . "%";
      $searchfor = str_replace(array("'", "+", "-"), "", $searchfor);
      $searchfor = "+" . preg_replace("/\s+/", " +", $searchfor);
      if ( $searchfor == "" ) return $thelist;
      $query = "SELECT id FROM " . SONGSTABLE . " WHERE CONCAT(path, song, artist) LIKE :likematch ORDER BY path, song LIMIT " . intval(SEARCHLIMIT);
      //$query = "SELECT id FROM " . SONGSTABLE . " WHERE MATCH (song, path, artist, album, title) AGAINST (:searchfor IN BOOLEAN MODE) OR CONCAT(path, song) LIKE :likematch ORDER BY path, song LIMIT " . intval(SEARCHLIMIT);
      $sth = $globaldbh->prepare($query);
      $fields = array();
      //$fields[':searchfor'] = $searchfor;
      $fields[':likematch'] = $likematch;
      $sth->execute($fields);
      while ( $row = $sth->fetch() ) {
         $thelist[] = new Song($row['id']);
      }
      return $thelist;
   }

   public function __construct($reqid = 0) {
      global $globaldbh;
      $reqid = intval($reqid);
      $query = "SELECT id, path, song, timesplayed, genre, title, artist, album, year, length, artfile, parsed FROM " . SONGSTABLE . " WHERE id=:id";
      $sth = $globaldbh->prepare($query);
      $fields = array();
      $fields[':id'] = $reqid;
      $sth->execute($fields);
      if ( $row = $sth->fetch() ) {
         $this->id = $row['id'];
         $this->path = $row['path'];
         $this->song = $row['song'];
         $this->timesplayed = $row['timesplayed'];
         $this->genre = $row['genre'];
         $this->title = $row['title'];
         $this->artist = $row['artist'];
         $this->album = $row['album'];
         $this->year = $row['year'];
         $this->length = $row['length'];
         $this->artfile = $row['artfile'];
         $this->parsed = ($row['path'] == 'true') ? true : false;
      }
   }

}

?>