Browse Source

NEW Mutualize code to manage email substitution variables. Show
available variables into page to edit email templates.

Laurent Destailleur 9 years ago
parent
commit
446a080c75

+ 13 - 3
htdocs/admin/dict.php

@@ -405,7 +405,7 @@ $tabhelp[21] = array('code'=>$langs->trans("EnterAnyCode"));
 $tabhelp[22] = array('code'=>$langs->trans("EnterAnyCode"));
 $tabhelp[23] = array();
 $tabhelp[24] = array('code'=>$langs->trans("EnterAnyCode"));
-$tabhelp[25] = array('type_template'=>$langs->trans("TemplateForElement"),'private'=>$langs->trans("TemplateIsVisibleByOwnerOnly"), 'position'=>$langs->trans("PositionIntoComboList"));
+$tabhelp[25] = array('topic'=>$langs->trans('SeeSubstitutionVars'),'content'=>$langs->trans('SeeSubstitutionVars'),'type_template'=>$langs->trans("TemplateForElement"),'private'=>$langs->trans("TemplateIsVisibleByOwnerOnly"), 'position'=>$langs->trans("PositionIntoComboList"));
 $tabhelp[26] = array('code'=>$langs->trans("EnterAnyCode"));
 $tabhelp[27] = array('code'=>$langs->trans("EnterAnyCode"));
 $tabhelp[28] = array('affect'=>$langs->trans("FollowedByACounter"),'delay'=>$langs->trans("MinimumNoticePeriod"), 'newByMonth'=>$langs->trans("NbAddedAutomatically"));
@@ -493,7 +493,7 @@ if ($id == 11)
 }
 if ($id == 25)
 {
-	// We save list of template type Dolibarr can manage. This list can found by a grep into code on "->param['models']"
+	// We save list of template email Dolibarr can manage. This list can found by a grep into code on "->param['models']"
 	$elementList = array();
 	if ($conf->propal->enabled) $elementList['propal_send']=$langs->trans('MailToSendProposal');
 	if ($conf->commande->enabled) $elementList['order_send']=$langs->trans('MailToSendOrder');
@@ -1017,8 +1017,18 @@ if ($id)
         }
         print '</td>';
         print "</tr>";
+        
+        if ($tabname[$id] == MAIN_DB_PREFIX.'c_email_templates')
+        {
+        	print '<tr><td colspan="8">* '.$langs->trans("AvailableVariables").": ";
+        	require_once DOL_DOCUMENT_ROOT.'/core/class/html.formmail.class.php';
+        	$formmail=new FormMail($db);
+        	$tmp=$formmail->getAvailableSubstitKey('form');
+        	print join(', ', $tmp);
+        	print '</td></tr>';
+        }
 
-        $colspan=count($fieldlist)+2;
+        $colspan=count($fieldlist)+3;
         if ($id == 4) $colspan++;
 
         if (! empty($alabelisused) && $id != 25)  // If there is one label among fields, we show legend of *

+ 12 - 21
htdocs/comm/mailing/card.php

@@ -32,6 +32,7 @@ require_once DOL_DOCUMENT_ROOT.'/core/class/CMailFile.class.php';
 require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
 require_once DOL_DOCUMENT_ROOT.'/comm/mailing/class/mailing.class.php';
 require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php';
+require_once DOL_DOCUMENT_ROOT.'/core/class/html.formmail.class.php';
 require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
 
 $langs->load("mails");
@@ -55,27 +56,7 @@ $extralabels=$extrafields->fetch_name_optionals_label($object->table_element);
 $hookmanager->initHooks(array('mailingcard','globalcard'));
 
 // Array of possible substitutions (See also file mailing-send.php that should manage same substitutions)
