XOOPS 2.5.6  Final
 All Classes Namespaces Files Functions Variables Pages
groupperm.php
Go to the documentation of this file.
1 <?php
19 defined('XOOPS_ROOT_PATH') or die('Restricted access');
20 
32 {
37  function XoopsGroupPerm()
38  {
39  $this->XoopsObject();
40  $this->initVar('gperm_id', XOBJ_DTYPE_INT, null, false);
41  $this->initVar('gperm_groupid', XOBJ_DTYPE_INT, null, false);
42  $this->initVar('gperm_itemid', XOBJ_DTYPE_INT, null, false);
43  $this->initVar('gperm_modid', XOBJ_DTYPE_INT, 0, false);
44  $this->initVar('gperm_name', XOBJ_DTYPE_OTHER, null, false);
45  }
46 
50  function id($format = 'N')
51  {
52  return $this->getVar('gperm_id', $format);
53  }
54 
58  function gperm_id($format = '')
59  {
60  return $this->getVar('gperm_id', $format);
61  }
62 
66  function gperm_groupid($format = '')
67  {
68  return $this->getVar('gperm_groupid', $format);
69  }
70 
74  function gperm_itemid($format = '')
75  {
76  return $this->getVar('gperm_itemid', $format);
77  }
78 
82  function gperm_modid($format = '')
83  {
84  return $this->getVar('gperm_modid', $format);
85  }
86 
90  function gperm_name($format = '')
91  {
92  return $this->getVar('gperm_name', $format);
93  }
94 
95 }
96 
109 {
115  function &create($isNew = true)
116  {
117  $perm = new XoopsGroupPerm();
118  if ($isNew) {
119  $perm->setNew();
120  }
121  return $perm;
122  }
123 
131  function &get($id)
132  {
133  $id = intval($id);
134  $perm = false;
135  if ($id > 0) {
136  $sql = sprintf("SELECT * FROM %s WHERE gperm_id = %u", $this->db->prefix('group_permission'), $id);
137  if (!$result = $this->db->query($sql)) {
138  return $perm;
139  }
140  $numrows = $this->db->getRowsNum($result);
141  if ($numrows == 1) {
142  $perm = new XoopsGroupPerm();
143  $perm->assignVars($this->db->fetchArray($result));
144  }
145  }
146  return $perm;
147  }
148 
156  function insert(&$perm)
157  {
161  if (!is_a($perm, 'xoopsgroupperm')) {
162  return false;
163  }
164  if (!$perm->isDirty()) {
165  return true;
166  }
167  if (!$perm->cleanVars()) {
168  return false;
169  }
170  foreach ($perm->cleanVars as $k => $v) {
171  ${$k} = $v;
172  }
173  if ($perm->isNew()) {
174  $gperm_id = $this->db->genId('group_permission_gperm_id_seq');
175  $sql = sprintf("INSERT INTO %s (gperm_id, gperm_groupid, gperm_itemid, gperm_modid, gperm_name) VALUES (%u, %u, %u, %u, %s)", $this->db->prefix('group_permission'), $gperm_id, $gperm_groupid, $gperm_itemid, $gperm_modid, $this->db->quoteString($gperm_name));
176  } else {
177  $sql = sprintf("UPDATE %s SET gperm_groupid = %u, gperm_itemid = %u, gperm_modid = %u WHERE gperm_id = %u", $this->db->prefix('group_permission'), $gperm_groupid, $gperm_itemid, $gperm_modid, $gperm_id);
178  }
179  if (!$result = $this->db->query($sql)) {
180  return false;
181  }
182  if (empty($gperm_id)) {
183  $gperm_id = $this->db->getInsertId();
184  }
185  $perm->assignVar('gperm_id', $gperm_id);
186  return true;
187  }
188 
196  function delete(&$perm)
197  {
201  if (!is_a($perm, 'xoopsgroupperm')) {
202  return false;
203  }
204  $sql = sprintf("DELETE FROM %s WHERE gperm_id = %u", $this->db->prefix('group_permission'), $perm->getVar('gperm_id'));
205  if (!$result = $this->db->query($sql)) {
206  return false;
207  }
208  return true;
209  }
210 
219  function getObjects($criteria = null, $id_as_key = false)
220  {
221  $ret = array();
222  $limit = $start = 0;
223  $sql = 'SELECT * FROM ' . $this->db->prefix('group_permission');
224  if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
225  $sql .= ' ' . $criteria->renderWhere();
226  $limit = $criteria->getLimit();
227  $start = $criteria->getStart();
228  }
229  $result = $this->db->query($sql, $limit, $start);
230  if (!$result) {
231  return $ret;
232  }
233  while ($myrow = $this->db->fetchArray($result)) {
234  $perm = new XoopsGroupPerm();
235  $perm->assignVars($myrow);
236  if (!$id_as_key) {
237  $ret[] =& $perm;
238  } else {
239  $ret[$myrow['gperm_id']] =& $perm;
240  }
241  unset($perm);
242  }
243  return $ret;
244  }
245 
253  function getCount($criteria = null)
254  {
255  $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('group_permission');
256  if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
257  $sql .= ' ' . $criteria->renderWhere();
258  }
259  $result = $this->db->query($sql);
260  if (!$result) {
261  return 0;
262  }
263  list ($count) = $this->db->fetchRow($result);
264  return $count;
265  }
266 
274  function deleteAll($criteria = null)
275  {
276  $sql = sprintf("DELETE FROM %s", $this->db->prefix('group_permission'));
277  if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
278  $sql .= ' ' . $criteria->renderWhere();
279  }
280  if (!$result = $this->db->query($sql)) {
281  return false;
282  }
283  return true;
284  }
285 
294  function deleteByGroup($gperm_groupid, $gperm_modid = null)
295  {
296  $criteria = new CriteriaCompo(new Criteria('gperm_groupid', intval($gperm_groupid)));
297  if (isset($gperm_modid)) {
298  $criteria->add(new Criteria('gperm_modid', intval($gperm_modid)));
299  }
300  return $this->deleteAll($criteria);
301  }
302 
312  function deleteByModule($gperm_modid, $gperm_name = null, $gperm_itemid = null)
313  {
314  $criteria = new CriteriaCompo(new Criteria('gperm_modid', intval($gperm_modid)));
315  if (isset($gperm_name)) {
316  $criteria->add(new Criteria('gperm_name', $gperm_name));
317  if (isset($gperm_itemid)) {
318  $criteria->add(new Criteria('gperm_itemid', intval($gperm_itemid)));
319  }
320  }
321  return $this->deleteAll($criteria);
322  }
323 
335  function checkRight($gperm_name, $gperm_itemid, $gperm_groupid, $gperm_modid = 1, $trueifadmin = true)
336  {
337  if (empty($gperm_groupid)) {
338  return false;
339  } else if (is_array($gperm_groupid)) {
340  if (in_array(XOOPS_GROUP_ADMIN, $gperm_groupid) && $trueifadmin) {
341  return true;
342  }
343  $criteria_group = new CriteriaCompo();
344  foreach ($gperm_groupid as $gid) {
345  $criteria_group->add(new Criteria('gperm_groupid', $gid), 'OR');
346  }
347  } else {
348  if (XOOPS_GROUP_ADMIN == $gperm_groupid && $trueifadmin) {
349  return true;
350  }
351  $criteria_group = new CriteriaCompo(new Criteria('gperm_groupid', $gperm_groupid));
352  }
353  $criteria = new CriteriaCompo(new Criteria('gperm_modid', $gperm_modid));
354  $criteria->add($criteria_group);
355  $criteria->add(new Criteria('gperm_name', $gperm_name));
356  $gperm_itemid = intval($gperm_itemid);
357  if ($gperm_itemid > 0) {
358  $criteria->add(new Criteria('gperm_itemid', $gperm_itemid));
359  }
360  if ($this->getCount($criteria) > 0) {
361  return true;
362  }
363  return false;
364  }
365 
376  function addRight($gperm_name, $gperm_itemid, $gperm_groupid, $gperm_modid = 1)
377  {
378  $perm =& $this->create();
379  $perm->setVar('gperm_name', $gperm_name);
380  $perm->setVar('gperm_groupid', $gperm_groupid);
381  $perm->setVar('gperm_itemid', $gperm_itemid);
382  $perm->setVar('gperm_modid', $gperm_modid);
383  return $this->insert($perm);
384  }
385 
395  function getItemIds($gperm_name, $gperm_groupid, $gperm_modid = 1)
396  {
397  $ret = array();
398  $criteria = new CriteriaCompo(new Criteria('gperm_name', $gperm_name));
399  $criteria->add(new Criteria('gperm_modid', intval($gperm_modid)));
400  if (is_array($gperm_groupid)) {
401  $criteria2 = new CriteriaCompo();
402  foreach($gperm_groupid as $gid) {
403  $criteria2->add(new Criteria('gperm_groupid', $gid), 'OR');
404  }
405  $criteria->add($criteria2);
406  } else {
407  $criteria->add(new Criteria('gperm_groupid', intval($gperm_groupid)));
408  }
409  $perms = $this->getObjects($criteria, true);
410  foreach (array_keys($perms) as $i) {
411  $ret[] = $perms[$i]->getVar('gperm_itemid');
412  }
413  return array_unique($ret);
414  }
415 
425  function getGroupIds($gperm_name, $gperm_itemid, $gperm_modid = 1)
426  {
427  $ret = array();
428  $criteria = new CriteriaCompo(new Criteria('gperm_name', $gperm_name));
429  $criteria->add(new Criteria('gperm_itemid', intval($gperm_itemid)));
430  $criteria->add(new Criteria('gperm_modid', intval($gperm_modid)));
431  $perms = $this->getObjects($criteria, true);
432  foreach(array_keys($perms) as $i) {
433  $ret[] = $perms[$i]->getVar('gperm_groupid');
434  }
435  return $ret;
436  }
437 }
438 ?>