/** * Return the single instance of this class. * * @param array|null $db * @return InvoicerDB */ public static function instance($db = null) { if (self::$instance === null) { self::$instance = new self($db); } return self::$instance; }
/** * @param string $email * @param string $password * @return string|true|null */ public static function log_in($email, $password) { if (empty($email)) { return 'Please enter your email'; } if (empty($password)) { return 'Please enter your password'; } $database = InvoicerDB::instance(); $user_id = $database->check_user($email, $password); // Database error. if ($user_id === null) { return null; } // Valid user id. if (is_int($user_id)) { $_SESSION[InvoicerDB::USER_ID] = $user_id; $_SESSION[self::USER_LOGGED_IN] = true; return true; } // Invalid email or password. return $user_id; }
" value="<?php echo isset($_POST[InvoicerDB::USER_PHONE]) ? $_POST[InvoicerDB::USER_PHONE] : $this->current_user[InvoicerDB::USER_PHONE]; ?> "> </div> <button type="submit" name="<?php echo Page::SAVE_ACCOUNT_BUTTON; ?> ">Save</button> </form> <h2 class="form-header">Companies</h2> <?php $database = InvoicerDB::instance(); $companies = $database->get_companies($this->current_user[InvoicerDB::USER_ID]); if (empty($companies)) { ?> <p class="none">No companies yet. Use the form below to add one.</p> <?php } else { ?> <form id="update-companies-form" action="<?php echo $this->full_url; ?> " method="post"> <?php foreach ($companies as $company) { $id = $company[InvoicerDB::COMPANY_ID];
/** * Update or create an invoice. * * @param bool $creating * @param array $new_data Data to update or create the invoice with. */ private function save_invoice($creating, $new_data) { // Validate the invoice, find what needs to be updated. // $this->invoice will also be updated so if we display it // it will have the latest user-submitted data. $result = Invoice::validate($creating, $new_data, $this->invoice, $this->current_user[InvoicerDB::USER_ID]); // Database error. if ($result === null) { $this->set_page_to_error(); return; } // Validation error. if (!empty($result['errors'])) { $this->field_errors = $result['errors']; return; } // Nothing changed. if (empty($result['update'])) { $this->success_message = 'Nothing changed'; return; } // Try updating/inserting. $database = InvoicerDB::instance(); $update_result = $database->update_invoice($creating ? null : $this->invoice[InvoicerDB::INVOICE_ID], $result['update'], $result['insert_rows'], $result['update_rows']); // Database error. if ($update_result === null || $update_result === false) { $this->set_page_to_error(); return; } // Saved successfully - redirect if any slugs or the title changed (always true when creating). if (isset($result['update'][InvoicerDB::INVOICE_SLUG]) || isset($result['update'][InvoicerDB::COMPANY_ID]) || isset($result['update'][InvoicerDB::INVOICE_TITLE])) { $reason = $creating ? 'created' : 'updated'; $company = $database->get_company_by(InvoicerDB::COMPANY_ID, $new_data[InvoicerDB::COMPANY_ID]); if (empty($company)) { $this->set_page_to_error(); return; } header('Location: ' . $this->url . '/' . $company[InvoicerDB::COMPANY_SLUG] . '/' . $this->invoice[InvoicerDB::INVOICE_SLUG] . '/?reason=' . $reason); exit; } else { $this->success_message = 'Invoice updated'; } }
/** * @param string $table * @param string $slug * @return string|true|null */ public static function check_slug($table, $slug) { if (!preg_match('/^[-a-z0-9]+$/', $slug)) { return 'Slug "' . $slug . '" can only contain lowercase letters, numbers, and dashes'; } $database = InvoicerDB::instance(); $unique = $database->unique_slug($table, $slug); // Database error. if ($unique === null) { return null; } // Not unique. if (!$unique) { return 'The slug "' . $slug . '" is already in use'; } // Companies cannot overlap page slugs. if ($table === InvoicerDB::COMPANIES_TABLE && in_array($slug, self::$restricted_slugs)) { return 'Slug "' . $slug . '" is not allowed'; } return true; }