View source
<?php
function uc_csv_drush_command() {
$items = array();
$items['uc-csv-export'] = array(
'description' => dt('Run an export and place the exported file in the specified location, or in the same place where the command is called if unspecified.'),
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL,
'arguments' => array(
'output-file' => dt('The relative path and file name to where you want the export file placed.'),
),
'options' => array(
'start' => array(
'description' => dt('The start date of when the export should take place (yyyy-mm-dd)'),
'example-value' => '2014-10-01',
),
'end' => array(
'description' => dt('The end date of when the export should end (yyyy-mm-dd)'),
'example-value' => '2014-10-31',
),
'shipping-address' => array(
'description' => dt('Include the shipping address.'),
),
'billing-address' => array(
'description' => dt('Include the billing address.'),
),
'products' => array(
'description' => dt('Include the products.'),
),
'file-type' => array(
'description' => dt('Export type (defaults to csv).'),
),
'order-by' => array(
'description' => dt('How to order the report (defaults to order-id).'),
'example-value' => 'order-id or last-name',
),
'email' => array(
'description' => dt('Optional: The email address to send the finished report to.'),
'example-value' => '--email=test@testing.com',
),
'examples' => array(
'Export With Dates' => 'uc-csv-export export_file.csv --sd=2014-10-01 --ed=2014-10-31',
'Export With Dates/Email' => 'uc-csv-export --sd=2014-10-01 --ed=2014-10-31--email=test@testing.com',
),
'aliases' => array(
'ucce',
),
),
);
$items['uc-csv-export-report'] = array(
'description' => 'Run an export of a specific report (by id)',
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL,
'arguments' => array(
'of' => dt('The relative path and file name to where you want the export file placed.'),
'id' => dt('The machine ID of the report to be generated'),
),
'options' => array(
'ignore' => array(
'description' => dt('Ignore state of previously exported entries (export all)'),
'example-value' => 'true',
),
),
'examples' => array(
'export report 1' => 'uc-csv-export-report export_file.csv 1',
'export report 1 ignore tracking' => 'uc-csv-export-report export_file.csv 1 --ignore=true',
),
'aliases' => array(
'ucer',
),
);
return $items;
}
function drush_uc_csv_uc_csv_export_validate($of = NULL) {
$email = drush_get_option('email');
if (empty($of) && empty($email)) {
drush_set_error(dt('You did not specify a file name and location for your export file.'));
}
$start_date = drush_get_option('start-date');
$end_date = drush_get_option('end-date');
if (!empty($start_date) && strlen($start_date) != 10) {
drush_set_error(dt('Your start date is not the proper length to be valid.'));
}
if (!empty($end_date) && strlen($end_date) != 10) {
drush_set_error(dt('Your end date is not the proper length to be valid.'));
}
foreach (array(
$start_date,
$end_date,
) as $date) {
list($year, $month, $day) = explode('-', $date);
if (checkdate($month, $day, $year) === FALSE) {
drush_set_error(dt('One of your specified dates was invalid. Please double check your date and try again.'));
}
}
$report = new stdClass();
}
function drush_uc_csv_export($of = NULL) {
$form = $form_state = array();
$order_by = drush_get_option('order-by');
$order_by($order_by != 'order-id' && $order_by != 'last-name') ? 'orderid' : $order_by;
if ($order_by == 'last-name') {
$order_by = 'last_name';
}
elseif ($order_by == 'order-id') {
$order_by = 'orderid';
}
$file_type = drush_get_option('file-type');
$file_type($file_type != 'csv' && $file_type != 'xls') ? 'csv' : $file_type;
$report = new stdClass();
$report->volatile = TRUE;
$report->shipping_address = drush_get_option('shipping-address');
$report->billing_address = drush_get_option('billing-address');
$report->products = drush_get_option('products');
$report->track = FALSE;
$report->start_date = drush_get_option('start-date');
$report->end_date = drush_get_option('end-date');
$report->file_type = $file_type;
$report->order_by = $order_by;
$report->report_name = !empty($of) ? $of : 'exported_report';
$report->email_address = drush_get_option('email');
$report->email_enable = !empty($report->email_address) ? TRUE : FALSE;
$result = db_query("SELECT order_status_id\n FROM {uc_order_statuses}\n ORDER BY weight ASC");
while ($sdata = $result
->fetchObject()) {
$statuses[] = $sdata->order_status_id;
}
$report->statuses = serialize($statuses);
$data = uc_csv_select_report_to_export_submit($form, $form_state, $report);
if ($report->email_enable == FALSE) {
_uc_csv_save_report($report, $data['contents'], $report->report_name);
}
}
function drush_uc_csv_export_report_validate($of = NULL, $id = NULL) {
if (empty($of)) {
drush_set_error(dt('You must specify a file name for your report.'));
return FALSE;
}
if (is_numeric($id)) {
$result = db_query("SELECT * FROM {uc_csv_reports} WHERE rid=:rid;", array(
':rid' => $id,
));
$report = $result
->fetchObject();
if (empty($report->report_name) || !$report->report_name) {
drush_set_error(dt('There is no report with an id of ' . $id));
return FALSE;
}
}
}
function drush_uc_csv_export_report($of, $id = NULL) {
if (empty($id)) {
$options = array();
$result = db_query('SELECT * from {uc_csv_reports} ORDER BY report_name ASC');
while ($rdata = $result
->fetchObject()) {
$options[$rdata->rid] = $rdata->report_name;
}
$choice = drush_choice($options, dt('Which report do you wish to export?'));
$form_state = array();
$form_state['values']['rid'] = $choice;
$data = uc_csv_select_report_to_export_submit(array(), $form_state);
}
else {
$form_state = array();
$form_state['values']['rid'] = $id;
$data = uc_csv_select_report_to_export_submit(array(), $form_state);
}
$of = drush_get_option('of');
if (!empty($of)) {
_uc_csv_save_report($data['report'], $data['contents'], $of);
}
}