Completed
Push — v3.0 ( e28df5...5be1a9 )
by Samir
13:00
created

html_helper.php ➔ _list()   B

Complexity

Conditions 8
Paths 10

Size

Total Lines 60
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 27
nc 10
nop 4
dl 0
loc 60
rs 7.0677
c 0
b 0
f 0

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  if ( ! defined('BASEPATH')) {
2
	exit('No direct script access allowed');
3
}
4
/**
5
 * CodeIgniter
6
 *
7
 * An open source application development framework for PHP 5.1.6 or newer
8
 *
9
 * @package		CodeIgniter
10
 * @author		ExpressionEngine Dev Team
11
 * @copyright	Copyright (c) 2008 - 2011, EllisLab, Inc.
12
 * @license		http://codeigniter.com/user_guide/license.html
13
 * @link		http://codeigniter.com
14
 * @since		Version 1.0
15
 * @filesource
16
 */
17
18
// ------------------------------------------------------------------------
19
20
/**
21
 * CodeIgniter HTML Helpers
22
 *
23
 * @package		CodeIgniter
24
 * @subpackage	Helpers
25
 * @category	Helpers
26
 * @author		ExpressionEngine Dev Team
27
 * @link		http://codeigniter.com/user_guide/helpers/html_helper.html
28
 */
29
30
// ------------------------------------------------------------------------
31
32
/**
33
 * Heading
34
 *
35
 * Generates an HTML heading tag.  First param is the data.
36
 * Second param is the size of the heading tag.
37
 *
38
 * @access	public
39
 * @param	string
40
 * @param	integer
41
 * @return	string
42
 */
43
if ( ! function_exists('heading'))
44
{
45
	function heading($data = '', $h = '1', $attributes = '')
46
	{
47
		$attributes = ($attributes != '') ? ' '.$attributes : $attributes;
48
		return "<h".$h.$attributes.">".$data."</h".$h.">";
49
	}
50
}
51
52
// ------------------------------------------------------------------------
53
54
/**
55
 * Unordered List
56
 *
57
 * Generates an HTML unordered list from an single or multi-dimensional array.
58
 *
59
 * @access	public
60
 * @param	array
61
 * @param	mixed
62
 * @return	string
63
 */
64
if ( ! function_exists('ul'))
65
{
66
	function ul($list, $attributes = '')
67
	{
68
		return _list('ul', $list, $attributes);
69
	}
70
}
71
72
// ------------------------------------------------------------------------
73
74
/**
75
 * Ordered List
76
 *
77
 * Generates an HTML ordered list from an single or multi-dimensional array.
78
 *
79
 * @access	public
80
 * @param	array
81
 * @param	mixed
82
 * @return	string
83
 */
84
if ( ! function_exists('ol'))
85
{
86
	function ol($list, $attributes = '')
87
	{
88
		return _list('ol', $list, $attributes);
89
	}
90
}
91
92
// ------------------------------------------------------------------------
93
94
/**
95
 * Generates the list
96
 *
97
 * Generates an HTML ordered list from an single or multi-dimensional array.
98
 *
99
 * @access	private
100
 * @param	string
101
 * @param	mixed
102
 * @param	mixed
103
 * @param	integer
104
 * @return	string
105
 */
