]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/SugarFields/Fields/Relate/SugarFieldRelate.php
Release 6.2.0
[Github/sugarcrm.git] / include / SugarFields / Fields / Relate / SugarFieldRelate.php
1 <?php
2 /*********************************************************************************
3  * SugarCRM Community Edition 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($this->findTemplate('DetailView'));
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($this->findTemplate('EditView')); 
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         
158         //Special Case for accounts; use the displayParams array and retrieve
159         //the key and copy indexes.  'key' is the suffix of the field we are searching
160         //the Account's address with.  'copy' is the suffix we are copying the addresses
161         //form fields into.
162         if(isset($vardef['module']) && preg_match('/Accounts/si',$vardef['module']) 
163            && isset($displayParams['key']) && isset($displayParams['copy'])) {
164             
165             if(isset($displayParams['key']) && is_array($displayParams['key'])) {
166               $database_key = $displayParams['key'];    
167             } else {
168               $database_key[] = $displayParams['key'];
169             }
170             
171             if(isset($displayParams['copy']) && is_array($displayParams['copy'])) {
172                 $form = $displayParams['copy'];
173             } else {
174                 $form[] = $displayParams['copy'];
175             }
176             
177             if(count($database_key) != count($form)) {
178               global $app_list_strings;
179               $this->ss->trigger_error($app_list_strings['ERR_SMARTY_UNEQUAL_RELATED_FIELD_PARAMETERS']);
180             } //if
181             
182             $copy_phone = isset($displayParams['copyPhone']) ? $displayParams['copyPhone'] : true;
183             
184             $field_to_name = array();
185             $field_to_name['id'] = $vardef['id_name'];
186             $field_to_name['name'] = $vardef['name'];
187             $address_fields = array('_address_street', '_address_city', '_address_state', '_address_postalcode', '_address_country');
188             $count = 0;
189             foreach($form as $f) {
190                 foreach($address_fields as $afield) {
191                     $field_to_name[$database_key[$count] . $afield] = $f . $afield;
192                 }
193                 $count++;
194             }
195
196             $popup_request_data = array(
197                 'call_back_function' => $call_back_function,
198                 'form_name' => $form_name,
199                 'field_to_name_array' => $field_to_name,
200             );
201                 
202             if($copy_phone) {
203               $popup_request_data['field_to_name_array']['phone_office'] = 'phone_work';
204             }               
205         } elseif(isset($displayParams['field_to_name_array'])) {
206             $popup_request_data = array(
207                 'call_back_function' => $call_back_function,
208                 'form_name' => $form_name,
209                 'field_to_name_array' => $displayParams['field_to_name_array'],
210             );  
211         } else {
212             $popup_request_data = array(
213                 'call_back_function' => $call_back_function,
214                 'form_name' => $form_name,
215                 'field_to_name_array' => array(
216                           'id' => $vardef['id_name'],
217                           ((empty($vardef['rname'])) ? 'name' : $vardef['rname']) => $vardef['name'],
218                     ),
219                 );
220         }
221         $json = getJSONobj();
222         $displayParams['popupData'] = '{literal}'.$json->encode($popup_request_data). '{/literal}';
223         if(!isset($displayParams['readOnly'])) {
224            $displayParams['readOnly'] = '';
225         } else {
226            $displayParams['readOnly'] = $displayParams['readOnly'] == false ? '' : 'READONLY';  
227         }
228         $this->setup($parentFieldArray, $vardef, $displayParams, $tabindex);
229         return $this->fetch($this->findTemplate('SearchView')); 
230     }    
231     
232     function formatField($rawField, $vardef) {
233         if ('contact_name' == $vardef['name']){
234             $default_locale_name_format = $GLOBALS['current_user']->getPreference('default_locale_name_format');
235             $default_locale_name_format = trim(preg_replace('/s/i', '', $default_locale_name_format));
236             $new_field = '';
237             $names = array();
238             $temp = explode(' ', $rawField);
239             if ( !isset($temp[1]) ) {
240                 $names['f'] = '';
241                 $names['l'] = $temp[0];
242             }
243             elseif ( !empty($temp) ) {
244                 $names['f'] = $temp[0];
245                 $names['l'] = $temp[1];
246             }
247             for($i=0;$i<strlen($default_locale_name_format);$i++){
248                     $new_field .= array_key_exists($default_locale_name_format{$i}, $names) ? $names[$default_locale_name_format{$i}] : $default_locale_name_format{$i};
249             }
250         }
251         else  $new_field = $rawField;
252         
253         return $new_field;
254     }
255     
256     /**
257      * @see SugarFieldBase::importSanitize()
258      */
259     public function importSanitize(
260         $value,
261         $vardef,
262         $focus,
263         ImportFieldSanitize $settings
264         )
265     {
266         if ( !isset($vardef['module']) )
267             return false;
268         $newbean = loadBean($vardef['module']);
269         
270         // Bug 38885 - If we are relating to the Users table on user_name, there's a good chance
271         // that the related field data is the full_name, rather than the user_name. So to be sure
272         // let's try to lookup the field the relationship is expecting to use (user_name).
273         if ( $vardef['module'] == 'Users' && $vardef['rname'] == 'user_name' ) {
274             $userFocus = new User;
275             $userFocus->retrieve_by_string_fields(
276                 array($userFocus->db->concat('users',array('first_name','last_name')) => $value ));
277             if ( !empty($userFocus->id) ) {
278                 $value = $userFocus->user_name;
279             }
280         }       
281         
282         // Bug 32869 - Assumed related field name is 'name' if it is not specified
283         if ( !isset($vardef['rname']) )
284             $vardef['rname'] = 'name';
285         
286         // Bug 27046 - Validate field against type as it is in the related field
287         $rvardef = $newbean->getFieldDefinition($vardef['rname']);
288         if ( isset($rvardef['type']) 
289                 && method_exists($this,$rvardef['type']) ) {
290             $fieldtype = $rvardef['type'];
291             $returnValue = $settings->$fieldtype($value,$rvardef);
292             if ( !$returnValue )
293                 return false;
294             else
295                 $value = $returnValue;
296         }
297         
298         if ( isset($vardef['id_name']) ) {
299             $idField = $vardef['id_name'];
300             
301             // Bug 24075 - clear out id field value if it is invalid
302             if ( isset($focus->$idField) ) {
303                 $checkfocus = loadBean($vardef['module']);
304                 if ( $checkfocus && is_null($checkfocus->retrieve($focus->$idField)) )
305                     $focus->$idField = '';
306             }
307             
308             // be sure that the id isn't already set for this row
309             if ( empty($focus->$idField)
310                     && $idField != $vardef['name']
311                     && !empty($vardef['rname']) 
312                     && !empty($vardef['table'])) {
313                 // Bug 27562 - Check db_concat_fields first to see if the field name is a concat
314                 $relatedFieldDef = $newbean->getFieldDefinition($vardef['rname']);
315                 if ( isset($relatedFieldDef['db_concat_fields']) 
316                         && is_array($relatedFieldDef['db_concat_fields']) )
317                     $fieldname = $focus->db->concat($vardef['table'],$relatedFieldDef['db_concat_fields']);
318                 else
319                     $fieldname = $vardef['rname'];
320                 // lookup first record that matches in linked table
321                 $query = "SELECT id 
322                             FROM {$vardef['table']} 
323                             WHERE {$fieldname} = '" . $focus->db->quote($value) . "'
324                                 AND deleted != 1";
325                 
326                 $result = $focus->db->limitQuery($query,0,1,true, "Want only a single row");
327                 if(!empty($result)){
328                     if ( $relaterow = $focus->db->fetchByAssoc($result) )
329                         $focus->$idField = $relaterow['id'];
330                     elseif ( !$settings->addRelatedBean 
331                             || ( $newbean->bean_implements('ACL') && !$newbean->ACLAccess('save') )
332                             || ( in_array($newbean->module_dir,array('Teams','Users')) )
333                             )
334                         return false;
335                     else {
336                         // add this as a new record in that bean, then relate
337                         if ( isset($relatedFieldDef['db_concat_fields']) 
338                                 && is_array($relatedFieldDef['db_concat_fields']) ) {
339                             $relatedFieldParts = explode(' ',$value);
340                             foreach ($relatedFieldDef['db_concat_fields'] as $relatedField)
341                                 $newbean->$relatedField = array_shift($relatedFieldParts);
342                         }
343                         else
344                             $newbean->$vardef['rname'] = $value;
345                         if ( !isset($focus->assigned_user_id) || $focus->assigned_user_id == '' )
346                             $newbean->assigned_user_id = $GLOBALS['current_user']->id;
347                         else
348                             $newbean->assigned_user_id = $focus->assigned_user_id;
349                         if ( !isset($focus->modified_user_id) || $focus->modified_user_id == '' )
350                             $newbean->modified_user_id = $GLOBALS['current_user']->id;
351                         else
352                             $newbean->modified_user_id = $focus->modified_user_id;
353                         
354                         // populate fields from the parent bean to the child bean
355                         $focus->populateRelatedBean($newbean);
356                         
357                         $newbean->save(false);
358                         $focus->$idField = $newbean->id;
359                         $settings->createdBeans[] = ImportFile::writeRowToLastImport(
360                                 $focus->module_dir,$newbean->object_name,$newbean->id);
361                     }
362                 }
363             }
364         }
365         
366         return $value;
367     }
368 }