From c8d8d8459223c6eb4d04de5f224eb4f246badc59 Mon Sep 17 00:00:00 2001
From: Sascha Grossenbacher <saschagros@gmail.com>
Date: Mon, 28 Feb 2011 17:23:18 +0100
Subject: [PATCH] Issue #1070138 by Berdir: Reordered local tasks in admin area.

---
 userpoints.admin.inc |   62 ++++++++-------------
 userpoints.module    |  150 +++++++++++++++++++++++++++++++++----------------
 userpoints.pages.inc |    2 +
 3 files changed, 127 insertions(+), 87 deletions(-)

diff --git a/userpoints.admin.inc b/userpoints.admin.inc
index 4073577..fbbb6ea 100644
--- a/userpoints.admin.inc
+++ b/userpoints.admin.inc
@@ -5,13 +5,6 @@
  * Admin menu callbacks for userpoints.module.
  */
 
-/**
- * Approves moderated points.
- */
-function userpoints_admin_approve($operation, $txn_id) {
-  return drupal_get_form('userpoints_confirm_approve', $operation, (int)$txn_id);
-}
-
 function userpoints_confirm_approve_submit($form, &$form_state) {
   global $user;
 
@@ -28,37 +21,28 @@ function userpoints_confirm_approve_submit($form, &$form_state) {
 /**
  * Form builder for add/edit userpoints transaction form.
  */
-function userpoints_admin_txn($form, &$form_state, $mode, $txn_id = NULL) {
+function userpoints_admin_txn($form, &$form_state, $mode, $txn = NULL) {
   global $user;
 
   drupal_add_css(drupal_get_path('module', 'userpoints') . '/userpoints.css');
 
   $timestamp = format_date(REQUEST_TIME, 'custom', 'Y-m-d H:i O');
-  if ($mode == 'edit' && $txn_id) {
-    $txn = db_query('SELECT * FROM {userpoints_txn} WHERE txn_id = :txn_id', array(':txn_id' => $txn_id))->fetch();
-
-    if (!$txn) {
-      // Failed to load transaction, display not found page.
-      drupal_not_found();
-      drupal_exit();
-    }
-
+  if ($mode == 'edit') {
+    drupal_set_title(t('Edit !points transaction', userpoints_translation()));
     $timestamp = format_date($txn->time_stamp, 'custom', 'Y-m-d H:i:s O');
-    $txn_user = user_load($txn->uid);
+    $txn_user = $txn->user;
 
     $form['txn'] = array(
       '#type' => 'value',
       '#value' => $txn,
     );
   }
-  elseif ($mode == 'add' && $txn_id) {
-    $txn_user = user_load($txn_id);
-  } elseif ($mode == 'edit') {
-    // Edit but now transaction id given, display not found page.
-    drupal_not_found();
-    drupal_exit();
+  elseif ($mode == 'add') {
+    drupal_set_title(t('Add !points', userpoints_translation()));
+    if ($txn) {
+      $txn_user = user_load($txn);
+    }
   }
-
   $form['txn_user'] = array(
       '#type' => 'textfield',
       '#title' => t('User Name'),
@@ -416,9 +400,8 @@ function userpoints_admin_points($tid = NULL) {
   $query->join('users', 'u', 'p.uid = u.uid');
   $query->leftJoin('taxonomy_term_data', 't', 'p.tid = t.tid');
 
-  // Check for filtering. isset() is used because 0 is a valid value
-  // (Uncategorized).
-  if (isset($tid)) {
+  // Check for filtering.
+  if (isset($categories[$tid])) {
     // If a category is selected, limit both the default query and the query
     // that displays pending points to this category.
     $query->condition('p.tid', (int)$tid);
@@ -488,7 +471,7 @@ function userpoints_admin_transactions($tid = NULL) {
   // to select from. This check is specific to the categories the user has
   // points in.
   if ($show_category) {
-    $output['form'] = drupal_get_form('userpoints_filter_cat_select', 'admin/config/people/userpoints/list/transactions/', $tid);
+    $output['form'] = drupal_get_form('userpoints_filter_cat_select', 'admin/config/people/userpoints/transaction/', $tid);
     $output['form']['#weight'] = -10;
   }
 
@@ -505,9 +488,8 @@ function userpoints_admin_transactions($tid = NULL) {
 
   $query->leftJoin('taxonomy_term_data', 't', 'p.tid = t.tid');
 
-  // Check for filtering. isset() is used because 0 is a valid value
-  // (Uncategorized).
-  if (isset($tid)) {
+  // Check for filtering.
+  if (isset($categories[$tid])) {
     // If a category is selected, limit both the default query and the query
     // that displays pending points to this category.
     $query->condition('p.tid', (int)$tid);
@@ -609,36 +591,40 @@ function userpoints_admin_manage($tid = NULL) {
 }
 
 
-function userpoints_confirm_approve($form, $form_state, $operation, $txn_id) {
+function userpoints_confirm_approve($form, $form_state, $operation, $transaction) {
   $form = array(
     'txn_id' => array(
         '#type' => 'value',
-        '#value' => $txn_id,
+        '#value' => $transaction->txn_id,
     ),
   );
 
-  $transaction = userpoints_transaction_load($txn_id);
   $arguments = array(
     '!user' => theme('username', array('account' => $transaction->user)),
     '%category' => $transaction->category,
   ) + userpoints_translation();
 
   if ($operation == 'approve') {
-    $question = format_plural($transaction->points, 'Do you want to approve @count !point for !user in the %category category?', 'Do you want to approve @count !points for !user in the %category category?', $arguments);
+    $question = t('Approve');
+    $description = format_plural($transaction->points, 'Do you want to approve @count !point for !user in the %category category?', 'Do you want to approve @count !points for !user in the %category category?', $arguments);
     $form['operation'] = array(
       '#type' => 'value',
       '#value' => USERPOINTS_TXN_STATUS_APPROVED,
     );
   }
   else {
-    $question = format_plural($transaction->points, 'Do you want to decline @count !point for !user in the %category category?', 'Do you want to decline @count !points for !user in the %category category?', $arguments);
+    $question = t('Decline');
+    $description = format_plural($transaction->points, 'Do you want to decline @count !point for !user in the %category category?', 'Do you want to decline @count !points for !user in the %category category?', $arguments);
     $form['operation'] = array(
       '#type' => 'value',
       '#value' => USERPOINTS_TXN_STATUS_DECLINED,
     );
   }
 
-  return confirm_form($form, $question, 'admin/config/people/userpoints/moderate', t('Reason: !reason', array('!reason' => userpoints_create_description($transaction))));
+  $description = '<p><strong>' . $description . '</strong></p>';
+  $description .= '<p>' . t('Reason: !reason', array('!reason' => userpoints_create_description($transaction))) . '</p>';
+
+  return confirm_form($form, $question, 'admin/config/people/userpoints/moderate', $description);
 }
 /**
  * Menu callback for settings form.
diff --git a/userpoints.module b/userpoints.module
index f37bbe6..f5a2c6b 100644
--- a/userpoints.module
+++ b/userpoints.module
@@ -145,24 +145,15 @@ function userpoints_menu() {
       'file' => 'userpoints.admin.inc',
   );
   $items['admin/config/people/userpoints/list'] = array(
-      'title' => 'List',
-      'description' => strtr('List users by !points', userpoints_translation()),
-      'access callback' => 'userpoints_admin_access',
-      'access arguments' => array('list'),
-      'file' => 'userpoints.admin.inc',
-      'type' => MENU_DEFAULT_LOCAL_TASK,
-      'weight' => -2,
-  );
-  $items['admin/config/people/userpoints/list/totals'] = array(
       'title' => 'Totals',
       'description' => strtr('List users by !points', userpoints_translation()),
       'access callback' => 'userpoints_admin_access',
       'access arguments' => array('list'),
       'file' => 'userpoints.admin.inc',
       'type' => MENU_DEFAULT_LOCAL_TASK,
-      'weight' => 0,
+      'weight' => -2,
   );
-  $items['admin/config/people/userpoints/list/transactions'] = array(
+  $items['admin/config/people/userpoints/transaction'] = array(
       'title' => 'Transactions',
       'title arguments' => userpoints_translation(),
       'description' => 'List transactions',
@@ -171,8 +162,14 @@ function userpoints_menu() {
       'access arguments' => array('edit'),
       'file' => 'userpoints.admin.inc',
       'type' => MENU_LOCAL_TASK,
-      'weight' => 2,
+      'weight' => 0,
+  );
+  $items['admin/config/people/userpoints/transaction/list'] = array(
+    'title' => 'List',
+    'type' => MENU_DEFAULT_LOCAL_TASK,
+    'weight' => -15,
   );
+
   $items['admin/config/people/userpoints/moderate'] = array(
       'title' => 'Moderation',
       'title arguments' => userpoints_translation(),
@@ -182,47 +179,62 @@ function userpoints_menu() {
       'access arguments' => array('moderate'),
       'file' => 'userpoints.admin.inc',
       'type' => MENU_LOCAL_TASK,
-      'weight' => -1,
+      'weight' => 2,
   );
   $items['admin/config/people/userpoints/add'] = array(
-      'title' => 'Add',
+      'title' => 'Add !points transaction',
+      'title arguments' => userpoints_translation(),
       'description' => 'Admin add/delete userpoints',
       'page callback' => 'drupal_get_form',
-      'page arguments' => array('userpoints_admin_txn', 4, 5),
+      'page arguments' => array('userpoints_admin_txn', 4),
       'access callback' => 'userpoints_admin_access',
       'access arguments' => array('add'),
       'file' => 'userpoints.admin.inc',
-      'type' => MENU_LOCAL_TASK,
+      'type' => MENU_LOCAL_ACTION,
       'weight' => 0,
   );
-  $items['admin/config/people/userpoints/edit'] = array(
+
+  $items['admin/config/people/userpoints/transaction/%userpoints_transaction/view'] = array(
+    'title' => 'View',
+    'page callback' => 'userpoints_view_transaction',
+    'page arguments' => array(5),
+    'access callback' => 'userpoints_access_view_transaction',
+    'access arguments' => array(5),
+    'file' => 'userpoints.pages.inc',
+    'type' => MENU_LOCAL_TASK,
+    'weight' => -10,
+  );
+  
+  $items['admin/config/people/userpoints/transaction/%userpoints_transaction/edit'] = array(
       'title' => 'Edit',
       'page callback' => 'drupal_get_form',
-      'page arguments' => array('userpoints_admin_txn', 4, 5),
+      'page arguments' => array('userpoints_admin_txn', 6, 5),
       'access callback' => 'userpoints_admin_access',
       'access arguments' => array('edit'),
       'file' => 'userpoints.admin.inc',
-      'type' => MENU_CALLBACK
+      'type' => MENU_LOCAL_TASK,
+      'weight' => -5,
   );
 
-  $items['admin/config/people/userpoints/approve'] = array(
-      'title' => 'Approve Userpoints',
-      'page callback' => 'userpoints_admin_approve',
-      'page arguments' => array(4, 5),
-      'access callback' => 'userpoints_admin_access',
-      'access arguments' => array('moderate'),
+  $items['admin/config/people/userpoints/transaction/%userpoints_transaction/approve'] = array(
+      'title' => 'Approve transaction',
+      'title arguments' => userpoints_translation(),
+      'page callback' => 'drupal_get_form',
+      'page arguments' => array('userpoints_confirm_approve', 6, 5),
+      'access callback' => 'userpoints_admin_access_transaction_pending',
+      'access arguments' => array(5),
       'file' => 'userpoints.admin.inc',
-      'type' => MENU_CALLBACK
+      'type' => MENU_LOCAL_TASK,
   );
-  $items['admin/config/people/userpoints/decline'] = array(
-      'title' => 'Approve !points',
+  $items['admin/config/people/userpoints/transaction/%userpoints_transaction/decline'] = array(
+      'title' => 'Decline transaction',
       'title arguments' => userpoints_translation(),
-      'page callback' => 'userpoints_admin_approve',
-      'page arguments' => array(4, 5),
-      'access callback' => 'userpoints_admin_access',
-      'access arguments' => array('moderate'),
+      'page callback' => 'drupal_get_form',
+      'page arguments' => array('userpoints_confirm_approve', 6, 5),
+      'access callback' => 'userpoints_admin_access_transaction_pending',
+      'access arguments' => array(5),
       'file' => 'userpoints.admin.inc',
-      'type' => MENU_CALLBACK
+      'type' => MENU_LOCAL_TASK,
   );
 
   $items['admin/config/people/userpoints/settings'] = array(
@@ -283,6 +295,49 @@ function userpoints_menu() {
 }
 
 /**
+ * Implements hook_menu_local_tasks_alter().
+ */
+function userpoints_menu_local_tasks_alter(&$data, $router_item, $root_path) {
+  // Add action link to add points on 'userpoints' administration pages.
+  if (strpos($root_path, 'admin/config/people/userpoints/') !== FALSE) {
+
+    // Don't display the action link on some pages like settings and
+    // approve or decline confirmation forms.
+    $blacklist = array('settings', 'approve', 'decline');
+    foreach ($blacklist as $blacklisted_path) {
+      if (strpos($root_path, $blacklisted_path) !== FALSE) {
+        return;
+      }
+    }
+
+    $item = menu_get_item('admin/config/people/userpoints/add');
+
+    // For the transaction view pages, we want to directly link to the
+    // user for this transaction.
+    if (arg(4) == 'transaction' && (arg(6) == 'view' || arg(6) == 'edit')) {
+      $transaction = userpoints_transaction_load(arg(5));
+      $item['href'] .= '/' . $transaction->uid;
+    }
+
+    if ($item['access']) {
+      $data['actions']['output'][] = array(
+        '#theme' => 'menu_local_action',
+        '#link' => $item,
+      );
+    }
+  }
+}
+
+/**
+ * Access callback for approve and decline local tasks.
+ *
+ * Only pending transactions can be approved or declined.
+ */
+function userpoints_admin_access_transaction_pending($transaction) {
+  return $transaction->status == USERPOINTS_TXN_STATUS_PENDING && userpoints_admin_access('moderate');
+}
+
+/**
  * Checks if user can access their points - used via hook_menu().
  *
  * @return
@@ -314,17 +369,6 @@ function userpoints_access_view_transaction($transaction) {
 }
 
 /**
- * Title callback function, display transaction ID.
- * @param $transaction
- *   Transaction object.
- * @return
- *   Page title.
- */
-function userpoints_view_transaction_title($transaction) {
-  return t('View !points transaction #@id', array('@id' => $transaction->txn_id) + userpoints_translation());
-}
-
-/**
  * Implements hook_permission().
  */
 function userpoints_permission() {
@@ -478,7 +522,7 @@ function userpoints_tokens($type, $tokens, array $data = array(), array $options
           break;
 
         case 'display':
-          $replacements[$original] = $txn->$name ? t('Yes') : t('No');
+          $replacements[$original] = !empty($txn->$name) ? t('Yes') : t('No');
           break;
 
         case 'status':
@@ -1714,6 +1758,9 @@ function userpoints_userpoints_info() {
  */
 function userpoints_transaction_load($txn_id) {
   $transaction = db_query('SELECT * from {userpoints_txn} WHERE txn_id = :txn', array(':txn' => $txn_id))->fetchObject();
+  if (!$transaction) {
+    return FALSE;
+  }
 
   // Load corresponding user object.
   $transaction->user = user_load($transaction->uid);
@@ -1737,19 +1784,24 @@ function userpoints_transaction_load($txn_id) {
  *   A string with operation links.
  */
 function userpoints_get_transaction_actions($transaction, $show_view = TRUE) {
+  global $user;
   $actions = array();
   $url_options = array('query' => drupal_get_destination());
 
   if ($show_view && userpoints_access_view_transaction($transaction)) {
-    $actions[] = l('view', 'userpoints/view/' . $transaction->txn_id);
+    $url = 'userpoints/view/' . $transaction->txn_id;
+    if ($transaction->uid != $user->uid || strpos($_GET['q'], 'admin/config/people/userpoints') !== FALSE) {
+      $url = 'admin/config/people/userpoints/transaction/' . $transaction->txn_id . '/view';
+    }
+    $actions[] = l('view', $url);
   }
 
   if (userpoints_admin_access('edit')) {
-    $actions[] = l('edit', "admin/config/people/userpoints/edit/$transaction->txn_id", $url_options);
+    $actions[] = l('edit', "admin/config/people/userpoints/transaction/$transaction->txn_id/edit", $url_options);
   }
   if (userpoints_admin_access('moderate') && $transaction->status == USERPOINTS_TXN_STATUS_PENDING) {
-    $actions[] = l('approve', "admin/config/people/userpoints/approve/$transaction->txn_id", $url_options);
-    $actions[] = l('decline', "admin/config/people/userpoints/decline/$transaction->txn_id", $url_options);
+    $actions[] = l('approve', "admin/config/people/userpoints/transaction/$transaction->txn_id/approve", $url_options);
+    $actions[] = l('decline', "admin/config/people/userpoints/transaction/$transaction->txn_id/decline", $url_options);
   }
   return implode(' ', $actions);
 }
diff --git a/userpoints.pages.inc b/userpoints.pages.inc
index 79cc630..262e673 100644
--- a/userpoints.pages.inc
+++ b/userpoints.pages.inc
@@ -189,6 +189,8 @@ function userpoints_list_users($tid = NULL) {
 function userpoints_view_transaction($transaction) {
   drupal_add_css(drupal_get_path('module', 'userpoints') . '/userpoints.css');
 
+  drupal_set_title(t('View transaction #@txn_id', array('@txn_id' => $transaction->txn_id)));
+
   $css_stati = array(
     USERPOINTS_TXN_STATUS_APPROVED => 'approved',
     USERPOINTS_TXN_STATUS_DECLINED => 'declined',
-- 
1.7.4.1

