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

/controllers/posts_controller.php

https://github.com/jcalado/planetuga
PHP | 327 lines | 217 code | 97 blank | 13 comment | 37 complexity | fa71c9a18cd57fbf0f0cc89f0e565028 MD5 | raw file
  1. <?php
  2. class PostsController extends AppController {
  3. var $name = 'Posts';
  4. var $paginate = array('limit' => 10, 'page' => 1, 'order' => 'Post.created_at DESC');
  5. var $belongsTo = 'Feed';
  6. var $helpers = array('Text','Time','Bookmark');
  7. var $components = array('Twitter','Tinyurl');
  8. function beforeFilter() {
  9. parent::beforeFilter();
  10. $this->Auth->allowedActions = array('index', 'view','update','top','latest','recent','featured','category','feed','search','widget');
  11. }
  12. function index() {
  13. $this->set('posts', $this->paginate('Post'));
  14. }
  15. function recent() {
  16. $posts = $this->Post->find('all',array(
  17. 'order' => 'Post.created_at DESC',
  18. 'limit' => '5'
  19. ));
  20. if(isset($this->params['requested'])) {
  21. return $posts;
  22. }
  23. }
  24. function top() {
  25. $today = date("Y-m-d H:i:s");
  26. $pastweek = strtotime("-1 week");
  27. $pastweek = date("Y-m-d H:i:s",$pastweek);
  28. $posts = $this->Post->find('all',array(
  29. 'conditions' => "Post.created_at BETWEEN '$pastweek' AND '$today'",
  30. 'order' => 'hits DESC',
  31. 'limit' => '5'
  32. ));
  33. if(isset($this->params['requested'])) {
  34. return $posts;
  35. }
  36. $this->set('posts', $this->Post->find('all',array(
  37. 'conditions' => "Post.created_at BETWEEN $pastweek AND $today",
  38. 'order' => 'hits DESC',
  39. 'limit' => '5'
  40. )));
  41. }
  42. function category() {
  43. $category = $this->params['pass']['0'];
  44. $this->set('posts', $this->paginate('Post',"category LIKE '%$category%'"));
  45. }
  46. function featured() {
  47. $today = date("Y-m-d H:i:s");
  48. $pastweek = strtotime("-1 week");
  49. $pastweek = date("Y-m-d H:i:s",$pastweek);
  50. $posts = $this->Post->find('all',array(
  51. 'conditions' => "Post.featured = 1 AND Post.created_at BETWEEN '$pastweek' AND '$today'",
  52. 'order' => 'Post.created_at DESC',
  53. 'limit' => '5'
  54. ));
  55. if(isset($this->params['requested'])) {
  56. return $posts;
  57. }
  58. }
  59. function view($slug = null) {
  60. $this->set('posts', $this->Post->find("Post.slug = '$slug'"));
  61. $posts = $this->Post->find("Post.slug = '$slug'");
  62. $id = $posts["Post"]["id"];
  63. $this->Post->increaseHits($id);
  64. }
  65. function search() {
  66. $this->Line->recursive = 1;
  67. $conditions = array();
  68. if (isset($this->passedArgs)) {
  69. $input = $_GET["q"];
  70. $q = Sanitize::escape($input);
  71. $conditions = array("Post.title LIKE '%$q%' OR Post.content LIKE '%$q%'");
  72. }
  73. $this->set('posts', $this->paginate('Post', $conditions));
  74. }
  75. function delete($id) {
  76. if ($this->Auth->user('id') == $this->Post->id) {
  77. $this->Post->del($id);
  78. $this->flash('O post foi apagado.', '/posts');
  79. } else {
  80. $this->flash('Não pode apagar posts que não são seus.', '/posts');
  81. }
  82. }
  83. function promote($id) {
  84. if ($this->Auth->user('group_id') == "1") {
  85. $this->Post->promote($id);
  86. $this->flash('O artigo foi promovido.', '/posts');
  87. } else {
  88. $this->flash('Não pode promover posts.', '/posts');
  89. }
  90. }
  91. function feed(){
  92. $this->layout = "rss/default";
  93. $this->set('posts', $this->Post->find('all',array(
  94. 'order' => 'Post.created_at DESC',
  95. 'limit' => '50'
  96. )));
  97. }
  98. function widget(){
  99. $this->layout = "clean";
  100. $this->set('posts', $this->Post->find('all',array(
  101. 'order' => 'Post.created_at DESC',
  102. 'limit' => '10'
  103. )));
  104. }
  105. function archive(){
  106. $this->layout = 'archive';
  107. if (isset($this->passedArgs[0])) {
  108. $year = $this->passedArgs[0];
  109. }
  110. if (isset($this->passedArgs[1])) {
  111. $month = $this->passedArgs[1];
  112. }
  113. if (isset($this->passedArgs[2])) {
  114. $day = $this->passedArgs[2];
  115. }
  116. if (isset($year) && !isset($month) && !isset($day)) {
  117. $conditions = array("Post.created_at LIKE '$year%'");
  118. }
  119. if (isset($year) && isset($month) && !isset($day)) {
  120. $conditions = array("Post.created_at LIKE '$year-$month%'");
  121. }
  122. if (isset($year) && isset($month) && isset($day)) {
  123. $conditions = array("Post.created_at LIKE '$year-$month-$day%'");
  124. }
  125. if (!isset($year) && !isset($month) && !isset($day)) {
  126. $conditions = array("Post.created_at LIKE '%%'");
  127. }
  128. $this->set('posts', $this->paginate('Post', $conditions));
  129. }
  130. function update(){
  131. require_once('inc/simplepie.inc');
  132. // List all feeds
  133. $sql = mysql_query("SELECT * FROM feeds ORDER BY id;");
  134. $numberfeeds = mysql_num_rows($sql);
  135. while($l = mysql_fetch_array($sql)) {
  136. $feed_id = $l["id"];
  137. $feed_name = $l["name"];
  138. $user_id = $l["user_id"];
  139. $feed = new SimplePie();
  140. $feed->set_feed_url("$l[feed]");
  141. $feed->set_cache_duration(200);
  142. $feed->init();
  143. foreach ($feed->get_items() as $item) {
  144. $rsstitle = $feed->get_title();
  145. $title = $item->get_title();
  146. $permalink = $item->get_permalink();
  147. $created_at = $item->get_date('Y-m-d H:i:s');
  148. if ($category = $item->get_category())
  149. {
  150. $category = "";
  151. foreach ($item->get_categories() as $cat)
  152. {
  153. $category .= $cat->get_label();
  154. $category .= ",";
  155. }
  156. }
  157. $category = addslashes($category);
  158. $content = $item->get_content();
  159. $content = addslashes($content);
  160. $title = addslashes($title);
  161. $slug = $this->Post->createSlug($title);
  162. // Check if it already exists using the permalink
  163. $check = mysql_query("SELECT * FROM posts WHERE permalink = '$permalink'") or die(mysql_error());
  164. if (mysql_num_rows($check) == "0") {
  165. // Insert post into the database
  166. $insert = "INSERT INTO posts (feed_id,user_id,title,slug,permalink,created_at,category,content) VALUES (\"$feed_id\",\"$user_id\",\"$title\",\"$slug\",'$permalink','$created_at','$category','$content')";
  167. mysql_query($insert) or die(mysql_error());
  168. $id = mysql_insert_id();
  169. // Shorten the URL using tinyURL :)
  170. // $url = Configure::read('Site.url');
  171. //$shortlink = $this->Tinyurl->shorten("$url/posts/view/$id");
  172. // Tweet it
  173. //$this->Twitter->status_update("$title - $shortlink");
  174. }
  175. }
  176. }
  177. }
  178. /**
  179. * Admin methods
  180. *
  181. **/
  182. function admin_index() {
  183. // Find all the freaking posts :)
  184. $this->set('posts', $this->paginate('Post'));
  185. }
  186. function admin_view($id = null) {
  187. $this->Post->id = $id;
  188. $this->set('posts', $this->Post->read());
  189. }
  190. function admin_add() {
  191. if (!empty($this->data)) {
  192. if ($this->Post->save($this->data)) {
  193. $this->flash('Your feed has been saved.', '/admin/posts');
  194. }
  195. }
  196. }
  197. function admin_delete($id) {
  198. $this->Post->del($id);
  199. $this->flash('The feed with id: '.$id.' has been deleted.', '/admin/posts');
  200. }
  201. function admin_edit($id = null) {
  202. $this->Post->id = $id;
  203. if (empty($this->data)) {
  204. $this->data = $this->Post->read();
  205. } else {
  206. if ($this->Post->save($this->data)) {
  207. $this->flash('Your feed has been updated.','/admin/posts');
  208. }
  209. }
  210. }
  211. function admin_featured() {
  212. $today = date("Y-m-d H:i:s");
  213. $pastweek = strtotime("-1 week");
  214. $pastweek = date("Y-m-d H:i:s",$pastweek);
  215. $posts = $this->paginate('Post',"Post.featured = 1 AND Post.created_at BETWEEN '$pastweek' AND '$today'");
  216. if(isset($this->params['requested'])) {
  217. return $posts;
  218. }
  219. $this->set('posts', $this->paginate('Post',"Post.featured = 1 AND Post.created_at BETWEEN '$pastweek' AND '$today'"));
  220. }
  221. function demote($id) {
  222. if ($this->Auth->user('group_id') == "1") {
  223. $this->Post->demote($id);
  224. $this->flash('O artigo foi despromovido.', '/admin/posts/featured');
  225. } else {
  226. $this->flash('Não pode despromover posts.', '/admin/posts/featured');
  227. }
  228. }
  229. }
  230. ?>