2022-10-02 18:38:41 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2023-09-08 11:00:28 +00:00
|
|
|
use Illuminate\Support\Collection;
|
2023-03-14 11:01:36 +00:00
|
|
|
use Spatie\Sluggable\HasSlug;
|
|
|
|
use Spatie\Sluggable\SlugOptions;
|
2022-10-02 18:38:41 +00:00
|
|
|
use Stevebauman\Purify\Facades\Purify;
|
|
|
|
|
|
|
|
class Template extends Model
|
|
|
|
{
|
2024-02-23 10:54:12 +00:00
|
|
|
use HasFactory;
|
|
|
|
use HasSlug;
|
2022-10-02 18:38:41 +00:00
|
|
|
|
|
|
|
protected $fillable = [
|
2023-10-13 10:11:03 +00:00
|
|
|
'creator_id',
|
2022-10-02 18:38:41 +00:00
|
|
|
'name',
|
|
|
|
'slug',
|
|
|
|
'description',
|
2023-09-08 11:00:28 +00:00
|
|
|
'short_description',
|
2022-10-02 18:38:41 +00:00
|
|
|
'image_url',
|
|
|
|
'structure',
|
|
|
|
'questions',
|
2023-09-08 11:00:28 +00:00
|
|
|
'publicly_listed',
|
|
|
|
'industries',
|
|
|
|
'types',
|
2024-02-23 10:54:12 +00:00
|
|
|
'related_templates',
|
2022-10-02 18:38:41 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
protected $casts = [
|
|
|
|
'structure' => 'array',
|
|
|
|
'questions' => 'array',
|
2023-09-08 11:00:28 +00:00
|
|
|
'industries' => 'array',
|
|
|
|
'types' => 'array',
|
|
|
|
'related_templates' => 'array',
|
|
|
|
'created_at' => 'datetime',
|
|
|
|
'updated_at' => 'datetime',
|
|
|
|
];
|
|
|
|
|
|
|
|
protected $attributes = [
|
|
|
|
'publicly_listed' => false,
|
2022-10-02 18:38:41 +00:00
|
|
|
];
|
|
|
|
|
2023-10-13 10:11:03 +00:00
|
|
|
protected $appends = [
|
|
|
|
'share_url',
|
|
|
|
];
|
|
|
|
|
|
|
|
public function getShareUrlAttribute()
|
|
|
|
{
|
2024-01-11 13:07:27 +00:00
|
|
|
return front_url('/form-templates/'.$this->slug);
|
2023-10-13 10:11:03 +00:00
|
|
|
}
|
|
|
|
|
2022-10-02 18:38:41 +00:00
|
|
|
public function setDescriptionAttribute($value)
|
|
|
|
{
|
|
|
|
// Strip out unwanted html
|
|
|
|
$this->attributes['description'] = Purify::clean($value);
|
|
|
|
}
|
2023-03-14 11:01:36 +00:00
|
|
|
|
2023-09-08 11:00:28 +00:00
|
|
|
public function scopePubliclyListed($query)
|
|
|
|
{
|
|
|
|
return $this->where('publicly_listed', true);
|
|
|
|
}
|
|
|
|
|
2023-03-14 11:01:36 +00:00
|
|
|
/**
|
|
|
|
* Config/options
|
|
|
|
*/
|
|
|
|
public function getSlugOptions(): SlugOptions
|
|
|
|
{
|
|
|
|
return SlugOptions::create()
|
|
|
|
->doNotGenerateSlugsOnUpdate()
|
|
|
|
->generateSlugsFrom('name')
|
|
|
|
->saveSlugsTo('slug');
|
|
|
|
}
|
2023-09-08 11:00:28 +00:00
|
|
|
|
|
|
|
public function getTypes(): Collection
|
|
|
|
{
|
|
|
|
return self::getAllTypes()->filter(function ($type) {
|
|
|
|
return in_array($type['slug'], $this->types);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getIndustries(): Collection
|
|
|
|
{
|
|
|
|
return self::getAllIndustries()->filter(function ($type) {
|
|
|
|
return in_array($type['slug'], $this->industries);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getAllTypes(): Collection
|
|
|
|
{
|
|
|
|
return collect(
|
|
|
|
array_values(
|
|
|
|
json_decode(
|
|
|
|
file_get_contents(resource_path('data/forms/templates/types.json')),
|
2024-02-23 10:54:12 +00:00
|
|
|
true
|
|
|
|
)
|
2023-09-08 11:00:28 +00:00
|
|
|
)
|
|
|
|
)->values();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getAllIndustries(): Collection
|
|
|
|
{
|
|
|
|
return collect(
|
|
|
|
array_values(
|
|
|
|
json_decode(
|
|
|
|
file_get_contents(resource_path('data/forms/templates/industries.json')),
|
2024-02-23 10:54:12 +00:00
|
|
|
true
|
|
|
|
)
|
2023-09-08 11:00:28 +00:00
|
|
|
)
|
|
|
|
)->values();
|
|
|
|
}
|
2022-10-02 18:38:41 +00:00
|
|
|
}
|