summaryrefslogtreecommitdiffstats
path: root/productedit.php
blob: c2b33fa41b44089770ea00e3e90e232288c3e29b (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
<?php 
	session_start();
	if(isset($_GET['catid']))
	{
		$catid = $_GET['catid'];
	}
	else
	{
		header('Location: categories.php');
	}
	if(!isset($_SESSION['username']))
	{
		header("Location: product.php?id={$catid}");
	}
	
	require_once('vendor.inc.php');
	
	if(isset($_GET['delete']))
	{
		$product = ProductQuery::create()->findOneById($_GET['delete']);
	}
	if(isset($_GET['modify']))
	{
		$type = 'modify';
		$product = ProductQuery::create()->findOneById($_GET['modify']);
	}
	if(isset($_GET['add']))
	{
		$type = "add";
		$product = new Product();
	}
	if(!is_object($product))
	{
		header("Location: products.php?id={$catid}");
	}
	if(isset($_GET['delete']))
	{
		$product->delete();
		header("Location: products.php?id={$catid}");
	}
	if(isset($_POST['name']) && isset($_POST['price']) && isset($_POST['width']) && isset($_POST['height']) && isset($_POST['desc']) && isset($_POST['cat']))
	{
		$product->setName($_POST['name']);
		$product->setPrice($_POST['price']);
		$product->setWidth($_POST['width']);
		$product->setHeight($_POST['height']);		
		$product->setDescription($_POST['desc']);
		
		if(is_array($_POST['cat']))
		{
			$catCollect = new Propel\Runtime\Collection\Collection();
			foreach ($_POST['cat'] as $categoryID)
			{
				$catCollect->set($categoryID, CategoryQuery::create()->findOneById($categoryID));
			}
			$product->setCategories($catCollect);
		}
		$product->save();
		header("Location: products.php?id={$catid}");
	}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Production</title>
<link rel="stylesheet" type="text/css" href="styles/style.css">
</head>
<body>
	<a href="products.php?id=<?php echo $catid; ?>" accesskey="b"><em>B</em>ack</a>
	<form action="productedit.php?<?php echo "{$type}={$product->getId()}" ?>&catid=<?php echo $catid; ?>" method="post">
		<h1>Product</h1>
		<div class="row">
			<label for="name"><u>N</u>ame:</label>
			<input class="formw" type="text"
				placeholder="Name" id="name" name="name" accesskey="n"
				maxlength="100" value="<?php echo $product->getName(); ?>">
		</div>
		<div class="row">
			<label for="price"><u>P</u>rice (Euro):</label>
			<input class="formw" type="number" step="0.01" min="0"
				placeholder="Price (Euro)" id="price" name="price" accesskey="p"
				maxlength="13" value="<?php echo $product->getPrice(); ?>">
		</div>
		<div class="row">
			<label for="width"><u>W</u>idth (mm):</label>
			<input class="formw" type="number"
				placeholder="Width (mm)" id="width" name="width" accesskey="w"
				maxlength="11" value="<?php echo $product->getWidth(); ?>">
		</div>
		<div class="row">
			<label for="height"><u>H</u>eight (mm):</label>
			<input class="formw" type="number"
				placeholder="Height (mm)" id="height" name="height" accesskey="h"
				maxlength="11" value="<?php echo $product->getHeight(); ?>">
		</div>
		<div class="row">
			<label for="desc"><u>D</u>escription:</label>
			<textarea class="formw" placeholder="Description" id="desc" name="desc"
				accesskey="d" maxlength="1000"><?php echo $product->getDescription(); ?></textarea>
		</div>
		<div class="row">
			<label for="cat"><u>D</u>escription:</label>
			<select multiple class="formw" id="cat" name="cat[]">
				<?php 
					foreach (CategoryQuery::create()->find() as $category)
					{
						echo "<option value=\"{$category->getId()}\" ";
						if($product->getCategories()->contains($category))
						{
							echo "selected";
						}
						echo ">{$category->getName()}</option>";
					}
				?>
				</select>
		</div>
		<div class="spacer">&nbsp;</div>
		<button type="submit">
			<?php 
			echo ucfirst($type);
			?>
		</button>
		<button type="reset">
			Reset
		</button>
	</form>
</body>
</html>