Issues (2)

code/ImageEditPartialCacheBustExtension.php (1 issue)

1
<?php
2
3
class ImageEditPartialCacheBustExtension extends DataExtension {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
4
5
	/*
6
	This is intended to be added to Image as an extension.  When an image is edited  checks are made
7
	against a configurable list of classes mapped to the image ID field.  If the image ID field value matches
8
	the  ID of the image being refocussed, the DataObject's LastEdited field is updated.
9
	*/
10
	public function onAfterWrite() {
11
		$config = Config::inst();
12
		$sitetreeclasses = $config->get('ImageEditCacheBust', 'SiteTree');
13
		$dataobjectclasses = $config->get('ImageEditCacheBust', 'DataObject');
14
		$stages = $config->get('ImageEditCacheBust', 'Stages');
15
16
		if ($sitetreeclasses) {
17
			// deal with SiteTree first
18
			foreach ($sitetreeclasses as $clazz => $idfield) {
19
				$instanceofclass = Injector::inst()->create($clazz);
20
				$objectsWithImage = $instanceofclass::get()->filter($idfield, $this->owner->ID);
21
				foreach ($objectsWithImage as $objectWithImage) {
22
					foreach ($stages as $stage) {
23
						$suffix = '_'.$stage;
24
						$suffix = str_replace('_Stage', '', $suffix);
25
						$sql = "UPDATE `SiteTree{$suffix}` SET LastEdited=NOW() where ID=".$objectWithImage->ID;
26
						DB::query($sql);
27
					}
28
				}
29
			}
30
		}
31
32
		
33
34
		if ($dataobjectclasses) {
35
			// deal with SiteTree first
36
			foreach ($dataobjectclasses as $clazz => $idfield) {
37
				$instanceofclass = Injector::inst()->create($clazz);
38
				$objectsWithImage = $instanceofclass::get()->filter($idfield, $this->owner->ID);
39
				foreach ($objectsWithImage as $objectWithImage) {
40
						$sql = "UPDATE `$clazz` SET LastEdited=NOW() where ID=".$objectWithImage->ID;
41
						DB::query($sql);
42
					
43
				}
44
			}
45
		}
46
	}
47
}