[Phpcms-plugins-cvs] admin4phpCMS/modules/phpcms-content/includes class.phpcmsMenu.php,NONE,1.1
Brought to you by:
mjahn
From: Martin J. <mj...@us...> - 2005-05-26 15:56:16
|
Update of /cvsroot/phpcms-plugins/admin4phpCMS/modules/phpcms-content/includes In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv19290/modules/phpcms-content/includes Added Files: class.phpcmsMenu.php Log Message: beginning of phpcms-content and filemanager --- NEW FILE: class.phpcmsMenu.php --- <?php /** * Backend of menufile-editor * * @author Martin Jahn <mj...@us...> * @license http://opensource.org/licenses/gpl-license.php GNU Public License * @copyright Copyright (c) 2004, Martin Jahn * @version $Id: class.phpcmsMenu.php,v 1.1 2005/05/26 15:55:55 mjahn Exp $ * @package admin4phpCMS * @subpackage module_phpcms **/ class phpcmsMenu { function phpcmsMenu () { return $this->__construct (); } function __construct () { $this->docroot = realpath($_SERVER['DOCUMENT_ROOT']); } function insert ($class, $menuEntry, $after = true, $recursive = false) { if (!isset($this->quicklink[$class])) { return false; } $parentclass = substr($class, 0, strrpos ($class, '.')); if ($parentclass == $this->root) { $menu =& $this->menu; } else { $menu =& $this->quicklink[$parentclass]['_sub_']; } for ($i = 0; $i < count ($menu); $i++) { if ($menu[$i]['CLASS'] != $class) { continue; } break; } if (isset($this->quicklink[$menuEntry['CLASS']])) { for ($j = 0; isset($this->quicklink[$menuEntry['CLASS'].$j]); $j++); $menuEntry['CLASS'] = $menuEntry['CLASS'].$j; } if ($after) { $i = ($i == count($menu)) ? count($menu) : $i + 2; array_splice($menu, $i, 0, array($menuEntry)); } else { $i = ($i == 0) ? 0 : $i; array_splice($menu, $i, 0, array($menuEntry)); } $this->quicklink[$menuEntry['CLASS']] = $menu[$i]; } function delete ($class) { if (isset($this->quicklink[$class])) { return false; } $parentclass = substr($class, 0, strrpos ($class, '.')); $menu =& $this->quicklink[$parentclass]; for ($i = 0; $i < count ($menu); $i++) { if ($menu[$i]['CLASS'] != $class) { continue; } break; } $this->quicklink['parentclass'] = array_splice($menu, $i, 1); } function load ($menufile) { if (!file_exists($menufile) || !is_file($menufile)) { return false; } $this->menufile = $menufile; $this->__parse(); } function save ($tofile, $as = 'phpcms') { if (!file_exists(dirname($tofile)) || !is_writeable($tofile)) { return false; } $this->savefile = $tofile; switch ($as) { case 'phpcms': $o = $this->__saveAsPhpCMS(); break; case 'xml': $o = $this->__saveAsXML(); break; default; } $fh = fopen ($tofile, 'wb'); fwrite ($fh, $o); fclose ($fh); } function getEntryByClass ($class) { $parentclass = substr ($class, 0, strrpos ($class, '.')); if (!isset ($this->menuclasses)) { return false; } for ($i = 0; $i < count($this->menuclasses); $i++) { if ($this->menuclasses[$i] == $parentclass) { } } } function __parse () { // check if the menufile exists if (!file_exists($this->menufile) || !is_file($this->menufile)) { return false; } // get content from menufile $data = file ($this->menufile); // initialize some variables $this->menu = array(); $new = false; $menufields = array(); $menucounter = -1; $entrycounter = 0; // walk through the lines of the menufile for ($i = 0; $i < count($data); $i++) { // remove trailing and leading whitespace $line = trim($data[$i]); // check for empty line if (trim($line) == '') { continue; } // check for comment-line if ($line[0] == ';') { countinue; } // check for menu-definition if (substr($line, 0, 5) == 'MENU:') { if (substr($line, -1) == ';') { $line = trim(substr($line, 0, strrpos($line, ';'))); } $menulevel = trim (substr($line, 5)); // increase the counter for the menublocks $menucounter++; // initialize the new element in the menuarray $this->menu[$menucounter] = array(); $entrycounter = 0; // save actual menuposition in pointer $actual =& $this->menu[$menucounter]; continue; } // check for field-definition line if (substr($line, 0, 6) == 'CLASS;') { if (substr($line, -1) == ';') { $line = trim(substr($line, 0, strrpos($line, ';'))); } // get field definitions $menufielddata = explode(';',$line); // initialize some variables $k = 0; $menufields = array(); // walk throught the array of field definitions for ($j = 0; $j < count($menufielddata); $j++) { $l = trim($menufielddata[$j]); // save name of field definition $menufields[$k++] = $l; } continue; } // now we have a menu entry, explode into field elements $entry = explode(';', $line); for ($j = 0; $j < count($menufields); $j++) { // put data of menu-entry into menu-structure $actual[$entrycounter][$menufields[$j]] = trim($entry[$j]); } $actual[$entrycounter]['_level_'] = $menulevel; // increase menucounter $entrycounter++; $this->menuclasses[$menucounter] = substr($actual[0]['CLASS'], 0, strrpos($actual[0]['CLASS'], '.')); } // sort the array with the menu-classes $temp = $this->menuclasses; sort ($temp); $this->root = $temp[0]; // complete building of menu-structure $this->menu = $this->__recurseMenu($this->root); } /** * recursive construction of the menu-tree **/ function __recurseMenu ($menuclass) { if (!in_array ($menuclass, $this->menuclasses)) { return false; } $rootmenu = $this->__getKeyByValue ($this->menuclasses, $menuclass); $menu = array(); $actual = 0; for ($i = 0; $i < count ($this->menu[$rootmenu]); $i++) { $submenuclass = $this->menu[$rootmenu][$i]['CLASS']; $this->quicklink[$submenuclass] =& $menu[$actual]; $menu[$actual] = $this->menu[$rootmenu][$i]; if ($sub = $this->__recurseMenu ($submenuclass)) { $menu[$actual]['_sub_'] = $sub; } $actual++; } return $menu; } function __getKeyByValue($array, $value) { for ($i = 0; $i < count($array); $i++) { if (isset ($array [$i]) && $array[$i] == $value) { return $i; } } } function __saveAsPhpCMS () { $i = 0; $o = ''; $menu = array($this->menu); while (isset($menu[$i])) { $o .= 'MENU:'.$menu[$i][0]['_level_']."\n"; $menuline = $menu[$i][0]; unset ($menuline['_level_']); if (isset($menuline['_sub_'])) { unset ($menuline['_sub_']); } $o .= join(';', array_keys($menuline))."\n"; for ($j = 0; $j < count ($menu[$i]); $j++) { $menuEntry = $menu[$i][$j]; if (isset($menuEntry['_sub_'])) { unset($menuEntry['_sub_']); $menu[] = $menu[$i][$j]['_sub_']; } if (isset($menuEntry['_level_'])) { unset($menuEntry['_level_']); } $o .= join (";\t", $menuEntry)."\n"; } $o .= "\n"; $i++; } return $o; } } ?> |