2 /*********************************************************************************
3 * SugarCRM Community Edition 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($this->findTemplate('DetailView'));
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($this->findTemplate('EditView'));
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;
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
162 if(isset($vardef['module']) && preg_match('/Accounts/si',$vardef['module'])
163 && isset($displayParams['key']) && isset($displayParams['copy'])) {
165 if(isset($displayParams['key']) && is_array($displayParams['key'])) {
166 $database_key = $displayParams['key'];
168 $database_key[] = $displayParams['key'];
171 if(isset($displayParams['copy']) && is_array($displayParams['copy'])) {
172 $form = $displayParams['copy'];
174 $form[] = $displayParams['copy'];
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']);
182 $copy_phone = isset($displayParams['copyPhone']) ? $displayParams['copyPhone'] : true;
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');
189 foreach($form as $f) {
190 foreach($address_fields as $afield) {
191 $field_to_name[$database_key[$count] . $afield] = $f . $afield;
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,
203 $popup_request_data['field_to_name_array']['phone_office'] = 'phone_work';
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'],
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'],
221 $json = getJSONobj();
222 $displayParams['popupData'] = '{literal}'.$json->encode($popup_request_data). '{/literal}';
223 if(!isset($displayParams['readOnly'])) {
224 $displayParams['readOnly'] = '';
226 $displayParams['readOnly'] = $displayParams['readOnly'] == false ? '' : 'READONLY';
228 $this->setup($parentFieldArray, $vardef, $displayParams, $tabindex);
229 return $this->fetch($this->findTemplate('SearchView'));
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));
238 $temp = explode(' ', $rawField);
239 if ( !isset($temp[1]) ) {
241 $names['l'] = $temp[0];
243 elseif ( !empty($temp) ) {
244 $names['f'] = $temp[0];
245 $names['l'] = $temp[1];
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};
251 else $new_field = $rawField;
257 * @see SugarFieldBase::importSanitize()
259 public function importSanitize(
263 ImportFieldSanitize $settings
266 if ( !isset($vardef['module']) )
268 $newbean = loadBean($vardef['module']);
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;
282 // Bug 32869 - Assumed related field name is 'name' if it is not specified
283 if ( !isset($vardef['rname']) )
284 $vardef['rname'] = 'name';
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);
295 $value = $returnValue;
298 if ( isset($vardef['id_name']) ) {
299 $idField = $vardef['id_name'];
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 = '';
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']);
319 $fieldname = $vardef['rname'];
320 // lookup first record that matches in linked table
322 FROM {$vardef['table']}
323 WHERE {$fieldname} = '" . $focus->db->quote($value) . "'
326 $result = $focus->db->limitQuery($query,0,1,true, "Want only a single row");
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')) )
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);
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;
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;
352 $newbean->modified_user_id = $focus->modified_user_id;
354 // populate fields from the parent bean to the child bean
355 $focus->populateRelatedBean($newbean);
357 $newbean->save(false);
358 $focus->$idField = $newbean->id;
359 $settings->createdBeans[] = ImportFile::writeRowToLastImport(
360 $focus->module_dir,$newbean->object_name,$newbean->id);