Completed
Push — master ( bf680a...c14dad )
by Morris
94:09 queued 79:58
created

functions.php ➔ html_select_options()   D

Complexity

Conditions 12
Paths 144

Size

Total Lines 27
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
eloc 19
nc 144
nop 3
dl 0
loc 27
rs 4.8484
c 0
b 0
f 0

How to fix   Complexity   

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
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Bart Visscher <[email protected]>
6
 * @author Bernhard Posselt <[email protected]>
7
 * @author Joas Schilling <[email protected]>
8
 * @author Julius Härtl <[email protected]>
9
 * @author Jörn Friedrich Dreyer <[email protected]>
10
 * @author Lukas Reschke <[email protected]>
11
 * @author Michael Letzgus <[email protected]>
12
 * @author Morris Jobke <[email protected]>
13
 * @author Robin McCorkell <[email protected]>
14
 * @author Roeland Jago Douma <[email protected]>
15
 * @author Thomas Müller <[email protected]>
16
 * @author Vincent Petry <[email protected]>
17
 *
18
 * @license AGPL-3.0
19
 *
20
 * This code is free software: you can redistribute it and/or modify
21
 * it under the terms of the GNU Affero General Public License, version 3,
22
 * as published by the Free Software Foundation.
23
 *
24
 * This program is distributed in the hope that it will be useful,
25
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27
 * GNU Affero General Public License for more details.
28
 *
29
 * You should have received a copy of the GNU Affero General Public License, version 3,
30
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
31
 *
32
 */
33
34
/**
35
 * Prints a sanitized string
36
 * @param string $string the string which will be escaped and printed
37
 */
38
function p($string) {
39
	print(\OCP\Util::sanitizeHTML($string));
40
}
41
42
43
/**
44
 * Prints a <link> tag for loading css
45
 * @param string $href the source URL, ignored when empty
46
 * @param string $opts, additional optional options
0 ignored issues
show
Bug introduced by
There is no parameter named $opts,. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
47
*/
48
function emit_css_tag($href, $opts = '') {
49
	$s='<link rel="stylesheet"';
50
	if (!empty($href)) {
51
		$s.=' href="' . $href .'"';
52
	}
53
	if (!empty($opts)) {
54
		$s.=' '.$opts;
55
	}
56
	print_unescaped($s.">\n");
57
}
58
59
/**
60
 * Prints all tags for CSS loading
61
 * @param array $obj all the script information from template
62
*/
63
function emit_css_loading_tags($obj) {
64
	foreach($obj['cssfiles'] as $css) {
65
		emit_css_tag($css);
66
	}
67
	foreach($obj['printcssfiles'] as $css) {
68
		emit_css_tag($css, 'media="print"');
69
	}
70
}
71
72
/**
73
 * Prints a <script> tag with nonce and defer depending on config
74
 * @param string $src the source URL, ignored when empty
75
 * @param string $script_content the inline script content, ignored when empty
76
*/
77
function emit_script_tag($src, $script_content='') {
78
	$defer_str=' defer';
79
	$s='<script nonce="' . \OC::$server->getContentSecurityPolicyNonceManager()->getNonce() . '"';
80
	if (!empty($src)) {
81
		 // emit script tag for deferred loading from $src
82
		$s.=$defer_str.' src="' . $src .'">';
83
	} else if (!empty($script_content)) {
84
		// emit script tag for inline script from $script_content without defer (see MDN)
85
		$s.=">\n".$script_content."\n";
86
	} else {
87
		// no $src nor $src_content, really useless empty tag
88
		$s.='>';
89
	}
90
	$s.='</script>';
91
	print_unescaped($s."\n");
92
}
93
94
/**
95
 * Print all <script> tags for loading JS
96
 * @param array $obj all the script information from template
97
*/
98
function emit_script_loading_tags($obj) {
99
	foreach($obj['jsfiles'] as $jsfile) {
100
		emit_script_tag($jsfile, '');
101
	}
102
	if (!empty($obj['inline_ocjs'])) {
103
		emit_script_tag('', $obj['inline_ocjs']);
104
	}
105
}
106
107
/**
108
 * Prints an unsanitized string - usage of this function may result into XSS.
109
 * Consider using p() instead.
110
 * @param string|array $string the string which will be printed as it is
111
 */
112
function print_unescaped($string) {
113
	print($string);
114
}
115
116
/**
117
 * Shortcut for adding scripts to a page
118
 * @param string $app the appname
119
 * @param string|string[] $file the filename,
120
 * if an array is given it will add all scripts
121
 */
122
function script($app, $file = null) {
123
	if(is_array($file)) {
124
		foreach($file as $f) {
125
			OC_Util::addScript($app, $f);
126
		}
127
	} else {
128
		OC_Util::addScript($app, $file);
129
	}
130
}
131
132
/**
133
 * Shortcut for adding vendor scripts to a page
134
 * @param string $app the appname
135
 * @param string|string[] $file the filename,
136
 * if an array is given it will add all scripts
137
 */
138
function vendor_script($app, $file = null) {
139
	if(is_array($file)) {
140
		foreach($file as $f) {
141
			OC_Util::addVendorScript($app, $f);
142
		}
143
	} else {
144
		OC_Util::addVendorScript($app, $file);
145
	}
146
}
147
148
/**
149
 * Shortcut for adding styles to a page
150
 * @param string $app the appname
151
 * @param string|string[] $file the filename,
152
 * if an array is given it will add all styles
153
 */
154
function style($app, $file = null) {
155
	if(is_array($file)) {
156
		foreach($file as $f) {
157
			OC_Util::addStyle($app, $f);
158
		}
159
	} else {
160
		OC_Util::addStyle($app, $file);
161
	}
162
}
163
164
/**
165
 * Shortcut for adding vendor styles to a page
166
 * @param string $app the appname
167
 * @param string|string[] $file the filename,
168
 * if an array is given it will add all styles
169
 */
170
function vendor_style($app, $file = null) {
171
	if(is_array($file)) {
172
		foreach($file as $f) {
173
			OC_Util::addVendorStyle($app, $f);
174
		}
175
	} else {
176
		OC_Util::addVendorStyle($app, $file);
177
	}
178
}
179
180
/**
181
 * Shortcut for adding translations to a page
182
 * @param string $app the appname
183
 * if an array is given it will add all styles
184
 */
185
function translation($app) {
186
	OC_Util::addTranslations($app);
187
}
188
189
/**
190
 * Shortcut for HTML imports
191
 * @param string $app the appname
192
 * @param string|string[] $file the path relative to the app's component folder,
193
 * if an array is given it will add all components
194
 */
195
function component($app, $file) {
196
	if(is_array($file)) {
197
		foreach($file as $f) {
198
			$url = link_to($app, 'component/' . $f . '.html');
199
			OC_Util::addHeader('link', array('rel' => 'import', 'href' => $url));
200
		}
201
	} else {
202
		$url = link_to($app, 'component/' . $file . '.html');
203
		OC_Util::addHeader('link', array('rel' => 'import', 'href' => $url));
204
	}
205
}
206
207
/**
208
 * make \OCP\IURLGenerator::linkTo available as a simple function
209
 * @param string $app app
210
 * @param string $file file
211
 * @param array $args array with param=>value, will be appended to the returned url
212
 * @return string link to the file
213
 *
214
 * For further information have a look at \OCP\IURLGenerator::linkTo
215
 */
216
function link_to( $app, $file, $args = array() ) {
217
	return \OC::$server->getURLGenerator()->linkTo($app, $file, $args);
218
}
219
220
/**
221
 * @param $key
222
 * @return string url to the online documentation
223
 */
224
function link_to_docs($key) {
225
	return \OC::$server->getURLGenerator()->linkToDocs($key);
226
}
227
228
/**
229
 * make \OCP\IURLGenerator::imagePath available as a simple function
230
 * @param string $app app
231
 * @param string $image image
232
 * @return string link to the image
233
 *
234
 * For further information have a look at \OCP\IURLGenerator::imagePath
235
 */
236
function image_path( $app, $image ) {
237
	return \OC::$server->getURLGenerator()->imagePath( $app, $image );
238
}
239
240
/**
241
 * make OC_Helper::mimetypeIcon available as a simple function
242
 * @param string $mimetype mimetype
243
 * @return string link to the image
244
 */
245
function mimetype_icon( $mimetype ) {
246
	return \OC::$server->getMimeTypeDetector()->mimeTypeIcon( $mimetype );
247
}
248
249
/**
250
 * make preview_icon available as a simple function
251
 * Returns the path to the preview of the image.
252
 * @param string $path path of file
253
 * @return string link to the preview
254
 */
255
function preview_icon( $path ) {
256
	return \OC::$server->getURLGenerator()->linkToRoute('core.Preview.getPreview', ['x' => 32, 'y' => 32, 'file' => $path]);
257
}
258
259
/**
260
 * @param string $path
261
 * @param string $token
262
 * @return string
263
 */
264
function publicPreview_icon ( $path, $token ) {
265
	return \OC::$server->getURLGenerator()->linkToRoute('files_sharing.PublicPreview.getPreview', ['x' => 32, 'y' => 32, 'file' => $path, 't' => $token]);
266
}
267
268
/**
269
 * make OC_Helper::humanFileSize available as a simple function
270
 * @param int $bytes size in bytes
271
 * @return string size as string
272
 *
273
 * For further information have a look at OC_Helper::humanFileSize
274
 */
275
function human_file_size( $bytes ) {
276
	return OC_Helper::humanFileSize( $bytes );
277
}
278
279
/**
280
 * Strips the timestamp of its time value
281
 * @param int $timestamp UNIX timestamp to strip
282
 * @return int timestamp without time value
283
 */
284
function strip_time($timestamp){
285
	$date = new \DateTime("@{$timestamp}");
286
	$date->setTime(0, 0, 0);
287
	return intval($date->format('U'));
288
}
289
290
/**
291
 * Formats timestamp relatively to the current time using
292
 * a human-friendly format like "x minutes ago" or "yesterday"
293
 * @param int $timestamp timestamp to format
294
 * @param int|null $fromTime timestamp to compare from, defaults to current time
295
 * @param bool|null $dateOnly whether to strip time information
296
 * @return string timestamp
297
 */
298
function relative_modified_date($timestamp, $fromTime = null, $dateOnly = false) {
299
	/** @var \OC\DateTimeFormatter $formatter */
300
	$formatter = \OC::$server->query('DateTimeFormatter');
301
302
	if ($dateOnly){
303
		return $formatter->formatDateSpan($timestamp, $fromTime);
304
	}
305
	return $formatter->formatTimeSpan($timestamp, $fromTime);
306
}
307
308
function html_select_options($options, $selected, $params=array()) {
309
	if (!is_array($selected)) {
310
		$selected=array($selected);
311
	}
312
	if (isset($params['combine']) && $params['combine']) {
313
		$options = array_combine($options, $options);
314
	}
315
	$value_name = $label_name = false;
316
	if (isset($params['value'])) {
317
		$value_name = $params['value'];
318
	}
319
	if (isset($params['label'])) {
320
		$label_name = $params['label'];
321
	}
322
	$html = '';
323
	foreach($options as $value => $label) {
324
		if ($value_name && is_array($label)) {
325
			$value = $label[$value_name];
326
		}
327
		if ($label_name && is_array($label)) {
328
			$label = $label[$label_name];
329
		}
330
		$select = in_array($value, $selected) ? ' selected="selected"' : '';
331
		$html .= '<option value="' . \OCP\Util::sanitizeHTML($value) . '"' . $select . '>' . \OCP\Util::sanitizeHTML($label) . '</option>'."\n";
332
	}
333
	return $html;
334
}
335