1<?php
2require_once("classes/DBUtils.class.php");
3require_once("classes/Import.class.php");
4
5$recipe_format = isset( $_REQUEST['recipe_format'] ) ? $_REQUEST['recipe_format'] : '';
6$mode = isset( $_REQUEST['mode'] ) ? $_REQUEST['mode'] : '';
7
8if (!$SMObj->checkAccessLevel("EDITOR")) {
9	die($LangUI->_('You do not have sufficient privileges to import recipes'));
10}
11
12$arr = array(
13			'XML' => $LangUI->_('PHPRecipeBook Format (XML)'),
14			'MM' => $LangUI->_('Meal Master')
15		);
16?>
17
18<table cellspacing="0" cellpadding="1" border="0" width="100%">
19<tr>
20	<td align="left" class="title"><?php echo $LangUI->_('Import Recipes');?></td>
21</tr>
22</table>
23<p>
24
25<?php if ($mode == "") { ?>
26
27<form action="index.php?m=recipes&a=import&mode=import" enctype="multipart/form-data" method="POST">
28<table cellpadding=2>
29	<tr>
30		<td ALIGN=left><?php echo $LangUI->_('File');?>: </td>
31		<td>
32			<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $g_rb_max_picture_size;?>">
33			<input type="file" name="import_file">
34		</td>
35		</tr><tr>
36		<td align=left><?php echo $LangUI->_('Format');?>: </td>
37		<td>
38			<?php echo DBUtils::arraySelect( $arr, 'recipe_format', 'size="1"', @$recipe_format );?>
39		</td>
40		</tr>
41		<tr>
42		<td colspan=2>
43			<input type=submit name="import" value="<?php echo $LangUI->_('Import');?>" class=button>
44		</td>
45	</tr>
46</table>
47</form>
48
49
50<?php
51} else if ($mode == "import") {
52	// Determine if the user has access to add new recipes, or edit this current one
53	if (!$SMObj->checkAccessLevel("AUTHOR")) {
54		die($LangUI->_('You do not have sufficient privileges to add/edit recipes'));
55	}
56
57	// confirm that we have an uploaded file
58	if (is_uploaded_file($_FILES['import_file']['tmp_name'])) {
59		$file = $_FILES['import_file']['tmp_name'];
60	} else {
61		echo $LangUI->_('Upload of file failed') . "<br />";
62	}
63
64	// Load the class and create the object
65	if ($recipe_format == 'XML') {
66		include_once("classes/Import_XML.class.php");
67		$importObj = new Import_XML($recipe_format);
68	} else if ($recipe_format == 'MM') {
69		include_once("classes/Import_MM.class.php");
70		$importObj = new Import_MM($recipe_format);
71	}
72
73	// Parse the file and import the data
74	echo $LangUI->_('Starting import...') . "<br />";
75	$importObj->parseDataFile($file);
76	// Check for duplicates
77	while (list($key, $val) = each($importObj->importRecipes)) {
78		if (recipe_name_exists($val[0]->name)) {
79			print "<font color=red>NOT importing ".$val[0]->name." since it already exists. sorry...</font><br />\n";
80			unset($importObj->importRecipes[$key]);
81		}
82    }
83    reset($importObj);
84	$importObj->importData();
85	echo $LangUI->_('Importing complete') . "<br />";
86}
87
88/*
89	Check to see if a recipe name already exists
90*/
91function recipe_name_exists($recipe_name) {
92    global $DB_LINK, $db_table_recipes;
93
94    $SQL = "SELECT recipe_name from $db_table_recipes where recipe_name = '".$DB_LINK->addq($recipe_name, get_magic_quotes_gpc())."'";
95    $rc = $DB_LINK->Execute($SQL);
96    DBUtils::checkResult($rc, NULL, NULL, $SQL);
97    if ($rc->RecordCount())
98      return true;
99    return false;
100}
101?>
102