template_block_edit()   F
last analyzed

Complexity

Conditions 55
Paths > 20000

Size

Total Lines 280
Code Lines 137

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 1 Features 0
Metric Value
cc 55
eloc 137
nc 67968
nop 0
dl 0
loc 280
rs 0
c 6
b 1
f 0

How to fix   Long Method    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
/**
4
 * @package SimplePortal ElkArte
5
 *
6
 * @author SimplePortal Team
7
 * @copyright 2015-2021 SimplePortal Team
8
 * @license BSD 3-clause
9
 * @version 1.0.0
10
 */
11
12
/**
13
 * Show the list of available blocks in the system
14
 * If no specific area is provided will show all areas with available blocks in each
15
 * otherwise will just show the chosen area
16
 */
17
function template_block_list()
18
{
19
	global $context, $scripturl, $txt;
20
21
	$sortables = array();
22
23
	echo '
24
	<div id="sp_manage_blocks">';
25
26
	// Show each portal area with the blocks in each one
27
	foreach($context['sides'] as $id => $side)
28
	{
29
		$sortables[] = '#side_' . $side['id'];
30
31
		echo '
32
		<h3 class="category_header">
33
			<a class="floatright" href="', $scripturl, '?action=admin;area=portalblocks;sa=add;col=', $side['id'], '">', sp_embed_image('add', sprintf($txt['sp-blocksCreate'], $side['label'])), '</a>
34
			<a class="hdicon cat_img_helptopics help" href="', $scripturl, '?action=quickhelp;help=', $side['help'], '" onclick="return reqOverlayDiv(this.href);" title="', $txt['help'], '"></a>
35
			<a href="', $scripturl, '?action=admin;area=portalblocks;sa=', $id, '">', $side['label'], ' ', $txt['sp-blocksBlocks'], '</a>
36
		</h3>
37
		<table class="table_grid">
38
			<thead>
39
				<tr class="table_head">';
40
41
		foreach ($context['columns'] as $column)
42
			echo '
43
					<th scope="col"', isset($column['class']) ? ' class="' . $column['class'] . '"' : '', isset($column['width']) ? ' style="width:' . $column['width'] . ';"' : '', '>', $column['label'], '</th>';
44
45
		echo '
46
				</tr>
47
			</thead>
48
			<tbody id="side_', $side['id'] ,'" class="sortme">';
49
50
		if (empty($context['blocks'][$side['name']]))
51
		{
52
			echo '
53
				<tr class="content">
54
					<td class="centertext noticebox" colspan="3"></td>
55
				</tr>';
56
		}
57
58
		foreach($context['blocks'][$side['name']] as $block)
59
		{
60
			echo '
61
				<tr id="block_',$block['id'],'" class="content">
62
					<td>', $block['label'], '</td>
63
					<td>', $block['type_text'], '</td>
64
					<td class="centertext">', $block['status'], '</td>
65
					<td class="centertext">', implode(' ', $block['actions']), '</td>
66
				</tr>';
67
		}
68
69
		echo '
70
			</tbody>
71
		</table>';
72
	}
73
74
	// Engage sortable to allow drag/drop arrangement of the blocks
75
	echo '
76
	</div>
77
	<script>
78
		// Set up our sortable call
79
		$().elkSortable({
80
			sa: "blockorder",
81
			error: "' . $txt['admin_order_error'] . '",
82
			title: "' . $txt['admin_order_title'] . '",
83
			token: {token_var: "' . $context['admin-sort_token_var'] . '", token_id: "' . $context['admin-sort_token'] . '"},
84
			tag: "' . implode(',', $sortables) . '",
85
			connect: ".sortme",
86
			containment: "#sp_manage_blocks",
87
			href: "?action=admin;area=portalblocks",
88
			placeholder: "ui-state-highlight",
89
			axis: "y"
90
		});
91
	</script>';
92
}
93
94
/**
95
 * Used to edit a blocks details when using the block on the portal
96
 */
