XOOPS RMCommon Utilities  2.1.8.91RC
 All Classes Namespaces Files Functions Variables
groups.class.php
Go to the documentation of this file.
1 <?php
2 // $Id: groups.class.php 928 2012-01-15 06:56:56Z 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 
16 {
17  private $_multi = 0;
18  private $_select = array();
23  private $_showtype = 0;
24  private $_showdesc = 0;
25  private $_cols = 2;
26 
32  function __construct($caption, $name, $multi=0, $type=0, $cols=2, $select=array()){
33  $this->setCaption($caption);
34  $this->setName($multi ? str_replace('[]', '', $name).'[]' : $name);
35  if (isset($_REQUEST[$name])) $this->_select = $_REQUEST[$name];
36  $this->_multi = $multi;
37  $this->_showtype = $type;
38  $this->_cols = $cols;
39 
40  if (isset($_REQUEST[$this->getName()])){
41  $this->_select = $_REQUEST[$this->getName()];
42  } else {
43  $this->_select = $select;
44  }
45 
46  }
53  public function setMulti($value){
54  if ($value==0 || $value==1){
55  $this->setName($value ? str_replace('[]','',$this->getName()).'[]' : str_replace('[]','',$this->getName()));
56  $this->_multi = $value;
57  }
58  }
63  public function getMulti(){
64  return $this->_multi;
65  }
74  public function setSelect($value){
75  if (is_array($value)){
76  $this->_select = $value;
77  } else {
78  $this->_select = explode(',',$value);
79  }
80  }
86  public function getSelect(){
87  return $this->_select;
88  }
94  public function setShowType($value){
95  if ($value==0 || $value==1) $this->_showtype = $value;
96  }
101  public function getShowType(){
102  return $this->_showtype;
103  }
110  public function showDesc($value){
111  if ($value==0 || $value==1) $this->_showdesc = $value;
112  }
117  public function getShowDesc(){
118  return $this->_showdesc;
119  }
126  public function setCols($value){
127  if ($value>0) $this->_cols = $value;
128  }
133  public function getCols(){
134  return $this->_cols;
135  }
140  public function render(){
141  $db = XoopsDatabaseFactory::getDatabaseConnection();
142  $result = $db->query("SELECT * FROM ".$db->prefix("groups")." ORDER BY `name`");
143  $rtn = '';
144  $col = 1;
145 
146  $typeinput = $this->_multi ? 'checkbox' : 'radio';
147  $name = $this->getName();
148 
149  if ($this->_showtype){
150  $rtn = "<ul class='groups_field_list ".$this->id()."_groups'>";
151  $rtn .= "<li><label><input type='$typeinput' name='$name' id='".$this->id()."' value='0'";
152  if (is_array($this->_select)){
153  if (in_array(0, $this->_select)){
154  $rtn .= " checked='checked'";
155  }
156  }
157  $rtn .= ">".__('All','rmcommon')."</label></li>";
158  while ($row = $db->fetchArray($result)){
159 
160  $rtn .= "<li><label><input type='$typeinput' name='$name' id='".$this->id()."' value='$row[groupid]'";
161  if (is_array($this->_select)){
162  if (in_array($row['groupid'], $this->_select)){
163  $rtn .= " checked='checked'";
164  }
165  }
166  $rtn .= ">$row[name]</label>";
167 
168  if ($this->_showdesc){
169  $rtn .= "<br /><span style='font-size: 10px;' class='description'>$row[description]</span>";
170  }
171 
172  $rtn .= "</li>";
173 
174  $col++;
175 
176  }
177  $rtn .= "</ul>";
178  } else {
179 
180  $rtn = "<select name='$name'";
181  $rtn .= $this->_multi ? " multiple='multiple' size='5'" : "";
182  $rtn .= "><option value='0'";
183  if (is_array($this->_select)){
184  if (in_array(0, $this->_select)){
185  $rtn .= " selected='selected'";
186  }
187  } else {
188  $rtn .= " selected='selected'";
189  }
190 
191  $rtn .= ">".__('All','rmcommon')."</option>";
192 
193  while ($row = $db->fetchArray($result)){
194  $rtn .= "<option value='$row[groupid]'";
195  if (is_array($this->_select)){
196  if (in_array($row['groupid'], $this->_select)){
197  $rtn .= " selected='selected'";
198  }
199  }
200  $rtn .= ">".$row['name']."</option>";
201  }
202 
203  $rtn .= "</select>";
204  }
205 
206  return $rtn;
207  }
208 
209 }