]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/generic/Save2.php
Release 6.5.6
[Github/sugarcrm.git] / include / generic / Save2.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 /*********************************************************************************
39
40  * Description:  TODO: To be written.
41  * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
42  * All Rights Reserved.
43  * Contributor(s): ______________________________________..
44  ********************************************************************************/
45
46 /*
47 ARGS:
48  $_REQUEST['method']; : options: 'SaveRelationship','Save','DeleteRelationship','Delete'
49  $_REQUEST['module']; : the module associated with this Bean instance (will be used to get the class name)
50  $_REQUEST['record']; : the id of the Bean instance
51 // $_REQUEST['related_field']; : the field name on the Bean instance that contains the relationship
52 // $_REQUEST['related_record']; : the id of the related record
53 // $_REQUEST['related_']; : the
54 // $_REQUEST['return_url']; : the URL to redirect to
55 //$_REQUEST['return_type']; : when set the results of a report will be linked with the parent.
56 */
57
58
59 require_once('include/formbase.php');
60
61 $refreshsubpanel=true;
62 if (isset($_REQUEST['return_type'])  && $_REQUEST['return_type'] == 'report') {
63         save_from_report($_REQUEST['subpanel_id'] //report_id
64                                          ,$_REQUEST['record'] //parent_id
65                                          ,$_REQUEST['module'] //module_name
66                                          ,$_REQUEST['subpanel_field_name'] //link attribute name
67         );
68 } else if (isset($_REQUEST['return_type'])  && $_REQUEST['return_type'] == 'addtoprospectlist') {
69
70         $GLOBALS['log']->debug(print_r($_REQUEST,true));
71         if(!empty($_REQUEST['prospect_list_id']) and !empty($_REQUEST['prospect_ids']))
72         {
73             add_prospects_to_prospect_list(
74                 $_REQUEST['prospect_list_id'],
75                 $_REQUEST['prospect_ids']
76             );
77         }
78         else
79         {
80             $parent = BeanFactory::getBean($_REQUEST['module'], $_REQUEST['record']);
81             add_to_prospect_list(urldecode($_REQUEST['subpanel_module_name']),$_REQUEST['parent_module'],$_REQUEST['parent_type'],$_REQUEST['subpanel_id'],
82                 $_REQUEST['child_id'],$_REQUEST['link_attribute'],$_REQUEST['link_type'], $parent);
83         }
84
85         $refreshsubpanel=false;
86 }else if (isset($_REQUEST['return_type'])  && $_REQUEST['return_type'] == 'addcampaignlog') {
87     //if param is set to "addcampaignlog", then we need to create a campaign log entry
88     //for each campaign id passed in.
89
90     // Get a list of campaigns selected.
91     if (isset($_REQUEST['subpanel_id'])  && !empty($_REQUEST['subpanel_id'])) {
92         $campaign_ids = $_REQUEST['subpanel_id'];
93         global $beanFiles;
94         global $beanList;
95         //retrieve current bean
96         $bean_name = $beanList[$_REQUEST['module']];
97         require_once($beanFiles[$bean_name]);
98         $focus = new $bean_name();
99         $focus->retrieve($_REQUEST['record']);
100
101         require_once('modules/Campaigns/utils.php');
102         //call util function to create the campaign log entry
103         foreach($campaign_ids as $id){
104             create_campaign_log_entry($id, $focus, $focus->module_dir,$focus, $focus->id);
105         }
106         $refreshsubpanel=true;
107     }
108 }
109 else {
110
111         global $beanFiles,$beanList;
112         $bean_name = $beanList[$_REQUEST['module']];
113         require_once($beanFiles[$bean_name]);
114         $focus = new $bean_name();
115
116         $focus->retrieve($_REQUEST['record']);
117
118         // If the user selected "All records" from the selection menu, we pull up the list
119         // based on the query they used on that popup to relate them to the parent record
120         if(!empty($_REQUEST['select_entire_list']) &&  $_REQUEST['select_entire_list'] != 'undefined' && isset($_REQUEST['current_query_by_page'])){
121                 $order_by = '';
122                 $current_query_by_page = $_REQUEST['current_query_by_page'];
123                 $current_query_by_page_array = unserialize(base64_decode($current_query_by_page));
124
125         $module = $current_query_by_page_array['module'];
126         $seed = BeanFactory::getBean($module);
127         if(empty($seed)) sugar_die($GLOBALS['app_strings']['ERROR_NO_BEAN']);
128                 $where_clauses = '';
129                 require_once('include/SearchForm/SearchForm2.php');
130
131                 if(file_exists('custom/modules/'.$module.'/metadata/metafiles.php')){
132             require('custom/modules/'.$module.'/metadata/metafiles.php');
133         }elseif(file_exists('modules/'.$module.'/metadata/metafiles.php')){
134             require('modules/'.$module.'/metadata/metafiles.php');
135         }
136
137         if (file_exists('custom/modules/'.$module.'/metadata/searchdefs.php'))
138         {
139                 require_once('custom/modules/'.$module.'/metadata/searchdefs.php');
140         }
141         elseif (!empty($metafiles[$module]['searchdefs']))
142         {
143                 require_once($metafiles[$module]['searchdefs']);
144         }
145         elseif (file_exists('modules/'.$module.'/metadata/searchdefs.php'))
146         {
147                 require_once('modules/'.$module.'/metadata/searchdefs.php');
148         }
149
150         if(!empty($metafiles[$module]['searchfields'])){
151                 require_once($metafiles[$module]['searchfields']);
152         }
153         elseif(file_exists('modules/'.$module.'/metadata/SearchFields.php')){
154                 require_once('modules/'.$module.'/metadata/SearchFields.php');
155         }
156         if(!empty($searchdefs) && !empty($searchFields)) {
157                 $searchForm = new SearchForm($seed, $module);
158                 $searchForm->setup($searchdefs, $searchFields, 'SearchFormGeneric.tpl');
159                 $searchForm->populateFromArray($current_query_by_page_array, 'advanced');
160                 $where_clauses_arr = $searchForm->generateSearchWhere(true, $module);
161                 if (count($where_clauses_arr) > 0 ) {
162                     $where_clauses = '('. implode(' ) AND ( ', $where_clauses_arr) . ')';
163                 }
164         }
165
166                 $ret_array = create_export_query_relate_link_patch($module, $searchFields, $where_clauses);
167                 $query = $seed->create_export_query($order_by, $ret_array['where'], $ret_array['join']);
168                 $result = $GLOBALS['db']->query($query,true);
169                 $uids = array();
170                 while($val = $GLOBALS['db']->fetchByAssoc($result,false))
171                 {
172                         array_push($uids, $val['id']);
173                 }
174                 $_REQUEST['subpanel_id'] = $uids;
175         }
176
177         if($bean_name == 'Team'){
178                 $subpanel_id = $_REQUEST['subpanel_id'];
179                 if(is_array($subpanel_id)){
180                         foreach($subpanel_id as $id){
181                                 $focus->add_user_to_team($id);
182                         }
183                 }
184                 else{
185                         $focus->add_user_to_team($subpanel_id);
186                 }
187         } else{
188                 //find request paramters with with prefix of REL_ATTRIBUTE_
189                 //convert them into an array of name value pairs add pass them as
190                 //parameters to the add metod.
191                 $add_values =array();
192                 foreach ($_REQUEST as $key=>$value) {
193                         if (strpos($key,"REL_ATTRIBUTE_") !== false) {
194                                 $add_values[substr($key,14)]=$value;
195                         }
196                 }
197                 $focus->load_relationship($_REQUEST['subpanel_field_name']);
198                 $focus->$_REQUEST['subpanel_field_name']->add($_REQUEST['subpanel_id'],$add_values);
199         $focus->save();
200         }
201 }
202
203 if ($refreshsubpanel) {
204         //refresh contents of the sub-panel.
205         $GLOBALS['log']->debug("Location: index.php?sugar_body_only=1&module=".$_REQUEST['module']."&subpanel=".$_REQUEST['subpanel_module_name']."&action=SubPanelViewer&inline=1&record=".$_REQUEST['record']);
206         if( empty($_REQUEST['refresh_page']) || $_REQUEST['refresh_page'] != 1){
207                 $inline = isset($_REQUEST['inline'])?$_REQUEST['inline']: $inline;
208                 header("Location: index.php?sugar_body_only=1&module=".$_REQUEST['module']."&subpanel=".$_REQUEST['subpanel_module_name']."&action=SubPanelViewer&inline=$inline&record=".$_REQUEST['record']);
209         }
210         exit;
211 }