From 17c5c48950968632eb6cb12da3867a796f2a19d8 Mon Sep 17 00:00:00 2001 From: Fredric N Date: Wed, 20 Oct 2010 20:33:23 +0200 Subject: [PATCH] Ongoing work on the blog module --- application/Bootstrap.php | 3 +- application/models/BlogPost.php | 28 ++++++++++ application/models/Mapper/BlogPost.php | 54 ++++++++++--------- application/models/Table/BlogPost.php | 2 +- .../blog/controllers/ArchiveController.php | 19 ++++++- .../blog/controllers/IndexController.php | 2 +- .../blog/views/scripts/archive/filter.phtml | 0 .../blog/views/scripts/archive/index.phtml | 9 ++-- .../blog/views/scripts/index/latest.phtml | 2 +- .../default/views/layout/default.phtml | 1 - library/Fiktiv/Model/Abstract.php | 11 ++++ .../Fiktiv/Model/Mapper/DbTableAbstract.php | 4 ++ public/js/menu.js | 3 -- 13 files changed, 98 insertions(+), 40 deletions(-) create mode 100644 application/modules/blog/views/scripts/archive/filter.phtml diff --git a/application/Bootstrap.php b/application/Bootstrap.php index 7e289c0..1f1c3c1 100644 --- a/application/Bootstrap.php +++ b/application/Bootstrap.php @@ -143,11 +143,10 @@ class Bootstrap extends Zend_Application_Bootstrap_Bootstrap // Routes for blog module - $router->addRoute('blog',new Zend_Controller_Router_Route(':lang/blog/:controller/:action/:id',array( + $router->addRoute('blog',new Zend_Controller_Router_Route(':lang/blog/:controller/:action/*',array( 'module' => 'blog', 'controller' => 'index', 'action' => 'index', - 'id' => '' ))); $router->addRoute('blog-default',new Zend_Controller_Router_Route(':lang/blog/:action/:id',array( 'module' => 'blog', diff --git a/application/models/BlogPost.php b/application/models/BlogPost.php index 1f69e3d..0aed522 100644 --- a/application/models/BlogPost.php +++ b/application/models/BlogPost.php @@ -10,6 +10,7 @@ class BlogPost extends Fiktiv_Model_Abstract 'content' => null, 'pubDate' => null, 'author' => null, + 'permalink' => null, 'isPublished' => false, 'tags' => array(), ); @@ -34,6 +35,9 @@ class BlogPost extends Fiktiv_Model_Abstract case 'tags': $this->setTags($value); break; + case 'permalink': + $this->setPermalink($value); + break; } } } @@ -126,4 +130,28 @@ class BlogPost extends Fiktiv_Model_Abstract $this->_data['isPublished'] = (boolean)$flag; } + + 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; + } } \ No newline at end of file diff --git a/application/models/Mapper/BlogPost.php b/application/models/Mapper/BlogPost.php index 7f8f329..feb9a51 100644 --- a/application/models/Mapper/BlogPost.php +++ b/application/models/Mapper/BlogPost.php @@ -5,27 +5,21 @@ 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 _createBlogPosts($obj) + { + $posts = array(); + foreach($obj as $post) + $posts[] = $this->_createBlogPost($post); + + return $posts; + } + protected function _createBlogPost($obj) { @@ -70,21 +64,31 @@ class Mapper_BlogPost extends Fiktiv_Model_Mapper_DbTableAbstract } } + public function findByMonth($month = null) + { + return $this->_dbTable->select() + ->from($this->_dbTable->_name,array('Month' => 'DATE_FORMAT(pubDate,"%Y %M")', 'Date' => 'DATE_FORMAT(pubDate,"%Y-%M")')) + ->group('Month') + ->query() + ->fetchAll(); + } + + public function findByDate($date) + { + $date = new Fiktiv_Date($date); + + $where = $this->quoteInto('pubDate LIKE ?', $date->getIso()); + + return $this->_createBlogPosts($this->_dbTable->fetchAll($where)); + } + 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; + return $this->_createBlogPosts($this->_dbTable->fetchAll(null, 'pubDate DESC', $limit)); } public function save(BlogPost $post) @@ -94,7 +98,7 @@ class Mapper_BlogPost extends Fiktiv_Model_Mapper_DbTableAbstract return $this->_dbTable->update( $data, - $this->_dbTable->getAdapter()->quoteInto('id = ?',$post->getId()) + $this->quoteInto('id = ?',$post->getId()) ); } } \ No newline at end of file diff --git a/application/models/Table/BlogPost.php b/application/models/Table/BlogPost.php index 1dd5fb7..dee6637 100644 --- a/application/models/Table/BlogPost.php +++ b/application/models/Table/BlogPost.php @@ -1,6 +1,6 @@ view->postsByMonthYear = $this->dataService->BlogPost->findByMonth(); + } + + public function filterAction() + { + + $params = $this->_getAllParams(); + + if (array_key_exists('date', $params)) { + + $this->view->posts = $this->dataService->BlogPost->findByDate($params['date']); + + } else if (array_key_exists('tag', $params)) { + + $this->view->posts = $this->dataService->BlogPost->findByTag($params['tag']); + + } + } } \ No newline at end of file diff --git a/application/modules/blog/controllers/IndexController.php b/application/modules/blog/controllers/IndexController.php index 96b09f0..4fd063f 100644 --- a/application/modules/blog/controllers/IndexController.php +++ b/application/modules/blog/controllers/IndexController.php @@ -20,7 +20,7 @@ class Blog_IndexController extends Fiktiv_Controller_Action public function readAction() { - $this->view->post = $this->dataService->BlogPost->findById($this->_getParam('id', null)); + $this->view->post = $this->dataService->BlogPost->findByPermalink($this->_getParam('id', null)); if (!$this->view->post) $this->_redirect(array('action' => 'latest')); diff --git a/application/modules/blog/views/scripts/archive/filter.phtml b/application/modules/blog/views/scripts/archive/filter.phtml new file mode 100644 index 0000000..e69de29 diff --git a/application/modules/blog/views/scripts/archive/index.phtml b/application/modules/blog/views/scripts/archive/index.phtml index 7722c88..6713739 100644 --- a/application/modules/blog/views/scripts/archive/index.phtml +++ b/application/modules/blog/views/scripts/archive/index.phtml @@ -1,10 +1,9 @@

