-
Notifications
You must be signed in to change notification settings - Fork 3
/
Version.php
154 lines (114 loc) · 3.12 KB
/
Version.php
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
<?php
/**
* @category Bundle
* @package Version
* @license MIT License <http://www.opensource.org/licenses/mit>
*
* @see https://github.com/Aboalarm/Versions-for-Laravel
*/
class Version {
static $table = "versions";
/**
* Get the table name, where we will save the versions
* from the Laravel config
*
* @return string
*/
public static function getVersionsTable() {
$cfg = Config::get('version');
return $cfg['table'];
}
/**
* Save a version of the current state of an object.
* Will automatically check for duplicates and won't save them.
* You can specify a name for the version if you want to.
*
* @param string $src
* @param string $name
* @return bool
*/
public static function add($src, $name = '') {
$data = json_encode($src->original);
$table = strtolower(get_class($src));
$obj_id = isset($src->original['id']) ? $src->original['id'] : false;
if($obj_id)
{
$creation_date = date('Y-m-d H:i:s');
try
{
return DB::table(Version::getVersionsTable())
->insert(array(
'data' => $data,
'object_table' => $table,
'object_id' => $obj_id,
'name' => $name,
'hash' => md5($data),
'created_at' => $creation_date,
'updated_at' => $creation_date,
));
}
catch (Exception $e)
{
return false;
}
}
return false;
}
/**
* Loads a specific saved version by its primary key
*
* @param int $version_id
* @return object
*/
public static function load($version_id) {
$data = DB::table(Version::getVersionsTable())->where_id($version_id)->first();
$data->data = json_decode($data->data);
return new $data->object_table($data->data);
}
/**
* Get all versions of an object
*
* @param string $obj
* @return array
*/
public static function all($obj) {
return DB::table(Version::getVersionsTable())->where('object_id', '=', $obj->attributes['id'])->where('object_table', '=', strtolower(get_class($obj)))->order_by('updated_at', 'desc')->get();
}
/**
* How many versions are saved for a given object?
*
* @param string $obj
* @return int
*/
public static function count($obj) {
return DB::table(Version::getVersionsTable())->where('object_id', '=', $obj->attributes['id'])->where('object_table', '=', strtolower(get_class($obj)))->count();
}
/**
* Retrieve the most recent version of an object
*
* @param string $obj
* @return object
*/
public static function latest($obj) {
return DB::table(Version::getVersionsTable())->where('object_id', '=', $obj->attributes['id'])->where('object_table', '=', strtolower(get_class($obj)))->order_by('created_at', 'desc')->first();
}
/**
* Delete a version of an object
*
* @param int $version_id
* @return bool
*/
public static function delete($version_id) {
return DB::table(Version::getVersionsTable())->delete($version_id);
}
/**
* Delete all versions of an object
*
* @param string $obj
* @return bool
*/
public static function deleteAll($obj) {
return DB::table(Version::getVersionsTable())->where('object_id', '=', $obj->attributes['id'])->where('object_table', '=', strtolower(get_class($obj)))->delete();
}
}
?>