customer.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. class Customer extends Model {
  3. protected static $table = "customer";
  4. protected static $id_field = "id";
  5. protected static $fillable_fields = ["name", "address_1", "address_2", "city", "county", "postal_code", "country", "email", "phone", "email_after_test", "email_locations_after_test"];
  6. // Static methods
  7. public static function getForAdmin() {
  8. $customers = Core::$user->getCustomers();
  9. if(count($customers) == 0) throw new Exception("At least one customer must exist before this page can be accessed.");
  10. else if(count($customers) == 1) {
  11. $customer = array_shift($customers);
  12. }
  13. else {
  14. $customer_id = http::value("customer_id", "int");
  15. if(!$customer_id) {
  16. $page_title = "Select Customer";
  17. View::includeOnce("header", compact("page_title"));
  18. View::include("customer_select", compact("customers"));
  19. View::includeOnce("footer");
  20. exit;
  21. }
  22. $customer = Customer::get($customer_id);
  23. // Enforce restriction if CUSTOMER or lower
  24. if(Core::$user->level < User::LEVEL_ADMIN) {
  25. $link = UserCustomer::get(Core::$user->id, $customer_id);
  26. if(!$link) $customer = false;
  27. }
  28. }
  29. if(empty($customer)) throw new Exception("Customer not found.");
  30. return $customer;
  31. }
  32. // Object methods
  33. public function getLocations($query = []) {
  34. $nvp = array_merge(["customer_id" => $this->id], $query);
  35. return Location::query($nvp, ["sort" => "name"]);
  36. }
  37. public function getProjects() {
  38. return Project::query(["customer_id" => $this->id], ["sort" => "dts_deadline", "order" => "desc"]);
  39. }
  40. public function getTemplates() {
  41. return Template::query(["customer_id" => $this->id], ["sort" => "name"]);
  42. }
  43. }
  44. ?>