summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorKarl 'vollmerk' Vollmer <vollmer@ampache.org>2005-09-08 18:02:12 +0000
committerKarl 'vollmerk' Vollmer <vollmer@ampache.org>2005-09-08 18:02:12 +0000
commitbacb5b9a62c475d6583dd3cec87b169859a27335 (patch)
treec689672b42bd18d3ff344b1321158ed547fb563e /lib
parent23fdc1659cf38e895e76a4f0b4767221db464bfa (diff)
downloadampache-bacb5b9a62c475d6583dd3cec87b169859a27335.tar.gz
ampache-bacb5b9a62c475d6583dd3cec87b169859a27335.tar.bz2
ampache-bacb5b9a62c475d6583dd3cec87b169859a27335.zip
new search mojo, only song object type current works...
Diffstat (limited to 'lib')
-rw-r--r--lib/general.lib.php13
-rw-r--r--lib/search.php276
2 files changed, 144 insertions, 145 deletions
diff --git a/lib/general.lib.php b/lib/general.lib.php
index 7eebe074..3dbe6d23 100644
--- a/lib/general.lib.php
+++ b/lib/general.lib.php
@@ -796,4 +796,17 @@ function check_username($username) {
} // check_username
+/**
+ * scrub_out
+ * This function is used to escape user data that is getting redisplayed
+ * onto the page, it htmlentities the mojo
+ */
+function scrub_out($str) {
+
+ $str = htmlentities($str);
+
+ return $str;
+
+} // scrub_out
+
?>
diff --git a/lib/search.php b/lib/search.php
index 54192750..65a87376 100644
--- a/lib/search.php
+++ b/lib/search.php
@@ -22,164 +22,150 @@
*/
-/*!
- @function run_search
- @discussion run a search, takes string,field,type and returns an array
- of results of the correct type (song, album, artist)
-*/
-function run_search($string,$field,$type) {
-
- // Clear this so it doesn't try any fanzy view mojo on us
- unset($_SESSION['view_script']);
- // Escape input string
- $string = sql_escape($string);
-
- // Switch on the field --> type and setup sql statement
- switch ($field === 0 ? '': $field) {
+/**
+ * run_search
+ * this function actually runs the search, and returns an array of the results. Unlike the previous
+ * function it does not do the display work its self.
+ * @package Search
+ * @catagory Search
+ */
+function run_search($data) {
+
+ /* Create an array of the object we need to search on */
+ foreach ($data['search_object'] as $type) {
+ /* generate the full name of the textbox */
+ $fullname = $type . "_string";
+ $search[$type] = sql_escape($data[$fullname]);
+ } // end foreach
+
+ /* Figure out if they want a AND based search or a OR based search */
+ switch($_REQUEST['method']) {
+ case 'fuzzy':
+ $method = 'OR';
+ break;
+ default:
+ $method = 'AND';
+ break;
+ } // end switch on method
+
+ /* Switch, and run the correct function */
+ switch($_REQUEST['object_type']) {
case 'artist':
- if ($type === 'fuzzy') {
- $sql = "SELECT id FROM artist WHERE name LIKE '%$string%'";
- }
- else {
- $sql = "SELECT id FROM artist WHERE name ='$string'";
- }
- $artists = get_artists($sql, 'format');
- if ($artists) {
- show_artists($artists);
- }
- else {
- echo "<div class=\"error\" align=\"center\">" . _("No Results Found") . "</div>";
- }
- break;
-
case 'album':
- if ($type === 'fuzzy') {
- $sql = "SELECT id FROM album WHERE name LIKE '%$string%'";
- }
- else {
- $sql = "SELECT id FROM album WHERE name='$string'";
- }
- $albums = get_albums($sql);
- if (count($albums)) {
- show_albums($albums);
- }
- else {
- echo "<div class=\"error\" align=\"center\">" . _("No Results Found") . "</div>";
+ case 'genre':
+ case 'song':
+ $function_name = 'search_' . $_REQUEST['object_type'];
+ if (function_exists($function_name)) {
+ $results = call_user_func($function_name,$search,$method);
+ return $results;
}
- break;
+ default:
+ $results = search_song($search,$method);
+ return $results;
+ break;
+ } // end switch
- case 'song_title':
- if ($type === 'fuzzy') {
- $sql = "SELECT id FROM song WHERE title LIKE '%$string%'";
- }
- else {
- $sql = "SELECT id FROM song WHERE title = '$string'";
- }
- $song_ids = get_songs($sql, 'format');
- if ($song_ids) {
- show_songs($song_ids);
- }
- else {
- echo "<div class=\"error\" align=\"center\">" . _("No Results Found") . "</div>";
- }
- break;
+ return false;
- case 'song_genre':
- if ($type === 'fuzzy') {
- $sql = "SELECT song.id FROM song,genre WHERE song.genre=genre.id AND genre.name LIKE '%$string%'";
- }
- else {
- $sql = "SELECT song.id FROM song,genre WHERE song.genre=genre.id AND genre.name='$string'";
- }
- $song_ids = get_songs($sql, 'format');
- if ($song_ids) {
- show_songs($song_ids);
- }
- else {
- echo "<div class=\"error\" align=\"center\">" . _("No Results Found") . "</div>";
- }
- break;
+} // run_search
- case 'song_year':
- if ($type === 'fuzzy') {
- $sql = "SELECT song.id FROM song WHERE song.year LIKE '%$string%'";
- }
- else {
- $sql = "SELECT song.id FROM song WHERE song.year='$string'";
- }
- $song_ids = get_songs($sql, 'format');
- if ($song_ids) {
- show_songs($song_ids);
- }
- else {
- echo "<div class=\"error\" align=\"center\">" . _("No Results Found") . "</div>";
- }
+/**
+ * search_song
+ * This function deals specificly with returning song object for the run_search
+ * function, it assumes that our root table is songs
+ * @package Search
+ * @catagory Search
+ */
+function search_song($data,$method) {
+
+ /* Generate BASE SQL */
+ $base_sql = "SELECT song.id FROM song";
+ $where_sql = '';
+ $table_sql = ',';
+
+ foreach ($data as $type=>$value) {
+
+ switch ($type) {
+ case 'title':
+ $where_sql .= " song.title LIKE '%$value%' $method";
break;
-
- case 'song_length':
- case 'song_bitrate':
- if ($type === 'fuzzy') {
- $sql = "SELECT song.id FROM song WHERE song.bitrate LIKE '%$string%'";
- }
- else {
- $sql = "SELECT song.id FROM song WHERE song.bitrate='$string'";
- }
- $song_ids = get_songs($sql, 'format');
- if ($song_ids) {
- show_songs($song_ids);
- }
- else {
- echo "<div class=\"error\" align=\"center\">" . _("No Results Found") . "</div>";
- }
+ case 'album':
+ $where_sql .= " ( song.album=album.id AND album.name LIKE '%$value%' ) $method";
+ $table_sql .= "album,";
break;
-
- case 'song_min_bitrate':
- $string = $string * 1000;
- $sql = "SELECT song.id FROM song WHERE song.bitrate >= '$string'";
- $song_ids = get_songs($sql, 'format');
- if ($song_ids) {
- show_songs($song_ids);
- }
- else {
- echo "<div class=\"error\" align=\"center\">" . _("No Results Found") . "</div>";
- }
+ case 'artist':
+ $where_sql .= " ( song.artist=artist.id AND artist.name LIKE '%$value%' ) $method";
+ $table_sql .= "artist,";
break;
-
- case 'song_comment':
- if ($type === 'fuzzy') {
- $sql = "SELECT song.id FROM song WHERE song.comment LIKE '%$string%'";
- }
- else {
- $sql = "SELECT song.id FROM song WHERE song.comment='$string'";
- }
- $song_ids = get_songs($sql, 'format');
- if ($song_ids) {
- show_songs($song_ids);
- }
- else {
- echo "<div class=\"error\" align=\"center\">" . _("No Results Found") . "</div>";
- }
+ case 'genre':
+ $where_sql .= " ( song.genre=genre.id AND genre.name LIKE '%$value%' ) $method";
+ $table_sql .= "genre,";
break;
-
- case 'song_filename':
- if ($type === 'fuzzy') {
- $sql = "SELECT song.id FROM song WHERE song.file LIKE '%$string%'";
- }
- else {
- $sql = "SELECT song.id FROM song WHERE song.file='$string'";
- }
- $song_ids = get_songs($sql, 'format');
- if ($song_ids) {
- show_songs($song_ids);
- }
- else {
- echo "<div class=\"error\" align=\"center\">" . _("No Results Found") . "</div>";
- }
+ case 'year':
+ $where_sql .= " song.year LIKE '%$value%' $method";
+ break;
+ case 'filename':
+ $where_sql .= " song.file LIKE '%$value%' $method";
+ break;
+ case 'played':
+ /* This is a 0/1 value so bool it */
+ $value = settype($value, "bool");
+ $where_sql .= " song.played = '$value' $method";
+ break;
+ case 'minbitrate':
+ $value = intval($value);
+ $where_sql .= " song.bitrate >= '$value' $method";
break;
+ default:
+ // Notzing!
+ break;
+ } // end switch on type
+
- } // end switch
+ } // foreach data
-} // run_search
+ /* Trim off the extra $method's and ,'s then combine the sucka! */
+ $table_sql = rtrim($table_sql,',');
+ $where_sql = rtrim($where_sql,$method);
+
+ $sql = $base_sql . $table_sql . " WHERE" . $where_sql;
+ $db_results = mysql_query($sql, dbh());
+
+ while ($r = mysql_fetch_assoc($db_results)) {
+ $results[] = new Song($r['id']);
+ }
+
+ return $results;
+
+} // search_songs
+
+
+/**
+ * show_search
+ * This shows the results of a search, it takes the input from a run_search function call
+ * @package Search
+ * @catagory Display
+ */
+function show_search($type,$results) {
+
+ /* Display based on the type of object we are trying to view */
+ switch ($type) {
+ case 'artist':
+
+ break;
+ case 'album':
+
+ break;
+ case 'genre':
+
+ break;
+ case 'song':
+ default:
+ show_songs($results);
+ break;
+ } // end type switch
+
+} // show_search
?>