-$object->substitutionarray=array(
-    '__ID__' => 'IdRecord',
-    '__EMAIL__' => 'EMail',
-    '__LASTNAME__' => 'Lastname',
-    '__FIRSTNAME__' => 'Firstname',
-    '__MAILTOEMAIL__' => 'TagMailtoEmail',
-    '__OTHER1__' => 'Other1',
-    '__OTHER2__' => 'Other2',
-    '__OTHER3__' => 'Other3',
-    '__OTHER4__' => 'Other4',
-    '__OTHER5__' => 'Other5',
-    '__SIGNATURE__' => 'TagSignature',
-    '__CHECK_READ__' => 'TagCheckMail',
-	'__UNSUBSCRIBE__' => 'TagUnsubscribe'
-	//,'__PERSONALIZED__' => 'Personalized'	// Hidden because not used yet
-);
-if (! empty($conf->paypal->enabled) && ! empty($conf->global->PAYPAL_SECURITY_TOKEN))
-{
-	$object->substitutionarray['__SECUREKEYPAYPAL__']='SecureKeyPaypal';
-	if (! empty($conf->global->PAYPAL_SECURITY_TOKEN_UNIQUE)) $object->substitutionarray['__SECUREKEYPAYPAL_MEMBER__']='SecureKeyPaypalUniquePerMember';
-}
+$object->substitutionarray=FormMail::getAvailableSubstitKey('emailing');
 
 $object->substitutionarrayfortest=array(
     '__ID__' => 'TESTIdRecord',
@@ -243,8 +224,18 @@ if (empty($reshook))
 						if (! empty($conf->paypal->enabled) && ! empty($conf->global->PAYPAL_SECURITY_TOKEN))
 						{
 							$substitutionarray['__SECUREKEYPAYPAL__']=dol_hash($conf->global->PAYPAL_SECURITY_TOKEN, 2);
+
 							if (empty($conf->global->PAYPAL_SECURITY_TOKEN_UNIQUE)) $substitutionarray['__SECUREKEYPAYPAL_MEMBER__']=dol_hash($conf->global->PAYPAL_SECURITY_TOKEN, 2);
 							else $substitutionarray['__SECUREKEYPAYPAL_MEMBER__']=dol_hash($conf->global->PAYPAL_SECURITY_TOKEN . 'membersubscription' . $obj->source_id, 2);
+
+							if (empty($conf->global->PAYPAL_SECURITY_TOKEN_UNIQUE)) $substitutionarray['__SECUREKEYPAYPAL_ORDER__']=dol_hash($conf->global->PAYPAL_SECURITY_TOKEN, 2);
+							else $substitutionarray['__SECUREKEYPAYPAL_ORDER__']=dol_hash($conf->global->PAYPAL_SECURITY_TOKEN . 'order' . $obj->source_id, 2);
+							
+							if (empty($conf->global->PAYPAL_SECURITY_TOKEN_UNIQUE)) $substitutionarray['__SECUREKEYPAYPAL_INVOICE__']=dol_hash($conf->global->PAYPAL_SECURITY_TOKEN, 2);
+							else $substitutionarray['__SECUREKEYPAYPAL_INVOICE__']=dol_hash($conf->global->PAYPAL_SECURITY_TOKEN . 'invoice' . $obj->source_id, 2);
+							
+							if (empty($conf->global->PAYPAL_SECURITY_TOKEN_UNIQUE)) $substitutionarray['__SECUREKEYPAYPAL_CONTRACTLINE__']=dol_hash($conf->global->PAYPAL_SECURITY_TOKEN, 2);
+							else $substitutionarray['__SECUREKEYPAYPAL_CONTRACTLINE__']=dol_hash($conf->global->PAYPAL_SECURITY_TOKEN . 'contractline' . $obj->source_id, 2);
 						}
 						$substitutionisok=true;
 	                    complete_substitutions_array($substitutionarray, $langs);

+ 1 - 6
htdocs/comm/propal.php

@@ -2445,13 +2445,8 @@ if ($action == 'create')
 		$formmail->withcancel = 1;
 
 		// Tableau des substitutions
+		$formmail->setSubstitFromObject($object);
 		$formmail->substit['__PROPREF__'] = $object->ref;
-		$formmail->substit['__SIGNATURE__'] = $user->signature;
-		$formmail->substit['__REFCLIENT__'] = $object->ref_client;
-		$formmail->substit['__THIRDPARTY_NAME__'] = $object->thirdparty->name;
-		$formmail->substit['__PROJECT_REF__'] = (is_object($object->projet)?$object->projet->ref:'');
-		$formmail->substit['__PERSONALIZED__'] = '';
-		$formmail->substit['__CONTACTCIVNAME__'] = '';
 
 		// Find the good contact adress
 		$custcontact = '';

+ 1 - 6
htdocs/commande/card.php

@@ -2667,13 +2667,8 @@ if ($action == 'create' && $user->rights->commande->creer)
 			$formmail->withdeliveryreceipt = 1;
 			$formmail->withcancel = 1;
 			// Tableau des substitutions
+			$formmail->setSubstitFromObject($object);
 			$formmail->substit ['__ORDERREF__'] = $object->ref;
-			$formmail->substit ['__SIGNATURE__'] = $user->signature;
-			$formmail->substit ['__REFCLIENT__'] = $object->ref_client;
-			$formmail->substit ['__THIRDPARTY_NAME__'] = $object->thirdparty->name;
-			$formmail->substit ['__PROJECT_REF__'] = (is_object($object->projet)?$object->projet->ref:'');
-			$formmail->substit ['__PERSONALIZED__'] = '';
-			$formmail->substit ['__CONTACTCIVNAME__'] = '';
 
 			$custcontact = '';
 			$contactarr = array();

+ 2 - 8
htdocs/compta/facture.php

@@ -4150,14 +4150,8 @@ else if ($id > 0 || ! empty($ref))
 		$formmail->withdeliveryreceipt = 1;
 		$formmail->withcancel = 1;
 		// Tableau des substitutions
-		$formmail->substit['__REF__'] = $object->ref;
-		$formmail->substit['__SIGNATURE__'] = $user->signature;
-		$formmail->substit['__REFCLIENT__'] = $object->ref_client;
-		$formmail->substit['__THIRDPARTY_NAME__'] = $object->thirdparty->name;
-		$formmail->substit['__PROJECT_REF__'] = (is_object($object->projet)?$object->projet->ref:'');
-		$formmail->substit['__PROJECT_NAME__'] = (is_object($object->projet)?$object->projet->title:'');
-		$formmail->substit['__PERSONALIZED__'] = '';
-		$formmail->substit['__CONTACTCIVNAME__'] = '';
+		$formmail->setSubstitFromObject($object);
+		$formmail->substit['__INVREF__'] = $object->ref;
 
 		// Find the good contact adress
 		$custcontact = '';

+ 89 - 1
htdocs/core/class/html.formmail.class.php

@@ -657,9 +657,10 @@ class FormMail extends Form
 
         			$langs->load('paypal');
 
+        			// Set the paypal message and url link into __PERSONALIZED__ key
         			if ($this->param["models"]=='order_send')
         			{
-        				$url=getPaypalPaymentUrl(0,'order',$this->substit['__ORDERREF__']);
+        				$url=getPaypalPaymentUrl(0,'order',$this->substit['__ORDERREF__']?$this->substit['__ORDERREF__']:$this->substit['__REF__']);
         				$this->substit['__PERSONALIZED__']=str_replace('\n',"\n",$langs->transnoentitiesnoconv("PredefinedMailContentLink",$url));
         			}
         			if ($this->param["models"]=='facture_send')
@@ -908,8 +909,95 @@ class FormMail extends Form
 			return -1;
 		}
 	}
+	
+	
+	
+	/**
+	 * Set substit array from object
+	 * 
+	 * @param	Object	$object		Object to use
+	 * @return	void
+	 */
+	function setSubstitFromObject($object)
+	{
+		$this->substit['__REF__'] = $object->ref;
+		$this->substit['__SIGNATURE__'] = $user->signature;
+		$this->substit['__REFCLIENT__'] = $object->ref_client;
+		$this->substit['__THIRDPARTY_NAME__'] = $object->thirdparty->name;
+		$this->substit['__PROJECT_REF__'] = (is_object($object->projet)?$object->projet->ref:'');
+		$this->substit['__PROJECT_NAME__'] = (is_object($object->projet)?$object->projet->title:'');
+		$this->substit['__PERSONALIZED__'] = '';
+		$this->substit['__CONTACTCIVNAME__'] = '';	// Will be replace just before sending
+	}
+	
+	/**
+	 * Set substit array from object
+	 * 
+	 * @param	Object	$object		Object to use
+	 * @param	string	$mode		'form' or 'emailing'
+	 * @return	void
+	 */
+	function getAvailableSubstitKey($mode='form')
+	{
+		global $conf;
+		
+		$vars=array();
+		
+		if ($mode == 'form')
+		{
+			$vars=array(
+				'__REF__', 
+				'__REFCLIENT__', 
+				'__THIRDPARTY_NAME__', 
+				'__PROJECT_REF__', 
+				'__PROJECT_NAME__',
+				'__CONTACTCIVNAME__',
+				'__PERSONALIZED__',			// Paypal link is added here
+				'__SIGNATURE__', 
+			);
+		}
+		if ($mode == 'emailing')
+		{
+			// For mass emailing, we have different keys
+			$vars=array(
+			    '__ID__' => 'IdRecord',
+			    '__EMAIL__' => 'EMail',
+			    '__LASTNAME__' => 'Lastname',
+			    '__FIRSTNAME__' => 'Firstname',
+			    '__MAILTOEMAIL__' => 'TagMailtoEmail',
+			    '__OTHER1__' => 'Other1',
+			    '__OTHER2__' => 'Other2',
+			    '__OTHER3__' => 'Other3',
+			    '__OTHER4__' => 'Other4',
+			    '__OTHER5__' => 'Other5',
+			    '__SIGNATURE__' => 'TagSignature',
+			    '__CHECK_READ__' => 'TagCheckMail',
+				'__UNSUBSCRIBE__' => 'TagUnsubscribe'
+				//,'__PERSONALIZED__' => 'Personalized'	// Hidden because not used yet in mass emailing
+			);
+			if (! empty($conf->paypal->enabled) && ! empty($conf->global->PAYPAL_SECURITY_TOKEN))
+			{
+				$vars['__SECUREKEYPAYPAL__']='SecureKeyPaypal';
+				if (! empty($conf->global->PAYPAL_SECURITY_TOKEN_UNIQUE))
+				{
+					if ($conf->adherent->enabled) $vars['__SECUREKEYPAYPAL_MEMBER__']='SecureKeyPaypalUniquePerMember';
+					if ($conf->facture->enabled) $vars['__SECUREKEYPAYPAL_INVOICE__']='SecureKeyPaypalUniquePerInvoice';
+					if ($conf->commande->enabled) $vars['__SECUREKEYPAYPAL_ORDER__']='SecureKeyPaypalUniquePerOrder';
+					if ($conf->contrat->enabled) $vars['__SECUREKEYPAYPAL_CONTRACTLINE__']='SecureKeyPaypalUniquePerContractLine';
+				}
+			}
+			else 
+			{
+				$vars['__SECUREKEYPAYPAL__']='';
+				$vars['__SECUREKEYPAYPAL_MEMBER__']='';
+			}
+		}
+		return $vars;
+	}
+
 }
 
+
 /**
  * ModelMail
  */

+ 1 - 3
htdocs/expedition/card.php

@@ -1895,10 +1895,8 @@ else if ($id || $ref)
 		$formmail->withdeliveryreceipt=1;
 		$formmail->withcancel=1;
 		// Tableau des substitutions
+		$formmail->setSubstitFromObject($object);
 		$formmail->substit['__SHIPPINGREF__']=$object->ref;
-		$formmail->substit['__SIGNATURE__']=$user->signature;
-		$formmail->substit['__PERSONALIZED__']='';
-		$formmail->substit['__CONTACTCIVNAME__']='';
 
 		//Find the good contact adress
 		//Find the good contact adress

+ 1 - 3
htdocs/fichinter/card.php

@@ -1866,10 +1866,8 @@ else if ($id > 0 || ! empty($ref))
 		$formmail->withcancel=1;
 
 		// Tableau des substitutions
+		$formmail->setSubstitFromObject($object);
 		$formmail->substit['__FICHINTERREF__']=$object->ref;
-		$formmail->substit['__SIGNATURE__']=$user->signature;
-		$formmail->substit['__PERSONALIZED__']='';
-		$formmail->substit['__CONTACTCIVNAME__']='';
 
 		//Find the good contact adress
 		$custcontact='';

