1: <?php
2: /**
3: * XOOPS Kernel Class
4: *
5: * You may not change or alter any portion of this comment or credits
6: * of supporting developers from this source code or any supporting source code
7: * which is considered copyrighted (c) material of the original comment or credit authors.
8: * This program is distributed in the hope that it will be useful,
9: * but WITHOUT ANY WARRANTY; without even the implied warranty of
10: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11: *
12: * @copyright (c) 2000-2016 XOOPS Project (www.xoops.org)
13: * @license GNU GPL 2 (https://www.gnu.org/licenses/gpl-2.0.html)
14: * @package kernel
15: * @since 2.0.0
16: * @author Kazumi Ono (AKA onokazu) http://www.myweb.ne.jp/, http://jp.xoops.org/
17: */
18: defined('XOOPS_ROOT_PATH') || exit('Restricted access');
19:
20: /**
21: * An Image
22: *
23: * @package kernel
24: * @author Kazumi Ono <onokazu@xoops.org>
25: * @copyright (c) 2000-2016 XOOPS Project - www.xoops.org
26: */
27: class XoopsImage extends XoopsObject
28: {
29: //PHP 8.2 Dynamic properties deprecated
30: public $image_id;
31: public $image_nam;
32: public $image_nicename;
33: public $image_mimetype;
34: public $image_created;
35: public $image_display;
36: public $image_weight;
37: public $image_body;
38: public $imgcat_id;
39:
40: /**
41: * Constructor
42: **/
43: public function __construct()
44: {
45: parent::__construct();
46: $this->initVar('image_id', XOBJ_DTYPE_INT, null, false);
47: $this->initVar('image_name', XOBJ_DTYPE_OTHER, null, false, 30);
48: $this->initVar('image_nicename', XOBJ_DTYPE_TXTBOX, null, true, 100);
49: $this->initVar('image_mimetype', XOBJ_DTYPE_OTHER, null, false);
50: $this->initVar('image_created', XOBJ_DTYPE_INT, null, false);
51: $this->initVar('image_display', XOBJ_DTYPE_INT, 1, false);
52: $this->initVar('image_weight', XOBJ_DTYPE_INT, 0, false);
53: $this->initVar('image_body', XOBJ_DTYPE_SOURCE, null, true);
54: $this->initVar('imgcat_id', XOBJ_DTYPE_INT, 0, false);
55: }
56:
57: /**
58: * Returns Class Base Variable image_id
59: * @param string $format
60: * @return mixed
61: */
62: public function id($format = 'N')
63: {
64: return $this->getVar('image_id', $format);
65: }
66:
67: /**
68: * Returns Class Base Variable image_id
69: * @param string $format
70: * @return mixed
71: */
72: public function image_id($format = '')
73: {
74: return $this->getVar('image_id', $format);
75: }
76:
77: /**
78: * Returns Class Base Variable image_name
79: * @param string $format
80: * @return mixed
81: */
82: public function image_name($format = '')
83: {
84: return $this->getVar('image_name', $format);
85: }
86:
87: /**
88: * Returns Class Base Variable image_nicename
89: * @param string $format
90: * @return mixed
91: */
92: public function image_nicename($format = '')
93: {
94: return $this->getVar('image_nicename', $format);
95: }
96:
97: /**
98: * Returns Class Base Variable image_mimetype
99: * @param string $format
100: * @return mixed
101: */
102: public function image_mimetype($format = '')
103: {
104: return $this->getVar('image_mimetype', $format);
105: }
106:
107: /**
108: * Returns Class Base Variable image_created
109: * @param string $format
110: * @return mixed
111: */
112: public function image_created($format = '')
113: {
114: return $this->getVar('image_created', $format);
115: }
116:
117: /**
118: * Returns Class Base Variable image_display
119: * @param string $format
120: * @return mixed
121: */
122: public function image_display($format = '')
123: {
124: return $this->getVar('image_display', $format);
125: }
126:
127: /**
128: * Returns Class Base Variable image_weight
129: * @param string $format
130: * @return mixed
131: */
132: public function image_weight($format = '')
133: {
134: return $this->getVar('image_weight', $format);
135: }
136:
137: /**
138: * Returns Class Base Variable image_body
139: * @param string $format
140: * @return mixed
141: */
142: public function image_body($format = '')
143: {
144: return $this->getVar('image_body', $format);
145: }
146:
147: /**
148: * Returns Class Base Variable imgcat_id
149: * @param string $format
150: * @return mixed
151: */
152: public function imgcat_id($format = '')
153: {
154: return $this->getVar('imgcat_id', $format);
155: }
156: }
157:
158: /**
159: * XOOPS image handler class.
160: *
161: * This class is responsible for providing data access mechanisms to the data source
162: * of XOOPS image class objects.
163: *
164: * @package kernel
165: *
166: * @author Kazumi Ono <onokazu@xoops.org>
167: * @copyright (c) 2000-2016 XOOPS Project - www.xoops.org
168: */
169: class XoopsImageHandler extends XoopsObjectHandler
170: {
171: /**
172: * Create a new {@link XoopsImage}
173: *
174: * @param boolean $isNew Flag the object as "new"
175: * @return XoopsImage
176: **/
177: public function create($isNew = true)
178: {
179: $image = new XoopsImage();
180: if ($isNew) {
181: $image->setNew();
182: }
183:
184: return $image;
185: }
186:
187: /**
188: * Load a {@link XoopsImage} object from the database
189: *
190: * @param int $id ID
191: * @param boolean $getbinary
192: * @return XoopsImage|false {@link XoopsImage}, false on fail
193: **/
194: public function get($id, $getbinary = true)
195: {
196: $image = false;
197: $id = (int)$id;
198: if ($id > 0) {
199: $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;
200: $result = $this->db->query($sql);
201: if (!$this->db->isResultSet($result)) {
202: return $image;
203: }
204: $numrows = $this->db->getRowsNum($result);
205: if ($numrows == 1) {
206: $image = new XoopsImage();
207: $image->assignVars($this->db->fetchArray($result));
208: }
209: }
210:
211: return $image;
212: }
213:
214: /**
215: * Write a {@link XoopsImage} object to the database
216: *
217: * @param XoopsObject|XoopsImage $image a XoopsImage object
218: *
219: * @return bool true on success, otherwise false
220: **/
221: public function insert(XoopsObject $image)
222: {
223: $className = 'XoopsImage';
224: if (!($image instanceof $className)) {
225: return false;
226: }
227:
228: if (!$image->isDirty()) {
229: return true;
230: }
231: if (!$image->cleanVars()) {
232: return false;
233: }
234: foreach ($image->cleanVars as $k => $v) {
235: ${$k} = $v;
236: }
237: if ($image->isNew()) {
238: $image_id = $this->db->genId('image_image_id_seq');
239: $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);
240: if (!$result = $this->db->query($sql)) {
241: return false;
242: }
243: if (empty($image_id)) {
244: $image_id = $this->db->getInsertId();
245: }
246: if (isset($image_body) && $image_body != '') {
247: $sql = sprintf('INSERT INTO %s (image_id, image_body) VALUES (%u, %s)', $this->db->prefix('imagebody'), $image_id, $this->db->quoteString($image_body));
248: if (!$result = $this->db->query($sql)) {
249: $sql = sprintf('DELETE FROM %s WHERE image_id = %u', $this->db->prefix('image'), $image_id);
250: $this->db->query($sql);
251:
252: return false;
253: }
254: }
255: $image->assignVar('image_id', $image_id);
256: } else {
257: $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);
258: if (!$result = $this->db->query($sql)) {
259: return false;
260: }
261: if (isset($image_body) && $image_body != '') {
262: $sql = sprintf('UPDATE %s SET image_body = %s WHERE image_id = %u', $this->db->prefix('imagebody'), $this->db->quoteString($image_body), $image_id);
263: if (!$result = $this->db->query($sql)) {
264: $this->db->query(sprintf('DELETE FROM %s WHERE image_id = %u', $this->db->prefix('image'), $image_id));
265:
266: return false;
267: }
268: }
269: }
270:
271: return true;
272: }
273:
274: /**
275: * Delete an image from the database
276: *
277: * @param XoopsObject|XoopsImage $image a XoopsImage object
278: *
279: * @return bool true on success, otherwise false
280: **/
281: public function delete(XoopsObject $image)
282: {
283: $className = 'XoopsImage';
284: if (!($image instanceof $className)) {
285: return false;
286: }
287:
288: $id = $image->getVar('image_id');
289: $sql = sprintf('DELETE FROM %s WHERE image_id = %u', $this->db->prefix('image'), $id);
290: if (!$result = $this->db->query($sql)) {
291: return false;
292: }
293: $sql = sprintf('DELETE FROM %s WHERE image_id = %u', $this->db->prefix('imagebody'), $id);
294: $this->db->query($sql);
295:
296: return true;
297: }
298:
299: /**
300: * Load {@link XoopsImage}s from the database
301: *
302: * @param CriteriaElement|CriteriaCompo $criteria {@link CriteriaElement}
303: * @param boolean $id_as_key Use the ID as key into the array
304: * @param boolean $getbinary
305: * @return array Array of {@link XoopsImage} objects
306: **/
307: public function getObjects(CriteriaElement $criteria = null, $id_as_key = false, $getbinary = false)
308: {
309: $ret = array();
310: $limit = $start = 0;
311: if ($getbinary) {
312: $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';
313: } else {
314: $sql = 'SELECT * FROM ' . $this->db->prefix('image');
315: }
316: if (isset($criteria) && \method_exists($criteria, 'renderWhere')) {
317: $sql .= ' ' . $criteria->renderWhere();
318: $sort = $criteria->getSort() == '' ? 'image_weight' : $criteria->getSort();
319: $sql .= ' ORDER BY ' . $sort . ' ' . $criteria->getOrder();
320: $limit = $criteria->getLimit();
321: $start = $criteria->getStart();
322: }
323: $result = $this->db->query($sql, $limit, $start);
324: if (!$this->db->isResultSet($result)) {
325: return $ret;
326: }
327: /** @var array $myrow */
328: while (false !== ($myrow = $this->db->fetchArray($result))) {
329: $image = new XoopsImage();
330: $image->assignVars($myrow);
331: if (!$id_as_key) {
332: $ret[] =& $image;
333: } else {
334: $ret[$myrow['image_id']] =& $image;
335: }
336: unset($image);
337: }
338:
339: return $ret;
340: }
341:
342: /**
343: * Count some images
344: *
345: * @param CriteriaElement|CriteriaCompo $criteria {@link CriteriaElement}
346: * @return int
347: **/
348: public function getCount(CriteriaElement $criteria = null)
349: {
350: $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('image');
351: if (isset($criteria) && \method_exists($criteria, 'renderWhere')) {
352: $sql .= ' ' . $criteria->renderWhere();
353: }
354: $result = $this->db->query($sql);
355: if (!$this->db->isResultSet($result)) {
356: return 0;
357: }
358: list($count) = $this->db->fetchRow($result);
359:
360: return (int)$count;
361: }
362:
363: /**
364: * Get a list of images
365: *
366: * @param int $imgcat_id
367: * @param bool $image_display
368: * @return array Array of {@link XoopsImage} objects
369: **/
370: public function getList($imgcat_id, $image_display = null)
371: {
372: $criteria = new CriteriaCompo(new Criteria('imgcat_id', (int)$imgcat_id));
373: if (isset($image_display)) {
374: $criteria->add(new Criteria('image_display', (int)$image_display));
375: }
376: $images = $this->getObjects($criteria, false, true);
377: $ret = array();
378: foreach (array_keys($images) as $i) {
379: $ret[$images[$i]->getVar('image_name')] = $images[$i]->getVar('image_nicename');
380: }
381:
382: return $ret;
383: }
384: }
385: