Projet

Général

Profil

Paste
Télécharger (4,42 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / commerce / modules / order / includes / commerce_order.controller.inc @ b858700c

1
<?php
2

    
3
/**
4
 * @file
5
 * The controller for the order entity containing the CRUD operations.
6
 */
7

    
8
/**
9
 * The controller class for orders contains methods for the order CRUD
10
 * operations. The load method is inherited from the default controller.
11
 */
12
class CommerceOrderEntityController extends DrupalCommerceEntityController {
13

    
14
  /**
15
   * Create a default order.
16
   *
17
   * @param array $values
18
   *   An array of values to set, keyed by property name.
19
   *
20
   * @return
21
   *   An order object with all default fields initialized.
22
   */
23
  public function create(array $values = array()) {
24
    $values += array(
25
      'order_id' => NULL,
26
      'order_number' => NULL,
27
      'revision_id' => NULL,
28
      'uid' => '',
29
      'mail' => ( !empty($values['uid']) && ($account = user_load($values['uid'])) ) ? $account->mail : '',
30
      'data' => array(),
31
      'created' => '',
32
      'changed' => '',
33
      'hostname' => '',
34
    );
35

    
36
    return parent::create($values);
37
  }
38

    
39
  /**
40
   * Saves an order.
41
   *
42
   * When saving an order without an order ID, this function will create a new
43
   * order at that time. For new orders, it will also determine and save the
44
   * order number and then save the initial revision of the order. Subsequent
45
   * orders that should be saved as new revisions should set $order->revision to
46
   * TRUE and include a log string in $order->log.
47
   *
48
   * @param $order
49
   *   The full order object to save.
50
   * @param $transaction
51
   *   An optional transaction object.
52
   *
53
   * @return
54
   *   SAVED_NEW or SAVED_UPDATED depending on the operation performed.
55
   */
56
  public function save($order, DatabaseTransaction $transaction = NULL) {
57
    if (!isset($transaction)) {
58
      $transaction = db_transaction();
59
      $started_transaction = TRUE;
60
    }
61

    
62
    try {
63
      global $user;
64

    
65
      // Determine if we will be inserting a new order.
66
      $order->is_new = empty($order->order_id);
67

    
68
      // Set the timestamp fields.
69
      if ($order->is_new) {
70
        if (empty($order->created)) {
71
          $order->created = REQUEST_TIME;
72
        }
73
        if (empty($order->hostname)) {
74
          $order->hostname = ip_address();
75
        }
76
      }
77
      else {
78
        // Otherwise if the order is not new but comes from an entity_create()
79
        // or similar function call that initializes the created timestamp, uid,
80
        // and hostname values to empty strings, unset them to prevent
81
        // destroying existing data in those properties on update.
82
        if ($order->created === '') {
83
          unset($order->created);
84
        }
85
        if ($order->uid === '') {
86
          unset($order->uid);
87
        }
88
        if ($order->hostname === '') {
89
          unset($order->hostname);
90
        }
91
      }
92

    
93
      $order->changed = REQUEST_TIME;
94

    
95
      $order->revision_timestamp = REQUEST_TIME;
96
      $order->revision_hostname = ip_address();
97
      $order->revision_uid = $user->uid;
98

    
99
      // Recalculate the order total using the current line item data.
100
      commerce_order_calculate_total($order);
101

    
102
      if ($order->is_new || !empty($order->revision)) {
103
        // When inserting either a new order or revision, $order->log must be set
104
        // because {commerce_order_revision}.log is a text column and therefore
105
        // cannot have a default value. However, it might not be set at this
106
        // point, so we ensure that it is at least an empty string in that case.
107
        if (!isset($order->log)) {
108
          $order->log = '';
109
        }
110
      }
111
      elseif (empty($order->log)) {
112
        // If we are updating an existing order without adding a new revision,
113
        // we need to make sure $order->log is unset whenever it is empty. As
114
        // long as $order->log is unset, drupal_write_record() will not attempt
115
        // to update the existing database column when re-saving the revision.
116
        unset($order->log);
117
      }
118

    
119
      return parent::save($order, $transaction);
120
    }
121
    catch (Exception $e) {
122
      if (!empty($started_transaction)) {
123
        $transaction->rollback();
124
        watchdog_exception($this->entityType, $e);
125
      }
126
      throw $e;
127
    }
128
  }
129

    
130
  /**
131
   * Unserializes the data property of loaded orders.
132
   */
133
  public function attachLoad(&$queried_orders, $revision_id = FALSE) {
134
    foreach ($queried_orders as $order_id => &$order) {
135
      $order->data = unserialize($order->data);
136
    }
137

    
138
    // Call the default attachLoad() method. This will add fields and call
139
    // hook_commerce_order_load().
140
    parent::attachLoad($queried_orders, $revision_id);
141
  }
142
}