location_search.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. namespace Searches;
  3. use Constraint;
  4. use Customer;
  5. use DB;
  6. use Location;
  7. use Test;
  8. use User;
  9. class LocationSearch {
  10. private Customer $customer;
  11. private ?User $user = null;
  12. private ?int $id = null;
  13. private ?array $ids = null;
  14. private ?string $internalId = null;
  15. private ?string $name = null;
  16. private ?string $legalEntityName = null;
  17. private bool $includeArchived = false;
  18. private bool $includeClosed = false;
  19. private ?string $minTestDate = null;
  20. private ?string $maxTestDate = null;
  21. private ?string $filter = null;
  22. /** @var Test[] */
  23. private array $liveTests = [];
  24. /** @var Test[] */
  25. private array $allTests = [];
  26. public function __construct(Customer $customer, ?User $user) {
  27. $this->customer = $customer;
  28. $this->user = $user;
  29. }
  30. public function getId(): ?int {
  31. return $this->id;
  32. }
  33. public function setId(?int $id): void {
  34. $this->id = $id;
  35. }
  36. public function getIds(): ?array {
  37. return $this->ids;
  38. }
  39. public function setIds(?array $ids): void {
  40. $this->ids = $ids;
  41. }
  42. public function getInternalId(): ?string {
  43. return $this->internalId;
  44. }
  45. public function setInternalId(?string $internalId): void {
  46. $this->internalId = $internalId;
  47. }
  48. public function getName(): ?string {
  49. return $this->name;
  50. }
  51. public function setName(?string $name): void {
  52. $this->name = $name;
  53. }
  54. public function getLegalEntityName(): ?string {
  55. return $this->legalEntityName;
  56. }
  57. public function setLegalEntityName(?string $legalEntityName): void {
  58. $this->legalEntityName = $legalEntityName;
  59. }
  60. public function isIncludeArchived(): bool {
  61. return $this->includeArchived;
  62. }
  63. public function setIncludeArchived(bool $includeArchived): void {
  64. $this->includeArchived = $includeArchived;
  65. }
  66. public function isIncludeClosed(): bool {
  67. return $this->includeClosed;
  68. }
  69. public function setIncludeClosed(bool $includeClosed): void {
  70. $this->includeClosed = $includeClosed;
  71. }
  72. public function getMinTestDate(): ?string {
  73. return $this->minTestDate;
  74. }
  75. public function setMinTestDate(?string $minTestDate): void {
  76. $this->minTestDate = $minTestDate;
  77. }
  78. public function getMaxTestDate(): ?string {
  79. return $this->maxTestDate;
  80. }
  81. public function setMaxTestDate(?string $maxTestDate): void {
  82. $this->maxTestDate = $maxTestDate;
  83. }
  84. public function getFilter(): ?string {
  85. return $this->filter;
  86. }
  87. public function setFilter(?string $filter): void {
  88. $this->filter = $filter;
  89. }
  90. /**
  91. * @return Location[]
  92. * @throws \Exception
  93. */
  94. public function run(): array {
  95. $nvp = [
  96. "customer_id" => $this->customer->id,
  97. "archived" => 0,
  98. "closed" => 0
  99. ];
  100. if($this->id) $nvp["id"] = $this->id;
  101. if($this->ids) $nvp["id"] = $this->ids;
  102. if($this->internalId) $nvp["internal_id"] = Db::escape($this->internalId);
  103. if($this->name) $nvp["name"] = Constraint::contains($this->name);
  104. if($this->legalEntityName) $nvp["legal_entity_name"] = Constraint::contains($this->legalEntityName);
  105. if($this->includeArchived) unset($nvp["archived"]);
  106. if($this->includeClosed) unset($nvp["closed"]);
  107. $locations = Location::query($nvp, ["sort" => "name", "order" => "asc"]);
  108. $base_test_nvp = [
  109. "status" => Constraint::notEquals(Test::STATUS_CANCELLED),
  110. "location_id" => array_column($locations, "id")
  111. ];
  112. if($this->user && $this->user->level <= User::LEVEL_CUSTOMER) {
  113. $base_test_nvp["status"] = Test::STATUS_COMPLETED;
  114. }
  115. $this->populateAllTests($base_test_nvp);
  116. $this->populateLiveTests($base_test_nvp);
  117. if($this->filter) {
  118. $exclude_location_flags = [];
  119. $tests = $this->allTests;
  120. if(preg_match("/current$/", $this->filter)) {
  121. $tests = $this->liveTests;
  122. }
  123. switch($this->filter) {
  124. case "untested-current":
  125. case "untested-all":
  126. foreach($tests as $test) {
  127. $exclude_location_flags[$test->location_id] = true;
  128. }
  129. break;
  130. case "failed-current":
  131. case "failed-all":
  132. foreach($tests as $test) {
  133. if(!$test->isFail() || $test->isVoid()) {
  134. $exclude_location_flags[$test->location_id] = true;
  135. }
  136. }
  137. foreach($locations as $location) {
  138. if(!isset($tests[$location->id])) {
  139. $exclude_location_flags[$location->id] = true;
  140. }
  141. }
  142. break;
  143. }
  144. $locations = array_filter($locations, static function(Location $location) use ($exclude_location_flags) {
  145. return empty($exclude_location_flags[$location->id]);
  146. });
  147. }
  148. return $locations;
  149. }
  150. /**
  151. * @param array $nvp
  152. * @return Test[]
  153. * @throws \Exception
  154. */
  155. public function populateAllTests(array $nvp): array {
  156. $all_tests = Test::query($nvp, ["sort" => "dts_start", "order" => "asc"]);
  157. foreach($all_tests as $test) {
  158. $this->allTests[$test->location_id] = $test;
  159. }
  160. return $this->allTests;
  161. }
  162. /**
  163. * @param array $nvp
  164. * @return Test[]
  165. * @throws \Exception
  166. */
  167. public function populateLiveTests(array $nvp): array {
  168. $nvp["dts_start"] = $this->getDateTimeConstraint();
  169. $this->liveTests = Test::query($nvp);
  170. return $this->liveTests;
  171. }
  172. public function getAllTests(): array {
  173. return $this->allTests;
  174. }
  175. private function getDateTimeConstraint(): Constraint {
  176. if($this->minTestDate && $this->maxTestDate) {
  177. $constraint = Constraint::between($this->minTestDate, $this->maxTestDate);
  178. }
  179. else {
  180. $constraint = $this->customer->getYearCycleConstraint(TIMEZONE);
  181. }
  182. return $constraint;
  183. }
  184. }