<?php

namespace Apps\Membre\Widget\Menu;

use Core\Control\Form\Form;
use Core\View\View;
use Core\View\ElementView;

use Apps\EeApp\Entity\EeAppApp;
use Apps\EeApp\Entity\EeMenuUser;

class Menu extends Form {

    /**
     * Constructeur
     */
    function __construct($core = "", $data = "") {
        $this->Core = $core;
        $this->Data = $data;
    }

    function Render(){

        $view = new View(__DIR__ . "/View/index.tpl", $this->Core);
        
        $MenuUser = new EeMenuUser($this->Core);
        $Menus = $MenuUser->Find("1= 1 ORDER BY Position");
       
        if(count($Menus) > 0){
            $apps = [];
             
             foreach($Menus as $menu){
                $app = new EeAppApp($this->Core);
                $app->Name->Value = $menu->AppCode->Value;
                $app->Description->Value = $this->Core->GetCode($menu->Code->Value);
                $app->Widget->Value = $menu->Data->Value;
              
                $apps[] = $app;
             }
            
             $view->AddElement(new ElementView("Apps", $apps));
        } else {

            $apps = \Apps\EeApp\Helper\AppHelper::GetForMember($this->Core);

            foreach($apps as $appBase){
                $app = new EeAppApp($this->Core);
                $app->Name->Value = $appBase->Name->Value;
                $app->Description->Value = $this->Core->GetCode("Menu.Member" . $appBase->Name->Value);
                $appTrasnlate[] = $app;
             }
            $view->AddElement(new ElementView("Apps", $appTrasnlate));
        }
       
        return $view->Render();
    }
}