You are here

class PaymentEntityController in Payment 7

Entity API controller for payment entities.

Hierarchy

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

Namesort descending Modifiers Type Description Overrides
DrupalDefaultEntityController::$cache protected property Whether this entity type should use the static cache.
DrupalDefaultEntityController::$entityCache protected property Static cache of entities, keyed by entity ID.
DrupalDefaultEntityController::$entityInfo protected property Array of information about the entity.
DrupalDefaultEntityController::$entityType protected property Entity type for this controller instance.
DrupalDefaultEntityController::$hookLoadArguments protected property Additional arguments to pass to hook_TYPE_load().
DrupalDefaultEntityController::$idKey protected property Name of the entity's ID field in the entity database table.
DrupalDefaultEntityController::$revisionKey protected property Name of entity's revision database table field, if it supports revisions.
DrupalDefaultEntityController::$revisionTable protected property The table that stores revisions, if the entity supports revisions.
DrupalDefaultEntityController::cacheGet protected function Gets entities from the static cache. 1
DrupalDefaultEntityController::cacheSet protected function Stores entities in the static entity cache.
DrupalDefaultEntityController::cleanIds protected function Ensures integer entity IDs are valid.
DrupalDefaultEntityController::filterId protected function Callback for array_filter that removes non-integer IDs.
EntityAPIController::$bundleKey protected property
EntityAPIController::$cacheComplete protected property
EntityAPIController::$defaultRevisionKey protected property
EntityAPIController::buildContent public function Implements EntityAPIControllerInterface. Overrides EntityAPIControllerInterface::buildContent
EntityAPIController::buildQuery protected function Overrides DrupalDefaultEntityController::buildQuery(). Overrides DrupalDefaultEntityController::buildQuery 1
EntityAPIController::create public function Implements EntityAPIControllerInterface. Overrides EntityAPIControllerInterface::create
EntityAPIController::deleteRevision public function Implements EntityAPIControllerRevisionableInterface::deleteRevision(). Overrides EntityAPIControllerRevisionableInterface::deleteRevision
EntityAPIController::export public function Implements EntityAPIControllerInterface. Overrides EntityAPIControllerInterface::export 1
EntityAPIController::import public function Implements EntityAPIControllerInterface. Overrides EntityAPIControllerInterface::import
EntityAPIController::invoke public function Implements EntityAPIControllerInterface. Overrides EntityAPIControllerInterface::invoke 1
EntityAPIController::query public function Builds and executes the query for loading.
EntityAPIController::renderEntityProperty protected function Renders a single entity property.
EntityAPIController::resetCache public function Overrides DrupalDefaultEntityController::resetCache(). Overrides DrupalDefaultEntityController::resetCache 1
EntityAPIController::saveRevision protected function Saves an entity revision.
EntityAPIController::__construct public function Overridden. Overrides DrupalDefaultEntityController::__construct 1
PaymentEntityController::attachLoad function Attaches data to entities upon loading. Overrides DrupalDefaultEntityController::attachLoad
PaymentEntityController::delete function Implements EntityAPIControllerInterface. Overrides EntityAPIController::delete
PaymentEntityController::load function Overridden. Overrides EntityAPIController::load
PaymentEntityController::save function Implements EntityAPIControllerInterface. Overrides EntityAPIController::save
PaymentEntityController::saveAttachedData function Save the line items and payment status items if needed.
PaymentEntityController::view function Implements EntityAPIControllerInterface. Overrides EntityAPIController::view