SettingsController.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. const READ_PERMISSIONS = "admin.settings.read";
  16. const WRITE_PERMISSIONS = "admin.settings.write";
  17. /**
  18. * Display a listing of the resource.
  19. *
  20. * @return Application|Factory|View|Response
  21. */
  22. public function index()
  23. {
  24. $this->checkPermission(self::READ_PERMISSIONS);
  25. // get all other settings in app/Settings directory
  26. // group items by file name like $categories
  27. $settings = collect();
  28. $settings_classes = [];
  29. // get all app settings
  30. $app_settings = scandir(app_path('Settings'));
  31. $app_settings = array_diff($app_settings, ['.', '..']);
  32. // append App\Settings to class name
  33. foreach ($app_settings as $app_setting) {
  34. $settings_classes[] = 'App\\Settings\\' . str_replace('.php', '', $app_setting);
  35. }
  36. // get all extension settings
  37. $settings_files = array_merge($settings_classes, ExtensionHelper::getAllExtensionSettingsClasses());
  38. foreach ($settings_files as $file) {
  39. $className = $file;
  40. // instantiate the class and call toArray method to get all options
  41. $options = (new $className())->toArray();
  42. // call getOptionInputData method to get all options
  43. if (method_exists($className, 'getOptionInputData')) {
  44. $optionInputData = $className::getOptionInputData();
  45. } else {
  46. $optionInputData = [];
  47. }
  48. // collect all option input data
  49. $optionsData = [];
  50. foreach ($options as $key => $value) {
  51. $optionsData[$key] = [
  52. 'value' => $value,
  53. 'label' => $optionInputData[$key]['label'] ?? ucwords(str_replace('_', ' ', $key)),
  54. 'type' => $optionInputData[$key]['type'] ?? 'string',
  55. 'description' => $optionInputData[$key]['description'] ?? '',
  56. 'options' => $optionInputData[$key]['options'] ?? [],
  57. ];
  58. }
  59. // collect category icon if available
  60. if (isset($optionInputData['category_icon'])) {
  61. $optionsData['category_icon'] = $optionInputData['category_icon'];
  62. }
  63. $optionsData['settings_class'] = $className;
  64. $settings[str_replace('Settings', '', class_basename($className))] = $optionsData;
  65. }
  66. $settings->sort();
  67. $themes = array_diff(scandir(base_path('themes')), array('..', '.'));
  68. return view('admin.settings.index', [
  69. 'settings' => $settings->all(),
  70. 'themes' => $themes,
  71. 'active_theme' => Theme::active(),
  72. ]);
  73. }
  74. /**
  75. * Update the specified resource in storage.
  76. *
  77. */
  78. public function update(Request $request)
  79. {
  80. $this->checkPermission(self::WRITE_PERMISSIONS);
  81. $category = request()->get('category');
  82. $settings_class = request()->get('settings_class');
  83. if (method_exists($settings_class, 'getValidations')) {
  84. $validations = $settings_class::getValidations();
  85. } else {
  86. $validations = [];
  87. }
  88. $validator = Validator::make($request->all(), $validations);
  89. if ($validator->fails()) {
  90. return Redirect::to('admin/settings' . '#' . $category)->withErrors($validator)->withInput();
  91. }
  92. $settingsClass = new $settings_class();
  93. foreach ($settingsClass->toArray() as $key => $value) {
  94. // Get the type of the settingsclass property
  95. $rp = new \ReflectionProperty($settingsClass, $key);
  96. $rpType = $rp->getType();
  97. if ($rpType == 'bool') {
  98. $settingsClass->$key = $request->has($key);
  99. continue;
  100. }
  101. $nullable = $rpType->allowsNull();
  102. if ($nullable) $settingsClass->$key = $request->input($key) ?? null;
  103. else $settingsClass->$key = $request->input($key);
  104. }
  105. $settingsClass->save();
  106. return Redirect::to('admin/settings' . '#' . $category)->with('success', 'Settings updated successfully.');
  107. }
  108. }