* @license PHP License * @package WB * @subpackage base */ WBClass::load('WBBlog'); /** * Blog tools * * @version 0.3.0 * @package WB * @subpackage base */ class WBBlog_Tool extends WBBlog { /** * current blog article * * @var array */ private $data = array(); /** * constructor */ public function __construct() { parent::__construct(); $this->req = WBClass::create('WBRequest'); } /** * Load article by raw data * * @param array $data * @return WBBlog_Tool */ public function loadArticleByData($data) { if (!is_array($data)) { $data = array(); } if (!isset($data[$this->bKey]) && isset($data['id'])) { $data[$this->bKey] = $data['id']; } if (!isset($data[$this->bKey])) { WBClass::load('WBException_Argument'); throw new WBException_Argument('Insufficient data to load article', 1, __CLASS__); } $this->data = $data; return $this; } /** * Load single article by id * * @param string $id * @param string $foreign * @param array $clause * @return WBBlog_Tool */ public function loadArticleById($id, $foreign = null, $clause = array()) { $list = $this->table->get(WBBlog::TABLE_BLOG, $id, $foreign, $clause); if (1 != count($list)) { WBClass::load('WBException_Argument'); throw new WBException_Argument('Blog article not found', 2, __CLASS__); } return $this->loadArticleByData($list[0]); } /** * Load article by day and title * * @see getArticleByDay() * @param string $day * @param string $title * @return WBBlog_Tool */ public function loadArticleByDay($day, $title = '') { $article = $this->getArticleByDay($day, $title); if (empty($article)) { WBClass::load('WBException_Argument'); throw new WBException_Argument('Blog article not found by day', 3, __CLASS__); } return $this->loadArticleByData($article); } /** * Get URL path of blog roll * * @return string */ public function getListPath() { return $this->config->get('blog/listpath', 'blog'); } /** * Get URL of blog roll * * @return string */ public function getListUrl() { return '[[PROTOCOL]]://[[SERVER]][[SERVICE_HTML]]' . $this->getListPath(); } /** * Get URL path of current article * * @return string */ public function getArticlePath() { if (empty($this->data)) { WBClass::load('WBException_Call'); throw new WBException_Call('There is no data, load article first.', 3, __CLASS__); } $urlFmt = $this->config->get('blog/articlepath', 'blog/%s/%s'); list($day) = explode(' ', $this->data['created'], 2); return sprintf($urlFmt, $day, urlencode($this->data['title'])); } /** * Get URL of current article * * @return string */ public function getArticleUrl() { return '[[PROTOCOL]]://[[SERVER]][[SERVICE_HTML]]' . $this->getArticlePath(); } /** * Get associative data array * * @return array */ public function getArticleData() { if (empty($this->data)) { WBClass::load('WBException_Call'); throw new WBException_Call('There is no data, load article first.', 3, __CLASS__); } return $this->data; } }