Completed
Pull Request — master (#608)
by Richard
15:29
created

XoopsTplFileHandler::loadSource()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3.004

Importance

Changes 0
Metric Value
cc 3
eloc 12
nc 3
nop 1
dl 0
loc 16
rs 9.8666
c 0
b 0
f 0
ccs 12
cts 13
cp 0.9231
crap 3.004
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
use Doctrine\DBAL\FetchMode;
0 ignored issues
show
Bug introduced by
The type Doctrine\DBAL\FetchMode was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
28
use Doctrine\DBAL\ParameterType;
0 ignored issues
show
Bug introduced by
The type Doctrine\DBAL\ParameterType was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

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