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