소스 검색

#969 XML: the menu option enable_admin_only hides the menu for everyone

git-svn-id: http://svn.code.sf.net/p/itop/code/trunk@3377 a333f486-631f-4898-b8df-5754b55c2be0
romainq 10 년 전
부모
커밋
1226e9d800
1개의 변경된 파일38개의 추가작업 그리고 26개의 파일을 삭제
  1. 38 26
      setup/compiler.class.inc.php

+ 38 - 26
setup/compiler.class.inc.php

@@ -204,14 +204,18 @@ class MFCompiler
 			}
 			else
 			{
+				$sMenuCreationClass = 'MenuCreation_'.preg_replace('/[^A-Za-z0-9_]/', '_', $sModuleName);
 				$sCompiledCode .=
 <<<EOF
 
 //
 // Menus
 //
-
-global \$__comp_menus__; // ensure that the global variable is indeed global !
+class $sMenuCreationClass extends ModuleHandlerAPI
+{
+	public static function OnMenuCreation()
+	{
+		global \$__comp_menus__; // ensure that the global variable is indeed global !
 
 EOF;
 				// Preliminary: determine parent menus not defined within the current module
@@ -229,6 +233,8 @@ EOF;
 					$aMenusToLoad[] = $sMenuId;
 				}
 				$aMenusToLoad = array_unique($aMenusToLoad);
+				$aMenusForAll = array();
+				$aMenusForAdmins = array();
 				foreach($aMenusToLoad as $sMenuId)
 				{
 					$oMenuNode = $aMenuNodes[$sMenuId];
@@ -249,13 +255,41 @@ EOF;
 					}
 					try
 					{
-						$sCompiledCode .= $this->CompileMenu($oMenuNode, $sTempTargetDir, $sFinalTargetDir, $sRelativeDir, $oP);
+						$aMenuLines = $this->CompileMenu($oMenuNode, $sTempTargetDir, $sFinalTargetDir, $sRelativeDir, $oP);
 					}
 					catch (DOMFormatException $e)
 					{
 						throw new Exception("Failed to process menu '$sMenuId', from '$sModuleRootDir': ".$e->getMessage());
 					}
+					if ($oMenuNode->GetChildText('enable_admin_only') == '1')
+					{
+						$aMenusForAdmins = array_merge($aMenusForAdmins, $aMenuLines);
+					}
+					else
+					{
+						$aMenusForAll = array_merge($aMenusForAll, $aMenuLines);
+					}
 				}
+				$sIndent = "\t\t";
+				foreach ($aMenusForAll as $sPHPLine)
+				{
+					$sCompiledCode .= $sIndent.$sPHPLine."\n";
+				}
+				if (count($aMenusForAdmins) > 0)
+				{
+					$sCompiledCode .= $sIndent."if (UserRights::IsAdministrator())\n";
+					$sCompiledCode .= $sIndent."{\n";
+					foreach ($aMenusForAdmins as $sPHPLine)
+					{
+						$sCompiledCode .= $sIndent."\t".$sPHPLine."\n";
+					}
+					$sCompiledCode .= $sIndent."}\n";
+				}
+				$sCompiledCode .=
+<<<EOF
+	}
+} // class $sMenuCreationClass
+EOF;
 			}
 
 			// User rights
@@ -1451,35 +1485,13 @@ EOF;
 			}
 		}
 
-		$sIndent = '';
 		$aPHPMenu = array("\$__comp_menus__['$sMenuId'] = $sNewMenu");
 		if ($sAutoReload = $oMenu->GetChildText('auto_reload'))
 		{
 			$sAutoReload = self::QuoteForPHP($sAutoReload);
 			$aPHPMenu[] = "\$__comp_menus__['$sMenuId']->SetParameters(array('auto_reload' => $sAutoReload));";
 		}
-
-		$sAdminOnly = $oMenu->GetChildText('enable_admin_only');
-		if ($sAdminOnly && ($sAdminOnly == '1'))
-		{
-			$sPHP = $sIndent."if (UserRights::IsAdministrator())\n";
-			$sPHP .= $sIndent."{\n";
-			foreach($aPHPMenu as $sPHPLine)
-			{
-				$sPHP .= $sIndent."   $sPHPLine\n";
-			}
-			$sPHP .= $sIndent."}\n";
-		}
-		else
-		{
-			$sPHP = '';
-			foreach($aPHPMenu as $sPHPLine)
-			{
-				$sPHP .= $sIndent.$sPHPLine."\n";
-			}
-		}
-
-		return $sPHP;
+		return $aPHPMenu;
 	} // function CompileMenu
 
 	/**