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: if (!defined('XOOPS_ROOT_PATH')) {
32: exit();
33: }
34: 35: 36: 37: 38: 39: 40: 41: 42: 43:
44: class XoopsPrivmessage extends XoopsObject
45: {
46:
47: 48: 49:
50: function XoopsPrivmessage()
51: {
52: $this->XoopsObject();
53: $this->initVar('msg_id', XOBJ_DTYPE_INT, null, false);
54: $this->initVar('msg_image', XOBJ_DTYPE_OTHER, 'icon1.gif', false, 100);
55: $this->initVar('subject', XOBJ_DTYPE_TXTBOX, null, true, 255);
56: $this->initVar('from_userid', XOBJ_DTYPE_INT, null, true);
57: $this->initVar('to_userid', XOBJ_DTYPE_INT, null, true);
58: $this->initVar('msg_time', XOBJ_DTYPE_OTHER, time(), false);
59: $this->initVar('msg_text', XOBJ_DTYPE_TXTAREA, null, true);
60: $this->initVar('read_msg', XOBJ_DTYPE_INT, 0, false);
61: }
62:
63: function &getFromUser()
64: {
65: $userHandler=xoops_gethandler('user');
66: $user=&$userHandler->get($this->getVar('from_userid'));
67: return $user;
68: }
69:
70: function isRead()
71: {
72: return $this->getVar('read_msg')==1 ? true : false;
73: }
74: }
75:
76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88:
89: class XoopsPrivmessageHandler extends XoopsObjectHandler
90: {
91:
92: 93: 94: 95: 96:
97: function &create($isNew = true)
98: {
99: $pm =new XoopsPrivmessage();
100: if ($isNew) {
101: $pm->setNew();
102: }
103: return $pm;
104: }
105:
106: 107: 108: 109: 110:
111: function &get($id)
112: {
113: $ret = false;
114: $id = (int)$id;
115: if ($id > 0) {
116: $sql = 'SELECT * FROM '.$this->db->prefix('priv_msgs').' WHERE msg_id='.$id;
117: if ($result = $this->db->query($sql)) {
118: $numrows = $this->db->getRowsNum($result);
119: if ($numrows == 1) {
120: $pm =new XoopsPrivmessage();
121: $pm->assignVars($this->db->fetchArray($result));
122: $ret =& $pm;
123: }
124: }
125: }
126: return $ret;
127: }
128:
129: 130: 131: 132: 133:
134: function insert(&$pm,$force=false)
135: {
136: if (strtolower(get_class($pm)) != 'xoopsprivmessage') {
137: return false;
138: }
139: if (!$pm->isDirty()) {
140: return true;
141: }
142: if (!$pm->cleanVars()) {
143: return false;
144: }
145: foreach ($pm->cleanVars as $k => $v) {
146: ${$k} = $v;
147: }
148: if ($pm->isNew()) {
149: $msg_id = $this->db->genId('priv_msgs_msg_id_seq');
150: $sql = sprintf("INSERT INTO %s (msg_id, msg_image, subject, from_userid, to_userid, msg_time, msg_text, read_msg) VALUES (%u, %s, %s, %u, %u, %u, %s, %u)", $this->db->prefix('priv_msgs'), $msg_id, $this->db->quoteString($msg_image), $this->db->quoteString($subject), $from_userid, $to_userid, time(), $this->db->quoteString($msg_text), 0);
151: } else {
152: $sql = sprintf("UPDATE %s SET msg_image = %s, subject = %s, from_userid = %u, to_userid = %u, msg_text = %s, read_msg = %u WHERE msg_id = %u", $this->db->prefix('priv_msgs'), $this->db->quoteString($msg_image), $this->db->quoteString($subject), $from_userid, $to_userid, $this->db->quoteString($msg_text), $read_msg, $msg_id);
153: }
154:
155: $result = $force ? $this->db->queryF($sql) : $this->db->query($sql);
156:
157: if (!$result) {
158: return false;
159: }
160: if (empty($msg_id)) {
161: $msg_id = $this->db->getInsertId();
162: }
163: $pm->assignVar('msg_id', $msg_id);
164:
165: return true;
166: }
167:
168: 169: 170: 171: 172:
173: function delete(&$pm)
174: {
175: if (strtolower(get_class($pm)) != 'xoopsprivmessage') {
176: return false;
177: }
178: if (!$result = $this->db->query(sprintf("DELETE FROM %s WHERE msg_id = %u", $this->db->prefix('priv_msgs'), $pm->getVar('msg_id')))) {
179: return false;
180: }
181: return true;
182: }
183:
184: 185: 186: 187: 188: 189:
190: function &getObjects($criteria = null, $id_as_key = false)
191: {
192: $ret = array();
193: $limit = $start = 0;
194: $sql = 'SELECT * FROM '.$this->db->prefix('priv_msgs');
195: if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
196: $sql .= ' '.$criteria->renderWhere();
197: $sort = !in_array($criteria->getSort(), array('msg_id', 'msg_time', 'from_userid')) ? 'msg_id' : $criteria->getSort();
198: $sql .= ' ORDER BY '.$sort.' '.$criteria->getOrder();
199: $limit = $criteria->getLimit();
200: $start = $criteria->getStart();
201: }
202: $result = $this->db->query($sql, $limit, $start);
203: if (!$result) {
204: return $ret;
205: }
206: while ($myrow = $this->db->fetchArray($result)) {
207: $pm =new XoopsPrivmessage();
208: $pm->assignVars($myrow);
209: if (!$id_as_key) {
210: $ret[] =& $pm;
211: } else {
212: $ret[$myrow['msg_id']] =& $pm;
213: }
214: unset($pm);
215: }
216: return $ret;
217: }
218:
219: 220: 221: 222: 223:
224: function &getObjectsByFromUid($uid,$start=0,$limit=20,$order = 'DESC')
225: {
226: $criteria=new Criteria('to_userid',$uid);
227: $criteria->addSort('msg_time', $order);
228: $criteria->setStart($start);
229: $criteria->setLimit($limit);
230: $ret =& $this->getObjects($criteria);
231:
232: return $ret;
233: }
234:
235: function getCountByFromUid($uid)
236: {
237: return $this->getCount(new Criteria('to_userid',$uid));
238: }
239:
240: function getCountUnreadByFromUid($uid)
241: {
242: $criteria = new CriteriaCompo(new Criteria('read_msg', 0));
243: $criteria->add(new Criteria('to_userid', $uid));
244: return $this->getCount($criteria);
245: }
246:
247: 248: 249: 250: 251:
252: function getCount($criteria = null)
253: {
254: $sql = 'SELECT COUNT(*) FROM '.$this->db->prefix('priv_msgs');
255: if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
256: $sql .= ' '.$criteria->renderWhere();
257: }
258: if (!$result = $this->db->query($sql)) {
259: return 0;
260: }
261: list($count) = $this->db->fetchRow($result);
262: return $count;
263: }
264:
265: 266: 267: 268: 269:
270: function setRead(&$pm)
271: {
272: if (strtolower(get_class($pm)) != 'xoopsprivmessage') {
273: return false;
274: }
275: $sql = sprintf("UPDATE %s SET read_msg = 1 WHERE msg_id = %u", $this->db->prefix('priv_msgs'), $pm->getVar('msg_id'));
276: if (!$this->db->queryF($sql)) {
277: return false;
278: }
279: return true;
280: }
281: }
282: ?>
283: