/** * Return HTML for an iTunes-esque table for browsing through audio files. TODO: implement ajax onkeyup auto filtering. */ function audio_playlist_browse($playlist_id = NULL) { global $user; // change some settings if we are managing a specific playlist if (is_numeric($playlist_id)) { $pager_length = 7; $children = playlist_get_children($playlist_id, 'audio_playlist'); } else { $pager_length = variable_get('audio_playlist_browser', 20); } //The blank field => '' is used because it creates nice $_get['sort'] and $_get['order'] URLS when clicked. $header = array( array(), array(), array(), array(), array('data' => t('Name'), 'field' => ''), array('data' => t('Time'), 'field' => ''), array('data' => t('Artist'), 'field' => ''), array('data' => t('Album'), 'field' => ''), array('data' => t('Genre'), 'field' => ''), array('data' => t('Track'), 'field' => '') ); //We have to build our own method to tablesort the SQL because the audio metadata is normalized //array used to convert human readable name to a database field name $order_by = array('Name' => 'title', 'Time' => 'playtime', 'Artist' => 'artist', 'Album' => 'album', 'Genre' => 'genre', 'Track' => 'track'); $tag = isset($_GET['order']) ? $_GET['order'] : ''; $sort = isset($_GET['sort']) ? $_GET['sort'] : ''; $keys = $_GET['edit']['search']; // Replace wildcards with MySQL/PostgreSQL wildcards. $keys = preg_replace('!\*+!', '%', $keys); $keys = check_plain($keys); //because playtime is stored in the audio table and not as a tagged value, we have to add extra logic. if ($keys && $order_by[$tag] == 'playtime') { $result = pager_query("SELECT DISTINCT a.value, n.nid FROM {node} n INNER JOIN {audio_metadata} a ON n.vid = a.vid INNER JOIN {audio} af ON n.vid = af.vid WHERE n.status = 1 AND LOWER(a.value) LIKE LOWER('%%%s%%') ORDER BY af.playtime %s", $pager_length, 0, null, $keys, $sort); } else if ($keys) { $result = pager_query("SELECT DISTINCT a.value, n.nid FROM {node} n INNER JOIN {audio_metadata} a ON n.vid = a.vid INNER JOIN {audio} af ON n.vid = af.vid WHERE n.status = 1 AND LOWER(a.value) LIKE LOWER('%%%s%%') ORDER BY a.clean %s", $pager_length, 0, null, $keys, $sort); } else if ($order_by[$tag] == 'playtime') { $qry_cnt = "SELECT count(DISTINCT n.nid) FROM {node} n INNER JOIN {audio_metadata} a ON n.vid = a.vid INNER JOIN {audio} af ON n.vid = af.vid WHERE n.status = 1 ORDER BY af.playtime"; $result = pager_query("SELECT DISTINCT af.playtime, n.nid, a.vid FROM {node} n INNER JOIN {audio_metadata} a ON n.vid = a.vid INNER JOIN {audio} af ON n.vid = af.vid WHERE n.status = 1 ORDER BY af.playtime %s", $pager_length, 0, $qry_cnt, $sort); } else if ($tag && $sort) { $result = pager_query("SELECT DISTINCT a.value, n.nid FROM {node} n INNER JOIN {audio_metadata} a ON n.vid = a.vid INNER JOIN {audio} af ON n.vid = af.vid WHERE a.tag = '%s' AND n.status = 1 ORDER BY a.clean %s", $pager_length, 0, NULL, $order_by[$tag], $sort); } else { $qry_cnt = "SELECT count(DISTINCT n.nid) FROM {node} n INNER JOIN {audio_metadata} a ON n.vid = a.vid INNER JOIN {audio} af ON n.vid = af.vid WHERE n.status = 1"; $result = pager_query("SELECT DISTINCT n.nid, a.vid FROM {node} n INNER JOIN {audio_metadata} a ON n.vid = a.vid INNER JOIN {audio} af ON n.vid = af.vid WHERE n.status = 1 ORDER BY n.created DESC", $pager_length, 0, $qry_cnt); } while ($row = db_fetch_object($result)) { $audio = node_load($row->nid); $title = $audio->audio_tags['title'] ? $audio->audio_tags['title'] : $audio->title; $edit_txt = node_access('update', $audio, $user->uid) ? l(t('[edit]'), 'node/'.$audio->nid .'/edit') : ""; // if a specific playlist id is sent, then we must change the $add_txt. if (is_numeric($playlist_id)) { $add_txt = !in_array($audio->nid, $children) ? l(t('[add]'), "playlist/manage/audio_playlist/{$playlist_id}/add/{$audio->nid}") : ""; } else { $add_txt = user_access('create audio playlist') ? l(t('[add to... ]'), 'playlist/add/audio_playlist/' . $audio->nid) : ""; } $rows[] = array('data' => array( // Cells "". $add_txt ."", theme('audio_mp3_player', $audio), "". l(t('[info]'), 'node/'.$audio->nid) ."", "". $edit_txt ."", audio_playlist_replacer($title, $keys), $audio->audio_fileinfo['playtime'], audio_playlist_replacer($audio->audio_tags['artist'], $keys), audio_playlist_replacer($audio->audio_tags['album'], $keys), audio_playlist_replacer($audio->audio_tags['genre'], $keys), $audio->audio_tags['track'] ), ); } if (!$rows) { $rows[] = array(array('data' => t('No audio was found.'), 'colspan' => 7)); } //Build the form, that uses METHOD = GET to filter the search results. //TODO: use POST instead? $form['search'] = array( '#type' => 'textfield', '#prefix' => '