Skip to content

Instantly share code, notes, and snippets.

@dbarbar
Created October 17, 2014 16:47
Show Gist options
  • Save dbarbar/20d65886039f1f29ffcf to your computer and use it in GitHub Desktop.
Save dbarbar/20d65886039f1f29ffcf to your computer and use it in GitHub Desktop.
Extend Litle sustainers via drush scr extend.php
<?php
$extender = new CommerceLitleSustainerExtender();
$extender->run();
class CommerceLitleSustainerExtender {
protected $tableName;
protected $gatewayName;
/**
* @param string $gatewayName
* Gateway name as stored in the donation table.
* @param string $tableName
* Name of the DB table to create and record progress.
*/
public function __construct($gatewayName = 'commerce_litle_cc|commerce_payment_commerce_litle_cc', $tableName = 'commerce_litle_sustainer_extender') {
$this->gatewayName = $gatewayName;
$this->tableName = $tableName;
$this->createTable();
}
/**
* Does the magic.
*
* Iterates through unprocessed and active sustainer serieses to determine
* the last donation in the series and create a new one for the following
* month.
*
* For each series changed, it prints out and saves the result to a table.
*/
public function run() {
$args = array(':gateway' => $this->gatewayName);
$results = db_query($this->getQuery(), $args);
foreach ($results as $row) {
if ($this->recordExists($row->master_did) || !$this->seriesIsActive($row->master_did)) {
continue;
}
$last_charge = _fundraiser_sustainers_get_donations_last_charge($row->master_did);
if (is_object($last_charge) && isset($last_charge->next_charge)) {
$next_charge = strtotime('+1 months', $last_charge->next_charge);
$donation = fundraiser_donation_get_donation($last_charge->did);
fundraiser_sustainers_add_order_to_series($donation, $next_charge);
$new_last_charge = _fundraiser_sustainers_get_donations_last_charge($row->master_did);
$record = array(
'master_did' => $row->master_did,
'old_last_did' => $last_charge->did,
'old_last_charge' => $last_charge->next_charge,
'new_last_did' => $new_last_charge->did,
'new_last_charge' => $new_last_charge->next_charge,
'created' => REQUEST_TIME,
);
$this->printRecord($record);
$this->writeRecord($record);
}
}
}
/**
* Save progress to a table.
*
* @param array $record
* The record to write to the extender table.
*
* @throws Exception
*/
public function writeRecord(array $record) {
$query = db_insert($this->tableName)->fields($record);
$query->execute();
}
/**
* Prints the series record to the screen.
*
* @param array $record
* The record for the sustainer series.
*/
public function printRecord(array $record) {
drush_print('master did: ' . $record['master_did']);
drush_print('old last did: ' . $record['old_last_did']);
drush_print('old last charge: ' . date('c', $record['old_last_charge']));
drush_print('new last did: ' . $record['new_last_did']);
drush_print('new last charge: ' . date('c', $record['new_last_charge']));
drush_print();
}
/**
* Determine if a sustainer series if active and should be updated.
*
* @param int $master_did
* The master did of the sustainer series.
*
* @return bool
* TRUE if the series is active. In other words, the series contains a
* donation that has a NULL gateway response.
*/
public function seriesIsActive($master_did) {
$query = 'SELECT 1 FROM {fundraiser_sustainers} WHERE master_did = :master_did AND gateway_resp IS NULL';
$args = array(':master_did' => $master_did);
return (bool) db_query_range($query, 0, 1, $args)->fetchField();
}
/**
* The main query to get master donation IDs related to the gateway.
*
* @return string
* The query to run.
*/
public function getQuery() {
return "SELECT DISTINCT s.master_did FROM {fundraiser_sustainers} AS s
JOIN {fundraiser_donation} AS d ON s.did = d.did
WHERE s.next_charge > unix_timestamp(now())
AND d.gateway = :gateway;";
}
/**
* See if the sustainer series has already been recorded in the DB table.
*
* @param int $master_did
* Master did to check.
*
* @return bool
* TRUE if the record exists in the table.
*/
public function recordExists($master_did) {
$query = 'SELECT 1 FROM {' . $this->tableName . '} WHERE master_did = :master_did';
$args = array(':master_did' => $master_did);
return (bool) db_query_range($query, 0, 1, $args)->fetchField();
}
/**
* Creates the extender table for logging progress.
*/
public function createTable() {
if (db_table_exists($this->tableName)) {
return;
}
$schema = array(
'description' => 'Log of donations affected by sustainer extending.',
'fields' => array(
'master_did' => array(
'description' => 'Master donation id for the series.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
),
'old_last_did' => array(
'description' => 'The donation ID for the last donation in the series before extending.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
),
'old_last_charge' => array(
'description' => 'The timestamp of the charge date for the old last donation.',
'type' => 'int',
'not null' => TRUE,
),
'new_last_did' => array(
'description' => 'The donation ID for the last donation in the series after extending.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
),
'new_last_charge' => array(
'description' => 'The timestamp of the charge date for the new last donation.',
'type' => 'int',
'not null' => TRUE,
),
'created' => array(
'description' => 'The Unix timestamp when this record was created.',
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
),
'primary key' => array('master_did'),
'indexes' => array(),
);
db_create_table($this->tableName, $schema);
}
/**
* Drops the extender table. Used for test runs.
*/
public function dropTable() {
db_drop_table($this->tableName);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment