]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Emails/Compose.php
Release 6.1.4
[Github/sugarcrm.git] / modules / Emails / Compose.php
1 <?php
2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4  * SugarCRM is a customer relationship management program developed by
5  * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
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
40  * Description: TODO:  To be written.
41  * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
42  * All Rights Reserved.
43  * Contributor(s): ______________________________________..
44  ********************************************************************************/
45
46 //Shorten name.
47 $data = $_REQUEST;
48
49 //For the full compose/email screen, the compose package is generated and script execution
50 //continues to the Emails/index.php page.  
51
52 if(!isset($data['forQuickCreate']))
53         $ret = generateComposeDataPackage($data);
54
55 /**
56  * Initialize the full compose window by creating the compose package 
57  * and then including Emails index.php file.  
58  *
59  * @param Array $ret
60  */
61 function initFullCompose($ret)
62 {
63         global $current_user;
64         $json = getJSONobj();
65         $composeOut = $json->encode($ret);
66
67         //For listview 'Email' call initiated by subpanels, just returned the composePackage data, do not
68         //include the entire Emails page
69         if ( isset($_REQUEST['ajaxCall']) && $_REQUEST['ajaxCall'])
70         {
71             echo $composeOut;
72         }
73         else 
74         {
75            //For normal full compose screen
76            include('modules/Emails/index.php');
77            echo "<script type='text/javascript' language='javascript'>\ncomposePackage = {$composeOut};\n</script>";
78         }
79 }
80
81 /**
82  * Generate the compose data package consumed by the full and quick compose screens.
83  *
84  * @param Array $data 
85  * @param Bool $forFullCompose If full compose is set to TRUE, then continue execution and include the full Emails UI.  Otherwise
86  *             the data generated is returned.
87  */
88 function generateComposeDataPackage($data,$forFullCompose = TRUE)
89 {
90         // we will need the following:
91         if( isset($data['parent_type']) && !empty($data['parent_type']) &&
92         isset($data['parent_id']) && !empty($data['parent_id']) &&
93         !isset($data['ListView']) && !isset($data['replyForward'])) {
94                 global $beanList;
95                 global $beanFiles;
96                 global $mod_strings;
97
98                 $parentName = '';
99                 $class = $beanList[$data['parent_type']];
100                 require_once($beanFiles[$class]);
101
102                 $bean = new $class();
103                 $bean->retrieve($data['parent_id']);
104                 if (isset($bean->full_name)) {
105                         $parentName = $bean->full_name;
106                 } elseif(isset($bean->name)) {
107                         $parentName = $bean->name;
108                 }else{
109                         $parentName = '';
110                 }
111                 $parentName = from_html($parentName);
112                 $namePlusEmail = '';
113                 if (isset($data['to_email_addrs'])) {
114                         $namePlusEmail = $data['to_email_addrs'];
115                         $namePlusEmail = from_html(str_replace("&nbsp;", " ", $namePlusEmail));
116                 } else {
117                         if (isset($bean->full_name)) {
118                                 $namePlusEmail = from_html($bean->full_name) . " <". from_html($bean->emailAddress->getPrimaryAddress($bean)).">";
119                         } else  if(isset($bean->emailAddress)){
120                                 $namePlusEmail = "<".from_html($bean->emailAddress->getPrimaryAddress($bean)).">";
121                         }
122                 }
123
124                 $subject = "";
125                 $body = "";
126                 $email_id = "";
127                 $attachments = array();
128                 if ($bean->module_dir == 'Cases') {
129                         $subject = str_replace('%1', $bean->case_number, $bean->getEmailSubjectMacro() . " ". $bean->name) ;
130                         $bean->load_relationship("contacts");
131                         $contact_ids = $bean->contacts->get();
132                         $contact = new Contact();
133                         foreach($contact_ids as $cid)
134                         {
135                                 $contact->retrieve($cid);
136                                 $namePlusEmail .= empty($namePlusEmail) ? "" : ", ";
137                                 $namePlusEmail .= from_html($contact->full_name) . " <".from_html($contact->emailAddress->getPrimaryAddress($contact)).">";
138                         }
139                 }
140                 if ($bean->module_dir == 'KBDocuments') {
141
142                         require_once("modules/Emails/EmailUI.php");
143                         $subject = $bean->kbdocument_name;
144                         $article_body = str_replace('/'.$GLOBALS['sugar_config']['cache_dir'].'images/',$GLOBALS['sugar_config']['site_url'].'/'.$GLOBALS['sugar_config']['cache_dir'].'images/',KBDocument::get_kbdoc_body_without_incrementing_count($bean->id));
145                         $body = from_html($article_body);
146                         $attachments = KBDocument::get_kbdoc_attachments_for_newemail($bean->id);
147                         $attachments = $attachments['attachments'];
148                 } // if
149                 if ($bean->module_dir == 'Quotes' && isset($data['recordId'])) {
150                         $quotesData = getQuotesRelatedData($bean,$data);
151                         global $current_language;
152                         $namePlusEmail = $quotesData['toAddress'];
153                         $subject = $quotesData['subject'];
154                         $body = $quotesData['body'];
155                         $attachments = $quotesData['attachments'];
156                         $email_id = $quotesData['email_id'];
157                 } // if
158                 $ret = array(
159                 'to_email_addrs' => $namePlusEmail,
160                 'parent_type'    => $data['parent_type'],
161                 'parent_id'          => $data['parent_id'],
162                 'parent_name'    => $parentName,
163                 'subject'                => $subject,
164                 'body'                   => $body,
165                 'attachments'    => $attachments,
166                 'email_id'               => $email_id,
167
168         );
169 } else if(isset($_REQUEST['ListView'])) {
170         
171         $email = new Email();
172         $namePlusEmail = $email->getNamePlusEmailAddressesForCompose($_REQUEST['action_module'], (explode(",", $_REQUEST['uid'])));
173         $ret = array(
174                 'to_email_addrs' => $namePlusEmail,
175                 );
176         } else if (isset($data['replyForward'])) {
177
178                 require_once("modules/Emails/EmailUI.php");
179
180                 $ret = array();
181                 $ie = new InboundEmail();
182                 $ie->email = new Email();
183                 $ie->email->email2init();
184                 $replyType = $data['reply'];
185                 $email_id = $data['record'];
186                 $ie->email->retrieve($email_id);
187                 $emailType = "";
188                 if ($ie->email->type == 'draft') {
189                         $emailType = $ie->email->type;
190                 }
191                 $ie->email->from_addr = $ie->email->from_addr_name;
192                 $ie->email->to_addrs = to_html($ie->email->to_addrs_names);
193                 $ie->email->cc_addrs = to_html($ie->email->cc_addrs_names);
194                 $ie->email->bcc_addrs = $ie->email->bcc_addrs_names;
195                 $ie->email->from_name = $ie->email->from_addr;
196                 $preBodyHTML = "&nbsp;<div><hr></div>";
197                 if ($ie->email->type != 'draft') {
198                         $email = $ie->email->et->handleReplyType($ie->email, $replyType);
199                 } else {
200                         $email = $ie->email;
201                         $preBodyHTML = "";
202                 } // else
203                 if ($ie->email->type != 'draft') {
204                         $emailHeader = $email->description;
205                 }
206                 $ret = $ie->email->et->displayComposeEmail($email);
207                 if ($ie->email->type != 'draft') {
208                         $ret['description'] = $emailHeader;
209                 }
210                 if ($replyType == 'forward' || $emailType == 'draft') {
211                         $ret = $ie->email->et->getDraftAttachments($ret);
212                 }
213                 $return = $ie->email->et->getFromAllAccountsArray($ie, $ret);
214
215                 if ($replyType == "forward") {
216                         $return['to'] = '';
217                 } else {
218                         if ($email->type != 'draft') {
219                                 $return['to'] = from_html($ie->email->from_addr);
220                         }
221                 } // else
222                 $ret = array(
223                 'to_email_addrs' => $return['to'],
224                 'parent_type'    => $return['parent_type'],
225                 'parent_id'          => $return['parent_id'],
226                 'parent_name'    => $return['parent_name'],
227                 'subject'                => $return['name'],
228                 'body'                   => $preBodyHTML . $return['description'],
229                 'attachments'    => (isset($return['attachments']) ? $return['attachments'] : array()),
230                 'email_id'               => $email_id,
231                 'fromAccounts'   => $return['fromAccounts'],
232                 );
233
234         } else {
235                 $ret = array(
236                 'to_email_addrs' => '',
237                 );
238         }
239         
240         if($forFullCompose)
241                 initFullCompose($ret);
242         else
243                 return $ret;
244 }
245
246 function getQuotesRelatedData($bean,$data) {
247         $return = array();
248         $emailId = $data['recordId'];
249         
250         require_once("modules/Emails/EmailUI.php");
251         $email = new Email();
252         $email->retrieve($emailId);
253         $return['subject'] = $email->name;
254         $return['body'] = from_html($email->description_html);
255         $return['toAddress'] = $email->to_addrs;
256         $ret = array();
257         $ret['uid'] = $emailId;
258         $ret = EmailUI::getDraftAttachments($ret);
259         $return['attachments'] = $ret['attachments'];
260         $return['email_id'] = $emailId;
261         return $return;
262 } // fn