97
function template_block_edit()
98
{
99
	global $context, $settings, $options, $scripturl, $txt, $helptxt;
100
101
	// Want to take a look before you save?
102
	if (!empty($context['SPortal']['preview']))
103
	{
104
		if (!empty($context['SPortal']['error']))
105
			echo '
106
	<div class="errorbox">' , $context['SPortal']['error'], '</div>';
107
108
		echo '
109
	<div class="sp_auto_align" style="width: ', $context['widths'][$context['SPortal']['block']['column']], ';">';
110
111
		template_block($context['SPortal']['block']);
112
113
		echo '
114
	</div>';
115
	}
116
117
	echo '
118
	<div id="sp_edit_block">
119
		<form id="admin_form_wrapper" name="sp_edit_block_form" id="sp_edit_block_form" action="', $scripturl, '?action=admin;area=portalblocks;sa=edit" method="post" accept-charset="UTF-8" onsubmit="submitonce(this);">
120
			<h3 class="category_header">
121
				<a class="hdicon cat_img_helptopics help" href="', $scripturl, '?action=quickhelp;help=sp-blocks', $context['SPortal']['is_new'] ? 'Add' : 'Edit', '" onclick="return reqOverlayDiv(this.href);" title="', $txt['help'], '"></a>
122
				', $context['SPortal']['is_new'] ? $txt['sp-blocksAdd'] : $txt['sp-blocksEdit'], '
123
			</h3>
124
				<div class="sp_content_padding">
125
					<h3 class="secondary_header">',$context['SPortal']['block']['type_text'], '</h3>
126
					<dl class="sp_form content">
127
						<dt>
128
							<label for="block_name">', $txt['sp-adminColumnName'], ':</label>
129
						</dt>
130
						<dd>
131
							<input type="text" name="block_name" id="block_name" value="', $context['SPortal']['block']['label'], '" size="30" class="input_text" />
132
						</dd>
133
						<dt>
134
							<label for="block_permissions">', $txt['sp_admin_blocks_col_permissions'], ':</label>
135
						</dt>
136
						<dd>
137
							<select name="permissions" id="block_permissions">';
138
139
	foreach ($context['SPortal']['block']['permission_profiles'] as $profile)
140
		echo '
141
									<option value="', $profile['id'], '"', $profile['id'] == $context['SPortal']['block']['permissions'] ? ' selected="selected"' : '', '>', $profile['label'], '</option>';
142
143
	echo '
144
							</select>
145
						</dd>
146
						<dt>
147
							<label for="block_styles">', $txt['sp_admin_blocks_col_styles'], ':</label>
148
						</dt>
149
						<dd>
150
							<select name="styles" id="block_styles">';
151
152
	foreach ($context['SPortal']['block']['style_profiles'] as $profile)
153
		echo '
154
							<option value="', $profile['id'], '"', $profile['id'] == $context['SPortal']['block']['styles'] ? ' selected="selected"' : '', '>', $profile['label'], '</option>';
155
156
	echo '
157
							</select>
158
						</dd>
159
						<dt>
160
							<label for="block_visibility">', $txt['sp_admin_blocks_col_visibility'], ':</label>
161
						</dt>
162
						<dd>
163
							<select name="visibility" id="block_visibility">';
164
165
	foreach ($context['SPortal']['block']['visibility_profiles'] as $profile)
166
		echo '
167
					<option value="', $profile['id'], '"', $profile['id'] == $context['SPortal']['block']['visibility'] ? ' selected="selected"' : '', '>', $profile['label'], '</option>';
168
169
	echo '
170
							</select>
171
						</dd>
172
					</dl>
173
					<h3 class="secondary_header">', $txt['sp-adminBlockSettingsName'], '</h3>
174
					<dl class="sp_form content">';
175
176
	// Display any options that are available for this block
177
	foreach ($context['SPortal']['block']['options'] as $name => $type)
178
	{
179
		if (empty($context['SPortal']['block']['parameters'][$name]))
180
			$context['SPortal']['block']['parameters'][$name] = '';
181
182
		echo '
183
						<dt>';
184
185
		// Look for the help text using legacyNaming
186
		$helpvar = lcfirst(ucwords(str_replace('_', ' ', $context['SPortal']['block']['type'])));
187
		$helpvar = str_replace(' ', '', $helpvar);
188
		$helpvar = 'sp_param_sp_' . $helpvar . '_' . $name;
189
190
		if (!empty($helptxt[$helpvar]))
191
			echo '
192
							<a class="help" href="', $scripturl, '?action=quickhelp;help=', $helpvar, '" onclick="return reqOverlayDiv(this.href);">
193
								<img class="icon" src="', $settings['images_url'], '/helptopics.png" alt="', $txt['help'], '" />
194
							</a>';
195
196
		if ($type === 'space')
197
			echo '';
198
		else
199
			echo '
200
							<label for="', $type === 'bbc' ? 'bbc_content' : $name, '">', $txt['sp_param_' . $context['SPortal']['block']['type'] . '_' . $name], ':</label>';
201
202
		echo '
203
						</dt>
204
						<dd>';
205
206
		if ($type === 'bbc')
207
		{
208
			echo '
209
						</dd>
210
					</dl>
211
					<div id="sp_rich_editor">
212
						<div id="sp_rich_bbc"></div>
213
						<div id="sp_rich_smileys"></div>
214
						', template_control_richedit($context['SPortal']['bbc'], 'sp_rich_smileys', 'sp_rich_bbc'), '
215
						<input type="hidden" name="bbc_name" value="', $name, '" />
216
						<input type="hidden" name="bbc_parameter" value="', $context['SPortal']['bbc'], '" />
217
					</div>
218
					<dl class="sp_form">';
219
		}
220
		elseif ($type === 'boards' || $type === 'board_select')
221
		{
222
			echo '
223
							<input type="hidden" name="parameters[', $name, ']" value="" />';
224
225
			if ($type === 'boards')
226
					echo '
227
							<select name="parameters[', $name, '][]" id="', $name, '" size="7" multiple="multiple">';
228
			else
229
					echo '
230
							<select name="parameters[', $name, '][]" id="', $name, '">';
231
232
			foreach ($context['SPortal']['block']['board_options'][$name] as $option)
233
				echo '
234
								<option value="', $option['value'], '"', ($option['selected'] ? ' selected="selected"' : ''), ' >', $option['text'], '</option>';
235
236
			echo '
237
							</select>';
238
		}
239
		elseif ($type === 'int')
240
			echo '
241
							<input type="text" name="parameters[', $name, ']" id="', $name, '" value="', $context['SPortal']['block']['parameters'][$name],'" size="7" class="input_text" />';
242
		elseif ($type === 'text')
243
			echo '
244
							<input type="text" name="parameters[', $name, ']" id="', $name, '" value="', $context['SPortal']['block']['parameters'][$name],'" size="25" class="input_text" />';
245
		elseif ($type === 'check')
246
				echo '
247
							<input type="checkbox" name="parameters[', $name, ']" id="', $name, '"', !empty($context['SPortal']['block']['parameters'][$name]) ? ' checked="checked"' : '', ' class="input_check" />';
248
		elseif ($type === 'select')
249
		{
250
				$options = explode('|', $txt['sp_param_' . $context['SPortal']['block']['type'] . '_' . $name . '_options']);
251
252
				echo '
253
							<select name="parameters[', $name, ']" id="', $name, '">';
254
255
				foreach ($options as $key => $option)
256
					echo '
257
								<option value="', $key, '"', $context['SPortal']['block']['parameters'][$name] == $key ? ' selected="selected"' : '', '>', $option, '</option>';
258
259
				echo '
260
							</select>';
261
		}
262
		elseif (is_array($type))
263
		{
264
				echo '
265
							<select name="parameters[', $name, ']" id="', $name, '">';
266
267
				foreach ($type as $key => $option)
268
					echo '
269
								<option value="', $key, '"', $context['SPortal']['block']['parameters'][$name] == $key ? ' selected="selected"' : '', '>', $option, '</option>';
270
271
				echo '
272
							</select>';
273
		}
274
		elseif ($type === 'textarea')
275
		{
276
			echo '
277
						</dd>
278
					</dl>
279
					<div id="sp_text_editor">
280
						<textarea name="parameters[', $name, ']" id="', $name, '" cols="45" rows="10">', $context['SPortal']['block']['parameters'][$name], '</textarea>
281
						<input type="button" class="button_submit" value="-" onclick="document.getElementById(\'', $name, '\').rows -= 10" />
282
						<input type="button" class="button_submit" value="+" onclick="document.getElementById(\'', $name, '\').rows += 10" />
283
					</div>
284
					<dl class="sp_form">';
285
		}
286
		elseif ($type === 'space')
287
		{
288
			echo '
289
						</dd>
290
					</dl>
291
					<hr>
292
					<dl class="sp_form">';
293
		}
294
295
		if ($type != 'bbc' && $type != 'textarea')
296
			echo '
297
						</dd>';
298
	}
299
300
	if (empty($context['SPortal']['block']['column']))
301
	{
302
		echo '
303
						<dt>
304
							<label for="block_column">', $txt['sp-blocksColumn'], ':</label>
305
						</dt>
306
						<dd>
307
							<select id="block_column" name="block_column">';
308
309
		$block_sides = array(5 => 'Header', 1 => 'Left', 2 => 'Top', 3 => 'Bottom', 4 => 'Right', 6 => 'Footer');
310
		foreach ($block_sides as $id => $side)
311
			echo '
312
								<option value="', $id, '">', $txt['sp-position' . $side], '</option>';
313
314
		echo '
315
							</select>
316
						</dd>';
317
	}
318
319
	if (count($context['SPortal']['block']['list_blocks']) > 1)
320
	{
321
		echo '
322
						<dt>
323
							', $txt['sp-blocksRow'], ':
324
						</dt>
325
						<dd>
326
							<select id="order" name="placement"', !$context['SPortal']['is_new'] ? ' onchange="this.form.block_row.disabled = this.options[this.selectedIndex].value == \'\';"' : '', '>
327
								', !$context['SPortal']['is_new'] ? '<option value="nochange">' . $txt['sp-placementUnchanged'] . '</option>' : '', '
328
								<option value="before"', (!empty($context['SPortal']['block']['placement']) && $context['SPortal']['block']['placement'] === 'before' ? ' selected="selected"' : ''), '>', $txt['sp-placementBefore'], '...</option>
329
								<option value="after"', (!empty($context['SPortal']['block']['placement']) && $context['SPortal']['block']['placement'] === 'after' ? ' selected="selected"' : ''), '>', $txt['sp-placementAfter'], '...</option>
330
							</select>
331
							<select id="block_row" name="block_row"', !$context['SPortal']['is_new'] ? ' disabled="disabled"' : '', '>';
332
333
		foreach ($context['SPortal']['block']['list_blocks'] as $block)
334
		{
335
			if ($block['id'] != $context['SPortal']['block']['id'])
336
				echo '
337
								<option value="', $block['row'], '"', (!empty($context['SPortal']['block']['row']) && $context['SPortal']['block']['row'] == $block['row'] ? ' selected="selected"' : ''), '>', $block['label'], '</option>';		}
338
339
		echo '
340
							</select>
341
						</dd>';
342
	}
343
344
	if ($context['SPortal']['block']['type'] !== 'sp_boardNews')
345
	{
346
		echo '
347
						<dt>
348
							<label for="block_force">', $txt['sp-blocksForce'], ':</label>
349
						</dt>
350
						<dd>
351
							<input type="checkbox" name="block_force" id="block_force" value="1"', $context['SPortal']['block']['force_view'] ? ' checked="checked"' : '', ' class="input_check" />
352
						</dd>';
353
	}
354
355
	echo '
356
				<dt>
357
					<label for="block_active">', $txt['sp-blocksActive'], ':</label>
358
				</dt>
359
				<dd>
360
					<input type="checkbox" name="block_active" id="block_active" value="1"', $context['SPortal']['block']['state'] ? ' checked="checked"' : '', ' class="input_check" />
361
				</dd>
362
			</dl>
363
			<div class="submitbutton">
364
				<input type="submit" name="preview_block" value="', $txt['sp-blocksPreview'], '" class="button_submit" />
365
				<input type="submit" name="add_block" value="', !$context['SPortal']['is_new'] ? $txt['sp-blocksEdit'] : $txt['sp-blocksAdd'], '" class="button_submit" />
366
			</div>
367
		</div>';
368
369
	if (!empty($context['SPortal']['block']['column']))
370
		echo '
371
			<input type="hidden" name="block_column" value="', $context['SPortal']['block']['column'], '" />';
372
373
	echo '
374
			<input type="hidden" name="block_type" value="', $context['SPortal']['block']['type'], '" />
375
			<input type="hidden" name="block_id" value="', $context['SPortal']['block']['id'], '" />
376
			<input type="hidden" name="', $context['session_var'], '" value="', $context['session_id'], '" />
377
		</form>
378
	</div>';
379
}
380
381
/**
382
 * Used to select one of our predefined blocks for use in the portal
383
 */
384
function template_block_select_type()
385
{
386
	global $context, $scripturl, $txt;
387
388
	echo '
389
	<div id="sp_select_block_type">
390
		<h3 class="category_header">
391
			<a class="hdicon cat_img_helptopics help" href="', $scripturl, '?action=quickhelp;help=sp-blocksSelectType" onclick="return reqOverlayDiv(this.href);" title="', $txt['help'], '"></a>
392
			', $txt['sp-blocksSelectType'], '
393
		</h3>
394
		<form id="admin_form_wrapper" action="', $scripturl, '?action=admin;area=portalblocks;sa=add" method="post" accept-charset="UTF-8">
395
			<ul class="reset">';
396
397
	// For every block type defined in the system
398
	foreach($context['SPortal']['block_types'] as $index => $type)
399
	{
400
		$this_block = isset($context['SPortal']['block_inuse'][$type['function']]) ? $context['SPortal']['block_inuse'][$type['function']] : false;
401
		$this_title = !empty($this_block) ? sprintf($txt['sp-adminBlockInuse'], $context['location'][$this_block['column']]) . ': ' . (!empty($this_block['state']) ? '(' . $txt['sp-blocksActive'] . ')' : '') : '';
402
403
		echo '
404
				<li class="content">
405
					<input type="radio" name="selected_type[]" id="block_', $type['function'], '" value="', $type['function'], '" class="input_radio" />
406
					<strong><label ', (!empty($this_block) ? 'class="sp_block_active" ' : ''), 'for="block_', $type['function'], '" title="', $this_title, '">', isset($txt['sp_function_' . $type['function'] . '_label']) ? $txt['sp_function_' . $type['function'] . '_label'] : $type['function'], '</label></strong>
407
					<p class="smalltext">', isset($txt['sp_function_' . $type['function'] . '_desc']) ? $txt['sp_function_' . $type['function'] . '_desc'] : $txt['not_applicable'], '</p>
408
				</li>';
409
	}
410
411
	echo '
412
				</ul>
413
				<input type="submit" name="select_type" value="', $txt['sp-blocksSelectType'], '" class="right_submit" />';
414
415
	if (!empty($context['SPortal']['block']['column']))
416
		echo '
417
			<input type="hidden" name="block_column" value="', $context['SPortal']['block']['column'], '" />';
418
419
	echo '
420
			<input type="hidden" name="', $context['session_var'], '" value="', $context['session_id'], '" />
421
		</form>
422
	</div>';
423
}
424