]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Emails/EditView.php
Release 6.5.0beta6
[Github/sugarcrm.git] / modules / Emails / EditView.php
1 <?php
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.
6  * 
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.
13  * 
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
17  * details.
18  * 
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
22  * 02110-1301 USA.
23  * 
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.
26  * 
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.
30  * 
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  ********************************************************************************/
37
38
39 $GLOBALS['log']->info("Email edit view");
40
41 require_once('include/SugarTinyMCE.php');
42
43
44
45
46 global $theme;
47 global $app_strings;
48 global $app_list_strings;
49 global $mod_strings;
50 global $current_user;
51 global $sugar_version, $sugar_config;
52 global $timedate;
53
54 ///////////////////////////////////////////////////////////////////////////////
55 ////    PREPROCESS BEAN DATA FOR DISPLAY
56 $focus = new Email();
57 $email_type = 'archived';
58
59 if(isset($_REQUEST['record'])) {
60     $focus->retrieve($_REQUEST['record']);
61 }
62 if(!empty($_REQUEST['type'])) {
63         $email_type = $_REQUEST['type'];
64 } elseif(!empty($focus->id)) {
65         $email_type = $focus->type;
66 }
67
68 $focus->type = $email_type;
69
70 //needed when creating a new email with default values passed in
71 if(isset($_REQUEST['contact_name']) && is_null($focus->contact_name)) {
72         $focus->contact_name = $_REQUEST['contact_name'];
73 }
74
75 if(!empty($_REQUEST['load_id']) && !empty($beanList[$_REQUEST['load_module']])) {
76         $class_name = $beanList[$_REQUEST['load_module']];
77         require_once($beanFiles[$class_name]);
78         $contact = new $class_name();
79         if($contact->retrieve($_REQUEST['load_id'])) {
80         $link_id = $class_name . '_id';
81         $focus->$link_id = $_REQUEST['load_id'];
82         $focus->contact_name = (isset($contact->full_name)) ? $contact->full_name : $contact->name;
83         $focus->to_addrs_names = $focus->contact_name;
84         $focus->to_addrs_ids = $_REQUEST['load_id'];
85         //Retrieve the email address.
86         //If Opportunity or Case then Oppurtinity/Case->Accounts->(email_addr_bean_rel->email_addresses)
87         //If Contacts, Leads etc.. then Contact->(email_addr_bean_rel->email_addresses)
88         $sugarEmailAddress = new SugarEmailAddress();
89         if($class_name == 'Opportunity' || $class_name == 'aCase'){
90                 $account = new Account();
91                 if($contact->account_id != null && $account->retrieve($contact->account_id)){
92                         $sugarEmailAddress->handleLegacyRetrieve($account);
93                     if(isset($account->email1)){
94                                 $focus->to_addrs_emails = $account->email1;
95                                 $focus->to_addrs = "$focus->contact_name <$account->email1>";
96                     }
97                 }
98         }
99         else{
100                 $sugarEmailAddress->handleLegacyRetrieve($contact);
101                 if(isset($contact->email1)){
102                         $focus->to_addrs_emails = $contact->email1;
103                         $focus->to_addrs = "$focus->contact_name <$contact->email1>";
104                 }
105             }
106         if(!empty($_REQUEST['parent_type']) && empty($app_list_strings['record_type_display'][$_REQUEST['parent_type']])){
107                 if(!empty($app_list_strings['record_type_display'][$_REQUEST['load_module']])){
108                         $_REQUEST['parent_type'] = $_REQUEST['load_module'];
109                         $_REQUEST['parent_id'] = $focus->contact_id;
110                         $_REQUEST['parent_name'] = $focus->to_addrs_names;
111                 } else {
112                         unset($_REQUEST['parent_type']);
113                         unset($_REQUEST['parent_id']);
114                         unset($_REQUEST['parent_name']);
115                 }
116         }
117         }
118 }
119 if(isset($_REQUEST['contact_id']) && is_null($focus->contact_id)) {
120         $focus->contact_id = $_REQUEST['contact_id'];
121 }
122 if(isset($_REQUEST['parent_name'])) {
123   $focus->parent_name = $_REQUEST['parent_name'];
124 }
125 if(isset($_REQUEST['parent_id'])) {
126         $focus->parent_id = $_REQUEST['parent_id'];
127 }
128 if(isset($_REQUEST['parent_type'])) {
129         $focus->parent_type = $_REQUEST['parent_type'];
130 }
131 elseif(is_null($focus->parent_type)) {
132         $focus->parent_type = $app_list_strings['record_type_default_key'];
133 }
134 if(isset($_REQUEST['to_email_addrs'])) {
135         $focus->to_addrs = $_REQUEST['to_email_addrs'];
136 }
137 // needed when clicking through a Contacts detail view:
138 if(isset($_REQUEST['to_addrs_ids'])) {
139         $focus->to_addrs_ids = $_REQUEST['to_addrs_ids'];
140 }
141 if(isset($_REQUEST['to_addrs_emails'])) {
142         $focus->to_addrs_emails = $_REQUEST['to_addrs_emails'];
143 }
144 if(isset($_REQUEST['to_addrs_names'])) {
145         $focus->to_addrs_names = $_REQUEST['to_addrs_names'];
146 }
147 // user's email, go through 3 levels of precedence:
148 $from = $current_user->getEmailInfo();
149 ////    END PREPROCESSING
150 ///////////////////////////////////////////////////////////////////////////////
151
152
153 ///////////////////////////////////////////////////////////////////////////////
154 ////    XTEMPLATE ASSIGNMENT
155 if($email_type == 'archived') {
156     $params_module_title=array($mod_strings['LBL_ARCHIVED_MODULE_NAME'], $focus->name);
157         echo getClassicModuleTitle('Emails', $params_module_title, true);
158         $xtpl=new XTemplate('modules/Emails/EditViewArchive.html');
159 } else {
160
161         if(empty($_REQUEST['parent_module'])) {
162                 $parent_module = "Emails";
163         } else {
164                 $parent_module = $_REQUEST['parent_module'];
165         }
166         if(empty($_REQUEST['parent_id'])) {
167                 $parent_id = "";
168         } else {
169                 $parent_id = $_REQUEST['parent_id'];
170         }
171         if(empty($_REQUEST['return_module'])) {
172                 $return_module = "Emails";
173         } else {
174                 $return_module = $_REQUEST['return_module'];
175         }
176         if(empty($_REQUEST['return_module'])) {
177                 $return_id = "";
178         } else {
179                 $return_id = $_REQUEST['return_module'];
180         }
181         $replyType = "reply";
182         if ($_REQUEST['type'] == 'forward') {
183                 $replyType = 'forward';
184         } // if
185         header("Location: index.php?module=Emails&action=Compose&record=$focus->id&replyForward=true&reply=$replyType");
186         return;
187 }
188 echo "\n</p>\n";
189
190 // CHECK USER'S EMAIL SETTINGS TO ENABLE/DISABLE 'SEND' BUTTON
191 if(!$focus->check_email_settings() &&($email_type == 'out' || $email_type == 'draft')) {
192         print "<font color='red'>".$mod_strings['WARNING_SETTINGS_NOT_CONF']." <a href='index.php?module=Users&action=EditView&record=".$current_user->id."&return_module=Emails&type=out&return_action=EditView'>".$mod_strings['LBL_EDIT_MY_SETTINGS']."</a></font>";
193         $xtpl->assign("DISABLE_SEND", 'DISABLED');
194 }
195
196 // CHECK THAT SERVER HAS A PLACE TO PUT UPLOADED TEMP FILES SO THAT ATTACHMENTS WILL WORK
197 // cn: Bug 5995
198 $tmpUploadDir = ini_get('upload_tmp_dir');
199 if(!empty($tmpUploadDir)) {
200         if(!is_writable($tmpUploadDir)) {
201                 echo "<font color='red'>{$mod_strings['WARNING_UPLOAD_DIR_NOT_WRITABLE']}</font>";
202         }
203 } else {
204         //echo "<font color='red'>{$mod_strings['WARNING_NO_UPLOAD_DIR']}</font>";
205 }
206
207
208 ///////////////////////////////////////////////////////////////////////////////
209 ////    INBOUND EMAIL HANDLING
210 if(isset($_REQUEST['email_name'])) {
211         $name = str_replace('_',' ',$_REQUEST['email_name']);
212 }
213 if(isset($_REQUEST['inbound_email_id'])) {
214         $ieMail = new Email();
215         $ieMail->retrieve($_REQUEST['inbound_email_id']);
216
217         $quoted = '';
218         // cn: bug 9725: replies/forwards lose real content
219         $quotedHtml = $ieMail->quoteHtmlEmail($ieMail->description_html);
220
221         // plain-text
222         $desc           = nl2br(trim($ieMail->description));
223
224         $exDesc = explode('<br />', $desc);
225         foreach($exDesc as $k => $line) {
226                 $quoted .= '> '.trim($line)."\r";
227         }
228
229         // prefill empties with the other's contents
230         if(empty($quotedHtml) && !empty($quoted)) {
231                 $quotedHtml = nl2br($quoted);
232         }
233         if(empty($quoted) && !empty($quotedHtml)) {
234                 $quoted = strip_tags(br2nl($quotedHtml));
235         }
236
237         // forwards have special text
238         if($_REQUEST['type'] == 'forward') {
239                 $header = $ieMail->getForwardHeader();
240                 // subject is handled in Subject line handling below
241         } else {
242                 // we have a reply in focus
243                 $header = $ieMail->getReplyHeader();
244         }
245
246         $quoted = br2nl($header.$quoted);
247         $quotedHtml = $header.$quotedHtml;
248
249
250         // if not a forward: it's a reply
251         if($_REQUEST['type'] != 'forward') {
252                 $ieMailName = 'RE: '.$ieMail->name;
253         } else {
254                 $ieMailName = $ieMail->name;
255         }
256
257         $focus->id                                      = null; // nulling this to prevent overwriting a replied email(we're basically doing a "Duplicate" function)
258         $focus->to_addrs                        = $ieMail->from_addr;
259         $focus->description             = $quoted; // don't know what i was thinking: ''; // this will be filled on save/send
260         $focus->description_html        = $quotedHtml; // cn: bug 7357 - htmlentities() breaks FCKEditor
261         $focus->parent_type                     = $ieMail->parent_type;
262         $focus->parent_id                       = $ieMail->parent_id;
263         $focus->parent_name                     = $ieMail->parent_name;
264         $focus->name                            = $ieMailName;
265         $xtpl->assign('INBOUND_EMAIL_ID',$_REQUEST['inbound_email_id']);
266         // un/READ flags
267         if(!empty($ieMail->status)) {
268                 // "Read" flag for InboundEmail
269                 if($ieMail->status == 'unread') {
270                         // creating a new instance here to avoid data corruption below
271                         $e = new Email();
272                         $e->retrieve($ieMail->id);
273                         $e->status = 'read';
274                         $e->save();
275                         $email_type = $e->status;
276                 }
277         }
278
279         ///////////////////////////////////////////////////////////////////////////
280         ////    PRIMARY PARENT LINKING
281         if(empty($focus->parent_type) && empty($focus->parent_id)) {
282                 $focus->fillPrimaryParentFields();
283         }
284         ////    END PRIMARY PARENT LINKING
285         ///////////////////////////////////////////////////////////////////////////
286
287         // setup for my/mailbox email switcher
288         $mbox = $ieMail->getMailboxDefaultEmail();
289         $user = $current_user->getPreferredEmail();
290         $useGroup = '&nbsp;<input id="use_mbox" name="use_mbox" type="checkbox" CHECKED onClick="switchEmail()" >
291                                 <script type="text/javascript">
292                                 function switchEmail() {
293                                         var mboxName = "'.$mbox['name'].'";
294                                         var mboxAddr = "'.$mbox['email'].'";
295                                         var userName = "'.$user['name'].'";
296                                         var userAddr = "'.$user['email'].'";
297
298                                         if(document.getElementById("use_mbox").checked) {
299                                                 document.getElementById("from_addr_field").value = mboxName + " <" + mboxAddr + ">";
300                                                 document.getElementById("from_addr_name").value = mboxName;
301                                                 document.getElementById("from_addr_email").value = mboxAddr;
302                                         } else {
303                                                 document.getElementById("from_addr_field").value = userName + " <" + userAddr + ">";
304                                                 document.getElementById("from_addr_name").value = userName;
305                                                 document.getElementById("from_addr_email").value = userAddr;
306                                         }
307
308                                 }
309
310                                 </script>';
311         $useGroup .= $mod_strings['LBL_USE_MAILBOX_INFO'];
312
313         $xtpl->assign('FROM_ADDR_GROUP', $useGroup);
314 }
315 ////    END INBOUND EMAIL HANDLING
316 ///////////////////////////////////////////////////////////////////////////////
317
318 ///////////////////////////////////////////////////////////////////////////////
319 ////    SUBJECT FIELD MANIPULATION
320 $name = '';
321 if(!empty($_REQUEST['parent_id']) && !empty($_REQUEST['parent_type'])) {
322         $focus->parent_id = $_REQUEST['parent_id'];
323         $focus->parent_type = $_REQUEST['parent_type'];
324 }
325 if(!empty($focus->parent_id) && !empty($focus->parent_type)) {
326         if($focus->parent_type == 'Cases') {
327
328                 $myCase = new aCase();
329                 $myCase->retrieve($focus->parent_id);
330                 $myCaseMacro = $myCase->getEmailSubjectMacro();
331                 if(isset($ieMail->name) && !empty($ieMail->name)) { // if replying directly to an InboundEmail
332                         $oldEmailSubj = $ieMail->name;
333                 } elseif(isset($_REQUEST['parent_name']) && !empty($_REQUEST['parent_name'])) {
334                         $oldEmailSubj = $_REQUEST['parent_name'];
335                 } else {
336                         $oldEmailSubj = $focus->name; // replying to an email using old subject
337                 }
338
339                 if(!preg_match('/^re:/i', $oldEmailSubj)) {
340                         $oldEmailSubj = 'RE: '.$oldEmailSubj;
341                 }
342                 $focus->name = $oldEmailSubj;
343
344                 if(strpos($focus->name, str_replace('%1',$myCase->case_number,$myCaseMacro))) {
345                         $name = $focus->name;
346                 } else {
347                         $name = $focus->name.' '.str_replace('%1',$myCase->case_number,$myCaseMacro);
348                 }
349         } else {
350                 $name = $focus->name;
351         }
352 } else {
353         if(empty($focus->name)) {
354                 $name = '';
355         } else {
356                 $name = $focus->name;
357         }
358 }
359 if($email_type == 'forward') {
360         $name = 'FW: '.$name;
361 }
362 ////    END SUBJECT FIELD MANIPULATION
363 ///////////////////////////////////////////////////////////////////////////////
364
365 ///////////////////////////////////////////////////////////////////////////////
366 ////    GENERAL TEMPLATE ASSIGNMENTS
367 $xtpl->assign('MOD', $mod_strings);
368 $xtpl->assign('APP', $app_strings);
369 $xtpl->assign('THEME', SugarThemeRegistry::current()->__toString());
370 if(!isset($focus->id)) $xtpl->assign('USER_ID', $current_user->id);
371 if(!isset($focus->id) && isset($_REQUEST['contact_id'])) $xtpl->assign('CONTACT_ID', $_REQUEST['contact_id']);
372
373 if(isset($_REQUEST['return_module']) && !empty($_REQUEST['return_module'])) {
374         $xtpl->assign('RETURN_MODULE', $_REQUEST['return_module']);
375 } else {
376         $xtpl->assign('RETURN_MODULE', 'Emails');
377 }
378 if(isset($_REQUEST['return_action']) && !empty($_REQUEST['return_action']) && ($_REQUEST['return_action'] != 'SubPanelViewer')) {
379         $xtpl->assign('RETURN_ACTION', $_REQUEST['return_action']);
380 } else {
381         $xtpl->assign('RETURN_ACTION', 'DetailView');
382 }
383 if(isset($_REQUEST['return_id']) && !empty($_REQUEST['return_id'])) {
384         $xtpl->assign('RETURN_ID', $_REQUEST['return_id']);
385 }
386 // handle Create $module then Cancel
387 if(empty($_REQUEST['return_id']) && !isset($_REQUEST['type'])) {
388         $xtpl->assign('RETURN_ACTION', 'index');
389 }
390
391 $xtpl->assign('PRINT_URL', 'index.php?'.$GLOBALS['request_string']);
392
393
394         ///////////////////////////////////////////////////////////////////////////////
395         ////    QUICKSEARCH CODE
396         require_once('include/QuickSearchDefaults.php');
397         $qsd = QuickSearchDefaults::getQuickSearchDefaults();
398         $sqs_objects = array('EditView_parent_name' => $qsd->getQSParent(),
399                                                 'EditView_assigned_user_name' => $qsd->getQSUser(),
400                                                 );
401
402
403         $json = getJSONobj();
404
405         $sqs_objects_encoded = $json->encode($sqs_objects);
406         $quicksearch_js = <<<EOQ
407                 <script type="text/javascript" language="javascript">sqs_objects = $sqs_objects_encoded; var dialog_loaded;
408                         function parent_typechangeQS() {
409                                 var new_module = document.EditView.parent_type.value;
410                                 var sqsId = 'EditView_parent_name';
411                                 if(new_module == 'Contacts' || new_module == 'Leads' || typeof(disabledModules[new_module]) != 'undefined') {
412                                         sqs_objects[sqsId]['disable'] = true;
413                                         document.getElementById('parent_name').readOnly = true;
414                                 }
415                                 else {
416                                         sqs_objects[sqsId]['disable'] = false;
417                                         document.getElementById('parent_name').readOnly = false;
418                                 }
419
420                                 sqs_objects[sqsId]['modules'] = new Array(new_module);
421                                 if (typeof(QSFieldsArray[sqsId]) != 'undefined')
422                 {
423                     QSFieldsArray[sqsId].sqs.modules = new Array(new_module);
424                 }
425                                 enableQS(false);
426                         }
427                         parent_typechangeQS();
428                 </script>
429 EOQ;
430         $xtpl->assign('JAVASCRIPT', get_set_focus_js().$quicksearch_js);
431         ////    END QUICKSEARCH CODE
432         ///////////////////////////////////////////////////////////////////////////////
433
434 // cn: bug 14191 - duping archive emails overwrites the original
435 if(!isset($_REQUEST['isDuplicate']) || $_REQUEST['isDuplicate'] != 'true') {
436         $xtpl->assign('ID', $focus->id);
437 }
438
439 if(isset($_REQUEST['parent_type']) && !empty($_REQUEST['parent_type']) && isset($_REQUEST['parent_id']) && !empty($_REQUEST['parent_id'])) {
440         $xtpl->assign('OBJECT_ID', $_REQUEST['parent_id']);
441         $xtpl->assign('OBJECT_TYPE', $_REQUEST['parent_type']);
442 }
443 $xtpl->assign('FROM_ADDR', $focus->from_addr);
444 //// prevent TO: prefill when type is 'forward'
445 if($email_type != 'forward') {
446         $xtpl->assign('TO_ADDRS', $focus->to_addrs);
447         $xtpl->assign('TO_ADDRS_IDS', $focus->to_addrs_ids);
448         $xtpl->assign('TO_ADDRS_NAMES', $focus->to_addrs_names);
449         $xtpl->assign('TO_ADDRS_EMAILS', $focus->to_addrs_emails);
450         $xtpl->assign('CC_ADDRS', $focus->cc_addrs);
451         $xtpl->assign('CC_ADDRS_IDS', $focus->cc_addrs_ids);
452         $xtpl->assign('CC_ADDRS_NAMES', $focus->cc_addrs_names);
453         $xtpl->assign('CC_ADDRS_EMAILS', $focus->cc_addrs_emails);
454         $xtpl->assign('BCC_ADDRS', $focus->bcc_addrs);
455         $xtpl->assign('BCC_ADDRS_IDS', $focus->bcc_addrs_ids);
456         $xtpl->assign('BCC_ADDRS_NAMES', $focus->bcc_addrs_names);
457         $xtpl->assign('BCC_ADDRS_EMAILS', $focus->bcc_addrs_emails);
458 }
459
460 //$xtpl->assign('FROM_ADDR', $from['name'].' <'.$from['email'].'>');
461 $xtpl->assign('FROM_ADDR_NAME', $from['name']);
462 $xtpl->assign('FROM_ADDR_EMAIL', $from['email']);
463
464 $xtpl->assign('NAME', from_html($name));
465 //$xtpl->assign('DESCRIPTION_HTML', from_html($focus->description_html));
466 $xtpl->assign('DESCRIPTION', $focus->description);
467 $xtpl->assign('TYPE',$email_type);
468
469 // Unimplemented until jscalendar language files are fixed
470 // $xtpl->assign('CALENDAR_LANG',((empty($cal_codes[$current_language])) ? $cal_codes[$default_language] : $cal_codes[$current_language]));
471 $xtpl->assign('CALENDAR_LANG', 'en');
472 $xtpl->assign('CALENDAR_DATEFORMAT', $timedate->get_cal_date_format());
473 $xtpl->assign('DATE_START', $focus->date_start);
474 $xtpl->assign('TIME_FORMAT', '('. $timedate->get_user_time_format().')');
475 $xtpl->assign('TIME_START', substr($focus->time_start,0,5));
476 $xtpl->assign('TIME_MERIDIEM', $timedate->AMPMMenu('',$focus->time_start));
477
478 $parent_types = $app_list_strings['record_type_display'];
479 $disabled_parent_types = ACLController::disabledModuleList($parent_types,false, 'list');
480
481 foreach($disabled_parent_types as $disabled_parent_type){
482         if($disabled_parent_type != $focus->parent_type){
483                 unset($parent_types[$disabled_parent_type]);
484         }
485 }
486
487 $xtpl->assign('TYPE_OPTIONS', get_select_options_with_id($parent_types, $focus->parent_type));
488 $xtpl->assign('USER_DATEFORMAT', '('. $timedate->get_user_date_format().')');
489 $xtpl->assign('PARENT_NAME', $focus->parent_name);
490 $xtpl->assign('PARENT_ID', $focus->parent_id);
491 if(empty($focus->parent_type)) {
492         $xtpl->assign('PARENT_RECORD_TYPE', '');
493 } else {
494         $xtpl->assign('PARENT_RECORD_TYPE', $focus->parent_type);
495 }
496
497 if(is_admin($current_user) && $_REQUEST['module'] != 'DynamicLayout' && !empty($_SESSION['editinplace'])){
498         $record = '';
499         if(!empty($_REQUEST['record'])){
500                 $record =       $_REQUEST['record'];
501         }
502         $xtpl->assign('ADMIN_EDIT',"<a href='index.php?action=index&module=DynamicLayout&from_action=".$_REQUEST['action'] ."&from_module=".$_REQUEST['module'] ."&record=".$record. "'>".SugarThemeRegistry::current()->getImage("EditLayout","border='0' align='bottom'",null,null,'.gif',$mod_strings['LBL_EDIT_LAYOUT'])."</a>");
503 }
504
505 ////    END GENERAL TEMPLATE ASSIGNMENTS
506 ///////////////////////////////////////////////////////////////////////////////
507
508
509 ///////////////////////////////////////
510 ///
511 /// SETUP PARENT POPUP
512
513 $popup_request_data = array(
514         'call_back_function' => 'set_return',
515         'form_name' => 'EditView',
516         'field_to_name_array' => array(
517                 'id' => 'parent_id',
518                 'name' => 'parent_name',
519                 ),
520         );
521
522 $encoded_popup_request_data = $json->encode($popup_request_data);
523
524 /// Users Popup
525 $popup_request_data = array(
526         'call_back_function' => 'set_return',
527         'form_name' => 'EditView',
528         'field_to_name_array' => array(
529                 'id' => 'assigned_user_id',
530                 'user_name' => 'assigned_user_name',
531                 ),
532         );
533 $xtpl->assign('encoded_users_popup_request_data', $json->encode($popup_request_data));
534
535
536 //
537 ///////////////////////////////////////
538
539 $change_parent_button = '<input type="button" name="button" tabindex="2" class="button" '
540         . 'title="' . $app_strings['LBL_SELECT_BUTTON_TITLE'] . '" '
541         . 'value="'     . $app_strings['LBL_SELECT_BUTTON_LABEL'] . '" '
542         . "onclick='ValidateParentType();' />\n"
543         .'<script>function ValidateParentType() {
544                 var parentTypeValue = document.getElementById("parent_type").value;
545                 if (trim(parentTypeValue) == ""){
546                         alert("' . $mod_strings['LBL_ERROR_SELECT_MODULE'] .'");
547                         return false;
548                 }
549                 open_popup(document.EditView.parent_type.value,600,400,"&tree=ProductsProd",true,false,' .$encoded_popup_request_data.');
550                 }</script>';
551
552 $xtpl->assign("CHANGE_PARENT_BUTTON", $change_parent_button);
553
554 $button_attr = '';
555 if(!ACLController::checkAccess('Contacts', 'list', true)){
556         $button_attr = 'disabled="disabled"';
557 }
558
559 $change_to_addrs_button = '<input type="button" name="to_button" tabindex="3" class="button" '
560         . 'title="' . $app_strings['LBL_SELECT_BUTTON_TITLE'] . '" '
561         . 'value="'     . $mod_strings['LBL_EMAIL_SELECTOR_SELECT'] . '" '
562         . "onclick='button_change_onclick(this);' $button_attr />\n";
563 $xtpl->assign("CHANGE_TO_ADDRS_BUTTON", $change_to_addrs_button);
564
565 $change_cc_addrs_button = '<input type="button" name="cc_button" tabindex="3" class="button" '
566         . 'title="' . $app_strings['LBL_SELECT_BUTTON_TITLE'] . '" '
567         . 'value="'     . $mod_strings['LBL_EMAIL_SELECTOR_SELECT'] . '" '
568         . "onclick='button_change_onclick(this);' $button_attr />\n";
569 $xtpl->assign("CHANGE_CC_ADDRS_BUTTON", $change_cc_addrs_button);
570
571 $change_bcc_addrs_button = '<input type="button" name="bcc_button" tabindex="3" class="button" '
572         . 'title="' . $app_strings['LBL_SELECT_BUTTON_TITLE'] . '" '
573         . 'value="'     . $mod_strings['LBL_EMAIL_SELECTOR_SELECT'] . '" '
574         . "onclick='button_change_onclick(this);' $button_attr />\n";
575 $xtpl->assign("CHANGE_BCC_ADDRS_BUTTON", $change_bcc_addrs_button);
576
577
578 ///////////////////////////////////////
579 ////    USER ASSIGNMENT
580 global $current_user;
581 if(is_admin($current_user) && $_REQUEST['module'] != 'DynamicLayout' && !empty($_SESSION['editinplace'])) {
582         $record = '';
583         if(!empty($_REQUEST['record'])) {
584                 $record = $_REQUEST['record'];
585         }
586         $xtpl->assign('ADMIN_EDIT',"<a href='index.php?action=index&module=DynamicLayout&from_action=".$_REQUEST['action'] ."&from_module=".$_REQUEST['module'] ."&record=".$record. "'>".SugarThemeRegistry::current()->getImage("EditLayout","border='0' align='bottom'",null,null,'.gif',$mod_strings['LBL_EDIT_LAYOUT'])."</a>");
587 }
588
589 if(empty($focus->assigned_user_id) && empty($focus->id))
590         $focus->assigned_user_id = $current_user->id;
591 if(empty($focus->assigned_name) && empty($focus->id))
592         $focus->assigned_user_name = $current_user->user_name;
593 $xtpl->assign('ASSIGNED_USER_OPTIONS', get_select_options_with_id(get_user_array(TRUE, 'Active', $focus->assigned_user_id), $focus->assigned_user_id));
594 $xtpl->assign('ASSIGNED_USER_NAME', $focus->assigned_user_name);
595 $xtpl->assign('ASSIGNED_USER_ID', $focus->assigned_user_id);
596 $xtpl->assign('DURATION_HOURS', $focus->duration_hours);
597 $xtpl->assign('TYPE_OPTIONS', get_select_options_with_id($parent_types, $focus->parent_type));
598
599 if(isset($focus->duration_minutes)) {
600         $xtpl->assign('DURATION_MINUTES_OPTIONS', get_select_options_with_id($focus->minutes_values,$focus->duration_minutes));
601 }
602 ////    END USER ASSIGNMENT
603 ///////////////////////////////////////
604
605
606
607 //Add Custom Fields
608 require_once('modules/DynamicFields/templates/Files/EditView.php');
609
610
611 ///////////////////////////////////////
612 ////    ATTACHMENTS
613 $attachments = '';
614 if(!empty($focus->id) || (!empty($_REQUEST['record']) && $_REQUEST['type'] == 'forward')) {
615
616         $attachments = "<input type='hidden' name='removeAttachment' id='removeAttachment' value=''>\n";
617         $ids = '';
618
619         $focusId = empty($focus->id) ? $_REQUEST['record'] : $focus->id;
620         $note = new Note();
621         $where = "notes.parent_id='{$focusId}' AND notes.filename IS NOT NULL";
622         $notes_list = $note->get_full_list("", $where,true);
623
624         if(!isset($notes_list)) {
625                 $notes_list = array();
626         }
627         for($i = 0;$i < count($notes_list);$i++) {
628                 $the_note = $notes_list[$i];
629                 if(empty($the_note->filename)) {
630                         continue;
631                 }
632
633                 // cn: bug 8034 - attachments from forwards/replies lost when saving drafts
634                 if(!empty($ids)) {
635                         $ids .= ",";
636                 }
637                 $ids .= $the_note->id;
638
639                 $attachments .= "
640                         <div id='noteDiv{$the_note->id}'>
641                                 " . SugarThemeRegistry::current()->getImage('delete_inline', "onclick='deletePriorAttachment(\"{$the_note->id}\");' value='{$the_note->id}'", null, null, ".gif", $mod_strings['LBL_DELETE_INLINE']) . "&nbsp;";
642                 $attachments .= "<a href=\"index.php?entryPoint=download&id=".$the_note->id."&type=Notes\">".$the_note->name."</a><div />";
643                 //$attachments .= '<a href="'.UploadFile::get_url($the_note->filename,$the_note->id).'&entryPoint=download&type=Notes' . '" target="_blank">'. $the_note->filename .'</a></div>';
644
645         }
646         // cn: bug 8034 - attachments from forwards/replies lost when saving drafts
647         $attachments .= "<input type='hidden' name='prior_attachments' value='{$ids}'>";
648
649     // workaround $mod_strings being overriden by Note object instantiation above.
650         global $current_language, $mod_strings;
651     $mod_strings = return_module_language($current_language, 'Emails');
652 }
653
654 $attJs  = '<script type="text/javascript">';
655 $attJs .= 'var lnk_remove = "'.$app_strings['LNK_REMOVE'].'";';
656 $attJs .= '</script>';
657 $xtpl->assign('ATTACHMENTS', $attachments);
658 $xtpl->assign('ATTACHMENTS_JAVASCRIPT', $attJs);
659 ////    END ATTACHMENTS
660 ///////////////////////////////////////
661
662 ///////////////////////////////////////////////////////////////////////////////
663 ////    DOCUMENTS
664 $popup_request_data = array(
665         'call_back_function' => 'document_set_return',
666         'form_name' => 'EditView',
667         'field_to_name_array' => array(
668                 'id' => 'related_doc_id',
669                 'document_name' => 'related_document_name',
670                 ),
671         );
672 $json = getJSONobj();
673 $xtpl->assign('encoded_document_popup_request_data', $json->encode($popup_request_data));
674 ////    END DOCUMENTS
675 ///////////////////////////////////////////////////////////////////////////////
676
677 $parse_open = true;
678
679 if($parse_open) {
680         $xtpl->parse('main.open_source_1');
681 }
682 ///////////////////////////////////////////////////////////////////////////////
683 ////    EMAIL TEMPLATES
684 if(ACLController::checkAccess('EmailTemplates', 'list', true) && ACLController::checkAccess('EmailTemplates', 'view', true)) {
685         $et = new EmailTemplate();
686         $etResult = $focus->db->query($et->create_new_list_query('','',array(),array(),''));
687         $email_templates_arr[] = '';
688         while($etA = $focus->db->fetchByAssoc($etResult)) {
689                 $email_templates_arr[$etA['id']] = $etA['name'];
690         }
691 } else {
692         $email_templates_arr = array('' => $app_strings['LBL_NONE']);
693 }
694
695 $xtpl->assign('EMAIL_TEMPLATE_OPTIONS', get_select_options_with_id($email_templates_arr, ''));
696 ////    END EMAIL TEMPLATES
697 ///////////////////////////////////////////////////////////////////////////////
698
699 ///////////////////////////////////////
700 ////    TEXT EDITOR
701 // cascade from User to Sys Default
702 $editor = $focus->getUserEditorPreference();
703
704 if($editor != 'plain') {
705         // this box is checked by Javascript on-load.
706         $xtpl->assign('EMAIL_EDITOR_OPTION', 'CHECKED');
707 }
708 $description_html = from_html($focus->description_html);
709 $description = $focus->description;
710
711 /////////////////////////////////////////////////
712 // signatures
713 if($sig = $current_user->getDefaultSignature()) {
714         if(!$focus->hasSignatureInBody($sig) && $focus->type != 'draft') {
715                 if($current_user->getPreference('signature_prepend')) {
716                         $description_html = '<br />'.from_html($sig['signature_html']).'<br /><br />'.$description_html;
717                         $description = "\n".$sig['signature']."\n\n".$description;
718                 } else {
719                         $description_html .= '<br /><br />'.from_html($sig['signature_html']);
720                         $description = $description."\n\n".$sig['signature'];
721                 }
722         }
723 }
724 $xtpl->assign('DESCRIPTION', $description);
725 // sigs
726 /////////////////////////////////////////////////
727 $tiny = new SugarTinyMCE();
728 $ed = $tiny->getInstance("description_html");
729 $xtpl->assign("TINY", $ed);
730 $xtpl->assign("DESCRIPTION_HTML", $description_html);
731
732 $xtpl->parse('main.htmlarea');
733 ////    END TEXT EDITOR
734 ///////////////////////////////////////
735
736 ///////////////////////////////////////
737 ////    SPECIAL INBOUND LANDING SCREEN ASSIGNS
738 if(!empty($_REQUEST['inbound_email_id'])) {
739         if(!empty($_REQUEST['start'])) {
740                 $parts = $focus->getStartPage(base64_decode($_REQUEST['start']));
741                 $xtpl->assign('RETURN_ACTION', $parts['action']);
742                 $xtpl->assign('RETURN_MODULE', $parts['module']);
743                 $xtpl->assign('GROUP', $parts['group']);
744         }
745                 $xtpl->assign('ASSIGNED_USER_ID', $current_user->id);
746                 $xtpl->assign('MYINBOX', 'this.form.type.value=\'inbound\';');
747 }
748 ////    END SPECIAL INBOUND LANDING SCREEN ASSIGNS
749 ///////////////////////////////////////
750
751 echo '<script>var disabledModules='. $json->encode($disabled_parent_types) . ';</script>';
752 $jsVars = 'var lbl_send_anyways = "'.$mod_strings['LBL_SEND_ANYWAYS'].'";';
753 $xtpl->assign('JS_VARS', $jsVars);
754 $xtpl->parse("main");
755 $xtpl->out("main");
756 echo '<script>checkParentType(document.EditView.parent_type.value, document.EditView.change_parent);</script>';
757 ////    END XTEMPLATE ASSIGNMENT
758 ///////////////////////////////////////////////////////////////////////////////
759
760
761 $javascript = new javascript();
762 $javascript->setFormName('EditView');
763 $javascript->setSugarBean($focus);
764 $skip_fields = array();
765 if($email_type == 'out') {
766         $skip_fields['name'] = 1;
767         $skip_fields['date_start'] = 1;
768 }
769 $javascript->addAllFields('',$skip_fields);
770 $javascript->addToValidateBinaryDependency('parent_name', 'alpha', $app_strings['ERR_SQS_NO_MATCH_FIELD'] . $mod_strings['LBL_MEMBER_OF'], 'false', '', 'parent_id');
771 $javascript->addToValidateBinaryDependency('parent_type', 'alpha', $app_strings['ERR_SQS_NO_MATCH_FIELD'] . $mod_strings['LBL_MEMBER_OF'], 'false', '', 'parent_id');
772 $javascript->addToValidateBinaryDependency('user_name', 'alpha', $app_strings['ERR_SQS_NO_MATCH_FIELD'] . $app_strings['LBL_ASSIGNED_TO'], 'false', '', 'assigned_user_id');
773 if($email_type == 'archived') {
774         $javascript->addFieldIsValidDate('date_start', 'date', $mod_strings['LBL_DATE'], $mod_strings['ERR_DATE_START'], true);
775         $javascript->addFieldIsValidTime('time_start', 'time', $mod_strings['LBL_TIME'], $mod_strings['ERR_TIME_SENT'], true);
776 }
777 echo $javascript->getScript();