CodeIgniter Polska Forum

Witamy na polskiej stronie wsparcia CodeIgniter. Nie zapomnij odwiedzić naszej strony głównej

Nie jesteś zalogowany na forum.

#1 05-01-2016 14:35:31

dominservice
Użytkownik
Data rejestracji: 05-01-2016
Liczba postów: 1

Wielopoziomowe menu plus parser z codeigniter

Witam wszystkich!!!
Robię bibliotekę do wielopoziomowego menu zarządzanego z poziomu administratora w panelu CMS.
zrobiłem coś takiego

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Menu_dso {
	
	/**
	* Menu_dso
	*
	* Menu_dso this open source application for authentication CodeIgniter 3.0.3 <=
	*
	* package Menu_dso
	* author Matthew Domin
	* Copyright Copyright (c) 2016 Matthew Domin. (http://dominservice.pl/)
	* license LGPL-3.0 - http://opensource.org/licenses/LGPL-3.0
	* Link Http://dominservice.pl/package/Menu_dso
	* /

	/ **
	* Menu_dso
	*
	* Library to create a multilevel menu
	*/
	
	// database tabless used in this library
	private $menu= 'menu';
	
	
	/**
	 * Reference to the CodeIgniter instance
	 *
	 * @var object
	 */
	public $c = '';
	public $a = '';
	public $p = '';
	
	protected $CI;
	
	public function __construct()
	{
		$this->CI =& get_instance();
		if( !empty($this->CI->session->userdata('role'))  ){
			$role = array();
			$role = explode(',', $this->CI->session->userdata('role'));
			
			if( in_array(7, $role) OR in_array(8, $role) )
			{ 
				$this->c = 1;
			}
			elseif( in_array(1, $role) OR in_array(2, $role) OR in_array(3, $role) OR in_array(4, $role) )
			{
				 $this->a= 1;
			}
			elseif( in_array(6, $role) OR in_array(7, $role) )
			{
				$this->p = 1;
			}
		}
		//$this->CI->load->library('parser');
    }
	
	public function menu()
	{
		$query = $this->CI->db->select(array('*'));
		if($this->c  === 1){ 
			 $this->CI->db->where('permissions', '1');
			 $this->CI->db->or_where('permissions', '0');
			 $folder = 'user/';
		}elseif($this->a === 1){
			 $this->CI->db->where('permissions', '2');
			 $this->CI->db->or_where('permissions', '1');
			 $this->CI->db->or_where('permissions', '0');
			 $folder = 'administration/';
		} elseif($this->c !==1 AND $this->a !==1 AND $this->p !==1){
			 $this->CI->db->where('permissions', '100');
			 $this->CI->db->or_where('permissions', '0');
			 $folder = '';
		} 
		$query = $this->CI->db->order_by('parent, sort, label', "asc");
		$query = $this->CI->db->get($this->menu);
		// Select all entries from the menu table
		// Create a multidimensional array to conatin a list of items and parents
		$menu = array(
			'items' => array(),
			'parents' => array()
		);
		// Builds the array lists with data from the menu table
		foreach ( $query->result_array() as $items)
		{
			// Creates entry into items array with current menu item id ie. $menu['items'][1]
			$menu['items'][$items['id_menu']] = $items;
			// Creates entry into parents array. Parents array contains a list of all items with children
			$menu['parents'][$items['parent']][] = $items['id_menu'];
		}
		return $this->sort_menu(0, $menu, $folder);		//.var_dump($menu)
	}
	
	protected function sort_menu($parent, $menu, $folder)
	{
		 $html = "";
		   if (isset($menu['parents'][$parent]))
		   {	
				if($parent <> 0)
				{
					$html .= '
					<ul class="sub-menu">'."\n";
				}
			   foreach ($menu['parents'][$parent] as $itemId)
			   {
				   $a =''; 
				   $_link = $menu['items'][$itemId]['link']; 
				   if($this->CI->uri->segment(2)."/".$this->CI->uri->segment(3).'/'.$this->CI->uri->segment(4).'/'.$this->CI->uri->segment(5) === $_link and empty($this->CI->uri->segment(6)))
					{ 
						$a = ' active open';
					}
					elseif($this->CI->uri->segment(2)."/".$this->CI->uri->segment(3).'/'.$this->CI->uri->segment(4) === $_link  and empty($this->CI->uri->segment(5)))
					{ 
						$a = ' active open';
					}
					elseif($this->CI->uri->segment(2)."/".$this->CI->uri->segment(3) === $_link  and empty($this->CI->uri->segment(4)))
					{ 
						$a = ' active open';
					} 
					elseif($this->CI->uri->segment(2) === $_link  and empty($this->CI->uri->segment(3)))
					{ 
						$a = ' active open';
					} 
					
					if(!isset($menu['parents'][$itemId]))
					{
						$html .= '<li class="'.$a.' tooltips" data-container="body" data-placement="right" data-html="true" data-original-title="'.$menu['items'][$itemId]['description'].'"><a href="'.base_url($folder.$_link ).'"><i class="fa fa-'.$menu['items'][$itemId]['icon'].'"></i><span class="'.$menu['items'][$itemId]['color'].'">&nbsp;'.$menu['items'][$itemId]['label'].'</span></a></li>'."\n";
					}
					if(isset($menu['parents'][$itemId]))
					{
						$html .= '<li class="'.$a.' tooltips" data-container="body" data-placement="right" data-html="true" data-original-title="'.$menu['items'][$itemId]['description'].'"><a href="javascript:;" ><i class="fa fa-'.$menu['items'][$itemId]['icon'].'"></i><span class="'.$menu['items'][$itemId]['color'].'">'.$menu['items'][$itemId]['label'].'</span><span class="arrow "></span></a>'."\n";
						$html .= $this->sort_menu($itemId, $menu, $folder);
						$html .= "</li> \n";
					}
			   }
			   if($parent <> 0)
				{
					$html .= "
					</ul>\n";
				}
		   }
		   return $html;
	}
}

