nexus
[ class tree: nexus ] [ index: nexus ] [ all elements ]

Source for file Nexus.php

Documentation is available at Nexus.php

  1. <?php
  2. /**
  3. * Nexus base class
  4. *
  5. @abstract
  6. @author   xing <xing@synapse.plus.com>
  7. @version  $Revision: 1.21 $
  8. @package  nexus
  9. */
  10.  
  11. /**
  12. * required setup
  13. */
  14. require_onceNEXUS_PKG_PATH.'NexusSystem.php' );
  15.  
  16. /**
  17. @package  nexus
  18. @subpackage  Nexus
  19. */
  20. class Nexus extends NexusSystem {
  21.     /**
  22.     * Primary key for the menu
  23.     */
  24.     var $mMenuId;
  25.  
  26.     /**
  27.     * Initialisation of this class
  28.     */
  29.     function Nexus$pMenuId=NULL {
  30.         NexusSystem::NexusSystem();
  31.         $this->mMenuId = $pMenuId;
  32.         // if the cache folder doesn't exist yet, create it
  33.         if!is_dirTEMP_PKG_PATH.NEXUS_PKG_NAME.'/modules' ) ) {
  34.             mkdir_pTEMP_PKG_PATH.NEXUS_PKG_NAME.'/modules' );
  35.         }
  36.     }
  37.  
  38.     /**
  39.     * Load the menu
  40.     */
  41.     function load({
  42.         if@BitBase::verifyId$this->mMenuId ) ) {
  43.             $this->mInfo = $this->getMenu$this->mMenuId );
  44.             $this->mInfo['items'$this->getItemList$this->mMenuId );
  45.             $this->mInfo['tree'$this->createMenuTree$this->mInfo['items');
  46.         }
  47.         returncount$this->mInfo ) );
  48.     }
  49.  
  50.     /**
  51.     * Get menu information from database
  52.     * @param $pMenuId menu id of the menu we want information from
  53.     */
  54.     function getMenu$pMenuId=NULL {
  55.         $ret array();
  56.         if!@BitBase::verifyId$pMenuId && $this->isValid() ) {
  57.             $pMenuId $this->mMenuId;
  58.         }
  59.  
  60.         $bindVars array();
  61.         $query 'SELECT nm.* FROM `'.BIT_DB_PREFIX.'nexus_menus` nm';
  62.         ifis_numeric$pMenuId ) ) {
  63.             $query .= ' WHERE nm.`menu_id`=?';
  64.             $bindVars array$pMenuId );
  65.         }
  66.         if$result $this->mDb->query$queryarray$bindVars ) ) ) {
  67.             $ret $result->fields;
  68.             $ret['cache']['file''mod_'.preg_replace"/ /""_"$ret['title').'_'.$pMenuId.'.tpl';
  69.             $ret['cache']['path'TEMP_PKG_PATH.NEXUS_PKG_NAME."/modules/".$ret['cache']['file'];
  70.             $ret['cache']['module'"bitpackage:temp/nexus/".$ret['cache']['file'];
  71.         }
  72.         return $ret;
  73.     }
  74.  
  75.     /**
  76.     * Get menu information from database
  77.     * @param $pMenuId menu id of the menu we want information from
  78.     */
  79.     function getMenuList$pFindString=NULL$pSortMode=NULL$pOffset=NULL$pMaxRows=NULL {
  80.         $bindVars array();
  81.         $mid '';
  82.         if$pFindString {
  83.             $mid .= " WHERE UPPER(nm.`title`) LIKE ? ";
  84.             $bindVars['%'.strtoupper$pFindString ).'%';
  85.         }
  86.         if$pSortMode {
  87.             $mid .= " ORDER BY ".$this->mDb->convertSortmode$pSortMode )." ";
  88.         }
  89.  
  90.         $query 'SELECT nm.`menu_id` FROM `'.BIT_DB_PREFIX.'nexus_menus` nm'.$mid;
  91.         if$pMaxRows && is_numeric$pMaxRows ) ) {
  92.             $result $this->mDb->query$query$bindVars$pOffset$pMaxRows );
  93.         else {
  94.             $result $this->mDb->query$query$bindVars );
  95.         }
  96.         $menuIds $result->getRows();
  97.         $menus array();
  98.         foreach$menuIds as $menuId {
  99.             $tmpMenu new Nexus$menuId['menu_id');
  100.             $tmpMenu->load();
  101.             $menus[$tmpMenu->mInfo;
  102.         }
  103.  
  104.         return $menus;
  105.     }
  106.  
  107.     /**
  108.     * Create usable menu tree
  109.     * @param $pMenuHash full menu as supplied by '$this->getItemList( $pMenuId );'
  110.     * @param $pStripped if set to true, removes all permissions, user isn't part of
  111.     * @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;
  112.     */
  113.     function createMenuTree$pMenuHash$pStripped=FALSE$parent_id=0$pForceBuild=FALSE {
  114.         $ret array();
  115.         if$pForceBuild || $this->isValid(&& empty$this->mInfo['tree')) {
  116.             if$pStripped && $parent_id == {
  117.                 $pMenuHash $this->checkUserPermission$pMenuHash );
  118.             }
  119.             // get all child menu items for this item_id
  120.             $children $this->getChildItems$pMenuHash$parent_id );
  121.             $pos 1;
  122.             $row_max count$children );
  123.             foreach$children as $item {
  124.                 $aux $item;
  125.                 $aux['first']       $pos == );
  126.                 $aux['last']        FALSE;
  127.                 $aux['head']        FALSE;
  128.                 $ret[$aux;
  129.                 //Recursively add any children
  130.                 $subs $this->createMenuTree$pMenuHash$pStripped$item['item_id']TRUE );
  131.                 if!empty$subs ) ) {
  132.                     // mark items that have children
  133.                     $row_last count$ret );
  134.                     $ret[$row_last 1]['head'TRUE;
  135.                     $ret array_merge$ret$subs );
  136.                 }
  137.                 if$pos == $row_max {
  138.                     if@BitBase::verifyId$item['parent_id') ) {
  139.                         $tmpItem $this->getItemListNULL$item['parent_id');
  140.                         $aux $tmpItem[$item['parent_id']];
  141.                     else {
  142.                         $aux['item_id'$item['item_id'];
  143.                     }
  144.                     $aux['first'FALSE;
  145.                     $aux['last']  TRUE;
  146.                     $ret[$aux;
  147.                 }
  148.                 $pos++;
  149.             }
  150.         else {
  151.             $ret $this->mInfo['tree'];
  152.         }
  153.         return $ret;
  154.     }
  155.  
  156.     /**
  157.     * Strip out all items a user doesn't have permission to view
  158.     * @param $pMenuHash full menu as supplied by '$this->getItemList( $pMenuId );'
  159.     * @return menu containing only items user is allowed to view
  160.     */
  161.     function checkUserPermission$pMenuHash {
  162.         global $gBitUser;
  163.         $ret array();
  164.         foreach$pMenuHash as $item {
  165.             if!empty$item['perm') ) {
  166.                 if$gBitUser->hasPermission$item['perm') ) {
  167.                     $ret[$item;
  168.                 }
  169.             else {
  170.                 $ret[$item;
  171.             }
  172.         }
  173.         return $ret;
  174.     }
  175.  
  176.     /**
  177.     * Get all items in $pMenuHash that have a given parent_id
  178.     * @param $pMenuHash full menu as supplied by '$this->getItemList( $pMenuId );'
  179.     * @return array of items with a given parent_id
  180.     */
  181.     function getChildItems$pMenuHash$parent_id={
  182.         $ret array();
  183.         foreach$pMenuHash as $item {
  184.             if$item['parent_id'== $parent_id {
  185.                 $ret[$item;
  186.             }
  187.         }
  188.         return $ret;
  189.     }
  190.  
  191.     /**
  192.     * Validate that a menu is being loaded and present
  193.     * @return TRUE if all is ok
  194.     */
  195.     function isValid({
  196.         returnBitBase::verifyId$this->mMenuId ) );
  197.     }
  198.  
  199.     /**
  200.     * Check if all required items are present for menu creation / insertion
  201.     * @return number of errors encountered
  202.     */
  203.     function verifyMenu&$pParamHash {
  204.         ifempty$pParamHash['title') ) {
  205.             $this->mErrors['verify_title'tra'Could not store menu because no title was given.' );
  206.         }
  207.         ifempty$pParamHash['description') ) {
  208.             $pParamHash['description'NULL;
  209.         }
  210.         // set the default plugin_guid to suckerfish menus
  211.         ifempty$pParamHash['plugin_guid') ) {
  212.             $pParamHash['plugin_guid'NEXUS_PLUGIN_GUID_SUCKERFISH;
  213.         }
  214.         $type_name 'type_' $pParamHash['plugin_guid'];
  215.         if empty$pParamHash[$type_name)) {
  216.             $pParamHash['menu_type''nor';
  217.         }
  218.         else {
  219.             $pParamHash['menu_type'$pParamHash[$type_name];
  220.         }
  221.         ifempty$pParamHash['editable'][0|| !is_numeric$pParamHash['editable'][0) ) {
  222.             $pParamHash['editable'][00;
  223.         }
  224.         $pParamHash['editable'$pParamHash['editable'][0];
  225.         returncount$this->mErrors == );
  226.     }
  227.  
  228.     /**
  229.     * Store menu in db
  230.     * @param menu_id if set, will update given menu - if not set, we create a new entry in the db
  231.     * @param title title of menu
  232.     * @param description description of menu
  233.     * @param menu_type type of menu
  234.     * @param editable if menu is editable by other users - takes 0 or 1
  235.     * @return new menu menu_id or FALSE if not created
  236.     */
  237.     function storeMenu&$pParamHash {
  238.         $ret FALSE;
  239.         if$this->verifyMenu$pParamHash ) ) {
  240.             if!@BitBase::verifyId$pParamHash['menu_id') ) {
  241.                 $query "INSERT INTO `".BIT_DB_PREFIX."nexus_menus`( `title`,`description`,`menu_type`,`plugin_guid`,`editable` ) VALUES(?,?,?,?,?)";
  242.                 $result $this->mDb->query$queryarray$pParamHash['title']$pParamHash['description']$pParamHash['menu_type']$pParamHash['plugin_guid']$pParamHash['editable') );
  243.                 $query "SELECT MAX(`menu_id`) FROM `".BIT_DB_PREFIX."nexus_menus`";
  244.                 $ret $this->mDb->getOne$queryarray() );
  245.             else {
  246.                 $query "UPDATE `".BIT_DB_PREFIX."nexus_menus` SET `title`=?,`description`=?,`menu_type`=?,`plugin_guid`=?,`editable`=? WHERE `".BIT_DB_PREFIX."nexus_menus`.`menu_id`=?";
  247.                 $result $this->mDb->query$queryarray$pParamHash['title']$pParamHash['description']$pParamHash['menu_type']$pParamHash['plugin_guid']$pParamHash['editable']$pParamHash['menu_id') );
  248.                 $ret $pParamHash['menu_id'];
  249.             }
  250.             $this->writeMenuCache$ret );
  251.         else {
  252.             vd$this->mErrors );
  253.         }
  254.         return $ret;
  255.     }
  256.  
  257.     /**
  258.     * Delete menu and associated menu items from db
  259.     * @return number of errors encountered
  260.     */
  261.     function expungeMenu$pMenuId {
  262.         // first off, remove the menu from the layout
  263.         global $gBitThemes$gBitSystem;
  264.         $menu $this->getMenu$this->mMenuId );
  265.         $gBitThemes->unassignModule$menu['cache']['module']ROOT_USER_ID );
  266.  
  267.         // delete menu items
  268.         $query "DELETE FROM `".BIT_DB_PREFIX."nexus_menu_items` WHERE `menu_id`=?";
  269.         $this->mDb->query$queryarray$pMenuId ) );
  270.  
  271.         // delete menu
  272.         $query "DELETE FROM `".BIT_DB_PREFIX."nexus_menus` WHERE `menu_id`=?";
  273.         $this->mDb->query$queryarray$pMenuId ) );
  274.  
  275.         // rewrite the entire cache, just to make sure everything is in order
  276.         $this->rewriteMenuCache();
  277.  
  278.         returncount$this->mErrors == );
  279.     }
  280.  
  281.     /**
  282.     * Get menu items from the database
  283.     * @param $pItemId ID of menu item to get. if set, we only get this item.
  284.     * @param $pMenuId ID of menu to get
  285.     * @return all menu items with a given menu ID
  286.     */
  287.     function getItemList$pMenuId=NULL$pItemId=NULL {
  288.         $bindVars array();
  289.         $ret array();
  290.         $query 'SELECT nmi.* FROM `'.BIT_DB_PREFIX.'nexus_menu_items` nmi';
  291.         ifis_numeric$pItemId ) ) {
  292.             $query .= ' WHERE nmi.`item_id`=?';
  293.             $bindVars array$pItemId );
  294.         elseifis_numeric$pMenuId ) ) {
  295.             $query .= ' WHERE nmi.`menu_id`=?';
  296.             $bindVars array$pMenuId );
  297.         }
  298.         $query .= ' ORDER BY nmi.`pos`';
  299.         $result $this->mDb->query$query,array$bindVars ) );
  300.         while!$result->EOF {
  301.             $item $result->fields;
  302.             $item['display_url'$this->printUrl$item );
  303.             $ret[$item['item_id']] $item;
  304.             $result->MoveNext();
  305.         }
  306.         // this version of the loop inserts the submenu at the point of choice
  307.         foreach$ret as $item {
  308.             if$item['rsrc_type'== 'menu_id' {
  309.                 $tmp $this->getItemList$item['rsrc');
  310.                 foreach$tmp as $i {
  311.                     if$i['parent_id'== {
  312.                         $tmp[$i['item_id']]['parent_id'$item['item_id'];
  313.                     }
  314.                     // pass all items on to ret
  315.                     $ret[$i['item_id']] $tmp[$i['item_id']];
  316.                 }
  317.             }
  318.         }
  319.         return $ret;
  320.     }
  321.  
  322.     /**
  323.     * Create the correct url for a given item
  324.     * @param $pItemHash complete item hash
  325.     * @return url 
  326.     */
  327.     function printUrl$pItemHash {
  328.         global $gLibertySystem$gBitSystem;
  329.         $contentTypes $gLibertySystem->mContentTypes;
  330.         $ret NULL;
  331.  
  332.         ifisset$pItemHash['rsrc'&& isset$pItemHash['rsrc_type')) {
  333.             switch$pItemHash['rsrc_type'{
  334.                 case 'external':
  335.                 case 'internal':
  336.                     $ret .= $pItemHash['rsrc'];
  337.                     break;
  338.                 case 'content_id':
  339.                     // create *one* object for each object *type* to  call virtual methods.
  340.                     $row $this->mDb->getRow"SELECT `title`,`content_id`,`content_type_guid` FROM `".BIT_DB_PREFIX."liberty_content` WHERE `content_id`=?"array$pItemHash['rsrc'));
  341.                     $type &$contentTypes[$row['content_type_guid']];
  342.  
  343.                     ifempty$type['content_object')) {
  344.                         include_once$gBitSystem->mPackages[$type['handler_package']]['path'].$type['handler_file');
  345.                         $type['content_object'new $type['handler_class']();
  346.                     }
  347.  
  348.                     $ret $type['content_object']->getDisplayUrlNULL$row );
  349.                     break;
  350.                 case 'structure_id':
  351.                     $ret .= BIT_ROOT_URL.'index.php?structure_id='.$pItemHash['rsrc'];
  352.                     break;
  353.             }
  354.         }
  355.         return $ret;
  356.     }
  357.  
  358.     /**
  359.     * Verify rsrc handed to us and specify what type it is
  360.     * @return fixed rsrc and corresponding rsrc_type
  361.     */
  362.     function verifyRsrc&$pParamHash {
  363.         $bit_root_pattern "/^".preg_replace"/\//""\/"BIT_ROOT_URL )."/i";
  364.         ifpreg_match"/^(http:\/\/)/i"$pParamHash['rsrc') ) {
  365.             $pParamHash['rsrc_type''external';
  366.         elseifis_numeric$pParamHash['rsrc') ) {
  367.             // if the resource type is numeric but we don't know what type it is, assume that it's a content_id
  368.             if!isset$pParamHash['rsrc_type') ) {
  369.                 $pParamHash['rsrc_type''content_id';
  370.             }
  371.         elseifpreg_match$bit_root_pattern$pParamHash['rsrc') ) {
  372.             // if BIT_ROOT_URL can be found at the beginning of the string, assume it's an internal link
  373.             // in most cases this will only be a '/' but hopefully that's enough
  374.             $pParamHash['rsrc_type''internal';
  375.         else {
  376.             // if we haven't caught this resource yet, we just prepend it with BIT_ROOT_URL and hope for the best
  377.             $pParamHash['rsrc'BIT_ROOT_URL.$pParamHash['rsrc'];
  378.             $pParamHash['rsrc_type''internal';
  379.         }
  380.     }
  381.  
  382.     /**
  383.     * Verify if a given menu item contains all required information and prepare menu_items table for item insertion.
  384.     * @return number of errors encountered
  385.     */
  386.     function verifyItem&$pParamHash {
  387.         ifempty$pParamHash['hint') )        $pParamHash['hint'NULL}
  388.         ifempty$pParamHash['perm') )        $pParamHash['perm'NULL}
  389.         ifempty$pParamHash['title') ) {
  390.             $this->mErrors['verify_item_title'tra'Could not store menu item. No item title was given.' );
  391.         }
  392.         if!@BitBase::verifyId$pParamHash['menu_id') ) {
  393.             $this->mErrors['verify_menu_id'tra'Could not store menu item. Invalid menu id. Menu id ' ).': '.$pParamHash['menu_id'];
  394.         else {
  395.             $this->mDb->StartTrans();
  396.             if!@BitBase::verifyId$pParamHash['parent_id') ) {
  397.                 $pParamHash['parent_id'0;
  398.                 // if no parent_id is not known, but we have an after_ref_id, we use that to work out the parent_id
  399.                 if@BitBase::verifyId$pParamHash['after_ref_id') ) {
  400.                     $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') );
  401.                 }
  402.             }
  403.             $pParamHash['max'0;
  404.