Browse Source

+ Preview for newsletters (both ajax and non); ! Sending fail going to preview if body or subject are empty

emanuele 12 years ago
parent
commit
87f83b0670

+ 99 - 8
Sources/ManageNews.php

@@ -410,6 +410,66 @@ function SelectMailingMembers()
 	$context['can_send_pm'] = allowedTo('pm_send');
 }
 
+/**
+ * Prepare subject and message of an email for the preview box
+ * Used in ComposeMailing and RetrievePreview (Xml.php)
+ */
+function prepareMailingForPreview ()
+{
+	global $context, $smcFunc, $modSettings, $scripturl, $user_info, $txt;
+	loadLanguage('Errors');
+
+	$processing = array('preview_subject' => 'subject', 'preview_message' => 'message');
+
+	// Use the default time format.
+	$user_info['time_format'] = $modSettings['time_format'];
+
+	$variables = array(
+		'{$board_url}',
+		'{$current_time}',
+		'{$latest_member.link}',
+		'{$latest_member.id}',
+		'{$latest_member.name}'
+	);
+
+	$html = $context['send_html'];
+
+	// We might need this in a bit
+	$cleanLatestMember = empty($context['send_html']) || $context['send_pm'] ? un_htmlspecialchars($modSettings['latestRealName']) : $modSettings['latestRealName'];
+
+	foreach ($processing as $key => $post)
+	{
+		$context[$key] = $smcFunc['htmlspecialchars']($_REQUEST[$post], ENT_QUOTES);
+
+		if (empty($context[$key]) && empty($_REQUEST['xml']))
+		{
+			$html = true;
+			$context[$key] = '[color=red]' . $txt['error_no_' . $post] . '[/color]';
+		}
+		elseif (!empty($_REQUEST['xml']))
+			continue;
+
+		preparsecode($context[$key]);
+		if ($html)
+		{
+			$enablePostHTML = $modSettings['enablePostHTML'];
+			$modSettings['enablePostHTML'] = $context['send_html'];
+			$context[$key] = parse_bbc($context[$key]);
+			$modSettings['enablePostHTML'] = $enablePostHTML;
+		}
+
+		// Replace in all the standard things.
+		$context[$key] = str_replace($variables,
+			array(
+				!empty($context['send_html']) ? '<a href="' . $scripturl . '">' . $scripturl . '</a>' : $scripturl,
+				timeformat(forum_time(), false),
+				!empty($context['send_html']) ? '<a href="' . $scripturl . '?action=profile;u=' . $modSettings['latestMember'] . '">' . $cleanLatestMember . '</a>' : ($context['send_pm'] ? '[url=' . $scripturl . '?action=profile;u=' . $modSettings['latestMember'] . ']' . $cleanLatestMember . '[/url]' : $cleanLatestMember),
+				$modSettings['latestMember'],
+				$cleanLatestMember
+			), $context[$key]);
+	}
+}
+
 /**
  * Shows a form to edit a forum mailing and its recipients.
  * Called by ?action=admin;area=news;sa=mailingcompose.
@@ -420,7 +480,33 @@ function SelectMailingMembers()
  */
 function ComposeMailing()
 {
-	global $txt, $sourcedir, $context, $smcFunc;
+	global $txt, $sourcedir, $context, $smcFunc, $scripturl, $modSettings;
+
+	// Setup the template!
+	$context['page_title'] = $txt['admin_newsletters'];
+	$context['sub_template'] = 'email_members_compose';
+
+	$context['default_subject'] = htmlspecialchars($context['forum_name'] . ': ' . $txt['subject']);
+	$context['default_message'] = htmlspecialchars($txt['message'] . "\n\n" . $txt['regards_team'] . "\n\n" . '{$board_url}');
+
+	if (isset($context['preview']))
+	{
+		require_once($sourcedir . '/Subs-Post.php');
+		$context['recipients']['members'] = explode(',', $_POST['exclude_members']);
+		$context['recipients']['exclude_members'] = explode(',', $_POST['exclude_members']);
+		$context['recipients']['groups'] = explode(',', $_POST['exclude_members']);
+		$context['recipients']['exclude_groups'] = explode(',', $_POST['exclude_members']);
+		$context['recipients']['emails'] = explode(';', $_POST['exclude_members']);
+		$context['email_force'] = !empty($_POST['email_force']) ? 1 : 0;
+		$context['total_emails'] = !empty($_POST['total_emails']) ? (int) $_POST['total_emails'] : 0;
+		$context['max_id_member'] = !empty($_POST['max_id_member']) ? (int) $_POST['max_id_member'] : 0;
+		$context['send_pm'] = !empty($_POST['send_pm']) ? 1 : 0;
+		$context['send_html'] = !empty($_POST['send_html']) ? '1' : '0';
+		$context['subject'] = $_POST['subject'];
+		$context['message'] = $_POST['message'];
+
+		return prepareMailingForPreview();
+	}
 
 	// Start by finding any members!
 	$toClean = array();
@@ -561,13 +647,6 @@ function ComposeMailing()
 	// Clean up the arrays.
 	$context['recipients']['members'] = array_unique($context['recipients']['members']);
 	$context['recipients']['exclude_members'] = array_unique($context['recipients']['exclude_members']);
-
-	// Setup the template!
-	$context['page_title'] = $txt['admin_newsletters'];
-	$context['sub_template'] = 'email_members_compose';
-
-	$context['default_subject'] = htmlspecialchars($context['forum_name'] . ': ' . $txt['subject']);
-	$context['default_message'] = htmlspecialchars($txt['message'] . "\n\n" . $txt['regards_team'] . "\n\n" . '{$board_url}');
 }
 
 /**
@@ -585,6 +664,12 @@ function SendMailing($clean_only = false)
 	global $txt, $sourcedir, $context, $smcFunc;
 	global $scripturl, $modSettings, $user_info;
 
+	if (isset($_POST['preview']))
+	{
+		$context['preview'] = true;
+		return ComposeMailing();
+	}
+
 	// How many to send at once? Quantity depends on whether we are queueing or not.
 	$num_at_once = empty($modSettings['mail_queue']) ? 60 : 1000;
 
@@ -698,6 +783,12 @@ function SendMailing($clean_only = false)
 		}
 	}
 
+	if (empty($_POST['message']) || empty($_POST['subject']))
+	{
+		$context['preview'] = true;
+		return ComposeMailing();
+	}
+
 	// Use the default time format.
 	$user_info['time_format'] = $modSettings['time_format'];
 

+ 46 - 2
Sources/Xml.php

@@ -80,6 +80,7 @@ function RetrievePreview()
 
 	$subActions = array(
 		'newspreview' => 'newspreview',
+		'newsletterpreview' => 'newsletterpreview',
 	);
 
 	$context['sub_template'] = 'generic_xml';
@@ -87,7 +88,6 @@ function RetrievePreview()
 	if (!isset($_POST['item']) || !in_array($_POST['item'], $subActions))
 		return false;
 
-// echo $subActions[$_REQUEST['item']];die();
 	$subActions[$_POST['item']]();
 }
 
@@ -98,7 +98,7 @@ function newspreview()
 	require_once($sourcedir . '/Subs-Post.php');
 
 	$errors = array();
-	$news = !isset($_POST['news'])? '' : $smcFunc['htmlspecialchars']($_POST['news'], ENT_QUOTES);;
+	$news = !isset($_POST['news'])? '' : $smcFunc['htmlspecialchars']($_POST['news'], ENT_QUOTES);
 	if (empty($news))
 		$errors[] = array('value' => 'no_news');
 	else
@@ -120,4 +120,48 @@ function newspreview()
 	);
 
 }
+function newsletterpreview()
+{
+	global $context, $sourcedir, $smcFunc, $txt;
+
+	require_once($sourcedir . '/Subs-Post.php');
+	require_once($sourcedir . '/ManageNews.php');
+	loadLanguage('Errors');
+
+	$errors = array();
+	$context['send_pm'] = !empty($_POST['send_pm']) ? 1 : 0;
+	$context['send_html'] = !empty($_POST['send_html']) ? 1 : 0;
+
+	if (empty($_POST['subject']))
+		$errors[] = array('value' => $txt['error_no_subject'], 'attributes' => array('type' => 'subject_preview'));
+	if (empty($_POST['message']))
+		$errors[] = array('value' => $txt['error_no_message'], 'attributes' => array('type' => 'message_preview'));
+
+	prepareMailingForPreview();
+
+	$context['xml_data'] = array(
+		'message' => array(
+			'identifier' => 'preview_message',
+			'children' => array(
+				array(
+					'value' => $context['preview_message'],
+				),
+			),
+		),
+		'subject' => array(
+			'identifier' => 'preview_subject',
+			'children' => array(
+				array(
+					'value' => $context['preview_subject'],
+				),
+			),
+		),
+		'errors' => array(
+			'identifier' => 'error',
+			'children' => $errors
+		),
+	);
+}
+
+
 ?>

+ 43 - 4
Themes/default/ManageNews.template.php

@@ -192,18 +192,26 @@ function template_email_members_compose()
 			<div class="windowbg">
 				<span class="topslice"><span></span></span>
 				<div class="content">
+					<div id="preview_box" ', empty($context['preview_subject']) && empty($context['preview_message']) ? 'style="display:none"' : '', '>
+						<div style="padding-bottom:10px"><em>', $txt['email_preview_warning'], '</em></div>
+						<strong>', $txt['subject'], '</strong>:
+						<div style="padding-left:10px;padding-bottom:10px" id="subject_preview">', !isset($context['preview_subject']) ? '' : $context['preview_subject'], '</div>
+						<strong>', $txt['message'], '</strong>:
+						<div style="padding-left:10px;padding-bottom:10px" id="message_preview">', !isset($context['preview_message']) ? '' : $context['preview_message'], '</div>
+					</div>
 					<p>
-						<input type="text" name="subject" size="60" value="', $context['default_subject'], '" class="input_text" />
+						<input type="text" name="subject" id="subject" size="60" value="', empty($context['subject']) ? $context['default_subject'] : $context['subject'], '" class="input_text" />
 					</p>
 					<p>
-						<textarea cols="70" rows="9" name="message" class="editor">', $context['default_message'], '</textarea>
+						<textarea cols="70" rows="9" name="message" id="message" class="editor">', empty($context['message']) ? $context['default_message'] : $context['message'], '</textarea>
 					</p>
 					<ul class="reset">
-						<li><label for="send_pm"><input type="checkbox" name="send_pm" id="send_pm" class="input_check" onclick="if (this.checked && ', $context['total_emails'], ' != 0 && !confirm(\'', $txt['admin_news_cannot_pm_emails_js'], '\')) return false; this.form.parse_html.disabled = this.checked; this.form.send_html.disabled = this.checked; " /> ', $txt['email_as_pms'], '</label></li>
-						<li><label for="send_html"><input type="checkbox" name="send_html" id="send_html" class="input_check" onclick="this.form.parse_html.disabled = !this.checked;" /> ', $txt['email_as_html'], '</label></li>
+						<li><label for="send_pm"><input type="checkbox" name="send_pm" id="send_pm" ', !empty($context['send_pm']) ? 'checked="checked"' : '', 'class="input_check" onclick="if (this.checked && ', $context['total_emails'], ' != 0 && !confirm(\'', $txt['admin_news_cannot_pm_emails_js'], '\')) return false; this.form.parse_html.disabled = this.checked; this.form.send_html.disabled = this.checked; " /> ', $txt['email_as_pms'], '</label></li>
+						<li><label for="send_html"><input type="checkbox" name="send_html" id="send_html" ', !empty($context['send_html']) ? 'checked="checked"' : '', 'class="input_check" onclick="this.form.parse_html.disabled = !this.checked;" /> ', $txt['email_as_html'], '</label></li>
 						<li><label for="parse_html"><input type="checkbox" name="parse_html" id="parse_html" checked="checked" disabled="disabled" class="input_check" /> ', $txt['email_parsed_html'], '</label></li>
 					</ul>
 					<p>
+						<input type="submit" onclick="make_preview(); return false;" name="preview" value="', $txt['preview'], '" class="button_submit" />
 						<input type="submit" value="', $txt['sendtopic_send'], '" class="button_submit" />
 					</p>
 				</div>
@@ -219,6 +227,37 @@ function template_email_members_compose()
 			<input type="hidden" name="', $key, '" value="', implode(($key == 'emails' ? ';' : ','), $values), '" />';
 
 	echo '
+		<script type="text/javascript"><!-- // --><![CDATA[
+			function make_preview ()
+			{
+				$("#preview_box").css({display: \'\'});
+				$("#subject_preview").html(\'', $txt['preview_fetch'], '\');
+				$("#message_preview").html(\'', $txt['preview_fetch'], '\');
+				$.ajax({
+					type: "POST",
+					url: "' . $scripturl . '?action=xmlhttp;sa=previews;xml",
+					data: {
+						item: "newsletterpreview",
+						message: $("#message").val(),
+						subject: $("#subject").val(),
+						send_html: $("#send_html").is(\':checked\') ? 1 : 0,
+						send_pm: $("#send_pm").is(\':checked\') ? 1 : 0,
+					},
+					context: document.body,
+					success: function(request){
+
+						if ($(request).find(\'[type="subject_preview"]\').text() == \'\')
+							$("#subject_preview").html($(request).find("subject").text());
+						else
+							$("#subject_preview").html($(request).find(\'[type="subject_preview"]\').text()).css({color: "red"});
+						if ($(request).find(\'[type="message_preview"]\').text() == \'\')
+							$("#message_preview").html($(request).find("message").text());
+						else
+							$("#message_preview").html($(request).find(\'[type="message_preview"]\').text()).css({color: "red"});
+					},
+				});
+			}
+		// ]]></script>
 		</form>
 	</div>
 	<br class="clear" />';

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

@@ -134,6 +134,7 @@ $txt['member_id'] = 'ID';
 $txt['unknown'] = 'unknown';
 $txt['security_wrong'] = 'Administration login attempt!' . "\n" . 'Referer: %1$s' . "\n" . 'User agent: %2$s' . "\n" . 'IP: %3$s';
 
+$txt['email_preview_warning'] = 'The preview is not 100% accurate. In order to preserve the functionality of the page only the basic html tags are represented';
 $txt['email_as_html'] = 'Send in HTML format.  (with this you can put normal HTML in the email.)';
 $txt['email_parsed_html'] = 'Add &lt;br /&gt;s and &amp;nbsp;s to this message.';
 $txt['email_variables'] = 'In this message you can use a few &quot;variables&quot;.  Click <a href="' . $scripturl . '?action=helpadmin;help=emailmembers" onclick="return reqWin(this.href);" class="help">here</a> for more information.';