diff --git a/action.php b/action.php index 4a40dbf..9e14918 100644 --- a/action.php +++ b/action.php @@ -289,6 +289,36 @@ END; $cards = $database->select('certificates', ['certid (id)', 'certcode (code)', 'amount (balance)', 'start_amount (amount)'], ['certcode' => $code]); exit(json_encode(["status" => "OK", "cards" => $cards])); break; + case "editcustomer": + $insert = true; + if (is_empty($VARS['id'])) { + $insert = true; + } else { + if ($database->has('customers', ['customerid' => $VARS['id']])) { + $insert = false; + } else { + returnToSender("invalid_customerid"); + } + } + if (is_empty($VARS['name'])) { + returnToSender('invalid_parameters'); + } + + $data = [ + 'name' => $VARS['name'], + 'email' => $VARS['email'], + 'phone' => $VARS['phone'], + 'address' => $VARS['address'], + 'notes' => $VARS['notes'] + ]; + + if ($insert) { + $database->insert('customers', $data); + } else { + $database->update('customers', $data, ['customerid' => $VARS['id']]); + } + + returnToSender("customer_saved"); case "session_keepalive": header("Content-Type: application/json"); exit(json_encode(["status" => "OK"])); diff --git a/lang/en_us.php b/lang/en_us.php index 11bfa09..fa9d610 100644 --- a/lang/en_us.php +++ b/lang/en_us.php @@ -54,5 +54,12 @@ define("STRINGS", [ "phone" => "Phone", "email" => "Email", "address" => "Address", - "notes" => "Notes" + "notes" => "Notes", + "edit" => "Edit", + "new customer" => "New Customer", + "adding customer" => "Adding Customer", + "editing customer" => "Editing {name}", + "save" => "Save", + "customer saved" => "Customer saved.", + "invalid customer id" => "Invalid customer ID", ]); \ No newline at end of file diff --git a/lang/messages.php b/lang/messages.php index 0744629..7b55a46 100644 --- a/lang/messages.php +++ b/lang/messages.php @@ -16,5 +16,13 @@ define("MESSAGES", [ "404_error" => [ "string" => "page not found", "type" => "info" - ] + ], + "customer_saved" => [ + "string" => "customer saved", + "type" => "success" + ], + "invalid_customerid" => [ + "string" => "invalid customer id", + "type" => "danger" + ], ]); diff --git a/pages.php b/pages.php index b418007..e319538 100644 --- a/pages.php +++ b/pages.php @@ -40,6 +40,13 @@ define("PAGES", [ "static/js/customers.js" ], ], + "editcustomer" => [ + "title" => "edit customer", + "navbar" => false, + "scripts" => [ + "static/js/editcustomer.js" + ] + ], "404" => [ "title" => "404 error" ] diff --git a/pages/customers.php b/pages/customers.php index 8f16bf4..bf03672 100644 --- a/pages/customers.php +++ b/pages/customers.php @@ -12,6 +12,10 @@ redirectIfNotLoggedIn(); $customers = $database->select('customers', ['customerid (id)', 'name', 'email', 'phone', 'address', 'notes']); ?> +
+ +
+ @@ -30,7 +34,9 @@ $customers = $database->select('customers', ['customerid (id)', 'name', 'email', ?> - + diff --git a/pages/editcustomer.php b/pages/editcustomer.php new file mode 100644 index 0000000..ece9dfd --- /dev/null +++ b/pages/editcustomer.php @@ -0,0 +1,89 @@ + '', + 'name' => '', + 'email' => '', + 'phone' => '', + 'address' => '', + 'notes' => '' +]; + +$editing = false; + +if (!empty($VARS['id']) && !is_empty($VARS['id'])) { + if ($database->has('customers', ['customerid' => $VARS['id']])) { + $editing = true; + $custdata = $database->get( + 'customers', [ + 'customerid (id)', + 'name', + 'email', + 'phone', + 'address', + 'notes' + ], [ + 'customerid' => $VARS['id'] + ]); + } else { + // customer id is invalid, redirect to a version of the page that won't + // cause an error when pressing Save + header('Location: app.php?page=editcustomer'); + } +} +?> + + +
+

+ + "" . htmlspecialchars($custdata['name']) . ""]); ?> + + + +

+
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + + + + + +
+ \ No newline at end of file diff --git a/static/js/editcustomer.js b/static/js/editcustomer.js new file mode 100644 index 0000000..0b0deec --- /dev/null +++ b/static/js/editcustomer.js @@ -0,0 +1,10 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + + +$('#name').on('input propertychange paste', function () { + $('#name_title').text($('#name').val()); +}); \ No newline at end of file
+ +