*/ 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', '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); } $this->tmpl = WBClass::create( 'patTemplate' ); $this->tmpl->addGlobalVar('keyvaluepairid', $value); $this->tmpl->addGlobalVars($params, 'ATT_'); $this->tmpl->readTemplatesFromInput($params['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': return $this->getMergedListLeading($value, $params); break; case 'ordered': default: break; } return $this->getMergedListOrdered($value, $params); } /** * Merge key value list with parent list * * Fill list with key-value-pairs from parent record. Child's * records are in front, parent's follow. * * @param string $value id * @param array $params */ private function getMergedListLeading($value, $params) { $list = $this->kvp->get($value, true); // simple list without merging if (empty($params['parent']) || $params['parent'] == $value) { foreach ($list as $j => &$l) { $l['parentvalue'] = ''; $l['class'] = ''; } return $list; } // remove duplicates $pList = $this->kvp->get($params['parent'], true); $found = array(); foreach ($pList as $pk => &$pv) { $pv['parentvalue'] = ''; $pv['class'] = ''; foreach ($list as $lk => &$lv) { if ($lv['key'] == $pv['key']) { $found[] = $lk; $lv['parentvalue'] = $pv['value']; $lv['class'] = 'merged'; unset($pList[$pk]); break; } if (!in_array($lk, $found)) { $lv['parentvalue'] = ''; $lv['class'] = 'added'; } } } $pList = array_values($pList); return array_merge($list, $pList); } /** * Merge key value list with parent list * * Fill list with key-value-pairs from parent record. Child's * records are ordered * * @param string $value id * @param array $params */ private function getMergedListOrdered($value, $params) { $id = array($value); if (!empty($params['parent']) && $params['parent'] != $value) { $id[] = $params['parent']; } $list = $this->kvp->get($id, true); // simple list without merging if (1 == count($id)) { foreach ($list as $j => &$c) { $l['parentvalue'] = ''; $l['class'] = ''; } return $list; } // merge list $merged = array(); foreach ($list as $i => &$l) { if ($id[1] == $l['id']) { foreach ($list as $j => &$c) { if ($id[0] == $c['id'] && $c['key'] == $l['key']) { $c['parentvalue'] = $l['value']; $c['class'] = 'merged'; $merged[] = $c['key']; unset($list[$i]); break; } } $l['parentvalue'] = ''; $l['class'] = ''; } else if (!in_array($l['key'], $merged)) { $l['class'] = 'added'; } } return array_values($list); } }