Location: PHPKode > scripts > Impleo Music Collection Script > impleo/classes/MusicItem.class.php
<?php
/* ----------------------------------------------------------------------------
 * "THE BEER-WARE LICENSE" (Revision 42):
 * <hide@address.com> wrote this file. As long as you retain this notice you
 * can do whatever you want with this stuff. If we meet some day, and you think
 * this stuff is worth it, you can buy me a beer in return Christoffer Kjeldgaard Petersen
 * ----------------------------------------------------------------------------
 */

class MusicItem
{
    private $id;
	private $artist;
	private $title;
	private $year;
	private $dateAdded;
	private $dateUpdated = null;
	private $arrInfo = array();
	private $imageName = null;
	
	static $sortKey;
	static $order;
	
	public function __construct($id, $artist, $title, $year, $dateAdded, $dateUpdated, $arrDetailInfo, $imageName=null)
	{
		$this->id = $id;
		$this->artist = $artist;
		$this->title = $title;
		$this->year = $year;
		$this->dateAdded = $dateAdded;
		if (strlen($dateUpdated) > 0)
			$this->dateUpdated = $dateUpdated;
		$this->imageName = $imageName;
		
		if (count($arrDetailInfo) > 0)
			$this->arrInfo = $arrDetailInfo;
	}
	
	public function getID() { return $this->id; }
	public function getArtist() { return $this->artist; }
	public function getTitle() { return $this->title; }
	public function getYear() { return $this->year; }
	public function getDateAdded() { return $this->dateAdded; }
	public function getDateUpdated() { return $this->dateUpdated; }
	public function getDetailInfo() { return $this->arrInfo; }
	public function getImageName() { return $this->imageName; }
	
	public function setDetailInfo($arr)
	{
		$this->arrInfo = $arr;
	}
	
	public function toString()
	{
		$str = "ID: {$this->id}";
		$str.= ", Artist: {$this->artist}";
		$str.= ", Title: {$this->title}";
		$str.= ", Year: {$this->year}";
		foreach ($this->arrInfo as $key => $value)
		{
			$str.= ", $key: $value";
		}
		$str.= ", Date added: {$this->dateAdded}";
		$str.= "\n";
		return $str;
	}
	
    /**
     * Compares two music item objects.
     * 
     * @param MusicItem $a
     * @param MusicItem $b
     * @return int
     */
	static function cmp($a, $b)
	{
		$sortKey = self::$sortKey;

		if ($sortKey == "artist" || $sortKey == "title" || $sortKey == "id" || $sortKey == "year")
		{
			return self::cmpValues($a->{$sortKey}, $b->{$sortKey}, self::$order);
		}
		else if (array_key_exists($sortKey, $a->arrInfo))
		{
			return self::cmpValues($a->arrInfo[$sortKey], $b->arrInfo[$sortKey], self::$order);
		}
		return 1;
	}
	
	/**
	 * Sorts an array of MusicItem objects by a specific sort key.
	 *
	 * @param array $collection Reference to the collection to be sorted
	 * @param string $sortKey The sort key (artist, title, year etc.)
	 * @param bool $order Sets the order direction. True for ascending order, false for descending order
	 */
	public static function sortMusic(&$collection, $sortKey, $order)
	{
		if ($sortKey == "id" || $sortKey == "artist" || $sortKey == "title" || $sortKey == "year" || array_key_exists($sortKey, $collection[0]->arrInfo))
		{
			self::$order =  $order;
			self::$sortKey = $sortKey;
			usort($collection, array( __CLASS__, 'cmp'));
		}
	}
	
    /**
     * Compares two music item objects depending on a specified order
     * 
     * @param MusicItem $a
     * @param MusicItem $b
     * @param string $order The order direction. True for ascending order, false for descending order
     * @return int
     */
	private function cmpValues($a, $b, $order)
	{
		if ($a == $b) {
			return 0;
		}
		if ($order)
			return ($a < $b) ? -1 : 1;
		else
			return ($a < $b) ? 1 : -1;
	}
}

?>
Return current item: Impleo Music Collection Script