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