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: * XOOPS Image Sets 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 XoopsImagesetimg extends XoopsObject
28: {
29: //PHP 8.2 Dynamic properties deprecated
30: public $imgsetimg_id;
31: public $imgsetimg_file;
32: public $imgsetimg_body;
33: public $imgsetimg_imgset;
34:
35: /**
36: * Constructor
37: */
38: public function __construct()
39: {
40: parent::__construct();
41: $this->initVar('imgsetimg_id', XOBJ_DTYPE_INT, null, false);
42: $this->initVar('imgsetimg_file', XOBJ_DTYPE_OTHER, null, false);
43: $this->initVar('imgsetimg_body', XOBJ_DTYPE_SOURCE, null, false);
44: $this->initVar('imgsetimg_imgset', XOBJ_DTYPE_INT, null, false);
45: }
46:
47: /**
48: * Returns Class Base Variable imgsetimg_id with default format N
49: * @param string $format
50: * @return mixed
51: */
52: public function id($format = 'N')
53: {
54: return $this->getVar('imgsetimg_id', $format);
55: }
56:
57: /**
58: * Returns Class Base Variable imgsetimg_id
59: * @param string $format
60: * @return mixed
61: */
62: public function imgsetimg_id($format = '')
63: {
64: return $this->getVar('imgsetimg_id', $format);
65: }
66:
67: /**
68: * Returns Class Base Variable imgsetimg_file
69: * @param string $format
70: * @return mixed
71: */
72: public function imgsetimg_file($format = '')
73: {
74: return $this->getVar('imgsetimg_file', $format);
75: }
76:
77: /**
78: * Returns Class Base Variable imgsetimg_body
79: * @param string $format
80: * @return mixed
81: */
82: public function imgsetimg_body($format = '')
83: {
84: return $this->getVar('imgsetimg_body', $format);
85: }
86:
87: /**
88: * Returns Class Base Variable imgsetimg_imgset
89: * @param string $format
90: * @return mixed
91: */
92: public function imgsetimg_imgset($format = '')
93: {
94: return $this->getVar('imgsetimg_imgset', $format);
95: }
96: }
97:
98: /**
99: * XOOPS imageset image handler class.
100: * This class is responsible for providing data access mechanisms to the data source
101: * of XOOPS imageset image class objects.
102: *
103: *
104: * @author Kazumi Ono <onokazu@xoops.org>
105: */
106: class XoopsImagesetimgHandler extends XoopsObjectHandler
107: {
108: /**
109: * Create a new {@link XoopsImageSetImg}
110: *
111: * @param boolean $isNew Flag the object as "new"
112: * @return XoopsImagesetimg
113: **/
114: public function create($isNew = true)
115: {
116: $imgsetimg = new XoopsImagesetimg();
117: if ($isNew) {
118: $imgsetimg->setNew();
119: }
120:
121: return $imgsetimg;
122: }
123:
124: /**
125: * Load a {@link XoopsImageSetImg} object from the database
126: *
127: * @param int $id ID
128: *
129: * @internal param bool $getbinary
130: * @return XoopsImageSetImg|false {@link XoopsImageSetImg}, false on fail
131: */
132: public function get($id)
133: {
134: $imgsetimg = false;
135: $id = (int)$id;
136: if ($id > 0) {
137: $sql = 'SELECT * FROM ' . $this->db->prefix('imgsetimg') . ' WHERE imgsetimg_id=' . $id;
138: $result = $this->db->query($sql);
139: if (!$this->db->isResultSet($result)) {
140: return $imgsetimg;
141: }
142: $numrows = $this->db->getRowsNum($result);
143: if ($numrows == 1) {
144: $imgsetimg = new XoopsImagesetimg();
145: $imgsetimg->assignVars($this->db->fetchArray($result));
146: }
147: }
148:
149: return $imgsetimg;
150: }
151:
152: /**
153: * Write a {@link XoopsImageSetImg} object to the database
154: *
155: * @param XoopsObject|XoopsImageSetImg $imgsetimg a XoopsImageSet object
156: *
157: * @return bool true on success, otherwise false
158: **/
159: public function insert(XoopsObject $imgsetimg)
160: {
161: $className = 'XoopsImageSetImg';
162: if (!($imgsetimg instanceof $className)) {
163: return false;
164: }
165:
166: if (!$imgsetimg->isDirty()) {
167: return true;
168: }
169: if (!$imgsetimg->cleanVars()) {
170: return false;
171: }
172: foreach ($imgsetimg->cleanVars as $k => $v) {
173: ${$k} = $v;
174: }
175: if ($imgsetimg->isNew()) {
176: $imgsetimg_id = $this->db->genId('imgsetimg_imgsetimg_id_seq');
177: $sql = sprintf('INSERT INTO %s (imgsetimg_id, imgsetimg_file, imgsetimg_body, imgsetimg_imgset) VALUES (%u, %s, %s, %s)', $this->db->prefix('imgsetimg'), $imgsetimg_id, $this->db->quoteString($imgsetimg_file), $this->db->quoteString($imgsetimg_body), $this->db->quoteString($imgsetimg_imgset));
178: } else {
179: $sql = sprintf('UPDATE %s SET imgsetimg_file = %s, imgsetimg_body = %s, imgsetimg_imgset = %s WHERE imgsetimg_id = %u', $this->db->prefix('imgsetimg'), $this->db->quoteString($imgsetimg_file), $this->db->quoteString($imgsetimg_body), $this->db->quoteString($imgsetimg_imgset), $imgsetimg_id);
180: }
181: if (!$result = $this->db->query($sql)) {
182: return false;
183: }
184: if (empty($imgsetimg_id)) {
185: $imgsetimg_id = $this->db->getInsertId();
186: }
187: $imgsetimg->assignVar('imgsetimg_id', $imgsetimg_id);
188:
189: return true;
190: }
191:
192: /**
193: * Delete an image from the database
194: *
195: * @param XoopsObject|XoopsImageSetImg $imgsetimg a XoopsImageSet object
196: *
197: * @return bool true on success, otherwise false
198: **/
199: public function delete(XoopsObject $imgsetimg)
200: {
201: $className = 'XoopsImageSetImg';
202: if (!($imgsetimg instanceof $className)) {
203: return false;
204: }
205:
206: $sql = sprintf('DELETE FROM %s WHERE imgsetimg_id = %u', $this->db->prefix('imgsetimg'), $imgsetimg->getVar('imgsetimg_id'));
207: if (!$result = $this->db->query($sql)) {
208: return false;
209: }
210:
211: return true;
212: }
213:
214: /**
215: * Load {@link XoopsImageSetImg}s from the database
216: *
217: * @param CriteriaElement|CriteriaCompo $criteria {@link CriteriaElement}
218: * @param boolean $id_as_key Use the ID as key into the array
219: * @internal param bool $getbinary
220: * @return array Array of {@link XoopsImageSetImg} objects
221: */
222: public function getObjects(CriteriaElement $criteria = null, $id_as_key = false)
223: {
224: $ret = array();
225: $limit = $start = 0;
226: $sql = 'SELECT DISTINCT i.* FROM ' . $this->db->prefix('imgsetimg') . ' i LEFT JOIN ' . $this->db->prefix('imgset_tplset_link') . ' l ON l.imgset_id=i.imgsetimg_imgset LEFT JOIN ' . $this->db->prefix('imgset') . ' s ON s.imgset_id=l.imgset_id';
227: if (isset($criteria) && \method_exists($criteria, 'renderWhere')) {
228: $sql .= ' ' . $criteria->renderWhere();
229: $sql .= ' ORDER BY imgsetimg_id ' . $criteria->getOrder();
230: $limit = $criteria->getLimit();
231: $start = $criteria->getStart();
232: }
233: $result = $this->db->query($sql, $limit, $start);
234: if (!$this->db->isResultSet($result)) {
235: return $ret;
236: }
237:
238: /** @var array $myrow */
239: while (false !== ($myrow = $this->db->fetchArray($result))) {
240: $imgsetimg = new XoopsImagesetimg();
241: $imgsetimg->assignVars($myrow);
242: if (!$id_as_key) {
243: $ret[] =& $imgsetimg;
244: } else {
245: $ret[$myrow['imgsetimg_id']] =& $imgsetimg;
246: }
247: unset($imgsetimg);
248: }
249:
250: return $ret;
251: }
252:
253: /**
254: * Count some imagessetsimg
255: *
256: * @param CriteriaElement|CriteriaCompo $criteria {@link CriteriaElement}
257: * @return int
258: **/
259: public function getCount(CriteriaElement $criteria = null)
260: {
261: $sql = 'SELECT COUNT(i.imgsetimg_id) FROM ' . $this->db->prefix('imgsetimg') . ' i LEFT JOIN ' . $this->db->prefix('imgset_tplset_link') . ' l ON l.imgset_id=i.imgsetimg_imgset';
262: if (isset($criteria) && \method_exists($criteria, 'renderWhere')) {
263: $sql .= ' ' . $criteria->renderWhere() . ' GROUP BY i.imgsetimg_id';
264: }
265: $result = $this->db->query($sql);
266: if (!$this->db->isResultSet($result)) {
267: return 0;
268: }
269: list($count) = $this->db->fetchRow($result);
270:
271: return (int)$count;
272: }
273:
274: /**
275: * Function-Documentation
276: * @param int $imgset_id id of image set
277: * @param bool $id_as_key Use the ID as key into the array
278: * @return array Array of {@link XoopsImageSetImg} objects
279: * @author Kazumi Ono <onokazu@xoops.org>
280: */
281: public function getByImageset($imgset_id, $id_as_key = false)
282: {
283: return $this->getObjects(new Criteria('imgsetimg_imgset', (int)$imgset_id), $id_as_key);
284: }
285:
286: /**
287: * Function-Documentation
288: * @param string $filename
289: * @param int $imgset_id
290: * @return bool true if image exists
291: * @author Kazumi Ono <onokazu@xoops.org>
292: **/
293: public function imageExists($filename, $imgset_id)
294: {
295: $criteria = new CriteriaCompo(new Criteria('imgsetimg_file', $filename));
296: $criteria->add(new Criteria('imgsetimg_imgset', (int)$imgset_id));
297: return $this->getCount($criteria) > 0;
298: }
299: }
300: