@php $isEdit = isset($template); $oldType = old('type', $template->type ?? 'sms'); $oldWhatsappType = old('whatsapp_type', $template->whatsapp_type ?? 'official'); $oldTitle = old('title', $template->title ?? ''); $oldMetaName = old('meta_name', $template->meta_name ?? ''); $oldLanguage = old('language', $template->language ?? 'en_US'); $oldCategory = old('category', $template->category ?? 'UTILITY'); $oldSubject = old('subject', $template->subject ?? ''); $oldContent = old('content', $template->content ?? ''); $existingExamples = []; $existingBtnUrlText = old('btn_url_text', ''); $existingBtnUrl = old('btn_url', ''); $existingBtnCallText = old('btn_call_text', ''); $existingBtnCallPhone = old('btn_call_phone', ''); if ($isEdit && !empty($template->components) && is_array($template->components)) { foreach ($template->components as $component) { if (($component['type'] ?? null) === 'BODY') { $bodyText = $component['example']['body_text'][0] ?? []; if (is_array($bodyText)) { foreach ($bodyText as $i => $val) { $existingExamples[$i + 1] = $val; } } } if (($component['type'] ?? null) === 'BUTTONS' && !empty($component['buttons']) && is_array($component['buttons'])) { foreach ($component['buttons'] as $btn) { if (($btn['type'] ?? null) === 'URL') { $existingBtnUrlText = old('btn_url_text', $btn['text'] ?? ''); $existingBtnUrl = old('btn_url', $btn['url'] ?? ''); } if (($btn['type'] ?? null) === 'PHONE_NUMBER') { $existingBtnCallText = old('btn_call_text', $btn['text'] ?? ''); $existingBtnCallPhone = old('btn_call_phone', $btn['phone_number'] ?? ''); } } } } } preg_match_all('/\{\{(\d+)\}\}/', $oldContent, $matches); $detectedVariables = collect($matches[1] ?? []) ->map(function ($v) { return (int) $v; }) ->unique() ->sort() ->values() ->all(); @endphp