]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/SugarFields/Fields/Relate/SugarFieldRelate.php
Release 6.1.4
[Github/sugarcrm.git] / include / SugarFields / Fields / Relate / SugarFieldRelate.php
1 <?php
2 /*********************************************************************************
3  * SugarCRM is a customer relationship management program developed by
4  * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
5  * 
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.
12  * 
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
16  * details.
17  * 
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
21  * 02110-1301 USA.
22  * 
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.
25  * 
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.
29  * 
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  ********************************************************************************/
36
37 require_once('include/SugarFields/Fields/Base/SugarFieldBase.php');
38
39 class SugarFieldRelate extends SugarFieldBase {
40     
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);
45         }else{
46             $this->ss->assign('nolink', false);
47         }
48         $this->setup($parentFieldArray, $vardef, $displayParams, $tabindex);
49         return $this->fetch('include/SugarFields/Fields/Relate/DetailView.tpl');
50     }
51     
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);
55         }
56         
57         $call_back_function = 'set_return';
58         if(isset($displayParams['call_back_function'])) {
59             $call_back_function = $displayParams['call_back_function'];
60         }
61         $form_name = 'EditView';
62         if(isset($displayParams['formName'])) {
63             $form_name = $displayParams['formName'];
64         }
65         
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
69         //form fields into.
70         if(isset($vardef['module']) && preg_match('/Accounts/si',$vardef['module']) 
71            && isset($displayParams['key']) && isset($displayParams['copy'])) {
72             
73             if(isset($displayParams['key']) && is_array($displayParams['key'])) {
74               $database_key = $displayParams['key'];    
75             } else {
76               $database_key[] = $displayParams['key'];
77             }
78             
79             if(isset($displayParams['copy']) && is_array($displayParams['copy'])) {
80                 $form = $displayParams['copy'];
81             } else {
82                 $form[] = $displayParams['copy'];
83             }
84             
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']);
88             } //if
89             
90             $copy_phone = isset($displayParams['copyPhone']) ? $displayParams['copyPhone'] : true;
91             
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');
96             $count = 0;
97             foreach($form as $f) {
98                 foreach($address_fields as $afield) {
99                     $field_to_name[$database_key[$count] . $afield] = $f . $afield;
100                 }
101                 $count++;
102             }
103
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,
108             );
109                 
110             if($copy_phone) {
111               $popup_request_data['field_to_name_array']['phone_office'] = 'phone_work';
112             }               
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'],
118             );  
119         } else {
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']),
126                     ),
127                 );
128         }
129         $json = getJSONobj();
130         $displayParams['popupData'] = '{literal}'.$json->encode($popup_request_data). '{/literal}';
131         if(!isset($displayParams['readOnly'])) {
132            $displayParams['readOnly'] = '';
133         } else {
134            $displayParams['readOnly'] = $displayParams['readOnly'] == false ? '' : 'READONLY';  
135         }
136         $this->setup($parentFieldArray, $vardef, $displayParams, $tabindex);
137         return $this->fetch('include/SugarFields/Fields/Relate/EditView.tpl'); 
138     }
139     
140     function getPopupViewSmarty($parentFieldArray, $vardef, $displayParams, $tabindex){
141         $displayParams['clearOnly'] = true;
142         return $this->getSearchViewSmarty($parentFieldArray, $vardef, $displayParams, $tabindex);
143     }
144     
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'];
149         }
150         $form_name = 'search_form';
151         if(isset($displayParams['formName'])) {
152             $form_name = $displayParams['formName'];
153         }
154         if(!empty($vardef['rname']) && $vardef['rname'] == 'user_name'){
155                 $displayParams['useIdSearch'] = true;
156         }
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
160         //form fields into.
161         if(isset($vardef['module']) && preg_match('/Accounts/si',$vardef['module']) 
162            && isset($displayParams['key']) && isset($displayParams['copy'])) {
163             
164             if(isset($displayParams['key']) && is_array($displayParams['key'])) {
165               $database_key = $displayParams['key'];    
166             } else {
167               $database_key[] = $displayParams['key'];
168             }
169             
170             if(isset($displayParams['copy']) && is_array($displayParams['copy'])) {
171                 $form = $displayParams['copy'];
172             } else {
173                 $form[] = $displayParams['copy'];
174             }
175             
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']);
179             } //if
180             
181             $copy_phone = isset($displayParams['copyPhone']) ? $displayParams['copyPhone'] : true;
182             
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');
187             $count = 0;
188             foreach($form as $f) {
189                 foreach($address_fields as $afield) {
190                     $field_to_name[$database_key[$count] . $afield] = $f . $afield;
191                 }
192                 $count++;
193             }
194
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,
199             );
200                 
201             if($copy_phone) {
202               $popup_request_data['field_to_name_array']['phone_office'] = 'phone_work';
203             }               
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'],
209             );  
210         } else {
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'],
217                     ),
218                 );
219         }
220         $json = getJSONobj();
221         $displayParams['popupData'] = '{literal}'.$json->encode($popup_request_data). '{/literal}';
222         if(!isset($displayParams['readOnly'])) {
223            $displayParams['readOnly'] = '';
224         } else {
225            $displayParams['readOnly'] = $displayParams['readOnly'] == false ? '' : 'READONLY';  
226         }
227         $this->setup($parentFieldArray, $vardef, $displayParams, $tabindex);
228         return $this->fetch('include/SugarFields/Fields/Relate/SearchView.tpl'); 
229     }    
230     
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));
235             $new_field = '';
236             $names = array();
237             $temp = explode(' ', $rawField);
238             if ( !isset($temp[1]) ) {
239                 $names['f'] = '';
240                 $names['l'] = $temp[0];
241             }
242             elseif ( !empty($temp) ) {
243                 $names['f'] = $temp[0];
244                 $names['l'] = $temp[1];
245             }
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};
248             }
249         }
250         else  $new_field = $rawField;
251         
252         return $new_field;
253     }
254 }
255 ?>