$val) { $sql[$key] = mysql_real_escape_string($val); } // Instantiate AMO_Object so we can get our categories and platforms. $amo = new AMO_Object(); $amo->getCats(); $amo->getPlatforms(); $dates = array( 'day' => 'Today', 'week' => 'This Week', 'month'=> 'This Month', 'year' => 'This Year' ); $sort = array( 'newest' => 'Newest', 'name' => 'Name', 'rating' => 'Rating', 'downloads' => 'Popularity' ); $apps = array( 'Firefox' => 'Firefox', 'Thunderbird' => 'Thunderbird', 'Mozilla' => 'Mozilla' ); $perpage = array( 10 => '10', 25 => '25', 50 => '50' ); // Now we need to build our query. Our query starts with four parts: // Select and joins. $select = " SELECT DISTINCT main.ID FROM main "; // Where clause. $where = " WHERE "; // Order by. $orderby = " ORDER BY "; if (!empty($sql['platform'])||!empty($sql['app'])) { $select .= " INNER JOIN version ON version.ID = main.ID "; } if (!empty($sql['cat'])) { $select .= " INNER JOIN categoryxref ON categoryxref.ID = main.ID "; $where .= " categoryxref.CategoryID = '{$sql['cat']}' AND "; } if (!empty($sql['platform'])) { $where .= " version.OSID = '{$sql['platform']}' AND "; } if (!empty($sql['app'])) { $select .= " INNER JOIN applications ON version.AppID = applications.AppID "; $where .= " applications.AppName = '{$sql['app']}' AND "; } if (!empty($sql['q'])) { $where .= " main.Name LIKE '%{$sql['q']}%' AND "; } if (!empty($sql['type'])) { $where .= " main.Type = '{$sql['type']}' AND "; } if (!empty($sql['date'])) { switch ($sql['date']) { case 'day': $compareTimestamp = strtotime('-1 day'); break; case 'week': $compareTimestamp = strtotime('-1 week'); break; case 'month': $compareTimestamp = strtotime('-1 month'); break; case 'year': $compareTimestamp = strtotime('-1 year'); break; } $compareDate = date('Y-m-d',$compareTimestamp); $where .= " main.DateUpdated > '{$compareDate}' AND "; unset($compareTimestamp); unset($compareDate); } if (!empty($sql['sort'])) { switch ($sql['sort']) { case 'newest': default: $orderby .= " main.DateUpdated DESC"; break; case 'name': $orderby .= " main.Name ASC"; break; case 'rating': $orderby .= " main.Rating DESC"; break; case 'downloads': $orderby .= " main.TotalDownloads DESC"; break; } } else { $orderby .= " main.DateUpdated DESC "; } $where .= ' 1 '; $query = $select.$where.$orderby; $results = array(); $rawResults = array(); $db->query($query, SQL_ALL); unset($select); unset($where); unset($orderby); unset($query); if (is_array($db->record)) { foreach ($db->record as $row) { $rawResults[] = $row[0]; } } // If we have only one result, redirect to the addon page. if ( count($rawResults) == 1) { header('Location: https://'.$_SERVER['HTTP_HOST'].WEB_PATH.'/addon.php?id='.$rawResults[0]); exit; } else { for ($i=$page['left'];$i<$page['right'];$i++) { if (isset($rawResults[$i])) { $results[] = new Addon($rawResults[$i]); } } } $resultCount = count($rawResults); if ($resultCount<$page['right']) { $page['right'] = $resultCount; } // Do we even have a next or previous page? $page['previous'] = ($page['left'] >= $clean['perpage']) ? $page['left']-$clean['perpage'] : null; $page['next'] = ($page['left']+$clean['perpage'] < $resultCount) ? $page['left']+$clean['perpage'] : null; $page['resultCount'] = $resultCount; $page['leftDisplay'] = $page['left']+1; // Build the URL based on passed arguments. foreach ($clean as $key=>$val) { if (!empty($val)) { $buf[] = $key.'='.$val; } } $page['url'] = implode('&',$buf); unset($buf); // Pass variables to template object. $tpl->assign( array( 'page' => $page, 'results' => $results, 'clean' => $clean, 'cats' => $amo->Cats, 'platforms' => $amo->Platforms, 'apps' => $apps, 'dates' => $dates, 'sort' => $sort, 'perpage' => $perpage, 'content' => 'search.tpl', 'title' => 'Search' ) ); // Set a non-default wrapper. $wrapper = 'inc/wrappers/nonav.tpl'; ?>