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:
28: class XoopsPrivmessage extends XoopsObject
29: {
30: 31: 32:
33: public function __construct()
34: {
35: parent::__construct();
36: $this->initVar('msg_id', XOBJ_DTYPE_INT, null, false);
37: $this->initVar('msg_image', XOBJ_DTYPE_OTHER, null, false, 100);
38: $this->initVar('subject', XOBJ_DTYPE_TXTBOX, null, true, 255);
39: $this->initVar('from_userid', XOBJ_DTYPE_INT, null, true);
40: $this->initVar('to_userid', XOBJ_DTYPE_INT, null, true);
41: $this->initVar('msg_time', XOBJ_DTYPE_OTHER, null, false);
42: $this->initVar('msg_text', XOBJ_DTYPE_TXTAREA, null, true);
43: $this->initVar('read_msg', XOBJ_DTYPE_INT, 0, false);
44: }
45:
46: 47: 48: 49: 50:
51: public function id($format = 'N')
52: {
53: return $this->getVar('msg_id', $format);
54: }
55:
56: 57: 58: 59: 60:
61: public function msg_id($format = '')
62: {
63: return $this->getVar('msg_id', $format);
64: }
65:
66: 67: 68: 69: 70:
71: public function msg_image($format = '')
72: {
73: return $this->getVar('msg_image', $format);
74: }
75:
76: 77: 78: 79: 80:
81: public function subject($format = '')
82: {
83: return $this->getVar('subject', $format);
84: }
85:
86: 87: 88: 89: 90:
91: public function from_userid($format = '')
92: {
93: return $this->getVar('from_userid', $format);
94: }
95:
96: 97: 98: 99: 100:
101: public function to_userid($format = '')
102: {
103: return $this->getVar('to_userid', $format);
104: }
105:
106: 107: 108: 109: 110:
111: public function msg_time($format = '')
112: {
113: return $this->getVar('msg_time', $format);
114: }
115:
116: 117: 118: 119: 120:
121: public function msg_text($format = '')
122: {
123: return $this->getVar('msg_text', $format);
124: }
125:
126: 127: 128: 129: 130:
131: public function read_msg($format = '')
132: {
133: return $this->getVar('read_msg', $format);
134: }
135: }
136:
137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150:
151: class XoopsPrivmessageHandler extends XoopsObjectHandler
152: {
153: 154: 155: 156: 157:
158: public function create($isNew = true)
159: {
160: $pm = new XoopsPrivmessage();
161: if ($isNew) {
162: $pm->setNew();
163: }
164:
165: return $pm;
166: }
167:
168: 169: 170: 171: 172:
173: public function get($id)
174: {
175: $pm = false;
176: $id = (int)$id;
177: if ($id > 0) {
178: $sql = 'SELECT * FROM ' . $this->db->prefix('priv_msgs') . ' WHERE msg_id=' . $id;
179: if (!$result = $this->db->query($sql)) {
180: return $pm;
181: }
182: $numrows = $this->db->getRowsNum($result);
183: if ($numrows == 1) {
184: $pm = new XoopsPrivmessage();
185: $pm->assignVars($this->db->fetchArray($result));
186: }
187: }
188:
189: return $pm;
190: }
191:
192: 193: 194: 195: 196: 197: 198: 199: 200:
201: public function insert(XoopsObject $pm, $force = false)
202: {
203: $className = 'XoopsPrivmessage';
204: if (!($pm instanceof $className)) {
205: return false;
206: }
207:
208: if (!$pm->isDirty()) {
209: return true;
210: }
211: if (!$pm->cleanVars()) {
212: return false;
213: }
214: foreach ($pm->cleanVars as $k => $v) {
215: ${$k} = $v;
216: }
217: if ($pm->isNew()) {
218: $msg_id = $this->db->genId('priv_msgs_msg_id_seq');
219: $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);
220: } else {
221: $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);
222: }
223: $queryFunc = empty($force) ? 'query' : 'queryF';
224: if (!$result = $this->db->{$queryFunc}($sql)) {
225: return false;
226: }
227: if (empty($msg_id)) {
228: $msg_id = $this->db->getInsertId();
229: }
230: $pm->assignVar('msg_id', $msg_id);
231:
232: return true;
233: }
234:
235: 236: 237: 238: 239:
240: public function delete(XoopsObject $pm)
241: {
242: $className = 'XoopsPrivmessage';
243: if (!($pm instanceof $className)) {
244: return false;
245: }
246:
247: if (!$result = $this->db->query(sprintf('DELETE FROM %s WHERE msg_id = %u', $this->db->prefix('priv_msgs'), $pm->getVar('msg_id')))) {
248: return false;
249: }
250:
251: return true;
252: }
253:
254: 255: 256: 257: 258: 259:
260: public function getObjects(CriteriaElement $criteria = null, $id_as_key = false)
261: {
262: $ret = array();
263: $limit = $start = 0;
264: $sql = 'SELECT * FROM ' . $this->db->prefix('priv_msgs');
265: if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
266: $sql .= ' ' . $criteria->renderWhere();
267: $sort = !in_array($criteria->getSort(), array(
268: 'msg_id',
269: 'msg_time',
270: 'from_userid')) ? 'msg_id' : $criteria->getSort();
271: $sql .= ' ORDER BY ' . $sort . ' ' . $criteria->getOrder();
272: $limit = $criteria->getLimit();
273: $start = $criteria->getStart();
274: }
275: $result = $this->db->query($sql, $limit, $start);
276: if (!$result) {
277: return $ret;
278: }
279: while ($myrow = $this->db->fetchArray($result)) {
280: $pm = new XoopsPrivmessage();
281: $pm->assignVars($myrow);
282: if (!$id_as_key) {
283: $ret[] =& $pm;
284: } else {
285: $ret[$myrow['msg_id']] =& $pm;
286: }
287: unset($pm);
288: }
289:
290: return $ret;
291: }
292:
293: 294: 295: 296: 297:
298: public function getCount(CriteriaElement $criteria = null)
299: {
300: $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('priv_msgs');
301: if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
302: $sql .= ' ' . $criteria->renderWhere();
303: }
304: if (!$result = $this->db->query($sql)) {
305: return 0;
306: }
307: list($count) = $this->db->fetchRow($result);
308:
309: return $count;
310: }
311:
312: 313: 314: 315: 316:
317: public function setRead(XoopsPrivmessage $pm)
318: {
319: 320: 321:
322: if (!is_a($pm, 'xoopsprivmessage')) {
323: return false;
324: }
325:
326: $sql = sprintf('UPDATE %s SET read_msg = 1 WHERE msg_id = %u', $this->db->prefix('priv_msgs'), $pm->getVar('msg_id'));
327: if (!$this->db->queryF($sql)) {
328: return false;
329: }
330:
331: return true;
332: }
333: }
334: