2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4 * SugarCRM Community Edition is a customer relationship management program developed by
5 * SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
7 * This program is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU Affero General Public License version 3 as published by the
9 * Free Software Foundation with the addition of the following permission added
10 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
11 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
12 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
19 * You should have received a copy of the GNU Affero General Public License along with
20 * this program; if not, see http://www.gnu.org/licenses or write to the Free
21 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
25 * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
27 * The interactive user interfaces in modified source and object code versions
28 * of this program must display Appropriate Legal Notices, as required under
29 * Section 5 of the GNU Affero General Public License version 3.
31 * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
32 * these Appropriate Legal Notices must retain the display of the "Powered by
33 * SugarCRM" logo. If the display of the logo is not reasonably feasible for
34 * technical reasons, the Appropriate Legal Notices must display the words
35 * "Powered by SugarCRM".
36 ********************************************************************************/
38 /*********************************************************************************
40 * Description: TODO: To be written.
41 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
42 * All Rights Reserved.
43 * Contributor(s): ______________________________________..
44 ********************************************************************************/
51 // EmailTemplate is used to store email email_template information.
52 class EmailTemplate extends SugarBean {
53 var $field_name_map = array();
58 var $modified_user_id;
61 var $modified_by_name;
62 var $assigned_user_id;
63 var $assigned_user_name;
73 var $table_name = "email_templates";
74 var $object_name = "EmailTemplate";
75 var $module_dir = "EmailTemplates";
76 var $new_schema = true;
77 // This is used to retrieve related fields from form posts.
78 var $additional_column_fields = array(
80 // add fields here that would not make sense in an email template
81 var $badFields = array(
82 'account_description',
88 'opportunity_role_id',
89 'opportunity_role_fields',
104 'receive_notifications',
110 'accept_status_name',
113 function EmailTemplate() {
118 * Generates the extended field_defs for creating macros
119 * @param object $bean SugarBean
120 * @param string $prefix "contact_", "user_" etc.
123 function generateFieldDefsJS() {
124 global $current_user;
130 $contact = new Contact();
131 $account = new Account();
133 $prospect = new Prospect();
136 $loopControl = array(
138 'Contacts' => $contact,
140 'Prospects' => $prospect,
143 'Accounts' => $account,
146 'Users' => $current_user,
151 'Contacts' => 'contact_',
152 'Accounts' => 'account_',
153 'Users' => 'contact_user_',
156 $collection = array();
157 foreach($loopControl as $collectionKey => $beans) {
158 $collection[$collectionKey] = array();
159 foreach($beans as $beankey => $bean) {
161 foreach($bean->field_defs as $key => $field_def) {
162 if( ($field_def['type'] == 'relate' && empty($field_def['custom_type'])) ||
163 ($field_def['type'] == 'assigned_user_name' || $field_def['type'] =='link') ||
164 ($field_def['type'] == 'bool') ||
165 (in_array($field_def['name'], $this->badFields)) ) {
168 if(!isset($field_def['vname'])) {
171 // valid def found, process
172 $optionKey = strtolower("{$prefixes[$collectionKey]}{$key}");
173 $optionLabel = preg_replace('/:$/', "", translate($field_def['vname'], $beankey));
175 foreach ($collection[$collectionKey] as $value){
176 if($value['name']==$optionKey){
182 $collection[$collectionKey][] = array("name" => $optionKey, "value" => $optionLabel);
187 $json = getJSONobj();
188 $ret = "var field_defs = ";
189 $ret .= $json->encode($collection, false);
194 function get_summary_text() {
195 return "$this->name";
198 function create_export_query(&$order_by, &$where) {
199 return $this->create_new_list_query($order_by, $where);
202 function fill_in_additional_list_fields() {
203 $this->fill_in_additional_parent_fields();
206 function fill_in_additional_detail_fields() {
207 if (empty($this->body) && !empty($this->body_html))
209 global $sugar_config;
210 $this->body = strip_tags(html_entity_decode($this->body_html, ENT_COMPAT, $sugar_config['default_charset']));
212 $this->created_by_name = get_assigned_user_name($this->created_by);
213 $this->modified_by_name = get_assigned_user_name($this->modified_user_id);
214 $this->assigned_user_name = get_assigned_user_name($this->assigned_user_id);
215 $this->fill_in_additional_parent_fields();
218 function fill_in_additional_parent_fields() {
221 function get_list_view_data() {
222 global $app_list_strings, $focus, $action, $currentModule;
223 $fields = $this->get_list_view_array();
224 $fields["DATE_MODIFIED"] = substr($fields["DATE_MODIFIED"], 0 , 10);
228 //function all string that match the pattern {.} , also catches the list of found strings.
229 //the cache will get refreshed when the template bean instance changes.
230 //The found url key patterns are replaced with name value pairs provided as function parameter. $tracked_urls.
231 //$url_template is used to construct the url for the email message. the template should have place holder for 1 varaible parameter, represented by %1
232 //$template_text_array is a list of text strings that need to be searched. usually the subject, html body and text body of the email message.
233 //$removeme_url_template, if the url has is_optout property checked then use this template.
234 function parse_tracker_urls($template_text_array,$url_template,$tracked_urls,$removeme_url_template) {
235 global $beanFiles,$beanList, $app_list_strings,$sugar_config;
236 if (!isset($this->parsed_urls))
237 $this->parsed_urls=array();
239 //parse the template and find all the dynamic strings that need replacement.
240 $pattern = '/\{*[^\{\}]*\}/'; // cn: bug 6638, find multibyte strings
241 foreach ($template_text_array as $key=>$template_text) {
242 if (!empty($template_text)) {
243 if(!isset($this->parsed_urls[$key]) || $this->parsed_urls[$key]['text'] != $template_text) {
245 $count=preg_match_all($pattern,$template_text,$matches,PREG_OFFSET_CAPTURE);
246 $this->parsed_urls[$key]=array('matches' => $matches, 'text' => $template_text);
248 $matches=$this->parsed_urls[$key]['matches'];
249 if(!empty($matches[0])) {
250 $count=count($matches[0]);
256 //navigate thru all the matched keys and replace the keys with actual strings.
257 for ($i=($count -1); $i>=0; $i--) {
258 $url_key_name=$matches[0][$i][0];
260 if (!empty($tracked_urls[$url_key_name])) {
261 if ($tracked_urls[$url_key_name]['is_optout']==1){
262 $tracker_url = $removeme_url_template;
264 $tracker_url = sprintf($url_template,$tracked_urls[$url_key_name]['id']);
267 if(!empty($tracker_url) && !empty($template_text) && !empty($matches[0][$i][0]) && !empty($tracked_urls[$matches[0][$i][0]])){
268 $template_text=substr_replace($template_text,$tracker_url,$matches[0][$i][1], strlen($matches[0][$i][0]));
269 $template_text=str_replace($sugar_config['site_url'].'/'.$sugar_config['site_url'],$sugar_config['site_url'],$template_text);
273 $return_array[$key]=$template_text;
276 return $return_array;
279 function parse_email_template($template_text_array, $focus_name, $focus, &$macro_nv) {
282 global $beanFiles, $beanList, $app_list_strings;
284 // generate User instance that owns this "Contact" for contact_user_* macros
286 if(isset($focus->assigned_user_id) && !empty($focus->assigned_user_id)){
287 $user->retrieve($focus->assigned_user_id);
290 if(!isset($this->parsed_entities))
291 $this->parsed_entities=array();
293 //parse the template and find all the dynamic strings that need replacement.
294 // Bug #48111 It's strange why prefix for User module is contact_user (see self::generateFieldDefsJS method)
295 if ($beanList[$focus_name] == 'User')
297 $pattern_prefix = '$contact_user_';
301 $pattern_prefix = '$'.strtolower($beanList[$focus_name]).'_';
303 $pattern_prefix_length = strlen($pattern_prefix);
304 $pattern = '/\\'.$pattern_prefix.'[A-Za-z_0-9]*/';
306 foreach($template_text_array as $key=>$template_text) {
307 if(!isset($this->parsed_entities[$key])) {
309 $count = preg_match_all($pattern, $template_text, $matches, PREG_OFFSET_CAPTURE);
312 for($i=($count -1); $i>=0; $i--) {
313 if(!isset($matches[0][$i][2])) {
314 //find the field name in the bean.
315 $matches[0][$i][2]=substr($matches[0][$i][0],$pattern_prefix_length,strlen($matches[0][$i][0]) - $pattern_prefix_length);
317 //store the localized strings if the field is of type enum..
318 if(isset($focus->field_defs[$matches[0][$i][2]]) && $focus->field_defs[$matches[0][$i][2]]['type']=='enum' && isset($focus->field_defs[$matches[0][$i][2]]['options'])) {
319 $matches[0][$i][3]=$focus->field_defs[$matches[0][$i][2]]['options'];
324 $this->parsed_entities[$key]=$matches;
326 $matches=$this->parsed_entities[$key];
327 if(!empty($matches[0])) {
328 $count=count($matches[0]);
334 for ($i=($count -1); $i>=0; $i--) {
335 $field_name=$matches[0][$i][2];
337 // cn: feel for User object attribute key and assign as found
338 if(strpos($field_name, "user_") === 0) {
339 $userFieldName = substr($field_name, 5);
340 $value = $user->$userFieldName;
341 //_pp($userFieldName."[{$value}]");
343 $value = $focus->{$field_name};
347 if(isset($matches[0][$i][3])) {
348 if(isset($app_list_strings[$matches[0][$i][3]][$value])) {
349 $value=$app_list_strings[$matches[0][$i][3]][$value];
353 //generate name value pair array of macros and corresponding values for the targed.
354 $macro_nv[$matches[0][$i][0]] =$value;
356 $template_text=substr_replace($template_text,$value,$matches[0][$i][1], strlen($matches[0][$i][0]));
359 //parse the template for tracker url strings. patter for these strings in {[a-zA-Z_0-9]+}
361 $return_array[$key]=$template_text;
364 return $return_array;
368 * Convenience method to convert raw value into appropriate type format
369 * @param string $type
370 * @param string $value
373 function _convertToType($type,$value) {
375 case 'currency' : return currency_format_number($value);
376 default: return $value;
381 * Convenience method to parse for user's values in a template
382 * @param array $repl_arr
383 * @param object $user
386 function _parseUserValues($repl_arr, &$user) {
387 foreach($user->field_defs as $field_def) {
388 if(($field_def['type'] == 'relate' && empty($field_def['custom_type'])) || $field_def['type'] == 'assigned_user_name') {
392 if($field_def['type'] == 'enum') {
393 $translated = translate($field_def['options'], 'Users', $user->$field_def['name']);
395 if(isset($translated) && ! is_array($translated)) {
396 $repl_arr["contact_user_".$field_def['name']] = $translated;
397 } else { // unset enum field, make sure we have a match string to replace with ""
398 $repl_arr["contact_user_".$field_def['name']] = '';
401 if(isset($user->$field_def['name'])) {
402 // bug 47647 - allow for fields to translate before adding to template
403 $repl_arr["contact_user_".$field_def['name']] = self::_convertToType($field_def['type'],$user->$field_def['name']);
405 $repl_arr["contact_user_".$field_def['name']] = "";
414 function parse_template_bean($string, $bean_name, &$focus) {
415 global $current_user;
416 global $beanFiles, $beanList;
419 // cn: bug 9277 - create a replace array with empty strings to blank-out invalid vars
421 $acct = new Account();
422 $contact = new Contact();
424 $prospect = new Prospect();
426 foreach($lead->field_defs as $field_def) {
427 if(($field_def['type'] == 'relate' && empty($field_def['custom_type'])) || $field_def['type'] == 'assigned_user_name') {
430 $repl_arr["contact_".$field_def['name']] = '';
431 $repl_arr["contact_account_".$field_def['name']] = '';
433 foreach($prospect->field_defs as $field_def) {
434 if(($field_def['type'] == 'relate' && empty($field_def['custom_type'])) || $field_def['type'] == 'assigned_user_name') {
437 $repl_arr["contact_".$field_def['name']] = '';
438 $repl_arr["contact_account_".$field_def['name']] = '';
440 foreach($contact->field_defs as $field_def) {
441 if(($field_def['type'] == 'relate' && empty($field_def['custom_type'])) || $field_def['type'] == 'assigned_user_name') {
444 $repl_arr["contact_".$field_def['name']] = '';
445 $repl_arr["contact_account_".$field_def['name']] = '';
447 foreach($acct->field_defs as $field_def) {
448 if(($field_def['type'] == 'relate' && empty($field_def['custom_type'])) || $field_def['type'] == 'assigned_user_name') {
451 $repl_arr["account_".$field_def['name']] = '';
452 $repl_arr["account_contact_".$field_def['name']] = '';
454 // cn: end bug 9277 fix
457 // feel for Parent account, only for Contacts traditionally, but written for future expansion
458 if(isset($focus->account_id) && !empty($focus->account_id)) {
459 $acct->retrieve($focus->account_id);
462 if($bean_name == 'Contacts') {
463 // cn: bug 9277 - email templates not loading account/opp info for templates
464 if(!empty($acct->id)) {
465 foreach($acct->field_defs as $field_def) {
466 if(($field_def['type'] == 'relate' && empty($field_def['custom_type'])) || $field_def['type'] == 'assigned_user_name') {
470 if($field_def['type'] == 'enum') {
471 $translated = translate($field_def['options'], 'Accounts' ,$acct->$field_def['name']);
473 if(isset($translated) && ! is_array($translated)) {
474 $repl_arr["account_".$field_def['name']] = $translated;
475 $repl_arr["contact_account_".$field_def['name']] = $translated;
476 } else { // unset enum field, make sure we have a match string to replace with ""
477 $repl_arr["account_".$field_def['name']] = '';
478 $repl_arr["contact_account_".$field_def['name']] = '';
481 // bug 47647 - allow for fields to translate before adding to template
482 $translated = self::_convertToType($field_def['type'],$acct->$field_def['name']);
483 $repl_arr["account_".$field_def['name']] = $translated;
484 $repl_arr["contact_account_".$field_def['name']] = $translated;
489 if(!empty($focus->assigned_user_id)) {
491 $user->retrieve($focus->assigned_user_id);
492 $repl_arr = EmailTemplate::_parseUserValues($repl_arr, $user);
494 } elseif($bean_name == 'Users') {
496 * This section of code will on do work when a blank Contact, Lead,
497 * etc. is passed in to parse the contact_* vars. At this point,
498 * $current_user will be used to fill in the blanks.
500 $repl_arr = EmailTemplate::_parseUserValues($repl_arr, $current_user);
502 // assumed we have an Account in focus
503 foreach($contact->field_defs as $field_def) {
504 if(($field_def['type'] == 'relate' && empty($field_def['custom_type'])) || $field_def['type'] == 'assigned_user_name' || $field_def['type'] == 'link') {
508 if($field_def['type'] == 'enum') {
509 $translated = translate($field_def['options'], 'Accounts' ,$contact->$field_def['name']);
511 if(isset($translated) && ! is_array($translated)) {
512 $repl_arr["contact_".$field_def['name']] = $translated;
513 $repl_arr["contact_account_".$field_def['name']] = $translated;
514 } else { // unset enum field, make sure we have a match string to replace with ""
515 $repl_arr["contact_".$field_def['name']] = '';
516 $repl_arr["contact_account_".$field_def['name']] = '';
519 if (isset($contact->$field_def['name'])) {
520 // bug 47647 - allow for fields to translate before adding to template
521 $translated = self::_convertToType($field_def['type'],$contact->$field_def['name']);
522 $repl_arr["contact_".$field_def['name']] = $translated;
523 $repl_arr["contact_account_".$field_def['name']] = $translated;
529 ///////////////////////////////////////////////////////////////////////
530 //// LOAD FOCUS DATA INTO REPL_ARR
531 foreach($focus->field_defs as $field_def) {
532 if(isset($focus->$field_def['name'])) {
533 if(($field_def['type'] == 'relate' && empty($field_def['custom_type'])) || $field_def['type'] == 'assigned_user_name') {
537 if($field_def['type'] == 'enum' && isset($field_def['options'])) {
538 $translated = translate($field_def['options'],$bean_name,$focus->$field_def['name']);
540 if(isset($translated) && ! is_array($translated)) {
541 $repl_arr[strtolower($beanList[$bean_name])."_".$field_def['name']] = $translated;
542 } else { // unset enum field, make sure we have a match string to replace with ""
543 $repl_arr[strtolower($beanList[$bean_name])."_".$field_def['name']] = '';
546 // bug 47647 - translate currencies to appropriate values
547 $repl_arr[strtolower($beanList[$bean_name])."_".$field_def['name']] = self::_convertToType($field_def['type'],$focus->$field_def['name']);
550 if($field_def['name'] == 'full_name') {
551 $repl_arr[strtolower($beanList[$bean_name]).'_full_name'] = $focus->get_summary_text();
553 $repl_arr[strtolower($beanList[$bean_name])."_".$field_def['name']] = '';
560 //20595 add nl2br() to respect the multi-lines formatting
561 if(isset($repl_arr['contact_primary_address_street'])){
562 $repl_arr['contact_primary_address_street'] = nl2br($repl_arr['contact_primary_address_street']);
564 if(isset($repl_arr['contact_alt_address_street'])){
565 $repl_arr['contact_alt_address_street'] = nl2br($repl_arr['contact_alt_address_street']);
568 foreach ($repl_arr as $name=>$value) {
569 if($value != '' && is_string($value)) {
570 $string = str_replace("\$$name", $value, $string);
572 $string = str_replace("\$$name", ' ', $string);
579 function parse_template($string, &$bean_arr) {
580 global $beanFiles, $beanList;
582 foreach($bean_arr as $bean_name => $bean_id) {
583 require_once($beanFiles[$beanList[$bean_name]]);
585 $focus = new $beanList[$bean_name];
586 $result = $focus->retrieve($bean_id);
588 if($bean_name == 'Leads' || $bean_name == 'Prospects') {
589 $bean_name = 'Contacts';
592 if(isset($this) && isset($this->module_dir) && $this->module_dir == 'EmailTemplates') {
593 $string = $this->parse_template_bean($string, $bean_name, $focus);
595 $string = EmailTemplate::parse_template_bean($string, $bean_name, $focus);
601 function bean_implements($interface) {
603 case 'ACL':return true;
608 function is_used_by_email_marketing() {
609 $query = "select id from email_marketing where template_id='$this->id' and deleted=0";
610 $result = $this->db->query($query);
611 if($this->db->fetchByAssoc($result)) {