class PaymentEntityController in Payment 7
Entity API controller for payment entities.
Hierarchy
- class \DrupalDefaultEntityController implements DrupalEntityControllerInterface
- class \EntityAPIController implements EntityAPIControllerRevisionableInterface
- class \PaymentEntityController
- class \EntityAPIController implements EntityAPIControllerRevisionableInterface
Expanded class hierarchy of PaymentEntityController
1 string reference to 'PaymentEntityController'
- payment_entity_info in ./
payment.module - Implements hook_entity_info().
File
- ./
payment.classes.inc, line 317 - The API and related functions for executing and managing payments.
View source
class PaymentEntityController extends EntityAPIController {
/**
* {@inheritdoc}
*/
function load($ids = array(), $conditions = array()) {
$entities = parent::load($ids, $conditions);
foreach ($entities as $payment) {
// Cast non-string scalars to their original types, because some backends
// store/return all variables as strings.
$payment->pid = (int) $payment->pid;
$payment->uid = (int) $payment->uid;
}
return $entities;
}
/**
* {@inheritdoc}
*/
function attachLoad(&$queried_entities, $revision_id = FALSE) {
$pids = array_keys($queried_entities);
// Load the payments's payment methods.
$pmids = array();
foreach ($queried_entities as $payment) {
$pmids[] = $payment->pmid;
}
$methods = entity_load('payment_method', $pmids);
// Load line items.
$result = db_select('payment_line_item')
->fields('payment_line_item')
->condition('pid', $pids)
->execute();
$line_items = array();
while ($line_item_data = $result
->fetchAssoc('name', PDO::FETCH_ASSOC)) {
$line_item_data['description_arguments'] = unserialize($line_item_data['description_arguments']);
$line_item_data['amount'] = (double) $line_item_data['amount'];
$line_item_data['tax_rate'] = (double) $line_item_data['tax_rate'];
$line_item_data['quantity'] = (double) $line_item_data['quantity'];
$line_items[$line_item_data['pid']][$line_item_data['name']] = new PaymentLineItem($line_item_data);
}
// Load the payments's status items.
$result = db_select('payment_status_item')
->fields('payment_status_item')
->condition('pid', $pids)
->orderBy('psiid')
->execute();
$status_items = array();
while ($data = $result
->fetchObject()) {
$status_item = new PaymentStatusItem($data->status, $data->created, $data->pid, $data->psiid);
$status_items[$status_item->pid][] = $status_item;
}
// Add all data to the payments.
foreach ($queried_entities as $payment) {
$payment->statuses = $status_items[$payment->pid];
$payment->line_items = isset($line_items[$payment->pid]) ? $line_items[$payment->pid] : array();
$payment->method = $methods[$payment->pmid];
unset($payment->pmid);
}
parent::attachLoad($queried_entities, $revision_id);
}
/**
* {@inheritdoc}
*/
function save($entity, DatabaseTransaction $transaction = NULL) {
$payment = $entity;
// Save the payment.
$payment->pmid = $payment->method->pmid;
$return = parent::save($payment, $transaction);
unset($payment->pmid);
return $return;
}
/**
* Save the line items and payment status items if needed.
*
* @param Payment $payment
* The payment object.
*/
function saveAttachedData($payment) {
// Save line items.
foreach ($payment->line_items as $line_item) {
$schema = drupal_get_schema('payment_line_item');
$fields = $schema['fields'];
$data = array();
foreach ($fields as $column => $spec) {
if (property_exists($line_item, $column)) {
$data[$column] = $line_item->{$column};
if (!empty($spec['serialize'])) {
$data[$column] = serialize($data[$column]);
}
}
}
unset($data['pid']);
unset($data['name']);
$data['amount_total'] = $line_item->amount * $line_item->quantity * ($line_item->tax_rate + 1);
db_merge('payment_line_item')
->key(array(
'name' => $line_item->name,
'pid' => $payment->pid,
))
->fields($data)
->execute();
}
// Save the payment's status items.
$update = empty(reset($payment->statuses)->psiid) || empty(end($payment->statuses)->psiid);
foreach ($payment->statuses as $status_item) {
// Statuses cannot be edited, so only save the ones without a PSIID set.
if (!$status_item->psiid) {
$status_item->pid = $payment->pid;
drupal_write_record('payment_status_item', $status_item);
}
}
if ($update) {
$payment->psiid_first = reset($payment->statuses)->psiid;
$payment->psiid_last = end($payment->statuses)->psiid;
$query = db_update('payment')
->condition('pid', $payment->pid)
->fields(array(
'psiid_first' => reset($payment->statuses)->psiid,
'psiid_last' => end($payment->statuses)->psiid,
));
$query
->execute();
}
}
/**
* {@inheritdoc}
*/
function view($entities, $view_mode = 'full', $langcode = NULL, $page = NULL) {
$build = parent::view($entities, $view_mode, $langcode, $page);
$type = 'payment';
foreach ($build['payment'] as &$payment_build) {
$payment = $payment_build['#entity'];
$payment_build['payment_line_items'] = payment_line_items($payment);
$payment_build['payment_status_items'] = payment_status_items($payment);
$payment_build['payment_method'] = array(
'#type' => 'item',
'#title' => t('Payment method'),
'#markup' => check_plain($payment->method->title_generic),
);
drupal_alter(array(
'payment_view',
'entity_view',
), $payment_build, $type);
}
return $build;
}
/**
* {@inheritdoc}
*/
function delete($ids, DatabaseTransaction $transaction = NULL) {
parent::delete($ids, $transaction);
db_delete('payment_line_item')
->condition('pid', $ids)
->execute();
db_delete('payment_status_item')
->condition('pid', $ids)
->execute();
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DrupalDefaultEntityController:: |
protected | property | Whether this entity type should use the static cache. | |
DrupalDefaultEntityController:: |
protected | property | Static cache of entities, keyed by entity ID. | |
DrupalDefaultEntityController:: |
protected | property | Array of information about the entity. | |
DrupalDefaultEntityController:: |
protected | property | Entity type for this controller instance. | |
DrupalDefaultEntityController:: |
protected | property | Additional arguments to pass to hook_TYPE_load(). | |
DrupalDefaultEntityController:: |
protected | property | Name of the entity's ID field in the entity database table. | |
DrupalDefaultEntityController:: |
protected | property | Name of entity's revision database table field, if it supports revisions. | |
DrupalDefaultEntityController:: |
protected | property | The table that stores revisions, if the entity supports revisions. | |
DrupalDefaultEntityController:: |
protected | function | Gets entities from the static cache. | 1 |
DrupalDefaultEntityController:: |
protected | function | Stores entities in the static entity cache. | |
DrupalDefaultEntityController:: |
protected | function | Ensures integer entity IDs are valid. | |
DrupalDefaultEntityController:: |
protected | function | Callback for array_filter that removes non-integer IDs. | |
EntityAPIController:: |
protected | property | ||
EntityAPIController:: |
protected | property | ||
EntityAPIController:: |
protected | property | ||
EntityAPIController:: |
public | function |
Implements EntityAPIControllerInterface. Overrides EntityAPIControllerInterface:: |
|
EntityAPIController:: |
protected | function |
Overrides DrupalDefaultEntityController::buildQuery(). Overrides DrupalDefaultEntityController:: |
1 |
EntityAPIController:: |
public | function |
Implements EntityAPIControllerInterface. Overrides EntityAPIControllerInterface:: |
|
EntityAPIController:: |
public | function |
Implements EntityAPIControllerRevisionableInterface::deleteRevision(). Overrides EntityAPIControllerRevisionableInterface:: |
|
EntityAPIController:: |
public | function |
Implements EntityAPIControllerInterface. Overrides EntityAPIControllerInterface:: |
1 |
EntityAPIController:: |
public | function |
Implements EntityAPIControllerInterface. Overrides EntityAPIControllerInterface:: |
|
EntityAPIController:: |
public | function |
Implements EntityAPIControllerInterface. Overrides EntityAPIControllerInterface:: |
1 |
EntityAPIController:: |
public | function | Builds and executes the query for loading. | |
EntityAPIController:: |
protected | function | Renders a single entity property. | |
EntityAPIController:: |
public | function |
Overrides DrupalDefaultEntityController::resetCache(). Overrides DrupalDefaultEntityController:: |
1 |
EntityAPIController:: |
protected | function | Saves an entity revision. | |
EntityAPIController:: |
public | function |
Overridden. Overrides DrupalDefaultEntityController:: |
1 |
PaymentEntityController:: |
function |
Attaches data to entities upon loading. Overrides DrupalDefaultEntityController:: |
||
PaymentEntityController:: |
function |
Implements EntityAPIControllerInterface. Overrides EntityAPIController:: |
||
PaymentEntityController:: |
function |
Overridden. Overrides EntityAPIController:: |
||
PaymentEntityController:: |
function |
Implements EntityAPIControllerInterface. Overrides EntityAPIController:: |
||
PaymentEntityController:: |
function | Save the line items and payment status items if needed. | ||
PaymentEntityController:: |
function |
Implements EntityAPIControllerInterface. Overrides EntityAPIController:: |