/**
  * Returns all sitewide posts that use the given tag slug.
  *
  * @param  string $slug the tag of the slug
  * @return array        a list of posts using the given tag
  *
  * @since 0.1
  */
 public function get_tagged_posts($slug)
 {
     global $nxtdb;
     return $nxtdb->get_results($nxtdb->prepare("\n\t\t\tSELECT p.*, p.cb_sw_blog_id\n\t\t\tFROM {$this->sw_tables->posts} AS p, {$this->sw_tables->tags} AS t, {$this->sw_tables->tag_usage} AS tu\n\t\t\tWHERE t.slug = %s AND t.term_id = tu.uses_tag AND tu.post_id = p.ID AND tu.blog_id = p.cb_sw_blog_id\n\t\t\tORDER BY post_date DESC ", ClassBlogs_Utils::slugify($slug)));
 }
Example #2
0
 /**
  * Adds or updates a record of a post to the sitewide posts table.
  *
  * In addition to modifying the record for the post, this also tracks the
  * tags used by the post, updating those in the sitewide tags tables.
  *
  * @param int $blog_id the ID of the blog on which the post was made
  * @param int $post_id the post's ID on its blog
  *
  * @access private
  * @since 0.2
  */
 private function _update_sw_post($blog_id, $post_id)
 {
     global $nxtdb;
     ClassBlogs_NXTClass::switch_to_blog($blog_id);
     // Update the post's record in the sitewide table
     $this->_copy_sw_object($blog_id, $post_id, 'ID', $nxtdb->posts, $this->sw_tables->posts);
     // Get a list of the tags used by the current post and those that have
     // been added to the list of sitewide tags
     $local_tags = $local_tag_slugs = $sw_tag_slugs = array();
     foreach (nxt_get_post_tags($post_id) as $local_tag) {
         $local_tags[$local_tag->slug] = $local_tag;
         $local_tag_slugs[] = $local_tag->slug;
     }
     $sw_tags = $nxtdb->get_results($nxtdb->prepare("\n\t\t\tSELECT t.slug\n\t\t\tFROM {$this->sw_tables->tag_usage} AS tu, {$this->sw_tables->tags} AS t\n\t\t\tWHERE tu.post_id=%d AND tu.blog_id=%d AND tu.uses_tag=t.term_id", $post_id, $blog_id));
     foreach ($sw_tags as $sw_tag) {
         $sw_tag_slugs[] = $sw_tag->slug;
     }
     // Create or modify records for tags that need to be added
     $add_slugs = array_diff($local_tag_slugs, $sw_tag_slugs);
     foreach ($add_slugs as $add_slug) {
         // See if a record of the tag already exists
         $tag = $local_tags[$add_slug];
         $sw_id = $nxtdb->get_var($nxtdb->prepare("SELECT term_id FROM {$this->sw_tables->tags} WHERE slug=%s", ClassBlogs_Utils::slugify($add_slug)));
         // Create a new record for the tag if it doesn't exist or update
         // the usage count for an existing tag record
         if ($sw_id) {
             $nxtdb->query($nxtdb->prepare("\n\t\t\t\t\tUPDATE {$this->sw_tables->tags}\n\t\t\t\t\tSET count=count+1\n\t\t\t\t\tWHERE term_id=%d", $sw_id));
         } else {
             $nxtdb->insert($this->sw_tables->tags, array('name' => $tag->name, 'slug' => $tag->slug, 'count' => 1), array('%s', '%s', '%d'));
             $sw_id = $nxtdb->insert_id;
         }
         // Create the actual tag-usage record
         $nxtdb->query($nxtdb->prepare("INSERT INTO {$this->sw_tables->tag_usage} (post_id, uses_tag, blog_id) VALUES (%d, %d, %d)", $post_id, $sw_id, $blog_id));
     }
     // Remove any unused tags, dropping the usage count, clearing the usage
     // record, and removing any now-unused tags from the sitewide table
     $drop_slugs = array_diff($sw_tag_slugs, $local_tag_slugs);
     foreach ($drop_slugs as $drop_slug) {
         $sw_id = $nxtdb->get_var($nxtdb->prepare("SELECT term_id FROM {$this->sw_tables->tags} WHERE slug=%s", ClassBlogs_Utils::slugify($drop_slug)));
         $nxtdb->query($nxtdb->prepare("UPDATE {$this->sw_tables->tags} SET count=count-1 WHERE term_id=%d", $sw_id));
         $nxtdb->query($nxtdb->prepare("DELETE FROM {$this->sw_tables->tag_usage}\n\t\t\t\tWHERE post_id=%d AND blog_id=%d AND uses_tag=%d", $post_id, $blog_id, $sw_id));
     }
     if (!empty($drop_slugs)) {
         $this->_remove_unused_sitewide_tags();
     }
     ClassBlogs_NXTClass::restore_current_blog();
 }