139 lines
No EOL
3.5 KiB
PHP
139 lines
No EOL
3.5 KiB
PHP
<?php
|
|
|
|
class Mapper_BlogPost extends Fiktiv_Model_Mapper_DbTableAbstract
|
|
{
|
|
|
|
protected $_mapperUser = null;
|
|
|
|
public function __construct($dbtable = null) {
|
|
parent::__construct($dbtable);
|
|
|
|
$this->_mapperUser = new Mapper_User(new Table_User());
|
|
}
|
|
|
|
protected function _createBlogPosts($obj)
|
|
{
|
|
$posts = array();
|
|
foreach($obj as $post)
|
|
$posts[] = $this->_createBlogPost($post);
|
|
|
|
return $posts;
|
|
}
|
|
|
|
protected function _createBlogPost($obj)
|
|
{
|
|
|
|
if ($obj instanceof Zend_Db_Table_Row)
|
|
$obj = $obj->toArray();
|
|
|
|
|
|
if ($obj instanceof stdClass)
|
|
$obj = (array) $obj;
|
|
|
|
|
|
if (is_array($obj)) {
|
|
|
|
// Get Author
|
|
if (isset($obj['userId'])) {
|
|
$obj['author'] = Fiktiv_Data_Service::getInstance()->User->findById($obj['userId']);
|
|
unset($obj['userId']);
|
|
}
|
|
|
|
// Get tags
|
|
$obj['tags'] = $this->getTags($obj['id']);
|
|
|
|
$blogPost = new BlogPost($obj);
|
|
|
|
} else {
|
|
$blogPost = null;
|
|
}
|
|
|
|
return $blogPost;
|
|
}
|
|
|
|
public function getTags($blogPostId)
|
|
{
|
|
return Fiktiv_Data_Service::getInstance()->Tag->findByBlogPost($blogPostId);
|
|
}
|
|
|
|
public function findById($id)
|
|
{
|
|
if (is_numeric($id)) {
|
|
return $this->_createBlogPost($this->_dbTable->find($id)->current());
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public function findByPermalink($permalink)
|
|
{
|
|
if (is_string($permalink)) {
|
|
return $this->_createBlogPost($this->_dbTable->fetchRow($this->quoteInto('permalink LIKE ?', $permalink)));
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public function findAllPostByAuthor($user)
|
|
{
|
|
if ($user instanceof User) {
|
|
return $this->_createBlogPost($this->_dbTable->findByUser($user->getId()));
|
|
}
|
|
}
|
|
|
|
public function findByTag($tag)
|
|
{
|
|
$rows = $this->_dbTable->getAdapter()
|
|
->select()->from('Post')
|
|
->join('Post_has_Tag', 'Post_has_Tag.postId = Post.id')
|
|
->join('Tag', 'Tag.id = Post_has_Tag.tagId')
|
|
->where('Tag.name = ?', $tag)
|
|
->query()->fetchAll();
|
|
|
|
return $this->_createBlogPosts($rows);
|
|
}
|
|
|
|
public function findByMonth($month = null)
|
|
{
|
|
return $this->_dbTable->select()
|
|
->from($this->_dbTable->_name,array('Month' => 'DATE_FORMAT(pubDate,"%Y %M")', 'Date' => 'DATE_FORMAT(pubDate,"%Y-%M")'))
|
|
->group('Month')
|
|
->query()
|
|
->fetchAll();
|
|
}
|
|
|
|
public function findByDate($date)
|
|
{
|
|
$date = new Fiktiv_Date($date);
|
|
|
|
$where = $this->quoteInto('pubDate LIKE ?', $date->getIso());
|
|
|
|
return $this->_createBlogPosts($this->_dbTable->fetchAll($where));
|
|
}
|
|
|
|
public function findAll($limit = null)
|
|
{
|
|
|
|
if (!is_numeric($limit))
|
|
throw new Fiktiv_Exception('limit must be numeric');
|
|
|
|
return $this->_createBlogPosts($this->_dbTable->fetchAll(null, 'pubDate DESC', $limit));
|
|
}
|
|
|
|
public function save(BlogPost $post)
|
|
{
|
|
$data = $post->toArray();
|
|
|
|
unset($data['id']);
|
|
|
|
$tags = $data['tags'];
|
|
unset($data['tags']);
|
|
|
|
|
|
|
|
return $this->_dbTable->update(
|
|
$data,
|
|
$this->quoteInto('id = ?',$post->getId())
|
|
);
|
|
}
|
|
} |