This question has been answered by Bruno17. See the first response.
class Service extends xPDOSimpleObject{
public $modx;
public $config = array();
function __construct(modX &$modx,array $config = array()){
$this->modx =& $modx;
$basePath = $this->modx->getOption('search_bar.core_path','',$this->modx->getOption('core_path').'components/search_bar/');
$assetsUrl = $this->modx->getOption('search_bar.assets_url','',$this->modx->getOption('assets_url').'components/search_bar/');
$this->config = array_merge(array(
'basePath' => $basePath,
'corePath' => $basePath,
'modelPath' => $basePath.'model/',
'processorsPath' => $basePath.'processors/',
'templatesPath' => $basePath.'templates/',
'chunksPath' => $basePath.'elements/chunks/',
'jsUrl' => $assetsUrl.'js/',
'cssUrl' => $assetsUrl.'css/',
'assetsUrl' => $assetsUrl,
'connectorUrl' => $assetsUrl . 'connector.php',
),$config);
}
public function searchServices($searchTerm = ''){
if($searchTerm != ""){
$query = $this->modx->newQuery('Service');
$query->select($this->modx->getSelectColumns('Service','Service','',array('id', 'subject', 'link', 'target')));
$query->leftJoin('ServiceTag', 'ServiceTag', 'Service.id = ServiceTag.service_id');
$query->leftJoin('Tag', 'Tag', 'ServiceTag.tag_id = Tag.id');
$query->where(array(
'Service.subject:LIKE' => '%'.$searchTerm.'%',
'OR:Tag.tag:LIKE' => '%'.$searchTerm.'%'
));
$query->sortby('Service.subject','ASC');
$query->limit(10);
$query->prepare();
print $query->toSQL();
$suggestions = $this->modx->getCollection('Service', $query);
if(count($suggestions) > 0){
foreach($suggestions as $suggestion) {
$listItems[] = array(
"label"=>$suggestion->get('subject'),
"link"=>$suggestion->get('link'),
"target"=>$suggestion->get('target')
);
}
}else{
$listItems[] = array(
"label"=>"no results found",
"link"=>"/services",
"target"=>""
);
}
return json_encode($listItems);
}
}
}
$modelPath = MODX_CORE_PATH . 'components/search_bar/model/';
//add package
if(!$modx->addPackage('search_bar', $modelPath,'res_search')){
$modx->log(modX::LOG_LEVEL_ERROR, 'Error loading Service class in snippet.service.php');
return 'Error adding search_bar package in snippet.service.php';
}
$action = (isset($_POST['action'])) ? $_POST['action'] : '';
$searchTerm = (isset($_POST['search_term'])) ? $_POST['search_term'] : '';
if($searchTerm != "" && $action == "service-search"){
$service = $modx->newObject('Service');
//search for services
return $service->searchServices($searchTerm);
}