XCL Web Application Platform 2.5.0
The XoopsCube Legacy Project
Loading...
Searching...
No Matches
comment.php
1<?php
12
13if (!defined('XOOPS_ROOT_PATH')) {
14 exit();
15}
16
17
26class XoopsComment extends XoopsObject
27{
28
29 public function __construct()
30 {
31 parent::__construct();
32 $this->initVar('com_id', XOBJ_DTYPE_INT, 0, false);
33 $this->initVar('com_pid', XOBJ_DTYPE_INT, 0, false);
34 $this->initVar('com_modid', XOBJ_DTYPE_INT, null, false);
35 $this->initVar('com_icon', XOBJ_DTYPE_OTHER, null, false);
36 $this->initVar('com_title', XOBJ_DTYPE_TXTBOX, null, true, 255, true);
37 $this->initVar('com_text', XOBJ_DTYPE_TXTAREA, null, true, null, true);
38 $this->initVar('com_created', XOBJ_DTYPE_INT, time(), false);
39 $this->initVar('com_modified', XOBJ_DTYPE_INT, time(), false);
40 $this->initVar('com_uid', XOBJ_DTYPE_INT, 0, true);
41 $this->initVar('com_ip', XOBJ_DTYPE_OTHER, null, false);
42 $this->initVar('com_sig', XOBJ_DTYPE_INT, 0, false);
43 $this->initVar('com_itemid', XOBJ_DTYPE_INT, 0, false);
44 $this->initVar('com_rootid', XOBJ_DTYPE_INT, 0, false);
45 $this->initVar('com_status', XOBJ_DTYPE_INT, 0, false);
46 $this->initVar('com_exparams', XOBJ_DTYPE_OTHER, null, false, 255);
47 $this->initVar('dohtml', XOBJ_DTYPE_INT, 0, false);
48 $this->initVar('dosmiley', XOBJ_DTYPE_INT, 1, false);
49 $this->initVar('doxcode', XOBJ_DTYPE_INT, 1, false);
50 $this->initVar('doimage', XOBJ_DTYPE_INT, 0, false);
51 $this->initVar('dobr', XOBJ_DTYPE_INT, 1, false);
52 }
53 public function XoopsComment()
54 {
55 return self::__construct();
56 }
57
63 public function isRoot()
64 {
65 return ($this->getVar('com_id') == $this->getVar('com_rootid'));
66 }
67
72 public function &createChild()
73 {
74 $ret=new XoopsComment();
75 $ret->setNew();
76 $ret->setVar('com_pid', $this->getVar('com_id'));
77 $ret->setVar('com_rootid', $this->getVar('com_rootid'));
78 $ret->setVar('com_modid', $this->getVar('com_modid'));
79 $ret->setVar('com_itemid', $this->getVar('com_itemid'));
80 $ret->setVar('com_exparams', $this->getVar('com_exparams'));
81
82 $title = $this->get('com_title');
83 if (preg_match('/^Re:(.+)$/', $title, $matches)) {
84 $ret->set('com_title', 'Re[2]: ' . $matches[1]);
85 } elseif (preg_match("/^Re\[(\d+)\]:(.+)$/", $title, $matches)) {
86 $ret->set('com_title', 'Re[' . ($matches[1] + 1) . ']: ' . $matches[2]);
87 } elseif (!preg_match('/^re:/i', $title)) {
88 $ret->set('com_title', 'Re: ' . xoops_substr($title, 0, 56));
89 } else {
90 $ret->set('com_title', $title);
91 }
92
93 return $ret;
94 }
95}
96
111{
112
120 public function &create($isNew = true)
121 {
122 $comment =new XoopsComment();
123 if ($isNew) {
124 $comment->setNew();
125 }
126 return $comment;
127 }
128
136 public function &get($id)
137 {
138 $ret = false;
139 $id = (int)$id;
140 if ($id > 0) {
141 $sql = 'SELECT * FROM '.$this->db->prefix('xoopscomments').' WHERE com_id='.$id;
142 if ($result = $this->db->query($sql)) {
143 $numrows = $this->db->getRowsNum($result);
144 if (1 == $numrows) {
145 $comment = new XoopsComment();
146 $comment->assignVars($this->db->fetchArray($result));
147 $ret =& $comment;
148 }
149 }
150 }
151 return $ret;
152 }
153
161 public function insert(&$comment)
162 {
163 $com_pid = null;
164 $com_modid = null;
165 $com_icon = null;
166 $com_title = null;
167 $com_text = null;
168 $com_created = null;
169 $com_modified = null;
170 $com_uid = null;
171 $com_ip = null;
172 $com_sig = null;
173 $com_itemid = null;
174 $com_rootid = null;
175 $com_status = null;
176 $com_exparams = null;
177 $dohtml = null;
178 $dosmiley = null;
179 $doxcode = null;
180 $doimage = null;
181 $dobr = null;
182 $com_id = null;
183 if ('xoopscomment' != strtolower(get_class($comment))) {
184 return false;
185 }
186 if (!$comment->isDirty()) {
187 return true;
188 }
189 if (!$comment->cleanVars()) {
190 return false;
191 }
192 foreach ($comment->cleanVars as $k => $v) {
193 ${$k} = $v;
194 }
195 if ($comment->isNew()) {
196 $com_id = $this->db->genId('xoopscomments_com_id_seq');
197 $sql = sprintf(
198 '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);
199 } else {
200 $sql = sprintf(
201 '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);
202 }
203 if (!$result = $this->db->query($sql)) {
204 return false;
205 }
206 if (empty($com_id)) {
207 $com_id = $this->db->getInsertId();
208 }
209 $comment->assignVar('com_id', $com_id);
210 return true;
211 }
212
220 public function delete(&$comment)
221 {
222 if ('xoopscomment' != strtolower(get_class($comment))) {
223 return false;
224 }
225 $sql = sprintf('DELETE FROM %s WHERE com_id = %u', $this->db->prefix('xoopscomments'), $comment->getVar('com_id'));
226 if (!$result = $this->db->query($sql)) {
227 return false;
228 }
229 return true;
230 }
231
240 public function &getObjects($criteria = null, $id_as_key = false)
241 {
242 $ret = [];
243 $limit = $start = 0;
244 $sql = 'SELECT * FROM '.$this->db->prefix('xoopscomments');
245 if (isset($criteria) && $criteria instanceof \criteriaelement) {
246 $sql .= ' '.$criteria->renderWhere();
247 $sort = ('' != $criteria->getSort()) ? $criteria->getSort() : 'com_id';
248 $sql .= ' ORDER BY '.$sort.' '.$criteria->getOrder();
249 $limit = $criteria->getLimit();
250 $start = $criteria->getStart();
251 }
252 $result = $this->db->query($sql, $limit, $start);
253 if (!$result) {
254 return $ret;
255 }
256 while ($myrow = $this->db->fetchArray($result)) {
257 $comment = new XoopsComment();
258 $comment->assignVars($myrow);
259 if (!$id_as_key) {
260 $ret[] =& $comment;
261 } else {
262 $ret[$myrow['com_id']] =& $comment;
263 }
264 unset($comment);
265 }
266 return $ret;
267 }
268
276 public function getCount($criteria = null)
277 {
278 $sql = 'SELECT COUNT(*) FROM '.$this->db->prefix('xoopscomments');
279 if (isset($criteria) && $criteria instanceof \criteriaelement) {
280 $sql .= ' '.$criteria->renderWhere();
281 }
282 if (!$result =& $this->db->query($sql)) {
283 return 0;
284 }
285 [$count] = $this->db->fetchRow($result);
286 return $count;
287 }
288
296 public function deleteAll($criteria = null)
297 {
298 $sql = 'DELETE FROM '.$this->db->prefix('xoopscomments');
299 if (isset($criteria) && $criteria instanceof \criteriaelement) {
300 $sql .= ' '.$criteria->renderWhere();
301 }
302 if (!$result = $this->db->query($sql)) {
303 return false;
304 }
305 return true;
306 }
307
315 public function &getList($criteria = null)
316 {
317 $comments =& $this->getObjects($criteria, true);
318 $ret = [];
319 foreach (array_keys($comments) as $i) {
320 $ret[$i] = $comments[$i]->getVar('com_title');
321 }
322 return $ret;
323 }
324
337 public function &getByItemId($module_id, $item_id, $order = null, $status = null, $limit = null, $start = 0)
338 {
339 $criteria = new CriteriaCompo(new Criteria('com_modid', (int)$module_id));
340 $criteria->add(new Criteria('com_itemid', (int)$item_id));
341 if (isset($status)) {
342 $criteria->add(new Criteria('com_status', (int)$status));
343 }
344 if (isset($order)) {
345 $criteria->setOrder($order);
346 }
347 if (isset($limit)) {
348 $criteria->setLimit($limit);
349 $criteria->setStart($start);
350 }
351 return $this->getObjects($criteria);
352 }
353
363 public function &getCountByItemId($module_id, $item_id, $status = null)
364 {
365 $criteria = new CriteriaCompo(new Criteria('com_modid', (int)$module_id));
366 $criteria->add(new Criteria('com_itemid', (int)$item_id));
367 if (isset($status)) {
368 $criteria->add(new Criteria('com_status', (int)$status));
369 }
370 return $this->getCount($criteria);
371 }
372
373
384 public function &getTopComments($module_id, $item_id, $order, $status = null)
385 {
386 $criteria = new CriteriaCompo(new Criteria('com_modid', (int)$module_id));
387 $criteria->add(new Criteria('com_itemid', (int)$item_id));
388 $criteria->add(new Criteria('com_pid', 0));
389 if (isset($status)) {
390 $criteria->add(new Criteria('com_status', (int)$status));
391 }
392 $criteria->setOrder($order);
393 $ret =& $this->getObjects($criteria);
394 return $ret;
395 }
396
406 public function &getThread($comment_rootid, $comment_id, $status = null)
407 {
408 $criteria = new CriteriaCompo(new Criteria('com_rootid', (int)$comment_rootid));
409 $criteria->add(new Criteria('com_id', (int)$comment_id, '>='));
410 if (isset($status)) {
411 $criteria->add(new Criteria('com_status', (int)$status));
412 }
413 return $this->getObjects($criteria);
414 }
415
425 public function updateByField(&$comment, $field_name, $field_value)
426 {
427 $comment->unsetNew();
428 $comment->setVar($field_name, $field_value);
429 return $this->insert($comment);
430 }
431
438 public function deleteByModule($module_id)
439 {
440 return $this->deleteAll(new Criteria('com_modid', (int)$module_id));
441 }
442
449/*
450 function updateAll($fieldname, $fieldvalue, $criteria = null)
451 {
452 $set_clause = is_numeric($fieldvalue) ? $filedname.' = '.$fieldvalue : $filedname.' = '.$this->db->quoteString($fieldvalue);
453 $sql = 'UPDATE '.$this->db->prefix('xoopscomments').' SET '.$set_clause;
454 if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
455 $sql .= ' '.$criteria->renderWhere();
456 }
457 if (!$result = $this->db->query($sql)) {
458 return false;
459 }
460 return true;
461 }
462*/
463
464 public function getChildObjects(&$comment)
465 {
466 $ret= [];
467
468 $table=$this->db->prefix('xoopscomments');
469 $sql = "SELECT * FROM " . $table . " WHERE com_pid=" . $comment->getVar('com_id') . ' AND com_id<>' . $comment->getVar('com_id');
470 $result=$this->db->query($sql);
471 while ($row=$this->db->fetchArray($result)) {
472 $comment=new XoopsComment();
473 $comment->assignVars($row);
474 $ret[]=&$comment;
475 unset($comment);
476 }
477
478 return $ret;
479 }
480
481 public function deleteWithChild(&$comment)
482 {
483 foreach ($this->getChildObjects($comment) as $child) {
484 $this->deleteWithChild($child);
485 }
486 $this->delete($comment);
487
488 return true; // TODO
489 }
490}
& getObjects($criteria=null, $id_as_key=false)
Definition comment.php:240
getChildObjects(&$comment)
Definition comment.php:464
deleteAll($criteria=null)
Definition comment.php:296
& getCountByItemId($module_id, $item_id, $status=null)
Definition comment.php:363
& getList($criteria=null)
Definition comment.php:315
& getTopComments($module_id, $item_id, $order, $status=null)
Definition comment.php:384
updateByField(&$comment, $field_name, $field_value)
Definition comment.php:425
& getThread($comment_rootid, $comment_id, $status=null)
Definition comment.php:406
deleteByModule($module_id)
Definition comment.php:438
getCount($criteria=null)
Definition comment.php:276
& getByItemId($module_id, $item_id, $order=null, $status=null, $limit=null, $start=0)
Definition comment.php:337
insert(&$comment)
Definition comment.php:161
& create($isNew=true)
Definition comment.php:120
& createChild()
Definition comment.php:72
initVar($key, $data_type, $value=null, $required=false, $maxlength=null, $options='')
Definition object.php:206
& getVar($key, $format='s')
Definition object.php:317