@@ -28,210 +28,210 @@ |
||
28 | 28 | */ |
29 | 29 | class Lumberjack extends SiteTreeExtension |
30 | 30 | { |
31 | - /** |
|
32 | - * Loops through subclasses of the owner (intended to be SiteTree) and checks if they've been hidden. |
|
33 | - * |
|
34 | - * @return array |
|
35 | - **/ |
|
36 | - public function getExcludedSiteTreeClassNames() |
|
37 | - { |
|
38 | - $classes = array(); |
|
39 | - $siteTreeClasses = $this->owner->allowedChildren(); |
|
40 | - |
|
41 | - foreach ($siteTreeClasses as $class) { |
|
42 | - if (Config::inst()->get($class, 'show_in_sitetree') === false) { |
|
43 | - $classes[$class] = $class; |
|
44 | - } |
|
45 | - } |
|
46 | - |
|
47 | - return $classes; |
|
48 | - } |
|
49 | - |
|
50 | - /** |
|
51 | - * This is responsible for adding the child pages tab and gridfield. |
|
52 | - * |
|
53 | - * @param FieldList $fields |
|
54 | - */ |
|
55 | - public function updateCMSFields(FieldList $fields) |
|
56 | - { |
|
57 | - $excluded = $this->owner->getExcludedSiteTreeClassNames(); |
|
58 | - if (!empty($excluded)) { |
|
59 | - $pages = $this->getLumberjackPagesForGridfield($excluded); |
|
60 | - $gridField = GridField::create( |
|
61 | - 'ChildPages', |
|
62 | - $this->getLumberjackTitle(), |
|
63 | - $pages, |
|
64 | - $this->getLumberjackGridFieldConfig() |
|
65 | - ); |
|
66 | - |
|
67 | - $tab = Tab::create('ChildPages', $this->getLumberjackTitle(), $gridField); |
|
68 | - $fields->insertAfter($tab, 'Main'); |
|
69 | - } |
|
70 | - } |
|
71 | - |
|
72 | - /** |
|
73 | - * Return children in the stage site. |
|
74 | - * |
|
75 | - * @param bool $showAll Include all of the elements, even those not shown in the menus. Only applicable when |
|
76 | - * extension is applied to {@link SiteTree}. |
|
77 | - * @return DataList |
|
78 | - */ |
|
79 | - public function stageChildren($showAll = false) |
|
80 | - { |
|
81 | - $config = $this->owner->config(); |
|
82 | - $hideFromHierarchy = $config->get('hide_from_hierarchy'); |
|
83 | - $hideFromCMSTree = $config->get('hide_from_cms_tree'); |
|
84 | - $baseClass = $this->owner->baseClass(); |
|
85 | - |
|
86 | - $staged = $baseClass::get() |
|
87 | - ->filter('ParentID', (int)$this->owner->ID) |
|
88 | - ->exclude('ID', (int)$this->owner->ID); |
|
89 | - |
|
90 | - if ($hideFromHierarchy) { |
|
91 | - $staged = $staged->exclude('ClassName', $hideFromHierarchy); |
|
92 | - } |
|
93 | - |
|
94 | - if ($hideFromCMSTree && $this->showingCMSTree()) { |
|
95 | - $staged = $staged->exclude('ClassName', $hideFromCMSTree); |
|
96 | - } |
|
97 | - |
|
98 | - if (!$showAll && DataObject::getSchema()->fieldSpec($this->owner, 'ShowInMenus')) { |
|
99 | - $staged = $staged->filter('ShowInMenus', 1); |
|
100 | - } |
|
101 | - |
|
102 | - $this->owner->extend("augmentStageChildren", $staged, $showAll); |
|
103 | - $staged = $this->excludeSiteTreeClassNames($staged); |
|
104 | - return $staged; |
|
105 | - } |
|
106 | - |
|
107 | - /** |
|
108 | - * Excludes any hidden owner subclasses. Note that the returned DataList will be a different |
|
109 | - * instance from the original. |
|
110 | - * |
|
111 | - * @param DataList $list |
|
112 | - * @return DataList |
|
113 | - */ |
|
114 | - protected function excludeSiteTreeClassNames($list) |
|
115 | - { |
|
116 | - $classNames = $this->owner->getExcludedSiteTreeClassNames(); |
|
117 | - if ($this->shouldFilter() && count($classNames)) { |
|
118 | - // Filter the SiteTree |
|
119 | - $list = $list->exclude('ClassName', $classNames); |
|
120 | - } |
|
121 | - return $list; |
|
122 | - } |
|
123 | - |
|
124 | - /** |
|
125 | - * Return children in the live site, if it exists. |
|
126 | - * |
|
127 | - * @param bool $showAll Include all of the elements, even those not shown in the menus. Only |
|
128 | - * applicable when extension is applied to {@link SiteTree}. |
|
129 | - * @param bool $onlyDeletedFromStage Only return items that have been deleted from stage |
|
130 | - * @return DataList |
|
131 | - * @throws Exception |
|
132 | - */ |
|
133 | - public function liveChildren($showAll = false, $onlyDeletedFromStage = false) |
|
134 | - { |
|
135 | - if (!$this->owner->hasExtension(Versioned::class)) { |
|
136 | - throw new Exception('Hierarchy->liveChildren() only works with Versioned extension applied'); |
|
137 | - } |
|
138 | - |
|
139 | - $config = $this->owner->config(); |
|
140 | - $hideFromHierarchy = $config->get('hide_from_hierarchy'); |
|
141 | - $hideFromCMSTree = $config->get('hide_from_cms_tree'); |
|
142 | - $baseClass = $this->owner->baseClass(); |
|
143 | - |
|
144 | - $children = $baseClass::get() |
|
145 | - ->filter('ParentID', (int)$this->owner->ID) |
|
146 | - ->exclude('ID', (int)$this->owner->ID) |
|
147 | - ->setDataQueryParam(array( |
|
148 | - 'Versioned.mode' => $onlyDeletedFromStage ? 'stage_unique' : 'stage', |
|
149 | - 'Versioned.stage' => 'Live' |
|
150 | - )); |
|
151 | - |
|
152 | - if ($hideFromHierarchy) { |
|
153 | - $children = $children->exclude('ClassName', $hideFromHierarchy); |
|
154 | - } |
|
155 | - |
|
156 | - if ($hideFromCMSTree && $this->showingCMSTree()) { |
|
157 | - $children = $children->exclude('ClassName', $hideFromCMSTree); |
|
158 | - } |
|
159 | - |
|
160 | - if (!$showAll && DataObject::getSchema()->fieldSpec($this->owner, 'ShowInMenus')) { |
|
161 | - $children = $children->filter('ShowInMenus', 1); |
|
162 | - } |
|
163 | - $children = $this->excludeSiteTreeClassNames($children); |
|
164 | - |
|
165 | - return $children; |
|
166 | - } |
|
167 | - |
|
168 | - /** |
|
169 | - * This returns the title for the tab and GridField. This can be overwritten |
|
170 | - * in the owner class. |
|
171 | - * |
|
172 | - * @return string |
|
173 | - */ |
|
174 | - protected function getLumberjackTitle() |
|
175 | - { |
|
176 | - if (method_exists($this->owner, 'getLumberjackTitle')) { |
|
177 | - return $this->owner->getLumberjackTitle(); |
|
178 | - } |
|
179 | - |
|
180 | - return _t('Lumberjack.TabTitle', 'Child Pages'); |
|
181 | - } |
|
182 | - |
|
183 | - /** |
|
184 | - * This returns the gird field config for the lumberjack gridfield. |
|
185 | - * |
|
186 | - * @return GridFieldConfig_Lumberjack |
|
187 | - */ |
|
188 | - protected function getLumberjackGridFieldConfig() |
|
189 | - { |
|
190 | - if (method_exists($this->owner, 'getLumberjackGridFieldConfig')) { |
|
191 | - return $this->owner->getLumberjackGridFieldConfig(); |
|
192 | - } |
|
193 | - |
|
194 | - return GridFieldConfig_Lumberjack::create(); |
|
195 | - } |
|
196 | - |
|
197 | - /** |
|
198 | - * Checks if we're on a controller where we should filter. ie. Are we loading the SiteTree? |
|
199 | - * NB: This only checks the current controller. See https://github.com/silverstripe/silverstripe-lumberjack/pull/60 |
|
200 | - * for a discussion around this. |
|
201 | - * |
|
202 | - * @return bool |
|
203 | - */ |
|
204 | - protected function shouldFilter() |
|
205 | - { |
|
206 | - $controller = Controller::curr(); |
|
207 | - |
|
208 | - // relevant only for CMS |
|
209 | - if (!($controller instanceof LeftAndMain)) { |
|
210 | - return false; |
|
211 | - } |
|
212 | - |
|
213 | - return in_array($controller->getAction(), [ |
|
214 | - 'index', 'show', 'treeview', 'listview', 'getsubtree' |
|
215 | - ]); |
|
216 | - } |
|
217 | - |
|
218 | - /** |
|
219 | - * Returns list of pages for the CMS gridfield |
|
220 | - * |
|
221 | - * This also allows the owner class to override this method, e.g. to provide custom ordering. |
|
222 | - * |
|
223 | - * @var array $excluded List of class names excluded from the SiteTree |
|
224 | - * @return DataList |
|
225 | - */ |
|
226 | - public function getLumberjackPagesForGridfield($excluded = array()) |
|
227 | - { |
|
228 | - if (method_exists($this->owner, 'getLumberjackPagesForGridfield')) { |
|
229 | - return $this->owner->getLumberjackPagesForGridfield($excluded); |
|
230 | - } |
|
231 | - |
|
232 | - return SiteTree::get()->filter([ |
|
233 | - 'ParentID' => $this->owner->ID, |
|
234 | - 'ClassName' => $excluded, |
|
235 | - ]); |
|
236 | - } |
|
31 | + /** |
|
32 | + * Loops through subclasses of the owner (intended to be SiteTree) and checks if they've been hidden. |
|
33 | + * |
|
34 | + * @return array |
|
35 | + **/ |
|
36 | + public function getExcludedSiteTreeClassNames() |
|
37 | + { |
|
38 | + $classes = array(); |
|
39 | + $siteTreeClasses = $this->owner->allowedChildren(); |
|
40 | + |
|
41 | + foreach ($siteTreeClasses as $class) { |
|
42 | + if (Config::inst()->get($class, 'show_in_sitetree') === false) { |
|
43 | + $classes[$class] = $class; |
|
44 | + } |
|
45 | + } |
|
46 | + |
|
47 | + return $classes; |
|
48 | + } |
|
49 | + |
|
50 | + /** |
|
51 | + * This is responsible for adding the child pages tab and gridfield. |
|
52 | + * |
|
53 | + * @param FieldList $fields |
|
54 | + */ |
|
55 | + public function updateCMSFields(FieldList $fields) |
|
56 | + { |
|
57 | + $excluded = $this->owner->getExcludedSiteTreeClassNames(); |
|
58 | + if (!empty($excluded)) { |
|
59 | + $pages = $this->getLumberjackPagesForGridfield($excluded); |
|
60 | + $gridField = GridField::create( |
|
61 | + 'ChildPages', |
|
62 | + $this->getLumberjackTitle(), |
|
63 | + $pages, |
|
64 | + $this->getLumberjackGridFieldConfig() |
|
65 | + ); |
|
66 | + |
|
67 | + $tab = Tab::create('ChildPages', $this->getLumberjackTitle(), $gridField); |
|
68 | + $fields->insertAfter($tab, 'Main'); |
|
69 | + } |
|
70 | + } |
|
71 | + |
|
72 | + /** |
|
73 | + * Return children in the stage site. |
|
74 | + * |
|
75 | + * @param bool $showAll Include all of the elements, even those not shown in the menus. Only applicable when |
|
76 | + * extension is applied to {@link SiteTree}. |
|
77 | + * @return DataList |
|
78 | + */ |
|
79 | + public function stageChildren($showAll = false) |
|
80 | + { |
|
81 | + $config = $this->owner->config(); |
|
82 | + $hideFromHierarchy = $config->get('hide_from_hierarchy'); |
|
83 | + $hideFromCMSTree = $config->get('hide_from_cms_tree'); |
|
84 | + $baseClass = $this->owner->baseClass(); |
|
85 | + |
|
86 | + $staged = $baseClass::get() |
|
87 | + ->filter('ParentID', (int)$this->owner->ID) |
|
88 | + ->exclude('ID', (int)$this->owner->ID); |
|
89 | + |
|
90 | + if ($hideFromHierarchy) { |
|
91 | + $staged = $staged->exclude('ClassName', $hideFromHierarchy); |
|
92 | + } |
|
93 | + |
|
94 | + if ($hideFromCMSTree && $this->showingCMSTree()) { |
|
95 | + $staged = $staged->exclude('ClassName', $hideFromCMSTree); |
|
96 | + } |
|
97 | + |
|
98 | + if (!$showAll && DataObject::getSchema()->fieldSpec($this->owner, 'ShowInMenus')) { |
|
99 | + $staged = $staged->filter('ShowInMenus', 1); |
|
100 | + } |
|
101 | + |
|
102 | + $this->owner->extend("augmentStageChildren", $staged, $showAll); |
|
103 | + $staged = $this->excludeSiteTreeClassNames($staged); |
|
104 | + return $staged; |
|
105 | + } |
|
106 | + |
|
107 | + /** |
|
108 | + * Excludes any hidden owner subclasses. Note that the returned DataList will be a different |
|
109 | + * instance from the original. |
|
110 | + * |
|
111 | + * @param DataList $list |
|
112 | + * @return DataList |
|
113 | + */ |
|
114 | + protected function excludeSiteTreeClassNames($list) |
|
115 | + { |
|
116 | + $classNames = $this->owner->getExcludedSiteTreeClassNames(); |
|
117 | + if ($this->shouldFilter() && count($classNames)) { |
|
118 | + // Filter the SiteTree |
|
119 | + $list = $list->exclude('ClassName', $classNames); |
|
120 | + } |
|
121 | + return $list; |
|
122 | + } |
|
123 | + |
|
124 | + /** |
|
125 | + * Return children in the live site, if it exists. |
|
126 | + * |
|
127 | + * @param bool $showAll Include all of the elements, even those not shown in the menus. Only |
|
128 | + * applicable when extension is applied to {@link SiteTree}. |
|
129 | + * @param bool $onlyDeletedFromStage Only return items that have been deleted from stage |
|
130 | + * @return DataList |
|
131 | + * @throws Exception |
|
132 | + */ |
|
133 | + public function liveChildren($showAll = false, $onlyDeletedFromStage = false) |
|
134 | + { |
|
135 | + if (!$this->owner->hasExtension(Versioned::class)) { |
|
136 | + throw new Exception('Hierarchy->liveChildren() only works with Versioned extension applied'); |
|
137 | + } |
|
138 | + |
|
139 | + $config = $this->owner->config(); |
|
140 | + $hideFromHierarchy = $config->get('hide_from_hierarchy'); |
|
141 | + $hideFromCMSTree = $config->get('hide_from_cms_tree'); |
|
142 | + $baseClass = $this->owner->baseClass(); |
|
143 | + |
|
144 | + $children = $baseClass::get() |
|
145 | + ->filter('ParentID', (int)$this->owner->ID) |
|
146 | + ->exclude('ID', (int)$this->owner->ID) |
|
147 | + ->setDataQueryParam(array( |
|
148 | + 'Versioned.mode' => $onlyDeletedFromStage ? 'stage_unique' : 'stage', |
|
149 | + 'Versioned.stage' => 'Live' |
|
150 | + )); |
|
151 | + |
|
152 | + if ($hideFromHierarchy) { |
|
153 | + $children = $children->exclude('ClassName', $hideFromHierarchy); |
|
154 | + } |
|
155 | + |
|
156 | + if ($hideFromCMSTree && $this->showingCMSTree()) { |
|
157 | + $children = $children->exclude('ClassName', $hideFromCMSTree); |
|
158 | + } |
|
159 | + |
|
160 | + if (!$showAll && DataObject::getSchema()->fieldSpec($this->owner, 'ShowInMenus')) { |
|
161 | + $children = $children->filter('ShowInMenus', 1); |
|
162 | + } |
|
163 | + $children = $this->excludeSiteTreeClassNames($children); |
|
164 | + |
|
165 | + return $children; |
|
166 | + } |
|
167 | + |
|
168 | + /** |
|
169 | + * This returns the title for the tab and GridField. This can be overwritten |
|
170 | + * in the owner class. |
|
171 | + * |
|
172 | + * @return string |
|
173 | + */ |
|
174 | + protected function getLumberjackTitle() |
|
175 | + { |
|
176 | + if (method_exists($this->owner, 'getLumberjackTitle')) { |
|
177 | + return $this->owner->getLumberjackTitle(); |
|
178 | + } |
|
179 | + |
|
180 | + return _t('Lumberjack.TabTitle', 'Child Pages'); |
|
181 | + } |
|
182 | + |
|
183 | + /** |
|
184 | + * This returns the gird field config for the lumberjack gridfield. |
|
185 | + * |
|
186 | + * @return GridFieldConfig_Lumberjack |
|
187 | + */ |
|
188 | + protected function getLumberjackGridFieldConfig() |
|
189 | + { |
|
190 | + if (method_exists($this->owner, 'getLumberjackGridFieldConfig')) { |
|
191 | + return $this->owner->getLumberjackGridFieldConfig(); |
|
192 | + } |
|
193 | + |
|
194 | + return GridFieldConfig_Lumberjack::create(); |
|
195 | + } |
|
196 | + |
|
197 | + /** |
|
198 | + * Checks if we're on a controller where we should filter. ie. Are we loading the SiteTree? |
|
199 | + * NB: This only checks the current controller. See https://github.com/silverstripe/silverstripe-lumberjack/pull/60 |
|
200 | + * for a discussion around this. |
|
201 | + * |
|
202 | + * @return bool |
|
203 | + */ |
|
204 | + protected function shouldFilter() |
|
205 | + { |
|
206 | + $controller = Controller::curr(); |
|
207 | + |
|
208 | + // relevant only for CMS |
|
209 | + if (!($controller instanceof LeftAndMain)) { |
|
210 | + return false; |
|
211 | + } |
|
212 | + |
|
213 | + return in_array($controller->getAction(), [ |
|
214 | + 'index', 'show', 'treeview', 'listview', 'getsubtree' |
|
215 | + ]); |
|
216 | + } |
|
217 | + |
|
218 | + /** |
|
219 | + * Returns list of pages for the CMS gridfield |
|
220 | + * |
|
221 | + * This also allows the owner class to override this method, e.g. to provide custom ordering. |
|
222 | + * |
|
223 | + * @var array $excluded List of class names excluded from the SiteTree |
|
224 | + * @return DataList |
|
225 | + */ |
|
226 | + public function getLumberjackPagesForGridfield($excluded = array()) |
|
227 | + { |
|
228 | + if (method_exists($this->owner, 'getLumberjackPagesForGridfield')) { |
|
229 | + return $this->owner->getLumberjackPagesForGridfield($excluded); |
|
230 | + } |
|
231 | + |
|
232 | + return SiteTree::get()->filter([ |
|
233 | + 'ParentID' => $this->owner->ID, |
|
234 | + 'ClassName' => $excluded, |
|
235 | + ]); |
|
236 | + } |
|
237 | 237 | } |