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