1 <?php
2 3 4 5 6
7 class FileSubsites extends DataObjectDecorator {
8
9
10
11 static $default_root_folders_global = false;
12
13 function () {
14 if(!method_exists('DataObjectDecorator', 'load_extra_statics') && $this->owner->class != 'File') return null;
15 return array(
16 'has_one' => array(
17 'Subsite' => 'Subsite',
18 ),
19 );
20 }
21
22 function Link($action = null) {
23 return $this->RelativeLink($action);
24 }
25
26 function RelativeLink($action = null){
27 return '/' . $this->owner->getFilename();
28 }
29
30 function URL(){
31 return '/' . $this->owner->getFilename();
32 }
33 34 35 36
37 function alternateTreeTitle() {
38 if($this->owner->SubsiteID == 0) return " * " . $this->owner->Title;
39 else return $this->owner->Title;
40 }
41
42 43 44
45 function updateCMSFields(FieldSet &$fields) {
46 if($this->owner instanceof Folder) {
47 $sites = Subsite::accessible_sites('CMS_ACCESS_AssetAdmin');
48 $dropdownValues = ($sites) ? $sites->toDropdownMap() : array();
49 $dropdownValues[0] = 'All sites';
50 ksort($dropdownValues);
51 if($sites)$fields->addFieldToTab('Root.Details', new DropdownField("SubsiteID", "Subsite", $dropdownValues));
52 }
53 }
54
55 56 57
58 function augmentSQL(SQLQuery &$query) {
59
60 if(!$query->where || !preg_match('/\.(\'|"|`|)ID(\'|"|`|)/', $query->where[0])) {
61 if($context = DataObject::context_obj()) $subsiteID = (int) $context->SubsiteID;
62 else $subsiteID = (int) Subsite::currentSubsiteID();
63
64
65 foreach($query->from as $tableName => $info) {
66 $where = "\"$tableName\".\"SubsiteID\" IN (0, $subsiteID)";
67 $query->where[] = $where;
68 break;
69 }
70
71 $isCounting = strpos($query->select[0], 'COUNT') !== false;
72
73
74 if(!$query->delete && !$isCounting) {
75 $query->orderby = "\"SubsiteID\"" . ($query->orderby ? ', ' : '') . $query->orderby;
76 }
77 }
78 }
79
80 function onBeforeWrite() {
81 if (!$this->owner->ID && !$this->owner->SubsiteID) {
82 if (self::$default_root_folders_global) {
83 $this->owner->SubsiteID = 0;
84 } else {
85 $this->owner->SubsiteID = Subsite::currentSubsiteID();
86 }
87 }
88 }
89
90 function onAfterUpload() {
91
92 if ($this->owner->Parent()) {
93 $this->owner->SubsiteID = $this->owner->Parent()->SubsiteID;
94 } else {
95 $this->owner->SubsiteID = Subsite::currentSubsiteID();
96 }
97 $this->owner->write();
98 }
99
100 function canEdit() {
101
102 $subsiteID = Session::get('SubsiteID');
103 if($subsiteID&&$subsiteID == $this->owner->SubsiteID) {
104 return true;
105 } else {
106 Session::set('SubsiteID', $this->owner->SubsiteID);
107 $access = Permission::check('CMS_ACCESS_AssetAdmin');
108 Session::set('SubsiteID', $subsiteID);
109
110 return $access;
111 }
112 }
113
114 115 116
117 function cacheKeyComponent() {
118 return 'subsite-'.Subsite::currentSubsiteID();
119 }
120
121 }
122
123
124
[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the
Silverstripe Forums to ask development related questions.
-