| 1 | <?php |
|---|
| 2 | // $Id: group.php,v 1.3 2006/05/01 02:37:28 onokazu Exp $ |
|---|
| 3 | // ------------------------------------------------------------------------ // |
|---|
| 4 | // XOOPS - PHP Content Management System // |
|---|
| 5 | // Copyright (c) 2000 XOOPS.org // |
|---|
| 6 | // <http://www.xoops.org/> // |
|---|
| 7 | // ------------------------------------------------------------------------ // |
|---|
| 8 | // This program is free software; you can redistribute it and/or modify // |
|---|
| 9 | // it under the terms of the GNU General Public License as published by // |
|---|
| 10 | // the Free Software Foundation; either version 2 of the License, or // |
|---|
| 11 | // (at your option) any later version. // |
|---|
| 12 | // // |
|---|
| 13 | // You may not change or alter any portion of this comment or credits // |
|---|
| 14 | // of supporting developers from this source code or any supporting // |
|---|
| 15 | // source code which is considered copyrighted (c) material of the // |
|---|
| 16 | // original comment or credit authors. // |
|---|
| 17 | // // |
|---|
| 18 | // This program is distributed in the hope that it will be useful, // |
|---|
| 19 | // but WITHOUT ANY WARRANTY; without even the implied warranty of // |
|---|
| 20 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // |
|---|
| 21 | // GNU General Public License for more details. // |
|---|
| 22 | // // |
|---|
| 23 | // You should have received a copy of the GNU General Public License // |
|---|
| 24 | // along with this program; if not, write to the Free Software // |
|---|
| 25 | // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // |
|---|
| 26 | // ------------------------------------------------------------------------ // |
|---|
| 27 | // Author: Kazumi Ono (AKA onokazu) // |
|---|
| 28 | // URL: http://www.myweb.ne.jp/, http://www.xoops.org/, http://jp.xoops.org/ // |
|---|
| 29 | // Project: The XOOPS Project // |
|---|
| 30 | // ------------------------------------------------------------------------- // |
|---|
| 31 | |
|---|
| 32 | if (!defined('XOOPS_ROOT_PATH')) { |
|---|
| 33 | exit(); |
|---|
| 34 | } |
|---|
| 35 | |
|---|
| 36 | /** |
|---|
| 37 | * a group of users |
|---|
| 38 | * |
|---|
| 39 | * @copyright copyright (c) 2000-2003 XOOPS.org |
|---|
| 40 | * @author Kazumi Ono <[email protected]> |
|---|
| 41 | * @package kernel |
|---|
| 42 | */ |
|---|
| 43 | class XoopsGroup extends XoopsObject |
|---|
| 44 | { |
|---|
| 45 | /** |
|---|
| 46 | * constructor |
|---|
| 47 | */ |
|---|
| 48 | function XoopsGroup() |
|---|
| 49 | { |
|---|
| 50 | $this->XoopsObject(); |
|---|
| 51 | $this->initVar('groupid', XOBJ_DTYPE_INT, null, false); |
|---|
| 52 | $this->initVar('name', XOBJ_DTYPE_TXTBOX, null, true, 100); |
|---|
| 53 | $this->initVar('description', XOBJ_DTYPE_TXTAREA, null, false); |
|---|
| 54 | $this->initVar('group_type', XOBJ_DTYPE_OTHER, null, false); |
|---|
| 55 | } |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | |
|---|
| 59 | /** |
|---|
| 60 | * XOOPS group handler class. |
|---|
| 61 | * This class is responsible for providing data access mechanisms to the data source |
|---|
| 62 | * of XOOPS group class objects. |
|---|
| 63 | * |
|---|
| 64 | * @author Kazumi Ono <[email protected]> |
|---|
| 65 | * @copyright copyright (c) 2000-2003 XOOPS.org |
|---|
| 66 | * @package kernel |
|---|
| 67 | * @subpackage member |
|---|
| 68 | */ |
|---|
| 69 | class XoopsGroupHandler extends XoopsObjectHandler |
|---|
| 70 | { |
|---|
| 71 | |
|---|
| 72 | /** |
|---|
| 73 | * create a new {@link XoopsGroup} object |
|---|
| 74 | * |
|---|
| 75 | * @param bool $isNew mark the new object as "new"? |
|---|
| 76 | * @return object XoopsGroup reference to the new object |
|---|
| 77 | * |
|---|
| 78 | */ |
|---|
| 79 | function &create($isNew = true) |
|---|
| 80 | { |
|---|
| 81 | $group =& new XoopsGroup(); |
|---|
| 82 | if ($isNew) { |
|---|
| 83 | $group->setNew(); |
|---|
| 84 | } |
|---|
| 85 | return $group; |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | /** |
|---|
| 89 | * retrieve a specific group |
|---|
| 90 | * |
|---|
| 91 | * @param int $id ID of the group to get |
|---|
| 92 | * @return object XoopsGroup reference to the group object, FALSE if failed |
|---|
| 93 | */ |
|---|
| 94 | function &get($id) |
|---|
| 95 | { |
|---|
| 96 | $ret = false; |
|---|
| 97 | if (intval($id) > 0) { |
|---|
| 98 | $sql = 'SELECT * FROM '.$this->db->prefix('groups').' WHERE groupid='.$id; |
|---|
| 99 | if ($result = $this->db->query($sql)) { |
|---|
| 100 | $numrows = $this->db->getRowsNum($result); |
|---|
| 101 | if ($numrows == 1) { |
|---|
| 102 | $group = new XoopsGroup(); |
|---|
| 103 | $group->assignVars($this->db->fetchArray($result)); |
|---|
| 104 | $ret =& $group; |
|---|
| 105 | } |
|---|
| 106 | } |
|---|
| 107 | } |
|---|
| 108 | return $ret; |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | /** |
|---|
| 112 | * insert a group into the database |
|---|
| 113 | * |
|---|
| 114 | * @param object reference to the group object |
|---|
| 115 | * @return mixed ID of the group if inserted, FALSE if failed, TRUE if already present and unchanged. |
|---|
| 116 | */ |
|---|
| 117 | function insert(&$group) |
|---|
| 118 | { |
|---|
| 119 | if (strtolower(get_class($group)) != 'xoopsgroup') { |
|---|
| 120 | return false; |
|---|
| 121 | } |
|---|
| 122 | if (!$group->isDirty()) { |
|---|
| 123 | return true; |
|---|
| 124 | } |
|---|
| 125 | if (!$group->cleanVars()) { |
|---|
| 126 | return false; |
|---|
| 127 | } |
|---|
| 128 | foreach ($group->cleanVars as $k => $v) { |
|---|
| 129 | ${$k} = $v; |
|---|
| 130 | } |
|---|
| 131 | if ($group->isNew()) { |
|---|
| 132 | $groupid = $this->db->genId('group_groupid_seq'); |
|---|
| 133 | $sql = sprintf("INSERT INTO %s (groupid, name, description, group_type) VALUES (%u, %s, %s, %s)", $this->db->prefix('groups'), $groupid, $this->db->quoteString($name), $this->db->quoteString($description), $this->db->quoteString($group_type)); |
|---|
| 134 | } else { |
|---|
| 135 | $sql = sprintf("UPDATE %s SET name = %s, description = %s, group_type = %s WHERE groupid = %u", $this->db->prefix('groups'), $this->db->quoteString($name), $this->db->quoteString($description), $this->db->quoteString($group_type), $groupid); |
|---|
| 136 | } |
|---|
| 137 | if (!$result = $this->db->query($sql)) { |
|---|
| 138 | return false; |
|---|
| 139 | } |
|---|
| 140 | if (empty($groupid)) { |
|---|
| 141 | $groupid = $this->db->getInsertId(); |
|---|
| 142 | } |
|---|
| 143 | $group->assignVar('groupid', $groupid); |
|---|
| 144 | return true; |
|---|
| 145 | } |
|---|
| 146 | |
|---|
| 147 | /** |
|---|
| 148 | * remove a group from the database |
|---|
| 149 | * |
|---|
| 150 | * @param object $group reference to the group to be removed |
|---|
| 151 | * @return bool FALSE if failed |
|---|
| 152 | */ |
|---|
| 153 | function delete(&$group) |
|---|
| 154 | { |
|---|
| 155 | if (strtolower(get_class($group)) != 'xoopsgroup') { |
|---|
| 156 | return false; |
|---|
| 157 | } |
|---|
| 158 | $sql = sprintf("DELETE FROM %s WHERE groupid = %u", $this->db->prefix('groups'), $group->getVar('groupid')); |
|---|
| 159 | if (!$result = $this->db->query($sql)) { |
|---|
| 160 | return false; |
|---|
| 161 | } |
|---|
| 162 | return true; |
|---|
| 163 | } |
|---|
| 164 | |
|---|
| 165 | /** |
|---|
| 166 | * retrieve groups from the database |
|---|
| 167 | * |
|---|
| 168 | * @param object $criteria {@link CriteriaElement} with conditions for the groups |
|---|
| 169 | * @param bool $id_as_key should the groups' IDs be used as keys for the associative array? |
|---|
| 170 | * @return mixed Array of groups |
|---|
| 171 | */ |
|---|
| 172 | function &getObjects($criteria = null, $id_as_key = false) |
|---|
| 173 | { |
|---|
| 174 | $ret = array(); |
|---|
| 175 | $limit = $start = 0; |
|---|
| 176 | $sql = 'SELECT * FROM '.$this->db->prefix('groups'); |
|---|
| 177 | if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) { |
|---|
| 178 | $sql .= ' '.$criteria->renderWhere(); |
|---|
| 179 | $limit = $criteria->getLimit(); |
|---|
| 180 | $start = $criteria->getStart(); |
|---|
| 181 | } |
|---|
| 182 | $result = $this->db->query($sql, $limit, $start); |
|---|
| 183 | if (!$result) { |
|---|
| 184 | return $ret; |
|---|
| 185 | } |
|---|
| 186 | while ($myrow = $this->db->fetchArray($result)) { |
|---|
| 187 | $group =& new XoopsGroup(); |
|---|
| 188 | $group->assignVars($myrow); |
|---|
| 189 | if (!$id_as_key) { |
|---|
| 190 | $ret[] =& $group; |
|---|
| 191 | } else { |
|---|
| 192 | $ret[$myrow['groupid']] =& $group; |
|---|
| 193 | } |
|---|
| 194 | unset($group); |
|---|
| 195 | } |
|---|
| 196 | return $ret; |
|---|
| 197 | } |
|---|
| 198 | } |
|---|
| 199 | |
|---|
| 200 | /** |
|---|
| 201 | * membership of a user in a group |
|---|
| 202 | * |
|---|
| 203 | * @author Kazumi Ono <[email protected]> |
|---|
| 204 | * @copyright copyright (c) 2000-2003 XOOPS.org |
|---|
| 205 | * @package kernel |
|---|
| 206 | */ |
|---|
| 207 | class XoopsMembership extends XoopsObject |
|---|
| 208 | { |
|---|
| 209 | /** |
|---|
| 210 | * constructor |
|---|
| 211 | */ |
|---|
| 212 | function XoopsMembership() |
|---|
| 213 | { |
|---|
| 214 | $this->XoopsObject(); |
|---|
| 215 | $this->initVar('linkid', XOBJ_DTYPE_INT, null, false); |
|---|
| 216 | $this->initVar('groupid', XOBJ_DTYPE_INT, null, false); |
|---|
| 217 | $this->initVar('uid', XOBJ_DTYPE_INT, null, false); |
|---|
| 218 | } |
|---|
| 219 | } |
|---|
| 220 | |
|---|
| 221 | /** |
|---|
| 222 | * XOOPS membership handler class. (Singleton) |
|---|
| 223 | * |
|---|
| 224 | * This class is responsible for providing data access mechanisms to the data source |
|---|
| 225 | * of XOOPS group membership class objects. |
|---|
| 226 | * |
|---|
| 227 | * @author Kazumi Ono <[email protected]> |
|---|
| 228 | * @copyright copyright (c) 2000-2003 XOOPS.org |
|---|
| 229 | * @package kernel |
|---|
| 230 | */ |
|---|
| 231 | class XoopsMembershipHandler extends XoopsObjectHandler |
|---|
| 232 | { |
|---|
| 233 | |
|---|
| 234 | /** |
|---|
| 235 | * create a new membership |
|---|
| 236 | * |
|---|
| 237 | * @param bool $isNew should the new object be set to "new"? |
|---|
| 238 | * @return object XoopsMembership |
|---|
| 239 | */ |
|---|
| 240 | function &create($isNew = true) |
|---|
| 241 | { |
|---|
| 242 | $mship =& new XoopsMembership(); |
|---|
| 243 | if ($isNew) { |
|---|
| 244 | $mship->setNew(); |
|---|
| 245 | } |
|---|
| 246 | return $mship; |
|---|
| 247 | } |
|---|
| 248 | |
|---|
| 249 | /** |
|---|
| 250 | * retrieve a membership |
|---|
| 251 | * |
|---|
| 252 | * @param int $id ID of the membership to get |
|---|
| 253 | * @return mixed reference to the object if successful, else FALSE |
|---|
| 254 | */ |
|---|
| 255 | function &get($id) |
|---|
| 256 | { |
|---|
| 257 | $ret = false; |
|---|
| 258 | if (intval($id) > 0) { |
|---|
| 259 | $sql = 'SELECT * FROM '.$this->db->prefix('groups_users_link').' WHERE linkid='.$id; |
|---|
| 260 | if ($result = $this->db->query($sql)) { |
|---|
| 261 | $numrows = $this->db->getRowsNum($result); |
|---|
| 262 | if ($numrows == 1) { |
|---|
| 263 | $mship =& new XoopsMembership(); |
|---|
| 264 | $mship->assignVars($this->db->fetchArray($result)); |
|---|
| 265 | $ret =& $mship; |
|---|
| 266 | } |
|---|
| 267 | } |
|---|
| 268 | } |
|---|
| 269 | return $ret; |
|---|
| 270 | } |
|---|
| 271 | |
|---|
| 272 | /** |
|---|
| 273 | * inserts a membership in the database |
|---|
| 274 | * |
|---|
| 275 | * @param object $mship reference to the membership object |
|---|
| 276 | * @return bool TRUE if already in DB or successful, FALSE if failed |
|---|
| 277 | */ |
|---|
| 278 | function insert(&$mship) |
|---|
| 279 | { |
|---|
| 280 | if (strtolower(get_class($mship)) != 'xoopsmembership') { |
|---|
| 281 | return false; |
|---|
| 282 | } |
|---|
| 283 | if (!$mship->isDirty()) { |
|---|
| 284 | return true; |
|---|
| 285 | } |
|---|
| 286 | if (!$mship->cleanVars()) { |
|---|
| 287 | return false; |
|---|
| 288 | } |
|---|
| 289 | foreach ($mship->cleanVars as $k => $v) { |
|---|
| 290 | ${$k} = $v; |
|---|
| 291 | } |
|---|
| 292 | if ($mship->isNew()) { |
|---|
| 293 | $linkid = $this->db->genId('groups_users_link_linkid_seq'); |
|---|
| 294 | $sql = sprintf("INSERT INTO %s (linkid, groupid, uid) VALUES (%u, %u, %u)", $this->db->prefix('groups_users_link'), $linkid, $groupid, $uid); |
|---|
| 295 | } else { |
|---|
| 296 | $sql = sprintf("UPDATE %s SET groupid = %u, uid = %u WHERE linkid = %u", $this->db->prefix('groups_users_link'), $groupid, $uid, $linkid); |
|---|
| 297 | } |
|---|
| 298 | if (!$result = $this->db->query($sql)) { |
|---|
| 299 | return false; |
|---|
| 300 | } |
|---|
| 301 | if (empty($linkid)) { |
|---|
| 302 | $linkid = $this->db->getInsertId(); |
|---|
| 303 | } |
|---|
| 304 | $mship->assignVar('linkid', $linkid); |
|---|
| 305 | return true; |
|---|
| 306 | } |
|---|
| 307 | |
|---|
| 308 | /** |
|---|
| 309 | * delete a membership from the database |
|---|
| 310 | * |
|---|
| 311 | * @param object $mship reference to the membership object |
|---|
| 312 | * @return bool FALSE if failed |
|---|
| 313 | */ |
|---|
| 314 | function delete(&$mship) |
|---|
| 315 | { |
|---|
| 316 | if (strtolower(get_class($mship)) != 'xoopsmembership') { |
|---|
| 317 | return false; |
|---|
| 318 | } |
|---|
| 319 | $sql = sprintf("DELETE FROM %s WHERE linkid = %u", $this->db->prefix('groups_users_link'), $groupm->getVar('linkid')); |
|---|
| 320 | if (!$result = $this->db->query($sql)) { |
|---|
| 321 | return false; |
|---|
| 322 | } |
|---|
| 323 | return true; |
|---|
| 324 | } |
|---|
| 325 | |
|---|
| 326 | /** |
|---|
| 327 | * retrieve memberships from the database |
|---|
| 328 | * |
|---|
| 329 | * @param object $criteria {@link CriteriaElement} conditions to meet |
|---|
| 330 | * @param bool $id_as_key should the ID be used as the array's key? |
|---|
| 331 | * @return array array of references |
|---|
| 332 | */ |
|---|
| 333 | function &getObjects($criteria = null, $id_as_key = false) |
|---|
| 334 | { |
|---|
| 335 | $ret = array(); |
|---|
| 336 | $limit = $start = 0; |
|---|
| 337 | $sql = 'SELECT * FROM '.$this->db->prefix('groups_users_link'); |
|---|
| 338 | if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) { |
|---|
| 339 | $sql .= ' '.$criteria->renderWhere(); |
|---|
| 340 | $limit = $criteria->getLimit(); |
|---|
| 341 | $start = $criteria->getStart(); |
|---|
| 342 | } |
|---|
| 343 | $result = $this->db->query($sql, $limit, $start); |
|---|
| 344 | if (!$result) { |
|---|
| 345 | return $ret; |
|---|
| 346 | } |
|---|
| 347 | while ($myrow = $this->db->fetchArray($result)) { |
|---|
| 348 | $mship = new XoopsMembership(); |
|---|
| 349 | $mship->assignVars($myrow); |
|---|
| 350 | if (!$id_as_key) { |
|---|
| 351 | $ret[] =& $mship; |
|---|
| 352 | } else { |
|---|
| 353 | $ret[$myrow['linkid']] =& $mship; |
|---|
| 354 | } |
|---|
| 355 | unset($mship); |
|---|
| 356 | } |
|---|
| 357 | return $ret; |
|---|
| 358 | } |
|---|
| 359 | |
|---|
| 360 | /** |
|---|
| 361 | * count how many memberships meet the conditions |
|---|
| 362 | * |
|---|
| 363 | * @param object $criteria {@link CriteriaElement} conditions to meet |
|---|
| 364 | * @return int |
|---|
| 365 | */ |
|---|
| 366 | function getCount($criteria = null) |
|---|
| 367 | { |
|---|
| 368 | $sql = 'SELECT COUNT(*) FROM '.$this->db->prefix('groups_users_link'); |
|---|
| 369 | if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) { |
|---|
| 370 | $sql .= ' '.$criteria->renderWhere(); |
|---|
| 371 | } |
|---|
| 372 | $result = $this->db->query($sql); |
|---|
| 373 | if (!$result) { |
|---|
| 374 | return 0; |
|---|
| 375 | } |
|---|
| 376 | list($count) = $this->db->fetchRow($result); |
|---|
| 377 | return $count; |
|---|
| 378 | } |
|---|
| 379 | |
|---|
| 380 | /** |
|---|
| 381 | * delete all memberships meeting the conditions |
|---|
| 382 | * |
|---|
| 383 | * @param object $criteria {@link CriteriaElement} with conditions to meet |
|---|
| 384 | * @return bool |
|---|
| 385 | */ |
|---|
| 386 | function deleteAll($criteria = null) |
|---|
| 387 | { |
|---|
| 388 | $sql = 'DELETE FROM '.$this->db->prefix('groups_users_link'); |
|---|
| 389 | if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) { |
|---|
| 390 | $sql .= ' '.$criteria->renderWhere(); |
|---|
| 391 | } |
|---|
| 392 | if (!$result = $this->db->query($sql)) { |
|---|
| 393 | return false; |
|---|
| 394 | } |
|---|
| 395 | return true; |
|---|
| 396 | } |
|---|
| 397 | |
|---|
| 398 | /** |
|---|
| 399 | * retrieve groups for a user |
|---|
| 400 | * |
|---|
| 401 | * @param int $uid ID of the user |
|---|
| 402 | * @param bool $asobject should the groups be returned as {@link XoopsGroup} |
|---|
| 403 | * objects? FALSE returns associative array. |
|---|
| 404 | * @return array array of groups the user belongs to |
|---|
| 405 | */ |
|---|
| 406 | function getGroupsByUser($uid) |
|---|
| 407 | { |
|---|
| 408 | $ret = array(); |
|---|
| 409 | $sql = 'SELECT groupid FROM '.$this->db->prefix('groups_users_link').' WHERE uid='.intval($uid); |
|---|
| 410 | $result = $this->db->query($sql); |
|---|
| 411 | if (!$result) { |
|---|
| 412 | return $ret; |
|---|
| 413 | } |
|---|
| 414 | while ($myrow = $this->db->fetchArray($result)) { |
|---|
| 415 | $ret[] = $myrow['groupid']; |
|---|
| 416 | } |
|---|
| 417 | return $ret; |
|---|
| 418 | } |
|---|
| 419 | |
|---|
| 420 | /** |
|---|
| 421 | * retrieve users belonging to a group |
|---|
| 422 | * |
|---|
| 423 | * @param int $groupid ID of the group |
|---|
| 424 | * @param bool $asobject return users as {@link XoopsUser} objects? |
|---|
| 425 | * FALSE will return arrays |
|---|
| 426 | * @param int $limit number of entries to return |
|---|
| 427 | * @param int $start offset of first entry to return |
|---|
| 428 | * @return array array of users belonging to the group |
|---|
| 429 | */ |
|---|
| 430 | function getUsersByGroup($groupid, $limit=0, $start=0) |
|---|
| 431 | { |
|---|
| 432 | $ret = array(); |
|---|
| 433 | $sql = 'SELECT uid FROM '.$this->db->prefix('groups_users_link').' WHERE groupid='.intval($groupid); |
|---|
| 434 | $result = $this->db->query($sql, $limit, $start); |
|---|
| 435 | if (!$result) { |
|---|
| 436 | return $ret; |
|---|
| 437 | } |
|---|
| 438 | while ($myrow = $this->db->fetchArray($result)) { |
|---|
| 439 | $ret[] = $myrow['uid']; |
|---|
| 440 | } |
|---|
| 441 | return $ret; |
|---|
| 442 | } |
|---|
| 443 | } |
|---|
| 444 | ?> |
|---|