diff --git a/uc_op_reports.info b/uc_op_reports.info
index 7e448b3..e096c0b 100644
--- a/uc_op_reports.info
+++ b/uc_op_reports.info
@@ -5,12 +5,14 @@ description = Add ordered products reports to user profile pages and add a site-
 dependencies[] = uc_order
 dependencies[] = uc_product
 package = Ubercart - extra
-core = 6.x
+core = 7.x
 php = 5.0
 
 ; Information added by drupal.org packaging script on 2009-05-20
 version = "6.x-1.0-rc1"
-core = "6.x"
+core = 7.x
 project = "uc_op_reports"
 datestamp = "1242860757"
 
+
+files[] = uc_op_reports.module
diff --git a/uc_op_reports.module b/uc_op_reports.module
index 04ce2e6..9cf79fa 100644
--- a/uc_op_reports.module
+++ b/uc_op_reports.module
@@ -1,6 +1,4 @@
 <?php
-// $Id: uc_op_reports.module,v 1.1.2.4 2009/05/20 23:00:19 rszrama Exp $
-
 /**
  * @file
  * Add ordered products reports to user profile pages and for administrators.
@@ -8,7 +6,7 @@
 
 
 /**
- * Implementation of hook_menu().
+ * Implements hook_menu().
  */
 function uc_op_reports_menu() {
   $items = array();
@@ -43,12 +41,16 @@ function uc_op_reports_menu() {
   return $items;
 }
 
-// Return the title of the user profile tab.
+/**
+ * Return the title of the user profile tab.
+ */
 function uc_op_reports_user_tab_title() {
   return uc_op_reports_user_tab_titles(variable_get('uc_op_reports_user_tab_title', 'ordered_products'));
 }
 
-// Determine access to the user profile tab.
+/**
+ * Determine access to the user profile tab.
+ */
 function uc_op_reports_user_tab_access($uid) {
   global $user;
 
@@ -66,25 +68,42 @@ function uc_op_reports_user_tab_access($uid) {
 }
 
 /**
- * Implementation of hook_perm().
+ * Implements hook_permission().
  */
-function uc_op_reports_perm() {
-  return array('admin ordered products reports', 'view ordered products admin report', 'view any ordered products report', 'view own ordered products report');
+function uc_op_reports_permission() {
+  return array(
+    'admin ordered products reports' => array(
+      'title' => t('admin ordered products reports'),
+      'description' => t('Administer ordered products reports'),
+    ),
+    'view ordered products admin report' => array(
+      'title' => t('view ordered products admin report'),
+      'description' => t('View ordered products admin report'),
+    ),
+    'view any ordered products report' => array(
+      'title' => t('view any ordered products report'),
+      'description' => t('View any ordered products report'),
+    ),
+    'view own ordered products report' => array(
+      'title' => t('view own ordered products report'),
+      'description' => t('View own ordered products report'),
+    ),
+  );
 }
 
 /**
- * Implementation of hook_theme().
+ * Implements hook_theme().
  */
 function uc_op_reports_theme() {
   return array(
     'uc_op_reports_fields' => array(
-      'arguments' => array('form' => NULL),
+      'render element' => 'form',
     ),
   );
 }
 
 /**
- * Implementation of hook_form_alter().
+ * Implements hook_form_alter().
  */
 function uc_op_reports_form_alter(&$form, &$form_state, $form_id) {
   if ($form_id == 'uc_reports_settings_form' && user_access('admin ordered products reports')) {
@@ -118,30 +137,33 @@ function uc_op_reports_form_alter(&$form, &$form_state, $form_id) {
     // Loop through all the available ordered products reports fields.
     foreach (uc_op_reports_fields() as $key => $value) {
       $form['uc_op_reports']['fields'][$key] = array(
-        '#weight' => variable_get('uc_opr_field_'. $key .'_weight', $value['weight']),
+        '#weight' => variable_get('uc_opr_field_' . $key . '_weight', $value['weight']),
       );
       $form['uc_op_reports']['fields'][$key]['field_name'] = array(
-        '#value' => $value['title'],
+        '#markup' => $value['title'],
       );
-      $form['uc_op_reports']['fields'][$key]['uc_opr_field_'. $key .'_weight'] = array(
+      $form['uc_op_reports']['fields'][$key]['uc_opr_field_' . $key . '_weight'] = array(
         '#type' => 'weight',
         '#delta' => 10,
-        '#default_value' => variable_get('uc_opr_field_'. $key .'_weight', $value['weight']),
+        '#default_value' => variable_get('uc_opr_field_' . $key . '_weight', $value['weight']),
       );
-      $form['uc_op_reports']['fields'][$key]['uc_opr_field_'. $key .'_customer'] = array(
+      $form['uc_op_reports']['fields'][$key]['uc_opr_field_' . $key . '_customer'] = array(
         '#type' => 'checkbox',
-        '#default_value' => variable_get('uc_opr_field_'. $key .'_customer', $value['customer']),
+        '#default_value' => variable_get('uc_opr_field_' . $key . '_customer', $value['customer']),
       );
-      $form['uc_op_reports']['fields'][$key]['uc_opr_field_'. $key .'_admin'] = array(
+      $form['uc_op_reports']['fields'][$key]['uc_opr_field_' . $key . '_admin'] = array(
         '#type' => 'checkbox',
-        '#default_value' => variable_get('uc_opr_field_'. $key .'_admin', $value['admin']),
+        '#default_value' => variable_get('uc_opr_field_' . $key . '_admin', $value['admin']),
       );
     }
   }
 }
 
-// Theme the table on the settings forms for the fields in the reports.
-function theme_uc_op_reports_fields($form) {
+/**
+ * Theme the table on the settings forms for the fields in the reports.
+ */
+function theme_uc_op_reports_fields($variables) {
+  $form = $variables['form'];
   $header = array(t('Report field'), t('Weight'), t('Customer'), t('Admin'));
 
   $rows = array();
@@ -149,16 +171,18 @@ function theme_uc_op_reports_fields($form) {
   foreach (element_children($form) as $field) {
     $rows[] = array(
       drupal_render($form[$field]['field_name']),
-      drupal_render($form[$field]['uc_opr_field_'. $field .'_weight']),
-      drupal_render($form[$field]['uc_opr_field_'. $field .'_customer']),
-      drupal_render($form[$field]['uc_opr_field_'. $field .'_admin']),
+      drupal_render($form[$field]['uc_opr_field_' . $field . '_weight']),
+      drupal_render($form[$field]['uc_opr_field_' . $field . '_customer']),
+      drupal_render($form[$field]['uc_opr_field_' . $field . '_admin']),
     );
   }
 
-  return theme('table', $header, $rows);
+  return theme('table', array('header' => $header, 'rows' => $rows));
 }
 
-// Return the title options for the user profile tab or a specific one.
+/**
+ * Return the title options for the user profile tab or a specific one.
+ */
 function uc_op_reports_user_tab_titles($title = NULL) {
   $titles = array(
     'ordered_products' => t('Ordered products'),
@@ -180,7 +204,9 @@ function uc_op_reports_user_tab_titles($title = NULL) {
   }
 }
 
-// Return an array of fields available to ordered products reports.
+/**
+ * Return an array of fields available to ordered products reports.
+ */
 function uc_op_reports_fields() {
   return array(
     'order_date' => array(
@@ -199,7 +225,6 @@ function uc_op_reports_fields() {
       'customer' => TRUE,
       'admin' => TRUE,
       'field' => 'order_id',
-
     ),
     'billing_name' => array(
       'title' => t('Order billing name'),
@@ -279,9 +304,12 @@ function uc_op_reports_display($display = 'admin', $uid = NULL, $csv = FALSE) {
   // Loop through all the available ordered products reports fields.
   foreach (uc_op_reports_fields() as $key => $value) {
     // If the field is enabled...
-    if (variable_get('uc_opr_field_'. $key .'_'. $display, $value[$display])) {
+    if (variable_get('uc_opr_field_' . $key . '_' . $display, $value[$display])) {
       // Add it to the header and the query fields.
-      $cell = array('data' => $value['label'], 'weight' => variable_get('uc_opr_field_'. $key .'_weight', $value[$display]));
+      $cell = array(
+        'data' => $value['label'],
+        'weight' => variable_get('uc_opr_field_' . $key . '_weight', $value[$display]),
+      );
 
       if (!empty($value['field'])) {
         $cell['field'] = $value['field'];
@@ -295,7 +323,7 @@ function uc_op_reports_display($display = 'admin', $uid = NULL, $csv = FALSE) {
 
       $fields[] = array(
         'field' => $key,
-        'weight' => variable_get('uc_opr_field_'. $key .'_weight', $value[$display]),
+        'weight' => variable_get('uc_opr_field_' . $key . '_weight', $value[$display]),
       );
     }
   }
@@ -307,79 +335,100 @@ function uc_op_reports_display($display = 'admin', $uid = NULL, $csv = FALSE) {
   $types = array_diff(array_values(variable_get('uc_op_reports_product_classes', array('product'))), array(0));
 
   // Determine extra conditions for the query based on the arguments.
-  $where = '';
+  $where = '1=1';
   $args = array();
 
   if (!empty($_GET['year'])) {
-    $where .= ' AND o.created >= %d AND o.created < %d';
-    $args[] = gmmktime(0, 0, 0, 1, 1, intval($_GET['year']));
-    $args[] = gmmktime(0, 0, 0, 1, 1, intval($_GET['year']) + 1);
+    $where .= ' AND o.created >= :created_start AND o.created < :created_end';
+    $args[':created_start'] = gmmktime(0, 0, 0, 1, 1, intval($_GET['year']));
+    $args[':created_end'] = gmmktime(0, 0, 0, 1, 1, intval($_GET['year']) + 1);
   }
 
   if (!empty($uid)) {
-    $where .= ' AND o.uid = %d';
-    $args[] = $uid;
+    $where .= ' AND o.uid = :uid';
+    $args[':uid'] = $uid;
   }
 
   // Determine the first year for use in the filter form.
-  $result = db_result(db_query("SELECT MIN(o.created) FROM {uc_orders} AS o LEFT JOIN {uc_order_products} AS op ON o.order_id = op.order_id LEFT JOIN {node} AS n ON op.nid = n.nid WHERE n.type IN('". implode("', '", $types) ."')". $where, $args));
+  $result = db_query("SELECT MIN(o.created)
+                      FROM {uc_orders} AS o
+                      LEFT JOIN {uc_order_products} AS op ON o.order_id = op.order_id
+                      LEFT JOIN {node} AS n ON op.nid = n.nid
+                      WHERE n.type IN(:ntypes) AND ($where)", array_merge($args, array(':ntypes' => $types)))->fetchField();
   $first_year = intval(date('Y', $result));
 
+  $query = db_select('uc_orders', 'o')->extend('TableSort');
+  $query->leftJoin('uc_order_products', 'op', 'o.order_id = op.order_id');
+  $query->leftJoin('node', 'n', 'op.nid = n.nid');
+  $query
+    ->fields('o', array('created', 'order_id', 'billing_first_name', 'billing_last_name'))
+    ->fields('op', array('nid', 'title', 'data', 'model', 'qty', 'price'))
+    ->fields('n', array('type'));
+  $query->addExpression('op.qty * op.price', 'total');
+  $query
+    ->condition('n.type', $types, 'IN')
+    ->condition('o.order_status', uc_order_status_list('general', TRUE), 'IN')
+    ->where($where, $args)
+    ->orderByHeader($header);
+
   // Select all the ordered products from the database within range.
   if ($csv || (!empty($_GET['one_page']) && $_GET['one_page'] == 1)) {
-    $result = db_query("SELECT o.created, o.order_id, o.billing_first_name, o.billing_last_name, op.nid, op.title, op.data, op.model, op.qty, op.price, op.qty * op.price AS total, n.type FROM {uc_orders} AS o LEFT JOIN {uc_order_products} AS op ON o.order_id = op.order_id LEFT JOIN {node} AS n ON op.nid = n.nid WHERE n.type IN('". implode("', '", $types) ."') AND o.order_status IN". uc_order_status_list('general', TRUE) . $where . tablesort_sql($header), $args);
+    $results = $query->execute();
   }
   else {
-    $result = pager_query("SELECT o.created, o.order_id, o.billing_first_name, o.billing_last_name, op.nid, op.title, op.data, op.model, op.qty, op.price, op.qty * op.price AS total, n.type FROM {uc_orders} AS o LEFT JOIN {uc_order_products} AS op ON o.order_id = op.order_id LEFT JOIN {node} AS n ON op.nid = n.nid WHERE n.type IN('". implode("', '", $types) ."') AND o.order_status IN". uc_order_status_list('general', TRUE) . $where . tablesort_sql($header), variable_get('uc_reports_table_size', 30), 0, NULL, $args);
+    $results = $query->extend('PagerDefault')->limit(variable_get('uc_reports_table_size', 30))->execute();
   }
 
   // Loop through all the products and add them to the report.
-  while ($data = db_fetch_object($result)) {
+  foreach ($results as $result) {
     $row = array();
 
     foreach ($fields as $key => $value) {
-      switch($value['field']) {
+      switch ($value['field']) {
         case 'order_date':
-          $row[] = date(variable_get('uc_date_format_default', 'm/d/Y'), $data->created);
+          $row[] = date(variable_get('uc_date_format_default', 'm/d/Y'), $result->created);
           break;
 
         case 'order_id':
           if ($csv) {
-            $row[] = $data->order_id;
+            $row[] = $result->order_id;
           }
           else {
             if ($display == 'admin') {
-              $path = 'admin/store/orders/'. $data->order_id;
+              $path = 'admin/store/orders/' . $result->order_id;
             }
             elseif ($display == 'customer') {
-              $path = 'user/'. $uid .'/order/'. $data->order_id;
+              $path = 'user/' . $uid . '/order/' . $result->order_id;
             }
 
-            $row[] = array('data' => l($data->order_id, $path), 'class' => 'order-id');
+            $row[] = array(
+              'data' => l($result->order_id, $path),
+              'class' => array('order-id'),
+            );
           }
           break;
 
         case 'billing_name':
           if ($csv) {
-            $row[] = $data->billing_first_name .' '. $data->billing_last_name;
+            $row[] = $result->billing_first_name . ' ' . $result->billing_last_name;
           }
           else {
-            $row[] = t('@last_name, @first_name', array('@last_name' => $data->billing_last_name, '@first_name' => $data->billing_first_name));
+            $row[] = t('@last_name, @first_name', array('@last_name' => $result->billing_last_name, '@first_name' => $result->billing_first_name));
           }
           break;
 
         case 'product_title':
           if ($csv) {
-            $row[] = $data->title;
+            $row[] = $result->title;
           }
           else {
-            $row[] = l($data->title, 'node/'. $data->nid);
+            $row[] = l($result->title, 'node/' . $result->nid);
           }
           break;
 
         case 'product_description':
-          $product_data = unserialize($data->data);
-          $options = $product_data['attributes'];
+          $product_data = unserialize($result->data);
+          $options = isset($product_data['attributes']) ? $product_data['attributes'] : array();
           $option_rows = array();
           if (module_exists('uc_attribute') && is_array($options)) {
             foreach ($options as $attribute => $option) {
@@ -388,37 +437,40 @@ function uc_op_reports_display($display = 'admin', $uid = NULL, $csv = FALSE) {
           }
 
           if ($csv) {
-            $row[] = implode(' | ', array_merge(array($data->title), $option_rows));
+            $row[] = implode(' | ', array_merge(array($result->title), $option_rows));
           }
           else {
-            $row[] = l($data->title, 'node/'. $data->nid) . theme('item_list', $option_rows, NULL, 'ul', array('class' => 'product-description'));
+            $row[] = l($result->title, 'node/' . $result->nid) . theme('item_list', array('items' => $option_rows, 'title' => NULL, 'type' => 'ul', 'attributes' => array('class' => 'product-description')));
           }
           break;
 
         case 'product_sku':
           if ($csv) {
-            $row[] = $data->model;
+            $row[] = $result->model;
           }
           else {
-            $row[] = check_plain($data->model);
+            $row[] = check_plain($result->model);
           }
           break;
 
         case 'qty':
           if ($csv) {
-            $row[] = $data->qty;
+            $row[] = $result->qty;
           }
           else {
-            $row[] = array('data' => $data->qty, 'class' => 'qty');
+            $row[] = array(
+              'data' => $result->qty,
+              array('class' => 'qty'),
+            );
           }
           break;
 
         case 'sell_price':
-          $row[] = uc_currency_format($data->price);
+          $row[] = uc_currency_format($result->price);
           break;
 
         case 'total':
-          $row[] = uc_currency_format($data->total);
+          $row[] = uc_currency_format($result->total);
           break;
 
         default:
@@ -428,7 +480,7 @@ function uc_op_reports_display($display = 'admin', $uid = NULL, $csv = FALSE) {
     }
 
     // Keep track of the grand total of the rows listed on the report.
-    $grand_total += $data->total;
+    $grand_total += $result->total;
 
     $rows[] = $row;
   }
@@ -438,10 +490,17 @@ function uc_op_reports_display($display = 'admin', $uid = NULL, $csv = FALSE) {
     $rows[] = t('No ordered products found.');
   }
   elseif (empty($rows)) {
-    $rows[] = array(array('data' => t('No ordered products found.'), 'colspan' => count($header)));
+    $rows[] = array(array(
+        'data' => t('No ordered products found.'),
+        'colspan' => count($header),
+      ));
   }
   elseif (!$csv) {
-    $rows[] = array(array('data' => '<span class="grand-total-label">'. t('Grand total: ') .'</span>'. uc_currency_format($grand_total), 'class' => 'grand-total', 'colspan' => count($header)));
+    $rows[] = array(array(
+        'data' => '<span class="grand-total-label">' . t('Grand total: ') . '</span>' . uc_currency_format($grand_total),
+        'class' => array('grand-total'),
+        'colspan' => count($header),
+      ));
   }
 
   // Build the appropriate output based on the arguments.
@@ -450,7 +509,7 @@ function uc_op_reports_display($display = 'admin', $uid = NULL, $csv = FALSE) {
 
     // Add the header to the report.
     foreach ($header as $cell) {
-      $output .= $cell['data'] .',';
+      $output .= $cell['data'] . ',';
     }
     $output .= "\n";
 
@@ -462,13 +521,13 @@ function uc_op_reports_display($display = 'admin', $uid = NULL, $csv = FALSE) {
         $csv_row[] = str_replace(',', '', $cell);
       }
 
-      $output .= implode(',', $csv_row) ."\n";
+      $output .= implode(',', $csv_row) . "\n";
     }
   }
   else {
     $output = '';
 
-    drupal_add_css(drupal_get_path('module', 'uc_op_reports') .'/uc_op_reports.css');
+    drupal_add_css(drupal_get_path('module', 'uc_op_reports') . '/uc_op_reports.css');
 
     // Add the CSV download link on the admin report.
     if ($display == 'admin') {
@@ -476,23 +535,26 @@ function uc_op_reports_display($display = 'admin', $uid = NULL, $csv = FALSE) {
       $query = array();
 
       foreach (array_diff_key($_GET, array('q' => '')) as $key => $value) {
-        $query[] = $key .'='. $value;
+        $query[$key] = $value;
       }
 
-      $output .= theme('item_list', array(l(t('Download the entire current report as a CSV file.'), 'admin/store/reports/ordered-products/csv', array('query' => implode('&', $query)))));
+      $output .= theme('item_list', array('items' => array(l(t('Download the entire current report as a CSV file.'), 'admin/store/reports/ordered-products/csv', array('query' => $query)))));
     }
 
-    $output .= drupal_get_form('uc_op_reports_filter_form', $display, $uid, $first_year)
-             . theme('table', $header, $rows, array('class' => 'op-report')) . theme('pager');
+    // TODO Please change this theme call to use an associative array for the $variables parameter.
+    $form = drupal_get_form('uc_op_reports_filter_form', $display, $uid, $first_year);
+    $output .= drupal_render($form);
+    $output .= theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('class' => array('op-report'))));
+    $output .= theme('pager');
   }
 
   return $output;
 }
 
-// Form to filter the ordered products reports.
-function uc_op_reports_filter_form($form_state, $display, $uid = NULL, $first_year = 2000) {
-  $form = array();
-
+/**
+ * Form to filter the ordered products reports.
+ */
+function uc_op_reports_filter_form($form, $form_state, $display, $uid = NULL, $first_year = 2000) {
   $form['display'] = array(
     '#type' => 'value',
     '#value' => $display,
@@ -522,21 +584,24 @@ function uc_op_reports_filter_form($form_state, $display, $uid = NULL, $first_ye
   $form['op_reports_filter']['submit'] = array(
     '#type' => 'submit',
     '#value' => t('Update report'),
-    '#suffix' => l(t('Reset report'), $display == 'admin' ? 'admin/store/reports/ordered-products' : 'user/'. $uid .'/op-report'),
+    '#suffix' => l(t('Reset report'), $display == 'admin' ? 'admin/store/reports/ordered-products' : 'user/' . $uid . '/op-report'),
   );
 
   return $form;
 }
 
+/**
+ * Submit handler for filter form
+ */
 function uc_op_reports_filter_form_submit($form, &$form_state) {
   $query = array();
 
   // Build the query string for the redirect based on form values.
   if (!empty($form_state['values']['year'])) {
-    $query[] = 'year='. $form_state['values']['year'];
+    $query['year'] = $form_state['values']['year'];
   }
   if ($form_state['values']['one_page']) {
-    $query[] = 'one_page=1';
+    $query['one_page'] = 1;
   }
 
   // Determine the redirect path based on the display type.
@@ -544,13 +609,15 @@ function uc_op_reports_filter_form_submit($form, &$form_state) {
     $path = 'admin/store/reports/ordered-products';
   }
   elseif ($form_state['values']['display'] == 'customer') {
-    $path = 'user/'. $form_state['values']['uid'] .'/op-report';
+    $path = 'user/' . $form_state['values']['uid'] . '/op-report';
   }
 
-  $form_state['redirect'] = array($path, implode('&', $query));
+  $form_state['redirect'] = array($path, array('query' => $query));
 }
 
-// Output the CSV file of the current report for download.
+/**
+ * Output the CSV file of the current report for download.
+ */
 function uc_op_reports_download_csv($display = 'admin', $uid = NULL) {
   // Build the report's CSV output.
   $output = uc_op_reports_display($display, $uid, TRUE);
@@ -563,17 +630,16 @@ function uc_op_reports_download_csv($display = 'admin', $uid = NULL) {
     'Cache-Control: no-cache, must-revalidate',
     'Cache-Control: private',
     'Content-Transfer-Encoding: binary',
-    'Content-Length:'. strlen($output),
+    'Content-Length:' . strlen($output),
     'Content-Disposition: attachment; filename="ordered-products.csv"',
-    'Content-Type: text/csv'
+    'Content-Type: text/csv',
   );
 
   foreach ($http_headers as $header) {
     $header = preg_replace('/\r?\n(?!\t| )/', '', $header);
-    drupal_set_header($header);
+    drupal_add_http_header($header);
   }
 
   print $output;
   exit();
 }
-
