summaryrefslogtreecommitdiff
path: root/include/controls.php
blob: 037e331cc86b5aa9e7cf01d743e03cfd91dfc877 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
<?php

function print_select($id, $default, $values, $attributes = "", $name = "") {
	if (!$name) $name = $id;

	print "<select name=\"$name\" id=\"$id\" $attributes>";
	foreach ($values as $v) {
		if ($v == $default)
			$sel = "selected=\"1\"";
		else
			$sel = "";

		$v = trim($v);

		print "<option value=\"$v\" $sel>$v</option>";
	}
	print "</select>";
}

function print_select_hash($id, $default, $values, $attributes = "", $name = "") {
	if (!$name) $name = $id;

	print "<select name=\"$name\" id='$id' $attributes>";
	foreach (array_keys($values) as $v) {
		if ($v == $default)
			$sel = 'selected="selected"';
		else
			$sel = "";

		$v = trim($v);

		print "<option $sel value=\"$v\">".$values[$v]."</option>";
	}

	print "</select>";
}

function print_hidden($name, $value) {
	print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"$name\" value=\"$value\">";
}

function print_checkbox($id, $checked, $value = "", $attributes = "") {
	$checked_str = $checked ? "checked" : "";
	$value_str = $value ? "value=\"$value\"" : "";

	print "<input dojoType=\"dijit.form.CheckBox\" id=\"$id\" $value_str $checked_str $attributes name=\"$id\">";
}

function print_button($type, $value, $attributes = "") {
	print "<p><button dojoType=\"dijit.form.Button\" $attributes type=\"$type\">$value</button>";
}

function print_radio($id, $default, $true_is, $values, $attributes = "") {
	foreach ($values as $v) {

		if ($v == $default)
			$sel = "checked";
		else
			$sel = "";

		if ($v == $true_is) {
			$sel .= " value=\"1\"";
		} else {
			$sel .= " value=\"0\"";
		}

		print "<input class=\"noborder\" dojoType=\"dijit.form.RadioButton\"
				type=\"radio\" $sel $attributes name=\"$id\">&nbsp;$v&nbsp;";

	}
}

