Changing the ListView Default Sort Order
Overview
This article addresses the need to customize the advanced search layout options for modules in backward compatibility mode to change the default sort order from ascending to descending.
Customization Information
This customization is only for modules in backward compatibility mode and involves creating custom files that extend stock files. You should note that this customization does not address all scenarios within the view that may assign a sort order.
Extending the Search Form
First, we will need to extend the SearchForm class. To do this, we will create a CustomSearchForm
class that extends the original SearchForm class located in ./include/SearchForm/SearchForm2.php
. We will then override the _displayTabs method to check the $_REQUEST['sortOrder']
and default it to descending if it isn't set.
./custom/include/SearchForm/SearchForm2.php
<?php
require_once 'include/SearchForm/SearchForm2.php';
class CustomSearchForm extends SearchForm
{
/**
* displays the tabs (top of the search form)
*
* @param string $currentKey key in $this->tabs to show as the current tab
*
* @return string html
*/
function _displayTabs($currentKey)
{
//check and set the default sort order
if (!isset($_REQUEST['sortOrder']))
{
$_REQUEST['sortOrder'] = 'DESC';
}
return parent::_displayTabs($currentKey);;
}
}
?>
Extending the List View
Next, we will need to extend the ListView. We will create a ViewCustomList class that extends the original ListView located in ./include/MVC/View/views/view.list.php
. In the ViewCustomList
class, we will override the prepareSearchForm
and getSearchForm2
methods to call the CustomSearchForm class.
./custom/include/MVC/View/views/view.customlist.php
<?php
require_once 'include/MVC/View/views/view.list.php';
class ViewCustomList extends ViewList
{
function prepareSearchForm()
{
$this->searchForm = null;
//search
$view = 'basic_search';
if(!empty($_REQUEST['search_form_view']) && $_REQUEST['search_form_view'] == 'advanced_search')
$view = $_REQUEST['search_form_view'];
$this->headers = true;
if(!empty($_REQUEST['search_form_only']) && $_REQUEST['search_form_only'])
$this->headers = false;
elseif(!isset($_REQUEST['search_form']) || $_REQUEST['search_form'] != 'false')
{
if(isset($_REQUEST['searchFormTab']) && $_REQUEST['searchFormTab'] == 'advanced_search')
{
$view = 'advanced_search';
}
else
{
$view = 'basic_search';
}
}
$this->view = $view;
$this->use_old_search = true;
if (SugarAutoLoader::existingCustom('modules/' . $this->module . '/SearchForm.html') &&
!SugarAutoLoader::existingCustom('modules/' . $this->module . '/metadata/searchdefs.php')) {
require_once('include/SearchForm/SearchForm.php');
$this->searchForm = new SearchForm($this->module, $this->seed);
} else {
$this->use_old_search = false;
//Updated to require the extended CustomSearchForm class
require_once('custom/include/SearchForm/SearchForm2.php');
$searchMetaData = SearchForm::retrieveSearchDefs($this->module);
$this->searchForm = $this->getSearchForm2($this->seed, $this->module, $this->action);
$this->searchForm->setup($searchMetaData['searchdefs'], $searchMetaData['searchFields'], 'SearchFormGeneric.tpl', $view, $this->listViewDefs);
$this->searchForm->lv = $this->lv;
}
}
/**
* Returns the search form object
*
* @return SearchForm
*/
protected function getSearchForm2($seed, $module, $action = "index")
{
//Updated to use the extended CustomSearchForm class
return new CustomSearchForm($seed, $module, $action);
}
}
?>
Extending the Sugar Controller
Finally, we will create a CustomSugarController
class that extends the orginal SugarController
located in ./include/MVC/Controller/SugarController.php
. We will then need to override the do_action and post_action methods to execute their parent methods as well as the action_listview method to assign the custom view to the view attribute.
./custom/include/MVC/Controller/SugarController.php
<?php
/**
* Custom SugarCRM controller
* @api
*/
class CustomSugarController extends SugarController
{
/**
* Perform the specified action.
* This can be overridde in a sub-class
*/
private function do_action()
{
return parent::do_action();
}
/**
* Perform an action after to the specified action has occurred.
* This can be overridde in a sub-class
*/
private function post_action()
{
return parent::post_action();
}
/**
* Perform the listview action
*/
protected function action_listview()
{
parent::action_listview();
//set the new custom view
$this->view = 'customlist';
}
}
?>