| 1 | <?php |
|---|
| 2 | // $Id: avatar.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 | // ------------------------------------------------------------------------- // |
|---|
| 31 | |
|---|
| 32 | if (!defined('XOOPS_ROOT_PATH')) { |
|---|
| 33 | exit(); |
|---|
| 34 | } |
|---|
| 35 | |
|---|
| 36 | class XoopsAvatar extends XoopsObject |
|---|
| 37 | { |
|---|
| 38 | var $_userCount; |
|---|
| 39 | |
|---|
| 40 | function XoopsAvatar() |
|---|
| 41 | { |
|---|
| 42 | $this->XoopsObject(); |
|---|
| 43 | $this->initVar('avatar_id', XOBJ_DTYPE_INT, null, false); |
|---|
| 44 | $this->initVar('avatar_file', XOBJ_DTYPE_OTHER, null, false, 30); |
|---|
| 45 | $this->initVar('avatar_name', XOBJ_DTYPE_TXTBOX, null, true, 100); |
|---|
| 46 | $this->initVar('avatar_mimetype', XOBJ_DTYPE_OTHER, null, false); |
|---|
| 47 | $this->initVar('avatar_created', XOBJ_DTYPE_INT, null, false); |
|---|
| 48 | $this->initVar('avatar_display', XOBJ_DTYPE_INT, 1, false); |
|---|
| 49 | $this->initVar('avatar_weight', XOBJ_DTYPE_INT, 0, false); |
|---|
| 50 | $this->initVar('avatar_type', XOBJ_DTYPE_OTHER, 0, false); |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | function setUserCount($value) |
|---|
| 54 | { |
|---|
| 55 | $this->_userCount = intval($value); |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | function getUserCount() |
|---|
| 59 | { |
|---|
| 60 | return $this->_userCount; |
|---|
| 61 | } |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | |
|---|
| 65 | /** |
|---|
| 66 | * XOOPS avatar handler class. |
|---|
| 67 | * This class is responsible for providing data access mechanisms to the data source |
|---|
| 68 | * of XOOPS avatar class objects. |
|---|
| 69 | * |
|---|
| 70 | * |
|---|
| 71 | * @author Kazumi Ono <[email protected]> |
|---|
| 72 | */ |
|---|
| 73 | |
|---|
| 74 | class XoopsAvatarHandler extends XoopsObjectHandler |
|---|
| 75 | { |
|---|
| 76 | |
|---|
| 77 | function &create($isNew = true) |
|---|
| 78 | { |
|---|
| 79 | $avatar =& new XoopsAvatar(); |
|---|
| 80 | if ($isNew) { |
|---|
| 81 | $avatar->setNew(); |
|---|
| 82 | } |
|---|
| 83 | return $avatar; |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | function &get($id) |
|---|
| 87 | { |
|---|
| 88 | $ret = false; |
|---|
| 89 | $id = intval($id); |
|---|
| 90 | if ($id > 0) { |
|---|
| 91 | $sql = 'SELECT * FROM '.$this->db->prefix('avatar').' WHERE avatar_id='.$id; |
|---|
| 92 | if ($result = $this->db->query($sql)) { |
|---|
| 93 | $numrows = $this->db->getRowsNum($result); |
|---|
| 94 | if ($numrows == 1) { |
|---|
| 95 | $avatar =& new XoopsAvatar(); |
|---|
| 96 | $avatar->assignVars($this->db->fetchArray($result)); |
|---|
| 97 | $ret =& $avatar; |
|---|
| 98 | } |
|---|
| 99 | } |
|---|
| 100 | } |
|---|
| 101 | return $ret; |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | function insert(&$avatar) |
|---|
| 105 | { |
|---|
| 106 | if (strtolower(get_class($avatar)) != 'xoopsavatar') { |
|---|
| 107 | return false; |
|---|
| 108 | } |
|---|
| 109 | if (!$avatar->isDirty()) { |
|---|
| 110 | return true; |
|---|
| 111 | } |
|---|
| 112 | if (!$avatar->cleanVars()) { |
|---|
| 113 | return false; |
|---|
| 114 | } |
|---|
| 115 | foreach ($avatar->cleanVars as $k => $v) { |
|---|
| 116 | ${$k} = $v; |
|---|
| 117 | } |
|---|
| 118 | if ($avatar->isNew()) { |
|---|
| 119 | $avatar_id = $this->db->genId('avatar_avatar_id_seq'); |
|---|
| 120 | $sql = sprintf("INSERT INTO %s (avatar_id, avatar_file, avatar_name, avatar_created, avatar_mimetype, avatar_display, avatar_weight, avatar_type) VALUES (%u, %s, %s, %u, %s, %u, %u, %s)", $this->db->prefix('avatar'), $avatar_id, $this->db->quoteString($avatar_file), $this->db->quoteString($avatar_name), time(), $this->db->quoteString($avatar_mimetype), $avatar_display, $avatar_weight, $this->db->quoteString($avatar_type)); |
|---|
| 121 | } else { |
|---|
| 122 | $sql = sprintf("UPDATE %s SET avatar_file = %s, avatar_name = %s, avatar_created = %u, avatar_mimetype= %s, avatar_display = %u, avatar_weight = %u, avatar_type = %s WHERE avatar_id = %u", $this->db->prefix('avatar'), $this->db->quoteString($avatar_file), $this->db->quoteString($avatar_name), $avatar_created, $this->db->quoteString($avatar_mimetype), $avatar_display, $avatar_weight, $this->db->quoteString($avatar_type), $avatar_id); |
|---|
| 123 | } |
|---|
| 124 | if (!$result = $this->db->query($sql)) { |
|---|
| 125 | return false; |
|---|
| 126 | } |
|---|
| 127 | if (empty($avatar_id)) { |
|---|
| 128 | $avatar_id = $this->db->getInsertId(); |
|---|
| 129 | } |
|---|
| 130 | $avatar->assignVar('avatar_id', $avatar_id); |
|---|
| 131 | return true; |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | function delete(&$avatar) |
|---|
| 135 | { |
|---|
| 136 | if (strtolower(get_class($avatar)) != 'xoopsavatar') { |
|---|
| 137 | return false; |
|---|
| 138 | } |
|---|
| 139 | $id = $avatar->getVar('avatar_id'); |
|---|
| 140 | $sql = sprintf("DELETE FROM %s WHERE avatar_id = %u", $this->db->prefix('avatar'), $id); |
|---|
| 141 | if (!$result = $this->db->query($sql)) { |
|---|
| 142 | return false; |
|---|
| 143 | } |
|---|
| 144 | $sql = sprintf("DELETE FROM %s WHERE avatar_id = %u", $this->db->prefix('avatar_user_link'), $id); |
|---|
| 145 | $result = $this->db->query($sql); |
|---|
| 146 | return true; |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | function &getObjects($criteria = null, $id_as_key = false) |
|---|
| 150 | { |
|---|
| 151 | $ret = array(); |
|---|
| 152 | $limit = $start = 0; |
|---|
| 153 | $sql = 'SELECT a.*, COUNT(u.user_id) AS count FROM '.$this->db->prefix('avatar').' a LEFT JOIN '.$this->db->prefix('avatar_user_link').' u ON u.avatar_id=a.avatar_id'; |
|---|
| 154 | if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) { |
|---|
| 155 | $sql .= ' '.$criteria->renderWhere(); |
|---|
| 156 | $sql .= ' GROUP BY a.avatar_id ORDER BY avatar_weight, avatar_id'; |
|---|
| 157 | $limit = $criteria->getLimit(); |
|---|
| 158 | $start = $criteria->getStart(); |
|---|
| 159 | } |
|---|
| 160 | $result = $this->db->query($sql, $limit, $start); |
|---|
| 161 | if (!$result) { |
|---|
| 162 | return $ret; |
|---|
| 163 | } |
|---|
| 164 | while ($myrow = $this->db->fetchArray($result)) { |
|---|
| 165 | $avatar =& new XoopsAvatar(); |
|---|
| 166 | $avatar->assignVars($myrow); |
|---|
| 167 | $avatar->setUserCount($myrow['count']); |
|---|
| 168 | if (!$id_as_key) { |
|---|
| 169 | $ret[] =& $avatar; |
|---|
| 170 | } else { |
|---|
| 171 | $ret[$myrow['avatar_id']] =& $avatar; |
|---|
| 172 | } |
|---|
| 173 | unset($avatar); |
|---|
| 174 | } |
|---|
| 175 | return $ret; |
|---|
| 176 | } |
|---|
| 177 | |
|---|
| 178 | function getCount($criteria = null) |
|---|
| 179 | { |
|---|
| 180 | $sql = 'SELECT COUNT(*) FROM '.$this->db->prefix('avatar'); |
|---|
| 181 | if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) { |
|---|
| 182 | $sql .= ' '.$criteria->renderWhere(); |
|---|
| 183 | } |
|---|
| 184 | if (!$result =& $this->db->query($sql)) { |
|---|
| 185 | return 0; |
|---|
| 186 | } |
|---|
| 187 | list($count) = $this->db->fetchRow($result); |
|---|
| 188 | return $count; |
|---|
| 189 | } |
|---|
| 190 | |
|---|
| 191 | function addUser($avatar_id, $user_id){ |
|---|
| 192 | $avatar_id = intval($avatar_id); |
|---|
| 193 | $user_id = intval($user_id); |
|---|
| 194 | if ($avatar_id < 1 || $user_id < 1) { |
|---|
| 195 | return false; |
|---|
| 196 | } |
|---|
| 197 | $sql = sprintf("DELETE FROM %s WHERE user_id = %u", $this->db->prefix('avatar_user_link'), $user_id); |
|---|
| 198 | $this->db->query($sql); |
|---|
| 199 | $sql = sprintf("INSERT INTO %s (avatar_id, user_id) VALUES (%u, %u)", $this->db->prefix('avatar_user_link'), $avatar_id, $user_id); |
|---|
| 200 | if (!$result =& $this->db->query($sql)) { |
|---|
| 201 | return false; |
|---|
| 202 | } |
|---|
| 203 | return true; |
|---|
| 204 | } |
|---|
| 205 | |
|---|
| 206 | function &getUser(&$avatar){ |
|---|
| 207 | $ret = array(); |
|---|
| 208 | if (strtolower(get_class($avatar)) != 'xoopsavatar') { |
|---|
| 209 | return $ret; |
|---|
| 210 | } |
|---|
| 211 | $sql = 'SELECT user_id FROM '.$this->db->prefix('avatar_user_link').' WHERE avatar_id='.$avatar->getVar('avatar_id'); |
|---|
| 212 | if (!$result = $this->db->query($sql)) { |
|---|
| 213 | return $ret; |
|---|
| 214 | } |
|---|
| 215 | while ($myrow = $this->db->fetchArray($result)) { |
|---|
| 216 | $ret[] =& $myrow['user_id']; |
|---|
| 217 | } |
|---|
| 218 | return $ret; |
|---|
| 219 | } |
|---|
| 220 | |
|---|
| 221 | function &getList($avatar_type = null, $avatar_display = null) |
|---|
| 222 | { |
|---|
| 223 | $criteria = new CriteriaCompo(); |
|---|
| 224 | if (isset($avatar_type)) { |
|---|
| 225 | $avatar_type = ($avatar_type == 'C') ? 'C' : 'S'; |
|---|
| 226 | $criteria->add(new Criteria('avatar_type', $avatar_type)); |
|---|
| 227 | } |
|---|
| 228 | if (isset($avatar_display)) { |
|---|
| 229 | $criteria->add(new Criteria('avatar_display', intval($avatar_display))); |
|---|
| 230 | } |
|---|
| 231 | $avatars =& $this->getObjects($criteria, true); |
|---|
| 232 | $ret = array('blank.gif' => _NONE); |
|---|
| 233 | foreach (array_keys($avatars) as $i) { |
|---|
| 234 | $ret[$avatars[$i]->getVar('avatar_file')] = $avatars[$i]->getVar('avatar_name'); |
|---|
| 235 | } |
|---|
| 236 | return $ret; |
|---|
| 237 | } |
|---|
| 238 | } |
|---|
| 239 | ?> |
|---|