]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Emails/Save.php
Release 6.2.0
[Github/sugarcrm.git] / modules / Emails / Save.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:
41  * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc. All Rights
42  * Reserved. Contributor(s): ______________________________________..
43  *********************************************************************************/
44
45
46
47
48
49 ///////////////////////////////////////////////////////////////////////////////
50 ////    EMAIL SEND/SAVE SETUP
51 $focus = new Email();
52
53 if(!isset($prefix)) {
54         $prefix = '';
55 }
56 if(isset($_POST[$prefix.'meridiem']) && !empty($_POST[$prefix.'meridiem'])) {
57         $_POST[$prefix.'time_start'] = $timedate->merge_time_meridiem($_POST[$prefix.'time_start'], $timedate->get_time_format(), $_POST[$prefix.'meridiem']);
58 }
59 //retrieve the record
60 if(isset($_POST['record']) && !empty($_POST['record'])) {
61         $focus->retrieve($_POST['record']);
62
63 }
64 if(isset($_REQUEST['user_id'])) {
65         $focus->assigned_user_id = $_REQUEST['user_id'];
66 }
67 if(!$focus->ACLAccess('Save')){
68                 ACLController::displayNoAccess(true);
69                 sugar_cleanup(true);
70 }
71 if(!empty($_POST['assigned_user_id']) && ($focus->assigned_user_id != $_POST['assigned_user_id']) && ($_POST['assigned_user_id'] != $current_user->id)) {
72         $check_notify = TRUE;
73 }
74 //populate the fields of this Email
75 $allfields = array_merge($focus->column_fields, $focus->additional_column_fields);
76 foreach($allfields as $field) {
77         if(isset($_POST[$field])) {
78                 $value = $_POST[$field];
79                 $focus->$field = $value;
80         }
81 }
82
83 $focus->description = $_REQUEST['description_html'];
84 $focus->description_html = $_REQUEST['description_html'];
85
86 if (!isset($_REQUEST['to_addrs'])) {
87         $_REQUEST['to_addrs'] = "";
88 }
89 if (!isset($_REQUEST['to_addrs_ids'])) {
90         $_REQUEST['to_addrs_ids'] = "";
91 }
92 if (!isset($_REQUEST['to_addrs_names'])) {
93         $_REQUEST['to_addrs_names'] = "";
94 }
95 if (!isset($_REQUEST['to_addrs_emails'])) {
96         $_REQUEST['to_addrs_emails'] = "";
97 }
98
99 //compare the 3 fields and return list of contact_ids to link:
100 $focus->to_addrs_arr = $focus->parse_addrs($_REQUEST['to_addrs'], $_REQUEST['to_addrs_ids'], $_REQUEST['to_addrs_names'], $_REQUEST['to_addrs_emails']);
101
102 // make sure the cc_* and bcc_* fields are at least empty if not set
103 $fields_to_check = array(
104         'cc_addrs',
105         'cc_addrs_ids',
106         'bcc_addrs',
107         'bcc_addrs_ids',
108         'cc_addrs_names',
109         'cc_addrs_emails',
110         'bcc_addrs_emails',
111 );
112 foreach ($fields_to_check as $field_to_check) {
113         if (!isset($_REQUEST[$field_to_check])) {
114                 $_REQUEST[$field_to_check] = '';
115         }
116 }
117
118 $focus->cc_addrs_arr = $focus->parse_addrs($_REQUEST['cc_addrs'], $_REQUEST['cc_addrs_ids'], $_REQUEST['cc_addrs_names'], $_REQUEST['cc_addrs_emails']);
119 $focus->bcc_addrs_arr = $focus->parse_addrs($_REQUEST['bcc_addrs'], $_REQUEST['bcc_addrs_ids'], $_REQUEST['to_addrs_names'], $_REQUEST['bcc_addrs_emails']);
120
121
122 if(!empty($_REQUEST['type'])) {
123         $focus->type = $_REQUEST['type'];
124 } elseif(empty($focus->type)) { // cn: from drafts/quotes
125         $focus->type = 'archived';
126 }
127
128 ///////////////////////////////////////////////////////////////////////////////
129 ////    TEMPLATE PARSING
130 // cn: bug 7244 - need to pass an empty bean to parse email templates
131 $object_arr = array();
132 if(!empty($focus->parent_id)) {
133         $object_arr[$focus->parent_type] = $focus->parent_id;
134 }
135 if(isset($focus->to_addrs_arr[0]['contact_id'])) {
136         $object_arr['Contacts'] = $focus->to_addrs_arr[0]['contact_id'];
137 }
138 if(empty($object_arr)) {
139         $object_arr = array('Contacts' => '123');
140 }
141
142 // do not parse email templates if the email is being saved as draft....
143 if($focus->type != 'draft' && count($object_arr) > 0) {
144         require_once($beanFiles['EmailTemplate']);
145         $focus->name = EmailTemplate::parse_template($focus->name, $object_arr);
146         $focus->description = EmailTemplate::parse_template($focus->description, $object_arr);
147         $focus->description_html = EmailTemplate::parse_template($focus->description_html, $object_arr);
148 }
149 ////    END TEMPLATE PARSING
150 ///////////////////////////////////////////////////////////////////////////////
151
152 ///////////////////////////////////////////////////////////////////////////////
153 ////    PREP FOR ATTACHMENTS
154 if(empty($focus->id)){
155     $focus->id = create_guid();
156     $focus->new_with_id = true;
157 }
158 ///////////////////////////////////////////////////////////////////////////////
159
160 ///////////////////////////////////////////////////////////////////////////////
161 ////    ATTACHMENT HANDLING
162 $focus->handleAttachments();
163 ////    END ATTACHMENT HANDLING
164 ///////////////////////////////////////////////////////////////////////////////
165 $focus->status = 'draft';
166 if($focus->type == 'archived' ) {
167         $focus->status= 'archived';
168         $focus->date_start = $_REQUEST['date_start'];
169         $focus->time_start = $_REQUEST['time_start'] . $_REQUEST['meridiem'];
170 } elseif(($focus->type == 'out' || $focus->type == 'forward') && isset($_REQUEST['send']) && $_REQUEST['send'] == '1') {
171         ///////////////////////////////////////////////////////////////////////////
172         ////    REPLY PROCESSING
173         $old = array('&lt;','&gt;');
174         $new = array('<','>');
175
176         if($_REQUEST['from_addr'] != $_REQUEST['from_addr_name'].' &lt;'.$_REQUEST['from_addr_email'].'&gt;') {
177                 if(false === strpos($_REQUEST['from_addr'], '&lt;')) { // we have an email only?
178                         $focus->from_addr = $_REQUEST['from_addr'];
179                         $focus->from_name = '';
180                 } else { // we have a compound string
181                         $newFromAddr =  str_replace($old, $new, $_REQUEST['from_addr']);
182                         $focus->from_addr = substr($newFromAddr, (1 + strpos($newFromAddr, '<')), (strpos($newFromAddr, '>') - strpos($newFromAddr, '<')) -1 );
183                         $focus->from_name = substr($newFromAddr, 0, (strpos($newFromAddr, '<') -1));
184                 }
185         } elseif(!empty($_REQUEST['from_addr_email']) && isset($_REQUEST['from_addr_email'])) {
186                 $focus->from_addr = $_REQUEST['from_addr_email'];
187                 $focus->from_name = $_REQUEST['from_addr_name'];
188         } else {
189                 $focus->from_addr = $focus->getSystemDefaultEmail();
190         }
191         ////    REPLY PROCESSING
192         ///////////////////////////////////////////////////////////////////////////
193         if($focus->send()) {
194         $focus->status = 'sent';
195         } else {
196                 $focus->status = 'send_error';
197         }
198 }
199 $focus->to_addrs = $_REQUEST['to_addrs'];
200 $focus->cc_addrs = $_REQUEST['cc_addrs'];
201 $focus->bcc_addrs = $_REQUEST['bcc_addrs'];
202 $focus->from_addr = $_REQUEST['from_addr'];
203
204 // delete the existing relationship of all the email addresses with this email
205 $query = "update emails_email_addr_rel set deleted = 1 WHERE email_id = '{$focus->id}'";
206 $focus->db->query($query);
207
208 // delete al the relationship of this email with all the beans
209 //$query = "update emails_beans set deleted = 1, bean_id = '', bean_module = '' WHERE email_id = '{$focus->id}'";
210 //$focus->db->query($query);
211
212 if(isset($_REQUEST['object_type']) && !empty($_REQUEST['object_type']) && isset($_REQUEST['object_id']) && !empty($_REQUEST['object_id'])) {
213     //run linking code only if the object_id has not been linked as part of the contacts above and it is an OOB relationship
214     $GLOBALS['log']->debug("CESELY".$_REQUEST['object_type']);
215     if(!in_array($_REQUEST['object_id'],$exContactIds)){
216         $rel = strtolower($_REQUEST['object_type']);
217         if ($focus->load_relationship($rel)) {
218                 $focus->$rel->add($_REQUEST['object_id']);
219                 $GLOBALS['log']->debug("CESELY LOADED".$_REQUEST['object_type']);
220         }
221     }
222 }
223 ////    END RELATIONSHIP LINKING
224 ///////////////////////////////////////////////////////////////////////////////
225
226 // If came from email archiving edit view, this would have been set from form input.
227 if (!isset($focus->date_start))
228 {
229     $timedate = TimeDate::getInstance();
230         list($focus->date_start,  $focus->time_start) = $timedate->split_date_time($timedate->now());
231 }
232
233 $focus->date_sent = "";
234
235 require_once('include/formbase.php');
236 $focus = populateFromPost('', $focus);
237
238 $focus->save(false);
239 ////    END EMAIL SAVE/SEND SETUP
240 ///////////////////////////////////////////////////////////////////////////////
241
242 ///////////////////////////////////////////////////////////////////////////////
243 ////    RELATIONSHIP LINKING
244 $focus->load_relationship('users');
245 $focus->users->add($current_user->id);
246
247 if(!empty($_REQUEST['to_addrs_ids'])) {
248         $focus->load_relationship('contacts');
249         $exContactIds = explode(';', $_REQUEST['to_addrs_ids']);
250         foreach($exContactIds as $contactId) {
251                 $contactId = trim($contactId);
252                 $focus->contacts->add($contactId);
253         }
254 }
255
256 ///////////////////////////////////////////////////////////////////////////////
257 ////    PAGE REDIRECTION
258 ///////////////////////////////////////////////////////////////////////////////
259 $return_id = $focus->id;
260
261 if(empty($_POST['return_module'])) {
262         $return_module = "Emails";
263 } else {
264         $return_module = $_POST['return_module'];
265 }
266 if(empty($_POST['return_action'])) {
267         $return_action = "DetailView";
268 } else {
269         $return_action = $_POST['return_action'];
270 }
271 $GLOBALS['log']->debug("Saved record with id of ".$return_id);
272
273 if($focus->type == 'draft') {
274         if($return_module == 'Emails') {
275                 header("Location: index.php?module=$return_module&action=ListViewDrafts");
276         } else {
277         handleRedirect($return_id, 'Emails');
278         }
279 } elseif($focus->type == 'out') {
280         if($return_module == 'Home') {
281                 header('Location: index.php?module='.$return_module.'&action=index');
282         }
283         if(!empty($_REQUEST['return_id'])) {
284                 $return_id = $_REQUEST['return_id'];
285         }
286         header('Location: index.php?action='.$return_action.'&module='.$return_module.'&record='.$return_id.'&assigned_user_id='.$current_user->id.'&type=inbound');
287 } elseif(isset($_POST['return_id']) && $_POST['return_id'] != "") {
288         $return_id = $_POST['return_id'];
289 }
290         header("Location: index.php?action=$return_action&module=$return_module&record=$return_id");
291 ?>