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