2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4 * SugarCRM is a customer relationship management program developed by
5 * SugarCRM, Inc. Copyright (C) 2004-2011 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;
71 var $table_name = "email_templates";
72 var $object_name = "EmailTemplate";
73 var $module_dir = "EmailTemplates";
74 var $new_schema = true;
75 // This is used to retrieve related fields from form posts.
76 var $additional_column_fields = array(
78 // add fields here that would not make sense in an email template
79 var $badFields = array(
80 'account_description',
86 'opportunity_role_id',
87 'opportunity_role_fields',
102 'receive_notifications',
108 'accept_status_name',
111 function EmailTemplate() {
116 * Generates the extended field_defs for creating macros
117 * @param object $bean SugarBean
118 * @param string $prefix "contact_", "user_" etc.
121 function generateFieldDefsJS() {
122 global $current_user;
128 $contact = new Contact();
129 $account = new Account();
131 $prospect = new Prospect();
134 $loopControl = array(
136 'Contacts' => $contact,
138 'Prospects' => $prospect,
141 'Accounts' => $account,
144 'Users' => $current_user,
149 'Contacts' => 'contact_',
150 'Accounts' => 'account_',
151 'Users' => 'contact_user_',
154 $collection = array();
155 foreach($loopControl as $collectionKey => $beans) {
156 $collection[$collectionKey] = array();
157 foreach($beans as $beankey => $bean) {
159 foreach($bean->field_defs as $key => $field_def) {
160 if( ($field_def['type'] == 'relate' && empty($field_def['custom_type'])) ||
161 ($field_def['type'] == 'assigned_user_name' || $field_def['type'] =='link') ||
162 ($field_def['type'] == 'bool') ||
163 (in_array($field_def['name'], $this->badFields)) ) {
166 if(!isset($field_def['vname'])) {
169 // valid def found, process
170 $optionKey = strtolower("{$prefixes[$collectionKey]}{$key}");
171 $optionLabel = preg_replace('/:$/', "", translate($field_def['vname'], $beankey));
173 foreach ($collection[$collectionKey] as $value){
174 if($value['name']==$optionKey){
180 $collection[$collectionKey][] = array("name" => $optionKey, "value" => $optionLabel);
185 $json = getJSONobj();
186 $ret = "var field_defs = ";
187 $ret .= $json->encode($collection, false);
192 function get_summary_text() {
193 return "$this->name";
196 function create_export_query(&$order_by, &$where) {
197 return $this->create_new_list_query($order_by, $where);
200 function fill_in_additional_list_fields() {
201 $this->fill_in_additional_parent_fields();
204 function fill_in_additional_detail_fields() {
205 $this->created_by_name = get_assigned_user_name($this->created_by);
206 $this->modified_by_name = get_assigned_user_name($this->modified_user_id);
207 $this->fill_in_additional_parent_fields();
210 function fill_in_additional_parent_fields() {
213 function get_list_view_data() {
214 global $app_list_strings, $focus, $action, $currentModule;
215 $fields = $this->get_list_view_array();
216 $fields["DATE_MODIFIED"] = substr($fields["DATE_MODIFIED"], 0 , 10);
220 //function all string that match the pattern {.} , also catches the list of found strings.
221 //the cache will get refreshed when the template bean instance changes.
222 //The found url key patterns are replaced with name value pairs provided as function parameter. $tracked_urls.
223 //$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
224 //$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.
225 //$removeme_url_template, if the url has is_optout property checked then use this template.
226 function parse_tracker_urls($template_text_array,$url_template,$tracked_urls,$removeme_url_template) {
227 global $beanFiles,$beanList, $app_list_strings,$sugar_config;
228 if (!isset($this->parsed_urls))
229 $this->parsed_urls=array();
231 //parse the template and find all the dynamic strings that need replacement.
232 $pattern = '/\{*[^\{\}]*\}/'; // cn: bug 6638, find multibyte strings
233 foreach ($template_text_array as $key=>$template_text) {
234 if (!empty($template_text)) {
235 if(!isset($this->parsed_urls[$key]) || $this->parsed_urls[$key]['text'] != $template_text) {
237 $count=preg_match_all($pattern,$template_text,$matches,PREG_OFFSET_CAPTURE);
238 $this->parsed_urls[$key]=array('matches' => $matches, 'text' => $template_text);
240 $matches=$this->parsed_urls[$key]['matches'];
241 if(!empty($matches[0])) {
242 $count=count($matches[0]);
248 //navigate thru all the matched keys and replace the keys with actual strings.
249 for ($i=($count -1); $i>=0; $i--) {
250 $url_key_name=$matches[0][$i][0];
252 if (!empty($tracked_urls[$url_key_name])) {
253 if ($tracked_urls[$url_key_name]['is_optout']==1){
254 $tracker_url = $removeme_url_template;
256 $tracker_url = sprintf($url_template,$tracked_urls[$url_key_name]['id']);
259 if(!empty($tracker_url) && !empty($template_text) && !empty($matches[0][$i][0]) && !empty($tracked_urls[$matches[0][$i][0]])){
260 $template_text=substr_replace($template_text,$tracker_url,$matches[0][$i][1], strlen($matches[0][$i][0]));
261 $template_text=str_replace($sugar_config['site_url'].'/'.$sugar_config['site_url'],$sugar_config['site_url'],$template_text);
265 $return_array[$key]=$template_text;
268 return $return_array;
271 function parse_email_template($template_text_array, $focus_name, $focus, &$macro_nv) {
274 global $beanFiles, $beanList, $app_list_strings;
276 // generate User instance that owns this "Contact" for contact_user_* macros
278 if(isset($focus->assigned_user_id) && !empty($focus->assigned_user_id)){
279 $user->retrieve($focus->assigned_user_id);
282 if(!isset($this->parsed_entities))
283 $this->parsed_entities=array();
285 //parse the template and find all the dynamic strings that need replacement.
286 $pattern_prefix = '$'.strtolower($beanList[$focus_name]).'_';
287 $pattern_prefix_length = strlen($pattern_prefix);
288 $pattern = '/\\'.$pattern_prefix.'[A-Za-z_0-9]*/';
290 foreach($template_text_array as $key=>$template_text) {
291 if(!isset($this->parsed_entities[$key])) {
293 $count = preg_match_all($pattern, $template_text, $matches, PREG_OFFSET_CAPTURE);
296 for($i=($count -1); $i>=0; $i--) {
297 if(!isset($matches[0][$i][2])) {
298 //find the field name in the bean.
299 $matches[0][$i][2]=substr($matches[0][$i][0],$pattern_prefix_length,strlen($matches[0][$i][0]) - $pattern_prefix_length);
301 //store the localized strings if the field is of type enum..
302 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'])) {
303 $matches[0][$i][3]=$focus->field_defs[$matches[0][$i][2]]['options'];
308 $this->parsed_entities[$key]=$matches;
310 $matches=$this->parsed_entities[$key];
311 if(!empty($matches[0])) {
312 $count=count($matches[0]);
318 for ($i=($count -1); $i>=0; $i--) {
319 $field_name=$matches[0][$i][2];
321 // cn: feel for User object attribute key and assign as found
322 if(strpos($field_name, "user_") === 0) {
323 $userFieldName = substr($field_name, 5);
324 $value = $user->$userFieldName;
325 //_pp($userFieldName."[{$value}]");
327 $value = $focus->{$field_name};
331 if(isset($matches[0][$i][3])) {
332 if(isset($app_list_strings[$matches[0][$i][3]][$value])) {
333 $value=$app_list_strings[$matches[0][$i][3]][$value];
337 //generate name value pair array of macros and corresponding values for the targed.
338 $macro_nv[$matches[0][$i][0]] =$value;
340 $template_text=substr_replace($template_text,$value,$matches[0][$i][1], strlen($matches[0][$i][0]));
343 //parse the template for tracker url strings. patter for these strings in {[a-zA-Z_0-9]+}
345 $return_array[$key]=$template_text;
348 return $return_array;
353 * Convenience method to parse for user's values in a template
354 * @param array $repl_arr
355 * @param object $user
358 function _parseUserValues($repl_arr, &$user) {
359 foreach($user->field_defs as $field_def) {
360 if(($field_def['type'] == 'relate' && empty($field_def['custom_type'])) || $field_def['type'] == 'assigned_user_name') {
364 if($field_def['type'] == 'enum') {
365 $translated = translate($field_def['options'], 'Users', $user->$field_def['name']);
367 if(isset($translated) && ! is_array($translated)) {
368 $repl_arr["contact_user_".$field_def['name']] = $translated;
369 } else { // unset enum field, make sure we have a match string to replace with ""
370 $repl_arr["contact_user_".$field_def['name']] = '';
373 if(isset($user->$field_def['name'])) {
374 $repl_arr["contact_user_".$field_def['name']] = $user->$field_def['name'];
376 $repl_arr["contact_user_".$field_def['name']] = "";
385 function parse_template_bean($string, $bean_name, &$focus) {
386 global $current_user;
387 global $beanFiles, $beanList;
390 // cn: bug 9277 - create a replace array with empty strings to blank-out invalid vars
391 if(!class_exists('Account'))
392 if(!class_exists('Contact'))
393 if(!class_exists('Leads'))
394 if(!class_exists('Prospects'))
396 require_once('modules/Accounts/Account.php');
397 $acct = new Account();
398 $contact = new Contact();
400 $prospect = new Prospect();
402 foreach($lead->field_defs as $field_def) {
403 if(($field_def['type'] == 'relate' && empty($field_def['custom_type'])) || $field_def['type'] == 'assigned_user_name') {
406 $repl_arr["contact_".$field_def['name']] = '';
407 $repl_arr["contact_account_".$field_def['name']] = '';
409 foreach($prospect->field_defs as $field_def) {
410 if(($field_def['type'] == 'relate' && empty($field_def['custom_type'])) || $field_def['type'] == 'assigned_user_name') {
413 $repl_arr["contact_".$field_def['name']] = '';
414 $repl_arr["contact_account_".$field_def['name']] = '';
416 foreach($contact->field_defs as $field_def) {
417 if(($field_def['type'] == 'relate' && empty($field_def['custom_type'])) || $field_def['type'] == 'assigned_user_name') {
420 $repl_arr["contact_".$field_def['name']] = '';
421 $repl_arr["contact_account_".$field_def['name']] = '';
423 foreach($acct->field_defs as $field_def) {
424 if(($field_def['type'] == 'relate' && empty($field_def['custom_type'])) || $field_def['type'] == 'assigned_user_name') {
427 $repl_arr["account_".$field_def['name']] = '';
428 $repl_arr["account_contact_".$field_def['name']] = '';
430 // cn: end bug 9277 fix
433 // feel for Parent account, only for Contacts traditionally, but written for future expansion
434 if(isset($focus->account_id) && !empty($focus->account_id)) {
435 $acct->retrieve($focus->account_id);
438 if($bean_name == 'Contacts') {
439 // cn: bug 9277 - email templates not loading account/opp info for templates
440 if(!empty($acct->id)) {
441 foreach($acct->field_defs as $field_def) {
442 if(($field_def['type'] == 'relate' && empty($field_def['custom_type'])) || $field_def['type'] == 'assigned_user_name') {
446 if($field_def['type'] == 'enum') {
447 $translated = translate($field_def['options'], 'Accounts' ,$acct->$field_def['name']);
449 if(isset($translated) && ! is_array($translated)) {
450 $repl_arr["account_".$field_def['name']] = $translated;
451 $repl_arr["contact_account_".$field_def['name']] = $translated;
452 } else { // unset enum field, make sure we have a match string to replace with ""
453 $repl_arr["account_".$field_def['name']] = '';
454 $repl_arr["contact_account_".$field_def['name']] = '';
457 $repl_arr["account_".$field_def['name']] = $acct->$field_def['name'];
458 $repl_arr["contact_account_".$field_def['name']] = $acct->$field_def['name'];
463 if(!empty($focus->assigned_user_id)) {
465 $user->retrieve($focus->assigned_user_id);
466 $repl_arr = EmailTemplate::_parseUserValues($repl_arr, $user);
468 } elseif($bean_name == 'Users') {
470 * This section of code will on do work when a blank Contact, Lead,
471 * etc. is passed in to parse the contact_* vars. At this point,
472 * $current_user will be used to fill in the blanks.
474 $repl_arr = EmailTemplate::_parseUserValues($repl_arr, $current_user);
476 // assumed we have an Account in focus
477 foreach($contact->field_defs as $field_def) {
478 if(($field_def['type'] == 'relate' && empty($field_def['custom_type'])) || $field_def['type'] == 'assigned_user_name' || $field_def['type'] == 'link') {
482 if($field_def['type'] == 'enum') {
483 $translated = translate($field_def['options'], 'Accounts' ,$contact->$field_def['name']);
485 if(isset($translated) && ! is_array($translated)) {
486 $repl_arr["contact_".$field_def['name']] = $translated;
487 $repl_arr["contact_account_".$field_def['name']] = $translated;
488 } else { // unset enum field, make sure we have a match string to replace with ""
489 $repl_arr["contact_".$field_def['name']] = '';
490 $repl_arr["contact_account_".$field_def['name']] = '';
493 if (isset($contact->$field_def['name'])) {
494 $repl_arr["contact_".$field_def['name']] = $contact->$field_def['name'];
495 $repl_arr["contact_account_".$field_def['name']] = $contact->$field_def['name'];
501 ///////////////////////////////////////////////////////////////////////
502 //// LOAD FOCUS DATA INTO REPL_ARR
503 foreach($focus->field_defs as $field_def) {
504 if(isset($focus->$field_def['name'])) {
505 if(($field_def['type'] == 'relate' && empty($field_def['custom_type'])) || $field_def['type'] == 'assigned_user_name') {
509 if($field_def['type'] == 'enum' && isset($field_def['options'])) {
510 $translated = translate($field_def['options'],$bean_name,$focus->$field_def['name']);
512 if(isset($translated) && ! is_array($translated)) {
513 $repl_arr[strtolower($beanList[$bean_name])."_".$field_def['name']] = $translated;
514 } else { // unset enum field, make sure we have a match string to replace with ""
515 $repl_arr[strtolower($beanList[$bean_name])."_".$field_def['name']] = '';
518 $repl_arr[strtolower($beanList[$bean_name])."_".$field_def['name']] = $focus->$field_def['name'];
521 if($field_def['name'] == 'full_name') {
522 $repl_arr[strtolower($beanList[$bean_name]).'_full_name'] = $focus->get_summary_text();
524 $repl_arr[strtolower($beanList[$bean_name])."_".$field_def['name']] = '';
531 //20595 add nl2br() to respect the multi-lines formatting
532 if(isset($repl_arr['contact_primary_address_street'])){
533 $repl_arr['contact_primary_address_street'] = nl2br($repl_arr['contact_primary_address_street']);
535 if(isset($repl_arr['contact_alt_address_street'])){
536 $repl_arr['contact_alt_address_street'] = nl2br($repl_arr['contact_alt_address_street']);
539 foreach ($repl_arr as $name=>$value) {
540 if($value != '' && is_string($value)) {
541 $string = str_replace("\$$name", $value, $string);
543 $string = str_replace("\$$name", ' ', $string);
550 function parse_template($string, &$bean_arr) {
551 global $beanFiles, $beanList;
553 foreach($bean_arr as $bean_name => $bean_id) {
554 require_once($beanFiles[$beanList[$bean_name]]);
556 $focus = new $beanList[$bean_name];
557 $result = $focus->retrieve($bean_id);
559 if($bean_name == 'Leads' || $bean_name == 'Prospects') {
560 $bean_name = 'Contacts';
563 if(isset($this) && isset($this->module_dir) && $this->module_dir == 'EmailTemplates') {
564 $string = $this->parse_template_bean($string, $bean_name, $focus);
566 $string = EmailTemplate::parse_template_bean($string, $bean_name, $focus);
572 function bean_implements($interface) {
574 case 'ACL':return true;
579 function is_used_by_email_marketing() {
580 $query = "select id from email_marketing where template_id='$this->id' and deleted=0";
581 $result = $this->db->query($query);
582 if($this->db->fetchByAssoc($result)) {