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 * A category of configs
22 *
23 * @author              Kazumi Ono    <onokazu@xoops.org>
24 * @copyright       (c) 2000-2016 XOOPS Project (www.xoops.org)
25 *
26 * @package             kernel
27 */
28class XoopsConfigCategory extends XoopsObject
29{
30    /**
31     * Constructor
32     *
33     */
34    public function __construct()
35    {
36        parent::__construct();
37        $this->initVar('confcat_id', XOBJ_DTYPE_INT, null);
38        $this->initVar('confcat_name', XOBJ_DTYPE_OTHER, null);
39        $this->initVar('confcat_order', XOBJ_DTYPE_INT, 0);
40    }
41
42    /**
43     * Returns Class Base Variable confcat_id
44     * @param  string $format
45     * @return mixed
46     */
47    public function id($format = 'N')
48    {
49        return $this->getVar('confcat_id', $format);
50    }
51
52    /**
53     * Returns Class Base Variable confcat_id
54     * @param  string $format
55     * @return mixed
56     */
57    public function confcat_id($format = '')
58    {
59        return $this->getVar('confcat_id', $format);
60    }
61
62    /**
63     * Returns Class Base Variable confcat_name
64     * @param  string $format
65     * @return mixed
66     */
67    public function confcat_name($format = '')
68    {
69        return $this->getVar('confcat_name', $format);
70    }
71
72    /**
73     * Returns Class Base Variable confcat_order
74     * @param  string $format
75     * @return mixed
76     */
77    public function confcat_order($format = '')
78    {
79        return $this->getVar('confcat_order', $format);
80    }
81}
82
83/**
84 * XOOPS configuration category handler class.
85 *
86 * This class is responsible for providing data access mechanisms to the data source
87 * of XOOPS configuration category class objects.
88 *
89 * @author              Kazumi Ono <onokazu@xoops.org>
90 * @copyright       (c) 2000-2016 XOOPS Project (www.xoops.org)
91 *
92 * @package             kernel
93 * @subpackage          config
94 */
95class XoopsConfigCategoryHandler extends XoopsObjectHandler
96{
97    /**
98     * Create a new category
99     *
100     * @param bool $isNew Flag the new object as "new"?
101     *
102     * @return XoopsConfigCategory New {@link XoopsConfigCategory}
103     */
104    public function create($isNew = true)
105    {
106        $confcat = new XoopsConfigCategory();
107        if ($isNew) {
108            $confcat->setNew();
109        }
110
111        return $confcat;
112    }
113
114    /**
115     * Retrieve a {@link XoopsConfigCategory}
116     *
117     * @param int $id ID
118     *
119     * @return XoopsConfigCategory {@link XoopsConfigCategory}, FALSE on fail
120     */
121    public function get($id)
122    {
123        $confcat = false;
124        $id      = (int)$id;
125        if ($id > 0) {
126            $sql = 'SELECT * FROM ' . $this->db->prefix('configcategory') . ' WHERE confcat_id=' . $id;
127            if (!$result = $this->db->query($sql)) {
128                return $confcat;
129            }
130            $numrows = $this->db->getRowsNum($result);
131            if ($numrows == 1) {
132                $confcat = new XoopsConfigCategory();
133                $confcat->assignVars($this->db->fetchArray($result));
134            }
135        }
136
137        return $confcat;
138    }
139
140    /**
141     * Store a {@link XoopsConfigCategory}
142     *
143     * @param XoopsObject|XoopsConfigCategory $confcat a XoopsConfigCategory object
144     *
145     * @return bool true on success, otherwise false
146     */
147    public function insert(XoopsObject $confcat)
148    {
149        $className = 'XoopsConfigCategory';
150        if (!($confcat instanceof $className)) {
151            return false;
152        }
153        if (!$confcat->isDirty()) {
154            return true;
155        }
156        if (!$confcat->cleanVars()) {
157            return false;
158        }
159        foreach ($confcat->cleanVars as $k => $v) {
160            ${$k} = $v;
161        }
162        if ($confcat->isNew()) {
163            $confcat_id = $this->db->genId('configcategory_confcat_id_seq');
164            $sql        = sprintf('INSERT INTO %s (confcat_id, confcat_name, confcat_order) VALUES (%u, %s, %u)', $this->db->prefix('configcategory'), $confcat_id, $this->db->quoteString($confcat_name), $confcat_order);
165        } else {
166            $sql = sprintf('UPDATE %s SET confcat_name = %s, confcat_order = %u WHERE confcat_id = %u', $this->db->prefix('configcategory'), $this->db->quoteString($confcat_name), $confcat_order, $confcat_id);
167        }
168        if (!$result = $this->db->query($sql)) {
169            return false;
170        }
171        if (empty($confcat_id)) {
172            $confcat_id = $this->db->getInsertId();
173        }
174        $confcat->assignVar('confcat_id', $confcat_id);
175
176        return $confcat_id;
177    }
178
179    /**
180     * Delete a {@link XoopsConfigCategory}
181     *
182     * @param XoopsObject|XoopsConfigCategory $confcat a XoopsConfigCategory object
183     *
184     * @return bool true on success, otherwise false
185     */
186    public function delete(XoopsObject $confcat)
187    {
188        $className = 'XoopsConfigCategory';
189        if (!($confcat instanceof $className)) {
190            return false;
191        }
192
193        $sql = sprintf('DELETE FROM %s WHERE confcat_id = %u', $this->db->prefix('configcategory'), $configcategory->getVar('confcat_id'));
194        if (!$result = $this->db->query($sql)) {
195            return false;
196        }
197
198        return true;
199    }
200
201    /**
202     * Get some {@link XoopsConfigCategory}s
203     *
204     * @param CriteriaElement|CriteriaCompo $criteria  {@link CriteriaElement}
205     * @param bool            $id_as_key Use the IDs as keys to the array?
206     *
207     * @return array Array of {@link XoopsConfigCategory}s
208     */
209    public function getObjects(CriteriaElement $criteria = null, $id_as_key = false)
210    {
211        $ret   = array();
212        $limit = $start = 0;
213        $sql   = 'SELECT * FROM ' . $this->db->prefix('configcategory');
214        if (isset($criteria) && is_subclass_of($criteria, 'CriteriaElement')) {
215            $sql .= ' ' . $criteria->renderWhere();
216            $sort = !in_array($criteria->getSort(), array(
217                'confcat_id',
218                'confcat_name',
219                'confcat_order')) ? 'confcat_order' : $criteria->getSort();
220            $sql .= ' ORDER BY ' . $sort . ' ' . $criteria->getOrder();
221            $limit = $criteria->getLimit();
222            $start = $criteria->getStart();
223        }
224        $result = $this->db->query($sql, $limit, $start);
225        if (!$result) {
226            return $ret;
227        }
228        while (false !== ($myrow = $this->db->fetchArray($result))) {
229            $confcat = new XoopsConfigCategory();
230            $confcat->assignVars($myrow);
231            if (!$id_as_key) {
232                $ret[] =& $confcat;
233            } else {
234                $ret[$myrow['confcat_id']] = &$confcat;
235            }
236            unset($confcat);
237        }
238
239        return $ret;
240    }
241
242    /**#@+
243     * @deprecated
244     * @param int $modid
245     * @return bool
246     */
247    public function getCatByModule($modid = 0)
248    {
249        trigger_error(__CLASS__ . '::' . __FUNCTION__ . ' is deprecated', E_USER_WARNING);
250
251        return false;
252    }
253    /**#@-*/
254}
255