customer.php 1.7 KB

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