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