<?php

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

namespace Apps\Seo;

use Core\Core\Core;
use Apps\Base\Base;
use Apps\Seo\Entity\SeoPage;
use Apps\Seo\Module\Widget\WidgetController;
use Apps\Seo\Module\Admin\AdminController;

use Apps\Seo\Helper\PageHelper;
use Core\Core\Request;

class Seo extends Base {

    /**
     * Auteur et version
     * */
    public $Author = 'Webemyos';
    public $Version = '1.0.0.0';

    /**
     * Constructeur
     * */
    function __construct($core) {
        $this->Core = Core::getInstance();
        parent::__construct($this->Core, "Seo");
    }

    /**
     * Execution de l'application
     */
    function Run() {
        echo parent::RunApp($this->Core, "Seo", "Seo");
    }

    /**
     * Définie les routes publiques
     */
    function GetRoute($routes = "") {
        parent::GetRoute(array());
        return $this->Route;
    }

    /*     * *
     * Fait le lien entre le pages et 
     */

    function SetTitleAndDescription($app, $action, $uri ="") {
        $SeoPage = new SeoPage($this->Core);

        if ($action == "") {
            $pages = $SeoPage->Find("Url = '" . $app . "'");
        } else {

            if($uri != ""){
                
                $pages = $SeoPage->Find("Url = '" . $app . "/" . $action . "/" . $uri.  "'");
            } else{
                $pages = $SeoPage->Find("Url = '" . $app . "/" . $action . "'");
            }
        }

        if (count($pages) > 0) {

            $page = $pages[0];

            $this->Core->MasterView->Set("Title", $page->Title->Value);
            $this->Core->MasterView->Set("Description", $page->Description->Value);
        }
    }

    /*     * *
     * Obtient les widget
     */

    public function GetWidget($type, $params) {
    
        switch ($type) {
            case "GoogleTagPlugin" :
                $widget = new \Plugins\GoogleTagPlugin\GoogleTagPlugin($this->Core);
                break;
             default : 
                $widget = new WidgetController($this->Core);
                return $widget->Show($type, $params);
             break;
        }
        return $widget->Render($params);

    }
    
    /**
     * Sauvegare une page avec le page Form 
     */
    public function SavePageSeoForm() {
        return PageHelper::SavePageSeoForm($this->Core, Request::GetPosts());
    }

    /***
     * Supprime une page
     */
    public function DeletePage() {
        return PageHelper::DeletePage($this->Core, Request::GetPost("PageId"));
    }


    /*     * *
    * Get cms Addable widget
    */

    public function GetListWidget() {
        $availablesPlugin = array();
        
        $plugins = \Apps\EeApp\Helper\AppHelper::GetPluginApp($this->Core, "Seo");

        foreach($plugins as $plugin){
            
           $availablesPlugin[] = array("Name" => $plugin->PluginName->Value,
                "Description" => $this->Core->GetCode("Seo.Description" .$plugin->PluginName->Value),
                "Parameters"  => $this->GetParameter()
            );
        }
        
        return $availablesPlugin;
    }

     /***
     * List of ressource
     */
    public function GetParameter(){
        $textBoxParameter = new \Core\Control\TextBox\TextBox("Data", $this->Core);
        return $textBoxParameter->Render();
    }
    
    public function RefreshGrid(){
        
        $adminController = new AdminController($this->Core);
    
        return $adminController->GetTabPage();
    }


}

?>