You are here

uc_csv.module in Ubercart CSV 7.2

Same filename and directory in other branches
  1. 6.2 uc_csv.module

File

uc_csv.module
View source
<?php

/**
 * Implements hook_menu
 */
function uc_csv_menu() {
  $items = array();
  $items['admin/store/export'] = array(
    'title' => t('CSV Export'),
    'description' => t('Export Settings'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'uc_csv_settings',
    ),
    'access callback' => 'uc_csv_both_perms',
    'type' => MENU_NORMAL_ITEM,
    'weight' => -1,
  );
  $items['admin/store/export/index'] = array(
    'title' => t('Export Report Settings'),
    'description' => t('Set up the reports you want to export.'),
    'page callback' => 'uc_csv_report_grid',
    'access callback' => 'user_access',
    'access arguments' => array(
      'administer ubercart csv export settings',
    ),
    'weight' => -25,
    'type' => MENU_LOCAL_TASK,
  );
  $items['admin/store/export/settings'] = array(
    'title' => t('Settings'),
    'description' => t('Export Settings'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'uc_csv_settings',
    ),
    'access callback' => 'uc_csv_both_perms',
    'type' => MENU_NORMAL_ITEM,
    'weight' => -30,
  );
  $items['admin/store/export/create'] = array(
    'title' => t('Create New Report'),
    'description' => t('Add a new export report.'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'uc_csv_add_new_report',
    ),
    'access callback' => 'user_access',
    'access arguments' => array(
      'administer ubercart csv export settings',
    ),
    'weight' => -20,
    'type' => MENU_LOCAL_TASK,
  );
  $items['admin/store/export/execute'] = array(
    'title' => t('Export Report'),
    'description' => t('Select report to export.'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'uc_csv_select_report_to_export',
    ),
    'access callback' => 'uc_csv_both_perms',
    'weight' => -15,
    'type' => MENU_LOCAL_TASK,
  );
  $items['admin/store/export/reports/update/%uc_csv_report_id'] = array(
    'title' => t('Update Report'),
    'description' => t('Update an existing report'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'uc_csv_add_new_report',
      5,
    ),
    'access callback' => 'user_access',
    'access arguments' => array(
      'administer ubercart csv export settings',
    ),
    'type' => MENU_CALLBACK,
  );
  $items['admin/store/export/reports/delete/%uc_csv_report_id'] = array(
    'title' => t('Delete Report'),
    'description' => t('Delete an existing export report.'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'uc_csv_delete_report',
      5,
    ),
    'access callback' => 'user_access',
    'access arguments' => array(
      'administer ubercart csv export settings',
    ),
    'type' => MENU_CALLBACK,
  );
  return $items;
}

/**
 * Permissions callback
 * 
 * allow access if one of either the permissions are checked for a user
 */
function uc_csv_both_perms() {
  return user_access('administer ubercart csv export settings') || user_access('export ubercart reports as csv files');
}

/**
 * Implements hook_perm
 */
function uc_csv_permission() {
  return array(
    'administer ubercart csv export settings' => array(
      'title' => 'Administer CSV Export Settings',
    ),
    'export ubercart reports as csv files' => array(
      'title' => 'Allow report exports as CSV files',
    ),
  );
}

/**
 * Load arguments from URL
 * 
 * If they are valid return a valid report object with all the trimmings.
 */
function uc_csv_report_id_load($id) {
  if (is_numeric($id)) {
    $result = db_query("SELECT * FROM {uc_csv_reports} WHERE rid=:rid;", array(
      ':rid' => $id,
    ));
    $report = $result
      ->fetchObject();
    if (isset($report->report_name) && $report->report_name) {
      return $report;
    }
    else {
      drupal_set_message('Unable to load the requested report. Please report this error with a snapshot of your uc_csv_reports table', 'error');
      return FALSE;
    }
  }
  else {
    return FALSE;
  }
}

/**
 * Settings form
 *
 * Controls cron functionality
 */
function uc_csv_settings($form, &$form_state) {
  $form['uc_csv_enable_cron'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable cron tasks when cron job is run.'),
    '#description' => t('Check this if you want the reports to export when crons are run on the intervals below.'),
    '#default_value' => variable_get('uc_csv_enable_cron', FALSE),
  );
  $form['uc_csv_report_interval'] = array(
    '#type' => 'select',
    '#title' => t('Report export intervals'),
    '#description' => t('If cron is enabled, the reports will be exported at the following intervals.'),
    '#default_value' => variable_get('uc_csv_report_interval', 86400),
    '#options' => array(
      '3600' => t('Hourly'),
      '10800' => t('Every Three Hours'),
      '21600' => t('Every Six Hours'),
      '43200' => t('Every Twelve Hours'),
      '86400' => t('Once per day'),
    ),
  );
  return system_settings_form($form);
}

/**
 * Implements hook_cron
 */
function uc_csv_cron() {

  // If this is the first time we're running, then we need to init
  if (variable_get('uc_csv_report_last_export', 0) == 0) {
    variable_set('uc_csv_report_last_export', time() - 3600);
  }

  // If we're configured to run the cron, then run only the ones where emails are
  // generated because nobody will be there to catch any of the others
  if (variable_get('uc_csv_enable_cron', FALSE) == 1) {

    // If we are past our interval, run and re-set the time
    if (variable_get('uc_csv_report_last_export', 0) < time() - variable_get('uc_csv_report_interval', 86400)) {
      $result = db_query("SELECT * FROM {uc_csv_reports} WHERE email_enable=:email_enable", array(
        ':email_enable' => 1,
      ));
      while ($report = $result
        ->fetchObject()) {
        $form = array();
        $form_state['values']['rid'] = $report->rid;
        uc_csv_select_report_to_export_submit($form, $form_state, FALSE, NULL);
      }
      variable_set('uc_csv_report_last_export', time());
    }
  }
}

/**
 * Present a list of exported reports
 *
 * Allows for viewing and downloading of exported reports
 */
function uc_csv_report_grid() {
  $i = 0;
  $rows = array();
  $header = array(
    array(
      'data' => t('Export Name'),
    ),
    array(
      'data' => t('Last Exported'),
      'style' => 'text-align: center;',
    ),
    array(
      'data' => t('Last Order ID'),
      'style' => 'text-align: center;',
    ),
    array(
      'data' => t('Actions'),
      'style' => 'text-align: center;',
    ),
  );
  $result = db_query('SELECT * FROM {uc_csv_reports} ORDER BY report_name ASC');
  while ($data = $result
    ->fetchObject()) {

    /* if this is an untrackable report, then it is not necessary for list the last exported */

    /* number since it will always be zero */
    if ($data->track < 1) {
      $data->last_order_id = 'N/A';
    }
    $row = array();
    $row[] = $data->report_name;
    $row[] = array(
      'data' => $data->last_exported,
      'style' => 'text-align: center;',
    );
    $row[] = array(
      'data' => $data->last_order_id,
      'style' => 'text-align: center;',
    );
    $row[] = array(
      'data' => l(t('Edit'), 'admin/store/export/reports/update/' . $data->rid) . ' | ' . l(t('Delete'), 'admin/store/export/reports/delete/' . $data->rid),
      'style' => 'text-align: center;',
    );
    $rows[] = $row;
  }
  if (count($rows) == 0) {
    $rows = array(
      array(
        'data' => array(
          array(
            'align' => 'center',
            'colspan' => 4,
            'data' => t('THERE ARE CURRENTLY NO CONFIGURED EXPORT REPORTS'),
          ),
        ),
      ),
    );
  }
  $output = theme('table', array(
    'header' => $header,
    'rows' => $rows,
  ));
  return $output;
}

/**
 * Put together an export based on the report id
 *
 * This does the heavy lifting of the report generation
 */
function uc_csv_select_report_to_export_submit($form, &$form_state, $report = NULL) {
  global $language;

  // If we are not being passed a drush report object, then look up the object we're using
  if (empty($report)) {
    $result = db_query("SELECT * FROM {uc_csv_reports} WHERE rid=:rid;", array(
      ':rid' => $form_state['values']['rid'],
    ));
    $report = $result
      ->fetchObject();

    // These kinds of reports are not volatile - only ones created on the fly using drush
    $report->volatile = FALSE;
  }

  // Generate the report
  $order = uc_csv_generate_report($report);

  /* update the table with our latest export time and order_id so we don't re-export things on */

  /* the next go-around - but only do this if it is a trackable report */
  if (!empty($report->track) && empty($report->volatile)) {

    /* get the max order id for storage */
    $result = db_query('SELECT MAX(order_id) AS order_id FROM {uc_orders}');
    $odata = $result
      ->fetchObject();
    $max_order = $odata->order_id;

    /* insert the max id into our report table */
    $result = db_query("UPDATE {uc_csv_reports} \n                        SET last_order_id=:last_order_id, last_exported=:last_exported\n                        WHERE rid=:rid;", array(
      ':last_order_id' => $max_order,
      ':last_exported' => date('Y-m-d H:i:s', time()),
      ':rid' => $form_state['values']['rid'],
    ));
  }

  /* name the report file name after the given name for this report, just make it filename friendly */
  $file_name = uc_csv_clean_filename($report->report_name);
  $file_name = $report->file_type ? $file_name . '.csv' : $file_name . ".xls";

  // If we are emailing the report, we do not need to save a copy of the report.
  if (!empty($report->email_enable)) {
    $contents = _uc_csv_prepare_report($report, $order);

    // Send the email to the person configured in the report
    $from = variable_get('site_mail', ini_get('sendmail_from'));
    $site_name = variable_get('site_name');
    $boundary = md5(time());
    $mime_boundary = "==Multipart_Boundary_x{$boundary}x";
    $email_content = theme('uc_csv_attached_report', array(
      'report' => $report,
      'boundary' => $mime_boundary,
      'contents' => $contents,
      'filename' => $file_name,
      'filesize' => strlen($contents),
    ));
    $params = array(
      'body' => $email_content,
      'subject' => t('Ubercart Export Report: ') . $report->report_name . ' on ' . date('m/d/Y'),
      'boundary' => $mime_boundary,
    );
    $result = drupal_mail('uc_csv', 'uc_csv_key', $report->email_address, $language, $params, $from, TRUE);

    // If drush and cron are not running, then we are manually exporting and need to set
    // a drupal message to let the user know we ran successfully
    if (!drupal_is_cli() || !function_exists('drush_main')) {
      if (!_uc_csv_is_cron_running()) {
        drupal_set_message(t('Ubercart Export Report: ') . $report->report_name . ' on ' . date('m/d/Y') . ' ' . t('sent'));
      }
    }
    else {
      drush_log(dt('Ubercart Export Report: ') . $report->report_name . ' on ' . date('m/d/Y') . ' ' . dt('sent'), 'ok');
    }
  }
  else {
    $contents = _uc_csv_prepare_report($report, $order);

    // If we are coming from a drush command, we need to return the contents of the file
    // to the calling function for handling
    if (drupal_is_cli() && function_exists('drush_main')) {
      return array(
        'contents' => $contents,
        'report' => $report,
      );
    }

    /* if we are reporting as a csv, then use standard csv export. to do multi-byte reports, then use */

    /* the Microsoft excel XML export method */
    if ($report->file_type == 'csv') {

      /* output the file so we download */
      header('Content-Type: text/x-csv');
      header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT');
      header('Content-Disposition: inline; filename="' . $file_name . '"');
      header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
      header('Pragma: public');
      echo $contents;
      exit;
    }
    else {
      $encoding = $contents['encoding'];
      $contents = $contents['contents'];
      header("Content-Type: application/vnd.ms-excel; charset=" . $encoding);
      header('Content-Disposition: inline; filename="' . $file_name . '"');
      echo $contents;
      exit;
    }
  }
}

/**
 * Prepare the report
 * 
 * Prepare the report regardless if we're preparing a csv or XLS report
 */
function _uc_csv_prepare_report($report, $order) {
  if ($report->file_type == 'csv') {
    $headers = implode(',', $order['headers']) . "\n";
    $source = NULL;
    foreach ($order['order_data'] as $order) {
      $csv_array = array();
      foreach ($order as $cell) {
        $csv_array[] = $cell;
      }

      // We need to make sure the lines are properly escaped.
      // from comments at: http://php.net/manual/en/function.fputcsv.php
      $csv = fopen('php://temp/maxmemory:' . 1 * 1024 * 1024, 'r+');
      fputcsv($csv, $csv_array);
      rewind($csv);
      $source = stream_get_contents($csv);
      fclose($csv);
      $source .= "\n";
    }
    return $headers . $source;
  }
  elseif ($report->file_type == 'xls') {
    module_load_include('inc', 'uc_csv', 'inc/excel-export');

    // generate file (constructor parameters are optional)
    $xls = new Excel_XML('UTF-8', FALSE, $report->report_name);
    $data = array(
      $order['headers'],
    );
    foreach ($order['order_data'] as $line) {
      $set = array();
      foreach ($line as $key => $value) {
        $set[] = $value;
      }
      $data[] = $set;
    }
    $xls
      ->addArray($data);
    $contents = $xls->generateXML;
    return $contents;
  }
  return FALSE;
}

/**
 * Add new reports
 *
 * Interface for adding a new report or editing an existing one
 */
function uc_csv_add_new_report($form, &$form_state) {
  $report = $form_state['build_info']['args'];

  /* if we are creating a new report, then the args will be empty and we will need to initialize */

  /* our object variables */
  if ($report == array()) {
    $report = new stdClass();
    $report->rid = $report->report_name = $report->shipping_address = NULL;
    $report->billing_address = $report->products = $report->orderby = NULL;
    $report->track = $report->file_type = $report->email_enable = $report->email_address = NULL;
  }
  else {
    $report = $report[0];
  }

  // set up our form of properties for the report being sought.
  $form = array();
  $form['rid'] = array(
    '#type' => 'hidden',
    '#default_value' => $report->rid,
  );
  $form['export_options'] = array(
    '#type' => 'fieldset',
    '#title' => t('Export Options'),
    '#weight' => 2,
    '#collapsible' => TRUE,
  );
  $form['status_options'] = array(
    '#type' => 'fieldset',
    '#title' => t('Order Statuses to Export'),
    '#weight' => 3,
    '#collapsible' => TRUE,
  );
  $form['email'] = array(
    '#type' => 'fieldset',
    '#title' => t('Email Options'),
    '#weight' => 4,
    '#collapsible' => TRUE,
  );
  $form['other'] = array(
    '#type' => 'fieldset',
    '#title' => t('Report Email Options'),
    '#weight' => 4,
    '#collapsible' => TRUE,
  );
  $form['report_name'] = array(
    '#type' => 'textfield',
    '#title' => t('Report Name'),
    '#default_value' => $report->report_name,
    '#max_length' => 96,
    '#required' => TRUE,
    '#weight' => 1,
  );
  $default_options = array();
  if ($report->shipping_address) {
    $default_options[] = 'shipping';
  }
  if ($report->billing_address) {
    $default_options[] = 'billing';
  }
  if ($report->products) {
    $default_options[] = 'products';
  }
  $form['export_options']['options'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Export Options'),
    '#default_value' => $default_options,
    '#options' => array(
      'shipping' => t('Shipping Address'),
      'billing' => t('Billing Address'),
      'products' => t('Products'),
    ),
    '#description' => t('Select the information you want exported in your report'),
  );

  /* get a list of the order statuses available/created */
  $statuses = array();
  if (isset($report->statuses) && $report->statuses) {
    $default_statuses = unserialize($report->statuses);
  }
  else {
    $default_statuses = array();
  }
  $result = db_query("SELECT order_status_id,title,state\n                      FROM {uc_order_statuses}\n                      ORDER BY weight ASC");
  while ($sdata = $result
    ->fetchObject()) {
    $statuses[$sdata->order_status_id] = $sdata->title;
  }
  $form['status_options']['statuses'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Order Statuses to Export'),
    '#default_value' => $default_statuses,
    '#options' => $statuses,
    '#description' => t('Select the order statuses to be exported in this report.'),
  );
  $form['email']['email_enable'] = array(
    '#type' => 'checkbox',
    '#title' => t('Check if you would like to have generated reports emailed as opposed to downloaded on generation.'),
    '#description' => t('This is especially useful for when you have cron or drush generated emails that you do not want to have to deal with retrieving. Emails will be sent to the email address configured in this report.'),
    '#default_value' => $report->email_enable,
  );
  $form['email']['email_address'] = array(
    '#type' => 'textfield',
    '#title' => t('Email address where you wish the report to be send to.'),
    '#default_value' => $report->email_address,
  );
  $form['other']['orderby'] = array(
    '#type' => 'select',
    '#title' => t('Order By'),
    '#default_value' => $report->orderby,
    '#options' => array(
      'order_id' => 'Order ID',
      'last_name' => 'Customer Last Name',
    ),
    '#description' => t('How you would like your report sorted.'),
  );
  $form['other']['track'] = array(
    '#type' => 'select',
    '#title' => t('Track Last Exported Orders'),
    '#default_value' => $report->track,
    '#options' => array(
      '0' => 'No',
      '1' => 'Yes',
    ),
    '#description' => t('Select Yes if you would like for each export to be progressive instead of all inclusive.'),
  );
  $form['other']['file_type'] = array(
    '#type' => 'select',
    '#title' => t('Type of File To Export'),
    '#default_value' => $report->file_type,
    '#options' => array(
      'csv' => 'CSV (Comma Separated Values)',
      'xls' => 'Microsoft Excel XML Export',
    ),
    '#description' => t('The type of file to be exported. Use Excel if trying to export data with special characters (foreign language) in it.'),
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Update Report Options'),
    '#weight' => 5,
  );
  return $form;
}

/** 
 * Submit handler for creating a new report
 *
 * take our form data and submit a new report to the database
 */
function uc_csv_add_new_report_submit($form, $form_state) {
  $shipping = $form_state['values']['options']['shipping'] ? 1 : 0;
  $billing = $form_state['values']['options']['billing'] ? 1 : 0;
  $products = $form_state['values']['options']['products'] ? 1 : 0;

  // This is data that is applicable whether we are updating or creatting a new record.
  $rdata = array(
    'report_name' => $form_state['values']['report_name'],
    'shipping_address' => $shipping,
    'billing_address' => $billing,
    'products' => $products,
    'orderby' => $form_state['values']['orderby'],
    'file_type' => $form_state['values']['file_type'],
    'statuses' => serialize($form_state['values']['statuses']),
    'track' => $form_state['values']['track'],
    'email_enable' => $form_state['values']['email_enable'],
    'email_address' => $form_state['values']['email_address'],
  );

  // If we are creating a new report, then we want to make sure we initizlize our last
  // exported date and order id for tracking purposes
  if (empty($form_state['values']['rid'])) {
    $extra = array(
      'last_exported' => date('Y-m-d H:i:s'),
      'last_order_id' => 0,
    );
    $wdata = array_merge($rdata, $extra);
    $result = drupal_write_record('uc_csv_reports', $wdata);
  }
  else {

    // Otherwise, we have a report, so populate the report id
    $extra = array(
      'rid' => $form_state['values']['rid'],
    );
    $wdata = array_merge($rdata, $extra);
    $result = drupal_write_record('uc_csv_reports', $wdata, 'rid');
  }
  if ($result == TRUE) {
    drupal_set_message("Report successfully saved");
  }
  else {
    drupal_set_message("Your report could not be saved. Please submit a bug report", 'error');
  }
  drupal_goto('admin/store/export/reports');
}

/**
 * Select Report To Export
 *
 * This is a rudimentary UI to export the latest data
 */
function uc_csv_select_report_to_export($form_state) {
  $options = array();
  $reports = FALSE;
  $result = db_query('SELECT * FROM {uc_csv_reports} ORDER BY report_name ASC;');
  while ($ldata = $result
    ->fetchObject()) {
    $reports = TRUE;
    $options[$ldata->rid] = $ldata->report_name;
  }
  if ($reports == TRUE) {
    $form = array();
    $form['rid'] = array(
      '#type' => 'select',
      '#title' => 'Select the report that you would like export',
      '#options' => $options,
    );
    $form['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Export Selected Report'),
    );
    return $form;
  }
  else {
    drupal_set_message(t('Unable to get list of available reports.'));
    drupal_goto('admin/store/export/reports');
  }
}

/**
 * Delete a report
 *
 * Useful for removing reports that are no longer needed
 */
function uc_csv_delete_report($form_state, $report) {
  $form = array();
  $form['rid'] = array(
    '#type' => 'hidden',
    '#value' => $report->rid,
  );
  $form['report_name'] = array(
    '#type' => 'hidden',
    '#value' => $report->report_name,
  );
  return confirm_form($form, t('Are you sure you wish to delete the %report export report?', array(
    '%report' => $report->report_name,
  )), 'admin/store/export/index', t('This action cannot be undone.'), t('Delete'), t('Cancel'));
}

/**
 * Submit Handler for Delete Confirm
 *
 * Performs the act of deleting the report
 */
function uc_csv_delete_report_submit($form, $form_state) {
  db_query("DELETE FROM {uc_csv_reports} WHERE rid=:rid", array(
    ':rid' => (int) $form_state['values']['rid'],
  ));
  drupal_set_message(t('%report report successfully deleted', array(
    '%report' => $form_state['values']['report_name'],
  )));
  drupal_goto('admin/store/export/index');
}

/**
 * Clean the file name of the report
 *
 * Prevents spaces and oddball characters from appearing in report filenames
 */
function uc_csv_clean_filename($str, $replace = array(), $delimiter = '-') {
  setlocale(LC_ALL, 'en_US.UTF8');
  if (!empty($replace)) {
    $str = str_replace((array) $replace, ' ', $str);
  }
  $clean = iconv('UTF-8', 'ASCII//TRANSLIT', $str);
  $clean = preg_replace("/[^a-zA-Z0-9\\/_|+ -]/", '', $clean);
  $clean = strtolower(trim($clean, '-'));
  $clean = preg_replace("/[\\/_|+ -]+/", $delimiter, $clean);
  return $clean;
}
function uc_csv_generate_report($report) {

  // If we have specified that only certain status levels are eligible for export, then
  // assign them here so we know what they are later
  $statuses = array();
  foreach (unserialize($report->statuses) as $key => $value) {
    if ($value != '0') {
      $statuses[] = $value;
    }
  }
  $selects = array();
  $country = array();
  $zone = array();
  $headers = array(
    'order_id',
    'last_name',
    'first_name',
    'email_address',
    'order_total',
    'product_count',
  );

  /* cache the list of countries and states in memory so we don't have to do repeated lookups */
  $result = db_query("SELECT country_id,country_name FROM {uc_countries}");
  while ($sdata = $result
    ->fetchObject()) {
    $country[$sdata->country_id] = $sdata->country_name;
  }
  $result = db_query("SELECT zone_id,zone_code FROM {uc_zones}");
  $date_range = NULL;
  if (isset($report->start_date) && $report->start_date) {
    $start_date = strtotime($report->start_date);
    $date_range = 'AND created >= ' . $start_date . ' ';
  }
  if (isset($report->end_date) && $report->end_date) {
    $end_date = strtotime($report->end_date);
    if (!empty($date_range)) {
      $date_range .= 'AND created <= ' . $end_date . ' ';
    }
    else {
      $date_range = 'AND created <= ' . $end_date . ' ';
    }
  }
  while ($sdata = $result
    ->fetchObject()) {
    $zone[$sdata->zone_id] = $sdata->zone_code;
  }
  if ($report->shipping_address == 1) {
    $selects[] = 'o.delivery_first_name,o.delivery_last_name,o.delivery_phone,o.delivery_company,o.delivery_street1,o.delivery_street2,o.delivery_city,o.delivery_zone,o.delivery_postal_code,o.delivery_country';
    $headers = array_merge($headers, array(
      'shipping_first_name',
      'shipping_last_name',
      'shipping_phone',
      'shipping_company',
      'shipping_street1',
      'shipping_street2',
      'shipping_city',
      'shipping_state',
      'shipping_zipcode',
      'shipping_country',
    ));
  }
  if ($report->billing_address == 1) {
    $selects[] = 'o.billing_phone,o.billing_company,o.billing_street1,o.billing_street2,o.billing_city,o.billing_zone,o.billing_postal_code,o.billing_country';
    $headers = array_merge($headers, array(
      'billing_phone',
      'billing_company',
      'billing_street1',
      'billing_street2',
      'billing_city',
      'billing_state',
      'billing_zipcode',
      'billing_country',
    ));
  }
  if ($report->products == 1) {
    $headers = array_merge($headers, array(
      'products',
      'tax',
      'shipping',
      'weight',
    ));
  }
  if ($report->orderby == 'orderid') {
    $orderby = 'o.order_id ASC';
  }
  elseif ($report->orderby == 'last_name') {
    $orderby = 'o.billing_last_name ASC';
  }
  else {
    $orderby = 'o.order_id ASC, o.billing_last_name ASC';
  }
  if (count($selects) > 0) {
    $sel = ',' . join(',', $selects);
  }
  else {
    $sel = NULL;
  }

  /* reports may have been trackable at one time and then switched to non-trackable. as such we */

  /* need to track the state of the report. if it is not trackable, then set last_order_id to */

  /* 0 so that we get all the orders */
  if (isset($report->track) && $report->track < 1) {
    $report->last_order_id = 0;
  }
  $complete_order = array();
  $result = db_query("SELECT o.order_id, o.billing_last_name, o.billing_first_name, o.primary_email,o.order_total, o.product_count" . $sel . "\n                      FROM {uc_orders} o\n                      WHERE o.order_id > :order_id && o.order_status IN ('" . join("','", $statuses) . "') " . $date_range . "\n                      ORDER BY " . $orderby, array(
    ':order_id' => $report->last_order_id,
  ));
  while ($order = $result
    ->fetchObject()) {

    /* only deal with the billing stuff if it was selected as part of our order configuration */
    if ($report->shipping_address == 1) {
      $order->delivery_zone = $zone[$order->delivery_zone];
      $order->delivery_country = $country[$order->delivery_country];
    }

    /* only deal with the billing stuff if it was selected as part of our order configuration */
    if ($report->billing_address == 1) {
      $order->billing_zone = $zone[$order->billing_zone];
      $order->billing_country = $country[$order->billing_country];
    }
    if ($report->products == 1) {
      $product = NULL;
      $calc_attribute = array();
      $total_weight = 0;
      $presults = db_query("SELECT model, title, qty, price, data, weight\n                            FROM {uc_order_products} \n                            WHERE order_id=:order_id", array(
        ':order_id' => $order->order_id,
      ));
      while ($pdata = $presults
        ->fetchObject()) {
        $total_weight += $pdata->weight;
        $data = unserialize($pdata->data);
        if (isset($data['attributes']) && is_array($data['attributes'])) {
          foreach ($data['attributes'] as $key => $attribute) {
            foreach ($attribute as $display_attribute) {
              $calc_attribute[] = $key . ": " . $display_attribute;
            }
          }
        }
        if (count($calc_attribute) > 0) {
          $title = $pdata->title . " - " . join(',', $calc_attribute);
        }
        else {
          $title = $pdata->title;
        }
        $product .= $pdata->qty . ' - ' . $title . ' (' . $pdata->model . '): $' . number_format($pdata->price, 2) . 'ea.';
      }
      $order->products = $product;

      /* now we need to get the shipping and sales tax data for this order */

      /* we will do this in two queries for now - first we do tax */
      $stResult = db_query("SELECT amount AS sales_tax\n                            FROM {uc_order_line_items}\n                            WHERE order_id = :order_id\n                              AND type=:type", array(
        ':order_id' => $order->order_id,
        ':type' => 'tax',
      ));
      $stdata = $stResult
        ->fetchObject();
      $order->sales_tax = isset($stdata->sales_tax) ? $stdata->sales_tax : "N/A";

      /* use the same sort of query to get the shipping amount */
      $stResult = db_query("SELECT amount AS shipping\n                            FROM {uc_order_line_items}\n                            WHERE order_id = :order_id\n                              AND type=:type", array(
        ':order_id' => $order->order_id,
        ':type' => 'shipping',
      ));
      $stdata = $stResult
        ->fetchObject();
      $order->shipping = isset($stdata->shipping) ? $stdata->shipping : "N/A";

      // Assign our total weight
      $order->weight = $total_weight;
    }
    $complete_order[] = (array) $order;
  }
  return array(
    'order_data' => $complete_order,
    'headers' => $headers,
  );
}

/**
 * Save the report
 *
 * Take the contents and place into a file. Make the uri to the file part of the report
 * object so it can be saved when completed
 */
function _uc_csv_save_report(&$report, $contents, $file_name) {
  if (drupal_is_cli() && function_exists('drush_main')) {
    file_put_contents($file_name, $contents);
    $report->filepath = getcwd() . '/' . $file_name;
    drush_log('File successfully saved.', 'ok');
  }
  else {
    if (!file_exists(drupal_realpath('public://uc-csv-reports/'))) {
      drupal_mkdir('public://uc-csv-reports/');
    }
    $destination = 'public://uc-csv-reports/' . $file_name;
    $filepath = file_unmanaged_save_data($contents, $destination, FILE_EXISTS_REPLACE);
    if ($filepath != FALSE) {
      $report->filepath = $filepath;
    }
    else {
      $report->filepath = FALSE;
      drupal_set_message('File could not be saved or assigned.', 'error');
    }
  }
}

/**
 * Implements hook_mail
 */
function uc_csv_mail($key, &$message, $params) {
  if ($key == 'uc_csv_key') {
    $headers = array(
      'MIME-Version' => '1.0',
      'Content-Type' => 'multipart/mixed; boundary="' . $params['boundary'] . '"',
      'Content-Transfer-Encoding' => '8Bit',
      'X-Mailer' => 'Drupal',
    );
    foreach ($headers as $key => $value) {
      $message['headers'][$key] = $value;
    }
    $message['subject'] = t($params['subject']);
    $message['body'][] = $params['body'];
  }
}

/**
 * Implements hook_theme
 */
function uc_csv_theme($existing, $type, $theme, $path) {
  return array(
    'uc_csv_attached_report' => array(
      'template' => 'uc-csv-attached-report',
      'arguments' => array(
        'report' => NULL,
        'boundary' => NULL,
        'contents' => NULL,
        'filename' => NULL,
        'filesize' => NULL,
      ),
    ),
    'uc_csv_help_page' => array(
      'template' => 'uc-csv-help-page',
      'arguments' => array(
        'create_new_report' => NULL,
      ),
    ),
  );
}

/**
 * Helper to see if cron is running 
 */
function _uc_csv_is_cron_running() {
  if (!lock_acquire('cron', 240.0)) {
    return TRUE;
  }
  else {
    lock_release('cron');
    return FALSE;
  }
}

/**
 * Implements hook_help
 */
function uc_csv_help($path, $args) {
  switch ($path) {
    case 'admin/help#uc_csv':

      // Define variables that will be used in our help file but only if we are going
      // to the main help page.
      $create_new_report = l('Create New Report', 'admin/store/export/create');
      return theme('uc_csv_help_page', array(
        'create_new_report' => $create_new_report,
      ));
      break;
  }
}

Functions

Namesort descending Description
uc_csv_add_new_report Add new reports
uc_csv_add_new_report_submit Submit handler for creating a new report
uc_csv_both_perms Permissions callback
uc_csv_clean_filename Clean the file name of the report
uc_csv_cron Implements hook_cron
uc_csv_delete_report Delete a report
uc_csv_delete_report_submit Submit Handler for Delete Confirm
uc_csv_generate_report
uc_csv_help Implements hook_help
uc_csv_mail Implements hook_mail
uc_csv_menu Implements hook_menu
uc_csv_permission Implements hook_perm
uc_csv_report_grid Present a list of exported reports
uc_csv_report_id_load Load arguments from URL
uc_csv_select_report_to_export Select Report To Export
uc_csv_select_report_to_export_submit Put together an export based on the report id
uc_csv_settings Settings form
uc_csv_theme Implements hook_theme
_uc_csv_is_cron_running Helper to see if cron is running
_uc_csv_prepare_report Prepare the report
_uc_csv_save_report Save the report