-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAsset.php
More file actions
210 lines (183 loc) · 4.3 KB
/
Asset.php
File metadata and controls
210 lines (183 loc) · 4.3 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
<?php
/**
* @package framework
* @copyright Copyright (c) 2005-2020 The Regents of the University of California.
* @license http://opensource.org/licenses/MIT MIT
*/
namespace Qubeshub\Database;
use Hubzero\Access\Asset as Model;
/**
* Database asset helper class for permissions compatibility
*/
class Asset
{
/**
* The database model
*
* @var \Hubzero\Database\Relational|static
**/
private $model = null;
/**
* Constructs a new object, setting the model
*
* @param object $model The model to which the asset will refer
* @return void
* @since 2.0.0
**/
public function __construct($model)
{
$this->model = $model;
}
/**
* Resolves the asset id based on the default parameters and expectations
*
* @param object $model The database model to which the asset refers
* @return int
* @since 2.0.0
**/
public static function resolve($model)
{
return with(new self($model))->getId();
}
/**
* Deletes the asset entry for the provided model
*
* @param object $model The model being deleted
* @return bool
* @since 2.0.0
**/
public static function destroy($model)
{
return with(new self($model))->delete();
}
/**
* Gets the asset id for the object instance
*
* @return int
* @since 2.0.0
**/
public function getId()
{
// Check for current asset id and compute other vars
$current = $this->model->get('asset_id', null);
$parentId = $this->getAssetParentId();
$name = $this->getAssetName();
$title = $this->getAssetTitle();
$title = $title ?: $name;
// Get model for assets
$asset = Model::oneByName($name);
// Re-inject the asset id into the model
$this->model->set('asset_id', $asset->get('id'));
// Prepare the asset to be stored
$asset->set('parent_id', $parentId);
$asset->set('name', $name);
$asset->set('title', $title);
if ($this->model->assetRules instanceof \Hubzero\Access\Rules)
{
$asset->set('rules', (string)$this->model->assetRules);
}
// Specify how a new or moved node asset is inserted into the tree
if (!$this->model->get('asset_id', null) || $asset->parent_id != $parentId)
{
$parent = Model::one($parentId);
if (!$asset->saveAsLastChildOf($parent))
{
return false;
}
}
elseif (!$asset->save())
{
return false;
}
// Register an event to update the asset name once we know the model id
if ($this->model->isNew())
{
$me = $this;
\Event::listen(
function($event) use ($asset, $me)
{
$asset->set('name', $me->getAssetName());
$asset->save();
},
$this->model->getTableName() . '_new'
);
}
// Return the id
return (int)$asset->get('id');
}
/**
* Deletes the current asset entry
*
* @return bool
* @since 2.0.0
**/
public function delete()
{
$asset = Model::oneByName($this->getAssetName());
if ($asset->get('id'))
{
if (!$asset->destroy())
{
return false;
}
}
return true;
}
/**
* Computes the (distinct) name of the asset
*
* @return string
* @since 2.0.0
*/
private function getAssetName()
{
// @FIXME: this scheme won't always work...
// * namespace isn't always defined, at which point the model name is the namespace
// * namespace might be something like time_hub, which should become time.hub
// * non-integer ids will fail
return strtolower("com_{$this->model->getNamespace()}.{$this->model->getModelName()}.") . (int)$this->model->getPkValue();
}
/**
* Gets the title to use for the asset table
*
* @return string
* @since 2.0.0
*/
private function getAssetTitle()
{
// @FIXME: need a way to override this
return $this->model->name;
}
/**
* Gets the parent asset id for the record
*
* @return int
* @since 2.0.0
*/
private function getAssetParentId()
{
$assetId = null;
// Build the query to get the asset id for the parent category
$asset = Model::oneByName('com_' . $this->model->getNamespace());
if ($asset->get('id'))
{
$assetId = (int)$asset->get('id');
}
return ($assetId) ? $assetId : $this->getRootId();
}
/**
* Gets the root asset id from the #__assets table, defaulting to 1
*
* @return int
* @since 2.0.0
*/
private function getRootId()
{
$rootId = Model::getRootId();
if (empty($rootId))
{
$rootId = 1;
}
return $rootId;
}
}