| 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: | 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: | |
| 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: | |
| 58: | |
| 59: | |
| 60: |
|
| 61: | public function id($format = 'N')
|
| 62: | {
|
| 63: | return $this->getVar('msg_id', $format);
|
| 64: | }
|
| 65: |
|
| 66: | |
| 67: | |
| 68: | |
| 69: | |
| 70: |
|
| 71: | public function msg_id($format = '')
|
| 72: | {
|
| 73: | return $this->getVar('msg_id', $format);
|
| 74: | }
|
| 75: |
|
| 76: | |
| 77: | |
| 78: | |
| 79: | |
| 80: |
|
| 81: | public function msg_image($format = '')
|
| 82: | {
|
| 83: | return $this->getVar('msg_image', $format);
|
| 84: | }
|
| 85: |
|
| 86: | |
| 87: | |
| 88: | |
| 89: | |
| 90: |
|
| 91: | public function subject($format = '')
|
| 92: | {
|
| 93: | return $this->getVar('subject', $format);
|
| 94: | }
|
| 95: |
|
| 96: | |
| 97: | |
| 98: | |
| 99: | |
| 100: |
|
| 101: | public function from_userid($format = '')
|
| 102: | {
|
| 103: | return $this->getVar('from_userid', $format);
|
| 104: | }
|
| 105: |
|
| 106: | |
| 107: | |
| 108: | |
| 109: | |
| 110: |
|
| 111: | public function to_userid($format = '')
|
| 112: | {
|
| 113: | return $this->getVar('to_userid', $format);
|
| 114: | }
|
| 115: |
|
| 116: | |
| 117: | |
| 118: | |
| 119: | |
| 120: |
|
| 121: | public function msg_time($format = '')
|
| 122: | {
|
| 123: | return $this->getVar('msg_time', $format);
|
| 124: | }
|
| 125: |
|
| 126: | |
| 127: | |
| 128: | |
| 129: | |
| 130: |
|
| 131: | public function msg_text($format = '')
|
| 132: | {
|
| 133: | return $this->getVar('msg_text', $format);
|
| 134: | }
|
| 135: |
|
| 136: | |
| 137: | |
| 138: | |
| 139: | |
| 140: |
|
| 141: | public function read_msg($format = '')
|
| 142: | {
|
| 143: | return $this->getVar('read_msg', $format);
|
| 144: | }
|
| 145: | }
|
| 146: |
|
| 147: | |
| 148: | |
| 149: | |
| 150: | |
| 151: | |
| 152: | |
| 153: | |
| 154: | |
| 155: | |
| 156: | |
| 157: | |
| 158: | |
| 159: | |
| 160: |
|
| 161: | class XoopsPrivmessageHandler extends XoopsObjectHandler
|
| 162: | {
|
| 163: | |
| 164: | |
| 165: | |
| 166: | |
| 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: | |
| 180: | |
| 181: | |
| 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: | |
| 205: | |
| 206: | |
| 207: | |
| 208: | |
| 209: | |
| 210: | |
| 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: | |
| 248: | |
| 249: | |
| 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: | |
| 267: | |
| 268: | |
| 269: | |
| 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: |
|
| 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: | |
| 307: | |
| 308: | |
| 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: | |
| 327: | |
| 328: | |
| 329: |
|
| 330: | public function setRead(XoopsPrivmessage $pm)
|
| 331: | {
|
| 332: | |
| 333: | |
| 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: | |