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