*/ WBClass::load('patI18n', 'WBCache'); /** * patTemplate modifier Locale * * Load column from database. The value should be the table's primary key * * Parameter: * - table, name of table to load from * - searchfield * - searchrelation * - limit * - tmpl * - pager * - mindate for caching * * @package patTemplate * @package Modifiers * @author gERD Schaufelberger */ class patTemplate_Modifier_Datalist extends patTemplate_Modifier { /** * shared table object * @var WBDatasource_Table */ static private $table = null; /** * 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( 'table' => '', 'limit' => '-1', 'tmpl' => '', 'searchfield' => '', 'searchrelation' => '=', 'mindate' => '' ), $params); if (empty($params['table']) || empty($params['tmpl']) || empty($params['searchfield']) || empty($params['searchrelation'])) { return $value; } // create cache id $cacheId = array( 'value:' . $value, '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); $html = WBCache::get($cacheId, $params['mindate']); if (null !== $html) { return $html; } $this->tmpl = WBClass::create( 'patTemplate' ); $this->tmpl->addGlobalVars($params, 'ATT_'); $this->tmpl->readTemplatesFromInput($params['tmpl'] . '.tmpl'); $table = $params['table']; $limit = intval($params['limit']); // load table object if( !self::$table ) { self::$table = WBClass::create( 'WBDatasource_Table' ); } $clause = array(); $clause[] = array( 'field' => $params['searchfield'], 'relation' => $params['searchrelation'], 'value' => $value ); $option = array(); if (1 < $limit) { $option['limit'] = $limit; } $list = self::$table->get($table, null, null, $clause, $option); $this->tmpl->addGlobalVar('list_entry_count', count($list)); if ($this->tmpl->exists('list_entry')) { $this->tmpl->addRows('list_entry', $list); } $html = $this->tmpl->getParsedTemplate('snippet'); WBCache::set($cacheId, $html); return $html; } } ?>