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