SettingsController.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Http\Controllers\Controller;
  4. use Illuminate\Contracts\Foundation\Application;
  5. use Illuminate\Contracts\View\Factory;
  6. use Illuminate\Contracts\View\View;
  7. use Illuminate\Http\Response;
  8. use Illuminate\Http\Request;
  9. use Illuminate\Support\Facades\Redirect;
  10. use Illuminate\Support\Facades\Validator;
  11. use Qirolab\Theme\Theme;
  12. class SettingsController extends Controller
  13. {
  14. /**
  15. * Display a listing of the resource.
  16. *
  17. * @return Application|Factory|View|Response
  18. */
  19. public function index()
  20. {
  21. // get all other settings in app/Settings directory
  22. // group items by file name like $categories
  23. $settings = collect();
  24. foreach (scandir(app_path('Settings')) as $file) {
  25. if (in_array($file, ['.', '..'])) {
  26. continue;
  27. }
  28. $className = 'App\\Settings\\' . str_replace('.php', '', $file);
  29. $options = (new $className())->toArray();
  30. if (method_exists($className, 'getOptionInputData')) {
  31. $optionInputData = $className::getOptionInputData();
  32. } else {
  33. $optionInputData = [];
  34. }
  35. // collect all option input data
  36. $optionsData = [];
  37. foreach ($options as $key => $value) {
  38. $optionsData[$key] = [
  39. 'value' => $value,
  40. 'label' => $optionInputData[$key]['label'] ?? ucwords(str_replace('_', ' ', $key)),
  41. 'type' => $optionInputData[$key]['type'] ?? 'string',
  42. 'description' => $optionInputData[$key]['description'] ?? '',
  43. 'options' => $optionInputData[$key]['options'] ?? [],
  44. ];
  45. }
  46. // collect category icon if available
  47. if (isset($optionInputData['category_icon'])) {
  48. $optionsData['category_icon'] = $optionInputData['category_icon'];
  49. }
  50. $settings[str_replace('Settings.php', '', $file)] = $optionsData;
  51. }
  52. $settings->sort();
  53. $themes = array_diff(scandir(base_path('themes')), array('..', '.'));
  54. return view('admin.settings.index', [
  55. 'settings' => $settings->all(),
  56. 'themes' => $themes,
  57. 'active_theme' => Theme::active(),
  58. ]);
  59. }
  60. /**
  61. * Update the specified resource in storage.
  62. *
  63. */
  64. public function update(Request $request)
  65. {
  66. $category = request()->get('category');
  67. $className = 'App\\Settings\\' . $category . 'Settings';
  68. if (method_exists($className, 'getValidations')) {
  69. $validations = $className::getValidations();
  70. } else {
  71. $validations = [];
  72. }
  73. $validator = Validator::make($request->all(), $validations);
  74. if ($validator->fails()) {
  75. return Redirect::to('admin/settings' . '#' . $category)->withErrors($validator)->withInput();
  76. }
  77. $settingsClass = new $className();
  78. foreach ($settingsClass->toArray() as $key => $value) {
  79. switch (gettype($value)) {
  80. case 'boolean':
  81. $settingsClass->$key = $request->has($key);
  82. break;
  83. case 'string':
  84. $settingsClass->$key = $request->input($key) ?? '';
  85. break;
  86. case 'integer':
  87. $settingsClass->$key = $request->input($key) ?? 0;
  88. break;
  89. case 'array':
  90. $settingsClass->$key = $request->input($key) ?? [];
  91. break;
  92. case 'double':
  93. $settingsClass->$key = $request->input($key) ?? 0.0;
  94. break;
  95. }
  96. }
  97. $settingsClass->save();
  98. return Redirect::to('admin/settings' . '#' . $category)->with('success', 'Settings updated successfully.');
  99. }
  100. }