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.
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.
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
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
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.
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.
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 ********************************************************************************/
41 require_once('include/generic/SugarWidgets/SugarWidget.php');
43 class SugarWidgetSubPanelTopButton extends SugarWidget
49 var $additional_form_fields;
52 //TODO rename defines to layout defs and make it a member variable instead of passing it multiple layers with extra copying.
54 /** Take the keys for the strings and look them up. Module is literal, the rest are label keys
56 function SugarWidgetSubPanelTopButton($module='', $title='', $access_key='', $form_value='')
62 // it is really the class details from the mapping
63 $class_details = $module;
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'];
82 $this->module = $module;
84 // If keys were passed into the constructor, translate them from keys to values.
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);
94 function &_get_form($defines, $additionalFormFields = null, $asUrl = false)
97 global $currentModule;
99 // Create the additional form fields with real values if they were not passed in
100 if(empty($additionalFormFields) && $this->additional_form_fields)
102 foreach($this->additional_form_fields as $key=>$value)
104 if(!empty($defines['focus']->$value))
106 $additionalFormFields[$key] = $defines['focus']->$value;
110 $additionalFormFields[$key] = '';
116 if(!empty($this->module))
118 $defines['child_module_name'] = $this->module;
122 $defines['child_module_name'] = $defines['module'];
125 $defines['parent_bean_name'] = get_class( $defines['focus']);
126 $relationship_name = $this->get_subpanel_relationship_name($defines);
129 $formValues = array();
131 //module_button is used to override the value of module name
132 $formValues['module'] = $defines['child_module_name'];
133 $formValues[strtolower($defines['parent_bean_name'])."_id"] = $defines['focus']->id;
135 if(isset($defines['focus']->name))
137 $formValues[strtolower($defines['parent_bean_name'])."_name"] = $defines['focus']->name;
138 // #26451,add these fields for custom one-to-many relate field.
139 if(!empty($defines['child_module_name'])){
140 $formValues[$relationship_name."_name"] = $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 $formValues[$GLOBALS['dictionary'][ $childFocusName ]["fields"][$relationship_name .'_name']['id_name']] = $defines['focus']->id;
148 $formValues['return_module'] = $currentModule;
150 if($currentModule == 'Campaigns'){
151 $formValues['return_action'] = "DetailView";
153 $formValues['return_action'] = $defines['action'];
154 if ( $formValues['return_action'] == 'SubPanelViewer' ) {
155 $formValues['return_action'] = 'DetailView';
159 $formValues['return_id'] = $defines['focus']->id;
160 $formValues['return_relationship'] = $relationship_name;
161 switch ( strtolower( $currentModule ) )
164 $name = $defines['focus']->account_name ;
167 $name = $defines['focus']->document_name ;
170 $name = $defines['focus']->kbdocument_name ;
174 $name = $defines['focus']->first_name . " " .$defines['focus']->last_name ;
177 $name = (isset($defines['focus']->name)) ? $defines['focus']->name : "";
179 $formValues['return_name'] = $name;
181 // TODO: move this out and get $additionalFormFields working properly
182 if(empty($additionalFormFields['parent_type']))
184 if($defines['focus']->object_name=='Contact') {
185 $additionalFormFields['parent_type'] = 'Accounts';
188 $additionalFormFields['parent_type'] = $defines['focus']->module_dir;
191 if(empty($additionalFormFields['parent_name']))
193 if($defines['focus']->object_name=='Contact') {
194 $additionalFormFields['parent_name'] = $defines['focus']->account_name;
195 $additionalFormFields['account_name'] = $defines['focus']->account_name;
198 $additionalFormFields['parent_name'] = $defines['focus']->name;
201 if(empty($additionalFormFields['parent_id']))
203 if($defines['focus']->object_name=='Contact') {
204 $additionalFormFields['parent_id'] = $defines['focus']->account_id;
205 $additionalFormFields['account_id'] = $defines['focus']->account_id;
206 } else if($defines['focus']->object_name=='Contract') {
207 $additionalFormFields['contract_id'] = $defines['focus']->id;
209 $additionalFormFields['parent_id'] = $defines['focus']->id;
213 if ($defines['focus']->object_name=='Opportunity') {
214 $additionalFormFields['account_id'] = $defines['focus']->account_id;
215 $additionalFormFields['account_name'] = $defines['focus']->account_name;
218 if (!empty($defines['child_module_name']) and $defines['child_module_name']=='Contacts' and !empty($defines['parent_bean_name']) and $defines['parent_bean_name']=='contact' ) {
219 if (!empty($defines['focus']->id ) and !empty($defines['focus']->name)) {
220 $formValues['reports_to_id'] = $defines['focus']->id;
221 $formValues['reports_to_name'] = $defines['focus']->name;
224 $formValues['action'] = "EditView";
228 foreach($formValues as $key => $value ) {
229 $returnLink .= $key.'='.$value.'&';
231 foreach($additionalFormFields as $key => $value ) {
232 $returnLink .= $key.'='.$value.'&';
234 $returnLink = rtrim($returnLink,'&');
239 $form = 'form' . $relationship_name;
240 $button = '<form action="index.php" method="post" name="form" id="' . $form . "\">\n";
241 foreach($formValues as $key => $value) {
242 $button .= "<input type='hidden' name='" . $key . "' value='" . $value . "' />\n";
245 // fill in additional form fields for all but action
246 foreach($additionalFormFields as $key => $value) {
247 if($key != 'action') {
248 $button .= "<input type='hidden' name='" . $key . "' value='" . $value . "' />\n";
257 /** This default function is used to create the HTML for a simple button */
258 function display($defines, $additionalFormFields = null)
261 $inputID = $this->getWidgetId() . '_'.preg_replace('[ ]', '', strtolower($this->form_value)).'_button';
263 if(!empty($this->acl) && ACLController::moduleSupportsACL($defines['module']) && !ACLController::checkAccess($defines['module'], $this->acl, true)){
264 $inputID = $this->getWidgetId() . '_'.preg_replace('[ ]', '', strtolower($this->form_value)).'_button';
265 $button = "<input title='$this->title' class='button' type='button' name='$inputID' id='$inputID' value=' $this->form_value ' disabled/>\n</form>";
271 if ( isset($_REQUEST['layout_def_key']) && $_REQUEST['layout_def_key'] == 'UserEAPM' ) {
272 // Subpanels generally don't go on the editview, so we have to handle this special
273 $megaLink = $this->_get_form($defines, $additionalFormFields,true);
274 $button = "<input title='$this->title' accesskey='$this->access_key' class='button' type='submit' name='$inputID' id='$inputID' value=' $this->form_value ' onclick='javascript:document.location=\"index.php?".$megaLink."\"; return false;'/>";
276 $button = $this->_get_form($defines, $additionalFormFields);
277 $button .= "<input title='$this->title' accesskey='$this->access_key' class='button' type='submit' name='$inputID' id='$inputID' value=' $this->form_value ' />\n</form>";
283 * Returns a string that is the JSON encoded version of the popup request.
284 * Perhaps this function should be moved to a more globally accessible location?
286 function _create_json_encoded_popup_request($popup_request_data)
288 $popup_request_array = array();
290 if(!empty($popup_request_data['call_back_function']))
292 $popup_request_array[] = '"call_back_function":"' . $popup_request_data['call_back_function'] . '"';
295 if(!empty($popup_request_data['form_name']))
297 $popup_request_array[] = '"form_name":"' . $popup_request_data['form_name'] . '"';
300 if(!empty($popup_request_data['field_to_name_array']))
302 $field_to_name_array = array();
303 foreach($popup_request_data['field_to_name_array'] as $field => $name)
305 $field_to_name_array[] = '"' . $field . '":"' . $name . '"';
308 $popup_request_array[] = '"field_to_name_array":{' . implode(',', $field_to_name_array) . '}';
311 if(!empty($popup_request_data['passthru_data']))
313 $passthru_array = array();
314 foreach($popup_request_data['passthru_data'] as $field => $name)
316 $passthru_array[] = '"' . $field . '":"' . $name . '"';
319 $popup_request_array[] = '"passthru_data":{' . implode(',', $passthru_array) . '}';
322 $encoded_popup_request = '{' . implode(',', $popup_request_array) . '}';
324 return $encoded_popup_request;
328 * get_subpanel_relationship_name
329 * Get the relationship name based on the subapnel definition
330 * @param mixed $defines The subpanel definition
332 function get_subpanel_relationship_name($defines) {
333 $relationship_name = '';
334 if(!empty($defines)) {
335 $relationship_name = isset($defines['module']) ? $defines['module'] : '';
336 $dataSource = $defines['subpanel_definition']->get_data_source_name(true);
337 if (!empty($dataSource)) {
338 $relationship_name = $dataSource;
339 //Try to set the relationship name to the real relationship, not the link.
340 if (!empty($defines['subpanel_definition']->parent_bean->field_defs[$dataSource])
341 && !empty($defines['subpanel_definition']->parent_bean->field_defs[$dataSource]['relationship']))
343 $relationship_name = $defines['subpanel_definition']->parent_bean->field_defs[$dataSource]['relationship'];
347 return $relationship_name;