2 /*********************************************************************************
3 * SugarCRM is a customer relationship management program developed by
4 * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
6 * This program is free software; you can redistribute it and/or modify it under
7 * the terms of the GNU Affero General Public License version 3 as published by the
8 * Free Software Foundation with the addition of the following permission added
9 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
11 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
18 * You should have received a copy of the GNU Affero General Public License along with
19 * this program; if not, see http://www.gnu.org/licenses or write to the Free
20 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
24 * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
26 * The interactive user interfaces in modified source and object code versions
27 * of this program must display Appropriate Legal Notices, as required under
28 * Section 5 of the GNU Affero General Public License version 3.
30 * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31 * these Appropriate Legal Notices must retain the display of the "Powered by
32 * SugarCRM" logo. If the display of the logo is not reasonably feasible for
33 * technical reasons, the Appropriate Legal Notices must display the words
34 * "Powered by SugarCRM".
35 ********************************************************************************/
37 require_once('include/SugarFields/Fields/Base/SugarFieldBase.php');
39 class SugarFieldRelate extends SugarFieldBase {
41 function getDetailViewSmarty($parentFieldArray, $vardef, $displayParams, $tabindex) {
42 $nolink = array('Users', 'Teams');
43 if(in_array($vardef['module'], $nolink)){
44 $this->ss->assign('nolink', true);
46 $this->ss->assign('nolink', false);
48 $this->setup($parentFieldArray, $vardef, $displayParams, $tabindex);
49 return $this->fetch('include/SugarFields/Fields/Relate/DetailView.tpl');
52 function getEditViewSmarty($parentFieldArray, $vardef, $displayParams, $tabindex) {
53 if(!empty($vardef['function']['returns']) && $vardef['function']['returns'] == 'html'){
54 return parent::getEditViewSmarty($parentFieldArray, $vardef, $displayParams, $tabindex);
57 $call_back_function = 'set_return';
58 if(isset($displayParams['call_back_function'])) {
59 $call_back_function = $displayParams['call_back_function'];
61 $form_name = 'EditView';
62 if(isset($displayParams['formName'])) {
63 $form_name = $displayParams['formName'];
66 //Special Case for accounts; use the displayParams array and retrieve
67 //the key and copy indexes. 'key' is the suffix of the field we are searching
68 //the Account's address with. 'copy' is the suffix we are copying the addresses
70 if(isset($vardef['module']) && preg_match('/Accounts/si',$vardef['module'])
71 && isset($displayParams['key']) && isset($displayParams['copy'])) {
73 if(isset($displayParams['key']) && is_array($displayParams['key'])) {
74 $database_key = $displayParams['key'];
76 $database_key[] = $displayParams['key'];
79 if(isset($displayParams['copy']) && is_array($displayParams['copy'])) {
80 $form = $displayParams['copy'];
82 $form[] = $displayParams['copy'];
85 if(count($database_key) != count($form)) {
86 global $app_list_strings;
87 $this->ss->trigger_error($app_list_strings['ERR_SMARTY_UNEQUAL_RELATED_FIELD_PARAMETERS']);
90 $copy_phone = isset($displayParams['copyPhone']) ? $displayParams['copyPhone'] : true;
92 $field_to_name = array();
93 $field_to_name['id'] = $vardef['id_name'];
94 $field_to_name['name'] = $vardef['name'];
95 $address_fields = array('_address_street', '_address_city', '_address_state', '_address_postalcode', '_address_country');
97 foreach($form as $f) {
98 foreach($address_fields as $afield) {
99 $field_to_name[$database_key[$count] . $afield] = $f . $afield;
104 $popup_request_data = array(
105 'call_back_function' => $call_back_function,
106 'form_name' => $form_name,
107 'field_to_name_array' => $field_to_name,
111 $popup_request_data['field_to_name_array']['phone_office'] = 'phone_work';
113 } elseif(isset($displayParams['field_to_name_array'])) {
114 $popup_request_data = array(
115 'call_back_function' => $call_back_function,
116 'form_name' => $form_name,
117 'field_to_name_array' => $displayParams['field_to_name_array'],
120 $popup_request_data = array(
121 'call_back_function' => $call_back_function,
122 'form_name' => $form_name,
123 'field_to_name_array' => array(
124 'id' => (empty($displayParams['idName']) ? $vardef['id_name'] : ($displayParams['idName'] . '_' . $vardef['id_name'])) ,
125 ((empty($vardef['rname'])) ? 'name' : $vardef['rname']) => (empty($displayParams['idName']) ? $vardef['name'] : $displayParams['idName']),
129 $json = getJSONobj();
130 $displayParams['popupData'] = '{literal}'.$json->encode($popup_request_data). '{/literal}';
131 if(!isset($displayParams['readOnly'])) {
132 $displayParams['readOnly'] = '';
134 $displayParams['readOnly'] = $displayParams['readOnly'] == false ? '' : 'READONLY';
136 $this->setup($parentFieldArray, $vardef, $displayParams, $tabindex);
137 return $this->fetch('include/SugarFields/Fields/Relate/EditView.tpl');
140 function getPopupViewSmarty($parentFieldArray, $vardef, $displayParams, $tabindex){
141 $displayParams['clearOnly'] = true;
142 return $this->getSearchViewSmarty($parentFieldArray, $vardef, $displayParams, $tabindex);
145 function getSearchViewSmarty($parentFieldArray, $vardef, $displayParams, $tabindex) {
146 $call_back_function = 'set_return';
147 if(isset($displayParams['call_back_function'])) {
148 $call_back_function = $displayParams['call_back_function'];
150 $form_name = 'search_form';
151 if(isset($displayParams['formName'])) {
152 $form_name = $displayParams['formName'];
154 if(!empty($vardef['rname']) && $vardef['rname'] == 'user_name'){
155 $displayParams['useIdSearch'] = true;
157 //Special Case for accounts; use the displayParams array and retrieve
158 //the key and copy indexes. 'key' is the suffix of the field we are searching
159 //the Account's address with. 'copy' is the suffix we are copying the addresses
161 if(isset($vardef['module']) && preg_match('/Accounts/si',$vardef['module'])
162 && isset($displayParams['key']) && isset($displayParams['copy'])) {
164 if(isset($displayParams['key']) && is_array($displayParams['key'])) {
165 $database_key = $displayParams['key'];
167 $database_key[] = $displayParams['key'];
170 if(isset($displayParams['copy']) && is_array($displayParams['copy'])) {
171 $form = $displayParams['copy'];
173 $form[] = $displayParams['copy'];
176 if(count($database_key) != count($form)) {
177 global $app_list_strings;
178 $this->ss->trigger_error($app_list_strings['ERR_SMARTY_UNEQUAL_RELATED_FIELD_PARAMETERS']);
181 $copy_phone = isset($displayParams['copyPhone']) ? $displayParams['copyPhone'] : true;
183 $field_to_name = array();
184 $field_to_name['id'] = $vardef['id_name'];
185 $field_to_name['name'] = $vardef['name'];
186 $address_fields = array('_address_street', '_address_city', '_address_state', '_address_postalcode', '_address_country');
188 foreach($form as $f) {
189 foreach($address_fields as $afield) {
190 $field_to_name[$database_key[$count] . $afield] = $f . $afield;
195 $popup_request_data = array(
196 'call_back_function' => $call_back_function,
197 'form_name' => $form_name,
198 'field_to_name_array' => $field_to_name,
202 $popup_request_data['field_to_name_array']['phone_office'] = 'phone_work';
204 } elseif(isset($displayParams['field_to_name_array'])) {
205 $popup_request_data = array(
206 'call_back_function' => $call_back_function,
207 'form_name' => $form_name,
208 'field_to_name_array' => $displayParams['field_to_name_array'],
211 $popup_request_data = array(
212 'call_back_function' => $call_back_function,
213 'form_name' => $form_name,
214 'field_to_name_array' => array(
215 'id' => $vardef['id_name'],
216 ((empty($vardef['rname'])) ? 'name' : $vardef['rname']) => $vardef['name'],
220 $json = getJSONobj();
221 $displayParams['popupData'] = '{literal}'.$json->encode($popup_request_data). '{/literal}';
222 if(!isset($displayParams['readOnly'])) {
223 $displayParams['readOnly'] = '';
225 $displayParams['readOnly'] = $displayParams['readOnly'] == false ? '' : 'READONLY';
227 $this->setup($parentFieldArray, $vardef, $displayParams, $tabindex);
228 return $this->fetch('include/SugarFields/Fields/Relate/SearchView.tpl');
231 function formatField($rawField, $vardef) {
232 if ('contact_name' == $vardef['name']){
233 $default_locale_name_format = $GLOBALS['current_user']->getPreference('default_locale_name_format');
234 $default_locale_name_format = trim(preg_replace('/s/i', '', $default_locale_name_format));
237 $temp = explode(' ', $rawField);
238 if ( !isset($temp[1]) ) {
240 $names['l'] = $temp[0];
242 elseif ( !empty($temp) ) {
243 $names['f'] = $temp[0];
244 $names['l'] = $temp[1];
246 for($i=0;$i<strlen($default_locale_name_format);$i++){
247 $new_field .= array_key_exists($default_locale_name_format{$i}, $names) ? $names[$default_locale_name_format{$i}] : $default_locale_name_format{$i};
250 else $new_field = $rawField;