diff --git a/uc_cart/src/Controller/CheckoutController.php b/uc_cart/src/Controller/CheckoutController.php index 9f021c0..b957668 100644 --- a/uc_cart/src/Controller/CheckoutController.php +++ b/uc_cart/src/Controller/CheckoutController.php @@ -86,8 +86,8 @@ class CheckoutController extends ControllerBase implements ContainerInjectionInt // there has been no activity for 10 minutes (to prevent identity theft). if ($order->getStateId() != 'in_checkout' || ($this->currentUser()->isAuthenticated() && $this->currentUser()->id() != $order->getUserId()) || - $order->modified->value < REQUEST_TIME - CartInterface::CHECKOUT_TIMEOUT) { - if ($order->getStateId() == 'in_checkout' && $order->modified->value < REQUEST_TIME - CartInterface::CHECKOUT_TIMEOUT) { + $order->getChangedTime() < REQUEST_TIME - CartInterface::CHECKOUT_TIMEOUT) { + if ($order->getStateId() == 'in_checkout' && $order->getChangedTime() < REQUEST_TIME - CartInterface::CHECKOUT_TIMEOUT) { // Mark expired orders as abandoned. $order->setStatusId('abandoned')->save(); } diff --git a/uc_cart/src/Form/CheckoutForm.php b/uc_cart/src/Form/CheckoutForm.php index 18d2710..eef09bb 100644 --- a/uc_cart/src/Form/CheckoutForm.php +++ b/uc_cart/src/Form/CheckoutForm.php @@ -132,8 +132,8 @@ class CheckoutForm extends FormBase { public function validateForm(array &$form, FormStateInterface $form_state) { $order = $form_state->get('order'); - // Update the order "modified" time to prevent timeout on ajax requests. - $order->modified->value = REQUEST_TIME; + // Update the order "changed" time to prevent timeout on ajax requests. + $order->setChangedTime(REQUEST_TIME); // Validate/process the cart panes. A FALSE value results in failed checkout. $form_state->set('checkout_valid', TRUE); diff --git a/uc_cart/src/Tests/CartCheckoutTest.php b/uc_cart/src/Tests/CartCheckoutTest.php index 34eca82..8157a9f 100644 --- a/uc_cart/src/Tests/CartCheckoutTest.php +++ b/uc_cart/src/Tests/CartCheckoutTest.php @@ -478,10 +478,10 @@ class CartCheckoutTest extends UbercartTestBase { $this->assertEqual($order_id, $new_order_id, 'Original order_id was reused.'); // Jump 10 minutes into the future. - // @todo: Can we set modified through the Entity API rather than DBTNG? + // @todo: Can we set changed through the Entity API rather than DBTNG? db_update('uc_orders') ->fields(array( - 'modified' => time() - CartInterface::ORDER_TIMEOUT - 1, + 'changed' => time() - CartInterface::ORDER_TIMEOUT - 1, )) ->condition('order_id', $order_id) ->execute(); diff --git a/uc_cart/uc_cart.module b/uc_cart/uc_cart.module index 5789be4..1bd4d30 100644 --- a/uc_cart/uc_cart.module +++ b/uc_cart/uc_cart.module @@ -40,7 +40,7 @@ function uc_cart_cron() { } // Update status of abandoned orders. - $result = db_query('SELECT order_id FROM {uc_orders} WHERE order_status = :status AND modified < :time', + $result = db_query('SELECT order_id FROM {uc_orders} WHERE order_status = :status AND changed < :time', [':status' => 'in_checkout', ':time' => REQUEST_TIME - CartInterface::ORDER_TIMEOUT])->fetchCol(); foreach ($result as $order_id) { \Drupal\uc_order\Entity\Order::load($order_id) diff --git a/uc_order/src/Entity/Order.php b/uc_order/src/Entity/Order.php index c864934..9c3435d 100644 --- a/uc_order/src/Entity/Order.php +++ b/uc_order/src/Entity/Order.php @@ -8,6 +8,7 @@ namespace Drupal\uc_order\Entity; use Drupal\Core\Entity\ContentEntityBase; +use Drupal\Core\Entity\EntityChangedTrait; use Drupal\Core\Entity\EntityStorageInterface; use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Field\BaseFieldDefinition; @@ -50,6 +51,8 @@ use Drupal\uc_store\Address; */ class Order extends ContentEntityBase implements OrderInterface { + use EntityChangedTrait; + public $products = array(); public $line_items = array(); @@ -88,7 +91,7 @@ class Order extends ContentEntityBase implements OrderInterface { $this->order_total->value = $this->getTotal(); $this->product_count->value = $this->getProductCount(); $this->host->value = \Drupal::request()->getClientIp(); - $this->modified->value = REQUEST_TIME; + $this->setChangedTime(REQUEST_TIME); } /** @@ -147,6 +150,22 @@ class Order extends ContentEntityBase implements OrderInterface { /** * {@inheritdoc} */ + public function getCreatedTime() { + return $this->get('created')->value; + } + + + /** + * {@inheritdoc} + */ + public function setCreatedTime($timestamp) { + $this->set('created', $timestamp); + return $this; + } + + /** + * {@inheritdoc} + */ public function getLineItems() { $items = array(); @@ -561,10 +580,10 @@ class Order extends ContentEntityBase implements OrderInterface { ->setLabel(t('Created')) ->setDescription(t('The time that the order was created.')); - $fields['modified'] = BaseFieldDefinition::create('integer') - ->setLabel(t('Modified')) - ->setDescription(t('The Unix timestamp indicating when the order was last modified.')) - ->setSetting('default_value', 0); + $fields['changed'] = BaseFieldDefinition::create('changed') + ->setLabel(t('Changed')) + ->setDescription(t('The time that the order was last edited.')); + $fields['host'] = BaseFieldDefinition::create('string') ->setLabel(t('Host')) ->setDescription(t('Host IP address of the person paying for the order.')) diff --git a/uc_order/src/OrderForm.php b/uc_order/src/OrderForm.php index 6f07781..2db1006 100644 --- a/uc_order/src/OrderForm.php +++ b/uc_order/src/OrderForm.php @@ -26,7 +26,7 @@ class OrderForm extends ContentEntityForm { $form['order_id'] = array('#type' => 'hidden', '#value' => $order->id()); $form['order_uid'] = array('#type' => 'hidden', '#value' => $order->getUserId()); - $modified = $form_state->getValue('order_modified') ?: $order->modified->value; + $modified = $form_state->getValue('order_modified') ?: $order->getChangedTime(); $form['order_modified'] = array('#type' => 'hidden', '#value' => $modified); $panes = _uc_order_pane_list('edit'); @@ -63,7 +63,7 @@ class OrderForm extends ContentEntityForm { public function validate(array $form, FormStateInterface $form_state) { $order = $this->buildEntity($form, $form_state); - if ($form_state->getValue('order_modified') != $order->modified->value) { + if ($form_state->getValue('order_modified') != $order->getChangedTime()) { $form_state->setErrorByName('order_modified', t('This order has been modified by another user, changes cannot be saved.')); } diff --git a/uc_order/src/OrderInterface.php b/uc_order/src/OrderInterface.php index 6d65be8..e72f8c5 100644 --- a/uc_order/src/OrderInterface.php +++ b/uc_order/src/OrderInterface.php @@ -7,13 +7,33 @@ namespace Drupal\uc_order; +use Drupal\Core\Entity\EntityChangedInterface; use Drupal\Core\Entity\ContentEntityInterface; use Drupal\uc_store\Address; /** * Provides an interface defining an Ubercart order entity. */ -interface OrderInterface extends ContentEntityInterface { +interface OrderInterface extends ContentEntityInterface, EntityChangedInterface { + + /** + * Gets the order creation timestamp. + * + * @return int + * Creation timestamp of the order. + */ + public function getCreatedTime(); + + /** + * Sets the order creation timestamp. + * + * @param int $timestamp + * The order creation timestamp. + * + * @return \Drupal\uc_order\OrderInterface + * The called order entity. + */ + public function setCreatedTime($timestamp); /** * Returns an array containing an order's line items ordered by weight. diff --git a/uc_order/src/OrderViewsData.php b/uc_order/src/OrderViewsData.php index f956dd6..ae8322d 100644 --- a/uc_order/src/OrderViewsData.php +++ b/uc_order/src/OrderViewsData.php @@ -139,21 +139,6 @@ class OrderViewsData extends EntityViewsData { ), ); - $data['uc_orders']['modified'] = array( - 'title' => t('Last modified'), - 'help' => t('The time the order was last modified.'), - 'field' => array( - 'id' => 'date', - 'click sortable' => TRUE, - ), - 'sort' => array( - 'id' => 'date' - ), - 'filter' => array( - 'id' => 'date', - ), - ); - $data['uc_orders']['actions'] = array( 'title' => t('Actions'), 'help' => t('Clickable links to actions a user may perform on an order.'), diff --git a/uc_order/uc_order.module b/uc_order/uc_order.module index 3bb0304..59b7f30 100644 --- a/uc_order/uc_order.module +++ b/uc_order/uc_order.module @@ -786,44 +786,4 @@ function uc_order_actions($order, $icon_html = FALSE) { '#type' => 'operations', '#links' => $actions, ); -} - -/** - * Implements hook_date_views_tables(). - */ -function uc_order_date_views_tables() { - return array('uc_orders'); -} - -/** - * Implements hook_date_views_fields(). - * - * All modules that create custom fields that use the - * 'views_handler_field_date' handler can provide - * additional information here about the type of - * date they create so the date can be used by - * the Date API views date argument and date filter. - */ -function uc_order_date_views_fields($field) { - $values = array( - // The type of date: DATE_UNIX, DATE_ISO, DATE_DATETIME. - 'sql_type' => DATE_UNIX, - // Timezone handling options: 'none', 'site', 'date', 'utc' . - 'tz_handling' => 'site', - // Needed only for dates that use 'date' tz_handling. - 'timezone_field' => '', - // Needed only for dates that use 'date' tz_handling. - 'offset_field' => '', - // Array of "table.field" values for related fields that should be - // loaded automatically in the Views SQL. - 'related_fields' => array(), - // Granularity of this date field's db data. - 'granularity' => array('year', 'month', 'day', 'hour', 'minute', 'second'), - ); - - switch ($field) { - case 'uc_orders.created': - case 'uc_orders.modified': - return $values; - } -} +} \ No newline at end of file diff --git a/uc_order/uc_order.tokens.inc b/uc_order/uc_order.tokens.inc index bb7f030..93dec83 100644 --- a/uc_order/uc_order.tokens.inc +++ b/uc_order/uc_order.tokens.inc @@ -5,6 +5,8 @@ * Token hooks for the uc_order module. */ +use Drupal\Core\Datetime\Entity\DateFormat; +use Drupal\Core\Language\LanguageInterface; use Drupal\Component\Utility\SafeMarkup; use Drupal\Core\Render\BubbleableMetadata; use Drupal\Core\Url; @@ -112,13 +114,13 @@ function uc_order_token_info() { 'type' => 'user', ); $tokens['created'] = array( - 'name' => t('Created'), + 'name' => t("Date created"), 'description' => t('The date and time when the order was created.'), 'type' => 'date', ); - $tokens['modified'] = array( - 'name' => t('Modified'), - 'description' => t('The date and time when the order was last modified.'), + $tokens['changed'] = array( + 'name' => t("Date changed"), + 'description' => t("The date the order was most recently updated."), 'type' => 'date', ); @@ -139,12 +141,19 @@ function uc_order_token_info() { function uc_order_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) { $token_service = \Drupal::token(); + if (isset($options['langcode'])) { + $langcode = $options['langcode']; + } + else { + $langcode = LanguageInterface::LANGCODE_DEFAULT; + } + $sanitize = !empty($options['sanitize']); - $replacements = []; + $replacements = array(); if ($type == 'uc_order' && !empty($data['uc_order'])) { - + /** @var \Drupal\uc_order\OrderInterface $order */ $order = $data['uc_order']; $path = 'user/' . $order->getUserId() . '/orders/' . $order->id(); $admin_path = 'admin/store/orders/' . $order->id(); @@ -292,11 +301,15 @@ function uc_order_tokens($type, $tokens, array $data, array $options, Bubbleable break; case 'created': - $replacements[$original] = \Drupal::service('date.formatter')->format($order->created->value, 'short'); + $date_format = DateFormat::load('short'); + $bubbleable_metadata->addCacheableDependency($date_format); + $replacements[$original] = \Drupal::service('date.formatter')->format($order->getCreatedTime(), 'short', NULL, $langcode); break; - case 'modified': - $replacements[$original] = \Drupal::service('date.formatter')->format($order->modified->value, 'short'); + case 'changed': + $date_format = DateFormat::load('short'); + $bubbleable_metadata->addCacheableDependency($date_format); + $replacements[$original] = \Drupal::service('date.formatter')->format($order->getChangedTime(), 'short', NULL, $langcode); break; case 'products': @@ -330,11 +343,11 @@ function uc_order_tokens($type, $tokens, array $data, array $options, Bubbleable } if ($created_tokens = $token_service->findWithPrefix($tokens, 'created')) { - $replacements += $token_service->generate('date', $created_tokens, ['date' => $order->created->value], $options, $bubbleable_metadata); + $replacements += $token_service->generate('date', $created_tokens, array('date' => $order->getCreatedTime()), $options, $bubbleable_metadata); } - if ($changed_tokens = $token_service->findWithPrefix($tokens, 'modified')) { - $replacements += $token_service->generate('date', $changed_tokens, ['date' => $order->modified->value], $options, $bubbleable_metadata); + if ($changed_tokens = $token_service->findWithPrefix($tokens, 'changed')) { + $replacements += $token_service->generate('date', $changed_tokens, array('date' => $order->getChangedTime()), $options, $bubbleable_metadata); } }