Ver código fonte

+ AJAX preview for signatures in profile

emanuele 13 anos atrás
pai
commit
0d23029ae5

+ 0 - 1
Sources/Profile-Modify.php

@@ -2430,7 +2430,6 @@ function profileLoadSignatureData()
 		censorText($signature);
 		$context['member']['signature_preview'] = parse_bbc($signature, true, 'sig' . $memberContext[$context['id_member']]);
 		$context['member']['signature'] = $_POST['signature'];
-		
 	}
 
 	return true;

+ 66 - 1
Sources/Xml.php

@@ -81,6 +81,7 @@ function RetrievePreview()
 	$subActions = array(
 		'newspreview' => 'newspreview',
 		'newsletterpreview' => 'newsletterpreview',
+		'sig_preview' => 'sig_preview',
 	);
 
 	$context['sub_template'] = 'generic_xml';
@@ -118,7 +119,6 @@ function newspreview()
 			'children' => $errors
 		),
 	);
-
 }
 function newsletterpreview()
 {
@@ -162,6 +162,71 @@ function newsletterpreview()
 		),
 	);
 }
+function sig_preview()
+{
+	global $context, $sourcedir, $smcFunc, $txt;
+
+	require_once($sourcedir . '/Profile-Modify.php');
+	loadLanguage('Errors');
+
+	$user = isset($_POST['user']) ? (int) $_POST['user'] : 0;
 
+	$errors = array();
+	if (!empty($user))
+	{
+		$request = $smcFunc['db_query']('', '
+			SELECT signature
+			FROM {db_prefix}members
+			WHERE id_member = {int:id_member}
+			LIMIT 1',
+			array(
+				'id_member' => $user,
+			)
+		);
+		list($current_signature) = $smcFunc['db_fetch_row']($request);
+		$smcFunc['db_free_result']($request);
+		censorText($current_signature);
+		$current_signature = parse_bbc($current_signature, true, 'sig' . $user);
+
+		$preview_signature = !empty($_POST['signature']) ? $_POST['signature'] : '';
+		$validation = profileValidateSignature($preview_signature);
+
+		if ($validation !== true && $validation !== false)
+			$errors[] = array('value' => $txt['profile_error_' . $validation], 'attributes' => array('type' => 'error'));
+
+		censorText($preview_signature);
+		$preview_signature = parse_bbc($preview_signature, true, 'sig' . $user);
+	}
+	else
+		$errors[] = array('value' => $txt['no_user_selected'], 'attributes' => array('type' => 'error'));
+
+	$context['xml_data']['signatures'] = array(
+			'identifier' => 'signature',
+			'children' => array()
+		);
+	if (isset($current_signature))
+		$context['xml_data']['signatures']['children'][] = array(
+					'value' => $current_signature,
+					'attributes' => array('type' => 'current'),
+				);
+	if (isset($preview_signature))
+		$context['xml_data']['signatures']['children'][] = array(
+					'value' => $preview_signature,
+					'attributes' => array('type' => 'preview'),
+				);
+	if (!empty($errors))
+		$context['xml_data']['errors'] = array(
+			'identifier' => 'error',
+			'children' => array_merge(
+				array(
+					array(
+						'value' => $txt['profile_errors_occurred'],
+						'attributes' => array('type' => 'errors_occurred'),
+					),
+				),
+				$errors
+			),
+		);
+}
 
 ?>

+ 51 - 8
Themes/default/Profile.template.php

@@ -26,8 +26,7 @@ function template_profile_above()
 	// ]]></script>';
 
 	// If an error occurred while trying to save previously, give the user a clue!
-	if (!empty($context['post_errors']))
-		echo '
+	echo '
 					', template_error_message();
 
 	// If the profile was update successfully, let the user know this.
@@ -1332,7 +1331,7 @@ function template_edit_options()
 
 	if (!empty($context['show_preview_button']))
 		echo '
-						<input type="submit" name="preview" value="', $txt['preview_signature'], '" class="button_submit" />';
+						<input type="submit" name="preview" id="preview_button" value="', $txt['preview_signature'], '" class="button_submit" />';
 
 	// The button shouldn't say "Change profile" unless we're changing the profile...
 	if (!empty($context['submit_button_text']))
@@ -2464,7 +2463,9 @@ function template_error_message()
 {
 	global $context, $txt;
 
-	echo '
+	if (!empty($context['post_errors']))
+	{
+		echo '
 		<div class="windowbg" id="profile_error">
 			<span>', !empty($context['custom_error_title']) ? $context['custom_error_title'] : $txt['profile_errors_occurred'], ':</span>
 			<ul class="reset">';
@@ -2477,6 +2478,10 @@ function template_error_message()
 		echo '
 			</ul>
 		</div>';
+	}
+	else
+		echo '
+		<div class="windowbg" style="display:none" id="profile_error"></div>';
 }
 
 // Display a load of drop down selectors for allowing the user to change group.
