<?php

/*
 * PuzzleApp
 * Webemyos
 * Jérôme Oliva
 * GNU Licence
 */

namespace Apps\EeApp\Module\App;

use Apps\EeApp\Entity\EeAppApp;
use Apps\EeApp\Helper\AppHelper;
use Core\Block\AjaxFormBlock\AjaxFormBlock;
use Core\Controller\Controller;
use Core\View\View;
use Core\Control\Button\Button;
use Core\Control\EntityGrid\EntityGrid;
use Core\Control\EntityGrid\EntityColumn;
use Core\Control\EntityGrid\EntityIconColumn;
use Core\Control\EntityGrid\EntityFunctionColumn;

class AppController extends Controller
{
    /**
     * Constructeur
     */
    function __construct($core="")
    {
          $this->Core = $core;
    }

    /**
     * Charges les applications de l'utilisateur
     */
    function LoadMyApp()
    {
      $view = new View(__DIR__ . "/View/LoadMyApps.tpl", $this->Core);

      //Recuperation
      $apps = AppHelper::GetByUser($this->Core, $this->Core->User->IdEntite);

      if(count($apps) > 0)
      {
          $view->AddElement($apps);
      }
      else
      {
          $view->AddElement(array());
      }
      return $view->Render();

    }

    /**
     * Popin d'ajout d'une application
     */
    function ShowAddApp($appId)
    {
        $jbApp = new AjaxFormBlock($this->Core, "jbApp");
        $jbApp->App = "EeApp";
        $jbApp->Action = "SaveApp";

        if($appId != null)
        {
           $app = new EeAppApp($this->Core);
           $app->GetById($appId);


           $jbApp->AddArgument("appId", $appId);
        }

        $jbApp->AddControls(array(
                                      array("Type"=>"EntityListBox", "Name"=> "lstCategory", "Entity"=> "Apps\EeApp\Entity\EeAppCategory", "Libelle" => $this->Core->GetCode("Category") , "Value" => ($appId != "")?$app->CategoryId->Value :""),
                                      array("Type"=>"TextBox", "Name"=> "tbName", "Libelle" => $this->Core->GetCode("Name") , "Value" => ($appId != "")?$app->Name->Value :""),
                                      array("Type"=>"TextArea", "Name"=> "tbDescription", "Libelle" => $this->Core->GetCode("Description"), "Value" => ($appId != "")?$app->Description->Value :""),
                                      array("Type"=>"Button", "Name"=> "BtnSave" , "Value" => $this->Core->GetCode("Save"), "CssClass" =>"btn btn-success" ),
                          )
                );

        return $jbApp->Show();
    }

    /**
     * Charge les applications disponibles
     */
    function LoadApps()
    {
      $view = new View(__DIR__ ."/View/LoadApps.tpl", $this->Core);

      //Recuperation
      $apps = AppHelper::GetByParameters($this->Core);
      $view->AddElement($apps);

      return $view->Render();
    }
    
    /**
     * Charge les template disponibles
     */
    function LoadTemplates()
    {
      $view = new View(__DIR__ ."/View/LoadTemplate.tpl", $this->Core);

      //Recuperation
      $template = AppHelper::GetTemplates($this->Core);
      $view->AddElement($template);

      return $view->Render();
    }
    
     /**
     * Charge les plugins disponibles
     */
    function LoadPlugins()
    {
      $view = new View(__DIR__ ."/View/LoadPlugins.tpl", $this->Core);

      //Recuperation
      $template = AppHelper::GetPlugins($this->Core);
      $view->AddElement($template);

      return $view->Render();
    }
    
    /***
     * Charge les applications disponibles du store
     */
    function LoadStore(){
      $view = new View(__DIR__ ."/View/LoadStore.tpl", $this->Core);

      return $view->Render();
    }
    
    /****
     * Menu utilisateur
     */
    function LoadMenuUser(){
       $view = new View(__DIR__ ."/View/LoadMenuUser.tpl", $this->Core);

        $gdMenuUser = new EntityGrid("gdMenuUser", $this->Core);
        $gdMenuUser->Entity = "Apps\EeApp\Entity\EeMenuUser";
        $gdMenuUser->App = "Annonce";
        $gdMenuUser->Action = "LoadMenuUser";
        $gdMenuUser->AddOrder("Position");

        $btnAdd = new Button(BUTTON, "btnAddMenuMember");
        $btnAdd->Value = $this->Core->GetCode("EeApp.AddMenuUser");

        $gdMenuUser->AddButton($btnAdd);
      
        $gdMenuUser->AddColumn(new EntityColumn($this->Core->GetCode("EeApp.Code"), "Code"));
        $gdMenuUser->AddColumn(new EntityColumn($this->Core->GetCode("EeApp.AppCode"), "AppCode"));
        $gdMenuUser->AddColumn(new EntityColumn($this->Core->GetCode("EeApp.Data"), "Data"));
        $gdMenuUser->AddColumn(new EntityColumn($this->Core->GetCode("EeApp.Position"), "Position"));
       
        $gdMenuUser->AddColumn(new EntityIconColumn("Action",
                        array(array("EditIcone", "EeAppAction.EditMenuUser", "EeAppAction.EditMenuUser"),
                    array("DeleteIcone", "EeAppAction.DeleteMenuUser", "EeAppAction.DeleteMenuUser"),
                        )
        ));

        $view->AddElement($gdMenuUser);
           
      return $view->Render();
    }
 }?>
