* @license PHP License * @package WB * @subpackage base */ /** * Blog stuff * * @version 1.0.0 * @package WB * @subpackage base */ class WBBlog extends WBStdClass { /** * config loader * @var WBConfig */ protected $config; /** * table access * @var WBDatasource_Table */ protected $table; /** * named tables */ const TABLE_BLOG = 'blog'; const TABLE_BLOGCATEGORY = 'blogcategory'; const TABLE_USER = 'user'; /** * primary key of blog table * @var string */ protected $bKey = ''; /** * primary key of blog category table * @var string */ protected $cKey = ''; /** * primary key of user table * @var string */ protected $uKey = ''; /** * constructor */ public function __construct() { $this->table = WBClass::create('WBDatasource_Table'); $this->bKey = $this->table->getIdentifier(self::TABLE_BLOG); $this->cKey = $this->table->getIdentifier(self::TABLE_BLOGCATEGORY); $this->uKey = $this->table->getIdentifier(self::TABLE_USER); $this->config = WBClass::create('WBConfig'); $this->config->load('blog/config'); } /** * Get list of articles * * Fetch data array from database. the options array corresponds to those * used in WBDatasource_Table * * @see WBDatasource_Table::get() * @see getListClause() * @param array $options * @param string $owner * @param bool $manager * @return array */ public function getList($options = array(), $owner = null, $manager = false) { $clause = $this->getListClause($owner, $manager); return $this->table->get('blog', null, null, $clause, $options); } /** * Get list pager * * Convenience function to get blog pager * * @see WBDatasource_Table::getPager() * @see getListClause() * @param string $pagerNS prefix to identify pager * @param array $options * @param string $owner * @param bool $manager * @return array */ public function getListPager($pagerNS = '', $options = array(), $owner = null, $manager = false) { $clause = $this->getListClause($owner, $manager); $pager = $this->table->getPager(__CLASS__ . $pagerNS, 'blog', null, $clause, $options); return $pager; } /** * Build generic clause to fetch article list * * Optionally set user id to identify article's owner and manager flag * to tell that user has permissions to maintain any article. * Build clause and respect published flag as well as future items for scheduled * publishing. * * @param string $owner * @param bool $manager * @return array */ public function getListClause($owner = null, $manager = false) { $clause = array(); // match category $categories = $this->config->get('blog/blogroll/category', array()); if (!empty($categories)) { if (!is_array($categories)) { $categories = array($categories); } $clause[] = array( 'field' => $this->cKey, 'relation' => 'IN', 'value' => $categories ); } if ($manager) { return $clause; } // only published or user owned records $c = array(); $c[0] = array(); if ($owner) { $c[1] = array( 'field' => $this->uKey, 'value' => $owner ); } // take care for drafts $c[0] = array( 'field' => 'published', 'value' => 1 ); $clause[] = array( 'type' => 'complex', 'bond' => 'or', 'clause' => $c ); // consider future records $c[0] = array( 'field' => 'created', 'relation' => 'le', 'value' => gmdate('Y-m-d H:i:s') ); $clause[] = array( 'type' => 'complex', 'bond' => 'or', 'clause' => $c ); return $clause; } /** * Get article by day and title * * In order to provider nice URLs for each article one can use: * - http://example.com/blog/2012-01-12/Title+of+blog+article * - http://example.com/blog/2317/Title+of+blog+article * These URLs contain either the articles' published date or its id as well * as the article's title. This makes pretty human readable URLs and allows * to select the corresponding article from database. * * BTW, as the id is quite hard to remember, it is recommended to use the * "date-form". * * When using the date (day) to select the article, the result may not be unique. * (e.g. there may be more than one article on the particular day). In that case, * it will use the given title to select the proper article. If title is empty, * the first article will be returned. * * @param string $day either a day string in format "YYYY-MM-DD" or the id * @param string $title article's title to verify article, if neccessary * @param bool $owner - rarely used * @param bool $manager - rarely used * @return array article data as associative array or null if not found */ public function getArticleByDay($day, $title = '', $owner = null, $manager = false) { $clause = $this->getListClause($owner, $manager); $options = array( // use large limit to make sure to find article, even if there are multiple article on that day 'limit' => 100 ); if (strstr($day, '-')) { // load by date and check title if date has more than one record $created = strtotime($day); $lClause = $clause; $lClause[] = array( 'field' => 'created', 'relation' => 'ge', 'value' => date('Y-m-d', $created) ); // plus one day 60 * 60 * 24 $created += 86400; $lClause[] = array( 'field' => 'created', 'relation' => 'le', 'value' => date('Y-m-d', $created) ); $list = $this->table->get('blog', null, null, $lClause, $options); } else { $list = $this->table->get('blog', $day, null, $clause, $options); } // not found if (0 == count($list)) { return null; } // there is just one record for this day if (1 == count($list)) { return $list[0]; } // fuzzy select: empty title matches the first record on a day if (empty($title)) { return $list[0]; } $title = strtolower($title); // fuzzy select: try to match title foreach ($list as $l) { if (strtolower($l['title']) == $title) { return $l; } } // fuzzy select: try to match the beginning foreach ($list as $l) { $length = min(strlen($title), strlen($l['title'])); if (0 == strncmp(strtolower($l['title']), $title, $length)) { return $l; } } return null; } }