@@ -2544,7 +2549,7 @@ function template_profile_birthdate()
 // Show the signature editing box?
 function template_profile_signature_modify()
 {
-	global $txt, $context, $settings;
+	global $txt, $context, $settings, $scripturl;
 
 	echo '
 							<dt id="current_signature"', !isset($context['member']['current_signature']) ? ' style="display:none"' : '', '>
@@ -2554,10 +2559,10 @@ function template_profile_signature_modify()
 								', isset($context['member']['current_signature']) ? $context['member']['current_signature'] : '', '<hr />
 							</dd>';
 	echo '
-							<dt id="signature_preview"', !isset($context['member']['signature_preview']) ? ' style="display:none"' : '', '>
+							<dt id="preview_signature"', !isset($context['member']['signature_preview']) ? ' style="display:none"' : '', '>
 								<strong>', $txt['signature_preview'], ':</strong>
 							</dt>
-							<dd id="signature_preview_display"', !isset($context['member']['signature_preview']) ? ' style="display:none"' : '', '>
+							<dd id="preview_signature_display"', !isset($context['member']['signature_preview']) ? ' style="display:none"' : '', '>
 								', isset($context['member']['signature_preview']) ? $context['member']['signature_preview'] : '', '<hr />
 							</dd>';
 
@@ -2574,7 +2579,7 @@ function template_profile_signature_modify()
 		echo '
 							</dt>
 							<dd>
-								<textarea class="editor" onkeyup="calcCharLeft();" name="signature" rows="5" cols="50">', $context['member']['signature'], '</textarea><br />';
+								<textarea class="editor" onkeyup="calcCharLeft();" id="signature" name="signature" rows="5" cols="50">', $context['member']['signature'], '</textarea><br />';
 
 	// If there is a limit at all!
 	if (!empty($context['signature_limits']['max_length']))
@@ -2625,6 +2630,44 @@ function template_profile_signature_modify()
 									}
 
 									addLoadEvent(tick);
+									$(document).ready(function() {
+										$("#preview_button").click(function() {
+											$.ajax({
+												type: "POST",
+												url: "' . $scripturl . '?action=xmlhttp;sa=previews;xml",
+												data: {item: "sig_preview", signature: $("#signature").val(), user: $(\'input[name="u"]\').attr("value")},
+												context: document.body,
+												success: function(request){
+													var signatures = new Array("current", "preview");
+													for (var i = 0; i < signatures.length; i++)
+													{
+														$("#" + signatures[i] + "_signature").css({display:""});
+														$("#" + signatures[i] + "_signature_display").css({display:""}).html($(request).find(\'[type="\' + signatures[i] + \'"]\').text() + \'<hr />\');
+													}
+
+													if ($(request).find("error").text() != \'\')
+													{
+														$("#profile_error").css({display:""});
+														var errors = $(request).find(\'[type="error"]\');
+														var errors_html = \'<span>\' + $(request).find(\'[type="errors_occurred"]\').text() + \'</span><ul>\';
+
+														for (var i = 0; i < errors.length; i++)
+															errors_html += \'<li>\' + $(errors).text() + \'</li>\';
+
+														errors_html += \'</ul>\';
+														$(document).find("#profile_error").html(errors_html);
+													}
+													else
+													{
+														$("#profile_error").css({display:"none"});
+														$("#profile_error").html(\'\');
+													}
+												return false;
+												},
+											});
+											return false;
+										});
+									});
 								// ]]></script>
 							</dd>';
 }

+ 1 - 0
Themes/default/languages/Errors.english.php

@@ -182,6 +182,7 @@ $txt['invalid_hostname'] = 'Invalid host name / host name range.<br />Example of
 $txt['invalid_ip'] = 'Invalid IP / IP range.<br />Example of a valid IP address: 127.0.0.1<br />Example of a valid IP range: 127.0.0-20.*';
 $txt['invalid_tracking_ip'] = 'Invalid IP / IP range.<br />Example of a valid IP address: 127.0.0.1<br />Example of a valid IP range: 127.0.0.*';
 $txt['invalid_username'] = 'Member name not found';
+$txt['no_user_selected'] = 'Member not found';
 $txt['no_ban_admin'] = 'You may not ban an admin - You must demote them first!';
 $txt['no_bantype_selected'] = 'No ban type was selected';
 $txt['ban_not_found'] = 'Ban not found';