function print_feed_multi_select($id, $default_ids = [],
						   $attributes = "", $include_all_feeds = true,
						   $root_id = null, $nest_level = 0) {

	$pdo = DB::pdo();

	print_r(in_array("CAT:6",$default_ids));

	if (!$root_id) {
		print "<select multiple=\true\" id=\"$id\" name=\"$id\" $attributes>";
		if ($include_all_feeds) {
			$is_selected = (in_array("0", $default_ids)) ? "selected=\"1\"" : "";
			print "<option $is_selected value=\"0\">".__('All feeds')."</option>";
		}
	}

	if (get_pref('ENABLE_FEED_CATS')) {

		if (!$root_id) $root_id = null;

		$sth = $pdo->prepare("SELECT id,title,
				(SELECT COUNT(id) FROM ttrss_feed_categories AS c2 WHERE
					c2.parent_cat = ttrss_feed_categories.id) AS num_children
				FROM ttrss_feed_categories
				WHERE owner_uid = :uid AND
				(parent_cat = :root_id OR (:root_id IS NULL AND parent_cat IS NULL)) ORDER BY title");

		$sth->execute([":uid" => $_SESSION['uid'], ":root_id" => $root_id]);

		while ($line = $sth->fetch()) {

			for ($i = 0; $i < $nest_level; $i++)
				$line["title"] = " - " . $line["title"];

			$is_selected = in_array("CAT:".$line["id"], $default_ids) ? "selected=\"1\"" : "";

			printf("<option $is_selected value='CAT:%d'>%s</option>",
				$line["id"], htmlspecialchars($line["title"]));

			if ($line["num_children"] > 0)
				print_feed_multi_select($id, $default_ids, $attributes,
					$include_all_feeds, $line["id"], $nest_level+1);

			$f_sth = $pdo->prepare("SELECT id,title FROM ttrss_feeds
					WHERE cat_id = ? AND owner_uid = ? ORDER BY title");

			$f_sth->execute([$line['id'], $_SESSION['uid']]);

			while ($fline = $f_sth->fetch()) {
				$is_selected = (in_array($fline["id"], $default_ids)) ? "selected=\"1\"" : "";

				$fline["title"] = " + " . $fline["title"];

				for ($i = 0; $i < $nest_level; $i++)
					$fline["title"] = " - " . $fline["title"];

				printf("<option $is_selected value='%d'>%s</option>",
					$fline["id"], htmlspecialchars($fline["title"]));
			}
		}

		if (!$root_id) {
			$is_selected = in_array("CAT:0", $default_ids) ? "selected=\"1\"" : "";

			printf("<option $is_selected value='CAT:0'>%s</option>",
				__("Uncategorized"));

			$f_sth = $pdo->prepare("SELECT id,title FROM ttrss_feeds
					WHERE cat_id IS NULL AND owner_uid = ? ORDER BY title");
			$f_sth->execute([$_SESSION['uid']]);

			while ($fline = $f_sth->fetch()) {
				$is_selected = in_array($fline["id"], $default_ids) ? "selected=\"1\"" : "";

				$fline["title"] = " + " . $fline["title"];

				for ($i = 0; $i < $nest_level; $i++)
					$fline["title"] = " - " . $fline["title"];

				printf("<option $is_selected value='%d'>%s</option>",
					$fline["id"], htmlspecialchars($fline["title"]));
			}
		}

	} else {
		$sth = $pdo->prepare("SELECT id,title FROM ttrss_feeds
				WHERE owner_uid = ? ORDER BY title");
		$sth->execute([$_SESSION['uid']]);

		while ($line = $sth->fetch()) {

			$is_selected = (in_array($line["id"], $default_ids)) ? "selected=\"1\"" : "";

			printf("<option $is_selected value='%d'>%s</option>",
				$line["id"], htmlspecialchars($line["title"]));
		}
	}

	if (!$root_id) {
		print "</select>";
	}
}

function print_feed_cat_select($id, $default_id,
							   $attributes, $include_all_cats = true, $root_id = null, $nest_level = 0) {

	if (!$root_id) {
		print "<select id=\"$id\" name=\"$id\" default=\"$default_id\" $attributes>";
	}

	$pdo = DB::pdo();

	if (!$root_id) $root_id = null;

	$sth = $pdo->prepare("SELECT id,title,
				(SELECT COUNT(id) FROM ttrss_feed_categories AS c2 WHERE
					c2.parent_cat = ttrss_feed_categories.id) AS num_children
				FROM ttrss_feed_categories
				WHERE owner_uid = :uid AND
				  (parent_cat = :root_id OR (:root_id IS NULL AND parent_cat IS NULL)) ORDER BY title");
	$sth->execute([":uid" => $_SESSION['uid'], ":root_id" => $root_id]);

	$found = 0;

	while ($line = $sth->fetch()) {
		++$found;

		if ($line["id"] == $default_id) {
			$is_selected = "selected=\"1\"";
		} else {
			$is_selected = "";
		}

		for ($i = 0; $i < $nest_level; $i++)
			$line["title"] = " - " . $line["title"];

		if ($line["title"])
			printf("<option $is_selected value='%d'>%s</option>",
				$line["id"], htmlspecialchars($line["title"]));

		if ($line["num_children"] > 0)
			print_feed_cat_select($id, $default_id, $attributes,
				$include_all_cats, $line["id"], $nest_level+1);
	}

	if (!$root_id) {
		if ($include_all_cats) {
			if ($found > 0) {
				print "<option disabled=\"1\">--------</option>";
			}

			if ($default_id == 0) {
				$is_selected = "selected=\"1\"";
			} else {
				$is_selected = "";
			}

			print "<option $is_selected value=\"0\">".__('Uncategorized')."</option>";
		}
		print "</select>";
	}
}

function stylesheet_tag($filename) {
	$timestamp = filemtime($filename);

	return "<link rel=\"stylesheet\" type=\"text/css\" href=\"$filename?$timestamp\"/>\n";
}

function javascript_tag($filename) {
	$query = "";

	if (!(strpos($filename, "?") === FALSE)) {
		$query = substr($filename, strpos($filename, "?")+1);
		$filename = substr($filename, 0, strpos($filename, "?"));
	}

	$timestamp = filemtime($filename);

	if ($query) $timestamp .= "&$query";

	return "<script type=\"text/javascript\" charset=\"utf-8\" src=\"$filename?$timestamp\"></script>\n";
}

function format_warning($msg, $id = "") {
	return "<div class=\"alert\" id=\"$id\">$msg</div>";
}

function format_notice($msg, $id = "") {
	return "<div class=\"alert alert-info\" id=\"$id\">$msg</div>";
}

function format_error($msg, $id = "") {
	return "<div class=\"alert alert-danger\" id=\"$id\">$msg</div>";
}

function print_notice($msg) {
	return print format_notice($msg);
}

function print_warning($msg) {
	return print format_warning($msg);
}

function print_error($msg) {
	return print format_error($msg);
}

function format_inline_player($url, $ctype) {

	$entry = "";

	$url = htmlspecialchars($url);

	if (strpos($ctype, "audio/") === 0) {

		if ($_SESSION["hasAudio"] && (strpos($ctype, "ogg") !== false ||
				$_SESSION["hasMp3"])) {

			$entry .= "<audio preload=\"none\" controls>
					<source type=\"$ctype\" src=\"$url\"/>
					</audio>";

		}

		if ($entry) $entry .= "&nbsp; <a target=\"_blank\" rel=\"noopener noreferrer\"
				href=\"$url\">" . basename($url) . "</a>";

		return $entry;

	}

	return "";
}

function print_label_select($name, $value, $attributes = "") {

	$pdo = Db::pdo();

	$sth = $pdo->prepare("SELECT caption FROM ttrss_labels2
			WHERE owner_uid = ? ORDER BY caption");
	$sth->execute([$_SESSION['uid']]);

	print "<select default=\"$value\" name=\"" . htmlspecialchars($name) .
		"\" $attributes>";

	while ($line = $sth->fetch()) {

		$issel = ($line["caption"] == $value) ? "selected=\"1\"" : "";

		print "<option value=\"".htmlspecialchars($line["caption"])."\"
				$issel>" . htmlspecialchars($line["caption"]) . "</option>";

	}

#		print "<option value=\"ADD_LABEL\">" .__("Add label...") . "</option>";

	print "</select>";


}