D7UcRolesLicense.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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->fields('uop', ['model']);
  120. $query->orderBy('created', 'DESC');
  121. $orders = $query->execute()->fetchAll();
  122. // print_r($orders);
  123. // default renewed value
  124. $renewed = strtotime('-1 year', $expiration);
  125. $row->setSourceProperty('created', $renewed);
  126. $row->setSourceProperty('renewed', $renewed);
  127. // "real" created and renewed values
  128. if(count($orders)){
  129. $first_order = array_shift($orders);
  130. $row->setSourceProperty('created', $first_order['created']);
  131. $row->setSourceProperty('renewed', $first_order['created']);
  132. if(count($orders)){
  133. $last_order = array_pop($orders);
  134. $row->setSourceProperty('renewed', $last_order['created']);
  135. }else{
  136. $last_order = $first_order;
  137. }
  138. drush_print(print_r($last_order, true));
  139. // put the right product variation web OR web-showroom
  140. switch($last_order['model']){
  141. case "abo01-A":
  142. $product_variation_id = 2;
  143. break;
  144. case "abo01-B":
  145. case "abo01-C":
  146. $product_variation_id = 6;
  147. break;
  148. }
  149. if(isset($product_variation_id)){
  150. drush_print($row->getSourceProperty('mail').' product_variation_id: '.$product_variation_id);
  151. $row->setSourceProperty('product_variation_id', $product_variation_id);
  152. }
  153. }
  154. // $expirationstr = date('Y-m-d', $expiration);
  155. // drush_print('expiration ' . $expirationstr);
  156. return parent::prepareRow($row);
  157. }
  158. /**
  159. * {@inheritdoc}
  160. */
  161. public function fields() {
  162. return ([
  163. 'reid' => $this->t('Record ID'),
  164. 'uid' => $this->t('User ID'),
  165. 'rid' => $this->t('The role ID'),
  166. 'expiration' => $this->t('The expiration date'),
  167. 'nid' => $this->t('Product node ID'),
  168. 'duration' => $this->t('The interval multiplier'),
  169. 'granularity' => $this->t('The interval'),
  170. 'created' => $this->t('Earliest order created time'),
  171. 'modified' => $this->t('Earliest order changed time'),
  172. 'renewed' => $this->t('Latest order created time'),
  173. ]);
  174. }
  175. /**
  176. * {@inheritdoc}
  177. */
  178. public function getIds() {
  179. return [
  180. 'reid' => [
  181. 'type' => 'integer',
  182. 'alias' => 'ure',
  183. ]
  184. // Add the order product ID as a key, so that order product migrations
  185. // can look up the license to reference it.
  186. // 'order_product_id' => [
  187. // 'type' => 'integer',
  188. // ],
  189. ];
  190. }
  191. }