117 lines
No EOL
2.8 KiB
PHP
117 lines
No EOL
2.8 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)) {
|
|
|
|
if (isset($obj['userId'])) {
|
|
$obj['author'] = Fiktiv_Data_Service::getInstance()->User->findById($obj['userId']);
|
|
unset($obj['userId']);
|
|
}
|
|
|
|
$blogPost = new BlogPost($obj);
|
|
|
|
} else {
|
|
$blogPost = null;
|
|
}
|
|
|
|
return $blogPost;
|
|
}
|
|
|
|
|
|
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 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())
|
|
);
|
|
}
|
|
} |