include_once("search_config.php");
mysql_pconnect($dbhost, $dbuser, $dbpass);
check_error();
function db_query($query, $exit_on_error = false)
{
global $_DEBUG;
$qres = mysql_query($query);
if ($qres) return $qres;
$error ="Database error: ".mysql_error()."
\n$query\n
$query
\n";
if ($_DEBUG) print $error;
if ($exit_on_error) exit;
return false;
}
// preserve_vars gets the named vars from the $_REQUEST array and puts them together
// for inclusion on a query string
// if there's any output, appends "&" at the end
function preserve_vars() {
foreach(func_get_args() as $arg) {
if ($_REQUEST[$arg]) {
if ($return_string) $return_string .= "&";
$return_string .= "$arg=".htmlspecialchars($_REQUEST[$arg]);
}
}
if ($return_string) $return_string .= "&";
return $return_string;
}
// creates the "latest" list on the index page, using the "creation_date" value in the resources table
function latest_list() {
global $dbname, $latest_resource_length;
$query = "select * from $dbname.resources
left join $dbname.media using (resource_id)
group by title
order by publish_date desc
limit $latest_resource_length";
$qres = db_query($query);
if (!mysql_num_rows($qres)) return "
No resources found";
while ($data = mysql_fetch_array($qres)) {
$pubseconds = strtotime("$data[publish_date] 00:00:00");
$pubdate = date("M j", $pubseconds);
$return_list[] = "$data[title] ($data[resource_type], $pubdate)";
}
return $return_list;
}
function latest_sermon_list() {
global $dbname, $latest_resource_length;
$query = "select * from $dbname.resources
where resource_type = 'sermon'
and love_changes = 0
order by publish_date desc, resource_id desc
limit $latest_resource_length";
$qres = db_query($query);
if (!mysql_num_rows($qres)) return "No sermons found";
while ($data = mysql_fetch_array($qres)) {
$pubseconds = strtotime("$data[publish_date] 00:00:00");
$pubdate = date("M j", $pubseconds);
$return_list[] = "$data[title] ($pubdate)";
}
return $return_list;
}
// creates the "most popular" list on the index page, using the "clicks" value in the media table
function most_popular_list() {
global $dbname, $most_popular_length;
$query = "select * from $dbname.resources left join $dbname.media using (resource_id) where clicks > 0 order by clicks desc limit $most_popular_length";
$qres = db_query($query);
if (!mysql_num_rows($qres)) return "No resources found";
while ($data = mysql_fetch_array($qres)) {
$return_string .= media_icon($data['media_type'], $data['media_id'], $data['filename'])." ".$data['title']."($data[media_type])
\n";
}
return $return_string;
}
/*
returns the title of the "most recent published sermon". since they could conceivably
enter sermons in advanced, that's defined as: the latest sermon with a publish date no later
than yesterday (because sunday's sermon shouldn't be posted that sunday).
if a number is passed in, limits the char length of the returned title. subtracts 1, goes back to the first whitespace, then adds ellipsis character (…).
*/
function last_published_sermon_title($link_prefix, $charlimit = 0) {
global $dbname;
$find_date = date("Y-m-d", time() - 86400);
$query = "select title, series_id from $dbname.resources r
left join $dbname.series s on (r.series = s.series_name)
where publish_date <= '$find_date'
and love_changes = 0
and s.series_id is not null
order by publish_date desc limit 1";
$qres = db_query($query);
if (!mysql_num_rows($qres)) return array("none found", "");
list($title, $series_id) = mysql_fetch_array($qres);
if ($charlimit && (strlen($title) > $charlimit)) {
$title = wordwrap($title, $charlimit-1, "-break-");
$title = preg_replace("/-break-.*/", "", $title);
$title .= "…";
}
return array($title, $link_prefix.$series_id);
}
/*
finds the last series id, and returns a link to that series, linked on the text of the presenter.
if the presenter isn't rev. meyer, sets $return_array['class'] to and alternate class for
use in the box in which the link is presented.
*/
function last_published_sermon_info($link_prefix) {
global $dbname;
$find_date = date("Y-m-d", time() - 86400);
$query = "select series_id, presenter from $dbname.resources r
left join $dbname.series s on (r.series = s.series_name)
where publish_date <= '$find_date'
and love_changes = 0
and s.series_id is not null
order by publish_date desc limit 1";
$qres = db_query($query);
if (!mysql_num_rows($qres)) {
$return_array = Array(
'link' => '(database unavailable)',
'class' => '' );
}
else {
$data = mysql_fetch_assoc($qres);
$presenter = $data['presenter'];
$series_id = $data['series_id'];
$return_array['link'] = "by $presenter";
if ($presenter != 'Rev. Daniel D. Meyer')
$return_array['class'] = ' class="alt"';
}
return $return_array;
}
return array("none found", "");
list($title, $series_id) = mysql_fetch_array($qres);
// creates a media icon, links it to click.php if filename is provided.
// "alt_prefix' is here to accomodate the download audio links, to get passed to click.php
// addl_opts_string is a string to add on the end of the links (the "&" is not
// expected at the beginning of the string, so it's added here if necessary)
function media_icon ($type, $media_id, $filename, $alt = '', $addl_opts_string = '') {
global $icon_tooltips;
if ($alt)
$image_select = strtolower($alt);
else
$image_select = strtolower($type);
if ($addl_opts_string) {
$addl_opts_string = "&$addl_opts_string";
}
$image_select = preg_replace("/ /", "", $image_select);
$icon_url = "images/i_media_$image_select.gif";
$return_text = "
";
// open in new window if type == PDF
//$target = ($type == "PDF" ? ' target="_blank"' : '');
// they now want them all to open in a new window
$target = ' target="_blank"';
// except flash, which pops up in a javascript window
if ($type == 'Flash') {
$return_text = "$return_text";
} else if ($filename != 'on') {
// 'on' is from checkboxes; these don't have links
$return_text = "$return_text";
}
return $return_text;
}
// browse_title makes the title on browse page
function browse_title($primary, $secondary) {
global $browse_labels;
$browse_heading = "Browse";
if ($browse_labels[$primary]) {
$browse_primary = $browse_labels[$primary];
if ($browse_labels[$secondary]) {
$browse_primary = "$browse_primary";
}
$browse_heading .= " > $browse_primary";
}
if ($browse_labels[$secondary])
$browse_heading .= " > ".$browse_labels[$secondary];
return $browse_heading;
}
// browse_text returns the text to display over the title for browse searches
// (favors the secondary)
function browse_text($primary, $secondary) {
global $browse_header_text;
if ($browse_header_text[$secondary])
return $browse_header_text[$secondary];
if ($browse_header_text[$primary])
return $browse_header_text[$primary];
}
/*
compile_search_data: takes a query result, returns a data structure for table display
each result row represents one media item in one resource. title is what defines a distinct display row,
but there can be multiple database rows for a single title (i.e., multiple resources for
a single title). so, we compile the results into one array item for each title, and creating
an array for multiple resources associated with that title
it was done this way because I originally expected there'd be multiple of one media type for
each resource, but that's no longer the case. so as the code stands now it seems more complicated
than it needs to be. I'm adding another method of putting media info into $search_data (media_items, see "second, flatter setup" below), in expectation of removing some complexity from the database.
v1.0
including a version number because I'm duplicating this on cc-ob and lovechangeslife.org
*/
function compile_search_data($qres) {
while($line = mysql_fetch_array($qres)) {
$media_item = Array($line['media_type'], $line['media_id'], $line['filename']);
if ($search_data[$line['title']]) {
// already have this title, just add resource to its listing
array_push($search_data[$line['title']]['media_list'], $media_item);
}
else {
$search_data[$line['title']] = $line;
$search_data[$line['title']]['media_list'] = Array($media_item);
}
// second, flatter setup; assumes only one of a media type per resource
$search_data[$line['title']]['media_items'][$line['media_type']] = $line['filename'];
// for use in admin interface - splits up some fields into parts to be used for
// separate dropdowns
$times = explode(":", $line['length']);
$search_data[$line['title']]['length_sec'] = array_pop($times);
$search_data[$line['title']]['length_min'] = array_pop($times);
$search_data[$line['title']]['length_hr'] = array_pop($times);
$publish_seconds = strtotime($line['publish_date']);
$search_data[$line['title']]['publish_date_yy'] = date('Y', $publish_seconds);
if ($line['publish_date_display_units'] > 1)
$search_data[$line['title']]['publish_date_mm'] = date('m', $publish_seconds);
if ($line['publish_date_display_units'] > 2)
$search_data[$line['title']]['publish_date_dd'] = date('d', $publish_seconds);
}
// sort media lists
foreach (array_keys($search_data) as $title) {
sort($search_data[$title]['media_list']);
ksort($search_data[$title]['media_items']);
}
return $search_data;
}
// using $search_data (as created above), returns text for the "most recent publication" display
function most_recent_pub($search_data) {
if (!$search_data) return;
foreach($search_data as $item) {
$pub_secs = strtotime($search_data['publish_date']);
$latest_pub_secs = max($pub_secs, $latest_pub_secs);
}
if ($latest_pub_secs)
return date("n/j/Y", $latest_pub_secs);
else
return false;
}
/*
FormLabel simply returns $text if $error_text is empty; if $error_text is not empty,
returns an error version
(currently just makes it class alert-text)
*/
function FormLabel($text, $error_text) {
if ($error_text)
return "$text";
else
return $text;
}
function esc_quote($text)
{
return "'".AddSlashes($text)."'";
}
function check_error() {
global $config_debug;
if (mysql_error() && $config_debug) print mysql_error();
}
function selected($test) {
if ($test) return " selected";
}
// "selected" is too common a name; I'll switch other ones to cc_selected when I get to them
function cc_selected($test) {
if ($test) return " selected";
else return "";
}
function cc_checked($test) {
if ($test) return " checked";
else return "";
}
function ccob_debug($string) {
global $config_debug;
if ($config_debug) {
print "\n\ndebug: $string
\n\n";
}
}
?>
Contact Us
We'd be happy to answer any questions you may have. If you are having any difficulties playing the audio or video, you may need to download RealPlayer.