Source for file Nexus.php
Documentation is available at Nexus.php
* @author xing <xing@synapse.plus.com>
* @version $Revision: 1.21 $
require_once( NEXUS_PKG_PATH. 'NexusSystem.php' );
* Primary key for the menu
* Initialisation of this class
function Nexus( $pMenuId= NULL ) {
// if the cache folder doesn't exist yet, create it
* Get menu information from database
* @param $pMenuId menu id of the menu we want information from
function getMenu( $pMenuId= NULL ) {
$query .= ' WHERE nm.`menu_id`=?';
$bindVars = array( $pMenuId );
if( $result = $this->mDb->query( $query, array( $bindVars ) ) ) {
$ret['cache']['file'] = 'mod_'. preg_replace( "/ /", "_", $ret['title'] ). '_'. $pMenuId. '.tpl';
$ret['cache']['path'] = TEMP_PKG_PATH. NEXUS_PKG_NAME. "/modules/". $ret['cache']['file'];
$ret['cache']['module'] = "bitpackage:temp/nexus/". $ret['cache']['file'];
* Get menu information from database
* @param $pMenuId menu id of the menu we want information from
function getMenuList( $pFindString= NULL, $pSortMode= NULL, $pOffset= NULL, $pMaxRows= NULL ) {
$mid .= " WHERE UPPER(nm.`title`) LIKE ? ";
$mid .= " ORDER BY ". $this->mDb->convertSortmode( $pSortMode ). " ";
$query = 'SELECT nm.`menu_id` FROM `'. BIT_DB_PREFIX. 'nexus_menus` nm'. $mid;
$result = $this->mDb->query( $query, $bindVars, $pOffset, $pMaxRows );
$result = $this->mDb->query( $query, $bindVars );
$menuIds = $result->getRows();
foreach( $menuIds as $menuId ) {
$tmpMenu = new Nexus( $menuId['menu_id'] );
$menus[] = $tmpMenu->mInfo;
* Create usable menu tree
* @param $pMenuHash full menu as supplied by '$this->getItemList( $pMenuId );'
* @param $pStripped if set to true, removes all permissions, user isn't part of
* @return menu with all menu items sorted with first and last items of each 'level' marked. items that contain siblings are marked with 'head' = TRUE;
function createMenuTree( $pMenuHash, $pStripped= FALSE, $parent_id= 0, $pForceBuild= FALSE ) {
if( $pForceBuild || $this->isValid() && empty( $this->mInfo['tree'] )) {
if( $pStripped && $parent_id == 0 ) {
// get all child menu items for this item_id
$row_max = count( $children );
foreach( $children as $item ) {
$aux['first'] = ( $pos == 1 );
//Recursively add any children
$subs = $this->createMenuTree( $pMenuHash, $pStripped, $item['item_id'], TRUE );
// mark items that have children
$row_last = count( $ret );
$ret[$row_last - 1]['head'] = TRUE;
$tmpItem = $this->getItemList( NULL, $item['parent_id'] );
$aux = $tmpItem[$item['parent_id']];
$aux['item_id'] = $item['item_id'];
$ret = $this->mInfo['tree'];
* Strip out all items a user doesn't have permission to view
* @param $pMenuHash full menu as supplied by '$this->getItemList( $pMenuId );'
* @return menu containing only items user is allowed to view
foreach( $pMenuHash as $item ) {
if( !empty( $item['perm'] ) ) {
if( $gBitUser->hasPermission( $item['perm'] ) ) {
* Get all items in $pMenuHash that have a given parent_id
* @param $pMenuHash full menu as supplied by '$this->getItemList( $pMenuId );'
* @return array of items with a given parent_id
foreach( $pMenuHash as $item ) {
if( $item['parent_id'] == $parent_id ) {
* Validate that a menu is being loaded and present
* @return TRUE if all is ok
* Check if all required items are present for menu creation / insertion
* @return number of errors encountered
if( empty( $pParamHash['title'] ) ) {
$this->mErrors['verify_title'] = tra( 'Could not store menu because no title was given.' );
if( empty( $pParamHash['description'] ) ) {
$pParamHash['description'] = NULL;
// set the default plugin_guid to suckerfish menus
if( empty( $pParamHash['plugin_guid'] ) ) {
$type_name = 'type_' . $pParamHash['plugin_guid'];
if ( empty( $pParamHash[$type_name] )) {
$pParamHash['menu_type'] = 'nor';
$pParamHash['menu_type'] = $pParamHash[$type_name];
if( empty( $pParamHash['editable'][0] ) || !is_numeric( $pParamHash['editable'][0] ) ) {
$pParamHash['editable'][0] = 0;
$pParamHash['editable'] = $pParamHash['editable'][0];
* @param menu_id if set, will update given menu - if not set, we create a new entry in the db
* @param title title of menu
* @param description description of menu
* @param menu_type type of menu
* @param editable if menu is editable by other users - takes 0 or 1
* @return new menu menu_id or FALSE if not created
$query = "INSERT INTO `". BIT_DB_PREFIX. "nexus_menus`( `title`,`description`,`menu_type`,`plugin_guid`,`editable` ) VALUES(?,?,?,?,?)";
$result = $this->mDb->query( $query, array( $pParamHash['title'], $pParamHash['description'], $pParamHash['menu_type'], $pParamHash['plugin_guid'], $pParamHash['editable'] ) );
$query = "SELECT MAX(`menu_id`) FROM `". BIT_DB_PREFIX. "nexus_menus`";
$ret = $this->mDb->getOne( $query, array() );
$query = "UPDATE `". BIT_DB_PREFIX. "nexus_menus` SET `title`=?,`description`=?,`menu_type`=?,`plugin_guid`=?,`editable`=? WHERE `". BIT_DB_PREFIX. "nexus_menus`.`menu_id`=?";
$result = $this->mDb->query( $query, array( $pParamHash['title'], $pParamHash['description'], $pParamHash['menu_type'], $pParamHash['plugin_guid'], $pParamHash['editable'], $pParamHash['menu_id'] ) );
$ret = $pParamHash['menu_id'];
* Delete menu and associated menu items from db
* @return number of errors encountered
// first off, remove the menu from the layout
global $gBitThemes, $gBitSystem;
$gBitThemes->unassignModule( $menu['cache']['module'], ROOT_USER_ID );
$query = "DELETE FROM `". BIT_DB_PREFIX. "nexus_menu_items` WHERE `menu_id`=?";
$this->mDb->query( $query, array( $pMenuId ) );
$query = "DELETE FROM `". BIT_DB_PREFIX. "nexus_menus` WHERE `menu_id`=?";
$this->mDb->query( $query, array( $pMenuId ) );
// rewrite the entire cache, just to make sure everything is in order
* Get menu items from the database
* @param $pItemId ID of menu item to get. if set, we only get this item.
* @param $pMenuId ID of menu to get
* @return all menu items with a given menu ID
$query = 'SELECT nmi.* FROM `'. BIT_DB_PREFIX. 'nexus_menu_items` nmi';
$query .= ' WHERE nmi.`item_id`=?';
$bindVars = array( $pItemId );
$query .= ' WHERE nmi.`menu_id`=?';
$bindVars = array( $pMenuId );
$query .= ' ORDER BY nmi.`pos`';
$result = $this->mDb->query( $query,array( $bindVars ) );
$item['display_url'] = $this->printUrl( $item );
$ret[$item['item_id']] = $item;
// this version of the loop inserts the submenu at the point of choice
foreach( $ret as $item ) {
if( $item['rsrc_type'] == 'menu_id' ) {
if( $i['parent_id'] == 0 ) {
$tmp[$i['item_id']]['parent_id'] = $item['item_id'];
// pass all items on to ret
$ret[$i['item_id']] = $tmp[$i['item_id']];
* Create the correct url for a given item
* @param $pItemHash complete item hash
global $gLibertySystem, $gBitSystem;
$contentTypes = $gLibertySystem->mContentTypes;
if( isset ( $pItemHash['rsrc'] ) && isset ( $pItemHash['rsrc_type'] )) {
switch( $pItemHash['rsrc_type'] ) {
$ret .= $pItemHash['rsrc'];
// create *one* object for each object *type* to call virtual methods.
$row = $this->mDb->getRow( "SELECT `title`,`content_id`,`content_type_guid` FROM `". BIT_DB_PREFIX. "liberty_content` WHERE `content_id`=?", array( $pItemHash['rsrc'] ));
$type = &$contentTypes[$row['content_type_guid']];
if( empty( $type['content_object'] )) {
include_once( $gBitSystem->mPackages[$type['handler_package']]['path']. $type['handler_file'] );
$type['content_object'] = new $type['handler_class']();
$ret = $type['content_object']->getDisplayUrl( NULL, $row );
$ret .= BIT_ROOT_URL. 'index.php?structure_id='. $pItemHash['rsrc'];
* Verify rsrc handed to us and specify what type it is
* @return fixed rsrc and corresponding rsrc_type
if( preg_match( "/^(http:\/\/)/i", $pParamHash['rsrc'] ) ) {
$pParamHash['rsrc_type'] = 'external';
// if the resource type is numeric but we don't know what type it is, assume that it's a content_id
if( !isset ( $pParamHash['rsrc_type'] ) ) {
$pParamHash['rsrc_type'] = 'content_id';
} elseif( preg_match( $bit_root_pattern, $pParamHash['rsrc'] ) ) {
// if BIT_ROOT_URL can be found at the beginning of the string, assume it's an internal link
// in most cases this will only be a '/' but hopefully that's enough
$pParamHash['rsrc_type'] = 'internal';
// if we haven't caught this resource yet, we just prepend it with BIT_ROOT_URL and hope for the best
$pParamHash['rsrc_type'] = 'internal';
* Verify if a given menu item contains all required information and prepare menu_items table for item insertion.
* @return number of errors encountered
if( empty( $pParamHash['hint'] ) ) { $pParamHash['hint'] = NULL; }
if( empty( $pParamHash['perm'] ) ) { $pParamHash['perm'] = NULL; }
if( empty( $pParamHash['title'] ) ) {
$this->mErrors['verify_item_title'] = tra( 'Could not store menu item. No item title was given.' );
$this->mErrors['verify_menu_id'] = tra( 'Could not store menu item. Invalid menu id. Menu id ' ). ': '. $pParamHash['menu_id'];
$this->mDb->StartTrans();
$pParamHash['parent_id'] = 0;
// if no parent_id is not known, but we have an after_ref_id, we use that to work out the parent_id
$pParamHash['parent_id'] = $this->mDb->getOne("SELECT `parent_id` FROM `". BIT_DB_PREFIX. "nexus_menu_items` WHERE `item_id`=?", array( (int) $pParamHash['after_ref_id'] ) );
|