AdvertiseController.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use Request;
  4. use App\Models\Advertise;
  5. use App\Models\AdvertisesType;
  6. use App\Http\Requests\AdvertiseRequest;
  7. class AdvertiseController extends BaseController
  8. {
  9. /* 视图文件相对路径 */
  10. protected $view = 'admin.advertise.list';
  11. protected $view_addupd = 'admin.advertise.addupd';
  12. protected $menu_name = 'advertise';
  13. protected $route_href = 'adv';
  14. protected $image_path = 'uploads/advertise/images';
  15. public function __construct()
  16. {
  17. parent::__construct($this->menu_name);
  18. }
  19. // 获取图片文件名
  20. public function getImageName($tpid, $sort)
  21. {
  22. return AdvertisesType::find($tpid)->ename.'_'.$sort;
  23. }
  24. // 获取图片新排序文件名
  25. public function getImageNewName($urlPath, $name)
  26. {
  27. $path = dirname($urlPath);
  28. $ext = pathinfo($urlPath, PATHINFO_EXTENSION);
  29. return $path.'/'.$name.'.'.$ext;
  30. }
  31. /* 广告位管理 */
  32. // 页面展示
  33. public function index($tpid)
  34. {
  35. $advs = Advertise::where('tpid', $tpid)
  36. ->orderBy('sort')
  37. ->paginate(10);
  38. $view_para = array_add($this->view_para, 'advertises', $advs);
  39. return view($this->view, $view_para);
  40. }
  41. /* 添加 */
  42. // 页面展示
  43. public function addView($tpid)
  44. {
  45. $advType = AdvertisesType::find($tpid);
  46. $view_para = array_add($this->view_para, 'advertiseType', $advType);
  47. return view($this->view_addupd, $view_para);
  48. }
  49. // 表单处理
  50. public function addPost($tpid, AdvertiseRequest $request)
  51. {
  52. $image = Request::file('image');
  53. if (!$image) {
  54. return redirect()->back()->with('errors_image', '图片不可为空')->withInput();
  55. }
  56. $errors_image = $this->imageValidator($image);
  57. if ($errors_image) {
  58. return redirect()->back()->with('errors_image', $errors_image)->withInput();
  59. }
  60. $data = Request::except('image');
  61. $ext = $image->getClientOriginalExtension();
  62. $image_name = $this->getImageName($tpid, $data['sort']);
  63. $path = $this->imageUpload($image_name, $image, $this->image_path);
  64. $data['image'] = $path;
  65. $data['tpid'] = $tpid;
  66. Advertise::create($data);
  67. return redirect()->route($this->route_href, ['tpid' => $tpid])->with('success', '添加成功');
  68. }
  69. /* 编辑 */
  70. // 页面展示
  71. public function updView($tpid, $id)
  72. {
  73. $adv = Advertise::find($id);
  74. $advType = AdvertisesType::find($tpid);
  75. $view_para = array_add($this->view_para, 'advertise', $adv);
  76. $view_para = array_add($view_para, 'advertiseType', $advType);
  77. return view($this->view_addupd, $view_para);
  78. }
  79. // 表单处理
  80. public function updPost($tpid, $id, AdvertiseRequest $request)
  81. {
  82. $adv = Advertise::find($id);
  83. $oldUrlPath = $adv->image;
  84. $sort = Request::input('sort');
  85. $image_name = $this->getImageName($adv->tpid, $sort);
  86. $image = Request::file('image');
  87. if ($image) {
  88. $adv->image = $this->imageUpload($image_name, $image, $this->image_path);
  89. }
  90. else if (file_exists($oldUrlPath) && $adv->sort!=$sort) {
  91. $newUrlPath = $this->getImageNewName($oldUrlPath, $image_name);
  92. $result = $this->imageRename($oldUrlPath, $newUrlPath);
  93. if (!$result) {
  94. return redirect()->route($this->route_href, ['tpid' => $tpid])->with('error', '编辑失败');
  95. }
  96. $adv->image = $newUrlPath;
  97. }
  98. $adv->sort = Request::input('sort');
  99. $adv->href = Request::input('href');
  100. $adv->save();
  101. return redirect()->route($this->route_href, ['tpid' => $tpid])->with('success', '编辑成功');
  102. }
  103. /* 删除 */
  104. public function del($tpid, $id)
  105. {
  106. $adv = Advertise::find($id);
  107. $image_name = $adv->image;
  108. /*if (!$image_name) {
  109. return redirect()->route($this->route_href, ['tpid' => $tpid])->with('success', '删除成功');
  110. }
  111. else if (file_exists($image_name)) {
  112. $result = $this->imageDelete($image_name);
  113. if (!$result) {
  114. return redirect()->route($this->route_href, ['tpid' => $tpid])->with('error', '删除失败');
  115. }
  116. }
  117. $adv->delete();
  118. return redirect()->route($this->route_href, ['tpid' => $tpid])->with('success', '删除成功');*/
  119. if ($image_name) {
  120. if (file_exists($image_name) && !$this->imageDelete($image_name)) {
  121. return redirect()->route($this->route_href, ['tpid' => $tpid])->with('error', '删除失败');
  122. }
  123. $adv->delete();
  124. }
  125. return redirect()->route($this->route_href, ['tpid' => $tpid])->with('success', '删除成功');
  126. }
  127. }