Commit efbe445a authored by git's avatar git Committed by Fernando Andrés Muñoz Bravo
Browse files

Issue #3296211 by waspper, KondratievaS: Add Views relationship to the first,...

Issue #3296211 by waspper, KondratievaS: Add Views relationship to the first, last and hammer bid to the Auction entity
parent c9664d48
Loading
Loading
Loading
Loading
+71 −2
Changes for auctioneer.install: 71 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -5,6 +5,8 @@
 * Installation and Updating functions for Auctioneer.
 */

use Drupal\Core\Database\Database;

/**
 * Implements hook_requirements().
 */
@@ -29,13 +31,13 @@ function auctioneer_requirements($phase) {
}

/**
 * Implements hook_schema().
 * Helper schema definition, to make it shareable between update functions.
 *
 * We're using small/dedicated tables to store useful data. This is
 * because we want to keep this separated from main entities, but it's not
 * as big, or as flexible as content entities.
 */
function auctioneer_schema() {
function auctioneer_schema_definition() {
  $schema = [];
  $schema['auctioneer_auction_hammer'] = [
    'description' => 'Stores auction fulfillments.',
@@ -51,6 +53,12 @@ function auctioneer_schema() {
        'unsigned' => TRUE,
        'not null' => TRUE,
      ],
      'auction' => [
        'description' => 'The auction id.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ],
      'uid' => [
        'type' => 'int',
        'unsigned' => TRUE,
@@ -68,6 +76,7 @@ function auctioneer_schema() {
    'primary key' => ['hid'],
    'indexes' => [
      'bid' => ['bid'],
      'auction' => ['auction'],
      'uid' => ['uid'],
    ],
    'foreign keys' => [
@@ -77,6 +86,12 @@ function auctioneer_schema() {
          'bid' => 'id',
        ],
      ],
      'auction' => [
        'table' => 'auction',
        'columns' => [
          'auction' => 'id',
        ],
      ],
      'uid' => [
        'table' => 'users',
        'columns' => [
@@ -110,6 +125,18 @@ function auctioneer_schema() {
        'not null' => TRUE,
        'default' => 0,
      ],
      'initial_bid' => [
        'description' => 'The initial bid id.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => FALSE,
      ],
      'last_bid' => [
        'description' => 'The last bid id.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => FALSE,
      ],
      'context' => [
        'description' => 'Arbitrary comma-separated list of strings about nature of this auction.',
        'type' => 'text',
@@ -118,6 +145,11 @@ function auctioneer_schema() {
      ],
    ],
    'primary key' => ['auction_id'],
    'indexes' => [
      'auction_id' => ['auction_id'],
      'initial_bid' => ['initial_bid'],
      'last_bid' => ['last_bid'],
    ],
    'foreign keys' => [
      'auction_id' => [
        'table' => 'auction',
@@ -125,8 +157,45 @@ function auctioneer_schema() {
          'auction_id' => 'id',
        ],
      ],
      'initial_bid' => [
        'table' => 'bid',
        'columns' => [
          'initial_bid' => 'id',
        ],
      ],
      'last_bid' => [
        'table' => 'bid',
        'columns' => [
          'last_bid' => 'id',
        ],
      ],
    ],
  ];

  return $schema;
}

/**
 * Implements hook_schema().
 */
function auctioneer_schema() {
  return auctioneer_schema_definition();
}

/**
 * Update Auctioner tables for extra information.
 */
function auctioneer_update_8001() {
  $schema = Database::getConnection()->schema();
  $provided_definitions = auctioneer_schema_definition();
  $new_definitions = [
    'auctioneer_auction_hammer' => ['auction'],
    'auctioneer_auction_statistics' => ['initial_bid', 'last_bid'],
  ];
  foreach ($new_definitions as $table_name => $field_names) {
    foreach ($field_names as $field_name) {
      $schema->addField($table_name, $field_name, $provided_definitions[$table_name]['fields'][$field_name]);
      $schema->addIndex($table_name, $field_name, [$field_name], $provided_definitions[$table_name]);
    }
  }
}
+50 −0
Changes for auctioneer.post_update.php: 50 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php

/**
 * @file
 * Post update functions for Auctioneer.
 */

/**
 * Reset auctions data in order to match with new added fields.
 */
function auctioneer_post_update_reset_auctions_data(&$sandbox) {
  $auction_type_manager = \Drupal::service('auctioneer.auction_type.manager');
  $entity_type_manager = \Drupal::service('entity_type.manager');
  if (!isset($sandbox['total'])) {
    $available = $entity_type_manager->getStorage('auction')->getQuery()->execute();
    $sandbox['total'] = count($available);
    $sandbox['available'] = array_values($available);
    $sandbox['current'] = 0;
    $sandbox['items_per_batch'] = 30;
  }
  $available = array_slice(
    $sandbox['available'],
    $sandbox['current'],
    $sandbox['current'] + $sandbox['items_per_batch']
  );
  $auctions = $entity_type_manager->getStorage('auction')->loadMultiple($available);
  foreach ($auctions as $auction) {
    if ($hammer = $auction_type_manager->getAuctionHammerBid($auction)) {
      // This method also calls the statistics update.
      $auction_type_manager->setAuctionHammerBid($hammer);
    }
    else {
      // If no hammer, then we need to directly reset statistics.
      $auction_type_manager->setAuctionStatistics($auction);
    }
    $sandbox['current']++;
  }
  if ($sandbox['total'] == 0) {
    $sandbox['#finished'] = 1;
  }
  else {
    $sandbox['#finished'] = ($sandbox['current'] / $sandbox['total']);
  }

  return t('@percentage% of auctions updated (@current of @total).', [
    '@percentage' => $sandbox['total'] > 0 ? round($sandbox['current'] / ($sandbox['total'] * 100)) : 0,
    '@current' => $sandbox['current'],
    '@total' => $sandbox['total'],
  ]);
}

src/AuctionTypeManager.php

100644 → 100755
+5 −0
Changes for src/AuctionTypeManager.php: 5 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -274,6 +274,8 @@ class AuctionTypeManager implements AuctionTypeManagerInterface {
    $bid_type = $auction->getAuctionType()->getBidType();
    $plugin_definition = $this->getBidTypePluginDefinition($bid_type);
    $total_bids = $this->getAuctionBidsCount($auction);
    $initial_bid = $auction->getFirstBid();
    $last_bid = $auction->getLastBid();
    $context = [];
    // Allow other modules to alter/add contexts.
    $this->moduleHandler->invokeAll(
@@ -290,6 +292,8 @@ class AuctionTypeManager implements AuctionTypeManagerInterface {
      ->fields([
        'total_amount_bids' => $total_bids,
        'timestamp' => time(),
        'initial_bid' => $initial_bid ? $initial_bid->id() : NULL,
        'last_bid' => $last_bid ? $last_bid->id() : NULL,
        'context' => implode(',', array_values($context)),
      ])
      ->execute();
@@ -326,6 +330,7 @@ class AuctionTypeManager implements AuctionTypeManagerInterface {
      $this->database->merge('auctioneer_auction_hammer')
        ->key('bid', $bid->id())
        ->fields([
          'auction' => $auction->id(),
          'uid' => $this->account->id(),
          'timestamp' => time(),
        ])
+75 −0
Changes for src/Entity/AuctionViewsData.php: 75 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -49,6 +49,52 @@ class AuctionViewsData extends EntityViewsData {
        'id' => 'date',
      ],
    ];
    $data['auctioneer_auction_statistics']['initial_bid'] = [
      'title' => $this->t('Initial bid'),
      'help' => $this->t('The initial (first) bid placed to the auction.'),
      'relationship' => [
        'title' => $this->t('Initial bid'),
        'base' => 'bid_field_data',
        'base field' => 'id',
        'id' => 'standard',
        'label' => $this->t('Initial bid'),
      ],
      'field' => [
        'id' => 'numeric',
      ],
      'filter' => [
        'id' => 'numeric',
      ],
      'sort' => [
        'id' => 'standard',
      ],
      'argument' => [
        'id' => 'standard',
      ],
    ];
    $data['auctioneer_auction_statistics']['last_bid'] = [
      'title' => $this->t('Last bid'),
      'help' => $this->t('The last bid placed to the auction.'),
      'relationship' => [
        'title' => $this->t('Last bid'),
        'base' => 'bid_field_data',
        'base field' => 'id',
        'id' => 'standard',
        'label' => $this->t('Last bid'),
      ],
      'field' => [
        'id' => 'numeric',
      ],
      'filter' => [
        'id' => 'numeric',
      ],
      'sort' => [
        'id' => 'standard',
      ],
      'argument' => [
        'id' => 'standard',
      ],
    ];
    $data['auctioneer_auction_statistics']['context'] = [
      'title' => $this->t('Context'),
      'help' => $this->t('Comma-separated list of contexts.'),
@@ -62,6 +108,35 @@ class AuctionViewsData extends EntityViewsData {
        'id' => 'string',
      ],
    ];
    $data['auctioneer_auction_hammer']['table']['group'] = $this->t('Hammer', [], ['context' => 'auction hammer bids']);
    $data['auctioneer_auction_hammer']['table']['join']['auction_field_data'] = [
      'type' => 'LEFT',
      'left_field' => 'id',
      'field' => 'auction',
    ];
    $data['auctioneer_auction_hammer']['bid'] = [
      'title' => $this->t('Hammer bid'),
      'help' => $this->t('The hammer-marked (winner) bid.'),
      'relationship' => [
        'title' => $this->t('Hammer bid'),
        'base' => 'bid_field_data',
        'base field' => 'id',
        'id' => 'standard',
        'label' => $this->t('Hammer bid'),
      ],
      'field' => [
        'id' => 'numeric',
      ],
      'filter' => [
        'id' => 'numeric',
      ],
      'sort' => [
        'id' => 'standard',
      ],
      'argument' => [
        'id' => 'standard',
      ],
    ];

    return $data;
  }
+23 −0
Changes for src/Entity/BidViewsData.php: 23 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -30,6 +30,29 @@ class BidViewsData extends EntityViewsData {
        'id' => 'is_hammer_bid',
      ],
    ];
    $data['auctioneer_auction_hammer']['uid'] = [
      'title' => $this->t('Hammer author'),
      'help' => $this->t('The user who marked this bid as winner.'),
      'relationship' => [
        'title' => $this->t('Hammer author'),
        'base' => 'users_field_data',
        'base field' => 'uid',
        'id' => 'standard',
        'label' => $this->t('Hammer author'),
      ],
      'field' => [
        'id' => 'numeric',
      ],
      'filter' => [
        'id' => 'numeric',
      ],
      'sort' => [
        'id' => 'standard',
      ],
      'argument' => [
        'id' => 'standard',
      ],
    ];

    return $data;
  }