customer.php 1.4 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"];
  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. }
  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() {
  34. return Location::query(["customer_id" => $this->id], ["sort" => "name"]);
  35. }
  36. public function getProjects() {
  37. return Project::query(["customer_id" => $this->id], ["sort" => "dts_deadline", "order" => "desc"]);
  38. }
  39. public function getTemplates() {
  40. return Template::query(["customer_id" => $this->id], ["sort" => "name"]);
  41. }
  42. }
  43. ?>