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