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