CommentsController.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. /**
  3. * 评论管理
  4. * @author system
  5. * @version 1.0
  6. * @date 2018-05-14 10:49:40
  7. *
  8. */
  9. namespace App\Http\Controllers\Admin\Album;
  10. use App\Http\Controllers\Admin\Controller;
  11. use App\Models\AlbumCommentsModel;
  12. use App\Models\AlbumInformationModel;
  13. use App\Models\AlbumNewsModel;
  14. use App\Repositories\Album\Criteria\CommentWhere;
  15. use Illuminate\Http\Request;
  16. use App\Repositories\Base\Criteria\OrderBy;
  17. use App\Repositories\Album\Criteria\MultiWhere;
  18. use App\Repositories\Album\CommentsRepository;
  19. class CommentsController extends Controller
  20. {
  21. private $repository;
  22. public function __construct(CommentsRepository $repository) {
  23. if(!$this->repository) $this->repository = $repository;
  24. }
  25. function index(Request $request) {
  26. $search['keyword'] = $request->input('keyword');
  27. $store_id = $this->getStoreId();
  28. $query = $this->repository->pushCriteria(new CommentWhere($search,$store_id));
  29. if(isset($request['sort_field']) && $request['sort_field'] && isset($request['sort_field_by'])) {
  30. $query = $query->pushCriteria(new OrderBy($request['sort_field'],$request['sort_field_by']));
  31. }else{
  32. $query = $query->pushCriteria(new OrderBy('id','DESC'));
  33. }
  34. $list = $query->paginate(10);
  35. foreach ($list as $key=>$item) {
  36. $info = AlbumNewsModel::where('id',$item->news_id)->first();
  37. if($info){
  38. $item->title = $info->title;
  39. } else {
  40. unset($list[$key]);
  41. }
  42. }
  43. return view('admin.album.comments.index',compact('list'));
  44. }
  45. function check(Request $request) {
  46. $request = $request->all();
  47. $search['keyword'] = $request->input('keyword');
  48. $orderby = array();
  49. if(isset($request['sort_field']) && $request['sort_field'] && isset($request['sort_field_by'])) {
  50. $orderby[$request['sort_field']] = $request['sort_field_by'];
  51. }
  52. $list = $this->repository->search($search,$orderby);
  53. return view('admin.album.comments.check',compact('list'));
  54. }
  55. /**
  56. * 添加
  57. *
  58. */
  59. public function create(Request $request)
  60. {
  61. if($request->method() == 'POST') {
  62. return $this->_createSave();
  63. }
  64. return view('admin.album.comments.edit');
  65. }
  66. /**
  67. * 保存修改
  68. */
  69. private function _createSave(){
  70. $data = (array) request('data');
  71. $id = $this->repository->create($data);
  72. if($id) {
  73. $this->showMessage('添加成功');
  74. }else{
  75. return $this->showWarning('添加失败');
  76. }
  77. }
  78. /**
  79. *
  80. * 修改
  81. *
  82. *
  83. */
  84. public function update(Request $request) {
  85. if($request->method() == 'POST') {
  86. return $this->_updateSave();
  87. }
  88. $data = $this->repository->find($request->get('id'));
  89. return view('admin.album.comments.edit',compact('data'));
  90. }
  91. /**
  92. * 保存修改
  93. */
  94. private function _updateSave() {
  95. $data = (array) request('data');
  96. //print_r($data);die;
  97. $ok = $this->repository->update(request('id'),$data);
  98. if($ok) {
  99. $url[] = array('url'=>U( 'Album/Comments/index'),'title'=>'返回列表');
  100. return $this->showMessage('操作成功',urldecode(request('_referer')));
  101. }else{
  102. $url[] = array('url'=>U( 'Album/Comments/index'),'title'=>'返回列表');
  103. return $this->showWarning('操作失败',$url);
  104. }
  105. }
  106. public function view(Request $request) {
  107. $data = $this->repository->find(request('id'));
  108. return view('admin.album.comments.view',compact('data'));
  109. }
  110. /**
  111. *
  112. * 状态改变
  113. *
  114. */
  115. public function status(Request $request) {
  116. $ok = $this->repository->updateStatus(request('id'),request('status'));
  117. if($ok) {
  118. return $this->showMessage('操作成功');
  119. }else{
  120. return $this->showWarning('操作失败');
  121. }
  122. }
  123. /**
  124. * 删除
  125. */
  126. public function destroy(Request $request) {
  127. $comments = AlbumCommentsModel::find($request->get('id'));
  128. $ok = $comments->delete();
  129. if($ok) {
  130. return $this->showMessage('操作成功');
  131. }else{
  132. return $this->showWarning("操作失败");
  133. }
  134. }
  135. }