File manager - Edit - /var/www/html/transparencia/app/Http/Controllers/MapaDoSiteController.php
Back
<?php namespace App\Http\Controllers; use App\Services\AngraMapaDoSiteCollector; use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Route; use Illuminate\Support\Str; use Illuminate\View\View; class MapaDoSiteController extends Controller { public function index(AngraMapaDoSiteCollector $angraCollector): View { $sections = $this->resolveSections(config('mapa-do-site.sections', [])); $cacheKey = 'mapa-do-site.angra-dynamic.' . config('mapa-do-site.cache_version', 2); $angra = Cache::remember( $cacheKey, config('mapa-do-site.cache_ttl', 3600), fn () => $angraCollector->collect() ); $totalStatic = collect($sections) ->flatMap(fn (array $section) => collect($section['groups'] ?? []) ->flatMap(fn (array $group) => $group['links'] ?? [])) ->count(); $totalDynamic = collect($angra['blocks'] ?? []) ->sum(fn (array $block) => $block['total'] ?? count($block['items'] ?? [])); $mapaAccordionSections = $this->buildMapaAccordionSections($sections, $angra); return view('pages.mapa-do-site', [ 'mapaAccordionSections' => $mapaAccordionSections, 'angra' => $angra, 'totalLinks' => $totalStatic + $totalDynamic, ]); } /** * @param array<int, array<string, mixed>> $sections * @return array<int, array<string, mixed>> */ private function resolveSections(array $sections): array { return array_map(function (array $section): array { $section['groups'] = array_map(function (array $group): array { $group['links'] = array_map(function (array $link): array { if (isset($link['route']) && Route::has($link['route'])) { $link['href'] = route($link['route']); $link['external'] = $link['external'] ?? false; } elseif (isset($link['url'])) { $link['href'] = $link['url']; $link['external'] = $link['external'] ?? ! str_starts_with($link['url'], url('/')); } return $link; }, $group['links'] ?? []); return $group; }, $section['groups'] ?? []); return $section; }, $sections); } /** * @param array<int, array<string, mixed>> $sections * @param array<string, mixed> $angra * @return array<int, array<string, mixed>> */ private function buildMapaAccordionSections(array $sections, array $angra): array { $mapaSections = []; foreach ($sections as $section) { $items = $this->buildGroupsAccordion($section); if ($items !== []) { $mapaSections[] = [ 'id' => Str::slug($section['title'] ?? 'secao'), 'title' => $section['title'] ?? '', 'description' => $section['description'] ?? null, 'icon' => $this->sectionIcon($section['title'] ?? ''), 'items' => $items, 'linkCount' => collect($items)->sum(fn (array $item) => $item['totalCount'] ?? count($item['links'] ?? [])), ]; } if (($section['title'] ?? '') === 'Portal da Transparência') { $angraDynamic = $this->buildAngraDynamicSection($angra); if ($angraDynamic !== null) { $mapaSections[] = $angraDynamic; } } } return $mapaSections; } /** * @param array<string, mixed> $angra * @return array<string, mixed>|null */ private function buildAngraDynamicSection(array $angra): ?array { if (empty($angra['blocks'])) { return null; } $angraItems = collect($angra['blocks'])->map(fn (array $block) => $this->mapBlockToAccordionItem($block))->all(); return [ 'id' => 'angra-dinamico', 'title' => __('angra.rj.gov.br — páginas dinâmicas'), 'description' => __('Secretarias, notícias, legislação, licitações, contratos e demais conteúdos cadastrados no portal institucional.'), 'icon' => 'fa-database', 'items' => $angraItems, 'linkCount' => collect($angraItems)->sum(fn (array $item) => $item['totalCount'] ?? count($item['links'] ?? [])), ]; } private function sectionIcon(string $title): string { return match ($title) { 'Portal da Transparência' => 'fa-shield-halved', 'Prefeitura Municipal de Angra dos Reis' => 'fa-landmark', 'Portais e sistemas relacionados' => 'fa-globe', default => 'fa-folder-open', }; } /** * @return array<int, array{id: string, title: string, description?: string, links: array<int, array<string, mixed>>}> */ private function buildGroupsAccordion(?array $section): array { if ($section === null) { return []; } $items = []; foreach ($section['groups'] ?? [] as $group) { $links = array_values(array_filter( $group['links'] ?? [], fn (array $link) => ! empty($link['href']) )); if ($links === []) { continue; } $items[] = $this->limitAccordionItem([ 'id' => Str::slug($group['title'] ?? 'grupo'), 'title' => $group['title'] ?? '', 'links' => $links, ]); } return $items; } /** * @param array<string, mixed> $block * @return array<string, mixed> */ private function mapBlockToAccordionItem(array $block): array { $limit = max(1, (int) config('mapa-do-site.accordion_limit', 100)); $rawItems = $block['items'] ?? []; $total = (int) ($block['total'] ?? count($rawItems)); if ($total > $limit && count($rawItems) > $limit) { $rawItems = array_slice($rawItems, 0, $limit); } return $this->limitAccordionItem([ 'id' => $block['id'], 'title' => $block['title'], 'description' => $block['description'] ?? null, 'links' => collect($rawItems)->map(fn (array $item) => [ 'title' => $item['title'], 'href' => $item['href'], 'external' => $item['external'] ?? true, ])->all(), 'moreUrl' => $block['more_url'] ?? null, 'moreLabel' => $block['more_label'] ?? null, 'totalCount' => $block['total'] ?? count($block['items'] ?? []), 'truncated' => $block['truncated'] ?? false, ], preserveMoreFromBlock: true); } /** * @param array<string, mixed> $item * @return array<string, mixed> */ private function limitAccordionItem(array $item, bool $preserveMoreFromBlock = false): array { $limit = max(1, (int) config('mapa-do-site.accordion_limit', 100)); $links = $item['links'] ?? []; $total = $preserveMoreFromBlock && isset($item['totalCount']) ? (int) $item['totalCount'] : count($links); if ($total <= $limit && empty($item['truncated'])) { return array_merge($item, [ 'links' => array_slice($links, 0, $limit), 'totalCount' => $total, 'truncated' => false, ]); } if (! $preserveMoreFromBlock || empty($item['moreUrl'])) { $more = config('mapa-do-site.more_links.' . ($item['id'] ?? '')); $base = rtrim(config('mapa-do-site.angra_base', 'https://angra.rj.gov.br'), '/'); if (is_array($more) && ! empty($more['url'])) { $item['moreUrl'] = str_replace('{base}', $base, $more['url']); $item['moreLabel'] = $more['label'] ?? __('Ver mais'); } } return array_merge($item, [ 'links' => array_slice($links, 0, $limit), 'totalCount' => $total, 'truncated' => $total > $limit, 'moreLabel' => $item['moreLabel'] ?? __('Ver mais'), ]); } }
| ver. 1.4 |
Github
|
.
| PHP 8.3.28 | Generation time: 0.02 |
proxy
|
phpinfo
|
Settings