Completed
Pull Request — master (#541)
by Richard
07:06
created

XoopsTplFileHandler::getModuleTplCount()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 24
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 4.0032

Importance

Changes 0
Metric Value
cc 4
eloc 17
nc 4
nop 1
dl 0
loc 24
rs 8.6845
c 0
b 0
f 0
ccs 16
cts 17
cp 0.9412
crap 4.0032
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       XOOPS Project (http://xoops.org)
13
 * @license         GNU GPL 2 or later (http://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
 * @version         $Id$
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
 * XOOPS template file handler class.
30
 * This class is responsible for providing data access mechanisms to the data source
31
 * of XOOPS template file class objects.
32
 *
33
 * @category  Xoops\Core\Kernel\XoopsTplFileHandler
34
 * @package   Xoops\Core\Kernel
35
 * @author    Kazumi Ono <[email protected]>
36
 * @copyright 2000-2015 XOOPS Project (http://xoops.org)
37
 * @license   GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
38
 * @link      http://xoops.org
39
 */
40
class XoopsTplFileHandler extends XoopsPersistableObjectHandler
41
{
42
43
    /**
44
     * Constructor
45
     *
46
     * @param Connection|null $db database
47
     */
48 13
    public function __construct(Connection $db = null)
49
    {
50 13
        parent::__construct($db, 'system_tplfile', '\Xoops\Core\Kernel\Handlers\XoopsTplFile', 'tpl_id', 'tpl_refid');
51 13
    }
52
53
    /**
54
     * retrieve a specific XoopsTplFile
55
     *
56
     * @param int  $id        tpl_id of the block to retrieve
57
     * @param bool $getsource true = also return source
58
     *
59
     * @return XoopsTplFile|bool
60
     */
61 1
    public function getById($id, $getsource = false)
62
    {
63 1
        $qb = $this->db2->createXoopsQueryBuilder();
64 1
        $eb = $qb->expr();
65 1
        $tplfile = false;
66 1
        $id = (int)($id);
67 1
        if ($id > 0) {
68 1
            if (!$getsource) {
69 1
                $qb->select('*')
70 1
                    ->fromPrefix('system_tplfile', 'f')
71 1
                    ->where($eb->eq('f.tpl_id', ':tplid'))
72 1
                    ->setParameter(':tplid', $id, \PDO::PARAM_INT);
73 View Code Duplication
            } else {
74 1
                $qb->select('f.*')
75 1
                    ->addSelect('s.tpl_source')
76 1
                    ->fromPrefix('system_tplfile', 'f')
77 1
                    ->leftJoinPrefix('f', 'system_tplsource', 's', $eb->eq('s.tpl_id', 'f.tpl_id'))
78 1
                    ->where($eb->eq('f.tpl_id', ':tplid'))
79 1
                    ->setParameter(':tplid', $id, \PDO::PARAM_INT);
80
            }
81 1
            $result = $qb->execute();
82 1
            if (!$result) {
83
                return $tplfile;
84
            }
85 1
            $allrows = $result->fetchAll();
86 1
            if (count($allrows) == 1) {
87 1
                $tplfile = new XoopsTplFile();
88 1
                $tplfile->assignVars(reset($allrows));
89
            }
90
        }
91 1
        return $tplfile;
92
    }
93
94
    /**
95
     * loadSource
96
     *
97
     * @param XoopsTplFile $tplfile object
98
     *
99
     * @return bool
100
     */
101 1
    public function loadSource(XoopsTplFile $tplfile)
102
    {
103 1
        if (!$tplfile->getVar('tpl_source')) {
104 1
            $qb = $this->db2->createXoopsQueryBuilder();
105 1
            $eb = $qb->expr();
106 1
            $qb->select('tpl_source')
107 1
                ->fromPrefix('system_tplsource', null)
108 1
                ->where($eb->eq('tpl_id', ':tplid'))
109 1
                ->setParameter(':tplid', $tplfile->getVar('tpl_id'), \PDO::PARAM_INT);
110 1
            if (!$result = $qb->execute()) {
111
                return false;
112
            }
113 1
            $myrow = $result->fetch(\PDO::FETCH_ASSOC);
114 1
            $tplfile->assignVar('tpl_source', $myrow['tpl_source']);
115
        }
116 1
        return true;
117
    }
118
119
    /**
120
     * write a new TplFile into the database
121
     *
122
     * @param XoopsTplFile $tplfile object
123
     *
124
     * @return bool
125
     */
126 2
    public function insertTpl(XoopsTplFile $tplfile)
127
    {
128 2
        $tpl_id = 0;
129 2
        if (!$tplfile->isDirty()) {
130 1
            return true;
131
        }
132 1
        if (!$tplfile->cleanVars()) {
133
            return false;
134
        }
135
136 1
        $vars = $tplfile->cleanVars;
137 1
        $tpl_module       = $vars['tpl_module'];
138 1
        $tpl_refid        = $vars['tpl_refid'];
139 1
        $tpl_tplset       = $vars['tpl_tplset'];
140 1
        $tpl_file         = $vars['tpl_file'];
141 1
        $tpl_desc         = $vars['tpl_desc'];
142 1
        $tpl_lastmodified = $vars['tpl_lastmodified'];
143 1
        $tpl_lastimported = $vars['tpl_lastimported'];
144 1
        $tpl_type         = $vars['tpl_type'];
145 1
        if (isset($vars['tpl_id'])) {
146
            $tpl_id = $vars['tpl_id'];
147
        }
148 1
        if (isset($vars['tpl_source'])) {
149
            $tpl_source = $vars['tpl_source'];
150
        }
151
152 1
        if ($tplfile->isNew()) {
153 1
            $tpl_id = 0;
154
            $values = array(
155
                // 'tpl_id' => $tpl_id,
156 1
                'tpl_module' => $tpl_module,
157 1
                'tpl_refid' => $tpl_refid,
158 1
                'tpl_tplset' => $tpl_tplset,
159 1
                'tpl_file' => $tpl_file,
160 1
                'tpl_desc' => $tpl_desc,
161 1
                'tpl_lastmodified' => $tpl_lastmodified,
162 1
                'tpl_lastimported' => $tpl_lastimported,
163 1
                'tpl_type' => $tpl_type,
164
            );
165 1
            if (!$this->db2->insertPrefix('system_tplfile', $values)) {
166
                return false;
167
            }
168 1
            if (empty($tpl_id)) {
169 1
                $tpl_id = $this->db2->lastInsertId();
170
            }
171 1
            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 1
            $tplfile->assignVar('tpl_id', $tpl_id);
182
        } else {
183
            $values = array(
184
                // 'tpl_id' => $tpl_id,
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 View Code Duplication
            if (isset($tpl_source) && $tpl_source != '') {
199
                $values = array(
200
                    // 'tpl_id' => $tpl_id,
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 1
        return true;
210
    }
211
212
    /**
213
     * forceUpdate
214
     *
215
     * @param XoopsTplFile $tplfile object
216
     *
217
     * @return bool
218
     */
219 1
    public function forceUpdate(XoopsTplFile $tplfile)
220
    {
221 1
        if (!$tplfile->isDirty()) {
222 1
            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
        //$tpl_id           = $vars['tpl_id'];
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
                // 'tpl_id' => $tpl_id,
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 View Code Duplication
            if (isset($tpl_source) && $tpl_source != '') {
260
                $tpl_id = 0;
261
                $values = array(
262
                    // 'tpl_id' => $tpl_id,
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
     * delete a block from the database
278
     *
279
     * @param XoopsTplFile $tplfile object
280
     *
281
     * @return bool
282
     */
283 1
    public function deleteTpl(XoopsTplFile $tplfile)
284
    {
285 1
        $tpl_id = $tplfile->getVar('tpl_id');
286 1
        if (!$this->db2->deletePrefix('system_tplfile', array('tpl_id' => $tpl_id))) {
287
            return false;
288
        }
289 1
        $this->db2->deletePrefix('system_tplsource', array('tpl_id' => $tpl_id));
290 1
        return true;
291
    }
292
293
    /**
294
     * getTplObjects
295
     *
296
     * @param CriteriaElement|null $criteria  criteria to match
297
     * @param bool                 $getsource include the source
298
     * @param bool                 $id_as_key use the object id as array key
299
     *
300
     * @return array
301
     */
302 2
    public function getTplObjects(CriteriaElement $criteria = null, $getsource = false, $id_as_key = false)
303
    {
304 2
        $qb = $this->db2->createXoopsQueryBuilder();
305 2
        $eb = $qb->expr();
306
307 2
        $ret = array();
308
309 2 View Code Duplication
        if (!$getsource) {
310 2
            $qb->select('*')
311 2
                ->fromPrefix('system_tplfile', 'f');
312
        } else {
313 1
            $qb->select('f.*')
314 1
                ->addSelect('s.tpl_source')
315 1
                ->fromPrefix('system_tplfile', 'f')
316 1
                ->leftJoinPrefix('f', 'system_tplsource', 's', $eb->eq('s.tpl_id', 'f.tpl_id'));
317
        }
318 2
        if (isset($criteria) && ($criteria instanceof CriteriaElement)) {
319 2
            $criteria->renderQb($qb);
320
        }
321 2
        $result = $qb->execute();
322 2
        if (!$result) {
323
            return $ret;
324
        }
325 2 View Code Duplication
        while ($myrow = $result->fetch(\PDO::FETCH_ASSOC)) {
326 2
            $tplfile = new XoopsTplFile();
327 2
            $tplfile->assignVars($myrow);
328 2
            if (!$id_as_key) {
329 2
                $ret[] = $tplfile;
330
            } else {
331 1
                $ret[$myrow['tpl_id']] = $tplfile;
332
            }
333 2
            unset($tplfile);
334
        }
335 2
        return $ret;
336
    }
337
338
    /**
339
     * getModuleTplCount
340
     *
341
     * @param string $tplset tpl set name
342
     *
343
     * @return array
344
     */
345 1
    public function getModuleTplCount($tplset)
346
    {
347 1
        $qb = $this->db2->createXoopsQueryBuilder();
348 1
        $eb = $qb->expr();
349
350 1
        $qb->select('tpl_module')
351 1
            ->addSelect('COUNT(tpl_id) AS count')
352 1
            ->fromPrefix('system_tplfile', null)
353 1
            ->where($eb->eq('tpl_tplset', ':tpset'))
354 1
            ->groupBy('tpl_module')
355 1
            ->setParameter(':tpset', $tplset, \PDO::PARAM_STR);
356
357 1
        $ret = array();
358 1
        $result = $qb->execute();
359 1
        if (!$result) {
360
            return $ret;
361
        }
362 1
        while ($myrow = $result->fetch(\PDO::FETCH_ASSOC)) {
363 1
            if ($myrow['tpl_module'] != '') {
364 1
                $ret[$myrow['tpl_module']] = $myrow['count'];
365
            }
366
        }
367 1
        return $ret;
368
    }
369
370
    /**
371
     * Find Template File
372
     *
373
     * @param string|null $tplset    template set
374
     * @param string|null $type      template type
375
     * @param string|null $refid     reference id
376
     * @param string|null $module    module
377
     * @param string|null $file      file name
378
     * @param bool        $getsource include template source
379
     *
380
     * @return array
381
     */
382 1
    public function find($tplset = null, $type = null, $refid = null, $module = null, $file = null, $getsource = false)
383
    {
384 1
        $criteria = new CriteriaCompo();
385 1
        if (isset($tplset)) {
386 1
            $criteria->add(new Criteria('tpl_tplset', $tplset));
387
        }
388 1
        if (isset($module)) {
389 1
            $criteria->add(new Criteria('tpl_module', $module));
390
        }
391 1
        if (isset($refid)) {
392 1
            $criteria->add(new Criteria('tpl_refid', $refid));
393
        }
394 1
        if (isset($file)) {
395 1
            $criteria->add(new Criteria('tpl_file', $file));
396
        }
397 1 View Code Duplication
        if (isset($type)) {
398 1
            if (is_array($type)) {
399 1
                $criteria2 = new CriteriaCompo();
400 1
                foreach ($type as $t) {
401 1
                    $criteria2->add(new Criteria('tpl_type', $t), 'OR');
402
                }
403 1
                $criteria->add($criteria2);
404
            } else {
405 1
                $criteria->add(new Criteria('tpl_type', $type));
406
            }
407
        }
408 1
        return $this->getTplObjects($criteria, $getsource, false);
409
    }
410
411
    /**
412
     * Template Exists
413
     *
414
     * @param string $tplname     template name
415
     * @param string $tplset_name set name
416
     *
417
     * @return bool
418
     */
419 1
    public function templateExists($tplname, $tplset_name)
420
    {
421 1
        $criteria = new CriteriaCompo(new Criteria('tpl_file', trim($tplname)));
422 1
        $criteria->add(new Criteria('tpl_tplset', trim($tplset_name)));
423 1
        if ($this->getCount($criteria) > 0) {
424 1
            return true;
425
        }
426 1
        return false;
427
    }
428
}
429