1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17:
18: defined('XOOPS_ROOT_PATH') || exit('Restricted access');
19:
20: 21: 22: 23: 24: 25: 26:
27: class XoopsGroup extends XoopsObject
28: {
29: 30: 31:
32: public function __construct()
33: {
34: parent::__construct();
35: $this->initVar('groupid', XOBJ_DTYPE_INT, null, false);
36: $this->initVar('name', XOBJ_DTYPE_TXTBOX, null, true, 100);
37: $this->initVar('description', XOBJ_DTYPE_TXTAREA, null, false);
38: $this->initVar('group_type', XOBJ_DTYPE_OTHER, null, false);
39: }
40:
41: 42: 43: 44: 45:
46: public function id($format = 'N')
47: {
48: return $this->getVar('groupid', $format);
49: }
50:
51: 52: 53: 54: 55:
56: public function groupid($format = '')
57: {
58: return $this->getVar('groupid', $format);
59: }
60:
61: 62: 63: 64: 65:
66: public function name($format = '')
67: {
68: return $this->getVar('name', $format);
69: }
70:
71: 72: 73: 74: 75:
76: public function description($format = '')
77: {
78: return $this->getVar('description', $format);
79: }
80:
81: 82: 83: 84: 85:
86: public function group_type($format = '')
87: {
88: return $this->getVar('group_type', $format);
89: }
90: }
91:
92: 93: 94: 95: 96: 97: 98: 99: 100: 101:
102: class XoopsGroupHandler extends XoopsObjectHandler
103: {
104: 105: 106: 107: 108: 109:
110: public $table;
111:
112: public function __construct(XoopsDatabase $db)
113: {
114: parent::__construct($db);
115: $this->table = $this->db->prefix('groups');
116: }
117:
118: 119: 120: 121: 122: 123: 124:
125: public function create($isNew = true)
126: {
127: $group = new XoopsGroup();
128: if ($isNew) {
129: $group->setNew();
130: }
131:
132: return $group;
133: }
134:
135: 136: 137: 138: 139: 140:
141: public function get($id)
142: {
143: $id = (int)$id;
144: $group = false;
145: if ($id > 0) {
146: $sql = 'SELECT * FROM ' . $this->db->prefix('groups') . ' WHERE groupid=' . $id;
147: if (!$result = $this->db->query($sql)) {
148: return $group;
149: }
150: $numrows = $this->db->getRowsNum($result);
151: if ($numrows == 1) {
152: $group = new XoopsGroup();
153: $group->assignVars($this->db->fetchArray($result));
154: }
155: }
156:
157: return $group;
158: }
159:
160: 161: 162: 163: 164: 165: 166:
167: public function insert(XoopsObject $group)
168: {
169: $className = 'XoopsGroup';
170: if (!($group instanceof $className)) {
171: return false;
172: }
173: if (!$group->isDirty()) {
174: return true;
175: }
176: if (!$group->cleanVars()) {
177: return false;
178: }
179: foreach ($group->cleanVars as $k => $v) {
180: ${$k} = $v;
181: }
182: if ($group->isNew()) {
183: $groupid = $this->db->genId('group_groupid_seq');
184: $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));
185: } else {
186: $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);
187: }
188: if (!$result = $this->db->query($sql)) {
189: return false;
190: }
191: if (empty($groupid)) {
192: $groupid = $this->db->getInsertId();
193: }
194: $group->assignVar('groupid', $groupid);
195:
196: return true;
197: }
198:
199: 200: 201: 202: 203: 204: 205:
206: public function delete(XoopsObject $group)
207: {
208: $className = 'XoopsGroup';
209: if (!($group instanceof $className)) {
210: return false;
211: }
212: $sql = sprintf('DELETE FROM %s WHERE groupid = %u', $this->db->prefix('groups'), $group->getVar('groupid'));
213: if (!$result = $this->db->query($sql)) {
214: return false;
215: }
216:
217: return true;
218: }
219:
220: 221: 222: 223: 224: 225: 226:
227: public function getObjects(CriteriaElement $criteria = null, $id_as_key = false)
228: {
229: $ret = array();
230: $limit = $start = 0;
231: $sql = 'SELECT * FROM ' . $this->db->prefix('groups');
232: if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
233: $sql .= ' ' . $criteria->renderWhere();
234: $limit = $criteria->getLimit();
235: $start = $criteria->getStart();
236: }
237: $result = $this->db->query($sql, $limit, $start);
238: if (!$result) {
239: return $ret;
240: }
241: while ($myrow = $this->db->fetchArray($result)) {
242: $group = new XoopsGroup();
243: $group->assignVars($myrow);
244: if (!$id_as_key) {
245: $ret[] =& $group;
246: } else {
247: $ret[$myrow['groupid']] = &$group;
248: }
249: unset($group);
250: }
251:
252: return $ret;
253: }
254: }
255:
256: 257: 258: 259: 260: 261: 262:
263: class XoopsMembership extends XoopsObject
264: {
265: 266: 267:
268: public function __construct()
269: {
270: parent::__construct();
271: $this->initVar('linkid', XOBJ_DTYPE_INT, null, false);
272: $this->initVar('groupid', XOBJ_DTYPE_INT, null, false);
273: $this->initVar('uid', XOBJ_DTYPE_INT, null, false);
274: }
275: }
276:
277: 278: 279: 280: 281: 282: 283: 284: 285: 286:
287: class XoopsMembershipHandler extends XoopsObjectHandler
288: {
289: 290: 291: 292: 293: 294:
295: public $table;
296:
297: public function __construct(XoopsDatabase $db)
298: {
299: parent::__construct($db);
300: $this->table = $this->db->prefix('groups_users_link');
301: }
302:
303: 304: 305: 306: 307: 308:
309: public function create($isNew = true)
310: {
311: $mship = new XoopsMembership();
312: if ($isNew) {
313: $mship->setNew();
314: }
315:
316: return $mship;
317: }
318:
319: 320: 321: 322: 323: 324:
325: public function get($id)
326: {
327: $id = (int)$id;
328: $mship = false;
329: if ($id > 0) {
330: $sql = 'SELECT * FROM ' . $this->db->prefix('groups_users_link') . ' WHERE linkid=' . $id;
331: if (!$result = $this->db->query($sql)) {
332: return $mship;
333: }
334: $numrows = $this->db->getRowsNum($result);
335: if ($numrows == 1) {
336: $mship = new XoopsMembership();
337: $mship->assignVars($this->db->fetchArray($result));
338: }
339: }
340:
341: return $mship;
342: }
343:
344: 345: 346: 347: 348: 349: 350:
351: public function insert(XoopsObject $mship)
352: {
353: $className = 'XoopsMembership';
354: if (!($mship instanceof $className)) {
355: return false;
356: }
357: if (!$mship->isDirty()) {
358: return true;
359: }
360: if (!$mship->cleanVars()) {
361: return false;
362: }
363: foreach ($mship->cleanVars as $k => $v) {
364: ${$k} = $v;
365: }
366: if ($mship->isNew()) {
367: $linkid = $this->db->genId('groups_users_link_linkid_seq');
368: $sql = sprintf('INSERT INTO %s (linkid, groupid, uid) VALUES (%u, %u, %u)', $this->db->prefix('groups_users_link'), $linkid, $groupid, $uid);
369: } else {
370: $sql = sprintf('UPDATE %s SET groupid = %u, uid = %u WHERE linkid = %u', $this->db->prefix('groups_users_link'), $groupid, $uid, $linkid);
371: }
372: if (!$result = $this->db->query($sql)) {
373: return false;
374: }
375: if (empty($linkid)) {
376: $linkid = $this->db->getInsertId();
377: }
378: $mship->assignVar('linkid', $linkid);
379:
380: return true;
381: }
382:
383: 384: 385: 386: 387: 388: 389:
390: public function delete(XoopsObject $mship)
391: {
392: $className = 'XoopsMembership';
393: if (!($mship instanceof $className)) {
394: return false;
395: }
396:
397: $sql = sprintf('DELETE FROM %s WHERE linkid = %u', $this->db->prefix('groups_users_link'), $groupm->getVar('linkid'));
398: if (!$result = $this->db->query($sql)) {
399: return false;
400: }
401:
402: return true;
403: }
404:
405: 406: 407: 408: 409: 410: 411:
412: public function getObjects(CriteriaElement $criteria = null, $id_as_key = false)
413: {
414: $ret = array();
415: $limit = $start = 0;
416: $sql = 'SELECT * FROM ' . $this->db->prefix('groups_users_link');
417: if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
418: $sql .= ' ' . $criteria->renderWhere();
419: $limit = $criteria->getLimit();
420: $start = $criteria->getStart();
421: }
422: $result = $this->db->query($sql, $limit, $start);
423: if (!$result) {
424: return $ret;
425: }
426: while ($myrow = $this->db->fetchArray($result)) {
427: $mship = new XoopsMembership();
428: $mship->assignVars($myrow);
429: if (!$id_as_key) {
430: $ret[] =& $mship;
431: } else {
432: $ret[$myrow['linkid']] = &$mship;
433: }
434: unset($mship);
435: }
436:
437: return $ret;
438: }
439:
440: 441: 442: 443: 444: 445:
446: public function getCount(CriteriaElement $criteria = null)
447: {
448: $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('groups_users_link');
449: if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
450: $sql .= ' ' . $criteria->renderWhere();
451: }
452: $result = $this->db->query($sql);
453: if (!$result) {
454: return 0;
455: }
456: list($count) = $this->db->fetchRow($result);
457:
458: return $count;
459: }
460:
461: 462: 463: 464: 465: 466:
467: public function deleteAll(CriteriaElement $criteria = null)
468: {
469: $sql = 'DELETE FROM ' . $this->db->prefix('groups_users_link');
470: if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
471: $sql .= ' ' . $criteria->renderWhere();
472: }
473: if (!$result = $this->db->query($sql)) {
474: return false;
475: }
476:
477: return true;
478: }
479:
480: 481: 482: 483: 484: 485: 486: 487: 488:
489: public function getGroupsByUser($uid)
490: {
491: $ret = array();
492: $sql = 'SELECT groupid FROM ' . $this->db->prefix('groups_users_link') . ' WHERE uid=' . (int)$uid;
493: $result = $this->db->query($sql);
494: if (!$result) {
495: return $ret;
496: }
497: while ($myrow = $this->db->fetchArray($result)) {
498: $ret[] = $myrow['groupid'];
499: }
500:
501: return $ret;
502: }
503:
504: 505: 506: 507: 508: 509: 510: 511: 512: 513:
514: public function getUsersByGroup($groupid, $limit = 0, $start = 0)
515: {
516: $ret = array();
517: $sql = 'SELECT uid FROM ' . $this->db->prefix('groups_users_link') . ' WHERE groupid=' . (int)$groupid;
518: $result = $this->db->query($sql, $limit, $start);
519: if (!$result) {
520: return $ret;
521: }
522: while ($myrow = $this->db->fetchArray($result)) {
523: $ret[] = $myrow['uid'];
524: }
525:
526: return $ret;
527: }
528: }
529: