1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LaravelFlare\Flare\Admin\Models\Traits; |
4
|
|
|
|
5
|
|
|
trait ModelCloning |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* Method fired before the Clone action is undertaken. |
9
|
|
|
* |
10
|
|
|
* @return |
11
|
|
|
*/ |
12
|
|
|
protected function beforeClone() |
13
|
|
|
{ |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Clone Action. |
18
|
|
|
* |
19
|
|
|
* Fires off beforeClone(), doClone() and afterClone() |
20
|
|
|
* |
21
|
|
|
* @param int $modelitemId |
22
|
|
|
* |
23
|
|
|
* @return |
24
|
|
|
*/ |
25
|
|
|
public function clone($modelitemId) |
|
|
|
|
26
|
|
|
{ |
27
|
|
|
event(new BeforeClone($this)); |
28
|
|
|
|
29
|
|
|
$this->beforeClone(); |
30
|
|
|
|
31
|
|
|
$this->doClone(); |
32
|
|
|
|
33
|
|
|
$this->afterClone(); |
34
|
|
|
|
35
|
|
|
event(new AfterClone($this)); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* The actual Clone action, which does all of hte pre-processing |
40
|
|
|
* required before we are able to perform the save() function. |
41
|
|
|
* |
42
|
|
|
* @return |
43
|
|
|
*/ |
44
|
|
|
private function doClone() |
45
|
|
|
{ |
46
|
|
|
if (is_callable(array('self', 'save'))) { |
47
|
|
|
$this->find($modelitemId)->replicate($this->excludeOnClone())->save(); |
|
|
|
|
48
|
|
|
|
49
|
|
|
event(new ModelClone($this)); |
50
|
|
|
|
51
|
|
|
return; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
throw new WriteableException('For a Model to be Creatable the ModelAdmin must have the Save method implemented using the ModelWriting trait', 1); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Method fired after the Clone action is complete. |
59
|
|
|
* |
60
|
|
|
* @return |
61
|
|
|
*/ |
62
|
|
|
protected function afterClone() |
63
|
|
|
{ |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* An Array of Fields to Exclude on Clone. |
68
|
|
|
* |
69
|
|
|
* When cloning a Model ceratin data might need to be skipped |
70
|
|
|
* either because it is irrelevant (such as datestamps) or |
71
|
|
|
* because it is primary or unique data in the database. |
72
|
|
|
* |
73
|
|
|
* @return array |
74
|
|
|
*/ |
75
|
|
|
public function excludeOnClone() |
76
|
|
|
{ |
77
|
|
|
return [ |
78
|
|
|
$this->model->getKeyName(), |
79
|
|
|
$this->model->getCreatedAtColumn(), |
80
|
|
|
$this->model->getUpdatedAtColumn(), |
81
|
|
|
]; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* |
86
|
|
|
*/ |
87
|
|
|
public function unCloneableColumns() |
88
|
|
|
{ |
89
|
|
|
// We will use this: SHOW INDEXES FROM users WHERE Non_unique != 0 AND Column_name != 'id' |
90
|
|
|
// To determine if there are unCloneable Columns and if that is the case, we will render |
91
|
|
|
// the clone view and validate the unCloneable Columns to be unique before saving. |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.