106
if ( ! function_exists('_list'))
107
{
108
	function _list($type = 'ul', $list, $attributes = '', $depth = 0)
109
	{
110
		// If an array wasn't submitted there's nothing to do...
111
		if ( ! is_array($list))
112
		{
113
			return $list;
114
		}
115
116
		// Set the indentation based on the depth
117
		$out = str_repeat(" ", $depth);
118
119
		// Were any attributes submitted?  If so generate a string
120
		if (is_array($attributes))
121
		{
122
			$atts = '';
123
			foreach ($attributes as $key => $val)
124
			{
125
				$atts .= ' '.$key.'="'.$val.'"';
126
			}
127
			$attributes = $atts;
128
		} elseif (is_string($attributes) AND strlen($attributes) > 0)
129
		{
130
			$attributes = ' '.$attributes;
131
		}
132
133
		// Write the opening list tag
134
		$out .= "<".$type.$attributes.">\n";
135
136
		// Cycle through the list elements.  If an array is
137
		// encountered we will recursively call _list()
138
139
		static $_last_list_item = '';
140
		foreach ($list as $key => $val)
141
		{
142
			$_last_list_item = $key;
143
144
			$out .= str_repeat(" ", $depth + 2);
145
			$out .= "<li>";
146
147
			if ( ! is_array($val))
148
			{
149
				$out .= $val;
150
			} else
151
			{
152
				$out .= $_last_list_item."\n";
153
				$out .= _list($type, $val, '', $depth + 4);
154
				$out .= str_repeat(" ", $depth + 2);
155
			}
156
157
			$out .= "</li>\n";
158
		}
159
160
		// Set the indentation for the closing tag
161
		$out .= str_repeat(" ", $depth);
162
163
		// Write the closing list tag
164
		$out .= "</".$type.">\n";
165
166
		return $out;
167
	}
168
}
169
170
// ------------------------------------------------------------------------
171
172
/**
173
 * Generates HTML BR tags based on number supplied
174
 *
175
 * @access	public
176
 * @param	integer
177
 * @return	string
178
 */
179
if ( ! function_exists('br'))
180
{
181
	function br($num = 1)
182
	{
183
		return str_repeat("<br />", $num);
184
	}
185
}
186
187
// ------------------------------------------------------------------------
188
189
/**
190
 * Image
191
 *
192
 * Generates an <img /> element
193
 *
194
 * @access	public
195
 * @param	mixed
196
 * @return	string
197
 */
198
if ( ! function_exists('img'))
199
{
200
	function img($src = '', $index_page = FALSE)
201
	{
202
		if ( ! is_array($src))
203
		{
204
			$src = array('src' => $src);
205
		}
206
207
		// If there is no alt attribute defined, set it to an empty string
208
		if ( ! isset($src['alt']))
209
		{
210
			$src['alt'] = '';
211
		}
212
213
		$img = '<img';
214
215 View Code Duplication
		foreach ($src as $k=>$v)
216
		{
217
218
			if ($k == 'src' AND strpos($v, '://') === FALSE)
219
			{
220
				$CI = & get_instance();
221
222
				if ($index_page === TRUE)
223
				{
224
					$img .= ' src="'.$CI->config->site_url($v).'"';
0 ignored issues
show
Bug introduced by
The property config does not seem to exist in CI_Controller.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
225
				} else
226
				{
227
					$img .= ' src="'.$CI->config->slash_item('base_url').$v.'"';
228
				}
229
			} else
230
			{
231
				$img .= " $k=\"$v\"";
232
			}
233
		}
234
235
		$img .= '/>';
236
237
		return $img;
238
	}
239
}
240
241
// ------------------------------------------------------------------------
242
243
/**
244
 * Doctype
245
 *
246
 * Generates a page document type declaration
247
 *
248
 * Valid options are xhtml-11, xhtml-strict, xhtml-trans, xhtml-frame,
249
 * html4-strict, html4-trans, and html4-frame.  Values are saved in the
250
 * doctypes config file.
251
 *
252
 * @access	public
253
 * @param	string	type	The doctype to be generated
254
 * @return	string
255
 */
256
if ( ! function_exists('doctype'))
257
{
258
	function doctype($type = 'xhtml1-strict')
259
	{
260
		global $_doctypes;
261
262 View Code Duplication
		if ( ! is_array($_doctypes))
263
		{
264
			if (defined('ENVIRONMENT') AND is_file(APPPATH.'config/'.ENVIRONMENT.'/doctypes.php'))
265
			{
266
				include(APPPATH.'config/'.ENVIRONMENT.'/doctypes.php');
267
			} elseif (is_file(APPPATH.'config/doctypes.php'))
268
			{
269
				include(APPPATH.'config/doctypes.php');
270
			}
271
272
			if ( ! is_array($_doctypes))
273
			{
274
				return FALSE;
275
			}
276
		}
277
278
		if (isset($_doctypes[$type]))
279
		{
280
			return $_doctypes[$type];
281
		} else
282
		{
283
			return FALSE;
284
		}
285
	}
286
}
287
288
// ------------------------------------------------------------------------
289
290
/**
291
 * Link
292
 *
293
 * Generates link to a CSS file
294
 *
295
 * @access	public
296
 * @param	mixed	stylesheet hrefs or an array
297
 * @param	string	rel
298
 * @param	string	type
299
 * @param	string	title
300
 * @param	string	media
301
 * @param	boolean	should index_page be added to the css path
302
 * @return	string
303
 */
