customer.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. class Customer extends Model {
  3. protected static $table = "customer";
  4. protected static $id_field = "id";
  5. protected static $fillable_fields = ["name"];
  6. protected static $field_map = [
  7. ];
  8. // Static methods
  9. public static function getForAdmin() {
  10. $customers = Core::$user->getCustomers();
  11. if(count($customers) == 0) throw new Exception("At least one customer must exist before this page can be accessed.");
  12. else if(count($customers) == 1) {
  13. $customer = array_shift($customers);
  14. }
  15. else {
  16. $customer_id = http::value("customer_id", "int");
  17. if(!$customer_id) {
  18. View::includeOnce("header", compact("page_title"));
  19. View::include("customer_select", compact("customers"));
  20. View::includeOnce("footer");
  21. exit;
  22. }
  23. $customer = Customer::get($customer_id);
  24. // Enforce restriction if CUSTOMER or lower
  25. if(Core::$user->level < User::LEVEL_ADMIN) {
  26. $link = UserCustomer::get(Core::$user->id, $customer_id);
  27. if(!$link) $customer = false;
  28. }
  29. }
  30. if(empty($customer)) throw new Exception("Customer not found.");
  31. return $customer;
  32. }
  33. // Object methods
  34. public function getLocations() {
  35. return Location::query(["customer_id" => $this->id], ["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. ?>