diff --git a/balance_tracker.css b/balance_tracker.css index be60e61..bb127bc 100644 --- a/balance_tracker.css +++ b/balance_tracker.css @@ -1,4 +1,6 @@ -/* $Id */ +td .balance { + font-weight: bold; +} td .credit { color: #33bb33; diff --git a/balance_tracker.module b/balance_tracker.module index 93f0e15..41b14e0 100644 --- a/balance_tracker.module +++ b/balance_tracker.module @@ -138,74 +138,78 @@ function balance_tracker_user($op, &$edit, &$account, $category = NULL) { */ function balance_tracker_date_form($form_state) { global $user; - $output = ''; - // Preset $to and $from based on form variables if available, or on sensible - // defaults if not. 86400 added to $to since to set the time to the end of the - // selected day. - if (isset($form_state['date_to'])) { - $to = strtotime($form_state['date_to']) + 86400; - } - else { - $to = $_SERVER['REQUEST_TIME']; - } + // If no balance entries, display only a message. + $uid = _balance_tracker_determine_uid(); - // Use value from form. - if (isset($form_state['date_from'])) { - $from = strtotime($form_state['date_from']); - } - // Use viewed user (looking at someone else's account). - else if (arg(0) == 'user' && arg(2) == 'balance' && is_numeric(arg(1)) && arg(1) != $user->uid) { - $uid = arg(1); - $viewed_user = user_load($uid); - $from = $viewed_user->created; - $output .= '

'. t("This is !user's balance sheet.", array('!user' => $viewed_user->name)) .'

'; - } - // Looking at own account. - else { - $from = $user->created; - $output .= '

'. t('This is your balance sheet.') .'

'; - } + $viewed_user = $uid == $user->uid? $user : user_load($uid); + drupal_set_title(t('%username\'s Balance', array('%username' => function_exists('realname_make_name')? realname_make_name($viewed_user) : $viewed_user->name))); - $output .= '

'. t('This shows recent credits and debits to your account. Entries from a specific date period may be viewed by selecting a date range using the boxes below labelled \'From\' and \'To\'') .'

'; + if (balance_tracker_get_balance($uid, NULL) === NULL) { + // User has no balance entries. + $form['helptext'] = array( + '#type' => 'markup', + '#value' => '

'.t('There are no entries in your balance yet.').'

', + ); + } - $form['helptext'] = array( - '#type' => 'markup', - '#value' => $output, - ); + else { + // Preset $to and $from based on form variables if available, or on sensible + // defaults if not. 86400 added to $to since to set the time to the end of the + // selected day. + if (isset($form_state['date_to'])) { + $to = strtotime($form_state['date_to']) + 86400; + } + else { + $to = $_SERVER['REQUEST_TIME']; + } - $form_state['redirect'] = NULL; + // Use value from form. + if (isset($form_state['date_from'])) { + $from = strtotime($form_state['date_from']); + } else { + $from = $viewed_user->created; + } - $format = 'm/d/Y'; + $form['helptext'] = array( + '#type' => 'markup', + '#value' => '

'. t('This shows recent credits and debits to your account. Entries from a specific date period may be viewed by selecting a date range using the boxes below labelled \'From\' and \'To\'') .'

