From 4c2c435f191bbf0491e4383703803bb7e3ecb35c Mon Sep 17 00:00:00 2001 From: Fredric N Date: Sun, 3 Oct 2010 01:38:06 +0200 Subject: [PATCH] models additions and changes --- application/models/BlogPost.php | 28 +++++++++++- application/models/Mapper/BlogPost.php | 34 ++++++++++---- application/models/User.php | 63 ++++++++++++++------------ library/Fiktiv/Model/Abstract.php | 8 +++- 4 files changed, 92 insertions(+), 41 deletions(-) diff --git a/application/models/BlogPost.php b/application/models/BlogPost.php index 1f2be3d..1f69e3d 100644 --- a/application/models/BlogPost.php +++ b/application/models/BlogPost.php @@ -10,7 +10,8 @@ class BlogPost extends Fiktiv_Model_Abstract 'content' => null, 'pubDate' => null, 'author' => null, - 'isPublished' => false + 'isPublished' => false, + 'tags' => array(), ); public function setAttribs(array $data) { @@ -30,6 +31,9 @@ class BlogPost extends Fiktiv_Model_Abstract case 'pubdate': $this->setPubDate(new Zend_Date($value)); break; + case 'tags': + $this->setTags($value); + break; } } } @@ -73,6 +77,28 @@ class BlogPost extends Fiktiv_Model_Abstract 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 $tag) { + if (!is_string($tag)) + return false; + } + + $this->_data['tags'] = $tags; + return true; + } + public function setPubDate($date) { diff --git a/application/models/Mapper/BlogPost.php b/application/models/Mapper/BlogPost.php index 4d955da..7f8f329 100644 --- a/application/models/Mapper/BlogPost.php +++ b/application/models/Mapper/BlogPost.php @@ -28,15 +28,23 @@ class Mapper_BlogPost extends Fiktiv_Model_Mapper_DbTableAbstract protected function _createBlogPost($obj) { - $blogPost = new BlogPost(); - if ($obj instanceof Zend_Db_Table_Row) { + if ($obj instanceof Zend_Db_Table_Row) + $obj = $obj->toArray(); + - $blogPost->setId($obj->id); - $blogPost->setTitle($obj->title); - $blogPost->setContent($obj->content); - $blogPost->setPubDate(new Fiktiv_Date($obj->pubDate)); - $blogPost->setAuthor($this->_mapperUser->findById($obj->userId)); + 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; @@ -78,5 +86,15 @@ class Mapper_BlogPost extends Fiktiv_Model_Mapper_DbTableAbstract 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()) + ); + } } \ No newline at end of file diff --git a/application/models/User.php b/application/models/User.php index e6583e6..284aadb 100644 --- a/application/models/User.php +++ b/application/models/User.php @@ -5,15 +5,21 @@ */ class User extends Fiktiv_Model_Abstract { + const AVATAR_NONE = 0; + const AVATAR_FIKTIV = 1; + const AVATAR_GRVTAR = 2; + protected $_data = array(); protected $_default = array( - 'id' => 0, - 'email' => null, - 'firstName' => null, - 'lastName' => null, - 'regDate' => null, - 'isDeleted' => false + 'id' => 0, + 'email' => null, + 'firstName' => null, + 'lastName' => null, + 'regDate' => null, + 'isDeleted' => false, + 'avatar' => self::AVATAR_NONE, + 'avatarImage' => null, ); public function setId($id) @@ -97,6 +103,27 @@ class User extends Fiktiv_Model_Abstract return true; } + public function setAvatar($type) + { + + if (self::AVATAR_NONE === $type) { + + $this->_data['avatar'] = self::AVATAR_NONE; + $this->_data['avatarImage'] = null; + + } else if (self::AVATAR_GRVTAR === $type) { + + $this->_data['avatar'] = self::AVATAR_GRVTAR; + $this->_data['avatarImage'] = null; + + } else if (is_string($type)) { + + $this->_data['avatar'] = self::AVATAR_FIKTIV; + $this->_data['avatarImage'] = $type; + } + + } + public function setPassword($password) { if (!is_string($password)) @@ -142,28 +169,4 @@ class User extends Fiktiv_Model_Abstract return $this->_data; } - /** - * Load user data from database (restore last saved) - */ - public function reload() - { - $userData = Fiktiv_Data_Service::getInstance()->User->findById($this->getId()); - - if ($userData instanceof User) { - - $this->setAttribs($userData->toArray()); - - return true; - } - - return false; - } - - /** - * Save User - */ - public function save() - { - return Fiktiv_Data_Service::getInstance()->User->save($this); - } } \ No newline at end of file diff --git a/library/Fiktiv/Model/Abstract.php b/library/Fiktiv/Model/Abstract.php index 09e4299..eb8ecdc 100644 --- a/library/Fiktiv/Model/Abstract.php +++ b/library/Fiktiv/Model/Abstract.php @@ -2,7 +2,6 @@ abstract class Fiktiv_Model_Abstract { - protected $_default = array(); public function __construct($data = array()) @@ -55,5 +54,10 @@ abstract class Fiktiv_Model_Abstract $this->$key = $value; } } - + + public function save() + { + $class = get_class($this); + return Fiktiv_Data_Service::getInstance()->$class->save($this); + } } \ No newline at end of file