*/ WBClass::load('patI18n' , 'WBCache' , 'WBDatasource'); /** * 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' => '-1', 'ttl' => '', 'addrowvar' => '', 'userequest' => '', 'condition' => '', 'mandator' => '-1' ), $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'); $expire = array(); if (!empty($params['ttl']) && -1 < intval($params['ttl'])) { $expire['ttl'] = intval($params['ttl']); } WBCache::set($cacheId, $html, $expire); 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) { // normalize value if (strstr($value, '-')) { $value = explode('-', $value); $value = array_reverse($value); } else { $value = array($value); } if (strstr($params['searchfield'], ',')) { $field = explode(',', $params['searchfield']); $field = array_reverse($field); while (count($field) > count($value)) { array_pop($field); } } else{ $field = array($params['searchfield']); } if ('__new' == $value[0]) { return array(); } $table = $params['table']; $limit = intval($params['limit']); // load table object if( !self::$table ) { self::$table = WBClass::create('WBDatasource_Table'); } $clause = array(); // mandator if (0 <= $params['mandator']) { $clause[] = array( 'field' => self::$table->getIdentifier(WBDatasource::TABLE_MANDATOR, true), 'value' => $params['mandator'] ); } foreach ($value as $i => $v) { if (empty($field[$i])) { continue; } $clause[] = array( 'table' => $params['searchtable'], 'field' => $field[$i], 'relation' => $params['searchrelation'], 'value' => $v ); } $this->injectCondition($params, $clause); $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] ); } } }