<?php

/**
 * PlaylistFilesTable
 *
 * This class has been auto-generated by the Doctrine ORM Framework
 */
class PlaylistFilesTable extends Doctrine_Table
{
  /**
   * Returns an instance of this class.
   *
   * @return object PlaylistFilesTable
   */
  public static function getInstance()
  {
    return Doctrine_Core::getTable('PlaylistFiles');
  }
    
  /**
   * Add a songs/albums/artists to the playlist
   * @param playlist_id int: playlist id
   * @param id          str: song uniqueid | album/artist id
   * @param type        str: song|artist|album
   * @return            bool: true if songs added to playlist
   */
  public function addPlaylistFiles( $playlist_id, $id, $type )
  {
    $filename_list = array();
    
    $filename_list = Doctrine_Core::getTable('Song')->getFileList( $id, $type );
        
    return $this->addFiles( $playlist_id, $filename_list);
  }
  
  /**
   * Add playlist files by array to the playlist
   * @param playlist_id   int: playlist id
   * @param filename_list arr: array of filenames
   * @return              bool: true if songs added to playlist
   */
  public function addFiles( $playlist_id, $filename_list )
  {
    if( is_array( $filename_list ) && count( $filename_list ) > 0 )
    {
      foreach( $filename_list as $row )
      {
        if ( !$this->isFileInPlaylist( $playlist_id, $row[ 'filename' ] ) )
        {
          $playlist_file = new PlaylistFiles();
          $playlist_file->playlist_id = $playlist_id;
          $playlist_file->filename = $row[ 'filename' ];
          $playlist_file->save();
          $playlist_file->free();
          unset($playlist_file);
        }
      }
      return true;
    }
    else
    {
      return false;
    }
  }
  
  /**
   * Check if a file is already in a playlist and return true if it exists
   * @param playlist_id int: the playlist to check
   * @param filename    str: the filename to check
   * @return            bool: true if file exists in playlist otherwise false
   */
  public function isFileInPlaylist( $playlist_id, $filename )
  {
    $q = Doctrine_Query::create()
      ->select( 'pf.id' )
      ->from( 'PlaylistFiles pf' )
      ->where( 'pf.playlist_id = ?', $playlist_id )
      ->andWhere( 'pf.filename = ?', $filename );
    $result = $q->fetchOne();
    if ( (int) $result > 0 )
    {
      return true;
    }
    else
    {
      return false;
    }
  }
  
  
  /**
   * delete a song from the playlist
   * @param playlist_id     int: playlist id
   * @param $unique_song_id str: unique song id
   * @return                int: affected records
   */
  public function deletePlaylistFile( $playlist_id, $unique_song_id )
  {
    $song = Doctrine_Core::getTable( 'Song' )->getSongByUniqueId( $unique_song_id );
    if ( is_object( $song ) )
    {
      $q = Doctrine_Query::create()
        ->delete( 'PlaylistFiles pf' )
        ->where( 'pf.playlist_id = ?', $playlist_id )
        ->andWhere( 'pf.filename = ?', $song->filename )
        ->execute();
      return $q;
    }
    else
    {
      return 0;
    }
  }
  
  /**
   * Remove all files from a given playlist
   *
   * @param playlist_id int: playlist id
   * @return            int: count of deleted items
   */
   public function deleteAllPlaylistFiles( $playlist_id )
   {
    //delete associated playlist files
    $qpf = Doctrine_Query::create()
      ->delete( 'PlaylistFiles pf' )
      ->where( 'pf.playlist_id = ?', $playlist_id  )
      ->execute();
    return $qpf;
   }
}