Sfoglia il codice sorgente

Should fix bug when using email editor see #5356

Julio Montoya 12 anni fa
parent
commit
c7580f24f9

+ 33 - 58
main/inc/email_editor.php

@@ -6,91 +6,66 @@
  * It can be called from the JavaScript library email_links.lib.php which
  * overtakes the mailto: links to use the internal interface instead.
  * @author	Yannick Warnier <ywarnier@beeznest.org>
+ * @author Julio Montoya <gugli100@gmail.com> Updating form with formvalidator
  */
 
 // name of the language file that needs to be included
 
 use \ChamiloSession as Session;
 
-$language_file = "index";
+$language_file = array('index', 'admin', 'registration');
 
 require_once '../inc/global.inc.php';
-require_once api_get_path(LIBRARY_PATH).'usermanager.lib.php';
 require_once api_get_path(LIBRARY_PATH).'mail.lib.inc.php';
+
 if (empty($_user['user_id'])) {
 	api_not_allowed(true);
 }
 
-//api_protect_course_script(); //not a course script, so no protection
-
 if (empty($_SESSION['origin_url'])) {
 	$origin_url = $_SERVER['HTTP_REFERER'];
 	Session::write('origin_url',$origin_url);
 }
 
-/* Process the form and redirect to origin */
-if (!empty($_POST['submit_email']) && !empty($_POST['email_title']) && !empty($_POST['email_text'])) {
-	$text = Security::remove_XSS($_POST['email_text'])."\n\n---\n".get_lang('EmailSentFromDokeos')." ".api_get_path(WEB_PATH);
+$form = new FormValidator('email_editor', 'post');
+$form->addElement('hidden', 'dest');
+$form->addElement('text', 'email_address', get_lang('EmailDestination'));
+$form->addElement('text', 'email_title', get_lang('EmailTitle'));
+$form->freeze('email_address');
+$form->addElement('textarea', 'email_text', get_lang('EmailText'));
+
+$form->addRule('email_address', get_lang('ThisFieldIsRequired'), 'required');
+$form->addRule('email_title', get_lang('ThisFieldIsRequired'), 'required');
+$form->addRule('email_text', get_lang('ThisFieldIsRequired'), 'required');
+$form->addRule('email_address', get_lang('EmailWrong'), 'email');
+
+$form->addElement('button', 'submit', get_lang('SendMail'));
+        
+$defaults = array(  'dest' => Security::remove_XSS($_REQUEST['dest']),
+                    'email_address' => Security::remove_XSS($_REQUEST['dest']),
+                    'email_title' => Security::remove_XSS($_POST['email_title']),
+                    'email_text' => Security::remove_XSS($_POST['email_text'])
+    
+);
+$form->setDefaults($defaults);
+
+if ($form->validate()) {
+    $text = Security::remove_XSS($_POST['email_text'])."\n\n---\n".get_lang('EmailSentFromDokeos')." ".api_get_path(WEB_PATH);
 	$email_administrator=Security::remove_XSS($_POST['dest']);
 	$user_id=api_get_user_id();
 	$title=Security::remove_XSS($_POST['email_title']);
 	$content=Security::remove_XSS($_POST['email_text']);
-	if (!empty($_user['mail'])) {
-		api_mail('',$email_administrator,$title,$text,api_get_person_name($_user['firstname'],$_user['lastname']), $_user['mail']);
+	if (!empty($_user['mail'])) {        
+		api_mail_html('',$email_administrator,$title,$text,api_get_person_name($_user['firstname'],$_user['lastname']), $_user['mail']);
 		UserManager::send_message_in_outbox ($email_administrator,$user_id,$title, $content);
 	} else {
-		api_mail('',$email_administrator,$title,$text,get_lang('Anonymous'));
+		api_mail_html('',$email_administrator,$title,$text,get_lang('Anonymous'));
 	}
 	$orig = $_SESSION['origin_url'];
 	Session::erase('origin_url');
 	header('location:'.$orig);
+    exit;
 }
-
-/* Header */
 Display::display_header(get_lang('SendEmail'));
-
-?>
-<table border="0">
-<form action="" method="POST">
-<input type="hidden" name="dest" value="<?php echo Security::remove_XSS($_REQUEST['dest']);?>" />
-	<tr>
-		<td>
-			<label for="email_address"><?php echo get_lang('EmailDestination');?></label>
-		</td>
-		<td>
-			<span id="email_address"><?php echo Security::remove_XSS($_REQUEST['dest']); ?></span>
-		</td>
-	</tr>
-	<tr>
-		<td>
-			<label for="email_title"><?php echo get_lang('EmailTitle');?></label>
-		</td>
-		<td>
-			<input name="email_title" id="email_title" value="<?php echo Security::remove_XSS($_POST['email_title']);?>" size="60"></input>
-		</td>
-	</tr>
-	<tr>
-		<td valign="top">
-			<label for="email_text"><?php echo get_lang('EmailText');?></label>
-		</td>
-		<td>
-			<?php
-			  echo '<textarea id="email_text" name="email_text" rows="10" cols="80">'.Security::remove_XSS($_POST['email_text']).'</textarea>';
-			  //htmlarea is not used otherwise we have to deal with HTML e-mail and all the related probs
-			  //api_disp_html_area('email_text',$_POST['email_text'],'250px');
-			?>
-		</td>
-	</tr>
-	<tr>
-		<td colspan="2">
-			<button class="save" type="submit" name="submit_email" value="<?php echo get_lang('SendMail');?>"><?php echo get_lang('SendMail');?></button>
-		</td>
-	</tr>
-</form>
-</table>
-
-<?php
-
-/* Footer */
-Display::display_footer();
-?>
+$form->display();
+Display::display_footer();

+ 11 - 14
main/inc/lib/template.lib.php

@@ -332,7 +332,7 @@ class Template {
     private function set_system_parameters() {
         global $_configuration;
 
-        //Setting app paths		
+        //Setting app paths/URLs
         $_p = array('web' => api_get_path(WEB_PATH),
             'web_course' => api_get_path(WEB_COURSE_PATH),
             'web_main' => api_get_path(WEB_CODE_PATH),
@@ -449,18 +449,16 @@ class Template {
         $this->set_theme();
 
         //Extra JS files
-
+        
         $js_files = array(
             'modernizr.js',
             'jquery.min.js',
             'chosen/chosen.jquery.min.js',
-            'thickbox.js',
-            //'dtree/dtree.js',
-            'email_links.lib.js.php',
+            'thickbox.js',            
             'bootstrap/bootstrap.js',
         );
         
-        
+      
         if (api_is_global_chat_enabled()) {            
             //Do not include the global chat in LP
             if ($this->show_learnpath == false && $this->show_footer == true && $this->hide_global_chat == false) {
@@ -475,18 +473,19 @@ class Template {
         if (api_get_setting('include_asciimathml_script') == 'true') {
             $js_files[] = 'asciimath/ASCIIMathML.js';
         }
-
-        $js_file_to_string = '';
-        
         
+        $js_file_to_string = '';
         
         foreach ($js_files as $js_file) {
             $js_file_to_string .= api_get_js($js_file);
         }
         
-
+        //Loading email_editor js
+        if (!api_is_anonymous() && api_get_setting('allow_email_editor') == 'true') {
+            $js_file_to_string .= $this->fetch('default/mail_editor/email_link.js.tpl');            
+        }
+        
         //Extra CSS files
-
         $css_files = array(
             api_get_path(WEB_LIBRARY_PATH) . 'javascript/thickbox.css',
             api_get_path(WEB_LIBRARY_PATH) . 'javascript/chosen/chosen.css'
@@ -500,13 +499,11 @@ class Template {
         if (api_is_global_chat_enabled()) {
             $css_files[] = api_get_path(WEB_LIBRARY_PATH) . 'javascript/chat/css/chat.css';
         }
-
         
         $css_file_to_string = '';
         foreach ($css_files as $css_file) {
             $css_file_to_string .= api_get_css($css_file);
-        }
-        
+        }        
 
         // @todo move this somewhere else. Special fix when using tablets in order to see the text near icons
         if (SHOW_TEXT_NEAR_ICONS == true) {

+ 29 - 35
main/inc/lib/javascript/email_links.lib.js.php → main/template/default/mail_editor/email_link.js.tpl

@@ -1,28 +1,23 @@
-<?php
+<script>
 /* For licensing terms, see /license.txt */
-/**
- * Pseudo JavaScript library to deal with event handlers.
+/*
+ * JS library to deal with event handlers.
  * This script needs to be included from a script where the global include file has already been loaded.
  * @package chamilo.inc.lib.javascript
- * @author Yannick Warnier <ywarnier@beeznest.org>
+ * @author Yannick Warnier
+ * @author Julio Montoya - Adding twig support
  */
-/**
- * If the user is not logged in, don't define anything, so the normal
- * handling of mailto link can proceed
- */
-if(!empty($_user['user_id']) AND string_2_boolean(api_get_setting('allow_email_editor'))){
-?>
-<script type="text/javascript">
-/**
+
+/*
  * Assigns any event handler to any element
  * @param	object	Element on which the event is added
  * @param	string	Name of event
  * @param	string	Function to trigger on event
  * @param	boolean	Capture the event and prevent
  */
-function addEvent(elm, evType, fn, useCapture)
-{ //by Scott Andrew
-	if(elm.addEventListener){
+
+function addEvent(elm, evType, fn, useCapture) {
+    if (elm.addEventListener){
 		elm.addEventListener(evType, fn, useCapture);
 		return true;
 	} else if(elm.attachEvent) {
@@ -32,47 +27,46 @@ function addEvent(elm, evType, fn, useCapture)
 		elm['on' + evType] = fn;
 	}
 }
-/**
+
+/*
  * Adds the event listener
  */
 function addListeners(e) {
-	var my_links = document.getElementsById('clickable_email_link');
-	for(var i=0;i < my_links.length;i++)
-	{
+	var my_links = $('.clickable_email_link');
+    console.log(my_links);
+	for(var i=0;i < my_links.length;i++) {
 		addEvent(my_links[i],'click',loadEmailEditor,false);
 	}
 }
-/**
+
+/*
  * Loads a specific page on event triggering
  */
-function loadEmailEditor(e)
-{
+function loadEmailEditor(e) {
 	var el;
-	if(window.event && window.event.srcElement)
-	{
+	if(window.event && window.event.srcElement) {
 		el = window.event.srcElement;
 	}
-	if (e && e.target)
-	{
+	if (e && e.target) {
 		el = e.target;
 	}
-	if(!el)
-	{
+	if(!el) {
 		return;
 	}
 	//el is now my link object, so I can get el.href here to load the new window
 	var link = el.href.replace('mailto:','');
-	document.location = "<?php echo api_get_path(WEB_CODE_PATH);?>inc/email_editor.php?dest=" + link;
+	document.location = "{{ _p.web_main }}inc/email_editor.php?dest=" + link;
 	//cancel default link action
-	if(window.event && window.event.returnValue){
+	if (window.event && window.event.returnValue){
 		window.event.returnValue = false;
 	}
 	if(e && e.preventDefault){
 		e.preventDefault();
 	}
 }
-addEvent(window,'load',addListeners,false);
-</script>
-<?php
-}
-?>
+
+$(document).ready(function() {
+    addEvent(window,'load',addListeners,false);
+});
+
+</script>