2015-08-21 04:01:10 +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.
|
|
|
|
*
|
|
|
|
* @deprecated 3.1 Use custom javascript with a ReadonlyField.
|
2016-01-06 00:34:58 +01:00
|
|
|
*
|
2015-08-21 04:01:10 +02:00
|
|
|
* 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;
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2015-08-21 04:01:10 +02:00
|
|
|
/**
|
2016-01-06 00:34:58 +01:00
|
|
|
* @var $labelLess string Text shown as a link to see the partial view of the field content
|
2015-08-21 04:01:10 +02:00
|
|
|
*/
|
|
|
|
public $labelLess;
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2015-08-21 04:01:10 +02:00
|
|
|
/**
|
|
|
|
* @see Text
|
|
|
|
* @var $truncateMethod string (FirstSentence|FirstParagraph)
|
|
|
|
*/
|
|
|
|
public $truncateMethod = 'FirstSentence';
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2015-08-21 04:01:10 +02:00
|
|
|
/**
|
2016-01-06 00:34:58 +01:00
|
|
|
* @var $truncateChars int Number of chars to preview (optional).
|
|
|
|
* Truncating will be applied with $truncateMethod by default.
|
2015-08-21 04:01:10 +02:00
|
|
|
*/
|
|
|
|
public $truncateChars;
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2016-04-20 05:00:38 +02:00
|
|
|
/**
|
|
|
|
* @var null|int
|
|
|
|
*/
|
|
|
|
public $charNum;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
public $startClosed;
|
|
|
|
|
2015-08-21 04:01:10 +02:00
|
|
|
/**
|
|
|
|
* @param name The field name
|
|
|
|
* @param title The field title
|
|
|
|
* @param value The current value
|
|
|
|
*/
|
2016-04-20 05:00:38 +02:00
|
|
|
public function __construct($name, $title = '', $value = '') {
|
2015-08-21 04:01:10 +02:00
|
|
|
Deprecation::notice('4.0', 'Use custom javascript with a ReadOnlyField');
|
2016-04-20 05:00:38 +02:00
|
|
|
|
2015-08-21 04:01:10 +02:00
|
|
|
$this->labelMore = _t('ToggleField.MORE', 'more');
|
|
|
|
$this->labelLess = _t('ToggleField.LESS', 'less');
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2015-08-21 04:01:10 +02:00
|
|
|
$this->startClosed(true);
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2015-08-21 04:01:10 +02:00
|
|
|
parent::__construct($name, $title, $value);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function Field($properties = array()) {
|
|
|
|
Requirements::javascript(FRAMEWORK_DIR . '/thirdparty/jquery/jquery.js');
|
2016-04-20 05:00:38 +02:00
|
|
|
Requirements::javascript(FRAMEWORK_DIR . '/javascript/ToggleField.js');
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2016-04-20 05:00:38 +02:00
|
|
|
if($this->startClosed) {
|
|
|
|
$this->addExtraClass('startClosed');
|
|
|
|
}
|
|
|
|
|
|
|
|
$valueForInput = '';
|
|
|
|
|
|
|
|
if($this->value) {
|
|
|
|
$valueForInput = Convert::raw2att($this->value);
|
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2016-04-20 05:00:38 +02:00
|
|
|
$rawInput = Convert::html2raw($valueForInput);
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2016-04-20 05:00:38 +02:00
|
|
|
if($this->charNum) {
|
|
|
|
$reducedValue = substr($rawInput, 0, $this->charNum);
|
|
|
|
} else {
|
|
|
|
$reducedValue = DBField::create_field('Text', $rawInput)->{$this->truncateMethod}();
|
|
|
|
}
|
2015-08-21 04:01:10 +02:00
|
|
|
|
2016-04-20 05:00:38 +02:00
|
|
|
// only create toggle field if the truncated content is shorter
|
|
|
|
if(strlen($reducedValue) < strlen($rawInput)) {
|
2015-08-21 04:01:10 +02:00
|
|
|
$content = <<<HTML
|
|
|
|
<div class="readonly typography contentLess" style="display: none">
|
2016-04-20 05:00:38 +02:00
|
|
|
$reducedValue
|
2015-08-21 04:01:10 +02:00
|
|
|
<a href="#" class="triggerMore">$this->labelMore</a>
|
|
|
|
</div>
|
|
|
|
<div class="readonly typography contentMore">
|
|
|
|
$this->value
|
|
|
|
<a href="#" class="triggerLess">$this->labelLess</a>
|
2016-01-06 00:34:58 +01:00
|
|
|
</div>
|
2015-08-21 04:01:10 +02:00
|
|
|
<br />
|
2016-04-20 05:00:38 +02:00
|
|
|
<input type="hidden" name="$this->name" value="$valueForInput" />
|
2015-08-21 04:01:10 +02:00
|
|
|
HTML;
|
|
|
|
} else {
|
|
|
|
$this->dontEscape = true;
|
|
|
|
$content = parent::Field();
|
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2015-08-21 04:01:10 +02:00
|
|
|
return $content;
|
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2015-08-21 04:01:10 +02:00
|
|
|
/**
|
|
|
|
* Determines if the field should render open or closed by default.
|
2016-01-06 00:34:58 +01:00
|
|
|
*
|
2015-08-21 04:01:10 +02:00
|
|
|
* @param boolean
|
|
|
|
*/
|
|
|
|
public function startClosed($bool) {
|
2016-04-20 05:00:38 +02:00
|
|
|
if($bool) {
|
|
|
|
$this->addExtraClass('startClosed');
|
|
|
|
} else {
|
|
|
|
$this->removeExtraClass('startClosed');
|
|
|
|
}
|
2015-08-21 04:01:10 +02:00
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2015-08-21 04:01:10 +02:00
|
|
|
public function Type() {
|
|
|
|
return "toggleField";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|