| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- <?php
- namespace App;
- use RuntimeException;
- /**
- * @var int $id
- */
- $core = Core::getInstance();
- $core->addBreadcrumb("/customers", __("Customers"));
- $user = $core->getUser();
- if(isset($id)) {
- $customer = Customer::get($id);
- if(!$customer) {
- throw new RuntimeException(__("Customer not found."));
- }
- $customer->dts_updated = Database::dts();
- $core->addBreadcrumb($customer->getUrl(), $customer->name);
- $title = __("Update Customer");
- }
- else {
- $customer = new Customer();
- $customer->dts_created = Database::dts();
- $title = __("Add Customer");
- }
- $core->addBreadcrumb($_SERVER["REQUEST_URI"], $title);
- $address = $customer->getAddress();
- $request = Request::fromPost();
- $submit = $request->int("submit");
- if($submit) {
- try {
- $user->requireValidCsrfToken();
- $customer->name = $request->string("name");
- $customer->phone = $request->string("phone");
- $customer->fax = $request->string("fax");
- $customer->email = $request->email("email");
- $address->address_1 = $request->string("address_1");
- $address->address_2 = $request->string("address_2");
- $address->city = $request->string("city");
- $address->county = $request->string("county");
- $address->postal_code = $request->string("postal_code");
- $address->country = $request->string("country");
- $address->latitude = $request->float("latitude");
- $address->longitude = $request->float("longitude");
- if(!$customer->name) {
- throw new RuntimeException(__("Name is required."));
- }
- $address->commit();
- $customer->address_id = $address->id;
- $customer->commit();
- Session::addAlert("success", __("Customer saved successfully."));
- Http::redirect($customer->getUrl());
- }
- catch(RuntimeException $e) {
- Session::addAlert("danger", $e->getMessage());
- }
- }
- View::include("header");
- View::include("entity_form", [
- "title" => $title,
- "model" => $customer,
- "address" => $address
- ]);
- View::include("footer");
|