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