1 <?php
2
3 4 5 6 7 8 9 10
11 class AssociatedFolderDecorator extends SiteTreeDecorator {
12
13 function () {
14 return array(
15 'has_one' => array(
16 'AssociatedFolder' => 'Folder'
17 )
18 );
19 }
20
21 22 23 24 25 26
27 static $createFolders = true;
28
29
30 31 32 33 34
35 protected static $defaultRootFolderName = "page-files";
36
37 38 39 40 41
42 static function setDefaultRootFolderName($folderName) {
43 AssociatedFolderDecorator::$defaultRootFolderName = $folderName;
44 }
45
46 47 48 49 50
51 static function getDefaultRootFolderName() {
52 return AssociatedFolderDecorator::$defaultRootFolderName;
53 }
54
55 56 57 58 59
60 function onAfterWrite() {
61 parent::onAfterWrite();
62
63 $folder = null;
64
65
66 if ($this->owner->AssociatedFolderID != 0) {
67 $folder = DataObject::get_by_id('Folder', $this->owner->AssociatedFolderID);
68 }
69
70 if (!$folder || !$folder->ID) {
71
72 if ((!class_exists('WebylonImportAdmin') || Director::urlParam('Controller') != 'ImportTask') && self::$createFolders) {
73 $this->createAssociatedFolder();
74 }
75 } else {
76 if (!file_exists($folder->getFullPath())) {
77
78 mkdir($folder->getFullPath(), Filesystem::$folder_create_mask, true);
79 }
80 if ($folder->Title != $this->getAssociatedFolderTitle()) {
81 $folder->Title = $this->getAssociatedFolderTitle();
82 $folder->write();
83 }
84 }
85 }
86
87 function onBeforeDuplicate($page) {
88 $page->AssociatedFolderID = 0;
89 }
90
91 function getAssociatedFolderName() {
92 $hash = ceil($this->owner->ID / 100);
93 return $this->getDefaultRootFolderName() . '/' . $hash . '/' . $this->owner->ID;
94 }
95
96 function getAssociatedFolderTitle() {
97 return $this->owner->ID . ' - ' . $this->owner->Title;
98 }
99
100 function getAssociatedFolderParentName() {
101 $hash = ceil($this->owner->ID / 100);
102 return $this->getDefaultRootFolderName() . '/' . $hash ;
103 }
104
105 function getAssociatedFolderParentTitle() {
106 $hash = ceil($this->owner->ID / 100);
107 return sprintf('%d01-%d00', $hash-1, $hash);
108 }
109
110 111 112 113 114
115 function createAssociatedFolder() {
116 if (!self::$createFolders) {
117 return false;
118 }
119
120 if (!$this->owner->ID || (!$this->owner->ExistsOnLive && $this->owner->IsDeletedFromStage)) {
121 return false;
122 }
123 $hashFolder = Folder::findOrMake($this->getAssociatedFolderParentName());
124 $hashTitle = $this->getAssociatedFolderParentTitle();
125 if ($hashFolder->Title != $hashTitle) {
126 $hashFolder->Title = $hashTitle;
127 $hashFolder->write();
128 }
129
130 $associatedFolder = Folder::findOrMake($this->getAssociatedFolderName());
131 $associatedFolder->Title = $this->getAssociatedFolderTitle();
132 $associatedFolder->write();
133
134 $this->owner->AssociatedFolderID = $associatedFolder->ID;
135 $this->owner->writeWithoutVersion();
136 return $associatedFolder;
137 }
138
139 140 141 142 143
144 function getAssociatedFolder() {
145 if ($this->owner->AssociatedFolderID && $this->owner->AssociatedFolder()->ID) {
146 $folder = $this->owner->AssociatedFolder();
147 } else {
148 $folder = $this->owner->createAssociatedFolder();
149 }
150 return $folder;
151 }
152
153 function onAfterDelete() {
154 parent::onAfterDelete();
155
156 if ($this->owner->IsDeletedFromStage && !$this->owner->ExistsOnLive) {
157 if ($folder = $this->owner->AssociatedFolder()) {
158 $folder->delete();
159 }
160 }
161 }
162
163 }
164
[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.
-