SettingsController.php 4.5 KB

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