";
if(!$post_ids){
$post_ids = $wpdb->get_col($query);;
}else{
$post_ids = array_intersect($post_ids, $wpdb->get_col($query));
}
}
}
//print_r($post_ids);
if(count($post_ids)){
$this->the_matching_posts = $post_ids;
// if(!$this->the_matching_posts_ordinals)
$this->the_matching_posts_ordinals = array_flip($post_ids);
return(TRUE);
}else if(count($the_search_tags)){
$this->the_matching_posts = FALSE;
$wp_query->is_404 = TRUE;
return(FALSE);
}else{
return(FALSE);
}
}
public function sort_matching_posts($the_posts){
//print_r($the_posts);
//print_r($this->the_matching_posts_ordinals);
// insert the ordinal into each post for sorting
foreach($the_posts as $post){
//echo $post->ID . '='. $this->the_matching_posts_ordinals[$post->ID] .', ';
$new_order[$this->the_matching_posts_ordinals[$post->ID]] = $post;
}
// now that the posts are re-keyed, sort them
ksort($new_order);
// This shuffle resets the keys on the array.
// A function later in WP expects the array keys to be sequential,
// and the output here might otherwise be non-sequential.
array_unshift($new_order, 'Junk');
array_shift($new_order);
//print_r($new_order);
return($new_order);
}
public function posts_where($query){
//echo "
$query
";
return(str_replace("AND (post_type = 'post')", "AND (post_type IN ('post', 'scrib'))", $query));
}
public function the_query($query, $limit = NULL){
global $bsuite, $btaggy, $wp_query, $wpdb;
if($wp_query->is_admin)
return($query);
if($wp_query->query['author_name']){
unset($query_vars['author']);
}else if($wp_query->query['author']){
$wp_query->query_vars['author'] = $wp_query->query['author'];
}
//echo "
$query
";
//print_r($wp_query);
// establish the query vars
$this->search_tags = array_map('stripslashes', (array_intersect_key($wp_query->query_vars, array_flip($this->data_facets))));
// figure out what page of posts to show
// $paged, $posts_per_page, and $limit are here for cases
// where the query doesn't have an explicit LIMIT declaration
$paged = $wp_query->query_vars['paged'] ? $wp_query->query_vars['paged'] : 1;
$posts_per_page = $wp_query->query_vars['posts_per_page'] ? $wp_query->query_vars['posts_per_page'] : get_settings('posts_per_page');
// figure out if we have matching posts, and which ones they are
$inids = '';
if($this->get_matching_posts($wp_query->query_vars['s'], $this->search_tags)){
$inids = 'AND ID IN ('. implode(array_slice($this->the_matching_posts, ($paged - 1) * $posts_per_page, $posts_per_page), ', ') .')';
$limit[1] = '0, '. $posts_per_page;
}else{
$limit[1] = ($paged - 1) * $posts_per_page .', '. $posts_per_page;
}
if (!empty($wp_query->query_vars['s']) && $this->the_matching_posts) {
$query = "SELECT * FROM $wpdb->posts WHERE 1=1 $inids
AND post_status IN ('publish', 'private')
";
add_filter('the_posts', array(&$this, 'sort_matching_posts'));
add_filter('the_posts', create_function('$a', 'global $wpdb, $wp_query ; $wp_query->found_posts = '. count($this->the_matching_posts) .' ; $wp_query->max_num_pages = ceil('. count($this->the_matching_posts) .' / '. $posts_per_page .') ; return($a);'));
}else if($wp_query->query['pagename'] && ($wp_query->query['pagename'] == $this->options['browse_name'])) {
$this->is_browse = TRUE;
$wp_query->is_search = TRUE;
$wp_query->is_singular = FALSE;
$wp_query->is_page = FALSE;
$content_type = "'post', 'page', 'scrib'";
if($wp_query->query_vars['scope'] == 'catalog')
$content_type = "'scrib'";
if($wp_query->query_vars['scope'] == 'blog')
$content_type = "'page', 'post'";
$query = "SELECT SQL_CALC_FOUND_ROWS $wpdb->posts.*
FROM $wpdb->posts
WHERE 1=1
$inids
AND (post_type IN ($content_type) AND (post_status IN ('publish', 'private')))
ORDER BY post_date_gmt DESC LIMIT $limit[1]
";
if(empty($inids)){
add_filter('the_posts', create_function('$a', 'global $wpdb, $wp_query ; $count = $wpdb->get_var("SELECT FOUND_ROWS()") ; $wp_query->found_posts = $count ; $wp_query->max_num_pages = ceil($count / '. $posts_per_page .') ; return($a);'));
}else{
add_filter('the_posts', create_function('$a', 'global $wpdb, $wp_query ; $wp_query->found_posts = '. count($this->the_matching_posts) .' ; $wp_query->max_num_pages = ceil('. count($this->the_matching_posts) .' / '. $posts_per_page .') ; return($a);'));
}
}
//print_r($this->the_matching_posts);
//print_r($wp_query);
//echo "
$query
";
if($this->the_matching_posts === FALSE){
$query = "SELECT SQL_CALC_FOUND_ROWS $wpdb->posts.*
FROM $wpdb->posts
WHERE 1=2
";
}
// we need something to protect against somebody trying to page beyond the found set of matching posts.
// if(count($this->the_matching_posts < (($paged - 1) * $posts_per_page)))
$this->the_query_string = $query;
return($query);
}
//
// fetch the facets and return an array
//
public function fetchfacets(){
global $wpdb, $bsuite, $btaggy, $wp_query;
static $result = NULL;
if($result || $is_404)
return($result);
//print_r($wp_query->query_vars['s']);
if (!empty($wp_query->query_vars['s'])) {
$inids = implode(array_slice($this->the_matching_posts, 0, 150), ', ');
if($inids){
$bibns = implode(', ', $rows);
$request = "SELECT tag_type, tag_raw, COUNT(post_id) AS hits
FROM $btaggy->meta_table
WHERE post_id IN ($inids)
GROUP BY tag_type, tag_small
ORDER BY hits DESC
";
//echo "
$request
";
$facets = $wpdb->get_results($request);
}
}else{
if($this->the_matching_posts){
$inids = 'WHERE post_id IN ('. implode($this->the_matching_posts, ', ') .')';
}else{
if($result = get_option('scrib_facet_cache')){
return($result);
}else{
$reset_facet_cache = TRUE;
}
$inids = '';
}
$request = "SELECT tag_type, tag_raw, COUNT(post_id) AS hits FROM $btaggy->meta_table $inids
GROUP BY tag_type, tag_small
ORDER BY hits DESC
LIMIT 1500
";
$facets = $wpdb->get_results($request);
}
//echo "
$request
";
if($facets){
foreach($facets as $row){
$result[$row->tag_type][] = array('namespace' => $row->tag_space, 'fieldname' => $row->tag_type, 'tag' => $row->tag_raw, 'hits' => $row->hits);
}
}
if ($reset_facet_cache) {
update_option('scrib_facet_cache', $result);
}
return($result);
}
public function the_facets($args) {
//run through an array of facets, report them
global $wpdb, $bsuite, $btaggy, $wp_query;
parse_str($args, $r);
$include = explode(',', str_replace(' ', '', $r['include']));
if(ereg('\*', $r['exclude'])){
$exclude = '*';
}else{
$exclude = explode(',', str_replace(' ', '', $r['exclude']));
}
unset($r['include']);
unset($r['exclude']);
$options = http_build_query($r);
$facets = $this->fetchfacets();
$result = NULL;
// unset the items named in $exclude
if($exclude && $exclude <> '*'){
foreach($exclude as $temp){
unset($facets[$temp]);
}
}
// print out each facet named in $include
if($include){
foreach($include as $temp){
if(array_key_exists($temp, $facets))
$result .= $btaggy->tags_formatlist($facets[$temp], $options, $this->search_tags);
unset($facets[$temp]);
}
}
// print out everything that remains
if($exclude <> '*'){
foreach(array_keys($facets) as $temp){
if(array_key_exists($temp, $facets))
$result .= $btaggy->tags_formatlist($facets[$temp], $options, $this->search_tags);
}
}
if($r['return']){
return($result);
}else{
echo $result;
}
}
public function the_scope() {
if(!is_search() && !$this->is_browse)
return(FALSE);
$url_base = trim(str_replace('scope='. $this->search_tags['scope'] , '', $_SERVER['REQUEST_URI']), '?&');
$joiner = '?';
if(strpos($url_base,'?'))
$joiner = '&';
echo '
';
}
}
public function the_image($size = 'small') {
global $id, $wpdb, $post_cache, $bsuite, $btaggy;
if($size == 'large'){
if($post_cache[$wpdb->blogid][$id]->post_content){
$image = $post_cache[$wpdb->blogid][$id]->post_content;
}else{
$image = $wpdb->get_var("SELECT post_content_filtered FROM $wpdb->posts WHERE ID = $id");
}
}else{
if($post_cache[$wpdb->blogid][$id]->post_excerpt){
$image = $post_cache[$wpdb->blogid][$id]->post_excerpt;
}else{
$image = $wpdb->get_var("SELECT post_excerpt FROM $wpdb->posts WHERE ID = $id");
}
}
preg_match('/\[\[bookjacket\|([^\]]*)\]\]/', $image, $matches);
unset($image);
$image = str_replace('ScribBase/', get_settings('siteurl') . '/', $matches[1]);
// fallback if no image
if(!$image)
$image = '';
echo("post_title}\" class=\"summarylink\" id=\"summary-$id\">$image");
}
public function the_format($return = NULL) {
global $id, $bsuite, $btaggy;
if(!$return){
echo $btaggy->meta_cache[$wpdb->blogid][$id]['tags_a']['scrib']['format'][0];
}else{
return($btaggy->meta_cache[$wpdb->blogid][$id]['tags_a']['scrib']['format'][0]);
}
}
public function the_content($content){
// if(!$this->is_scrib)
// return($content);
$content = str_replace(array('ScribCatBase/subject/', 'ScribCatBase/author/'), array($this->options['browse_url'] .'/?subject=', $this->options['browse_url'] .'/?author='), $content);
return($content);
}
//
// Scriblio specific tokens (requires bsuite)
//
public function tokens_set($tokens){
// setup some tokens
$tokens['bookjacket'] = array(&$this, 'token_bookjacket');
$tokens['availability'] = array(&$this, 'token_availability');
$tokens['reviews'] = array(&$this, 'token_reviews');
return($tokens);
}
public function token_bookjacket($thing) {
// [[bookjacket|]]
global $id;
return(''. $thing .'');
}
public function token_reviews($thing) {
// [[reviews|0738202444]]
if(function_exists('scrib_reviews')){
return(scrib_reviews($thing));
}else{
return('');
}
}
public function token_availability($thing) {
// [[availability|l1292675]]
if(function_exists('scrib_availability')){
return(scrib_availability($thing));
}else{
return('');
}
// return('click for location and availability');
// return('click for availability');
}
// end token-related functions
public function athooks_set($athooks){
// setup some default athooks
// $athooks[hook name][where to act (request or redirect)] = 'function name';
$athooks['availability']['redirect'] = array(&$this, 'athook_availability');
$athooks['availability']['title'] = 'Availability';
return($athooks);
}
public function athook_availability(){
global $wp_query;
$query_vars['p'] = $wp_query->athook['post'];
if($query_vars['p']){
$athook = $wp_query->athook;
query_posts("p={$query_vars['p']}");
global $id, $wp_query, $post_cache, $posts, $wpdb, $wp, $bsuite;
$wp_query->post->ID = $id = $query_vars['p'];
$btaggy->fetchmeta();
$return = 'No availability information is available at this time.';
if(function_exists('scrib_availability')){
$return = scrib_availability($btaggy->meta_cache[$wpdb->blogid][$id]['tags_a']['scrib']['sourceid'][0]);
}
status_header(200);
unset($wp_query->query_vars['attachment']);
$post_cache[1][$id]->post_type = 'post';
$post_cache[1][$id]->post_content = $return;
$post_cache[1][$id]->post_status = 'publish';
$post_cache[1][$id]->comment_status = 'closed';
$post_cache[1][$id]->ping_status = get_option('default_pingback_flag');
$wp_query->is_page = TRUE;
$wp_query->is_single = FALSE;
$wp_query->is_404 = FALSE;
$wp_query->is_attachment = FALSE;
$wp_query->is_search = FALSE;
$wp_query->is_archive = FALSE;
$wp_query->is_category = FALSE;
$wp_query->is_search = FALSE;
$wp_query->is_athook = TRUE;
$wp_query->athook = $athook;
$posts = $wp_query->posts;
}
return(TRUE);
}
}
// now instantiate this object
$scrib = & new Scrib;
// some template functions...
function is_browse() {
global $scrib;
return( $scrib->is_browse );
}
function is_scrib( $post_id = '' ) {
global $scrib;
return( $scrib->is_scrib($post_id) );
}
function scrib_feedlink() {
global $scrib;
return( $scrib->feedlink() );
}
// the widget
function scrib_widget_init() {
if ( !function_exists('register_sidebar_widget') )
return;
// This is the function that outputs our little Google search form.
function scrib_widget($args) {
global $scrib, $bsuite, $btaggy;
// $args is an array of strings that help widgets to conform to
// the active theme: before_widget, before_title, after_widget,
// and after_title are the array keys. Default tags: li and h2.
extract($args);
//print_r($args);
// Each widget can store its own options. We keep strings here.
$options = get_option('scrib_widget');
// These lines generate our output. Widgets can be very complex
// but as you can see here, they can also be very, very simple.
// echo $before_widget . $before_title . $title . $after_title;
if(is_search() || $scrib->is_browse){
?>
editsearch(); ?>
the_scope(); ?>
the_facets($options['scrib_thefacets']);; ?>
the_tags($options['scrib_thetags']);
}
// echo $after_widget;
}
// This is the function that outputs the form to let the users edit
// the widget's title. It's an optional feature that users cry for.
function scrib_widget_control() {
// Get our options and see if we're handling a form submission.
$options = get_option('scrib_widget');
if ( !is_array($options) )
$options = array('scrib_thefacets' => 'include=format,subject&exclude=sourceid,title,asin,lccn,isbn&limit=12', 'scrib_thetags' => 'include=author,subject&exclude=sourceid', scrib_listpages => 'title_li=&depth=1&sort_column=menu_order');
if ( $_POST['scrib-submit'] ) {
$options['scrib_thefacets'] = $_POST['scrib_thefacets'];
$options['scrib_thetags'] = $_POST['scrib_thetags'];
$options['scrib_listpages'] = $_POST['scrib_listpages'];
update_option('scrib_widget', $options);
}
// Be sure you format your options to be valid HTML attributes.
$options = array_map('htmlspecialchars', $options);
// Here is our little form segment. Notice that we don't need a
// complete form. This will be embedded into the existing form.
echo '';
echo '';
echo '';
echo '';
}
// This registers our widget so it appears with the other available
// widgets and can be dragged and dropped into any active sidebars.
register_sidebar_widget(array('Scriblio widget', 'widgets'), 'scrib_widget');
// This registers our optional widget control form. Because of this
// our widget will have a button that reveals a 300x100 pixel form.
register_widget_control(array('Scriblio widget', 'widgets'), 'scrib_widget_control', 300, 150);
}
function scrib_availability($id){
$id = substr($id, 2);
$WPopac_ilsbase = 'http://lola.plymouth.edu/';
$WPopac_ilssso = 'http://www.plymouth.edu/library/r/?destination=';
$raw = utf8_encode(file_get_contents($WPopac_ilsbase .'record='. $id));
//print_r($raw);
/*
$pattern = '/((.*?)<\/title>)/i';
$raw = preg_match_all($pattern, $raw, $array);
*/
$temp = explode('', $raw);
//print_r($array);
$availability = NULL;
foreach($temp as $tem){
$te = explode('bibItems">', $tem);
if($te[1]){
$availability = strip_tags(('