1: <?php
2: /**
3: * XOOPS Kernel Class
4: *
5: * You may not change or alter any portion of this comment or credits
6: * of supporting developers from this source code or any supporting source code
7: * which is considered copyrighted (c) material of the original comment or credit authors.
8: * This program is distributed in the hope that it will be useful,
9: * but WITHOUT ANY WARRANTY; without even the implied warranty of
10: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11: *
12: * @copyright (c) 2000-2016 XOOPS Project (www.xoops.org)
13: * @license GNU GPL 2 (https://www.gnu.org/licenses/gpl-2.0.html)
14: * @package kernel
15: * @since 2.0.0
16: * @author Kazumi Ono (AKA onokazu) http://www.myweb.ne.jp/, http://jp.xoops.org/
17: */
18: defined('XOOPS_ROOT_PATH') || exit('Restricted access');
19:
20: /**
21: * Private Messages
22: *
23: * @author Kazumi Ono <onokazu@xoops.org>
24: * @copyright (c) 2000-2016 XOOPS Project (www.xoops.org)
25: *
26: * @package kernel
27: **/
28: class XoopsPrivmessage extends XoopsObject
29: {
30: //PHP 8.2 Dynamic properties deprecated
31: public $msg_id;
32: public $msg_image;
33: public $subject;
34: public $from_userid;
35: public $to_userid;
36: public $msg_time;
37: public $msg_text;
38: public $read_msg;
39:
40: /**
41: * constructor
42: **/
43: public function __construct()
44: {
45: parent::__construct();
46: $this->initVar('msg_id', XOBJ_DTYPE_INT, null, false);
47: $this->initVar('msg_image', XOBJ_DTYPE_OTHER, null, false, 100);
48: $this->initVar('subject', XOBJ_DTYPE_TXTBOX, null, true, 255);
49: $this->initVar('from_userid', XOBJ_DTYPE_INT, null, true);
50: $this->initVar('to_userid', XOBJ_DTYPE_INT, null, true);
51: $this->initVar('msg_time', XOBJ_DTYPE_OTHER, null, false);
52: $this->initVar('msg_text', XOBJ_DTYPE_TXTAREA, null, true);
53: $this->initVar('read_msg', XOBJ_DTYPE_INT, 0, false);
54: }
55:
56: /**
57: * Returns Class Base Variable msg_id
58: * @param string $format
59: * @return mixed
60: */
61: public function id($format = 'N')
62: {
63: return $this->getVar('msg_id', $format);
64: }
65:
66: /**
67: * Returns Class Base Variable msg_id
68: * @param string $format
69: * @return mixed
70: */
71: public function msg_id($format = '')
72: {
73: return $this->getVar('msg_id', $format);
74: }
75:
76: /**
77: * Returns Class Base Variable msg_image
78: * @param string $format
79: * @return mixed
80: */
81: public function msg_image($format = '')
82: {
83: return $this->getVar('msg_image', $format);
84: }
85:
86: /**
87: * Returns Class Base Variable subject
88: * @param string $format
89: * @return mixed
90: */
91: public function subject($format = '')
92: {
93: return $this->getVar('subject', $format);
94: }
95:
96: /**
97: * Returns Class Base Variable not_id
98: * @param string $format
99: * @return mixed
100: */
101: public function from_userid($format = '')
102: {
103: return $this->getVar('from_userid', $format);
104: }
105:
106: /**
107: * Returns Class Base Variable to_userid
108: * @param string $format
109: * @return mixed
110: */
111: public function to_userid($format = '')
112: {
113: return $this->getVar('to_userid', $format);
114: }
115:
116: /**
117: * Returns Class Base Variable msg_time
118: * @param string $format
119: * @return mixed
120: */
121: public function msg_time($format = '')
122: {
123: return $this->getVar('msg_time', $format);
124: }
125:
126: /**
127: * Returns Class Base Variable msg_text
128: * @param string $format
129: * @return mixed
130: */
131: public function msg_text($format = '')
132: {
133: return $this->getVar('msg_text', $format);
134: }
135:
136: /**
137: * Returns Class Base Variable read_msg
138: * @param string $format
139: * @return mixed
140: */
141: public function read_msg($format = '')
142: {
143: return $this->getVar('read_msg', $format);
144: }
145: }
146:
147: /**
148: * XOOPS private message handler class.
149: *
150: * This class is responsible for providing data access mechanisms to the data source
151: * of XOOPS private message class objects.
152: *
153: * @package kernel
154: *
155: * @author Kazumi Ono <onokazu@xoops.org>
156: * @copyright (c) 2000-2016 XOOPS Project (www.xoops.org)
157: *
158: *
159: * @todo Why is this not a XoopsPersistableObjectHandler?
160: */
161: class XoopsPrivmessageHandler extends XoopsObjectHandler
162: {
163: /**
164: * Create a new {@link XoopsPrivmessage} object
165: * @param bool $isNew Flag as "new"?
166: * @return XoopsPrivmessage
167: **/
168: public function create($isNew = true)
169: {
170: $pm = new XoopsPrivmessage();
171: if ($isNew) {
172: $pm->setNew();
173: }
174:
175: return $pm;
176: }
177:
178: /**
179: * Load a {@link XoopsPrivmessage} object
180: * @param int $id ID of the message
181: * @return XoopsPrivmessage|false
182: **/
183: public function get($id)
184: {
185: $pm = false;
186: $id = (int)$id;
187: if ($id > 0) {
188: $sql = 'SELECT * FROM ' . $this->db->prefix('priv_msgs') . ' WHERE msg_id=' . $id;
189: $result = $this->db->query($sql);
190: if (!$this->db->isResultSet($result)) {
191: return $pm;
192: }
193: $numrows = $this->db->getRowsNum($result);
194: if ($numrows == 1) {
195: $pm = new XoopsPrivmessage();
196: $pm->assignVars($this->db->fetchArray($result));
197: }
198: }
199:
200: return $pm;
201: }
202:
203: /**
204: * Insert a message in the database
205: *
206: * @param XoopsPrivmessage $pm {@link XoopsPrivmessage} object
207: * @param bool $force flag to force the query execution skip request method check, which might be required in some situations
208: * @param XoopsObject|XoopsPrivmessage $pm a XoopsMembership object
209: *
210: * @return bool true on success, otherwise false
211: **/
212: public function insert(XoopsObject $pm, $force = false)
213: {
214: $className = 'XoopsPrivmessage';
215: if (!($pm instanceof $className)) {
216: return false;
217: }
218:
219: if (!$pm->isDirty()) {
220: return true;
221: }
222: if (!$pm->cleanVars()) {
223: return false;
224: }
225: foreach ($pm->cleanVars as $k => $v) {
226: ${$k} = $v;
227: }
228: if ($pm->isNew()) {
229: $msg_id = $this->db->genId('priv_msgs_msg_id_seq');
230: $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);
231: } else {
232: $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);
233: }
234: $queryFunc = empty($force) ? 'query' : 'queryF';
235: if (!$result = $this->db->{$queryFunc}($sql)) {
236: return false;
237: }
238: if (empty($msg_id)) {
239: $msg_id = $this->db->getInsertId();
240: }
241: $pm->assignVar('msg_id', $msg_id);
242:
243: return true;
244: }
245:
246: /**
247: * Delete from the database
248: * @param XoopsPrivmessage $pm {@link XoopsPrivmessage} object
249: * @return bool
250: **/
251: public function delete(XoopsObject $pm)
252: {
253: $className = 'XoopsPrivmessage';
254: if (!($pm instanceof $className)) {
255: return false;
256: }
257:
258: if (!$result = $this->db->query(sprintf('DELETE FROM %s WHERE msg_id = %u', $this->db->prefix('priv_msgs'), $pm->getVar('msg_id')))) {
259: return false;
260: }
261:
262: return true;
263: }
264:
265: /**
266: * Load messages from the database
267: * @param CriteriaElement|CriteriaCompo $criteria {@link CriteriaElement} object
268: * @param bool $id_as_key use ID as key into the array?
269: * @return array Array of {@link XoopsPrivmessage} objects
270: **/
271: public function getObjects(CriteriaElement $criteria = null, $id_as_key = false)
272: {
273: $ret = array();
274: $limit = $start = 0;
275: $sql = 'SELECT * FROM ' . $this->db->prefix('priv_msgs');
276: if (isset($criteria) && \method_exists($criteria, 'renderWhere')) {
277: $sql .= ' ' . $criteria->renderWhere();
278: $sort = !in_array($criteria->getSort(), array(
279: 'msg_id',
280: 'msg_time',
281: 'from_userid')) ? 'msg_id' : $criteria->getSort();
282: $sql .= ' ORDER BY ' . $sort . ' ' . $criteria->getOrder();
283: $limit = $criteria->getLimit();
284: $start = $criteria->getStart();
285: }
286: $result = $this->db->query($sql, $limit, $start);
287: if (!$this->db->isResultSet($result)) {
288: return $ret;
289: }
290: /** @var array $myrow */
291: while (false !== ($myrow = $this->db->fetchArray($result))) {
292: $pm = new XoopsPrivmessage();
293: $pm->assignVars($myrow);
294: if (!$id_as_key) {
295: $ret[] =& $pm;
296: } else {
297: $ret[$myrow['msg_id']] =& $pm;
298: }
299: unset($pm);
300: }
301:
302: return $ret;
303: }
304:
305: /**
306: * Count message
307: * @param CriteriaElement|CriteriaCompo $criteria = null {@link CriteriaElement} object
308: * @return int
309: **/
310: public function getCount(CriteriaElement $criteria = null)
311: {
312: $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('priv_msgs');
313: if (isset($criteria) && \method_exists($criteria, 'renderWhere')) {
314: $sql .= ' ' . $criteria->renderWhere();
315: }
316: $result = $this->db->query($sql);
317: if (!$this->db->isResultSet($result)) {
318: return 0;
319: }
320: list($count) = $this->db->fetchRow($result);
321:
322: return (int)$count;
323: }
324:
325: /**
326: * Mark a message as read
327: * @param XoopsPrivmessage $pm {@link XoopsPrivmessage} object
328: * @return bool
329: **/
330: public function setRead(XoopsPrivmessage $pm)
331: {
332: /**
333: * @TODO: Change to if (!(class_exists($this->className) && $obj instanceof $this->className)) when going fully PHP5
334: */
335: if (!is_a($pm, 'xoopsprivmessage')) {
336: return false;
337: }
338:
339: $sql = sprintf('UPDATE %s SET read_msg = 1 WHERE msg_id = %u', $this->db->prefix('priv_msgs'), $pm->getVar('msg_id'));
340: if (!$this->db->queryF($sql)) {
341: return false;
342: }
343:
344: return true;
345: }
346: }
347: