1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18:
19:
20: namespace Xoops\Core\Kernel\Handlers;
21:
22: use Xoops\Core\Database\Connection;
23: use Xoops\Core\Kernel\Criteria;
24: use Xoops\Core\Kernel\CriteriaCompo;
25: use Xoops\Core\Kernel\CriteriaElement;
26: use Xoops\Core\Kernel\XoopsPersistableObjectHandler;
27:
28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39:
40: class XoopsTplFileHandler extends XoopsPersistableObjectHandler
41: {
42:
43: 44: 45: 46: 47:
48: public function __construct(Connection $db = null)
49: {
50: parent::__construct($db, 'system_tplfile', '\Xoops\Core\Kernel\Handlers\XoopsTplFile', 'tpl_id', 'tpl_refid');
51: }
52:
53: 54: 55: 56: 57: 58: 59: 60:
61: public function getById($id, $getsource = false)
62: {
63: $qb = $this->db2->createXoopsQueryBuilder();
64: $eb = $qb->expr();
65: $tplfile = false;
66: $id = (int)($id);
67: if ($id > 0) {
68: if (!$getsource) {
69: $qb->select('*')
70: ->fromPrefix('system_tplfile', 'f')
71: ->where($eb->eq('f.tpl_id', ':tplid'))
72: ->setParameter(':tplid', $id, \PDO::PARAM_INT);
73: } else {
74: $qb->select('f.*')
75: ->addSelect('s.tpl_source')
76: ->fromPrefix('system_tplfile', 'f')
77: ->leftJoinPrefix('f', 'system_tplsource', 's', $eb->eq('s.tpl_id', 'f.tpl_id'))
78: ->where($eb->eq('f.tpl_id', ':tplid'))
79: ->setParameter(':tplid', $id, \PDO::PARAM_INT);
80: }
81: $result = $qb->execute();
82: if (!$result) {
83: return $tplfile;
84: }
85: $allrows = $result->fetchAll();
86: if (count($allrows) == 1) {
87: $tplfile = new XoopsTplFile();
88: $tplfile->assignVars(reset($allrows));
89: }
90: }
91: return $tplfile;
92: }
93:
94: 95: 96: 97: 98: 99: 100:
101: public function loadSource(XoopsTplFile $tplfile)
102: {
103: if (!$tplfile->getVar('tpl_source')) {
104: $qb = $this->db2->createXoopsQueryBuilder();
105: $eb = $qb->expr();
106: $qb->select('tpl_source')
107: ->fromPrefix('system_tplsource', null)
108: ->where($eb->eq('tpl_id', ':tplid'))
109: ->setParameter(':tplid', $tplfile->getVar('tpl_id'), \PDO::PARAM_INT);
110: if (!$result = $qb->execute()) {
111: return false;
112: }
113: $myrow = $result->fetch(\PDO::FETCH_ASSOC);
114: $tplfile->assignVar('tpl_source', $myrow['tpl_source']);
115: }
116: return true;
117: }
118:
119: 120: 121: 122: 123: 124: 125:
126: public function insertTpl(XoopsTplFile $tplfile)
127: {
128: $tpl_id = 0;
129: if (!$tplfile->isDirty()) {
130: return true;
131: }
132: if (!$tplfile->cleanVars()) {
133: return false;
134: }
135:
136: $vars = $tplfile->cleanVars;
137: $tpl_module = $vars['tpl_module'];
138: $tpl_refid = $vars['tpl_refid'];
139: $tpl_tplset = $vars['tpl_tplset'];
140: $tpl_file = $vars['tpl_file'];
141: $tpl_desc = $vars['tpl_desc'];
142: $tpl_lastmodified = $vars['tpl_lastmodified'];
143: $tpl_lastimported = $vars['tpl_lastimported'];
144: $tpl_type = $vars['tpl_type'];
145: if (isset($vars['tpl_id'])) {
146: $tpl_id = $vars['tpl_id'];
147: }
148: if (isset($vars['tpl_source'])) {
149: $tpl_source = $vars['tpl_source'];
150: }
151:
152: if ($tplfile->isNew()) {
153: $tpl_id = 0;
154: $values = array(
155:
156: 'tpl_module' => $tpl_module,
157: 'tpl_refid' => $tpl_refid,
158: 'tpl_tplset' => $tpl_tplset,
159: 'tpl_file' => $tpl_file,
160: 'tpl_desc' => $tpl_desc,
161: 'tpl_lastmodified' => $tpl_lastmodified,
162: 'tpl_lastimported' => $tpl_lastimported,
163: 'tpl_type' => $tpl_type,
164: );
165: if (!$this->db2->insertPrefix('system_tplfile', $values)) {
166: return false;
167: }
168: if (empty($tpl_id)) {
169: $tpl_id = $this->db2->lastInsertId();
170: }
171: if (isset($tpl_source) && $tpl_source != '') {
172: $values = array(
173: 'tpl_id' => $tpl_id,
174: 'tpl_source' => $tpl_source,
175: );
176: if (!$this->db2->insertPrefix('system_tplsource', $values)) {
177: $this->db2->deletePrefix('system_tplfile', array('tpl_id' => $tpl_id));
178: return false;
179: }
180: }
181: $tplfile->assignVar('tpl_id', $tpl_id);
182: } else {
183: $values = array(
184:
185: 'tpl_module' => $tpl_module,
186: 'tpl_refid' => $tpl_refid,
187: 'tpl_tplset' => $tpl_tplset,
188: 'tpl_file' => $tpl_file,
189: 'tpl_desc' => $tpl_desc,
190: 'tpl_lastmodified' => $tpl_lastmodified,
191: 'tpl_lastimported' => $tpl_lastimported,
192: 'tpl_type' => $tpl_type,
193: );
194: if (!$this->db2->updatePrefix('system_tplfile', $values, array('tpl_id' => $tpl_id))) {
195: return false;
196: }
197:
198: if (isset($tpl_source) && $tpl_source != '') {
199: $values = array(
200:
201: 'tpl_source' => $tpl_source,
202: );
203: if ($this->db2->updatePrefix('system_tplsource', $values, array('tpl_id' => $tpl_id))) {
204: return false;
205: }
206: }
207: }
208:
209: return true;
210: }
211:
212: 213: 214: 215: 216: 217: 218:
219: public function forceUpdate(XoopsTplFile $tplfile)
220: {
221: if (!$tplfile->isDirty()) {
222: return true;
223: }
224: if (!$tplfile->cleanVars()) {
225: return false;
226: }
227:
228: $vars = $tplfile->cleanVars;
229: $tpl_module = $vars['tpl_module'];
230: $tpl_refid = $vars['tpl_refid'];
231: $tpl_tplset = $vars['tpl_tplset'];
232: $tpl_file = $vars['tpl_file'];
233: $tpl_desc = $vars['tpl_desc'];
234: $tpl_lastmodified = $vars['tpl_lastmodified'];
235: $tpl_lastimported = $vars['tpl_lastimported'];
236: $tpl_type = $vars['tpl_type'];
237:
238: if (isset($vars['tpl_source'])) {
239: $tpl_source = $vars['tpl_source'];
240: }
241:
242: if (!$tplfile->isNew()) {
243: $tpl_id = 0;
244: $values = array(
245:
246: 'tpl_module' => $tpl_module,
247: 'tpl_refid' => $tpl_refid,
248: 'tpl_tplset' => $tpl_tplset,
249: 'tpl_file' => $tpl_file,
250: 'tpl_desc' => $tpl_desc,
251: 'tpl_lastmodified' => $tpl_lastmodified,
252: 'tpl_lastimported' => $tpl_lastimported,
253: 'tpl_type' => $tpl_type,
254: );
255: if (!$this->db2->updatePrefix('system_tplfile', $values, array('tpl_id' => $tpl_id))) {
256: return false;
257: }
258:
259: if (isset($tpl_source) && $tpl_source != '') {
260: $tpl_id = 0;
261: $values = array(
262:
263: 'tpl_source' => $tpl_source,
264: );
265: if ($this->db2->updatePrefix('system_tplsource', $values, array('tpl_id' => $tpl_id))) {
266: return false;
267: }
268: }
269:
270: return true;
271: } else {
272: return false;
273: }
274: }
275:
276: 277: 278: 279: 280: 281: 282:
283: public function deleteTpl(XoopsTplFile $tplfile)
284: {
285: $tpl_id = $tplfile->getVar('tpl_id');
286: if (!$this->db2->deletePrefix('system_tplfile', array('tpl_id' => $tpl_id))) {
287: return false;
288: }
289: $this->db2->deletePrefix('system_tplsource', array('tpl_id' => $tpl_id));
290: return true;
291: }
292:
293: 294: 295: 296: 297: 298: 299: 300: 301:
302: public function getTplObjects(CriteriaElement $criteria = null, $getsource = false, $id_as_key = false)
303: {
304: $qb = $this->db2->createXoopsQueryBuilder();
305: $eb = $qb->expr();
306:
307: $ret = array();
308:
309: if (!$getsource) {
310: $qb->select('*')
311: ->fromPrefix('system_tplfile', 'f');
312: } else {
313: $qb->select('f.*')
314: ->addSelect('s.tpl_source')
315: ->fromPrefix('system_tplfile', 'f')
316: ->leftJoinPrefix('f', 'system_tplsource', 's', $eb->eq('s.tpl_id', 'f.tpl_id'));
317: }
318: if (isset($criteria) && ($criteria instanceof CriteriaElement)) {
319: $criteria->renderQb($qb);
320: }
321: $result = $qb->execute();
322: if (!$result) {
323: return $ret;
324: }
325: while ($myrow = $result->fetch(\PDO::FETCH_ASSOC)) {
326: $tplfile = new XoopsTplFile();
327: $tplfile->assignVars($myrow);
328: if (!$id_as_key) {
329: $ret[] = $tplfile;
330: } else {
331: $ret[$myrow['tpl_id']] = $tplfile;
332: }
333: unset($tplfile);
334: }
335: return $ret;
336: }
337:
338: 339: 340: 341: 342: 343: 344:
345: public function getModuleTplCount($tplset)
346: {
347: $qb = $this->db2->createXoopsQueryBuilder();
348: $eb = $qb->expr();
349:
350: $qb->select('tpl_module')
351: ->addSelect('COUNT(tpl_id) AS count')
352: ->fromPrefix('system_tplfile', null)
353: ->where($eb->eq('tpl_tplset', ':tpset'))
354: ->groupBy('tpl_module')
355: ->setParameter(':tpset', $tplset, \PDO::PARAM_STR);
356:
357: $ret = array();
358: $result = $qb->execute();
359: if (!$result) {
360: return $ret;
361: }
362: while ($myrow = $result->fetch(\PDO::FETCH_ASSOC)) {
363: if ($myrow['tpl_module'] != '') {
364: $ret[$myrow['tpl_module']] = $myrow['count'];
365: }
366: }
367: return $ret;
368: }
369:
370: 371: 372: 373: 374: 375: 376: 377: 378: 379: 380: 381:
382: public function find($tplset = null, $type = null, $refid = null, $module = null, $file = null, $getsource = false)
383: {
384: $criteria = new CriteriaCompo();
385: if (isset($tplset)) {
386: $criteria->add(new Criteria('tpl_tplset', $tplset));
387: }
388: if (isset($module)) {
389: $criteria->add(new Criteria('tpl_module', $module));
390: }
391: if (isset($refid)) {
392: $criteria->add(new Criteria('tpl_refid', $refid));
393: }
394: if (isset($file)) {
395: $criteria->add(new Criteria('tpl_file', $file));
396: }
397: if (isset($type)) {
398: if (is_array($type)) {
399: $criteria2 = new CriteriaCompo();
400: foreach ($type as $t) {
401: $criteria2->add(new Criteria('tpl_type', $t), 'OR');
402: }
403: $criteria->add($criteria2);
404: } else {
405: $criteria->add(new Criteria('tpl_type', $type));
406: }
407: }
408: return $this->getTplObjects($criteria, $getsource, false);
409: }
410:
411: 412: 413: 414: 415: 416: 417: 418:
419: public function templateExists($tplname, $tplset_name)
420: {
421: $criteria = new CriteriaCompo(new Criteria('tpl_file', trim($tplname)));
422: $criteria->add(new Criteria('tpl_tplset', trim($tplset_name)));
423: if ($this->getCount($criteria) > 0) {
424: return true;
425: }
426: return false;
427: }
428: }
429: