You are here

uc_views_bulk_operations.module in Ubercart Views 6.2

Same filename and directory in other branches
  1. 6.3 uc_views_bulk_operations/uc_views_bulk_operations.module

File

uc_views_bulk_operations/uc_views_bulk_operations.module
View source
<?php

/**
 * Implementation of hook_views_api().
 */
function uc_views_bulk_operations_views_api() {
  return array(
    'api' => '2.0',
    'path' => drupal_get_path('module', 'uc_views_bulk_operations') . '/views',
  );
}

/**
 * Implementation of hook_views_bulk_operations_object_info()
 *
 * Hook used by VBO to be able to handle different objects as does Views 2.
 *
 * The array returned for each object type contains:
 *  'type' => the object type name, should be the same as 'type' field in actions. 
 *  'base_table' => the Views 2 table name corresponding to that object type, should be the same as the $view->base_table attribute.
 *  'load' => a function that given a $id, loads the corresponding object
 *  'title' => an attribute on the object that returns a human-friendly identifier of the object.
 */
function uc_views_bulk_operations_views_bulk_operations_object_info() {
  return array(
    'order' => array(
      'type' => 'order',
      'base_table' => 'uc_orders',
      'load' => 'uc_order_load',
      'title' => 'order_id',
    ),
  );
}

/*
 * Implementation of hook_node_operations(). (rather, hook_order_operations, which is based off the other)
 */
function uc_views_bulk_operations_order_operations() {
  return array(
    'process_orders' => array(
      'label' => 'Move Orders to next State',
      'callback' => 'uc_views_bulk_operations_orders_process_orders',
      'disabled' => TRUE,
    ),
    'print_orders' => array(
      'label' => 'Print Orders',
      'callback' => 'uc_views_bulk_operations_orders_print_orders',
      'disabled' => TRUE,
    ),
  );
}

/**
 * Processes selected orders (moves them to next state in CA)
 */
function uc_views_bulk_operations_orders_process_orders($order_ids) {
  $states = uc_order_status_list();
  foreach ($order_ids as $order_id) {
    $order = uc_order_load($order_id);
    foreach ($states as $key => $state) {
      if ($state['id'] == $order->order_status) {

        // state matches current one, so grab the next one if it exists.
        if ($states[$key + 1]) {
          $new_status = $states[$key + 1]['id'];
        }
        else {

          // there is no "next state", so we're at the end. return error msg.
          $new_status = NULL;
          drupal_set_message(t('Order #' . $order_id . ' already at final state. Unable to update.'), 'error', FALSE);
        }
      }
    }

    // now we need to update each order accordingly.
    if ($new_status) {
      uc_order_update_status($order_id, $new_status);
      drupal_set_message(t('Order #' . $order_id . ' updated to status: ') . $new_status);
    }
  }
}

/**
 * Display the invoice for given order
 */
function _uc_order_bulkops_view_invoice($order_id) {
  $order = uc_order_load($order_id);
  $output = uc_order_load_invoice($order, 'print', variable_get('uc_cust_order_invoice_template', 'customer'));
  return $output;
}

/**
 * Prints Bulk Forders
 * @param array $orders
 */
function uc_views_bulk_operations_orders_print_orders($orders) {
  $args = implode(',', $orders);
  foreach ($orders as $order_id) {
    $output .= _uc_order_bulkops_view_invoice($order_id);
    $output .= '<hr style="page-break-after: always;"/>';
  }
  $output .= '<div align="right" id="_toolbar_to_hide" style="margin-top: 1em; margin-right: 1em;"><input type="button" value="' . t('Print invoice') . '" onclick="$(\'input\').hide(); window.print();" /> ' . '<input type="button" value="' . t('Back') . '" onclick="history.go(-1);" /></div>';
  die(print $output);
}