Blame view

framework/forms/ToggleField.php 2.71 KB
0084d336   Administrator   Importers CRUD
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
  <?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.
   * 
   * 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;
  	
  	/**
  	 * @see Text
  	 * @var $truncateMethod string (FirstSentence|FirstParagraph)
  	 */
  	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
  	 */
  	public 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);
  	}
  
  	public function Field($properties = array()) {
  		$content = '';
  
  		Requirements::javascript(FRAMEWORK_DIR . '/thirdparty/jquery/jquery.js');
  		Requirements::javascript(FRAMEWORK_DIR . "/javascript/ToggleField.js");
  		
  		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);
  		else $reducedVal = DBField::create_field('Text',$rawInput)->{$this->truncateMethod}();
  
  		// 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
  				&nbsp;<a href="#" class="triggerMore">$this->labelMore</a>
  			</div>
  			<div class="readonly typography contentMore">
  				$this->value
  				&nbsp;<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');
  	}
  	
  	public function Type() {
  		return "toggleField";
  	}
  }