public function setUp()
{
global $beanList, $beanFiles;
include 'include/modules.php';
//Reload langauge strings
$GLOBALS['app_strings'] = return_application_language($GLOBALS['current_language']);
$GLOBALS['app_list_strings'] = return_app_list_strings_language($GLOBALS['current_language']);
$GLOBALS['mod_strings'] = return_module_language($GLOBALS['current_language'], 'Accounts');
//Create an anonymous user for login purposes/
$this->_user = new User();
$this->_user->retrieve('1');
$GLOBALS['current_user'] = $this->_user;
self::$helperObject = new APIv3Helper();
if (file_exists(sugar_cached('modules/unified_search_modules.php'))) {
$this->unified_search_modules_content = file_get_contents(sugar_cached('modules/unified_search_modules.php'));
unlink(sugar_cached('modules/unified_search_modules.php'));
}
require_once 'modules/Home/UnifiedSearchAdvanced.php';
$unifiedSearchAdvanced = new UnifiedSearchAdvanced();
$_REQUEST['enabled_modules'] = 'Accounts,Contacts,Opportunities';
$unifiedSearchAdvanced->saveGlobalSearchSettings();
$GLOBALS['db']->query("DELETE FROM accounts WHERE name like 'UNIT TEST%' ");
$GLOBALS['db']->query("DELETE FROM opportunities WHERE name like 'UNIT TEST%' ");
$GLOBALS['db']->query("DELETE FROM contacts WHERE first_name like 'UNIT TEST%' ");
$GLOBALS['db']->query("DELETE FROM calls WHERE name like 'UNIT TEST%' ");
$GLOBALS['db']->query("DELETE FROM tasks WHERE name like 'UNIT TEST%' ");
$GLOBALS['db']->query("DELETE FROM meetings WHERE name like 'UNIT TEST%' ");
//$this->useOutputBuffering = false;
}
public function testFisrtUnifiedSearchWithoutUserPreferences()
{
//Enable the Tasks, Accounts and Contacts modules
require_once 'modules/Home/UnifiedSearchAdvanced.php';
$_REQUEST = array();
$_REQUEST['enabled_modules'] = 'Tasks,Accounts,Contacts';
$unifiedSearchAdvanced = new UnifiedSearchAdvanced();
$unifiedSearchAdvanced->saveGlobalSearchSettings();
$_REQUEST = array();
$_REQUEST['advanced'] = 'false';
$unifiedSearchAdvanced->query_stirng = 'blah';
$unifiedSearchAdvanced->search();
global $current_user;
$users_modules = $current_user->getPreference('globalSearch', 'search');
$this->assertTrue(!empty($users_modules), 'Assert we have set the user preferences properly');
$this->assertTrue(isset($users_modules['Tasks']), 'Assert that we have added the Tasks module');
$this->assertEquals(count($users_modules), 3, 'Assert that we have 3 modules in user preferences for global search');
}
/**
* @brief generation of new cache file and search for force_unifiedsearch fields in it
* @group 42961
*/
public function testBuildCache()
{
$beanList = array();
$beanFiles = array();
require 'include/modules.php';
$GLOBALS['beanList'] = $beanList;
$GLOBALS['beanFiles'] = $beanFiles;
$unifiedSearchAdvanced = new UnifiedSearchAdvanced();
$unifiedSearchAdvanced->buildCache();
$this->assertFileExists($GLOBALS['sugar_config']['cache_dir'] . 'modules/unified_search_modules.php', 'Here should be cache file with data');
include $GLOBALS['sugar_config']['cache_dir'] . 'modules/unified_search_modules.php';
$force_unifiedsearch = 0;
foreach ($unified_search_modules as $moduleName => $moduleInformation) {
foreach ($moduleInformation['fields'] as $fieldName => $fieldInformation) {
if (key_exists('force_unifiedsearch', $fieldInformation)) {
$force_unifiedsearch++;
}
}
}
$this->assertGreaterThan(0, $force_unifiedsearch, 'Here should be fields with force_unifiedsearch key');
}
public function setUp()
{
//Create an anonymous user for login purposes/
$this->_user = SugarTestUserUtilities::createAnonymousUser();
$GLOBALS['current_user'] = $this->_user;
self::$helperObject = new APIv3Helper();
if (file_exists(sugar_cached('modules/unified_search_modules.php'))) {
$this->unified_search_modules_content = file_get_contents(sugar_cached('modules/unified_search_modules.php'));
unlink(sugar_cached('modules/unified_search_modules.php'));
}
require_once 'modules/Home/UnifiedSearchAdvanced.php';
$unifiedSearchAdvanced = new UnifiedSearchAdvanced();
$_REQUEST['enabled_modules'] = 'Accounts,Contacts,Opportunities';
$unifiedSearchAdvanced->saveGlobalSearchSettings();
$GLOBALS['db']->query("DELETE FROM accounts WHERE name like 'UNIT TEST%' ");
$GLOBALS['db']->query("DELETE FROM opportunities WHERE name like 'UNIT TEST%' ");
$GLOBALS['db']->query("DELETE FROM contacts WHERE first_name like 'UNIT TEST%' ");
$GLOBALS['db']->query("DELETE FROM calls WHERE name like 'UNIT TEST%' ");
$GLOBALS['db']->query("DELETE FROM tasks WHERE name like 'UNIT TEST%' ");
$GLOBALS['db']->query("DELETE FROM meetings WHERE name like 'UNIT TEST%' ");
$GLOBALS['db']->commit();
//$this->useOutputBuffering = false;
}
public function test_create_unified_search_modules_display()
{
if (file_exists('custom/modules/unified_search_modules_display.php')) {
unlink('custom/modules/unified_search_modules_display.php');
}
require_once 'modules/UpgradeWizard/uw_utils.php';
$usa = new UnifiedSearchAdvanced();
$_REQUEST['enabled_modules'] = 'Accounts,Bug36845Test';
$usa->saveGlobalSearchSettings();
$this->assertTrue(file_exists('custom/modules/unified_search_modules_display.php'), 'Assert that unified_search_modules_display.php file was created');
}
public function testUserPreferencesSearch()
{
global $mod_strings, $modListHeader, $app_strings, $beanList, $beanFiles, $current_user;
require 'config.php';
require 'include/modules.php';
$usa = new UnifiedSearchAdvanced();
$_REQUEST['enabled_modules'] = 'Accounts,Contacts';
$usa->saveGlobalSearchSettings();
$current_user->setPreference('globalSearch', array('Accounts', 'Contacts'), 0, 'search');
$current_user->savePreferencesToDB();
$_REQUEST = array();
$_REQUEST['query_string'] = $this->_contact->first_name . ' ' . $this->_contact->last_name;
$_REQUEST['module'] = 'Home';
$usa->search();
$modules = $current_user->getPreference('globalSearch', 'search');
$this->assertEquals(count($modules), 2, 'Assert that there are two modules in the user preferences as defined from the global search');
$this->assertEquals('Accounts', $modules[0], 'Assert that the Accounts module has been added');
$this->assertEquals('Contacts', $modules[1], 'Assert that the Contacts module has been added');
}
/**
* Determine which modules should be searched against.
*
* @return array
*/
protected function getSearchModules()
{
$usa = new UnifiedSearchAdvanced();
$unified_search_modules_display = $usa->getUnifiedSearchModulesDisplay();
// load the list of unified search enabled modules
$modules = array();
//check to see if the user has customized the list of modules available to search
$users_modules = $GLOBALS['current_user']->getPreference('globalSearch', 'search');
if (!empty($users_modules)) {
// use user's previous selections
foreach ($users_modules as $key => $value) {
if (isset($unified_search_modules_display[$key]) && !empty($unified_search_modules_display[$key]['visible'])) {
$modules[$key] = $key;
}
}
} else {
foreach ($unified_search_modules_display as $key => $data) {
if (!empty($data['visible'])) {
$modules[$key] = $key;
}
}
}
// make sure the current module appears first in the list
if (isset($modules[$this->module])) {
unset($modules[$this->module]);
$modules = array_merge(array($this->module => $this->module), $modules);
}
return $modules;
}
/**
* action_saveglobalsearchsettings
*
* This method handles saving the selected modules to display in the Global Search Settings.
* It instantiates an instance of UnifiedSearchAdvanced and then calls the saveGlobalSearchSettings
* method.
*
*/
public function action_saveglobalsearchsettings()
{
global $current_user, $app_strings;
if (!is_admin($current_user)) {
sugar_die($GLOBALS['app_strings']['ERR_NOT_ADMIN']);
}
try {
require_once 'modules/Home/UnifiedSearchAdvanced.php';
$unifiedSearchAdvanced = new UnifiedSearchAdvanced();
$unifiedSearchAdvanced->saveGlobalSearchSettings();
//Save FTS Settings
$type = !empty($_REQUEST['type']) ? $_REQUEST['type'] : '';
$host = !empty($_REQUEST['host']) ? $_REQUEST['host'] : '';
$port = !empty($_REQUEST['port']) ? $_REQUEST['port'] : '';
$ftsConfig = $this->mergeFtsConfig($type, array('port' => $port, 'host' => $host));
$this->cfg = new Configurator();
$this->cfg->config['full_text_engine'] = '';
$this->cfg->saveConfig();
$ftsConnectionValid = TRUE;
if (!empty($type)) {
//Check if the connection is valid on save:
require_once 'include/SugarSearchEngine/SugarSearchEngineFactory.php';
$searchEngine = SugarSearchEngineFactory::getInstance($type, $ftsConfig);
$result = $searchEngine->getServerStatus();
if (!$result['valid']) {
$ftsConnectionValid = FALSE;
}
// bug 54274 -- only bother with an override if we have data to place there, empty string breaks Sugar On-Demand!
$ftsConfig['valid'] = $ftsConnectionValid;
$this->cfg->config['full_text_engine'] = array($type => $ftsConfig);
$this->cfg->handleOverride();
}
// Refresh the server info & module list sections of the metadata
MetaDataManager::refreshSectionCache(array(MetaDataManager::MM_SERVERINFO, MetaDataManager::MM_MODULES));
if (!$ftsConnectionValid) {
echo $GLOBALS['mod_strings']['LBL_FTS_CONNECTION_INVALID'];
} else {
echo "true";
}
} catch (Exception $ex) {
echo "false";
}
}
* 02110-1301 USA.
*
* You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
* SW2-130, Cupertino, CA 95014, USA. or at email address [email protected].
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "Powered by
* SugarCRM" logo. If the display of the logo is not reasonably feasible for
* technical reasons, the Appropriate Legal Notices must display the words
* "Powered by SugarCRM".
********************************************************************************/
/*********************************************************************************
* Description: TODO: To be written.
* Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
* All Rights Reserved.
* Contributor(s): ______________________________________..
********************************************************************************/
require_once 'modules/Home/UnifiedSearchAdvanced.php';
if (!empty($_REQUEST['usa_form'])) {
$usa = new UnifiedSearchAdvanced();
echo $usa->getDropDownDiv();
} else {
global $mod_strings, $modListHeader, $app_strings, $beanList, $beanFiles;
echo get_module_title("Search", $mod_strings['LBL_SEARCH_RESULTS'], true);
$usa = new UnifiedSearchAdvanced();
$usa->search();
}
function uninstall($base_dir)
{
if (defined('TEMPLATE_URL')) {
SugarTemplateUtilities::disableCache();
}
global $app_strings;
$total_steps = 5;
//min steps with no tasks
$current_step = 0;
$this->base_dir = $base_dir;
$tasks = array('pre_uninstall', 'uninstall_relationships', 'uninstall_copy', 'uninstall_dcactions', 'uninstall_dashlets', 'uninstall_connectors', 'uninstall_layoutfields', 'uninstall_extensions', 'uninstall_global_search', 'disable_manifest_logichooks', 'post_uninstall');
$total_steps += count($tasks);
//now the real number of steps
if (file_exists($this->base_dir . '/manifest.php')) {
if (!$this->silent) {
$current_step++;
display_progress_bar('install', $current_step, $total_steps);
echo '<div id ="displayLoglink" ><a href="#" onclick="toggleDisplay(\'displayLog\')">' . $app_strings['LBL_DISPLAY_LOG'] . '</a> </div><div id="displayLog" style="display:none">';
}
global $moduleList;
include $this->base_dir . '/manifest.php';
$this->installdefs = $installdefs;
$this->id_name = $this->installdefs['id'];
$installed_modules = array();
if (isset($this->installdefs['beans'])) {
foreach ($this->installdefs['beans'] as $bean) {
$installed_modules[] = $bean['module'];
$this->uninstall_user_prefs($bean['module']);
}
$this->modulesInPackage = $installed_modules;
$this->uninstall_beans($installed_modules);
$this->uninstall_customizations($installed_modules);
if (!$this->silent) {
$current_step++;
update_progress_bar('install', $total_steps, $total_steps);
}
}
if (!$this->silent) {
$current_step++;
update_progress_bar('install', $current_step, $total_steps);
}
foreach ($tasks as $task) {
$this->{$task}();
if (!$this->silent) {
$current_step++;
update_progress_bar('install', $current_step, $total_steps);
}
}
if (isset($installdefs['custom_fields']) && (isset($GLOBALS['mi_remove_tables']) && $GLOBALS['mi_remove_tables'])) {
$this->log(translate('LBL_MI_UN_CUSTOMFIELD'));
$this->uninstall_custom_fields($installdefs['custom_fields']);
}
if (!$this->silent) {
$current_step++;
update_progress_bar('install', $current_step, $total_steps);
echo '</div>';
}
//since we are passing $silent = true to rebuildAll() in that method it will set $this->silent = true, so
//we need to save the setting to set it back after rebuildAll() completes.
$silentBak = $this->silent;
$this->rebuild_all(true);
$this->silent = $silentBak;
//#27877, If the request from MB redeploy a custom module , we will not remove the ACL actions for this package.
if (!isset($_REQUEST['action']) || $_REQUEST['action'] != 'DeployPackage') {
$this->remove_acl_actions();
}
//end
if (!$this->silent) {
$current_step++;
update_progress_bar('install', $current_step, $total_steps);
echo '</div>';
}
UpdateSystemTabs('Restore', $installed_modules);
//clear the unified_search_module.php file
require_once 'modules/Home/UnifiedSearchAdvanced.php';
UnifiedSearchAdvanced::unlinkUnifiedSearchModulesFile();
$this->log('<br><b>' . translate('LBL_MI_COMPLETE') . '</b>');
if (!$this->silent) {
update_progress_bar('install', $total_steps, $total_steps);
}
} else {
die("No manifest.php Defined In {$this->base_dir}/manifest.php");
}
}
/**
* action_saveglobalsearchsettings
*
* This method handles saving the selected modules to display in the Global Search Settings.
* It instantiates an instance of UnifiedSearchAdvanced and then calls the saveGlobalSearchSettings
* method.
*
*/
public function action_saveglobalsearchsettings()
{
global $current_user, $app_strings;
if (!is_admin($current_user)) {
sugar_die($GLOBALS['app_strings']['ERR_NOT_ADMIN']);
}
try {
require_once 'modules/Home/UnifiedSearchAdvanced.php';
$unifiedSearchAdvanced = new UnifiedSearchAdvanced();
$unifiedSearchAdvanced->saveGlobalSearchSettings();
$return = 'true';
echo $return;
} catch (Exception $ex) {
echo "false";
}
}
/**
* This function is used to hand off the global search to the built-in SugarSearchEngine (aka SugarSpot)
* @param $api ServiceBase The API class of the request
* @param $args array The arguments array passed in from the API
* @param $searchEngine SugarSearchEngine The SugarSpot search engine created using the Factory in the caller
* @param $options array An array of options to pass through to the search engine, they get translated to the $searchOptions array so you can see exactly what gets passed through
* @return array Two elements, 'records' the list of returned records formatted through FormatBean, and 'next_offset' which will indicate to the user if there are additional records to be returned.
*/
public function globalSearchSpot(ServiceBase $api, array $args, $searchEngine, array $options)
{
require_once 'modules/Home/UnifiedSearchAdvanced.php';
$searchOptions = array('modules' => $options['moduleList'], 'current_module' => $options['primaryModule'], 'return_beans' => true, 'my_items' => $options['my_items'], 'favorites' => $options['favorites'], 'orderBy' => $options['orderBy'], 'fields' => $options['fieldFilters'], 'selectFields' => $options['selectFields'], 'limitPerModule' => $options['limitPerModule'], 'allowEmptySearch' => true, 'distinct' => 'DISTINCT', 'return_beans' => true);
if (isset($options['deleted'])) {
$searchOptions['deleted'] = $options['deleted'];
}
$multiModule = false;
if (empty($options['moduleList']) || count($options['moduleList']) == 0 || count($options['moduleList']) > 1) {
$multiModule = true;
}
if (empty($options['moduleList'])) {
require_once 'modules/ACL/ACLController.php';
$usa = new UnifiedSearchAdvanced();
$moduleList = $usa->getUnifiedSearchModules();
// get the module names [array keys]
$moduleList = array_keys($moduleList);
// filter based on User Access if Blank
$ACL = new ACLController();
// moduleList is passed by reference
$ACL->filterModuleList($moduleList);
$searchOptions['modules'] = $options['moduleList'] = $moduleList;
}
if (!empty($options['searchFields'])) {
$customWhere = array();
foreach ($options['moduleList'] as $module) {
$seed = BeanFactory::getBean($module);
$fields = array_keys($seed->field_defs);
$existingfields = array_intersect($fields, $options['searchFields']);
if (!empty($existingfields)) {
$customTable = $seed->get_custom_table_name();
$table = $seed->table_name;
foreach ($existingfields as $field) {
if (!isset($seed->field_defs[$field]['unified_search']) || $seed->field_defs[$field]['unified_search'] !== true) {
continue;
}
$prefix = $table;
if (isset($GLOBALS['dictionary'][$seed->object_name]['custom_fields'][$field])) {
$prefix = $customTable;
}
if (!isset($seed->field_defs[$field]['source']) || $seed->field_defs[$field]['source'] != 'non-db') {
$customWhere[$module][] = "{$prefix}.{$field} LIKE '{$options['query']}%'";
}
}
if (isset($customWhere[$module])) {
$searchOptions['custom_where_module'][$module] = '(' . implode(' OR ', $customWhere[$module]) . ')';
}
}
}
}
$offset = $options['offset'];
// One for luck.
// Well, actually it's so that we know that there are additional results
$limit = $options['limit'] + 1;
if ($multiModule && $options['offset'] != 0) {
// With more than one module, there is no way to do offsets for real, so we have to fake it.
$limit = $limit + $offset;
$offset = 0;
}
if (!$multiModule) {
// It's not multi-module, the per-module limit should be the same as the master limit
$searchOptions['limitPerModule'] = $limit;
}
if (isset($options['custom_select'])) {
$searchOptions['custom_select'] = $options['custom_select'];
}
if (isset($options['custom_from'])) {
$searchOptions['custom_from'] = $options['custom_from'];
}
if (isset($options['custom_where'])) {
$searchOptions['custom_where'] = $options['custom_where'];
}
$results = $searchEngine->search($options['query'], $offset, $limit, $searchOptions);
$returnedRecords = array();
$api->action = 'list';
foreach ($results as $module => $moduleResults) {
if (!is_array($moduleResults['data'])) {
continue;
}
$moduleArgs = $args;
// Need to override the filter arg so that it looks like something formatBean expects
if (!empty($options['fieldFilters'][$module])) {
$moduleFields = $options['fieldFilters'][$module];
} else {
if (!empty($options['fieldFilters']['_default'])) {
$moduleFields = $options['fieldFilters']['_default'];
} else {
$moduleFields = array();
}
}
$moduleArgs['fields'] = implode(',', $moduleFields);
foreach ($moduleResults['data'] as $record) {
//.........这里部分代码省略.........
/**
* Remove the cached unified_search_modules.php file
*/
public function clearSearchCache()
{
global $mod_strings, $sugar_config;
if ($this->show_output) {
echo "<h3>{$mod_strings['LBL_QR_CLEARSEARCH']}</h3>";
}
// clear sugar_cache backend for SugarSearchEngine
SugarSearchEngineMetadataHelper::clearCache();
// Clear the cache file AFTER the cache clear, as it will be rebuilt by
// clearCache otherwise
UnifiedSearchAdvanced::unlinkUnifiedSearchModulesFile();
}
请发表评论