forked from ProcessMaker/processmaker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWizardTemplate.php
More file actions
101 lines (87 loc) · 2.97 KB
/
WizardTemplate.php
File metadata and controls
101 lines (87 loc) · 2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
namespace ProcessMaker\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Support\Facades\File;
use ProcessMaker\Models\Media;
use ProcessMaker\Traits\HasUuids;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
class WizardTemplate extends ProcessMakerModel implements HasMedia
{
use HasFactory;
use HasUuids;
use InteractsWithMedia;
protected $table = 'wizard_templates';
protected $fillable = [
'uuid',
'process_template_id',
'helper_process_id',
'name',
'description',
'media_collection',
'template_details',
'unique_template_id',
];
protected $appends = [
'template_media',
];
/**
* Get the process associated with the wizard template.
*/
public function process(): BelongsTo
{
return $this->belongsTo(Process::class, 'helper_process_id');
}
/**
* Get the process template associated with the wizard template.
*/
public function process_template(): BelongsTo
{
return $this->belongsTo(ProcessTemplates::class, 'process_template_id');
}
/**
* Filter settings with a string
*
* @param $query
*
* @param $filter string
*/
public function scopeFilter($query, $filterStr)
{
$filter = '%' . mb_strtolower($filterStr) . '%';
$query->where(function ($query) use ($filter) {
$query->where('wizard_templates.name', 'like', $filter)
->orWhere('wizard_templates.description', 'like', $filter);
});
return $query;
}
public function getTemplateMediaAttribute()
{
$mediaCollectionName = 'wt-' . $this->uuid . '-media';
$slides = $this->getMedia($mediaCollectionName, ['media_type' => 'slide']);
$slideUrls = $slides->map(function ($slide) {
return $slide->getFullUrl();
});
$iconMedia = $this->getMedia($mediaCollectionName, ['media_type' => 'icon'])->first();
$cardBackgroundMedia = $this->getMedia($mediaCollectionName, ['media_type' => 'cardBackground'])->first();
$listIconMedia = $this->getMedia($mediaCollectionName, ['media_type' => 'listIcon'])->first();
return [
'icon' => !is_null($iconMedia) ? $iconMedia->getFullUrl() : '',
'cardBackground' => !is_null($cardBackgroundMedia) ? $cardBackgroundMedia->getFullUrl() : '',
'listIcon' => !is_null($listIconMedia) ? $listIconMedia->getFullUrl() : '',
'slides' => $slideUrls,
];
}
/**
* Add files to media collection
*/
public function addFilesToMediaCollection(string $directoryPath)
{
$files = File::allFiles($directoryPath);
$collectionName = basename($directoryPath);
foreach ($files as $file) {
$this->addMedia($file->getPathname())->toMediaCollection($collectionName);
}
}
}