*/ 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); $max = WBParam::get('wb/template/listtemplatemax', 10); for ($i = 1; $i < $max; ++$i) { if (!$this->tmpl->exists('list_entry_' . $i)) { break; } $this->tmpl->addRows('list_entry_' . $i, $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 (0 < $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; } /** * Inject Conditions to Clause * * Conditionattribute e.g. "foo=bar,bli=bar" to clause * * @param array * @param array */ private function injectCondition($param, &$clause) { $param['condition'] = trim($param['condition']); if (empty($param['condition'])) { return; } $con = array_map('trim', explode(',', $param['condition'])); foreach ($con as $c) { $c = explode('=', $c); if (2 != count($c)) { continue; } $f = explode('.', $c[0]); if (2 == count($f)) { $clause[] = array( 'table' => $f[0], 'field' => $f[1], 'value' => $c[1] ); continue; } $clause[] = array( 'field' => $c[0], 'value' => $c[1] ); } } }