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 Category
27: *
28: * @package kernel
29: *
30: * @author Kazumi Ono <onokazu@xoops.org>
31: * @copyright (c) 2000-2016 XOOPS Project (www.xoops.org)
32: */
33: class XoopsImagecategory extends XoopsObject
34: {
35: public $_imageCount;
36: //PHP 8.2 Dynamic properties deprecated
37: public $imgcat_id;
38: public $imgcat_name;
39: public $imgcat_display;
40: public $imgcat_weight;
41: public $imgcat_maxsize;
42: public $imgcat_maxwidth;
43: public $imgcat_maxheight;
44: public $imgcat_type;
45: public $imgcat_storetype;
46:
47: /**
48: * Constructor
49: **/
50: public function __construct()
51: {
52: parent::__construct();
53: $this->initVar('imgcat_id', XOBJ_DTYPE_INT, null, false);
54: $this->initVar('imgcat_name', XOBJ_DTYPE_TXTBOX, null, true, 100);
55: $this->initVar('imgcat_display', XOBJ_DTYPE_INT, 1, false);
56: $this->initVar('imgcat_weight', XOBJ_DTYPE_INT, 0, false);
57: $this->initVar('imgcat_maxsize', XOBJ_DTYPE_INT, 0, false);
58: $this->initVar('imgcat_maxwidth', XOBJ_DTYPE_INT, 0, false);
59: $this->initVar('imgcat_maxheight', XOBJ_DTYPE_INT, 0, false);
60: $this->initVar('imgcat_type', XOBJ_DTYPE_OTHER, null, false);
61: $this->initVar('imgcat_storetype', XOBJ_DTYPE_OTHER, null, false);
62: }
63:
64: /**
65: * Returns Class Base Variable imgcat_id
66: * @param string $format
67: * @return mixed
68: */
69: public function id($format = 'N')
70: {
71: return $this->getVar('imgcat_id', $format);
72: }
73:
74: /**
75: * Returns Class Base Variable imgcat_id
76: * @param string $format
77: * @return mixed
78: */
79: public function imgcat_id($format = '')
80: {
81: return $this->getVar('imgcat_id', $format);
82: }
83:
84: /**
85: * Returns Class Base Variable imgcat_name
86: * @param string $format
87: * @return mixed
88: */
89: public function imgcat_name($format = '')
90: {
91: return $this->getVar('imgcat_name', $format);
92: }
93:
94: /**
95: * Returns Class Base Variable imgcat_display
96: * @param string $format
97: * @return mixed
98: */
99: public function imgcat_display($format = '')
100: {
101: return $this->getVar('imgcat_display', $format);
102: }
103:
104: /**
105: * Returns Class Base Variable imgcat_weight
106: * @param string $format
107: * @return mixed
108: */
109: public function imgcat_weight($format = '')
110: {
111: return $this->getVar('imgcat_weight', $format);
112: }
113:
114: /**
115: * Returns Class Base Variable imgcat_maxsize
116: * @param string $format
117: * @return mixed
118: */
119: public function imgcat_maxsize($format = '')
120: {
121: return $this->getVar('imgcat_maxsize', $format);
122: }
123:
124: /**
125: * Returns Class Base Variable imgcat_maxwidth
126: * @param string $format
127: * @return mixed
128: */
129: public function imgcat_maxwidth($format = '')
130: {
131: return $this->getVar('imgcat_maxwidth', $format);
132: }
133:
134: /**
135: * Returns Class Base Variable imgcat_maxheight
136: * @param string $format
137: * @return mixed
138: */
139: public function imgcat_maxheight($format = '')
140: {
141: return $this->getVar('imgcat_maxheight', $format);
142: }
143:
144: /**
145: * Returns Class Base Variable imgcat_type
146: * @param string $format
147: * @return mixed
148: */
149: public function imgcat_type($format = '')
150: {
151: return $this->getVar('imgcat_type', $format);
152: }
153:
154: /**
155: * Returns Class Base Variable imgcat_storetype
156: * @param string $format
157: * @return mixed
158: */
159: public function imgcat_storetype($format = '')
160: {
161: return $this->getVar('imgcat_storetype', $format);
162: }
163:
164: /**
165: * Enter description here...
166: *
167: * @param int $value
168: */
169: public function setImageCount($value)
170: {
171: $this->_imageCount = (int)$value;
172: }
173:
174: /**
175: * Enter description here...
176: *
177: * @return int
178: */
179: public function getImageCount()
180: {
181: return $this->_imageCount;
182: }
183: }
184:
185: /**
186: * XOOPS image caetgory handler class.
187: * This class is responsible for providing data access mechanisms to the data source
188: * of XOOPS image category class objects.
189: *
190: *
191: * @author Kazumi Ono <onokazu@xoops.org>
192: */
193: class XoopsImagecategoryHandler extends XoopsObjectHandler
194: {
195: /**
196: * Create a new {@link XoopsImageCategory}
197: *
198: * @param boolean $isNew Flag the object as "new"
199: * @return XoopsImagecategory
200: **/
201: public function create($isNew = true)
202: {
203: $imgcat = new XoopsImagecategory();
204: if ($isNew) {
205: $imgcat->setNew();
206: }
207:
208: return $imgcat;
209: }
210:
211: /**
212: * Load a {@link XoopsImageCategory} object from the database
213: *
214: * @param int $id ID
215: *
216: * @internal param bool $getbinary
217: * @return XoopsImageCategory|false {@link XoopsImageCategory}, false on fail
218: */
219: public function get($id)
220: {
221: $id = (int)$id;
222: $imgcat = false;
223: if ($id > 0) {
224: $sql = 'SELECT * FROM ' . $this->db->prefix('imagecategory') . ' WHERE imgcat_id=' . $id;
225: $result = $this->db->query($sql);
226: if (!$this->db->isResultSet($result)) {
227: return $imgcat;
228: }
229: $numrows = $this->db->getRowsNum($result);
230: if ($numrows == 1) {
231: $imgcat = new XoopsImagecategory();
232: $imgcat->assignVars($this->db->fetchArray($result));
233: }
234: }
235:
236: return $imgcat;
237: }
238:
239: /**
240: * Write a {@link XoopsImageCategory} object to the database
241: *
242: * @param XoopsObject|XoopsImageCategory $imgcat a XoopsImageCategory object
243: *
244: * @return bool true on success, otherwise false
245: **/
246: public function insert(XoopsObject $imgcat)
247: {
248: $className = 'XoopsImageCategory';
249: if (!($imgcat instanceof $className)) {
250: return false;
251: }
252:
253: if (!$imgcat->isDirty()) {
254: return true;
255: }
256: if (!$imgcat->cleanVars()) {
257: return false;
258: }
259: foreach ($imgcat->cleanVars as $k => $v) {
260: ${$k} = $v;
261: }
262: if ($imgcat->isNew()) {
263: $imgcat_id = $this->db->genId('imgcat_imgcat_id_seq');
264: $sql = sprintf('INSERT INTO %s (imgcat_id, imgcat_name, imgcat_display, imgcat_weight, imgcat_maxsize, imgcat_maxwidth, imgcat_maxheight, imgcat_type, imgcat_storetype) VALUES (%u, %s, %u, %u, %u, %u, %u, %s, %s)', $this->db->prefix('imagecategory'), $imgcat_id, $this->db->quoteString($imgcat_name), $imgcat_display, $imgcat_weight, $imgcat_maxsize, $imgcat_maxwidth, $imgcat_maxheight, $this->db->quoteString($imgcat_type), $this->db->quoteString($imgcat_storetype));
265: } else {
266: $sql = sprintf('UPDATE %s SET imgcat_name = %s, imgcat_display = %u, imgcat_weight = %u, imgcat_maxsize = %u, imgcat_maxwidth = %u, imgcat_maxheight = %u, imgcat_type = %s WHERE imgcat_id = %u', $this->db->prefix('imagecategory'), $this->db->quoteString($imgcat_name), $imgcat_display, $imgcat_weight, $imgcat_maxsize, $imgcat_maxwidth, $imgcat_maxheight, $this->db->quoteString($imgcat_type), $imgcat_id);
267: }
268: if (!$result = $this->db->query($sql)) {
269: return false;
270: }
271: if (empty($imgcat_id)) {
272: $imgcat_id = $this->db->getInsertId();
273: }
274: $imgcat->assignVar('imgcat_id', $imgcat_id);
275:
276: return true;
277: }
278:
279: /**
280: * Delete an image from the database
281: *
282: * @param XoopsObject|XoopsImageCategory $imgcat a XoopsImageCategory object
283: *
284: * @return bool true on success, otherwise false
285: **/
286: public function delete(XoopsObject $imgcat)
287: {
288: $className = 'XoopsImageCategory';
289: if (!($imgcat instanceof $className)) {
290: return false;
291: }
292:
293: $sql = sprintf('DELETE FROM %s WHERE imgcat_id = %u', $this->db->prefix('imagecategory'), $imgcat->getVar('imgcat_id'));
294: if (!$result = $this->db->query($sql)) {
295: return false;
296: }
297:
298: return true;
299: }
300:
301: /**
302: * Enter description here...
303: *
304: * @param CriteriaElement $criteria
305: * @param bool $id_as_key if true, use id as array key
306: * @return array of XoopsImagecategory objects
307: */
308: public function getObjects(CriteriaElement $criteria = null, $id_as_key = false)
309: {
310: $ret = array();
311: $limit = $start = 0;
312: $sql = 'SELECT DISTINCT c.* FROM ' . $this->db->prefix('imagecategory') . ' c LEFT JOIN ' . $this->db->prefix('group_permission') . " l ON l.gperm_itemid=c.imgcat_id WHERE (l.gperm_name = 'imgcat_read' OR l.gperm_name = 'imgcat_write')";
313: if (isset($criteria) && is_subclass_of($criteria, 'CriteriaElement')) {
314: $where = $criteria->render();
315: $sql .= ($where != '') ? ' AND ' . $where : '';
316: $limit = $criteria->getLimit();
317: $start = $criteria->getStart();
318: }
319: $sql .= ' ORDER BY imgcat_weight, imgcat_id ASC';
320: $result = $this->db->query($sql, $limit, $start);
321: if (!$this->db->isResultSet($result)) {
322: return $ret;
323: }
324: /** @var array $myrow */
325: while (false !== ($myrow = $this->db->fetchArray($result))) {
326: $imgcat = new XoopsImagecategory();
327: $imgcat->assignVars($myrow);
328: if (!$id_as_key) {
329: $ret[] = $imgcat;
330: } else {
331: $ret[$myrow['imgcat_id']] = $imgcat;
332: }
333: unset($imgcat);
334: }
335:
336: return $ret;
337: }
338:
339: /**
340: * Count some images
341: *
342: * @param CriteriaElement $criteria {@link CriteriaElement}
343: * @return int
344: **/
345: public function getCount(CriteriaElement $criteria = null)
346: {
347: $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('imagecategory') . ' i LEFT JOIN ' . $this->db->prefix('group_permission') . " l ON l.gperm_itemid=i.imgcat_id WHERE (l.gperm_name = 'imgcat_read' OR l.gperm_name = 'imgcat_write')";
348: if (isset($criteria) && is_subclass_of($criteria, 'CriteriaElement')) {
349: $where = $criteria->render();
350: $sql .= ($where != '') ? ' AND ' . $where : '';
351: }
352: $result = $this->db->query($sql);
353: if (!$this->db->isResultSet($result)) {
354: return 0;
355: }
356: list($count) = $this->db->fetchRow($result);
357:
358: return (int)$count;
359: }
360:
361: /**
362: * Get a list of image categories
363: *
364: * @param array $groups
365: * @param string $perm
366: * @param null $display
367: * @param null $storetype
368: * @internal param int $imgcat_id
369: * @internal param bool $image_display
370: * @return array Array of {@link XoopsImage} objects
371: */
372: public function getList($groups = array(), $perm = 'imgcat_read', $display = null, $storetype = null)
373: {
374: $criteria = new CriteriaCompo();
375: if (!empty($groups) && \is_array($groups)) {
376: $criteriaTray = new CriteriaCompo();
377: foreach ($groups as $gid) {
378: $criteriaTray->add(new Criteria('gperm_groupid', $gid), 'OR');
379: }
380: $criteria->add($criteriaTray);
381: if ($perm === 'imgcat_read' || $perm === 'imgcat_write') {
382: $criteria->add(new Criteria('gperm_name', $perm));
383: $criteria->add(new Criteria('gperm_modid', 1));
384: }
385: }
386: if (isset($display)) {
387: $criteria->add(new Criteria('imgcat_display', (int)$display));
388: }
389: if (isset($storetype)) {
390: $criteria->add(new Criteria('imgcat_storetype', $storetype));
391: }
392: $categories = $this->getObjects($criteria, true);
393: $ret = array();
394: foreach (array_keys($categories) as $i) {
395: $ret[$i] = $categories[$i]->getVar('imgcat_name');
396: }
397:
398: return $ret;
399: }
400: }
401: