<?php

use Source\Conn\Read;

/**
 * Função responsavel por relacionar todas as páginas para funcionamento do mesmo
 * @param type $File pagina que deseja recupera as <meta tags> delas , como title, description, image, link, etc.. para usa em suas <meta>
 * @return string retorna um array com os dados da página
 */ 
 
function _getConfigPagina($DataPage) {
    $WIDGET_FILES = [];

    $Read = new Read();
    $DEFAULT = Read(DB_CONFIG_PAGINA, 'pagina_type=:st', 'st=default')->getUnique();
    $result = Read(DB_CONFIG_PAGINA, 'pagina_id_page=:st', "st={$DataPage['page_id']}")->getUnique();
    $PAGINA_CONFIG = ($result ?? $DEFAULT);

    if (!$PAGINA_CONFIG):
        return array_merge($DataPage, ['pagina_full' => 2, 'pagina_modo' => 'cd', 'pagina_padding' => 1], ['files' => $WIDGET_FILES]);
    endif;

    if ($PAGINA_CONFIG['pagina_top']):
        $PAGE = explode("|", $PAGINA_CONFIG['pagina_top']);
        foreach ($PAGE as $widget):
            $Read->ExeRead(DB_WIDGET, "WHERE widget_id=:id", "id={$widget}");
            if ($Read->getResult()):
                $WIDGET_FILES['pagina_top'][] = $Read->getResult()[0];
            endif;
        endforeach;
    endif;
    if ($PAGINA_CONFIG['pagina_top_left']):
        $PAGE = explode("|", $PAGINA_CONFIG['pagina_top_left']);
        foreach ($PAGE as $widget):
            $Read->ExeRead(DB_WIDGET, "WHERE widget_id=:id", "id={$widget}");
            if ($Read->getResult()):
                $WIDGET_FILES['pagina_top_left'][] = $Read->getResult()[0];
            endif;
        endforeach;
    endif;
    if ($PAGINA_CONFIG['pagina_top_center']):
        $PAGE = explode("|", $PAGINA_CONFIG['pagina_top_center']);
        foreach ($PAGE as $widget):
            $Read->ExeRead(DB_WIDGET, "WHERE widget_id=:id", "id={$widget}");
            if ($Read->getResult()):
                $WIDGET_FILES['pagina_top_center'][] = $Read->getResult()[0];
            endif;
        endforeach;
    endif;
    if ($PAGINA_CONFIG['pagina_top_right']):
        $PAGE = explode("|", $PAGINA_CONFIG['pagina_top_right']);
        foreach ($PAGE as $widget):
            $Read->ExeRead(DB_WIDGET, "WHERE widget_id=:id", "id={$widget}");
            if ($Read->getResult()):
                $WIDGET_FILES['pagina_top_right'][] = $Read->getResult()[0];
            endif;
        endforeach;
    endif;
    if ($PAGINA_CONFIG['pagina_two_right']):
        $PAGE = explode("|", $PAGINA_CONFIG['pagina_two_right']);
        foreach ($PAGE as $widget):
            $Read->ExeRead(DB_WIDGET, "WHERE widget_id=:id", "id={$widget}");
            if ($Read->getResult()):
                $WIDGET_FILES['pagina_two_right'][] = $Read->getResult()[0];
            endif;
        endforeach;
    endif;
    if ($PAGINA_CONFIG['pagina_two_left']):
        $PAGE = explode("|", $PAGINA_CONFIG['pagina_two_left']);
        foreach ($PAGE as $widget):
            $Read->ExeRead(DB_WIDGET, "WHERE widget_id=:id", "id={$widget}");
            if ($Read->getResult()):
                $WIDGET_FILES['pagina_two_left'][] = $Read->getResult()[0];
            endif;
        endforeach;
    endif;

    if ($PAGINA_CONFIG['pagina_left']):
        $PAGE = explode("|", $PAGINA_CONFIG['pagina_left']);
        foreach ($PAGE as $widget):
            $Read->ExeRead(DB_WIDGET, "WHERE widget_id=:id", "id={$widget}");
            if ($Read->getResult()):
                $WIDGET_FILES['pagina_left'][] = $Read->getResult()[0];
            endif;
        endforeach;
    endif;
    if ($PAGINA_CONFIG['pagina_center']):
        $PAGE = explode("|", $PAGINA_CONFIG['pagina_center']);
        foreach ($PAGE as $widget):
            if ($widget == 'conteudo'):
                $WIDGET_FILES['pagina_center'][] = ['widget_path' => null, 'widget_title' => 'Conteúdo da página', 'widget_name' => null, 'widget_limit' => null,'widget_content'=>null];
            else:
                $Read->ExeRead(DB_WIDGET, "WHERE widget_id=:id", "id={$widget}");
                if ($Read->getResult()):
                    $WIDGET_FILES['pagina_center'][] = $Read->getResult()[0];
                endif;
            endif;
        endforeach;
    endif;
    if ($PAGINA_CONFIG['pagina_right']):
        $PAGE = explode("|", $PAGINA_CONFIG['pagina_right']);
        foreach ($PAGE as $widget):
            $Read->ExeRead(DB_WIDGET, "WHERE widget_id=:id", "id={$widget}");
            if ($Read->getResult()):
                $WIDGET_FILES['pagina_right'][] = $Read->getResult()[0];
            endif;
        endforeach;
    endif;
    if ($PAGINA_CONFIG['pagina_botton']):
        $PAGE = explode("|", $PAGINA_CONFIG['pagina_botton']);
        foreach ($PAGE as $widget):
            $Read->ExeRead(DB_WIDGET, "WHERE widget_id=:id", "id={$widget}");
            if ($Read->getResult()):
                $WIDGET_FILES['pagina_botton'][] = $Read->getResult()[0];
            endif;
        endforeach;
    endif;
    if ($PAGINA_CONFIG['pagina_bottom_col_left']):
        $PAGE = explode("|", $PAGINA_CONFIG['pagina_bottom_col_left']);
        foreach ($PAGE as $widget):
            $Read->ExeRead(DB_WIDGET, "WHERE widget_id=:id", "id={$widget}");
            if ($Read->getResult()):
                $WIDGET_FILES['pagina_bottom_col_left'][] = $Read->getResult()[0];
            endif;
        endforeach;
    endif;
    if ($PAGINA_CONFIG['pagina_bottom_col_center']):
        $PAGE = explode("|", $PAGINA_CONFIG['pagina_bottom_col_center']);
        foreach ($PAGE as $widget):
            $Read->ExeRead(DB_WIDGET, "WHERE widget_id=:id", "id={$widget}");
            if ($Read->getResult()):
                $WIDGET_FILES['pagina_bottom_col_center'][] = $Read->getResult()[0];
            endif;
        endforeach;
    endif;
    if ($PAGINA_CONFIG['pagina_bottom_col_right']):
        $PAGE = explode("|", $PAGINA_CONFIG['pagina_bottom_col_right']);
        foreach ($PAGE as $widget):
            $Read->ExeRead(DB_WIDGET, "WHERE widget_id=:id", "id={$widget}");
            if ($Read->getResult()):
                $WIDGET_FILES['pagina_bottom_col_right'][] = $Read->getResult()[0];
            endif;
        endforeach;
    endif;
    if ($PAGINA_CONFIG['pagina_footer_left']):
        $PAGE = explode("|", $PAGINA_CONFIG['pagina_footer_left']);
        foreach ($PAGE as $widget):
            $Read->ExeRead(DB_WIDGET, "WHERE widget_id=:id", "id={$widget}");
            if ($Read->getResult()):
                $WIDGET_FILES['pagina_footer_left'][] = $Read->getResult()[0];
            endif;
        endforeach;
    endif;
    if ($PAGINA_CONFIG['pagina_footer_center']):
        $PAGE = explode("|", $PAGINA_CONFIG['pagina_footer_center']);
        foreach ($PAGE as $widget):
            $Read->ExeRead(DB_WIDGET, "WHERE widget_id=:id", "id={$widget}");
            if ($Read->getResult()):
                $WIDGET_FILES['pagina_footer_center'][] = $Read->getResult()[0];
            endif;
        endforeach;
    endif;
    if ($PAGINA_CONFIG['pagina_footer_right']):
        $PAGE = explode("|", $PAGINA_CONFIG['pagina_footer_right']);
        foreach ($PAGE as $widget):
            $Read->ExeRead(DB_WIDGET, "WHERE widget_id=:id", "id={$widget}");
            if ($Read->getResult()):
                $l = $Read->getResult()[0];
                $WIDGET_FILES['pagina_footer_right'][] = $Read->getResult()[0];
            endif;
        endforeach;
    endif;

    return array_merge($DataPage, $PAGINA_CONFIG, ['files' => $WIDGET_FILES]);
}
 