Created
December 29, 2009 10:01
-
-
Save nickdunn/265240 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once(TOOLKIT . '/class.datasource.php'); | |
class DatasourceSearchException extends Exception { | |
} | |
class DatasourceSearch extends Datasource { | |
protected $fieldDefaults = array(); | |
protected $fieldModes = array(); | |
protected $rootElement = 'search'; | |
public function __construct(&$parent, $env = null, $process_params = true) { | |
parent::__construct($parent, $env, $process_params); | |
$this->_dependencies = array(); | |
} | |
public function allowEditorToParse() { | |
return false; | |
} | |
public function setFieldDefaults(Array $defaults) { | |
$this->fieldDefaults = $defaults; | |
} | |
public function setFieldModes(Array $modes) { | |
$this->fieldModes = $modes; | |
} | |
public function setRootElement($name) { | |
$this->rootElement = $name; | |
} | |
public function grab(&$param_pool, $search = null, $sort_by = null, $sort_order = null, $length = 10, $page = 1) { | |
if (is_null($search)) return; | |
if (is_null($sort_order)) { | |
$sort_order = 'asc'; | |
} | |
if (@$_REQUEST['sort-by'] != '') { | |
$sort_by = $_REQUEST['sort-by']; | |
} | |
if (@$_REQUEST['sort-order'] == 'desc') { | |
$sort_order = $_REQUEST['sort-order']; | |
} | |
if ((integer)@$_REQUEST['length']) { | |
$length = $_REQUEST['length']; | |
} | |
if ((integer)@$_REQUEST['page']) { | |
$page = $_REQUEST['page']; | |
} | |
// Make it a GET request: | |
if ( | |
$_SERVER['REQUEST_METHOD'] == 'POST' | |
and ( | |
isset($_POST['sort_by']) | |
or isset($_POST['sortsortby']) | |
or isset($_POST['fields']) | |
) | |
) { | |
$query = ''; $count = 0; | |
if (is_array($_POST['fields'])) foreach ($_POST['fields'] as $key => $value) { | |
if (is_null($value) or $value == '') continue; | |
if ($count++) $query .= '&'; else $query .= '?'; | |
$query .= sprintf( | |
'fields[%s]=%s', | |
$key, rawurlencode($value) | |
); | |
} | |
if ($sort_by) $sort_by = "/sort-by:{$sort_by}"; | |
if ($sort_order) $sort_order = "/sort-order:{$sort_order}"; | |
$query = sprintf( | |
'%s/%s%s%s/%s', | |
$this->_env['param']['root'], | |
trim($this->_env['param']['current-path'], '/'), | |
$sort_by, $sort_order, $query | |
); | |
redirect($query); | |
} | |
else { | |
$xml_output = new XMLElement($this->rootElement); | |
$xml_results = new XMLElement('results'); | |
// include SymQL | |
require_once(EXTENSIONS . '/symql/lib/class.symql.php'); | |
// create a new SymQLQuery | |
$query = new SymQLQuery(); | |
$query->select('*')->from($search); | |
foreach ($_REQUEST['fields'] as $handle => $value) { | |
$value = rawurldecode($value); | |
$value = stripslashes($value); | |
$query->where($handle, $value); | |
} | |
if (isset($_REQUEST['sort-by']) && isset($_REQUEST['sort-order'])) { | |
$query->orderby($_REQUEST['sort-by'], $_REQUEST['sort-order']) | |
} | |
$query->perPage(10)->page($page); | |
$xml_values = SymQL::run($query); | |
$xml_output->appendChild($xml_values); | |
$xml_output->appendChild($xml_results); | |
return $xml_output; | |
} | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment