Skip to content

Instantly share code, notes, and snippets.

@esimonetti
Last active August 21, 2017 06:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save esimonetti/39f48eea1fc7b022caae57e90d49122e to your computer and use it in GitHub Desktop.
Save esimonetti/39f48eea1fc7b022caae57e90d49122e to your computer and use it in GitHub Desktop.
Sugar 7.9.1.0 POC to remove sorting for all listview, subpanel and popup selection list fields that do not have a database index
<?php
// Enrico Simonetti
// enricosimonetti.com
//
// 2017-08-18 on Sugar 7.9.1.0
// filename: custom/include/indexFinder.php
//
// Tool that helps retrieve and cache indexes on a per-module basis
class indexFinder
{
public static function getIndexes($module = '')
{
if(!empty($module)) {
if(SugarCache::instance()->useBackend()) {
$cached_indexes = SugarCache::instance()->get(self::getCacheKey($module));
if(!empty($cached_indexes)) {
// we have cached values
return json_decode($cached_indexes, true);
} else {
// fresh values
$indexes = self::getIndexesFromDB($module);
SugarCache::instance()->set(self::getCacheKey($module), json_encode($indexes));
return $indexes;
}
} else {
return self::getIndexesFromVardefs($module);
}
} else {
return array();
}
}
protected static function getCacheKey($module = '')
{
return !empty($module) ? strtolower($module).'_modules_indexes' : '';
}
protected static function getIndexesFromDB($module)
{
$bean = BeanFactory::getBean($module);
if(!empty($bean) && !empty($bean->table_name)) {
$indexes = $GLOBALS['db']->get_indices($bean->table_name);
return self::extractIndexedFields($indexes);
}
return array();
}
protected static function getIndexesFromVardefs($module)
{
$bean = BeanFactory::getBean($module);
if(!empty($bean) && !empty($bean->object_name)) {
$indexes = $GLOBALS['dictionary'][$bean->object_name]['indices'];
return self::extractIndexedFields($indexes);
}
return array();
}
protected static function extractIndexedFields($indexes)
{
$indexed_fields = array();
if(!empty($indexes)) {
foreach($indexes as $index) {
if(!empty($index['fields'])) {
foreach($index['fields'] as $field) {
if(empty($indexed_fields[$field])) {
$indexed_fields[$field] = $field;
}
}
}
}
}
return $indexed_fields;
}
}
<?php
// Enrico Simonetti
// enricosimonetti.com
//
// 2017-08-18 on Sugar 7.9.1.0
// filename: custom/Extension/modules/Accounts/Ext/clients/base/views/list/removesort-list.php
//
// Listview sorting removal of all non-indexed fields
$module = 'Accounts';
$indexed_fields = indexFinder::getIndexes($module);
foreach($viewdefs[$module]['base']['view']['list']['panels']['0']['fields'] as $key => $field) {
if(!in_array($field['name'], $indexed_fields)) {
$viewdefs[$module]['base']['view']['list']['panels']['0']['fields'][$key]['sortable'] = 0;
} else {
$viewdefs[$module]['base']['view']['list']['panels']['0']['fields'][$key]['sortable'] = 1;
}
}
<?php
// Enrico Simonetti
// enricosimonetti.com
//
// 2017-08-18 on Sugar 7.9.1.0
// filename: custom/Extension/modules/Accounts/Ext/clients/base/views/selection-list/removesort-selection-list.php
//
// Selection list sorting removal of all non-indexed fields
$module = 'Accounts';
$indexed_fields = indexFinder::getIndexes($module);
foreach($viewdefs[$module]['base']['view']['selection-list']['panels']['0']['fields'] as $key => $field) {
if(!in_array($field['name'], $indexed_fields)) {
$viewdefs[$module]['base']['view']['selection-list']['panels']['0']['fields'][$key]['sortable'] = 0;
} else {
$viewdefs[$module]['base']['view']['selection-list']['panels']['0']['fields'][$key]['sortable'] = 1;
}
}
<?php
// Enrico Simonetti
// enricosimonetti.com
//
// 2017-08-18 on Sugar 7.9.1.0
// filename: custom/Extension/modules/Accounts/Ext/clients/base/views/subpanel-list/removesort-subpanel-list.php
//
// Subpanel list sorting removal of all non-indexed fields
$module = 'Accounts';
$indexed_fields = indexFinder::getIndexes($module);
foreach($viewdefs[$module]['base']['view']['subpanel-list']['panels']['0']['fields'] as $key => $field) {
if(!in_array($field['name'], $indexed_fields)) {
$viewdefs[$module]['base']['view']['subpanel-list']['panels']['0']['fields'][$key]['sortable'] = 0;
} else {
$viewdefs[$module]['base']['view']['subpanel-list']['panels']['0']['fields'][$key]['sortable'] = 1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment