Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**************************
- * Active Kids
- **************************/
- public static function importActiveKids()
- {
- static $sportFieldMap = array(
- 'voucher_number_child_1' => 'voucher_to_be_used_towards',
- 'voucher_number_child_2' => 'voucher_to_be_used_towards_2',
- 'child_3_voucher_number' => 'voucher_to_be_used_towards_3'
- );
- static $kidFieldMap = array(
- 'voucher_number_child_1' => array(
- 'voucher_number_child_1' => 'voucher_number',
- 'child_1_first_name' => 'child_first_name',
- 'child_1_last_name' => 'child_last_name',
- 'child_1_dob' => 'dob'
- ),
- 'voucher_number_child_2' => array(
- 'voucher_number_child_2' => 'voucher_number',
- 'child_2_first_name' => 'child_first_name',
- 'child_2_last_name' => 'child_last_name',
- 'child_2_dob' => 'dob'
- ),
- 'child_3_voucher_number' => array(
- 'child_3_voucher_number' => 'voucher_number',
- 'child_3_first_name' => 'child_first_name',
- 'child_3_last_name' => 'child_last_name',
- 'child_3_dob' => 'dob'
- )
- );
- $fieldMap = array(
- 'date_time' => 'date_time',
- 'location_id' => 'location',
- 'parent_first_name' => 'first_name',
- 'parent_last_name' => 'last_name',
- 'mobile' => 'mobile',
- 'email' => 'email',
- 'admin_name' => 'admin_name',
- 'credit_applied' => 'credit_applied',
- 'date_processed' => 'date_credit_applied',
- 'comment' => 'comment',
- 'admin_comments' => 'admin_comment'
- );
- $db = \JFactory::getDbo();
- $query = $db->getQuery(true);
- $query->select('*')
- ->from('active_kids_voucher');
- $db->setQuery($query);
- $oldKids = $db->loadObjectList();
- $query->clear()
- ->select('*')
- ->from('active_kids_submissions');
- $db->setQuery($query);
- $newKids = $db->loadObjectList();
- $query->clear()
- ->select('*')
- ->from('active_kids_sport');
- $db->setQuery($query);
- $sports = $db->loadObjectList('sport');
- $newRows = array();
- foreach ($oldKids as $oldKid)
- {
- if ($oldKid->id === '1')
- {
- continue;
- }
- $skipKids = array();
- $copyKids = array();
- foreach ($newKids as $newKid)
- {
- foreach (array('voucher_number_child_1', 'voucher_number_child_2', 'child_3_voucher_number') as $oldVoucherField)
- {
- if (!empty($oldKid->$oldVoucherField) && $oldKid->$oldVoucherField === $newKid->voucher_number)
- {
- self::$app->enqueueMessage(\
- sprintf(
- 'SKIPPING: Voucher %s exists, old parent "%s %s", new parent "%s %s"',
- $newKid->voucher_number,
- $oldKid->parent_first_name,
- $oldKid->parent_last_name,
- $newKid->first_name,
- $newKid->last_name
- )
- );
- $skipKids[] = $oldVoucherField;
- continue;
- }
- }
- }
- foreach (array('voucher_number_child_1', 'voucher_number_child_2', 'child_3_voucher_number') as $oldVoucherField)
- {
- if (!empty($oldKid->$oldVoucherField) && !in_array($oldVoucherField, $skipKids))
- {
- $copyKids[] = $oldVoucherField;
- }
- else
- {
- /*
- self::$app->enqueueMessage(\
- sprintf(
- 'SKIPPING empty %s for parent "%s %s"',
- $oldVoucherField,
- $oldKid->parent_first_name,
- $oldKid->parent_last_name,
- )
- );
- */
- $skipKids[] = $oldVoucherField;
- continue;
- }
- }
- foreach ($copyKids as $oldVoucherField)
- {
- if (in_array($oldVoucherField, $skipKids))
- {
- continue;
- }
- $fields = array();
- $oldSportField = $sportFieldMap[$oldVoucherField];
- $sportLabel = $oldKid->$oldSportField;
- $sportLabel = Worker::JSONtoData($sportLabel);
- $sportLabel = is_array($sportLabel) ? $sportLabel[0] : $sportLabel;
- $sport = ArrayHelper::getValue($sports, $sportLabel, '');
- $sportValue = !empty($sport) ? $sport->id : '';
- $fields['voucher_to_be_used_towards'] = $sportValue;
- foreach ($kidFieldMap[$oldVoucherField] as $oldFieldName => $newFieldName)
- {
- $fields[$newFieldName] = $oldKid->$oldFieldName;
- }
- foreach ($fieldMap as $oldFieldName => $newFieldName)
- {
- $fields[$newFieldName] = $oldKid->$oldFieldName;
- }
- if (count($copyKids) > 1)
- {
- $fields['review'] = '1';
- }
- else
- {
- $fields['review'] = '0';
- }
- $fields['rejected'] = '0';
- $newRows[] = $fields;
- self::$app->enqueueMessage(\
- sprintf(
- 'INSERTING %s kids for parent "%s %s"',
- count($copyKids),
- $oldKid->parent_first_name,
- $oldKid->parent_last_name,
- )
- );
- }
- }
- self::$app->enqueueMessage(sprintf('Inserting %s new rows', count($newRows)));
- foreach ($newRows as $newRow)
- {
- $query->clear()
- ->insert('active_kids_submissions');
- foreach ($newRow as $fieldName => $fieldValue)
- {
- $query->set($db->qn($fieldName) . ' = ' . $db->quote($fieldValue));
- }
- $db->setQuery($query);
- try {
- $db->execute();
- //self::$app->enqueueMessage(sprintf('Query: %s', (string)$query));
- }
- catch (\Exception $e)
- {
- self::$app->enqueueMessage($e->getMessage());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement