Skip to content

Commit

Permalink
Updated with latest 1.6.x changes
Browse files Browse the repository at this point in the history
  • Loading branch information
kasperg committed Oct 14, 2011
1 parent 03e302b commit 2225e00
Show file tree
Hide file tree
Showing 9 changed files with 266 additions and 15 deletions.
9 changes: 9 additions & 0 deletions css/ting_admin_boost_form.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* @file
* CSS rules for boost field editing form.
*/

#boost-fields-wrapper .boost-field-instance .form-item,
#boost-fields-wrapper .boost-field-instance input { float: left; }
#boost-fields-wrapper .boost-field-instance input.form-submit { margin: 1.92em 0 0 0.2em; }

147 changes: 147 additions & 0 deletions includes/ting.admin.inc
Original file line number Diff line number Diff line change
Expand Up @@ -251,3 +251,150 @@ function ting_admin_ranking_add_field_ahah() {
exit();
}

/**
* Form builder; Configure custom search result ranking for this site.
*
* @ingroup forms
*/
function ting_admin_boost_settings($form, &$form_state) {
$form = array('#after_build' => array('ting_admin_boost_form_after_build'));

// Get our stored data.
$field_data = variable_get('ting_boost_fields', array());

if (!isset($form_state['boost_field_count'])) {
$form_state['boost_field_count'] = count($field_data);
}

// Wrapper, so that the AJAX callback have some place to put new elements
$form['ting_boost_fields'] = array(
'#title' => t('Custom fields boost values'),
'#type' => 'fieldset',
'#prefix' => '<div id="boost-fields-wrapper">',
'#suffix' => '</div>',
'#tree' => TRUE,
);

foreach (range(0, $form_state['boost_field_count']) as $i) {
$form['ting_boost_fields']['fields'][$i] = array(
'#type' => 'ting_boost_field',
'#default_value' => (isset($field_data[$i])) ? $field_data[$i] : array(),
);
}

$form['ting_boost_fields']['add_value'] = array(
'#type' => 'submit',
'#value' => t('Add another boost value'),
'#submit' => array('ting_admin_boost_add_more_submit'),
'#weight' => 10,
'#ahah' => array(
'path' => 'ting/boost_field/ahah',
'wrapper' => 'boost-fields-wrapper',
'method' => 'append',
'effect' => 'slide'
),
);

$form['buttons']['save'] = array(
'#type' => 'submit',
'#value' => t('Save changes'),
);

return $form;
}

/**
* Submit handler for the "Add another item" button of a field form.
*
* This handler is run regardless of whether JS is enabled or not. It makes
* changes to the form state. If the button was clicked with JS disabled, then
* the page is reloaded with the complete rebuilt form. If the button was
* clicked with JS enabled, then ajax_form_callback() calls field_add_more_js()
* to return just the changed part of the form.
*/
function ting_admin_boost_add_more_submit($form, &$form_state) {
// Simply add another empty set of values to the field array and ask
// to have the form rebuilt. The form code will take care of the rest.
$form_state['boost_field_count'] += 1;
$form_state['rebuild'] = TRUE;
}

/**
* AHAH callback to add another ranking field to the form.
*/
function ting_admin_boost_add_field_ahah() {
$form_state = array('submitted' => FALSE);
$form_build_id = $_POST['form_build_id'];

// Drupal is not aware of this new elements existence and will not
// process it. We retreive the cached form, add the element, and resave.
$form = form_get_cache($form_build_id, $form_state);

// Add one to the current highest delta value to get the next one.
$delta = max(element_children($form['ting_boost_fields']['fields'])) + 1;

// Added field and update form cache
$form['ting_ranking_fields']['fields'][$delta] = array(
'#type' => 'ting_boost_field',
'#default_value' => array(),
);
form_set_cache($form_build_id, $form, $form_state);

// Reset form vars
$form += array(
'#post' => $_POST,
'#programmed' => FALSE,
);

// Rebuild the form.
$form = form_builder($_POST['form_id'], $form, $form_state);

// Render and return the new field.
drupal_json(array(
'data' => drupal_render($form['ting_ranking_fields'][$delta]),
'status' => TRUE));
exit();
}

/**
* After build callback to the boost settings form.
*
* Add a little bit of CSS and JS to the form.
*/
function ting_admin_boost_form_after_build($form, &$form_state) {
$path = drupal_get_path('module', 'ting');

drupal_add_css($path .'/css/ting_admin_boost_form.css');

return $form;
}

