Created
September 29, 2011 12:46
-
-
Save brendo/1250660 to your computer and use it in GitHub Desktop.
RL Autocomplete
This file contains hidden or 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 | |
Class contentExtensionReferencelinkAutocomplete extends AjaxPage{ | |
public function handleFailedAuthorisation(){ | |
$this->_status = self::STATUS_UNAUTHORISED; | |
$this->_Result = json_encode(array('status' => __('You are not authorised to access this page.'))); | |
} | |
public function view(){ | |
require_once(TOOLKIT . '/class.entrymanager.php'); | |
$entryManager = new EntryManager(Symphony::Engine()); | |
$fieldManager = $entryManager->fieldManager; | |
$sectionManager = $entryManager->sectionManager; | |
if(!isset($_GET['term'])) { | |
$this->_Result = json_encode(array('status' => __('No results'))); | |
return; | |
} | |
else { | |
$query = General::sanitize(urldecode($_GET['term'])); | |
if(empty($query)) { | |
$this->_Result = json_encode(array('status' => __('No results'))); | |
return; | |
} | |
} | |
if(!isset($_GET['field-id'])) { | |
$this->_Result = json_encode(array('status' => __('Missing <code>field-id</code>'))); | |
return; | |
} | |
else { | |
$field_ids = explode(',',$_GET['field-id']); | |
$results = array(); | |
foreach($field_ids as $field_id) { | |
$field = $fieldManager->fetch($field_id); | |
$section_id = $field->get('parent_section'); | |
$joins = $where = ''; | |
$field->buildDSRetrievalSQL($query, $joins, $where, false); | |
// Need to make it a wildcard search | |
$where = str_replace("IN ('", "LIKE '%", $where); | |
$where = str_replace("')", "%'", $where); | |
// Fetch entries | |
$entries = $entryManager->fetch(null, $section_id, 10, 0, $where, $joins, false, true, array($field->get('element_name'))); | |
foreach($entries as $entry) { | |
$results[] = array( | |
'id' => $entry->get('id'), | |
'label' => $entry->getData($field->get('id'), true)->value | |
); | |
} | |
} | |
// Return | |
$this->_Result = json_encode($results); | |
} | |
} | |
public function generate(){ | |
header('Content-Type: application/json'); | |
echo $this->_Result; | |
exit; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment