]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/InboundEmail/Save.php
Release 6.1.4
[Github/sugarcrm.git] / modules / InboundEmail / Save.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 require_once('include/SugarFolders/SugarFolders.php');
39
40
41
42
43
44 global $current_user;
45
46 $focus = new InboundEmail();
47 $focus->retrieve($_REQUEST['record']);
48
49 foreach($focus->column_fields as $field) {
50         if(isset($_REQUEST[$field])) {
51                 if ($field != "group_id") {
52                         $focus->$field = trim($_REQUEST[$field]);
53                 }
54         }
55 }
56 foreach($focus->additional_column_fields as $field) {
57         if(isset($_REQUEST[$field])) {
58                 $value = trim($_REQUEST[$field]);
59                 $focus->$field = $value;
60         }
61 }
62 foreach($focus->required_fields as $field) {
63         if(isset($_REQUEST[$field])) {
64                 $value = trim($_REQUEST[$field]);
65                 $focus->$field = $value;
66         }
67 }
68 $focus->email_password = $_REQUEST['email_password'];
69 $focus->protocol = $_REQUEST['protocol'];
70
71 if( isset($_REQUEST['is_create_case']) && $_REQUEST['is_create_case'] == 'on' )
72     $focus->mailbox_type = 'createcase';
73 else 
74 {
75     if( empty($focus->mailbox_type) || $focus->mailbox_type == 'createcase' )
76         $focus->mailbox_type = 'pick';
77 }
78
79 /////////////////////////////////////////////////////////
80 ////    SERVICE STRING CONCATENATION
81 $useSsl = (isset($_REQUEST['ssl']) && $_REQUEST['ssl'] == 1) ? true : false;
82 $optimum = $focus->getSessionConnectionString($focus->server_url, $focus->email_user, $focus->port, $focus->protocol);
83 if (empty($optimum)) {
84         $optimum = $focus->findOptimumSettings($useSsl, $focus->email_user, $focus->email_password, $focus->server_url, $focus->port, $focus->protocol, $focus->mailbox);
85 } // if
86 $delimiter = $focus->getSessionInboundDelimiterString($focus->server_url, $focus->email_user, $focus->port, $focus->protocol);
87
88 //added check to ensure the $optimum['serial']) is not empty.
89 if(is_array($optimum) && (count($optimum) > 0) && !empty( $optimum['serial'])) {
90         $focus->service = $optimum['serial'];
91 } else {
92         // no save
93         // allowing bad save to allow Email Campaigns configuration to continue even without IMAP
94         $focus->service = "::::::".$focus->protocol."::::"; // save bogus info.
95         $error = "&error=true";
96 }
97 ////    END SERVICE STRING CONCAT
98 /////////////////////////////////////////////////////////
99
100 if(isset($_REQUEST['mark_read']) && $_REQUEST['mark_read'] == 1) {
101         $focus->delete_seen = 0;
102 } else {
103         $focus->delete_seen = 0;
104 }
105
106 // handle stored_options serialization
107 if(isset($_REQUEST['only_since']) && $_REQUEST['only_since'] == 1) {
108         $onlySince = true;
109 } else {
110         $onlySince = false;
111 }
112 $stored_options = array();
113 $stored_options['from_name'] = trim($_REQUEST['from_name']);
114 $stored_options['from_addr'] = trim($_REQUEST['from_addr']);
115 $stored_options['reply_to_name'] = trim($_REQUEST['reply_to_name']);
116 $stored_options['reply_to_addr'] = trim($_REQUEST['reply_to_addr']);
117 $stored_options['only_since'] = $onlySince;
118 $stored_options['filter_domain'] = $_REQUEST['filter_domain'];
119 $stored_options['email_num_autoreplies_24_hours'] = $_REQUEST['email_num_autoreplies_24_hours'];
120 $stored_options['allow_outbound_group_usage'] = isset($_REQUEST['allow_outbound_group_usage']) ? true : false; 
121
122 if (!$focus->isPop3Protocol()) {
123         $stored_options['trashFolder'] = (isset($_REQUEST['trashFolder']) ? trim($_REQUEST['trashFolder']) : "");
124         $stored_options['sentFolder'] = (isset($_REQUEST['sentFolder']) ? trim($_REQUEST['sentFolder']) : "");
125 } // if
126 if ( $focus->isMailBoxTypeCreateCase() || ($focus->mailbox_type == 'createcase' && empty($_REQUEST['id']) ) ) 
127 {
128         $stored_options['distrib_method'] = (isset($_REQUEST['distrib_method'])) ? $_REQUEST['distrib_method'] : "";
129         $stored_options['create_case_email_template'] = (isset($_REQUEST['create_case_template_id'])) ? $_REQUEST['create_case_template_id'] : "";
130 } // if
131 $storedOptions['folderDelimiter'] = $delimiter;
132
133 ////////////////////////////////////////////////////////////////////////////////
134 ////    CREATE MAILBOX QUEUE
135 ////////////////////////////////////////////////////////////////////////////////
136 if (!isset($focus->id)) {
137         $groupId = "";
138         if (isset($_REQUEST['group_id']) && empty($_REQUEST['group_id'])) {
139                 $groupId = $_REQUEST['group_id'];
140         } else {
141                 $groupId = create_guid();
142         }
143         $focus->group_id = $groupId;
144 }
145
146
147 if( isset($_REQUEST['is_auto_import']) && $_REQUEST['is_auto_import'] == 'on' )
148 {
149     if( empty($focus->groupfolder_id) ) 
150     {    
151         $groupFolderId = $focus->createAutoImportSugarFolder();
152         $focus->groupfolder_id = $groupFolderId;
153     }
154 }
155 else 
156 {
157     $focus->groupfolder_id = "";
158     //If the user is turning the auto-import feature off then remove all previous subscriptions.
159     if( !empty($focus->fetched_row['groupfolder_id'] ) )
160     {
161         $GLOBALS['log']->debug("Clearining all subscriptions to folder id: {$focus->fetched_row['groupfolder_id']}");
162         $f = new SugarFolder();
163         $f->clearSubscriptionsForFolder($focus->fetched_row['groupfolder_id']);
164         //Now delete the old group folder.
165         $f->retrieve($focus->fetched_row['groupfolder_id']);
166         $f->delete();
167     }
168 }
169
170 if (!empty($focus->groupfolder_id)) 
171 {
172         if ($_REQUEST['leaveMessagesOnMailServer'] == "1") 
173                 $stored_options['leaveMessagesOnMailServer'] = 1;
174         else 
175                 $stored_options['leaveMessagesOnMailServer'] = 0;
176 }
177
178 $focus->stored_options = base64_encode(serialize($stored_options));
179 $GLOBALS['log']->info('----->InboundEmail now saving self');
180
181
182 ////////////////////////////////////////////////////////////////////////////////
183 ////    SEND US TO SAVE DESTINATION
184 ////////////////////////////////////////////////////////////////////////////////
185
186 //When an admin is creating an IE account we do not want their private team to be added
187 //or they may be included in a round robin assignment.
188 $previousTeamAccessCheck = isset($GLOBALS['sugar_config']['disable_team_access_check']) ? $GLOBALS['sugar_config']['disable_team_access_check'] : null;
189 $GLOBALS['sugar_config']['disable_team_access_check'] = TRUE;
190
191 $focus->save();
192
193 //Reset the value so no other saves are affected.
194 $GLOBALS['sugar_config']['disable_team_access_check'] = $previousTeamAccessCheck;
195
196
197
198 //Sync any changes within the IE account that need to be synced with the Sugar Folder.
199 //Need to do this post save so the correct team/teamset id is generated correctly.
200 $monitor_fields = array('name', 'status', 
201                         ); 
202
203 //Only sync IE accounts with a group folder.  Need to sync new records as team set assignment is processed
204 //after save.                      
205 if( !empty($focus->groupfolder_id) )    
206 {                   
207     foreach ($monitor_fields as $singleField)
208     {
209         //Check if the value is being changed during save.
210         if($focus->fetched_row[$singleField] != $focus->$singleField)
211             syncSugarFoldersWithBeanChanges($singleField, $focus);
212     }
213 }
214
215 $_REQUEST['return_id'] = $focus->id;
216
217
218 $edit='';
219 if(isset($_REQUEST['return_module']) && $_REQUEST['return_module'] != "") {
220         $return_module = $_REQUEST['return_module'];
221 } else {
222         $return_module = "InboundEmail";
223 }
224 if(isset($_REQUEST['return_action']) && $_REQUEST['return_action'] != "") {
225         $return_action = $_REQUEST['return_action'];
226 } else {
227         $return_action = "DetailView";
228 }
229 if(isset($_REQUEST['return_id']) && $_REQUEST['return_id'] != "") {
230         $return_id = $_REQUEST['return_id'];
231 }
232 if(!empty($_REQUEST['edit'])) {
233         $return_id='';
234         $edit='&edit=true';
235 }
236
237 $GLOBALS['log']->debug("Saved record with id of ".$return_id);
238
239
240 header("Location: index.php?module=$return_module&action=$return_action&record=$return_id$edit$error");
241
242  
243 /**
244  * Certain updates to the IE account need to be reflected in the related SugarFolder since they are
245  * created automatically.  Only valid for IE accounts with auto import turned on.
246  *
247  * @param string $fieldName The field name that changed
248  * @param SugarBean $focus The InboundEmail bean being saved.
249  */
250 function syncSugarFoldersWithBeanChanges($fieldName, $focus)
251 {  
252     $f = new SugarFolder();
253     $f->retrieve($focus->groupfolder_id);
254     
255     switch ($fieldName)
256     {
257         case 'name':
258         case 'team_id':
259         case 'team_set_id':
260             $f->$fieldName = $focus->$fieldName; 
261             $f->save();
262             break;
263                 
264         case 'status':
265             if($focus->status == 'Inactive')
266                 $f->clearSubscriptionsForFolder($focus->groupfolder_id);
267             else if($focus->mailbox_type != 'bounce' )
268                 $f->addSubscriptionsToGroupFolder();
269             break;
270     } 
271 }
272