<?php

Goweb::loadClass('Cms_GenericController', APPLICATION_PATH);

/**
 * Goweb Sitemanager 5 Framework
 * @version    5.0.0
 * @category   Frontoffice
 * @package    Cms
 * @subpackage Controller
 * @author     I&D <dev@goweb.pt>, Pedro Pinto <ppinto@goweb.pt>
 * @copyright  2009 (c) Goweb (http://www.goweb.pt)
 *
 * A classe _Controller para o Front-Office disponibiliza
 * as accoes referentes a cada Modulo.
 *
 */
class Cms_Controller extends Cms_GenericController
{
	public function footerAction(){
        $this->view->setViewsPath(VIEWS_PATH . '/Cms');
    }
	
	public function headerhomeAction(){
        $this->view->setViewsPath(VIEWS_PATH . '/Cms');
    }
	
	
	public function headerAction(){
        $this->view->setViewsPath(VIEWS_PATH . '/Cms');
    }
	/**
     * Menu em lista
     *
     */
    public function menuListTopoAction(){
        $pai_id = $this->_getParam('pai_id');
        if (!$pai_id) $pai_id = 0;
        $id = $this->_getParam('id_selected');
        if (!$id) $id = 0;
        $this->view->setViewsPath(VIEWS_PATH . DIRECTORY_SEPARATOR . 'Cms');
        $this->_helper->viewRenderer->setRender('menu-list');
        $this->view->menu = '';
        if ($this->_validateId($id) && $id > 0 && !$this->_model->find($id)) return false;
        if ($this->_validateId($pai_id) && $pai_id > 0 && !$this->_model->find($pai_id)) return false;
        $tree = $this->_getTree();
        $tree->build($pai_id);
        $parents = $this->_getAllParents($id);
		
        $this->view->menu = $this->_getListTopo(array_reverse($tree->getTree()), null, $pai_id, $id, 0, $parents);
    }
	
	/**
     * obter lista ordenada
     *
     * @param Goweb_Tree $tree
     * @param String $html
     * @param int $parentId
     * @param int $idSelected
     * @param int $level
     * @return String
     */
    protected function _getListTopo($tree = null, $html = null, $parentId = 0, $idSelected = 0, $level = 0, $parents = array()){
        if ($tree) {
            if (is_null($html)){
                $html = str_pad(' ', $level) . "<ul id='menu_id_$parentId' class='pro_dropdown_3'>\n";
            } else {
                if ($level == 2){
                    $html .= str_pad(' ', $level) . "<ul class='sub'>\n";
                }else{
                    $html .= str_pad(' ', $level) . "<ul>\n";
                }
            }
            $count = 0;
            $limit = $this->_getParam('limit');
            foreach ($tree as $node) {
                if ($limit && $node->pai_id == $parentId && $count >= $limit) continue;
                if ($node->pai_id == $parentId) $count++;
                $html .= str_pad(' ', $level+1) . "<li ";
                $cssClass = '';
                if (count($node->childs) > 0) {
                    if (strlen($cssClass)) $cssClass .= ' ';
                    $cssClass .= 'child';
                }
                if ($level == 0){
                    if (strlen($cssClass)) $cssClass .= ' ';
                    $cssClass .= 'top';
                }
                if (strlen($cssClass)) $html .= ' class="' . $cssClass . '"';
                $html .= "><a href='";
                if (is_null($node->url)){
                    $html .= $this->view->baseUrl ."' onclick='return false;'";
                } else {
                    $html .= ($node->url ? $this->view->link('/' . $this->_goString . '/'.$node->url) : $this->view->link('/cms/view/id/' . $node->id)) . "'";
                }
                $cssClass = '';
                if (in_array($node->id, $parents)){
                    $cssClass .= 'selected';
                }
                if ($level == 0){
                    if (strlen($cssClass)) $cssClass .= ' ';
                    $cssClass .= " top_link";
                }
                if (count($node->childs) > 0 && $level > 0) {
                    if (strlen($cssClass)) $cssClass .= ' ';
                    $cssClass .= 'fly';
                }
                if (strlen($cssClass)) $html .= ' class="' . $cssClass . '"';
                if ($this->_model->colExists('popup') && $node->popup){
                    $html .= ' target="_blank"';
                }
                $html .= ">";
                if ($level == 0){
                    $html .= "<span";
                    if (count($node->childs) > 0) $html .= " class='down'";
                    $html .= ">";
                }
                $html .=  '<span>' . $node->titulo . '</span>';
                if ($level == 0) $html .=  '</span>';
                $html .= "</a>";
                if (count($node->childs) > 0) {
                    $level++;
                    $level++;
                    $html = $this->_getListTopo($node->childs, $html, $parentId, $idSelected, $level, $parents);
                    $level--;
                    $level--;
                }
                $html .= str_pad(' ', $level+1) . "</li>\n";
            }
            $html .= str_pad(' ', $level) . "</ul>\n";
        }
        return $html;
    }
	
	
	/**
     * Acao para exibir o menu de footer
     */
    public function menuFooterAction() {
	
        $pai_id = $this->_getParam('pai_id');
        $limit = $this->_getParam('limit');
        if (!$limit)
        $limit ="6";
        // paginas
        $this->view->setViewsPath(VIEWS_PATH . DIRECTORY_SEPARATOR . 'Cms');
        /**
         * Query usando a plataforma!!
         */
        $pagina=$this->_model->getTableName();


        $query="";
        if ($this->_session->logged){
            $perfilId = $this->_session->utilizador['perfil_id'];
            $query .= " (permissao.perfil_id={$perfilId} AND {$pagina}.activo=1 AND {$pagina}.sistema=0 AND {$pagina}.menu=1 AND {$pagina}.pai_id={$pai_id}) OR ";
        }
        $query .= " ({$pagina}.activo=1 AND {$pagina}.sistema=0 AND {$pagina}.menu=1 AND {$pagina}.privado=0  AND {$pagina}.pai_id={$pai_id}) ";

        $this->_model->where($query);
        if ($this->_session->logged)
        $this->_model->joinLeft("cms_permissao AS permissao", "{$pagina}.id=permissao.pagina_id");


        $this->_model->limit($limit, 0);
        $this->_model->order('ordem');
        $result = $this->_model->fetchAll();

        if ($result)
        $this->view->count = $result->count();
        $this->view->result = $result;
        $this->_helper->viewRenderer->setRender('menu_footer');
    }
	
	
}