richtext.php
Go to the documentation of this file.
1 <?php
10 namespace depage\htmlform\elements;
11 
37 class richtext extends textarea
38 {
48  protected function setDefaults()
49  {
50  parent::setDefaults();
51 
52  $this->defaults['rows'] = null;
53  $this->defaults['cols'] = null;
54  $this->defaults['stylesheet'] = null;
55  $this->defaults['autogrow'] = true;
56  $this->defaults['allowedTags'] = array(
57  // block elements
58  "p",
59  "br",
60  "h1",
61  "h2",
62  "ul",
63  "ol",
64  "li",
65 
66  // inline elements
67  "a",
68  "b",
69  "strong",
70  "i",
71  "em",
72  );
73  }
79  protected function htmlWrapperAttributes()
80  {
81  $attributes = parent::htmlWrapperAttributes();
82 
83  $options = array();
84  $options['stylesheet'] = $this->stylesheet;
85  $options['allowedTags'] = $this->allowedTags;
86 
87  $attributes .= " data-richtext-options=\"" . $this->htmlEscape(json_encode($options)) . "\"";
88 
89  return $attributes;
90  }
96  protected function htmlValue()
97  {
98  if ($this->value === null) {
99  $htmlDOM = $this->parseHtml($this->defaultValue);
100  } else {
101  $htmlDOM = $this->value;
102  }
103 
104  $html = "";
105 
106  // add content of every node in body
107  foreach ($htmlDOM->documentElement->childNodes as $node) {
108  $html .= $htmlDOM->saveXML($node);
109  }
110 
111  return $this->htmlEscape($html);
112  }
118  protected function typeCastValue()
119  {
120  if (is_string($this->value)) {
121  $this->value = $this->parseHtml($this->value);
122  }
123  }
131  protected function parseHtml($html)
132  {
133  $htmlDOM = new \depage\htmlform\abstracts\htmldom();
134 
135  $htmlDOM->loadHTML($html);
136  $htmlDOM->cleanHTML($this->allowedTags);
137 
138  return $htmlDOM;
139  }
140 }
141 
142 /* vim:set ft=php sw=4 sts=4 fdm=marker et : */
htmlEscape($options=array())
Escapes HTML in strings and arrays of strings.
Definition: element.php:193
typeCastValue()
Converts value into htmlDOM.
Definition: richtext.php:118
htmlWrapperAttributes()
Returns string of HTML attributes for element wrapper paragraph.
Definition: richtext.php:79
$value
Input elements&#39;s value.
Definition: input.php:79
HTML textarea element.
Definition: textarea.php:34
htmlValue()
Returns HTML-rendered element value.
Definition: richtext.php:96
parseHtml($html)
Parses html-string into htmlDOM.
Definition: richtext.php:131
setDefaults()
collects initial values across subclasses
Definition: richtext.php:48
HTML richtext element.
Definition: richtext.php:37