summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDominique CLAUSE2014-09-28 13:38:57 (GMT)
committerDominique CLAUSE2014-09-28 13:38:57 (GMT)
commitaf693e9fd726cf3351ec5eade492a55921e32c7e (patch)
tree81b3a46fc3d98d3ba9cdb79f59cffde56ab18da3
parentd4ba4b2c40a0133afc7aa1695021ec985fccd85c (diff)
Feature #2329307 : add a "no_results" custom label / value / link configurations.
Feature #2270267 : add a "all_results" custom label / value / link configurations.
-rw-r--r--js/jquery.autocomplete.js130
-rw-r--r--search_autocomplete.form.add.inc26
-rw-r--r--search_autocomplete.form.configure.inc106
-rw-r--r--search_autocomplete.install132
-rw-r--r--search_autocomplete.module1
5 files changed, 301 insertions, 94 deletions
diff --git a/js/jquery.autocomplete.js b/js/jquery.autocomplete.js
index be18b96..09b889f 100644
--- a/js/jquery.autocomplete.js
+++ b/js/jquery.autocomplete.js
@@ -23,20 +23,25 @@
innerHTML += ('<div class="ui-autocomplete-field-' + key + '">' + output + '</div>');
});
} else {
- // Case no results :
- innerHTML += ('<div class="ui-autocomplete-field-noresult">' + item.label + '</div>');
+ innerHTML += ('<div class="ui-autocomplete-field">' + item.label + '</div>');
}
innerHTML += '</div>';
var group = '';
if ("group" in item) {
- group += ('<div class="ui-autocomplete-field-group ' + item.group.group_id + '">' + item.group.group_name + '</div>');
+ groupId = typeof(item.group.group_id) !== 'undefined' ? item.group.group_id : '';
+ groupName = typeof(item.group.group_name) !== 'undefined' ? item.group.group_name : '';
+ group += ('<div class="ui-autocomplete-field-group ' + groupId + '">' + groupName + '</div>');
$(group).appendTo(ul);
}
var elem = $("<li class=ui-menu-item-" + first + "></li>" )
- .data("item.autocomplete", item)
- .append("<a>" + innerHTML + "</a>")
- .appendTo(ul);
+ .append("<a>" + innerHTML + "</a>");
+ if (item.value == '') {
+ elem = $("<li class='ui-state-disabled ui-menu-item-" + first + " ui-menu-item'>" + item.label + "</li>" );
+ }
+ elem.data("item.autocomplete", item)
+ .appendTo(ul);
+
Drupal.attachBehaviors(elem);
return elem;
};
@@ -50,61 +55,70 @@
attach: function(context) {
if (Drupal.settings.search_autocomplete) {
$.each(Drupal.settings.search_autocomplete, function(key, value) {
- var NoResultsLabel = Drupal.settings.search_autocomplete[key].no_results;
+ var no_results = Drupal.settings.search_autocomplete[key].no_results;
+ var all_results = Drupal.settings.search_autocomplete[key].all_results;
$(Drupal.settings.search_autocomplete[key].selector).bind("mouseover", function() {
$(Drupal.settings.search_autocomplete[key].selector).addClass('ui-autocomplete-processed ui-theme-' + Drupal.settings.search_autocomplete[key].theme).autocomplete({
- minLength: Drupal.settings.search_autocomplete[key].minChars,
- source: function(request, response) {
- // External URL:
- if (Drupal.settings.search_autocomplete[key].type == 'external') {
- $.getJSON(Drupal.settings.search_autocomplete[key].datas, { q: request.term }, function (results) {
- // Only return the number of values set in the settings.
- if (!results.length && NoResultsLabel) {
- results = [NoResultsLabel.replace("[search-phrase]", request.term)];
- }
- response(results.slice(0, Drupal.settings.search_autocomplete[key].max_sug));
- });
- }
- // Internal URL:
- else if (Drupal.settings.search_autocomplete[key].type == 'internal' || Drupal.settings.search_autocomplete[key].type == 'view') {
- $.getJSON(Drupal.settings.search_autocomplete[key].datas + request.term, { }, function (results) {
- // Only return the number of values set in the settings.
- if (!results.length && NoResultsLabel) {
- results = [NoResultsLabel.replace("[search-phrase]", request.term)];
+ minLength: Drupal.settings.search_autocomplete[key].minChars,
+ source: function(request, response) {
+ // External URL:
+ if (Drupal.settings.search_autocomplete[key].type == 'external') {
+ $.getJSON(Drupal.settings.search_autocomplete[key].datas, { q: request.term }, function (results) {
+ // Only return the number of values set in the settings.
+ if (!results.length && no_results) {
+ results = [jQuery.parseJSON(no_results.replace(/\[search-phrase\]/g, request.term))];
+ } else {
+ results.slice(0, Drupal.settings.search_autocomplete[key].max_sug);
+ results.push(jQuery.parseJSON(all_results.replace(/\[search-phrase\]/g, request.term)));
+ }
+ response(results);
+ });
+ }
+ // Internal URL:
+ else if (Drupal.settings.search_autocomplete[key].type == 'internal' || Drupal.settings.search_autocomplete[key].type == 'view') {
+ $.getJSON(Drupal.settings.search_autocomplete[key].datas + request.term, { }, function (results) {
+ // Only return the number of values set in the settings.
+ if (!results.length && no_results) {
+ results = [jQuery.parseJSON(no_results.replace(/\[search-phrase\]/g, request.term))];
+ } else {
+ results.slice(0, Drupal.settings.search_autocomplete[key].max_sug);
+ results.push(jQuery.parseJSON(all_results.replace(/\[search-phrase\]/g, request.term)));
+ }
+ response(results);
+ });
+ }
+ // Static resources:
+ else if (Drupal.settings.search_autocomplete[key].type == 'static') {
+ var results = $.ui.autocomplete.filter(Drupal.settings.search_autocomplete[key].datas, request.term);
+ if (!results.length && no_results) {
+ results = [jQuery.parseJSON(no_results.replace(/\[search-phrase\]/g, request.term))];
+ } else {
+ results.slice(0, Drupal.settings.search_autocomplete[key].max_sug);
+ results.push(jQuery.parseJSON(all_results.replace(/\[search-phrase\]/g, request.term)));
}
- response(results.slice(0, Drupal.settings.search_autocomplete[key].max_sug));
- });
- }
- // Static resources:
- else if (Drupal.settings.search_autocomplete[key].type == 'static') {
- var results = $.ui.autocomplete.filter(Drupal.settings.search_autocomplete[key].datas, request.term);
- if (!results.length && NoResultsLabel) {
- results = [NoResultsLabel.replace("[search-phrase]", request.term)];
- }
- // Only return the number of values set in the settings.
- response(results.slice(0, Drupal.settings.search_autocomplete[key].max_sug));
- }
- },
- open: function(event, ui) {
- $(".ui-autocomplete li.ui-menu-item:odd").addClass("ui-menu-item-odd");
- $(".ui-autocomplete li.ui-menu-item:even").addClass("ui-menu-item-even");
- },
- select: function(event, ui) {
- if (ui.item.label === NoResultsLabel) {
- event.preventDefault();
- } else if (Drupal.settings.search_autocomplete[key].auto_redirect == 1 && ui.item.link) {
- document.location.href = ui.item.link;
- } else if (Drupal.settings.search_autocomplete[key].auto_submit == 1) {
- $(this).val(ui.item.label);
- $(this).closest("form").submit();
- }
- },
- focus: function (event, ui) {
- if (ui.item.label === NoResultsLabel) {
- event.preventDefault();
- }
- },
- appendTo: $(Drupal.settings.search_autocomplete[key].selector).parent(),
+ response(results);
+ }
+ },
+ open: function(event, ui) {
+ $(".ui-autocomplete li.ui-menu-item:odd").addClass("ui-menu-item-odd");
+ $(".ui-autocomplete li.ui-menu-item:even").addClass("ui-menu-item-even");
+ },
+ select: function(event, ui) {
+ if (Drupal.settings.search_autocomplete[key].auto_redirect == 1 && ui.item.link) {
+ document.location.href = ui.item.link;
+ } else if (Drupal.settings.search_autocomplete[key].auto_submit == 1 && ui.item.value) {
+ $(this).val(ui.item.value);
+ $(this).closest("form").submit();
+ } else {
+ event.preventDefault();
+ }
+ },
+ focus: function (event, ui) {
+ if (ui.item.label === no_results) {
+ event.preventDefault();
+ }
+ },
+ appendTo: $(Drupal.settings.search_autocomplete[key].selector).parent(),
}).autocomplete("widget").attr("id", "ui-theme-" + Drupal.settings.search_autocomplete[key].theme);
});
$(Drupal.settings.search_autocomplete[key].selector).trigger('mouseover');
diff --git a/search_autocomplete.form.add.inc b/search_autocomplete.form.add.inc
index a553f7d..ec38756 100644
--- a/search_autocomplete.form.add.inc
+++ b/search_autocomplete.form.add.inc
@@ -77,11 +77,27 @@ function search_autocomplete_form_add_submit($form, &$form_state) {
// Insert the new form in database.
$fid = db_insert('search_autocomplete_forms')
- ->fields(array(
- 'title' => $values['title'],
- 'selector' => $values['selector'],
- ))
- ->execute();
+ ->fields(array(
+ 'title' => $values['title'],
+ 'selector' => $values['selector'],
+ 'no_results' => json_encode(array( // manually add no_results default
+ 'label' => 'No results found for <b>[search-phrase]</b>.<br/>Click to perform full search.',
+ 'value' => '[search-phrase]',
+ 'link' => '',
+ 'group' => array(
+ 'group_id' => 'no_results',
+ ),
+ )),
+ 'all_results' => json_encode(array( // manually add all_results default
+ 'label' => 'View all results for <b>[search-phrase]</b>.',
+ 'value' => '[search-phrase]',
+ 'link' => '',
+ 'group' => array(
+ 'group_id' => 'all_results',
+ ),
+ )),
+ ))
+ ->execute();
// Redirect to configuration page.
$form_state['redirect'] = 'admin/config/search/search_autocomplete/' . $fid . '/configure';
diff --git a/search_autocomplete.form.configure.inc b/search_autocomplete.form.configure.inc
index 13566b1..c76943a 100644
--- a/search_autocomplete.form.configure.inc
+++ b/search_autocomplete.form.configure.inc
@@ -57,15 +57,7 @@ function search_autocomplete_form_configure($form, &$form_state) {
'#maxlength' => 2,
'#required' => TRUE,
);
- // Check if form should be autosubmitted.
- $form['search_autocomplete_how']['no_results'] = array(
- '#type' => 'textfield',
- '#title' => t('Behavior when no suggestions are found'),
- '#description' => t('Enter a message to display when no results are found. Leave empty for no message. You can use HTML tags as well as the token [search-phrase] to replace user input.'),
- '#default_value' => $item->no_results,
- '#maxlength' => 50,
- '#required' => FALSE,
- );
+
// Check if form should be autosubmitted.
$form['search_autocomplete_how']['auto_submit'] = array(
'#type' => 'checkbox',
@@ -81,6 +73,80 @@ function search_autocomplete_form_configure($form, &$form_state) {
'#default_value' => $item->auto_redirect,
);
+ // ###
+ $all_results = json_decode($item->all_results);
+ // "View all results" custom configurations
+ $form['search_autocomplete_how']['view_all_results'] = array(
+ '#type' => 'fieldset',
+ '#title' => t('Custom behavior when some suggestions are available'),
+ '#collapsible' => TRUE,
+ '#collapsed' => TRUE,
+ );
+ // Check if form should be autosubmitted.
+ $form['search_autocomplete_how']['view_all_results']['all_results_label'] = array(
+ '#type' => 'textfield',
+ '#title' => t('Custom "view all results" message label'),
+ '#description' => t('This message is going to be displayed at the end of suggestion list when suggestions are found. Leave empty to disable this functionality. You can use HTML tags as well as the token [search-phrase] to replace user input.'),
+ '#default_value' => $all_results->label,
+ '#maxlength' => 255,
+ '#required' => FALSE,
+ );
+ // Check if form should be autosubmitted.
+ $form['search_autocomplete_how']['view_all_results']['all_results_value'] = array(
+ '#type' => 'textfield',
+ '#title' => t('Custom "view all results" message value'),
+ '#description' => t('If a label is filled above, this is the value that will be picked when the message is selected. Leave empty if you don\'t when the message to be selectable. You can use the token [search-phrase] to replace user input.'),
+ '#default_value' => $all_results->value,
+ '#maxlength' => 255,
+ '#required' => FALSE,
+ );
+ // Check if form should be autosubmitted.
+ $form['search_autocomplete_how']['view_all_results']['all_results_link'] = array(
+ '#type' => 'textfield',
+ '#title' => t('Custom "view all results" URL redirection'),
+ '#description' => t('If "Auto redirect" is checked and a label is given for this configuration, the user will be redirected to this URL when the message is selected. Leave empty if you rather like a standard Drupal search to be performed on the "value" given above.'),
+ '#default_value' => $all_results->link,
+ '#maxlength' => 255,
+ '#required' => FALSE,
+ );
+
+ // ###
+ $no_results = json_decode($item->no_results);
+ // "No resuls" custom configurations
+ $form['search_autocomplete_how']['no_results'] = array(
+ '#type' => 'fieldset',
+ '#title' => t('Custom behavior when no suggestions are found'),
+ '#collapsible' => TRUE,
+ '#collapsed' => TRUE,
+ );
+ // Check if form should be autosubmitted.
+ $form['search_autocomplete_how']['no_results']['no_results_label'] = array(
+ '#type' => 'textfield',
+ '#title' => t('Custom "no result" message label'),
+ '#description' => t('This message is going to be displayed when no suggestions can be found. Leave empty to disable this functionality. You can use HTML tags as well as the token [search-phrase] to replace user input.'),
+ '#default_value' => $no_results->label,
+ '#maxlength' => 255,
+ '#required' => FALSE,
+ );
+ // Check if form should be autosubmitted.
+ $form['search_autocomplete_how']['no_results']['no_results_value'] = array(
+ '#type' => 'textfield',
+ '#title' => t('Custom "no result" message value'),
+ '#description' => t('If a label is filled above, this is the value that will be picked when the message is selected. Leave empty if you don\'t when the message to be selectable. You can use the token [search-phrase] to replace user input.'),
+ '#default_value' => $no_results->value,
+ '#maxlength' => 255,
+ '#required' => FALSE,
+ );
+ // Check if form should be autosubmitted.
+ $form['search_autocomplete_how']['no_results']['no_results_link'] = array(
+ '#type' => 'textfield',
+ '#title' => t('Custom "no result" URL redirection'),
+ '#description' => t('If "Auto redirect" is checked and a label is given for this configuration, the user will be redirected to this URL when the message is selected. Leave empty if you rather like a standard Drupal search to be performed on the "value" given above.'),
+ '#default_value' => $no_results->link,
+ '#maxlength' => 255,
+ '#required' => FALSE,
+ );
+
// ------------------------------------------------------------------.
// WHAT - What to display in Search Autocomplete suggestions.
$form['search_autocomplete_what'] = array(
@@ -246,6 +312,23 @@ function search_autocomplete_form_configure_submit($form, &$form_state) {
$callback = 'search_autocomplete/autocomplete/' . $values['fid'] . '/';
}
+ $all_results = array(
+ 'label' => $values['all_results_label'],
+ 'value' => $values['all_results_value'],
+ 'link' => $values['all_results_link'],
+ 'group' => array(
+ 'group_id' => 'all_results',
+ ),
+ );
+ $no_results = array(
+ 'label' => $values['no_results_label'],
+ 'value' => $values['no_results_value'],
+ 'link' => $values['no_results_link'],
+ 'group' => array(
+ 'group_id' => 'no_results',
+ ),
+ );
+
// ###.
// UPDATE THE FORM.
// -> update form.
@@ -255,7 +338,8 @@ function search_autocomplete_form_configure_submit($form, &$form_state) {
'max_sug' => $values['max_sug'],
'auto_submit' => $values['auto_submit'],
'auto_redirect' => $values['auto_redirect'],
- 'no_results' => $values['no_results'],
+ 'all_results' => json_encode($all_results),
+ 'no_results' => json_encode($no_results),
'selector' => $values['selector'],
'data_source' => $values['suggestions'],
'data_callback' => $callback,
@@ -312,4 +396,4 @@ function _search_autocomplete_get_all_children($fid, &$items = array(), $depth =
_search_autocomplete_get_all_children($item->fid, $items, $depth);
}
return $items;
-}
+} \ No newline at end of file
diff --git a/search_autocomplete.install b/search_autocomplete.install
index 49025fa..b51e0fa 100644
--- a/search_autocomplete.install
+++ b/search_autocomplete.install
@@ -73,11 +73,14 @@ function search_autocomplete_schema() {
'default' => 10,
),
'no_results' => array(
- 'description' => 'Maximum number of suggestions',
- 'type' => 'varchar',
- 'length' => 50,
- 'not null' => FALSE,
- 'default' => '-- No results found for <b>[search-phrase]</b> --',
+ 'description' => 'Display field when no suggestion available.',
+ 'type' => 'text',
+ 'size' => 'big',
+ ),
+ 'all_results' => array(
+ 'description' => 'Display field when some suggestion are available.',
+ 'type' => 'text',
+ 'size' => 'big',
),
'auto_submit' => array(
'description' => 'Define if form should be autosubmitted when suggestion is choosen',
@@ -147,7 +150,7 @@ function search_autocomplete_install() {
// ----------------
// Declare insertion statement.
$insert = db_insert('search_autocomplete_forms')
- ->fields(array('title', 'selector', 'weight', 'enabled', 'min_char', 'max_sug', 'auto_submit', 'auto_redirect', 'data_source', 'data_callback', 'data_static', 'data_view', 'theme'));
+ ->fields(array('title', 'selector', 'weight', 'enabled', 'min_char', 'max_sug', 'no_results', 'all_results', 'auto_submit', 'auto_redirect', 'data_source', 'data_callback', 'data_static', 'data_view', 'theme'));
$insert->values(array(
'title' => st('Search page - Node Tab') . " (search/node/%)",
'selector' => '#search-form[action="/search/node"] #edit-keys',
@@ -155,6 +158,22 @@ function search_autocomplete_install() {
'enabled' => $enabled,
'min_char' => $trigger,
'max_sug' => $limit,
+ 'no_results' => json_encode(array(
+ 'label' => 'No results found for <b>[search-phrase]</b>.<br/>Click to perform full search.',
+ 'value' => '[search-phrase]',
+ 'link' => '',
+ 'group' => array(
+ 'group_id' => 'no_results',
+ ),
+ )),
+ 'all_results' => json_encode(array(
+ 'label' => 'View all results for <b>[search-phrase]</b>.',
+ 'value' => '[search-phrase]',
+ 'link' => '',
+ 'group' => array(
+ 'group_id' => 'all_results',
+ ),
+ )),
'translite' => 1,
'auto_submit' => 1,
'auto_redirect' => 1,
@@ -165,12 +184,28 @@ function search_autocomplete_install() {
'theme' => 'basic-green.css',
));
$insert->values(array(
- 'title' => st('Search page - User Tab') . " (search/user/%)",
- 'selector' => '#search-form[action="/search/user"] #edit-keys',
- 'weight' => 1,
- 'enabled' => $enabled,
- 'min_char' => $trigger,
- 'max_sug' => $limit,
+ 'title' => st('Search page - User Tab') . " (search/user/%)",
+ 'selector' => '#search-form[action="/search/user"] #edit-keys',
+ 'weight' => 1,
+ 'enabled' => $enabled,
+ 'min_char' => $trigger,
+ 'max_sug' => $limit,
+ 'no_results' => json_encode(array(
+ 'label' => 'No results found for <b>[search-phrase]</b>.<br/> Click to perform full search.',
+ 'value' => '[search-phrase]',
+ 'link' => '',
+ 'group' => array(
+ 'group_id' => 'no_results',
+ ),
+ )),
+ 'all_results' => json_encode(array(
+ 'label' => 'View all results for <b>[search-phrase]</b>.',
+ 'value' => '[search-phrase]',
+ 'link' => '',
+ 'group' => array(
+ 'group_id' => 'all_results',
+ ),
+ )),
'translite' => 1,
'auto_submit' => 1,
'auto_redirect' => 1,
@@ -181,12 +216,28 @@ function search_autocomplete_install() {
'theme' => 'user-blue.css',
));
$insert->values(array(
- 'title' => st('Search Block'),
- 'selector' => "#edit-search-block-form--2",
- 'weight' => 0,
- 'enabled' => $enabled,
- 'min_char' => $trigger,
- 'max_sug' => $limit,
+ 'title' => st('Search Block'),
+ 'selector' => "#edit-search-block-form--2",
+ 'weight' => 0,
+ 'enabled' => $enabled,
+ 'min_char' => $trigger,
+ 'max_sug' => $limit,
+ 'no_results' => json_encode(array(
+ 'label' => 'No results found for <b>[search-phrase]</b>.<br/>Click to perform full search.',
+ 'value' => '[search-phrase]',
+ 'link' => '',
+ 'group' => array(
+ 'group_id' => 'no_results',
+ ),
+ )),
+ 'all_results' => json_encode(array(
+ 'label' => 'View all results for <b>[search-phrase]</b>.',
+ 'value' => '[search-phrase]',
+ 'link' => '',
+ 'group' => array(
+ 'group_id' => 'all_results',
+ ),
+ )),
'translite' => 1,
'auto_submit' => 1,
'auto_redirect' => 1,
@@ -335,12 +386,53 @@ function search_autocomplete_update_7408(&$sandbox) {
* Migrate no_results field from 4.0 to 4.1 default value
*/
function search_autocomplete_update_7410(&$sandbox) {
-
if (db_field_exists('search_autocomplete_forms', 'no_results')) {
db_update('search_autocomplete_forms') // Table name no longer needs {}
->fields(array('no_results' => '-- No results found for <b>[search-phrase]</b> --'))
->execute();
}
-
return (t('Update no_results field default value has been done.'));
+}
+
+/**
+ * Migrate no_results and all_resuts fields from 4.0 to 4.1 default value
+ */
+function search_autocomplete_update_7411(&$sandbox) {
+ if (!db_field_exists('search_autocomplete_forms', 'no_results')) {
+ $field = array(
+ 'description' => 'Display field when no suggestion available.',
+ 'type' => 'text',
+ 'size' => 'big',
+ );
+ db_add_field('search_autocomplete_forms', 'no_results', $field);
+ }
+ db_update('search_autocomplete_forms') // Table name no longer needs {}
+ ->fields(array('no_results' => json_encode(array(
+ 'label' => 'No results found for <b>[search-phrase]</b>.<br/>Click to perform full search.',
+ 'value' => '[search-phrase]',
+ 'link' => '',
+ 'group' => array(
+ 'group_id' => 'no_results',
+ ),
+ ))))
+ ->execute();
+
+ if (!db_field_exists('search_autocomplete_forms', 'all_results')) {
+ $field = array(
+ 'description' => 'Display field when some suggestion are available.',
+ 'type' => 'text',
+ 'size' => 'big',
+ );
+ db_add_field('search_autocomplete_forms', 'all_results', $field);
+ }
+ db_update('search_autocomplete_forms') // Table name no longer needs {}
+ ->fields(array('all_results' => json_encode(array(
+ 'label' => 'View all results for <b>[search-phrase]</b>.',
+ 'value' => '[search-phrase]',
+ 'link' => '',
+ 'group' => array(
+ 'group_id' => 'all_results',
+ ),
+ ))))
+ ->execute();
} \ No newline at end of file
diff --git a/search_autocomplete.module b/search_autocomplete.module
index a9a521c..0a77c71 100644
--- a/search_autocomplete.module
+++ b/search_autocomplete.module
@@ -76,6 +76,7 @@ function search_autocomplete_init() {
'minChars' => $match->min_char,
'max_sug' => $match->max_sug,
'no_results' => $match->no_results,
+ 'all_results' => $match->all_results,
'type' => $data_source,
'datas' => $match->data_source == 'static' ? $data_static : $data_callback,
'fid' => $match->fid,