i to działa, więc zacząłem iść dalej, aby struktura html była w bazie danych, abym mógł tak zrobić to musiałem użyć praser

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Menu_dso {
	
	/**
	* Menu_dso
	*
	* Menu_dso this open source application for authentication CodeIgniter 3.0.3 <=
	*
	* package Menu_dso
	* author Matthew Domin
	* Copyright Copyright (c) 2016 Matthew Domin. (http://dominservice.pl/)
	* license LGPL-3.0 - http://opensource.org/licenses/LGPL-3.0
	* Link Http://dominservice.pl/package/Menu_dso
	* /

	/ **
	* Menu_dso
	*
	* Library to create a two-tier menu
	*/
	
	// database tabless used in this library
	private $menu= 'dso_menu';
	private $menu_html= 'dso_menu_html';
	
	
	/**
	 * Reference to the CodeIgniter instance
	 *
	 * @var object
	 */
	public $c = '';
	public $a = '';
	public $p = '';
	
	protected $CI;
	
	public function __construct()
	{
		$this->CI =& get_instance();
		if( !empty($this->CI->session->userdata('role'))  ){
			$role = array();
			$role = explode(',', $this->CI->session->userdata('role'));
			
			if( in_array(7, $role) OR in_array(8, $role) )
			{ 
				$this->c = 1;
			}
			elseif( in_array(1, $role) OR in_array(2, $role) OR in_array(3, $role) OR in_array(4, $role) )
			{
				 $this->a= 1;
			}
			elseif( in_array(6, $role) OR in_array(7, $role) )
			{
				$this->p = 1;
			}
		}
		$this->CI->load->library('parser');
    }
	
	public function menu()
	{
		$array = array();
		$query = $this->CI->db->select(array('*'));
		if($this->c  === 1){ 
			 $this->CI->db->where('permissions', '1');
			 $this->CI->db->or_where('permissions', '0');
			 $folder = 'user/';
		}elseif($this->a === 1){
			 $this->CI->db->where('permissions', '2');
			 $this->CI->db->or_where('permissions', '1');
			 $this->CI->db->or_where('permissions', '0');
			 $folder = 'administration/';
		} elseif($this->c !==1 AND $this->a !==1 AND $this->p !==1){
			 $this->CI->db->where('permissions', '100');
			 $this->CI->db->or_where('permissions', '0');
			 $folder = '';
		} 
		$query = $this->CI->db->order_by('parent, sort, label', "asc");
		$query = $this->CI->db->get($this->menu);
		// Select all entries from the menu table
		// Create a multidimensional array to conatin a list of items and parents
		$menu = array(
			'items' => array(),
			'parents' => array()
		);
		// Builds the array lists with data from the menu table
		foreach ( $query->result_array() as $items)
		{
			// Creates entry into items array with current menu item id ie. $menu['items'][1]
			$menu['items'][$items['id_menu']] = $items;
			// Creates entry into parents array. Parents array contains a list of all items with children
			$menu['parents'][$items['parent']][] = $items['id_menu'];
		}
		$hhh = $this->sort_menu(0, $menu, $folder, $array);
			$html = $hhh['html'];
			$array_menu = $hhh['array_menu'];
		var_dump($this->sort_menu(0, $menu, $folder, $array));
		
		return $this->CI->parser->parse_string($html, $array_menu);
	//	return	$this->sort_menu(0, $menu, $folder, $array);	//.var_dump($menu)
	}
	
	protected function sort_menu($parent, $menu, $folder, $array)
	{
		 $html = "";
		 $array_menu = "";
		 if (isset($menu['parents'][$parent]))
		 {		
			$html .= "";
			$array_menu = "";
			if($parent <> 0)
			{
				$html .= $this->menu_html_string('submenu');
				
				//'<ul class="sub-menu">'."\n";
			}
			foreach ($menu['parents'][$parent] as $itemId)
			{
				if($menu['items'][$itemId]['permissions'] === "0" OR $menu['items'][$itemId]['permissions'] === "100") $folder=''; 
				$a =''; 
				$_link = $menu['items'][$itemId]['link']; 
				if($this->CI->uri->segment(2)."/".$this->CI->uri->segment(3).'/'.$this->CI->uri->segment(4).'/'.$this->CI->uri->segment(5) === $_link and empty($this->CI->uri->segment(6)))
				{ 
					$a = ' active open';
				}
				elseif($this->CI->uri->segment(2)."/".$this->CI->uri->segment(3).'/'.$this->CI->uri->segment(4) === $_link  and empty($this->CI->uri->segment(5)))
				{ 
					$a = ' active open';
				}
				elseif($this->CI->uri->segment(2)."/".$this->CI->uri->segment(3) === $_link  and empty($this->CI->uri->segment(4)))
				{ 
					$a = ' active open';
				} 
				elseif($this->CI->uri->segment(2) === $_link  and empty($this->CI->uri->segment(3)))
				{ 
					$a = ' active open';
				} 
				
				if(!isset($menu['parents'][$itemId]))
				{
					$html .= $this->menu_html_string('link');
				}
				if(isset($menu['parents'][$itemId]))
				{
					$html .= $this->menu_html_string('link2');
					$html .= $this->sort_menu($itemId, $menu, $folder, $array);
					$html .= $this->menu_html_string('link3');
				}
				$array_menu = array(
					'a' => $a,
					'description' => $menu['items'][$itemId]['description'],
					'link' => base_url($folder.$_link),
					'icon' => $menu['items'][$itemId]['icon'],
					'color' => $menu['items'][$itemId]['color'],
					'label' => $menu['items'][$itemId]['label'],
				);
			}
			if($parent <> 0)
			{
				$html .= $this->menu_html_string('submenu2');
			}
		}
		$array = array(
			'html' => $html,
			'array_menu' => $array_menu,
		);
		return $array;
	}
	
	protected function menu_html_string($name)
	{
		$this->CI->db->select(array('*'));
		$this->CI->db->where('name_dso_menu_html', $name);
		$query2 = $this->CI->db->get($this->menu_html);
		foreach ( $query2->result_array() as $html)
		{
			$menu_html = $html['value_dso_menu_html'];
		}
		return $menu_html;
	}
}

