You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

HomeController.php 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Deal;
  4. use Illuminate\Http\Request;
  5. use Symfony\Component\Finder\Iterator\RecursiveDirectoryIterator;
  6. class HomeController extends Controller
  7. {
  8. /**
  9. * Create a new controller instance.
  10. *
  11. */
  12. public function __construct()
  13. {
  14. $this->middleware('auth');
  15. }
  16. /**
  17. * Show the application dashboard.
  18. *
  19. * @return \Illuminate\Http\Response
  20. */
  21. public function index()
  22. {
  23. return view('home');
  24. }
  25. public function list($type)
  26. {
  27. $deals = [];
  28. $folder_path = sprintf('%s/%s', config('app.deals_dir'), $type);
  29. if (file_exists($folder_path)) {
  30. foreach (new \RecursiveDirectoryIterator($folder_path) as $folder) {
  31. if (file_exists($folder->getPathname().'/data.json')) {
  32. $deals[] = new Deal($folder_path, $folder->getBasename());
  33. }
  34. }
  35. }
  36. return view('list', ['deals' => $deals, 'type' => $type]);
  37. }
  38. public function view($type, $id)
  39. {
  40. $folder_path = sprintf('%s/%s/%s', config('app.deals_dir'), $type, $id);
  41. if (file_exists($folder_path)) {
  42. $deal = new Deal(sprintf('%s/%s', config('app.deals_dir'), $type), $id);
  43. }
  44. return view('view', ['deal' => $deal, 'type' => $type]);
  45. }
  46. public function update(Request $request, $type, $id)
  47. {
  48. $folder_path = sprintf('%s/%s/%s', config('app.deals_dir'), $type, $id);
  49. if (file_exists($folder_path)) {
  50. $deal = new Deal(sprintf('%s/%s', config('app.deals_dir'), $type), $id);
  51. }
  52. if ($request->isMethod('post')) {
  53. // TODO filter
  54. $updated_deal = \Shikiryu\LBCReposter\Deal::fromJSON($folder_path . '/data.json');
  55. $result =
  56. $updated_deal
  57. ->setSubject($request->subject)
  58. ->setBody($request->body)
  59. ->setCategory($request->category)
  60. ->setPrice($request->price)
  61. ->save(sprintf('%s/%s', config('app.deals_dir'), $type), false);
  62. return redirect()->route('deals.list', ['type' => $type])->with('status', ($result ? 'Deal mis à jour': 'ERREUR'));
  63. }
  64. return view('update', ['deal' => $deal, 'type' => $type]);
  65. }
  66. public function delete($type, $id)
  67. {
  68. $result = false;
  69. $folder = sprintf('%s/%s/%s', config('app.deals_dir'), $type, $id);
  70. if (file_exists($folder)) {
  71. $files = array_diff(scandir($folder), ['.','..']);
  72. foreach ($files as $file) {
  73. (is_dir(sprintf('%s/%s', $folder, $file))) ? delete_folder(sprintf('%s/%s', $folder, $file)) : unlink(sprintf('%s/%s', $folder, $file));
  74. }
  75. $result = rmdir($folder);
  76. }
  77. return redirect()->route('deals.list', ['type' => $type])->with('status', ($result ? 'Deal supprimé': 'ERREUR'));
  78. }
  79. }