Issue #2212819 by mikeryan: Update sample API for 2.6 changes
[project/migrate.git] / migrate_example_baseball / migrate_example_baseball.migrate.inc
1 <?php
2
3 /**
4 * @file
5 * A baseball game migration example.
6 */
7
8
9 /*
10 * You must implement hook_migrate_api(), setting the API level to 2, for
11 * your migration classes to be recognized by the Migrate module.
12 */
13 function migrate_example_baseball_migrate_api() {
14 $api = array(
15 'api' => 2,
16 'groups' => array(
17 'baseball' => array(
18 'title' => t('Baseball'),
19 ),
20 ),
21 );
22 return $api;
23 }
24
25 /**
26 * A migration that is reused for each source CSV file.
27 */
28 class GameBaseball extends Migration {
29 public function __construct($arguments) {
30 parent::__construct($arguments);
31 $this->description = t('Import box scores from CSV file.');
32
33 // Create a map object for tracking the relationships between source rows
34 $this->map = new MigrateSQLMap($this->machineName,
35 array(
36 'start_date' => array('type' => 'varchar',
37 'length' => 8,
38 'not null' => TRUE,
39 'description' => 'Start date',
40 ),
41 'home_team' => array('type' => 'varchar',
42 'length' => 255,
43 'not null' => TRUE,
44 'description' => 'Home team',
45 ),
46 'home_game_number' => array('type' => 'int',
47 'not null' => TRUE,
48 'description' => 'Home team game number',
49 ),
50 ),
51 MigrateDestinationNode::getKeySchema()
52 );
53
54 // Create a MigrateSource object, which manages retrieving the input data.
55 $this->source = new MigrateSourceCSV($arguments['source_file'], $this->csvcolumns(), array(), $this->fields());
56
57 $this->destination = new MigrateDestinationNode('migrate_example_baseball');
58
59 $this->addFieldMapping('title', 'title')
60 ->description('See prepareRow().');
61 $this->addFieldMapping('field_start_date', 'start_date');
62 $this->addFieldMapping('field_park', 'park_id');
63 $this->addFieldMapping('field_visiting_team', 'visiting_team');
64 $this->addFieldMapping('field_home_team', 'home_team');
65 $this->addFieldMapping('field_home_game_number', 'home_game_number');
66 $this->addFieldMapping('field_home_score', 'home_score');
67 $this->addFieldMapping('field_visiting_score', 'visiting_score');
68 $this->addFieldMapping('field_outs', 'outs');
69 $this->addFieldMapping('field_attendance', 'attendance');
70 $this->addFieldMapping('field_duration', 'duration')
71 ->defaultValue(NULL);
72 $this->addFieldMapping('field_home_pitcher', 'home_pitcher');
73 $this->addFieldMapping('field_visiting_pitcher', 'visiting_pitcher');
74 $this->addFieldMapping('field_home_batters', 'home_batters')
75 ->separator(',')
76 ->description('See prepareRow().');
77 $this->addFieldMapping('field_visiting_batters', 'visiting_batters')
78 ->separator(',')
79 ->description('See prepareRow().');
80 for ($i=1; $i <= 9; $i++ ) {
81 $this->addFieldMapping(NULL, "visiting_batter_$i")
82 ->description('Not needed since we use the multi-value field: visiting_batters.');
83 $this->addFieldMapping(NULL, "home_batter_$i")
84 ->description('Not needed since we use the multi-value field: home_batters.');
85 }
86 }
87
88 protected function csvcolumns() {
89 // Note: Remember to subtract 1 from column number at http://www.retrosheet.org/gamelogs/glfields.txt
90 $columns[0] = array('start_date', 'Date of game');
91 $columns[3] = array('visiting_team', 'Visiting team');
92 $columns[6] = array('home_team', 'Home team');
93 $columns[8] = array('home_game_number', 'Home team game number');
94 $columns[9] = array('home_score', 'Home score');
95 $columns[10] = array('visiting_score', 'Visiting score');
96 $columns[11] = array('outs', 'Length of game in outs');
97 $columns[16] = array('park_id', 'Ballpark ID');
98 $columns[17] = array('attendance', 'Attendance');
99 $columns[18] = array('duration', 'Duration in minutes');
100 for ($i=1; $i <= 9; $i++ ) {
101 $columns[103+3*$i] = array("visiting_batter_$i", "Visiting batter $i");
102 $columns[130+3*$i] = array("home_batter_$i", "Home batter $i");
103 }
104 $columns[102] = array('visiting_pitcher', 'Visiting starting pitcher');
105 $columns[104] = array('home_pitcher', 'Home starting pitcher');
106 return $columns;
107 }
108
109 public function prepareRow($row) {
110 // Collect all the batters into one multi-value field.
111 for ($i=1; $i <= 9; $i++ ) {
112 $key = "visiting_batter_$i";
113 $visiting_batters[] = $row->$key;
114 $key = "home_batter_$i";
115 $home_batters[] = $row->$key;
116 }
117 $row->visiting_batters = implode(',', $visiting_batters);
118 $row->home_batters = implode(',', $home_batters);
119 $row->title = "$row->home_team vs. $row->visiting_team. " . gmdate('M d, Y', strtotime($row->start_date));
120 }
121
122 /**
123 * Construct the machine name from the source file name.
124 */
125 protected function generateMachineName($class_name = NULL) {
126 return drupal_strtolower(pathinfo($this->arguments['source_file'],
127 PATHINFO_FILENAME));
128 }
129
130 public function fields() {
131 return array(
132 'title' => 'A composite field made during prepareRow().',
133 'home_batters' => 'An array of batters, populated during prepareRow().',
134 'visiting_batters' => 'An array of batters, populated during prepareRow().',
135 );
136 }
137 }