customer.php 1.7 KB

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