00001 <?php
00002
00003
00004 lt_include( PLOG_CLASS_PATH."class/locale/locales.class.php" );
00005 lt_include( PLOG_CLASS_PATH."class/template/templateservice.class.php" );
00006 lt_include( PLOG_CLASS_PATH."class/template/menu/menu.class.php" );
00007 lt_include( PLOG_CLASS_PATH."class/template/menu/menuentry.class.php" );
00008 lt_include( PLOG_CLASS_PATH."class/locale/locales.class.php" );
00009
00022 class MenuRenderer
00023 {
00024 var $_menu;
00025 var $_blogInfo;
00026 var $_userInfo;
00027
00038 function MenuRenderer( $menu, $blogInfo, $userInfo )
00039 {
00040
00041 $this->_menu = $menu;
00042 $this->_blogInfo = $blogInfo;
00043 $this->_userInfo = $userInfo;
00044
00045 $config =& Config::getConfig();
00046
00047
00048 if( $blogInfo != null ) {
00049 $this->_locale =& $blogInfo->getLocale();
00050 }
00051 else {
00052 $localeCode = $config->getValue( "default_locale" );
00053 $this->_locale =& Locales::getLocale( $localeCode );
00054 }
00055 }
00056
00063 function generate( $depth = 99, $renderFunc = "Plain" )
00064 {
00065 $node = $this->_menu->getRoot();
00066
00067 if ( $renderFunc != "JavaScript" ) {
00068 return $this->_render( $node, $depth );
00069 } else {
00070 $menu = "<script type=\"text/javascript\">\n";
00071 $menu .= "<!-- \n";
00072 $menu .= "var mainMenu = [\n";
00073 $menu .= $this->_renderJS( $node, $depth );
00074 $menu .= "];\n";
00075 $menu .= "cmDraw ('menu', mainMenu, 'hbr', cmThemeOffice, 'ThemeOffice');\n";
00076 $menu .= "-->\n";
00077 $menu .= "</script>\n";
00078
00079 return $menu;
00080 }
00081 }
00082
00092 function generateAt( $nodeId, $depth = 1, $activeOpt = "", $renderFunc = "Plain" )
00093 {
00094 $nodePath = $this->_menu->entryPath( $nodeId );
00095 $node = $this->_menu->getEntryAt( $nodePath );
00096 $start = 0;
00097
00098 if ( $renderFunc == "JavaScript" ) {
00099 $menu = "<script type=\"text/javascript\">\n";
00100 $menu .= "<!-- \n";
00101 $menu .= "var mainMenu = [\n";
00102 $menu .= $this->_renderJS( $node, $depth, $start, $activeOpt );
00103 $menu .= "];\n";
00104 $menu .= "cmDraw ('menu', mainMenu, 'hbr', cmThemeOffice, 'ThemeOffice');\n";
00105 $menu .= "-->\n";
00106 $menu .= "</script>\n";
00107 return $menu;
00108 } else {
00109 return $this->_render( $node, $depth, $activeOpt );
00110 }
00111 }
00112
00120 function userCanSee( $node )
00121 {
00122
00123 if( $node->getAttribute( "admin" ) != "1" ) {
00124 if( $this->_userInfo->getId() == $this->_blogInfo->getOwnerId())
00125 return true;
00126 }
00127
00128 if( $this->_userInfo->hasPermissionByName( "edit_blog_admin_mode", 0 )) {
00129 return( true );
00130 }
00131
00132
00133 $nodeAndPerms = $node->getAttribute("andPerms");
00134 if( $nodeAndPerms != "" ) {
00135
00136
00137 $perms = explode( ",", $nodeAndPerms );
00138
00139 foreach( $perms as $perm ) {
00140 $perm = trim( $perm );
00141 if( (!$this->_userInfo->hasPermissionByName( $perm, $this->_blogInfo->getId())) &&
00142 (!$this->_userInfo->hasPermissionByName( $perm, 0 ))) {
00143 return false;
00144 }
00145 }
00146 }
00147 else {
00148
00149 $nodeOrPerms = $node->getAttribute("orPerms");
00150 if( $nodeOrPerms == "" )
00151 return true;
00152
00153
00154 $perms = explode( ",", $nodeOrPerms );
00155
00156 foreach( $perms as $perm ) {
00157 $perm = trim( $perm );
00158 if( ($this->_userInfo->hasPermissionByName( $perm, $this->_blogInfo->getId())) ||
00159 ($this->_userInfo->hasPermissionByName( $perm, 0 ))) {
00160 return true;
00161 }
00162 }
00163
00164 return false;
00165 }
00166
00167
00168 return true;
00169 }
00170
00175 function _render( $node, $depth = 0, $activeOpt = "", $menuTop = true )
00176 {
00177 $result = ($menuTop === true) ? "<ul class=\"menuTop\">" : "<li><ul class=\"menuTop\">";
00178 $depth--;
00179 foreach( $node->children as $child ) {
00180 if( $child->name != "" ) {
00181
00182 if( $this->userCanSee( $child )) {
00183 $url = $child->getAttribute( "url" );
00184 $localeId = $this->getLocaleId( $child );
00185 $cssClass = "Level_".$depth;
00186 if( $url != "" )
00187 $result .= "<li class=\"$cssClass\"><a href=\"".$child->getAttribute("url")."\">".$this->_locale->tr($localeId)."</a></li>";
00188 else
00189 $result .= "<li class=\"$cssClass\">".$this->_locale->tr($child->name)."</li>";
00190
00191 if( $depth > 0 ) {
00192 $result .= $this->_render( $child, $depth, $activeOpt, false );
00193 }
00194 }
00195 }
00196 }
00197 $result .= ($menuTop === true) ? "</ul>" : "</ul></li>";
00198
00199 return $result;
00200 }
00201
00206 function _renderJS( $node, $depth = 0, $start = 0, $activeOpt = "" )
00207 {
00208 $result = "";
00209 $depth--;
00210 foreach( $node->children as $child ) {
00211 if( $child->name != "" ) {
00212
00213 if( $this->userCanSee( $child )) {
00214 if( $start == 0 ) {
00215 $result .= "[";
00216 $start = 1;
00217 } else {
00218 $result .= ",[";
00219 }
00220
00221 $url = $child->getAttribute( "url" );
00222 $localeId = $this->getLocaleId( $child );
00223
00224
00225 $menuText = strtr($this->_locale->tr( $localeId ), array('\\'=>'\\\\',"'"=>"\\'",'"'=>'\\"',"\r"=>'\\r',"\n"=>'\\n','</'=>'<\/'));
00226
00227 if( $url != "" )
00228 $result .= "null, '".$menuText."', '".$child->getAttribute("url")."', '_self', null";
00229 else
00230 $result .= "null, '".$menuText."', '#', '_self', null";
00231
00232 if( $depth > 0 )
00233 $result .= $this->_renderJS( $child, $depth, $start, $activeOpt );
00234
00235 $result .= "]";
00236 }
00237 }
00238 }
00239
00240 return $result;
00241 }
00242
00250 function getLocaleId( $entry )
00251 {
00252 $localeId = $entry->getAttribute("localeId");
00253 if( $localeId == "" )
00254 $localeId = $entry->name;
00255
00256 return $localeId;
00257 }
00258
00264 function breadCrumbs( $entryId )
00265 {
00266 $nodePath = $this->_menu->entryPath( $entryId );
00267 $path = "";
00268
00269 if( $nodePath ) {
00270 $parts = explode( "/", $nodePath );
00271 $totalParts = count($parts);
00272 if( $totalParts == 1 ) $insertBlogLink = true;
00273 else $insertBlogLink = false;
00274
00275 $count=0;
00276 foreach( $parts as $part ) {
00277 $path .= $part;
00278 $menuEntry = $this->_menu->getEntryAt( $path );
00279 if( $menuEntry->getAttribute("ignoreBreadCrumbs") != "1" ) {
00280 $localeId = $this->getLocaleId( $menuEntry );
00281 if( $entryId == $part ) $result .= "<b>";
00282 if( $menuEntry->getAttribute("url") != "" )
00283 $result .= "<a href=\"".$menuEntry->getAttribute("url")."\">".$this->_locale->tr($localeId)."</a>";
00284 else
00285 $result .= $this->_locale->tr($part);
00286 if( $entryId == $part ) $result .= "</b>";
00287 if( $count == 0 ) $result .= " » <a href=\"?op=blogSelect&blogId=".$this->_blogInfo->getId()."\">".$this->_blogInfo->getBlog()."</a>";
00288 if( $count < $totalParts-1 ) $result .= " » ";
00289 }
00290 $count++;
00291 $path .= "/";
00292 }
00293 }
00294
00295 return $result;
00296 }
00297
00301 function getOpts( $entryId)
00302 {
00303 $nodePath = $this->_menu->entryPath( $entryId );
00304 $parts = explode( "/", $nodePath );
00305 array_pop( $parts );
00306 $parentPath = implode( "/", $parts );
00307
00308 $parentNode = $this->_menu->getNodeAt( $parentPath );
00309
00310 $children = $parentNode->children;
00311
00312
00313 return $children;
00314 }
00315 }
00316 ?>