1<?php
2// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project
3//
4// All Rights Reserved. See copyright.txt for details and a complete list of authors.
5// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
6// $Id$
7
8function wikiplugin_div_info()
9{
10	global $prefs;
11
12	$info = [
13		'name' => tra('Div'),
14		'documentation' => 'PluginDiv',
15		'description' => tra('Define and format sections of a page or text'),
16		'prefs' => ['wikiplugin_div'],
17		'body' => tra('text'),
18		'iconname' => 'code',
19		'filter' => 'wikicontent',
20		'tags' => [ 'basic' ],
21		'validate' => 'arguments',
22		'introduced' => 1,
23		'params' => [
24			'type' => [
25				'required' => false,
26				'name' => tra('Type'),
27				'description' => tr('Indicate the type of HTML tag to use (default is %0)', '<code>div</code>'),
28				'since' => '1',
29				'filter' => 'text',
30				'safe' => true,
31				'default' => 'div',
32				'options' => [
33					['text' => tra('None'), 'value' => ''],
34					['text' => tra('Div'), 'value' => 'div'],
35					['text' => tra('Span'), 'value' => 'span'],
36					['text' => tra('Pre'), 'value' => 'pre'],
37					['text' => tra('Bold'), 'value' => 'b'],
38					['text' => tra('Italic'), 'value' => 'i'],
39					['text' => tra('Teletype'), 'value' => 'tt'],
40					['text' => tra('Paragraph'), 'value' => 'p'],
41					['text' => tra('Block quote'), 'value' => 'blockquote'],
42					['text' => tra('H1'), 'value' => 'h1'],
43					['text' => tra('H2'), 'value' => 'h2'],
44					['text' => tra('H3'), 'value' => 'h3'],
45					['text' => tra('H4'), 'value' => 'h4'],
46					['text' => tra('H5'), 'value' => 'h5'],
47					['text' => tra('H6'), 'value' => 'h6'],
48				],
49			],
50			'bg' => [
51				'required' => false,
52				'name' => tra('Background Color'),
53				'description' => tra('As defined by CSS, name, or color hex code.'),
54				'since' => '1',
55				'filter' => 'text',
56				'accepted' => tra('Valid CSS color name or hex code'),
57				'safe' => true,
58				'default' => '',
59			],
60			'width' => [
61				'required' => false,
62				'name' => tra('Box width'),
63				'description' => tra('In pixels or percentage. Default is original size'),
64				'since' => '1',
65				'default' => '',
66				'filter' => 'text',
67				'safe' => true,
68			],
69			'align' => [
70				'required' => false,
71				'name' => tra('Text Alignment'),
72				'description' => tra('Aligns the text within the element'),
73				'since' => '1',
74				'filter' => 'alpha',
75				'safe' => true,
76				'default' => '',
77				'options' => [
78					['text' => '', 'value' => ''],
79					['text' => tra('Left'), 'value' => 'left'],
80					['text' => tra('Right'), 'value' => 'right'],
81					['text' => tra('Center'), 'value' => 'center'],
82					['text' => tra('Justify'), 'value' => 'justify'],
83				],
84			],
85			'float' => [
86				'required' => false,
87				'name' => tra('Float Position'),
88				'description' => tr('Set the alignment for the entire element. For elements with a width of less than
89				100%, other elements will wrap around it unless the %0 parameter is appropriately set.', '<code>clear</code>'),
90				'since' => '1',
91				'filter' => 'alpha',
92				'safe' => true,
93				'default' => '',
94				'options' => [
95					['text' => '', 'value' => ''],
96					['text' => tra('Right'), 'value' => 'right'],
97					['text' => tra('Left'), 'value' => 'left'],
98					['text' => tra('None'), 'value' => 'none'],
99				],
100			],
101			'clear' => [
102				'required' => false,
103				'name' => tra('Clear'),
104				'description' => tra('Content cannot wrap around this object because of what the parameter is set to.'),
105				'since' => '1',
106				'filter' => 'text',
107				'safe' => true,
108				'default' => '',
109				'options' => [
110					['text' => '', 'value' => ''],
111					['text' => tra('Right'), 'value' => 'right'],
112					['text' => tra('Left'), 'value' => 'left'],
113					['text' => tra('Both'), 'value' => 'both'],
114					['text' => tra('None'), 'value' => 'none'],
115				],
116			],
117			'class' => [
118				'required' => false,
119				'name' => tra('CSS Class'),
120				'description' => tra('Apply custom CSS class to the div.'),
121				'since' => '1',
122				'filter' => 'text',
123				'safe' => true,
124				'default' => '',
125			],
126			'id' => [
127				'required' => false,
128				'name' => tra('HTML ID'),
129				'description' => tra("Sets the id attribute of the div, as defined by HTML."),
130				'since' => '1',
131				'filter' => 'text',
132				'safe' => true,
133				'default' => '',
134			],
135			'title' => [
136				'required' => false,
137				'name' => tra('Title attribute'),
138				'description' => tra('Title for the div, usually displayed as a tooltip.'),
139				'since' => '9.2',
140				'filter' => 'text',
141				'safe' => true,
142				'default' => '',
143			],
144			'onclick' => [
145				'required' => false,
146				'name' => tra('onClick attribute'),
147				'description' => tra('Enter on onclick event'),
148				'filter' => 'text',
149				'advanced' => true,
150				'since' => '14.0',
151				'default' => '',
152			],
153			'style' => [
154				// Note that this is ignored unless preference wiki_plugindiv_approvable is set in
155				// Configuration → Configuration Panels → Editing and Plugins → Miscellaneous
156				'required' => false,
157				'name' => tra('Style attribute'),
158				'description' => tra('Enter CSS styling tags for the div type used.'),
159				'since' => '13.0',
160				'filter' => 'text',
161				'advanced' => true,
162				'default' => '',
163			],
164			'data' => [
165				'required' => false,
166				'name' => tra('Data attributes'),
167				'description' => tra('URL encoded list or attributes and values.'),
168				'filter' => 'text',
169				'safe' => true,
170				'advanced' => true,
171				'since' => '19.0',
172				'default' => '',
173			],
174			'aria' => [
175				'required' => false,
176				'name' => tra('ARIA attributes'),
177				'description' => tra('URL encoded list or attributes and values, e.g. label, expanded etc.'),
178				'filter' => 'text',
179				'safe' => true,
180				'advanced' => true,
181				'since' => '19.0',
182				'default' => '',
183			],
184			'role' => [
185				'required' => false,
186				'name' => tra('Role attribute'),
187				'description' => tra('e.g. button, heading, search etc.'),
188				'since' => '19.0',
189				'filter' => 'text',
190				'safe' => true,
191				'default' => '',
192			],
193		],
194	];
195
196	return $info;
197}
198
199function wikiplugin_div($content, $params)
200{
201	$style = '';
202	extract($params);
203
204	$possibletypes = ['div','span','pre','b','i','tt','p','blockquote', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6'];
205	$t    = (isset($type) and in_array($type, $possibletypes)) ? "$type" : "div";
206	$c    = (isset($class)) ? " class='$class'" : "";
207	$id   = (isset($id)) ? " id='$id'" : "";
208	$oc   = (isset($onclick)) ? " onclick='$onclick'" : "";
209	$w    = (isset($width)) ? " width: $width;" : "";
210	$bg   = (isset($bg)) ? " background-color: $bg;" : "";
211	$al   = (isset($align) && ($align == 'right' || $align == "center" || $align == "justify" || $align == 'left')) ? " text-align: $align;" : '';
212	$fl   = (isset($float) && ($float == 'left' || $float == 'right' || $float == 'none')) ? " float: $float;" : '';
213	$cl   = (isset($clear) && ($clear == 'left' || $clear == 'right' || $clear == 'both' || $clear == 'none')) ? " clear: $clear;" : '';
214
215	if (! empty($params['data'])) {
216		parse_str($params['data'],$attrs);
217		$dt = ' ';
218		foreach ($attrs as $attr => $value) {
219			$dt .= " data-$attr=\"$value\"";
220		}
221	} else {
222		$dt = '';
223	}
224	if (! empty($params['aria'])) {
225		parse_str($params['aria'],$attrs);
226		$ar = ' ';
227		foreach ($attrs as $attr => $value) {
228			$ar .= " aria-$attr=\"$value\"";
229		}
230	} else {
231		$ar = '';
232	}
233	$ro = isset($params['role']) ? "role=\"{$params['role']}\"" : '';
234
235	if (! empty($title)) {
236		$title = " title=\"$title\"";
237	} else {
238		$title = '';
239	}
240	$begin  = "<{$t}{$title}";
241	$format = "$style$bg$al$w$fl$cl";
242	if (! empty($format)) {
243		$begin .= " style=\"$format\"";
244	}
245	$begin .= "$c$id$oc$dt$ar$ro>";
246	$end = "</$t>";
247	return $begin . $content . $end;
248}
249