Ejemplo n.º 1
0
 /**
  * Fabrik Search method
  *
  * The sql must return the following fields that are
  * used in a common display routine: href, title, section, created, text,
  * browsernav
  *
  * @param   string     $text      Target search string
  * @param   JRegistry  $params    Search plugin params
  * @param   string     $phrase    Matching option, exact|any|all
  * @param   string     $ordering  Option, newest|oldest|popular|alpha|category
  *
  * @return  array
  */
 public static function onDoContentSearch($text, $params, $phrase = '', $ordering = '')
 {
     $app = JFactory::getApplication();
     $fbConfig = JComponentHelper::getParams('com_fabrik');
     $package = $app->getUserState('com_fabrik.package', 'fabrik');
     if (defined('COM_FABRIK_SEARCH_RUN')) {
         return;
     }
     $input = $app->input;
     define('COM_FABRIK_SEARCH_RUN', true);
     JModelLegacy::addIncludePath(COM_FABRIK_FRONTEND . '/models', 'FabrikFEModel');
     $user = JFactory::getUser();
     $db = FabrikWorker::getDbo(true);
     require_once JPATH_SITE . '/components/com_content/helpers/route.php';
     // Load plugin params info
     $limit = $params->def('search_limit', 50);
     $text = trim($text);
     if ($text == '') {
         return array();
     }
     switch ($ordering) {
         case 'oldest':
             $order = 'a.created ASC';
             break;
         case 'popular':
             $order = 'a.hits DESC';
             break;
         case 'alpha':
             $order = 'a.title ASC';
             break;
         case 'category':
             $order = 'b.title ASC, a.title ASC';
             $morder = 'a.title ASC';
             break;
         case 'newest':
         default:
             $order = 'a.created DESC';
             break;
     }
     // Set heading prefix
     $headingPrefix = $params->get('include_list_title', true);
     // Get all tables with search on
     $query = $db->getQuery(true);
     $query->select('id')->from('#__{package}_lists')->where('published = 1');
     $db->setQuery($query);
     $list = array();
     $ids = $db->loadColumn();
     $section = $params->get('search_section_heading');
     $urls = array();
     // $$$ rob remove previous search results?
     $input->set('resetfilters', 1);
     // Ensure search doesn't go over memory limits
     $memory = ini_get('memory_limit');
     $memory = (int) FabrikString::rtrimword($memory, 'M') * 1000000;
     $usage = array();
     $memSafety = 0;
     $listModel = JModelLegacy::getInstance('list', 'FabrikFEModel');
     $app = JFactory::getApplication();
     foreach ($ids as $id) {
         // Re-ini the list model (was using reset() but that was flaky)
         $listModel = JModelLegacy::getInstance('list', 'FabrikFEModel');
         // $$$ geros - http://fabrikar.com/forums/showthread.php?t=21134&page=2
         $key = 'com_' . $package . '.list' . $id . '.filter.searchall';
         $app->setUserState($key, null);
         $used = memory_get_usage();
         $usage[] = memory_get_usage();
         if (count($usage) > 2) {
             $diff = $usage[count($usage) - 1] - $usage[count($usage) - 2];
             if ($diff + $usage[count($usage) - 1] > $memory - $memSafety) {
                 $app->enqueueMessage('Some records were not searched due to memory limitations');
                 break;
             }
         }
         // $$$rob set this to current table
         // Otherwise the fabrik_list_filter_all var is not used
         $input->set('listid', $id);
         $listModel->setId($id);
         $searchFields = $listModel->getSearchAllFields();
         if (empty($searchFields)) {
             continue;
         }
         $filterModel = $listModel->getFilterModel();
         $requestKey = $filterModel->getSearchAllRequestKey();
         // Set the request variable that fabrik uses to search all records
         $input->set($requestKey, $text, 'post');
         $table = $listModel->getTable();
         $fabrikDb = $listModel->getDb();
         $params = $listModel->getParams();
         // Test for swap too boolean mode
         $mode = $input->get('searchphrase', '') === 'all' ? 0 : 1;
         // $params->set('search-mode-advanced', true);
         $params->set('search-mode-advanced', $mode);
         // The table shouldn't be included in the search results or we have reached the max number of records to show.
         if (!$params->get('search_use') || $limit <= 0) {
             continue;
         }
         // Set the table search mode to OR - this will search ALL fields with the search term
         $params->set('search-mode', 'OR');
         $allrows = $listModel->getData();
         $elementModel = $listModel->getFormModel()->getElement($params->get('search_description', $table->label), true);
         $descname = is_object($elementModel) ? $elementModel->getFullName() : '';
         $elementModel = $listModel->getFormModel()->getElement($params->get('search_title', 0), true);
         $title = is_object($elementModel) ? $elementModel->getFullName() : '';
         /**
          * $$$ hugh - added date element ... always use raw, as anything that isn't in
          * standard MySQL format will cause a fatal error in J!'s search code when it does the JDate create
          */
         $elementModel = $listModel->getFormModel()->getElement($params->get('search_date', 0), true);
         $date_element = is_object($elementModel) ? $elementModel->getFullName() : '';
         if (!empty($date_element)) {
             $date_element .= '_raw';
         }
         $aAllowedList = array();
         $pk = $table->db_primary_key;
         foreach ($allrows as $group) {
             foreach ($group as $oData) {
                 $pkval = $oData->__pk_val;
                 if ($app->isAdmin() || $params->get('search_link_type') === 'form') {
                     $href = $oData->fabrik_edit_url;
                 } else {
                     $href = $oData->fabrik_view_url;
                 }
                 if (!in_array($href, $urls)) {
                     $limit--;
                     $urls[] = $href;
                     $o = new stdClass();
                     if (isset($oData->{$title})) {
                         $o->title = $headingPrefix ? $table->label . ' : ' . $oData->{$title} : $oData->{$title};
                     } else {
                         $o->title = $table->label;
                     }
                     $o->_pkey = $table->db_primary_key;
                     $o->section = $section;
                     $o->href = $href;
                     // Need to make sure it's a valid date in MySQL format, otherwise J!'s code will pitch a fatal error
                     if (isset($oData->{$date_element}) && FabrikString::isMySQLDate($oData->{$date_element})) {
                         $o->created = $oData->{$date_element};
                     } else {
                         $o->created = '';
                     }
                     $o->browsernav = 2;
                     if (isset($oData->{$descname})) {
                         $o->text = $oData->{$descname};
                     } else {
                         $o->text = '';
                     }
                     $o->title = strip_tags($o->title);
                     $aAllowedList[] = $o;
                 }
             }
             $list[] = $aAllowedList;
         }
     }
     $allList = array();
     foreach ($list as $li) {
         if (is_array($li) && !empty($li)) {
             $allList = array_merge($allList, $li);
         }
     }
     return $allList;
 }