/**
* Submit handler for boost settings form.
*/
function ting_admin_boost_settings_submit($form, &$form_state) {
$fields = $form_state['values']['ting_boost_fields']['fields'];

$fields = array_filter($fields, '_ting_boost_field_filter');
usort($fields, '_ting_boost_field_sort');

variable_set('ting_boost_fields', $fields);
}

/**
* array_filter() callback to remove empty/deleted elements.
*/
function _ting_boost_field_filter($element) {
return !(empty($element['field_name']) || empty($element['field_value']));
}

/**
* usort() callback to remove empty/deleted elements.
*/
function _ting_boost_field_sort($a, $b) {
if ($a['weight'] == $b['weight']) {
return 0;
}
return ($a['weight'] > $b['weight']) ? -1 : 1;
}

1 change: 1 addition & 0 deletions js/ting.buttons.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ Drupal.tingButtons.dialogButton = function (selector, options) {
.dialog({
'title': title,
'buttons': buttons,
'height': 'auto',
'close': function (event, ui) {
$(this).dialog('destroy').remove();
}
Expand Down
24 changes: 16 additions & 8 deletions modules/ting_availability/js/ting.availability.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,45 +75,53 @@ Drupal.tingAvailability.updateStatus = function (data, textStatus) {
$.each(data, function(itemId, itemData) {
var $item = $('#ting-item-' + itemId);
if (!itemData.show_reservation_button) {
$item.find('.ting-status')
$item.find('.ting-availability-status')
.addClass('unreservable')
.removeClass('waiting')
.text(Drupal.t('not reservable'))
.end()
.find('.ting-object-buttons .buttons > li > a')
// FIXME: There are two problems here:
// - Reservation adds a class to the link. Cart to the li.
// The class for the link should be moved to the li in
// ding_reservation_ting_object_buttons().
// - The carting button is toggled based on whether the item
// is reservable or not. This it harmful to sites which
// offer other options besides reservation from the cart.
.find('.ting-status ul.buttons > li > a.reserve-now,' +
'.ting-status ul.buttons > li.add-to-cart > a')
.addClass('disabled');
}
else if (itemData.available_from) {
$item.find('.ting-status')
$item.find('.ting-availability-status')
.addClass('unavailable')
.removeClass('waiting')
.text(Drupal.t('available from @date', {'@date': itemData.available_from}));
}
else if (itemData.deferred_period) {
$item.find('.ting-status')
$item.find('.ting-availability-status')
.addClass('unavailable')
.removeClass('waiting')
.text(Drupal.t('waiting period'));
}
else if (itemData.available) {
$item.find('.ting-status')
$item.find('.ting-availability-status')
.addClass('available')
.removeClass('waiting')
.text(Drupal.t('available'));
}
else if (!itemData.reservable) {
$item.find('.ting-status')
$item.find('.ting-availability-status')
.addClass('unavailable')
.removeClass('waiting')
.text(Drupal.t('unavailable'));
}
else if (itemData.reserved_count < 1) {
$item.find('.ting-status')
$item.find('.ting-availability-status')
.addClass('unavailable')
.removeClass('waiting')
.text(Drupal.t('on loan'));
} else {
$item.find('.ting-status')
$item.find('.ting-availability-status')
.addClass('unavailable')
.addClass('reserved')
.removeClass('waiting')
Expand Down
4 changes: 2 additions & 2 deletions modules/ting_availability/ting_availability.module
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ function ting_availability_preprocess_ting_object(&$vars) {
if (ting_object_is($object, 'limited_availability')) {
$vars['additional_main_content'][] = array(
'#type' => 'markup',
'#value' => '<div class="ting-status waiting">' . t('waiting for data') . '</div>',
'#value' => '<div class="ting-status ting-availability-status waiting">' . t('waiting for data') . '</div>',
);

$vars['additional_content'][] = array(
Expand All @@ -147,7 +147,7 @@ function ting_availability_preprocess_ting_list_item(&$vars) {
if (ting_object_is($object, 'limited_availability')) {
$vars['additional_content'][] = array(
'#type' => 'markup',
'#value' => '<div class="ting-status waiting">Afventer data</div>',
'#value' => '<div class="ting-status ting-availability-status waiting">' . t('waiting for data') . '</div>',
);

};
Expand Down
6 changes: 4 additions & 2 deletions ting-list-item.tpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@
<span class='date'><?php print $date; ?></span>
<h3><?php print $title; ?></h3>

<em><?php echo t('by'); ?></em>
<?php print $creator ?>
<?php if ($creator) { ?>
<em><?php echo t('by'); ?></em>
<?php print $creator ?>
<?php } ?>

<div class='language'><?php echo t('Language') . ': ' . $language; ?></div>
<?php
Expand Down
8 changes: 7 additions & 1 deletion ting.client.inc
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,12 @@ function ting_do_search($query, $page = 1, $resultsPerPage = 10, $options = arra
}
}

// Apply custom boosts if any.
$boosts = variable_get('ting_boost_fields', array());
if ($boosts) {
$request->userDefinedBoost = $boosts;
}

$searchResult = ting_execute($request);

//Decorate search result with additional information
Expand Down Expand Up @@ -484,4 +490,4 @@ function ting_add_relations($request, $type = 'full') {
$request->setAllRelations(TRUE);
$request->setRelationData($type);
return $request;
}
}
76 changes: 76 additions & 0 deletions ting.module
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@ function ting_menu() {
'type' => MENU_LOCAL_TASK,
);

$items['admin/settings/ting/boost'] = array(
'title' => 'Boost',
'description' => 'Enable the user to boost specific values for specific fields in search results.',
'page callback' => 'drupal_get_form',
'page arguments' => array('ting_admin_boost_settings'),
'access arguments' => array('administer site configuration'),
'file' => 'ting.admin.inc',
'file path' => $path,
'type' => MENU_LOCAL_TASK,
);

$items['ting/ranking_field/ahah'] = array(
'title' => 'Ting ranking field AHAH callback',
'title callback' => FALSE,
Expand All @@ -55,6 +66,16 @@ function ting_menu() {
'type' => MENU_CALLBACK,
);

$items['ting/boost_field/ahah'] = array(
'title' => 'Ting boost field AHAH callback',
'title callback' => FALSE,
'page callback' => 'ting_admin_boost_add_field_ahah',
'access arguments' => array('administer site configuration'),
'file' => 'ting.admin.inc',
'file path' => $path,
'type' => MENU_CALLBACK,
);

return $items;
}

Expand All @@ -68,6 +89,10 @@ function ting_menu() {
*/
function ting_elements() {
return array(
'ting_boost_field' => array(
'#input' => TRUE,
'#process' => array('ting_boost_field_element_process'),
),
'ting_ranking_field' => array(
'#input' => TRUE,
'#process' => array('ting_ranking_field_element_process'),
Expand Down Expand Up @@ -110,6 +135,9 @@ function ting_theme() {
'ting_ranking_field' => array(
'arguments' => array('element' => NULL),
),
'ting_boost_field' => array(
'arguments' => array('element' => NULL),
),
);
}

Expand Down Expand Up @@ -581,3 +609,51 @@ function theme_ting_ranking_field($element) {
return theme('form_element', $element, '<div class="ranking-field-instance clear-block">'. $element['#children'] .'</div>');
}

/**
* Processor for the ting_boost_field form element.
*/
function ting_boost_field_element_process($element, $form_state) {
$element['#tree'] = TRUE;
$element['#prefix'] = '<div class="ting-boost-field-element clearfix">';
$element['#suffix'] = '</div>';

$element['field_name'] = array(
'#title' => t('Field name'),
'#type' => 'select',
'#options' => array(
'' => '- ' . t('Choose') . ' -',
'ac.source' => t('Source'),
'dc.creator' => t('Author'),
'dc.date' => t('Year of publish'),
'dc.language' => t('Language'),
'dc.type' => t('Material type'),
'dc.identifier' => t('ISBN number'),
),
'#default_value' => (isset($element['#value']['field_name'])) ? $element['#value']['field_name'] : NULL,
'#attributes' => array('class' => array('field-name')),
);

$element['field_value'] = array(
'#title' => t('Value'),
'#type' => 'textfield',
'#default_value' => (isset($element['#value']['field_value'])) ? $element['#value']['field_value'] : NULL,
);

$element['weight'] = array(
'#title' => t('Weight'),
'#type' => 'select',
'#options' => drupal_map_assoc(range(1, 10)),
'#default_value' => (isset($element['#value']['weight'])) ? $element['#value']['weight'] : NULL,
);

return $element;
}

/**
* Theme function to format the custom form element ting_boost_field.
*/
function theme_ting_boost_field($element) {
return theme('form_element', $element, '<div class="boost-field-instance clear-block">'. $element['#children'] .'</div>');
}


Loading

0 comments on commit 2225e00

Please sign in to comment.