XOOPS RMCommon Utilities  2.1.8.91RC
 All Classes Namespaces Files Functions Variables
select.class.php
Go to the documentation of this file.
1 <?php
2 // $Id: select.class.php 825 2011-12-09 00:06:11Z i.bitcero $
3 // --------------------------------------------------------------
4 // Red México Common Utilities
5 // A framework for Red México Modules
6 // Author: Eduardo Cortés <i.bitcero@gmail.com>
7 // Email: i.bitcero@gmail.com
8 // License: GPL 2.0
9 // --------------------------------------------------------------
10 
15 {
16  private $_rows = 5;
17  private $_multi = 0;
18  private $_options = array();
19  private $_selected = null;
20 
27  function __construct($caption, $name, $multi=0, $selected = null){
28  $this->setCaption($caption);
29  $this->setName($name);
30  $this->_multi = $multi;
31  $this->_selected = $selected!=null ? (is_array($selected) ? $selected : array($selected)) : null;
32  }
38  public function setRows($value){
39  $this->_rows = $value;
40  }
45  public function getRows(){
46  return $this->_rows;
47  }
56  public function addOption($value, $caption, $select=0, $disabled=false, $style=''){
57  $rtn = array();
58  $rtn['value'] = $value;
59  $rtn['text'] = $caption;
60  $rtn['select'] = isset($_REQUEST[$this->getName()]) && $_REQUEST[$this->getName()]==$value ? 1 : $select;
61  $rtn['disabled'] = $disabled;
62  $rtn['style'] = trim($style);
63  $this->_options[] = $rtn;
64  }
69  public function addOptionsArray($options){
70  foreach ($options as $k => $v){
71  $rtn = array();
72  $rtn['value'] = is_array($v) && isset($v['value']) ? $v['value'] : $k;
73  $rtn['text'] = is_array($v) && isset($v['text']) ? $v['text'] : $v;
74  $rtn['select'] = isset($_REQUEST[$this->getName()]) && $_REQUEST[$this->getName()]==$rtn['value'] ? 1 : (is_array($v) && isset($v['select']) ? 1 : (is_array($this->_selected) && in_array($rtn['value'], $this->_selected) ? 1 : 0));
75  $rtn['disabled'] = is_array($v) && isset($v['disabled']) ? $v['disabled'] : 0;
76  $rtn['style'] = is_array($v) && isset($v['style']) ? trim($v['style']) : '';
77  $this->_options[] = $rtn;
78  }
79  }
83  public function setSelected($index){
84  foreach ($this->_options as $k => $v){
85  if ($v['value']==$index){
86  $this->_options[$k]['select'] = 1;
87  break;
88  }
89  }
90  }
95  function getOptions(){
96  return $this->_options;
97  }
102  function render(){
103  $rtn = "<select name='".$this->getName()."' id='".$this->id()."'";
104  if ($this->_multi){ $rtn .= " multiple='multiple' size='".$this->_rows."'"; }
105 
106  if ($this->getClass() != ''){
107  $rtn .= 'class="'.$this->getClass().'" '.$this->getExtra();
108  } else {
109  $rtn .= $this->getExtra();
110  }
111 
112  $rtn .= ">";
113 
114  foreach ($this->_options as $k => $v){
115  $rtn .= "<option value='$v[value]'";
116  if ($v['select'] || (is_array($this->_selected) && in_array($v['value'], $this->_selected))){ $rtn .= " selected='selected'"; }
117  if ($v['disabled']){ $rtn .= " disabled='disabled'"; }
118  if ($v['style']!='') $rtn .= " style='$v[style]'";
119  $rtn .= ">$v[text]</option>";
120  }
121 
122  $rtn .= "</select>";
123  return $rtn;
124  }
125 }
126 
127 ?>