<?php



require_once dirname( __FILE__ ) . '/Music.php';

class MusicLibrary 
{
   /**
    * loaded library
    * @var array
    */
    public static $library = array();
    
   /**
    * the entry to be displayed
    * @var object
    */
    public static $looking = null;
		
   /**
    * load music database 
    * 
    * Database is a tab-separated list (CSV-file)
    * 
    * @param string $path to file
    */      
	public static function load( $path ) 
    {
		$fp = fopen( dirname( __FILE__ ) .'/' . $path, 'r' );
		
        // skip column headings
		fgets( $fp ); 
		
		while( $line = fgets( $fp ) ) {
			self::$library[] = Music::parse( $line );
		}
		//var_dump(self::$library);
		fclose( $fp );
	}
	
	/**
	 * @param Music m 
	 */
	 	
    static function select( $m ) {
        self::$looking = $m;
    }
    
   /**
    * 
    * 
    * @return int
    */
    static function displayCount() 
    {
        return count( self::$library );
    }
    
   /**
    * 
    * list of content
    * @return array $displayed music items
    */
    static function displayContents() {
        $displayed  = array();
        $count      = self::displayCount();
        
        for( $i = 0; $i < $count; ++$i ) {
            if( self::$library[$i]->selected ) {
                array_push( $displayed, self::$library[$i] );
            }
        }
        echo "<pre>\n"; print_r( $displayed ); echo "</pre>\n";
        return $displayed;    
    }
}
?>
