search.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php /** @noinspection PhpUnhandledExceptionInspection */
  2. namespace App;
  3. $core = Core::getInstance();
  4. $user = $core->getUser();
  5. $user->requireAdmin();
  6. $core->addBreadcrumb("/search", __("Search"));
  7. $request = Request::fromRequest();
  8. $term = $request->string("term");
  9. $results = [];
  10. if($term) {
  11. if(mb_strlen($term) < 3) {
  12. Session::addAlert("danger", __("Search term must be 3 or more characters long."));
  13. }
  14. else {
  15. // Search sale and document numbers
  16. if(is_numeric($term)) {
  17. $documents = Document::query([
  18. "number" => $term,
  19. "dts_deleted" => null
  20. ]);
  21. foreach($documents as $document) {
  22. $results[] = [
  23. "type" => $document->getDisplayType(),
  24. "match_reason" => __("%s number match.", $document->getDisplayType()),
  25. "name" => $document->getTitle(),
  26. "url" => $document->getUrl()
  27. ];
  28. }
  29. $sale = Sale::getByNumber($term);
  30. if($sale) {
  31. $results[] = [
  32. "type" => __("Sale"),
  33. "match_reason" => __("Sale number match."),
  34. "name" => $sale->getTitle(),
  35. "url" => $sale->getUrl()
  36. ];
  37. }
  38. }
  39. // Document items - match serial number
  40. $items = DocumentItem::queryFuzzy([
  41. "serial_number" => $term,
  42. "dts_deleted" => null
  43. ]);
  44. foreach($items as $item) {
  45. $document = $item->getDocument();
  46. $results[] = [
  47. "type" => __("%s Item", $document->getDisplayType()),
  48. "match_reason" => __("%s item serial number match.", $document->getDisplayType()),
  49. "name" => $document->getTitle(),
  50. "url" => $document->getUrl()
  51. ];
  52. }
  53. // Document items - match description
  54. $items = DocumentItem::queryFuzzy([
  55. "description" => $term,
  56. "dts_deleted" => null
  57. ]);
  58. foreach($items as $item) {
  59. $document = $item->getDocument();
  60. $results[] = [
  61. "type" => __("%s Item", $document->getDisplayType()),
  62. "match_reason" => __("%s item description match.", $document->getDisplayType()),
  63. "name" => $document->getTitle(),
  64. "url" => $document->getUrl()
  65. ];
  66. }
  67. // Sale notes
  68. $notes = SaleNote::queryFuzzy([
  69. "content" => $term,
  70. "dts_deleted" => null
  71. ]);
  72. foreach($notes as $note) {
  73. $results[] = [
  74. "type" => __("Sale Item"),
  75. "match_reason" => __("Sale note match."),
  76. "name" => $note->getSale()->getTitle(),
  77. "url" => $note->getSale()->getUrl()
  78. ];
  79. }
  80. // Ticket comments
  81. $comments = TicketComment::queryFuzzy([
  82. "content" => $term,
  83. "dts_deleted" => null
  84. ]);
  85. foreach($comments as $comment) {
  86. $results[] = [
  87. "type" => __("Ticket / Comment"),
  88. "match_reason" => __("Ticket or ticket comment match."),
  89. "name" => $comment->getTicket()->getTitle(),
  90. "url" => $comment->getTicket()->getUrl() . "#ticket-comment-" . $comment->id
  91. ];
  92. }
  93. // Customers
  94. $customers = Customer::queryFuzzy([
  95. "name" => $term,
  96. "dts_deleted" => null
  97. ]);
  98. foreach($customers as $customer) {
  99. $results[] = [
  100. "type" => __("Customer"),
  101. "match_reason" => __("Customer name match."),
  102. "name" => $customer->name,
  103. "url" => $customer->getUrl()
  104. ];
  105. }
  106. // Locations
  107. $locations = Location::queryFuzzy([
  108. "name" => $term,
  109. "dts_deleted" => null
  110. ]);
  111. foreach($locations as $location) {
  112. $results[] = [
  113. "type" => __("Location"),
  114. "match_reason" => __("Location name match."),
  115. "name" => $location->getDisplayName(),
  116. "url" => $location->getUrl()
  117. ];
  118. }
  119. // Products
  120. $products = Product::queryFuzzy([
  121. "name" => $term,
  122. "dts_deleted" => null
  123. ]);
  124. foreach($products as $product) {
  125. $results[] = [
  126. "type" => __("Product"),
  127. "match_reason" => __("Product name match."),
  128. "name" => $product->name,
  129. "url" => $product->getUrl()
  130. ];
  131. }
  132. // Suppliers
  133. $suppliers = Supplier::queryFuzzy([
  134. "name" => $term,
  135. "dts_deleted" => null
  136. ]);
  137. foreach($suppliers as $supplier) {
  138. $results[] = [
  139. "type" => __("Supplier"),
  140. "match_reason" => __("Supplier name match."),
  141. "name" => $supplier->name,
  142. "url" => $supplier->getUrl()
  143. ];
  144. }
  145. }
  146. }
  147. View::include("header");
  148. View::include("search", compact("term", "results"));
  149. View::include("footer");