]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/generic/Save2.php
Release 6.5.5
[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 function add_prospects_to_prospect_list($parent_id,$child_id)
62 {
63     $focus=BeanFactory::getBean('Prospects');
64     if(is_array($child_id)){
65         $uids = $child_id;
66     }
67     else{
68         $uids = array($child_id);
69     }
70
71     $relationship = '';
72     foreach($focus->get_linked_fields() as $field => $def) {
73         if ($focus->load_relationship($field)) {
74             if ( $focus->$field->getRelatedModuleName() == 'ProspectLists' ) {
75                 $relationship = $field;
76                 break;
77             }
78         }
79     }
80
81     if ( $relationship != '' ) {
82         foreach ( $uids as $id) {
83             $focus->retrieve($id);
84             $focus->load_relationship($relationship);
85             $focus->prospect_lists->add( $parent_id );
86         }
87     }
88 }
89
90 function add_to_prospect_list($query_panel,$parent_module,$parent_type,$parent_id,$child_id,$link_attribute,$link_type,$parent)
91 {
92     $GLOBALS['log']->debug('add_prospects_to_prospect_list:parameters:'.$query_panel);
93     $GLOBALS['log']->debug('add_prospects_to_prospect_list:parameters:'.$parent_module);
94     $GLOBALS['log']->debug('add_prospects_to_prospect_list:parameters:'.$parent_type);
95     $GLOBALS['log']->debug('add_prospects_to_prospect_list:parameters:'.$parent_id);
96     $GLOBALS['log']->debug('add_prospects_to_prospect_list:parameters:'.$child_id);
97     $GLOBALS['log']->debug('add_prospects_to_prospect_list:parameters:'.$link_attribute);
98     $GLOBALS['log']->debug('add_prospects_to_prospect_list:parameters:'.$link_type);
99     require_once('include/SubPanel/SubPanelTiles.php');
100
101
102     if (!class_exists($parent_type)) {
103         require_once('modules/'.cleanDirName($parent_module).'/'.cleanDirName($parent_type).'.php');
104     }
105     $focus = new $parent_type();
106     $focus->retrieve($parent_id);
107     if(empty($focus->id)) {
108         return false;
109     }
110     if(empty($parent)) {
111         return false;
112     }
113
114     //if link_type is default then load relationship once and add all the child ids.
115     $relationship_attribute=$link_attribute;
116
117     //find all prospects based on the query
118
119     $subpanel = new SubPanelTiles($parent, $parent->module_dir);
120     $thisPanel=$subpanel->subpanel_definitions->load_subpanel($query_panel);
121     if(empty($thisPanel)) {
122         return false;
123     }
124     $result = SugarBean::get_union_related_list($parent, '', '', '', 0, -1,-1,'', $thisPanel);
125
126     if(!empty($result['list'])) {
127         foreach($result['list'] as $object) {
128             if ($link_type != 'default') {
129                 $relationship_attribute=strtolower($object->$link_attribute);
130             }
131             $GLOBALS['log']->debug('add_prospects_to_prospect_list:relationship_attribute:'.$relationship_attribute);
132             // load relationship for the first time or on change of relationship atribute.
133             if (empty($focus->$relationship_attribute)) {
134                 $focus->load_relationship($relationship_attribute);
135             }
136             //add
137             $focus->$relationship_attribute->add($object->$child_id);
138         }
139     }
140 }
141
142 //Link rows returned by a report to parent record.
143 function save_from_report($report_id,$parent_id, $module_name, $relationship_attr_name) {
144         global $beanFiles;
145         global $beanList;
146
147         $GLOBALS['log']->debug("Save2: Linking with report output");
148         $GLOBALS['log']->debug("Save2:Report ID=".$report_id);
149         $GLOBALS['log']->debug("Save2:Parent ID=".$parent_id);
150         $GLOBALS['log']->debug("Save2:Module Name=".$module_name);
151         $GLOBALS['log']->debug("Save2:Relationship Attribute Name=".$relationship_attr_name);
152
153         $bean_name = $beanList[$module_name];
154         $GLOBALS['log']->debug("Save2:Bean Name=".$bean_name);
155         require_once($beanFiles[$bean_name]);
156         $focus = new $bean_name();
157
158         $focus->retrieve($parent_id);
159         $focus->load_relationship($relationship_attr_name);
160
161         //fetch report definition.
162 global $current_language, $report_modules, $modules_report;
163
164 $mod_strings = return_module_language($current_language,"Reports");
165
166
167         $saved = new SavedReport();
168         $saved->disable_row_level_security = true;
169         $saved->retrieve($report_id, false);
170
171         //initiailize reports engine with the report definition.
172         require_once('modules/Reports/SubpanelFromReports.php');
173         $report = new SubpanelFromReports($saved);
174         $report->run_query();
175
176         $sql = $report->query_list[0];
177         $GLOBALS['log']->debug("Save2:Report Query=".$sql);
178         $result = $report->db->query($sql);
179         while($row = $report->db->fetchByAssoc($result))
180         {
181                 $focus->$relationship_attr_name->add($row['primaryid']);
182         }
183 }
184
185 $refreshsubpanel=true;
186 if (isset($_REQUEST['return_type'])  && $_REQUEST['return_type'] == 'report') {
187         save_from_report($_REQUEST['subpanel_id'] //report_id
188                                          ,$_REQUEST['record'] //parent_id
189                                          ,$_REQUEST['module'] //module_name
190                                          ,$_REQUEST['subpanel_field_name'] //link attribute name
191         );
192 } else if (isset($_REQUEST['return_type'])  && $_REQUEST['return_type'] == 'addtoprospectlist') {
193
194         $GLOBALS['log']->debug(print_r($_REQUEST,true));
195         if(!empty($_REQUEST['prospect_list_id']) and !empty($_REQUEST['prospect_ids']))
196         {
197             add_prospects_to_prospect_list(
198                 $_REQUEST['prospect_list_id'],
199                 $_REQUEST['prospect_ids']
200             );
201         }
202         else
203         {
204             $parent = BeanFactory::getBean($_REQUEST['module'], $_REQUEST['record']);
205             add_to_prospect_list(urldecode($_REQUEST['subpanel_module_name']),$_REQUEST['parent_module'],$_REQUEST['parent_type'],$_REQUEST['subpanel_id'],
206                 $_REQUEST['child_id'],$_REQUEST['link_attribute'],$_REQUEST['link_type'], $parent);
207         }
208
209         $refreshsubpanel=false;
210 }else if (isset($_REQUEST['return_type'])  && $_REQUEST['return_type'] == 'addcampaignlog') {
211     //if param is set to "addcampaignlog", then we need to create a campaign log entry
212     //for each campaign id passed in.
213
214     // Get a list of campaigns selected.
215     if (isset($_REQUEST['subpanel_id'])  && !empty($_REQUEST['subpanel_id'])) {
216         $campaign_ids = $_REQUEST['subpanel_id'];
217         global $beanFiles;
218         global $beanList;
219         //retrieve current bean
220         $bean_name = $beanList[$_REQUEST['module']];
221         require_once($beanFiles[$bean_name]);
222         $focus = new $bean_name();
223         $focus->retrieve($_REQUEST['record']);
224
225         require_once('modules/Campaigns/utils.php');
226         //call util function to create the campaign log entry
227         foreach($campaign_ids as $id){
228             create_campaign_log_entry($id, $focus, $focus->module_dir,$focus, $focus->id);
229         }
230         $refreshsubpanel=true;
231     }
232 }
233 else {
234
235         global $beanFiles,$beanList;
236         $bean_name = $beanList[$_REQUEST['module']];
237         require_once($beanFiles[$bean_name]);
238         $focus = new $bean_name();
239
240         $focus->retrieve($_REQUEST['record']);
241
242         // If the user selected "All records" from the selection menu, we pull up the list
243         // based on the query they used on that popup to relate them to the parent record
244         if(!empty($_REQUEST['select_entire_list']) &&  $_REQUEST['select_entire_list'] != 'undefined' && isset($_REQUEST['current_query_by_page'])){
245                 $order_by = '';
246                 $current_query_by_page = $_REQUEST['current_query_by_page'];
247                 $current_query_by_page_array = unserialize(base64_decode($current_query_by_page));
248
249         $module = $current_query_by_page_array['module'];
250         $seed = BeanFactory::getBean($module);
251         if(empty($seed)) sugar_die($GLOBALS['app_strings']['ERROR_NO_BEAN']);
252                 $where_clauses = '';
253                 require_once('include/SearchForm/SearchForm2.php');
254
255                 if(file_exists('custom/modules/'.$module.'/metadata/metafiles.php')){
256             require('custom/modules/'.$module.'/metadata/metafiles.php');
257         }elseif(file_exists('modules/'.$module.'/metadata/metafiles.php')){
258             require('modules/'.$module.'/metadata/metafiles.php');
259         }
260
261         if (file_exists('custom/modules/'.$module.'/metadata/searchdefs.php'))
262         {
263                 require_once('custom/modules/'.$module.'/metadata/searchdefs.php');
264         }
265         elseif (!empty($metafiles[$module]['searchdefs']))
266         {
267                 require_once($metafiles[$module]['searchdefs']);
268         }
269         elseif (file_exists('modules/'.$module.'/metadata/searchdefs.php'))
270         {
271                 require_once('modules/'.$module.'/metadata/searchdefs.php');
272         }
273
274         if(!empty($metafiles[$module]['searchfields'])){
275                 require_once($metafiles[$module]['searchfields']);
276         }
277         elseif(file_exists('modules/'.$module.'/metadata/SearchFields.php')){
278                 require_once('modules/'.$module.'/metadata/SearchFields.php');
279         }
280         if(!empty($searchdefs) && !empty($searchFields)) {
281                 $searchForm = new SearchForm($seed, $module);
282                 $searchForm->setup($searchdefs, $searchFields, 'SearchFormGeneric.tpl');
283                 $searchForm->populateFromArray($current_query_by_page_array, 'advanced');
284                 $where_clauses_arr = $searchForm->generateSearchWhere(true, $module);
285                 if (count($where_clauses_arr) > 0 ) {
286                     $where_clauses = '('. implode(' ) AND ( ', $where_clauses_arr) . ')';
287                 }
288         }
289
290                 $ret_array = create_export_query_relate_link_patch($module, $searchFields, $where_clauses);
291                 $query = $seed->create_export_query($order_by, $ret_array['where'], $ret_array['join']);
292                 $result = $GLOBALS['db']->query($query,true);
293                 $uids = array();
294                 while($val = $GLOBALS['db']->fetchByAssoc($result,false))
295                 {
296                         array_push($uids, $val['id']);
297                 }
298                 $_REQUEST['subpanel_id'] = $uids;
299         }
300
301         if($bean_name == 'Team'){
302                 $subpanel_id = $_REQUEST['subpanel_id'];
303                 if(is_array($subpanel_id)){
304                         foreach($subpanel_id as $id){
305                                 $focus->add_user_to_team($id);
306                         }
307                 }
308                 else{
309                         $focus->add_user_to_team($subpanel_id);
310                 }
311         } else{
312                 //find request paramters with with prefix of REL_ATTRIBUTE_
313                 //convert them into an array of name value pairs add pass them as
314                 //parameters to the add metod.
315                 $add_values =array();
316                 foreach ($_REQUEST as $key=>$value) {
317                         if (strpos($key,"REL_ATTRIBUTE_") !== false) {
318                                 $add_values[substr($key,14)]=$value;
319                         }
320                 }
321                 $focus->load_relationship($_REQUEST['subpanel_field_name']);
322                 $focus->$_REQUEST['subpanel_field_name']->add($_REQUEST['subpanel_id'],$add_values);
323         $focus->save();
324         }
325 }
326
327 if ($refreshsubpanel) {
328         //refresh contents of the sub-panel.
329         $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']);
330         if( empty($_REQUEST['refresh_page']) || $_REQUEST['refresh_page'] != 1){
331                 $inline = isset($_REQUEST['inline'])?$_REQUEST['inline']: $inline;
332                 header("Location: index.php?sugar_body_only=1&module=".$_REQUEST['module']."&subpanel=".$_REQUEST['subpanel_module_name']."&action=SubPanelViewer&inline=$inline&record=".$_REQUEST['record']);
333         }
334         exit;
335 }