D7UcRolesLicense.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. namespace Drupal\materio_migrate\Plugin\migrate\source;
  3. use Drupal\migrate\Row;
  4. use Drupal\migrate_drupal\Plugin\migrate\source\DrupalSqlBase;
  5. /**
  6. * Drupal 7 Ubercart roles expiration source.
  7. *
  8. * This is for migrating roles granted with the uc_roles module to licenses.
  9. *
  10. * This class is just a starting point:
  11. * - The expiration settings are not handled. In particular, for licenses used
  12. * with subscriptions, the expiry should be set to 'unlimited'.
  13. * - The case of more than one product selling the same role is not handled.
  14. * - Assumptions are made about renewals.
  15. *
  16. * @MigrateSource(
  17. * id = "d7_uc_license_role",
  18. * source_module = "uc_roles"
  19. * )
  20. */
  21. class D7UcRolesLicense extends DrupalSqlBase {
  22. /**
  23. * {@inheritdoc}
  24. */
  25. public function query() {
  26. $query = $this->select('uc_roles_expirations', 'ure')
  27. ->fields('ure', [
  28. 'reid',
  29. 'uid',
  30. 'rid',
  31. 'expiration',
  32. ])
  33. // ->condition('ure.rid', 6)
  34. ->orderBy('ure.expiration');
  35. // // Joining to {uc_roles_products} gets us the product node ID and the
  36. // // duration configuration.
  37. // // TODO: this join assumes there is only one product per role. If some
  38. // // roles are sold by multiple products, this will break!
  39. // $query->innerJoin('uc_roles_products', 'urp', 'ure.rid = urp.rid');
  40. // $query->fields('urp', [
  41. // 'nid',
  42. // 'duration',
  43. // 'granularity',
  44. // ]);
  45. // // Get the orders that purchased this product.
  46. // // Join to {uc_orders} via {uc_order_products}, getting first the order
  47. // // line items that hold the product, and the the corresponding order.
  48. // $query->innerJoin('uc_order_products', 'uop', 'urp.nid = uop.nid');
  49. // // This join also ensures that the orders are purchased by the users who
  50. // // have a role granted.
  51. // $query->innerJoin('uc_orders', 'uo', 'uop.order_id = uo.order_id AND ure.uid = uo.uid');
  52. //
  53. // $query->fields('uop', ['order_product_id']);
  54. // $query->fields('uo', [
  55. // 'created',
  56. // 'modified',
  57. // 'order_id',
  58. // ]);
  59. // // Use a groupwise mininum selfjoin to get only the earliest order by each
  60. // // user for a role.
  61. // // TODO: this assumes that later orders are renewals, and that there are no
  62. // // gaps in a user's license ownership, e.g. user buys license, lets it
  63. // // expire, buys another one.
  64. // // TODO: this join should also have a condition on the product nid!
  65. // $query->leftJoin('uc_orders', 'uo_later', 'uo.uid = uo_later.uid AND uo.modified > uo_later.modified');
  66. // $query->isNull('uo_later.order_id');
  67. return $query;
  68. }
  69. /**
  70. * {@inheritdoc}
  71. */
  72. public function prepareRow(Row $row) {
  73. // drush_print("_ _ _ _ _");
  74. // drush_print('uid '. $row->getSourceProperty('uid'));
  75. // // Get the most recent order for this user and role product, if different
  76. // // from the earliest order we retrieved in query().
  77. // $query = $this->select('uc_order_products', 'uop');
  78. // $query->innerJoin('uc_orders', 'uo', 'uop.order_id = uo.order_id');
  79. // // $query->condition('uop.nid', $row->getSourceProperty('nid'));
  80. // $query->condition('uo.uid', $row->getSourceProperty('uid'));
  81. // // $query->condition('uo.order_id', $row->getSourceProperty('order_id'), '<>');
  82. // $query->fields('uo', [
  83. // 'order_id',
  84. // 'created',
  85. // 'modified',
  86. // ]);
  87. // $query->orderBy('created', DESC);
  88. // $query->range(0, 1);
  89. //
  90. // $latest_order_data = $query->execute()->fetchAssoc();
  91. // // print_r($latest_order_data);
  92. //
  93. // if ($latest_order_data) {
  94. // // drush_print('renewed ' . $latest_order_data['created']);
  95. // // Set the date of the last renewal to the creation date of the most
  96. // // recent order for this role.
  97. // // This is the closest thing we have?
  98. // $row->setSourceProperty('renewed', $latest_order_data['created']);
  99. // // drush_print('renewed ' . date('Y-m-d', $row->getSourceProperty('renewed')));
  100. // }
  101. // else {
  102. // // drush_print('renewed NULL');
  103. // $row->setSourceProperty('renewed', NULL);
  104. // // drush_print('renewed NULL');
  105. // }
  106. $expiration = $row->getSourceProperty('expiration');
  107. $state = $expiration > time() ? 'active' : 'expired';
  108. $row->setSourceProperty('state', $state);
  109. // Get orders for this user
  110. // will get the first and the last if diffents.
  111. $query = $this->select('uc_order_products', 'uop');
  112. $query->innerJoin('uc_orders', 'uo', 'uop.order_id = uo.order_id');
  113. $query->condition('uo.uid', $row->getSourceProperty('uid'));
  114. $query->fields('uo', [
  115. 'order_id',
  116. 'created',
  117. 'modified',
  118. ]);
  119. $query->orderBy('created', 'DESC');
  120. $orders = $query->execute()->fetchAll();
  121. // print_r($orders);
  122. // default renewed value
  123. $renewed = strtotime('-1 year', $expiration);
  124. $row->setSourceProperty('created', $renewed);
  125. $row->setSourceProperty('renewed', $renewed);
  126. // "real" created and renewed values
  127. if(count($orders)){
  128. $first_order = array_shift($orders);
  129. $row->setSourceProperty('created', $first_order['created']);
  130. $row->setSourceProperty('renewed', $first_order['created']);
  131. if(count($orders)){
  132. $last_order = array_pop($orders);
  133. $row->setSourceProperty('renewed', $last_order['created']);
  134. }
  135. }
  136. // $expirationstr = date('Y-m-d', $expiration);
  137. // drush_print('expiration ' . $expirationstr);
  138. return parent::prepareRow($row);
  139. }
  140. /**
  141. * {@inheritdoc}
  142. */
  143. public function fields() {
  144. return ([
  145. 'reid' => $this->t('Record ID'),
  146. 'uid' => $this->t('User ID'),
  147. 'rid' => $this->t('The role ID'),
  148. 'expiration' => $this->t('The expiration date'),
  149. 'nid' => $this->t('Product node ID'),
  150. 'duration' => $this->t('The interval multiplier'),
  151. 'granularity' => $this->t('The interval'),
  152. 'created' => $this->t('Earliest order created time'),
  153. 'modified' => $this->t('Earliest order changed time'),
  154. 'renewed' => $this->t('Latest order created time'),
  155. ]);
  156. }
  157. /**
  158. * {@inheritdoc}
  159. */
  160. public function getIds() {
  161. return [
  162. 'reid' => [
  163. 'type' => 'integer',
  164. 'alias' => 'ure',
  165. ]
  166. // Add the order product ID as a key, so that order product migrations
  167. // can look up the license to reference it.
  168. // 'order_product_id' => [
  169. // 'type' => 'integer',
  170. // ],
  171. ];
  172. }
  173. }