summaryrefslogtreecommitdiff
path: root/trunk/reprap/web/part-lister/controllers/import.php
blob: f4849e83ff489f13bef0cf9ba36c1044ffe49a32 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
<?
	class ImportController extends Controller
	{
		function home()
		{
			//run for as long as it takes.
			set_time_limit(0);
			
			//to keep the output for the view.
			ob_start();
			
			//create our structure.
			$sql = explode(";", file_get_contents(HOME_DIR . "/sql/structure.sql"));
			
			foreach ($sql AS $line)
			{
				$line = trim($line);
				if ($line){
					db()->query($line);
					if (db()->error())
						echo db()->error() . "<br/>";					
				}
			}
			echo "Database structure created.<br/><br/>";
			
			//import our data!  order is important here.
			loadCountries();
			loadSuppliers();
			loadUniqueParts();
			loadRawSheets();

			//dump our database to the file for others
			dumpDatabaseToFile();
			
			$this->set('content', ob_get_clean());
		}
	}
	
	
	function getRawSheetData($key, $id)
	{
		$url = "http://spreadsheets.google.com/pub?key={$key}&output=csv&gid={$id}";
	
		echo "<!-- getting $url -->\n";

		//open the file.
		$file = new File(TEMP_DIR);
		$fp = $file->open($url);
		while ($ar = fgetcsv($fp))
			$data[] = $ar;
	
		return $data;
	}

	function loadCountries()
	{
		global $SOURCE_DATA;

		//load all our country arrays.
		foreach ($SOURCE_DATA['countries'] AS $legend_row)
		{
			$data = getRawSheetData($legend_row[0], $legend_row[1]);
		
			array_shift($data);
			array_shift($data);
		
			foreach ($data AS $row)
			{
				$country = new Country();
				$country->set('name', $row[0]);
				$country->set('code', $row[1]);
				$country->set('group', $row[2]);
				$country->save();

				$count++;
			}
		}
		
		echo "Loaded {$count} countries.<br/><br/>\n";
	}
	
	function loadSuppliers()
	{
		global $SOURCE_DATA;

		//load all our supplier arrays.
		foreach ($SOURCE_DATA['suppliers'] AS $legend_row)
		{
			$data = getRawSheetData($legend_row[0], $legend_row[1]);
		
			#get rid of the first two lines of human text
			array_shift($data);
			array_shift($data);

			foreach ($data AS $row)
			{
				//create our supplier
				$supplier = new Supplier();
				$supplier->set('name', $row[0]);
				$supplier->set('prefix', $row[1]);
				$supplier->set('website', $row[2]);
				$supplier->set('description', $row[3]);
				$supplier->set('buy_link', $row[5]);
				$supplier->save();
			
				//add in our country relations
				/*
				$countries = explode(",", $row[4]);
				if (count($countries))
				{
					foreach ($countries AS $short)
					{
						$country = Country::lookupShort($short);
					
						$sc = new SupplierCountry();
						$sc->set('supplier_id', $supplier->id);
						$sc->set('country_id', $country->id);
						$sc->save();
					}
				}
				*/
			
				$count++;
				
				echo "Added supplier " . $supplier->get('name') . "<br/>\n";
			}
		}
		
		echo "<b>Added {$count} total suppliers.</b><br/><br/>\n";
	}
	
	function loadUniqueParts()
	{
		global $SOURCE_DATA;

		//load all our supplier arrays.
		foreach ($SOURCE_DATA['unique_parts'] AS $legend_row)
		{
			$data = getRawSheetData($legend_row[0], $legend_row[1]);
			
			//junk.
			if (!empty($data))
			{
				array_shift($data);
				array_shift($data);
			}
		
			if (!empty($data))
			{
				foreach ($data AS $row)
				{
					if ($row[1])
					{
						$count++;
						
						$part = new UniquePart();
						$part->set('name', $row[0]);
						$part->set('type', $row[1]);
						$part->set('description', $row[2]);
						$part->set('url', $row[3]);
						$part->set('units', $row[4]);
						$part->save();

						echo "Added unique part '" . $part->get('name') . "'<br/>\n";

						loadSuppliedParts($part, array_slice($row, 5));
					}
				}
			}
		}
		
		echo "<b>Added {$count} total unique parts.</b><br/><br/>\n";
	}
	
	function loadSuppliedParts($part, $data)
	{
		foreach ($data AS $name)
		{
			if ($name)
			{
				//check for quantity first
				if (preg_match("/^([A-Za-z]+):(.+)::([0-9]+)$/", $name, $matches))
				{
					$supplier = Supplier::lookupKey($matches[1]);
					$part_num = $matches[2];
					$quantity = $matches[3];
				}
				//otherwise, check for a normal 
				else if (preg_match("/^([A-Za-z]+):(.+)$/", $name, $matches))
				{
					$supplier = Supplier::lookupKey($matches[1]);
					$part_num = $matches[2];
					$quantity = 1;
				}

				if ($supplier instanceOf Supplier)
				{
					$sp = new SupplierPart();
					$sp->set('supplier_id', $supplier->id);
					$sp->set('part_id', $part->id);
					$sp->set('part_num', $part_num);
					$sp->set('url', $url);
					$sp->set('quantity', $quantity);
					$sp->save();
					
					echo db()->error();
					
					//echo "Added supplier " . $supplier->get('name') . "<br/>";
				}
				else
					echo "Couldn't find supplier for " . $part->get('name') . "!<br/>\n";
			}
		}
		//echo "<br/>\n";
	}
	
	function loadLegend()
	{
		global $SOURCE_DATA;
		
		$legend = array();

		//open our legend page with info on what sheets to use.
		foreach ($SOURCE_DATA['legend'] AS $legend_row)
		{
			$data = getRawSheetData($legend_row[0], $legend_row[1]);
			
			//discard the first two human rows.
			//array_shift($data);
			//array_shift($data);

			//get the data!
			foreach ($data AS $row)
			{
				//only add it if it has a name and number.
				if ($row[0] && (int)$row[1])
				{
					//the first one is our 'main sheet'
					if (!$legend['main_sheet'])
						$legend['main_sheet'] = $row[1];

					//load up our keyed array.	
					$legend['raw_keys'][$row[0]] = array($legend_row[0], $row[1], $row[2]);
				}
			}
		}

		return $legend;
	}
	
	function loadRawSheets()
	{
		$legend = loadLegend();
		
		if (!empty($legend['raw_keys']))
		{
			foreach ($legend['raw_keys'] AS $name => $legend_row)
			{
//				ob_end_flush();
				
				$count++;
				
				//if ($count == 2)
				//	die();
				
				//get our initial data.
				$data = getRawSheetData($legend_row[0], $legend_row[1]);

				//create our root node.
				$root = new RawPart();
				$root->set('raw_text', $name);
				$root->set('type', 'module');
				$unique = $root->lookupUnique();
				
				if ($unique instanceOf UniquePart)
				{
					$root->set('quantity', 1);
					$root->set('parent_id',0);
					$root->set('part_id', $unique->id);
					$root->save();
					
					//save it to our legend
					$legend = new LegendPart();
					$legend->set("unique_part_id", $unique->id);
					$legend->set("status", $legend_row[2]);
					$legend->save();
										
					echo "<b>Added root module " . $root->get('raw_text') . "</b><br>";
					
					//get rid of human junk
					array_shift($data);
					array_shift($data);

					//load them all.
					foreach ($data AS $row)
					{
						//get the normal data.
						$raw = new RawPart();
						$raw->set('raw_id', $row[0]);
						$raw->set('raw_text', $row[1]);
						$raw->set('quantity', $row[2]);
						$raw->set('type', $row[3]);
						
						//make sure we actually have a real row.
						if ($raw->get('raw_text') && $raw->get('type'))
						{
							//if we have an assembly set, and we're not an assembly ourselves, use the assembly as parent
							if (is_object($assembly) && $raw->get('type') != 'assembly')
								$raw->set('parent_id', $assembly->id);
							//otherwise, its the module.
							else
								$raw->set('parent_id', $root->id);

							//lookup our unique part
							$unique = $raw->lookupUnique();
							if ($unique instanceOf UniquePart)
								$raw->set('part_id', $unique->id);
							
							//dont forget to save our model!	
							$raw->save();

							echo "Added raw part " . $raw->get('raw_text') . "<br>";

							//if we're an assembly, then save it here.
							if ($raw->get('type') == 'assembly')
								$assembly = $raw;
						}
					}
					echo "<br/>\n";
					
					//forget past assemblies.
					unset($assembly);
				}

				//if ($count == 3)
				//	die();
			}			
		}
	}
	
	function dumpDatabaseToFile()
	{
		$cmd = "mysqldump -u " . RR_DB_USER . " --password=" . RR_DB_PASS . " -h " . RR_DB_HOST . " -P " . RR_DB_PORT . " " . RR_DB_NAME . " > " . WEB_DIR . "reprap-bill-of-materials.sql";
		system($cmd);
	}
?>