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/BlogPost.php
2010-10-30 15:11:34 +02:00

175 lines
No EOL
3.9 KiB
PHP

<?php
class BlogPost extends Fiktiv_Model_Abstract
{
protected $_data = array();
protected $_default = array(
'id' => 0,
'title' => null,
'content' => null,
'pubDate' => null,
'author' => null,
'permalink' => null,
'isPublished' => false,
'tags' => array(),
);
public function setAttribs(array $data) {
foreach ($data as $key => $value) {
switch(strtolower($key)) {
case 'title':
$this->setTitle($value);
break;
case 'content':
$this->setContent($value);
break;
case 'author':
$this->setAuthor($value);
break;
case 'pubdate':
$this->setPubDate(new Zend_Date($value));
break;
case 'tags':
$this->setTags($value);
break;
case 'permalink':
$this->setPermalink($value);
break;
}
}
}
public function setId($id)
{
if (is_numeric($id))
$this->_data['id'] = $id;
}
public function setTitle($title)
{
if (is_string($title) && !isset($title[30])) {
$this->_data['title'] = $title;
$this->generatePermalink();
return true;
}
return false;
}
public function setContent($content)
{
if (is_string($content)) {
$this->_data['content'] = $content;
return true;
}
return false;
}
public function setAuthor($author)
{
if ($author instanceof User) {
$this->_data['author'] = $author;
return true;
}
return false;
}
public function addTag($tag)
{
if (is_string($tag)) {
$this->_data['tags'][] = $tag;
return true;
}
return false;
}
public function setTags(array $tags)
{
// make sure the all elements are strings
foreach ($tags as $key => $tag) {
if (!is_string($tag)) {
return false;
}
}
$this->_data['tags'] = $tags;
return true;
}
public function setPubDate($date)
{
if (Fiktiv_Date::isDate($date)) {
$this->_data['pubDate'] = new Fiktiv_Date($date);
} else if ($date instanceof Zend_Date) {
$this->_data['pubDate'] = $date;
} else {
return false;
}
return true;
}
public function isPublished($flag = null)
{
if (null === $flag) {
return $this->_data['isPublished'];
}
$this->_data['isPublished'] = (boolean)$flag;
}
public function setPermalink($permalink)
{
if (is_string($permalink)) {
$this->_data['permalink'] = $permalink;
return true;
}
return false;
}
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;
}
}