
Sabot
By: a guest on
Nov 7th, 2009 | syntax:
PHP | size: 4.37 KB | hits: 65 | expires: Never
class Tag_Model extends Model
{
public function get_related_tags( $tags )
{
## Get db entries with specific tags and build array with counts
## is it cached already? ------------------------------------------------
$this->cache = Cache::instance();
$cache = $this->cache->get( $cache_name );
if( $cache )
return $cache;
## not cached, fire up ---------------------------------------------------
$db = Database::instance();
## count tagged items ----------------------------------------------------
// build like string
foreach( $tags as $tag )
$like[] = "tags LIKE '%$tag%'";
// get counts
$count = $db->query("SELECT count(id) AS count FROM `articles` WHERE $like")->current()->count;
## check what tags are related ------------------------------------------
$offset = 0;
$step = 300;
while( $offset < $count )
{
$assets = $db->query("SELECT tags FROM `articles` WHERE $like ORDER BY id ASC LIMIT $step OFFSET $offset");
foreach($assets as $asset)
{
// tags
foreach( $input as $k => $v )
{
if( $v == ''){
//do nothing, shouldnt be here anyway
}
$related_tags[$v]++;
}
else{
$related_tags[$v] = 1;
}
}
}
$offset += $step;
}
// remove already displayed from list
foreach( $tags as $tag )
unset( $related_tags[$tag] );
// set cache
$this->cache->set( $cache_name, array($related_tags, $count), 'related_tags_counts', 0
);
return array($related_tags, $count);
}
public function update_tags( $tags, $saved_tags )
{
// make array if not already
{
}
{
}
// find new tags
// update counts
if( count($new_tags) > 0
)
{
$this->add_tags($new_tags);
}
// find removed tags
// update counts
if( count($removed_tags) > 0
)
{
$this->remove_tags($removed_tags);
}
}
private function add_tags( $new_tags )
{
$db = Database::instance();
// check if tags are new system wide
// try to load all and then compare
$tags_in_db = $db->in('tag_name', $new_tags)->get('tags');
foreach( $tags_in_db as $t )
{
$tags_db[] = $t->tag_name;
}
// insert new tags to db if necessary
if( count($system_new_tags) > 0
)
{
foreach( $system_new_tags as $t )
{
$tag_array[] = "('$t')";
}
$tag_str = implode(',', $tag_array);
$sql = "INSERT INTO tags (tag_name) VALUES $tag_str";
$db->query($sql);
}
// update counts
foreach( $new_tags as $t )
{
$tag_array[] = "'$t'";
}
$tag_str = implode(',', $tag_array);
$sql = "UPDATE tags SET `tag_count`=`tag_count`+1 WHERE `tag_name` IN ($tag_str)";
$db->query($sql);
}
private function remove_tags( $removed_tags )
{
$db = Database::instance();
// update counts
foreach( $removed_tags as $t )
{
$tag_array[] = "'$t'";
}
$tag_str = implode(',', $tag_array);
$sql = "UPDATE tags SET `tag_count`=`tag_count`-1 WHERE `tag_name` IN ($tag_str)";
$db->query($sql);
}
}