[ Index ] |
|
Code source de LifeType 1.2.4 |
1 <?php 2 3 4 lt_include( PLOG_CLASS_PATH."class/locale/locales.class.php" ); 5 lt_include( PLOG_CLASS_PATH."class/template/templateservice.class.php" ); 6 lt_include( PLOG_CLASS_PATH."class/template/menu/menu.class.php" ); 7 lt_include( PLOG_CLASS_PATH."class/template/menu/menuentry.class.php" ); 8 lt_include( PLOG_CLASS_PATH."class/locale/locales.class.php" ); 9 10 /** 11 * \ingroup Template 12 * 13 * dumps the contents of the menu in a tree-fashion way using unordered html lists. It is necessary 14 * to use some additional CSS styling to give some formatting to the menu. 15 * 16 * This class is only used at the presentation layer of pLog to generate the menu structure, 17 * and class users should never need to create an instance of this class. 18 * 19 * See the "Menu API reference" document in the wiki for more information: 20 * http://wiki.plogworld.net/index.php/PLog_1.0/The_Menu_API 21 */ 22 class MenuRenderer 23 { 24 var $_menu; 25 var $_blogInfo; 26 var $_userInfo; 27 28 /** 29 * Initializes the renderer 30 * 31 * @param menu A vaid Menu object 32 * @param blogInfo A reference to a BlogInfo object containing information of the blog for whom 33 * we are going to render this menu tree. 34 * @param userInfo A reference to a UserInfo object containing information of the user for whom 35 * we are going to render this menu tree, because we need to know which options he/she is allowed 36 * to see and which not. 37 */ 38 function MenuRenderer( $menu, $blogInfo, $userInfo ) 39 { 40 41 $this->_menu = $menu; 42 $this->_blogInfo = $blogInfo; 43 $this->_userInfo = $userInfo; 44 45 $config =& Config::getConfig(); 46 47 // load the right locale 48 if( $blogInfo != null ) { 49 $this->_locale =& $blogInfo->getLocale(); 50 } 51 else { 52 $localeCode = $config->getValue( "default_locale" ); 53 $this->_locale =& Locales::getLocale( $localeCode ); 54 } 55 } 56 57 /** 58 * renders the contents of the menu 59 * 60 * @param depth How many depth levels we should generate. 61 * @param renderFunc render method for menu tree , the value is "JavaScript" and "Plain" 62 */ 63 function generate( $depth = 99, $renderFunc = "Plain" ) 64 { 65 $node = $this->_menu->getRoot(); 66 67 if ( $renderFunc != "JavaScript" ) { 68 return $this->_render( $node, $depth ); 69 } else { 70 $menu = "<script type=\"text/javascript\">\n"; 71 $menu .= "<!-- \n"; 72 $menu .= "var mainMenu = [\n"; 73 $menu .= $this->_renderJS( $node, $depth ); 74 $menu .= "];\n"; 75 $menu .= "cmDraw ('menu', mainMenu, 'hbr', cmThemeOffice, 'ThemeOffice');\n"; 76 $menu .= "-->\n"; 77 $menu .= "</script>\n"; 78 79 return $menu; 80 } 81 } 82 83 /** 84 * Generates a tree starting from another node which can be different than the 85 * root node 86 * 87 * @param nodeId 88 * @param depth 89 * @param activeOpt 90 * @param renderFunc render method for menu tree , the value is "JavaScript" and "Plain" 91 */ 92 function generateAt( $nodeId, $depth = 1, $activeOpt = "", $renderFunc = "Plain" ) 93 { 94 $nodePath = $this->_menu->entryPath( $nodeId ); 95 $node = $this->_menu->getEntryAt( $nodePath ); 96 $start = 0; 97 98 if ( $renderFunc == "JavaScript" ) { 99 $menu = "<script type=\"text/javascript\">\n"; 100 $menu .= "<!-- \n"; 101 $menu .= "var mainMenu = [\n"; 102 $menu .= $this->_renderJS( $node, $depth, $start, $activeOpt ); 103 $menu .= "];\n"; 104 $menu .= "cmDraw ('menu', mainMenu, 'hbr', cmThemeOffice, 'ThemeOffice');\n"; 105 $menu .= "-->\n"; 106 $menu .= "</script>\n"; 107 return $menu; 108 } else { 109 return $this->_render( $node, $depth, $activeOpt ); 110 } 111 } 112 113 /** 114 * returns true if the user has enough permissions to see this node from the menu 115 * 116 * @param node 117 * @param userInfo 118 * @return true if the user can, false otherwise 119 */ 120 function userCanSee( $node ) 121 { 122 // if the user is the blog owner, then he can see 123 if( $node->getAttribute( "admin" ) != "1" ) { 124 if( $this->_userInfo->getId() == $this->_blogInfo->getOwnerId()) 125 return true; 126 } 127 // if the user has the 'admin mode' permission, then he can see too 128 if( $this->_userInfo->hasPermissionByName( "edit_blog_admin_mode", 0 )) { 129 return( true ); 130 } 131 132 // get the AND permissions for the node but if there are no permissions assigned, then we can see 133 $nodeAndPerms = $node->getAttribute("andPerms"); 134 if( $nodeAndPerms != "" ) { 135 //print("here? node: ".$node->name."<br/>"); 136 // we can specify more than one permissions separated with a comma 137 $perms = explode( ",", $nodeAndPerms ); 138 // and check if the current user has such permission in this blog 139 foreach( $perms as $perm ) { 140 $perm = trim( $perm ); 141 if( (!$this->_userInfo->hasPermissionByName( $perm, $this->_blogInfo->getId())) && 142 (!$this->_userInfo->hasPermissionByName( $perm, 0 ))) { 143 return false; 144 } 145 } 146 } 147 else { 148 // get the OR permissions for the node but if there are no permissions assigned, then we can see 149 $nodeOrPerms = $node->getAttribute("orPerms"); 150 if( $nodeOrPerms == "" ) 151 return true; 152 153 // we can specify more than one permissions separated with a comma 154 $perms = explode( ",", $nodeOrPerms ); 155 // and check if the current user has such permission in this blog 156 foreach( $perms as $perm ) { 157 $perm = trim( $perm ); 158 if( ($this->_userInfo->hasPermissionByName( $perm, $this->_blogInfo->getId())) || 159 ($this->_userInfo->hasPermissionByName( $perm, 0 ))) { 160 return true; 161 } 162 } 163 164 return false; 165 } 166 167 // if none of the above is true, then the user does not have enough permissions! 168 return true; 169 } 170 171 /** 172 * @private 173 * Used by render() to really render the tree 174 */ 175 function _render( $node, $depth = 0, $activeOpt = "", $menuTop = true ) 176 { 177 $result = ($menuTop === true) ? "<ul class=\"menuTop\">" : "<li><ul class=\"menuTop\">"; 178 $depth--; 179 foreach( $node->children as $child ) { 180 if( $child->name != "" ) { 181 // check whether the user has the right permissions to see this 182 if( $this->userCanSee( $child )) { 183 $url = $child->getAttribute( "url" ); 184 $localeId = $this->getLocaleId( $child ); 185 $cssClass = "Level_".$depth; 186 if( $url != "" ) 187 $result .= "<li class=\"$cssClass\"><a href=\"".$child->getAttribute("url")."\">".$this->_locale->tr($localeId)."</a></li>"; 188 else 189 $result .= "<li class=\"$cssClass\">".$this->_locale->tr($child->name)."</li>"; 190 191 if( $depth > 0 ) { 192 $result .= $this->_render( $child, $depth, $activeOpt, false ); 193 } 194 } 195 } 196 } 197 $result .= ($menuTop === true) ? "</ul>" : "</ul></li>"; 198 199 return $result; 200 } 201 202 /** 203 * @private 204 * Used by renderJS() to really render the Javascriipt menu tree 205 */ 206 function _renderJS( $node, $depth = 0, $start = 0, $activeOpt = "" ) 207 { 208 $result = ""; 209 $depth--; 210 foreach( $node->children as $child ) { 211 if( $child->name != "" ) { 212 // check whether the user has the right permissions to see this 213 if( $this->userCanSee( $child )) { 214 if( $start == 0 ) { 215 $result .= "["; 216 $start = 1; 217 } else { 218 $result .= ",["; 219 } 220 221 $url = $child->getAttribute( "url" ); 222 $localeId = $this->getLocaleId( $child ); 223 224 // escape the string for javascript 225 $menuText = strtr($this->_locale->tr( $localeId ), array('\\'=>'\\\\',"'"=>"\\'",'"'=>'\\"',"\r"=>'\\r',"\n"=>'\\n','</'=>'<\/')); 226 227 if( $url != "" ) 228 $result .= "null, '".$menuText."', '".$child->getAttribute("url")."', '_self', null"; 229 else 230 $result .= "null, '".$menuText."', '#', '_self', null"; 231 232 if( $depth > 0 ) 233 $result .= $this->_renderJS( $child, $depth, $start, $activeOpt ); 234 235 $result .= "]"; 236 } 237 } 238 } 239 240 return $result; 241 } 242 243 /** 244 * gets the locale id, given a node. The locale id can be either the value of the 245 * "localeId" attribute if specified or the name of the tag otherwise 246 * 247 * @param entry A valid menu node 248 * @return the locale identifier 249 */ 250 function getLocaleId( $entry ) 251 { 252 $localeId = $entry->getAttribute("localeId"); 253 if( $localeId == "" ) 254 $localeId = $entry->name; 255 256 return $localeId; 257 } 258 259 /** 260 * simple version of a "breadcrumbs" feature 261 * 262 * @param entryId 263 */ 264 function breadCrumbs( $entryId ) 265 { 266 $nodePath = $this->_menu->entryPath( $entryId ); 267 $path = ""; 268 269 if( $nodePath ) { 270 $parts = explode( "/", $nodePath ); 271 $totalParts = count($parts); 272 if( $totalParts == 1 ) $insertBlogLink = true; 273 else $insertBlogLink = false; 274 275 $count=0; 276 foreach( $parts as $part ) { 277 $path .= $part; 278 $menuEntry = $this->_menu->getEntryAt( $path ); 279 if( $menuEntry->getAttribute("ignoreBreadCrumbs") != "1" ) { 280 $localeId = $this->getLocaleId( $menuEntry ); 281 if( $entryId == $part ) $result .= "<b>"; 282 if( $menuEntry->getAttribute("url") != "" ) 283 $result .= "<a href=\"".$menuEntry->getAttribute("url")."\">".$this->_locale->tr($localeId)."</a>"; 284 else 285 $result .= $this->_locale->tr($part); 286 if( $entryId == $part ) $result .= "</b>"; 287 if( $count == 0 ) $result .= " » <a href=\"?op=blogSelect&blogId=".$this->_blogInfo->getId()."\">".$this->_blogInfo->getBlog()."</a>"; 288 if( $count < $totalParts-1 ) $result .= " » "; 289 } 290 $count++; 291 $path .= "/"; 292 } 293 } 294 295 return $result; 296 } 297 298 /** 299 * @param entryId 300 */ 301 function getOpts( $entryId) 302 { 303 $nodePath = $this->_menu->entryPath( $entryId ); 304 $parts = explode( "/", $nodePath ); 305 array_pop( $parts ); 306 $parentPath = implode( "/", $parts ); 307 308 $parentNode = $this->_menu->getNodeAt( $parentPath ); 309 310 $children = $parentNode->children; 311 312 // the children of my parent are... my brothers :))) 313 return $children; 314 } 315 } 316 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Mon Nov 26 21:04:15 2007 | par Balluche grâce à PHPXref 0.7 |
![]() |