1
0

customer.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. $page_title = "Select Customer";
  19. View::includeOnce("header", compact("page_title"));
  20. View::include("customer_select", compact("customers"));
  21. View::includeOnce("footer");
  22. exit;
  23. }
  24. $customer = Customer::get($customer_id);
  25. // Enforce restriction if CUSTOMER or lower
  26. if(Core::$user->level < User::LEVEL_ADMIN) {
  27. $link = UserCustomer::get(Core::$user->id, $customer_id);
  28. if(!$link) $customer = false;
  29. }
  30. }
  31. if(empty($customer)) throw new Exception("Customer not found.");
  32. return $customer;
  33. }
  34. // Object methods
  35. public function getLocations() {
  36. return Location::query(["customer_id" => $this->id], ["sort" => "name"]);
  37. }
  38. public function getProjects() {
  39. return Project::query(["customer_id" => $this->id], ["sort" => "dts_deadline", "order" => "desc"]);
  40. }
  41. public function getTemplates() {
  42. return Template::query(["customer_id" => $this->id], ["sort" => "name"]);
  43. }
  44. }
  45. ?>