2012-03-25 18:35:01 +02:00
|
|
|
<?php
|
2016-09-22 16:38:29 +02:00
|
|
|
|
2017-05-24 12:32:05 +02:00
|
|
|
namespace SilverStripe\Subsites\Forms;
|
|
|
|
|
2017-08-28 11:52:32 +02:00
|
|
|
use SilverStripe\Control\Controller;
|
2016-09-22 16:38:29 +02:00
|
|
|
use SilverStripe\Control\HTTPRequest;
|
|
|
|
use SilverStripe\Forms\TreeDropdownField;
|
2018-01-30 04:21:45 +01:00
|
|
|
use SilverStripe\Subsites\Model\Subsite;
|
2017-05-24 15:26:28 +02:00
|
|
|
use SilverStripe\View\Requirements;
|
2017-10-06 00:09:58 +02:00
|
|
|
use SilverStripe\Subsites\State\SubsiteState;
|
2017-05-24 12:32:05 +02:00
|
|
|
|
2012-03-25 18:35:01 +02:00
|
|
|
/**
|
|
|
|
* Wraps around a TreedropdownField to add ability for temporary
|
|
|
|
* switching of subsite sessions.
|
2017-05-24 13:36:04 +02:00
|
|
|
*
|
2012-03-25 18:35:01 +02:00
|
|
|
* @package subsites
|
|
|
|
*/
|
2017-05-24 15:26:28 +02:00
|
|
|
class SubsitesTreeDropdownField extends TreeDropdownField
|
|
|
|
{
|
2017-06-01 15:57:53 +02:00
|
|
|
private static $allowed_actions = [
|
2017-05-24 15:26:28 +02:00
|
|
|
'tree'
|
2017-06-01 15:57:53 +02:00
|
|
|
];
|
2017-05-24 13:36:04 +02:00
|
|
|
|
2018-01-30 04:21:45 +01:00
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
protected $subsiteId = 0;
|
2017-05-24 13:36:04 +02:00
|
|
|
|
2017-09-07 07:01:49 +02:00
|
|
|
/**
|
|
|
|
* Extra HTML classes
|
|
|
|
*
|
|
|
|
* @skipUpgrade
|
|
|
|
* @var string[]
|
|
|
|
*/
|
2017-06-01 15:57:53 +02:00
|
|
|
protected $extraClasses = ['SubsitesTreeDropdownField'];
|
2017-05-24 13:36:04 +02:00
|
|
|
|
2017-06-01 15:57:53 +02:00
|
|
|
public function Field($properties = [])
|
2017-05-24 15:26:28 +02:00
|
|
|
{
|
|
|
|
$html = parent::Field($properties);
|
2017-05-24 13:36:04 +02:00
|
|
|
|
2018-01-26 04:09:42 +01:00
|
|
|
Requirements::javascript('silverstripe/subsites:javascript/SubsitesTreeDropdownField.js');
|
2017-05-24 13:36:04 +02:00
|
|
|
|
2017-05-24 15:26:28 +02:00
|
|
|
return $html;
|
|
|
|
}
|
2017-05-24 13:36:04 +02:00
|
|
|
|
2018-01-30 04:21:45 +01:00
|
|
|
/**
|
|
|
|
* Sets the subsite ID to use when generating the tree
|
|
|
|
*
|
|
|
|
* @param int $id
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setSubsiteId($id)
|
2017-05-24 15:26:28 +02:00
|
|
|
{
|
2018-01-30 04:21:45 +01:00
|
|
|
$this->subsiteId = $id;
|
|
|
|
return $this;
|
2017-05-24 15:26:28 +02:00
|
|
|
}
|
2017-05-24 13:36:04 +02:00
|
|
|
|
2018-01-30 04:21:45 +01:00
|
|
|
/**
|
|
|
|
* Get the subsite ID to use when generating the tree
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getSubsiteId()
|
2017-05-24 15:26:28 +02:00
|
|
|
{
|
2018-01-30 04:21:45 +01:00
|
|
|
return $this->subsiteId;
|
2017-05-24 15:26:28 +02:00
|
|
|
}
|
2017-05-24 13:36:04 +02:00
|
|
|
|
2018-01-30 04:21:45 +01:00
|
|
|
/**
|
|
|
|
* Get the CMS tree with the provided subsite ID applied to the state
|
|
|
|
*
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
2017-05-29 13:42:42 +02:00
|
|
|
public function tree(HTTPRequest $request)
|
2017-05-24 15:26:28 +02:00
|
|
|
{
|
2018-01-30 04:21:45 +01:00
|
|
|
// Detect subsite ID from the request
|
|
|
|
if ($request->getVar($this->getName() . '_SubsiteID')) {
|
|
|
|
$this->setSubsiteId($request->getVar($this->getName() . '_SubsiteID'));
|
|
|
|
}
|
|
|
|
|
|
|
|
$results = SubsiteState::singleton()->withState(function (SubsiteState $newState) use ($request) {
|
|
|
|
$newState->setSubsiteId($this->getSubsiteId());
|
2017-10-06 00:09:58 +02:00
|
|
|
return parent::tree($request);
|
|
|
|
});
|
2017-05-24 13:36:04 +02:00
|
|
|
|
2017-05-24 15:26:28 +02:00
|
|
|
return $results;
|
|
|
|
}
|
2017-05-24 13:36:04 +02:00
|
|
|
}
|