products.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php /** @noinspection PhpUnhandledExceptionInspection */
  2. namespace App;
  3. use App\Constraint\HasTextConstraint;
  4. $core = Core::getInstance();
  5. $core->addBreadcrumb("/products", __("Products"));
  6. $nvp = [];
  7. $request = Request::fromRequest();
  8. $product_id = $request->int("product_id");
  9. if($product_id) {
  10. $nvp["id"] = $product_id;
  11. }
  12. $supplier_id = $request->int("supplier_id");
  13. if($supplier_id) {
  14. $nvp["supplier_id"] = $supplier_id;
  15. }
  16. $name = $request->string("name");
  17. if($name) {
  18. $nvp["name"] = new HasTextConstraint($name);
  19. }
  20. $sku = $request->string("sku");
  21. if($sku) {
  22. $nvp["sku"] = new HasTextConstraint($sku);
  23. }
  24. $type = $request->int("type");
  25. if($type) {
  26. $nvp["type"] = $type;
  27. }
  28. $sort_options = [
  29. "id" => __("ID"),
  30. "name" => __("Name"),
  31. "sku" => __("SKU"),
  32. "price_asc" => __("Price (Ascending)"),
  33. "price_desc" => __("Price (Descending)"),
  34. ];
  35. $sort = $request->string("sort");
  36. $sort_column = "id";
  37. $sort_order = "asc";
  38. switch($sort) {
  39. case "name":
  40. $sort_column = "name";
  41. break;
  42. case "sku":
  43. $sort_column = "sku";
  44. break;
  45. case "price_asc":
  46. $sort_column = "price";
  47. break;
  48. case "price_desc":
  49. $sort_column = "price";
  50. $sort_order = "desc";
  51. break;
  52. }
  53. $count = Product::count($nvp);
  54. $pagination = new Pagination($count);
  55. $pagination->setTerms(__("%s product"), __("%s products"));
  56. $products = Product::query($nvp, [
  57. "sort" => $sort_column,
  58. "order" => $sort_order,
  59. "offset" => $pagination->getOffset(),
  60. "limit" => $pagination->getPageSize()
  61. ]);
  62. View::include("header");
  63. View::include("products", compact("products", "pagination", "sort_options", "product_id", "supplier_id", "name", "sku", "type", "sort"));
  64. View::include("footer");