products.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. $archived = $request->int("archived");
  29. if($archived === 0) {
  30. $nvp["archived"] = $archived;
  31. }
  32. $sort_options = [
  33. "id" => __("ID"),
  34. "name" => __("Name"),
  35. "sku" => __("SKU"),
  36. "price_asc" => __("Price (Ascending)"),
  37. "price_desc" => __("Price (Descending)"),
  38. ];
  39. $sort = $request->string("sort");
  40. $sort_column = "id";
  41. $sort_order = "asc";
  42. switch($sort) {
  43. case "name":
  44. $sort_column = "name";
  45. break;
  46. case "sku":
  47. $sort_column = "sku";
  48. break;
  49. case "price_asc":
  50. $sort_column = "price";
  51. break;
  52. case "price_desc":
  53. $sort_column = "price";
  54. $sort_order = "desc";
  55. break;
  56. }
  57. $archived_options = [
  58. "0" => __("Hide"),
  59. "1" => __("Show"),
  60. ];
  61. $count = Product::count($nvp);
  62. $pagination = new Pagination($count);
  63. $pagination->setTerms(__("%s product"), __("%s products"));
  64. $products = Product::query($nvp, [
  65. "sort" => $sort_column,
  66. "order" => $sort_order,
  67. "offset" => $pagination->getOffset(),
  68. "limit" => $pagination->getPageSize()
  69. ]);
  70. View::include("header");
  71. View::include("products", compact("products", "pagination", "archived_options", "sort_options", "product_id", "supplier_id", "name", "sku", "type", "archived", "sort"));
  72. View::include("footer");