Completed
Push — master ( f2ac6e...70aca0 )
by Ingo
09:36 queued 09:18
created

TextareaField::getSchemaDataDefaults()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\Forms;
4
5
/**
6
 * TextareaField creates a multi-line text field,
7
 * allowing more data to be entered than a standard
8
 * text field. It creates the <textarea> tag in the
9
 * form HTML.
10
 *
11
 * <code>
12
 * new TextareaField(
13
 *    $name = "description",
14
 *    $title = "Description",
15
 *    $value = "This is the default description"
16
 * );
17
 * </code>
18
 */
19
class TextareaField extends FormField {
20
21
	/**
22
	 * Value should be XML
23
	 *
24
	 * @var array
25
	 */
26
	private static $casting = array(
27
		'Value' => 'Text',
28
		'ValueEntities' => 'HTMLFragment',
29
	);
30
    
31
    protected $schemaDataType = FormField::SCHEMA_DATA_TYPE_TEXT;
32
    
33
    /**
34
	 * Visible number of text lines.
35
	 *
36
	 * @var int
37
	 */
38
	protected $rows = 5;
39
40
	/**
41
	 * Visible number of text columns.
42
	 *
43
	 * @var int
44
	 */
45
	protected $cols = 20;
46
47
	/**
48
	 * Set textarea specific schema data
49
     */
50
    public function getSchemaDataDefaults()
51
    {
52
        $data = parent::getSchemaDataDefaults();
53
        
54
        $data['data']['rows'] = $this->getRows();
55
        $data['data']['columns'] = $this->getColumns();
56
        
57
        return $data;
58
    }
59
    
60
    /**
61
     * Set the number of rows in the textarea
62
	 *
63
	 * @param int $rows
64
	 *
65
	 * @return $this
66
	 */
67
	public function setRows($rows) {
68
		$this->rows = $rows;
69
70
		return $this;
71
	}
72
73
	/**
74
	 * Gets number of rows
75
	 *
76
	 * @return int
77
	 */
78
	public function getRows() {
79
		return $this->rows;
80
	}
81
82
	/**
83
	 * Set the number of columns in the textarea
84
	 *
85
	 * @param int $cols
86
	 *
87
	 * @return $this
88
	 */
89
	public function setColumns($cols) {
90
		$this->cols = $cols;
91
92
		return $this;
93
	}
94
95
	/**
96
	 * Gets the number of columns in this textarea
97
	 *
98
	 * @return int
99
	 */
100
	public function getColumns() {
101
		return $this->cols;
102
	}
103
104
	/**
105
	 * {@inheritdoc}
106
	 */
107
	public function getAttributes() {
108
		return array_merge(
109
			parent::getAttributes(),
110
			array(
111
				'rows' => $this->getRows(),
112
				'cols' => $this->getColumns(),
113
				'value' => null,
114
				'type' => null
115
			)
116
		);
117
	}
118
119
120
	/**
121
	 * {@inheritdoc}
122
	 */
123
	public function Type() {
124
		$parent = parent::Type();
125
126
		if($this->readonly) {
127
			return $parent . ' readonly';
128
		}
129
130
		return $parent;
131
	}
132
133
	/**
134
	 * Return value with all values encoded in html entities
135
	 *
136
	 * @return string Raw HTML
137
	 */
138
	public function ValueEntities() {
139
		return htmlentities($this->Value(), ENT_COMPAT, 'UTF-8');
140
	}
141
}
142