2026-02-09 14:09:19 +03:30
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
|
|
|
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
|
use App\Models\Setting;
|
2026-07-08 13:15:29 +03:30
|
|
|
use App\Services\PriceChannelBroadcastService;
|
2026-02-09 14:09:19 +03:30
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
|
|
|
|
|
class SettingsController extends Controller
|
|
|
|
|
{
|
2026-06-21 21:44:44 +03:30
|
|
|
private const DEPLOY_ADMIN_MOBILE_SUFFIX = '9128081392';
|
|
|
|
|
|
|
|
|
|
private const NEW_SETTING_NOTIFY_MOBILE = '09134469985';
|
|
|
|
|
|
2026-07-19 15:53:05 +03:30
|
|
|
/**
|
|
|
|
|
* کلیدهایی که پنل ادمین باید بتواند ویرایش کند حتی اگر در DB هنوز can_edit=0 باشد
|
|
|
|
|
* (بنرها / سایتهای ریگیری).
|
|
|
|
|
*/
|
|
|
|
|
private const PANEL_MANAGED_KEYS = [
|
|
|
|
|
'tracking_systems',
|
|
|
|
|
'tracking_systems_enabled',
|
|
|
|
|
'banner_enabled',
|
|
|
|
|
];
|
|
|
|
|
|
2026-06-04 16:01:13 +03:30
|
|
|
public function index(Request $request)
|
|
|
|
|
{
|
|
|
|
|
$query = Setting::query()
|
2026-06-04 14:19:54 +03:30
|
|
|
->select(Setting::adminSelectColumns())
|
2026-06-04 16:01:13 +03:30
|
|
|
->orderBy('key');
|
2026-06-04 14:19:54 +03:30
|
|
|
|
2026-06-21 21:44:44 +03:30
|
|
|
if ($request->boolean('system_only')) {
|
|
|
|
|
return response()->json(
|
|
|
|
|
$query
|
|
|
|
|
->where(function ($q) {
|
|
|
|
|
$q->where('can_edit', 0)->orWhere('can_edit', false);
|
|
|
|
|
})
|
|
|
|
|
->where(function ($q) {
|
|
|
|
|
$q->where('is_active', 1)
|
|
|
|
|
->orWhere('is_active', true)
|
|
|
|
|
->orWhere('is_active', '1');
|
|
|
|
|
})
|
|
|
|
|
->get()
|
|
|
|
|
);
|
2026-06-04 16:01:13 +03:30
|
|
|
}
|
|
|
|
|
|
2026-06-21 21:44:44 +03:30
|
|
|
if ($request->boolean('editable_only')) {
|
|
|
|
|
return response()->json($query->where('can_edit', 1)->get());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return response()->json($query->get());
|
2026-02-09 14:09:19 +03:30
|
|
|
}
|
2026-06-21 21:44:44 +03:30
|
|
|
|
2026-02-09 14:09:19 +03:30
|
|
|
public function edit(Request $request)
|
|
|
|
|
{
|
2026-06-21 21:44:44 +03:30
|
|
|
$isDeployAdmin = $this->isDeployAdminUser();
|
|
|
|
|
$isSystemDeploy = $request->boolean('system_deploy');
|
2026-02-09 14:09:19 +03:30
|
|
|
$setting = Setting::where('key', $request->key)->first();
|
2026-06-21 21:44:44 +03:30
|
|
|
$isNew = !$setting;
|
2026-02-09 14:09:19 +03:30
|
|
|
|
2026-06-21 21:44:44 +03:30
|
|
|
if ($isNew) {
|
|
|
|
|
if ($isSystemDeploy && !$isDeployAdmin) {
|
|
|
|
|
return response()->json(['message' => 'Unauthorized'], 403);
|
2026-06-05 17:43:02 +03:30
|
|
|
}
|
2026-06-21 21:44:44 +03:30
|
|
|
|
2026-02-09 14:09:19 +03:30
|
|
|
$setting = new Setting();
|
|
|
|
|
$setting->key = $request->key;
|
2026-06-21 21:44:44 +03:30
|
|
|
$setting->can_edit = $isSystemDeploy ? 0 : 1;
|
2026-07-19 15:53:05 +03:30
|
|
|
} elseif (!$setting->can_edit && !$isDeployAdmin && !$this->isPanelManagedKey((string) $request->key)) {
|
2026-06-21 21:44:44 +03:30
|
|
|
return response()->json(['message' => 'Forbidden'], 403);
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-08 13:15:29 +03:30
|
|
|
$previousValue = $setting->exists ? (string) ($setting->value ?? '') : null;
|
|
|
|
|
|
2026-06-21 21:44:44 +03:30
|
|
|
$setting->value = $request->value;
|
|
|
|
|
$setting->description = $request->description;
|
|
|
|
|
if ($request->has('is_active')) {
|
|
|
|
|
$setting->is_active = filter_var($request->is_active, FILTER_VALIDATE_BOOLEAN) ? 1 : 0;
|
|
|
|
|
} elseif ($isNew) {
|
|
|
|
|
$setting->is_active = 1;
|
|
|
|
|
}
|
|
|
|
|
$setting->title_fa = $request->title_fa ?? '';
|
|
|
|
|
if (Setting::hasTypeColumn()) {
|
|
|
|
|
$setting->type = $request->type ?? '';
|
|
|
|
|
}
|
|
|
|
|
$setting->save();
|
|
|
|
|
|
2026-07-08 13:15:29 +03:30
|
|
|
if (
|
|
|
|
|
$request->key === 'price_channel_message_text'
|
|
|
|
|
&& $previousValue !== (string) ($request->value ?? '')
|
|
|
|
|
) {
|
|
|
|
|
dispatch(function () {
|
|
|
|
|
app(PriceChannelBroadcastService::class)->broadcastAfterTemplateChange();
|
|
|
|
|
})->afterResponse();
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-21 21:44:44 +03:30
|
|
|
if ($isNew && $isSystemDeploy) {
|
|
|
|
|
$projectName = Setting::where('key', 'name_fa')->value('value') ?: 'پروژه';
|
|
|
|
|
$settingTitle = trim($request->title_fa ?? '') ?: $request->key;
|
|
|
|
|
$this->Send(
|
|
|
|
|
self::NEW_SETTING_NOTIFY_MOBILE,
|
|
|
|
|
"تنظیم جدید: {$settingTitle}\nپروژه: {$projectName}",
|
|
|
|
|
false
|
|
|
|
|
);
|
2026-02-09 14:09:19 +03:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return response()->json($setting);
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-21 21:44:44 +03:30
|
|
|
private function normalizeAdminMobile(?string $mobile): string
|
|
|
|
|
{
|
|
|
|
|
$digits = preg_replace('/\D+/', '', (string) $mobile) ?? '';
|
|
|
|
|
if (str_starts_with($digits, '98') && strlen($digits) >= 12) {
|
|
|
|
|
$digits = substr($digits, 2);
|
|
|
|
|
}
|
|
|
|
|
if (strlen($digits) === 11 && str_starts_with($digits, '0')) {
|
|
|
|
|
$digits = substr($digits, 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $digits;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function isDeployAdminUser(): bool
|
|
|
|
|
{
|
|
|
|
|
$user = auth('user')->user() ?? auth()->user();
|
|
|
|
|
if (!$user) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->normalizeAdminMobile($user->mobile ?? '') === self::DEPLOY_ADMIN_MOBILE_SUFFIX;
|
|
|
|
|
}
|
2026-07-19 15:53:05 +03:30
|
|
|
|
|
|
|
|
private function isPanelManagedKey(string $key): bool
|
|
|
|
|
{
|
|
|
|
|
return in_array($key, self::PANEL_MANAGED_KEYS, true);
|
|
|
|
|
}
|
2026-02-09 14:09:19 +03:30
|
|
|
}
|