Completed
Push — release-2.1 ( 754615...c3ff7f )
by Mathias
13s
created

GenericControls.template.php ➔ template_control_richedit()   C

Complexity

Conditions 12
Paths 4

Size

Total Lines 41
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
eloc 24
nc 4
nop 3
dl 0
loc 41
rs 5.1612
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
 * Simple Machines Forum (SMF)
4
 *
5
 * @package SMF
6
 * @author Simple Machines http://www.simplemachines.org
7
 * @copyright 2018 Simple Machines and individual contributors
8
 * @license http://www.simplemachines.org/about/smf/license.php BSD
9
 *
10
 * @version 2.1 Beta 4
11
 */
12
13
/**
14
 * This function displays all the stuff you get with a richedit box - BBC, smileys, etc.
15
 *
16
 * @param string $editor_id The editor ID
17
 * @param null|bool $smileyContainer If null, hides the smiley section regardless of settings
18
 * @param null|bool $bbcContainer If null, hides the bbcode buttons regardless of settings
19
 */
20
function template_control_richedit($editor_id, $smileyContainer = null, $bbcContainer = null)
21
{
22
	global $context, $settings, $modSettings, $smcFunc;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
23
24
	$editor_context = &$context['controls']['richedit'][$editor_id];
25
26
	if ($smileyContainer === null)
27
		$editor_context['sce_options']['emoticonsEnabled'] = false;
28
29
	if ($bbcContainer === null)
30
		$editor_context['sce_options']['toolbar'] = '';
31
32
	echo '
33
		<textarea class="editor" name="', $editor_id, '" id="', $editor_id, '" cols="600" onselect="storeCaret(this);" onclick="storeCaret(this);" onkeyup="storeCaret(this);" onchange="storeCaret(this);" tabindex="', $context['tabindex']++, '" style="width: ', $editor_context['width'], '; height: ', $editor_context['height'], ';', isset($context['post_error']['no_message']) || isset($context['post_error']['long_message']) ? 'border: 1px solid red;' : '', '"', !empty($context['editor']['required']) ? ' required' : '', '>', $editor_context['value'], '</textarea>
34
		<div id="', $editor_id, '_resizer" class="richedit_resize"></div>
35
		<input type="hidden" name="', $editor_id, '_mode" id="', $editor_id, '_mode" value="0">
36
		<script>
37
			$(document).ready(function() {
38
				', !empty($context['bbcodes_handlers']) ? $context['bbcodes_handlers'] : '', '
39
40
				var textarea = $("#', $editor_id, '").get(0);
41
				sceditor.create(textarea, ', $smcFunc['json_encode']($editor_context['sce_options'], JSON_PRETTY_PRINT), ');', !$editor_context['sce_options']['emoticonsEnabled'] ? '' : '
42
				sceditor.instance(textarea).createPermanentDropDown();', empty($editor_context['rich_active']) ? '' : '
43
				sceditor.instance(textarea).toggleSourceMode();', isset($context['post_error']['no_message']) || isset($context['post_error']['long_message']) ? '
44
				$(".sceditor-container").find("textarea").each(function() {$(this).css({border: "1px solid red"})});
45
				$(".sceditor-container").find("iframe").each(function() {$(this).css({border: "1px solid red"})});' : '', '
46
			});';
47
48
		// Now for backward compatibility let's collect few infos in the good ol' style
49
		echo '
50
			var oEditorHandle_', $editor_id, ' = new smc_Editor({
51
				sUniqueId: ', JavaScriptEscape($editor_id), ',
52
				sEditWidth: ', JavaScriptEscape($editor_context['width']), ',
53
				sEditHeight: ', JavaScriptEscape($editor_context['height']), ',
54
				bRichEditOff: ', empty($modSettings['disable_wysiwyg']) ? 'false' : 'true', ',
55
				oSmileyBox: null,
56
				oBBCBox: null
57
			});
58
			smf_editorArray[smf_editorArray.length] = oEditorHandle_', $editor_id, ';
59
		</script>';
60
}
61
62
/**
63
 * This template shows the form buttons at the bottom of the editor
64
 *
65
 * @param string $editor_id The editor ID
66
 */
