Predefined searches in SugarCRM / SuiteCRM

In ​SugarCRM or SuiteCRM​ each user can create, on each module, some saved search filters to be reused many times in the ListView, moreover the system “remember” the last search done in a particolar module.

Very often, however, customers request some predefined search filters to be defined for all users, or users of a given role, to find them ready in the modules they work on most frequently, or that some module “always starts” with a specific pre-set search.

The key is to use the “after_login” event of the Users module, creating or extending the custom/modules/Users/logic_hooks.php file:

<?php
$hook_version = 1; 
$hook_array = Array(); 
$hook_array['after_login'] = Array(); 
$hook_array['after_login'][] = Array(1, 'Pre-saved searches',
    'custom/modules/Users/DefaultSearches.php', 'DefaultSearches', 'addSavedSearch');

If the file already exists we should only add the logic hook that interests us, without eliminating those already present.

At this point we can create the custom/modules/Users/DefaultSearches.php file which will contain all the logic to create our default saved search filters and preset searches for all users.

In this file I have inserted two generic methods that are createdSavedSearch($strModule, $strName, $arySearchQuery), which is responsible for creating a saved search named $strName, on the module $strModule with the search query $arySearchQuery (here we have to specify how we will compile the search filter form, in which all the fields we want to filter on should be present), and createStoreQuery($strModule, $arySearchQuery), which instead creates the $arySearchQuery search filter preset for the $strModule module.

​In the addSavedSearch(&$bean, $event, $arguments) method, you can see some examples of using these methods.

<?php
// Manage pre-saved searches (SavedSearch and StoreQuery) for the current user

require_once('modules/SavedSearch/SavedSearch.php');
require_once('modules/MySettings/StoreQuery.php');

class DefaultSearches {

    /**
     * Creation of SavedSearch and StoreQuery for the current user
     * 
     * @param object $bean
     * @param object $event
     * @param array $arguments
     */
    function addSavedSearch(&$bean, $event, $arguments) {
        global $current_user;

        // Create and save the query filter for the Prospect customers (Accounts) assigned to the current user
        $this->createSavedSearch('Accounts', 'Prospects', array('searchFormTab' => 'advanced_search',
                'query' => true, 'account_type_advanced' => 'Prospect', 'assigned_user_id_advanced' =>
                $current_user->id, 'search_module' => 'Accounts', 'saved_search_action' => 'save',
                'advanced' => true, 'orderBy' => 'NAME', 'sortOrder' => 'ASC'));

        // Create and save the query filter for the not started tasks (Tasks) assigned to the current user
        $this->createSavedSearch('Tasks', 'Not Started Tasks', array('searchFormTab' => 'advanced_search',
                'query' => true, 'status' => 'Not Started', 'assigned_user_id_advanced' => $current_user->id,
                'search_module' => 'Tasks', 'saved_search_action' => 'save', 'advanced' => true));

        // Create and save the preset query for the filter on the meetings (Meetings)
        $this->createStoreQuery('Meetings', array('searchFormTab' => 'advanced_search', 'query' => true,
                'status_advanced' => 'Planned', 'assigned_user_id_advanced' => $current_user->id,
                'module' => 'Meetings', 'action' => 'index', 'orderBy' => 'DATE_START', 'sortOrder' => 'ASC'));
    }

    /**
     * Creation of SavedSearch for the current user
     * 
     * @param string $strModule The module on which to create the SavedSearch
     * @param string $strName The name of the SavedSearch
     * @param array $arySearchQuery The search to be saved
     */
    private function createSavedSearch($strModule, $strName, $arySearchQuery) {
        global $current_user, $db;

        $beanSavedSearch = new SavedSearch('');
        $beanSavedSearch->name = $strName;
        $beanSavedSearch->search_module = $strModule;
        $beanSavedSearch->contents = base64_encode(serialize($arySearchQuery));
        $beanSavedSearch->assigned_user_id = $current_user->id;
        $strQuery = 'SELECT id FROM saved_search WHERE deleted = "0" AND assigned_user_id = "'
            . $current_user->id . '"' . ' AND search_module =  "' . $strModule . '" AND name = "' . $strName . '"';
        $rst = $db->query($strQuery);
        if ($row = $db->fetchByAssoc($rst)) {
            $beanSavedSearch->id = $row['id'];
            $strId = $beanSavedSearch->save();
            $GLOBALS['log']->debug('SavedSearch ' . $strName . ' for module ' . $strModule . ' updated for user '
                . $current_user->name . ' [' . $current_user->id . ']: ' . $row['id']);
        }
        else {
            $beanSavedSearch->new_schema = true;
            $strId = $beanSavedSearch->save();
            $GLOBALS['log']->debug('SavedSearch ' . $strName . ' for module ' . $strModule . ' created for user '
                . $current_user->name . ' [' . $current_user->id . ']: ' . $strId);
        }
    }

    /**
     * Creation of StoreQueryfor the current user
     * 
     * @param string $strModule The module on which to create the StoreQuery
     * @param array $arySearchQuery The search to be saved
     */
    private function createStoreQuery($strModule, $arySearchQuery) {
        global $current_user, $db;

        $beanStoreQuery = new StoreQuery();
        $beanStoreQuery->query = $arySearchQuery;
        $beanStoreQuery->SaveQuery($strModule);
    }

}
?>

Hope you find this blog post helpful.

Feel free to add comments and queries, that helps us to improve the quality of posts.

You can contact us at info@infotechbuddies.com

Thank you.

Spread the love

Leave a Reply

Your email address will not be published. Required fields are marked *