You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
315 lines
8.9 KiB
315 lines
8.9 KiB
8 years ago
|
<?php
|
||
|
/*
|
||
|
* Created on 01.09.2016
|
||
|
*
|
||
|
* @copyright (c) 2016 animegame.eu
|
||
|
* @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public Licence
|
||
|
*
|
||
|
*/
|
||
|
|
||
|
function getItemsMap() {
|
||
|
$items_map = array();
|
||
|
$qry = db_query('SELECT * from sp_item WHERE type != \'Teufels Frucht\'');
|
||
|
while( $row = mysql_fetch_assoc($qry) ) {
|
||
|
$row['table_name'] = 'sp_item';
|
||
|
$items_map['sp_item'][$row['id']] = $row;
|
||
|
}
|
||
|
|
||
|
$qry = db_query('SELECT * from item');
|
||
|
while( $row = mysql_fetch_assoc($qry)) {
|
||
|
$row['table_name'] = 'item';
|
||
|
$items_map['item'][$row['id']] = $row;
|
||
|
}
|
||
|
|
||
|
$qry = db_query('SELECT * from wochen_markt');
|
||
|
while( $row = mysql_fetch_assoc($qry) ) {
|
||
|
$row['name'] = $row['item'];
|
||
|
$row['table_name'] = 'wochen_markt';
|
||
|
$items_map['wochen_markt'][$row['id']] = $row;
|
||
|
}
|
||
|
return $items_map;
|
||
|
}
|
||
|
|
||
|
function item2Name($item) {
|
||
|
return $item['name'].'('.$item['table_name'].')';
|
||
|
}
|
||
|
|
||
|
function item2Value($item) {
|
||
|
return $item['id'].'_'.$item['table_name'];
|
||
|
}
|
||
|
|
||
|
function get_set_definitions() {
|
||
|
$items_map = getItemsMap();
|
||
|
|
||
|
$qry = db_query('SELECT * from sp_sets');
|
||
|
$sets = array();
|
||
|
while( $row = mysql_fetch_assoc($qry) ) {
|
||
|
$set = array();
|
||
|
$set['item'] = $items_map[$row['table_name']][$row['item_id']];
|
||
|
$dep_items = array();
|
||
|
$qry2 = db_query('SELECT * from sp_set_ingredient WHERE set_id = '.$row['set_id']);
|
||
|
while( $row2 = mysql_fetch_assoc($qry2) ) {
|
||
|
$dep_items[] = $items_map[$row2['table_name']][$row2['item_id']];
|
||
|
}
|
||
|
$set['deps'] = $dep_items;
|
||
|
$set['id'] = $row['set_id'];
|
||
|
$sets[] = $set;
|
||
|
}
|
||
|
return $sets;
|
||
|
}
|
||
|
|
||
|
function getSetDefinition($set_id) {
|
||
|
$sets = get_set_definitions();
|
||
|
foreach ($sets as $set) {
|
||
|
if ($set['id'] == $set_id) {
|
||
|
return $set;
|
||
|
}
|
||
|
}
|
||
|
return NULL;
|
||
|
}
|
||
|
|
||
|
function lookupItem($items, $itemIdHash) {
|
||
|
$pair = explode('_', $itemIdHash, 2);
|
||
|
return $items[$pair[1]][$pair[0]];
|
||
|
}
|
||
|
|
||
|
function getSetDefinitionFromRequest($request) {
|
||
|
$items = getItemsMap();
|
||
|
$craft_item = lookupItem($items, $request['crafted']);
|
||
|
$deps = array();
|
||
|
for ($i=0; $i<5;$i++) {
|
||
|
$dep_item = lookupItem($items, $request['ingredient_'.$i]);
|
||
|
if ( $dep_item !== NULL ) {
|
||
|
$deps[]=$dep_item;
|
||
|
}
|
||
|
}
|
||
|
$set['item'] = $craft_item;
|
||
|
$set['deps'] = $deps;
|
||
|
return $set;
|
||
|
}
|
||
|
|
||
|
|
||
|
function displayRezepte($request) {
|
||
|
switch ( $request['action'] ) {
|
||
|
case 'create':
|
||
|
displayRezepteCreateMenu($request);
|
||
|
break;
|
||
|
case 'edit':
|
||
|
displayRezepteEditMenu($request);
|
||
|
break;
|
||
|
case 'delete':
|
||
|
displayRezepteDeleteMenu($request);
|
||
|
break;
|
||
|
default:
|
||
|
displayRezepteMainMenu($request);
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
function displayRezepteMainMenu($request) {
|
||
|
?>
|
||
|
<table>
|
||
|
<tr>
|
||
|
<th colspan="3">Rezepte</th>
|
||
|
</tr>
|
||
|
<tr>
|
||
|
<th>Item</th><th width="50%">Zutaten</th><th>Action</th>
|
||
|
</tr>
|
||
|
<?php
|
||
|
$sets = get_set_definitions();
|
||
|
foreach ($sets as $set) {
|
||
|
$dep_item_names = array();
|
||
|
foreach( $set['deps'] as $dep_item ) {
|
||
|
$dep_item_names[]=$dep_item['name'];
|
||
|
}
|
||
|
echo '<tr><td>'.$set['item']['name'].'</td><td align="center">'.join(',',$dep_item_names).'</td><td align="center"><a href="?choose=rezept&action=edit&set='.$set['id'].'">Edit</a> | <a href="?choose=rezept&action=delete&set='.$set['id'].'">Delete</a></td></tr>';
|
||
|
}
|
||
|
?>
|
||
|
<tr><td colspan="3" align="center"><a href="?choose=rezept&action=create">Neues Rezept erstellen</a></td></tr>
|
||
|
<tr>
|
||
|
<td colspan="3"><a href="<?php echo $_SERVER['PHP_SELF']; ?>">Zum Hauptmenu</a>
|
||
|
</td>
|
||
|
</tr>
|
||
|
</table>
|
||
|
|
||
|
<?php
|
||
|
}
|
||
|
|
||
|
function itemEquals($lhs, $rhs) {
|
||
|
return $lhs['id'] == $rhs['id'] && $lhs['table_name'] == $rhs['table_name'];
|
||
|
}
|
||
|
|
||
|
function createRezeptEntry($set) {
|
||
|
$qry = db_query('Select max(set_id) FROM sp_sets');
|
||
|
$new_id = mysql_fetch_row($qry)[0];
|
||
|
do {
|
||
|
$new_id++;
|
||
|
db_query('INSERT sp_sets(set_id, table_name, item_id) values('.$new_id.',\''.$set['item']['table_name'].'\','.$set['item']['id'].')');
|
||
|
} while(mysql_affected_rows() == 0);
|
||
|
return $new_id;
|
||
|
}
|
||
|
|
||
|
function updateRezept($base, $edit) {
|
||
|
if ($base['id'] === NULL) {
|
||
|
$base['id'] = createRezeptEntry($edit);
|
||
|
} else {
|
||
|
if ( !itemEquals($base['item'], $edit['item']) ) {
|
||
|
db_query('UPDATE sp_sets SET table_name=\''.$edit['item']['table_name'].'\', item_id='.$edit['item']['id'].' WHERE set_id='.$base['id']);
|
||
|
}
|
||
|
}
|
||
|
if( $base['deps'] === NULL ) {
|
||
|
$base['deps'] = array();
|
||
|
}
|
||
|
|
||
|
$to_add = array();
|
||
|
// first add the items that are in edit, but not in base
|
||
|
foreach ($edit['deps'] as $new_dep) {
|
||
|
$add = true;
|
||
|
foreach ($base['deps'] as $old_dep) {
|
||
|
if (itemEquals($new_dep, $old_dep)) {
|
||
|
$add = false;
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
if($add) {
|
||
|
$to_add[]=$new_dep;
|
||
|
}
|
||
|
}
|
||
|
$to_remove = array();
|
||
|
foreach ($base['deps'] as $old_dep) {
|
||
|
$remove = true;
|
||
|
foreach ($edit['deps'] as $new_dep) {
|
||
|
if (itemEquals($old_dep, $new_dep)) {
|
||
|
$remove=false;
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
if($remove) {
|
||
|
$to_remove[]=$old_dep;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
foreach ($to_add as $item) {
|
||
|
db_query('INSERT sp_set_ingredient(set_id, table_name, item_id) values('.$base['id'].', \''.$item['table_name'].'\', '.$item['id'].')');
|
||
|
}
|
||
|
foreach ($to_remove as $item) {
|
||
|
db_query('DELETE FROM sp_set_ingredient WHERE set_id = '.$base['id'].' and table_name = \''.$item['table_name'].'\' and item_id = '.$item['id']);
|
||
|
}
|
||
|
return getSetDefinition($base['id']);
|
||
|
}
|
||
|
|
||
|
|
||
|
function displayRezepteEditMenu($request) {
|
||
|
$set = getSetDefinition($request['set']);
|
||
|
if ($set === NULL) {
|
||
|
displayErrorMessage("Rezept konnte nicht bearbeitet werden", "Set existiert nicht!", displayHistoryBackLink());
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
if ( isset($request['crafted']) ) {
|
||
|
$set_changes = getSetDefinitionFromRequest($request);
|
||
|
updateRezept($set, $set_changes);
|
||
|
$set = getSetDefinition($request['set']);
|
||
|
}
|
||
|
displayForm($request, $set);
|
||
|
}
|
||
|
|
||
|
function displayRezepteCreateMenu($request) {
|
||
|
if ( isset($request['crafted']) ) {
|
||
|
$set_changes = getSetDefinitionFromRequest($request);
|
||
|
$set = updateRezept(array(), $set_changes);
|
||
|
$request['set'] = $set['id'];
|
||
|
$request['action'] = 'edit';
|
||
|
displayForm($request, $set);
|
||
|
return;
|
||
|
}
|
||
|
displayForm($request, array());
|
||
|
}
|
||
|
|
||
|
function displayForm($request, $set) {
|
||
|
|
||
|
$itemsMap = getItemsMap();
|
||
|
?>
|
||
|
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
|
||
|
<input name="choose" value="<?php echo $request['choose']; ?>" type="hidden" />
|
||
|
<input name="action" value="<?php echo $request['action']; ?>" type="hidden" />
|
||
|
<input name="set" value="<?php echo $set['id']; ?>" type="hidden" />
|
||
|
|
||
|
<table>
|
||
|
<tr>
|
||
|
<th colspan="2">Rezept <?php echo $set['id']; ?></th>
|
||
|
</tr>
|
||
|
<tr>
|
||
|
<td>Item</td>
|
||
|
<td>
|
||
|
<select name="crafted"><?php
|
||
|
foreach ($itemsMap as $table) {
|
||
|
foreach ($table as $item) {
|
||
|
$selected = '';
|
||
|
if($set['item']['table_name'] == $item['table_name'] && $set['item']['id'] == $item['id']) {
|
||
|
$selected = 'selected="selected"';
|
||
|
}
|
||
|
|
||
|
echo '<option value="'.item2Value($item).'" '.$selected.'>'.item2Name($item).'</option>';
|
||
|
}
|
||
|
}
|
||
|
?>
|
||
|
</select>
|
||
|
</td>
|
||
|
</tr>
|
||
|
<?php
|
||
|
for ($i=0;$i<5;$i++){
|
||
|
?>
|
||
|
<tr>
|
||
|
<td>Zutat <?php echo $i+1; ?>:</td>
|
||
|
<td>
|
||
|
<?php
|
||
|
$dep_item = $set['deps'][$i];
|
||
|
echo '<select name="ingredient_'.$i.'">';
|
||
|
echo '<option value="">None</option>';
|
||
|
foreach ($itemsMap as $table) {
|
||
|
foreach ($table as $item) {
|
||
|
$selected = '';
|
||
|
if($dep_item['table_name'] == $item['table_name'] && $dep_item['id'] == $item['id']) {
|
||
|
$selected = 'selected="selected"';
|
||
|
}
|
||
|
|
||
|
echo '<option value="'.item2Value($item).'" '.$selected.'>'.item2Name($item).'</option>';
|
||
|
}
|
||
|
}
|
||
|
?>
|
||
|
</select>
|
||
|
|
||
|
</td>
|
||
|
<?php
|
||
|
}
|
||
|
?>
|
||
|
<tr>
|
||
|
<td colspan="2" align="center"><input type="submit" value="Ändern" /></td>
|
||
|
</tr>
|
||
|
<tr>
|
||
|
<td colspan="2"><a href="<?php echo $_SERVER['PHP_SELF']; ?>?choose=rezept">Zum Rezeptemenu</a>
|
||
|
</td>
|
||
|
</tr>
|
||
|
<tr>
|
||
|
<td colspan="2"><a href="<?php echo $_SERVER['PHP_SELF']; ?>">Zum Hauptmenu</a>
|
||
|
</td>
|
||
|
</tr>
|
||
|
</table>
|
||
|
</form>
|
||
|
<?php
|
||
|
}
|
||
|
|
||
|
|
||
|
function displayRezepteDeleteMenu($request) {
|
||
|
$set = getSetDefinition($request['set']);
|
||
|
if ($set === NULL) {
|
||
|
displayErrorMessage("Rezept konnte nicht gelöscht werden", "Set existiert nicht!", displayHistoryBackLink());
|
||
|
return;
|
||
|
}
|
||
|
db_query('DELETE FROM sp_sets WHERE set_id = '.$set['id']);
|
||
|
db_query('DELETE FROM sp_set_ingredient WHERE set_id = '.$set['id']);
|
||
|
displayRezepteMainMenu(array());
|
||
|
}
|
||
|
|