Completed
Push — development ( cfd391...deed4d )
by Andrij
12:03
created

category_helper.php ➔ _get_category_pages()   C

Complexity

Conditions 7
Paths 10

Size

Total Lines 38
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 29
nc 10
nop 4
dl 0
loc 38
rs 6.7272
c 0
b 0
f 0
1
<?php
2
3
(defined('BASEPATH')) OR exit('No direct script access allowed');
0 ignored issues
show
Coding Style introduced by
As per coding-style, PHP keywords should be in lowercase; expected or, but found OR.
Loading history...
4
5
if (!function_exists('get_sub_categories')) {
6
7
    /**
8
     * Short description for function
9
     *
10
     * Long description (if any) ...
11
     *
12
     * @param integer $category_id Parameter description (if any) ...
13
     * @return array   Return description (if any) ...
14
     */
15
    function get_sub_categories($category_id = 0) {
16
        $ci = & get_instance();
17
        $categories = $ci->lib_category->unsorted();
18
19
        $result = [];
20
21
        foreach ($categories as $category) {
22
            if ($category['parent_id'] == $category_id) {
23
                $result[] = $category;
24
            }
25
        }
26
27
        return $result;
28
    }
29
30
}
31
32
if (!function_exists('category_list')) {
33
34
    /**
35
     * @param string $exclude_cats
36
     * @return array
37
     */
38
    function category_list($exclude_cats = '') {
39
        $ci = & get_instance();
40
        $ci->load->helper('html');
41
        $ci->load->module('core');
42
        $categories = $ci->lib_category->unsorted();
43
44
        $exclude_cats = explode(',', $exclude_cats);
45
46
        $result = [];
47
48
        foreach ($categories as $row) {
49
            if (!in_array($row['id'], $exclude_cats)) {
50
                $row['fetch_pages'] = unserialize($row['fetch_pages']);
51
52
                $total_pages = _get_category_pages($row, 0, 0, TRUE);
53
                $result[] = '<a href="' . site_url($row['path_url']) . '">' . $row['name'] . ' (' . $total_pages . ')</a>';
54
            }
55
        }
56
57
        return ul($result);
58
    }
59
60
}
61
62
if (!function_exists('sub_category_list')) {
63
64
    /**
65
     * @param integer $category_id
66
     * @return mixed
67
     */
68
    function sub_category_list($category_id = 0) {
69
        $ci = & get_instance();
70
        $ci->load->helper('html');
71
        $ci->load->module('core');
72
73
        if ($category_id > 0) {
74
            $categories = get_sub_categories($category_id);
75
76
            if (count($categories) > 0) {
77
                foreach ($categories as $row) {
78
                    $row['fetch_pages'] = unserialize($row['fetch_pages']);
79
80
                    $total_pages = _get_category_pages($row, 0, 0, TRUE);
81
                    $result[] = '<a href="' . site_url($row['path_url']) . '">' . $row['name'] . ' (' . $total_pages . ')</a>';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$result was never initialized. Although not strictly required by PHP, it is generally a good practice to add $result = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
82
                }
83
84
                return ul($result);
85
            } else {
86
                return FALSE;
87
            }
88
        } else {
89
            return FALSE;
90
        }
91
    }
92
93
}
94
95
if (!function_exists('get_category_name')) {
96
97
    /**
98
     * @param integer $id
99
     * @return mixed
100
     */
101
    function get_category_name($id) {
102
        $ci = & get_instance();
103
        $c = $ci->lib_category->get_category($id);
104
105
        if ($c['name'] == '') {
106
            $c['name'] = lang('No category');
107
        }
108
109
        return $c['name'];
110
    }
111
112
}
113
114
if (!function_exists('_get_category_pages')) {
115
116
    /**
117
     * Select or count pages in category
118
     * @param array $category
119
     * @param int $row_count
120
     * @param int $offset
121
     * @param bool|FALSE $count
122
     * @return array|string
123
     */
124
    function _get_category_pages(array $category = [], $row_count = 0, $offset = 0, $count = FALSE) {
125
        $ci = & get_instance();
126
127
        $ci->db->where('post_status', 'publish');
128
        $ci->db->where('publish_date <=', time());
129
        $ci->db->where('lang', $ci->config->item('cur_lang'));
130
        if (count($category['fetch_pages']) > 0) {
131
            $category['fetch_pages'][] = $category['id'];
132
            $ci->db->where_in('category', $category['fetch_pages']);
133
        } else {
134
            $ci->db->where('category', $category['id']);
135
        }
136
        $ci->db->select('content.*');
137
        $ci->db->select('IF(route.parent_url <> \'\', concat(route.parent_url, \'/\', route.url), route.url) as full_url', FALSE);
138
        $ci->db->order_by($category['order_by'], $category['sort_order']);
139
        $ci->db->join('route', 'route.id=content.route_id');
140
141
        if ($count === FALSE) {
142
            if ($row_count > 0) {
143
                $query = $ci->db->get('content', (int) $row_count, (int) $offset);
144
            } else {
145
                $query = $ci->db->get('content');
146
            }
147
        } else {
148
            $ci->db->from('content');
149
            return $ci->db->count_all_results();
150
        }
151
        $pages = $query->result_array();
152
153
        if (count($pages) > 0 AND is_array($pages)) {
0 ignored issues
show
Coding Style introduced by
As per coding-style, PHP keywords should be in lowercase; expected and, but found AND.
Loading history...
154
            $n = 0;
155
            foreach ($pages as $p) {
156
                $pages[$n] = $ci->cfcm->connect_fields($p, 'page');
157
                $n++;
158
            }
159
        }
160
        return $pages;
161
    }
162
163
}
164
165
/* End of file category_helper.php */