* @license PHP License * @package WB * @subpackage base */ WBClass::load('WBAjax'); /** * Simple AJAX stub: Rating * * Allow to call method via AJAX request * * @version 0.1.1 * @package WB * @subpackage base */ class WBAjax_Rate extends WBAjax { /** * Rating * @var WBDatasource_Rate */ protected $rate; /** * second construcotr * * Called by constructer of super class. * * @see include/WB/WBAjax#init() */ protected function init() { $this->rate = WBClass::create('WBDatasource_Rate'); } /** * Add rating * * Save rating and optionally render current rating * * @see display() * @return string HTML */ protected function add() { $namespace = $this->req->get('namespace', ''); $xid = $this->req->get('xid', 0); $value = $this->req->get('value', 0); $display = intval($this->req->get('display', 0)); try{ $this->rate->setNamespace($namespace); $this->rate->setXId($xid); $this->rate->add($value); } catch (WBException $e) { if (1 <= $display) { return $this->display(); } return 'failed'; } if (1 <= $display) { return $this->display(); } return 'OK'; } /** * Render current rating * * Load templates, set rates as variables and get parsed template */ private function display() { $ns = $this->rate->getNamespace(); $xid = $this->rate->getXid(); $this->loadTemplates($ns); $rate = array(); if (!empty($xid) && !empty($ns)) { $rate = $this->rate->get(); } $rate['avg'] = sprintf('%0.1f', $rate['average']); for ($i = 1; $i <= $rate['average']; ++$i) { $rate[$i . '_set'] = 'set'; } $this->tmpl->addGlobalVars($rate, 'rate_'); return $this->tmpl->getParsedTemplate('snippet'); } }