100 lines
No EOL
2.2 KiB
PHP
100 lines
No EOL
2.2 KiB
PHP
<?php
|
|
|
|
class Mapper_BlogPost extends Fiktiv_Model_Mapper_DbTableAbstract
|
|
{
|
|
|
|
protected $_mapperUser = null;
|
|
|
|
public static function generatePermalink(BlogPost $post)
|
|
{
|
|
$chars = array(
|
|
' ' => '-',
|
|
);
|
|
|
|
$permalink = $post->getTitle();
|
|
|
|
foreach ($chars as $ch => $rep) {
|
|
$permalink = str_replace($ch, $rep, $permalink);
|
|
}
|
|
|
|
return strtolower($permalink);
|
|
}
|
|
|
|
public function __construct($dbtable = null) {
|
|
parent::__construct($dbtable);
|
|
|
|
$this->_mapperUser = new Mapper_User(new Table_User());
|
|
}
|
|
|
|
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 findAllPostByAuthor($user)
|
|
{
|
|
if ($user instanceof User) {
|
|
return $this->_createBlogPost($this->_dbTable->findByUser($user->getId()));
|
|
}
|
|
}
|
|
|
|
public function findAll($limit = null)
|
|
{
|
|
|
|
if (!is_numeric($limit))
|
|
throw new Fiktiv_Exception('limit must be numeric');
|
|
|
|
$rows = $this->_dbTable->fetchAll(null, 'pubDate DESC', $limit);
|
|
|
|
$posts = array();
|
|
|
|
foreach ($rows as $row) {
|
|
$posts[] = $this->_createBlogPost($row);
|
|
}
|
|
|
|
return $posts;
|
|
}
|
|
|
|
public function save(BlogPost $post)
|
|
{
|
|
$data = $post->toArray();
|
|
unset($data['id']);
|
|
|
|
return $this->_dbTable->update(
|
|
$data,
|
|
$this->_dbTable->getAdapter()->quoteInto('id = ?',$post->getId())
|
|
);
|
|
}
|
|
} |