67
function template_control_richedit_buttons($editor_id)
68
{
69
	global $context, $settings, $txt, $modSettings;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
70
71
	$editor_context = &$context['controls']['richedit'][$editor_id];
72
73
	echo '
74
		<span class="smalltext">
75
			', $context['shortcuts_text'], '
76
		</span>';
77
78
	$tempTab = $context['tabindex'];
79
80
	if (!empty($context['drafts_pm_save']))
81
		$tempTab++;
82
	elseif (!empty($context['drafts_save']))
83
		$tempTab++;
84
	elseif ($editor_context['preview_type'])
85
		$tempTab++;
86
	elseif ($context['show_spellchecking'])
87
		$tempTab++;
88
89
	$tempTab++;
90
	$context['tabindex'] = $tempTab;
91
92
	if (!empty($context['drafts_pm_save']))
93
		echo '
94
		<input type="submit" name="save_draft" value="', $txt['draft_save'], '" tabindex="', --$tempTab, '" onclick="submitThisOnce(this);" accesskey="d" class="button">
95
		<input type="hidden" id="id_pm_draft" name="id_pm_draft" value="', empty($context['id_pm_draft']) ? 0 : $context['id_pm_draft'], '">';
96
97
	if (!empty($context['drafts_save']))
98
		echo '
99
		<input type="submit" name="save_draft" value="', $txt['draft_save'], '" tabindex="', --$tempTab, '" onclick="return confirm(' . JavaScriptEscape($txt['draft_save_note']) . ') && submitThisOnce(this);" accesskey="d" class="button">
100
		<input type="hidden" id="id_draft" name="id_draft" value="', empty($context['id_draft']) ? 0 : $context['id_draft'], '">';
101
102
	if ($context['show_spellchecking'])
103
		echo '
104
		<input type="button" value="', $txt['spell_check'], '" tabindex="', --$tempTab, '" onclick="oEditorHandle_', $editor_id, '.spellCheckStart();" class="button">';
105
106
	if ($editor_context['preview_type'])
107
		echo '
108
		<input type="submit" name="preview" value="', isset($editor_context['labels']['preview_button']) ? $editor_context['labels']['preview_button'] : $txt['preview'], '" tabindex="', --$tempTab, '" onclick="', $editor_context['preview_type'] == 2 ? 'return event.ctrlKey || previewPost();' : 'return submitThisOnce(this);', '" accesskey="p" class="button">';
109
110
	echo '
111
		<input type="submit" value="', isset($editor_context['labels']['post_button']) ? $editor_context['labels']['post_button'] : $txt['post'], '" name="post" tabindex="', --$tempTab, '" onclick="return submitThisOnce(this);" accesskey="s" class="button">';
112
113
	// Load in the PM autosaver if it's enabled
114 View Code Duplication
	if (!empty($context['drafts_pm_save']) && !empty($context['drafts_autosave']))
115
		echo '
116
		<span class="righttext padding" style="display: block">
117
			<span id="throbber" style="display:none"><img src="' . $settings['images_url'] . '/loading_sm.gif" alt="" class="centericon"></span>
118
			<span id="draft_lastautosave" ></span>
119
		</span>
120
		<script src="', $settings['default_theme_url'], '/scripts/drafts.js', $modSettings['browser_cache'], '"></script>
121
		<script>
122
			var oDraftAutoSave = new smf_DraftAutoSave({
123
				sSelf: \'oDraftAutoSave\',
124
				sLastNote: \'draft_lastautosave\',
125
				sLastID: \'id_pm_draft\',
126
				sSceditorID: \'', $editor_id, '\',
127
				sType: \'post\',
128
				bPM: true,
129
				iBoard: 0,
130
				iFreq: ', (empty($modSettings['drafts_autosave_frequency']) ? 60000 : $modSettings['drafts_autosave_frequency'] * 1000), '
131
			});
132
		</script>';
133
134
	// Start an instance of the auto saver if its enabled
135 View Code Duplication
	if (!empty($context['drafts_save']) && !empty($context['drafts_autosave']))
136
		echo '
137
		<span class="righttext padding" style="display: block">
138
			<span id="throbber" style="display:none"><img src="', $settings['images_url'], '/loading_sm.gif" alt="" class="centericon"></span>
139
			<span id="draft_lastautosave" ></span>
140
		</span>
141
		<script src="', $settings['default_theme_url'], '/scripts/drafts.js', $modSettings['browser_cache'], '"></script>
142
		<script>
143
			var oDraftAutoSave = new smf_DraftAutoSave({
144
				sSelf: \'oDraftAutoSave\',
145
				sLastNote: \'draft_lastautosave\',
146
				sLastID: \'id_draft\',
147
				sSceditorID: \'', $editor_id, '\',
148
				sType: \'post\',
149
				iBoard: ', (empty($context['current_board']) ? 0 : $context['current_board']), ',
150
				iFreq: ', $context['drafts_autosave_frequency'], '
151
			});
152
		</script>';
153
}
154
155
/**
156
 * This template displays a verification form
157
 *
158
 * @param int|string $verify_id The verification control ID
159
 * @param string $display_type What type to display. Can be 'single' to only show one verification option or 'all' to show all of them
160
 * @param bool $reset Whether to reset the internal tracking counter
161
 * @return bool False if there's nothing else to show, true if $display_type is 'single', nothing otherwise
0 ignored issues
show
Documentation introduced by
Should the return type not be boolean|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
162
 */
