64 lines
2.1 KiB
PHP
64 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace Modules\Portal\Http\Controllers;
|
|
|
|
use App\Business;
|
|
use App\Utils\ModuleUtil;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Routing\Controller;
|
|
use Modules\Portal\Utils\PortalUtil;
|
|
|
|
class SettingsController extends Controller
|
|
{
|
|
public function __construct(protected ModuleUtil $moduleUtil) {}
|
|
|
|
public function index()
|
|
{
|
|
$this->authorizeSettings();
|
|
|
|
$business_id = (int) request()->session()->get('user.business_id');
|
|
$business = Business::findOrFail($business_id);
|
|
$common_settings = $business->common_settings ?? [];
|
|
|
|
$portals = [];
|
|
foreach (PortalUtil::portals() as $portal) {
|
|
$portals[$portal] = [
|
|
'label' => PortalUtil::portalLabel($portal),
|
|
'subscription' => PortalUtil::hasPortalSubscription($business_id, $portal, $this->moduleUtil),
|
|
'enabled' => ! array_key_exists(PortalUtil::businessSettingKey($portal), $common_settings)
|
|
|| ! empty($common_settings[PortalUtil::businessSettingKey($portal)]),
|
|
];
|
|
}
|
|
|
|
return view('portal::admin.settings', compact('business', 'common_settings', 'portals'));
|
|
}
|
|
|
|
public function update(Request $request)
|
|
{
|
|
$this->authorizeSettings();
|
|
|
|
$business_id = (int) request()->session()->get('user.business_id');
|
|
$business = Business::findOrFail($business_id);
|
|
$common_settings = $business->common_settings ?? [];
|
|
|
|
foreach (PortalUtil::portals() as $portal) {
|
|
$key = PortalUtil::businessSettingKey($portal);
|
|
$common_settings[$key] = $request->boolean($key) ? 1 : 0;
|
|
}
|
|
|
|
$business->common_settings = $common_settings;
|
|
$business->save();
|
|
|
|
return redirect()
|
|
->action([self::class, 'index'])
|
|
->with('status', ['success' => 1, 'msg' => __('portal::lang.portal_settings_saved')]);
|
|
}
|
|
|
|
private function authorizeSettings(): void
|
|
{
|
|
if (! auth()->user()->can('superadmin') && ! auth()->user()->can('portal.settings.manage')) {
|
|
abort(403, 'Unauthorized action.');
|
|
}
|
|
}
|
|
}
|