1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305:
<?php
// ------------------------------------------------------------------------ //
// XOOPS - PHP Content Management System //
// Copyright (c) 2000 XOOPS.org //
// <http://www.xoops.org/> //
// ------------------------------------------------------------------------ //
// This program is free software; you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation; either version 2 of the License, or //
// (at your option) any later version. //
// //
// You may not change or alter any portion of this comment or credits //
// of supporting developers from this source code or any supporting //
// source code which is considered copyrighted (c) material of the //
// original comment or credit authors. //
// //
// This program is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with this program; if not, write to the Free Software //
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA //
// ------------------------------------------------------------------------ //
// Author: Kazumi Ono (AKA onokazu) //
// URL: http://www.myweb.ne.jp/, http://www.xoops.org/, http://jp.xoops.org/ //
// Project: The XOOPS Project //
// ------------------------------------------------------------------------- //
/**
* Manage images
*
* @copyright http://www.impresscms.org/ The ImpressCMS Project
* @license LICENSE.txt
* @category ICMS
* @package Image
* @version SVN: $Id:Handler.php 19775 2010-07-11 18:54:25Z malanciault $
*/
defined('ICMS_ROOT_PATH') or die("ImpressCMS root path not defined");
/**
* Image handler class.
*
* This class is responsible for providing data access mechanisms to the data source
* of image class objects.
*
* @category ICMS
* @package Image
* @author Kazumi Ono <onokazu@xoops.org>
* @copyright Copyright (c) 2000 XOOPS.org
*/
class icms_image_Handler extends icms_core_ObjectHandler {
/**
* Create a new {@link icms_image_Object}
*
* @param boolean $isNew Flag the object as "new"
* @return object
**/
public function &create($isNew = true) {
$image = new icms_image_Object();
if ($isNew) {
$image->setNew();
}
return $image;
}
/**
* Load a {@link icms_image_Object} object from the database
*
* @param int $id ID
* @param boolean $getbinary
* @return object {@link icms_image_Object}, FALSE on fail
**/
public function &get($id, $getbinary=true) {
$image = false;
$id = (int) $id;
if ($id > 0) {
$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 . "'";
if (!$result = $this->db->query($sql)) {
return $image;
}
$numrows = $this->db->getRowsNum($result);
if ($numrows == 1) {
$image = new icms_image_Object();
$image->assignVars($this->db->fetchArray($result));
}
}
return $image;
}
/**
* Write a {@link icms_image_Object} object to the database
*
* @param object &$image {@link icms_image_Object}
* @return bool
**/
public function insert(&$image) {
/* As of PHP 5.3, is_a is no longer deprecated, this is an acceptable usage
* and is compatible with more versions of PHP. http://us2.php.net/manual/en/language.operators.type.php
*/
if (!is_a($image, 'icms_image_Object')) {
return false;
}
if (!$image->isDirty()) {
return true;
}
if (!$image->cleanVars()) {
return false;
}
foreach ( $image->cleanVars as $k => $v) {
${$k} = $v;
}
if ($image->isNew()) {
$image_id = $this->db->genId('image_image_id_seq');
$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'),
(int) $image_id,
$this->db->quoteString($image_name),
$this->db->quoteString($image_nicename),
$this->db->quoteString($image_mimetype),
time(),
(int) $image_display,
(int) $image_weight,
(int) $imgcat_id
);
if (!$result = $this->db->queryF($sql)) {
return false;
}
if (empty($image_id)) {
$image_id = $this->db->getInsertId();
}
if (isset($image_body) && $image_body != '') {
$sql = sprintf(
"INSERT INTO %s (image_id, image_body) VALUES ('%u', %s)",
$this->db->prefix('imagebody'),
(int) ($image_id),
$this->db->quoteString($image_body)
);
if (!$result = $this->db->queryF($sql)) {
$sql = sprintf("DELETE FROM %s WHERE image_id = '%u'", $this->db->prefix('image'), (int) ($image_id));
$this->db->query($sql);
return false;
}
}
$image->assignVar('image_id', $image_id);
} else {
$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),
(int) $image_display,
(int) $image_weight,
(int) $imgcat_id,
(int) $image_id
);
if (!$result = $this->db->queryF($sql)) {
return false;
}
if (isset($image_body) && $image_body != '') {
$sql = sprintf(
"UPDATE %s SET image_body = %s WHERE image_id = '%u'",
$this->db->prefix('imagebody'),
$this->db->quoteString($image_body),
(int) $image_id
);
if (!$result = $this->db->queryF($sql)) {
$this->db->query(sprintf("DELETE FROM %s WHERE image_id = '%u'", $this->db->prefix('image'), (int) $image_id));
return false;
}
}
}
return true;
}
/**
* Delete an image from the database
*
* @param object &$image {@link icms_image_Object}
* @return bool
**/
public function delete(&$image) {
/* As of PHP 5.3, is_a is no longer deprecated, this is an acceptable usage
* and is compatible with more versions of PHP. http://us2.php.net/manual/en/language.operators.type.php
*/
if (!is_a($image, 'icms_image_Object')) {
return false;
}
$id = (int) ($image->getVar('image_id'));
$sql = sprintf("DELETE FROM %s WHERE image_id = '%u'", $this->db->prefix('image'), $id);
if (!$result = $this->db->query($sql)) {
return false;
}
$sql = sprintf("DELETE FROM %s WHERE image_id = '%u'", $this->db->prefix('imagebody'), $id);
$this->db->query($sql);
return true;
}
/**
* Load {@link icms_image_Object}s from the database
*
* @param null|icms_db_criteria_Item $criteria {@link icms_db_criteria_Element}
* @param bool $id_as_key Use the ID as key into the array
* @param bool $getbinary Get binary image?
* @param bool|string $sql Extra sql (unused; only used for compatibility function signature)
* @param bool $debug Debug mode?
*
* @return icms_image_Object[]
*/
public function getObjects($criteria = null, $id_as_key = false, $getbinary = false, $sql = false, $debug = false) {
$ret = array();
$limit = $start = 0;
if ($getbinary) {
$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";
} else {
$sql = "SELECT * FROM ".$this->db->prefix('image');
}
if (isset($criteria) && is_subclass_of($criteria, 'icms_db_criteria_Element')) {
$sql .= " ".$criteria->renderWhere();
$sort = !in_array($criteria->getSort(), array('image_id', 'image_created', 'image_mimetype', 'image_display', 'image_weight'))
? 'image_weight'
: $criteria->getSort();
$sql .= " ORDER BY " . $sort . " " . $criteria->getOrder();
$limit = $criteria->getLimit();
$start = $criteria->getStart();
}
if ($debug) {
icms_core_Debug::message($sql);
}
$result = $this->db->query($sql, $limit, $start);
if (!$result) {
return $ret;
}
while ($myrow = $this->db->fetchArray($result)) {
$image = new icms_image_Object();
$image->assignVars($myrow);
if (!$id_as_key) {
$ret[] = &$image;
} else {
$ret[$myrow['image_id']] = &$image;
}
unset($image);
}
return $ret;
}
/**
* Count some images
*
* @param object $criteria {@link icms_db_criteria_Element}
* @return int
**/
public function getCount($criteria = null) {
$sql = 'SELECT COUNT(*) FROM '.$this->db->prefix('image');
if (isset($criteria) && is_subclass_of($criteria, 'icms_db_criteria_Element')) {
$sql .= ' '.$criteria->renderWhere();
}
if (!$result = &$this->db->query($sql)) {
return 0;
}
list($count) = $this->db->fetchRow($result);
return $count;
}
/**
* Get a list of images
*
* @param int|null $imgcat_id Image category ID
* @param bool|null|int $image_display List only displayed images?
* @param int $notinuse Not use param (only added for fixing Declaration of icms_image_Handler::getList($imgcat_id, $image_display = NULL, $notinuse1 = 0, $debug = false) should be compatible with icms_ipf_Handler::getList($criteria = NULL, $limit = 0, $start = 0, $debug = false) error)
* @param bool $debug Enable debug mode?
*
* @return array Array of <a href='psi_element://icms_image_Object'>icms_image_Object</a> objects
* objects
*
* @todo Do better fix here for declaration compatibility
*/
public function getList($imgcat_id = null, $image_display = 0, $notinuse = 0, $debug = false) {
$criteria = new icms_db_criteria_Compo();
if ($imgcat_id !== null) {
$criteria->add(
new icms_db_criteria_Item('imgcat_id', (int) ($imgcat_id))
);
}
if ($image_display) {
$criteria->add(new icms_db_criteria_Item('image_display', (int) ($image_display)));
}
$images = & $this->getObjects($criteria, false, true, false, true);
$ret = array();
foreach (array_keys($images) as $i) {
$ret[$images[$i]->getVar('image_name')] = $images[$i]->getVar('image_nicename');
}
return $ret;
}
}