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