*/ WBClass::load('WBHtml_JS' , 'WBException_Config' , 'WBException_Argument' ); /** * Add User Rating * * Simple template function to add list of user's comments * * @version 0.2.2 * @package Wombat * @subpackage patTemplate */ class patTemplate_Function_Rate extends patTemplate_Function { /** * name of the function * @var string */ public $_name = 'Rate'; /** * tell that this function has to be executed during runtime * @var string */ public $_type = PATTEMPLATE_FUNCTION_RUNTIME; /** * Rating * @var WBDatasource_Rate */ protected $rate; /** * Template engine * @var patTemplate */ protected $tmpl; /** * call the function * * @access public * @param array $params parameters of the function (= attributes of the tag) * @param string $content inside tag * @return string content to insert into the template * @todo reconsider how to load JavaScript-Class: Comment */ public function call($params, $content) { // content must be the xid $content = trim($content); if (empty($content)) { return ''; } // Id starts with number if (!preg_match('/^\d+/', $content)) { return ''; } // namespace is required if (!isset($params['namespace']) || empty($params['namespace'])){ return ''; } // show form element if (isset($params['showform'])){ $params['showform'] = intval($params['showform']); } else { $params['showform'] = 1; } // init comments $this->rate = WBClass::create('WBDatasource_Rate'); try { $this->rate->setNamespace($params['namespace']); $this->rate->setXId($content); } catch (WBException_Config $e) { // looks like namespace is not configured return ''; } catch (WBException_Argument $e) { // looks like xid is wrong return ''; } $this->fillTemplate($params, $content); return $this->tmpl->getParsedTemplate('snippet'); } /** * start and setup template engine * * Load patTemplate, add important global variables and attributes * and finally load template * @param array $params * @param string xid */ protected function fillTemplate($params, $xid) { $this->tmpl = WBClass::create('patTemplate'); $this->tmpl->addGlobalVars($params, 'att_'); $this->tmpl->addGlobalVar('att_xid', $xid); $this->tmpl->addGlobalVars($this->rate->get(), 'rate_'); $this->tmpl->addGlobalVar('att_allowed', intval($this->rate->isAllowed())); // allow to transport placeholder to surrounding templates $this->tmpl->setDefaultAttribute('unusedvars', 'ignore'); // add user data WBClass::load('WBUser'); $user = WBUser::getCurrent(); if ($user->isAuthenticated()) { $this->tmpl->addGlobalVars($user->getData(), 'user_current_'); $groups = array(); foreach ($user->getGroups() as $gid => $gname) { $groups[$gname] = '1'; } $this->tmpl->addGlobalVars($groups, 'user_current_group_'); } // load template $this->tmpl->readTemplatesFromInput(sprintf('Rate/namespace/%s.tmpl', $params['namespace'])); } }