-
Notifications
You must be signed in to change notification settings - Fork 243
Expand file tree
/
Copy pathHasVersioning.php
More file actions
249 lines (224 loc) · 7.18 KB
/
HasVersioning.php
File metadata and controls
249 lines (224 loc) · 7.18 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
<?php
namespace ProcessMaker\Traits;
use Facades\ProcessMaker\Helpers\CachedSchema;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\QueryException;
use Illuminate\Support\Facades\Schema;
use ProcessMaker\Facades\WorkflowManager;
use ProcessMaker\Models\Process;
use ProcessMaker\Models\ProcessMakerModel;
use ProcessMaker\Models\ProcessRequest;
use ProcessMaker\Models\ProcessVersion;
trait HasVersioning
{
/**
* The "boot" method of HasVersioning.
*/
public static function bootHasVersioning()
{
static::addGlobalScope('published', function (Builder $builder) {
$builder->published();
});
// Save a version every time the model is saved.
static::saved([static::class, 'saveNewVersion']);
}
/**
* Get the published for the model.
*/
public function scopePublished($query)
{
$query->whereHas('versions', function ($query) {
// Avoid migration errors when 'draft' column does not exist.
$hasDraftColumn = CachedSchema::hasColumn($query->getModel()->getTable(), 'draft');
$query->when($hasDraftColumn, function ($query) {
$query->where('draft', false);
});
});
}
/**
* Save a new version of a model
*/
public static function saveNewVersion(Model $model)
{
$model->saveVersion();
}
/**
* Save a published version of the model.
*/
public function saveVersion()
{
$attributes = $this->getModelAttributes();
$version = $this->versions()->create($attributes);
// Delete draft version.
try {
$this->deleteDraft($this->alternative ?? null);
} catch(QueryException $e) {
// Skip delete if the screen version is used in a process.
}
return $version;
}
/**
* Save a draft version of the model
*
* @param string|null $alternative Overwrite the alternative identifier.
*
* @return ProcessMakerModel
*/
public function saveDraft(string $alternative = null)
{
$attributes = $this->getModelAttributes();
$attributes['draft'] = true;
if ($this->hasAlternative()) {
$alternative = $alternative ?: $this->alternative;
$attributes['alternative'] = $alternative;
$processVersion = ProcessVersion::where('process_id', $this->id)
->where('alternative', $attributes['alternative'])
->where('draft', 1)
->first();
if ($processVersion) {
$attributes['stages'] = $processVersion->stages;
}
}
return $this->versions()->updateOrCreate(
[
'draft' => true,
...(
$this->hasAlternative()
? ['alternative' => $alternative]
: []
),
],
$attributes
);
}
public function deleteDraft(string $alternative = null)
{
$this->versions()->draft()
->when(
$this->hasAlternative() && $alternative,
function ($query) use ($alternative) {
$query->where('alternative', $alternative);
}
)->delete();
}
private function getModelAttributes(): array
{
$attributes = $this->attributesToArray();
foreach ($this->hidden as $field) {
$attributes[$field] = $this->$field;
}
unset($attributes['id'],
$attributes['uuid'],
$attributes['updated_at'],
$attributes['created_at'],
$attributes['has_timer_start_events'],
$attributes['projects']);
return $attributes;
}
/**
* Get the latest version of the executable artifact (screen, script)
*
* @param string $alternative The alternative version identifier. [A|B]
*
* @return ProcessMakerModel
*/
public function getLatestVersion(string $alternative = 'A')
{
return $this->versions()
->when(
$this->hasAlternative(),
function ($query) use ($alternative) {
$query->where('alternative', $alternative);
}
)
->orderBy('id', 'desc')
->published()
->first();
}
/**
* Get the latest version of the model (process, screen, script)
*
* @return ProcessMakerModel The published version of the artifact
*/
public function getPublishedVersion(array $data)
{
$implementation = WorkflowManager::NAYRA_PUBLISHER . get_class($this);
$existsCustomPublisher = $implementation
&& WorkflowManager::existsServiceImplementation($implementation);
if ($existsCustomPublisher) {
$response = WorkflowManager::runServiceImplementation(
$implementation,
$data,
[
'process' => $this,
],
);
return $response['publishedVersion'];
}
return $this->getLatestVersion();
}
/**
* Get the latest version of artifact (screen, script)
*
* @param string $alternative The alternative version identifier. [A|B]
*
* @return ProcessMakerModel
*/
public function getDraftOrPublishedLatestVersion(string $alternative = 'A')
{
return $this->versions()
->when(
$this->hasAlternative(),
function ($query) use ($alternative) {
$query->where('alternative', $alternative);
}
)
->orderBy('id', 'desc')
->first();
}
/**
* Get the latest version of artifact (screen, script)
*
* @param string $alternative The alternative version identifier. [A|B]
*
* @return ProcessMakerModel
*/
public function getDraftVersion(string $alternative = null)
{
$alternative = $alternative ?: ($this->alternative ?? null);
return $this->versions()
->when(
$this->hasAlternative(),
function ($query) use ($alternative) {
$query->where('alternative', $alternative);
}
)
->draft()
->first();
}
/**
* Return the version that was active when the task's request was started
*
* @param ProcessRequest $processRequest The process request object.
*
* @return ProcessMakerModel
*/
public function versionFor(ProcessRequest $processRequest = null)
{
// Skip version locking for now
// It will be re-added with more configurable options in a future version
return $processRequest && $processRequest->process && $this instanceof Process
? $processRequest->process->getLatestVersion($processRequest->processVersion->alternative)
: $this->getLatestVersion();
}
/**
* Returns true if the model has alternatives.
*
* @return false
*/
public function hasAlternative()
{
return false;
}
}