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