* @license PHP License * @package WB * @subpackage base */ /** * load base class */ WBClass::load('WBFormProcessor'); /** * Simple AJAX stub * * Allow to call method via AJAX request * * @version 0.3.1 * @package WB * @subpackage base */ abstract class WBAjax extends WBFormProcessor { /** * Request object * @var WBRequest */ protected $req; /** * template engine * @var patTemplate */ protected $tmpl; /** * current user's session * @var patSession */ protected $sess; /** * current user * @var WBUser_Auth */ protected $user; /** * local template folder * @var unknown_type */ protected $tmplDir; /** * whether to use template engine or not * @var bool */ protected $withFormTmpl = true; /** * default constructor */ final public function __construct() { $this->req = WBClass::create('WBRequest'); $this->sess = WBClass::create('patSession'); $this->tmpl = WBClass::create('patTemplate'); // extract template folder from class name minus WBAjax_ $this->tmplDir = str_replace('_', '/', substr(get_class($this), 7)); // add user data to template engine WBClass::load('WBUser'); $this->user = WBUser::getCurrent(); if ($this->user->isAuthenticated()) { $this->tmpl->addGlobalVars($this->user->getData(), 'user_current_'); $groups = array(); foreach ($this->user->getGroups() as $gid => $gname) { $groups[$gname] = '1'; } $this->tmpl->addGlobalVars($groups, 'user_current_group_'); } $this->init(); } /** * second constructor * * This protect the actual constructor. */ protected function init() { // empty default implementation } final public function call($method) { if (!method_exists($this, $method)) { WBClass::load('WBException_Class'); throw new WBException_Class('Undefined method "'.$method.'" called!', 1, __CLASS__); } return call_user_func(array($this, $method)); } /** * load templates from file * * @see * * @param string $tmpl * @param bool $local * @return bool true on success */ protected function loadTemplates($tmpl, $local = true) { if ($local) { $tmpl = $this->tmplDir . '/' . $tmpl; } return $this->tmpl->readTemplatesFromInput($tmpl . '.tmpl'); } /** * merge form parameters * * This allows to overwrite default form parameters * * @param array $param list of form parameters * @return bool */ protected function mergeFormParameters(&$params) { $clazz = get_class($this); if (strncmp('WBAjax_', $clazz, 7) == 0) { $clazz = 'WB.Ajax.' . substr($clazz, 7); } $clazz = str_replace('_', '.', $clazz); $params['action'] = '#'; $params['onsubmit'] = 'return ' . $clazz . '.current.submit(this);'; return true; } }