PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/include/generic/SugarWidgets/SugarWidgetSubPanelTopButton.php

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