diff options
Diffstat (limited to 'lib/search.php')
-rw-r--r-- | lib/search.php | 138 |
1 files changed, 69 insertions, 69 deletions
diff --git a/lib/search.php b/lib/search.php index 06385874..2133b0fc 100644 --- a/lib/search.php +++ b/lib/search.php @@ -17,38 +17,38 @@ You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - + This library handles all the searching! */ -/** +/** * 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. + * function it does not do the display work its self. * @package Search * @catagory Search */ -function run_search($data) { +function run_search($data) { /* Create an array of the object we need to search on */ - foreach ($data as $key=>$value) { + foreach ($data as $key=>$value) { /* Get the first two chars to check * and see if it's s_ */ $prefix = substr($key,0,2); $value = trim($value); - - if ($prefix == 's_' AND strlen($value)) { + + if ($prefix == 's_' AND strlen($value)) { $true_name = substr($key,2,strlen($key)); $search[$true_name] = Dba::escape($value); } - + } // end foreach - + /* Figure out if they want a AND based search or a OR based search */ - switch($_REQUEST['operator']) { + switch($_REQUEST['operator']) { case 'or': $operator = 'OR'; break; @@ -58,7 +58,7 @@ function run_search($data) { } // end switch on operator /* Figure out what type of method they would like to use, exact or fuzzy */ - switch($_REQUEST['method']) { + switch($_REQUEST['method']) { case 'fuzzy': $method = "LIKE '%__%'"; break; @@ -68,14 +68,14 @@ function run_search($data) { } // end switch on method $limit = intval($_REQUEST['limit']); - + /* Switch, and run the correct function */ - switch($_REQUEST['object_type']) { + switch($_REQUEST['object_type']) { case 'artist': case 'album': case 'song': $function_name = 'search_' . $_REQUEST['object_type']; - if (function_exists($function_name)) { + if (function_exists($function_name)) { $results = call_user_func($function_name,$search,$operator,$method,$limit); return $results; } @@ -84,20 +84,20 @@ function run_search($data) { $results = search_song($search,$operator,$method,$limit); return $results; break; - } // end switch + } // end switch return array(); } // run_search -/** +/** * 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,$operator,$method,$limit) { +function search_song($data,$operator,$method,$limit) { /* Generate BASE SQL */ @@ -108,45 +108,45 @@ function search_song($data,$operator,$method,$limit) { $field_sql = ''; $order_sql = ''; - if ($limit > 0) { + if ($limit > 0) { $limit_sql = " LIMIT $limit"; } - - foreach ($data as $type=>$value) { - + + foreach ($data as $type=>$value) { + /* Create correct Value statement based on method */ $value_string = str_replace("__",$value,$method); - - switch ($type) { + + switch ($type) { case 'all': $additional_soundex = false; - + if (!(strpos($value, '-'))) // if we want a fuzzier search $additional_soundex = true; - + $where_sql = " MATCH (`artist2`.`name`, `album2`.`name`, `song`.`title`) AGAINST ('$value' IN BOOLEAN MODE)"; - + if ($additional_soundex) { $where_sql.= " OR `artist2`.`name` SOUNDS LIKE '$value'"; $where_sql.= " OR `album2`.`name` SOUNDS LIKE '$value'"; $where_sql.= " OR `song`.`title` SOUNDS LIKE '$value'"; } - + $table_sql = " LEFT JOIN `album` as `album2` ON `song`.`album`=`album2`.`id`"; $table_sql.= " LEFT JOIN `artist` AS `artist2` ON `song`.`artist`=`artist2`.`id`"; - + $order_sql = " ORDER BY"; - + $order_sql.= " MATCH (`artist2`.`name`) AGAINST ('$value' IN BOOLEAN MODE)"; if ($additional_soundex) $order_sql.= " + (SOUNDEX(`artist2`.`name`)=SOUNDEX('$value')) DESC,"; else $order_sql.= " DESC,"; - + $order_sql.= " MATCH (`album2`.`name`) AGAINST ('$value' IN BOOLEAN MODE)"; if ($additional_soundex) $order_sql.= " + (SOUNDEX(`album2`.`name`)=SOUNDEX('$value')) DESC,"; else $order_sql.= " DESC,"; - + $order_sql.= " MATCH (`song`.`title`) AGAINST ('$value' IN BOOLEAN MODE)"; if ($additional_soundex) $order_sql.= " + (SOUNDEX(`song`.`title`)=SOUNDEX('$value')) DESC,"; else $order_sql.= " DESC,"; - + $order_sql.= " `artist2`.`name`,"; $order_sql.= " `album2`.`name`,"; $order_sql.= " `song`.`track`,"; @@ -171,14 +171,14 @@ function search_song($data,$operator,$method,$limit) { $where_sql .= " (`song`.`year` BETWEEN ".$data["year"]." AND ".$data["year2"].") $operator"; } break; - case 'time': - if (!empty($data['time2'])) { - $where_sql .= " `song`.`time` <= " . Dba::escape(intval($data['time2'])*60) . " $operator"; + case 'time': + if (!empty($data['time2'])) { + $where_sql .= " `song`.`time` <= " . Dba::escape(intval($data['time2'])*60) . " $operator"; } - if (!empty($data['time'])) { - $where_sql .= " `song`.`time` >= " . Dba::escape(intval($data['time'])*60) . " $operator"; - } - break; + if (!empty($data['time'])) { + $where_sql .= " `song`.`time` >= " . Dba::escape(intval($data['time'])*60) . " $operator"; + } + break; case 'filename': $where_sql .= " `song`.`file` $value_string $operator"; break; @@ -199,70 +199,70 @@ function search_song($data,$operator,$method,$limit) { $value = intval($value); // This is a little more complext, pull a list of IDs that have this average rating - $rating_sql = "SELECT `object_id`,AVG(`rating`.`rating`) AS `avgrating` FROM `rating` " . - "WHERE `object_type`='song' GROUP BY `object_id`"; - $db_results = Dba::read($rating_sql); + $rating_sql = "SELECT `object_id`,AVG(`rating`.`rating`) AS `avgrating` FROM `rating` " . + "WHERE `object_type`='song' GROUP BY `object_id`"; + $db_results = Dba::read($rating_sql); // Fill it with one value to prevent sql error on no results $where_sql .= " `song`.`id` IN ("; - $ids = array('0'); + $ids = array('0'); - while ($row = Dba::fetch_assoc($db_results)) { - if ($row['avgrating'] < $value) { continue; } - $ids[] = $row['object_id']; - } + while ($row = Dba::fetch_assoc($db_results)) { + if ($row['avgrating'] < $value) { continue; } + $ids[] = $row['object_id']; + } - $where_sql .= implode(',',$ids) . ') ' . $operator; + $where_sql .= implode(',',$ids) . ') ' . $operator; break; - case 'tag': + case 'tag': // Fill it with one value to prevent sql error on no results - $ids = array('0'); + $ids = array('0'); - $tag_sql = "SELECT `object_id` FROM `tag` LEFT JOIN `tag_map` ON `tag`.`id`=`tag_map`.`tag_id` " . - "WHERE `tag_map`.`object_type`='song' AND `tag`.`name` $value_string "; - $db_results = Dba::read($tag_sql); + $tag_sql = "SELECT `object_id` FROM `tag` LEFT JOIN `tag_map` ON `tag`.`id`=`tag_map`.`tag_id` " . + "WHERE `tag_map`.`object_type`='song' AND `tag`.`name` $value_string "; + $db_results = Dba::read($tag_sql); - while ($row = Dba::fetch_assoc($db_results)) { - $ids[] = $row['object_id']; - } + while ($row = Dba::fetch_assoc($db_results)) { + $ids[] = $row['object_id']; + } - $where_sql = " `song`.`id` IN (" . implode(',',$ids) . ") $operator"; + $where_sql = " `song`.`id` IN (" . implode(',',$ids) . ") $operator"; - break; + break; default: // Notzing! break; } // end switch on type - + } // foreach data - + /* Trim off the extra $method's and ,'s then combine the sucka! */ $where_sql = rtrim($where_sql,$operator); $group_sql = rtrim($group_sql,','); $select_sql = rtrim($select_sql,','); - if ($group_sql == ' GROUP BY') { $group_sql = ''; } - + if ($group_sql == ' GROUP BY') { $group_sql = ''; } + $base_sql = "SELECT DISTINCT(`song`.`id`) $field_sql $select_sql FROM `song`"; $sql = $base_sql . $table_sql . " WHERE " . $where_sql . $group_sql . $order_sql . $limit_sql; - + /** - * Because we might need this for Dynamic Playlist Action + * Because we might need this for Dynamic Playlist Action * but we don't trust users to provide this store it in the * session where they can't get to it! */ - + $_SESSION['userdata']['stored_search'] = $sql; $db_results = Dba::read($sql); - - $results = array(); - - while ($row = Dba::fetch_assoc($db_results)) { + + $results = array(); + + while ($row = Dba::fetch_assoc($db_results)) { $results[] = $row['id']; } |