XOOPS  2.6.0
Element.php
Go to the documentation of this file.
1 <?php
2 /*
3  You may not change or alter any portion of this comment or credits
4  of supporting developers from this source code or any supporting source code
5  which is considered copyrighted (c) material of the original comment or credit authors.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 */
11 
12 
13 namespace Xoops\Form;
14 
16 
28 abstract class Element extends Attributes
29 {
30 
41  public $customValidationCode = array();
42 
48  //private $name = '';
49 
55  private $caption = '';
56 
62  //private $accesskey = '';
63 
69  //private $class = array();
70 
77  //private $pattern;
78 
86 
93  private $datalist;
94 
100  //private $hidden = false;
101 
107  private $extra = array();
108 
114  //private $required = false;
115 
121  private $description = '';
122 
131  protected $value = '';
132 
139  private $maxcols = 6;
140 
148  abstract public function render();
149 
150 
156  public function renderAttributeString()
157  {
158  // title attribute needs to be generated if not already set
159  if (!$this->hasAttribute('title')) {
160  $this->setAttribute('title', $this->getTitle());
161  }
162  // generate id from name if not already set
163  if (!$this->hasAttribute('id')) {
164  $id = $this->getAttribute('name');
165  if (substr($id, -2) == '[]') {
166  $id = substr($id, 0, strlen($id)-2);
167  }
168  $this->setAttribute('id', $id);
169  }
170  return parent::renderAttributeString();
171  }
172 
180  public function getValue($encode = false)
181  {
182  if (is_array($this->value)) {
183  $ret = array();
184  foreach ($this->value as $value) {
185  $ret[] = $encode ? htmlspecialchars($value, ENT_QUOTES) : $value;
186  }
187  return $ret;
188  }
189  return $encode ? htmlspecialchars($this->value, ENT_QUOTES) : $this->value;
190  }
191 
199  public function setValue($value)
200  {
201  if (is_array($value)) {
202  $this->value = (array)$this->value;
203  foreach ($value as $v) {
204  $this->value[] = $v;
205  }
206  } elseif (is_array($this->value)) {
207  $this->value[] = $value;
208  } else {
209  $this->value = $value;
210  }
211  }
212 
220  public function setName($name)
221  {
222  $this->setAttribute('name', $name);
223  }
224 
230  public function getName()
231  {
232  return (string) $this->getAttribute('name');
233  }
234 
242  public function setAccessKey($key)
243  {
244  $this->setAttribute('accesskey', $key);
245  }
246 
252  public function getAccessKey()
253  {
254  return (string) $this->getAttribute('accesskey');
255  }
256 
264  public function getAccessString($str)
265  {
266  $access = $this->getAccessKey();
267  if (!empty($access) && (false !== ($pos = strpos($str, $access)))) {
268  return htmlspecialchars(substr($str, 0, $pos), ENT_QUOTES)
269  . '<span style="text-decoration: underline;">'
270  . htmlspecialchars(substr($str, $pos, 1), ENT_QUOTES) . '</span>'
271  . htmlspecialchars(substr($str, $pos + 1), ENT_QUOTES);
272  }
273  return htmlspecialchars($str, ENT_QUOTES);
274  }
275 
283  public function setClass($class)
284  {
285  $this->addAttribute('class', (string) $class);
286  }
287 
293  public function getClass()
294  {
295  $class = $this->getAttribute('class');
296  if ($class === false) {
297  return false;
298  }
299  return htmlspecialchars(implode(' ', $class), ENT_QUOTES);
300  }
301 
310  public function setPattern($pattern, $pattern_description = '')
311  {
312  $this->setAttribute('pattern', $pattern);
313  $this->pattern_description = trim($pattern_description);
314  }
315 
321  public function getPattern()
322  {
323  return (string) $this->getAttribute('pattern');
324  }
325 
331  public function getPatternDescription()
332  {
333  if (empty($this->pattern_description)) {
334  return '';
335  }
337  }
338 
347  public function setDatalist($datalist)
348  {
349  if (is_array($datalist)) {
350  if (!empty($datalist)) {
351  $this->datalist = $datalist;
352  }
353  } else {
354  $this->datalist[] = trim($datalist);
355  }
356  }
357 
363  public function getDatalist()
364  {
365  if (empty($this->datalist)) {
366  return '';
367  }
368  $ret = NWLINE . '<datalist id="list_' . $this->getName() . '">' . NWLINE;
369  foreach ($this->datalist as $datalist) {
370  $ret .= '<option value="' . htmlspecialchars($datalist, ENT_QUOTES) . '">' . NWLINE;
371  }
372  $ret .= '</datalist>' . NWLINE;
373  return $ret;
374  }
375 
381  public function isDatalist()
382  {
383  if (empty($this->datalist)) {
384  return false;
385  }
386  return true;
387  }
388 
396  public function setCaption($caption)
397  {
398  $this->caption = trim($caption);
399  }
400 
406  public function getCaption()
407  {
408  return $this->caption;
409  //return htmlspecialchars($this->caption, ENT_QUOTES);
410  }
411 
419  public function setTitle($title)
420  {
421  $this->setAttribute('title', $title);
422  }
423 
429  public function getTitle()
430  {
431  if ($this->hasAttribute('title')) {
432  return $this->getAttribute('title');
433  } else {
434  if (strlen($this->pattern_description) > 0) {
435  return htmlspecialchars(strip_tags($this->caption . ' - ' . $this->pattern_description), ENT_QUOTES);
436  } else {
437  return htmlspecialchars(strip_tags($this->caption), ENT_QUOTES);
438  }
439  }
440  }
441 
449  public function setDescription($description)
450  {
451  $this->description = trim($description);
452  }
453 
461  public function getDescription($encode = false)
462  {
463  return $encode ? htmlspecialchars($this->description, ENT_QUOTES) : $this->description;
464  }
465 
471  public function setHidden()
472  {
473  $this->setAttribute('hidden');
474  }
475 
481  public function isHidden()
482  {
483  return $this->hasAttribute('hidden');
484  }
485 
493  public function setRequired($bool = true)
494  {
495  if ($bool) {
496  $this->setAttribute('required');
497  }
498  }
499 
505  public function isRequired()
506  {
507  return $this->hasAttribute('required');
508  }
509 
522  public function setExtra($extra, $replace = false)
523  {
524  if ($replace) {
525  $this->extra = array(trim($extra));
526  } else {
527  $this->extra[] = trim($extra);
528  }
529  return $this->extra;
530  }
531 
539  public function getExtra($encode = false)
540  {
541  if (!$encode) {
542  return implode(' ', $this->extra);
543  }
544  $value = array();
545  foreach ($this->extra as $val) {
546  $value[] = str_replace('>', '&gt;', str_replace('<', '&lt;', $val));
547  }
548  return empty($value) ? '' : ' ' . implode(' ', $value);
549  }
550 
556  public function renderValidationJS()
557  {
558  // render custom validation code if any
559  if (!empty($this->customValidationCode)) {
560  return implode(NWLINE, $this->customValidationCode);
561  // generate validation code if required
562  } else {
563  if ($this->isRequired() && $eltname = $this->getName()) {
564  // $eltname = $this->getName();
565  $eltcaption = $this->getCaption();
566  $eltmsg = empty($eltcaption)
567  ? sprintf(\XoopsLocale::F_ENTER, $eltname)
568  : sprintf(\XoopsLocale::F_ENTER, $eltcaption);
569  $eltmsg = str_replace(array(':', '?', '%'), '', $eltmsg);
570  $eltmsg = str_replace('"', '\"', stripslashes($eltmsg));
571  $eltmsg = strip_tags($eltmsg);
572  return NWLINE
573  . "if ( myform.{$eltname}.value == \"\" ) { window.alert(\"{$eltmsg}\");"
574  . " myform.{$eltname}.focus(); return false; }\n";
575  }
576  }
577  return false;
578  }
579 
585  public function getMaxcols()
586  {
587  return $this->maxcols;
588  }
589 }
setValue($value)
Definition: Element.php:199
getValue($encode=false)
Definition: Element.php:180
setCaption($caption)
Definition: Element.php:396
const F_ENTER
Definition: en_US.php:408
setAttribute($name, $value=null)
Definition: Attributes.php:42
getAccessString($str)
Definition: Element.php:264
setRequired($bool=true)
Definition: Element.php:493
$id
Definition: admin_menu.php:36
getExtra($encode=false)
Definition: Element.php:539
setDatalist($datalist)
Definition: Element.php:347
addAttribute($name, $value)
Definition: Attributes.php:117
setClass($class)
Definition: Element.php:283
setExtra($extra, $replace=false)
Definition: Element.php:522
setTitle($title)
Definition: Element.php:419
setPattern($pattern, $pattern_description= '')
Definition: Element.php:310
getDescription($encode=false)
Definition: Element.php:461
setDescription($description)
Definition: Element.php:449