diff options
Diffstat (limited to 'BitBoard.php')
| -rw-r--r-- | BitBoard.php | 424 |
1 files changed, 341 insertions, 83 deletions
diff --git a/BitBoard.php b/BitBoard.php index f10bdef..3b3936e 100644 --- a/BitBoard.php +++ b/BitBoard.php @@ -1,72 +1,364 @@ <?php /** -* $Header: /cvsroot/bitweaver/_bit_boards/BitBoard.php,v 1.1 2006/06/28 15:45:26 spiderr Exp $ -* $Id: BitBoard.php,v 1.1 2006/06/28 15:45:26 spiderr Exp $ +* $Header: /cvsroot/bitweaver/_bit_boards/BitBoard.php,v 1.2 2006/07/12 16:57:33 hash9 Exp $ +* $Id: BitBoard.php,v 1.2 2006/07/12 16:57:33 hash9 Exp $ */ /** -* BitBoardBoard class to illustrate best practices when creating a new bitweaver package that +* BitBoard class to illustrate best practices when creating a new bitweaver package that * builds on core bitweaver functionality, such as the Liberty CMS engine * * @date created 2004/8/15 * @author spider <spider@steelsun.com> -* @version $Revision: 1.1 $ $Date: 2006/06/28 15:45:26 $ $Author: spiderr $ -* @class BitBoardBoard +* @version $Revision: 1.2 $ $Date: 2006/07/12 16:57:33 $ $Author: hash9 $ +* @class BitBoard */ require_once( LIBERTY_PKG_PATH.'LibertyAttachable.php' ); +/** +* This is used to uniquely identify the object +*/ +define( 'BITFORUM_CONTENT_TYPE_GUID', 'bitforum' ); + class BitBoard extends LibertyAttachable { + /** + * Primary key for our mythical BitBoard class object & table + * @public + */ + var $mBitBoardId; /** * During initialisation, be sure to call our base constructors **/ - function BitBoard() { + function BitBoard( $pBitBoardId=NULL, $pContentId=NULL ) { LibertyAttachable::LibertyAttachable(); + $this->mBitBoardId = $pBitBoardId; + $this->mContentId = $pContentId; + $this->mContentTypeGuid = BITFORUM_CONTENT_TYPE_GUID; + $this->registerContentType( BITFORUM_CONTENT_TYPE_GUID, array( + 'content_type_guid' => BITFORUM_CONTENT_TYPE_GUID, + 'content_description' => 'Forum Board', + 'handler_class' => 'BitBoard', + 'handler_package' => 'bitboards', + 'handler_file' => 'BitBoard.php', + 'maintainer_url' => 'http://www.bitweaver.org' + ) ); } - function loadContent($contentId) { - global $gBitDb; - global $gBitUser; - //var_dump($GLOBALS); - if( LibertyContent::verifyId( $contentId ) ) { + /** + * Load the data from the database + * @param pParamHash be sure to pass by reference in case we need to make modifcations to the hash + **/ + function load() { + if( $this->verifyId( $this->mBitBoardId ) || $this->verifyId( $this->mContentId ) ) { // LibertyContent::load()assumes you have joined already, and will not execute any sql! // This is a significant performance optimization + $lookupColumn = $this->verifyId( $this->mBitBoardId ) ? 'board_id' : 'content_id'; $bindVars = array(); $selectSql = $joinSql = $whereSql = ''; - array_push( $bindVars, $contentId ); - $gBitUser->getServicesSql( 'content_load_sql_function', $selectSql, $joinSql, $whereSql, $bindVars ); + array_push( $bindVars, $lookupId = @BitBase::verifyId( $this->mBitBoardId ) ? $this->mBitBoardId : $this->mContentId ); + $this->getServicesSql( 'content_load_sql_function', $selectSql, $joinSql, $whereSql, $bindVars ); - $query = "SELECT lc.*, uue.`login` AS modifier_user, uue.`real_name` AS modifier_real_name, uuc.`login` AS creator_user, uuc.`real_name` AS creator_real_name $selectSql - FROM `".BIT_DB_PREFIX."liberty_content` lc $joinSql - LEFT JOIN `".BIT_DB_PREFIX."users_users` uue ON( uue.`user_id` = lc.`modifier_user_id` ) - LEFT JOIN `".BIT_DB_PREFIX."users_users` uuc ON( uuc.`user_id` = lc.`user_id` ) - WHERE lc.`content_id`=? $whereSql"; - $result = $gBitDb->query( $query, $bindVars ); + $query = "SELECT s.*, lc.*, " . + "uue.`login` AS modifier_user, uue.`real_name` AS modifier_real_name, " . + "uuc.`login` AS creator_user, uuc.`real_name` AS creator_real_name " . + "$selectSql " . + "FROM `".BIT_DB_PREFIX."forum_board` s " . + "INNER JOIN `".BIT_DB_PREFIX."liberty_content` lc ON( lc.`content_id` = s.`content_id` ) $joinSql" . + "LEFT JOIN `".BIT_DB_PREFIX."users_users` uue ON( uue.`user_id` = lc.`modifier_user_id` )" . + "LEFT JOIN `".BIT_DB_PREFIX."users_users` uuc ON( uuc.`user_id` = lc.`user_id` )" . + "WHERE s.`$lookupColumn`=? $whereSql"; + $result = $this->mDb->query( $query, $bindVars ); - $ret = array(); if( $result && $result->numRows() ) { - $ret = $result->fields; + $this->mInfo = $result->fields; + $this->mContentId = $result->fields['content_id']; + $this->mBitBoardId = $result->fields['board_id']; + + $this->mInfo['creator'] =( isset( $result->fields['creator_real_name'] )? $result->fields['creator_real_name'] : $result->fields['creator_user'] ); + $this->mInfo['editor'] =( isset( $result->fields['modifier_real_name'] )? $result->fields['modifier_real_name'] : $result->fields['modifier_user'] ); + $this->mInfo['display_url'] = $this->getDisplayUrl(); + $this->mInfo['parsed_data'] = $this->parseData(); + + LibertyAttachable::load(); + } + } + return( count( $this->mInfo ) ); + } + + /** + * Any method named Store inherently implies data will be written to the database + * @param pParamHash be sure to pass by reference in case we need to make modifcations to the hash + * This is the ONLY method that should be called in order to store( create or update )an bitforum! + * It is very smart and will figure out what to do for you. It should be considered a black box. + * + * @param array pParams hash of values that will be used to store the page + * + * @return bool TRUE on success, FALSE if store could not occur. If FALSE, $this->mErrors will have reason why + * + * @access public + **/ + function store( &$pParamHash ) { + if( $this->verify( $pParamHash )&& LibertyAttachable::store( $pParamHash ) ) { + $table = BIT_DB_PREFIX."forum_board"; + $this->mDb->StartTrans(); + if( $this->mBitBoardId ) { + $locId = array( "board_id" => $pParamHash['board_id'] ); + $result = $this->mDb->associateUpdate( $table, $pParamHash['bitforum_store'], $locId ); + } else { + $pParamHash['bitforum_store']['content_id'] = $pParamHash['content_id']; + if( @$this->verifyId( $pParamHash['board_id'] ) ) { + // if pParamHash['board_id'] is set, some is requesting a particular board_id. Use with caution! + $pParamHash['bitforum_store']['board_id'] = $pParamHash['board_id']; + } else { + $pParamHash['bitforum_store']['board_id'] = $this->mDb->GenID( 'forum_board_id_seq' ); + } + $this->mBitBoardId = $pParamHash['bitforum_store']['board_id']; + + $result = $this->mDb->associateInsert( $table, $pParamHash['bitforum_store'] ); + $result = $this->mDb->associateInsert( BIT_DB_PREFIX."forum_map",array('board_content_id'=>$pParamHash['bitforum_store']['content_id'],'topic_content_id'=>$pParamHash['bitforum_store']['content_id'])); + } + + + $this->mDb->CompleteTrans(); + $this->load(); + } + return( count( $this->mErrors )== 0 ); + } + + /** + * Make sure the data is safe to store + * @param pParamHash be sure to pass by reference in case we need to make modifcations to the hash + * This function is responsible for data integrity and validation before any operations are performed with the $pParamHash + * NOTE: This is a PRIVATE METHOD!!!! do not call outside this class, under penalty of death! + * + * @param array pParams reference to hash of values that will be used to store the page, they will be modified where necessary + * + * @return bool TRUE on success, FALSE if verify failed. If FALSE, $this->mErrors will have reason why + * + * @access private + **/ + function verify( &$pParamHash ) { + // make sure we're all loaded up of we have a mBitBoardId + if( $this->verifyId( $this->mBitBoardId ) && empty( $this->mInfo ) ) { + $this->load(); + } + + if( @$this->verifyId( $this->mInfo['content_id'] ) ) { + $pParamHash['content_id'] = $this->mInfo['content_id']; + } + + // It is possible a derived class set this to something different + if( @$this->verifyId( $pParamHash['content_type_guid'] ) ) { + $pParamHash['content_type_guid'] = $this->mContentTypeGuid; + } + + if( @$this->verifyId( $pParamHash['content_id'] ) ) { + $pParamHash['bitforum_store']['content_id'] = $pParamHash['content_id']; + } + + // check some lengths, if too long, then truncate + if( $this->isValid() && !empty( $this->mInfo['description'] ) && empty( $pParamHash['description'] ) ) { + // someone has deleted the description, we need to null it out + $pParamHash['bitforum_store']['description'] = ''; + } else if( empty( $pParamHash['description'] ) ) { + unset( $pParamHash['description'] ); + } else { + $pParamHash['bitforum_store']['description'] = substr( $pParamHash['description'], 0, 200 ); + } + + if( !empty( $pParamHash['data'] ) ) { + $pParamHash['edit'] = $pParamHash['data']; + } + + // check for name issues, first truncate length if too long + if( !empty( $pParamHash['title'] ) ) { + if( empty( $this->mBitBoardId ) ) { + if( empty( $pParamHash['title'] ) ) { + $this->mErrors['title'] = 'You must enter a name for this page.'; + } else { + $pParamHash['content_store']['title'] = substr( $pParamHash['title'], 0, 160 ); + } + } else { + $pParamHash['content_store']['title'] =( isset( $pParamHash['title'] ) )? substr( $pParamHash['title'], 0, 160 ): ''; + } + } else if( empty( $pParamHash['title'] ) ) { + // no name specified + $this->mErrors['title'] = 'You must specify a name'; + } + + return( count( $this->mErrors )== 0 ); + } + + /** + * This function removes a bitforum entry + **/ + function expunge() { + $ret = FALSE; + if( $this->isValid() ) { + $this->mDb->StartTrans(); + $query = "DELETE FROM `".BIT_DB_PREFIX."forum_map` WHERE `board_content_id` = ?"; + $result = $this->mDb->query( $query, array( $this->mContentId ) ); + $query = "DELETE FROM `".BIT_DB_PREFIX."forum_board` WHERE `content_id` = ?"; + $result = $this->mDb->query( $query, array( $this->mContentId ) ); + if( LibertyAttachable::expunge() ) { + $ret = TRUE; + $this->mDb->CompleteTrans(); + } else { + $this->mDb->RollbackTrans(); + } + } + return $ret; + } + + /** + * Make sure bitforum is loaded and valid + **/ + function isValid() { + return( $this->verifyId( $this->mBitBoardId ) ); + } - $ret['creator'] =( isset( $result->fields['creator_real_name'] )? $result->fields['creator_real_name'] : $result->fields['creator_user'] ); - $ret['editor'] =( isset( $result->fields['modifier_real_name'] )? $result->fields['modifier_real_name'] : $result->fields['modifier_user'] ); - $ret['display_url'] = BIT_ROOT_URL."index.php?content_id=$contentId"; + function getAllMap() { + $b = new BitBoard(); + $listHash = array(); + $l = $b->getList($listHash); + $ret = array(); + foreach ($l as $k => $boardd) { + $board = new BitBoard($boardd['board_id']); + $board->mInfo=$boardd; + $ret['map'][$k]=$boardd; + $ret['map'][$k]['map'] = $board->getMap(); + $ret['map'][$k]['integrity'] = $board->verifyIntegrity(); + } + $ret['umap'] = $b->getUnMapped(); + return $ret; + } + + function getUnMapped() { + global $gBitSystem; + $ret = NULL; + $sql = "SELECT + lc.`title`, + lc.`content_id`, + lct.`content_description`, + ( SELECT count(*) + FROM `".BIT_DB_PREFIX."liberty_comments` lcom + WHERE lcom.`root_id`=lcom.`parent_id` AND lcom.`root_id`=lc.`content_id` + ) AS thread_count + FROM `".BIT_DB_PREFIX."liberty_content` lc + INNER JOIN `".BIT_DB_PREFIX."liberty_content_types` lct ON (lc.`content_type_guid`=lct.`content_type_guid`) + WHERE lc.`content_id` NOT IN ( + SELECT lc.`content_id` AS content_id + FROM `".BIT_DB_PREFIX."forum_board` b + INNER JOIN `".BIT_DB_PREFIX."liberty_content` blc ON (blc.`content_id`=b.`content_id`) + INNER JOIN `".BIT_DB_PREFIX."forum_map` map ON (map.`board_content_id`= blc.`content_id`) + INNER JOIN `".BIT_DB_PREFIX."liberty_content` lc ON (lc.`content_id`=map.`topic_content_id`) + ) + AND lc.`content_type_guid` != 'pigeonholes' + AND lc.`content_type_guid` != 'bitforum' + AND lc.`content_type_guid` != 'bitcomment' + AND lc.`title` != '' + ORDER BY lc.`content_type_guid`, lc.`title` + "; + $rs = $this->mDb->query( $sql ); + while( $row = $rs->fetchRow() ) { + $ret[$row['content_id']] = $row; + } + return $ret; + } + + function addContent($content_id) { + if (@BitBase::verifyId($content_id)) { + $data = array( + 'board_content_id'=>$this->mContentId, + 'topic_content_id'=>$content_id, + ); + $this->mDb->associateInsert( BIT_DB_PREFIX."forum_map",$data); + } + } + + function removeContent($content_id) { + if (@BitBase::verifyId($content_id) && @BitBase::verifyId($this->mContentId)) { + $sql = "DELETE FROM `".BIT_DB_PREFIX."forum_map` WHERE `board_content_id` = ? AND `topic_content_id` = ?"; + $result = $this->mDb->query( $sql, array( $this->mContentId,$content_id ) ); + } + } + + function verifyIntegrity() { + global $gBitSystem; + $ret = false; + if( $this->isValid() ) { + $sql = "SELECT + COUNT(*) + FROM `".BIT_DB_PREFIX."forum_board` b + INNER JOIN `".BIT_DB_PREFIX."forum_map` map ON (map.`board_content_id`= b.`content_id`) + WHERE b.`board_id`=? AND map.`board_content_id` = map.`topic_content_id` + "; + $count = $this->mDb->getOne( $sql, array( $this->mBitBoardId )); + return ($count==1); + } + return $ret; + } + + function fixContentMap() { + if( $this->isValid() && @BitBase::verifyId($this->mContentId)) { + $this->removeContent($this->mContentId); + $this->addContent($this->mContentId); + } + } + + function lookupMapRev($content_id) { + global $gBitDb; + $ret = NULL; + if (@BitBase::verifyId($content_id)) { + $sql = "SELECT `board_content_id` FROM `".BIT_DB_PREFIX."forum_map` map WHERE map.`topic_content_id`=?"; + $ret = $gBitDb->getOne( $sql, array( $content_id )); + } + return $ret; + } + + function getMap() { + global $gBitSystem; + $ret = NULL; + if( $this->isValid() ) { + $sql = "SELECT + lc.`title` AS t_title, + lc.`content_id` AS t_content_id, + blc.`title` AS b_title, + blc.`content_id` AS b_content_id, + b.`board_id` AS b_board_id, + ( SELECT count(*) + FROM `".BIT_DB_PREFIX."liberty_comments` lcom + WHERE lcom.`root_id`=lcom.`parent_id` AND lcom.`root_id`=lc.`content_id` + ) AS thread_count, + ((blc.`content_id`- lc.`content_id)*(blc.`content_id`- lc.`content_id)) AS order_key + FROM `".BIT_DB_PREFIX."forum_board` b + INNER JOIN `".BIT_DB_PREFIX."liberty_content` blc ON (blc.`content_id`=b.`content_id`) + INNER JOIN `".BIT_DB_PREFIX."forum_map` map ON (map.`board_content_id`= blc.`content_id`) + INNER JOIN `".BIT_DB_PREFIX."liberty_content` lc ON (lc.`content_id`=map.`topic_content_id`) + WHERE b.`board_id`=? AND map.`board_content_id`!=map.`topic_content_id` + ORDER BY order_key + "; + $rs = $this->mDb->query( $sql, array( $this->mBitBoardId )); + while( $row = $rs->fetchRow() ) { + $ret[$row['t_content_id']] = $row; } } - return( $ret ); + return $ret; } /** * This function generates a list of records from the liberty_content database for use in a list page **/ - function getFullList( &$pParamHash ) { + function getList( &$pParamHash ) { global $gBitSystem, $gBitUser; - $BIT_DB_PREFIX=BIT_DB_PREFIX; // this makes sure parameters used later on are set - LibertyAttachable::prepGetList( $pParamHash ); + LibertyContent::prepGetList( $pParamHash ); $selectSql = $joinSql = $whereSql = ''; $bindVars = array(); + array_push( $bindVars, $this->mContentTypeGuid ); $this->getServicesSql( 'content_list_sql_function', $selectSql, $joinSql, $whereSql, $bindVars ); // this will set $find, $sort_mode, $max_records and $offset @@ -82,75 +374,41 @@ class BitBoard extends LibertyAttachable { $bindVars[] = '%' . strtoupper( $find ). '%'; } - if (empty($pParamHash['ct'])) { - $query= "SELECT DISTINCT lcom.`root_id` AS root_id, lc.`content_id` AS content_id, lc.`title` AS title, lc.`content_type_guid` AS content_type_guid, - ( SELECT count(*) FROM `".BIT_DB_PREFIX."liberty_comments` slcom WHERE slcom.`root_id`=slcom.`parent_id` AND slcom.`root_id`=lc.`content_id` ) AS post_count - $selectSql - FROM ".BIT_DB_PREFIX."liberty_content lc - INNER JOIN ".BIT_DB_PREFIX."liberty_comments lcom ON( lc.`content_id`=lcom.`root_id` ) - $joinSql - WHERE TRUE $whereSql - "; - - $query_cant= "SELECT COUNT(DISTINCT lcom.root_id) - FROM ".BIT_DB_PREFIX."liberty_content lc - INNER JOIN ".BIT_DB_PREFIX."liberty_comments lcom ON( lc.content_id=lcom.root_id ) - $joinSql - WHERE TRUE $whereSql - "; - } else { - $whereSql .= " AND lc.`content_type_guid`= '{$pParamHash['ct']}'"; - $query= "SELECT lc.`content_id` AS content_id, lc.`title` AS title, lc.`content_type_guid` AS content_type_guid, - ( SELECT count(*) FROM `".BIT_DB_PREFIX."liberty_comments` lcom WHERE lcom.`root_id`=lcom.`parent_id` AND lcom.`root_id`=lc.`content_id` ) AS post_count - $selectSql - FROM ".BIT_DB_PREFIX."liberty_content lc - $joinSql - WHERE TRUE $whereSql - "; - - $query_cant= "SELECT COUNT(*) - FROM ".BIT_DB_PREFIX."liberty_content lc - $joinSql - WHERE TRUE $whereSql - "; - } + $query = "SELECT ts.*, lc.`content_id`, lc.`title`, lc.`data`, + ( SELECT count(*) + FROM `".BIT_DB_PREFIX."forum_map` AS map + INNER JOIN `".BIT_DB_PREFIX."liberty_comments` lcom ON (map.`topic_content_id` = lcom.`root_id`) + WHERE lcom.`root_id`=lcom.`parent_id` AND map.`board_content_id`=lc.`content_id` + ) AS post_count + $selectSql + FROM `".BIT_DB_PREFIX."forum_board` ts INNER JOIN `".BIT_DB_PREFIX."liberty_content` lc ON( lc.`content_id` = ts.`content_id` ) $joinSql + WHERE lc.`content_type_guid` = ? $whereSql + ORDER BY ".$this->mDb->convert_sortmode( $sort_mode ); + $query_cant = "select count(*) + FROM `".BIT_DB_PREFIX."forum_board` ts INNER JOIN `".BIT_DB_PREFIX."liberty_content` lc ON( lc.`content_id` = ts.`content_id` ) $joinSql + WHERE lc.`content_type_guid` = ? $whereSql"; $result = $this->mDb->query( $query, $bindVars, $max_records, $offset ); $ret = array(); while( $res = $result->fetchRow() ) { - $res['url']= BITBOARDS_PKG_URL."index.php?c={$res['content_id']}"; + $res['url']= BITBOARDS_PKG_URL."index.php?b={$res['board_id']}"; $ret[] = $res; } $pParamHash["cant"] = $this->mDb->getOne( $query_cant, $bindVars ); - // add all pagination info to pParamHash - LibertyAttachable::postGetList( $pParamHash ); - return $ret; - } - function getForumBoardSelectList() { - global $gBitDb; - $query = "SELECT lc.`content_id` as content_id, lc.`title` as title, lct.`content_description` AS description, - ( SELECT count(*) FROM `".BIT_DB_PREFIX."liberty_comments` slcom WHERE slcom.`root_id`=slcom.`parent_id` AND slcom.`root_id`=lc.`content_id ) AS post_count - FROM `".BIT_DB_PREFIX."liberty_content` lc - INNER JOIN `".BIT_DB_PREFIX."liberty_content_types` lct ON( lc.`content_type_guid` = lct.`content_type_guid` ) - WHERE lc.`content_type_guid`!='bitcomment' - ORDER BY lc.`content_type_guid` ASC"; - - $result = $gBitDb->query( $query); - $ret = array(); - while( $res = $result->fetchRow() ) { - $ret[] = $res; - } + // add all pagination info to pParamHash + LibertyContent::postGetList( $pParamHash ); return $ret; } /** - * Generates the URL to the bitboard page + * Generates the URL to the bitforum page + * @param pExistsHash the hash that was returned by LibertyContent::pageExists * @return the link to display the page. */ - function getForumDisplayUrl(&$lcontent) { + function getDisplayUrl() { $ret = NULL; - if( @$lcontent->verifyId( $lcontent->mContentId ) ) { - $ret = BITBOARDS_PKG_URL."index.php?c=".$lcontent->mContentId; + if( @$this->verifyId( $this->mBitBoardId ) ) { + $ret = BITBOARDS_PKG_URL."index.php?b=".$this->mBitBoardId; } return $ret; } |
