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

Source for file LibertyStructure.php

Documentation is available at LibertyStructure.php

  1. <?php
  2. /**
  3.  * Management of Liberty Content
  4.  *
  5.  * @package  liberty
  6.  * @author   spider <spider@steelsun.com>
  7.  */
  8.  
  9. /**
  10.  * required setup
  11.  */
  12. require_once( LIBERTY_PKG_PATH.'LibertyBase.php' );
  13.  
  14. /**
  15.  * System class for handling the liberty package
  16.  *
  17.  * @package liberty
  18.  */
  19. class LibertyStructure extends LibertyBase {
  20.     var $mStructureId;
  21.  
  22.     function __construct( $pStructureId=NULL, $pContentId=NULL ) {
  23.         // we need to init our database connection early
  24.         parent::__construct();
  25.         $this->mStructureId = $pStructureId;
  26.         $this->mContentId = $pContentId;
  27.     }
  28.  
  29.     function load( $pContentId=NULL ) {
  30.         if( $this->mStructureId || $this->mContentId ) {
  31.             if( $this->mInfo = $this->getNode( $this->mStructureId, $this->mContentId ) ) {
  32.                 global $gLibertySystem;
  33.                 $this->mStructureId = $this->mInfo['structure_id'];
  34.                 $this->mContentId = $this->mInfo['content_id'];
  35.                 $this->mInfo['content_type'] = $gLibertySystem->mContentTypes[$this->mInfo['content_type_guid']];
  36.             }
  37.         }
  38.         return( $this->mInfo && count( $this->mInfo ) );
  39.     }
  40.  
  41.     /**
  42.      * get the details to a given node
  43.      *
  44.      * @param array $pStructureId Structure ID of the node
  45.      * @param array $pContentId Content ID of the node
  46.      * @access public
  47.      * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
  48.      */
  49.     function getNode( $pStructureId=NULL, $pContentId=NULL ) {
  50.         global $gLibertySystem, $gBitSystem;
  51.         static $sStructureNodeCache;
  52.         $contentTypes = $gLibertySystem->mContentTypes;
  53.  
  54.         if( @$this->verifyId( $pStructureId ) ) {
  55.             if (!empty($sStructureNodeCache['structure_id'][$pStructureId])) {
  56.                 return $sStructureNodeCache['structure_id'][$pStructureId];
  57.             }
  58.             $where = ' WHERE ls.`structure_id`=?';
  59.             $bindVars = array( $pStructureId );
  60.         } elseif( @$this->verifyId( $pContentId ) ) {
  61.             if (!empty($sStructureNodeCache['content_id'][$pContentId])) {
  62.                 return $sStructureNodeCache['content_id'][$pContentId];
  63.             }
  64.             $where = ' WHERE ls.`content_id`=?';
  65.             $bindVars = array( $pContentId );
  66.         }
  67.  
  68.         $ret = NULL;
  69.         $query = 'SELECT ls.*, lc.`user_id`, lc.`title`, lc.`content_type_guid`, uu.`login`, uu.`real_name`
  70.                   FROM `'.BIT_DB_PREFIX.'liberty_structures` ls
  71.                   INNER JOIN `'.BIT_DB_PREFIX.'liberty_content` lc ON (ls.`content_id`=lc.`content_id`)
  72.                   LEFT JOIN `'.BIT_DB_PREFIX.'users_users` uu ON ( uu.`user_id` = lc.`user_id` )' . $where;
  73.  
  74.         if( $result = $this->mDb->query( $query, $bindVars ) ) {
  75.             $ret = $result->fetchRow();
  76.         }
  77.  
  78.         if( !empty( $contentTypes[$ret['content_type_guid']] ) ) {
  79.             // quick alias for code readability
  80.             $type = &$contentTypes[$ret['content_type_guid']];
  81.             if( empty( $type['content_object'] ) && !empty( $gBitSystem->mPackages[$type['handler_package']] ) ) {
  82.                 // create *one* object for each object *type* to  call virtual methods.
  83.                 $handlerFile = $gBitSystem->mPackages[$type['handler_package']]['path'].$type['handler_file'];
  84.                 if( file_exists( $handlerFile ) ) {
  85.                     include_once( $handlerFile );
  86.                     if( class_exists( $type['handler_class'] ) ) {
  87.                         $type['content_object'] = new $type['handler_class']();
  88.                     }
  89.                 }
  90.             }
  91.             if( !empty( $type['content_object'] ) && is_object( $type['content_object'] ) ) {
  92.                 $ret['title'] = $type['content_object']->getTitleFromHash( $ret );
  93.             }
  94.         }
  95.  
  96.         $sStructureNodeCache['structure_id'][$ret['structure_id']] = $ret;
  97.         $sStructureNodeCache['content_id'][$ret['content_id']] = $ret;
  98.  
  99.         return $ret;
  100.     }
  101.  
  102.     function hasViewPermission( $pVerifyAccessControl=TRUE ) {
  103.         $ret = FALSE;
  104.         if( !empty( $this->mInfo['content_object'] ) && is_a( $this->mInfo['content_object'], 'LibertyContent' ) ) {
  105.             return( $this->mInfo['content_object']->hasUpdatePermission( $pVerifyAccessControl ) || empty( $this->mInfo['content_object']->mViewContentPerm ) || $this->mInfo['content_object']->hasUserPermission( $this->mInfo['content_object']->mViewContentPerm, $pVerifyAccessControl ));
  106.         } 
  107.         return $ret;
  108.     }
  109.  
  110.     /**
  111.      * Check if a node is a root node
  112.      *
  113.      * @access public
  114.      * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
  115.      */
  116.     function isRootNode() {
  117.         $ret = FALSE;
  118.         if( @$this->verifyId( $this->mInfo['structure_id'] ) ) {
  119.             $ret = $this->mInfo['root_structure_id'] == $this->mInfo['structure_id'];
  120.         }
  121.         return $ret;
  122.     }
  123.  
  124.     /**
  125.      * get the title of the root node
  126.      *
  127.      * @access public
  128.      * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
  129.      */
  130.     function getRootTitle() {
  131.         $ret = NULL;
  132.         if( isset( $this->mInfo['structure_path'][0]['title'] ) ) {
  133.             $ret = $this->mInfo['structure_path'][0]['title'];
  134.         }
  135.         return $ret;
  136.     }
  137.  
  138.     /**
  139.      * if you only have a structure id and you want to figure out the root structure id, use this
  140.      *
  141.      * @param array $pParamHash['structure_id'] is the structure id from which you want to figure out the root structure id
  142.      * @access public
  143.      * @return none. updates $pParamHash['root_structure_id'] by reference
  144.      */
  145.     function getRootStructureId( &$pParamHash ) {
  146.         if( @BitBase::verifyId( $pParamHash['root_structure_id'] ) ) {
  147.             $pParamHash['root_structure_id'] = $pParamHash['root_structure_id'];
  148.         } elseif( @BitBase::verifyId( $this->mInfo['root_structure_id'] ) ) {
  149.             $pParamHash['root_structure_id'] = $this->mInfo['root_structure_id'];
  150.         } elseif( @BitBase::verifyId( $pParamHash['structure_id'] ) ) {
  151.             $pParamHash['root_structure_id'] = $this->mDb->getOne( "SELECT `root_structure_id` FROM `".BIT_DB_PREFIX."liberty_structures` WHERE `structure_id` = ?", array( $pParamHash['structure_id'] ) );
  152.         } else {
  153.             $pParamHash['root_structure_id'] = NULL;
  154.         }
  155.     }
  156.  
  157.     // This is a utility function mainly used for upgrading sites.
  158.     function setTreeRoot( $pRootId, $pTree ) {
  159.         foreach( $pTree as $structRow ) {
  160.             $this->mDb->query( "UPDATE `".BIT_DB_PREFIX."liberty_structures` SET `root_structure_id`=? WHERE `structure_id`=?", array( $pRootId, $structRow["structure_id"] ) );
  161.             if( !empty( $structRow["sub"] ) ) {
  162.                 $this->setTreeRoot( $pRootId, $structRow["sub"] );
  163.             }
  164.         }
  165.     }
  166.  
  167.  
  168.     function isValid() {
  169.         return( $this->verifyId( $this->mStructureId ) );
  170.     }
  171.  
  172.     /**
  173.      * loadNavigation
  174.      *
  175.      * @access public
  176.      * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
  177.      */
  178.     function loadNavigation() {
  179.         if( $this->isValid() ) {
  180.             $this->mInfo["prev"] = null;
  181.             // Get structure info for this page
  182.             if( !$this->isRootNode() && ($prev_structure_id = $this->getPrevStructureNode( $this->mStructureId )) ) {
  183.                 $this->mInfo["prev"]   = $this->getNode($prev_structure_id);
  184.             }
  185.             $next_structure_id = $this->getNextStructureNode( $this->mStructureId );
  186.             $this->mInfo["next"] = null;
  187.             if (isset($next_structure_id)) {
  188.                 $this->mInfo["next"]   = $this->getNode( $next_structure_id) ;
  189.             }
  190.             $this->mInfo["parent"] = $this->getStructureParentInfo( $this->mStructureId );
  191.             $this->mInfo["home"]   = $this->getNode( $this->mStructureId );
  192.         }
  193.         return TRUE;
  194.     }
  195.  
  196.     function loadPath() {
  197.         if( $this->isValid() ) {
  198.             $this->mInfo['structure_path'] = $this->getPath( $this->mStructureId );
  199.         }
  200.         return( !empty( $this->mInfo['structure_path'] ) );
  201.     }
  202.  
  203.     /**
  204.     * This can be used to construct a path from the structure head to the requested page.
  205.     * @returns an array of page_info arrays.
  206.     */
  207.     function getPath( $pStructureId ) {
  208.         $structure_path = array();
  209.         $page_info = $this->getNode($pStructureId);
  210.  
  211.         if ($page_info["parent_id"]) {
  212.             $structure_path = $this->getPath($page_info["parent_id"]);
  213.         }
  214.         $structure_path[] = $page_info;
  215.         return $structure_path;
  216.     }
  217.  
  218.     /**
  219.     * Get full structure from database
  220.     * @param $pStructureId structure for which we want structure
  221.     * @return full structure
  222.     */
  223.     function getStructure( &$pParamHash ) {
  224.         global $gBitSystem, $gLibertySystem;
  225.         // make sure we have the correct id to get the entire structure
  226.         LibertyStructure::getRootStructureId( $pParamHash );
  227.  
  228.         $ret = FALSE;
  229.  
  230.         if( @BitBase::verifyId( $pParamHash['root_structure_id'] ) ) {
  231.             // Get all nodes for this structure
  232.             $query = "SELECT ls.*, lc.`user_id`, lc.`title`, lc.`content_type_guid`, uu.`login`, uu.`real_name`
  233.                 FROM `".BIT_DB_PREFIX."liberty_structures` ls
  234.                 INNER JOIN `".BIT_DB_PREFIX."liberty_content` lc ON ( ls.`content_id` = lc.`content_id` )
  235.                 INNER JOIN `".BIT_DB_PREFIX."users_users` uu ON ( uu.`user_id` = lc.`user_id` )
  236.                 WHERE ls.`root_structure_id` = ? ORDER BY `pos` ASC";
  237.             $result = $this->mDb->query( $query, array( $pParamHash['root_structure_id'] ) );
  238.  
  239.             $subs = array();
  240.             $row_max = $result->numRows();
  241.             $contentTypes = $gLibertySystem->mContentTypes;
  242.             while( $res = $result->fetchRow() ) {
  243.                 $aux = array();
  244.                 $aux = $res;
  245.                 if( !empty( $contentTypes[$res['content_type_guid']] ) ) {
  246.                     // quick alias for code readability
  247.                     $type = &$contentTypes[$res['content_type_guid']];
  248.                     if( empty( $type['content_object'] ) ) {
  249.                         // create *one* object for each object *type* to  call virtual methods.
  250.                         $handlerFile = $gBitSystem->mPackages[$type['handler_package']]['path'].$type['handler_file'];
  251.                         if( file_exists( $handlerFile ) ) {
  252.                             include_once( $handlerFile );
  253.                             if( class_exists( $type['handler_class'] ) ) {
  254.                                 $type['content_object'] = new $type['handler_class']();
  255.                             }
  256.                         }
  257.                     }
  258.                     if( !empty( $pParamHash['thumbnail_size'] ) ) {
  259.                         $aux['content_object'] = new $type['handler_class']( NULL, $aux['content_id'] );
  260.                         if( $aux['content_object']->load() ) {
  261.                             $aux['thumbnail_url'] = $aux['content_object']->getThumbnailUrl( $pParamHash['thumbnail_size'] );
  262.                         }
  263.                     }
  264.                     if( !empty( $type['content_object'] ) && is_object( $type['content_object'] ) ) {
  265.                         $aux['title'] = $type['content_object']->getTitleFromHash( $aux );
  266.                     }
  267.                     $ret[$aux['structure_id']] = $aux;
  268.                 }
  269.             }
  270.         }
  271.         return $ret;
  272.     }
  273.  
  274.     /**
  275.     * Get all structures in $pStructureHash that have a given parent_id
  276.     * @param $pStructureHash full menu as supplied by '$this->getItemList( $pMenuId );'
  277.     * @return array of nodes with a given parent_id
  278.     */
  279.     function getChildNodes( $pStructureHash, $pParentId = 0 ) {
  280.         $ret = array();
  281.         if( !empty( $pStructureHash ) ) {
  282.             foreach( $pStructureHash as $node ) {
  283.                 if( $node['parent_id'] == $pParentId ) {
  284.                     $ret[] = $node;
  285.                 }
  286.             }
  287.         }
  288.         return $ret;
  289.     }
  290.  
  291.     /**
  292.     * Create a usable array from the data in the database from getStructure()
  293.     * @param $pStructureHash raw structure data from database
  294.     * @return nicely formatted and cleaned up structure array
  295.     */
  296.     function createSubTree( $pStructureHash, $pParentId = 0, $pParentPos = '', $pLevel = 0 ) {
  297.         $ret = array();
  298.         // get all child menu Nodes for this structure_id
  299.         $children = LibertyStructure::getChildNodes( $pStructureHash, $pParentId );
  300.         $pos = 1;
  301.         $row_max = count( $children );
  302.  
  303.         // we need to insert the root structure item first
  304.         if (!$pLevel && !empty($pStructureHash)) {
  305.             foreach( $pStructureHash as $node ) {
  306.               if( ( $pParentId == 0 && $node['structure_id'] == $node['root_structure_id'] ) || $node['structure_id'] == $pParentId) {
  307.                     $aux          = $node;
  308.                     $aux["first"] = true;
  309.                     $aux["last"]  = true;
  310.                     $aux["pos"]   = '';
  311.                     $aux["level"] = $pLevel++;
  312.                     $ret[] = $aux;
  313.                 }
  314.             }
  315.         }
  316.  
  317.         foreach( $children as $node ) {
  318.             $aux = $node;
  319.             $aux['level'] = $pLevel;
  320.             $aux['first'] = ( $pos == 1 );
  321.             $aux['last']  = FALSE;
  322.             $aux['has_children'] = FALSE;
  323.             if( strlen( $pParentPos ) == 0 ) {
  324.                 $aux["pos"] = "$pos";
  325.             } else {
  326.                 $aux["pos"] = $pParentPos . '.' . "$pos";
  327.             }
  328.             $ret[] = $aux;
  329.             //Recursively add any children
  330.             $subs = LibertyStructure::createSubTree( $pStructureHash, $node['structure_id'], $aux['pos'], ( $pLevel + 1 ) );
  331.             if( !empty( $subs ) ) {
  332.                 $r = array_pop( $ret );
  333.                 $r['has_children'] = TRUE;
  334.                 array_push( $ret, $r );
  335.                 $ret = array_merge( $ret, $subs );
  336.             }
  337.  
  338.             if( $pos == $row_max ) {
  339.                 $aux['structure_id'] = $node['structure_id'];
  340.                 $aux['first'] = FALSE;
  341.                 $aux['last']  = TRUE;
  342.                 $ret[] = $aux;
  343.             }
  344.             $pos++;
  345.         }
  346.         return $ret;
  347.     }
  348.  
  349.     // get sub tree of $pStructureId
  350.     function getSubTree( $pStructureId, $pRootTree = FALSE, $pListHash=NULL ) {
  351.         global $gLibertySystem, $gBitSystem;
  352.         $ret = array();
  353.         if( @BitBase::verifyId( $pStructureId ) ) {
  354.             $pListHash['structure_id'] = $pStructureId;
  355.             $structureHash = $this->getStructure( $pListHash );
  356.             $ret = $this->createSubTree( $structureHash, ( ( $pRootTree ) ? $pListHash['root_structure_id'] : $pStructureId ) );
  357.         }
  358.         return $ret;
  359.     }
  360.  
  361.     /**
  362.      * getList
  363.      *
  364.      * @param array $pListHash 
  365.      * @access public
  366.      * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
  367.      */
  368.     function getList( &$pListHash ) {
  369.         global $gBitSystem, $gBitUser;
  370.  
  371.         BitBase::prepGetList( $pListHash );
  372.  
  373.         if( !empty( $pListHash['find'] ) ) {
  374.             $findesc = '%' . $pListHash['find'] . '%';
  375.             $mid = " (`parent_id` is null or `parent_id`=0) and (lc.`title` like ?)";
  376.             $bindVars=array($findesc);
  377.         } else {
  378.             $mid = " (`parent_id` is null or `parent_id`=0) ";
  379.             $bindVars=array();
  380.         }
  381.  
  382.         if( @$this->verifyId( $pListHash['user_id'] ) ) {
  383.             $mid .= " AND lc.`user_id` = ? ";
  384.             array_push( $bindVars, $pListHash['user_id'] );
  385.         }
  386.  
  387.         if( empty( $pListHash['sort_mode'] ) ) {
  388.             $pListHash['sort_mode'] = 'last_modified_desc';
  389.         }
  390.  
  391.         if( !empty( $pListHash['content_type_guid'] ) ) {
  392.             $mid .= " AND lc.`content_type_guid`=? ";
  393.             array_push( $bindVars, $pListHash['content_type_guid'] );
  394.         }
  395.         $query = "SELECT ls.`structure_id`, ls.`parent_id`, ls.`content_id`, `page_alias`, `pos`, lc.`title`, `data`, `last_modified`, lc.`modifier_user_id`, `ip`, lc.`user_id` AS `creator_user_id`, uu.`login` AS `creator_user`, uu.`real_name` , uu.`email`
  396.                   FROM `".BIT_DB_PREFIX."liberty_structures` ls INNER JOIN `".BIT_DB_PREFIX."liberty_content` lc ON ( ls.`content_id` = lc.`content_id` ) INNER JOIN `".BIT_DB_PREFIX."users_users` uu ON ( lc.`user_id` = uu.`user_id` )
  397.                   WHERE $mid
  398.                   ORDER BY ".$this->mDb->convertSortmode($pListHash['sort_mode']);
  399.         $query_cant = "SELECT count(*)
  400.                        FROM `".BIT_DB_PREFIX."liberty_structures` ls INNER JOIN `".BIT_DB_PREFIX."liberty_content` lc ON ( ls.`content_id` = lc.`content_id` )
  401.                        WHERE $mid";
  402.         $result = $this->mDb->query($query,$bindVars,$pListHash['max_records'],$pListHash['offset']);
  403.         $cant = $this->mDb->getOne($query_cant,$bindVars);
  404.         $ret = array();
  405.  
  406.         while ($res = $result->fetchRow()) {
  407.             if( $gBitSystem->isPackageActive( 'bithelp' ) && file_exists(BITHELP_PKG_PATH.$res['title'].'/index.html')) {
  408.                 $res['webhelp']='y';
  409.             } else {
  410.                 $res['webhelp']='n';
  411.             }
  412.             $ret[] = $res;
  413.         }
  414.  
  415.         $retval = array();
  416.         $retval["data"] = $ret;
  417.         $retval["cant"] = $cant;
  418.         return $retval;
  419.     }
  420.  
  421.     /**
  422.     * clean up and prepare a complete structure in the form of arrays about to be stored
  423.     * @param $pParamHash is a set of arrays generated by the DynamicTree javascript tree builder
  424.     * @return TRUE on success, FALSE on failure where $this->mErrors will contain the reason why it failed
  425.     */
  426.     function verifyStructure( &$pParamHash ) {
  427.         $storeNodes = array();
  428.         if( !self::getParameter( $pParamHash, 'root_structure_id' ) ) {
  429.             $pParamHash['root_structure_id'] = $this->getField( 'root_structure_id' );
  430.         }
  431.  
  432.         if( !self::verifyId( $pParamHash['root_structure_id'] ) ) {
  433.             $this->mErrors['verify_structure'] = tra( "Unknown root structure." );
  434.         } else {
  435.             if( !empty( $pParamHash['structure_json'] ) ) {
  436. //vd( $pParamHash['structure_json'] );
  437.                 // {{{ closure here to recursively parse json
  438.                 $parseStructureJson = function($treeHash, $pRootId, $pParentId, $pLevel, $pPos ) use ( &$parseStructureJson, &$storeNodes ) {
  439.                     if( is_numeric( key( $treeHash ) ) ) {
  440.                         $pos = 1;
  441.                         foreach( array_keys( $treeHash ) as $i ) {
  442.                             // Array of nodes came in. Process each at the same level
  443.                             $parseStructureJson( $treeHash[$i], $pRootId, $pParentId, $pLevel, $pos++ );
  444.                         }
  445.                     } else {
  446.                         // Base node with data
  447.                         $storeNode = array();
  448.                         $storeNode['root_structure_id'] = $pRootId;
  449.                         $storeNode['parent_id'] = $pParentId;
  450.                         $storeNode['structure_id'] = $treeHash['structure_id'];
  451.                         $storeNode['content_id'] = $treeHash['content_id'];
  452.                         $storeNode['pos'] = $pPos++;
  453.                         //$storeNode['page_alias']
  454.                         $storeNode['structure_level'] = $pLevel;
  455.                         $storeNodes[$treeHash['structure_id']] = $storeNode;
  456.                         if( !empty( $treeHash['children'] ) ) {
  457.                             // current node has children, recurse down in
  458.                             $parseStructureJson( $treeHash['children'], $pRootId, $treeHash['structure_id'], $pLevel + 1, 1 );
  459.                         }
  460.                     }
  461.                 };
  462.                 // }}}
  463.  
  464.                 $parseStructureJson( $pParamHash['structure_json'], $this->mStructureId, $pParamHash['root_structure_id'], 1, 1 );
  465.                 if( !empty( $storeNodes ) ) {
  466.                     $pParamHash['structure_store'] = $storeNodes;
  467.                 }
  468. //vd( $storeNodes );
  469.             }
  470.  
  471.             
  472.             // deprecated flat tree store, code is unused AFAIK.-- spiderr
  473.             if( !empty( $pParamHash['structure'] ) ) {
  474.     //            LibertyStructure::embellishStructureHash( $pParamHash['structure'] );
  475.     //            $structureHash = LibertyStructure::flattenStructureHash( $pParamHash['structure'] );
  476.  
  477.                 // replace the 'tree' in the data array with the root_structure_id
  478.                 foreach( $pParamHash['data'] as $structure_id => $node ) {
  479.                     if( !@BitBase::verifyId( $pParamHash['data'][$structure_id]['parent_id'] ) ) {
  480.                         $pParamHash['data'][$structure_id]['parent_id'] = $pParamHash['root_structure_id'];
  481.                     }
  482.                 }
  483.  
  484.                 foreach( $structureHash as $node ) {
  485.                     if( @BitBase::verifyId( $node['structure_id'] ) ) {
  486.                         $pParamHash['structure_store'][$node['structure_id']] = array_merge( $node, $pParamHash['data'][$node['structure_id']] );
  487.                         $pParamHash['structure_store'][$node['structure_id']]['root_structure_id'] = $pParamHash['root_structure_id'];
  488.                     }
  489.                 }
  490.             }
  491.         }
  492.  
  493.         if( empty( $pParamHash['structure_store'] ) ) {
  494.             $this->mErrors['verify_structure'] = tra( "There are no changes to save." );
  495.         }
  496.  
  497.         // clear up some memory
  498.         if( !empty( $pParamHash['structure_json'] ) ) { unset( $pParamHash['structure_json'] ); }
  499.         if( !empty( $pParamHash['structure'] ) )        { unset( $pParamHash['structure'] ); }
  500.         if( !empty( $pParamHash['data'] ) )             { unset( $pParamHash['data'] ); }
  501.  
  502.         return( count( $this->mErrors ) == 0 );
  503.     }
  504.  
  505.     /**
  506.     * store a complete structure where ever subarray contains a complete node as it should go into the database
  507.     * @param $pParamHash is an array with subarrays, each representing a structure node ready to associativley inserted into the database
  508.     * @return TRUE on success, FALSE on failure where $this->mErrors will contain the reason why it failed
  509.     */
  510.     function storeStructure( $pParamHash ) {
  511.         if( $this->verifyStructure( $pParamHash ) ) {
  512.             // now that the structure is ready to be stored, we remove the old structure first and then insert the new one.
  513.             $this->mDb->StartTrans();
  514.             $query = "DELETE FROM `".BIT_DB_PREFIX."liberty_structures` WHERE `root_structure_id`=? AND `structure_id`!=?";
  515.             $result = $this->mDb->query( $query, array( (int)$pParamHash['root_structure_id'], (int)$pParamHash['root_structure_id'] ) );
  516.             foreach( $pParamHash['structure_store'] as $node ) {
  517.                 $this->mDb->associateInsert( BIT_DB_PREFIX."liberty_structures", $node );
  518.             }
  519.             $this->mDb->CompleteTrans();
  520.         }
  521.         return( count( $this->mErrors ) == 0 );
  522.     }
  523.  
  524.     /**
  525.     * make sure the array only contains one level depth
  526.     * @param $pParamHash contains a nested set of arrays with structure_id and pos values set
  527.     * @return flattened array
  528.     */
  529.     function flattenStructureHash( $pParamHash, $i = -10000 ) {
  530.         $ret = array();
  531.         foreach( $pParamHash as $key => $node ) {
  532.             if( !empty( $node ) && count( $node ) > 2 ) {
  533.                 $ret = array_merge( $ret, LibertyStructure::flattenStructureHash( $node, $i ) );
  534.                 $i++;
  535.             } elseif( count( $node ) == 2 ) {
  536.                 $ret[] = $node;
  537.                 $i++;
  538.             } else {
  539.                 $ret[$i][$key] = $node;
  540.             }
  541.         }
  542.         return $ret;
  543.     }
  544.  
  545.     /**
  546.     * cleans up and reorganises data in nested array where keys are structure_id
  547.     * @param $pParamHash contains a nested set of arrays with structure_id as key
  548.     * @return reorganised array
  549.     */
  550.     function embellishStructureHash( &$pParamHash ) {
  551.         $pos = 1;
  552.         foreach( $pParamHash as $structure_id => $node ) {
  553.             if( !empty( $node ) ) {
  554.                 LibertyStructure::embellishStructureHash( $node );
  555.             }
  556.             $node['pos'] = $pos++;
  557.             $node['structure_id'] = $structure_id;
  558.             $pParamHash[$structure_id] = $node;
  559.         }
  560.     }
  561.  
  562.     /**
  563.     * prepare a structure node for storage in the database
  564.     * @param $pParamHash contains various settings for the node to be stored
  565.     * @return TRUE on success, FALSE on failure where $this->mErrors will contain the reason why it failed
  566.     */
  567.     function verifyNode( &$pParamHash ) {
  568.         if( !@$this->verifyId( $pParamHash['content_id'] ) ) {
  569.             $this->mErrors['content'] = 'Could not store structure. Invalid content id. '.$pParamHash['content_id'];
  570.         } else {
  571.             if( !@$this->verifyId( $pParamHash['parent_id'] ) ) {
  572.                 $pParamHash['parent_id'] = 0;
  573.             }
  574.             if( empty( $pParamHash['alias'] ) ) {
  575.                 $pParamHash['alias'] = '';
  576.             }
  577.             if( isset( $pParamHash['after_ref_id'] ) ) {
  578.                 $pParamHash['max'] = $this->mDb->getOne("select `pos` from `".BIT_DB_PREFIX."liberty_structures` where `structure_id`=?",array((int)$pParamHash['after_ref_id']));
  579.             } else {
  580.                 $pParamHash['max'] = $this->mDb->getOne("select max(`pos`) from `".BIT_DB_PREFIX."liberty_structures` where `parent_id`=?",array((int)$pParamHash['parent_id']));
  581.             }
  582.             if( $pParamHash['max'] > 0 ) {
  583.                 // For example, if max is 5 then we are inserting after position 5 so we'll insert 5 and move all the others
  584.                 $query = "update `".BIT_DB_PREFIX."liberty_structures` set `pos`=`pos`+1 where `pos`>? and `parent_id`=?";
  585.                 $result = $this->mDb->query($query,array((int)$pParamHash['max'], (int)$pParamHash['parent_id']));
  586.             }
  587.             $pParamHash['max']++;
  588.             if( $pParamHash['structure_id'] = $this->mDb->getOne( "SELECT `structure_id` FROM `".BIT_DB_PREFIX."liberty_structures` WHERE `root_structure_id`=? and `content_id`=?", array($pParamHash['root_structure_id'], $pParamHash['content_id'] ) ) ) {
  589.                 $this->mErrors[] = tra( 'Content already exists in structure.' )." ($pParamHash[structure_id])";
  590.             }
  591.         }
  592.         return( count( $this->mErrors ) == 0 );
  593.     }
  594.  
  595.     /**  Create a structure entry with the given name
  596.     * @param parent_id The parent entry to add this to. If NULL, create new structure.
  597.     * @param after_ref_id The entry to add this one after. If NULL, put it in position 0.
  598.     * @param name The wiki page to reference
  599.     * @param alias An alias for the wiki page name.
  600.     * @return the new entries structure_id or null if not created.
  601.     */
  602.     function storeNode( &$pParamHash ) {
  603.         global $gBitSystem;
  604.         $ret = null;
  605.         // If the page doesn't exist then create a new wiki page!
  606.         $now = $gBitSystem->getUTCTime();
  607. //        $created = $this->create_page($name, 0, '', $now, tra('created from structure'), 'system', '0.0.0.0', '');
  608.         // if were not trying to add a duplicate structure head
  609.         if ( $this->verifyNode( $pParamHash ) ) {
  610.             $this->mDb->StartTrans();
  611.  
  612.             //Create a new structure entry
  613.             $pParamHash['structure_id'] = $this->mDb->GenID( 'liberty_structures_id_seq' );
  614.             if( !@$this->verifyId( $pParamHash['root_structure_id'] ) ) {
  615.                 $pParamHash['root_structure_id'] = $pParamHash['structure_id'];
  616.             }
  617.             $query = "INSERT INTO `".BIT_DB_PREFIX."liberty_structures`( `structure_id`, `parent_id`,`content_id`, `root_structure_id`, `page_alias`, `pos` ) values(?,?,?,?,?,?)";
  618.             $result = $this->mDb->query( $query, array( $pParamHash['structure_id'], $pParamHash['parent_id'], (int)$pParamHash['content_id'], (int)$pParamHash['root_structure_id'], $pParamHash['alias'], $pParamHash['max'] ) );
  619.             $this->mDb->CompleteTrans();
  620.             $ret = $pParamHash['structure_id'];
  621.         } else {
  622.             //vd( $this->mErrors );
  623.         }
  624.         return $ret;
  625.     }
  626.  
  627.     /**
  628.      * moveNodeWest
  629.      *
  630.      * @access public
  631.      * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
  632.      */
  633.     function moveNodeWest() {
  634.         if( $this->isValid() ) {
  635.             //If there is a parent and the parent isnt the structure root node.
  636.             $this->mDb->StartTrans();
  637.             if( @$this->verifyId( $this->mInfo["parent_id"] ) ) {
  638.                 $parentNode = $this->getNode( $this->mInfo["parent_id"] );
  639.                 if( @$this->verifyId( $parentNode['parent_id'] ) ) {
  640.                     //Make a space for the node after its parent
  641.                     $query = "update `".BIT_DB_PREFIX."liberty_structures` set `pos`=`pos`+1 where `pos`>? and `parent_id`=?";
  642.                     $this->mDb->query( $query, array( $parentNode['pos'], $parentNode['parent_id'] ) );
  643.                     //Move the node up one level
  644.                     $query = "update `".BIT_DB_PREFIX."liberty_structures` set `parent_id`=?, `pos`=(? + 1) where `structure_id`=?";
  645.                     $this->mDb->query($query, array( $parentNode['parent_id'], $parentNode['pos'], $this->mStructureId ) );
  646.                 }
  647.             }
  648.             $this->mDb->CompleteTrans();
  649.         }
  650.     }
  651.  
  652.     /**
  653.      * moveNodeEast
  654.      *
  655.      * @access public
  656.      * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
  657.      */
  658.     function moveNodeEast() {
  659.         if( $this->isValid() ) {
  660.             $this->mDb->StartTrans();
  661.             $query = "select `structure_id`, `pos` from `".BIT_DB_PREFIX."liberty_structures` where `pos`<? and `parent_id`=? order by `pos` desc";
  662.             $result = $this->mDb->query($query,array($this->mInfo["pos"], (int)$this->mInfo["parent_id"]));
  663.             if ($previous = $result->fetchRow()) {
  664.                 //Get last child nodes for previous sibling
  665.                 $query = "select `pos` from `".BIT_DB_PREFIX."liberty_structures` where `parent_id`=? order by `pos` desc";
  666.                 $result = $this->mDb->query($query,array((int)$previous["structure_id"]));
  667.                 if ($res = $result->fetchRow()) {
  668.                     $pos = $res["pos"];
  669.                 } else{
  670.                     $pos = 0;
  671.                 }
  672.                 $query = "update `".BIT_DB_PREFIX."liberty_structures` set `parent_id`=?, `pos`=(? + 1) where `structure_id`=?";
  673.                 $this->mDb->query( $query, array((int)$previous["structure_id"], (int)$pos, (int)$this->mStructureId) );
  674.                 //Move nodes up below that had previous parent and pos
  675.                 $query = "update `".BIT_DB_PREFIX."liberty_structures` set `pos`=`pos`-1 where `pos`>? and `parent_id`=?";
  676.                 $this->mDb->query( $query, array( $this->mInfo['pos'], $this->mInfo['parent_id'] ) );
  677.             }
  678.             $this->mDb->CompleteTrans();
  679.         }
  680.     }
  681.  
  682.     /**
  683.      * moveNodeSouth
  684.      *
  685.      * @access public
  686.      * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
  687.      */
  688.     function moveNodeSouth() {
  689.         if( $this->isValid() ) {
  690.             $this->mDb->StartTrans();
  691.             $query = "select `structure_id`, `pos` from `".BIT_DB_PREFIX."liberty_structures` where `pos`>? and `parent_id`=? order by `pos` asc";
  692.             $result = $this->mDb->query($query,array((int)$this->mInfo["pos"], (int)$this->mInfo["parent_id"]));
  693.             $res = $result->fetchRow();
  694.             if ($res) {
  695.                 //Swap position values
  696.                 $query = "update `".BIT_DB_PREFIX."liberty_structures` set `pos`=? where `structure_id`=?";
  697.                 $this->mDb->query($query,array((int)$this->mInfo["pos"], (int)$res["structure_id"]) );
  698.                 $this->mDb->query($query,array((int)$res["pos"], (int)$this->mInfo["structure_id"]) );
  699.             }
  700.             $this->mDb->CompleteTrans();
  701.         }
  702.     }
  703.  
  704.     /**
  705.      * moveNodeNorth
  706.      *
  707.      * @access public
  708.      * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
  709.      */
  710.     function moveNodeNorth() {
  711.         if( $this->isValid() ) {
  712.             $this->mDb->StartTrans();
  713.             $query = "select `structure_id`, `pos` from `".BIT_DB_PREFIX."liberty_structures` where `pos`<? and `parent_id`=? order by `pos` desc";
  714.             $result = $this->mDb->query($query,array((int)$this->mInfo["pos"], (int)$this->mInfo["parent_id"]));
  715.             $res = $result->fetchRow();
  716.             if ($res) {
  717.                 //Swap position values
  718.                 $query = "update `".BIT_DB_PREFIX."liberty_structures` set `pos`=? where `structure_id`=?";
  719.                 $this->mDb->query($query,array((int)$res["pos"], (int)$this->mInfo["structure_id"]) );
  720.                 $this->mDb->query($query,array((int)$this->mInfo["pos"], (int)$res["structure_id"]) );
  721.             }
  722.             $this->mDb->CompleteTrans();
  723.         }
  724.     }
  725.  
  726.  
  727.  
  728.  
  729.  
  730.  
  731.  
  732.  
  733.  
  734.     // ============== OLD struct_lib STUFF
  735.  
  736.  
  737.  
  738.     function removeStructureNode( $structure_id, $delete=FALSE ) {
  739.         // Now recursively remove
  740.         if( @$this->verifyId( $structure_id ) ) {
  741.             $query = "SELECT *
  742.                       FROM `".BIT_DB_PREFIX."liberty_structures`
  743.                       WHERE `parent_id`=?";
  744.             $result = $this->mDb->query( $query, array( (int)$structure_id ) );
  745.             // Iterate down through the child nodes
  746.             while( $res = $result->fetchRow() ) {
  747.                 $this->removeStructureNode( $res["structure_id"], $delete );
  748.             }
  749.  
  750.             // Only delete a page if other structures arent referencing it
  751.             if( $delete ) {
  752.                 $page_info = $this->getNode( $structure_id );
  753.                 $query = "SELECT COUNT(*) FROM `".BIT_DB_PREFIX."liberty_structures` WHERE `content_id`=?";
  754.                 $count = $this->mDb->getOne( $query, array( (int)$page_info["page_id"] ) );
  755.                 if( $count = 1 ) {
  756.                     $this->remove_all_versions( $page_info["page_id"] );
  757.                 }
  758.             }
  759.  
  760.             // If we are removing the root node, remove the entry in liberty_content as well
  761.             $query = "SELECT `content_id`
  762.                       FROM `".BIT_DB_PREFIX."liberty_structures`
  763.                       WHERE `structure_id`=? AND `structure_id`=`root_structure_id`";
  764.             $content_id = $this->mDb->getOne( $query, array( (int)$structure_id ) );
  765.  
  766.             // Delete the liberty_content stuff
  767.             $lc = new LibertyContent($content_id);
  768.             $lc->expunge();
  769.  
  770.             // Remove the structure node
  771.             $query = "DELETE FROM `".BIT_DB_PREFIX."liberty_structures` WHERE `structure_id`=?";
  772.             $result = $this->mDb->query( $query, array( (int)$structure_id) );
  773.             return true;
  774.         }
  775.     }
  776.  
  777.     /**
  778.      * Returns an array of info about the parent
  779.      *
  780.      * @param array $structure_id 
  781.      * @access public
  782.      * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
  783.      */
  784.     function getStructureParentInfo($structure_id) {
  785.         $parent_id = $this->mDb->getOne( "SELECT `parent_id` FROM `".BIT_DB_PREFIX."liberty_structures` WHERE `structure_id`=?", array( (int)$structure_id ) );
  786.  
  787.         if( !@BitBase::verifyId( $parent_id ) ) {
  788.             return null;
  789.         }
  790.  
  791.         return( $this->getNode( $parent_id ) );
  792.     }
  793.  
  794.     /**
  795.      * getContentIds
  796.      *
  797.      * @param array $pStructureId 
  798.      * @param array $pToc 
  799.      * @param float $pLevel 
  800.      * @access public
  801.      * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
  802.      */
  803.     function getContentIds( $pStructureId, &$pToc, $pLevel=0 ) {
  804.         $ret = array();
  805.  
  806.         $query = "SELECT * from `".BIT_DB_PREFIX."liberty_structures` where `parent_id`=? ORDER BY pos, page_alias, content_id";
  807.         $result = $this->mDb->query( $query, array( (int)$pStructureId ) );
  808.         while ( $row = $result->fetchRow() ) {
  809.             array_push( $pToc, $row['content_id'] );
  810.             $this->getContentIds( $row['structure_id'], $pToc, ++$pLevel );
  811.         }
  812.     }
  813.  
  814.     /**
  815.      * getContentArray
  816.      *
  817.      * @param array $pStructureId 
  818.      * @param array $pToc 
  819.      * @param float $pLevel 
  820.      * @access public
  821.      * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
  822.      */
  823.     function getContentArray( $pStructureId, &$pToc, $pLevel=0 ) {
  824.         $query = "SELECT * from `".BIT_DB_PREFIX."liberty_structures` where `structure_id`=?";
  825.         $result = $this->mDb->query( $query, array( (int)$pStructureId ) );
  826.         while ( $row = $result->fetchRow() ) {
  827.             array_push( $pToc, $row['content_id'] );
  828.             $this->getContentIds( $pStructureId, $pToc, $pLevel );
  829.         }
  830.     }
  831.  
  832.     /**
  833.      * exportHtml
  834.      *
  835.      * @access public
  836.      * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
  837.      */
  838.     function exportHtml() {
  839.         $ret = array();
  840.         $toc = array();
  841.         $this->getContentArray( $this->mStructureId, $toc );
  842.         if( count( $toc ) ) {
  843.             foreach( $toc as $conId ) {
  844.                 if( $viewContent = LibertyBase::getLibertyObject( $conId ) ) {
  845.                     $ret[] = array(
  846.                         'type'       => $viewContent->mContentTypeGuid,
  847.                         'landscape'  => FALSE,
  848.                         'url'        => $viewContent->getDisplayUrl(),
  849.                         'content_id' => $viewContent->mContentId,
  850.                     );
  851.                 }
  852.             }
  853.         }
  854.         return $ret;
  855.     }
  856.  
  857.     function isInStructure( $pContentId ) {
  858.         $ret = FALSE;
  859.         if( $this->isValid() ) {
  860.             $ret = $this->mDb->getOne( "SELECT structure_id FROM `".BIT_DB_PREFIX."liberty_structures` WHERE `root_structure_id`=? AND `content_id`=?", array( $this->mStructureId, $pContentId ) );
  861.         }
  862.         return $ret;
  863.     }
  864.  
  865.     function loadStructure() {
  866.         if( $this->isValid() ) {
  867.             if( empty( $this->mTree ) ) {
  868.                 $this->mTree = $this->buildSubtreeToc();
  869.             }
  870.         }
  871.         return( !empty( $this->mTree ) );
  872.     }
  873.  
  874.     /**
  875.      * buildSubtreeToc
  876.      *
  877.      * @param array $id 
  878.      * @param array $slide 
  879.      * @param string $order 
  880.      * @param string $tocPrefix can be used to Prefix a subtree as it would start from a given number (e.g. 2.1.3)
  881.      * @access public
  882.      * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
  883.      */
  884.     function buildTreeToc( $id, $order='asc', $tocPrefix='', $pPrefixDepth=1, $pDepth=1 ) {
  885.         if( $ret[0] = $this->getNode( $id ) ) {
  886.             $ret[0]['sub'] = $this->buildSubtreeToc( $id, $order, $tocPrefix, $pPrefixDepth, $pDepth );
  887.         }
  888.         return $ret;
  889.     }
  890.  
  891.  
  892.     /**
  893.      * buildSubtreeToc
  894.      *
  895.      * @param array $id 
  896.      * @param array $slide 
  897.      * @param string $order 
  898.      * @param string $tocPrefix can be used to Prefix a subtree as it would start from a given number (e.g. 2.1.3)
  899.      * @access public
  900.      * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
  901.      */
  902.     function buildSubtreeToc( $id, $order='asc', $tocPrefix='', $pPrefixDepth=1, $pDepth=1 ) {
  903.         global $gLibertySystem, $gBitSystem;
  904.         $back = array();
  905.         $cant = $this->mDb->getOne("select count(*) from `".BIT_DB_PREFIX."liberty_structures` where `parent_id`=?",array((int)$id));
  906.         if ($cant) {
  907.             $query = "SELECT `structure_id`, `root_structure_id`, `parent_id`, `page_alias`, `pos`, `structure_level`, lc.`user_id`, lc.`title`, lc.`content_type_guid`, uu.`login`, uu.`real_name`, lc.`content_id`, lc.`last_modified`, lct.*
  908.                       FROM `".BIT_DB_PREFIX."liberty_structures` ls
  909.                         INNER JOIN `".BIT_DB_PREFIX."liberty_content` lc ON ( lc.`content_id`=ls.`content_id` )
  910.                         INNER JOIN `".BIT_DB_PREFIX."liberty_content_types` lct ON ( lc.`content_type_guid`=lct.`content_type_guid` )
  911.                         LEFT JOIN `".BIT_DB_PREFIX."users_users` uu ON ( uu.`user_id` = lc.`user_id` )
  912.                       WHERE `parent_id`=?
  913.                       ORDER BY ".$this->mDb->convertSortmode("pos_".$order);
  914.             $result = $this->mDb->query($query,array((int)$id));
  915.             $prefix = 1;
  916.             $contentTypes = $gLibertySystem->mContentTypes;
  917.             while ($res = $result->fetchRow()) {
  918.                 $res['prefix']='';
  919.                 if( $pDepth >= $pPrefixDepth ) {
  920.                     $res['prefix']=($tocPrefix=='')?'':"$tocPrefix.";
  921.                     $res['prefix'].=$prefix;
  922.                 }
  923.                 $prefix++;
  924.                 if( !empty( $contentTypes[$res['content_type_guid']] ) ) {
  925.                     // quick alias for code readability
  926.                     $type = &$contentTypes[$res['content_type_guid']];
  927.                     if( empty( $type['content_object'] ) && !empty( $gBitSystem->mPackages[$type['handler_package']] ) ) {
  928.                         // create *one* object for each object *type* to  call virtual methods.
  929.                         $handlerFile = $gBitSystem->mPackages[$type['handler_package']]['path'].$type['handler_file'];
  930.                         if( file_exists( $handlerFile ) ) {
  931.                             include_once( $handlerFile );
  932.                             if( class_exists( $type['handler_class'] ) ) {
  933.                                 $type['content_object'] = new $type['handler_class']();
  934.                             }
  935.                         }
  936.                     }
  937.                     if( !empty( $type['content_object'] ) && is_object( $type['content_object'] ) ) {
  938.                         $res['title'] = $type['content_object']->getTitleFromHash( $res );
  939.                     }
  940.                     if ($res['structure_id'] != $id) {
  941.                         $sub = $this->buildSubtreeToc( $res['structure_id'],$order,$res['prefix'], $pPrefixDepth, ($pDepth + 1) );
  942.                         if (is_array($sub)) {
  943.                             $res['sub'] = $sub;
  944.                         }
  945.                     }
  946.                 }
  947.                 $pkgPath = strtoupper( $res['handler_package'] ).'_PKG_PATH';
  948.                 if( defined( $pkgPath ) ) {
  949.                     $classFile = constant( strtoupper( $res['handler_package'] ).'_PKG_PATH' ).$res['handler_file'];
  950.                     if( file_exists( $classFile ) ) {
  951.                         require_once( $classFile );
  952.                         if( class_exists( $res['handler_class'] ) ) {
  953.                             $res['display_url'] = $res['handler_class']::getDisplayUrlFromHash( $res );
  954.                         }
  955.                     }
  956.                 }
  957.                 $back[] = $res;
  958.             }
  959.         } else {
  960.             return false;
  961.         }
  962.         return $back;
  963.     }
  964.  
  965.     /**
  966.      * getToc
  967.      *
  968.      * @param array $pStructureId 
  969.      * @param string $order 
  970.      * @param array $showdesc 
  971.      * @param array $numbering 
  972.      * @param string $numberPrefix 
  973.      * @access public
  974.      * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
  975.      */
  976.     function getToc($pStructureId=NULL,$order='asc',$showdesc=false,$pNumberDepth=true,$numberPrefix='',$pCss='') {
  977.         if( !@$this->verifyId( $pStructureId ) ) {
  978.             $pStructureId = $this->mStructureId;
  979.         }
  980.         $structureTree = $this->buildSubtreeToc( $pStructureId, $order, $numberPrefix, $pNumberDepth, $pCss );
  981.         return '<div class="aciTree" id="structure-'.$this->mStructureId.'">'.$this->fetchToc( $structureTree,$showdesc,$pNumberDepth ).'</div>';
  982.     }
  983.  
  984.     /**
  985.      * fetchToc
  986.      *
  987.      * @param array $structureTree 
  988.      * @param array $showdesc 
  989.      * @param array $numbering 
  990.      * @access public
  991.      * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
  992.      */
  993.     function fetchToc($structureTree,$showdesc,$numbering,$pCss='') {
  994.         global $gBitSmarty;
  995.         $ret='';
  996.         if ($structureTree != '') {
  997.             $gBitSmarty->verifyCompileDir();
  998.             $gBitSmarty->assign_by_ref( 'structureId', $this->mStructureId );
  999.             $ret.=$gBitSmarty->fetch( "bitpackage:liberty/structure_toc_startul.tpl");
  1000.             foreach($structureTree as $leaf) {
  1001.                 //echo "<br />";print_r($leaf);echo "<br />";
  1002.                 $gBitSmarty->assign_by_ref('structure_tree',$leaf);
  1003.                 $gBitSmarty->assign('showdesc',$showdesc);
  1004.                 $gBitSmarty->assign('numbering',$numbering);
  1005.                 $ret .= $gBitSmarty->fetch( "bitpackage:liberty/structure_toc_leaf.tpl");
  1006.                 if(isset($leaf["sub"]) && is_array($leaf["sub"])) {
  1007.                     // recurse down in - li tags are for w3c standard
  1008.                     $ret .= $this->fetchToc($leaf["sub"],$showdesc,$numbering);
  1009.                 }
  1010.                 $ret .= '</li>';
  1011.             }
  1012.             $ret.=$gBitSmarty->fetch( "bitpackage:liberty/structure_toc_endul.tpl");
  1013.         }
  1014.         return $ret;
  1015.     }
  1016.  
  1017.     /**
  1018.      * getNextStructureNode
  1019.      *
  1020.      * @param array $structure_id 
  1021.      * @param array $deep 
  1022.      * @access public
  1023.      * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
  1024.      */
  1025.     function getNextStructureNode($structure_id, $deep = true) {
  1026.         // If we have children then get the first child
  1027.         if ($deep) {
  1028.             $query  = "SELECT `structure_id`
  1029.                        FROM `".BIT_DB_PREFIX."liberty_structures` ls
  1030.                        WHERE `parent_id`=?
  1031.                        ORDER BY ".$this->mDb->convertSortmode("pos_asc");
  1032.             $result1 = $this->mDb->query($query,array((int)$structure_id));
  1033.  
  1034.             if ($result1->numRows()) {
  1035.                 $res = $result1->fetchRow();
  1036.                 return $res["structure_id"];
  1037.             }
  1038.         }
  1039.  
  1040.         // Try to get the next page with the same parent as this
  1041.         $page_info = $this->getNode($structure_id);
  1042.         $parent_id = $page_info["parent_id"];
  1043.         $page_pos = $page_info["pos"];
  1044.  
  1045.         if (!$parent_id)
  1046.             return null;
  1047.  
  1048.         $query  = "SELECT `structure_id`
  1049.                    FROM `".BIT_DB_PREFIX."liberty_structures` ls
  1050.                    WHERE `parent_id`=? and `pos`>?
  1051.                    ORDER BY ".$this->mDb->convertSortmode("pos_asc");
  1052.         $result2 = $this->mDb->query($query,array((int)$parent_id, (int)$page_pos));
  1053.  
  1054.         if ($result2->numRows()) {
  1055.             $res = $result2->fetchRow();
  1056.             return $res["structure_id"];
  1057.         }
  1058.         else {
  1059.             return $this->getNextStructureNode($parent_id, false);
  1060.         }
  1061.     }
  1062.  
  1063.     /**
  1064.      * getPrevStructureNode
  1065.      *
  1066.      * @param array $structure_id 
  1067.      * @param array $deep 
  1068.      * @access public
  1069.      * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
  1070.      */
  1071.     function getPrevStructureNode($structure_id, $deep = false) {
  1072.         //Drill down to last child for this tree node
  1073.         if ($deep) {
  1074.             $query  = "select `structure_id` ";
  1075.             $query .= "from `".BIT_DB_PREFIX."liberty_structures` ls ";
  1076.             $query .= "where `parent_id`=? ";
  1077.             $query .= "order by ".$this->mDb->convertSortmode("pos_desc");
  1078.             $result = $this->mDb->query($query,array($structure_id));
  1079.  
  1080.             if ($result->numRows()) {
  1081.                 //There are more children
  1082.                 $res = $result->fetchRow();
  1083.                 $structure_id = $this->getPrevStructureNode($res["structure_id"], true);
  1084.             }
  1085.             return $structure_id;
  1086.         }
  1087.         // Try to get the previous page with the same parent as this
  1088.         $page_info = $this->getNode($structure_id);
  1089.         $parent_id = $page_info["parent_id"];
  1090.         $pos       = $page_info["pos"];
  1091.  
  1092.         //At the top of the tree
  1093.         if (!isset($parent_id))
  1094.             return null;
  1095.  
  1096.         $query  = "select `structure_id` ";
  1097.         $query .= "from `".BIT_DB_PREFIX."liberty_structures` ls ";
  1098.         $query .= "where `parent_id`=? and `pos`<? ";
  1099.         $query .= "order by ".$this->mDb->convertSortmode("pos_desc");
  1100.         $result =  $this->mDb->query($query,array((int)$parent_id, (int)$pos));
  1101.  
  1102.         if ($result->numRows()) {
  1103.             //There is a previous sibling
  1104.             $res = $result->fetchRow();
  1105.             $structure_id = $this->getPrevStructureNode($res["structure_id"], true);
  1106.         }
  1107.         else {
  1108.             //No previous siblings, just the parent
  1109.             $structure_id = $parent_id;
  1110.         }
  1111.         return $structure_id;
  1112.     }
  1113.  
  1114.     /**
  1115.      * Return an array of subpages
  1116.      *
  1117.      * @param array $pParentId 
  1118.      * @access public
  1119.      * @return array of child structure pages
  1120.      */
  1121.     function getStructureNodes( $pParentId ) {
  1122.         $ret = array();
  1123.         $query =  "SELECT `pos`, `structure_id`, `parent_id`, ls.`content_id`, lc.`title`, `page_alias`
  1124.             FROM `".BIT_DB_PREFIX."liberty_structures` ls, `".BIT_DB_PREFIX."liberty_content` lc
  1125.             WHERE ls.`content_id` = lc.`content_id` AND `parent_id`=? ";
  1126.         $query .= "order by ".$this->mDb->convertSortmode("pos_asc");
  1127.         $result = $this->mDb->query($query,array((int)$pParentId));
  1128.         while ($res = $result->fetchRow()) {
  1129.             //$ret[] = $this->populate_page_info($res);
  1130.             $ret[] = $res;
  1131.         }
  1132.         return $ret;
  1133.     }
  1134. }
  1135. ?>

Documentation generated on Wed, 29 Jul 2015 13:56:58 +0000 by phpDocumentor 1.5.0-lsces