* @package WB */ /** * Load base class */ WBClass::load( 'WBContent' ); /** * Content component: FAQ * * @version 0.4.0 * @package WB */ class WBContent_FAQ extends WBContent { const TABLE_FAQ = 'faq'; const TABLE_FAQTYPE = 'faqtype'; /** * FAQ parameter list * * action: article (dafault), type * * @var array */ protected $config = array( 'action' => 'article', 'id' => '', 'goto' => '0', 'limit' => 20, 'titlelist' => 'FAQ', 'titlearticle' => '%s - FAQ', 'titletypelist' => 'FAQ', 'titletype' => '%s - FAQ' ); /** * @var WBFormProcessor_Filter */ protected $filter; /** * pager information * @var array */ protected $pagerInfo = array(); /** * @var WBDatasource_Table */ private $table; /** * List of Allowed Types * * @var array */ private $typeList = null; /** * 2nd constructor * * Called after configuration was done */ protected function init() { $this->table = WBClass::create('WBDatasource_Table'); } /** * run * * run component * * @return array parameter list */ public function run() { $this->config['searchfields'] = array('question', 'answer'); $this->initFilter(); $this->addConfigAsGlobalVars(); // runArticle is the default method $method = $this->config['action']; if (empty($method)) { $method = 'article'; } $method = 'run' . ucFirst($method); if (method_exists($this, $method)) { return $this->$method(); } return $this->runArticle(); } /** * Run Article * * Show Article or article list * * @return array parameter list */ public function runArticle() { if ($this->displayArticleById($this->config['id'])) { return $this->config; } $this->bubbles['title'] = $this->config['titlelist']; $this->displayArticleList(); return $this->config; } /** * Run Type * * Show type or type list * * @return array parameter list */ public function runType() { if ($this->displayTypeById($this->config['id'])) { return $this->config; } $this->bubbles['title'] = $this->config['titletypelist']; $this->displayTypeList(); return $this->config; } /** * displayArticle * * displays single FAQ article * * @param int faqid * @return bool */ private function displayTypeById($id) { if (empty($id)) { return false; } $clause = array(); $this->injectAllowedTypes2Clause($clause); $type = $this->table->get(self::TABLE_FAQTYPE, $id, null, $clause); if (1 != count($type)) { return false; } $type = $type[0]; $this->bubbles['title'] = sprintf($this->config['titletype'], $type['title']); $this->tmpl->addGlobalVars($type, 'TYPE_'); $this->loadTemplates('type'); if ($this->tmpl->exists('list_entry')) { $this->articleList2Tmpl($type['id']); } return true; } /** * displayList * * displays list of FAQ entrys with pagination * * @return array parameter list */ private function displayTypeList() { $clause = array(); $this->injectAllowedTypes2Clause($clause); $list = $this->table->get(self::TABLE_FAQTYPE, null, null, $clause); $this->loadTemplates('type-list'); $this->list2Template($list); } /** * displayArticle * * displays single FAQ article * * @param int faqid * @return bool */ private function displayArticleById($id) { if (empty($id)) { return false; } $options = array(); $clause = array(); $clause[] = array( 'field' => 'enabled', 'value' => 1 ); $types = $this->injectAllowedTypes2Clause($clause); $faq = $this->table->get(self::TABLE_FAQ, $id, null, $clause); if (1 != count($faq)) { return false; } $faq = $faq[0]; $this->loadTemplates('article'); $this->tmpl->addGlobalVars($faq); $this->bubbles['title'] = sprintf($this->config['titlearticle'], $faq['question']); return true; } /** * Initialize Filter, if configured * */ protected function initFilter() { $this->filter = WBClass::create('WBFormProcessor_Filter'); $this->filter->setRequest($this->req); $this->filter->setTemplate($this->tmpl, $this->config['tmplDir']); /* $this->filter->setExternalVars($this->getFilterVars()); if ($this->config['usefilter']) { $this->filter->setFormConfigDir($this->getFormConfigDir(), 'snippet'); } */ $this->filter->buildClause($this->config['searchfields']); } /** * displayList * * displays list of FAQ entrys with pagination * * @param string typeid * @return array parameter list */ private function displayArticleList($typeid = null) { $this->loadTemplates('list'); $this->articleList2Tmpl($typeid); } /** * Load Articles and Populate Template * * @param string typeid * @return array parameter list */ private function articleList2Tmpl($typeid) { $options = array( 'limit' => $this->config['limit'], ); $clause = array(); $clause = $this->filter->getClause(); $clause[] = array( 'field' => 'enabled', 'value' => 1 ); if (!empty($typeid)) { $clause[] = array( 'field' => $this->table->getIdentifier(self::TABLE_FAQTYPE, true), 'value' => $typeid ); } $types = $this->injectAllowedTypes2Clause($clause); $pager = $this->table->getPager($this->part . __CLASS__, self::TABLE_FAQ, null, $clause, $options); $this->pagerInfo = $pager->browse($this->config['goto']); $faq = $pager->get(); $this->list2Template($faq); $this->tmpl->addGlobalVars($this->pagerInfo, 'pager_'); $this->tmpl->addGlobalVar('total_count', $this->pagerInfo['total']); } /** * Inject Types 2 Clause * * @param array * @return array */ private function injectAllowedTypes2Clause(&$clause) { if (!is_array($clause)) { $clause = array(); } $types = $this->getAllowedTypeIds(); if (empty($types)) { $types = array(); $clause[] = array( 'field' => $this->table->getIdentifier(self::TABLE_FAQTYPE, true), 'value' => 'NOT ALLOWED' ); return $types; } $clause[] = array( 'field' => $this->table->getIdentifier(self::TABLE_FAQTYPE, true), 'relation' => 'in', 'value' => $types ); return $types; } /** * Get List of Allowed Types * * Return list of type ids * * @return array */ private function getAllowedTypeIds() { // local cache - get list only once if (is_array($this->typeList)) { return $this->typeList; } $all = $this->table->get(self::TABLE_FAQTYPE, null, null); $list = array(); foreach ($all as $l) { switch ($l['requireuser']) { case 'anybody': $list[] = $l['id']; break; case 'anonymous'; if (!$this->user->isAuthenticated()) { $list[] = $l['id']; } break; case 'user'; if($this->user->isInGroup($l['requiregroup'], true)) { $list[] = $l['id']; } break; default: break; } } $this->typeList = $list; return $this->typeList; } }