glFusion Wiki

Site Tools


The SearchCriteria() Class

(glFusion v1.1.0)

The process of returning the values is done by initializing a SearchCriteria() object, setting the parameters for the search then returning the object. Here is an example:

$search = new SearchCriteria('myplugin', 'My Plugin');

This indicates that the 'myplugin' plugin supports Full-Text searches and will have a ranking of 4.

buildSearchSQL() Function

As a lot of the plugins will be processing the same or similar SQL queries the buildSearchSQL() function has been provided to simplify things. This function will take four parameters then build the searching part of the SQL query. It will also return the Full-Text query strings should they be required. The parameters are:

  • $keyType – search key type: 'all', 'phrase', 'any'.
  • $query – the query string.
  • $columns – the column names to search.
  • $sql – the sql query to append to. (optional)

And an example of how it can be put to use:

$sql = 'SELECT ... FROM ... WHERE ... ';
$columns = array('title','bodytext');
list($sql,$ftsql) = buildSearchSQL('any', 'my glfusion', $columns, $sql);

// $sql => SELECT ... FROM ... WHERE ... AND ((title LIKE '%my%' OR bodytext LIKE '%my%') OR  (title LIKE '%glfusion%' OR bodytext LIKE '%glfusion%'))
// $ftsql[mysql] => SELECT ... FROM ... WHERE ... AND MATCH(title,bodytext) AGAINST ('my glfusion' IN BOOLEAN MODE)
// $ftsql[mssql] => SELECT ... FROM ... WHERE ... AND CONTAINS((title,bodytext), '"my" OR "glfusion"')

A proper example of its use is at the bottom of the page.

Enabling URL Rewrite

If the plugin requires the URL to be passed through the COM_buildUrl() function then it should set the setURLRewrite() function to true. For example:

$search = new SearchCriteria('myplugin', 'My Plugin');

Returning Multiple Objects

In some cases a plugin may be required to search across two or more tables that have no relation with one another. To accommodate this they may return an array of SearchCriteria() objects, each with a different SQL query. To allow the user to differentiate between the results an array of names can be passed, each name will be a separate sub group and will be appended to one another using the separator from the configuration. The following example shows how this works:

// Search the main table
//   These results will be labelled 'My Plugin > Main'
$search_main = new SearchCriteria('myplugin', array('My Plugin', 'Main'));

// Search a sub tables
//   These results will be labelled 'My Plugin > Sub'
$search_sub = new SearchCriteria('myplugin', array('My Plugin', 'Sub'));

// Return both objects
return array($search_main, $search_sub);

Note: The plugin identifier needs to stay the same across all objects, in this case 'myplugin'.

glfusion/development/api/search/searchcriteria.txt · Last modified: 2016/10/09 10:10 (external edit)

Page Tools