|
3 | 3 | use PhpOffice\PhpSpreadsheet\Calculation\Engineering\ConvertUOM; |
4 | 4 | use PhpOffice\PhpSpreadsheet\Helper\Sample; |
5 | 5 | use PhpOffice\PhpSpreadsheet\Settings; |
| 6 | +use PhpOffice\PhpSpreadsheet\Shared\StringHelper; |
6 | 7 |
|
7 | 8 | require __DIR__ . '/../Header.php'; |
8 | 9 |
|
|
12 | 13 |
|
13 | 14 | return; |
14 | 15 | } |
15 | | - |
| 16 | +$post = []; |
| 17 | +foreach (['category', 'quantity', 'fromUnit', 'toUnit'] as $value) { |
| 18 | + if (isset($_POST[$value])) { |
| 19 | + $post[$value] = StringHelper::convertToString($_POST[$value]); |
| 20 | + } |
| 21 | +} |
16 | 22 | $categories = ConvertUOM::getConversionCategories(); |
17 | | -$defaultCategory = $_POST['category'] ?? $categories[0]; |
| 23 | +$defaultCategory = $post['category'] ?? $categories[0]; |
18 | 24 | $units = []; |
19 | 25 | foreach ($categories as $category) { |
20 | 26 | $categoryUnits = ConvertUOM::getConversionCategoryUnitDetails($category)[$category]; |
|
34 | 40 | <div class="col-sm-10"> |
35 | 41 | <select name="category" class="form-select" onchange="this.form.submit()"> |
36 | 42 | <?php foreach ($categories as $category) { |
37 | | - echo "<option value=\"{$category}\" " . ((isset($_POST['category']) && $_POST['category'] === $category) ? 'selected' : '') . ">{$category}</option>", PHP_EOL; |
| 43 | + echo "<option value=\"{$category}\" " . ((isset($post['category']) && $post['category'] === $category) ? 'selected' : '') . ">{$category}</option>", PHP_EOL; |
38 | 44 | } ?> |
39 | 45 | </select> |
40 | 46 | </div> |
41 | 47 | </div> |
42 | 48 | <div class="mb-3 row"> |
43 | 49 | <label for="quantity" class="col-sm-2 col-form-label">Quantity</label> |
44 | 50 | <div class="col-sm-10"> |
45 | | - <input name="quantity" type="text" size="8" value="<?php echo (isset($_POST['quantity'])) ? htmlentities($_POST['quantity'], Settings::htmlEntityFlags()) : '1.0'; ?>"> |
| 51 | + <input name="quantity" type="text" size="8" value="<?php echo (isset($post['quantity'])) ? htmlentities($post['quantity'], Settings::htmlEntityFlags()) : '1.0'; ?>"> |
46 | 52 | </div> |
47 | 53 | </div> |
48 | 54 | <div class="mb-3 row"> |
49 | 55 | <label for="fromUnit" class="col-sm-2 col-form-label">From Unit</label> |
50 | 56 | <div class="col-sm-10"> |
51 | 57 | <select name="fromUnit" class="form-select"> |
52 | 58 | <?php foreach ($units[$defaultCategory] as $fromUnitCode => $fromUnitName) { |
53 | | - echo "<option value=\"{$fromUnitCode}\" " . ((isset($_POST['fromUnit']) && $_POST['fromUnit'] === $fromUnitCode) ? 'selected' : '') . ">{$fromUnitName}</option>", PHP_EOL; |
| 59 | + echo "<option value=\"{$fromUnitCode}\" " . ((isset($post['fromUnit']) && $post['fromUnit'] === $fromUnitCode) ? 'selected' : '') . ">{$fromUnitName}</option>", PHP_EOL; |
54 | 60 | } ?> |
55 | 61 | </select> |
56 | 62 | </div> |
|
60 | 66 | <div class="col-sm-10"> |
61 | 67 | <select name="toUnit" class="form-select"> |
62 | 68 | <?php foreach ($units[$defaultCategory] as $toUnitCode => $toUnitName) { |
63 | | - echo "<option value=\"{$toUnitCode}\" " . ((isset($_POST['toUnit']) && $_POST['toUnit'] === $toUnitCode) ? 'selected' : '') . ">{$toUnitName}</option>", PHP_EOL; |
| 69 | + echo "<option value=\"{$toUnitCode}\" " . ((isset($post['toUnit']) && $post['toUnit'] === $toUnitCode) ? 'selected' : '') . ">{$toUnitName}</option>", PHP_EOL; |
64 | 70 | } ?> |
65 | 71 | </select> |
66 | 72 | </div> |
|
74 | 80 |
|
75 | 81 | <?php |
76 | 82 | /** If the user has submitted the form, then we need to calculate the value and display the result */ |
77 | | -if (isset($_POST['quantity'], $_POST['fromUnit'], $_POST['toUnit'])) { |
78 | | - $quantity = $_POST['quantity']; |
79 | | - $fromUnit = $_POST['fromUnit']; |
80 | | - $toUnit = $_POST['toUnit']; |
| 83 | +if (isset($post['quantity'], $post['fromUnit'], $post['toUnit'])) { |
| 84 | + $quantity = $post['quantity']; |
| 85 | + $fromUnit = $post['fromUnit']; |
| 86 | + $toUnit = $post['toUnit']; |
81 | 87 | if (!is_numeric($quantity)) { |
82 | 88 | $helper->log('Quantity is not numeric'); |
83 | | - } elseif (isset($units[$_POST['category']][$fromUnit], $units[$_POST['category']][$toUnit])) { |
| 89 | + } elseif (isset($units[$post['category']][$fromUnit], $units[$post['category']][$toUnit])) { |
84 | 90 | /** @var float|string */ |
85 | 91 | $result = ConvertUOM::CONVERT($quantity, $fromUnit, $toUnit); |
86 | 92 |
|
87 | | - $helper->log("{$quantity} {$units[$_POST['category']][$fromUnit]} is {$result} {$units[$_POST['category']][$toUnit]}"); |
| 93 | + $helper->log("{$quantity} {$units[$post['category']][$fromUnit]} is {$result} {$units[$post['category']][$toUnit]}"); |
88 | 94 | } else { |
89 | 95 | $helper->log('Please enter quantity and select From Unit and To Unit'); |
90 | 96 | } |
|
0 commit comments