*/ WBClass::load('patI18n' , 'WBCache'); /** * patTemplate modfifier Key-Value-Pair * * Load list of key-value pairs and display using a template * * @package patTemplate * @package Modifiers * @version 1.0.0 * @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', 'limit' => 0, 'mandatorid' => 0 ), $params); if (empty($params['tmpl'])) { return $value; } // create cache id $cacheId = array( 'value:' . $value, 'mandatorid:' . $params['mandatorid'], '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) { /** @var WBMandator */ $mandator = WBClass::create('WBMandator'); if (!empty($params['mandatorid'])) { $mandator->setId($params['mandatorid']); } $parameter = array( 'mandator' => $mandator ); $this->kvp = WBClass::create('WBDatasource_KeyValuePair', $parameter); $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; } $limit = intval($params['limit']); if (0 < $limit && $count > $limit) { $this->tmpl->addGlobalVar('list_entry_cut', 1); $list = array_slice($list, 0, $limit); } // 2col mode if (strstr($params['tmpl'] , '2col')) { $new = array(); for ($i = 0; $i < count($list);) { $j = $i + 1; $tmp = array(); foreach ($list[$i] as $k => $v) { $tmp[$k . '0'] = $v; } if (isset($list[$j])) { foreach ($list[$j] as $k => $v) { $tmp[$k . '1'] = $v; } } else { foreach ($list[$i] as $k => $v) { $tmp[$k . '1'] = ''; } } $new[] = $tmp; $i += 2; } $list = $new; } $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': /** @var WBDatasource_KeyValuePair_ListMerger */ $merger = WBClass::create('WBDatasource_KeyValuePair_ListMerger_Leading'); break; case 'ordered': default: /** @var WBDatasource_KeyValuePair_ListMerger */ $merger = WBClass::create('WBDatasource_KeyValuePair_ListMerger_Ordered'); break; } $merger->setKeyValuePair($this->kvp); return $merger->get($value, $params); } }