diff --git a/salsa_advocacy/salsa_advocacy.module b/salsa_advocacy/salsa_advocacy.module index 07ef79b..bc03b62 100644 --- a/salsa_advocacy/salsa_advocacy.module +++ b/salsa_advocacy/salsa_advocacy.module @@ -506,19 +506,17 @@ function salsa_advocacy_targeted_form($form, &$form_state, $action) { } // Check if progress bar is enabled. - $field_name = variable_get('salsa_advocacy_goal_field'); - if (!empty($action->{$field_name})) { - // Add the corresponding CSS to the form. - $form['#attached'] = array( - 'css' => array( - array( - 'data' => drupal_get_path('module', 'salsa_entity') . '/theme/salsa_progress_bar.css' - ), - ), - ); + $field_key = variable_get('salsa_advocacy_goal_field'); + if (!empty($action->{$field_key})) { // Add the progress bar. $form['progress'] = array( - '#markup' => salsa_advocacy_get_barometer($action), + '#markup' => salsa_entity_get_progress_bar( + $action, + $form_state['build_info']['form_id'], + salsa_advocacy_get_supporter_actions_total($action), + $field_key, + $form + ), ); } @@ -1245,30 +1243,6 @@ function salsa_advocacy_settings_form_submit($form, &$form_state) { } /** - * Wrapper function to get the markup of the barometer. - * - * @param object $action - * Salsa entity of type action. - * - * @return string - * The markup for the barometer. - */ -function salsa_advocacy_get_barometer($action) { - $field_name = variable_get('salsa_advocacy_goal_field'); - - // Get total supporter actions (submissions) on for the given action. - $total = salsa_advocacy_get_supporter_actions_total($action); - - $goal = $action->{$field_name}; - $value = (($total / $goal) < 1) ? ($total / $goal) : 1; - return theme('salsa_entity_progress_bar', array( - 'goal' => $goal, - 'value' => min(round($value * 100), 100), - 'total' => $total, - )); -} - -/** * Retrieves the supporter action objects related to a given action. * * Caches the data that is fetched from Salsa if the count isn't cached yet, diff --git a/salsa_donate_page/salsa_donate_page.module b/salsa_donate_page/salsa_donate_page.module index e316fe0..d710d74 100644 --- a/salsa_donate_page/salsa_donate_page.module +++ b/salsa_donate_page/salsa_donate_page.module @@ -78,6 +78,19 @@ function salsa_donate_page_mollom_form_info($form_id) { } /** + * Implements hook_salsa_entity_progres_bar_labels(). + * + * Add currencies to the amounts and change the labels of the donation progress + * bar. + */ +function salsa_donate_page_salsa_entity_progres_bar_labels(&$labels) { + $labels['goal'] = salsa_entity_get_amount($labels['goal'], TRUE); + $labels['total'] = salsa_entity_get_amount($labels['total'], TRUE); + $labels['goal_label'] = t('Donation goal:'); + $labels['total_label'] = t('Donated:'); +} + +/** * Donate Page form. * * @todo State / Province should be dynamic. @@ -103,11 +116,6 @@ function salsa_donate_page_form($form, &$form_state, $donate_page, $view_mode) { 'data' => drupal_get_path('module', 'salsa_entity') . '/salsa_donate_page/js/salsa_donate_page.behaviors.js', ), ), - 'css' => array( - array( - 'data' => drupal_get_path('module', 'salsa_entity') . '/theme/salsa_progress_bar.css' - ), - ), ); // Add default required fields for a donation. @@ -203,9 +211,18 @@ function salsa_donate_page_form($form, &$form_state, $donate_page, $view_mode) { '#title' => t('Donation Amount'), ); - $form['Donation']['progress'] = array( - '#markup' => salsa_donate_page_get_barometer($donate_page), - ); + $field_key = variable_get('salsa_donations_goal_field'); + if (!empty($donate_page->{$field_key})) { + $form['Donation']['progress'] = array( + '#markup' => salsa_entity_get_progress_bar( + $donate_page, + $form_state['build_info']['form_id'], + salsa_donate_page_total_donation_amount($donate_page->donate_page_KEY), + $field_key, + $form + ), + ); + } if (variable_get('salsa_donations_donors_list_row_count', 0)) { $form['Donation']['donors_list'] = salsa_donate_page_get_donors_list($donate_page); @@ -1043,31 +1060,6 @@ function theme_salsa_donate_page_donors_list($vars) { } /** - * Wrapper function to get the markup of the barometer. - * - * @param object $donate_page - * The salsa donation object. - * - * @return string - * The markup of the barometer. - */ -function salsa_donate_page_get_barometer($donate_page) { - $field_name = variable_get('salsa_donations_goal_field'); - // Return an empty string if the goal isn't set. - if (empty($donate_page->{$field_name})) { - return ''; - } - $total = salsa_donate_page_total_donation_amount($donate_page->donate_page_KEY); - $goal = $donate_page->{$field_name}; - $value = (($total / $goal) < 1) ? ($total / $goal) : 1; - return theme('salsa_entity_progress_bar', array( - 'goal' => salsa_entity_get_amount($goal, TRUE), - 'value' => min(round($value * 100), 100), - 'total' => salsa_entity_get_amount($total, TRUE), - )); -} - -/** * Wrapper that prints the markup for the donors list. * * @param object $donate_page diff --git a/salsa_entity.api.php b/salsa_entity.api.php index bbec6ae..75556f6 100644 --- a/salsa_entity.api.php +++ b/salsa_entity.api.php @@ -145,3 +145,18 @@ function hook_salsa_query_TAG_alter(array &$conditions, EntityFieldQuery $query) // Define a different order field, prepend - for descending order. $conditions['#order'] = array('-field_name_2'); } + +/** + * Alter the labels that are displayed within the progress bar. + * + * @param int $goal + * The number representing the total width of the progress bar. + * @param int $total + * Total items counted so far. + */ +function hook_salsa_entity_progres_bar_labels(&$labels) { + $labels['goal'] = salsa_entity_get_amount($labels['goal'], TRUE); + $labels['total'] = salsa_entity_get_amount($labels['total'], TRUE); + $labels['goal_label'] = t('Donation goal:'); + $labels['total_label'] = t('Donated:'); +} diff --git a/salsa_entity.module b/salsa_entity.module index da699bb..88b67aa 100644 --- a/salsa_entity.module +++ b/salsa_entity.module @@ -71,6 +71,9 @@ function salsa_entity_theme() { 'goal' => NULL, 'value' => NULL, 'total' => NULL, + 'goal_label' => NULL, + 'total_label' => NULL, + 'barometer_type' => NULL, ), ), ); @@ -832,6 +835,75 @@ function salsa_entity_filter_info() { } /** + * + * @param $salsa_object + * @param $barometer_type + * @param $total + * @param $goal_field_key + * @param $form + * @return string + */ + +/** + * Creates the markup of the barometer. + * + * Also adds the corresponding CSS to the form and gives other modules a chance + * to alter the labels that will be used. + * + * @param object $salsa_object + * Salsa entity object of any type. + * @param string $barometer_type + * Identifier passed by the requesting function, can be used as translation + * context to have varying labels for progress bars on the different salsa + * forms. + * @param int $total + * Counted amount for the current progress. + * @param string $goal_field_key + * Key for the property on the salsa object that contains the goal amount. + * @param array $form + * The form on which the progress bar will be displayed, passed by reference. + * + * @return string + * The markup for the barometer. + */ +function salsa_entity_get_progress_bar($salsa_object, $barometer_type, $total, $goal_field_key, &$form) { + // Add the progress bar CSS to the requesting form. + $form['#attached']['css'] = array( + array( + 'data' => drupal_get_path('module', 'salsa_entity') . '/theme/salsa_progress_bar.css' + ), + ); + + $goal = !empty($salsa_object->{$goal_field_key}) ? $salsa_object->{$goal_field_key} : NULL; + // Don't allow rendering of progress bar without values that make sense. + if (!$total && !$goal) { + return ''; + } + + // Calculate the percentage of the total for the styling of the progress bar. + $value = (($total / $goal) < 1) ? ($total / $goal) : 1; + + // Give other modules a chance to alter the labels. + $labels = array( + 'goal' => $goal, + 'total' => $total, + 'goal_label' => t('Goal'), + 'total_label' => t('Total'), + ); + drupal_alter('salsa_entity_progres_bar_labels', $labels); + + // Theme the progress bar with the collected values. + return theme('salsa_entity_progress_bar', array( + 'goal' => $labels['goal'], + 'goal_label' => $labels['goal_label'], + 'total' => $labels['total'], + 'total_label' => $labels['total_label'], + 'value' => min(round($value * 100), 100), + 'barometer_type' => $barometer_type, + )); +} + +/** * Filter process callback to render an entity. */ function _salsa_entity_filter_entity_view($text, $filter, $format, $langcode, $cache, $cache_id) { @@ -866,9 +938,9 @@ function _salsa_entity_filter_replace_callback($matches) { */ function theme_salsa_entity_progress_bar($vars) { $output = '
';