diff --git a/application/configs/application.ini b/application/configs/application.ini index 6e6c177..3a59829 100644 --- a/application/configs/application.ini +++ b/application/configs/application.ini @@ -12,7 +12,7 @@ app.url = "fiktivkod.org" defaults.lang = sv // Database -db.adapter = mongo +db.adapter = mysqli db.host = 127.0.0.1 db.username = fiktivkod db.password = pw diff --git a/application/datastorage/Posts.php b/application/datastorage/Posts.php deleted file mode 100644 index a79177e..0000000 --- a/application/datastorage/Posts.php +++ /dev/null @@ -1,7 +0,0 @@ - diff --git a/application/datastorage/Users.php b/application/datastorage/Users.php deleted file mode 100644 index 3c6683f..0000000 --- a/application/datastorage/Users.php +++ /dev/null @@ -1,70 +0,0 @@ -getAdapter(), $this->_name, 'email', 'password'); - - // Set credentials - $authAdapter->setIdentity($email); - $authAdapter->setCredential(hash('sha256',$password)); - - // Authenticate - $result = $auth->authenticate($authAdapter); - - // Check result - if ($result->isValid()) { - // Keep all but password and salt in session. - $storage = $auth->getStorage(); - $storage->write($authAdapter->getResultRowObject(null, array('password', 'salt'))); - - return true; - } - - return false; - } - - /** - * Fetch user based on email - * - * @return User - */ - public function findByEmail($email) - { - // Atleast 6 character long - if (is_string($email) && isset($email[5])) { - - return $this->fetchRow($this->getAdapter()->quoteInto('email = ?', $email)); - } - - return null; - } - - /** - * Get random user - * - * @return User - */ - public function findRandom() - { - return $this->fetchAll(null, 'RAND()', 1)->current(); - } -} \ No newline at end of file diff --git a/application/models/BlogPost.php b/application/models/BlogPost.php new file mode 100644 index 0000000..33f513e --- /dev/null +++ b/application/models/BlogPost.php @@ -0,0 +1,86 @@ +_title = $title; + return true; + } + + return false; + } + + + public function setContent($content) + { + if (is_string($content)) { + + $this->_content = $content; + return true; + } + + return false; + } + + + public function setAuthor($author) + { + if ($author instanceof User) { + + $this->_author = $author; + return true; + } + + return false; + } + + + public function setPubDate($date) + { + if (is_string($date) && preg_match('/^(?:(?:19[0-9]{2})|(?:20[0-9]{2}))\-' . + '(?:(?:0[1-9])|(?:1[012]))\-(?:(?:0[1-9])|(?:[12][0-9])|(?:3[01]))$/', $date)) { + + $this->_pubDate = $date; + } else if ($date instanceof Zend_Date) { + + $this->_pubDate = $date->toString('yyyy-MM-dd'); + } else { + + return false; + } + + return true; + } + + + public function isPublished($flag = null) + { + if (null === $flag) { + return $this->_isPublished; + } + + $this->_isPublished = (boolean)$flag; + } + + public function __call($name,$args) { + + // getX methods + if ('get' == substr($name,0,3)) { + $dataPart = '_'.lcfirst(substr($name,3)); + if (isset($this->$dataPart)) + return $this->$dataPart; + } + + } +} \ No newline at end of file diff --git a/application/models/Mapper/BlogPost.php b/application/models/Mapper/BlogPost.php new file mode 100644 index 0000000..dbcaf36 --- /dev/null +++ b/application/models/Mapper/BlogPost.php @@ -0,0 +1,47 @@ +setTitle($obj->title); + $blogPost->setContent($obj->content); + $blogPost->setPubDate($obj->pubDate); + + $mapperUser = new Mapper_User(new Table_User()); + $user = $mapperUser->findById($obj->userId); + $blogPost->setAuthor($user); + + } else { + $blogPost = null; + } + + return $blogPost; + } + + public function findAllPostByAuthor($user) + { + if ($user instanceof User) { + return $this->_createBlogPost($this->_dbTable->findByUser($user->getId())); + } + } + + public function findAll($limit = null) + { + $rows = $this->_dbTable->fetchAll(); + + $posts = array(); + + foreach ($rows as $row) { + $posts[] = $this->_createBlogPost($row); + } + + return $posts; + } + +} \ No newline at end of file diff --git a/application/models/Mapper/User.php b/application/models/Mapper/User.php index 8ea87ca..f8e14f0 100644 --- a/application/models/Mapper/User.php +++ b/application/models/Mapper/User.php @@ -1,35 +1,42 @@ setDbTable($dbtable); + + $user = new User(); + + if ($object instanceof Zend_Db_Table_Row) { + + $user->setEmail($object->email); + $user->setFirstName($object->firstName); + $user->setLastName($object->lastName); + + } else if (is_array($object)) { + + $user->setEmail($object['email']); + $user->setFirstName($object['firstName']); + $user->setLastName($object['lastName']); + + } else { + $user = null; + } + + + + return $user; } - - public function setDbTable($dbtable) + + public function findById($id) { - if (null === $dbtable) { - - $this->_dbTable = new Table_User(); - } else { - - if ($dbtable instanceof Zend_Db_Table) { - $this->_dbTable = $dbtable; - } else if (is_string($dbtable) && class_exists($dbtable)) { - $this->_dbTable = new $dbtable(); - } else { - throw new Fiktiv_Exception('Invalid database table supplied to ' . __CLASS__); - } + if (is_numeric($id)) { + return $this->_createUser($this->_dbTable->find($id)->current()); } } - /** * Fetch user based on email * @@ -40,7 +47,9 @@ class Mapper_User // Atleast 6 character long if (is_string($email) && isset($email[5])) { - return $this->_dbTable->fetchRow($this->getAdapter()->quoteInto('email = ?', $email)); + $user = $this->_createUser($this->_dbTable->fetchRow($this->_dbTable->getAdapter()->quoteInto('email = ?', $email))); + + return $user; } return null; @@ -53,17 +62,7 @@ class Mapper_User */ public function findRandom() { - $row = $this->_dbTable->fetchAll(null, 'RAND()', 1)->current(); - - if ($row) { - $user = new User(); - $user->setFirstName($row->firstName); - $user->setLastName($row->lastName); - $user->setRegDate($row->regDate); - - } - - return null; + return $this->_createUser($this->_dbTable->fetchAll(null, 'RAND()', 1)->current()); } diff --git a/application/models/Table/BlogPost.php b/application/models/Table/BlogPost.php new file mode 100644 index 0000000..1dd5fb7 --- /dev/null +++ b/application/models/Table/BlogPost.php @@ -0,0 +1,9 @@ +isValid($email)) { + $this->_email = $email; + + return true; + } + + return false; + } + + /** * Set user lastname * @@ -37,6 +56,8 @@ class User */ public function setFirstName($name) { + $name = trim($name); + if (is_string($name) && preg_match('/^[A-ö]{2,8}\-?[A-ö]{2,8}$/', $name)) { $this->_firstName = $name; return true; @@ -44,8 +65,14 @@ class User return false; } + - + /** + * Set the user registration date + * + * @param Zend_Date | string $date + * @return boolean + */ public function setRegDate($date) { if (is_string($date) && preg_match('/^(?:(?:19[0-9]{2})|(?:20[0-9]{2}))\-' . @@ -76,14 +103,25 @@ class User /** - * Controll wheter the user is currently active on the - * site. - * TODO: Make sure we have a way to see if the user is logged in. + * Returns the current status of the account. + * * @return boolean */ public function isActive() { - return false; + return !$this->_isDeleted; + } + + + public function __call($name,$args) { + + // getX methods + if ('get' == substr($name,0,3)) { + $dataPart = '_'.lcfirst(substr($name,3)); + if (isset($this->$dataPart)) + return $this->$dataPart; + } + } @@ -93,6 +131,6 @@ class User */ public function __toString() { - return '{' . $this->_firstName . ' ' . $this->_lastName . '}'; + return '(' . __CLASS__ . '){' . $this->_email . ' ' . $this->_firstName . ' ' . $this->_lastName . '}'; } } \ No newline at end of file diff --git a/application/modules/blog/controllers/IndexController.php b/application/modules/blog/controllers/IndexController.php index 23bc73b..aa77765 100644 --- a/application/modules/blog/controllers/IndexController.php +++ b/application/modules/blog/controllers/IndexController.php @@ -5,6 +5,9 @@ class Blog_IndexController extends Fiktiv_Controller_Action public function indexAction() { // TODO: Set latest as default + + $this->view->posts = $this->dbService->BlogPost->findAll(); + } public function latestAction() diff --git a/application/modules/blog/views/scripts/index/index.phtml b/application/modules/blog/views/scripts/index/index.phtml index a7881c7..830cfe0 100644 --- a/application/modules/blog/views/scripts/index/index.phtml +++ b/application/modules/blog/views/scripts/index/index.phtml @@ -1,3 +1,14 @@
=$this->translate('BLOG_TXT') ?>
\ No newline at end of file +=$this->translate('BLOG_TXT') ?>
+ + +posts as $post): ?> + +