File: /home/xedaptot/be.naniguide.com/app/Model/SystemTemplate.php
<?php
namespace App\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Library\Contracts\TemplateSubjectInterface;
use App\Library\Traits\HasUid;
use App\Services\TemplateService;
class SystemTemplate extends Model implements TemplateSubjectInterface
{
use HasFactory;
use HasUid;
protected $fillable = [
'alias'
];
public function template()
{
return $this->belongsTo(Template::class, 'template_id', 'id');
}
public function hasTemplate(): bool
{
return !is_null($this->template_id) && $this->template()->exists();
}
public function getThumbUrl(): string
{
return $this->template
? $this->template->getThumbUrl()
: url('images/placeholder.jpg');
}
public function setTemplate($template, string $name): void
{
TemplateService::for($this)->setTemplate($template, $name);
}
public function removeTemplate(): void
{
TemplateService::for($this)->removeTemplate();
}
public function setTemplateContent($content, $json): void
{
$this->template->updateContent($json, $content);
}
public function getTemplateContent(): string
{
return $this->template->content;
}
public static function scopeSearch($query, $request)
{
$query->orderBy($request->sort_order ?: 'created_at', $request->sort_direction ?: 'desc');
}
public static function findByAlias($alias)
{
// get latest layout
return self::whereAlias($alias)->first();
}
public static function findByAliasOrFail($alias)
{
$row = self::findByAlias($alias);
if (is_null($row)) {
throw new \LogicException("Template '{$alias}' does not exist, it is likely a misconfiguration with your data. Re-run SystemTemplateSeeder to restore.");
}
return $row;
}
public static function getPricingPage()
{
return self::findByAlias('pricing_page');
}
public static function getPricingPageSystemTemplate()
{
return self::where('alias', 'pricing_page')->first();
}
public static function getPricingListCollection()
{
$plans = \App\Model\Plan::getAvailablePlans();
return $plans->map(function ($plan) {
return [
'id' => $plan->uid,
'title' => $plan->name,
'description' => $plan->description,
'price' => format_price($plan->price, $plan->currency->format, true),
'currency' => $plan->currency->code,
'duration' => $plan->displayFrequencyTime(),
];
});
}
public function deleteAndCleanup()
{
TemplateService::for($this)->deleteSubjectAndTemplate();
}
public static function getTermsPage()
{
return self::findByAlias('terms_and_policy_page');
}
public static function getInvoiceTemplate()
{
return self::findByAlias('invoice_template');
}
/**
* Tags that MUST appear in the template content for each alias.
* Used by SystemTemplateController::save() to block saving an incomplete template.
*/
public static function requiredTagsByAlias(): array
{
return [
'invoice_template' => ['{{ ITEMS }}'],
];
}
public function requiredTags(): array
{
return self::requiredTagsByAlias()[$this->alias] ?? [];
}
/**
* Variable tags available in the builder sidebar for this system template.
* Returns null when the template has no tag panel (most system templates).
*/
public function getTags(): ?array
{
return match ($this->alias) {
'invoice_template' => \App\Model\Invoice::getTags(),
default => null,
};
}
public static function getDefaultCampaignApiTemplate()
{
return self::whereAlias('default_campaign_api_template')->first();
}
}