Override the Smarty template files that render the range search widgets to customize search fields for range searches or to add custom javascript validation to filter acceptable numbers or dates.
Copy the contents listed in Column A to contents listed in Column B as shown in table below:
Type of
The following is an example of changing the single input field in a numeric range search to be a dropdown list of five possible values.
1. Create the file custom/include/SugarFields/Fields/Int/RangeSearchForm.tpl
154
2. Change the div contents of the section with id {$id}_range_div to:
<div id="{$id}_range_div" style="{if
$starting_choice=='between'}display:none;{else}display:'';{/if}">
{if empty($smarty.request.{{$id_range}}) &&
!empty($smarty.request.{{$original_id}})}
{assign var="dropdown_value" value=$smarty.request.{{$original_id}}}
{else}
{assign var="dropdown_value" value=$smarty.request.{{$id_range}}}
{/if}
<select name='range_{$id}' id='range_{$id}' tabindex='{{$tabindex}}' size='1'>
<option value="-10" {if $dropdown_value==-10}SELECTED{/if}>-10</option>
<option value="-5" {if $dropdown_value==-5}SELECTED{/if}>-5</option>
<option value="-0" {if $dropdown_value==0}SELECTED{/if}>0</option>
<option value="5" {if $dropdown_value==5}SELECTED{/if}>5</option>
<option value="10" {if $dropdown_value==10}SELECTED{/if}>10</option>
</select>
</div>
Figure: Numeric range search with custom dropdown for input values
155
As of version 6.2, Sugar does not enable upgrade safe customization of adding additional operators to drop-down lists (for example, This Quarter, Last Quarter, Next Quarter).
Follow the steps listed below to add additional operators:
1. Customize the drop-down array lists in the global language file(s):
date_range_search_dom for dates and numeric_range_search_dom for numbers.
For example, to add a First Quarter operator, add the following to the file custom/include/language/en_us.lang.php:
$app_list_strings['date_range_search_dom'] = array(
'=' => 'Equals',
'not_equal' => 'Not On', 'greater_than' => 'After', 'less_than' => 'Before',
'last_7_days' => 'Last 7 Days', 'next_7_days' => 'Next 7 Days', 'last_30_days' => 'Last 30 Days', 'next_30_days' => 'Next 30 Days', 'last_month' => 'Last Month', 'this_month' => 'This Month', 'next_month' => 'Next Month',
'first_quarter' => 'First Quarter', //Added "First Quarter" operator 'last_year' => 'Last Year',
'this_year' => 'This Year', 'next_year' => 'Next Year', 'between' => 'Is Between', );
The drop-down list of operators for date range searches should now have a First Quarter choice.
Selecting First Quarter creates a macro field. In this example, actual value submitted to the SearchForm code is [first_quarter]. This is because the key is defined as first_quarter.
2. Alter the include/MVC/View/views/view.list.php.
This step is not upgrade safe, but necessary since there are a lot of files that reference this file.
The code to change is in the prepareSearchForm function.
Load your custom SearchForm2.php implementation (say custom/include/SearchForm/SearchForm2.php) instead of include/SearchForm/SearchForm2.php,
function prepareSearchForm() { ...
$this->use_old_search = false;
156
require_once('custom/include/SearchForm/SearchForm2.php');
...}
3. Write the code to handle your custom operator in the generateSearchWhere method of SearchForm2.php.
a. Locate the area in the code that falls between the switch statements to handle next_30_days and this_year.
b. Insert the custom operator for first_quarter handling here.
In this example, assume first_quarter to be the dates between January 1 and March 31, inclusive.
Also, for brevity, this example covers mysql queries only.
case 'this_quarter':
if ($GLOBALS['db']->dbType == 'mysql') { //Get the current year
global $timedate;
$current_year = $timedate->getNow(true)->year;
$where .= "{$db_field} >= '{$current_year}-01-01' AND {$db_field} <=
'{$current_year}-03-31'";
} break;
Tips
This section provides some tips you will find useful in using the new MVC and metadata framework and to avoid pitfalls that you may run into from the subtle details of the metadata framework that sometimes cause frustration and confusion. Here are some common scenarios that you may have to address.
Grouping Required Fields Together
To group all required fields together all you need to do is set the config to:
$GLOBALS['sugar_config']['forms']['requireFirst'] = true;
Displaying data on EditViews with a read-only ACL setting
$GLOBALS['sugar_config']['showDetailData'] = true;
157
The field value specified in metadata does not appear
This usually happens when the user is attempting to specify a variable name in the module's class definition, but not in the vardefs.php file. For example, consider the Products module. In the Products.php class file there is the variable:
var $quote_name;
If you attempt to retrieve the value in your metadata file as follows:
...
array (
'quote_name', 'date_purchased', )
...
You must also make sure that 'quote_name' is specified in the vardefs.php file:
'quote_name' =>
array (
'name' => 'quote_name', 'type' => 'varchar',
'vname' => 'LBL_QUOTE_NAME', 'source'=>'non-db',
'comment' => 'Quote Name' )
This is because the metadata framework populates the Smarty variable $fields using the variables listed in the vardefs.php file. If they are not listed, there is no way for the metadata framework to know for sure which class variables have been set and are to be used. The metadata framework works in conjunction with ACL (Access Control Level) checks in the system and these are tied to fields defined in the vardefs.php file.
The field value specified in metadata does not appear but is in vardefs.php This may occur if the variable in the module has not been initialized. The metadata framework invokes the fill_in_additional_detail_fields() method so be sure the values are either set in the constructor or in the fill_in_additional_detail_fields() method.
A good strategy to debug the Smarty templates that are generated via the metadata files is to check the cache/modules/<module> directory for the .tpl files (DetailView.tpl, EditView.tpl, etc.). Enable the Developer Mode setting under the Advanced panel found through the Admin
->System Settings link to allow for the Smarty templates to be regenerated on every request.