/** * Handle a search query from the GET/POST variables. * @author Ben Dodson */ function handleSearch($search_string = false, $search_type = false) { global $jzUSER; $root =& new jzMediaNode(); $timer = microtime_float(); if ($search_string === false) { $search_string = $_GET['search_query']; } if ($search_type === false) { $search_type = $_GET['search_type']; } $string_array = splitKeywords($search_string); $keywords = $string_array['keywords']; $string = $string_array['search']; if (isset($keywords['genres'])) { $locations[sizeof($locations)] = 'genre'; $search_type = "genres"; } if (isset($keywords['artists'])) { $locations[sizeof($locations)] = 'artist'; $search_type = "artists"; } if (isset($keywords['albums'])) { $locations[sizeof($locations)] = 'album'; $search_type = "albums"; } if (isset($keywords['tracks'])) { $locations[sizeof($locations)] = 'track'; $search_type = "tracks"; } if (isset($keywords['lyrics'])) { $search_type = "lyrics"; } if (isset($keywords['radio'])) { $search_type = "artists"; $max_res = 1; } else { if (isset($keywords['limit'])) { $max_res = $keywords['limit']; } else { $max_res = 100; } } switch (strtolower($search_type)) { case "all": $stype = "both"; $distance = -1; break; case "genres": case "genre": $stype = "nodes"; $distance = distanceTo("genre"); break; case "artists": case "artist": $stype = "nodes"; $distance = distanceTo("artist"); break; case "albums": case "album": $stype = "nodes"; $distance = distanceTo("album"); break; case "tracks": case "track": $stype = "tracks"; $distance = -1; break; case "lyrics": case "lyric": $stype = "lyrics"; $distance = -1; break; case "best": default: $stype = "both"; $distance = -1; $keywords['best'] = true; } if ($distance === false) { die("Could not search for {$search_type}."); } // Are they searching by ID explicitly? if (isset($keywords['id'])) { $stype = "id"; // We handle this differently than above in // case they set @genre and @id (or whatever). } /* if we have 2 locations, the closest to the root is our anchor and the further is our return type. */ if (sizeof($locations) > 1) { if ($locations[1] == 'track') { $r = 'tracks'; } else { $r = 'nodes'; } $limit = 1; if (isset($keywords['limit'])) { $limit = $keywords['limit']; } $results = $root->search($string, "nodes", distanceTo($locations[0]), 1, 'exact'); if (sizeof($results) > 0) { $results = $results[0]->getSubNodes($r, distanceTo($locations[1], $results[0]), true, $limit); } else { $results = $root->search($string, "nodes", distanceTo($locations[0]), 1); if (sizeof($results) > 0) { $results = $results[0]->getSubNodes($r, distanceTo($locations[1], $results[0]), true, $limit); } } } else { if (isset($keywords['play']) || isset($keywords['radio'])) { $results = $root->search($string, $stype, $distance, 1, 'exact'); if (sizeof($results) == 0) { $results = $root->search($string, $stype, $distance, 1); // better to limit 1 or $max_res? } } else { if (isset($keywords['best'])) { $results = $root->search($string, $stype, $distance, -1, 'exact'); if (sizeof($results) == 0) { $results = $root->search($string, $stype, $distance, $max_res); // better to limit 1 or $max_res? } } else { $results = $root->search($string, $stype, $distance, $max_res); } } } if (sizeof($results) == 0) { // Maybe a search by ID will work... $results = $root->search($string, "id", $distance, $max_res); if (sizeof($results) == 0) { return $results; } } $timer = round(microtime_float() - $timer, 2); writeLogData('search', "searched '{$search_type}' for '{$string}' in {$timer} seconds."); // What about keywords? if (isset($keywords['play']) && sizeof($results) > 0) { $pl = new jzPlaylist(); $pl->add($results); // if (isset($keywords['limit'])) { // $pl->flatten(); //} if (isset($keywords['random'])) { $pl = $pl->getSmartPlaylist($max_res); } else { if (isset($keywords['limit'])) { $pl->flatten(); $pl->truncate($max_res); } } $pl->play(); exit; } else { if (isset($keywords['radio'])) { $pl = new jzPlaylist(); $pl->add($results); $pl = $pl->getSmartPlaylist(50, "radio"); $pl->play(); exit; } } return $results; }
/** * Draws the search results page. * * @author Ben Dodson * @version 12/18/04 * @since 11/20/04 */ function searchResults($string, $type, $power_search = false) { global $cms_type, $jzSERVICES; $display =& new jzDisplay(); $blocks =& new jzBlocks(); $tracks = array(); $nodes = array(); // remember, $this is a frontend. // This has to go before SQL querries. // If our keywords say to play the results // we cannot print any HTML. if ($power_search === false) { $check = splitKeywords($string); if (!muteOutput($check['keywords'])) { $display->preheader('Search Results', $this->width, $this->align); $this->pageTop('Search Results'); } $results = handleSearch($string, $type); if (sizeof($results) == 0 && muteOutput($check['keywords'])) { $display->preheader('Search Results', $this->width, $this->align); $this->pageTop('Search Results'); } } else { // Power search: $display->preheader('Search Results', $this->width, $this->align); $this->pageTop('Search Results'); $root = new jzMediaNode(); $results = $root->powerSearch(); } echo '<table width="100%" cellpadding="3"><tr><td>'; foreach ($results as $val) { if ($val->isLeaf()) { $tracks[] = $val; } else { $nodes[] = $val; } } // show the page if (sizeof($tracks) > 0) { $blocks->blockHeader(sizeof($tracks) . " " . word("Matching Tracks") . " " . word("for search") . ' "' . $_POST['search_query'] . '"'); $blocks->blockBodyOpen(); $blocks->trackTable($tracks, "search"); $blocks->blockBodyClose(); $blocks->blockSpacer(); } if (sizeof($nodes) > 0) { $blocks->blockHeader(sizeof($nodes) . " " . word("Other Matches") . " " . word("for search") . ' "' . $_POST['search_query'] . '"'); $blocks->blockBodyOpen(); $blocks->nodeTable($nodes, "search"); $blocks->blockBodyClose(); $blocks->blockSpacer(); } if (sizeof($nodes) == 0 && sizeof($tracks) == 0) { $blocks->blockHeader(word("No matches found")); } $this->footer(); $jzSERVICES->cmsClose(); }