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 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 XoopsTplfile extends XoopsObject
30: {
31: //PHP 8.2 Dynamic properties deprecated
32: public $tpl_id;
33: public $tpl_refid;
34: public $tpl_tplset;
35: public $tpl_file;
36: public $tpl_desc;
37: public $tpl_lastmodified;
38: public $tpl_lastimported;
39: public $tpl_module;
40: public $tpl_type;
41: public $tpl_source;
42:
43: /**
44: * Constructor
45: *
46: * @return XoopsTplfile
47: */
48: public function __construct()
49: {
50: parent::__construct();
51: $this->initVar('tpl_id', XOBJ_DTYPE_INT, null, false);
52: $this->initVar('tpl_refid', XOBJ_DTYPE_INT, 0, false);
53: $this->initVar('tpl_tplset', XOBJ_DTYPE_OTHER, null, false);
54: $this->initVar('tpl_file', XOBJ_DTYPE_TXTBOX, null, true, 100);
55: $this->initVar('tpl_desc', XOBJ_DTYPE_TXTBOX, null, false, 100);
56: $this->initVar('tpl_lastmodified', XOBJ_DTYPE_INT, 0, false);
57: $this->initVar('tpl_lastimported', XOBJ_DTYPE_INT, 0, false);
58: $this->initVar('tpl_module', XOBJ_DTYPE_OTHER, null, false);
59: $this->initVar('tpl_type', XOBJ_DTYPE_OTHER, null, false);
60: $this->initVar('tpl_source', XOBJ_DTYPE_SOURCE, null, false);
61: }
62:
63: /**
64: * Returns Class Base Variable tpl_id
65: * @param string $format
66: * @return mixed
67: */
68: public function id($format = 'N')
69: {
70: return $this->getVar('tpl_id', $format);
71: }
72:
73: /**
74: * Returns Class Base Variable tpl_id
75: * @param string $format
76: * @return mixed
77: */
78: public function tpl_id($format = '')
79: {
80: return $this->getVar('tpl_id', $format);
81: }
82:
83: /**
84: * Returns Class Base Variable tpl_refid
85: * @param string $format
86: * @return mixed
87: */
88: public function tpl_refid($format = '')
89: {
90: return $this->getVar('tpl_refid', $format);
91: }
92:
93: /**
94: * Returns Class Base Variable tpl_tplset
95: * @param string $format
96: * @return mixed
97: */
98: public function tpl_tplset($format = '')
99: {
100: return $this->getVar('tpl_tplset', $format);
101: }
102:
103: /**
104: * Returns Class Base Variable tpl_file
105: * @param string $format
106: * @return mixed
107: */
108: public function tpl_file($format = '')
109: {
110: return $this->getVar('tpl_file', $format);
111: }
112:
113: /**
114: * Returns Class Base Variable tpl_desc
115: * @param string $format
116: * @return mixed
117: */
118: public function tpl_desc($format = '')
119: {
120: return $this->getVar('tpl_desc', $format);
121: }
122:
123: /**
124: * Returns Class Base Variable tpl_lastmodified
125: * @param string $format
126: * @return mixed
127: */
128: public function tpl_lastmodified($format = '')
129: {
130: return $this->getVar('tpl_lastmodified', $format);
131: }
132:
133: /**
134: * Returns Class Base Variable tpl_lastimported
135: * @param string $format
136: * @return mixed
137: */
138: public function tpl_lastimported($format = '')
139: {
140: return $this->getVar('tpl_lastimported', $format);
141: }
142:
143: /**
144: * Returns Class Base Variable tpl_module
145: * @param string $format
146: * @return mixed
147: */
148: public function tpl_module($format = '')
149: {
150: return $this->getVar('tpl_module', $format);
151: }
152:
153: /**
154: * Returns Class Base Variable tpl_type
155: * @param string $format
156: * @return mixed
157: */
158: public function tpl_type($format = '')
159: {
160: return $this->getVar('tpl_type', $format);
161: }
162:
163: /**
164: * Returns Class Base Variable tpl_source
165: * @param string $format
166: * @return mixed
167: */
168: public function tpl_source($format = '')
169: {
170: return $this->getVar('tpl_source', $format);
171: }
172:
173: /**
174: * getSource
175: *
176: * @return string
177: */
178: public function getSource()
179: {
180: return $this->getVar('tpl_source');
181: }
182:
183: /**
184: * getLastModified
185: *
186: * @return int unixtimestamp
187: */
188: public function getLastModified()
189: {
190: return $this->getVar('tpl_lastmodified');
191: }
192: }
193:
194: /**
195: * XOOPS template file handler class.
196: * This class is responsible for providing data access mechanisms to the data source
197: * of XOOPS template file class objects.
198: *
199: *
200: * @author Kazumi Ono <onokazu@xoops.org>
201: *
202: * @todo this is not a XoopsPersistableObjectHandler?
203: */
204: class XoopsTplfileHandler extends XoopsObjectHandler
205: {
206: /**
207: * create a new XoopsTplfile
208: *
209: * @see \XoopsBlock
210: * @param bool $isNew is the new Tplfile new??
211: * @return \XoopsTplfile reference to the new XoopsTplfile
212: */
213: public function create($isNew = true)
214: {
215: $tplfile = new XoopsTplfile();
216: if ($isNew) {
217: $tplfile->setNew();
218: }
219:
220: return $tplfile;
221: }
222:
223: /**
224: * retrieve a specific {@link XoopsTplfile}
225: *
226: * @see \XoopsBlock
227: *
228: * @param int $id tpl_id of the block to retrieve
229: * @param bool $getsource
230: *
231: * @return \XoopsTplfile|false reference to the Tplfile
232: */
233: public function get($id, $getsource = false)
234: {
235: $tplfile = false;
236: $id = (int)$id;
237: if ($id > 0) {
238: if (!$getsource) {
239: $sql = 'SELECT * FROM ' . $this->db->prefix('tplfile') . ' WHERE tpl_id=' . $id;
240: } else {
241: $sql = 'SELECT f.*, s.tpl_source FROM ' . $this->db->prefix('tplfile') . ' f LEFT JOIN ' . $this->db->prefix('tplsource') . ' s ON s.tpl_id=f.tpl_id WHERE f.tpl_id=' . $id;
242: }
243: $result = $this->db->query($sql);
244: if (!$this->db->isResultSet($result)) {
245: return $tplfile;
246: }
247: $numrows = $this->db->getRowsNum($result);
248: if ($numrows == 1) {
249: $tplfile = new XoopsTplfile();
250: $tplfile->assignVars($this->db->fetchArray($result));
251: }
252: }
253:
254: return $tplfile;
255: }
256:
257: /**
258: * Load template source
259: *
260: * @param XoopsObject|XoopsTplfile $tplfile a XoopsTplfile object
261: *
262: * @return bool true on success, otherwise false
263: */
264: public function loadSource(XoopsObject $tplfile)
265: {
266: $className = 'XoopsTplfile';
267: if (!($tplfile instanceof $className)) {
268: return false;
269: }
270:
271: if (!$tplfile->getVar('tpl_source')) {
272: $sql = 'SELECT tpl_source FROM ' . $this->db->prefix('tplsource') . ' WHERE tpl_id=' . $tplfile->getVar('tpl_id');
273: $result = $this->db->query($sql);
274: if (!$this->db->isResultSet($result)) {
275: return false;
276: }
277: /** @var array $myrow */
278: $myrow = $this->db->fetchArray($result);
279: $tplfile->assignVar('tpl_source', $myrow['tpl_source']);
280: }
281:
282: return true;
283: }
284:
285: /**
286: * write a new Tplfile into the database
287: *
288: * @param XoopsObject|XoopsTplfile $tplfile a XoopsTplfile object
289: *
290: * @return bool true on success, otherwise false
291: */
292: public function insert(XoopsObject $tplfile)
293: {
294: $className = 'XoopsTplfile';
295: if (!($tplfile instanceof $className)) {
296: return false;
297: }
298: if (!$tplfile->isDirty()) {
299: return true;
300: }
301: if (!$tplfile->cleanVars()) {
302: return false;
303: }
304: foreach ($tplfile->cleanVars as $k => $v) {
305: ${$k} = $v;
306: }
307: if ($tplfile->isNew()) {
308: $tpl_id = $this->db->genId('tpltpl_file_id_seq');
309: $sql = sprintf('INSERT INTO %s (tpl_id, tpl_module, tpl_refid, tpl_tplset, tpl_file, tpl_desc, tpl_lastmodified, tpl_lastimported, tpl_type) VALUES (%u, %s, %u, %s, %s, %s, %u, %u, %s)', $this->db->prefix('tplfile'), $tpl_id, $this->db->quoteString($tpl_module), $tpl_refid, $this->db->quoteString($tpl_tplset), $this->db->quoteString($tpl_file), $this->db->quoteString($tpl_desc), $tpl_lastmodified, $tpl_lastimported, $this->db->quoteString($tpl_type));
310: if (!$result = $this->db->query($sql)) {
311: return false;
312: }
313: if (empty($tpl_id)) {
314: $tpl_id = $this->db->getInsertId();
315: }
316: if (isset($tpl_source) && $tpl_source != '') {
317: $sql = sprintf('INSERT INTO %s (tpl_id, tpl_source) VALUES (%u, %s)', $this->db->prefix('tplsource'), $tpl_id, $this->db->quoteString($tpl_source));
318: if (!$result = $this->db->query($sql)) {
319: $this->db->query(sprintf('DELETE FROM %s WHERE tpl_id = %u', $this->db->prefix('tplfile'), $tpl_id));
320:
321: return false;
322: }
323: }
324: $tplfile->assignVar('tpl_id', $tpl_id);
325: } else {
326: $sql = sprintf('UPDATE %s SET tpl_tplset = %s, tpl_file = %s, tpl_desc = %s, tpl_lastimported = %u, tpl_lastmodified = %u WHERE tpl_id = %u', $this->db->prefix('tplfile'), $this->db->quoteString($tpl_tplset), $this->db->quoteString($tpl_file), $this->db->quoteString($tpl_desc), $tpl_lastimported, $tpl_lastmodified, $tpl_id);
327: if (!$result = $this->db->query($sql)) {
328: return false;
329: }
330: if (isset($tpl_source) && $tpl_source != '') {
331: $sql = sprintf('UPDATE %s SET tpl_source = %s WHERE tpl_id = %u', $this->db->prefix('tplsource'), $this->db->quoteString($tpl_source), $tpl_id);
332: if (!$result = $this->db->query($sql)) {
333: return false;
334: }
335: }
336: }
337:
338: return true;
339: }
340:
341: /**
342: * Update XoopsTplfile, even if a safe transaction (i.e. http get)
343: *
344: * @param XoopsTplfile $tplfile
345: * @return bool true on success, otherwise false
346: */
347: public function forceUpdate(XoopsTplfile $tplfile)
348: {
349: $className = 'XoopsTplfile';
350: if (!($tplfile instanceof $className)) {
351: return false;
352: }
353: if (!$tplfile->isDirty()) {
354: return true;
355: }
356: if (!$tplfile->cleanVars()) {
357: return false;
358: }
359: foreach ($tplfile->cleanVars as $k => $v) {
360: ${$k} = $v;
361: }
362: if (!$tplfile->isNew()) {
363: $sql = sprintf('UPDATE %s SET tpl_tplset = %s, tpl_file = %s, tpl_desc = %s, tpl_lastimported = %u, tpl_lastmodified = %u WHERE tpl_id = %u', $this->db->prefix('tplfile'), $this->db->quoteString($tpl_tplset), $this->db->quoteString($tpl_file), $this->db->quoteString($tpl_desc), $tpl_lastimported, $tpl_lastmodified, $tpl_id);
364: if (!$result = $this->db->queryF($sql)) {
365: return false;
366: }
367: if (isset($tpl_source) && $tpl_source != '') {
368: $sql = sprintf('UPDATE %s SET tpl_source = %s WHERE tpl_id = %u', $this->db->prefix('tplsource'), $this->db->quoteString($tpl_source), $tpl_id);
369: if (!$result = $this->db->queryF($sql)) {
370: return false;
371: }
372: }
373:
374: return true;
375: } else {
376: return false;
377: }
378: }
379:
380: /**
381: * delete a block from the database
382: *
383: * @param XoopsObject|XoopsTplfile $tplfile a XoopsTplfile object
384: *
385: * @return bool true on success, otherwise false
386: */
387: public function delete(XoopsObject $tplfile)
388: {
389: $className = 'XoopsTplfile';
390: if (!($tplfile instanceof $className)) {
391: return false;
392: }
393: $id = $tplfile->getVar('tpl_id');
394: $sql = sprintf('DELETE FROM %s WHERE tpl_id = %u', $this->db->prefix('tplfile'), $id);
395: if (!$result = $this->db->query($sql)) {
396: return false;
397: }
398: $sql = sprintf('DELETE FROM %s WHERE tpl_id = %u', $this->db->prefix('tplsource'), $id);
399: $this->db->query($sql);
400:
401: return true;
402: }
403:
404: /**
405: * retrieve array of {@link XoopsBlock}s meeting certain conditions
406: * @param CriteriaElement|CriteriaCompo $criteria {@link CriteriaElement} with conditions for the blocks
407: * @param bool $getsource
408: * @param bool $id_as_key should the blocks' bid be the key for the returned array?
409: * @return array {@link XoopsBlock}s matching the conditions
410: */
411: public function getObjects(CriteriaElement $criteria = null, $getsource = false, $id_as_key = false)
412: {
413: $ret = array();
414: $limit = $start = 0;
415: if ($getsource) {
416: $sql = 'SELECT f.*, s.tpl_source FROM ' . $this->db->prefix('tplfile') . ' f LEFT JOIN ' . $this->db->prefix('tplsource') . ' s ON s.tpl_id=f.tpl_id';
417: } else {
418: $sql = 'SELECT * FROM ' . $this->db->prefix('tplfile');
419: }
420: if (isset($criteria) && \method_exists($criteria, 'renderWhere')) {
421: $sql .= ' ' . $criteria->renderWhere() . ' ORDER BY tpl_refid';
422: $limit = $criteria->getLimit();
423: $start = $criteria->getStart();
424: }
425: $result = $this->db->query($sql, $limit, $start);
426: if (!$this->db->isResultSet($result)) {
427: return $ret;
428: }
429: /** @var array $myrow */
430: while (false !== ($myrow = $this->db->fetchArray($result))) {
431: $tplfile = new XoopsTplfile();
432: $tplfile->assignVars($myrow);
433: if (!$id_as_key) {
434: $ret[] =& $tplfile;
435: } else {
436: $ret[$myrow['tpl_id']] =& $tplfile;
437: }
438: unset($tplfile);
439: }
440:
441: return $ret;
442: }
443:
444: /**
445: * Get count
446: *
447: * @param CriteriaElement|CriteriaCompo $criteria
448: * @return int
449: */
450: public function getCount(CriteriaElement $criteria = null)
451: {
452: $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('tplfile');
453: if (isset($criteria) && \method_exists($criteria, 'renderWhere')) {
454: $sql .= ' ' . $criteria->renderWhere();
455: }
456: $result = $this->db->query($sql);
457: if (!$this->db->isResultSet($result)) {
458: return 0;
459: }
460: list($count) = $this->db->fetchRow($result);
461:
462: return (int)$count;
463: }
464:
465: /**
466: * getModuleTplCount
467: *
468: * @param string $tplset
469: * @return array
470: */
471: public function getModuleTplCount($tplset)
472: {
473: $ret = array();
474: $sql = 'SELECT tpl_module, COUNT(tpl_id) AS count FROM ' . $this->db->prefix('tplfile') . " WHERE tpl_tplset='" . $tplset . "' GROUP BY tpl_module";
475: $result = $this->db->query($sql);
476: if (!$this->db->isResultSet($result)) {
477: return $ret;
478: }
479: /** @var array $myrow */
480: while (false !== ($myrow = $this->db->fetchArray($result))) {
481: if ($myrow['tpl_module'] != '') {
482: $ret[$myrow['tpl_module']] = $myrow['count'];
483: }
484: }
485:
486: return $ret;
487: }
488:
489: /**
490: * Find Template File
491: *
492: * @param string|null $tplset
493: * @param string|array|null $type
494: * @param string|null $refid
495: * @param string|null $module
496: * @param string|null $file
497: * @param bool|string $getsource
498: * @return array
499: */
500: public function find($tplset = null, $type = null, $refid = null, $module = null, $file = null, $getsource = false)
501: {
502: $criteria = new CriteriaCompo();
503: if (isset($tplset)) {
504: $criteria->add(new Criteria('tpl_tplset', $tplset));
505: }
506: if (isset($module)) {
507: $criteria->add(new Criteria('tpl_module', $module));
508: }
509: if (isset($refid)) {
510: $criteria->add(new Criteria('tpl_refid', $refid));
511: }
512: if (isset($file)) {
513: $criteria->add(new Criteria('tpl_file', $file));
514: }
515: if (isset($type)) {
516: if (is_array($type)) {
517: $criteria2 = new CriteriaCompo();
518: foreach ($type as $t) {
519: $criteria2->add(new Criteria('tpl_type', $t), 'OR');
520: }
521: $criteria->add($criteria2);
522: } else {
523: $criteria->add(new Criteria('tpl_type', $type));
524: }
525: }
526:
527: return $this->getObjects($criteria, $getsource, false);
528: }
529:
530: /**
531: * Template Exists
532: *
533: * @param string $tplname
534: * @param string $tplset_name
535: * @return bool true if template exists, otherwise false
536: */
537: public function templateExists($tplname, $tplset_name)
538: {
539: $criteria = new CriteriaCompo(new Criteria('tpl_file', trim($tplname)));
540: $criteria->add(new Criteria('tpl_tplset', trim($tplset_name)));
541: return $this->getCount($criteria) > 0;
542: }
543: }
544: