Index: branches/5.2.x/core/kernel/db/db_event_handler.php
===================================================================
diff -u -N -r16414 -r16415
--- branches/5.2.x/core/kernel/db/db_event_handler.php (.../db_event_handler.php) (revision 16414)
+++ branches/5.2.x/core/kernel/db/db_event_handler.php (.../db_event_handler.php) (revision 16415)
@@ -1,6 +1,6 @@
Array ('self' => 'view', 'subitem' => 'view'),
'OnItemBuild' => Array ('self' => 'view', 'subitem' => 'view'),
'OnSuggestValues' => Array ('self' => 'admin', 'subitem' => 'admin'),
+ 'OnSuggestValuesJSON' => Array ('self' => 'admin', 'subitem' => 'admin'),
'OnBuild' => Array ('self' => true),
@@ -3472,37 +3473,85 @@
{
$event->status = kEvent::erSTOP;
- $field = $this->Application->GetVar('field');
- $cur_value = $this->Application->GetVar('cur_value');
+ $this->Application->XMLHeader();
+ $data = $this->getAutoCompleteSuggestions($event, $this->Application->GetVar('cur_value'));
$fields = $this->Application->getUnitOption($event->Prefix, 'Fields');
+ echo '';
+
+ if ( kUtil::isAssoc($data) ) {
+ foreach ($data as $key => $title) {
+ echo '- ' . kUtil::escape($title, kUtil::ESCAPE_HTML) . '
';
+ }
+ }
+ else {
+ foreach ($data as $title) {
+ echo '- ' . kUtil::escape($title, kUtil::ESCAPE_HTML) . '
';
+ }
+ }
+
+ echo '';
+ }
+
+ /**
+ * Returns auto-complete values for jQueryUI.AutoComplete
+ *
+ * @param kEvent $event
+ * @return void
+ * @access protected
+ */
+ protected function OnSuggestValuesJSON(kEvent $event)
+ {
+ $event->status = kEvent::erSTOP;
+
+ $data = $this->getAutoCompleteSuggestions($event, $this->Application->GetVar('term'));
+
+ if ( kUtil::isAssoc($data) ) {
+ $transformed_data = array();
+
+ foreach ($data as $key => $title) {
+ $transformed_data[] = array('value' => $key, 'label' => $title);
+ }
+
+ $data = $transformed_data;
+ }
+
+ echo json_encode($data);
+ }
+
+ /**
+ * Prepares a suggestion list based on a given term.
+ *
+ * @param kEvent $event Event.
+ * @param string $term Term.
+ *
+ * @return Array
+ * @access protected
+ */
+ protected function getAutoCompleteSuggestions(kEvent $event, $term)
+ {
$object = $event->getObject();
+ /* @var $object kDBItem */
- if ( !$field || !$cur_value || !$object->isField($field) ) {
- return;
+ $field = $this->Application->GetVar('field');
+
+ if ( !$field || !$term || !$object->isField($field) ) {
+ return array();
}
$limit = $this->Application->GetVar('limit');
+
if ( !$limit ) {
$limit = 20;
}
$sql = 'SELECT DISTINCT ' . $field . '
FROM ' . $this->Application->getUnitOption($event->Prefix, 'TableName') . '
- WHERE ' . $field . ' LIKE ' . $this->Conn->qstr($cur_value . '%') . '
+ WHERE ' . $field . ' LIKE ' . $this->Conn->qstr($term . '%') . '
ORDER BY ' . $field . '
LIMIT 0,' . $limit;
- $data = $this->Conn->GetCol($sql);
- $this->Application->XMLHeader();
-
- echo '';
-
- foreach ($data as $item) {
- echo '- ' . kUtil::escape($item, kUtil::ESCAPE_HTML) . '
';
- }
-
- echo '';
+ return $this->Conn->GetCol($sql);
}
/**