source: temp/test-xoops.ec-cube.net/html/kernel/privmessage.php @ 405

Revision 405, 8.8 KB checked in by root, 20 years ago (diff)
Line 
1<?php
2// $Id: privmessage.php,v 1.3 2006/05/01 02:37:28 onokazu Exp $
3//  ------------------------------------------------------------------------ //
4//                XOOPS - PHP Content Management System                      //
5//                    Copyright (c) 2000 XOOPS.org                           //
6//                       <http://www.xoops.org/>                             //
7//  ------------------------------------------------------------------------ //
8//  This program is free software; you can redistribute it and/or modify     //
9//  it under the terms of the GNU General Public License as published by     //
10//  the Free Software Foundation; either version 2 of the License, or        //
11//  (at your option) any later version.                                      //
12//                                                                           //
13//  You may not change or alter any portion of this comment or credits       //
14//  of supporting developers from this source code or any supporting         //
15//  source code which is considered copyrighted (c) material of the          //
16//  original comment or credit authors.                                      //
17//                                                                           //
18//  This program is distributed in the hope that it will be useful,          //
19//  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
20//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
21//  GNU General Public License for more details.                             //
22//                                                                           //
23//  You should have received a copy of the GNU General Public License        //
24//  along with this program; if not, write to the Free Software              //
25//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
26//  ------------------------------------------------------------------------ //
27// Author: Kazumi Ono (AKA onokazu)                                          //
28// URL: http://www.myweb.ne.jp/, http://www.xoops.org/, http://jp.xoops.org/ //
29// Project: The XOOPS Project                                                //
30// ------------------------------------------------------------------------- //
31if (!defined('XOOPS_ROOT_PATH')) {
32    exit();
33}
34/**
35 * {description}
36 *
37 * @package     kernel
38 *
39 * @author      Kazumi Ono  <[email protected]>
40 * @copyright   copyright (c) 2000-2003 The XOOPS Project (http://www.xoops.org)
41 *
42 * @version     $Revision: 1.3 $ - $Date: 2006/05/01 02:37:28 $
43 */
44class XoopsPrivmessage extends XoopsObject
45{
46
47/**
48 * constructor
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, null, false);
59        $this->initVar('msg_text', XOBJ_DTYPE_TXTAREA, null, true);
60        $this->initVar('read_msg', XOBJ_DTYPE_INT, 0, false);
61    }
62}
63
64/**
65 * XOOPS private message handler class.
66 *
67 * This class is responsible for providing data access mechanisms to the data source
68 * of XOOPS private message class objects.
69 *
70 * @package     kernel
71 *
72 * @author      Kazumi Ono  <[email protected]>
73 * @copyright   copyright (c) 2000-2003 The XOOPS Project (http://www.xoops.org)
74 *
75 * @version     $Revision: 1.3 $ - $Date: 2006/05/01 02:37:28 $
76 */
77class XoopsPrivmessageHandler extends XoopsObjectHandler
78{
79
80/**
81 * Create a new {@link XoopsPrivmessage} object
82 * @param   bool    $isNew  Flag as "new"?
83 * @return  object
84 **/
85    function &create($isNew = true)
86    {
87        $pm =& new XoopsPrivmessage();
88        if ($isNew) {
89            $pm->setNew();
90        }
91        return $pm;
92    }
93
94/**
95 * Load a {@link XoopsPrivmessage} object
96 * @param   int     $id ID of the message
97 * @return  object
98 **/
99    function &get($id)
100    {
101        $ret = false;
102        $id = intval($id);
103        if ($id > 0) {
104            $sql = 'SELECT * FROM '.$this->db->prefix('priv_msgs').' WHERE msg_id='.$id;
105            if ($result = $this->db->query($sql)) {
106                $numrows = $this->db->getRowsNum($result);
107                if ($numrows == 1) {
108                    $pm =& new XoopsPrivmessage();
109                    $pm->assignVars($this->db->fetchArray($result));
110                    $ret =& $pm;
111                }
112            }
113        }
114        return $ret;
115    }
116
117/**
118 * Insert a message in the database
119 * @param   object  $pm     {@link XoopsPrivmessage} object
120 * @return  bool
121 **/
122    function insert(&$pm)
123    {
124        if (strtolower(get_class($pm)) != 'xoopsprivmessage') {
125            return false;
126        }
127        if (!$pm->isDirty()) {
128            return true;
129        }
130        if (!$pm->cleanVars()) {
131            return false;
132        }
133        foreach ($pm->cleanVars as $k => $v) {
134            ${$k} = $v;
135        }
136        if ($pm->isNew()) {
137            $msg_id = $this->db->genId('priv_msgs_msg_id_seq');
138            $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);
139        } else {
140            $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);
141        }
142        if (!$result = $this->db->query($sql)) {
143            return false;
144        }
145        if (empty($msg_id)) {
146            $msg_id = $this->db->getInsertId();
147        }
148        $pm->assignVar('msg_id', $msg_id);
149        return true;
150    }
151
152/**
153 * Delete from the database
154 * @param   object  $pm     {@link XoopsPrivmessage} object
155 * @return  bool
156 **/
157    function delete(&$pm)
158    {
159        if (strtolower(get_class($pm)) != 'xoopsprivmessage') {
160            return false;
161        }
162        if (!$result = $this->db->query(sprintf("DELETE FROM %s WHERE msg_id = %u", $this->db->prefix('priv_msgs'), $pm->getVar('msg_id')))) {
163            return false;
164        }
165        return true;
166    }
167
168/**
169 * Load messages from the database
170 * @param   object  $criteria   {@link CriteriaElement} object
171 * @param   bool    $id_as_key  use ID as key into the array?
172 * @return  array   Array of {@link XoopsPrivmessage} objects
173 **/
174    function &getObjects($criteria = null, $id_as_key = false)
175    {
176        $ret = array();
177        $limit = $start = 0;
178        $sql = 'SELECT * FROM '.$this->db->prefix('priv_msgs');
179        if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
180            $sql .= ' '.$criteria->renderWhere();
181            $sort = !in_array($criteria->getSort(), array('msg_id', 'msg_time', 'from_userid')) ? 'msg_id' : $criteria->getSort();
182            $sql .= ' ORDER BY '.$sort.' '.$criteria->getOrder();
183            $limit = $criteria->getLimit();
184            $start = $criteria->getStart();
185        }
186        $result = $this->db->query($sql, $limit, $start);
187        if (!$result) {
188            return $ret;
189        }
190        while ($myrow = $this->db->fetchArray($result)) {
191            $pm =& new XoopsPrivmessage();
192            $pm->assignVars($myrow);
193            if (!$id_as_key) {
194                $ret[] =& $pm;
195            } else {
196                $ret[$myrow['msg_id']] =& $pm;
197            }
198            unset($pm);
199        }
200        return $ret;
201    }
202
203/**
204 * Count message
205 * @param   object  $criteria = null    {@link CriteriaElement} object
206 * @return  int
207 **/
208    function getCount($criteria = null)
209    {
210        $sql = 'SELECT COUNT(*) FROM '.$this->db->prefix('priv_msgs');
211        if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
212            $sql .= ' '.$criteria->renderWhere();
213        }
214        if (!$result = $this->db->query($sql)) {
215            return 0;
216        }
217        list($count) = $this->db->fetchRow($result);
218        return $count;
219    }
220
221/**
222 * Mark a message as read
223 * @param   object  $pm     {@link XoopsPrivmessage} object
224 * @return  bool
225 **/
226    function setRead(&$pm)
227    {
228        if (strtolower(get_class($pm)) != 'xoopsprivmessage') {
229            return false;
230        }
231        $sql = sprintf("UPDATE %s SET read_msg = 1 WHERE msg_id = %u", $this->db->prefix('priv_msgs'), $pm->getVar('msg_id'));
232        if (!$this->db->queryF($sql)) {
233            return false;
234        }
235        return true;
236    }
237}
238?>
Note: See TracBrowser for help on using the repository browser.