Completed
Push — master ( 50d5fe...d807c2 )
by Michael
13s
created

functions.render.php ➔ about_getTemplateList()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 2
nop 2
dl 0
loc 12
rs 9.2
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 22 and the first side effect is on line 19.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
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 (http://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
19
defined('XOOPS_ROOT_PATH') || exit('XOOPS root path not defined');
20
21
include __DIR__ . '/vars.php';
22
define($GLOBALS['artdirname'] . '_FUNCTIONS_RENDER_LOADED', true);
23
24
/**
25
 * Function to get template file of a specified style of a specified page
26
 *
27
 * @var string $page  page name
28
 * @var string $style template style
29
 *
30
 * @return string template file name, using default style if style is invalid
31
 */
32
function about_getTemplate($page = 'index', $style = null)
0 ignored issues
show
Coding Style introduced by
about_getTemplate uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
33
{
34
    global $xoops;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
35
36
    $template_dir = $xoops->path("modules/{$GLOBALS['artdirname']}/templates/");
37
    $style        = empty($style) ? '' : '_' . $style;
38
    $file_name    = "{$GLOBALS['artdirname']}_{$page}{$style}.tpl";
39
    if (file_exists($template_dir . $file_name)) {
40
        return $file_name;
41
    }
42
    // Couldn't find file, try to see if the "default" style for this page exists
43
    if (!empty($style)) {
44
        $style     = '';
45
        $file_name = "{$GLOBALS['artdirname']}_{$page}{$style}.tpl";
46
        if (file_exists($template_dir . $file_name)) {
47
            return $file_name;
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
 * @var    string      $page page name
58
 * @param  bool|boolean $refresh recreate the data
59
 * @return array
60
 *
61
 */
62
function &about_getTemplateList($page = 'index', $refresh = false)
63
{
64
    $TplFiles = about_getTplPageList($page, $refresh);
65
    $template = array();
66
    if (is_array($TplFiles) && count($TplFiles) > 0) {
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
 * @var string $style
81
 *
82
 * @return string file URL, false if not found
83
 */
84
function about_getCss($style = 'default')
0 ignored issues
show
Coding Style introduced by
about_getCss uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
85
{
86
    global $xoops;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
87
88
    if (is_readable($xoops->path('modules/' . $GLOBALS['artdirname'] . '/assets/css/style_' . strtolower($style) . '.css'))) {
89
        return $xoops->path('modules/' . $GLOBALS['artdirname'] . '/assets/css/style_' . 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
 * @var string $style
99
 *
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
 * @var string  $page page name
113
 *
114
 * @param  bool $refresh
115
 * @return array
116
 */
117
function &about_getTplPageList($page = '', $refresh = true)
0 ignored issues
show
Coding Style introduced by
about_getTplPageList uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
118
{
119
    $list = null;
0 ignored issues
show
Unused Code introduced by
$list is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
120
121
    $cache_file = empty($page) ? 'template-list' : 'template-page';
122
    /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
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)
0 ignored issues
show
Coding Style introduced by
about_template_lookup uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
145
{
146
    include_once XOOPS_ROOT_PATH . '/class/xoopslists.php';
147
148
    $files = XoopsLists::getHtmlListAsArray(XOOPS_ROOT_PATH . '/modules/' . $GLOBALS['artdirname'] . '/templates/');
149
    $list  = array();
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[] = array('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"]);
0 ignored issues
show
Unused Code Comprehensibility introduced by
79% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
174
    return $list;
175
}
176