+ 3 - 7
htdocs/fourn/commande/card.php

@@ -2481,13 +2481,9 @@ elseif (! empty($object->id))
 
 		$object->fetch_projet();
 		// Tableau des substitutions
-		$formmail->substit['__ORDERREF__']=$object->ref;
-		$formmail->substit['__ORDERSUPPLIERREF__']=$object->ref_supplier;
-		$formmail->substit['__THIRDPARTY_NAME__'] = $object->thirdparty->name;
-		$formmail->substit['__PROJECT_REF__'] = (is_object($object->projet)?$object->projet->ref:'');
-		$formmail->substit['__SIGNATURE__']=$user->signature;
-		$formmail->substit['__PERSONALIZED__']='';
-		$formmail->substit['__CONTACTCIVNAME__']='';
+		$formmail->setSubstitFromObject($object);
+		$formmail->substit['__ORDERSUPPLIERREF__']=$object->ref_supplier;	// For backward compatibility
+		$formmail->substit['__SUPPLIERORDERREF__']=$object->ref_supplier;
 
 		//Find the good contact adress
 		$custcontact='';

+ 3 - 5
htdocs/fourn/facture/card.php

@@ -2474,11 +2474,9 @@ else
             $formmail->withbody=1;
             $formmail->withdeliveryreceipt=1;
             $formmail->withcancel=1;
-            // Tableau des substitutions
-            $formmail->substit['__REF__']=$object->ref;
-            $formmail->substit['__SIGNATURE__']=$user->signature;
-            $formmail->substit['__PERSONALIZED__']='';
-            $formmail->substit['__CONTACTCIVNAME__']='';
+			// Tableau des substitutions
+			$formmail->setSubstitFromObject($object);
+            $formmail->substit['__SUPPLIERINVREF__']=$object->ref;
 
             //Find the good contact adress
             $custcontact='';

+ 2 - 1
htdocs/langs/en_US/admin.lang

@@ -1714,4 +1714,5 @@ ExampleOfNewsMessageForMajorRelease=Dolibarr ERP & CRM %s is available. Version
 ExampleOfNewsMessageForMaintenanceRelease=Dolibarr ERP & CRM %s is available. Version %s is a maintenance version, so it contains only fixes of bugs. We recommend everybody using an older version to upgrade to this one. As any maintenance release, no new features, nor data structure change is present into this version. You can download it from the download area of http://www.dolibarr.org portal (subdirectory Stable versions). You can read <a href="https://github.com/Dolibarr/dolibarr/blob/develop/ChangeLog">ChangeLog</a> for complete list of changes.
 MultiPriceRuleDesc=When option "Several level of prices per product/service" is on, you can define different prices (one per price level) for each product. To save you time, you can enter here rule to have price for each level autocalculated according to price of first level, so you will have to enter only price for first level on each product. This page is here to save you time and can be usefull only if your prices for each leve are relative to first level. You can ignore this page in most cases.
 ModelModulesProduct=Templates for product documents
-ToGenerateCodeDefineAutomaticRuleFirst=To be able to generate automatically codes, you must first define a manager to auto define barcode number.
+ToGenerateCodeDefineAutomaticRuleFirst=To be able to generate automatically codes, you must first define a manager to auto define barcode number.
+SeeSubstitutionVars=See * note for list of possible substitution variables

+ 1 - 0
htdocs/langs/en_US/main.lang

@@ -25,6 +25,7 @@ FormatDateHourTextShort=%b %d, %Y, %I:%M %p
 FormatDateHourText=%B %d, %Y, %I:%M %p
 DatabaseConnection=Database connection
 NoTemplateDefined=No template defined for this email type
+AvailableVariables=Available substitution variables
 NoTranslation=No translation
 NoRecordFound=No record found
 NoError=No error

+ 8 - 8
htdocs/paypal/lib/paypal.lib.php

