]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Campaigns/WebToLeadCapture.php
Release 6.5.1
[Github/sugarcrm.git] / modules / Campaigns / WebToLeadCapture.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-2012 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/formbase.php');
39 require_once('modules/Leads/LeadFormBase.php');
40
41
42
43 global $app_strings, $app_list_strings, $sugar_config, $timedate, $current_user;
44
45 $mod_strings = return_module_language($sugar_config['default_language'], 'Leads');
46
47 $app_list_strings['record_type_module'] = array('Contact'=>'Contacts', 'Account'=>'Accounts', 'Opportunity'=>'Opportunities', 'Case'=>'Cases', 'Note'=>'Notes', 'Call'=>'Calls', 'Email'=>'Emails', 'Meeting'=>'Meetings', 'Task'=>'Tasks', 'Lead'=>'Leads','Bug'=>'Bugs',
48
49 );
50
51 /**
52  * To make your changes upgrade safe create a file called leadCapture_override.php and place the changes there
53  */
54 $users = array(
55         'PUT A RANDOM KEY FROM THE WEBSITE HERE' => array('name'=>'PUT THE USER_NAME HERE', 'pass'=>'PUT THE USER_HASH FOR THE RESPECTIVE USER HERE'),
56 );
57
58 if (isset($_POST['campaign_id']) && !empty($_POST['campaign_id'])) {
59             //adding the client ip address
60             $_POST['client_id_address'] = query_client_ip();
61                 $campaign_id=$_POST['campaign_id'];
62                 $campaign = new Campaign();
63                 $camp_query  = "select name,id from campaigns where id='$campaign_id'";
64                 $camp_query .= " and deleted=0";
65         $camp_result=$campaign->db->query($camp_query);
66         $camp_data = $campaign->db->fetchByAssoc($camp_result);
67         // Bug 41292 - have to select marketing_id for new lead
68         $db = DBManagerFactory::getInstance();
69         $marketing = new EmailMarketing();
70         $marketing_query = $marketing->create_new_list_query(
71                 'date_start desc, date_modified desc',
72                 "campaign_id = '{$campaign_id}' and status = 'active' and date_start < " . $db->convert('', 'today'),
73                 array('id')
74         );
75         $marketing_result = $db->limitQuery($marketing_query, 0, 1, true);
76         $marketing_data = $db->fetchByAssoc($marketing_result);
77         // .Bug 41292
78                 if (isset($_REQUEST['assigned_user_id']) && !empty($_REQUEST['assigned_user_id'])) {
79                         $current_user = new User();
80                         $current_user->retrieve($_REQUEST['assigned_user_id']);
81                 } 
82
83             if(isset($camp_data) && $camp_data != null ){
84                         $leadForm = new LeadFormBase();
85             $lead = new Lead();
86                         $prefix = '';
87                         if(!empty($_POST['prefix'])){
88                                 $prefix = $_POST['prefix'];
89                         }
90
91                         if(empty($lead->id)) {
92                 $lead->id = create_guid();
93                 $lead->new_with_id = true;
94             }
95             $GLOBALS['check_notify'] = true;
96
97             //bug: 47574 - make sure, that webtolead_email1 field has same required attribute as email1 field
98             if(isset($lead->required_fields['email1'])){
99                 $lead->required_fields['webtolead_email1'] = $lead->required_fields['email1'];
100             }
101             
102             //bug: 42398 - have to unset the id from the required_fields since it is not populated in the $_POST
103             unset($lead->required_fields['id']);
104             unset($lead->required_fields['team_name']);
105             unset($lead->required_fields['team_count']);
106
107             // Bug #52563 : Web to Lead form redirects to Sugar when duplicate detected
108             // prevent duplicates check
109             $_POST['dup_checked'] = true;
110
111             // checkRequired needs a major overhaul before it works for web to lead forms.
112             $lead = $leadForm->handleSave($prefix, false, false, false, $lead);
113             
114                         if(!empty($lead)){
115                                 
116                     //create campaign log
117                     $camplog = new CampaignLog();
118                     $camplog->campaign_id  = $_POST['campaign_id'];
119                     $camplog->related_id   = $lead->id;
120                     $camplog->related_type = $lead->module_dir;
121                     $camplog->activity_type = "lead";
122                     $camplog->target_type = $lead->module_dir;
123                     $campaign_log->activity_date=$timedate->now();
124                     $camplog->target_id    = $lead->id;
125                 if(isset($marketing_data['id']))
126                 {
127                     $camplog->marketing_id = $marketing_data['id'];
128                 }
129                     $camplog->save();
130
131                         //link campaignlog and lead
132
133                         if (isset($_POST['email1']) && $_POST['email1'] != null)
134                 {
135                     $lead->email1 = $_POST['email1'];
136                         } 
137                 //in case there are old forms used webtolead_email1
138                 elseif (isset($_POST['webtolead_email1']) && $_POST['webtolead_email1'] != null)
139                 {
140                     $lead->email1 = $_POST['webtolead_email1'];
141                 }
142                 
143                         if (isset($_POST['email2']) && $_POST['email2'] != null)
144                 {
145                     $lead->email2 = $_POST['email2'];
146                         } 
147                 //in case there are old forms used webtolead_email2
148                 elseif (isset($_POST['webtolead_email2']) && $_POST['webtolead_email2'] != null)
149                 {
150                     $lead->email2 = $_POST['webtolead_email2'];
151                 }
152                 
153                         $lead->load_relationship('campaigns');
154                         $lead->campaigns->add($camplog->id);
155                 if(!empty($GLOBALS['check_notify'])) {
156                     $lead->save($GLOBALS['check_notify']);
157                 }
158                 else {
159                     $lead->save(FALSE);
160                 }
161             }
162
163             //in case there are forms out there still using email_opt_out
164             if(isset($_POST['webtolead_email_opt_out']) || isset($_POST['email_opt_out'])){
165                     
166                 if(isset ($lead->email1) && !empty($lead->email1)){
167                     $sea = new SugarEmailAddress();
168                     $sea->AddUpdateEmailAddress($lead->email1,0,1);
169                 }   
170                 if(isset ($lead->email2) && !empty($lead->email2)){
171                     $sea = new SugarEmailAddress();
172                     $sea->AddUpdateEmailAddress($lead->email2,0,1);
173                     
174                 }
175             }              
176                         if(isset($_POST['redirect_url']) && !empty($_POST['redirect_url'])){
177                             // Get the redirect url, and make sure the query string is not too long
178                         $redirect_url = $_POST['redirect_url'];
179                         $query_string = '';
180                                 $first_char = '&';
181                                 if(strpos($redirect_url, '?') === FALSE){
182                                         $first_char = '?';
183                                 }
184                                 $first_iteration = true;
185                                 $get_and_post = array_merge($_GET, $_POST);
186                                 foreach($get_and_post as $param => $value) {
187
188                                         if($param == 'redirect_url' && $param == 'submit')
189                                                 continue;
190                                         
191                                         if($first_iteration){
192                                                 $first_iteration = false;
193                                                 $query_string .= $first_char;
194                                         }
195                                         else{
196                                                 $query_string .= "&";
197                                         }
198                                         $query_string .= "{$param}=".urlencode($value);
199                                 }
200                                 if(empty($lead)) {
201                                         if($first_iteration){
202                                                 $query_string .= $first_char;
203                                         }
204                                         else{
205                                                 $query_string .= "&";
206                                         }
207                                         $query_string .= "error=1";
208                                 }
209                                 
210                                 $redirect_url = $redirect_url.$query_string;
211
212
213                                 // Check if the headers have been sent, or if the redirect url is greater than 2083 characters (IE max URL length)
214                                 //   and use a javascript form submission if that is the case.
215                             if(headers_sent() || strlen($redirect_url) > 2083){
216                                 echo '<html ' . get_language_header() . '><head><title>SugarCRM</title></head><body>';
217                                 echo '<form name="redirect" action="' .$_POST['redirect_url']. '" method="GET">';
218     
219                                 foreach($_POST as $param => $value) {
220                                         if($param != 'redirect_url' ||$param != 'submit') {
221                                                 echo '<input type="hidden" name="'.$param.'" value="'.$value.'">';
222                                         }
223                                 }
224                                 if(empty($lead)) {
225                                         echo '<input type="hidden" name="error" value="1">';
226                                 }
227                                 echo '</form><script language="javascript" type="text/javascript">document.redirect.submit();</script>';
228                                 echo '</body></html>';
229                         }
230                                 else{
231                                 header("Location: {$redirect_url}");
232                                 die();
233                             }
234                         }
235                         else{
236                                 echo $mod_strings['LBL_THANKS_FOR_SUBMITTING_LEAD'];
237                         }
238                         sugar_cleanup();
239                         // die to keep code from running into redirect case below
240                         die();
241             }
242            else{
243                   echo $mod_strings['LBL_SERVER_IS_CURRENTLY_UNAVAILABLE'];
244           }
245 }
246
247 if (!empty($_POST['redirect'])) {
248     if(headers_sent()){
249         echo '<html ' . get_language_header() . '><head><title>SugarCRM</title></head><body>';
250         echo '<form name="redirect" action="' .$_POST['redirect']. '" method="GET">';
251         echo '</form><script language="javascript" type="text/javascript">document.redirect.submit();</script>';
252         echo '</body></html>';
253     }
254     else{
255         header("Location: {$_POST['redirect']}");
256         die();
257     }
258 }
259
260 echo $mod_strings['LBL_SERVER_IS_CURRENTLY_UNAVAILABLE'];
261
262 ?>