]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Emails/EditView.php
Release 6.4.0
[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-2011 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
393         ///////////////////////////////////////////////////////////////////////////////
394         ////    QUICKSEARCH CODE
395         require_once('include/QuickSearchDefaults.php');
396         $qsd = new QuickSearchDefaults();
397         $sqs_objects = array('EditView_parent_name' => $qsd->getQSParent(),
398                                                 'EditView_assigned_user_name' => $qsd->getQSUser(),
399                                                 );
400
401
402         $json = getJSONobj();
403
404         $sqs_objects_encoded = $json->encode($sqs_objects);
405         $quicksearch_js = <<<EOQ
406                 <script type="text/javascript" language="javascript">sqs_objects = $sqs_objects_encoded; var dialog_loaded;
407                         function parent_typechangeQS() {
408                                 var new_module = document.EditView.parent_type.value;
409                                 var sqsId = 'EditView_parent_name';
410                                 if(new_module == 'Contacts' || new_module == 'Leads' || typeof(disabledModules[new_module]) != 'undefined') {
411                                         sqs_objects[sqsId]['disable'] = true;
412                                         document.getElementById('parent_name').readOnly = true;
413                                 }
414                                 else {
415                                         sqs_objects[sqsId]['disable'] = false;
416                                         document.getElementById('parent_name').readOnly = false;
417                                 }
418
419                                 sqs_objects[sqsId]['modules'] = new Array(new_module);
420                                 if (typeof(QSFieldsArray[sqsId]) != 'undefined')
421                 {
422                     QSFieldsArray[sqsId].sqs.modules = new Array(new_module);
423                 }
424                                 enableQS(false);
425                         }
426                         parent_typechangeQS();
427                 </script>
428 EOQ;
429         $xtpl->assign('JAVASCRIPT', get_set_focus_js().$quicksearch_js);
430         ////    END QUICKSEARCH CODE
431         ///////////////////////////////////////////////////////////////////////////////
432
433 // cn: bug 14191 - duping archive emails overwrites the original
434 if(!isset($_REQUEST['isDuplicate']) || $_REQUEST['isDuplicate'] != 'true') {
435         $xtpl->assign('ID', $focus->id);
436 }
437
438 if(isset($_REQUEST['parent_type']) && !empty($_REQUEST['parent_type']) && isset($_REQUEST['parent_id']) && !empty($_REQUEST['parent_id'])) {
439         $xtpl->assign('OBJECT_ID', $_REQUEST['parent_id']);
440         $xtpl->assign('OBJECT_TYPE', $_REQUEST['parent_type']);
441 }
442 $xtpl->assign('FROM_ADDR', $focus->from_addr);
443 //// prevent TO: prefill when type is 'forward'
444 if($email_type != 'forward') {
445         $xtpl->assign('TO_ADDRS', $focus->to_addrs);
446         $xtpl->assign('TO_ADDRS_IDS', $focus->to_addrs_ids);
447         $xtpl->assign('TO_ADDRS_NAMES', $focus->to_addrs_names);
448         $xtpl->assign('TO_ADDRS_EMAILS', $focus->to_addrs_emails);
449         $xtpl->assign('CC_ADDRS', $focus->cc_addrs);
450         $xtpl->assign('CC_ADDRS_IDS', $focus->cc_addrs_ids);
451         $xtpl->assign('CC_ADDRS_NAMES', $focus->cc_addrs_names);
452         $xtpl->assign('CC_ADDRS_EMAILS', $focus->cc_addrs_emails);
453         $xtpl->assign('BCC_ADDRS', $focus->bcc_addrs);
454         $xtpl->assign('BCC_ADDRS_IDS', $focus->bcc_addrs_ids);
455         $xtpl->assign('BCC_ADDRS_NAMES', $focus->bcc_addrs_names);
456         $xtpl->assign('BCC_ADDRS_EMAILS', $focus->bcc_addrs_emails);
457 }
458
459 //$xtpl->assign('FROM_ADDR', $from['name'].' <'.$from['email'].'>');
460 $xtpl->assign('FROM_ADDR_NAME', $from['name']);
461 $xtpl->assign('FROM_ADDR_EMAIL', $from['email']);
462
463 $xtpl->assign('NAME', from_html($name));
464 //$xtpl->assign('DESCRIPTION_HTML', from_html($focus->description_html));
465 $xtpl->assign('DESCRIPTION', $focus->description);
466 $xtpl->assign('TYPE',$email_type);
467
468 // Unimplemented until jscalendar language files are fixed
469 // $xtpl->assign('CALENDAR_LANG',((empty($cal_codes[$current_language])) ? $cal_codes[$default_language] : $cal_codes[$current_language]));
470 $xtpl->assign('CALENDAR_LANG', 'en');
471 $xtpl->assign('CALENDAR_DATEFORMAT', $timedate->get_cal_date_format());
472 $xtpl->assign('DATE_START', $focus->date_start);
473 $xtpl->assign('TIME_FORMAT', '('. $timedate->get_user_time_format().')');
474 $xtpl->assign('TIME_START', substr($focus->time_start,0,5));
475 $xtpl->assign('TIME_MERIDIEM', $timedate->AMPMMenu('',$focus->time_start));
476
477 $parent_types = $app_list_strings['record_type_display'];
478 $disabled_parent_types = ACLController::disabledModuleList($parent_types,false, 'list');
479
480 foreach($disabled_parent_types as $disabled_parent_type){
481         if($disabled_parent_type != $focus->parent_type){
482                 unset($parent_types[$disabled_parent_type]);
483         }
484 }
485
486 $xtpl->assign('TYPE_OPTIONS', get_select_options_with_id($parent_types, $focus->parent_type));
487 $xtpl->assign('USER_DATEFORMAT', '('. $timedate->get_user_date_format().')');
488 $xtpl->assign('PARENT_NAME', $focus->parent_name);
489 $xtpl->assign('PARENT_ID', $focus->parent_id);
490 if(empty($focus->parent_type)) {
491         $xtpl->assign('PARENT_RECORD_TYPE', '');
492 } else {
493         $xtpl->assign('PARENT_RECORD_TYPE', $focus->parent_type);
494 }
495
496 if(is_admin($current_user) && $_REQUEST['module'] != 'DynamicLayout' && !empty($_SESSION['editinplace'])){
497         $record = '';
498         if(!empty($_REQUEST['record'])){
499                 $record =       $_REQUEST['record'];
500         }
501         $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>");
502 }
503
504 ////    END GENERAL TEMPLATE ASSIGNMENTS
505 ///////////////////////////////////////////////////////////////////////////////
506
507
508 ///////////////////////////////////////
509 ///
510 /// SETUP PARENT POPUP
511
512 $popup_request_data = array(
513         'call_back_function' => 'set_return',
514         'form_name' => 'EditView',
515         'field_to_name_array' => array(
516                 'id' => 'parent_id',
517                 'name' => 'parent_name',
518                 ),
519         );
520
521 $encoded_popup_request_data = $json->encode($popup_request_data);
522
523 /// Users Popup
524 $popup_request_data = array(
525         'call_back_function' => 'set_return',
526         'form_name' => 'EditView',
527         'field_to_name_array' => array(
528                 'id' => 'assigned_user_id',
529                 'user_name' => 'assigned_user_name',
530                 ),
531         );
532 $xtpl->assign('encoded_users_popup_request_data', $json->encode($popup_request_data));
533
534
535 //
536 ///////////////////////////////////////
537
538 $change_parent_button = '<input type="button" name="button" tabindex="2" class="button" '
539         . 'title="' . $app_strings['LBL_SELECT_BUTTON_TITLE'] . '" '
540         . 'accesskey="' . $app_strings['LBL_SELECT_BUTTON_KEY'] . '" '
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         . 'accesskey="' . $app_strings['LBL_SELECT_BUTTON_KEY'] . '" '
562         . 'value="'     . $mod_strings['LBL_EMAIL_SELECTOR'] . '" '
563         . "onclick='button_change_onclick(this);' $button_attr />\n";
564 $xtpl->assign("CHANGE_TO_ADDRS_BUTTON", $change_to_addrs_button);
565
566 $change_cc_addrs_button = '<input type="button" name="cc_button" tabindex="3" class="button" '
567         . 'title="' . $app_strings['LBL_SELECT_BUTTON_TITLE'] . '" '
568         . 'accesskey="' . $app_strings['LBL_SELECT_BUTTON_KEY'] . '" '
569         . 'value="'     . $mod_strings['LBL_EMAIL_SELECTOR'] . '" '
570         . "onclick='button_change_onclick(this);' $button_attr />\n";
571 $xtpl->assign("CHANGE_CC_ADDRS_BUTTON", $change_cc_addrs_button);
572
573 $change_bcc_addrs_button = '<input type="button" name="bcc_button" tabindex="3" class="button" '
574         . 'title="' . $app_strings['LBL_SELECT_BUTTON_TITLE'] . '" '
575         . 'accesskey="' . $app_strings['LBL_SELECT_BUTTON_KEY'] . '" '
576         . 'value="'     . $mod_strings['LBL_EMAIL_SELECTOR'] . '" '
577         . "onclick='button_change_onclick(this);' $button_attr />\n";
578 $xtpl->assign("CHANGE_BCC_ADDRS_BUTTON", $change_bcc_addrs_button);
579
580
581 ///////////////////////////////////////
582 ////    USER ASSIGNMENT
583 global $current_user;
584 if(is_admin($current_user) && $_REQUEST['module'] != 'DynamicLayout' && !empty($_SESSION['editinplace'])) {
585         $record = '';
586         if(!empty($_REQUEST['record'])) {
587                 $record = $_REQUEST['record'];
588         }
589         $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>");
590 }
591
592 if(empty($focus->assigned_user_id) && empty($focus->id))
593         $focus->assigned_user_id = $current_user->id;
594 if(empty($focus->assigned_name) && empty($focus->id))
595         $focus->assigned_user_name = $current_user->user_name;
596 $xtpl->assign('ASSIGNED_USER_OPTIONS', get_select_options_with_id(get_user_array(TRUE, 'Active', $focus->assigned_user_id), $focus->assigned_user_id));
597 $xtpl->assign('ASSIGNED_USER_NAME', $focus->assigned_user_name);
598 $xtpl->assign('ASSIGNED_USER_ID', $focus->assigned_user_id);
599 $xtpl->assign('DURATION_HOURS', $focus->duration_hours);
600 $xtpl->assign('TYPE_OPTIONS', get_select_options_with_id($parent_types, $focus->parent_type));
601
602 if(isset($focus->duration_minutes)) {
603         $xtpl->assign('DURATION_MINUTES_OPTIONS', get_select_options_with_id($focus->minutes_values,$focus->duration_minutes));
604 }
605 ////    END USER ASSIGNMENT
606 ///////////////////////////////////////
607
608
609
610 //Add Custom Fields
611 require_once('modules/DynamicFields/templates/Files/EditView.php');
612
613
614 ///////////////////////////////////////
615 ////    ATTACHMENTS
616 $attachments = '';
617 if(!empty($focus->id) || (!empty($_REQUEST['record']) && $_REQUEST['type'] == 'forward')) {
618
619         $attachments = "<input type='hidden' name='removeAttachment' id='removeAttachment' value=''>\n";
620         $ids = '';
621
622         $focusId = empty($focus->id) ? $_REQUEST['record'] : $focus->id;
623         $note = new Note();
624         $where = "notes.parent_id='{$focusId}' AND notes.filename IS NOT NULL";
625         $notes_list = $note->get_full_list("", $where,true);
626
627         if(!isset($notes_list)) {
628                 $notes_list = array();
629         }
630         for($i = 0;$i < count($notes_list);$i++) {
631                 $the_note = $notes_list[$i];
632                 if(empty($the_note->filename)) {
633                         continue;
634                 }
635
636                 // cn: bug 8034 - attachments from forwards/replies lost when saving drafts
637                 if(!empty($ids)) {
638                         $ids .= ",";
639                 }
640                 $ids .= $the_note->id;
641
642                 $attachments .= "
643                         <div id='noteDiv{$the_note->id}'>
644                                 " . SugarThemeRegistry::current()->getImage('delete_inline', "onclick='deletePriorAttachment(\"{$the_note->id}\");' value='{$the_note->id}'", null, null, ".gif", $mod_strings['LBL_DELETE_INLINE']) . "&nbsp;";
645                 $attachments .= "<a href=\"index.php?entryPoint=download&id=".$the_note->id."&type=Notes\">".$the_note->name."</a><div />";
646                 //$attachments .= '<a href="'.UploadFile::get_url($the_note->filename,$the_note->id).'&entryPoint=download&type=Notes' . '" target="_blank">'. $the_note->filename .'</a></div>';
647
648         }
649         // cn: bug 8034 - attachments from forwards/replies lost when saving drafts
650         $attachments .= "<input type='hidden' name='prior_attachments' value='{$ids}'>";
651
652     // workaround $mod_strings being overriden by Note object instantiation above.
653         global $current_language, $mod_strings;
654     $mod_strings = return_module_language($current_language, 'Emails');
655 }
656
657 $attJs  = '<script type="text/javascript">';
658 $attJs .= 'var lnk_remove = "'.$app_strings['LNK_REMOVE'].'";';
659 $attJs .= '</script>';
660 $xtpl->assign('ATTACHMENTS', $attachments);
661 $xtpl->assign('ATTACHMENTS_JAVASCRIPT', $attJs);
662 ////    END ATTACHMENTS
663 ///////////////////////////////////////
664
665 ///////////////////////////////////////////////////////////////////////////////
666 ////    DOCUMENTS
667 $popup_request_data = array(
668         'call_back_function' => 'document_set_return',
669         'form_name' => 'EditView',
670         'field_to_name_array' => array(
671                 'id' => 'related_doc_id',
672                 'document_name' => 'related_document_name',
673                 ),
674         );
675 $json = getJSONobj();
676 $xtpl->assign('encoded_document_popup_request_data', $json->encode($popup_request_data));
677 ////    END DOCUMENTS
678 ///////////////////////////////////////////////////////////////////////////////
679
680 $parse_open = true;
681
682 if($parse_open) {
683         $xtpl->parse('main.open_source_1');
684 }
685 ///////////////////////////////////////////////////////////////////////////////
686 ////    EMAIL TEMPLATES
687 if(ACLController::checkAccess('EmailTemplates', 'list', true) && ACLController::checkAccess('EmailTemplates', 'view', true)) {
688         $et = new EmailTemplate();
689         $etResult = $focus->db->query($et->create_new_list_query('','',array(),array(),''));
690         $email_templates_arr[] = '';
691         while($etA = $focus->db->fetchByAssoc($etResult)) {
692                 $email_templates_arr[$etA['id']] = $etA['name'];
693         }
694 } else {
695         $email_templates_arr = array('' => $app_strings['LBL_NONE']);
696 }
697
698 $xtpl->assign('EMAIL_TEMPLATE_OPTIONS', get_select_options_with_id($email_templates_arr, ''));
699 ////    END EMAIL TEMPLATES
700 ///////////////////////////////////////////////////////////////////////////////
701
702 ///////////////////////////////////////
703 ////    TEXT EDITOR
704 // cascade from User to Sys Default
705 $editor = $focus->getUserEditorPreference();
706
707 if($editor != 'plain') {
708         // this box is checked by Javascript on-load.
709         $xtpl->assign('EMAIL_EDITOR_OPTION', 'CHECKED');
710 }
711 $description_html = from_html($focus->description_html);
712 $description = $focus->description;
713
714 /////////////////////////////////////////////////
715 // signatures
716 if($sig = $current_user->getDefaultSignature()) {
717         if(!$focus->hasSignatureInBody($sig) && $focus->type != 'draft') {
718                 if($current_user->getPreference('signature_prepend')) {
719                         $description_html = '<br />'.from_html($sig['signature_html']).'<br /><br />'.$description_html;
720                         $description = "\n".$sig['signature']."\n\n".$description;
721                 } else {
722                         $description_html .= '<br /><br />'.from_html($sig['signature_html']);
723                         $description = $description."\n\n".$sig['signature'];
724                 }
725         }
726 }
727 $xtpl->assign('DESCRIPTION', $description);
728 // sigs
729 /////////////////////////////////////////////////
730 $tiny = new SugarTinyMCE();
731 $ed = $tiny->getInstance("description_html");
732 $xtpl->assign("TINY", $ed);
733 $xtpl->assign("DESCRIPTION_HTML", $description_html);
734
735 $xtpl->parse('main.htmlarea');
736 ////    END TEXT EDITOR
737 ///////////////////////////////////////
738
739 ///////////////////////////////////////
740 ////    SPECIAL INBOUND LANDING SCREEN ASSIGNS
741 if(!empty($_REQUEST['inbound_email_id'])) {
742         if(!empty($_REQUEST['start'])) {
743                 $parts = $focus->getStartPage(base64_decode($_REQUEST['start']));
744                 $xtpl->assign('RETURN_ACTION', $parts['action']);
745                 $xtpl->assign('RETURN_MODULE', $parts['module']);
746                 $xtpl->assign('GROUP', $parts['group']);
747         }
748                 $xtpl->assign('ASSIGNED_USER_ID', $current_user->id);
749                 $xtpl->assign('MYINBOX', 'this.form.type.value=\'inbound\';');
750 }
751 ////    END SPECIAL INBOUND LANDING SCREEN ASSIGNS
752 ///////////////////////////////////////
753
754 echo '<script>var disabledModules='. $json->encode($disabled_parent_types) . ';</script>';
755 $jsVars = 'var lbl_send_anyways = "'.$mod_strings['LBL_SEND_ANYWAYS'].'";';
756 $xtpl->assign('JS_VARS', $jsVars);
757 $xtpl->parse("main");
758 $xtpl->out("main");
759 echo '<script>checkParentType(document.EditView.parent_type.value, document.EditView.change_parent);</script>';
760 ////    END XTEMPLATE ASSIGNMENT
761 ///////////////////////////////////////////////////////////////////////////////
762
763
764 $javascript = new javascript();
765 $javascript->setFormName('EditView');
766 $javascript->setSugarBean($focus);
767 $skip_fields = array();
768 if($email_type == 'out') {
769         $skip_fields['name'] = 1;
770         $skip_fields['date_start'] = 1;
771 }
772 $javascript->addAllFields('',$skip_fields);
773 $javascript->addToValidateBinaryDependency('parent_name', 'alpha', $app_strings['ERR_SQS_NO_MATCH_FIELD'] . $mod_strings['LBL_MEMBER_OF'], 'false', '', 'parent_id');
774 $javascript->addToValidateBinaryDependency('parent_type', 'alpha', $app_strings['ERR_SQS_NO_MATCH_FIELD'] . $mod_strings['LBL_MEMBER_OF'], 'false', '', 'parent_id');
775 $javascript->addToValidateBinaryDependency('user_name', 'alpha', $app_strings['ERR_SQS_NO_MATCH_FIELD'] . $app_strings['LBL_ASSIGNED_TO'], 'false', '', 'assigned_user_id');
776 if($email_type == 'archived') {
777         $javascript->addFieldIsValidDate('date_start', 'date', $mod_strings['LBL_DATE'], $mod_strings['ERR_DATE_START'], true);
778         $javascript->addFieldIsValidTime('time_start', 'time', $mod_strings['LBL_TIME'], $mod_strings['ERR_TIME_SENT'], true);
779 }
780 echo $javascript->getScript();