Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.