| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- <?php
- class Customer extends Model {
- protected static $table = "customer";
- protected static $id_field = "id";
- protected static $fillable_fields = ["name", "address_1", "address_2", "city", "county", "postal_code", "email", "phone", "email_after_test", "email_locations_after_test"];
- // Static methods
- public static function getForAdmin() {
- $customers = Core::$user->getCustomers();
- if(count($customers) == 0) throw new Exception("At least one customer must exist before this page can be accessed.");
- else if(count($customers) == 1) {
- $customer = array_shift($customers);
- }
- else {
- $customer_id = http::value("customer_id", "int");
- if(!$customer_id) {
- $page_title = "Select Customer";
- View::includeOnce("header", compact("page_title"));
- View::include("customer_select", compact("customers"));
- View::includeOnce("footer");
- exit;
- }
- $customer = Customer::get($customer_id);
- // Enforce restriction if CUSTOMER or lower
- if(Core::$user->level < User::LEVEL_ADMIN) {
- $link = UserCustomer::get(Core::$user->id, $customer_id);
- if(!$link) $customer = false;
- }
- }
- if(empty($customer)) throw new Exception("Customer not found.");
- return $customer;
- }
- // Object methods
- public function getLocations() {
- return Location::query(["customer_id" => $this->id], ["sort" => "name"]);
- }
- public function getProjects() {
- return Project::query(["customer_id" => $this->id], ["sort" => "dts_deadline", "order" => "desc"]);
- }
- public function getTemplates() {
- return Template::query(["customer_id" => $this->id], ["sort" => "name"]);
- }
- }
- ?>
|