Archived
1
0
Fork 0
This repository has been archived on 2026-05-10. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
fiktivkod/application/models/ModelBlogPost.php
2011-02-26 14:04:06 +01:00

110 lines
No EOL
2.8 KiB
PHP

<?php
class ModelBlogPost extends Fiktiv_Model_Abstract
{
protected $_schema = 'fiktivkod';
protected $_name = 'Post';
protected $_primary = 'id';
protected $_rowClass = 'Post';
protected $_dependentTables = array('ModelUser', 'ModelPostTag');
public function findByMonth()
{
$posts = $this->select()->from($this->_name, array('Month' => 'DATE_FORMAT(pubDate,"%Y %M")', 'Date' => 'DATE_FORMAT(pubDate,"%Y-%M")'))
->group('Month')
->query()
->fetchAll();
return $posts;
}
public function findByAuthor($user)
{
if ($user instanceof User) {
} else if (is_integer($user)) {
}
}
public function findByTag($tag)
{
// Is this right?
$tags = new ModelTag();
$row = $tags->fetchRow("name = 'Linux'");
$posts = $row->findManyToManyRowset('ModelBlogPost', 'ModelPostTag');
return $posts;
}
public function findByDate($date, $format)
{
// This is a joke, FIXIT!
$date = new Fiktiv_Date($date,$format);
$where = $this->quoteInto('DATE_FORMAT(pubDate,"%Y %M") LIKE ?', $date->toString('Y MMMM'));
return $this->fetchAll($where);
}
public function findByPermlink($permlink)
{
return $this->fetchRow($this->quoteInto('permlink', $permlink));
}
public function createPost($data, $id = null)
{
$myTags = explode(',', $data['tags']);
unset($data['tags']);
$data['userId'] = Zend_Auth::getInstance()->getIdentity()->id;
$data['pubDate'] = Zend_Date::now()->getIso();
$data['isPublished'] = 1;
if ($id) {
$row = $this->find($id)->current();
$row->setFromArray($data);
$row->save();
} else {
$row = $this->find($this->insert($data))->current();
}
if ($myTags) {
$tags = new ModelTag();
$tags->tagsToPost($myTags, $row->id);
}
}
protected function generatePermalink()
{
$permalink = $this->_data['title'];
// All to lowercase
$permalink = strtolower($permalink);
// Remove special characters
$permalink = filter_var($permalink, FILTER_SANITIZE_SPECIAL_CHARS);
// Replace stuff
$replace = array(
' ' => '-',
'å' => 'a',
'ä' => 'a',
'ö' => 'o',
'_' => '-',
'!' => '',
'?' => '',
'.' => '',
',' => '',
);
foreach($replace as $char => $value) {
$permalink = str_replace($char,$value,$permalink);
}
$this->_data['permalink'] = $permalink;
}
}