Utility::getTemplate()   A
last analyzed

Complexity

Conditions 5
Paths 8

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 12
c 0
b 0
f 0
dl 0
loc 21
rs 9.5555
cc 5
nc 8
nop 2
1
<?php
2
3
namespace XoopsModules\About;
4
5
/*
6
 About Utility Class Definition
7
8
 You may not change or alter any portion of this comment or credits of
9
 supporting developers from this source code or any supporting source code
10
 which is considered copyrighted (c) material of the original comment or credit
11
 authors.
12
13
 This program is distributed in the hope that it will be useful, but
14
 WITHOUT ANY WARRANTY; without even the implied warranty of
15
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16
 */
17
18
/**
19
 * Module:  About
20
 *
21
 * @package      ::    \module\About\class
22
 * @license      http://www.fsf.org/copyleft/gpl.html GNU public license
23
 * @copyright    https://xoops.org 2001-2017 &copy; XOOPS Project
24
 * @author       ZySpec <[email protected]>
25
 * @author       Mamba <[email protected]>
26
 * @since        ::      File available since version 1.54
27
 */
28
29
use XoopsModules\About;
30
use XoopsModules\About\Common;
31
use XoopsModules\About\Constants;
32
33
require_once dirname(__DIR__) . '/include/vars.php';
34
define($GLOBALS['artdirname'] . '_FUNCTIONS_RENDER_LOADED', true);
35
36
/**
37
 * Class Utility
38
 */
39
class Utility extends Common\SysUtility
40
{
41
    //--------------- Custom module methods -----------------------------
42
    /**
43
     * @param       $dir
44
     * @param int   $mode
45
     * @param bool  $recursive
46
     * @return bool
47
     */
48
    public static function aboutmkdirs($dir, $mode = 0777, $recursive = true)
49
    {
50
        if ('' === $dir || null === $dir) {
51
            return $dir;
52
        }
53
        if ('/' === $dir || \is_dir($dir)) {
54
            return $dir;
55
        }
56
        if (static::aboutmkdirs(\dirname($dir), $mode, $recursive)) {
57
            return \mkdir($dir, $mode);
58
        }
59
60
        return $dir;
61
    }
62
63
    /**
64
     * Function to get template file of a specified style of a specified page
65
     *
66
     *
67
     * @param mixed      $page
68
     * @param null|mixed $style
69
     * @return string template file name, using default style if style is invalid
70
     */
71
    public static function getTemplate($page = 'index', $style = null)
72
    {
73
        global $xoops;
74
75
        $template_dir = $xoops->path("modules/{$GLOBALS['artdirname']}/templates/");
76
        $style        = null === $style ? '' : '_' . $style;
77
        $file_name    = "{$GLOBALS['artdirname']}_{$page}{$style}.tpl";
78
        if (file_exists($template_dir . $file_name)) {
79
            return $file_name;
80
        }
81
        // Couldn't find file, try to see if the "default" style for this page exists
82
        if (!empty($style)) {
83
            $style     = '';
84
            $file_name = "{$GLOBALS['artdirname']}_{$page}{$style}.tpl";
85
            if (file_exists($template_dir . $file_name)) {
86
                return $file_name;
87
            }
88
        }
89
90
        // Couldn't find a suitable template for this page
91
        return null;
92
    }
93
94
    /**
95
     * Function to get a list of template files of a page, indexed by file name
96
     *
97
     *
98
     * @param mixed     $page
99
     * @param bool $refresh recreate the data
100
     * @return array
101
     */
102
    public static function getTemplateList($page = 'index', $refresh = false)
103
    {
104
        $tplFiles = self::getTplPageList($page, $refresh);
105
        $template = [];
106
        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...
107
            foreach (array_keys($tplFiles) as $temp) {
108
                $template[$temp] = $temp;
109
            }
110
        }
111
112
        return $template;
113
    }
114
115
    /**
116
     * Function to get CSS file URL of a style
117
     *
118
     * The hardcoded path is not desirable for theme switch, however, we have to keabout it before getting a good solution for cache
119
     *
120
     *
121
     * @param mixed $style
122
     * @return string file URL, false if not found
123
     */
124
    public static function getCss($style = 'default')
125
    {
126
        global $xoops;
127
128
        if (is_readable($xoops->path('modules/' . $GLOBALS['artdirname'] . '/assets/css/style_' . mb_strtolower($style) . '.css'))) {
129
            return $xoops->path('modules/' . $GLOBALS['artdirname'] . '/assets/css/style_' . mb_strtolower($style) . '.css', true);
130
        }
131
132
        return $xoops->path('modules/' . $GLOBALS['artdirname'] . '/assets/css/style.css', true);
133
    }
134
135
    /**
136
     * Function to module header for a page with specified style
137
     *
138
     *
139
     * @param mixed $style
140
     * @return string
141
     */
142
    public static function getModuleHeader($style = 'default')
143
    {
144
        $xoops_module_header = '<link rel="stylesheet" type="text/css" href="' . self::getCss($style) . '">';
145
146
        return $xoops_module_header;
147
    }
148
149
    /**
150
     * Function to get a list of template files of a page, indexed by style
151
     *
152
     *
153
     * @param mixed $page
154
     * @param bool  $refresh
155
     * @return array
156
     */
157
    public static function &getTplPageList($page = '', $refresh = true)
158
    {
159
        $list = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $list is dead and can be removed.
Loading history...
160
161
        $cache_file = empty($page) ? 'template-list' : 'template-page';
162
        /*
163
        load_functions("cache");
164
        $list = mod_loadCacheFile($cache_file, $GLOBALS["artdirname"]);
165
        */
166
167
        xoops_load('xoopscache');
168
        $key  = $GLOBALS['artdirname'] . "_{$cache_file}";
169
        $list = \XoopsCache::read($key);
170
171
        if (!is_array($list) || $refresh) {
172
            $list = self::template_lookup(!empty($page));
173
        }
174
175
        $ret = empty($page) ? $list : @$list[$page];
176
177
        return $ret;
178
    }
179
180
    /**
181
     * @param bool $index_by_page
182
     * @return array
183
     */
184
    public static function &template_lookup($index_by_page = false)
185
    {
186
        require_once XOOPS_ROOT_PATH . '/class/xoopslists.php';
187
188
        $files = \XoopsLists::getHtmlListAsArray(XOOPS_ROOT_PATH . '/modules/' . $GLOBALS['artdirname'] . '/templates/');
189
        $list  = [];
190
        foreach ($files as $file => $name) {
191
            // The valid file name must be: art_article_mytpl.html OR art_category-1_your-trial.html
192
            if (preg_match('/^' . $GLOBALS['artdirname'] . "_([^_]*)(_(.*))?\.(tpl|xotpl)$/i", $name, $matches)) {
193
                if (empty($matches[1])) {
194
                    continue;
195
                }
196
                if (empty($matches[3])) {
197
                    $matches[3] = 'default';
198
                }
199
                if (empty($index_by_page)) {
200
                    $list[] = ['file' => $name, 'description' => $matches[3]];
201
                } else {
202
                    $list[$matches[1]][$matches[3]] = $name;
203
                }
204
            }
205
        }
206
207
        $cache_file = empty($index_by_page) ? 'template-list' : 'template-page';
208
        xoops_load('xoopscache');
209
        $key = $GLOBALS['artdirname'] . "_{$cache_file}";
210
        \XoopsCache::write($key, $list);
211
212
        //load_functions("cache");
213
        //mod_createCacheFile($list, $cache_file, $GLOBALS["artdirname"]);
214
        return $list;
215
    }
216
217
}
218