1 | <?php |
||
2 | |||
3 | namespace SilverStripe\Subsites\Forms; |
||
4 | |||
5 | use SilverStripe\Control\Controller; |
||
6 | use SilverStripe\Control\HTTPRequest; |
||
7 | use SilverStripe\Forms\TreeDropdownField; |
||
8 | use SilverStripe\Subsites\Model\Subsite; |
||
9 | use SilverStripe\View\Requirements; |
||
10 | use SilverStripe\Subsites\State\SubsiteState; |
||
11 | |||
12 | /** |
||
13 | * Wraps around a TreedropdownField to add ability for temporary |
||
14 | * switching of subsite sessions. |
||
15 | * |
||
16 | * @package subsites |
||
17 | */ |
||
18 | class SubsitesTreeDropdownField extends TreeDropdownField |
||
19 | { |
||
20 | private static $allowed_actions = [ |
||
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
21 | 'tree' |
||
22 | ]; |
||
23 | |||
24 | /** |
||
25 | * @var int |
||
26 | */ |
||
27 | protected $subsiteId = 0; |
||
28 | |||
29 | /** |
||
30 | * Extra HTML classes |
||
31 | * |
||
32 | * @skipUpgrade |
||
33 | * @var string[] |
||
34 | */ |
||
35 | protected $extraClasses = ['SubsitesTreeDropdownField']; |
||
36 | |||
37 | public function Field($properties = []) |
||
38 | { |
||
39 | $html = parent::Field($properties); |
||
40 | |||
41 | Requirements::javascript('silverstripe/subsites:javascript/SubsitesTreeDropdownField.js'); |
||
42 | |||
43 | return $html; |
||
44 | } |
||
45 | |||
46 | /** |
||
47 | * Sets the subsite ID to use when generating the tree |
||
48 | * |
||
49 | * @param int $id |
||
50 | * @return $this |
||
51 | */ |
||
52 | public function setSubsiteId($id) |
||
53 | { |
||
54 | $this->subsiteId = $id; |
||
55 | return $this; |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * Get the subsite ID to use when generating the tree |
||
60 | * |
||
61 | * @return int |
||
62 | */ |
||
63 | public function getSubsiteId() |
||
64 | { |
||
65 | return $this->subsiteId; |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * Get the CMS tree with the provided subsite ID applied to the state |
||
70 | * |
||
71 | * {@inheritDoc} |
||
72 | */ |
||
73 | public function tree(HTTPRequest $request) |
||
74 | { |
||
75 | // Detect subsite ID from the request |
||
76 | if ($request->getVar($this->getName() . '_SubsiteID')) { |
||
77 | $this->setSubsiteId($request->getVar($this->getName() . '_SubsiteID')); |
||
78 | } |
||
79 | |||
80 | $results = SubsiteState::singleton()->withState(function (SubsiteState $newState) use ($request) { |
||
81 | $newState->setSubsiteId($this->getSubsiteId()); |
||
82 | return parent::tree($request); |
||
83 | }); |
||
84 | |||
85 | return $results; |
||
86 | } |
||
87 | } |
||
88 |