1 | <?php |
||
26 | class AssetControlExtension extends \DataExtension |
||
27 | { |
||
28 | |||
29 | /** |
||
30 | * When archiving versioned dataobjects, should assets be archived with them? |
||
31 | * If false, assets will be deleted when the dataobject is archived. |
||
32 | * If true, assets will be instead moved to the protected store, and can be |
||
33 | * restored when the dataobject is restored from archive. |
||
34 | * |
||
35 | * Note that this does not affect the archiving of the actual database record in any way, |
||
36 | * only the physical file. |
||
37 | * |
||
38 | * Unversioned dataobjects will ignore this option and always delete attached |
||
39 | * assets on deletion. |
||
40 | * |
||
41 | * @config |
||
42 | * @var bool |
||
43 | */ |
||
44 | private static $keep_archived_assets = false; |
||
45 | |||
46 | /** |
||
47 | * Ensure that deletes records remove their underlying file assets, without affecting |
||
48 | * other staged records. |
||
49 | */ |
||
50 | public function onAfterDelete() |
||
51 | { |
||
52 | // Prepare blank manipulation |
||
53 | $manipulations = new AssetManipulationList(); |
||
54 | |||
55 | // Add all assets for deletion |
||
56 | $this->addAssetsFromRecord($manipulations, $this->owner, AssetManipulationList::STATE_DELETED); |
||
|
|||
57 | |||
58 | // Whitelist assets that exist in other stages |
||
59 | $this->addAssetsFromOtherStages($manipulations); |
||
60 | |||
61 | // Apply visibility rules based on the final manipulation |
||
62 | $this->processManipulation($manipulations); |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * Ensure that changes to records flush overwritten files, and update the visibility |
||
67 | * of other assets. |
||
68 | */ |
||
69 | public function onBeforeWrite() |
||
92 | |||
93 | /** |
||
94 | * Check default state of this record |
||
95 | * |
||
96 | * @param DataObject $record |
||
97 | * @return string One of AssetManipulationList::STATE_* constants |
||
98 | */ |
||
99 | protected function getRecordState($record) { |
||
115 | |||
116 | /** |
||
117 | * Given a set of asset manipulations, trigger any necessary publish, protect, or |
||
118 | * delete actions on each asset. |
||
119 | * |
||
120 | * @param AssetManipulationList $manipulations |
||
121 | */ |
||
122 | protected function processManipulation(AssetManipulationList $manipulations) |
||
142 | |||
143 | /** |
||
144 | * Checks all stages other than the current stage, and check the visibility |
||
145 | * of assets attached to those records. |
||
146 | * |
||
147 | * @param AssetManipulationList $manipulation Set of manipulations to add assets to |
||
148 | */ |
||
149 | protected function addAssetsFromOtherStages(AssetManipulationList $manipulation) |
||
177 | |||
178 | /** |
||
179 | * Given a record, add all assets it contains to the given manipulation. |
||
180 | * State can be declared for this record, otherwise the underlying DataObject |
||
181 | * will be queried for canView() to see if those assets are public |
||
182 | * |
||
183 | * @param AssetManipulationList $manipulation Set of manipulations to add assets to |
||
184 | * @param DataObject $record Record |
||
185 | * @param string $state One of AssetManipulationList::STATE_* constant values. |
||
186 | */ |
||
187 | protected function addAssetsFromRecord(AssetManipulationList $manipulation, DataObject $record, $state) |
||
200 | |||
201 | /** |
||
202 | * Return a list of all tuples attached to this dataobject |
||
203 | * Note: Variants are excluded |
||
204 | * |
||
205 | * @param DataObject $record to search |
||
206 | * @return array |
||
207 | */ |
||
208 | protected function findAssets(DataObject $record) |
||
209 | { |
||
210 | // Search for dbfile instances |
||
211 | $files = array(); |
||
212 | foreach ($record->db() as $field => $db) { |
||
213 | // Extract assets from this database field |
||
214 | list($dbClass) = explode('(', $db); |
||
215 | if (!is_a($dbClass, 'DBFile', true)) { |
||
216 | continue; |
||
217 | } |
||
218 | |||
219 | // Omit variant and merge with set |
||
220 | $next = $record->dbObject($field)->getValue(); |
||
221 | unset($next['Variant']); |
||
222 | if ($next) { |
||
223 | $files[] = $next; |
||
224 | } |
||
225 | } |
||
226 | |||
227 | // De-dupe |
||
228 | return array_map("unserialize", array_unique(array_map("serialize", $files))); |
||
229 | } |
||
230 | |||
231 | /** |
||
232 | * Determine if {@see Versioned) extension rules should be applied to this object |
||
233 | * |
||
234 | * @return bool |
||
235 | */ |
||
236 | protected function isVersioned() |
||
237 | { |
||
238 | return $this->owner->has_extension('Versioned') && class_exists('Versioned'); |
||
239 | } |
||
240 | |||
241 | /** |
||
242 | * Delete all assets in the tuple list |
||
243 | * |
||
244 | * @param array $assets |
||
245 | */ |
||
246 | protected function deleteAll($assets) |
||
247 | { |
||
248 | if (empty($assets)) { |
||
249 | return; |
||
250 | } |
||
251 | $store = $this->getAssetStore(); |
||
252 | foreach ($assets as $asset) { |
||
253 | $store->delete($asset['Filename'], $asset['Hash']); |
||
254 | } |
||
255 | } |
||
256 | |||
257 | /** |
||
258 | * Move all assets in the list to the public store |
||
259 | * |
||
260 | * @param array $assets |
||
261 | */ |
||
262 | protected function publishAll($assets) |
||
273 | |||
274 | /** |
||
275 | * Move all assets in the list to the protected store |
||
276 | * |
||
277 | * @param array $assets |
||
278 | */ |
||
279 | protected function protectAll($assets) |
||
289 | |||
290 | /** |
||
291 | * @return AssetStore |
||
292 | */ |
||
293 | protected function getAssetStore() |
||
297 | } |
||
298 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.