select( 'pp_contacts_maps_provinces', [ 'id', 'code', 'name' ], [ 'ORDER' => [ 'id' => 'ASC' ] ] ); $provinces_by_id = []; foreach ($provinces as $p) { $provinces_by_id[(int)$p['id']] = $p; } $products = $mdb->select( 'pp_contacts_maps_products', [ 'id', 'code', 'name', 'icon' ], [ 'is_active' => 1, 'ORDER' => [ 'sort' => 'ASC' ] ] ); $products_by_id = []; foreach ($products as $pr) { $products_by_id[(int)$pr['id']] = $pr; } // 2) забираємо салони $where = [ 'ORDER' => [ 'sort' => 'ASC' ] ]; if ($only_active) $where['is_active'] = 1; $rows = $mdb->select('pp_contacts_maps', '*', $where); // 3) збираємо API формат $out = []; foreach ($rows as $r) { $phones = json_decode($r['phones_json'] ?? '[]', true); if (!is_array($phones)) $phones = []; $emails = json_decode($r['emails_json'] ?? '[]', true); if (!is_array($emails)) $emails = []; $prod_ids = json_decode($r['products_json'] ?? '[]', true); if (!is_array($prod_ids)) $prod_ids = []; $prod_out = []; foreach ($prod_ids as $pid) { $pid = (int)$pid; if (isset($products_by_id[$pid])) { $prod_out[] = [ 'id' => (int)$products_by_id[$pid]['id'], 'code' => $products_by_id[$pid]['code'], 'name' => $products_by_id[$pid]['name'], 'icon' => $products_by_id[$pid]['icon'], ]; } } $prov = $provinces_by_id[(int)$r['province_id']] ?? null; $out[] = [ 'id' => (int)$r['id'], 'province' => $prov ? [ 'id' => (int)$prov['id'], 'code' => $prov['code'], 'name' => $prov['name'], ] : null, 'city' => $r['city'], 'salon_type' => $r['salon_type'], 'salon_name' => $r['salon_name'], 'address' => $r['address'], 'position' => [ 'lat' => (float)$r['lat'], 'lng' => (float)$r['lng'], ], 'opening_hours' => $r['opening_hours'] ?? '', 'contact' => [ 'phones' => array_values($phones), 'emails' => array_values($emails), ], 'products' => $prod_out, 'button' => [ 'label' => $r['button_label'] ?? '', 'url' => $r['button_url'] ?? '', ], 'banner_image' => $r['banner_image'] ?? '', 'is_active' => (int)$r['is_active'], 'sort' => (int)$r['sort'], 'updated_at' => $r['updated_at'] ?? null, ]; } return $out; } }