i to już nie działa, proszę przejżyjcie ten kod co ja tam źle robię?

zrzut struktury tabeli dso_menu

-- phpMyAdmin SQL Dump
-- version 4.4.12
-- http://www.phpmyadmin.net
--
-- Host: dominservice.nazwa.pl:3307
-- Czas generowania: 05 Sty 2016, 14:07
-- Wersja serwera: 5.5.43-MariaDB-log
-- Wersja PHP: 5.5.9-1ubuntu4.14

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";

--
-- Baza danych: `dominservice_3`
--

-- --------------------------------------------------------

--
-- Struktura tabeli dla tabeli `dso_menu`
--

CREATE TABLE `dso_menu` (
  `id_menu` int(11) NOT NULL,
  `label` varchar(50) NOT NULL DEFAULT '',
  `link` varchar(100) NOT NULL DEFAULT 'javascript:;',
  `parent` int(11) NOT NULL DEFAULT '0',
  `sort` int(11) DEFAULT NULL,
  `keywords` varchar(255) CHARACTER SET utf8 COLLATE utf8_polish_ci NOT NULL DEFAULT '',
  `description` varchar(255) CHARACTER SET utf8 COLLATE utf8_polish_ci NOT NULL DEFAULT '',
  `localization` varchar(50) CHARACTER SET utf8 COLLATE utf8_polish_ci NOT NULL,
  `permissions` varchar(5) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
  `icon` varchar(50) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
  `color` varchar(25) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL DEFAULT ''
) ENGINE=InnoDB AUTO_INCREMENT=57 DEFAULT CHARSET=latin2;


