2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4 * SugarCRM Community Edition is a customer relationship management program developed by
5 * SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
7 * This program is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU Affero General Public License version 3 as published by the
9 * Free Software Foundation with the addition of the following permission added
10 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
11 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
12 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
19 * You should have received a copy of the GNU Affero General Public License along with
20 * this program; if not, see http://www.gnu.org/licenses or write to the Free
21 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
25 * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
27 * The interactive user interfaces in modified source and object code versions
28 * of this program must display Appropriate Legal Notices, as required under
29 * Section 5 of the GNU Affero General Public License version 3.
31 * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
32 * these Appropriate Legal Notices must retain the display of the "Powered by
33 * SugarCRM" logo. If the display of the logo is not reasonably feasible for
34 * technical reasons, the Appropriate Legal Notices must display the words
35 * "Powered by SugarCRM".
36 ********************************************************************************/
38 $GLOBALS['studioReadOnlyFields'] = array('date_entered'=>1, 'date_modified'=>1, 'created_by'=>1, 'id'=>1, 'modified_user_id'=>1);
41 The view is the context this field will be used in
53 var $required = false;
55 var $default_value = null;
56 var $type = 'varchar';
65 var $importable = 'true' ;
66 var $duplicate_merge=0;
67 var $new_field_definition;
68 var $reportable = true;
69 var $label_value = '';
72 var $unified_search = 0;
73 var $supports_unified_search = false;
74 var $vardef_map = array(
77 // bug 15801 - need to ALWAYS keep default and default_value consistent as some methods/classes use one, some use another...
78 'default_value'=>'default',
79 'default'=>'default_value',
80 'display_default'=>'default_value',
81 // 'default_value'=>'default_value',
82 // 'default'=>'default_value',
84 'required'=>'required',
87 'massupdate'=>'massupdate',
90 'comments'=>'comment',
91 'importable'=>'importable',
92 'duplicate_merge'=>'duplicate_merge',
93 'duplicate_merge_dom_value'=>'duplicate_merge_dom_value', //bug #14897
94 'merge_filter'=>'merge_filter',
95 'reportable' => 'reportable',
100 //'disable_num_format'=>'ext3',
102 'label_value'=>'label_value',
103 'unified_search'=>'unified_search',
106 // fields to decode from post request
107 var $decode_from_request_fields_map = array('formula', 'dependency');
113 if(!empty($GLOBALS['studioReadOnlyFields'][$this->name]))$view = 'detail';
115 case 'search':return $this->get_html_search();
116 case 'edit': return $this->get_html_edit();
117 case 'list': return $this->get_html_list();
118 case 'detail': return $this->get_html_detail();
122 function set($values){
123 foreach($values as $name=>$value){
124 $this->$name = $value;
129 function get_html_edit(){
130 return 'not implemented';
133 function get_html_list(){
134 return $this->get_html_detail();
137 function get_html_detail(){
138 return 'not implemented';
141 function get_html_search(){
142 return $this->get_html_edit();
144 function get_html_label(){
146 $label = "{MOD." .$this->vname . "}";
147 if(!empty($GLOBALS['app_strings'][$this->vname])){
148 $label = "{APP." .$this->label . "}";
150 if($this->view == 'edit' && $this->is_required()){
151 $label .= '<span class="required">*</span>';
153 if($this->view == 'list'){
154 if(isset($this->bean)){
155 if(!empty($this->id)){
156 $name = $this->bean->table_name . '_cstm.'. $this->name;
157 $arrow = $this->bean->table_name . '_cstm_'. $this->name;
159 $name = $this->bean->table_name . '.'. $this->name;
160 $arrow = $this->bean->table_name . '_'. $this->name;
166 $label = "<a href='{ORDER_BY}$name' class='listViewThLinkS1'>{MOD.$this->label}{arrow_start}{".$arrow."_arrow}{arrow_end}</a>";
176 function get_xtpl($bean = false){
180 if(!empty($GLOBALS['studioReadOnlyFields'][$this->name]))$view = 'detail';
182 case 'search':return $this->get_xtpl_search();
183 case 'edit': return $this->get_xtpl_edit();
184 case 'list': return $this->get_xtpl_list();
185 case 'detail': return $this->get_xtpl_detail();
190 function get_xtpl_edit(){
191 return '/*not implemented*/';
194 function get_xtpl_list(){
195 return get_xtpl_detail();
198 function get_xtpl_detail(){
199 return '/*not implemented*/';
202 function get_xtpl_search(){
203 //return get_xtpl_edit();
206 function is_required(){
221 function get_db_type(){
222 if(!empty($this->type)) {
223 $type = $GLOBALS['db']->getColumnType($this->type);
225 if(!empty($type)) return " $type";
226 $type = $GLOBALS['db']->getColumnType("varchar");
227 return " $type({$this->len})";
230 function get_db_default($modify=false){
231 $GLOBALS['log']->debug('get_db_default(): default_value='.$this->default_value);
232 if (!$modify or empty($this->new_field_definition['default_value']) or $this->new_field_definition['default_value'] != $this->default_value ) {
233 if(!is_null($this->default_value)){ // add a default value if it is not null - we want to set a default even if default_value is '0', which is not null, but which is empty()
234 if(NULL == trim($this->default_value)){
235 return " DEFAULT NULL";
238 return " DEFAULT '$this->default_value'";
247 * Return the required clause for this field
248 * Confusingly, when modifying an existing field ($modify=true) there are two exactly opposite cases:
249 * 1. if called by Studio, only $this->required is set. If set, we return "NOT NULL" otherwise we return "NULL"
250 * 2. if not called by Studio, $this->required holds the OLD value of required, and new_field_definition['required'] is the NEW
251 * So if not called by Studio we want to return NULL if required=true (because we are changing FROM this setting)
254 function get_db_required($modify=false){
255 // $GLOBALS['log']->debug('get_db_required required='.$this->required." and ".(($modify)?"true":"false")." and ".print_r($this->new_field_definition,true));
259 if (!empty($this->new_field_definition['required'])) {
260 if ($this->required and $this->new_field_definition['required'] != $this->required) {
266 $req = ($this->required) ? " NOT NULL " : ''; // bug 17184 tyoung - set required correctly when modifying custom field in Studio
271 if (empty($this->new_field_definition['required']) or $this->new_field_definition['required'] != $this->required ) {
272 if(!empty($this->required) && $this->required){
281 /* function get_db_required($modify=false){
282 $GLOBALS['log']->debug('get_db_required required='.$this->required." and ".(($modify)?"true":"false")." and ".print_r($this->new_field_definition,true));
284 if (!empty($this->new_field_definition['required'])) {
285 if ($this->required and $this->new_field_definition['required'] != $this->required) {
291 if (empty($this->new_field_definition['required']) or $this->new_field_definition['required'] != $this->required ) {
292 if(!empty($this->required) && $this->required){
300 * Oracle Support: do not set required constraint if no default value is supplied.
301 * In this case the default value will be handled by the application/sugarbean.
303 function get_db_add_alter_table($table)
305 return $GLOBALS['db']->getHelper()->addColumnSQL($table, $this->get_field_def(), true);
308 function get_db_delete_alter_table($table)
310 return $GLOBALS['db']->getHelper()->dropColumnSQL(
312 $this->get_field_def()
317 * mysql requires the datatype caluse in the alter statment.it will be no-op anyway.
319 function get_db_modify_alter_table($table){
320 return $GLOBALS['db']->alterColumnSQL($table, $this->get_field_def());
328 function get_field_def(){
330 'required'=>$this->convertBooleanValue($this->required),
331 'source'=>'custom_fields',
333 'vname'=>$this->vname,
335 'massupdate'=>$this->massupdate,
336 'default'=>$this->default,
337 'comments'=> (isset($this->comments)) ? $this->comments : '',
338 'help'=> (isset($this->help)) ? $this->help : '',
339 'importable'=>$this->importable,
340 'duplicate_merge'=>$this->duplicate_merge,
341 'duplicate_merge_dom_value'=> isset($this->duplicate_merge_dom_value) ? $this->duplicate_merge_dom_value : $this->duplicate_merge,
342 'audited'=>$this->convertBooleanValue($this->audited),
343 'reportable'=>$this->convertBooleanValue($this->reportable),
344 'unified_search'=>$this->convertBooleanValue($this->unified_search)
346 if(!empty($this->len)){
347 $array['len'] = $this->len;
349 if(!empty($this->size)){
350 $array['size'] = $this->size;
352 $this->get_dup_merge_def($array);
357 protected function convertBooleanValue($value)
359 if ($value === 'true' || $value === '1' || $value === 1)
361 else if ($value === 'false' || $value === '0' || $value === 0)
368 /* if the field is duplicate merge enabled this function will return the vardef entry for the same.
370 function get_dup_merge_def(&$def) {
372 switch ($def['duplicate_merge_dom_value']) {
374 $def['duplicate_merge']='disabled';
377 $def['duplicate_merge']='enabled';
380 $def['merge_filter']='enabled';
381 $def['duplicate_merge']='enabled';
384 $def['merge_filter']='selected';
385 $def['duplicate_merge']='enabled';
388 $def['merge_filter']='enabled';
389 $def['duplicate_merge']='disabled';
401 if(empty($this->id)){
402 $this->id = $this->name;
408 * This function supports setting the values of all TemplateField instances.
409 * @param $row The Array key/value pairs from fields_meta_data table
411 function populateFromRow($row=array()) {
412 $fmd_to_dyn_map = array('comments' => 'comment', 'require_option' => 'required', 'label' => 'vname',
413 'mass_update' => 'massupdate', 'max_size' => 'len', 'default_value' => 'default', 'id_name' => 'ext3');
414 if(!is_array($row)) {
415 $GLOBALS['log']->error("Error: TemplateField->populateFromRow expecting Array");
417 //Bug 24189: Copy fields from FMD format to Field objects
418 foreach ($fmd_to_dyn_map as $fmd_key => $dyn_key) {
419 if (isset($row[$fmd_key])) {
420 $this->$dyn_key = $row[$fmd_key];
423 foreach($row as $key=>$value) {
424 $this->$key = $value;
428 function populateFromPost(){
429 foreach($this->vardef_map as $vardef=>$field){
431 if(isset($_REQUEST[$vardef])){
432 $this->$vardef = $_REQUEST[$vardef];
434 // Bug #48826. Some fields are allowed to have special characters and must be decoded from the request
435 if (is_string($this->$vardef) && in_array($vardef, $this->decode_from_request_fields_map))
436 $this->$vardef = html_entity_decode($this->$vardef);
438 // Bug 49774, 49775: Strip html tags from 'formula' and 'dependency'.
439 // Add to the list below if we need to do the same for other fields.
440 if (!empty($this->$vardef) && in_array($vardef, array('formula', 'dependency'))){
441 $this->$vardef = to_html(strip_tags(from_html($this->$vardef)));
444 //Remove potential xss code from help field
445 if($field == 'help' && !empty($this->$vardef))
447 $help = htmlspecialchars_decode($this->$vardef, ENT_QUOTES);
448 $this->$vardef = htmlentities(remove_xss($help));
452 if($vardef != $field){
453 $this->$field = $this->$vardef;
457 $this->applyVardefRules();
458 $GLOBALS['log']->debug('populate: '.print_r($this,true));
462 protected function applyVardefRules()
466 function get_additional_defs(){
470 function delete($df){
471 $df->deleteField($this);
477 * This is a helper function to return a field's proper name. It checks to see if an instance of the module can
478 * be created and then attempts to retrieve the field's name based on the name lookup skey supplied to the method.
480 * @param String $module The name of the module
481 * @param String $name The field name key
482 * @return The field name for the module
484 protected function get_field_name($module, $name)
486 $bean = loadBean($module);
487 if(empty($bean) || is_null($bean))
492 $field_defs = $bean->field_defs;
493 return isset($field_defs[$name]['name']) ? $field_defs[$name]['name'] : $name;
499 * This function says the field template by calling the DynamicField addFieldObject function. It then
500 * checks to see if updates are needed for the SearchFields.php file. In the event that the unified_search
501 * member variable is set to true, a search field definition is updated/created to the SearchFields.php file.
503 * @param $df Instance of DynamicField
506 // $GLOBALS['log']->debug('saving field: '.print_r($this,true));
507 $df->addFieldObject($this);
509 require_once('modules/ModuleBuilder/parsers/parser.searchfields.php');
510 $searchFieldParser = new ParserSearchFields( $df->getModuleName() , $df->getPackageName() ) ;
511 //If unified_search is enabled for this field, then create the SearchFields entry
512 $fieldName = $this->get_field_name($df->getModuleName(), $this->name);
513 if($this->unified_search && !isset($searchFieldParser->searchFields[$df->getModuleName()][$fieldName]))
515 $searchFieldParser->addSearchField($fieldName, array('query_type'=>'default'));
516 $searchFieldParser->saveSearchFields($searchFieldParser->searchFields);