Exemplo n.º 1
0
 public static function getPlaylistSongs($search_words, $sort_letter, $start, $limit, $search_fields)
 {
     //Set some bounds
     if ($start <= 0) {
         $start = 0;
     }
     if ($limit <= 5) {
         $limit = 5;
     }
     //Set search filters
     $doSearchTitle = !$search_fields || $search_fields == 't';
     // Title
     $doSearchArtist = !$search_fields || $search_fields == 'a';
     // Artist
     $doSearchAlbum = !$search_fields || $search_fields == 'album';
     // Album
     $doSearchGenre = !$search_fields || $search_fields == 'g';
     // Genre
     $db = Database::getInstance();
     $select_where = $db->select();
     $select_where->where('songtype = ?', 'S')->where('status = ?', 0);
     /* ORIGINAL CODE
     		
     		if (is_array($search_words)) {
     			reset($search_words);
     			while (list($key, $val) = each($search_words)) {
     				$val = "%$val%";
     				
     				if($doSearchTitle)
     					$whereFields[] = $db->quoteInto('(title like ?)', $val);
     				if($doSearchArtist)
     					$whereFields[] = $db->quoteInto('(artist like ?)', $val);
     				if($doSearchAlbum)
     					$whereFields[] = $db->quoteInto('(album like ?)', $val);
     				
     				$orWhere[] = implode(' OR ', $whereFields);
     			}
     
     			$select_where->where(implode(' OR ', $orWhere));
     		}
     		*/
     if (is_array($search_words)) {
         reset($search_words);
         while (list($key, $val) = each($search_words)) {
             // $val = search string
             // Check if string contains one word, or multiple.
             // If only one word in the string is present, use % for wildcard around $val to help expand results.
             $val = "{$val}%";
             if (preg_match("/ /", $val)) {
                 $val = "%{$val}%";
             } else {
                 $val = "%{$val}%";
             }
             if ($doSearchTitle) {
                 $whereFields[] = $db->quoteInto('(title like ?)', $val);
             }
             if ($doSearchArtist) {
                 $whereFields[] = $db->quoteInto('(artist like ?)', $val);
             }
             if ($doSearchAlbum) {
                 $whereFields[] = $db->quoteInto('(album like ?)', $val);
             }
             if ($doSearchGenre) {
                 $whereFields[] = $db->quoteInto('(genre like ?)', $val);
             }
             $orWhere[] = implode(' OR ', $whereFields);
         }
         $select_where->where(implode(' OR ', $orWhere));
     }
     if ($sort_letter == '0') {
         $select_where->where($db->quoteInto('NOT((artist>=?)', 'A') . ' AND ' . $db->quoteInto('(artist<?))', 'ZZZZZZZZZZZ'));
     } elseif ($sort_letter != '') {
         $nextletter = chr(ord($sort_letter) + 1);
         $select_where->where($db->quoteInto('(artist>=?)', $sort_letter) . ' AND ' . $db->quoteInto('(artist<?)', $nextletter));
     }
     //Calculate total
     $total_select = clone $select_where;
     $total_select->from('songlist', array('cnt' => 'count(*)'));
     try {
         $row = $db->fetchRow($total_select);
     } catch (Zend_Db_Adapter_Exception $ex) {
         echo "Please verify database settings.<br />";
         exit;
     }
     self::$playlistSongCount = $row['CNT'];
     //Now grab a section of that
     $playlist_select = $select_where;
     $playlist_select->from('songlist')->order(array('artist ASC', 'title ASC'))->limit($limit, $start);
     try {
         $rows = $db->fetchAll($playlist_select);
         $songs = array();
         foreach ($rows as $key => $row) {
             $songs[$key] = new self();
             $songs[$key]->setValues($row);
         }
     } catch (Zend_Db_Adapter_Exception $ex) {
         echo "Please verify database settings.<br />";
         exit;
     }
     return $songs;
 }