XOOPS  2.6.0
GroupFormCheckbox.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 namespace Xoops\Form;
13 
26 {
32  protected $value = array();
33 
39  private $groupId;
40 
46  private $optionTree = array();
47 
56  public function __construct($caption, $name, $groupId, $values = null)
57  {
58  $this->setCaption($caption);
59  $this->setName($name);
60  if (isset($values)) {
61  $this->setValue($values);
62  }
63  $this->groupId = $groupId;
64  }
65 
73  public function setOptionTree(&$optionTree)
74  {
75  $this->optionTree = $optionTree;
76  }
77 
83  public function render()
84  {
85  $ele_name = $this->getName();
86  $ret = '<table class="outer"><tr><td class="odd"><table><tr>';
87  $cols = 1;
88 
89  foreach ($this->optionTree[0]['children'] as $topitem) {
90  if ($cols > 4) {
91  $ret .= '</tr><tr>';
92  $cols = 1;
93  }
94  $tree = '<td valign="top">';
95  $prefix = '';
96  $this->renderOptionTree($tree, $this->optionTree[$topitem], $prefix);
97  $ret .= $tree . '</td>';
98  ++$cols;
99  }
100  $ret .= '</tr></table></td><td class="even" valign="top">';
101  $option_ids = array();
102  foreach (array_keys($this->optionTree) as $id) {
103  if (!empty($id)) {
104  $option_ids[] = "'" . $ele_name . '[groups][' . $this->groupId . '][' . $id . ']' . "'";
105  }
106  }
107  $checkallbtn_id = $ele_name . '[checkallbtn][' . $this->groupId . ']';
108  $option_ids_str = implode(', ', $option_ids);
109  $ret .= \XoopsLocale::ALL . " <input id=\"" . $checkallbtn_id . "\" type=\"checkbox\" value=\"\" onclick=\"var optionids = new Array(" . $option_ids_str . "); xoopsCheckAllElements(optionids, '" . $checkallbtn_id . "');\" />";
110  $ret .= '</td></tr></table>';
111  return $ret;
112  }
113 
122  private function renderOptionTree(&$tree, $option, $prefix, $parentIds = array())
123  {
124  $ele_name = $this->getName();
125  $tree .= $prefix . "<input type=\"checkbox\" name=\"" . $ele_name . "[groups][" . $this->groupId . "][" . $option['id'] . "]\" id=\"" . $ele_name . "[groups][" . $this->groupId . "][" . $option['id'] . "]\" onclick=\"";
126  // If there are parent elements, add javascript that will
127  // make them selecteded when this element is checked to make
128  // sure permissions to parent items are added as well.
129  foreach ($parentIds as $pid) {
130  $parent_ele = $ele_name . '[groups][' . $this->groupId . '][' . $pid . ']';
131  $tree .= "var ele = xoopsGetElementById('" . $parent_ele . "'); if(ele.checked != true) {ele.checked = this.checked;}";
132  }
133  // If there are child elements, add javascript that will
134  // make them unchecked when this element is unchecked to make
135  // sure permissions to child items are not added when there
136  // is no permission to this item.
137  foreach ($option['allchild'] as $cid) {
138  $child_ele = $ele_name . '[groups][' . $this->groupId . '][' . $cid . ']';
139  $tree .= "var ele = xoopsGetElementById('" . $child_ele . "'); if(this.checked != true) {ele.checked = false;}";
140  }
141  $tree .= '" value="1"';
142  if (in_array($option['id'], $this->value)) {
143  $tree .= ' checked="checked"';
144  }
145  $tree .= " />" . $option['name'] . "<input type=\"hidden\" name=\"" . $ele_name . "[parents][" . $option['id'] . "]\" value=\"" . implode(':', $parentIds) . "\" /><input type=\"hidden\" name=\"" . $ele_name . "[itemname][" . $option['id'] . "]\" value=\"" . htmlspecialchars($option['name']) . "\" /><br />\n";
146  if (isset($option['children'])) {
147  foreach ($option['children'] as $child) {
148  array_push($parentIds, $option['id']);
149  $this->renderOptionTree($tree, $this->optionTree[$child], $prefix . '&nbsp;-', $parentIds);
150  }
151  }
152  }
153 }
setValue($value)
Definition: Element.php:199
setCaption($caption)
Definition: Element.php:396
renderOptionTree(&$tree, $option, $prefix, $parentIds=array())
$pid
Definition: admin_menu.php:37
$option
Definition: Xoops.php:68
$id
Definition: admin_menu.php:36
__construct($caption, $name, $groupId, $values=null)