--- /dev/null
+- FE languages like i18n support
--- /dev/null
+-- phpMyAdmin SQL Dump
+-- version 5.2.0
+-- https://www.phpmyadmin.net/
+--
+-- Host: 127.0.0.1
+-- Generation Time: Sep 25, 2022 at 10:53 AM
+-- Server version: 8.0.27
+-- PHP Version: 8.1.8
+
+SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
+START TRANSACTION;
+SET time_zone = "+00:00";
+
+
+/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
+/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
+/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
+/*!40101 SET NAMES utf8mb4 */;
+
+--
+-- Database: `emere`
+--
+
+-- --------------------------------------------------------
+
+--
+-- Table structure for table `item`
+--
+
+CREATE TABLE `item` (
+ `id` int NOT NULL,
+ `fk_receipt` int NOT NULL,
+ `fk_market` int NOT NULL,
+ `fk_product` int NOT NULL,
+ `fk_list` int NOT NULL
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
+
+-- --------------------------------------------------------
+
+--
+-- Table structure for table `list`
+--
+
+CREATE TABLE `list` (
+ `id` int NOT NULL,
+ `name` varchar(64) COLLATE utf8mb4_bin NOT NULL,
+ `description` varchar(255) COLLATE utf8mb4_bin NOT NULL
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
+
+-- --------------------------------------------------------
+
+--
+-- Table structure for table `market`
+--
+
+CREATE TABLE `market` (
+ `id` int NOT NULL,
+ `name` varchar(128) COLLATE utf8mb4_bin NOT NULL
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
+
+-- --------------------------------------------------------
+
+--
+-- Table structure for table `product`
+--
+
+CREATE TABLE `product` (
+ `id` int NOT NULL,
+ `name` varchar(128) COLLATE utf8mb4_bin NOT NULL,
+ `manufacturer` varchar(128) COLLATE utf8mb4_bin NOT NULL,
+ `weight` int NOT NULL,
+ `itemcount` int NOT NULL,
+ `price` int NOT NULL
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
+
+-- --------------------------------------------------------
+
+--
+-- Table structure for table `receipt`
+--
+
+CREATE TABLE `receipt` (
+ `id` int NOT NULL,
+ `receiptdate` datetime NOT NULL
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
+
+--
+-- Indexes for dumped tables
+--
+
+--
+-- Indexes for table `item`
+--
+ALTER TABLE `item`
+ ADD PRIMARY KEY (`id`);
+
+--
+-- Indexes for table `list`
+--
+ALTER TABLE `list`
+ ADD PRIMARY KEY (`id`);
+
+--
+-- Indexes for table `market`
+--
+ALTER TABLE `market`
+ ADD PRIMARY KEY (`id`);
+
+--
+-- Indexes for table `product`
+--
+ALTER TABLE `product`
+ ADD PRIMARY KEY (`id`);
+
+--
+-- Indexes for table `receipt`
+--
+ALTER TABLE `receipt`
+ ADD PRIMARY KEY (`id`);
+
+--
+-- AUTO_INCREMENT for dumped tables
+--
+
+--
+-- AUTO_INCREMENT for table `item`
+--
+ALTER TABLE `item`
+ MODIFY `id` int NOT NULL AUTO_INCREMENT;
+
+--
+-- AUTO_INCREMENT for table `list`
+--
+ALTER TABLE `list`
+ MODIFY `id` int NOT NULL AUTO_INCREMENT;
+
+--
+-- AUTO_INCREMENT for table `market`
+--
+ALTER TABLE `market`
+ MODIFY `id` int NOT NULL AUTO_INCREMENT;
+
+--
+-- AUTO_INCREMENT for table `product`
+--
+ALTER TABLE `product`
+ MODIFY `id` int NOT NULL AUTO_INCREMENT;
+
+--
+-- AUTO_INCREMENT for table `receipt`
+--
+ALTER TABLE `receipt`
+ MODIFY `id` int NOT NULL AUTO_INCREMENT;
+COMMIT;
+
+/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
+/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
+/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
# timezone settings
const TIMEZONE = 'Europe/Berlin';
+# lang settings for number inputs
+const NUMBER_INPUT_LANG = "de-DE";
+
+# lang setting
+const FE_LANG = "en";
+
# path settings
const PATH_ABSOLUTE = '/home/some/path/emere/';
const PATH_LOGDIRECTORY = PATH_ABSOLUTE . '/log';
*/
class Item {
- /**
+
+ /**
* The DB object
*
* @var mysqli
--- /dev/null
+<?php
+/**
+ * emere
+ *
+ * Copyright (C) 2022 Johannes 'Banana' Keßler
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+/**
+ * Process the input data from adding or edit an item
+ */
+class ItemInput {
+ /**
+ * The DB object
+ *
+ * @var mysqli
+ */
+ private mysqli $_DB;
+
+ /**
+ * The data for this item
+ *
+ * @var array
+ */
+ private array $_data;
+
+ /**
+ * @param mysqli $databaseConnectionObject
+ */
+ public function __construct(mysqli $databaseConnectionObject) {
+ $this->_DB = $databaseConnectionObject;
+ }
+
+ /**
+ * Check for required fields, valid input and prepare the data
+ * @param array $data
+ * @return array
+ */
+ public function validateAndPrepare(array $data): array {
+ $status = true;
+ $error = array();
+
+
+ if(!empty($data)) {
+ if(!isset($data['receiptdate']) || empty($data['receiptdate']) || !Summoner::validate($data['receiptdate'], 'datetime')) {
+ $status = false;
+ $error[] = array('receiptdata' => '');
+ }
+ }
+
+ return array(
+ 'status' => $status,
+ 'error' => $error
+ );
+ }
+}
else {
return false;
}
- break;
+ break;
case 'nospace':
// text without any whitespace and special chars
$pattern = '/[\p{L}\p{N}\-_]/u';
break;
- case 'text':
+ case 'datetime':
+ // based on the format eg. 2022-09-25T22:00
+ $dt = DateTime::createFromFormat('Y-m-d\TH:i', $input);
+ if ($dt !== false) {
+ return(checkdate($dt->format('m'), $dt->format('d'), $dt->format('Y')));
+ }
+ break;
+
+
+ case 'text':
default:
$pattern = '/[\p{L}\p{N}\p{P}\p{S}\p{Z}\p{M}\s]/u';
}
<!DOCTYPE html>
-<html lang="en">
+<html lang="<?php echo FE_LANG; ?>">
<head>
<meta charset="utf-8">
<title><?php echo Summoner::ifset($TemplateData,'pageTitle').' - '; ?>emere</title>
<form method="post">
<fieldset>
- <label for="receipt">Receipt* <small>Just leave it blank to create a new based on data and market name.</small></label>
- <input name="fdata[receipt]" id="receipt" type="text" list="receiptList" autocomplete="off" required
+ <label for="receipt">Receipt <small>Just leave it blank to create a new based on date and market name.</small></label>
+ <input name="fdata[receipt]" id="receipt" type="text" list="receiptList" autocomplete="off"
value="<?php echo Summoner::ifset($TemplateData['editData'], 'receipt'); ?>" />
<datalist id="receiptList">
<option>Rewe</option>
</datalist>
<label for="weight">Weight(g)</label>
- <input name="fdata[weight]" id="weight" type="number" autocomplete="off"
+ <input name="fdata[weight]" id="weight" type="number" autocomplete="off" lang="<?php echo NUMBER_INPUT_LANG; ?>"
value="<?php echo Summoner::ifset($TemplateData['editData'], 'weight'); ?>"/>
<label for="itemcount">Itemcount</label>
- <input name="fdata[itemcount]" id="itemcount" type="number" autocomplete="off"
+ <input name="fdata[itemcount]" id="itemcount" type="number" autocomplete="off" lang="<?php echo NUMBER_INPUT_LANG; ?>"
value="<?php echo Summoner::ifset($TemplateData['editData'], 'itemcount'); ?>"/>
<label for="price">Price*</label>
<input name="fdata[price]" id="price" type="number" autocomplete="off" required
+ lang="<?php echo NUMBER_INPUT_LANG; ?>" step="0.01"
value="<?php echo Summoner::ifset($TemplateData['editData'], 'price'); ?>"/>
<label for="catalog">Catalog</label>
<?php
-require_once 'lib/item.class.php';
-$Item = new Item($DB);
+require_once 'lib/itemInput.class.php';
+$ItemInput = new ItemInput($DB);
$_id = false;
if(isset($_GET['id']) && !empty($_GET['id'])) {
var_dump($fdata);
+ $ItemInput->validateAndPrepare($fdata);
+
} else {
$TemplateData['message']['content'] = "Collection could not be loaded.";
$TemplateData['message']['status'] = "error";