* @license PHP License * @package WB * @subpackage base */ WBClass::load('WBBlog'); /** * Blog tools * * @version 0.4.1 * @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 $this->config->get('blog/listurl', '[[PROTOCOL]]://[[SERVER]][[SELF]]blog'); } /** * 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/articleurl', '[[PROTOCOL]]://[[SERVER]][[SELF]]blog/%s/%s/'); list($day) = explode(' ', $this->data['created'], 2); // remove slashes to avoid server errors $title = substr($this->data['title'], 0, 50); if (strstr($title, '/')) { $title = trim(substr($title, 0, strpos($title, '/'))); } $title = trim($title); return sprintf($urlFmt, $day, urlencode($title)); } /** * Get URL of current article * * @return string */ public function getArticleUrl() { return $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; } /** * Get Open Graph Information * * @todo just a fragment, so far * @param array target * @param string type * @return string */ public function addOpenGraph(&$target, $type = 'image') { if (empty($this->data)) { return; } $src = $this->config->get('blog/og/' . $type); if (empty($src)) { return; } switch ($type) { case 'image': return $this->getOpenGraphImage($target); break; case 'created': $type = 'article_created'; break; // simple mapping default: break; } $lang = patI18n::getLocale(patI18n::LOCALE_TYPE_SHORT); if (!empty($this->data[$src . '_' . $lang])) { $target[$type] = $this->data[$src . '_' . $lang]; return; } if (!empty($this->data[$src])) { $target[$type] = $this->data[$src]; return; } } /** * Get Open Graph Image * * @param string type * @return string */ public function getOpenGraphImage(&$target) { if (empty($this->data['vfsfid'])) { return; } $size = $this->config->get('blog/og/image/size', ''); if (empty($size)) { $size = $this->config->get('blog', ''); return; } /** @var WBVFS_File */ $file = WBClass::create('WBVFS_File'); $file = $file->loadById($this->data['vfsfid']); if (!$file->isOK()) { return; } $target['og_image'] = $file->getUrl('image') . '?size=' . $size; } }