Completed
Push — develop ( 06fc14...733603 )
by Daniel
27:44 queued 12:50
created

item::is_extension_route()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
ccs 0
cts 0
cp 0
rs 9.2
cc 4
eloc 3
nc 6
nop 1
crap 20
1
<?php
2
/**
3
 *
4
 * @package sitemaker
5
 * @copyright (c) 2013 Daniel A. (blitze)
6
 * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
7
 *
8
 */
9
10
namespace blitze\sitemaker\model\menus\entity;
11
12
use blitze\sitemaker\model\base_entity;
13
14
/**
15
 * @method integer get_item_id()
16
 * @method object set_menu_id($menu_id)
17
 * @method integer get_menu_id()
18
 * @method object set_parent_id($parent_id)
19
 * @method integer get_parent_id()
20
 * @method string get_item_title()
21
 * @method string get_item_url()
22
 * @method string get_item_icon()
23
 * @method object set_item_target($item_target)
24
 * @method integer get_item_target()
25
 * @method object set_left_id($left_id)
26
 * @method integer get_left_id()
27
 * @method object set_right_id($right_id)
28
 * @method integer get_right_id()
29
 * @method object item_parents($item_parents)
30
 * @method integer get_item_parents()
31
 * @method object set_depth($depth)
32
 * @method integer get_depth()
33
 */
34
final class item extends base_entity
35
{
36
	/** @var integer */
37
	protected $item_id;
38
39
	/** @var integer */
40
	protected $menu_id = 0;
41
42
	/** @var integer */
43
	protected $parent_id = 0;
44
45
	/** @var string */
46
	protected $item_title = '';
47
48
	/** @var string */
49
	protected $item_url = '';
50
51
	/** @var string */
52
	protected $item_icon = '';
53
54
	/** @var integer */
55
	protected $item_target = 0;
56
57
	/** @var integer */
58
	protected $left_id = 0;
59
60
	/** @var integer */
61
	protected $right_id = 0;
62
63
	/** @var string */
64
	protected $item_parents = '';
65
66
	/** @var integer */
67
	protected $depth = 0;
68
69
	/** @var string */
70
	protected $full_url = '';
71
72
	/** @var string */
73
	protected $board_url;
74
75
	/** @var boolean */
76
	protected $mod_rewrite_enabled;
77
78
	/** @var array */
79
	protected $required_fields = array('menu_id');
80
81
	/** @var array */
82
	protected $db_fields = array(
83
		'item_id',
84
		'menu_id',
85
		'parent_id',
86
		'item_title',
87
		'item_url',
88
		'item_icon',
89
		'item_target',
90
		'left_id',
91
		'right_id',
92
		'item_parents',
93
		'depth',
94
	);
95
96
	/**
97
	 * Class constructor
98
	 * @param array $data
99
	 * @param bool  $mod_rewrite_enabled
100 62
	 */
101
	public function __construct(array $data, $mod_rewrite_enabled = false)
102 62
	{
103 62
		$this->board_url = generate_board_url();
104
		$this->mod_rewrite_enabled = $mod_rewrite_enabled;
105 62
106 62
		parent::__construct($data);
107
	}
108
109
	/**
110
	 * Set block ID
111
	 * @param int $item_id
112
	 * @return $this
113 44
	 */
114
	public function set_item_id($item_id)
115 44
	{
116 44
		if (!$this->item_id)
117 44
		{
118 44
			$this->item_id = (int) $item_id;
119 44
		}
120
		return $this;
121
	}
122
123
	/**
124
	 * @param string $item_title
125
	 * @return $this
126 44
	 */
127
	public function set_item_title($item_title)
128 44
	{
129 44
		$this->item_title = utf8_ucfirst(trim($item_title));
130
		return $this;
131
	}
132
133
	/**
134
	 * @param string $icon
135
	 * @return $this
136 42
	 */
137
	public function set_item_icon($icon)
138 42
	{
139 42
		$this->item_icon = ($icon) ? trim($icon) . ' ' : '';
140
		return $this;
141
	}
142
143
	/**
144
	 * @param string $item_url
145
	 * @return $this
146 49
	 */
147
	public function set_item_url($item_url)
148 49
	{
149 49
		$this->item_url = ltrim(str_replace($this->board_url, '', $item_url), './');
150
151 49
		// to make things uniform and easy to switch between mod_rewrite_enabled or not without
152 49
		// having to edit menu items, we add app.php/ for all extension routes
153 4
		if ($this->item_url && $this->is_extension_route($item_url))
154 4
		{
155
			$this->item_url = 'app.php/' . $this->item_url;
156 49
		}
157
158
		return $this;
159
	}
160
161
	/**
162 32
	 * @return string
163
	 */
164 32
	public function get_full_url()
165 32
	{
166
		$item_url = $this->item_url;
167 32
		$host = parse_url($item_url, PHP_URL_HOST);
168 32
169 20
		if ($item_url && empty($host))
170
		{
171 20
			$item_url = $this->board_url . '/' . $item_url;
172 20
173 2
			if ($this->mod_rewrite_enabled === true)
174 2
			{
175 20
				$item_url = str_replace('app.php/', '', $item_url);
176
			}
177 32
		}
178
179
		return $item_url;
180
	}
181
182
	/**
183
	 * Checks if a url is an extension route
184
	 *
185
	 * @param string $item_url
186
	 * @return true|false
187
	 */
188
	public function is_extension_route($item_url)
189
	{
190
		$parts = parse_url($item_url);
191
		return (empty($parts['host']) && strpos($parts['path'], '.') === false && !is_dir($item_url)) ? true : false;
192
	}
193
}
194