Advertisement
Guest User

Untitled

a guest
Oct 18th, 2019
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.07 KB | None | 0 0
  1. /**************************
  2. * Active Kids
  3. **************************/
  4.  
  5. public static function importActiveKids()
  6. {
  7. static $sportFieldMap = array(
  8. 'voucher_number_child_1' => 'voucher_to_be_used_towards',
  9. 'voucher_number_child_2' => 'voucher_to_be_used_towards_2',
  10. 'child_3_voucher_number' => 'voucher_to_be_used_towards_3'
  11. );
  12.  
  13. static $kidFieldMap = array(
  14. 'voucher_number_child_1' => array(
  15. 'voucher_number_child_1' => 'voucher_number',
  16. 'child_1_first_name' => 'child_first_name',
  17. 'child_1_last_name' => 'child_last_name',
  18. 'child_1_dob' => 'dob'
  19. ),
  20. 'voucher_number_child_2' => array(
  21. 'voucher_number_child_2' => 'voucher_number',
  22. 'child_2_first_name' => 'child_first_name',
  23. 'child_2_last_name' => 'child_last_name',
  24. 'child_2_dob' => 'dob'
  25. ),
  26. 'child_3_voucher_number' => array(
  27. 'child_3_voucher_number' => 'voucher_number',
  28. 'child_3_first_name' => 'child_first_name',
  29. 'child_3_last_name' => 'child_last_name',
  30. 'child_3_dob' => 'dob'
  31. )
  32. );
  33.  
  34. $fieldMap = array(
  35. 'date_time' => 'date_time',
  36. 'location_id' => 'location',
  37. 'parent_first_name' => 'first_name',
  38. 'parent_last_name' => 'last_name',
  39. 'mobile' => 'mobile',
  40. 'email' => 'email',
  41. 'admin_name' => 'admin_name',
  42. 'credit_applied' => 'credit_applied',
  43. 'date_processed' => 'date_credit_applied',
  44. 'comment' => 'comment',
  45. 'admin_comments' => 'admin_comment'
  46. );
  47.  
  48. $db = \JFactory::getDbo();
  49. $query = $db->getQuery(true);
  50.  
  51. $query->select('*')
  52. ->from('active_kids_voucher');
  53. $db->setQuery($query);
  54. $oldKids = $db->loadObjectList();
  55.  
  56. $query->clear()
  57. ->select('*')
  58. ->from('active_kids_submissions');
  59. $db->setQuery($query);
  60. $newKids = $db->loadObjectList();
  61.  
  62. $query->clear()
  63. ->select('*')
  64. ->from('active_kids_sport');
  65. $db->setQuery($query);
  66. $sports = $db->loadObjectList('sport');
  67.  
  68. $newRows = array();
  69.  
  70. foreach ($oldKids as $oldKid)
  71. {
  72. if ($oldKid->id === '1')
  73. {
  74. continue;
  75. }
  76.  
  77. $skipKids = array();
  78. $copyKids = array();
  79.  
  80. foreach ($newKids as $newKid)
  81. {
  82. foreach (array('voucher_number_child_1', 'voucher_number_child_2', 'child_3_voucher_number') as $oldVoucherField)
  83. {
  84. if (!empty($oldKid->$oldVoucherField) && $oldKid->$oldVoucherField === $newKid->voucher_number)
  85. {
  86. self::$app->enqueueMessage(\
  87. sprintf(
  88. 'SKIPPING: Voucher %s exists, old parent "%s %s", new parent "%s %s"',
  89. $newKid->voucher_number,
  90. $oldKid->parent_first_name,
  91. $oldKid->parent_last_name,
  92. $newKid->first_name,
  93. $newKid->last_name
  94. )
  95. );
  96.  
  97. $skipKids[] = $oldVoucherField;
  98. continue;
  99. }
  100. }
  101. }
  102.  
  103. foreach (array('voucher_number_child_1', 'voucher_number_child_2', 'child_3_voucher_number') as $oldVoucherField)
  104. {
  105. if (!empty($oldKid->$oldVoucherField) && !in_array($oldVoucherField, $skipKids))
  106. {
  107. $copyKids[] = $oldVoucherField;
  108. }
  109. else
  110. {
  111. /*
  112. self::$app->enqueueMessage(\
  113. sprintf(
  114. 'SKIPPING empty %s for parent "%s %s"',
  115. $oldVoucherField,
  116. $oldKid->parent_first_name,
  117. $oldKid->parent_last_name,
  118. )
  119. );
  120. */
  121. $skipKids[] = $oldVoucherField;
  122.  
  123. continue;
  124. }
  125. }
  126.  
  127. foreach ($copyKids as $oldVoucherField)
  128. {
  129. if (in_array($oldVoucherField, $skipKids))
  130. {
  131. continue;
  132. }
  133.  
  134. $fields = array();
  135. $oldSportField = $sportFieldMap[$oldVoucherField];
  136. $sportLabel = $oldKid->$oldSportField;
  137. $sportLabel = Worker::JSONtoData($sportLabel);
  138. $sportLabel = is_array($sportLabel) ? $sportLabel[0] : $sportLabel;
  139. $sport = ArrayHelper::getValue($sports, $sportLabel, '');
  140. $sportValue = !empty($sport) ? $sport->id : '';
  141.  
  142. $fields['voucher_to_be_used_towards'] = $sportValue;
  143.  
  144. foreach ($kidFieldMap[$oldVoucherField] as $oldFieldName => $newFieldName)
  145. {
  146. $fields[$newFieldName] = $oldKid->$oldFieldName;
  147. }
  148.  
  149. foreach ($fieldMap as $oldFieldName => $newFieldName)
  150. {
  151. $fields[$newFieldName] = $oldKid->$oldFieldName;
  152. }
  153.  
  154. if (count($copyKids) > 1)
  155. {
  156. $fields['review'] = '1';
  157. }
  158. else
  159. {
  160. $fields['review'] = '0';
  161. }
  162.  
  163. $fields['rejected'] = '0';
  164. $newRows[] = $fields;
  165.  
  166. self::$app->enqueueMessage(\
  167. sprintf(
  168. 'INSERTING %s kids for parent "%s %s"',
  169. count($copyKids),
  170. $oldKid->parent_first_name,
  171. $oldKid->parent_last_name,
  172. )
  173. );
  174. }
  175. }
  176.  
  177. self::$app->enqueueMessage(sprintf('Inserting %s new rows', count($newRows)));
  178.  
  179. foreach ($newRows as $newRow)
  180. {
  181. $query->clear()
  182. ->insert('active_kids_submissions');
  183.  
  184. foreach ($newRow as $fieldName => $fieldValue)
  185. {
  186. $query->set($db->qn($fieldName) . ' = ' . $db->quote($fieldValue));
  187. }
  188.  
  189. $db->setQuery($query);
  190.  
  191. try {
  192. $db->execute();
  193. //self::$app->enqueueMessage(sprintf('Query: %s', (string)$query));
  194. }
  195. catch (\Exception $e)
  196. {
  197. self::$app->enqueueMessage($e->getMessage());
  198. }
  199. }
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement