Skip to content

Commit

Permalink
Taggable Behavior enhancements
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre-Emmanuel Fringant committed Apr 18, 2009
1 parent e502aa0 commit 898151b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 15 deletions.
2 changes: 1 addition & 1 deletion README
Expand Up @@ -9,7 +9,7 @@ Multi-model tagging plugin for CakePHP.

git clone git://github.com/kalt/tagging.git

Or click 'download' and copy the content of the zip file into your 'plugins' directory.
Or click 'download' and copy the content of the zip file into your 'plugins' folder.

1.2. DB tables
--------------
Expand Down
9 changes: 1 addition & 8 deletions models/behaviors/taggable.php
Expand Up @@ -165,13 +165,6 @@ function findRelated(&$model, $id = null, $restrict_to_model = true, $limit = nu

$tag_ids = Set::extract('/Tag/id', $tags);

// Excludind this $id from the results
$exclude = $this->Tagged->find('first', array(
'fields' => 'id',
'conditions' => array('model' => $model->alias, 'model_id' => $id),
'recursive' => -1
));

// Restrict to Model ?
$taggedWith_model = null;

Expand All @@ -181,7 +174,7 @@ function findRelated(&$model, $id = null, $restrict_to_model = true, $limit = nu
}

// Related records
$related = $this->Tagged->taggedWith($taggedWith_model, $tag_ids, $exclude['Tagged']['id'], $limit);
$related = $this->Tagged->taggedWith($taggedWith_model, $tag_ids, $id, $limit);

if(empty($related))
{
Expand Down
18 changes: 12 additions & 6 deletions models/tagged.php
Expand Up @@ -91,14 +91,14 @@ function tagCloud($model, $options = array())
}

/**
* Find records tagged with $tag_ids, excluding record of id $exclude_id
* Find records tagged with $tag_ids, excluding a model_id
*
* @param unknown_type $model
* @param unknown_type $tag_ids
* @param unknown_type $exclude_model_id
* @param string $model Model name
* @param mixed $tag_ids Tag id(s)
* @param int $exclude_id Model id to exclude
* @return array
*/
function taggedWith($model = null, $tag_ids = array(), $exclude_id = null, $limit = null)
function taggedWith($model = null, $tag_ids = null, $exclude_id = null, $limit = null)
{
$conditions = array(
'tag_id' => $tag_ids,
Expand All @@ -111,7 +111,13 @@ function taggedWith($model = null, $tag_ids = array(), $exclude_id = null, $limi

if($exclude_id)
{
$conditions['id !='] = $exclude_id;
$exclude_ids = array_values($this->find('list', array(
'fields' => 'id',
'conditions' => array('model' => $model, 'model_id' => $exclude_id),
'recursive' => -1
)));

$conditions['NOT'] = array('id' => $exclude_ids);
}

$fields = array('model', 'model_id', 'COUNT(*) as count');
Expand Down

0 comments on commit 898151b

Please sign in to comment.