2008-09-26 04:22:51 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* ReadonlyField with added toggle-capabilities - will preview the first sentence of the contained text-value,
|
|
|
|
* and show the full content by a javascript-switch.
|
|
|
|
*
|
|
|
|
* Caution: Strips HTML-encoding for the preview.
|
|
|
|
* @package forms
|
|
|
|
* @subpackage fields-dataless
|
|
|
|
*/
|
|
|
|
|
|
|
|
class ToggleField extends ReadonlyField {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var $labelMore string Text shown as a link to see the full content of the field
|
|
|
|
*/
|
|
|
|
public $labelMore;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var $labelLess string Text shown as a link to see the partial view of the field content
|
|
|
|
*/
|
|
|
|
public $labelLess;
|
|
|
|
|
|
|
|
/**
|
2008-10-02 02:34:16 +02:00
|
|
|
* @see Text
|
|
|
|
* @var $truncateMethod string (FirstSentence|FirstParagraph)
|
2008-09-26 04:22:51 +02:00
|
|
|
*/
|
|
|
|
public $truncateMethod = 'FirstSentence';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var $truncateChars int Number of chars to preview (optional).
|
|
|
|
* Truncating will be applied with $truncateMethod by default.
|
|
|
|
*/
|
|
|
|
public $truncateChars;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param name The field name
|
|
|
|
* @param title The field title
|
|
|
|
* @param value The current value
|
|
|
|
*/
|
|
|
|
function __construct($name, $title = "", $value = "") {
|
|
|
|
$this->labelMore = _t('ToggleField.MORE', 'more');
|
|
|
|
$this->labelLess = _t('ToggleField.LESS', 'less');
|
|
|
|
|
|
|
|
$this->startClosed(true);
|
|
|
|
|
|
|
|
parent::__construct($name, $title, $value);
|
|
|
|
}
|
|
|
|
|
2012-04-11 07:33:36 +02:00
|
|
|
function Field($properties = array()) {
|
2008-09-26 04:22:51 +02:00
|
|
|
$content = '';
|
|
|
|
|
2009-11-21 03:29:59 +01:00
|
|
|
Requirements::javascript(SAPPHIRE_DIR . "/thirdparty/prototype/prototype.js");
|
|
|
|
Requirements::javascript(SAPPHIRE_DIR . "/thirdparty/behaviour/behaviour.js");
|
ENHANCEMENT Introduced constants for system paths like /sapphire in preparation for a more flexible directory reorganisation. Instead of hardcoding your path, please use the following constants: BASE_PATH, BASE_URL, SAPPHIRE_DIR, SAPPHIRE_PATH, CMS_DIR, CMS_PATH, THIRDPARTY_DIR, THIRDPARTY_PATH, ASSETS_DIR, ASSETS_PATH, THEMES_DIR, THEMES_PATH
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@63154 467b73ca-7a2a-4603-9d3b-597d59a354a9
2008-09-27 18:02:38 +02:00
|
|
|
Requirements::javascript(SAPPHIRE_DIR . "/javascript/ToggleField.js");
|
2008-09-26 04:22:51 +02:00
|
|
|
|
|
|
|
if($this->startClosed) $this->addExtraClass('startClosed');
|
|
|
|
|
|
|
|
$valforInput = $this->value ? Convert::raw2att($this->value) : "";
|
|
|
|
$rawInput = Convert::html2raw($valforInput);
|
|
|
|
|
|
|
|
if($this->charNum) $reducedVal = substr($rawInput,0,$this->charNum);
|
2012-03-27 06:57:42 +02:00
|
|
|
else $reducedVal = DBField::create_field('Text',$rawInput)->{$this->truncateMethod}();
|
2008-09-26 04:22:51 +02:00
|
|
|
|
|
|
|
// only create togglefield if the truncated content is shorter
|
|
|
|
if(strlen($reducedVal) < strlen($rawInput)) {
|
|
|
|
$content = <<<HTML
|
|
|
|
<div class="readonly typography contentLess" style="display: none">
|
|
|
|
$reducedVal
|
|
|
|
<a href="#" class="triggerMore">$this->labelMore</a>
|
|
|
|
</div>
|
|
|
|
<div class="readonly typography contentMore">
|
|
|
|
$this->value
|
|
|
|
<a href="#" class="triggerLess">$this->labelLess</a>
|
|
|
|
</div>
|
|
|
|
<br />
|
|
|
|
<input type="hidden" name="$this->name" value="$valforInput" />
|
|
|
|
HTML;
|
|
|
|
} else {
|
|
|
|
$this->dontEscape = true;
|
|
|
|
$content = parent::Field();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $content;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines if the field should render open or closed by default.
|
|
|
|
*
|
|
|
|
* @param boolean
|
|
|
|
*/
|
|
|
|
public function startClosed($bool) {
|
|
|
|
($bool) ? $this->addExtraClass('startClosed') : $this->removeExtraClass('startClosed');
|
|
|
|
}
|
|
|
|
|
|
|
|
function Type() {
|
|
|
|
return "toggleField";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|