Blog.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. ApplicationSettings::RegisterDefaultSetting("blog", "posts_per_page", 10);
  3. class Blog extends Controller {
  4. public function Index($page=1, IBlogPostRepository $repo=null){
  5. $count=(int)ApplicationSettings::GetSetting("blog", "posts_per_page");
  6. $offset=($page-1)*$count;
  7. $total=$repo->GetCount();
  8. $maxPages=$total/$count;
  9. $posts=$repo->GetLatest($count,$offset);
  10. $vars=array(
  11. "posts"=>$posts,
  12. "page"=>$page,
  13. "maxPages"=>$maxPages
  14. );
  15. return new View("Blog/index.view",$vars);
  16. }
  17. public function Page($page=1, IBlogPostRepository $repo=null) {
  18. return $this->Index($page,$repo);
  19. }
  20. public function Create($title,$draft){
  21. Breadcrumbs::Add("Create", "/blog/create");
  22. $post=new BlogPost();
  23. if (isset($title))
  24. $post->PostTitle=$title;
  25. if (isset($draft))
  26. $post->PostDraft=$draft;
  27. if ($post->PostTitle!="" && $post->PostDraft!=""){
  28. $post->Save();
  29. header("location:/blog/manage");
  30. return;
  31. }
  32. return new View("Blog/create.view",array("post"=>$post));
  33. }
  34. public function Manage(IBlogPostRepository $repo,$formSubmitted=false,$publish=null,$delete=null){
  35. Breadcrumbs::Add("Manage", "/blog/manage");
  36. if ($formSubmitted!=false && $formSubmitted==="yes"){
  37. if ($delete!=null){
  38. $post=new BlogPost($delete);
  39. $post->Delete();
  40. }
  41. if ($publish!=null){
  42. $post=new BlogPost($publish);
  43. $post->Publish();
  44. }
  45. header("location:/blog/manage");
  46. return;
  47. }
  48. $posts=$repo->GetAll();
  49. return new View("Blog/manage.view",array("posts"=>$posts));
  50. }
  51. public function Edit($postId,$title,$draft) {
  52. $post=new BlogPost($postId);
  53. Breadcrumbs::Add("Edit", "");
  54. if($title!=null && $draft!=null){
  55. $post->PostTitle=$title;
  56. $post->PostDraft=$draft;
  57. $post->Save();
  58. }
  59. return new View("Blog/create.view",array("post"=>$post));
  60. }
  61. public function View($url){
  62. if ($url==null){
  63. header("location:/blog");
  64. return;
  65. }
  66. $post=new BlogPost($url);
  67. if (!$post->PostId)
  68. return new View("E404/index.view");
  69. Breadcrumbs::Add(htmlspecialchars($post->PostTitle), "");
  70. return new View("Blog/view.view",array("content"=>$post->PostContent,"timestamp"=>$post->PostTimestamp));
  71. }
  72. public function ViewDraft($url){
  73. if ($url==null){
  74. header("location:/blog/manage");
  75. return;
  76. }
  77. $post=new BlogPost($url);
  78. if (!$post->PostId)
  79. return new View("E404/index.view");
  80. Breadcrumbs::Add("Draft", "");
  81. Breadcrumbs::Add(htmlspecialchars($post->PostTitle), "");
  82. return new View("Blog/view.view",array("content"=>$post->PostDraft,"timestamp"=>$post->PostTimestamp));
  83. }
  84. }