1: <?php
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31: if (!defined('XOOPS_ROOT_PATH')) {
32: exit();
33: }
34: class XoopsTplfile extends XoopsObject
35: {
36:
37: function XoopsTplfile()
38: {
39: static $initVars;
40: if (isset($initVars)) {
41: $this->vars = $initVars;
42: return;
43: }
44: $this->XoopsObject();
45: $this->initVar('tpl_id', XOBJ_DTYPE_INT, null, false);
46: $this->initVar('tpl_refid', XOBJ_DTYPE_INT, 0, false);
47: $this->initVar('tpl_tplset', XOBJ_DTYPE_OTHER, null, false);
48: $this->initVar('tpl_file', XOBJ_DTYPE_TXTBOX, null, true, 100);
49: $this->initVar('tpl_desc', XOBJ_DTYPE_TXTBOX, null, false, 100);
50: $this->initVar('tpl_lastmodified', XOBJ_DTYPE_INT, 0, false);
51: $this->initVar('tpl_lastimported', XOBJ_DTYPE_INT, 0, false);
52: $this->initVar('tpl_module', XOBJ_DTYPE_OTHER, null, false);
53: $this->initVar('tpl_type', XOBJ_DTYPE_OTHER, null, false);
54: $this->initVar('tpl_source', XOBJ_DTYPE_SOURCE, null, false);
55: $initVars = $this->vars;
56: }
57:
58: function &getSource()
59: {
60: $ret =& $this->getVar('tpl_source');
61: return $ret;
62: }
63:
64: function getLastModified()
65: {
66: return $this->getVar('tpl_lastmodified');
67: }
68: }
69:
70: 71: 72: 73: 74: 75: 76: 77:
78:
79: class XoopsTplfileHandler extends XoopsObjectHandler
80: {
81:
82: function &create($isNew = true)
83: {
84: $tplfile =new XoopsTplfile();
85: if ($isNew) {
86: $tplfile->setNew();
87: }
88: return $tplfile;
89: }
90:
91: function &get($id, $getsource = false)
92: {
93: $ret = false;
94: $id = (int)$id;
95: if ($id > 0) {
96: if (!$getsource) {
97: $sql = 'SELECT * FROM '.$this->db->prefix('tplfile').' WHERE tpl_id='.$id;
98: } else {
99: $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;
100: }
101: if ($result = $this->db->query($sql)) {
102: $numrows = $this->db->getRowsNum($result);
103: if ($numrows == 1) {
104: $ret =new XoopsTplfile();
105: $ret->assignVars($this->db->fetchArray($result));
106: }
107: }
108: }
109: return $ret;
110: }
111:
112: function loadSource(&$tplfile)
113: {
114: if (strtolower(get_class($tplfile)) != 'xoopstplfile') {
115: return false;
116: }
117: if (!$tplfile->getVar('tpl_source')) {
118: $sql = 'SELECT tpl_source FROM '.$this->db->prefix('tplsource').' WHERE tpl_id='.$tplfile->getVar('tpl_id');
119: if (!$result = $this->db->query($sql)) {
120: return false;
121: }
122: $myrow = $this->db->fetchArray($result);
123: $tplfile->assignVar('tpl_source', $myrow['tpl_source']);
124: }
125: return true;
126: }
127:
128: function insert(&$tplfile)
129: {
130: if (strtolower(get_class($tplfile)) != 'xoopstplfile') {
131: return false;
132: }
133: if (!$tplfile->isDirty()) {
134: return true;
135: }
136: if (!$tplfile->cleanVars()) {
137: return false;
138: }
139: foreach ($tplfile->cleanVars as $k => $v) {
140: ${$k} = $v;
141: }
142: if ($tplfile->isNew()) {
143: $tpl_id = $this->db->genId('tplfile_tpl_id_seq');
144: $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));
145: if (!$result = $this->db->query($sql)) {
146: return false;
147: }
148: if (empty($tpl_id)) {
149: $tpl_id = $this->db->getInsertId();
150: }
151: if (isset($tpl_source) && $tpl_source != '') {
152: $sql = sprintf("INSERT INTO %s (tpl_id, tpl_source) VALUES (%u, %s)", $this->db->prefix('tplsource'), $tpl_id, $this->db->quoteString($tpl_source));
153: if (!$result = $this->db->query($sql)) {
154: $this->db->query(sprintf("DELETE FROM %s WHERE tpl_id = %u", $this->db->prefix('tplfile'), $tpl_id));
155: return false;
156: }
157: }
158: $tplfile->assignVar('tpl_id', $tpl_id);
159: } else {
160: $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);
161: if (!$result = $this->db->query($sql)) {
162: return false;
163: }
164: if (isset($tpl_source) && $tpl_source != '') {
165: $sql = sprintf("UPDATE %s SET tpl_source = %s WHERE tpl_id = %u", $this->db->prefix('tplsource'), $this->db->quoteString($tpl_source), $tpl_id);
166: if (!$result = $this->db->query($sql)) {
167: return false;
168: }
169: }
170: }
171: return true;
172: }
173:
174: function forceUpdate(&$tplfile)
175: {
176: if (strtolower(get_class($tplfile)) != 'xoopstplfile') {
177: return false;
178: }
179: if (!$tplfile->isDirty()) {
180: return true;
181: }
182: if (!$tplfile->cleanVars()) {
183: return false;
184: }
185: foreach ($tplfile->cleanVars as $k => $v) {
186: ${$k} = $v;
187: }
188: if (!$tplfile->isNew()) {
189: $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);
190: if (!$result = $this->db->queryF($sql)) {
191: return false;
192: }
193: if (isset($tpl_source) && $tpl_source != '') {
194: $sql = sprintf("UPDATE %s SET tpl_source = %s WHERE tpl_id = %u", $this->db->prefix('tplsource'), $this->db->quoteString($tpl_source), $tpl_id);
195: if (!$result = $this->db->queryF($sql)) {
196: return false;
197: }
198: }
199: return true;
200: } else {
201: return false;
202: }
203: }
204:
205: function delete(&$tplfile)
206: {
207: if (strtolower(get_class($tplfile)) != 'xoopstplfile') {
208: return false;
209: }
210: $id = $tplfile->getVar('tpl_id');
211: $sql = sprintf("DELETE FROM %s WHERE tpl_id = %u", $this->db->prefix('tplfile'), $id);
212: if (!$result = $this->db->query($sql)) {
213: return false;
214: }
215: $sql = sprintf("DELETE FROM %s WHERE tpl_id = %u", $this->db->prefix('tplsource'), $id);
216: $this->db->query($sql);
217: return true;
218: }
219:
220: 221: 222: 223: 224:
225: function deleteAll($criteria = null)
226: {
227: $sql = sprintf("SELECT tpl_id FROM %s", $this->db->prefix('tplfile'));
228: $sql .= ' ' . $criteria->renderWhere();
229:
230: $result = $this->db->query($sql);
231: while ($row = $this->db->fetchArray($result)) {
232: $sql = sprintf("DELETE FROM %s WHERE tpl_id=%u", $this->db->prefix('tplsource'), $row['tpl_id']);
233: $this->db->query($sql);
234: }
235:
236: $sql = sprintf("DELETE FROM %s", $this->db->prefix('tplfile'));
237: $sql .= ' ' . $criteria->renderWhere();
238:
239: return $this->db->query($sql);
240: }
241:
242: function &getObjects($criteria = null, $getsource = false, $id_as_key = false)
243: {
244: $ret = array();
245: $limit = $start = 0;
246: if ($getsource) {
247: $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';
248: } else {
249: $sql = 'SELECT * FROM '.$this->db->prefix('tplfile');
250: }
251: if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
252: $sql .= ' '.$criteria->renderWhere().' ORDER BY tpl_refid';
253: $limit = $criteria->getLimit();
254: $start = $criteria->getStart();
255: }
256: $result = $this->db->query($sql, $limit, $start);
257: if (!$result) {
258: return $ret;
259: }
260: while ($myrow = $this->db->fetchArray($result)) {
261: $tplfile =new XoopsTplfile();
262: $tplfile->assignVars($myrow);
263: if (!$id_as_key) {
264: $ret[] =& $tplfile;
265: } else {
266: $ret[$myrow['tpl_id']] =& $tplfile;
267: }
268: unset($tplfile);
269: }
270: return $ret;
271: }
272:
273: function getCount($criteria = null)
274: {
275: $sql = 'SELECT COUNT(*) FROM '.$this->db->prefix('tplfile');
276: if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
277: $sql .= ' '.$criteria->renderWhere();
278: }
279: if (!$result =& $this->db->query($sql)) {
280: return 0;
281: }
282: list($count) = $this->db->fetchRow($result);
283: return $count;
284: }
285:
286: function getModuleTplCount($tplset)
287: {
288: $ret = array();
289: $sql = "SELECT tpl_module, COUNT(tpl_id) AS count FROM ".$this->db->prefix('tplfile')." WHERE tpl_tplset=".$this->db->quoteString($tplset)." GROUP BY tpl_module";
290: $result = $this->db->query($sql);
291: if (!$result) {
292: return $ret;
293: }
294: while ($myrow = $this->db->fetchArray($result)) {
295: if ($myrow['tpl_module'] != '') {
296: $ret[$myrow['tpl_module']] = $myrow['count'];
297: }
298: }
299: return $ret;
300: }
301:
302: function &find($tplset = null, $type = null, $refid = null, $module = null, $file = null, $getsource = false)
303: {
304: $criteria = new CriteriaCompo();
305: if (isset($tplset)) {
306: $criteria->add(new Criteria('tpl_tplset', addslashes(trim($tplset))));
307: }
308: if (isset($module)) {
309: $criteria->add(new Criteria('tpl_module', $module));
310: }
311: if (isset($refid)) {
312: $criteria->add(new Criteria('tpl_refid', $refid));
313: }
314: if (isset($file)) {
315: $criteria->add(new Criteria('tpl_file', addslashes(trim($file))));
316: }
317: if (isset($type)) {
318: if (is_array($type)) {
319: $criteria2 = new CriteriaCompo();
320: foreach ($type as $t) {
321: $criteria2->add(new Criteria('tpl_type', addslashes(trim($t))), 'OR');
322: }
323: $criteria->add($criteria2);
324: } else {
325: $criteria->add(new Criteria('tpl_type', addslashes(trim($type))));
326: }
327: }
328: $ret =& $this->getObjects($criteria, $getsource, false);
329: return $ret;
330: }
331:
332: function templateExists($tplname, $tplset_name)
333: {
334: $criteria = new CriteriaCompo(new Criteria('tpl_file', addslashes(trim($tplname))));
335: $criteria->add(new Criteria('tpl_tplset', addslashes(trim($tplset_name))));
336: if ($this->getCount($criteria) > 0) {
337: return true;
338: }
339: return false;
340: }
341: }
342: ?>
343: