Beispiel #1
0
 /**
  * run
  *
  * This function actually runs the search and returns an array of the
  * results.
  */
 public static function run($data)
 {
     $limit = intval($data['limit']);
     $offset = intval($data['offset']);
     $data = Search::clean_request($data);
     $search = new Search(null, $data['type']);
     $search->parse_rules($data);
     // Generate BASE SQL
     $limit_sql = "";
     if ($limit > 0) {
         $limit_sql = ' LIMIT ';
         if ($offset) {
             $limit_sql .= $offset . ",";
         }
         $limit_sql .= $limit;
     }
     $search_info = $search->to_sql();
     $sql = $search_info['base'] . ' ' . $search_info['table_sql'];
     if (!empty($search_info['where_sql'])) {
         $sql .= ' WHERE ' . $search_info['where_sql'];
     }
     if (!empty($search_info['group_sql'])) {
         $sql .= ' GROUP BY ' . $search_info['group_sql'];
         if (!empty($search_info['having_sql'])) {
             $sql .= ' HAVING ' . $search_info['having_sql'];
         }
     }
     $sql .= ' ' . $limit_sql;
     $sql = trim($sql);
     $db_results = Dba::read($sql);
     $results = array();
     while ($row = Dba::fetch_assoc($db_results)) {
         $results[] = $row['id'];
     }
     return $results;
 }
Beispiel #2
0
        $playlist->save();
        break;
    case 'delete_playlist':
        // If we made it here, we didn't have sufficient rights.
        UI::access_denied();
        break;
    case 'show_playlist':
        $playlist = new Search($_REQUEST['playlist_id'], 'song');
        $playlist->format();
        $object_ids = $playlist->get_items();
        require_once AmpConfig::get('prefix') . UI::find_template('show_search.inc.php');
        break;
    case 'update_playlist':
        $playlist = new Search($_REQUEST['playlist_id'], 'song');
        if ($playlist->has_access()) {
            $playlist->parse_rules(Search::clean_request($_REQUEST));
            $playlist->update();
            $playlist->format();
        } else {
            UI::access_denied();
            break;
        }
        $object_ids = $playlist->get_items();
        require_once AmpConfig::get('prefix') . UI::find_template('show_search.inc.php');
        break;
    default:
        $object_ids = $playlist->get_items();
        require_once AmpConfig::get('prefix') . UI::find_template('show_search.inc.php');
        break;
}
// switch on the action
Beispiel #3
0
    <td>
        <a id="addrowbutton" href="javascript:void(0)">
            <?php 
echo UI::get_icon('add');
?>
        <?php 
echo T_('Add Another Rule');
?>
        </a>
        <script type="text/javascript">$('#addrowbutton').on('click', SearchRow.add);</script>
    </td>
    </tr>
</tbody>
</table>
<?php 
UI::show_box_bottom();
?>

