PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/controllers/posts_controller.php

https://github.com/felixding/LonelyThinker
PHP | 194 lines | 95 code | 31 blank | 68 comment | 12 complexity | 16fe688d7054a5eec200b52e2872ffe2 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /* SVN FILE: $Id: posts_controller.php 24 2009-05-08 13:23:53Z $ */
  3. /**
  4. * Short description for file.
  5. *
  6. * Long description for file
  7. *
  8. * PHP versions 5
  9. *
  10. * Licensed under The BSD License
  11. * Redistributions of files must retain the above copyright notice.
  12. *
  13. * @filesource
  14. * @copyright Copyright 2007-2009, Felix Ding (http://dingyu.me)
  15. * @link http://lonelythinker.org Project LonelyThinker
  16. * @package LonelyThinker
  17. * @author $LastChangedBy: $
  18. * @version $Revision: 24 $
  19. * @modifiedby $LastChangedBy: $
  20. * @lastmodified $Date: 2009-05-08 21:23:53 +0800 (Fri, 08 May 2009) $
  21. * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
  22. */
  23. class PostsController extends AppController
  24. {
  25. var $name = 'Posts';
  26. var $scaffold;
  27. var $components = array('Cookie', 'Session');
  28. var $helpers = array('Gravatar', 'Rss', 'Session', 'Geshi');
  29. var $othAuthRestrictions = array('add','edit','delete');
  30. var $paginate = array('limit' => 5, 'page' => 1, 'order'=>array('Post.created' => 'desc'));
  31. /**
  32. * blog homepage
  33. */
  34. public function index()
  35. {
  36. //get latest posts
  37. $posts = $this->readCache('posts');
  38. $this->params['paging'] = $this->readCache('paging');
  39. if(!$posts || !$this->params['paging'])
  40. {
  41. $posts = $this->paginate('Post', array('Post.status'=>'published'));
  42. //caching
  43. $this->writeCache('posts', $posts);
  44. $this->writeCache('paging', $this->params['paging']);
  45. }
  46. $this->set('posts', $posts);
  47. }
  48. /**
  49. * rss feed
  50. */
  51. public function feed()
  52. {
  53. //$channel = array ('title' => Configure::read('LT.siteName'), 'link' => Configure::read('LT.siteUrl'), 'description' => Configure::read('LT.siteSlogan'));
  54. //$this->set('channel', $channel);
  55. $posts = $this->Post->findAll(array('Post.status'=>'published'), '*', 'ORDER BY Post.created DESC', 30);
  56. $this->set('posts', $posts);
  57. $this->render('rss/feed');
  58. }
  59. /**
  60. * add a post
  61. */
  62. public function add()
  63. {
  64. if(!empty($this->data))
  65. {
  66. if($this->Post->save($this->data))
  67. {
  68. //if($this->data['Post']['status'] == 'published') $this->redirect('/posts/view/'.Inflector::slug($this->data['Post']['slug'], '-'));
  69. //else $this->set('saved', true);
  70. $this->redirect('/posts/view/'.$this->data['Post']['slug']);
  71. }
  72. else
  73. {
  74. $this->set('post', $this->data);
  75. }
  76. if($this->RequestHandler->isAjax()) $this->renderView('saved');
  77. }
  78. //$this->set('drafts', $this->getDrafts());
  79. //$this->set('statuses', $this->getEnumFields('status'));
  80. $this->set('comment', $this->getEnumFields('comment'));
  81. //extract the tags
  82. $tags = Set::extract('{n}.Tag', $this->requestAction('/tags/get'));
  83. $tagsForTheView = array();
  84. foreach($tags as $tag) $tagsForTheView[$tag['id']] = $tag['title'];
  85. $this->set('tags', $tagsForTheView);
  86. }
  87. /**
  88. * edit a post
  89. */
  90. public function edit($id = null)
  91. {
  92. //does the post exist?
  93. if(!empty($this->data)) $this->Post->id = $this->data['Post']['id'];
  94. else $this->Post->id = $id;
  95. if(!$post = $this->Post->read()) {
  96. $this->cakeError('error404');
  97. return;
  98. }
  99. if(!empty($this->data))
  100. {
  101. $post = $this->data;
  102. $post['Tag'] = $post['Tag']['Tag'];
  103. /**/
  104. if($this->Post->save($this->data))
  105. {
  106. $this->redirect('/posts/view/'.Inflector::slug($this->data['Post']['slug'], '-'));
  107. return;
  108. }
  109. }
  110. //replace '-' with a white space
  111. //$post['Post']['slug'] = isset($post['Post']['slug']) ? str_replace('-', ' ', $post['Post']['slug']) : '';
  112. $this->set('post', $post);
  113. $this->set('comment', $this->getEnumFields('comment'));
  114. //extract the tags, tried with Set library but failed
  115. $tags = Set::extract('{n}.Tag', $this->requestAction('/tags/get'));
  116. $tagsForTheView = array();
  117. foreach($tags as $tag) $tagsForTheView[$tag['id']] = $tag['title'];
  118. $this->set('tags', $tagsForTheView);
  119. $this->render('add');
  120. }
  121. /**
  122. * render the view page
  123. */
  124. public function view($slug = null)
  125. {
  126. //set post
  127. if(($post = $this->readCache('post')) === false)
  128. {
  129. if(!$post = $this->Post->findBySlug($slug))
  130. {
  131. $this->cakeError('error404');
  132. return false;
  133. }
  134. $this->writeCache('post', $post);
  135. }
  136. $this->set('post' ,$post);
  137. //get comments data
  138. $comments = $this->requestAction('/comments/getCommentsByPostId/' . $post['Post']['id']);
  139. $this->set('comments', $comments);
  140. //set a time point
  141. $this->setTimePoint($post['Post']['id']);
  142. }
  143. /**
  144. * get cookies about pass them to the view for CommentAddForm
  145. */
  146. public function getCookies()
  147. {
  148. return array($this->Cookie->read('commentName'), $this->Cookie->read('commentEmail'), $this->Cookie->read('commentWebsite'));
  149. }
  150. /**
  151. * render the view page
  152. */
  153. /*
  154. private function getDrafts()
  155. {
  156. $this->Post->recursive = -1;
  157. $drafts = $this->Post->findAll(array('Post.status'=>'draft'), '*', 'ORDER BY Post.created DESC');
  158. return $drafts;
  159. }
  160. */
  161. }
  162. ?>