163
function template_control_verification($verify_id, $display_type = 'all', $reset = false)
164
{
165
	global $context, $txt;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
166
167
	$verify_context = &$context['controls']['verification'][$verify_id];
168
169
	// Keep track of where we are.
170
	if (empty($verify_context['tracking']) || $reset)
171
		$verify_context['tracking'] = 0;
172
173
	// How many items are there to display in total.
174
	$total_items = count($verify_context['questions']) + ($verify_context['show_visual'] || $verify_context['can_recaptcha'] ? 1 : 0);
175
176
	// If we've gone too far, stop.
177
	if ($verify_context['tracking'] > $total_items)
178
		return false;
179
180
	// Loop through each item to show them.
181
	for ($i = 0; $i < $total_items; $i++)
182
	{
183
		// If we're after a single item only show it if we're in the right place.
184
		if ($display_type == 'single' && $verify_context['tracking'] != $i)
185
			continue;
186
187
		if ($display_type != 'single')
188
			echo '
189
			<div id="verification_control_', $i, '" class="verification_control">';
190
191
		// Display empty field, but only if we have one, and it's the first time.
192
		if ($verify_context['empty_field'] && empty($i))
193
			echo '
194
				<div class="smalltext vv_special">
195
					', $txt['visual_verification_hidden'], ':
196
					<input type="text" name="', $_SESSION[$verify_id . '_vv']['empty_field'], '" autocomplete="off" size="30" value="">
197
				</div>';
198
199
		// Do the actual stuff
200
		if ($i == 0 && ($verify_context['show_visual'] || $verify_context['can_recaptcha']))
201
		{
202
			if ($verify_context['show_visual'])
203
			{
204
				if ($context['use_graphic_library'])
205
					echo '
206
				<img src="', $verify_context['image_href'], '" alt="', $txt['visual_verification_description'], '" id="verification_image_', $verify_id, '">';
207
				else
208
					echo '
209
				<img src="', $verify_context['image_href'], ';letter=1" alt="', $txt['visual_verification_description'], '" id="verification_image_', $verify_id, '_1">
210
				<img src="', $verify_context['image_href'], ';letter=2" alt="', $txt['visual_verification_description'], '" id="verification_image_', $verify_id, '_2">
211
				<img src="', $verify_context['image_href'], ';letter=3" alt="', $txt['visual_verification_description'], '" id="verification_image_', $verify_id, '_3">
212
				<img src="', $verify_context['image_href'], ';letter=4" alt="', $txt['visual_verification_description'], '" id="verification_image_', $verify_id, '_4">
213
				<img src="', $verify_context['image_href'], ';letter=5" alt="', $txt['visual_verification_description'], '" id="verification_image_', $verify_id, '_5">
214
				<img src="', $verify_context['image_href'], ';letter=6" alt="', $txt['visual_verification_description'], '" id="verification_image_', $verify_id, '_6">';
215
216
				echo '
217
				<div class="smalltext" style="margin: 4px 0 8px 0;">
218
					<a href="', $verify_context['image_href'], ';sound" id="visual_verification_', $verify_id, '_sound" rel="nofollow">', $txt['visual_verification_sound'], '</a> / <a href="#visual_verification_', $verify_id, '_refresh" id="visual_verification_', $verify_id, '_refresh">', $txt['visual_verification_request_new'], '</a>', $display_type != 'quick_reply' ? '<br>' : '', '<br>
219
					', $txt['visual_verification_description'], ':', $display_type != 'quick_reply' ? '<br>' : '', '
220
					<input type="text" name="', $verify_id, '_vv[code]" value="" size="30" tabindex="', $context['tabindex']++, '" autocomplete="off" required>
221
				</div>';
222
			}
223
224
			if ($verify_context['can_recaptcha'])
225
			{
226
				$lang = (isset($txt['lang_recaptcha']) ? $txt['lang_recaptcha'] : $txt['lang_dictionary']);
227
				echo '
228
				<div class="g-recaptcha centertext" data-sitekey="' . $verify_context['recaptcha_site_key'] . '" data-theme="' . $verify_context['recaptcha_theme'] . '"></div>
229
				<br>
230
				<script type="text/javascript" src="https://www.google.com/recaptcha/api.js?hl='.$lang.'"></script>';
231
			}
232
		}
233
		else
234
		{
235
			// Where in the question array is this question?
236
			$qIndex = $verify_context['show_visual'] ? $i - 1 : $i;
237
238
			echo '
239
				<div class="smalltext">
240
					', $verify_context['questions'][$qIndex]['q'], ':<br>
241
					<input type="text" name="', $verify_id, '_vv[q][', $verify_context['questions'][$qIndex]['id'], ']" size="30" value="', $verify_context['questions'][$qIndex]['a'], '" ', $verify_context['questions'][$qIndex]['is_error'] ? 'style="border: 1px red solid;"' : '', ' tabindex="', $context['tabindex']++, '" required>
242
				</div>';
243
		}
244
245
		if ($display_type != 'single')
246
			echo '
247
			</div><!-- #verification_control_[i] -->';
248
249
		// If we were displaying just one and we did it, break.
250
		if ($display_type == 'single' && $verify_context['tracking'] == $i)
251
			break;
252
	}
253
254
	// Assume we found something, always.
255
	$verify_context['tracking']++;
256
257
	// Tell something displaying piecemeal to keep going.
258
	if ($display_type == 'single')
259
		return true;
260
}
261
262
?>