]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/generic/SugarWidgets/SugarWidgetSubPanelTopButton.php
Release 6.1.4
[Github/sugarcrm.git] / include / generic / SugarWidgets / SugarWidgetSubPanelTopButton.php
1 <?php
2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4  * SugarCRM 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
41 require_once('include/generic/SugarWidgets/SugarWidget.php');
42
43 class SugarWidgetSubPanelTopButton extends SugarWidget
44 {
45     var $module;
46         var $title;
47         var $access_key;
48         var $form_value;
49         var $additional_form_fields;
50         var $acl;
51
52 //TODO rename defines to layout defs and make it a member variable instead of passing it multiple layers with extra copying.
53         
54         /** Take the keys for the strings and look them up.  Module is literal, the rest are label keys
55         */
56         function SugarWidgetSubPanelTopButton($module='', $title='', $access_key='', $form_value='')
57         {
58                 global $app_strings;
59
60                 if(is_array($module))
61                 {
62                         // it is really the class details from the mapping
63                         $class_details = $module;
64                         
65                         // If keys were passed into the constructor, translate them from keys to values.
66                         if(!empty($class_details['module']))
67                                 $this->module = $class_details['module'];
68                         if(!empty($class_details['title']))
69                                 $this->title = $app_strings[$class_details['title']];
70                         if(!empty($class_details['access_key']))
71                                 $this->access_key = $app_strings[$class_details['access_key']];
72                         if(!empty($class_details['form_value']))
73                                 $this->form_value = translate($class_details['form_value'], $this->module);
74                         if(!empty($class_details['additional_form_fields']))
75                                 $this->additional_form_fields = $class_details['additional_form_fields'];
76                         if(!empty($class_details['ACL'])){
77                                 $this->acl = $class_details['ACL'];
78                         }
79                 }
80                 else
81                 {
82                         $this->module = $module;
83                 
84                         // If keys were passed into the constructor, translate them from keys to values.
85                         if(!empty($title))
86                                 $this->title = $app_strings[$title];
87                         if(!empty($access_key))
88                                 $this->access_key = $app_strings[$access_key];
89                         if(!empty($form_value))
90                                 $this->form_value = translate($form_value, $module);
91                 }
92         }
93         
94     function &_get_form($defines, $additionalFormFields = null)
95     {
96         global $app_strings;
97         global $currentModule;
98
99         // Create the additional form fields with real values if they were not passed in
100         if(empty($additionalFormFields) && $this->additional_form_fields)
101         {
102             foreach($this->additional_form_fields as $key=>$value)
103             {
104                 if(!empty($defines['focus']->$value))
105                 {
106                     $additionalFormFields[$key] = $defines['focus']->$value;
107                 }
108                 else
109                 {
110                     $additionalFormFields[$key] = '';
111                 }
112             }
113         }
114         
115         
116                 if(!empty($this->module))
117         {
118             $defines['child_module_name'] = $this->module;
119         }
120         else
121         {
122             $defines['child_module_name'] = $defines['module'];
123         }
124
125         $defines['parent_bean_name'] = get_class( $defines['focus']);
126                 $relationship_name = $this->get_subpanel_relationship_name($defines);
127         
128         $form = 'form' . $relationship_name;
129         $button = '<form action="index.php" method="post" name="form" id="' . $form . "\">\n";
130
131         //module_button is used to override the value of module name
132         $button .= "<input type='hidden' name='module' value='".$defines['child_module_name']."'>\n";
133         $button .= "<input type='hidden' name='".strtolower($defines['parent_bean_name'])."_id' value='".$defines['focus']->id."'>\n";
134
135         if(isset($defines['focus']->name))
136         {
137             $button .= "<input type='hidden' name='".strtolower($defines['parent_bean_name'])."_name' value='".$defines['focus']->name."'>";
138             #26451,add these fields for custom one-to-many relate field.
139             if(!empty($defines['child_module_name'])){
140                 $button .= "<input type='hidden' name='". $relationship_name ."_name' value='".$defines['focus']->name."'>";
141                 $childFocusName = !empty($GLOBALS['beanList'][$defines['child_module_name']]) ? $GLOBALS['beanList'][$defines['child_module_name']] : "";
142                 if(!empty($GLOBALS['dictionary'][ $childFocusName ]["fields"][$relationship_name .'_name']['id_name'])){
143                         $button .= "<input type='hidden' name='". $GLOBALS['dictionary'][ $childFocusName ]["fields"][$relationship_name .'_name']['id_name'] ."' value='".$defines['focus']->id."'>";
144                 }
145             }
146         }
147
148         $button .= '<input type="hidden" name="return_module" value="' . $currentModule . "\" />\n";
149
150         if($currentModule == 'Campaigns'){
151             $button .= '<input type="hidden" name="return_action" value="DetailView" />';
152         }else{
153             $button .= '<input type="hidden" name="return_action" value="' . $defines['action'] . "\" />\n";    
154         }
155         
156         $button .= '<input type="hidden" name="return_id" value="' . $defines['focus']->id . "\" />\n";
157         $button .= '<input type="hidden" name="return_relationship" value="' . $relationship_name . "\" />\n";        
158         switch ( strtolower( $currentModule ) )
159         {
160             case 'prospects' :
161                 $name = $defines['focus']->account_name ;
162                 break ;
163             case 'documents' :
164                 $name = $defines['focus']->document_name ;
165                 break ;
166             case 'kbdocuments' :
167                 $name = $defines['focus']->kbdocument_name ;
168                 break ;
169             case 'leads' :
170             case 'contacts' : 
171                 $name = $defines['focus']->first_name . " " .$defines['focus']->last_name ;
172                 break ;
173             default :
174                $name = (isset($defines['focus']->name)) ? $defines['focus']->name : "";
175         }
176         $button .= '<input type="hidden" name="return_name" value="' . $name . "\" />\n";
177         
178         // TODO: move this out and get $additionalFormFields working properly
179         if(empty($additionalFormFields['parent_type']))
180         {
181             if($defines['focus']->object_name=='Contact') {
182                 $additionalFormFields['parent_type'] = 'Accounts';
183             }
184             else {
185                 $additionalFormFields['parent_type'] = $defines['focus']->module_dir;
186             }
187         }
188         if(empty($additionalFormFields['parent_name']))
189         {
190             if($defines['focus']->object_name=='Contact') {
191                 $additionalFormFields['parent_name'] = $defines['focus']->account_name;
192                 $additionalFormFields['account_name'] = $defines['focus']->account_name;
193             }
194             else {
195                 $additionalFormFields['parent_name'] = $defines['focus']->name;
196             }
197         }
198         if(empty($additionalFormFields['parent_id']))
199         {
200             if($defines['focus']->object_name=='Contact') {
201                 $additionalFormFields['parent_id'] = $defines['focus']->account_id;
202                 $additionalFormFields['account_id'] = $defines['focus']->account_id;
203             } else if($defines['focus']->object_name=='Contract') {
204                 $additionalFormFields['contract_id'] = $defines['focus']->id;
205             } else {
206                 $additionalFormFields['parent_id'] = $defines['focus']->id;
207             }
208         }
209
210         if ($defines['focus']->object_name=='Opportunity') {
211             $additionalFormFields['account_id'] = $defines['focus']->account_id;
212             $additionalFormFields['account_name'] = $defines['focus']->account_name;
213         }
214
215         if (!empty($defines['child_module_name']) and $defines['child_module_name']=='Contacts' and !empty($defines['parent_bean_name']) and $defines['parent_bean_name']=='contact' ) {
216             if (!empty($defines['focus']->id ) and !empty($defines['focus']->name)) {
217                 $button .= '<input type="hidden" name="reports_to_id" value="'. $defines['focus']->id .'"  />' . "\n";
218                 $button .= '<input type="hidden" name="reports_to_name" value="'. $defines['focus']->name .'"  />' . "\n";
219             }
220         }
221         $button .= '<input type="hidden" name="action" value="EditView" />' . "\n";
222         
223         // fill in additional form fields for all but action
224         foreach($additionalFormFields as $key => $value)
225         {
226             if($key != 'action')
227             {
228                 $button .= '<input type="hidden" name="' . $key . '" value="' . $value . '" />' . "\n";
229             }
230         }
231
232         return $button;
233     }
234
235         /** This default function is used to create the HTML for a simple button */
236         function display($defines, $additionalFormFields = null)
237         {
238                 $temp='';
239                 $inputID = $this->getWidgetId() . '_'.preg_replace('[ ]', '', strtolower($this->form_value)).'_button';
240                 
241                 if(!empty($this->acl) && ACLController::moduleSupportsACL($defines['module'])  &&  !ACLController::checkAccess($defines['module'], $this->acl, true)){
242                         $inputID = $this->getWidgetId() . '_'.preg_replace('[ ]', '', strtolower($this->form_value)).'_button';
243                         $button = "<input title='$this->title'  class='button' type='button' name='$inputID' id='$inputID' value='  $this->form_value  ' disabled/>\n</form>";
244                         return $temp;
245                 }
246                 
247                 global $app_strings;
248                 
249                 $button = $this->_get_form($defines, $additionalFormFields);
250                 $button .= "<input title='$this->title' accesskey='$this->access_key' class='button' type='submit' name='$inputID' id='$inputID' value='  $this->form_value  ' />\n</form>";
251                 return $button;
252         }
253
254         /**
255          * Returns a string that is the JSON encoded version of the popup request.
256          * Perhaps this function should be moved to a more globally accessible location?
257          */
258         function _create_json_encoded_popup_request($popup_request_data)
259         {
260                 $popup_request_array = array();
261                 
262                 if(!empty($popup_request_data['call_back_function']))
263                 {
264                         $popup_request_array[] = '"call_back_function":"' . $popup_request_data['call_back_function'] . '"';
265                 }
266
267                 if(!empty($popup_request_data['form_name']))
268                 {
269                         $popup_request_array[] = '"form_name":"' . $popup_request_data['form_name'] . '"';
270                 }
271                 
272                 if(!empty($popup_request_data['field_to_name_array']))
273                 {
274                         $field_to_name_array = array();
275                         foreach($popup_request_data['field_to_name_array'] as $field => $name)
276                         {
277                                 $field_to_name_array[] = '"' . $field . '":"' . $name . '"';
278                         }
279                         
280                         $popup_request_array[] = '"field_to_name_array":{' . implode(',', $field_to_name_array) . '}';
281                 }
282
283                 if(!empty($popup_request_data['passthru_data']))
284                 {
285                         $passthru_array = array();
286                         foreach($popup_request_data['passthru_data'] as $field => $name)
287                         {
288                                 $passthru_array[] = '"' . $field . '":"' . $name . '"';
289                         }
290                         
291                         $popup_request_array[] = '"passthru_data":{' . implode(',', $passthru_array) . '}';
292                 }
293
294                 $encoded_popup_request = '{' . implode(',', $popup_request_array) . '}';
295                 
296                 return $encoded_popup_request;
297         }
298         
299         /**
300          * get_subpanel_relationship_name
301          * Get the relationship name based on the subapnel definition
302          * @param mixed $defines The subpanel definition
303          */
304         function get_subpanel_relationship_name($defines) {
305                  $relationship_name = '';
306                  if(!empty($defines)) {
307                         $relationship_name = isset($defines['module']) ? $defines['module'] : '';
308                 $dataSource = $defines['subpanel_definition']->get_data_source_name(true);
309                 if (!empty($dataSource)) {
310                                 $relationship_name = $dataSource;
311                                 //Try to set the relationship name to the real relationship, not the link.
312                                 if (!empty($defines['subpanel_definition']->parent_bean->field_defs[$dataSource])
313                                  && !empty($defines['subpanel_definition']->parent_bean->field_defs[$dataSource]['relationship']))
314                                 {
315                                         $relationship_name = $defines['subpanel_definition']->parent_bean->field_defs[$dataSource]['relationship'];
316                                 }
317                         }
318                  }
319                  return $relationship_name;
320         }
321         
322 }
323 ?>