forked from ProcessMaker/processmaker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTemplate.php
More file actions
144 lines (117 loc) · 4.26 KB
/
Template.php
File metadata and controls
144 lines (117 loc) · 4.26 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
namespace ProcessMaker\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Validation\Rule;
use ProcessMaker\Models\Process;
use ProcessMaker\Models\ProcessCategory;
use ProcessMaker\Models\ProcessMakerModel;
use ProcessMaker\Models\Screen;
use ProcessMaker\Models\ScreenCategory;
use ProcessMaker\Models\TemplateCategory;
use ProcessMaker\Templates\ProcessTemplate;
use ProcessMaker\Templates\ScreenTemplate;
use ProcessMaker\Traits\Exportable;
use ProcessMaker\Traits\HasCategories;
use ProcessMaker\Traits\HasUuids;
use ProcessMaker\Traits\HideSystemResources;
use ProcessMaker\Traits\SerializeToIso8601;
class Template extends ProcessMakerModel
{
use HasFactory;
use HasUuids;
use HideSystemResources;
use HasCategories;
use Exportable;
use SerializeToIso8601;
protected $guarded = [
'id',
'uuid',
'created_at',
'updated_at',
];
private $templateType;
protected array $types = [
'process' => [Process::class, ProcessTemplate::class, ProcessCategory::class, 'process_category_id', 'process_templates'],
'update-assets' => [Process::class, ProcessTemplate::class, ProcessCategory::class, 'process_category_id', 'process_templates'],
'screen' => [Screen::class, ScreenTemplate::class, ScreenCategory::class, 'screen_category_id', 'screen_templates'],
];
public function index(String $type, Request $request)
{
return (new $this->types[$type][1])->index($request);
}
public function show(String $type, Request $request)
{
return (new $this->types[$type][1])->show($request);
}
public function store(string $type, Request $request)
{
return (new $this->types[$type][1])->save($request);
}
public function updateTemplateManifest(string $type, int $processId, $request)
{
return (new $this->types[$type][1])->updateTemplateManifest($processId, $request);
}
public function updateTemplate(string $type, Request $request)
{
return (new $this->types[$type][1])->updateTemplate($request);
}
public function updateTemplateConfigs(string $type, Request $request)
{
return (new $this->types[$type][1])->updateTemplateConfigs($request);
}
public function create(string $type, Request $request)
{
return (new $this->types[$type][1])->create($request);
}
public function deleteTemplate(string $type, Request $request)
{
$id = $request->id;
return (new $this->types[$type][1])->destroy($id);
}
public function importTemplate(string $type, Request $request)
{
return (new $this->types[$type][1])->importTemplate($request);
}
public function publishTemplate(string $type, Request $request)
{
return (new $this->types[$type][1])->publishTemplate($request);
}
public function deleteMediaImages(string $type, Request $request)
{
return (new $this->types[$type][1])->deleteMediaImages($request);
}
public function user()
{
return $this->belongsTo(User::class, 'user_id');
}
public function categories()
{
$categoryClass = $this->types[$type][2];
$categoryColumn = $this->types[$type][3];
return $this->belongsTo($categoryClass, $categoryColumn)->withDefault();
}
public static function rules($existing = null, $table = null)
{
$user = Auth::user();
$unique = Rule::unique($table ?? 'templates')->where(function ($query) use ($user, $table) {
if ($table === 'screen_templates') {
return $query->where('user_id', $user->id)->where('is_public', 0);
}
})->ignore($existing);
return [
'name' => ['required', $unique, 'alpha_spaces', 'max:255'],
'description' => 'required',
'version' => ['required', 'regex:/^[0-9.]+$/'],
];
}
public function checkForExistingTemplates($type, $request)
{
$result = (new $this->types[$type][1])->existingTemplate($request);
if (!is_null($result)) {
return ['id' => $result['id'], 'name' => $result['name'], 'owner_id' => $result['owner_id']];
}
return null;
}
}