<?php 
if ($playlist) {
    $out = $playlist->to_js();
} else {
    $mysearch = new Search(null, $_REQUEST['type']);
    $mysearch->parse_rules(Search::clean_request($_REQUEST));
    $out = $mysearch->to_js();
}
if ($out) {
    echo $out;
} else {
    echo '<script type="text/javascript">SearchRow.add();</script>';
}
Beispiel #4
0
 /**
  * advanced
  * This processes the results of a post from a form and returns an
  * array of song items that were returned from said randomness
  */
 public static function advanced($type, $data)
 {
     /* Figure out our object limit */
     $limit = intval($data['random']);
     // Generate our matchlist
     /* If they've passed -1 as limit then get everything */
     $limit_sql = "";
     if ($data['random'] == "-1") {
         unset($data['random']);
     } else {
         $limit_sql = "LIMIT " . Dba::escape($limit);
     }
     $search_data = Search::clean_request($data);
     $search_info = false;
     if (count($search_data) > 1) {
         $search = new Search(null, $type);
         $search->parse_rules($search_data);
         $search_info = $search->to_sql();
     }
     $sql = "";
     switch ($type) {
         case 'song':
             $sql = "SELECT `song`.`id`, `size`, `time` " . "FROM `song` ";
             if ($search_info) {
                 $sql .= $search_info['table_sql'];
             }
             if (AmpConfig::get('catalog_disable')) {
                 $sql .= " LEFT JOIN `catalog` ON `catalog`.`id` = `song`.`catalog`";
                 $sql .= " WHERE `catalog`.`enabled` = '1'";
             }
             if ($search_info) {
                 if (AmpConfig::get('catalog_disable')) {
                     $sql .= ' AND ' . $search_info['where_sql'];
                 } else {
                     $sql .= ' WHERE ' . $search_info['where_sql'];
                 }
             }
             break;
         case 'album':
             $sql = "SELECT `album`.`id`, SUM(`song`.`size`) AS `size`, SUM(`song`.`time`) AS `time` FROM `album` ";
             if (!$search_info || !$search_info['join']['song']) {
                 $sql .= "LEFT JOIN `song` ON `song`.`album`=`album`.`id` ";
             }
             if ($search_info) {
                 $sql .= $search_info['table_sql'];
             }
             if (AmpConfig::get('catalog_disable')) {
                 $sql .= " LEFT JOIN `catalog` ON `catalog`.`id` = `song`.`catalog`";
                 $sql .= " WHERE `catalog`.`enabled` = '1'";
             }
             if ($search_info) {
                 if (AmpConfig::get('catalog_disable')) {
                     $sql .= ' AND ' . $search_info['where_sql'];
                 } else {
                     $sql .= ' WHERE ' . $search_info['where_sql'];
                 }
             }
             $sql .= ' GROUP BY `album`.`id`';
             break;
         case 'artist':
             $sql = "SELECT `artist`.`id`, SUM(`song`.`size`) AS `size`, SUM(`song`.`time`) AS `time` FROM `artist` ";
             if (!$search_info || !$search_info['join']['song']) {
                 $sql .= "LEFT JOIN `song` ON `song`.`artist`=`artist`.`id` ";
             }
             if ($search_info) {
                 $sql .= $search_info['table_sql'];
             }
             if (AmpConfig::get('catalog_disable')) {
                 $sql .= " LEFT JOIN `catalog` ON `catalog`.`id` = `song`.`catalog`";
                 $sql .= " WHERE `catalog`.`enabled` = '1'";
             }
             if ($search_info) {
                 if (AmpConfig::get('catalog_disable')) {
                     $sql .= ' AND ' . $search_info['where_sql'];
                 } else {
                     $sql .= ' WHERE ' . $search_info['where_sql'];
                 }
             }
             $sql .= ' GROUP BY `artist`.`id`';
             break;
     }
     $sql .= " ORDER BY RAND() {$limit_sql}";
     // Run the query generated above so we can while it
     $db_results = Dba::read($sql);
     $results = array();
     $size_total = 0;
     $fuzzy_size = 0;
     $time_total = 0;
     $fuzzy_time = 0;
     while ($row = Dba::fetch_assoc($db_results)) {
         // If size limit is specified
         if ($data['size_limit']) {
             // Convert
             $new_size = $row['size'] / 1024 / 1024;
             // Only fuzzy 100 times
             if ($fuzzy_size > 100) {
                 break;
             }
             // Add and check, skip if over size
             if ($size_total + $new_size > $data['size_limit']) {
                 $fuzzy_size++;
                 continue;
             }
             $size_total = $size_total + $new_size;
             $results[] = $row['id'];
             // If we are within 4mb of target then jump ship
             if ($data['size_limit'] - floor($size_total) < 4) {
                 break;
             }
         }
         // if size_limit
         // If length really does matter
         if ($data['length']) {
             // base on min, seconds are for chumps and chumpettes
             $new_time = floor($row['time'] / 60);
             if ($fuzzy_time > 100) {
                 break;
             }
             // If the new one would go over skip!
             if ($time_total + $new_time > $data['length']) {
                 $fuzzy_time++;
                 continue;
             }
             $time_total = $time_total + $new_time;
             $results[] = $row['id'];
             // If there are less then 2 min of free space return
             if ($data['length'] - $time_total < 2) {
                 return $results;
             }
         }
         // if length does matter
         if (!$data['size_limit'] && !$data['length']) {
             $results[] = $row['id'];
         }
     }
     // end while results
     switch ($type) {
         case 'song':
             return $results;
         case 'album':
             $songs = array();
             foreach ($results as $result) {
                 $album = new Album($result);
                 $songs = array_merge($songs, $album->get_songs());
             }
             return $songs;
         case 'artist':
             $songs = array();
             foreach ($results as $result) {
                 $artist = new Artist($result);
                 $songs = array_merge($songs, $artist->get_songs());
             }
             return $songs;
         default:
             return false;
     }
 }