SettingsController.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. /**
  3. * 配置列表
  4. * @author system
  5. * @version 1.0
  6. * @date 2017-05-31 04:56:09
  7. *
  8. */
  9. namespace App\Http\Controllers\Admin\Base;
  10. use App\Http\Controllers\Admin\Controller;
  11. use Illuminate\Http\Request;
  12. use App\Repositories\Base\Criteria\OrderBy;
  13. use App\Repositories\Settings\Criteria\MultiWhere;
  14. use App\Repositories\Settings\SettingsRepository;
  15. use App\Models\BaseSettingsModel;
  16. class SettingsController extends Controller
  17. {
  18. private $repository;
  19. public function __construct(SettingsRepository $repository) {
  20. if(!$this->repository) $this->repository = $repository;
  21. }
  22. function index(Request $reqeust) {
  23. $search['keyword'] = $reqeust->input('keyword');
  24. $query = $this->repository->pushCriteria(new MultiWhere($search));
  25. if(isset($request['sort_field']) && $request['sort_field'] && isset($request['sort_field_by'])) {
  26. $query = $query->pushCriteria(new OrderBy($request['sort_field'],$request['sort_field_by']));
  27. }
  28. $list = $query->paginate();
  29. return view('admin.base.settings.index',compact('list'));
  30. }
  31. function check(Request $reqeust) {
  32. $request = $reqeust->all();
  33. $search['keyword'] = $reqeust->input('keyword');
  34. $orderby = array();
  35. if(isset($request['sort_field']) && $request['sort_field'] && isset($request['sort_field_by'])) {
  36. $orderby[$request['sort_field']] = $request['sort_field_by'];
  37. }
  38. $list = $this->repository->search($search,$orderby);
  39. return view('admin.base.settings.check',compact('list'));
  40. }
  41. /**
  42. * 添加
  43. *
  44. */
  45. public function create(Request $reqeust)
  46. {
  47. if($reqeust->method() == 'POST') {
  48. return $this->_createSave();
  49. }
  50. return view('admin.base.settings.edit', [
  51. 'categories' => BaseSettingsModel::where('category', '=', 'category')->get(),
  52. ]);
  53. }
  54. /**
  55. * 保存修改
  56. */
  57. private function _createSave(){
  58. $data = (array) request('data');
  59. if ($this->repository->where(['category' => $data['category'], 'key' => $data['key']])->exists()) {
  60. $url[] = array('url'=>U( 'Base/Settings/index'),'title'=>'返回列表');
  61. return $this->showWarning('该类别下已有相同键存在!',$url);
  62. }
  63. $data['sort'] = empty($request->sort) ? 0 : $request->sort;
  64. $id = $this->repository->create($data);
  65. if($id) {
  66. $url[] = array('url'=>U( 'Base/Settings/index'),'title'=>'返回列表');
  67. $url[] = array('url'=>U( 'Base/Settings/create'),'title'=>'继续添加');
  68. $this->showMessage('添加成功',$url);
  69. }else{
  70. $url[] = array('url'=>U( 'Base/Settings/index'),'title'=>'返回列表');
  71. return $this->showWarning('添加失败',$url);
  72. }
  73. }
  74. /**
  75. *
  76. * 修改
  77. *
  78. *
  79. */
  80. public function update(Request $reqeust) {
  81. if($reqeust->method() == 'POST') {
  82. return $this->_updateSave();
  83. }
  84. $data = $this->repository->find($reqeust->get('id'));
  85. $categories = BaseSettingsModel::where('category', '=', 'category')->get();
  86. return view('admin.base.settings.edit',compact('data','categories'));
  87. }
  88. /**
  89. * 保存修改
  90. */
  91. private function _updateSave() {
  92. $data = (array) request('data');
  93. if ($this->repository->where(['category' => $data['category'], 'key' => $data['key']])->exists()) {
  94. $url[] = array('url'=>U( 'Base/Settings/index'),'title'=>'返回列表');
  95. return $this->showWarning('该类别下已有相同键存在!',$url);
  96. }
  97. $ok = $this->repository->update(request('id'),$data);
  98. if($ok) {
  99. $url[] = array('url'=>U( 'Base/Settings/index'),'title'=>'返回列表');
  100. return $this->showMessage('操作成功',urldecode(request('_referer')));
  101. }else{
  102. $url[] = array('url'=>U( 'Base/Settings/index'),'title'=>'返回列表');
  103. return $this->showWarning('操作失败',$url);
  104. }
  105. }
  106. public function view(Request $reqeust) {
  107. $data = $this->repository->find(request('id'));
  108. return view('admin.base.settings.view',compact('data'));
  109. }
  110. /**
  111. *
  112. * 状态改变
  113. *
  114. */
  115. public function status(Request $reqeust) {
  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 $reqeust) {
  127. $bool = $this->repository->destroy($reqeust->get('id'));
  128. if($bool) {
  129. return $this->showMessage('操作成功');
  130. }else{
  131. return $this->showWarning("操作失败");
  132. }
  133. }
  134. }