translate('u:archive') ?>

-

translate('BLOG_ARCHIVE_INTRO') ?>

+

translate('u:BLOG_ARCHIVE_INTRO') ?>

\ No newline at end of file diff --git a/application/modules/blog/views/scripts/index/latest.phtml b/application/modules/blog/views/scripts/index/latest.phtml index f3b926a..94b24ad 100644 --- a/application/modules/blog/views/scripts/index/latest.phtml +++ b/application/modules/blog/views/scripts/index/latest.phtml @@ -11,7 +11,7 @@ for ($i = 0; $i < $postCount; $i++): ?>
-

getTitle() ?>

+

getTitle() ?>

getPubDate()->toString('yyyy-MM-dd').' at '.$post->getPubDate()->toString('HH:mm').' '.$post->getAuthor()->getFirstName() ?>
getContent() ?>
getTags()) ?>
diff --git a/application/modules/default/views/layout/default.phtml b/application/modules/default/views/layout/default.phtml index 302f820..bdf3c50 100644 --- a/application/modules/default/views/layout/default.phtml +++ b/application/modules/default/views/layout/default.phtml @@ -8,7 +8,6 @@ headScript() ?> - fancybox()->headScript() ?> diff --git a/library/Fiktiv/Model/Abstract.php b/library/Fiktiv/Model/Abstract.php index 0df2f0d..3e03347 100644 --- a/library/Fiktiv/Model/Abstract.php +++ b/library/Fiktiv/Model/Abstract.php @@ -20,10 +20,21 @@ abstract class Fiktiv_Model_Abstract if ('get' == substr($name,0,3)) { $property = substr($name, 3); + // Wrong! use mb_ $property[0] = strtolower($property[0]); if (isset($this->_data[$property])) return $this->_data[$property]; + + + } else if ('set' == substr($name,0,3)) { + $property = substr($name, 3); + + // Wrong! use mb_ + $property[0] = strtolower($property[0]); + + if (in_array($property,$this->_default)) + $this->_data[$property] = $args[0]; } } diff --git a/library/Fiktiv/Model/Mapper/DbTableAbstract.php b/library/Fiktiv/Model/Mapper/DbTableAbstract.php index f4abd00..755f8db 100644 --- a/library/Fiktiv/Model/Mapper/DbTableAbstract.php +++ b/library/Fiktiv/Model/Mapper/DbTableAbstract.php @@ -27,4 +27,8 @@ abstract class Fiktiv_Model_Mapper_DbTableAbstract return $this->_dbTable->update($data, $where); } + public function quoteInto($text, $value) + { + return $this->_dbTable->getAdapter()->quoteInto($text,$value); + } } \ No newline at end of file diff --git a/public/js/menu.js b/public/js/menu.js index 2f89cd3..e69de29 100644 --- a/public/js/menu.js +++ b/public/js/menu.js @@ -1,3 +0,0 @@ -$(document).ready(function(){ - $(".subnav").hide().slideDown(500); -}); \ No newline at end of file