Passed
Push — master ( 6ae33d...022918 )
by Michael
03:45 queued 02:16
created

functions.render.php ➔ about_template_lookup()   C

Complexity

Conditions 7
Paths 14

Size

Total Lines 32
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 19
nc 14
nop 1
dl 0
loc 32
rs 6.7272
c 0
b 0
f 0
1
<?php
2
/**
3
 * Article module for XOOPS
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 (https://xoops.org)
13
 * @license         http://www.fsf.org/copyleft/gpl.html GNU public license
14
 * @package         article
15
 * @since           1.0
16
 * @author          Taiwen Jiang <[email protected]>
17
 */
18
defined('XOOPS_ROOT_PATH') || die('Restricted access');
19
20
require_once __DIR__ . '/vars.php';
21
define($GLOBALS['artdirname'] . '_FUNCTIONS_RENDER_LOADED', true);
22
23
/**
24
 * Function to get template file of a specified style of a specified page
25
 *
26
 *
27
 * @param mixed      $page
28
 * @param null|mixed $style
29
 * @return string template file name, using default style if style is invalid
30
 */
31
function about_getTemplate($page = 'index', $style = null)
32
{
33
    global $xoops;
34
35
    $template_dir = $xoops->path("modules/{$GLOBALS['artdirname']}/templates/");
36
    $style        = null === $style ? '' : '_' . $style;
37
    $file_name    = "{$GLOBALS['artdirname']}_{$page}{$style}.tpl";
38
    if (file_exists($template_dir . $file_name)) {
39
        return $file_name;
40
    }
41
    // Couldn't find file, try to see if the "default" style for this page exists
42
    if (!empty($style)) {
43
        $style     = '';
44
        $file_name = "{$GLOBALS['artdirname']}_{$page}{$style}.tpl";
45
        if (file_exists($template_dir . $file_name)) {
46
            return $file_name;
47
        }
48
    }
49
50
    // Couldn't find a suitable template for this page
51
    return null;
52
}
53
54
/**
55
 * Function to get a list of template files of a page, indexed by file name
56
 *
57
 *
58
 * @param mixed      $page
59
 * @param  bool|bool $refresh recreate the data
60
 * @return array
61
 */
62
function about_getTemplateList($page = 'index', $refresh = false)
63
{
64
    $TplFiles = about_getTplPageList($page, $refresh);
65
    $template = [];
66
    if ($TplFiles && is_array($TplFiles)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $TplFiles of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
67
        foreach (array_keys($TplFiles) as $temp) {
68
            $template[$temp] = $temp;
69
        }
70
    }
71
72
    return $template;
73
}
74
75
/**
76
 * Function to get CSS file URL of a style
77
 *
78
 * The hardcoded path is not desirable for theme switch, however, we have to keabout it before getting a good solution for cache
79
 *
80
 *
81
 * @param mixed $style
82
 * @return string file URL, false if not found
83
 */
84
function about_getCss($style = 'default')
85
{
86
    global $xoops;
87
88
    if (is_readable($xoops->path('modules/' . $GLOBALS['artdirname'] . '/assets/css/style_' . mb_strtolower($style) . '.css'))) {
89
        return $xoops->path('modules/' . $GLOBALS['artdirname'] . '/assets/css/style_' . mb_strtolower($style) . '.css', true);
90
    }
91
92
    return $xoops->path('modules/' . $GLOBALS['artdirname'] . '/assets/css/style.css', true);
93
}
94
95
/**
96
 * Function to module header for a page with specified style
97
 *
98
 *
99
 * @param mixed $style
100
 * @return string
101
 */
102
function about_getModuleHeader($style = 'default')
103
{
104
    $xoops_module_header = '<link rel="stylesheet" type="text/css" href="' . about_getCss($style) . '">';
105
106
    return $xoops_module_header;
107
}
108
109
/**
110
 * Function to get a list of template files of a page, indexed by style
111
 *
112
 *
113
 * @param mixed $page
114
 * @param  bool $refresh
115
 * @return array
116
 */
117
function &about_getTplPageList($page = '', $refresh = true)
118
{
119
    $list = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $list is dead and can be removed.
Loading history...
120
121
    $cache_file = empty($page) ? 'template-list' : 'template-page';
122
    /*
123
    load_functions("cache");
124
    $list = mod_loadCacheFile($cache_file, $GLOBALS["artdirname"]);
125
    */
126
127
    xoops_load('xoopscache');
128
    $key  = $GLOBALS['artdirname'] . "_{$cache_file}";
129
    $list = XoopsCache::read($key);
130
131
    if (!is_array($list) || $refresh) {
132
        $list = about_template_lookup(!empty($page));
133
    }
134
135
    $ret = empty($page) ? $list : @$list[$page];
136
137
    return $ret;
138
}
139
140
/**
141
 * @param  bool $index_by_page
142
 * @return array
143
 */
144
function &about_template_lookup($index_by_page = false)
145
{
146
    require_once XOOPS_ROOT_PATH . '/class/xoopslists.php';
147
148
    $files = \XoopsLists::getHtmlListAsArray(XOOPS_ROOT_PATH . '/modules/' . $GLOBALS['artdirname'] . '/templates/');
149
    $list  = [];
150
    foreach ($files as $file => $name) {
151
        // The valid file name must be: art_article_mytpl.html OR art_category-1_your-trial.html
152
        if (preg_match('/^' . $GLOBALS['artdirname'] . "_([^_]*)(_(.*))?\.(tpl|xotpl)$/i", $name, $matches)) {
153
            if (empty($matches[1])) {
154
                continue;
155
            }
156
            if (empty($matches[3])) {
157
                $matches[3] = 'default';
158
            }
159
            if (empty($index_by_page)) {
160
                $list[] = ['file' => $name, 'description' => $matches[3]];
161
            } else {
162
                $list[$matches[1]][$matches[3]] = $name;
163
            }
164
        }
165
    }
166
167
    $cache_file = empty($index_by_page) ? 'template-list' : 'template-page';
168
    xoops_load('xoopscache');
169
    $key = $GLOBALS['artdirname'] . "_{$cache_file}";
170
    XoopsCache::write($key, $list);
171
172
    //load_functions("cache");
173
    //mod_createCacheFile($list, $cache_file, $GLOBALS["artdirname"]);
174
    return $list;
175
}
176