]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/SugarFields/Fields/Relate/SugarFieldRelate.php
Release 6.3.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     /**
53      * @see SugarFieldBase::getEditViewSmarty()
54      */
55     public function getEditViewSmarty($parentFieldArray, $vardef, $displayParams, $tabindex) 
56     {
57         if(!empty($vardef['function']['returns']) && $vardef['function']['returns'] == 'html'){
58             return parent::getEditViewSmarty($parentFieldArray, $vardef, $displayParams, $tabindex);
59         }
60         
61         $call_back_function = 'set_return';
62         if(isset($displayParams['call_back_function'])) {
63             $call_back_function = $displayParams['call_back_function'];
64         }
65         $form_name = 'EditView';
66         if(isset($displayParams['formName'])) {
67             $form_name = $displayParams['formName'];
68         }
69
70         if (isset($displayParams['idName']))
71         {
72             $rpos = strrpos($displayParams['idName'], $vardef['name']);
73             $displayParams['idNameHidden'] = substr($displayParams['idName'], 0, $rpos);
74         }
75         //Special Case for accounts; use the displayParams array and retrieve
76         //the key and copy indexes.  'key' is the suffix of the field we are searching
77         //the Account's address with.  'copy' is the suffix we are copying the addresses
78         //form fields into.
79         if(isset($vardef['module']) && preg_match('/Accounts/si',$vardef['module']) 
80            && isset($displayParams['key']) && isset($displayParams['copy'])) {
81             
82             if(isset($displayParams['key']) && is_array($displayParams['key'])) {
83               $database_key = $displayParams['key'];    
84             } else {
85               $database_key[] = $displayParams['key'];
86             }
87             
88             if(isset($displayParams['copy']) && is_array($displayParams['copy'])) {
89                 $form = $displayParams['copy'];
90             } else {
91                 $form[] = $displayParams['copy'];
92             }
93             
94             if(count($database_key) != count($form)) {
95               global $app_list_strings;
96               $this->ss->trigger_error($app_list_strings['ERR_SMARTY_UNEQUAL_RELATED_FIELD_PARAMETERS']);
97             } //if
98             
99             $copy_phone = isset($displayParams['copyPhone']) ? $displayParams['copyPhone'] : true;
100             
101             $field_to_name = array();
102             $field_to_name['id'] = $vardef['id_name'];
103             $field_to_name['name'] = $vardef['name'];
104             $address_fields = array('_address_street', '_address_city', '_address_state', '_address_postalcode', '_address_country');
105             $count = 0;
106             foreach($form as $f) {
107                 foreach($address_fields as $afield) {
108                     $field_to_name[$database_key[$count] . $afield] = $f . $afield;
109                 }
110                 $count++;
111             }
112
113             $popup_request_data = array(
114                 'call_back_function' => $call_back_function,
115                 'form_name' => $form_name,
116                 'field_to_name_array' => $field_to_name,
117             );
118                 
119             if($copy_phone) {
120               $popup_request_data['field_to_name_array']['phone_office'] = 'phone_work';
121             }               
122         } elseif(isset($displayParams['field_to_name_array'])) {
123             $popup_request_data = array(
124                 'call_back_function' => $call_back_function,
125                 'form_name' => $form_name,
126                 'field_to_name_array' => $displayParams['field_to_name_array'],
127             );  
128         } else {
129             $popup_request_data = array(
130                 'call_back_function' => $call_back_function,
131                 'form_name' => $form_name,
132                 'field_to_name_array' => array(
133                           //'id' => (empty($displayParams['idName']) ? $vardef['id_name'] : ($displayParams['idName'] . '_' . $vardef['id_name'])) ,
134                           //bug 43770: Assigned to value could not be saved during lead conversion
135                           'id' => (empty($displayParams['idNameHidden']) ? $vardef['id_name'] : ($displayParams['idNameHidden'] . $vardef['id_name'])) ,
136                           ((empty($vardef['rname'])) ? 'name' : $vardef['rname']) => (empty($displayParams['idName']) ? $vardef['name'] : $displayParams['idName']),
137                     ),
138                 );
139         }
140         $json = getJSONobj();
141         $displayParams['popupData'] = '{literal}'.$json->encode($popup_request_data). '{/literal}';
142         if(!isset($displayParams['readOnly'])) {
143            $displayParams['readOnly'] = '';
144         } else {
145            $displayParams['readOnly'] = $displayParams['readOnly'] == false ? '' : 'READONLY';  
146         }
147         
148         $keys = $this->getAccessKey($vardef,'RELATE',$vardef['module']);
149         $displayParams['accessKeySelect'] = $keys['accessKeySelect'];
150         $displayParams['accessKeySelectLabel'] = $keys['accessKeySelectLabel'];
151         $displayParams['accessKeySelectTitle'] = $keys['accessKeySelectTitle'];
152         $displayParams['accessKeyClear'] = $keys['accessKeyClear'];
153         $displayParams['accessKeyClearLabel'] = $keys['accessKeyClearLabel'];
154         $displayParams['accessKeyClearTitle'] = $keys['accessKeyClearTitle'];
155
156         $this->setup($parentFieldArray, $vardef, $displayParams, $tabindex);
157         return $this->fetch($this->findTemplate('EditView')); 
158     }
159     
160     function getPopupViewSmarty($parentFieldArray, $vardef, $displayParams, $tabindex){
161         $displayParams['clearOnly'] = true;
162         return $this->getSearchViewSmarty($parentFieldArray, $vardef, $displayParams, $tabindex);
163     }
164     
165     function getSearchViewSmarty($parentFieldArray, $vardef, $displayParams, $tabindex) {
166         $call_back_function = 'set_return';
167         if(isset($displayParams['call_back_function'])) {
168             $call_back_function = $displayParams['call_back_function'];
169         }
170         $form_name = 'search_form';
171         if(isset($displayParams['formName'])) {
172             $form_name = $displayParams['formName'];
173         }
174         if(!empty($vardef['rname']) && $vardef['rname'] == 'user_name'){
175                 $displayParams['useIdSearch'] = true;
176         }
177         
178         //Special Case for accounts; use the displayParams array and retrieve
179         //the key and copy indexes.  'key' is the suffix of the field we are searching
180         //the Account's address with.  'copy' is the suffix we are copying the addresses
181         //form fields into.
182         if(isset($vardef['module']) && preg_match('/Accounts/si',$vardef['module']) 
183            && isset($displayParams['key']) && isset($displayParams['copy'])) {
184             
185             if(isset($displayParams['key']) && is_array($displayParams['key'])) {
186               $database_key = $displayParams['key'];    
187             } else {
188               $database_key[] = $displayParams['key'];
189             }
190             
191             if(isset($displayParams['copy']) && is_array($displayParams['copy'])) {
192                 $form = $displayParams['copy'];
193             } else {
194                 $form[] = $displayParams['copy'];
195             }
196             
197             if(count($database_key) != count($form)) {
198               global $app_list_strings;
199               $this->ss->trigger_error($app_list_strings['ERR_SMARTY_UNEQUAL_RELATED_FIELD_PARAMETERS']);
200             } //if
201             
202             $copy_phone = isset($displayParams['copyPhone']) ? $displayParams['copyPhone'] : true;
203             
204             $field_to_name = array();
205             $field_to_name['id'] = $vardef['id_name'];
206             $field_to_name['name'] = $vardef['name'];
207             $address_fields = array('_address_street', '_address_city', '_address_state', '_address_postalcode', '_address_country');
208             $count = 0;
209             foreach($form as $f) {
210                 foreach($address_fields as $afield) {
211                     $field_to_name[$database_key[$count] . $afield] = $f . $afield;
212                 }
213                 $count++;
214             }
215
216             $popup_request_data = array(
217                 'call_back_function' => $call_back_function,
218                 'form_name' => $form_name,
219                 'field_to_name_array' => $field_to_name,
220             );
221                 
222             if($copy_phone) {
223               $popup_request_data['field_to_name_array']['phone_office'] = 'phone_work';
224             }               
225         } elseif(isset($displayParams['field_to_name_array'])) {
226             $popup_request_data = array(
227                 'call_back_function' => $call_back_function,
228                 'form_name' => $form_name,
229                 'field_to_name_array' => $displayParams['field_to_name_array'],
230             );  
231         } else {
232             $popup_request_data = array(
233                 'call_back_function' => $call_back_function,
234                 'form_name' => $form_name,
235                 'field_to_name_array' => array(
236                           'id' => $vardef['id_name'],
237                           ((empty($vardef['rname'])) ? 'name' : $vardef['rname']) => $vardef['name'],
238                     ),
239                 );
240         }
241         $json = getJSONobj();
242         $displayParams['popupData'] = '{literal}'.$json->encode($popup_request_data). '{/literal}';
243         if(!isset($displayParams['readOnly'])) {
244            $displayParams['readOnly'] = '';
245         } else {
246            $displayParams['readOnly'] = $displayParams['readOnly'] == false ? '' : 'READONLY';  
247         }
248         $this->setup($parentFieldArray, $vardef, $displayParams, $tabindex);
249         return $this->fetch($this->findTemplate('SearchView')); 
250     }    
251     
252     function formatField($rawField, $vardef) {
253         if ('contact_name' == $vardef['name']){
254             $default_locale_name_format = $GLOBALS['current_user']->getPreference('default_locale_name_format');
255             $default_locale_name_format = trim(preg_replace('/s/i', '', $default_locale_name_format));
256             $new_field = '';
257             $names = array();
258             $temp = explode(' ', $rawField);
259             if ( !isset($temp[1]) ) {
260                 $names['f'] = '';
261                 $names['l'] = $temp[0];
262             }
263             elseif ( !empty($temp) ) {
264                 $names['f'] = $temp[0];
265                 $names['l'] = $temp[1];
266             }
267             for($i=0;$i<strlen($default_locale_name_format);$i++){
268                     $new_field .= array_key_exists($default_locale_name_format{$i}, $names) ? $names[$default_locale_name_format{$i}] : $default_locale_name_format{$i};
269             }
270         }
271         else  $new_field = $rawField;
272         
273         return $new_field;
274     }
275     
276     /**
277      * @see SugarFieldBase::importSanitize()
278      */
279     public function importSanitize(
280         $value,
281         $vardef,
282         $focus,
283         ImportFieldSanitize $settings
284         )
285     {
286         if ( !isset($vardef['module']) )
287             return false;
288         $newbean = loadBean($vardef['module']);
289
290         // Bug 38885 - If we are relating to the Users table on user_name, there's a good chance
291         // that the related field data is the full_name, rather than the user_name. So to be sure
292         // let's try to lookup the field the relationship is expecting to use (user_name).
293         if ( $vardef['module'] == 'Users' && isset($vardef['rname']) && $vardef['rname'] == 'user_name' ) {
294             $userFocus = new User;
295             $userFocus->retrieve_by_string_fields(
296                 array($userFocus->db->concat('users',array('first_name','last_name')) => $value ));
297             if ( !empty($userFocus->id) ) {
298                 $value = $userFocus->user_name;
299             }
300         }       
301         
302         // Bug 32869 - Assumed related field name is 'name' if it is not specified
303         if ( !isset($vardef['rname']) )
304             $vardef['rname'] = 'name';
305         
306         // Bug 27046 - Validate field against type as it is in the related field
307         $rvardef = $newbean->getFieldDefinition($vardef['rname']);
308         if ( isset($rvardef['type']) 
309                 && method_exists($this,$rvardef['type']) ) {
310             $fieldtype = $rvardef['type'];
311             $returnValue = $settings->$fieldtype($value,$rvardef);
312             if ( !$returnValue )
313                 return false;
314             else
315                 $value = $returnValue;
316         }
317         
318         if ( isset($vardef['id_name']) ) {
319             $idField = $vardef['id_name'];
320             
321             // Bug 24075 - clear out id field value if it is invalid
322             if ( isset($focus->$idField) ) {
323                 $checkfocus = loadBean($vardef['module']);
324                 if ( $checkfocus && is_null($checkfocus->retrieve($focus->$idField)) )
325                     $focus->$idField = '';
326             }
327             
328             // be sure that the id isn't already set for this row
329             if ( empty($focus->$idField)
330                     && $idField != $vardef['name']
331                     && !empty($vardef['rname']) 
332                     && !empty($vardef['table'])) {
333                 // Bug 27562 - Check db_concat_fields first to see if the field name is a concat
334                 $relatedFieldDef = $newbean->getFieldDefinition($vardef['rname']);
335                 if ( isset($relatedFieldDef['db_concat_fields']) 
336                         && is_array($relatedFieldDef['db_concat_fields']) )
337                     $fieldname = $focus->db->concat($vardef['table'],$relatedFieldDef['db_concat_fields']);
338                 else
339                     $fieldname = $vardef['rname'];
340                 // lookup first record that matches in linked table
341                 $query = "SELECT id 
342                             FROM {$vardef['table']} 
343                             WHERE {$fieldname} = '" . $focus->db->quote($value) . "'
344                                 AND deleted != 1";
345                 
346                 $result = $focus->db->limitQuery($query,0,1,true, "Want only a single row");
347                 if(!empty($result)){
348                     if ( $relaterow = $focus->db->fetchByAssoc($result) )
349                         $focus->$idField = $relaterow['id'];
350                     elseif ( !$settings->addRelatedBean 
351                             || ( $newbean->bean_implements('ACL') && !$newbean->ACLAccess('save') )
352                             || ( in_array($newbean->module_dir,array('Teams','Users')) )
353                             )
354                         return false;
355                     else {
356                         // add this as a new record in that bean, then relate
357                         if ( isset($relatedFieldDef['db_concat_fields']) 
358                                 && is_array($relatedFieldDef['db_concat_fields']) ) {
359                             $relatedFieldParts = explode(' ',$value);
360                             foreach ($relatedFieldDef['db_concat_fields'] as $relatedField)
361                                 $newbean->$relatedField = array_shift($relatedFieldParts);
362                         }
363                         else
364                             $newbean->$vardef['rname'] = $value;
365                         if ( !isset($focus->assigned_user_id) || $focus->assigned_user_id == '' )
366                             $newbean->assigned_user_id = $GLOBALS['current_user']->id;
367                         else
368                             $newbean->assigned_user_id = $focus->assigned_user_id;
369                         if ( !isset($focus->modified_user_id) || $focus->modified_user_id == '' )
370                             $newbean->modified_user_id = $GLOBALS['current_user']->id;
371                         else
372                             $newbean->modified_user_id = $focus->modified_user_id;
373                         
374                         // populate fields from the parent bean to the child bean
375                         $focus->populateRelatedBean($newbean);
376                         
377                         $newbean->save(false);
378                         $focus->$idField = $newbean->id;
379                         $settings->createdBeans[] = ImportFile::writeRowToLastImport(
380                                 $focus->module_dir,$newbean->object_name,$newbean->id);
381                     }
382                 }
383             }
384         }
385         
386         return $value;
387     }
388 }