--
-- Indeksy dla zrzutów tabel
--

--
-- Indexes for table `dso_menu`
--
ALTER TABLE `dso_menu`
  ADD PRIMARY KEY (`id_menu`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT dla tabeli `dso_menu`
--
ALTER TABLE `dso_menu`
  MODIFY `id_menu` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=57;

zrzut tabeli dso_menu_html

-- phpMyAdmin SQL Dump
-- version 4.4.12
-- http://www.phpmyadmin.net
--
-- Host: dominservice.nazwa.pl:3307
-- Czas generowania: 05 Sty 2016, 14:10
-- Wersja serwera: 5.5.43-MariaDB-log
-- Wersja PHP: 5.5.9-1ubuntu4.14

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";

--
-- Baza danych: `dominservice_3`
--

-- --------------------------------------------------------

--
-- Struktura tabeli dla tabeli `dso_menu_html`
--

CREATE TABLE `dso_menu_html` (
  `id_dso_menu_html` int(11) NOT NULL,
  `name_dso_menu_html` varchar(255) NOT NULL,
  `value_dso_menu_html` text NOT NULL,
  `date_upd_dso_menu_html` datetime NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=latin2;

--
-- Zrzut danych tabeli `dso_menu_html`
--

INSERT INTO `dso_menu_html` (`id_dso_menu_html`, `name_dso_menu_html`, `value_dso_menu_html`, `date_upd_dso_menu_html`) VALUES
(4, 'link', '<li class="{a} tooltips" data-container="body" data-placement="right" data-html="true" data-original-title="{description}"><a href="{link}" ><i class="fa fa-{icon}"></i><span class="{color}">{label}</span><span class="arrow "></span></a><li>\n', '2016-01-05 09:00:00'),
(5, 'link2', '<li class="{a} tooltips" data-container="body" data-placement="right" data-html="true" data-original-title="{description}"><a href="{link}" ><i class="fa fa-{icon}"></i><span class="{color}">{label}</span><span class="arrow "></span></a>\n', '2016-01-05 09:00:00'),
(6, 'submenu', '<ul class="sub-menu">\n', '0000-00-00 00:00:00'),
(7, 'submenu2', '</ul>\n', '0000-00-00 00:00:00'),
(8, 'link3', '</li>\n', '0000-00-00 00:00:00');

--
-- Indeksy dla zrzutów tabel
--

--
-- Indexes for table `dso_menu_html`
--
ALTER TABLE `dso_menu_html`
  ADD PRIMARY KEY (`id_dso_menu_html`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT dla tabeli `dso_menu_html`
--
ALTER TABLE `dso_menu_html`
  MODIFY `id_dso_menu_html` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=9;

proszę o sugestie każda pomoc będzie cenna
Pozdrawiam

Ostatnio edytowany przez dominservice (05-01-2016 15:11:23)

Offline

Stopka