Advertisement
Guest User

Sabot

a guest
Nov 7th, 2009
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.37 KB | None | 0 0
  1. class Tag_Model extends Model
  2. {
  3.     public function get_related_tags( $tags )
  4.     {
  5.         ## Get db entries with specific tags and build array with counts
  6.  
  7.         ## is it cached already? ------------------------------------------------
  8.        $this->cache = Cache::instance();
  9.         $tags = array_filter( array_flip(array_flip($tags)) );
  10.         sort($tags);
  11.         $cache_name = implode('', $tags);
  12.         $cache = $this->cache->get( $cache_name );
  13.          
  14.         if( $cache )
  15.          return $cache;
  16.  
  17.         ## not cached, fire up ---------------------------------------------------
  18.  
  19.         $db = Database::instance();
  20.        
  21.         ## count tagged items ----------------------------------------------------
  22.  
  23.         // build like string
  24.         $like = array();
  25.         foreach( $tags as $tag )
  26.            $like[] = "tags LIKE '%$tag%'";
  27.        
  28.         $like = implode(' AND ', $like);
  29.        
  30.         // get counts
  31.         $count = $db->query("SELECT count(id) AS count FROM `articles` WHERE $like")->current()->count;
  32.        
  33.         ## check what tags are related ------------------------------------------
  34.  
  35.         $offset = 0;
  36.         $step = 300;
  37.        
  38.         $related_tags = array();
  39.        
  40.         while( $offset < $count )
  41.         {
  42.             $assets = $db->query("SELECT tags FROM `articles` WHERE $like ORDER BY id ASC LIMIT $step OFFSET $offset");
  43.        
  44.             foreach($assets as $asset)
  45.             {
  46.                 // tags
  47.                 $input = explode( ' ', trim($asset->tags) );
  48.                 foreach( $input as $k => $v )
  49.                 {
  50.                      if( $v == ''){
  51.                          //do nothing, shouldnt be here anyway
  52.                      }
  53.                      elseif( array_key_exists($v, $related_tags) ){
  54.                          $related_tags[$v]++;
  55.                      }
  56.                      else{
  57.                         $related_tags[$v] = 1;
  58.                      }        
  59.                 }
  60.             }
  61.             $offset += $step;
  62.         }
  63.  
  64.         // remove already displayed from list
  65.         foreach( $tags as $tag )
  66.             unset( $related_tags[$tag] );
  67.        
  68.         ksort($related_tags);
  69.  
  70.         // set cache
  71.         $this->cache->set( $cache_name, array($related_tags, $count), 'related_tags_counts', 0);
  72.        
  73.         return array($related_tags, $count);
  74.     }
  75.  
  76.     public function update_tags( $tags, $saved_tags )
  77.     {      
  78.         // make array if not already
  79.         if( ! is_array($tags) )
  80.         {
  81.             $tags = explode(' ', trim($tags) );
  82.         }
  83.        
  84.         if( ! is_array($saved_tags) )
  85.         {
  86.             $saved_tags = explode(' ', trim($saved_tags) );
  87.         }
  88.        
  89.         // find new tags
  90.         $new_tags = array_filter( array_diff($tags, $saved_tags) );
  91.        
  92.             // update counts
  93.             if( count($new_tags) > 0 )
  94.             {
  95.                 $this->add_tags($new_tags);
  96.             }  
  97.        
  98.         // find removed tags
  99.         $removed_tags = array_filter( array_diff($saved_tags, $tags) );
  100.    
  101.             // update counts
  102.             if( count($removed_tags) > 0 )
  103.             {
  104.                 $this->remove_tags($removed_tags);     
  105.             }
  106.     }
  107.    
  108.     private function add_tags( $new_tags )
  109.     {
  110.         $db = Database::instance();
  111.    
  112.         // check if tags are new system wide
  113.         // try to load all and then compare
  114.         $tags_db = array();    
  115.         $tags_in_db = $db->in('tag_name', $new_tags)->get('tags');
  116.        
  117.         foreach( $tags_in_db as $t )
  118.         {
  119.             $tags_db[] = $t->tag_name;
  120.         }
  121.        
  122.         $system_new_tags = array_filter( array_diff($new_tags, $tags_db) );
  123.    
  124.         // insert new tags to db if necessary
  125.         if( count($system_new_tags) > 0 )
  126.         {
  127.             $tag_array = array();
  128.             foreach( $system_new_tags as $t )
  129.             {
  130.                 $tag_array[] = "('$t')";
  131.             }
  132.            
  133.             $tag_str = implode(',', $tag_array);
  134.                    
  135.             $sql = "INSERT INTO tags (tag_name) VALUES $tag_str";
  136.             $db->query($sql);
  137.         }  
  138.            
  139.         // update counts
  140.         $tag_array = array();
  141.         foreach( $new_tags as $t )
  142.         {
  143.             $tag_array[] = "'$t'";
  144.         }
  145.        
  146.         $tag_str = implode(',', $tag_array);
  147.        
  148.         $sql = "UPDATE tags SET `tag_count`=`tag_count`+1 WHERE `tag_name` IN ($tag_str)";
  149.         $db->query($sql);
  150.     }
  151.    
  152.     private function remove_tags( $removed_tags )
  153.     {
  154.         $db = Database::instance();
  155.  
  156.         // update counts
  157.         $tag_array = array();
  158.         foreach( $removed_tags as $t )
  159.         {
  160.             $tag_array[] = "'$t'";
  161.         }
  162.        
  163.         $tag_str = implode(',', $tag_array);
  164.        
  165.         $sql = "UPDATE tags SET `tag_count`=`tag_count`-1 WHERE `tag_name` IN ($tag_str)";
  166.         $db->query($sql);      
  167.     }
  168.    
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement