* @license http://opensource.org/licenses/bsd-license.php */ class Add2Fav { public $label_add; public $label_rem; public $key_name_add = 'add2fav_label_add'; public $key_name_rem = 'add2fav_label_rem'; public $key_name_reg = 'add2fav_label_reg'; public $key_name_off = 'add2fav_label_off'; public $def_add = "Add to Favorites"; public $def_rem = "Remove from Favorites"; public $def_reg = ""; public $def_off = "#"; private $url; private $lastaction; public function getOptionAdd(){ return $this->getOption($this->key_name_add,$this->def_add); } public function getOptionRem(){ return $this->getOption($this->key_name_rem,$this->def_rem); } public function getOptionReg(){ return $this->getOption($this->key_name_reg,$this->def_reg); } public function getOptionOff(){ return $this->getOption($this->key_name_off,$this->def_off); } public function saveOptionAdd($v){ $vv = trim($v); if(empty($vv)) $vv = $this->def_add; update_option($this->key_name_add, $vv); } public function saveOptionRem($v){ $vv = trim($v); if(empty($vv)) $vv = $this->def_rem; update_option($this->key_name_rem, $vv); } public function saveOptionReg($v){ $vv = trim($v); if(empty($vv)) $vv = $this->def_reg; update_option($this->key_name_reg, $vv); } public function saveOptionOff($v){ $vv = trim($v); if(empty($vv)) $vv = $this->def_off; update_option($this->key_name_off, $vv); } /* called to inform the new instance about the current URL to be processed for the current logged on user */ public function prepare($url){ $this->url = trim(rtrim($url,"#/?")," "); } public function exportJs(){ return json_encode(array( 'label'=> $this->getCurrentUserLabel(), 'status'=> $this->getCurrentUserStatus(), 'url'=> $this->getOptionOff(), 'hasuser'=> $this->isUserLoggedOn(), 'lastaction'=> $this->lastaction, 'lasturl'=> $this->url, ) ); } /* take an action, */ public function toggle(){ if($this->isUserLoggedOn()){ if($this->isDataSaved($this->url)){ $this->removeData($this->url); $this->lastaction = 'removed'; }else{ $this->saveData($this->url); $this->lastaction = 'added'; } } } /* query if this URL is saved for the current user */ public function getCurrentUserStatus(){ return "mustreg"; } /* returns the appropiated label depending on the association between the URL in the current user */ public function getCurrentUserLabel(){ if($this->isUserLoggedOn()){ if($this->isDataSaved($this->url)){ return $this->getOptionRem(); }else return $this->getOptionAdd(); } else return $this->getOptionReg(); } /* create the layout */ public function buildTag($iconname, $cssname){ if($this->isUserLoggedOn() || ((false==$this->isUserLoggedOn()) && ($this->getOptionReg()!=''))){ $iconurl = $this->getIcon($iconname); return "