]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Emails/Compose.php
Release 6.4.0
[Github/sugarcrm.git] / modules / Emails / Compose.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
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  * @param SugarBean $bean Optional - parent object with data
92  */
93 function generateComposeDataPackage($data,$forFullCompose = TRUE, $bean = null)
94 {
95         // we will need the following:
96         if( isset($data['parent_type']) && !empty($data['parent_type']) &&
97         isset($data['parent_id']) && !empty($data['parent_id']) &&
98         !isset($data['ListView']) && !isset($data['replyForward'])) {
99             if(empty($bean)) {
100                 global $beanList;
101                 global $beanFiles;
102                 global $mod_strings;
103
104                 $parentName = '';
105                 $class = $beanList[$data['parent_type']];
106                 require_once($beanFiles[$class]);
107
108                 $bean = new $class();
109                 $bean->retrieve($data['parent_id']);
110             }
111                 if (isset($bean->full_name)) {
112                         $parentName = $bean->full_name;
113                 } elseif(isset($bean->name)) {
114                         $parentName = $bean->name;
115                 }else{
116                         $parentName = '';
117                 }
118                 $parentName = from_html($parentName);
119                 $namePlusEmail = '';
120                 if (isset($data['to_email_addrs'])) {
121                         $namePlusEmail = $data['to_email_addrs'];
122                         $namePlusEmail = from_html(str_replace("&nbsp;", " ", $namePlusEmail));
123                 } else {
124                         if (isset($bean->full_name)) {
125                                 $namePlusEmail = from_html($bean->full_name) . " <". from_html($bean->emailAddress->getPrimaryAddress($bean)).">";
126                         } else  if(isset($bean->emailAddress)){
127                                 $namePlusEmail = "<".from_html($bean->emailAddress->getPrimaryAddress($bean)).">";
128                         }
129                 }
130
131                 $subject = "";
132                 $body = "";
133                 $email_id = "";
134                 $attachments = array();
135                 if ($bean->module_dir == 'Cases') {
136                         $subject = str_replace('%1', $bean->case_number, $bean->getEmailSubjectMacro() . " ". from_html($bean->name)) ;//bug 41928
137                         $bean->load_relationship("contacts");
138                         $contact_ids = $bean->contacts->get();
139                         $contact = new Contact();
140                         foreach($contact_ids as $cid)
141                         {
142                                 $contact->retrieve($cid);
143                                 $namePlusEmail .= empty($namePlusEmail) ? "" : ", ";
144                                 $namePlusEmail .= from_html($contact->full_name) . " <".from_html($contact->emailAddress->getPrimaryAddress($contact)).">";
145                         }
146                 }
147                 if ($bean->module_dir == 'KBDocuments') {
148
149                         require_once("modules/Emails/EmailUI.php");
150                         $subject = $bean->kbdocument_name;
151                         $article_body = str_replace('/cache/images/',$GLOBALS['sugar_config']['site_url'].'/cache/images/',KBDocument::get_kbdoc_body_without_incrementing_count($bean->id));
152                         $body = from_html($article_body);
153                         $attachments = KBDocument::get_kbdoc_attachments_for_newemail($bean->id);
154                         $attachments = $attachments['attachments'];
155                 } // if
156                 if ($bean->module_dir == 'Quotes' && isset($data['recordId'])) {
157                         $quotesData = getQuotesRelatedData($bean,$data);
158                         global $current_language;
159                         $namePlusEmail = $quotesData['toAddress'];
160                         $subject = $quotesData['subject'];
161                         $body = $quotesData['body'];
162                         $attachments = $quotesData['attachments'];
163                         $email_id = $quotesData['email_id'];
164                 } // if
165                 $ret = array(
166                 'to_email_addrs' => $namePlusEmail,
167                 'parent_type'    => $data['parent_type'],
168                 'parent_id'          => $data['parent_id'],
169                 'parent_name'    => $parentName,
170                 'subject'                => $subject,
171                 'body'                   => $body,
172                 'attachments'    => $attachments,
173                 'email_id'               => $email_id,
174
175         );
176 } else if(isset($_REQUEST['ListView'])) {
177
178         $email = new Email();
179         $namePlusEmail = $email->getNamePlusEmailAddressesForCompose($_REQUEST['action_module'], (explode(",", $_REQUEST['uid'])));
180         $ret = array(
181                 'to_email_addrs' => $namePlusEmail,
182                 );
183         } else if (isset($data['replyForward'])) {
184
185                 require_once("modules/Emails/EmailUI.php");
186
187                 $ret = array();
188                 $ie = new InboundEmail();
189                 $ie->email = new Email();
190                 $ie->email->email2init();
191                 $replyType = $data['reply'];
192                 $email_id = $data['record'];
193                 $ie->email->retrieve($email_id);
194                 $emailType = "";
195                 if ($ie->email->type == 'draft') {
196                         $emailType = $ie->email->type;
197                 }
198                 $ie->email->from_addr = $ie->email->from_addr_name;
199                 $ie->email->to_addrs = to_html($ie->email->to_addrs_names);
200                 $ie->email->cc_addrs = to_html($ie->email->cc_addrs_names);
201                 $ie->email->bcc_addrs = $ie->email->bcc_addrs_names;
202                 $ie->email->from_name = $ie->email->from_addr;
203                 $preBodyHTML = "&nbsp;<div><hr></div>";
204                 if ($ie->email->type != 'draft') {
205                         $email = $ie->email->et->handleReplyType($ie->email, $replyType);
206                 } else {
207                         $email = $ie->email;
208                         $preBodyHTML = "";
209                 } // else
210                 if ($ie->email->type != 'draft') {
211                         $emailHeader = $email->description;
212                 }
213                 $ret = $ie->email->et->displayComposeEmail($email);
214                 if ($ie->email->type != 'draft') {
215                         $ret['description'] = $emailHeader;
216                 }
217                 if ($replyType == 'forward' || $emailType == 'draft') {
218                         $ret = $ie->email->et->getDraftAttachments($ret);
219                 }
220                 $return = $ie->email->et->getFromAllAccountsArray($ie, $ret);
221
222                 if ($replyType == "forward") {
223                         $return['to'] = '';
224                 } else {
225                         if ($email->type != 'draft') {
226                                 $return['to'] = from_html($ie->email->from_addr);
227                         }
228                 } // else
229                 $ret = array(
230                 'to_email_addrs' => $return['to'],
231                 'parent_type'    => $return['parent_type'],
232                 'parent_id'          => $return['parent_id'],
233                 'parent_name'    => $return['parent_name'],
234                 'subject'                => $return['name'],
235                 'body'                   => $preBodyHTML . $return['description'],
236                 'attachments'    => (isset($return['attachments']) ? $return['attachments'] : array()),
237                 'email_id'               => $email_id,
238                 'fromAccounts'   => $return['fromAccounts'],
239                 );
240
241         } else {
242                 $ret = array(
243                 'to_email_addrs' => '',
244                 );
245         }
246
247         if($forFullCompose)
248                 initFullCompose($ret);
249         else
250                 return $ret;
251 }
252
253 function getQuotesRelatedData($bean,$data) {
254         $return = array();
255         $emailId = $data['recordId'];
256
257         require_once("modules/Emails/EmailUI.php");
258         $email = new Email();
259         $email->retrieve($emailId);
260         $return['subject'] = $email->name;
261         $return['body'] = from_html($email->description_html);
262         $return['toAddress'] = $email->to_addrs;
263         $ret = array();
264         $ret['uid'] = $emailId;
265         $ret = EmailUI::getDraftAttachments($ret);
266         $return['attachments'] = $ret['attachments'];
267         $return['email_id'] = $emailId;
268         return $return;
269 } // fn