/[drupal]/contributions/modules/family/import.inc
ViewVC logotype

Contents of /contributions/modules/family/import.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.9 - (show annotations) (download) (as text)
Sat Dec 6 03:18:15 2008 UTC (11 months, 2 weeks ago) by pyutaros
Branch: MAIN
CVS Tags: HEAD
Changes since 1.8: +6 -2 lines
File MIME type: text/x-php
#322607 by Microbe:  Ascendants and Descendants tabs appear on every node type
#326432 by Microbe:  New 6.x-1.0-beta2 instalation - warning: mysql_fetch_array()
#339263 by Microbe:  Misspelling of word "Birth" in table
#339265 by Microbe:  Marriage type fields requested, in addition to "Religious"
#331459 by Microbe:  GEDCOM data confused on import
1 <?php
2 // $Id: import.inc,v 1.7.2.2 2008/10/09 23:48:19 pyutaros Exp $
3 // import.inc
4 // Functions for importing GEDCOM files to database
5 // Using the data base defined in simple.mysql
6 // This may also be used as a temporary storage before making more processing
7 // for import to other database format.
8
9 //Generate a form for uploading a GEDCOM file
10 function family_import() {
11 $content='';
12 //$content.='Import_view';
13 $content.=drupal_get_form('family_import_form');
14 return $content;
15 }
16
17 function family_import_form() {
18 $form['#attributes'] = array('enctype' => "multipart/form-data");
19 $form['gedcom_file'] = array(
20 '#type' => 'file',
21 '#title' => t('GED file to upload'),
22 '#size' => 40,
23 );
24 //$form['merge'] = array(
25 // '#type' => 'radios',
26 // '#title' => t('Merge options'),
27 // '#options' => array(t('replace existing data'), t('augment current data'), t('merge individuals by name')),
28 // '#default_value' => variable_get('family_import_replace', 1),
29 //);
30
31 $form['range'] = array(
32 '#type' => 'fieldset',
33 '#title' => t('Import range'),
34 '#description' => t('Select a range of records (lines staring with 0) to import. This allows breaking very large files into multiple import sessions.'),
35 );
36 $form['range']['start'] = array(
37 '#type' => 'textfield',
38 '#title' => t('First record to import'),
39 '#size' => 10,
40 '#maxlength' => 10,
41 '#description' => t('Enter the number of the first record in the GEDCOM file to include in this import session'),
42 );
43 $form['range']['nrecords'] = array(
44 '#type' => 'textfield',
45 '#title' => t('Number of records to import'),
46 '#size' => 10,
47 '#maxlength' => 10,
48 '#description' => t('Enter the number of records to process in this import session'),
49 );
50 $form['replace'] = array(
51 '#type' => 'checkbox',
52 '#title' => t('Replace existing GED data'),
53 '#default_value' => variable_get('family_import_replace', 1),
54 );
55 $form['submit'] = array('#type' => 'submit', '#value' => t('Start Import'));
56 $form['#submit'] = array('family_import_submit');
57 return $form;
58 }
59
60 // Check the uploaded GEDCOM file
61 function family_import_submit($form, &$form_state) {
62 if (!($file = file_save_upload('gedcom_file'))) {
63 form_set_error('',t("Didn't get GED file"));
64 }else{
65 if (!($fp = fopen($file->filepath , "r"))) {
66 form_set_error('',t("Couldn't open get GED file"));
67 }else{
68
69
70 //
71 // Empty current content. This is useful for debugging, but more caution should be
72 // done before deleting database in the working version
73 //
74 db_query("CREATE TABLE {family_relations_temp} (`nid` VARCHAR( 128 ) NOT NULL ,`famc_xref` VARCHAR( 128 ) NOT NULL ,`fams_xref` VARCHAR( 128 ) NOT NULL) ENGINE = MYISAM");
75 if ($form['replace']) {
76 db_query("TRUNCATE {family_individual}");
77 db_query("TRUNCATE {family_group}");
78 db_query("TRUNCATE {family_location}");
79 db_query("TRUNCATE {family_variable}");
80
81
82 $q = db_query("SELECT nid FROM {node} WHERE type = 'family_individual'");
83 $n = 0;
84 while ($o = db_fetch_array($q)) {
85 node_delete($o->nid);
86 $n++;
87 }
88 drupal_set_message(t('Deleted @n family_individual nodes.', array('@n' => $n)));
89
90 $q = db_query("SELECT nid FROM {node} WHERE type = 'family_group'");
91 $n = 0;
92 while ($o = db_fetch_array($q)) {
93 node_delete($o->nid);
94 $n++;
95 }
96 drupal_set_message(t('Deleted @n family_group nodes.', array('@n' => $n)));
97
98 $q = db_query("SELECT nid FROM {node} WHERE type = 'family_location'");
99 $n = 0;
100 while ($o = db_fetch_array($q)) {
101 node_delete($o->nid);
102 $n++;
103 }
104 drupal_set_message(t('Deleted @n family_location nodes.', array('@n' => $n)));
105 }
106
107 $rmin = $form['start']? $form['start']:0;
108 $rcount = $form['nrecords']? $form['nrecords']:99999999;
109 $rmax = $rmin + $rcount - 1;
110 $rnum = 0;
111 $rprocessed = 0;
112 $lprocessed = 0;
113
114 $lnum = 0;
115 $gedcom_hier=array(); // References to GEDCOM parents on each level
116
117 //declare variables for evaluation
118 $current0record = NULL;
119
120 while (!feof ($fp))
121 {
122 $gedline = fgets( $fp, 1024 );
123 $lnum++;
124
125 if (preg_match("/^\s*(\d+)\s*(?:@([^@]+)@)?\s*(\S+)\s*(.*\S)?\s*$/i", $gedline , $matches)) {
126 $level=$matches[1];
127 if ($level == 0) ++$rnum;
128 if ($rnum < $rmin) continue;
129 if ($rnum > $rmax) break;
130 if ($level == 0) ++$rprocessed;
131 ++$lprocessed;
132 $xref=$matches[2];
133 $fact_code=$matches[3];
134 $value=$matches[4];
135 $gedcom_source=$gedline;
136 $parent=$gedcom_hier[$level-1];
137
138 //new ged file evaluation code
139 //next line is debug
140 //echo $level . "<br>";
141 switch($level){
142 case '0':
143 $current0xref = $xref;
144 $current0record = $fact_code;
145 //next line is debug
146 //echo $current0record . "<br>";
147 switch($current0record){
148 case 'INDI':
149 //create title_format variable that has not yet been set.
150 $title_format = $firstname . " " . $middlename . " " . $lastname; //Will change with the implementation of tokens.
151 //next line is debug
152 //echo $title_format . "<br>";
153 //Create family_individual node
154 unset($node);
155 $node->type = family_individual;
156 $node->uid = $user->uid;
157 $node->title = $title_format;
158 $node->status = 1;
159 $node->moderate = 0;
160 $node->comment = 2;
161 $node->revision = 0;
162 node_validate($node, $error);
163 if (!node_access("create", $node)) {
164 $error['access'] = message_access();
165 }
166 if ($error) {
167 drupal_set_message(
168 t('Error at line @lnum of GED (@line): @error.',
169 array('@lnum' => $lnum, '@line' => $gedline, '@error' => print_r($error,true))
170 )
171 );
172 }
173 else {
174 $node->title=$title_format;
175 $node->FORE=$firstname;
176 $node->MIDN=$middlename;
177 $node->SURN=$lastname;
178 $node->SEX=$gender;
179 $node->BIRT_DATE=$birthdate;
180 $node->BIRT_PLAC=$birthplace;
181 $node->DEAT_DATE=$deathdate;
182 $node->DEAT_PLAC=$deathplace;
183 node_save($node);
184 $nid=$node->nid;
185 //Insert relationship variables into temporary database
186 db_query("INSERT INTO {family_relations_temp} (nid, famc_xref, fams_xref) VALUES (%d, '%s', '%s')", $nid, $famc_xref, $fams_xref);
187 //next line is debug
188 }
189 unset($node);
190 //unset all INDI variables
191 unset ($famc_xref);
192 unset ($fams_xref);
193 unset ($vid);
194 unset ($nid);
195 unset ($title_format);
196 unset ($firstname);
197 unset ($middlename);
198 unset ($lastname);
199 unset ($gender);
200 unset ($birthdate);
201 unset ($birthplace);
202 unset ($deathdate);
203 unset ($deathplace);
204 unset ($children_num);
205 break;
206 case 'FAM':
207 //Find the group surname shared by the children of the group. - This may be incorrect in cases where the name has not been passed to the children.
208 $current0xref='@'.$current0xref.'@';
209 $result = db_query("SELECT r.lastname FROM {family_individual} r INNER JOIN {family_relations_temp} t ON r.nid=t.nid WHERE t.famc_xref = '%s'", $current0xref);
210 $group_surname = db_fetch_object($result);
211 //Debug Line Below
212 //drupal_set_message(t('FAM XREF @n', array('@n' => $current0xref)));
213
214 //Find Parents of group
215 $result = db_query("SELECT nid FROM {family_relations_temp} WHERE fams_xref = '%s'", $current0xref);
216 while ($parent = db_fetch_array($result)) {
217 //Debug Line Below
218 //drupal_set_message(t('PARENT NID @n', array('@n' => $parent['nid'])));
219 $parents[] = $parent['nid'];
220 }
221 $parent1 = $parents[0];
222 $parent2 = $parents[1];
223
224 //create group node
225 unset($node);
226 $node->type = family_group;
227 $node->uid = $user->uid;
228 $node->title = $title_format;
229 $node->status = 1;
230 $node->moderate = 0;
231 $node->comment = 2;
232 $node->revision = 0;
233 node_validate($node, $error);
234 if (!node_access("create", $node)) {
235 $error['access'] = message_access();
236 }
237 if ($error) {
238 drupal_set_message(
239 t('Error at line @lnum of GED (@line): @error.',
240 array('@lnum' => $lnum, '@line' => $gedline, '@error' => print_r($error,true))
241 )
242 );
243 }
244 else {
245 $node->title=$title_format;
246 $node->MARR_TYPE=$marr_type;
247 $node->MARR_DATE=$marr_date;
248 $node->MARR_PLAC=$marr_plac;
249 $node->DIV_DATE=$div_date;
250 $node->DIV_PLAC=$div_plac;
251 $node->PAR1=$parent1;
252 $node->PAR2=$parent2;
253 node_save($node);
254 $nid=$node->nid;
255 //insert ancestor group value into INDI nodes related to this group
256 //db_query("UPDATE {family_individual} SET ancestor_group='%d' WHERE lastname='%s'", $nid, $group_surname);
257 //insert variables into family_group table
258 //db_query("INSERT INTO {family_group} (vid, nid, title_format, marr_type, marr_date, marr_plac, div_date, div_plac, parent1, parent2) VALUES (%d, %d, '%s', '%s', '%s', '%s', '%s', '%s', '%d', '%d')", $vid, $nid, $title_format, $marr_type, $marr_date, $marr_plac, $div_date, $div_plac, $parent1, $parent2);
259 //unset all FAM variables
260 $result = db_query("SELECT nid FROM {family_relations_temp} WHERE famc_xref = '%s'", $current0xref);
261 while ($child = db_fetch_array($result)) {
262 //Debug Line Below
263 //drupal_set_message(t('CHILD NID @n', array('@n' => $child['nid'])));
264 $childnid = $child['nid'];
265 db_query("UPDATE {family_individual} SET ancestor_group = %d WHERE nid =%d", $nid, $childnid);
266 }
267 }
268 unset($node);
269 unset ($current0xref);
270 unset ($child_ref_nid);
271 unset ($group_surname);
272 unset ($parent_surname);
273 unset ($parent1_firstname);
274 unset ($parent2_firstname);
275 unset ($vid);
276 unset ($nid);
277 unset ($title_format);
278 unset ($marr_type);
279 unset ($marr_date);
280 unset ($marr_plac);
281 unset ($div_date);
282 unset ($div_plac);
283 unset ($parent1);
284 unset ($parent2);
285 unset ($parents);
286 break;
287 }
288 $current0record = NULL;
289 //next line is debug
290 //echo $fact_code . "<br>";
291 switch($fact_code){
292 case 'FAM':
293 case 'INDI':
294 $current0record = $fact_code;
295 $current0xref = $xref;
296 break;
297 }
298 break;
299 case '1':
300 $current1record = $fact_code;
301 //next line is debug
302 //echo $current0record . "<br>";
303 switch($current0record){
304 case 'INDI':
305 //next line is debug
306 //echo $fact_code . "<br>";
307 switch($fact_code){
308 case 'SEX':
309 $gender = $value;
310 $current1record = $fact_code;
311 break;
312 case 'NCHI':
313 $children_num = $value;
314 $current1record = $fact_code;
315 break;
316 case 'NAME':
317 //split name value by / to separate surname
318 $splitName1 = explode("/", $value);
319 $lastname = $splitName1[1];
320 // split name by spaces
321 $splitName2 = explode(" ", $splitName1[0]);
322 // take the first name to be firstname
323 $firstname = $splitName2[0];
324 // add all the other names together in a string
325 $middlename = $splitName2[1] . " " . $splitName2[2] . " " . $splitName2[3] . " " . $splitName2[4] . " " . $splitName2[5] . " " . $splitName2[6] . " " . $splitName2[7];
326 $current1record = $fact_code;
327 break;
328 case 'DEAT':
329 case 'BIRT':
330 $current1record = $fact_code;
331 break;
332 case 'FAMS':
333 $fams_xref = $value;
334 break;
335 case 'FAMC':
336 $famc_xref = $value;
337 break;
338 }
339 break;
340 case 'FAM':
341 //next line is debug
342 //echo $fact_code . "<br>";
343 switch($fact_code){
344 case 'MARR':
345 case 'DIV':
346 $current1record = $fact_code;
347 break;
348 }
349 break;
350 }
351 break;
352 case '2':
353 $current2record = $fact_code;
354 //next line is debug
355 //echo $current1record . "<br>";
356 switch($current1record){
357 case 'BIRT':
358 //next line is debug
359 //echo $fact_code . "<br>";
360 switch($fact_code){
361 case 'DATE':
362 $birthdate = family_changeDateFormat($value);
363 break;
364 case 'PLAC':
365 $birthplace = $value;
366 break;
367 }
368 break;
369 case 'DEAT':
370 //next line is debug
371 //echo $fact_code . "<br>";
372 switch($fact_code){
373 case 'DATE':
374 $deathdate = family_changeDateFormat($value);
375 break;
376 case 'PLAC':
377 $deathplace = $value;
378 break;
379 }
380 break;
381 case 'MARR':
382 //next line is debug
383 //echo $fact_code . "<br>";
384 switch($fact_code){
385 case 'TYPE':
386 $marr_type = $value;
387 break;
388 case 'DATE':
389 $marr_date = family_changeDateFormat($value);
390 break;
391 case 'PLAC':
392 $marr_plac = $value;
393 break;
394 }
395 break;
396 case 'DIV':
397 //next line is debug
398 //echo $fact_code . "<br>";
399 switch($fact_code){
400 case 'DATE':
401 $div_date = family_changeDateFormat($value);
402 break;
403 case 'PLAC':
404 $div_plac = $value;
405 break;
406 }
407 break;
408 }
409 break;
410 }
411 }
412 }
413
414 fclose ($fp);
415 db_query("DROP TABLE {family_relations_temp}");
416 drupal_set_message(t('Processed @r records (@n lines) of GED.', array('@r' => $rprocessed, '@n' => $lprocessed)));
417 if ($rnum > $rmax) drupal_set_message(t('Next start record: @r.', array('@r' => $rmax + 1)));
418 else drupal_set_message(t('No more records to process'));
419
420 }}
421 return;
422 }
423

  ViewVC Help
Powered by ViewVC 1.1.2