| 1 | <?php |
|---|
| 2 | // $Id: image.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 | /** |
|---|
| 37 | * An Image |
|---|
| 38 | * |
|---|
| 39 | * @package kernel |
|---|
| 40 | * @author Kazumi Ono <[email protected]> |
|---|
| 41 | * @copyright (c) 2000-2003 The Xoops Project - www.xoops.org |
|---|
| 42 | */ |
|---|
| 43 | class XoopsImage extends XoopsObject |
|---|
| 44 | { |
|---|
| 45 | /** |
|---|
| 46 | * Constructor |
|---|
| 47 | **/ |
|---|
| 48 | function XoopsImage() |
|---|
| 49 | { |
|---|
| 50 | $this->XoopsObject(); |
|---|
| 51 | $this->initVar('image_id', XOBJ_DTYPE_INT, null, false); |
|---|
| 52 | $this->initVar('image_name', XOBJ_DTYPE_OTHER, null, false, 30); |
|---|
| 53 | $this->initVar('image_nicename', XOBJ_DTYPE_TXTBOX, null, true, 100); |
|---|
| 54 | $this->initVar('image_mimetype', XOBJ_DTYPE_OTHER, null, false); |
|---|
| 55 | $this->initVar('image_created', XOBJ_DTYPE_INT, null, false); |
|---|
| 56 | $this->initVar('image_display', XOBJ_DTYPE_INT, 1, false); |
|---|
| 57 | $this->initVar('image_weight', XOBJ_DTYPE_INT, 0, false); |
|---|
| 58 | $this->initVar('image_body', XOBJ_DTYPE_SOURCE, null, true); |
|---|
| 59 | $this->initVar('imgcat_id', XOBJ_DTYPE_INT, 0, false); |
|---|
| 60 | } |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | /** |
|---|
| 64 | * XOOPS image handler class. |
|---|
| 65 | * |
|---|
| 66 | * This class is responsible for providing data access mechanisms to the data source |
|---|
| 67 | * of XOOPS image class objects. |
|---|
| 68 | * |
|---|
| 69 | * @package kernel |
|---|
| 70 | * |
|---|
| 71 | * @author Kazumi Ono <[email protected]> |
|---|
| 72 | * @copyright (c) 2000-2003 The Xoops Project - www.xoops.org |
|---|
| 73 | */ |
|---|
| 74 | class XoopsImageHandler extends XoopsObjectHandler |
|---|
| 75 | { |
|---|
| 76 | |
|---|
| 77 | /** |
|---|
| 78 | * Create a new {@link XoopsImage} |
|---|
| 79 | * |
|---|
| 80 | * @param boolean $isNew Flag the object as "new" |
|---|
| 81 | * @return object |
|---|
| 82 | **/ |
|---|
| 83 | function &create($isNew = true) |
|---|
| 84 | { |
|---|
| 85 | $image =& new XoopsImage(); |
|---|
| 86 | if ($isNew) { |
|---|
| 87 | $image->setNew(); |
|---|
| 88 | } |
|---|
| 89 | return $image; |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | /** |
|---|
| 93 | * Load a {@link XoopsImage} object from the database |
|---|
| 94 | * |
|---|
| 95 | * @param int $id ID |
|---|
| 96 | * @param boolean $getbinary |
|---|
| 97 | * @return object {@link XoopsImage}, FALSE on fail |
|---|
| 98 | **/ |
|---|
| 99 | function &get($id, $getbinary=true) |
|---|
| 100 | { |
|---|
| 101 | $ret = false; |
|---|
| 102 | $id = intval($id); |
|---|
| 103 | if ($id > 0) { |
|---|
| 104 | $sql = 'SELECT i.*, b.image_body FROM '.$this->db->prefix('image').' i LEFT JOIN '.$this->db->prefix('imagebody').' b ON b.image_id=i.image_id WHERE i.image_id='.$id; |
|---|
| 105 | if ($result = $this->db->query($sql)) { |
|---|
| 106 | $numrows = $this->db->getRowsNum($result); |
|---|
| 107 | if ($numrows == 1) { |
|---|
| 108 | $image =& new XoopsImage(); |
|---|
| 109 | $image->assignVars($this->db->fetchArray($result)); |
|---|
| 110 | $ret =& $image; |
|---|
| 111 | } |
|---|
| 112 | } |
|---|
| 113 | } |
|---|
| 114 | return $ret; |
|---|
| 115 | } |
|---|
| 116 | |
|---|
| 117 | /** |
|---|
| 118 | * Write a {@link XoopsImage} object to the database |
|---|
| 119 | * |
|---|
| 120 | * @param object &$image {@link XoopsImage} |
|---|
| 121 | * @return bool |
|---|
| 122 | **/ |
|---|
| 123 | function insert(&$image) |
|---|
| 124 | { |
|---|
| 125 | if (strtolower(get_class($image)) != 'xoopsimage') { |
|---|
| 126 | return false; |
|---|
| 127 | } |
|---|
| 128 | if (!$image->isDirty()) { |
|---|
| 129 | return true; |
|---|
| 130 | } |
|---|
| 131 | if (!$image->cleanVars()) { |
|---|
| 132 | return false; |
|---|
| 133 | } |
|---|
| 134 | foreach ($image->cleanVars as $k => $v) { |
|---|
| 135 | ${$k} = $v; |
|---|
| 136 | } |
|---|
| 137 | if ($image->isNew()) { |
|---|
| 138 | $image_id = $this->db->genId('image_image_id_seq'); |
|---|
| 139 | $sql = sprintf("INSERT INTO %s (image_id, image_name, image_nicename, image_mimetype, image_created, image_display, image_weight, imgcat_id) VALUES (%u, %s, %s, %s, %u, %u, %u, %u)", $this->db->prefix('image'), $image_id, $this->db->quoteString($image_name), $this->db->quoteString($image_nicename), $this->db->quoteString($image_mimetype), time(), $image_display, $image_weight, $imgcat_id); |
|---|
| 140 | if (!$result = $this->db->query($sql)) { |
|---|
| 141 | return false; |
|---|
| 142 | } |
|---|
| 143 | if (empty($image_id)) { |
|---|
| 144 | $image_id = $this->db->getInsertId(); |
|---|
| 145 | } |
|---|
| 146 | if (isset($image_body) && $image_body != '') { |
|---|
| 147 | $sql = sprintf("INSERT INTO %s (image_id, image_body) VALUES (%u, %s)", $this->db->prefix('imagebody'), $image_id, $this->db->quoteString($image_body)); |
|---|
| 148 | if (!$result = $this->db->query($sql)) { |
|---|
| 149 | $sql = sprintf("DELETE FROM %s WHERE image_id = %u", $this->db->prefix('image'), $image_id); |
|---|
| 150 | $this->db->query($sql); |
|---|
| 151 | return false; |
|---|
| 152 | } |
|---|
| 153 | } |
|---|
| 154 | $image->assignVar('image_id', $image_id); |
|---|
| 155 | } else { |
|---|
| 156 | $sql = sprintf("UPDATE %s SET image_name = %s, image_nicename = %s, image_display = %u, image_weight = %u, imgcat_id = %u WHERE image_id = %u", $this->db->prefix('image'), $this->db->quoteString($image_name), $this->db->quoteString($image_nicename), $image_display, $image_weight, $imgcat_id, $image_id); |
|---|
| 157 | if (!$result = $this->db->query($sql)) { |
|---|
| 158 | return false; |
|---|
| 159 | } |
|---|
| 160 | if (isset($image_body) && $image_body != '') { |
|---|
| 161 | $sql = sprintf("UPDATE %s SET image_body = %s WHERE image_id = %u", $this->db->prefix('imagebody'), $this->db->quoteString($image_body), $image_id); |
|---|
| 162 | if (!$result = $this->db->query($sql)) { |
|---|
| 163 | $this->db->query(sprintf("DELETE FROM %s WHERE image_id = %u", $this->db->prefix('image'), $image_id)); |
|---|
| 164 | return false; |
|---|
| 165 | } |
|---|
| 166 | } |
|---|
| 167 | } |
|---|
| 168 | return true; |
|---|
| 169 | } |
|---|
| 170 | |
|---|
| 171 | /** |
|---|
| 172 | * Delete an image from the database |
|---|
| 173 | * |
|---|
| 174 | * @param object &$image {@link XoopsImage} |
|---|
| 175 | * @return bool |
|---|
| 176 | **/ |
|---|
| 177 | function delete(&$image) |
|---|
| 178 | { |
|---|
| 179 | if (strtolower(get_class($image)) != 'xoopsimage') { |
|---|
| 180 | return false; |
|---|
| 181 | } |
|---|
| 182 | $id = $image->getVar('image_id'); |
|---|
| 183 | $sql = sprintf("DELETE FROM %s WHERE image_id = %u", $this->db->prefix('image'), $id); |
|---|
| 184 | if (!$result = $this->db->query($sql)) { |
|---|
| 185 | return false; |
|---|
| 186 | } |
|---|
| 187 | $sql = sprintf("DELETE FROM %s WHERE image_id = %u", $this->db->prefix('imagebody'), $id); |
|---|
| 188 | $this->db->query($sql); |
|---|
| 189 | return true; |
|---|
| 190 | } |
|---|
| 191 | |
|---|
| 192 | /** |
|---|
| 193 | * Load {@link XoopsImage}s from the database |
|---|
| 194 | * |
|---|
| 195 | * @param object $criteria {@link CriteriaElement} |
|---|
| 196 | * @param boolean $id_as_key Use the ID as key into the array |
|---|
| 197 | * @param boolean $getbinary |
|---|
| 198 | * @return array Array of {@link XoopsImage} objects |
|---|
| 199 | **/ |
|---|
| 200 | function &getObjects($criteria = null, $id_as_key = false, $getbinary = false) |
|---|
| 201 | { |
|---|
| 202 | $ret = array(); |
|---|
| 203 | $limit = $start = 0; |
|---|
| 204 | if ($getbinary) { |
|---|
| 205 | $sql = 'SELECT i.*, b.image_body FROM '.$this->db->prefix('image').' i LEFT JOIN '.$this->db->prefix('imagebody').' b ON b.image_id=i.image_id'; |
|---|
| 206 | } else { |
|---|
| 207 | $sql = 'SELECT * FROM '.$this->db->prefix('image'); |
|---|
| 208 | } |
|---|
| 209 | if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) { |
|---|
| 210 | $sql .= ' '.$criteria->renderWhere(); |
|---|
| 211 | $sort = !in_array($criteria->getSort(), array('image_id', 'image_created', 'image_mimetype', 'image_display', 'image_weight')) ? 'image_weight' : $criteria->getSort(); |
|---|
| 212 | $sql .= ' ORDER BY '.$sort.' '.$criteria->getOrder(); |
|---|
| 213 | $limit = $criteria->getLimit(); |
|---|
| 214 | $start = $criteria->getStart(); |
|---|
| 215 | } |
|---|
| 216 | $result = $this->db->query($sql, $limit, $start); |
|---|
| 217 | if (!$result) { |
|---|
| 218 | return $ret; |
|---|
| 219 | } |
|---|
| 220 | while ($myrow = $this->db->fetchArray($result)) { |
|---|
| 221 | $image =& new XoopsImage(); |
|---|
| 222 | $image->assignVars($myrow); |
|---|
| 223 | if (!$id_as_key) { |
|---|
| 224 | $ret[] =& $image; |
|---|
| 225 | } else { |
|---|
| 226 | $ret[$myrow['image_id']] =& $image; |
|---|
| 227 | } |
|---|
| 228 | unset($image); |
|---|
| 229 | } |
|---|
| 230 | return $ret; |
|---|
| 231 | } |
|---|
| 232 | |
|---|
| 233 | /** |
|---|
| 234 | * Count some images |
|---|
| 235 | * |
|---|
| 236 | * @param object $criteria {@link CriteriaElement} |
|---|
| 237 | * @return int |
|---|
| 238 | **/ |
|---|
| 239 | function getCount($criteria = null) |
|---|
| 240 | { |
|---|
| 241 | $sql = 'SELECT COUNT(*) FROM '.$this->db->prefix('image'); |
|---|
| 242 | if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) { |
|---|
| 243 | $sql .= ' '.$criteria->renderWhere(); |
|---|
| 244 | } |
|---|
| 245 | if (!$result =& $this->db->query($sql)) { |
|---|
| 246 | return 0; |
|---|
| 247 | } |
|---|
| 248 | list($count) = $this->db->fetchRow($result); |
|---|
| 249 | return $count; |
|---|
| 250 | } |
|---|
| 251 | |
|---|
| 252 | /** |
|---|
| 253 | * Get a list of images |
|---|
| 254 | * |
|---|
| 255 | * @param int $imgcat_id |
|---|
| 256 | * @param bool $image_display |
|---|
| 257 | * @return array Array of {@link XoopsImage} objects |
|---|
| 258 | **/ |
|---|
| 259 | function &getList($imgcat_id, $image_display = null) |
|---|
| 260 | { |
|---|
| 261 | $criteria = new CriteriaCompo(new Criteria('imgcat_id', intval($imgcat_id))); |
|---|
| 262 | if (isset($image_display)) { |
|---|
| 263 | $criteria->add(new Criteria('image_display', intval($image_display))); |
|---|
| 264 | } |
|---|
| 265 | $images =& $this->getObjects($criteria, false, true); |
|---|
| 266 | $ret = array(); |
|---|
| 267 | foreach (array_keys($images) as $i) { |
|---|
| 268 | $ret[$images[$i]->getVar('image_name')] = $images[$i]->getVar('image_nicename'); |
|---|
| 269 | } |
|---|
| 270 | return $ret; |
|---|
| 271 | } |
|---|
| 272 | } |
|---|
| 273 | ?> |
|---|