CoolFace
Apppublic

kenken999/php

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
runnermenu.php213 linesDownload Raw Back to classes
1<?php2require_once( getabspath("include/menuitem.php") );3class RunnerMenu {4	/** @var MenuItem */5	protected $root;6	protected $_name;7 8	protected $activeItem = null;9 10	function __construct( $name, $root ) {11		$this->_name = $name;12		$this->root = $root;13	}14 15	public function name() {16		return $this->_name;17	}18 19	/**20	 * return corresponding RunnerMenu object21	 * @return RunnerMenu22	 */23	public static function getMenuObject( $name ) {24		if( !$name ) {25			$name = "main";26		}27		global $menuCache;28		if( $menuCache[ $name ] ) {29			return $menuCache[ $name ];30		}31 32		$menuNodes = loadMenuNodes( $name );33 34		$nullParent = NULL;35		$rootInfoArr = array("id"=>0, "href"=>"");36		/* $menuNodesIndex must be set to 0 for to operate properly */37		global $menuNodesIndex;38		$menuNodesIndex = 0;39 40		$menuMap = array();41		$rootElement = new MenuItem($rootInfoArr, $menuNodes, $nullParent, $menuMap, $name );42		global $menuNodesCache;43		unset( $menuNodesCache[ $name ] );44 45		$menuObj = new RunnerMenu( $name, $rootElement );46		$menuCache[ $name ] = $menuObj;47 48		global $globalEvents;49		if( $globalEvents->exists("ModifyMenu") ) {50			$globalEvents->ModifyMenu( $menuObj );51		}52		53		return $menuObj;54	}55 56	public function getRoot() {57		return $this->root;58	}59 60	/**61	 * Finds active item62	 * @return MenuItem63	 */64	public function findActiveItem( $savedActiveId, $hostTable, $hostPageType ) {65		$item = $this->root->findActiveItem( $savedActiveId, $hostTable, $hostPageType );66		if( !$item && $savedActiveId ) {67			// $savedActiveId might belong to other page68			$item = $this->root->findActiveItem( null, $hostTable, $hostPageType );69		}70		return $item;71 72	}73	74	/**75	 * @return MenuItem76	 */77	public function addURL( $label, $url, $parentId = 0 ) {78		$item = $this->makeURLItem( $label, $url );79		$this->addItemToMenu( $item, $parentId );80		return $item;81	}82 83	/**84	 * @return MenuItem85	 */86	public function addPageLink( $label, $table, $pageType, $parentId = 0 ) {87		$item = $this->makePageLinkItem( $label, $table, $pageType );88		$this->addItemToMenu( $item, $parentId );89		return $item;90	}91 92	/**93	 * @return MenuItem94	 */95	public function addGroup( $label, $parentId = 0 ) {96		$item = $this->makeGroupItem( $label );97		$this->addItemToMenu( $item, $parentId );98		return $item;99	}100 101	// ?102	public function makeGroupItem( $label ) {103		$menuNode = array();104		$menuNode["id"] = MenuItem::maxChildId( $this->root ) + 1;105		$menuNode["type"] = "Group";106		$menuNode["title"] = $label;107 108		// pass stub variables109		$childNodes = array();110		$parent = array(); 111		return new MenuItem( $menuNode, $childNodes, $parent, $this->root->menuTableMap, $this->name() );112	}113 114	public function makePageLinkItem( $label, $table, $pageType ) {115		$menuNode = array();116		$menuNode["id"] = MenuItem::maxChildId( $this->root ) + 1;117		$menuNode["type"] = "Leaf";118		$menuNode["title"] = $label;119		$menuNode["linkType"] = "Internal";120		$menuNode["pageType"] = $pageType;121		$menuNode["table"] = $table;122 123		// pass stub variables124		$childNodes = array();125		$parent = array(); 126		return new MenuItem( $menuNode, $childNodes, $parent, $this->root->menuTableMap, $this->name() );127	}128 129	public function makeURLItem( $label, $url ) {130		$menuNode = array();131		$menuNode["id"] = MenuItem::maxChildId( $this->root ) + 1;132		$menuNode["type"] = "Leaf";133		$menuNode["title"] = $label;134		$menuNode["linkType"] = "External";135		$menuNode["href"] = $url;136 137		// pass stub variables138		$childNodes = array();139		$parent = array(); 140		return new MenuItem( $menuNode, $childNodes, $parent, $this->root->menuTableMap, $this->name() );141	}142 143	protected function addItemToMenu( $menuItem, $parentId ) {144		$parent = MenuItem::findItemById( $this->root, $parentId );145 146		if( !$parent ) {147			return false;148		}149 150		$parent->AddChild( $menuItem );151	}152 153	public function removeItem( $id ) {154		$item = MenuItem::findItemById( $this->root, $id );155		if( !$item ) {156			return;157		}158 159		if( $item === $this->root ) {160			return;161		}162 163		// must have parent, because root case already handled164		$parent = $item->parentItem;165 166		// copy parents children, exclude needle167		$filteredChildren = array();168		foreach( $parent->children as $child ) {169			if( $child !== $item ) {170				$filteredChildren[] = $child;171			}172		}173 174		$parent->children = $filteredChildren;175		// requires recalculation (depends on children)176	}177 178	public function copyItem( $id, $parentId = -1 ) {179		$item = MenuItem::findItemById( $this->root, $id );180		if( !$item ) {181			return false;182		}183 184		$clone = MenuItem::cloneNode( $item );185		// set unique id186		$clone->id = MenuItem::maxChildId( $this->root ) + 1;187		188		// if $parentId == -1, add as $item sibling189		$parent = $parentId == -1 ? $item->parentItem : MenuItem::findItemById( $this->root, $parentId );190		if( !$parent ) {191			return false;192		}193 194		$parent->AddChild( $clone );195 196		return $clone;197	}198 199	public function getItem( $id ) {200		return MenuItem::findItemById( $this->root, $id );201	}202 203 204	/**205	 * Collect all menu nodes206	 * @return Array<MenuItem>207	 */208	public function collectNodes() {209		return $this->root->collectNodes();210	}211}212 213?>