2007-10-22 01:05:46 +02:00
|
|
|
<?php
|
2015-04-27 05:08:56 +02:00
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
namespace SilverStripe\Forms;
|
|
|
|
|
|
|
|
use SilverStripe\View\Requirements;
|
|
|
|
|
2007-10-22 01:05:46 +02:00
|
|
|
/**
|
2012-06-13 06:20:07 +02:00
|
|
|
* Allows visibility of a group of fields to be toggled.
|
2007-10-22 01:05:46 +02:00
|
|
|
*/
|
|
|
|
class ToggleCompositeField extends CompositeField {
|
2012-06-13 06:20:07 +02:00
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
protected $startClosed = true;
|
|
|
|
|
2007-10-22 01:05:46 +02:00
|
|
|
/**
|
2015-04-27 05:08:56 +02:00
|
|
|
* @var int
|
2007-10-22 01:05:46 +02:00
|
|
|
*/
|
2012-06-13 06:20:07 +02:00
|
|
|
protected $headingLevel = 3;
|
|
|
|
|
2015-04-27 05:08:56 +02:00
|
|
|
/**
|
2016-04-20 06:00:49 +02:00
|
|
|
* @inheritdoc
|
|
|
|
*
|
2015-04-27 05:08:56 +02:00
|
|
|
* @param string $name
|
|
|
|
* @param string $title
|
|
|
|
* @param array|FieldList $children
|
|
|
|
*/
|
2012-06-13 06:20:07 +02:00
|
|
|
public function __construct($name, $title, $children) {
|
2007-10-22 01:05:46 +02:00
|
|
|
parent::__construct($children);
|
2016-07-06 06:34:09 +02:00
|
|
|
$this->setName($name);
|
|
|
|
$this->setTitle($title);
|
2007-10-22 01:05:46 +02:00
|
|
|
}
|
2012-06-13 06:20:07 +02:00
|
|
|
|
2015-04-27 05:08:56 +02:00
|
|
|
/**
|
2016-04-20 06:00:49 +02:00
|
|
|
* @inheritdoc
|
|
|
|
*
|
2015-04-27 05:08:56 +02:00
|
|
|
* @param array $properties
|
2016-06-03 10:51:02 +02:00
|
|
|
* @return string
|
2015-04-27 05:08:56 +02:00
|
|
|
*/
|
2012-04-11 08:07:55 +02:00
|
|
|
public function FieldHolder($properties = array()) {
|
2015-04-27 05:08:56 +02:00
|
|
|
$context = $this;
|
|
|
|
|
|
|
|
if(count($properties)) {
|
|
|
|
$context = $this->customise($properties);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $context->renderWith($this->getTemplates());
|
2012-06-13 06:20:07 +02:00
|
|
|
}
|
|
|
|
|
2015-04-27 05:08:56 +02:00
|
|
|
/**
|
2016-04-20 06:00:49 +02:00
|
|
|
* @inheritdoc
|
|
|
|
*
|
|
|
|
* @return array
|
2015-04-27 05:08:56 +02:00
|
|
|
*/
|
2012-06-13 06:20:07 +02:00
|
|
|
public function getAttributes() {
|
2015-04-27 05:08:56 +02:00
|
|
|
$attributes = array(
|
2016-08-19 00:51:35 +02:00
|
|
|
'id' => $this->ID(),
|
2015-04-27 05:08:56 +02:00
|
|
|
'class' => $this->extraClass(),
|
|
|
|
);
|
|
|
|
|
2012-06-13 06:20:07 +02:00
|
|
|
if($this->getStartClosed()) {
|
2015-04-27 05:08:56 +02:00
|
|
|
$attributes['class'] .= ' ss-toggle ss-toggle-start-closed';
|
2012-06-13 06:20:07 +02:00
|
|
|
} else {
|
2015-04-27 05:08:56 +02:00
|
|
|
$attributes['class'] .= ' ss-toggle';
|
2012-06-13 06:20:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return array_merge(
|
|
|
|
$this->attributes,
|
2015-04-27 05:08:56 +02:00
|
|
|
$attributes
|
2012-06-13 06:20:07 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2007-10-22 01:05:46 +02:00
|
|
|
/**
|
2012-06-13 06:20:07 +02:00
|
|
|
* @return bool
|
2007-10-22 01:05:46 +02:00
|
|
|
*/
|
2012-06-13 06:20:07 +02:00
|
|
|
public function getStartClosed() {
|
|
|
|
return $this->startClosed;
|
2007-10-22 01:05:46 +02:00
|
|
|
}
|
2012-06-13 06:20:07 +02:00
|
|
|
|
2007-10-22 01:05:46 +02:00
|
|
|
/**
|
2015-04-27 05:08:56 +02:00
|
|
|
* Controls whether the field is open or closed by default. By default the field is closed.
|
|
|
|
*
|
|
|
|
* @param bool $startClosed
|
2012-06-13 06:20:07 +02:00
|
|
|
*
|
2015-04-27 05:08:56 +02:00
|
|
|
* @return $this
|
2007-10-22 01:05:46 +02:00
|
|
|
*/
|
2015-04-27 05:08:56 +02:00
|
|
|
public function setStartClosed($startClosed) {
|
|
|
|
$this->startClosed = (bool) $startClosed;
|
|
|
|
|
2012-07-15 21:28:09 +02:00
|
|
|
return $this;
|
2012-06-13 06:20:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getHeadingLevel() {
|
2007-10-22 01:05:46 +02:00
|
|
|
return $this->headingLevel;
|
|
|
|
}
|
|
|
|
|
2012-06-13 06:20:07 +02:00
|
|
|
/**
|
2015-04-27 05:08:56 +02:00
|
|
|
* @param int $headingLevel
|
|
|
|
*
|
|
|
|
* @return $this
|
2012-06-13 06:20:07 +02:00
|
|
|
*/
|
2015-04-27 05:08:56 +02:00
|
|
|
public function setHeadingLevel($headingLevel) {
|
2016-04-20 06:16:59 +02:00
|
|
|
$this->headingLevel = (int) $headingLevel;
|
2015-04-27 05:08:56 +02:00
|
|
|
|
2012-07-15 21:28:09 +02:00
|
|
|
return $this;
|
2007-10-22 01:05:46 +02:00
|
|
|
}
|
|
|
|
}
|