Ejemplo n.º 1
0
 /**
  * This function is used to hand off the global search to the FTS Search Emgine
  * @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.
  */
 protected function globalSearchFullText(ServiceBase $api, array $args, SugarSearchEngineAbstractBase $searchEngine, array $options)
 {
     $options['append_wildcard'] = 1;
     if (empty($options['moduleList'])) {
         require_once 'modules/ACL/ACLController.php';
         $moduleList = SugarSearchEngineMetadataHelper::getSystemEnabledFTSModules();
         // filter based on User Access if Blank
         $ACL = new ACLController();
         // moduleList is passed by reference
         $ACL->filterModuleList($moduleList);
         $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)) {
                 foreach ($existingfields as $field) {
                     if (empty($seed->field_defs[$field]['full_text_search'])) {
                         continue;
                     }
                     $prefix = $seed->module_name;
                     if (!isset($seed->field_defs[$field]['source']) || $seed->field_defs[$field]['source'] != 'non-db') {
                         $customWhere[] = "{$prefix}.{$field}";
                     }
                 }
             }
         }
         $options['searchFields'] = $customWhere;
     }
     $options['moduleFilter'] = $options['moduleList'];
     $results = $searchEngine->search($options['query'], $options['offset'], $options['limit'], $options);
     $returnedRecords = array();
     $total = 0;
     $api->action = 'list';
     if (is_object($results)) {
         foreach ($results as $result) {
             $record = BeanFactory::retrieveBean($result->getModule(), $result->getId());
             // if we can't get the bean skip it
             if (!$record) {
                 continue;
             }
             $module = $record->module_dir;
             // 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();
                 }
             }
             if (!empty($moduleFields) && !in_array('id', $moduleFields)) {
                 $moduleFields[] = 'id';
             }
             $moduleArgs['fields'] = implode(',', $moduleFields);
             $formattedRecord = $this->formatBean($api, $moduleArgs, $record);
             $formattedRecord['_module'] = $module;
             // The SQL based search engine doesn't know how to score records, so set it to 1
             $formattedRecord['_search']['score'] = $result->getScore();
             //Add highlighted text
             $formattedRecord['_search']['highlighted'] = $result->getHighlightedHitText();
             $returnedRecords[] = $formattedRecord;
         }
         $total = $results->getTotalHits();
     }
     if ($total > $options['limit'] + $options['offset']) {
         $nextOffset = $options['offset'] + $options['limit'];
     } else {
         $nextOffset = -1;
     }
     return array('next_offset' => $nextOffset, 'records' => $returnedRecords);
 }
Ejemplo n.º 2
0
 /**
  * Get the enabled and disabled modules for the datatable
  *
  * @param $moduleFilter array Requested modules for search
  * @param $disabledModules array Requested modules for disable in search
  * @return array
  */
 protected function getFilterModules($moduleFilter, $disabledModules)
 {
     $filteredEnabled = SugarSearchEngineMetadataHelper::getUserEnabledFTSModules();
     $userDisabled = $GLOBALS['current_user']->getPreference('fts_disabled_modules');
     $userDisabled = explode(",", $userDisabled);
     // Filter by System enabled FTS modules
     $systemEnabledModules = SugarSearchEngineMetadataHelper::getSystemEnabledFTSModules();
     $userDisabled = array_intersect_key($systemEnabledModules, array_flip($userDisabled));
     $filteredEnabled = array_intersect_key($systemEnabledModules, array_flip($filteredEnabled));
     $userDisabled = $this->translateModulesList($userDisabled);
     $filteredEnabled = $this->translateModulesList($filteredEnabled);
     sort($filteredEnabled);
     if (!empty($moduleFilter)) {
         foreach ($filteredEnabled as $key => $info) {
             if (!in_array($info['module'], $moduleFilter) && in_array($info['module'], $disabledModules)) {
                 unset($filteredEnabled[$key]);
                 // its not enabled, its disabled
                 $userDisabled = $info;
             }
         }
     }
     return array('enabled' => $filteredEnabled, 'disabled' => $userDisabled);
 }