Skip to content

Instantly share code, notes, and snippets.

@romaninsh
Created November 21, 2012 17:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save romaninsh/4126496 to your computer and use it in GitHub Desktop.
Save romaninsh/4126496 to your computer and use it in GitHub Desktop.
class Controller_Element extends Controller {
function addFields($m){
$m->addField('name');
$m->hasOne('Profile','owner_profile_id');
$m->hasOne('User','created_by');
$m->addField('element_type');
$m->addField('descr')->type('text')->caption('Description');
$m->addField('seo_name');
$m->addField('tmp_followed_by')->type('int')->defaultValue(0)->caption('Followers');
$this->owner->addMethod('follow,unfollow',$this);
}
function follow($m,Model_Profile $p=null){
if($p==null)$p=$this->api->myProfile();
if ($p->id == $m->id) throw $this->exception('You can\'t follow yourself','ForUser');
$f=$m->ref('Follower','model')
->addCondition('profile_id',$p->id)
->tryLoadAny();
if(!$f->loaded()){
$f->saveAndUnload();
//$m['tmp_followed_by']=$m['tmp_followed_by']+1;
$fm = $this->add('Model_Follower');
$fm->setMasterField('element_id',$m->get('element_id'));
$m['tmp_followed_by'] = count($fm->getRows());
$m->saveLater();
}
$this->api->al->log('followed',$m,$p);
}
function unfollow($m,Model_Profile $p=null){
if ($this->api->myProfile()->id == $m->id)
throw $this->exception('You can\'t follow yourself','ForUser');
if($p==null)$p=$this->api->myProfile();
$f=$m->ref('Follower','model')
->addCondition('profile_id',$p->id)
->tryLoadAny();
if($f->loaded()){
$f->delete();
//$m['tmp_followed_by']=$m['tmp_followed_by']-1;
$fm = $this->add('Model_Follower');
$fm->setMasterField('element_id',$m->get('element_id'));
$m['tmp_followed_by'] = count($fm->getRows());
$m->saveLater();
}
$this->api->al->log('stopped following',$m,$p);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment