Completed
Push — master ( a615bf...3f14d2 )
by
unknown
14:10
created

functions.php ➔ html_select_options()   C

Complexity

Conditions 12
Paths 144

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
nc 144
nop 3
dl 0
loc 27
rs 6.6
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
 * @author Bart Visscher <[email protected]>
4
 * @author Bernhard Posselt <[email protected]>
5
 * @author Joas Schilling <[email protected]>
6
 * @author Jörn Friedrich Dreyer <[email protected]>
7
 * @author Lukas Reschke <[email protected]>
8
 * @author Morris Jobke <[email protected]>
9
 * @author Robin McCorkell <[email protected]>
10
 * @author Roeland Jago Douma <[email protected]>
11
 * @author Thomas Müller <[email protected]>
12
 * @author Vincent Petry <[email protected]>
13
 *
14
 * @copyright Copyright (c) 2018, ownCloud GmbH
15
 * @license AGPL-3.0
16
 *
17
 * This code is free software: you can redistribute it and/or modify
18
 * it under the terms of the GNU Affero General Public License, version 3,
19
 * as published by the Free Software Foundation.
20
 *
21
 * This program is distributed in the hope that it will be useful,
22
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24
 * GNU Affero General Public License for more details.
25
 *
26
 * You should have received a copy of the GNU Affero General Public License, version 3,
27
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
28
 *
29
 */
30
31
/**
32
 * Prints a sanitized string
33
 * @param string $string the string which will be escaped and printed
34
 */
35
function p($string) {
36
	print(\OCP\Util::sanitizeHTML($string));
37
}
38
39
/**
40
 * Prints an unsanitized string - usage of this function may result into XSS.
41
 * Consider using p() instead.
42
 * @param string|array $string the string which will be printed as it is
43
 */
44
function print_unescaped($string) {
45
	print($string);
46
}
47
48
/**
49
 * Shortcut for adding scripts to a page
50
 * @param string $app the appname
51
 * @param string|string[] $file the filename,
52
 * if an array is given it will add all scripts
53
 */
54
function script($app, $file = null) {
55
	if (\is_array($file)) {
56
		foreach ($file as $f) {
57
			OC_Util::addScript($app, $f);
58
		}
59
	} else {
60
		OC_Util::addScript($app, $file);
61
	}
62
}
63
64
/**
65
 * Shortcut for adding vendor scripts to a page
66
 * @param string $app the appname
67
 * @param string|string[] $file the filename,
68
 * if an array is given it will add all scripts
69
 */
70
function vendor_script($app, $file = null) {
71
	if (\is_array($file)) {
72
		foreach ($file as $f) {
73
			OC_Util::addVendorScript($app, $f);
74
		}
75
	} else {
76
		OC_Util::addVendorScript($app, $file);
77
	}
78
}
79
80
/**
81
 * Shortcut for adding styles to a page
82
 * @param string $app the appname
83
 * @param string|string[] $file the filename,
84
 * if an array is given it will add all styles
85
 */
86
function style($app, $file = null) {
87
	if (\is_array($file)) {
88
		foreach ($file as $f) {
89
			OC_Util::addStyle($app, $f);
90
		}
91
	} else {
92
		OC_Util::addStyle($app, $file);
93
	}
94
}
95
96
/**
97
 * Shortcut for adding vendor styles to a page
98
 * @param string $app the appname
99
 * @param string|string[] $file the filename,
100
 * if an array is given it will add all styles
101
 */
102
function vendor_style($app, $file = null) {
103
	if (\is_array($file)) {
104
		foreach ($file as $f) {
105
			OC_Util::addVendorStyle($app, $f);
106
		}
107
	} else {
108
		OC_Util::addVendorStyle($app, $file);
109
	}
110
}
111
112
/**
113
 * Shortcut for adding translations to a page
114
 * @param string $app the appname
115
 * if an array is given it will add all styles
116
 */
117
function translation($app) {
118
	OC_Util::addTranslations($app);
119
}
120
121
/**
122
 * Shortcut for HTML imports
123
 * @param string $app the appname
124
 * @param string|string[] $file the path relative to the app's component folder,
125
 * if an array is given it will add all components
126
 */
127
function component($app, $file) {
128
	if (\is_array($file)) {
129
		foreach ($file as $f) {
130
			$url = link_to($app, 'component/' . $f . '.html');
131
			OC_Util::addHeader('link', ['rel' => 'import', 'href' => $url]);
132
		}
133
	} else {
134
		$url = link_to($app, 'component/' . $file . '.html');
135
		OC_Util::addHeader('link', ['rel' => 'import', 'href' => $url]);
136
	}
137
}
138
139
/**
140
 * make \OCP\IURLGenerator::linkTo available as a simple function
141
 * @param string $app app
142
 * @param string $file file
143
 * @param array $args array with param=>value, will be appended to the returned url
144
 * @return string link to the file
145
 *
146
 * For further information have a look at \OCP\IURLGenerator::linkTo
147
 */
148
function link_to($app, $file, $args = []) {
149
	return \OC::$server->getURLGenerator()->linkTo($app, $file, $args);
150
}
151
152
/**
153
 * @param $key
154
 * @return string url to the online documentation
155
 */
156
function link_to_docs($key) {
157
	return \OC::$server->getURLGenerator()->linkToDocs($key);
158
}
159
160
/**
161
 * make \OCP\IURLGenerator::imagePath available as a simple function
162
 * @param string $app app
163
 * @param string $image image
164
 * @return string link to the image
165
 * @throws \RuntimeException
166
 *
167
 * For further information have a look at \OCP\IURLGenerator::imagePath
168
 */
169
function image_path($app, $image) {
170
	return \OC::$server->getURLGenerator()->imagePath($app, $image);
171
}
172
173
/**
174
 * make OC_Helper::mimetypeIcon available as a simple function
175
 * @param string $mimetype mimetype
176
 * @return string link to the image
177
 */
178
function mimetype_icon($mimetype) {
179
	return \OC::$server->getMimeTypeDetector()->mimeTypeIcon($mimetype);
180
}
181
182
/**
183
 * make preview_icon available as a simple function
184
 * Returns the path to the preview of the image.
185
 * @param string $path path of file
186
 * @return string link to the preview
187
 */
188
function preview_icon($path) {
189
	return \OC::$server->getURLGenerator()->linkToRoute('core_ajax_preview', ['x' => 32, 'y' => 32, 'file' => $path]);
190
}
191
192
/**
193
 * @param string $path
194
 * @param string $token
195
 * @return string link to the public preview
196
 */
197
function publicPreview_icon($path, $token) {
198
	return \OC::$server->getURLGenerator()->linkToRoute('core_ajax_public_preview', ['x' => 32, 'y' => 32, 'file' => $path, 't' => $token]);
199
}
200
201
/**
202
 * make OC_Helper::humanFileSize available as a simple function
203
 * @param int $bytes size in bytes
204
 * @return string size as string
205
 *
206
 * For further information have a look at OC_Helper::humanFileSize
207
 */
208
function human_file_size($bytes) {
209
	return OC_Helper::humanFileSize($bytes);
210
}
211
212
/**
213
 * Strips the timestamp of its time value
214
 * @param int $timestamp UNIX timestamp to strip
215
 * @return $timestamp without time value
0 ignored issues
show
Documentation introduced by
The doc-type $timestamp could not be parsed: Unknown type name "$timestamp" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
216
 */
217
function strip_time($timestamp) {
218
	$date = new \DateTime("@{$timestamp}");
219
	$date->setTime(0, 0, 0);
220
	return \intval($date->format('U'));
221
}
222
223
/**
224
 * Formats timestamp relatively to the current time using
225
 * a human-friendly format like "x minutes ago" or "yesterday"
226
 * @param int $timestamp timestamp to format
227
 * @param int $fromTime timestamp to compare from, defaults to current time
228
 * @param bool $dateOnly whether to strip time information
229
 * @return string timestamp
230
 */
231
function relative_modified_date($timestamp, $fromTime = null, $dateOnly = false) {
232
	/** @var \OC\DateTimeFormatter $formatter */
233
	$formatter = \OC::$server->query('DateTimeFormatter');
234
235
	if ($dateOnly) {
236
		return $formatter->formatDateSpan($timestamp, $fromTime);
237
	}
238
	return $formatter->formatTimeSpan($timestamp, $fromTime);
239
}
240
241
function html_select_options($options, $selected, $params= []) {
242
	if (!\is_array($selected)) {
243
		$selected= [$selected];
244
	}
245
	if (isset($params['combine']) && $params['combine']) {
246
		$options = \array_combine($options, $options);
247
	}
248
	$value_name = $label_name = false;
249
	if (isset($params['value'])) {
250
		$value_name = $params['value'];
251
	}
252
	if (isset($params['label'])) {
253
		$label_name = $params['label'];
254
	}
255
	$html = '';
256
	foreach ($options as $value => $label) {
257
		if ($value_name && \is_array($label)) {
258
			$value = $label[$value_name];
259
		}
260
		if ($label_name && \is_array($label)) {
261
			$label = $label[$label_name];
262
		}
263
		$select = \in_array($value, $selected) ? ' selected="selected"' : '';
264
		$html .= '<option value="' . \OCP\Util::sanitizeHTML($value) . '"' . $select . '>' . \OCP\Util::sanitizeHTML($label) . '</option>'."\n";
265
	}
266
	return $html;
267
}
268