diff --git a/commerce.info b/commerce.info index a9282bf..f867f0e 100644 --- a/commerce.info +++ b/commerce.info @@ -11,3 +11,6 @@ files[] = tests/commerce_base.test ; Central Entity Controller. files[] = includes/commerce.controller.inc + +; Views related includes. +files[] = includes/views/handlers/commerce_order_handler_non_locking_field.inc diff --git a/includes/views/handlers/commerce_order_handler_non_locking_field.inc b/includes/views/handlers/commerce_order_handler_non_locking_field.inc new file mode 100644 index 0000000..077c25d --- /dev/null +++ b/includes/views/handlers/commerce_order_handler_non_locking_field.inc @@ -0,0 +1,84 @@ +query instanceof CommerceOrderViewsQuery) { + // For non-locking Order loading, we need to pass the "_lock" condition. + $conditions = array('_lock' => !empty($this->query->options['lock_loaded_orders'])); + } + + // The following code is basically a copy-and-paste from the default + // views_handler_field_field::post_execute() code. The difference is that + // here we handle the "_lock" condition when loading Order instances. + if (!empty($values)) { + // Divide the entity ids by entity type, so they can be loaded in bulk. + $entities_by_type = array(); + $revisions_by_type = array(); + foreach ($values as $key => $object) { + if (isset($this->aliases['entity_type']) && isset($object->{$this->aliases['entity_type']}) && isset($object->{$this->field_alias}) && !isset($values[$key]->_field_data[$this->field_alias])) { + $entity_type = $object->{$this->aliases['entity_type']}; + if (empty($this->definition['is revision'])) { + $entity_id = $object->{$this->field_alias}; + $entities_by_type[$entity_type][$key] = $entity_id; + } + else { + $revision_id = $object->{$this->field_alias}; + $entity_id = $object->{$this->aliases['entity_id']}; + $entities_by_type[$entity_type][$key] = array($entity_id, $revision_id); + } + } + } + + // Load the entities. + foreach ($entities_by_type as $entity_type => $entity_ids) { + $entity_info = entity_get_info($entity_type); + if (empty($this->definition['is revision'])) { + $entities = entity_load($entity_type, $entity_ids, $conditions); + $keys = $entity_ids; + } + else { + // Revisions can't be loaded multiple, so we have to load them + // one by one. + $entities = array(); + $keys = array(); + foreach ($entity_ids as $key => $combined) { + list($entity_id, $revision_id) = $combined; + // Adding the revision as a condition to the previous ones. + $conditions[$entity_info['entity keys']['revision']] = $revision_id; + $entity = entity_load($entity_type, array($entity_id), $conditions); + if ($entity) { + $entities[$revision_id] = array_shift($entity); + $keys[$key] = $revision_id; + } + } + } + + foreach ($keys as $key => $entity_id) { + // If this is a revision, load the revision instead. + if (isset($entities[$entity_id])) { + $values[$key]->_field_data[$this->field_alias] = array( + 'entity_type' => $entity_type, + 'entity' => $entities[$entity_id], + ); + } + } + } + + // Now, transfer the data back into the resultset so it can be easily used. + foreach ($values as $row_id => &$value) { + $value->{'field_' . $this->options['id']} = $this->set_items($value, $row_id); + } + } + } +} diff --git a/modules/order/commerce_order.info b/modules/order/commerce_order.info index 506536a..013bf83 100644 --- a/modules/order/commerce_order.info +++ b/modules/order/commerce_order.info @@ -31,6 +31,7 @@ files[] = includes/views/handlers/commerce_order_handler_filter_order_type.inc ; Views plugins files[] = includes/views/handlers/commerce_order_plugin_argument_validate_user.inc +files[] = includes/views/commerce_order_views_query.inc ; Tests files[] = tests/commerce_order.rules.test diff --git a/modules/order/commerce_order.module b/modules/order/commerce_order.module index 889f407..f1c3f6d 100644 --- a/modules/order/commerce_order.module +++ b/modules/order/commerce_order.module @@ -629,7 +629,7 @@ function commerce_order_form_commerce_customer_customer_profile_delete_form_alte if ($entity_type == 'commerce_order') { // Load the referencing order. $order = reset($data); - $order = commerce_order_load($order->order_id); + $order = commerce_order_load($order->order_id, FALSE); // Only exit here if the order is in a non-cart status. if (!in_array($order->status, array_keys(commerce_order_statuses(array('cart' => TRUE))))) { diff --git a/modules/order/includes/views/commerce_order.views.inc b/modules/order/includes/views/commerce_order.views.inc index e5a8b59..96fb8f6 100644 --- a/modules/order/includes/views/commerce_order.views.inc +++ b/modules/order/includes/views/commerce_order.views.inc @@ -17,6 +17,7 @@ function commerce_order_views_data() { 'title' => t('Commerce Order'), 'help' => t('Order placed in the store.'), 'access query tag' => 'commerce_order_access', + 'query class' => 'commerce_order_views_query', ); $data['commerce_order']['table']['entity type'] = 'commerce_order'; @@ -545,5 +546,12 @@ function commerce_order_views_plugins() { 'handler' => 'commerce_order_plugin_argument_validate_user', ), ), + 'query' => array( + 'commerce_order_views_query' => array( + 'title' => t('Commerce Order Query'), + 'help' => t('Query will be generated and run for Order views.'), + 'handler' => 'CommerceOrderViewsQuery', + ), + ), ); } diff --git a/modules/order/includes/views/commerce_order_views_query.inc b/modules/order/includes/views/commerce_order_views_query.inc new file mode 100644 index 0000000..eda02bc --- /dev/null +++ b/modules/order/includes/views/commerce_order_views_query.inc @@ -0,0 +1,114 @@ + TRUE, + 'translatable' => FALSE, + 'bool' => TRUE, + ); + + return $options; + } + + /** + * Add settings for the ui. + * @return array + */ + function options_form(&$form, &$form_state) { + parent::options_form($form, $form_state); + + $form['lock_loaded_orders'] = array( + '#title' => t('Lock loaded Orders'), + '#description' => t('When loading orders, those will be locked for further processing. Disable locking to avoid lock-timeouts or deadlocks in queries.'), + '#type' => 'checkbox', + '#default_value' => !empty($this->options['lock_loaded_orders']), + ); + + return $form; + } + + /** + * Returns the according entity objects for the given query results. + * + * Mimics the same behaviour of views_plugin_query, but allows admins to define + * if the loaded orders should be locked or not. + * + */ + function get_result_entities($results, $relationship = NULL) { + // The following code is basically a copy-and-paste from the default + // views_plugin_query_default::get_result_entities() code. The difference is + // that here we handle the "_lock" condition when loading Order instances. + + $base_table = $this->base_table; + $base_table_alias = $base_table; + + if (!empty($relationship)) { + foreach ($this->view->relationship as $current) { + if ($current->alias == $relationship) { + $base_table = $current->definition['base']; + $base_table_alias = $relationship; + break; + } + } + } + $table_data = views_fetch_data($base_table); + + // Bail out if the table has not specified the according entity-type. + if (!isset($table_data['table']['entity type'])) { + return FALSE; + } + $entity_type = $table_data['table']['entity type']; + $info = entity_get_info($entity_type); + $is_revision = !empty($table_data['table']['revision']); + $id_alias = $this->get_field_alias($base_table_alias, $info['entity keys'][$is_revision ? 'revision' : 'id']); + + // Assemble the ids of the entities to load. + $ids = array(); + foreach ($results as $key => $result) { + if (isset($result->$id_alias)) { + $ids[$key] = $result->$id_alias; + } + } + + if (!$is_revision) { + // Creating the entity_load conditions given the locking strategy. + $conditions = array('_lock' => !empty($this->options['lock_loaded_orders'])); + $entities = entity_load($entity_type, $ids, $conditions); + // Re-key the array by row-index. + $result = array(); + foreach ($ids as $key => $id) { + $result[$key] = isset($entities[$id]) ? $entities[$id] : FALSE; + } + } + else { + // There's no way in core to load revisions in bulk. + $result = array(); + foreach ($ids as $key => $id) { + if (module_exists('entity')) { + $result[$key] = entity_revision_load($entity_type, $id); + } + else { + // Otherwise this isn't supported. + watchdog('views', 'Attempt to load a revision on an unsupported entity type @entity_type.', array('@entity_type' => $entity_type), WATCHDOG_WARNING); + } + } + } + + return array($entity_type, $result); + } +} diff --git a/modules/order/includes/views/handlers/commerce_order_handler_area_order_total.inc b/modules/order/includes/views/handlers/commerce_order_handler_area_order_total.inc index c0d238f..51c1e37 100644 --- a/modules/order/includes/views/handlers/commerce_order_handler_area_order_total.inc +++ b/modules/order/includes/views/handlers/commerce_order_handler_area_order_total.inc @@ -32,7 +32,7 @@ class commerce_order_handler_area_order_total extends views_handler_area { // If it is single value... if (count($argument->value) == 1) { // Load the order. - if ($order = commerce_order_load(reset($argument->value))) { + if ($order = commerce_order_load(reset($argument->value), FALSE)) { // Prepare a display settings array. $display = array( diff --git a/modules/price/includes/views/handlers/commerce_price_handler_field_commerce_price.inc b/modules/price/includes/views/handlers/commerce_price_handler_field_commerce_price.inc index 919b655..ce27cfa 100644 --- a/modules/price/includes/views/handlers/commerce_price_handler_field_commerce_price.inc +++ b/modules/price/includes/views/handlers/commerce_price_handler_field_commerce_price.inc @@ -4,7 +4,7 @@ * An extension of the default Views field handler that supports aggregating * price fields. */ -class commerce_price_handler_field_commerce_price extends views_handler_field_field { +class commerce_price_handler_field_commerce_price extends commerce_order_handler_non_locking_field { function get_value($values, $field = NULL) { // If this field has aggregation enabled... if (!empty($this->group_fields)) {