@@ -249,8 +249,8 @@ function getPaypalPaymentUrl($mode,$type,$ref='',$amount='9.99',$freetag='your_f
             else
             {
                 $out.='&securekey='.($mode?'<font color="#666666">':'');
-                if ($mode == 1) $out.="hash('".$conf->global->PAYPAL_SECURITY_TOKEN."' + 'order' + order_ref)";
-                if ($mode == 0) $out.= dol_hash($conf->global->PAYPAL_SECURITY_TOKEN . 'order' . $ref, 2);
+                if ($mode == 1) $out.="hash('".$conf->global->PAYPAL_SECURITY_TOKEN."' + '".$type."' + order_ref)";
+                if ($mode == 0) $out.= dol_hash($conf->global->PAYPAL_SECURITY_TOKEN . $type . $ref, 2);
                 $out.=($mode?'</font>':'');
             }
         }
@@ -267,8 +267,8 @@ function getPaypalPaymentUrl($mode,$type,$ref='',$amount='9.99',$freetag='your_f
             else
             {
                 $out.='&securekey='.($mode?'<font color="#666666">':'');
-                if ($mode == 1) $out.="hash('".$conf->global->PAYPAL_SECURITY_TOKEN."' + 'invoice' + invoice_ref)";
-                if ($mode == 0) $out.= dol_hash($conf->global->PAYPAL_SECURITY_TOKEN . 'invoice' . $ref, 2);
+                if ($mode == 1) $out.="hash('".$conf->global->PAYPAL_SECURITY_TOKEN."' + '".$type."' + invoice_ref)";
+                if ($mode == 0) $out.= dol_hash($conf->global->PAYPAL_SECURITY_TOKEN . $type . $ref, 2);
                 $out.=($mode?'</font>':'');
             }
         }
@@ -285,8 +285,8 @@ function getPaypalPaymentUrl($mode,$type,$ref='',$amount='9.99',$freetag='your_f
             else
             {
                 $out.='&securekey='.($mode?'<font color="#666666">':'');
-                if ($mode == 1) $out.="hash('".$conf->global->PAYPAL_SECURITY_TOKEN."' + 'contactline' + contractline_ref)";
-                if ($mode == 0) $out.= dol_hash($conf->global->PAYPAL_SECURITY_TOKEN . 'contractline' . $ref, 2);
+                if ($mode == 1) $out.="hash('".$conf->global->PAYPAL_SECURITY_TOKEN."' + '".$type."' + contractline_ref)";
+                if ($mode == 0) $out.= dol_hash($conf->global->PAYPAL_SECURITY_TOKEN . $type . $ref, 2);
                 $out.=($mode?'</font>':'');
             }
         }
@@ -303,8 +303,8 @@ function getPaypalPaymentUrl($mode,$type,$ref='',$amount='9.99',$freetag='your_f
             else
             {
                 $out.='&securekey='.($mode?'<font color="#666666">':'');
-                if ($mode == 1) $out.="hash('".$conf->global->PAYPAL_SECURITY_TOKEN."' + 'membersubscription' + member_ref)";
-                if ($mode == 0) $out.= dol_hash($conf->global->PAYPAL_SECURITY_TOKEN . 'membersubscription' . $ref, 2);
+                if ($mode == 1) $out.="hash('".$conf->global->PAYPAL_SECURITY_TOKEN."' + '".$type."' + member_ref)";
+                if ($mode == 0) $out.= dol_hash($conf->global->PAYPAL_SECURITY_TOKEN . $type . $ref, 2);
                 $out.=($mode?'</font>':'');
             }
         }

+ 2 - 6
htdocs/supplier_proposal/card.php

@@ -1930,12 +1930,8 @@ if ($action == 'create')
 		$formmail->withcancel = 1;
 
 		// Tableau des substitutions
-		$formmail->substit['__ASKREF__'] = $object->ref;
-		$formmail->substit['__SIGNATURE__'] = $user->signature;
-		$formmail->substit['__THIRDPARTY_NAME__'] = $object->thirdparty->name;
-		$formmail->substit['__PROJECT_REF__'] = (is_object($object->projet)?$object->projet->ref:'');
-		$formmail->substit['__CONTACTCIVNAME__'] = '';
-		$formmail->substit['__PERSONALIZED__'] = '';
+		$formmail->setSubstitFromObject($object);
+		$formmail->substit['__SUPPLIERPROPREF__'] = $object->ref;
 
 		// Tableau des parametres complementaires
 		$formmail->param['action'] = 'send';