*/ 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; /** * Request * @var WBRequest */ private $req; /** * 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' => '', 'searchtable' => '', 'searchfield' => '', 'searchrelation' => '=', 'join' => '', 'using' => '', 'order' => '', 'mindate' => '', 'addrowvar' => '', 'userequest' => '', ), $params); if (empty($params['table']) || empty($params['tmpl']) || empty($params['searchfield']) || empty($params['searchrelation'])) { return $value; } if (empty($params['searchtable'])) { $params['searchtable'] = $params['table']; } // 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->addGlobalVar('value', $value); $this->tmpl->addGlobalVars($params, 'ATT_'); $this->tmpl->readTemplatesFromInput($params['tmpl'] . '.tmpl'); $list = $this->getListFromRquest($value, $params); if (empty($list)) { $list = $this->getListFromTable($value, $params); } if (!empty($params['addrowvar'])) { foreach ($list as $i => &$l) { $l[$params['addrowvar']] = $i + 1; } } $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; } /** * Fetch List From Request * * @param string value * @param array parameter * @return array */ private function getListFromRquest($value, $params) { $list = array(); if (empty($params['userequest'])) { return $list; } $this->req = WBClass::create('WBRequest'); $list = $this->req->get($params['userequest'], $list); $list = array_values($list); return $list; } /** * Fetch List From Database * * @param string value * @param array parameter * @return array */ private function getListFromTable($value, $params) { $table = $params['table']; $limit = intval($params['limit']); // load table object if( !self::$table ) { self::$table = WBClass::create( 'WBDatasource_Table' ); } $clause = array(); $clause[] = array( 'table' => $params['searchtable'], 'field' => $params['searchfield'], 'relation' => $params['searchrelation'], 'value' => $value ); $option = array(); if (1 < $limit) { $option['limit'] = $limit; } // order if (!empty($params['order'])) { $option['order'] = array( 'field' => $params['order'], 'asc' => 1 ); if ('!' == $params['order'][0]) { $option['order']['field'] = substr($params['order'], 1); $option['order']['asc'] = 0; } if (strstr($option['order']['field'], '.')) { $tmp = explode('.', $option['order']['field'], 2); $option['order']['table'] = $tmp[0]; $option['order']['field'] = $tmp[1]; } } // simple join if (!empty($params['join'])) { $join = array( 'table' => $params['join'] ); $joinParam = array( 'using', 'onleft', 'lefttable', 'onright' ); foreach ($joinParam as $jp) { if (!empty($params[$jp])) { $join[$jp] = $params[$jp]; } } $option['join'] = array($join); } $list = self::$table->get($table, null, null, $clause, $option); return $list; } }