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:
19: defined('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: **/
29: class 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: */
114: class 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|false 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: $result = $this->db->query($sql);
147: if (!$this->db->isResultSet($result)) {
148: return $tplset;
149: }
150: $numrows = $this->db->getRowsNum($result);
151: if ($numrows == 1) {
152: $tplset = new XoopsTplset();
153: $tplset->assignVars($this->db->fetchArray($result));
154: }
155: }
156:
157: return $tplset;
158: }
159:
160: /**
161: * retrieve a specific {@link XoopsBlock}
162: *
163: * @see XoopsTplset
164: *
165: * @param $tplset_name
166: *
167: * @internal param int $id tplset_id of the block to retrieve
168: * @return object|false XoopsTplset reference to the tplsets
169: */
170: public function getByName($tplset_name)
171: {
172: $tplset = false;
173: $tplset_name = trim($tplset_name);
174: if ($tplset_name != '') {
175: $sql = 'SELECT * FROM ' . $this->db->prefix('tplset') . ' WHERE tplset_name=' . $this->db->quoteString($tplset_name);
176: $result = $this->db->query($sql);
177: if (!$this->db->isResultSet($result)) {
178: return $tplset;
179: }
180: $numrows = $this->db->getRowsNum($result);
181: if ($numrows == 1) {
182: $tplset = new XoopsTplset();
183: $tplset->assignVars($this->db->fetchArray($result));
184: }
185: }
186:
187: return $tplset;
188: }
189:
190: /**
191: * write a new block into the database
192: *
193: * @param XoopsObject|XoopsTplset $tplset a XoopsTplset object
194: *
195: * @return bool true on success, otherwise false
196: */
197: public function insert(XoopsObject $tplset)
198: {
199: $className = 'XoopsTplset';
200: if (!($tplset instanceof $className)) {
201: return false;
202: }
203: if (!$tplset->isDirty()) {
204: return true;
205: }
206: if (!$tplset->cleanVars()) {
207: return false;
208: }
209: foreach ($tplset->cleanVars as $k => $v) {
210: ${$k} = $v;
211: }
212: if ($tplset->isNew()) {
213: $tplset_id = $this->db->genId('tplset_tplset_id_seq');
214: $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);
215: } else {
216: $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);
217: }
218: if (!$result = $this->db->query($sql)) {
219: return false;
220: }
221: if (empty($tplset_id)) {
222: $tplset_id = $this->db->getInsertId();
223: }
224: $tplset->assignVar('tplset_id', $tplset_id);
225:
226: return true;
227: }
228:
229: /**
230: * delete a tplset from the database
231: *
232: * @param XoopsObject|XoopsTplset $tplset a XoopsTplset object
233: *
234: * @return bool true on success, otherwise false
235: **/
236: public function delete(XoopsObject $tplset)
237: {
238: $className = 'XoopsTplset';
239: if (!($tplset instanceof $className)) {
240: return false;
241: }
242: $sql = sprintf('DELETE FROM %s WHERE tplset_id = %u', $this->db->prefix('tplset'), $tplset->getVar('tplset_id'));
243: if (!$result = $this->db->query($sql)) {
244: return false;
245: }
246: $sql = sprintf('DELETE FROM %s WHERE tplset_name = %s', $this->db->prefix('imgset_tplset_link'), $this->db->quoteString($tplset->getVar('tplset_name')));
247: $this->db->query($sql);
248:
249: return true;
250: }
251:
252: /**
253: * Get tplsets from the database
254: *
255: * @param CriteriaElement|CriteriaCompo $criteria {@link CriteriaElement}
256: * @param bool $id_as_key return the tplsets id as key?
257: * @return array Array of {@link XoopsTplset} objects
258: */
259: public function getObjects(CriteriaElement $criteria = null, $id_as_key = false)
260: {
261: $ret = array();
262: $limit = $start = 0;
263: $sql = 'SELECT * FROM ' . $this->db->prefix('tplset');
264: if (isset($criteria) && \method_exists($criteria, 'renderWhere')) {
265: $sql .= ' ' . $criteria->renderWhere() . ' ORDER BY tplset_id';
266: $limit = $criteria->getLimit();
267: $start = $criteria->getStart();
268: }
269: $result = $this->db->query($sql, $limit, $start);
270: if (!$this->db->isResultSet($result)) {
271: return $ret;
272: }
273: /** @var array $myrow */
274: while (false !== ($myrow = $this->db->fetchArray($result))) {
275: $tplset = new XoopsTplset();
276: $tplset->assignVars($myrow);
277: if (!$id_as_key) {
278: $ret[] =& $tplset;
279: } else {
280: $ret[$myrow['tplset_id']] =& $tplset;
281: }
282: unset($tplset);
283: }
284:
285: return $ret;
286: }
287:
288: /**
289: * Count tplsets
290: *
291: * @param CriteriaElement|CriteriaCompo $criteria {@link CriteriaElement}
292: * @return int Count of tplsets matching $criteria
293: */
294: public function getCount(CriteriaElement $criteria = null)
295: {
296: $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('tplset');
297: if (isset($criteria) && \method_exists($criteria, 'renderWhere')) {
298: $sql .= ' ' . $criteria->renderWhere();
299: }
300: $result = $this->db->query($sql);
301: if (!$this->db->isResultSet($result)) {
302: return 0;
303: }
304: list($count) = $this->db->fetchRow($result);
305:
306: return (int)$count;
307: }
308:
309: /**
310: * get a list of tplsets matchich certain conditions
311: *
312: * @param CriteriaElement $criteria conditions to match
313: * @return array array of tplsets matching the conditions
314: **/
315: public function getList(CriteriaElement $criteria = null)
316: {
317: $ret = array();
318: $tplsets = $this->getObjects($criteria, true);
319: foreach (array_keys($tplsets) as $i) {
320: $temp = $tplsets[$i]->getVar('tplset_name');
321: $ret[$temp] = $temp;
322: }
323:
324: return $ret;
325: }
326: }
327: