* @license PHP License * @package WB * @subpackage content */ /** * Load base class */ WBClass::load('WBContent'); /** * Content component * * Composite component does not provide any content of its own, * it executes and collect sub-components and renders threi HTML * strings to templates. The current version implements to renderer * processors: * - "Name" which renders the content to named template placeholders * - "List" renders contents to template list * * @version 0.5.1 * @package WB * @subpackage content */ class WBContent_Composite extends WBContent { /** * template object * @var patTemplate */ public $tmpl; /** * current status code * @var int */ private $statusCode = 100; /** * run * * run component * * @return array parameter list */ public function run() { $proc = strtolower($this->config['renderer']['processor']); switch ($proc) { case 'list': case 'name': $proc = ucfirst($proc); break; default: $proc = 'Name'; break; } if (isset($this->config['renderer']['bubbles']) && is_array($this->config['renderer']['bubbles'])) { $this->bubbles = $this->config['renderer']['bubbles']; } if (!isset($this->config['renderer']['params']['ajax'])) { $this->config['renderer']['params']['ajax'] = 1; } $this->config['renderer']['params']['ajax'] = intval($this->config['renderer']['params']['ajax']); $params = array( 'renderer' => array( 'processor' => $proc, 'params' => array( 'tmpl' => $this->config['renderer']['params']['tmpl'], 'ajax' => $this->config['renderer']['params']['ajax'] ), 'bubbles' => $this->bubbles ), 'content' => array(), ); $this->loadTemplates($params['renderer']['params']['tmpl']); $method = 'render' . $proc; return $this->$method($params); } /** * render list items * * Items are going to be displayed as simple list, one after the other * * @param array $params * @return array */ protected function renderList($params) { $list = array(); foreach($this->config['content'] as $name => $conf) { if (empty($conf)) { continue; } $string = $this->getComponentString($name, $conf, $params); $list[] = array( 'item' => $string, 'name' => $name, 'part' => $this->part ); } $this->tmpl->addRows('list', $list); return $params; } /** * render items into named template positions * * Each content item will be rendert to a particular, * named template placeholder * * @param array $params * @return array */ protected function renderName($params) { foreach($this->config['content'] as $name => $conf) { if (empty($conf) || !isset($conf['processor']) || empty($conf['processor'])) { continue; } $string = $this->getComponentString($name, $conf, $params); $this->tmpl->addGlobalVar( $name, $string); } return $params; } /** * run component end fetch resultung string * * Configure and execute component * * @param string $name or position * @param array $conf current component config * @param array $params passthrough parameters * @return string */ protected function getComponentString($name, $conf, &$params) { $comp = WBClass::create('WBContent_' . $conf['processor']); if (!isset($conf['params'])) { $conf['params'] = array(); } if (!isset($conf['bubbles'])) { $conf['bubbles'] = array(); } if (!isset($conf['ajax'])) { $conf['ajax'] = $this->config['renderer']['params']['ajax']; } $part = $this->part; if (!empty($part)) { $part .= '/'; } // remove bubbles according config if (is_array($conf['bubbles'])) { foreach ($conf['bubbles'] as $k => $v) { if (!is_array($v) || !empty($v)) { continue; } unset($conf['bubbles'][$k]); if (isset($this->bubbles[$k])) { unset($this->bubbles[$k]); } } } $comp->configure($part . $name, $conf['params'], $conf['bubbles']); $params['content'][$name] = $conf; $params['content'][$name]['params'] = $comp->run(); $js = ''; if (intval($conf['ajax'])) { $js = $this->addComponentJavaScript($part . $name); } $this->bubbles = array_merge($this->bubbles, $comp->getBubbles()); // disable caching if one component don't allow caches $this->cachable &= $comp->isCachable(); // update status code $this->statusCode = max($this->statusCode, $comp->getStatusCode()); return $comp->getString() . $js; } /** * add javascipt invoker * * Originally Wombat uses prototype.js and therefore WB.ContentArea is bases on * prototype. Override thif method in case you want to use custom JavaScript claaes * * @param $id * @return string */ protected function addComponentJavaScript($id) { return sprintf('', $id); } /** * receive output * * fetch output of this content component * * @return string */ public function getString() { $this->tmpl->addGlobalVars($this->bubbles); return $this->tmpl->getParsedTemplate('snippet'); } /** * Get response status code * * @return int */ public function getStatusCode() { return $this->statusCode; } }