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

/app/controllers/admin/AdminBlogsController.php

https://gitlab.com/jnettome/myproject
PHP | 241 lines | 110 code | 40 blank | 91 comment | 6 complexity | a3d7a2d6e768ab6601a20d537b3c81b8 MD5 | raw file
  1. <?php
  2. class AdminBlogsController extends AdminController {
  3. /**
  4. * Post Model
  5. * @var Post
  6. */
  7. protected $post;
  8. /**
  9. * Inject the models.
  10. * @param Post $post
  11. */
  12. public function __construct(Post $post)
  13. {
  14. parent::__construct();
  15. $this->post = $post;
  16. }
  17. /**
  18. * Show a list of all the blog posts.
  19. *
  20. * @return View
  21. */
  22. public function getIndex()
  23. {
  24. // Title
  25. $title = Lang::get('admin/blogs/title.blog_management');
  26. // Grab all the blog posts
  27. $posts = $this->post;
  28. // Show the page
  29. return View::make('admin/blogs/index', compact('posts', 'title'));
  30. }
  31. /**
  32. * Show the form for creating a new resource.
  33. *
  34. * @return Response
  35. */
  36. public function getCreate()
  37. {
  38. // Title
  39. $title = Lang::get('admin/blogs/title.create_a_new_blog');
  40. // Show the page
  41. return View::make('admin/blogs/create_edit', compact('title'));
  42. }
  43. /**
  44. * Store a newly created resource in storage.
  45. *
  46. * @return Response
  47. */
  48. public function postCreate()
  49. {
  50. // Declare the rules for the form validation
  51. $rules = array(
  52. 'title' => 'required|min:3',
  53. 'content' => 'required|min:3'
  54. );
  55. // Validate the inputs
  56. $validator = Validator::make(Input::all(), $rules);
  57. // Check if the form validates with success
  58. if ($validator->passes())
  59. {
  60. // Create a new blog post
  61. $user = Auth::user();
  62. // Update the blog post data
  63. $this->post->title = Input::get('title');
  64. $this->post->slug = Str::slug(Input::get('title'));
  65. $this->post->content = Input::get('content');
  66. $this->post->meta_title = Input::get('meta-title');
  67. $this->post->meta_description = Input::get('meta-description');
  68. $this->post->meta_keywords = Input::get('meta-keywords');
  69. $this->post->user_id = $user->id;
  70. // Was the blog post created?
  71. if($this->post->save())
  72. {
  73. // Redirect to the new blog post page
  74. return Redirect::to('admin/blogs/' . $this->post->id . '/edit')->with('success', Lang::get('admin/blogs/messages.create.success'));
  75. }
  76. // Redirect to the blog post create page
  77. return Redirect::to('admin/blogs/create')->with('error', Lang::get('admin/blogs/messages.create.error'));
  78. }
  79. // Form validation failed
  80. return Redirect::to('admin/blogs/create')->withInput()->withErrors($validator);
  81. }
  82. /**
  83. * Display the specified resource.
  84. *
  85. * @param $post
  86. * @return Response
  87. */
  88. public function getShow($post)
  89. {
  90. // redirect to the frontend
  91. }
  92. /**
  93. * Show the form for editing the specified resource.
  94. *
  95. * @param $post
  96. * @return Response
  97. */
  98. public function getEdit($post)
  99. {
  100. // Title
  101. $title = Lang::get('admin/blogs/title.blog_update');
  102. // Show the page
  103. return View::make('admin/blogs/create_edit', compact('post', 'title'));
  104. }
  105. /**
  106. * Update the specified resource in storage.
  107. *
  108. * @param $post
  109. * @return Response
  110. */
  111. public function postEdit($post)
  112. {
  113. // Declare the rules for the form validation
  114. $rules = array(
  115. 'title' => 'required|min:3',
  116. 'content' => 'required|min:3'
  117. );
  118. // Validate the inputs
  119. $validator = Validator::make(Input::all(), $rules);
  120. // Check if the form validates with success
  121. if ($validator->passes())
  122. {
  123. // Update the blog post data
  124. $post->title = Input::get('title');
  125. $post->slug = Str::slug(Input::get('title'));
  126. $post->content = Input::get('content');
  127. $post->meta_title = Input::get('meta-title');
  128. $post->meta_description = Input::get('meta-description');
  129. $post->meta_keywords = Input::get('meta-keywords');
  130. // Was the blog post updated?
  131. if($post->save())
  132. {
  133. // Redirect to the new blog post page
  134. return Redirect::to('admin/blogs/' . $post->id . '/edit')->with('success', Lang::get('admin/blogs/messages.update.success'));
  135. }
  136. // Redirect to the blogs post management page
  137. return Redirect::to('admin/blogs/' . $post->id . '/edit')->with('error', Lang::get('admin/blogs/messages.update.error'));
  138. }
  139. // Form validation failed
  140. return Redirect::to('admin/blogs/' . $post->id . '/edit')->withInput()->withErrors($validator);
  141. }
  142. /**
  143. * Remove the specified resource from storage.
  144. *
  145. * @param $post
  146. * @return Response
  147. */
  148. public function getDelete($post)
  149. {
  150. // Title
  151. $title = Lang::get('admin/blogs/title.blog_delete');
  152. // Show the page
  153. return View::make('admin/blogs/delete', compact('post', 'title'));
  154. }
  155. /**
  156. * Remove the specified resource from storage.
  157. *
  158. * @param $post
  159. * @return Response
  160. */
  161. public function postDelete($post)
  162. {
  163. // Declare the rules for the form validation
  164. $rules = array(
  165. 'id' => 'required|integer'
  166. );
  167. // Validate the inputs
  168. $validator = Validator::make(Input::all(), $rules);
  169. // Check if the form validates with success
  170. if ($validator->passes())
  171. {
  172. $id = $post->id;
  173. $post->delete();
  174. // Was the blog post deleted?
  175. $post = Post::find($id);
  176. if(empty($post))
  177. {
  178. // Redirect to the blog posts management page
  179. return Redirect::to('admin/blogs')->with('success', Lang::get('admin/blogs/messages.delete.success'));
  180. }
  181. }
  182. // There was a problem deleting the blog post
  183. return Redirect::to('admin/blogs')->with('error', Lang::get('admin/blogs/messages.delete.error'));
  184. }
  185. /**
  186. * Show a list of all the blog posts formatted for Datatables.
  187. *
  188. * @return Datatables JSON
  189. */
  190. public function getData()
  191. {
  192. $posts = Post::select(array('posts.id', 'posts.title', 'posts.id as comments', 'posts.created_at'));
  193. return Datatables::of($posts)
  194. ->edit_column('comments', '{{ DB::table(\'comments\')->where(\'post_id\', \'=\', $id)->count() }}')
  195. ->add_column('actions', '<a href="{{{ URL::to(\'admin/blogs/\' . $id . \'/edit\' ) }}}" class="btn btn-default btn-xs iframe" >{{{ Lang::get(\'button.edit\') }}}</a>
  196. <a href="{{{ URL::to(\'admin/blogs/\' . $id . \'/delete\' ) }}}" class="btn btn-xs btn-danger iframe">{{{ Lang::get(\'button.delete\') }}}</a>
  197. ')
  198. ->remove_column('id')
  199. ->make();
  200. }
  201. }