| 18 | | class Service extends Base |
| 19 | | { |
| 20 | | |
| 21 | | |
| 22 | | |
| 23 | | #[\Override] |
| 24 | | protected function parseData(mixed $data) |
| 25 | | { |
| 26 | | $itemList = new Collection(); |
| 27 | | foreach ($data['data'] as $item) { |
| 28 | | $service = new Entity($item); |
| 29 | | if ($service->isLocale($this->locale)) { |
| 30 | | $itemList[$item['id']] = $service; |
| 31 | | } |
| 32 | | } |
| 33 | | return $itemList; |
| 34 | | } |
| 35 | | |
| 36 | | |
| 37 | | |
| 38 | | |
| 39 | | |
| 40 | | |
| 41 | | |
| 42 | | public function searchAll(mixed $querystring, bool|string $service_csv = false, bool|string $location_csv = false) |
| 43 | | { |
| 44 | | $serviceList = new Collection($this->fetchList($location_csv)->getArrayCopy()); |
| 45 | | if ($querystring) { |
| 46 | | $serviceList = new Collection(array_filter((array) $serviceList, function ($item) use ($querystring) { |
| 47 | | $length = (3 < strlen($querystring)) ? strlen($querystring) : 3; |
| 48 | | $nameMatch = preg_match('/[' . $querystring . ']{' . $length . ',}/i', $item['name']); |
| 49 | | $keywordMatch = preg_match('/[' . $querystring . ']{' . $length . ',}/i', $item['meta']['keywords']); |
| 50 | | return ($nameMatch || $keywordMatch); |
| 51 | | })); |
| 52 | | } |
| 53 | | |
| 54 | | $sortedItems = $serviceList->sortByName()->getArrayCopy(); |
| 55 | | $serviceList = new Collection($sortedItems); |
| 56 | | if (!is_string($location_csv)) { |
| 57 | | return $serviceList; |
| 58 | | } |
| 59 | | return $serviceList->containsLocation($location_csv); |
| 60 | | } |
| 61 | | |
| 62 | | |
| 63 | | |
| 64 | | |
| 65 | | |
| 66 | | |
| 67 | | public function fetchList(bool|string $location_csv = false) |
| 68 | | { |
| 69 | | |
| 70 | | $servicelist = $this->getItemList(); |
| 71 | | if (is_string($location_csv) && $location_csv !== '') { |
| 72 | | $servicelist = new Collection(array_filter((array) $servicelist, function ($item) use ($location_csv) { |
| 73 | | $service = new Entity($item); |
| 74 | | return $service->containsLocation($location_csv); |
| 75 | | })); |
| 76 | | } |
| 77 | | return $servicelist; |
| 78 | | } |
| 79 | | |
| 80 | | |
| 81 | | |
| 82 | | |
| 83 | | |
| 84 | | |
| 85 | | public function fetchListRelated(mixed $service_id) |
| 86 | | { |
| 87 | | $service = $this->fetchId($service_id); |
| 88 | | if ($service === false) { |
| 89 | | return new Collection(); |
| 90 | | } |
| 91 | | $serviceList = $this->getItemList(); |
| 92 | | |
| 93 | | $relatedList = new Collection( |
| 94 | | array_filter( |
| 95 | | (array) $serviceList, |
| 96 | | function ($item) use ($service) { |
| 97 | | $leikaIdentItem = substr(strval($item['leika']), 0, 11); |
| 98 | | $leikaIdentService = substr(strval($service['leika']), 0, 11); |
| 99 | | return ($leikaIdentItem == $leikaIdentService && $item['id'] != $service['id']); |
| 100 | | } |
| 101 | | ) |
| 102 | | ); |
| 103 | | return $relatedList; |
| 104 | | } |
| 105 | | |
| 106 | | |
| 107 | | |
| 108 | | |
| 109 | | |
| 110 | | public function fetchCombinations(mixed $service_csv) |
| 111 | | { |
| 112 | | return $this->fetchList($this->fetchLocationCsv($service_csv)); |
| 113 | | } |
| 114 | | |
| 115 | | |
| 116 | | |
| 117 | | |
| 118 | | |
| 119 | | protected function fetchLocationCsv(mixed $service_csv) |
| 120 | | { |
| 121 | | $locationlist = $this->access() |
| 122 | | ->fromLocation() |
| 123 | | ->fetchList($service_csv); |
| 124 | | $locationIdList = array(); |
| 125 | | foreach ($locationlist as $location) { |
| 126 | | $locationIdList[] = $location['id']; |
| 127 | | } |
| 128 | | return implode(',', $locationIdList); |
| 129 | | } |
| 130 | | |
| 131 | | |
| 132 | | |
| 133 | | |
| 134 | | |
| 135 | | public function fetchFromCsv(string $service_csv) |
| 136 | | { |
| 137 | | $servicelist = new Collection(); |
| 138 | | foreach (explode(',', $service_csv) as $service_id) { |
| 139 | | $service = $this->fetchId($service_id); |
| 140 | | if ($service && $service->isLocale($this->locale)) { |
| 141 | | $servicelist[$service_id] = $service; |
| 142 | | } |
| 143 | | } |
| 144 | | return $servicelist; |
| 145 | | } |
| 146 | | |
| 147 | | |
| 148 | | |
| 149 | | |
| 150 | | |
| 151 | | |
| 152 | | |
| 153 | | |
| 154 | | |
| 155 | | public function fetchListFromTopic(\BO\Zmsdldb\Entity\Topic $topic) |
| 156 | | { |
| 157 | | $itemlist = new Collection(); |
| 158 | | $serviceIds = $topic->getServiceIds(); |
| 159 | | if ($topic['relation']['navi'] && isset($topic['relation']['childs'])) { |
| 160 | | foreach ($topic['relation']['childs'] as $child) { |
| 161 | | $childtopic = $this->access() |
| 162 | | ->fromTopic() |
| 163 | | ->fetchPath($child['path']); |
| 164 | | if ($childtopic) { |
| 165 | | $serviceIds = array_merge($serviceIds, $childtopic->getServiceIds()); |
| 166 | | } |
| 167 | | } |
| 168 | | } |
| 169 | | if (count($serviceIds)) { |
| 170 | | $servicelistCSV = implode(',', $serviceIds); |
| 171 | | $servicelist = $this->fetchFromCsv($servicelistCSV); |
| 172 | | return $servicelist; |
| 173 | | } |
| 174 | | |
| 175 | | $sorted = $itemlist->sortByName(); |
| 176 | | return $sorted; |
| 177 | | } |
| 178 | | |
| 179 | | |
| 180 | | |
| 181 | | |
| 182 | | |
| 183 | | |
| 184 | | public function readSearchResultList(mixed $query, string $service_csv = '') |
| 185 | | { |
| 186 | | $servicelist = $this->fetchCombinations($service_csv); |
| 187 | | $servicelist = new Collection(array_filter((array) $servicelist, function ($item) use ($query) { |
| 188 | | return false !== strpos($item['name'], $query); |
| 189 | | })); |
| 190 | | return $servicelist; |
| 191 | | } |
| 192 | | } |