1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10:
11:
12: 13: 14: 15: 16: 17: 18: 19:
20:
21: use Xoops\Core\Database\Connection;
22: use Xoops\Core\Kernel\XoopsObject;
23: use Xoops\Core\Kernel\XoopsPersistableObjectHandler;
24:
25: 26: 27: 28: 29: 30:
31: class CommentsComment extends XoopsObject
32: {
33:
34: 35: 36:
37: public function __construct()
38: {
39: $this->initVar('id', XOBJ_DTYPE_INT, 0, false);
40: $this->initVar('pid', XOBJ_DTYPE_INT, 0, false);
41: $this->initVar('modid', XOBJ_DTYPE_INT, null, false);
42: $this->initVar('icon', XOBJ_DTYPE_OTHER, '', false);
43: $this->initVar('title', XOBJ_DTYPE_TXTBOX, null, true, 255, true);
44: $this->initVar('text', XOBJ_DTYPE_TXTAREA, '', true, null, true);
45: $this->initVar('created', XOBJ_DTYPE_INT, 0, false);
46: $this->initVar('modified', XOBJ_DTYPE_INT, 0, false);
47: $this->initVar('uid', XOBJ_DTYPE_INT, 0, true);
48: $this->initVar('ip', XOBJ_DTYPE_OTHER, null, false);
49: $this->initVar('sig', XOBJ_DTYPE_INT, 0, false);
50: $this->initVar('itemid', XOBJ_DTYPE_INT, 0, false);
51: $this->initVar('rootid', XOBJ_DTYPE_INT, 0, false);
52: $this->initVar('status', XOBJ_DTYPE_INT, 0, false);
53: $this->initVar('exparams', XOBJ_DTYPE_OTHER, null, false, 255);
54: $this->initVar('dohtml', XOBJ_DTYPE_INT, 0, false);
55: $this->initVar('dosmiley', XOBJ_DTYPE_INT, 1, false);
56: $this->initVar('doxcode', XOBJ_DTYPE_INT, 1, false);
57: $this->initVar('doimage', XOBJ_DTYPE_INT, 1, false);
58: $this->initVar('dobr', XOBJ_DTYPE_INT, 1, false);
59: }
60:
61: 62: 63:
64: public function isRoot()
65: {
66: return ($this->getVar('id') == $this->getVar('rootid'));
67: }
68: }
69:
70: 71: 72: 73: 74: 75:
76: class CommentsCommentHandler extends XoopsPersistableObjectHandler
77: {
78: 79: 80: 81: 82:
83: public function __construct(Connection $db = null)
84: {
85: parent::__construct($db, 'comments', 'CommentsComment', 'id', 'title');
86: }
87:
88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99:
100: public function getByItemId($module_id, $item_id, $order = null, $status = null, $limit = null, $start = 0)
101: {
102: $criteria = new CriteriaCompo(new Criteria('modid', (int)($module_id)));
103: $criteria->add(new Criteria('itemid', (int)($item_id)));
104: if (isset($status)) {
105: $criteria->add(new Criteria('status', (int)($status)));
106: }
107: if (isset($order)) {
108: $criteria->setOrder($order);
109: }
110: if (isset($limit)) {
111: $criteria->setLimit($limit);
112: $criteria->setStart($start);
113: }
114: return $this->getObjects($criteria);
115: }
116:
117: 118: 119: 120: 121: 122: 123: 124: 125:
126: public function getCountByItemId($module_id, $item_id, $status = null)
127: {
128: $criteria = new CriteriaCompo(new Criteria('modid', (int)($module_id)));
129: $criteria->add(new Criteria('itemid', (int)($item_id)));
130: if (isset($status)) {
131: $criteria->add(new Criteria('status', (int)($status)));
132: }
133: return $this->getCount($criteria);
134: }
135:
136: 137: 138: 139: 140:
141: public function getCountByModuleId($module_id, $item_id = null)
142: {
143: $criteria = new CriteriaCompo(new Criteria('modid', (int)($module_id)));
144: if (isset($item_id)) {
145: $criteria->add(new Criteria('itemid', (int)($item_id)));
146: }
147: return $this->getCount($criteria);
148: }
149:
150: 151: 152: 153: 154: 155: 156: 157: 158: 159:
160: public function getTopComments($module_id, $item_id, $order, $status = null)
161: {
162: $criteria = new CriteriaCompo(new Criteria('modid', (int)($module_id)));
163: $criteria->add(new Criteria('itemid', (int)($item_id)));
164: $criteria->add(new Criteria('pid', 0));
165: if (isset($status)) {
166: $criteria->add(new Criteria('status', (int)($status)));
167: }
168: $criteria->setOrder($order);
169: return $this->getObjects($criteria);
170: }
171:
172: 173: 174: 175: 176: 177: 178: 179: 180:
181: public function getThread($comment_rootid, $comment_id, $status = null)
182: {
183: $criteria = new CriteriaCompo(new Criteria('rootid', (int)($comment_rootid)));
184: $criteria->add(new Criteria('id', (int)($comment_id), '>='));
185: if (isset($status)) {
186: $criteria->add(new Criteria('status', (int)($status)));
187: }
188: return $this->getObjects($criteria);
189: }
190:
191: 192: 193: 194: 195: 196: 197: 198: 199:
200: public function updateByField(CommentsComment $comment, $field_name, $field_value)
201: {
202: $comment->unsetNew();
203: $comment->setVar($field_name, $field_value);
204: return $this->insert($comment);
205: }
206:
207: 208: 209: 210: 211: 212:
213: public function deleteByModule($module_id)
214: {
215: return $this->deleteAll(new Criteria('modid', (int)($module_id)));
216: }
217:
218: 219: 220: 221: 222:
223: function deleteByItemId($module_id, $item_id)
224: {
225: $module_id = (int)($module_id);
226: $item_id = (int)($item_id);
227: if ($module_id > 0 && $item_id > 0) {
228: $comments = $this->getByItemId($module_id, $item_id);
229: if (is_array($comments)) {
230: $count = count($comments);
231: $deleted_num = array();
232: for ($i = 0; $i < $count; ++$i) {
233: if (false != $this->delete($comments[$i])) {
234:
235: $poster_id = $comments[$i]->getVar('uid');
236: if ($poster_id != 0) {
237: $deleted_num[$poster_id] = !isset($deleted_num[$poster_id]) ? 1
238: : ($deleted_num[$poster_id] + 1);
239: }
240: }
241: }
242:
243: $member_handler = Xoops::getInstance()->getHandlerMember();
244: foreach ($deleted_num as $user_id => $post_num) {
245:
246:
247: $poster = $member_handler->getUser($user_id);
248: if (is_object($poster)) {
249: $member_handler->updateUserByField($poster, 'posts', $poster->getVar('posts') - $post_num);
250: }
251: }
252: return true;
253: }
254: }
255: return false;
256: }
257: }
258: