Created
April 26, 2009 09:25
-
-
Save slywalker/101978 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class TagBehavior extends ModelBehavior { | |
var $targetModel = array(); | |
var $dependModel = array(); | |
var $transactionModel = array(); | |
var $dependTransactionModel = array(); | |
var $countField = array(); | |
function setup(&$model, $config=array()) | |
{ | |
if (isset($config['targetModel'])) { | |
$this->targetModel[$model->alias] = $config['targetModel']; | |
$this->countField[$model->alias] = | |
Inflector::underscore($config['targetModel']).'_count'; | |
$array = array($model->alias, $config['targetModel']); | |
sort($array); | |
$this->transactionModel[$model->alias] = | |
Inflector::pluralize($array[0]).$array[1]; | |
} else { | |
trigger_error(__("TagBehavior: TargetModel is not set in config {$model->alias}.", true), E_USER_ERROR); | |
} | |
if (isset($config['dependModel'])) { | |
$this->dependModel[$model->alias] = $config['dependModel']; | |
$array = array($model->alias, $config['dependModel']); | |
sort($array); | |
$this->dependTransactionModel[$model->alias] = | |
Inflector::pluralize($array[0]).$array[1]; | |
} | |
} | |
function findDefault(&$model, $options = array(), $fieldName = 'default') | |
{ | |
$conditions = array($model->alias.'.'.$fieldName=>1); | |
if (isset($options['conditions'])) { | |
$conditions = Set::merge($conditions, $options['conditions']); | |
} | |
return $model->find('list', compact('conditions')); | |
} | |
function findTop(&$model, $options = array()) | |
{ | |
$countField = $this->countField[$model->alias]; | |
$conditions = array($countField.' >'=>0); | |
if (isset($options['conditions'])) { | |
$conditions = Set::merge($conditions, $options['conditions']); | |
} | |
$limit = 30; | |
if (isset($options['limit'])) { | |
$limit = $options['limit']; | |
} | |
$order = array($model->alias.'.'.$countField=>'DESC'); | |
$contain = false; | |
$tags = $model->find('all', | |
compact('conditions', 'order', 'limit', 'contain')); | |
$result = array(); | |
foreach ($tags as $tag) { | |
$result[$tag[$model->alias]['id']] = | |
$tag[$model->alias]['name']. | |
'('.$tag[$model->alias][$countField].')'; | |
} | |
return $result; | |
} | |
function findWithCount(&$model, $method, $options = array()) | |
{ | |
$targetModel = $this->targetModel[$model->alias]; | |
$transactionModel = $this->transactionModel[$model->alias]; | |
$fk = $model->hasAndBelongsToMany[$targetModel]['foreignKey']; | |
$model->unbindModel(array( | |
'hasAndBelongsToMany'=>array($targetModel))); | |
$model->bindModel(array( | |
'hasOne'=>array( | |
$transactionModel=>array( | |
'fields'=>array( | |
'COUNT('.$transactionModel.'.'.$fk.') AS count', | |
), | |
), | |
), | |
)); | |
$options['group'] = array($model->alias.'.id'); | |
$order = array('count'=>'DESC'); | |
if (isset($options['order'])) { | |
$options['order'] = array_merge($order, $options['order']); | |
} else { | |
$options['order'] = $order; | |
} | |
return $model->find($method, $options); | |
} | |
function updateCount(&$model, $id) | |
{ | |
$countField = $this->countField[$model->alias]; | |
$conditions = array($model->alias.'.id'=>$id); | |
$count = $model->findWithCount('first', compact('conditions')); | |
if ($count) { | |
$model->id = $id; | |
return $model->saveField($countField, $count[0]['count']); | |
} | |
return true; | |
} | |
function deleteHabtm(&$model, $id) | |
{ | |
$dependModel = $this->dependModel[$model->alias]; | |
$dependTransactionModel = | |
$this->dependTransactionModel[$model->alias]; | |
$fk = $model->hasAndBelongsToMany[$dependModel]['foreignKey']; | |
$model->unbindModel(array( | |
'hasAndBelongsToMany'=>array($dependModel))); | |
$model->bindModel(array( | |
'hasOne'=>array( | |
$dependTransactionModel=>array( | |
'fields'=>array( | |
'COUNT('.$dependTransactionModel.'.'.$fk.') AS count', | |
), | |
), | |
), | |
)); | |
$conditions = array($dependTransactionModel.'.'.$fk=>$id); | |
$id = $model->{$dependTransactionModel}->field('id', $conditions); | |
if ($id) { | |
return $model->{$dependTransactionModel}->delete($id); | |
} | |
return true; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment