2 /*********************************************************************************
3 * SugarCRM Community Edition is a customer relationship management program developed by
4 * SugarCRM, Inc. Copyright (C) 2004-2012 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'));
53 * @see SugarFieldBase::getEditViewSmarty()
55 public function getEditViewSmarty($parentFieldArray, $vardef, $displayParams, $tabindex)
57 if(!empty($vardef['function']['returns']) && $vardef['function']['returns'] == 'html'){
58 return parent::getEditViewSmarty($parentFieldArray, $vardef, $displayParams, $tabindex);
61 $call_back_function = 'set_return';
62 if(isset($displayParams['call_back_function'])) {
63 $call_back_function = $displayParams['call_back_function'];
65 $form_name = 'EditView';
66 if(isset($displayParams['formName'])) {
67 $form_name = $displayParams['formName'];
70 if (isset($displayParams['idName']))
72 $rpos = strrpos($displayParams['idName'], $vardef['name']);
73 $displayParams['idNameHidden'] = substr($displayParams['idName'], 0, $rpos);
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
79 if(isset($vardef['module']) && preg_match('/Accounts/si',$vardef['module'])
80 && isset($displayParams['key']) && isset($displayParams['copy'])) {
82 if(isset($displayParams['key']) && is_array($displayParams['key'])) {
83 $database_key = $displayParams['key'];
85 $database_key[] = $displayParams['key'];
88 if(isset($displayParams['copy']) && is_array($displayParams['copy'])) {
89 $form = $displayParams['copy'];
91 $form[] = $displayParams['copy'];
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']);
99 $copy_phone = isset($displayParams['copyPhone']) ? $displayParams['copyPhone'] : true;
101 $field_to_name = array();
102 $field_to_name['id'] = $vardef['id_name'];
103 $field_to_name['name'] = $vardef['name'];
104 $address_fields = isset($displayParams['field_to_name_array']) ? $displayParams['field_to_name_array'] : array('_address_street', '_address_city', '_address_state', '_address_postalcode', '_address_country');
106 foreach($form as $f) {
107 foreach($address_fields as $afield) {
108 $field_to_name[$database_key[$count] . $afield] = $f . $afield;
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,
120 $popup_request_data['field_to_name_array']['phone_office'] = 'phone_work';
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'],
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']),
140 $json = getJSONobj();
141 $displayParams['popupData'] = '{literal}'.$json->encode($popup_request_data). '{/literal}';
142 if(!isset($displayParams['readOnly'])) {
143 $displayParams['readOnly'] = '';
145 $displayParams['readOnly'] = $displayParams['readOnly'] == false ? '' : 'READONLY';
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'];
156 $this->setup($parentFieldArray, $vardef, $displayParams, $tabindex);
157 return $this->fetch($this->findTemplate('EditView'));
160 function getPopupViewSmarty($parentFieldArray, $vardef, $displayParams, $tabindex){
161 return $this->getSearchViewSmarty($parentFieldArray, $vardef, $displayParams, $tabindex);
164 function getSearchViewSmarty($parentFieldArray, $vardef, $displayParams, $tabindex) {
165 $call_back_function = 'set_return';
166 if(isset($displayParams['call_back_function'])) {
167 $call_back_function = $displayParams['call_back_function'];
169 $form_name = 'search_form';
170 if(isset($displayParams['formName'])) {
171 $form_name = $displayParams['formName'];
173 if(!empty($vardef['rname']) && $vardef['rname'] == 'user_name'){
174 $displayParams['useIdSearch'] = true;
177 //Special Case for accounts; use the displayParams array and retrieve
178 //the key and copy indexes. 'key' is the suffix of the field we are searching
179 //the Account's address with. 'copy' is the suffix we are copying the addresses
181 if(isset($vardef['module']) && preg_match('/Accounts/si',$vardef['module'])
182 && isset($displayParams['key']) && isset($displayParams['copy'])) {
184 if(isset($displayParams['key']) && is_array($displayParams['key'])) {
185 $database_key = $displayParams['key'];
187 $database_key[] = $displayParams['key'];
190 if(isset($displayParams['copy']) && is_array($displayParams['copy'])) {
191 $form = $displayParams['copy'];
193 $form[] = $displayParams['copy'];
196 if(count($database_key) != count($form)) {
197 global $app_list_strings;
198 $this->ss->trigger_error($app_list_strings['ERR_SMARTY_UNEQUAL_RELATED_FIELD_PARAMETERS']);
201 $copy_phone = isset($displayParams['copyPhone']) ? $displayParams['copyPhone'] : true;
203 $field_to_name = array();
204 $field_to_name['id'] = $vardef['id_name'];
205 $field_to_name['name'] = $vardef['name'];
206 $address_fields = array('_address_street', '_address_city', '_address_state', '_address_postalcode', '_address_country');
208 foreach($form as $f) {
209 foreach($address_fields as $afield) {
210 $field_to_name[$database_key[$count] . $afield] = $f . $afield;
215 $popup_request_data = array(
216 'call_back_function' => $call_back_function,
217 'form_name' => $form_name,
218 'field_to_name_array' => $field_to_name,
222 $popup_request_data['field_to_name_array']['phone_office'] = 'phone_work';
224 } elseif(isset($displayParams['field_to_name_array'])) {
225 $popup_request_data = array(
226 'call_back_function' => $call_back_function,
227 'form_name' => $form_name,
228 'field_to_name_array' => $displayParams['field_to_name_array'],
231 $popup_request_data = array(
232 'call_back_function' => $call_back_function,
233 'form_name' => $form_name,
234 'field_to_name_array' => array(
235 'id' => $vardef['id_name'],
236 ((empty($vardef['rname'])) ? 'name' : $vardef['rname']) => $vardef['name'],
240 $json = getJSONobj();
241 $displayParams['popupData'] = '{literal}'.$json->encode($popup_request_data). '{/literal}';
242 if(!isset($displayParams['readOnly'])) {
243 $displayParams['readOnly'] = '';
245 $displayParams['readOnly'] = $displayParams['readOnly'] == false ? '' : 'READONLY';
247 $this->setup($parentFieldArray, $vardef, $displayParams, $tabindex);
248 return $this->fetch($this->findTemplate('SearchView'));
251 function formatField($rawField, $vardef) {
252 if ('contact_name' == $vardef['name']){
253 $default_locale_name_format = $GLOBALS['current_user']->getPreference('default_locale_name_format');
254 $default_locale_name_format = trim(preg_replace('/s/i', '', $default_locale_name_format));
257 $temp = explode(' ', $rawField);
258 if ( !isset($temp[1]) ) {
260 $names['l'] = $temp[0];
262 elseif ( !empty($temp) ) {
263 $names['f'] = $temp[0];
264 $names['l'] = $temp[1];
266 for($i=0;$i<strlen($default_locale_name_format);$i++){
267 $new_field .= array_key_exists($default_locale_name_format{$i}, $names) ? $names[$default_locale_name_format{$i}] : $default_locale_name_format{$i};
270 else $new_field = $rawField;
276 * @see SugarFieldBase::importSanitize()
278 public function importSanitize(
282 ImportFieldSanitize $settings
285 if ( !isset($vardef['module']) )
287 $newbean = loadBean($vardef['module']);
289 // Bug 38885 - If we are relating to the Users table on user_name, there's a good chance
290 // that the related field data is the full_name, rather than the user_name. So to be sure
291 // let's try to lookup the field the relationship is expecting to use (user_name).
292 if ( $vardef['module'] == 'Users' && isset($vardef['rname']) && $vardef['rname'] == 'user_name' ) {
293 $userFocus = new User;
294 $query = sprintf("SELECT user_name FROM {$userFocus->table_name} WHERE %s=%s AND deleted=0",
295 $userFocus->db->concat('users',array('first_name','last_name')), $userFocus->db->quoted($value));
296 $username = $userFocus->db->getOne($query);
297 if(!empty($username)) {
302 // Bug 32869 - Assumed related field name is 'name' if it is not specified
303 if ( !isset($vardef['rname']) )
304 $vardef['rname'] = 'name';
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);
315 $value = $returnValue;
318 if ( isset($vardef['id_name']) ) {
319 $idField = $vardef['id_name'];
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 = '';
328 // fixing bug #47722: Imports to Custom Relate Fields Do Not Work
329 if (!isset($vardef['table']))
331 // Set target module table as the default table name
332 $vardef['table'] = $newbean->table_name;
334 // be sure that the id isn't already set for this row
335 if ( empty($focus->$idField)
336 && $idField != $vardef['name']
337 && !empty($vardef['rname'])
338 && !empty($vardef['table'])) {
339 // Bug 27562 - Check db_concat_fields first to see if the field name is a concatenation.
340 $relatedFieldDef = $newbean->getFieldDefinition($vardef['rname']);
341 if ( isset($relatedFieldDef['db_concat_fields'])
342 && is_array($relatedFieldDef['db_concat_fields']) )
343 $fieldname = $focus->db->concat($vardef['table'],$relatedFieldDef['db_concat_fields']);
345 $fieldname = $vardef['rname'];
346 // lookup first record that matches in linked table
348 FROM {$vardef['table']}
349 WHERE {$fieldname} = '" . $focus->db->quote($value) . "'
352 $result = $focus->db->limitQuery($query,0,1,true, "Want only a single row");
354 if ( $relaterow = $focus->db->fetchByAssoc($result) )
355 $focus->$idField = $relaterow['id'];
356 elseif ( !$settings->addRelatedBean
357 || ( $newbean->bean_implements('ACL') && !$newbean->ACLAccess('save') )
358 || ( in_array($newbean->module_dir,array('Teams','Users')) )
362 // add this as a new record in that bean, then relate
363 if ( isset($relatedFieldDef['db_concat_fields'])
364 && is_array($relatedFieldDef['db_concat_fields']) ) {
365 $relatedFieldParts = explode(' ',$value);
366 foreach ($relatedFieldDef['db_concat_fields'] as $relatedField)
367 $newbean->$relatedField = array_shift($relatedFieldParts);
370 $newbean->$vardef['rname'] = $value;
371 if ( !isset($focus->assigned_user_id) || $focus->assigned_user_id == '' )
372 $newbean->assigned_user_id = $GLOBALS['current_user']->id;
374 $newbean->assigned_user_id = $focus->assigned_user_id;
375 if ( !isset($focus->modified_user_id) || $focus->modified_user_id == '' )
376 $newbean->modified_user_id = $GLOBALS['current_user']->id;
378 $newbean->modified_user_id = $focus->modified_user_id;
380 // populate fields from the parent bean to the child bean
381 $focus->populateRelatedBean($newbean);
383 $newbean->save(false);
384 $focus->$idField = $newbean->id;
385 $settings->createdBeans[] = ImportFile::writeRowToLastImport(
386 $focus->module_dir,$newbean->object_name,$newbean->id);