<?php

class MenuComponent extends Component {

	public function getMainMenu(){
		$pageModel = ClassRegistry::init('Page');
		$main_menu = Cache::read('main_menu', 'short');
		if(!$main_menu){
			$main_menu = $pageModel->find('all', array(
				'fields' => array('id', 'title', 'alias')
			));
			Cache::write('main_menu', $main_menu, 'short');
		}
		return $main_menu;
	}

	public function getCatMenu($admin = false){
		$categoryModel = ClassRegistry::init('Category');
		if($admin){
			$cat_menu_tree = $categoryModel->find('threaded');
			$cat_menu = $this->_catMenuHtml($cat_menu_tree, $admin);
			return $cat_menu;
		}
		$cat_menu = Cache::read('cat_menu', 'short');
		if(!$cat_menu){
			$cat_menu_tree = $categoryModel->find('threaded');
			$cat_menu = $this->_catMenuHtml($cat_menu_tree, $admin);
			Cache::write('cat_menu', $cat_menu, 'short');
		}
		return $cat_menu;
	}

	protected function _catMenuHtml($cat_menu_tree = false, $admin){
		if(!$cat_menu_tree) return false;
		$html = '';
		foreach($cat_menu_tree as $item){
			$html .= $this->_catMenuTemplate($item, $admin);
		}
		return $html;
	}

	protected function _catMenuTemplate($category, $admin){
		ob_start();
		include APP . "View/Elements/menu_tpl.ctp";
		return ob_get_clean();
	}

}