SettingsController.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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::getAllExtensionSettingsClasses());
  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. // call getOptionInputData method to get all options
  40. if (method_exists($className, 'getOptionInputData')) {
  41. $optionInputData = $className::getOptionInputData();
  42. } else {
  43. $optionInputData = [];
  44. }
  45. // collect all option input data
  46. $optionsData = [];
  47. foreach ($options as $key => $value) {
  48. $optionsData[$key] = [
  49. 'value' => $value,
  50. 'label' => $optionInputData[$key]['label'] ?? ucwords(str_replace('_', ' ', $key)),
  51. 'type' => $optionInputData[$key]['type'] ?? 'string',
  52. 'description' => $optionInputData[$key]['description'] ?? '',
  53. 'options' => $optionInputData[$key]['options'] ?? [],
  54. 'identifier' => $optionInputData[$key]['identifier'] ?? 'option'
  55. ];
  56. }
  57. // collect category icon if available
  58. if (isset($optionInputData['category_icon'])) {
  59. $optionsData['category_icon'] = $optionInputData['category_icon'];
  60. }
  61. $optionsData['settings_class'] = $className;
  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. $this->checkPermission("settings." . strtolower($category) . ".write");
  80. $settings_class = request()->get('settings_class');
  81. if (method_exists($settings_class, 'getValidations')) {
  82. $validations = $settings_class::getValidations();
  83. } else {
  84. $validations = [];
  85. }
  86. $validator = Validator::make($request->all(), $validations);
  87. if ($validator->fails()) {
  88. return Redirect::to('admin/settings' . '#' . $category)->withErrors($validator)->withInput();
  89. }
  90. $settingsClass = new $settings_class();
  91. foreach ($settingsClass->toArray() as $key => $value) {
  92. // Get the type of the settingsclass property
  93. $rp = new \ReflectionProperty($settingsClass, $key);
  94. $rpType = $rp->getType();
  95. if ($rpType == 'bool') {
  96. $settingsClass->$key = $request->has($key);
  97. continue;
  98. }
  99. if ($rp->name == 'available') {
  100. $settingsClass->$key = implode(",", $request->$key);
  101. continue;
  102. }
  103. $nullable = $rpType->allowsNull();
  104. if ($nullable) $settingsClass->$key = $request->input($key) ?? null;
  105. else $settingsClass->$key = $request->input($key);
  106. }
  107. $settingsClass->save();
  108. return Redirect::to('admin/settings' . '#' . $category)->with('success', 'Settings updated successfully.');
  109. }
  110. public function updateIcons(Request $request)
  111. {
  112. $request->validate([
  113. 'icon' => 'nullable|max:10000|mimes:jpg,png,jpeg',
  114. 'logo' => 'nullable|max:10000|mimes:jpg,png,jpeg',
  115. 'favicon' => 'nullable|max:10000|mimes:ico',
  116. ]);
  117. if ($request->hasFile('icon')) {
  118. $request->file('icon')->storeAs('public', 'icon.png');
  119. }
  120. if ($request->hasFile('logo')) {
  121. $request->file('logo')->storeAs('public', 'logo.png');
  122. }
  123. if ($request->hasFile('favicon')) {
  124. $request->file('favicon')->storeAs('public', 'favicon.ico');
  125. }
  126. return Redirect::to('admin/settings')->with('success', 'Icons updated successfully.');
  127. }
  128. }