', + ); + + $form_state['redirect'] = NULL; + + $format = 'm/d/Y'; + + $form['date_from'] = array( + '#type' => 'date_popup', + '#title' => t('From date'), + '#default_value' => date($format, $user->created), + '#date_format' => $format, + '#date_label_position' => 'within', + '#date_timezone' => 'America/Chicago', + '#date_increment' => 15, + '#date_year_range' => '-3:+0', + ); + $form['date_to'] = array( + '#type' => 'date_popup', + '#title' => t('To date'), + '#default_value' => date($format, $_SERVER['REQUEST_TIME']), + '#date_format' => $format, + '#date_label_position' => 'within', + '#date_timezone' => 'America/Chicago', + '#date_increment' => 15, + '#date_year_range' => '-3:+0', + ); + $form['submit'] = array( + '#type' => 'submit', + '#value' => t('Filter'), + ); + $form['table'] = array( + '#type' => 'markup', + '#value' => balance_tracker_balance_table($from, $to, $viewed_user), + ); + } - $form['date_from'] = array( - '#type' => 'date_popup', - '#title' => t('From'), - '#default_value' => date('Y-m-d H:i:s', $from), - '#date_format' => $format, - '#date_label_position' => 'within', - '#date_timezone' => 'America/Chicago', - '#date_increment' => 15, - '#date_year_range' => '-3:+3', - ); - $form['date_to'] = array( - '#type' => 'date_popup', - '#title' => t('To'), - '#default_value' => date('Y-m-d H:i:s', $to), - '#date_format' => $format, - '#date_label_position' => 'within', - '#date_timezone' => 'America/Chicago', - '#date_increment' => 15, - '#date_year_range' => '-3:+3', - ); - $form['submit'] = array( - '#type' => 'submit', - '#value' => t('Display'), - ); - $form['table'] = array( - '#type' => 'markup', - '#value' => balance_tracker_balance_table($from, $to), - ); return $form; } @@ -481,39 +485,39 @@ function balance_tracker_admin_form_submit($form, &$form_state) { /** * Show a page with a user's balance sheet items. * - * @param $uid + * @param $from + * Timestamp indicating start of period of which to display records. + * @param $to + * Timestamp indicating end of period of which to display records. + * @param $viewed_user * The uid of the user to display the balance sheet for. If $uid is not provided, * the user making the request is assumed. */ -function balance_tracker_balance_table($from, $to) { - global $user; +function balance_tracker_balance_table($from, $to, $viewed_user) { $output = ''; - - // Determine the UID to use - if (arg(0) == 'balance') { - $uid = $user->uid; - drupal_set_title(t("@name's Balance", array('@name' => $user->name))); - } - else if (arg(0) == 'user' && arg(2) == 'balance' && is_numeric(arg(1))) { - $uid = arg(1); - $viewed_user = user_load($uid); - drupal_set_title(t("@name's Balance", array('@name' => $viewed_user->name))); - } + $uid = $viewed_user->uid; // Add the CSS to theme the table. drupal_add_css(drupal_get_path('module', 'balance_tracker') .'/balance_tracker.css'); + if (!$_GET['page'] && variable_get('balance_tracker_table_sort', 'descending') == 'ascending' && !$_GET['notdefault']) { + // Show last page when ascendin + $_GET['page'] = '9999'; + } + $records = balance_tracker_get_items_range($uid, 10, $from, $to); // Format the records from the DB before we display them. $rows = array(); + $last_record = _balance_tracker_get_last_entry($viewed_user->uid); foreach ($records as $record) { // Get our date and time settings + $islast = $record['timestamp'] == $last_record['timestamp']; $date_type = variable_get('balance_tracker_date_format', 'small'); $custom_format = variable_get('balance_tracker_custom_date_format', ''); $record['timestamp'] = format_date($record['timestamp'], $date_type, $custom_format); // Format the currency as needed. - $record['balance'] = balance_tracker_format_currency($record['balance']); + $record['balance'] = '' . balance_tracker_format_currency($record['balance']) . ''; // Add class names to credits and debits so we can color them if ($record['type'] == 'debit') { @@ -524,17 +528,49 @@ function balance_tracker_balance_table($from, $to) { } unset($record['type']); - $rows[] = $record; + $rows[] = array('data' => $record, 'class' => $islast? 'last' : NULL); } $header = array(t('Time'), t('Message'), t('Amount'), t('Balance')); $output .= theme('table', $header, $rows); - $output .= theme('pager'); + $output .= theme('pager', NULL, NULL, 0, variable_get('balance_tracker_table_sort', 'descending') == 'ascending' ? array('notdefault' => '1') : NULL); return $output; } /** + * Determine balance uid. + * + * @return + * The uid. NULL if it could not be determined. + */ +function _balance_tracker_determine_uid() { + global $user; + $uid = NULL; + + if (arg(0) == 'balance') { + $uid = $user->uid; + } + else if (arg(0) == 'user' && arg(2) == 'balance' && is_numeric(arg(1))) { + $uid = arg(1); + } + + return($uid); +} + +/** + * Get last balance item of a user. + * + * @return + * An array of the query result. + */ +function _balance_tracker_get_last_entry($uid) { + $results = db_query_range("SELECT * FROM {balance_items} b WHERE b.uid = %d ORDER BY b.bid DESC", $uid, 0, 1); + + return(db_fetch_array($results)); +} + +/** * Adds a credit to a user's account. * * @param $uid @@ -607,21 +643,22 @@ function _balance_tracker_create_entry($uid, $type, $amount, $message) { * * @param $uid * The UID of the user for whom the balance should be retrieved. + * @param $default + * Default value to return if user has no entries in the balance + * items table. * * @return * A float containing the balance of the user. If the user has no entries * in the balance items table, the balance is assumed to be 0.00. */ -function balance_tracker_get_balance($uid) { - $results = db_query_range("SELECT * FROM {balance_items} b WHERE b.uid = %d ORDER BY b.bid DESC", $uid, 0, 1); - - $result = db_fetch_object($results); +function balance_tracker_get_balance($uid, $default = 0.00) { + $result = _balance_tracker_get_last_entry($uid); if ($result !== FALSE) { - return (float)$result->balance; + return (float)$result['balance']; } else { - return 0.00; + return $default; } } @@ -665,7 +702,8 @@ function balance_tracker_get_items($uid, $per_page = 25) { * An array of balance sheet entries in chronological order. */ function balance_tracker_get_items_range($uid, $per_page = 25, $from, $to) { - $results = pager_query("SELECT timestamp, message, type, amount, balance FROM {balance_items} b WHERE b.uid = %d AND b.timestamp >= %d AND b.timestamp <= %d ORDER BY b.bid DESC", $per_page, 0, NULL, $uid, $from, $to); + $sort = variable_get('balance_tracker_table_sort', 'descending') == 'descending' ? 'DESC' : 'ASC'; + $results = pager_query("SELECT timestamp, message, type, amount, balance FROM {balance_items} b WHERE b.uid = %d AND b.timestamp >= %d AND b.timestamp <= %d ORDER BY b.bid ".$sort, $per_page, 0, NULL, $uid, $from, $to); $rows = array(); while ($row = db_fetch_array($results)) {