www/import/from_brouwhulp.php

changeset 48
bdd1ec9b9c06
parent 45
95251bedfab4
child 49
4d27a7fb1265
equal deleted inserted replaced
47:94cd5ac04b6a 48:bdd1ec9b9c06
341 if ($style->STYLE_LETTER) 341 if ($style->STYLE_LETTER)
342 $sql .= "', style_letter='" . mysqli_real_escape_string($db, $style->STYLE_LETTER); 342 $sql .= "', style_letter='" . mysqli_real_escape_string($db, $style->STYLE_LETTER);
343 if ($style->STYLE_GUIDE) 343 if ($style->STYLE_GUIDE)
344 $sql .= "', style_guide='" . mysqli_real_escape_string($db, $style->STYLE_GUIDE); 344 $sql .= "', style_guide='" . mysqli_real_escape_string($db, $style->STYLE_GUIDE);
345 $sql .= "', type='" . $style->TYPE; 345 $sql .= "', type='" . $style->TYPE;
346 $sql .= "', og_min='" . $style->OG_MIN; 346 $sql .= "', og_min='" . floatval($style->OG_MIN);
347 $sql .= "', og_max='" . $style->OG_MAX; 347 $sql .= "', og_max='" . floatval($style->OG_MAX);
348 $sql .= "', fg_min='" . $style->FG_MIN; 348 $sql .= "', fg_min='" . floatval($style->FG_MIN);
349 $sql .= "', fg_max='" . $style->FG_MAX; 349 $sql .= "', fg_max='" . floatval($style->FG_MAX);
350 $sql .= "', ibu_min='" . $style->IBU_MIN; 350 $sql .= "', ibu_min='" . floatval($style->IBU_MIN);
351 $sql .= "', ibu_max='" . $style->IBU_MAX; 351 $sql .= "', ibu_max='" . floatval($style->IBU_MAX);
352 $sql .= "', color_min='" . $style->COLOR_MIN; 352 $sql .= "', color_min='" . floatval($style->COLOR_MIN);
353 $sql .= "', color_max='" . $style->COLOR_MAX; 353 $sql .= "', color_max='" . floatval($style->COLOR_MAX);
354 $sql .= "', carb_min='" . $style->CARB_MIN; 354 $sql .= "', carb_min='" . floatval($style->CARB_MIN);
355 $sql .= "', carb_max='" . $style->CARB_MAX; 355 $sql .= "', carb_max='" . floatval($style->CARB_MAX);
356 $sql .= "', abv_min='" . $style->ABV_MIN; 356 $sql .= "', abv_min='" . floatval($style->ABV_MIN);
357 $sql .= "', abv_max='" . $style->ABV_MAX; 357 $sql .= "', abv_max='" . floatval($style->ABV_MAX);
358 if ($style->PROFILE) 358 if ($style->PROFILE)
359 $sql .= "', profile='" . mysqli_real_escape_string($db, $style->PROFILE); 359 $sql .= "', profile='" . mysqli_real_escape_string($db, $style->PROFILE);
360 if ($style->INGREDIENTS) 360 if ($style->INGREDIENTS)
361 $sql .= "', ingredients='" . mysqli_real_escape_string($db, $style->INGREDIENTS); 361 $sql .= "', ingredients='" . mysqli_real_escape_string($db, $style->INGREDIENTS);
362 if ($style->EXAMPLES) 362 if ($style->EXAMPLES)
427 } 427 }
428 } 428 }
429 429
430 430
431 431
432 do_fermentables(); 432
433 do_hops(); 433 function do_recipes()
434 do_yeasts(); 434 {
435 do_miscs(); 435 global $brouwhulp, $db;
436 do_waters(); 436 $len_fermentables = 0;
437 do_equipments(); 437 $len_hops = 0;
438 do_styles(); 438 $len_miscs = 0;
439 do_mash(); 439 $len_waters = 0;
440 $len_mash = 0;
441
442 echo " Start adding recipes to the database\n";
443 $sql = "TRUNCATE TABLE recipes;";
444 if (! $result = mysqli_query($db, $sql)) {
445 printf("Error: %s\n", mysqli_error($db));
446 }
447
448 $recipes= simplexml_load_file($brouwhulp . '/recipes.xml');
449
450 foreach ($recipes->RECIPE as $recipe) {
451 $sql = "INSERT INTO recipes SET name='" . mysqli_real_escape_string($db, $recipe->NAME);
452 if ($recipe->NOTES)
453 $sql .= "', notes='" . mysqli_real_escape_string($db, $recipe->NOTES);
454 if ($recipe->TYPE)
455 $sql .= "', type='" . mysqli_real_escape_string($db, $recipe->TYPE);
456 if ($recipe->BATCH_SIZE)
457 $sql .= "', batch_size='" . floatval($recipe->BATCH_SIZE);
458 if ($recipe->BOIL_TIME)
459 $sql .= "', boil_time='" . floatval($recipe->BOIL_TIME);
460 if ($recipe->EFFICIENCY)
461 $sql .= "', efficiency='" . floatval($recipe->EFFICIENCY);
462 if ($recipe->EST_OG)
463 $sql .= "', est_og='" . floatval($recipe->EST_OG);
464 if ($recipe->EST_FG)
465 $sql .= "', est_fg='" . floatval($recipe->EST_FG);
466 if ($recipe->EST_COLOR)
467 $sql .= "', est_color='" . floatval($recipe->EST_COLOR);
468 if ($recipe->COLOR_METHOD)
469 $sql .= "', color_method='" . mysqli_real_escape_string($db, $recipe->COLOR_METHOD);
470 if ($recipe->IBU)
471 $sql .= "', est_ibu='" . floatval($recipe->IBU);
472 if ($recipe->IBU_METHOD)
473 $sql .= "', ibu_method='" . mysqli_real_escape_string($db, $recipe->IBU_METHOD);
474
475 if ($recipe->STYLE) {
476 if ($recipe->STYLE->NAME)
477 $sql .= "', style_name='" . mysqli_real_escape_string($db, $recipe->STYLE->NAME);
478 if ($recipe->STYLE->STYLE_LETTER)
479 $sql .= "', style_letter='" . mysqli_real_escape_string($db, $recipe->STYLE->STYLE_LETTER);
480 if ($recipe->STYLE->STYLE_GUIDE)
481 $sql .= "', style_guide='" . mysqli_real_escape_string($db, $recipe->STYLE->STYLE_GUIDE);
482 }
483
484 /*
485 * Put the fermentables in a json array
486 */
487 if ($recipe->FERMENTABLES) {
488 $fermentables = "[";
489 $comma = FALSE;
490 foreach ($recipe->FERMENTABLES->FERMENTABLE as $fermentable) {
491 if ($comma)
492 $fermentables .= ',';
493 $comma = TRUE;
494 $fermentables .= '{"name":"' . mysqli_real_escape_string($db, $fermentable->NAME) . '"';
495 $fermentables .= ',"amount":' . floatval($fermentable->AMOUNT);
496 $fermentables .= ',"cost":' . floatval($fermentable->COST);
497 $fermentables .= ',"type":"' . mysqli_real_escape_string($db, $fermentable->TYPE) . '"';
498 $fermentables .= ',"yield":' . floatval($fermentable->YIELD);
499 if ($fermentable->COLOR) {
500 $srm = floatval($fermentable->COLOR);
501 $ebc = srm_to_ebc($srm);
502 $fermentables .= ',"color":' . $ebc;
503 }
504 if ($fermentable->COARSE_FINE_DIFF) {
505 $fermentables .= ',"coarse_fine_diff":' . floatval($fermentable->COARSE_FINE_DIFF);
506 }
507 if ($fermentable->MOISTURE) {
508 $fermentables .= ',"moisture":' . floatval($fermentable->MOISTURE);
509 }
510 if ($fermentable->DIASTATIC_POWER) {
511 $fermentables .= ',"diastatic_power":' . floatval($fermentable->DIASTATIC_POWER);
512 }
513 if ($fermentable->PROTEIN) {
514 $fermentables .= ',"protein":' . floatval($fermentable->PROTEIN);
515 }
516 if ($fermentable->MAX_IN_BATCH) {
517 $fermentables .= ',"max_in_batch":' . floatval($fermentable->MAX_IN_BATCH);
518 }
519 if ($fermentable->GRAINTYPE) {
520 $fermentables .= ',"graintype":"' . mysqli_real_escape_string($db, $fermentable->GRAINTYPE) . '"';
521 }
522 if ($fermentable->ADDED) {
523 $fermentables .= ',"added":"' . mysqli_real_escape_string($db, $fermentable->ADDED) . '"';
524 }
525 if ($fermentable->DISSOLVED_PROTEIN) {
526 $fermentables .= ',"dissolved_protein":' . floatval($fermentable->DISSOLVED_PROTEIN);
527 }
528 ($fermentable->ADJUST_TO_TOTAL_100 == "TRUE") ? $fermentables .= ',"adjust_to_total_100":true' : $fermentables .= ',"adjust_to_total_100":false';
529 if ($fermentable->DI_pH) {
530 $fermentables .= ',"di_ph":' . floatval($fermentable->DI_pH);
531 }
532 $fermentables .= "}";
533 }
534
535 $fermentables .= ']';
536 // echo $fermentables . PHP_EOL;
537 $sql .= "', json_fermentables='" . $fermentables;
538 if (strlen($fermentables) > $len_fermentables)
539 $len_fermentables = strlen($fermentables);
540 }
541
542 /*
543 * Put the hops in a json array
544 */
545 if ($recipe->HOPS) {
546 $hops = "[";
547 $comma = FALSE;
548 foreach ($recipe->HOPS->HOP as $hop) {
549 if ($comma)
550 $hops .= ',';
551 $comma = TRUE;
552 $hops .= '{"name":"' . mysqli_real_escape_string($db, $hop->NAME) . '"';
553 $hops .= ',"amount":' . floatval($hop->AMOUNT);
554 $hops .= ',"cost":' . floatval($hop->COST);
555 $hops .= ',"type":"' . mysqli_real_escape_string($db, $hop->TYPE) . '"';
556 $hops .= ',"form":"' . mysqli_real_escape_string($db, $hop->FORM) . '"';
557 $hops .= ',"use":"' . mysqli_real_escape_string($db, $hop->USE) . '"';
558 if ($hop->TIME)
559 $hops .= ',"time":' . floatval($hop->TIME);
560 $hops .= ',"alpha":' . floatval($hop->ALPHA);
561 $hops .= ',"beta":' . floatval($hop->BETA);
562 $hops .= ',"hsi":' . floatval($hop->HSI);
563 $hops .= ',"humulene":' . floatval($hop->HUMULENE);
564 $hops .= ',"carophyllene":' . floatval($hop->CAROPHYLLENE);
565 $hops .= ',"cohumulone":' . floatval($hop->COHUMULONE);
566 $hops .= ',"myrcene":' . floatval($hop->MYRCENE);
567 if ($hop->TOTAL_OIL)
568 $hops .= ',"total_oil":' . floatval($hop->TOTAL_OIL);
569 $hops .= "}";
570 }
571
572 $hops .= ']';
573 // echo $hops . PHP_EOL;
574 $sql .= "', json_hops='" . $hops;
575 if (strlen($hops) > $len_hops)
576 $len_hops = strlen($hops);
577 }
578
579 /*
580 * Put the miscs in a json array
581 */
582 if ($recipe->MISCS) {
583 $miscs = "[";
584 $comma = FALSE;
585 foreach ($recipe->MISCS->MISC as $misc) {
586 if ($comma)
587 $miscs .= ',';
588 $comma = TRUE;
589 $miscs .= '{"name":"' . mysqli_real_escape_string($db, $misc->NAME) . '"';
590 $miscs .= ',"amount":' . floatval($misc->AMOUNT);
591 if ($misc->COST)
592 $miscs .= ',"cost":' . floatval($misc->COST);
593 $miscs .= ',"type":"' . mysqli_real_escape_string($db, $misc->TYPE) . '"';
594 $miscs .= ',"use":"' . mysqli_real_escape_string($db, $misc->USE) . '"';
595 ($misc->AMOUNT_IS_WEIGHT== "TRUE") ? $miscs .= ',"amount_is_weight":true' : $miscs.= ',"amount_is_weight":false';
596 if ($misc->TIME)
597 $miscs .= ',"time":' . floatval($misc->TIME);
598 $miscs .= "}";
599 }
600
601 $miscs .= ']';
602 echo $miscs . PHP_EOL;
603 $sql .= "', json_miscs='" . $miscs;
604 if (strlen($miscs) > $len_miscs)
605 $len_miscs = strlen($miscs);
606 }
607
608 $sql .= "';";
609 if (! $result = mysqli_query($db, $sql)) {
610 printf("Error: %s\n", mysqli_error($db));
611 }
612 }
613 echo "Fermentables: " . $len_fermentables . PHP_EOL;
614 echo "Hops: " . $len_hops . PHP_EOL;
615 echo "Miscs: " . $len_miscs . PHP_EOL;
616 echo "Waters: " . $len_waters . PHP_EOL;
617 echo "Mash: " . $len_mash . PHP_EOL;
618 }
619
620
621
622 //do_fermentables();
623 //do_hops();
624 //do_yeasts();
625 //do_miscs();
626 //do_waters();
627 //do_equipments();
628 //do_styles();
629 //do_mash();
630 do_recipes();
631
440 632
441 mysqli_close($db); 633 mysqli_close($db);
442 634
443 echo "Finished adding data\n"; 635 echo "Finished adding data\n";
444 636

mercurial