XCL Web Application Platform 2.5.0
The XoopsCube Legacy Project
Loading...
Searching...
No Matches
privmessage.php
1<?php
13
14if (!defined('XOOPS_ROOT_PATH')) {
15 exit();
16}
17
18class XoopsPrivmessage extends XoopsObject
19{
20
24 public function __construct()
25 {
26 parent::__construct();
27 $this->initVar('msg_id', XOBJ_DTYPE_INT, null, false);
28 $this->initVar('msg_image', XOBJ_DTYPE_OTHER, 'icon1.gif', false, 100);
29 $this->initVar('subject', XOBJ_DTYPE_TXTBOX, null, true, 191);
30 $this->initVar('from_userid', XOBJ_DTYPE_INT, null, true);
31 $this->initVar('to_userid', XOBJ_DTYPE_INT, null, true);
32 $this->initVar('msg_time', XOBJ_DTYPE_OTHER, time(), false);
33 $this->initVar('msg_text', XOBJ_DTYPE_TXTAREA, null, true);
34 $this->initVar('read_msg', XOBJ_DTYPE_INT, 0, false);
35 }
36 public function XoopsPrivmessage()
37 {
38 return self::__construct();
39 }
40
41 public function &getFromUser()
42 {
43 $userHandler=xoops_gethandler('user');
44 $user=&$userHandler->get($this->getVar('from_userid'));
45 return $user;
46 }
47
48 public function isRead()
49 {
50 return 1 == $this->getVar('read_msg') ? true : false;
51 }
52}
53
62{
63
69 public function &create($isNew = true)
70 {
71 $pm =new XoopsPrivmessage();
72 if ($isNew) {
73 $pm->setNew();
74 }
75 return $pm;
76 }
77
83 public function &get($id)
84 {
85 $ret = false;
86 $id = (int)$id;
87 if ($id > 0) {
88 $sql = 'SELECT * FROM '.$this->db->prefix('priv_msgs').' WHERE msg_id='.$id;
89 if ($result = $this->db->query($sql)) {
90 $numrows = $this->db->getRowsNum($result);
91 if (1 == $numrows) {
92 $pm =new XoopsPrivmessage();
93 $pm->assignVars($this->db->fetchArray($result));
94 $ret =& $pm;
95 }
96 }
97 }
98 return $ret;
99 }
100
107 public function insert(&$pm, $force=false)
108 {
109 $msg_image = null;
110 $subject = null;
111 $from_userid = null;
112 $to_userid = null;
113 $msg_text = null;
114 $read_msg = null;
115 $msg_id = null;
116 if ('xoopsprivmessage' != strtolower(get_class($pm))) {
117 return false;
118 }
119 if (!$pm->isDirty()) {
120 return true;
121 }
122 if (!$pm->cleanVars()) {
123 return false;
124 }
125 foreach ($pm->cleanVars as $k => $v) {
126 ${$k} = $v;
127 }
128 if ($pm->isNew()) {
129 $msg_id = $this->db->genId('priv_msgs_msg_id_seq');
130 $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);
131 } else {
132 $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);
133 }
134
135 $result = $force ? $this->db->queryF($sql) : $this->db->query($sql);
136
137 if (!$result) {
138 return false;
139 }
140 if (empty($msg_id)) {
141 $msg_id = $this->db->getInsertId();
142 }
143 $pm->assignVar('msg_id', $msg_id);
144
145 return true;
146 }
147
153 public function delete(&$pm)
154 {
155 if ('xoopsprivmessage' != strtolower(get_class($pm))) {
156 return false;
157 }
158 if (!$result = $this->db->query(sprintf('DELETE FROM %s WHERE msg_id = %u', $this->db->prefix('priv_msgs'), $pm->getVar('msg_id')))) {
159 return false;
160 }
161 return true;
162 }
163
170 public function &getObjects($criteria = null, $id_as_key = false)
171 {
172 $ret = [];
173 $limit = $start = 0;
174 $sql = 'SELECT * FROM '.$this->db->prefix('priv_msgs');
175 if (isset($criteria) && $criteria instanceof \criteriaelement) {
176 $sql .= ' '.$criteria->renderWhere();
177 $sort = !in_array($criteria->getSort(), ['msg_id', 'msg_time', 'from_userid']) ? 'msg_id' : $criteria->getSort();
178 $sql .= ' ORDER BY '.$sort.' '.$criteria->getOrder();
179 $limit = $criteria->getLimit();
180 $start = $criteria->getStart();
181 }
182 $result = $this->db->query($sql, $limit, $start);
183 if (!$result) {
184 return $ret;
185 }
186 while ($myrow = $this->db->fetchArray($result)) {
187 $pm =new XoopsPrivmessage();
188 $pm->assignVars($myrow);
189 if (!$id_as_key) {
190 $ret[] =& $pm;
191 } else {
192 $ret[$myrow['msg_id']] =& $pm;
193 }
194 unset($pm);
195 }
196 return $ret;
197 }
198
207 public function &getObjectsByFromUid($uid, $start=0, $limit=20, $order = 'DESC')
208 {
209 $criteria=new Criteria('to_userid', $uid);
210 $criteria->addSort('msg_time', $order);
211 $criteria->setStart($start);
212 $criteria->setLimit($limit);
213 $ret =& $this->getObjects($criteria);
214
215 return $ret;
216 }
217
218 public function getCountByFromUid($uid)
219 {
220 return $this->getCount(new Criteria('to_userid', $uid));
221 }
222
223 public function getCountUnreadByFromUid($uid)
224 {
225 $criteria = new CriteriaCompo(new Criteria('read_msg', 0));
226 $criteria->add(new Criteria('to_userid', $uid));
227 return $this->getCount($criteria);
228 }
229
235 public function getCount($criteria = null)
236 {
237 $sql = 'SELECT COUNT(*) FROM '.$this->db->prefix('priv_msgs');
238 if (isset($criteria) && $criteria instanceof \criteriaelement) {
239 $sql .= ' '.$criteria->renderWhere();
240 }
241 if (!$result = $this->db->query($sql)) {
242 return 0;
243 }
244 [$count] = $this->db->fetchRow($result);
245 return $count;
246 }
247
253 public function setRead(&$pm)
254 {
255 if ('xoopsprivmessage' != strtolower(get_class($pm))) {
256 return false;
257 }
258 $sql = sprintf('UPDATE %s SET read_msg = 1 WHERE msg_id = %u', $this->db->prefix('priv_msgs'), $pm->getVar('msg_id'));
259 if (!$this->db->queryF($sql)) {
260 return false;
261 }
262 return true;
263 }
264}
initVar($key, $data_type, $value=null, $required=false, $maxlength=null, $options='')
Definition object.php:206
& getVar($key, $format='s')
Definition object.php:317
& getObjects($criteria=null, $id_as_key=false)
insert(&$pm, $force=false)
getCount($criteria=null)
& getObjectsByFromUid($uid, $start=0, $limit=20, $order='DESC')
& create($isNew=true)