Completed
Push — master ( 77bd5e...c97259 )
by Nazar
04:13
created

prepare.php ➔ cs\modules\Static_pages\get_pages_rows()   C

Complexity

Conditions 8
Paths 8

Size

Total Lines 52
Code Lines 32

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 8
eloc 32
nc 8
nop 0
dl 0
loc 52
rs 6.8493

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * @package   Static Pages
4
 * @category  modules
5
 * @author    Nazar Mokrynskyi <[email protected]>
6
 * @copyright Copyright (c) 2011-2016, Nazar Mokrynskyi
7
 * @license   MIT License, see license.txt
8
 */
9
namespace cs\modules\Static_pages;
10
use
11
	h,
12
	cs\Language\Prefix,
13
	cs\Page,
14
	cs\Request;
15
16
$Page = Page::instance();
17
$Page->css('components/modules/Static_pages/includes/css/style.css');
18
function get_categories_rows ($structure = null, $level = 0, $parent_categories = []) {
19
	$L    = new Prefix('static_pages_');
20
	$root = false;
21
	if ($structure === null) {
22
		$structure          = Pages::instance()->get_structure();
23
		$structure['title'] = $L->root_category;
24
		$root               = true;
25
	}
26
	$parent_categories[] = $structure['id'];
27
	$content             = [
28
		[
29
			[
30
				h::a(
31
					$structure['title'].
32
					h::{'b.cs-static-pages-count'}(
33
						count($structure['pages']),
34
						[
35
							'tooltip' => $L->pages_in_category
36
						]
37
					),
38
					[
39
						'href' => 'admin/Static_pages/browse_pages/'.implode('/', $parent_categories)
40
					]
41
				),
42
				[
43
					'class' => "cs-static-pages-padding-left-$level"
44
				]
45
			],
46
			h::{'a[is=cs-link-button][icon=plus]'}(
47
				[
48
					'href'    => "admin/Static_pages/add_category/$structure[id]",
49
					'tooltip' => $L->add_subcategory
50
				]
51
			).
52
			h::{'a[is=cs-link-button][icon=file-text]'}(
53
				[
54
					'href'    => "admin/Static_pages/add_page/$structure[id]",
55
					'tooltip' => $L->add_page
56
				]
57
			).
58
			(!$root ?
59
				h::{'a[is=cs-link-button][icon=pencil]'}(
60
					[
61
						'href'    => "admin/Static_pages/edit_category/$structure[id]",
62
						'tooltip' => $L->edit
63
					]
64
				).
65
				h::{'a[is=cs-link-button][icon=trash]'}(
66
					[
67
						'href'    => "admin/Static_pages/delete_category/$structure[id]",
68
						'tooltip' => $L->delete
69
					]
70
				)
71
				: false
72
			)
73
		]
74
	];
75
	if (!empty($structure['categories'])) {
76
		foreach ($structure['categories'] as $category) {
77
			$content = array_merge($content, get_categories_rows($category, $level + 1, $parent_categories));
78
		}
79
	}
80
	return $content;
81
}
82
83
function get_categories_list ($current = null, $structure = null, $level = 0) {
84
	$list = [
85
		'in'    => [],
86
		'value' => []
87
	];
88
	if ($structure === null) {
89
		$structure       = Pages::instance()->get_structure();
90
		$L               = new Prefix('static_pages_');
91
		$list['in'][]    = $L->root_category;
92
		$list['value'][] = 0;
93
	} else {
94
		if ($structure['id'] == $current) {
95
			return $list;
96
		}
97
		$list['in'][]    = str_repeat('&nbsp;', $level).$structure['title'];
98
		$list['value'][] = $structure['id'];
99
	}
100
	if (!empty($structure['categories'])) {
101
		foreach ($structure['categories'] as $category) {
102
			$tmp           = get_categories_list($current, $category, $level + 1);
103
			$list['in']    = array_merge($list['in'], $tmp['in']);
104
			$list['value'] = array_merge($list['value'], $tmp['value']);
105
		}
106
	}
107
	return $list;
108
}
109
110
function get_pages_rows () {
111
	$L          = new Prefix('static_pages_');
112
	$Pages      = Pages::instance();
113
	$Categories = Categories::instance();
114
	$categories = array_slice(Request::instance()->route, 2);
115
	$structure  = $Pages->get_structure();
116
	$path       = [];
117
	if (!empty($categories)) {
118
		foreach ($categories as $category) {
119
			$category = $Categories->get($category)['path'];
120
			if (isset($structure['categories'][$category])) {
121
				$structure = $structure['categories'][$category];
122
				$path[]    = $structure['path'];
123
			}
124
		}
125
	}
126
	Page::instance()->title($structure['id'] == 0 ? $L->root_category : $structure['title']);
127
	$path    = !empty($path) ? implode('/', $path).'/' : '';
128
	$content = [];
129
	if (!empty($structure['pages'])) {
130
		foreach ($structure['pages'] as &$page) {
131
			$page      = $Pages->get($page);
132
			$content[] = [
133
				[
134
					h::a(
135
						$page['title'],
136
						[
137
							'href' => $path.$page['path']
138
						]
139
					),
140
					[
141
						'class' => 'cs-static-pages-padding-left-0'
142
					]
143
				],
144
				h::{'a[is=cs-link-button][icon=file-text]'}(
145
					[
146
						'href'    => "admin/Static_pages/edit_page/$page[id]",
147
						'tooltip' => $L->edit
148
					]
149
				).
150
				h::{'a[is=cs-link-button][icon=trash]'}(
151
					[
152
						'href'    => "admin/Static_pages/delete_page/$page[id]",
153
						'tooltip' => $L->delete
154
					]
155
				)
156
			];
157
		}
158
		unset($page);
159
	}
160
	return $content;
161
}
162