*/ WBClass::load('patI18n' , 'WBCache'); /** * patTemplate modfifier Key-Value-Pair * * Load list of key-value pairs and display using a template * * @package patTemplate * @package Modifiers * @author gERD Schaufelberger */ class patTemplate_Modifier_Keyvaluepair extends patTemplate_Modifier { /** * Key Value Pairs * @var WBDatasource_KeyValuePair */ private $kvp; /** * template engine * @var patTemplate */ private $tmpl; /** * modify the value * * @param string value * @param array list of parameter * @return string modified value */ public function modify($value, $params = array()) { $params = array_merge( array( 'domain' => '', 'tmpl' => 'KeyValuePair/default', 'tmpltype' => 'html', 'mindate' => '', 'parent' => '', 'mergemode' => 'ordered', // or 'leading' 'mode' => 'html' ), $params); if (empty($params['tmpl'])) { return $value; } // create cache id $cacheId = array( 'value:' . $value, 'mergemode:' . $params['mergemode'], 'lang:' . patI18n::getLocale(patI18n::LOCALE_TYPE_LANG) ); foreach ($params as $k => $v) { if ($k == 'mindate') { continue; } $cacheId[] = sprintf('%s:%s', $k, $v); } $cacheId = implode('-', $cacheId); $list = WBCache::get($cacheId, $params['mindate']); if (null === $list) { $this->kvp = WBClass::create('WBDatasource_KeyValuePair'); $this->kvp->setDomain($params['domain']); $list = $this->getMergedList($value, $params); } $tmpl = $params['tmpl']; $this->tmpl = WBClass::create('patTemplate'); if ('html' != $params['tmpltype']) { $this->tmpl->setType($params['tmpltype']); $this->tmpl->setDefaultAttribute('whitespace', 'keep'); $tmpl .= '.' . $params['tmpltype']; } $this->tmpl->addGlobalVar('keyvaluepairid', $value); $this->tmpl->addGlobalVars($params, 'ATT_'); $this->tmpl->readTemplatesFromInput($tmpl . '.tmpl'); $count = count($list); if ('count' == $params['mode']) { WBCache::set($cacheId, $list); return $count; } $this->tmpl->addGlobalVar('list_entry_count', $count); if ($this->tmpl->exists('list_entry')) { $this->tmpl->addRows('list_entry', $list); } $html = $this->tmpl->getParsedTemplate('snippet'); WBCache::set($cacheId, $list); return $html; } /** * Get list of key value pairs according to merge mode * * @param string $value * @param array $params * @return array */ private function getMergedList($value, $params) { switch (strtolower($params['mergemode'])) { case 'leading': $merger = WBClass::create('WBDatasource_KeyValuePair_ListMerger_Leading'); break; case 'ordered': default: $merger = WBClass::create('WBDatasource_KeyValuePair_ListMerger_Ordered'); break; } $merger->setKeyValuePair($this->kvp); return $merger->get($value, $params); } }