304
if ( ! function_exists('link_tag'))
305
{
306
	function link_tag($href = '', $rel = 'stylesheet', $type = 'text/css', $title = '', $media = '', $index_page = FALSE)
307
	{
308
		$CI = & get_instance();
309
310
		$link = '<link ';
311
312
		if (is_array($href))
313
		{
314 View Code Duplication
			foreach ($href as $k=>$v)
315
			{
316
				if ($k == 'href' AND strpos($v, '://') === FALSE)
317
				{
318
					if ($index_page === TRUE)
319
					{
320
						$link .= 'href="'.$CI->config->site_url($v).'" ';
0 ignored issues
show
Bug introduced by
The property config does not seem to exist in CI_Controller.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
321
					}
322
					else
323
					{
324
						$link .= 'href="'.$CI->config->slash_item('base_url').$v.'" ';
325
					}
326
				}
327
				else
328
				{
329
					$link .= "$k=\"$v\" ";
330
				}
331
			}
332
333
			$link .= "/>";
334
		}
335
		else
336
		{
337
			if (strpos($href, '://') !== FALSE)
338
			{
339
				$link .= 'href="'.$href.'" ';
340
			}
341
			elseif ($index_page === TRUE)
342
			{
343
				$link .= 'href="'.$CI->config->site_url($href).'" ';
344
			}
345
			else
346
			{
347
				$link .= 'href="'.$CI->config->slash_item('base_url').$href.'" ';
348
			}
349
350
			$link .= 'rel="'.$rel.'" type="'.$type.'" ';
351
352
			if ($media != '')
353
			{
354
				$link .= 'media="'.$media.'" ';
355
			}
356
357
			if ($title != '')
358
			{
359
				$link .= 'title="'.$title.'" ';
360
			}
361
362
			$link .= '/>';
363
		}
364
365
366
		return $link;
367
	}
368
}
369
370
// ------------------------------------------------------------------------
371
372
/**
373
 * Generates meta tags from an array of key/values
374
 *
375
 * @access	public
376
 * @param	array
377
 * @return	string
378
 */
379
if ( ! function_exists('meta'))
380
{
381
	function meta($name = '', $content = '', $type = 'name', $newline = "\n")
382
	{
383
		// Since we allow the data to be passes as a string, a simple array
384
		// or a multidimensional one, we need to do a little prepping.
385
		if ( ! is_array($name))
386
		{
387
			$name = array(array('name' => $name, 'content' => $content, 'type' => $type, 'newline' => $newline));
388
		} else
389
		{
390
			// Turn single array into multidimensional
391
			if (isset($name['name']))
392
			{
393
				$name = array($name);
394
			}
395
		}
396
397
		$str = '';
398
		foreach ($name as $meta)
399
		{
400
			$type		= ( ! isset($meta['type']) OR $meta['type'] == 'name') ? 'name' : 'http-equiv';
401
			$name		= ( ! isset($meta['name'])) ? '' : $meta['name'];
402
			$content	= ( ! isset($meta['content'])) ? '' : $meta['content'];
403
			$newline	= ( ! isset($meta['newline'])) ? "\n" : $meta['newline'];
404
405
			$str .= '<meta '.$type.'="'.$name.'" content="'.$content.'" />'.$newline;
406
		}
407
408
		return $str;
409
	}
410
}
411
412
// ------------------------------------------------------------------------
413
414
/**
415
 * Generates non-breaking space entities based on number supplied
416
 *
417
 * @access	public
418
 * @param	integer
419
 * @return	string
420
 */
421
if ( ! function_exists('nbs'))
422
{
423
	function nbs($num = 1)
424
	{
425
		return str_repeat("&nbsp;", $num);
426
	}
427
}
428
429
430
/* End of file html_helper.php */
431
/* Location: ./system/helpers/html_helper.php */