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.
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.
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
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
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.
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.
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 ********************************************************************************/
38 /*********************************************************************************
41 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc. All Rights
42 * Reserved. Contributor(s): ______________________________________..
43 *********************************************************************************/
49 ///////////////////////////////////////////////////////////////////////////////
50 //// EMAIL SEND/SAVE SETUP
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']);
60 if(isset($_POST['record']) && !empty($_POST['record'])) {
61 $focus->retrieve($_POST['record']);
64 if(isset($_REQUEST['user_id'])) {
65 $focus->assigned_user_id = $_REQUEST['user_id'];
67 if(!$focus->ACLAccess('Save')){
68 ACLController::displayNoAccess(true);
71 if(!empty($_POST['assigned_user_id']) && ($focus->assigned_user_id != $_POST['assigned_user_id']) && ($_POST['assigned_user_id'] != $current_user->id)) {
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;
83 $focus->description = $_REQUEST['description_html'];
84 $focus->description_html = $_REQUEST['description_html'];
86 if (!isset($_REQUEST['to_addrs'])) {
87 $_REQUEST['to_addrs'] = "";
89 if (!isset($_REQUEST['to_addrs_ids'])) {
90 $_REQUEST['to_addrs_ids'] = "";
92 if (!isset($_REQUEST['to_addrs_names'])) {
93 $_REQUEST['to_addrs_names'] = "";
95 if (!isset($_REQUEST['to_addrs_emails'])) {
96 $_REQUEST['to_addrs_emails'] = "";
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']);
102 // make sure the cc_* and bcc_* fields are at least empty if not set
103 $fields_to_check = array(
112 foreach ($fields_to_check as $field_to_check) {
113 if (!isset($_REQUEST[$field_to_check])) {
114 $_REQUEST[$field_to_check] = '';
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']);
122 if(!empty($_REQUEST['type'])) {
123 $focus->type = $_REQUEST['type'];
124 } elseif(empty($focus->type)) { // cn: from drafts/quotes
125 $focus->type = 'archived';
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;
135 if(isset($focus->to_addrs_arr[0]['contact_id'])) {
136 $object_arr['Contacts'] = $focus->to_addrs_arr[0]['contact_id'];
138 if(empty($object_arr)) {
139 $object_arr = array('Contacts' => '123');
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);
149 //// END TEMPLATE PARSING
150 ///////////////////////////////////////////////////////////////////////////////
152 ///////////////////////////////////////////////////////////////////////////////
153 //// PREP FOR ATTACHMENTS
154 if(empty($focus->id)){
155 $focus->id = create_guid();
156 $focus->new_with_id = true;
158 ///////////////////////////////////////////////////////////////////////////////
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('<','>');
174 $new = array('<','>');
176 if($_REQUEST['from_addr'] != $_REQUEST['from_addr_name'].' <'.$_REQUEST['from_addr_email'].'>') {
177 if(false === strpos($_REQUEST['from_addr'], '<')) { // 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));
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'];
189 $focus->from_addr = $focus->getSystemDefaultEmail();
191 //// REPLY PROCESSING
192 ///////////////////////////////////////////////////////////////////////////
194 $focus->status = 'sent';
196 $focus->status = 'send_error';
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'];
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);
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);
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']);
223 //// END RELATIONSHIP LINKING
224 ///////////////////////////////////////////////////////////////////////////////
226 // If came from email archiving edit view, this would have been set from form input.
227 if (!isset($focus->date_start))
229 $timedate = TimeDate::getInstance();
230 list($focus->date_start, $focus->time_start) = $timedate->split_date_time($timedate->now());
233 $focus->date_sent = "";
235 require_once('include/formbase.php');
236 $focus = populateFromPost('', $focus);
239 //// END EMAIL SAVE/SEND SETUP
240 ///////////////////////////////////////////////////////////////////////////////
242 ///////////////////////////////////////////////////////////////////////////////
243 //// RELATIONSHIP LINKING
244 $focus->load_relationship('users');
245 $focus->users->add($current_user->id);
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);
256 ///////////////////////////////////////////////////////////////////////////////
257 //// PAGE REDIRECTION
258 ///////////////////////////////////////////////////////////////////////////////
259 $return_id = $focus->id;
261 if(empty($_POST['return_module'])) {
262 $return_module = "Emails";
264 $return_module = $_POST['return_module'];
266 if(empty($_POST['return_action'])) {
267 $return_action = "DetailView";
269 $return_action = $_POST['return_action'];
271 $GLOBALS['log']->debug("Saved record with id of ".$return_id);
273 if($focus->type == 'draft') {
274 if($return_module == 'Emails') {
275 header("Location: index.php?module=$return_module&action=ListViewDrafts");
277 handleRedirect($return_id, 'Emails');
279 } elseif($focus->type == 'out') {
280 if($return_module == 'Home') {
281 header('Location: index.php?module='.$return_module.'&action=index');
283 if(!empty($_REQUEST['return_id'])) {
284 $return_id = $_REQUEST['return_id'];
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'];
290 header("Location: index.php?action=$return_action&module=$return_module&record=$return_id");