* @license PHP License * @package WB * @subpackage datasource */ WBClass::load('WBDatasource_TreeMenu_Child' ); /** * Datasource TreeMenu Children: Blog * * Add recent blog articles to TreeMenu * * @todo read config * @todo add config parameters for output and article list * @version 1.0.0 * @package WB * @subpackage datasource */ class WBDatasource_TreeMenu_Child_Blog extends WBDatasource_TreeMenu_Child { /** * Blog Tool * @var WBBlog_Tool */ private $blogTool; /** * Constructor * * Init blog tool */ protected function init() { $this->blogTool = WBClass::create('WBBlog_Tool'); $this->params['pathprefix'] = $this->blogTool->getListPath(); } /** * Check URL dictionary record * * Return menu record * * @param string $urlid * @param string $url * @return array */ public function getNode($urlid, $url) { $url = WBString::replaceSuperPlaceholders($url); $listPath = WBString::replaceSuperPlaceholders('[[SERVICE_HTML]]' . $this->blogTool->getListPath()); if (strncmp($url, $listPath, strlen($listPath)) != 0) { return array(); } $path = explode('/', trim(substr($url, strlen($listPath)), '/')); if (empty($path[0])) { $data = array(); $data['url'] = $url; $data['urlid'] = $urlid; return $this->blogData2Node($data); } try { $this->blogTool->loadArticleByDay($path[0], $path[1]); } catch (WBException $e) { return array(); } $data = $this->blogTool->getArticleData(); $data['url'] = $url; $data['urlid'] = $urlid; return $this->blogData2Node($data); } /** * Get list of child nodes * * @todo use config to format linktext and tooltip * @param string $url * @return array */ public function getChildren($url) { $url = WBString::replaceSuperPlaceholders($url); $listPath = WBString::replaceSuperPlaceholders('[[SERVICE_HTML]]' . $this->blogTool->getListPath()); if ($url != $listPath) { return array(); } $options = array( 'limit' => 10 ); $list = $this->blogTool->getList($options); $children = array(); foreach ($list as $i => $l) { $this->blogTool->loadArticleByData($l); $this->url->addWord($this->blogTool->getArticleUrl()); $data = $this->blogTool->getArticleData(); $data['url'] = $this->blogTool->getArticleUrl(); $data['urlid'] = $this->url->getId(); $children[] = $this->blogData2Node($data);; } return $children; } /** * Get parent nodes until normal tree menu * * List parent nodes until normal (tree manu) parent nodes are reached. * The tree menu class will merge parents together * * @param array $child * @return array */ public function getParents($child) { $parents = array(); $listPath = WBString::replaceSuperPlaceholders('[[SERVICE_HTML]]' . $this->blogTool->getListPath()); $this->url->addWord($listPath); $data = array(); $data['url'] = $this->url->getWord(); $data['urlid'] = $this->url->getId(); $parents[] = $this->blogData2Node($data); if (strlen($child['url']) <= strlen($listPath)) { return $parents; } $parents[] = $child; return $parents; } private function blogData2Node($data = array()) { $node = $this->nodePrototype; $node['id'] = 'blog'; $node['url'] = $data['url']; $node['urlid'] = $data['urlid']; if (!isset($data['id']) || empty($data['id'])) { return $node; } $node['id'] = 'blog-' . $data['id']; $node['linktext'] = $data['title']; if (20 < strlen($data['title'])) { $node['linktext'] = mb_substr($data['title'], 0, 17) . '...'; } $node['tooltip'] = sprintf('%s (%s)', $data['title'], $data['created']); return $node; } }