From d584ab0b50199c5c87111fc159c175cf5d4a4711 Mon Sep 17 00:00:00 2001 From: nbm Date: Tue, 22 Nov 2005 14:18:59 +0000 Subject: [PATCH] Modernise the discussion entities using the usual KTEntity style. --- lib/discussions/DiscussionComment.inc | 256 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- lib/discussions/DiscussionThread.inc | 237 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 2 files changed, 130 insertions(+), 363 deletions(-) diff --git a/lib/discussions/DiscussionComment.inc b/lib/discussions/DiscussionComment.inc index 5f07211..704d57c 100644 --- a/lib/discussions/DiscussionComment.inc +++ b/lib/discussions/DiscussionComment.inc @@ -4,7 +4,7 @@ * * Represents a document discussion comment. * - * Copyright (c) 2003 Jam Warehouse http://www.jamwarehouse.com + * Copyright (c) 2005 Jam Warehouse http://www.jamwarehouse.com * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -21,206 +21,92 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * @version $Revision$ - * @author Omar Rahbeeni, CS Holdings, South Africa - * @package lib.discussions + * @author Neil Blakey-Milner, Jam Warehouse, South Africa + * @package lib.discussions */ + class DiscussionComment extends KTEntity { + var $_bUsePearError = true; + + var $iThreadId; + var $iUserId; + var $sSubject; + var $sBody; + var $dDate; + var $iInReplyTo = -1; - /** - * The underlying Discussion Comments class - */ - var $iId; - var $iThreadID; - var $iUserID; - var $sSubject; - var $sBody; - var $dDate; - var $iInReplyTo; - - /** - * DiscussionComment Constructor - * - * @param string for body of text - * @param string for subject line - * @param integer for userID who is creating the Discussion Comment - * @param integer for the ThreadID which links all the comments for a document - */ - function DiscussionComment($sNewBody, $sNewSubject, $iNewUserID, $iNewThreadID, $iNewInReplyTo) { - global $default; + var $_aFieldToSelect = array( + 'iId' => 'id', + 'iThreadId' => 'thread_id', + 'iUserId' => 'user_id', + 'sSubject' => 'subject', + 'sBody' => 'body', + 'iInReplyTo' => 'in_reply_to', + 'dDate' => 'date', + ); - // start initializing variables. - $this->sBody = $sNewBody; - $this->sSubject = $sNewSubject; - $this->iUserID = $iNewUserID; - $this->iThreadID = $iNewThreadID; - $this->dDate = null; // This will get added in the SQL statement - $this->iInReplyTo = $iNewInReplyTo; - $this->iId = -1; // This will get created when the entry SQL statement + function DiscussionComment($sBody = null, $sSubject = null, $iUserId = null, $iThreadId = null, $iInReplyTo = null) { + $this->sBody = $sBody; + $this->sSubject = $sSubject; + $this->iUserId = $iUserId; + $this->iThreadId = $iThreadId; + $this->iInReplyTo = $iInReplyTo; } - - /** - * Return the CommentID - */ - function getID(){ - return $this->iId; - } - - /** - * Return the ThreadID for the Comment - */ - function getThreadID(){ - return $this->iThreadID; - } - - /** - * Set a new Thread ID - */ - function setThreadID($iNewThreadID){ - $this->iThreadID = $iNewThreadID; - } - - /** - * Return the UserID - */ - function getUserID(){ - return $this->iUserID; - } - - /* - * Set a new User ID - */ - function setUserID($iNewUserID){ - $this->iUserID = $iNewUserID; - } - - /** - * Return the Subject text - */ - function getSubject(){ - return $this->sSubject; - } - - /** - * Set a new Subject text - */ - function setSubject($sNewSubject){ - $this->sSubject = $sNewSubject; - - } - - /** - * Return the Text Body - */ - function getBody(){ - return $this->sBody; - } - - /** - * Set a new TextBody - */ - function setBody($sNewBody){ - $this->sBody = $sNewBody; - } - - /** - * Get a Date Created - */ - function getDate(){ - return $this->dDate; - } - - /** - * Return the comment this is a reply to - */ - function getInReplyTo(){ - return $this->iInReplyTo; - } - - /** - * Sets the comment this is a reply to - */ - function setInReplyTo($sNewCommentID){ - $this->iInReplyTo = $sNewCommentID; - } - - /** - * Static function. - * Given a web_documents primary key it will create a - * discusson comment object and populate it with the - * corresponding database values - * - * @return Comment populated Comment object on successful query, false otherwise and set $_SESSION["errorMessage"] - */ - function & get($iNewCommentID) { - global $default; - $sql = $default->db; - $result = $sql->query(array("SELECT * FROM $default->discussion_comments_table WHERE id = ?", $iNewCommentID));/*ok*/ - if ($result) { - if ($sql->next_record()) { - $oDiscussionComment = & new DiscussionComment($sql->f("body"),$sql->f("subject"),$sql->f("user_id"),$sql->f("thread_id"),$sql->f("in_reply_to")); - $oDiscussionComment->iId = $iNewCommentID; - $oDiscussionComment->dDate = $sql->f("date"); - return $oDiscussionComment; - } - return false; - } - return false; + function getThreadId(){ return $this->iThreadId; } + function setThreadId($iThreadId){ $this->iThreadId = $iThreadId; } + function getUserId(){ return $this->iUserId; } + function setUserId($iNewUserId){ $this->iUserId = $iNewUserId; } + function getSubject(){ return $this->sSubject; } + function setSubject($sNewSubject){ $this->sSubject = $sNewSubject; } + function getBody(){ return $this->sBody; } + function setBody($sNewBody){ $this->sBody = $sNewBody; } + function getDate(){ return $this->dDate; } + function getInReplyTo(){ return $this->iInReplyTo; } + function setInReplyTo($sNewCommentId){ $this->iInReplyTo = $sNewCommentId; } + + function & get($iId) { + return KTEntityUtil::get('DiscussionComment', $iId); } - - /** - * Static function - * Get a list of DiscussionComments - * - * @param String Where clause (optional) - * - * @return Array array of DiscussionComments objects, false otherwise - */ + function getList($sWhereClause = null) { - return KTEntityUtil::getList(DiscussionComment::_table(), 'DiscussionComment', $sWhereClause); + return KTEntityUtil::getList2('DiscussionComment', $sWhereClause); } - function _fieldValues () { - return array( - 'thread_id' => $this->iThreadID, - 'user_id' => $this->iUserID, - 'subject' => $this->sSubject, - 'body' => $this->sBody, - 'date' => getCurrentDateTime(), - 'in_reply_to' => $this->iInReplyTo, - ); + function create() { + if (empty($this->dDate)) { + $this->dDate = getCurrentDateTime(); + } + return parent::create(); } function _table () { global $default; return $default->discussion_comments_table; } - - function delete(){ - global $default; - - // only delete the object if it exists in the database - if ($this->iId > 0) { - //check to see if group is linked to a unit - $sql = $default->db; - $query = array("SELECT * FROM ". $default->discussion_comments_table . " WHERE id = ?", $this->iId);/*ok*/ - $sql->query($query); - $rows = $sql->num_rows($sql); - - if ($rows > 1) { - // duplicate Thread exists - return false; - } else { - $sql = $default->db; - $result = $sql->query("DELETE FROM $default->discussion_comments_table WHERE id = $this->iId"); - if ($result) { - return true; - } - return false; - } - } - return false; - } + + function &createFromArray($aArray) { + return KTEntityUtil::createFromArray('DiscussionComment', $aArray); + } + + function &getByThread($oThread) { + $iThreadId = KTUtil::getId($oThread); + return KTEntityUtil::getByDict('DiscussionComment', array( + 'thread_id' => $iThreadId, + ), array( + 'multi' => true, + )); + } + + function &getByThreadSortedByDate($oThread) { + $iThreadId = KTUtil::getId($oThread); + return KTEntityUtil::getByDict('DiscussionComment', array( + 'thread_id' => $iThreadId, + ), array( + 'multi' => true, + 'order' => 'date', + )); + } } ?> diff --git a/lib/discussions/DiscussionThread.inc b/lib/discussions/DiscussionThread.inc index b2d53f6..15865ec 100644 --- a/lib/discussions/DiscussionThread.inc +++ b/lib/discussions/DiscussionThread.inc @@ -25,143 +25,80 @@ * @package lib.discussions */ class DiscussionThread extends KTEntity{ + var $_bUsePearError = true; - /** - * The underlying Discussion Comments class - */ - var $iId; - var $iDocumentID; - var $iFirstCommentID; - var $iLastCommentID; - var $iNumberOfViews; - var $iNumberOfReplies; - var $iCreatorID; + var $iDocumentId; + var $iFirstCommentId = -1; + var $iLastCommentId = -1; + var $iNumberOfViews = 0; + var $iNumberOfReplies = 0; + var $iCreatorId; + var $_aFieldToSelect = array( + 'iId' => 'id', + 'iDocumentId' => 'document_id', + 'iFirstCommentId' => 'first_comment_id', + 'iLastCommentId' => 'last_comment_id', + 'iNumberOfViews' => 'views', + 'iNumberOfReplies' => 'replies', + 'iCreatorId' => 'creator_id', + ); /** * DiscussionThread Constructor */ - function DiscussionThread($iNewFirstCommentID, $iNewDocumentID, $iNewCreatorID) { - global $default; - // create a new Discussion Thread object. - $this->iDocumentID = $iNewDocumentID; - $this->iCreatorID = $iNewCreatorID; - $this->iId = -1; - $this->iFirstCommentID = $iNewFirstCommentID; - $this->iLastCommentID = -1; - $this->iNumberOfViews = 0; - $this->iNumberOfReplies = 0; + function DiscussionThread($iFirstCommentId = null, $iDocumentId = null, $iCreatorId = null) { + if (!empty($iDocumentId)) { + $this->iDocumentId = $iDocumentId; + } + if (!empty($iCreatorId)) { + $this->iCreatorId = $iCreatorId; + } + if (!empty($iFirstCommentId)) { + $this->iFirstCommentId = $iFirstCommentId; + } } - /** - * Get the iId for current thread - */ - function getID(){ - return $this->iId; - } - - /** - * Get document id - */ - function getDocumentID(){ - return $this->iDocumentID; - } - - /** - * Get the id of the first comment - */ - function getFirstCommentID(){ - return $this->iFirstCommentID; - } - - /** - * Set the First Comment ID - */ - function setFirstCommentID($NewFirstCommentID){ - $this->iFirstCommentID = $NewFirstCommentID; - } - - /** - * get the id of the last comment - */ - function getLastCommentID(){ - return $this->iLastCommentID; - } - - /** - * Set the id of the last comment - */ - function setLastCommentID($iNewLastComment){ - $this->iLastCommentID = $iNewLastComment; - } - - /** - * Get the total number of time the thread was viewed - */ - function getNumberOfViews(){ - return $this->iNumberOfViews; - } - /** - * Set the total number of times the thread was viewed - */ - function setNumberOfViews($iValue) { - $this->iNumberOfViews = $iValue; - } - - /** - * Increment the total number of times the thread was viewed - */ - function incrementNumberOfViews() { - $this->iNumberOfViews += 1; - } - - /** - * Get the total number of replies(comments) in a thread - */ - function getNumberOfReplies(){ - return $this->iNumberOfReplies; - } - - /** - * Increment the total number of replies (comments) in a thread - */ - function incrementNumberOfReplies(){ - $this->iNumberOfReplies += 1; - } - /** - * Set the total number of number of replies (comments) in a thread - */ - function setNumberOfReplies($iValue){ - $this->iNumberOfReplies = $iValue; - } + function getDocumentId(){ return $this->iDocumentId; } + function getCreatorId(){ return $this->iCreatorId; } + function getFirstCommentId(){ return $this->iFirstCommentId; } + function setFirstCommentId($NewFirstCommentId){ $this->iFirstCommentId = $NewFirstCommentId; } + function getLastCommentId(){ return $this->iLastCommentId; } + function setLastCommentId($iNewLastComment){ $this->iLastCommentId = $iNewLastComment; } + function getNumberOfViews(){ return $this->iNumberOfViews; } + function setNumberOfViews($iValue) { $this->iNumberOfViews = $iValue; } + function incrementNumberOfViews() { $this->iNumberOfViews += 1; } + function getNumberOfReplies(){ return $this->iNumberOfReplies; } + function incrementNumberOfReplies(){ $this->iNumberOfReplies += 1; } + function setNumberOfReplies($iValue){ $this->iNumberOfReplies = $iValue; } /** - * Get a All commentID's seperated by a comma "," + * Get a All commentId's seperated by a comma "," */ - function getAllCommentID() { + function getAllCommentId() { global $default; $sql = $default->db; $aQuery = array("SELECT id FROM $default->discussion_threads_table WHERE document_id = ? ORDER BY id",/*ok*/ - $this->iDocumentID); + $this->iDocumentId); $result = $sql->query($aQuery); if ($result) { $sql->next_record(); - $iThreadID = $sql->f("id"); + $iThreadId = $sql->f("id"); $aQuery = array("SELECT id FROM $default->discussion_comments_table WHERE thread_id = ? ORDER BY date DESC",/*ok*/ - $iThreadID); + $iThreadId); $result = $sql->query($aQuery); if ($result) { while ($sql->next_record()) { if ($sql->f("id") > 0) { - $sAllCommentID .= $sql->f("id") . ","; + $sAllCommentId .= $sql->f("id") . ","; } else { - //ID not valid + //Id not valid } } - return $sAllCommentID ; + return $sAllCommentId ; } return false; } else { @@ -182,10 +119,10 @@ class DiscussionThread extends KTEntity{ return KTEntityUtil::getList(DiscussionThread::_table(), 'DiscussionThread', $sWhereClause); } - function getThreadIDforDoc($iDocumentID){ + function getThreadIdforDoc($iDocumentId){ global $default; $sql = $default->db; - $result = $sql->query(array("SELECT id FROM $default->discussion_threads_table WHERE document_id = ?", $iDocumentID));/*ok*/ + $result = $sql->query(array("SELECT id FROM $default->discussion_threads_table WHERE document_id = ?", $iDocumentId));/*ok*/ if ($result) { if ($sql->next_record()) { if ($sql->f("id") > 0) { @@ -200,45 +137,8 @@ class DiscussionThread extends KTEntity{ } - /** - * Static function. - * Given a web_documents primary key it will create a - * DiscussionThread object and populate it with the - * corresponding database values - * - * @return Unit populated Unit object on successful query, false otherwise and set $_SESSION["errorMessage"] - */ - function & get($iNewThreadID) { - global $default; - $sql = $default->db; - $result = $sql->query(array("SELECT * FROM $default->discussion_threads_table WHERE id = ?", $iNewThreadID));/*ok*/ - if ($result) { - if ($sql->next_record()) { - - $oDiscussionThread = & new DiscussionThread($sql->f("first_comment_id"), - $sql->f("document_id"), - $sql->f("creator_id")); - - $oDiscussionThread->iId = $iNewThreadID; - $oDiscussionThread->iLastCommentID = $sql->f("last_comment_id"); - $oDiscussionThread->iNumberOfViews = $sql->f("views"); - $oDiscussionThread->iNumberOfReplies = $sql->f("replies"); - return $oDiscussionThread; - } - return false; - } - return false; - } - - function _fieldValues () { - return array( - 'document_id' => $this->iDocumentID, - 'first_comment_id' => $this->iFirstCommentID, - 'last_comment_id' => $this->iLastCommentID, - 'views' => $this->iNumberOfViews, - 'replies' => $this->iNumberOfReplies, - 'creator_id' => $this->iCreatorID, - ); + function &get($iId) { + return KTEntityUtil::get('DiscussionThread', $iId); } function _table () { @@ -246,36 +146,17 @@ class DiscussionThread extends KTEntity{ return $default->discussion_threads_table; } - /** - * Delete a thread in the table - */ - function delete(){ - global $default; - - //only delete the object if it exists in the database - if ($this->iId > 0) { - //check to see if group is linked to a unit - $sql = $default->db; - $query = array("SELECT * FROM ". $default->discussion_threads_table ." WHERE id = ?", $this->iId);/*ok*/ - $sql->query($query); - $rows = $sql->num_rows($sql); + function &createFromArray($aArray) { + return KTEntityUTil::createFromArray('DiscussionThread', $aArray); + } - if ($rows > 1) { - // duplicate Thread exists - $_SESSION["errorMessage"] = "Thread::The Thread id " . $this->iId . " has duplicates!"; - return false; + function getComments() { + return DiscussionComment::getByThread($this); + } - } else { - $sql = $default->db; - $result = $sql->query("DELETE FROM $default->discussion_threads_table WHERE id = $this->iId"); - if ($result) { - return true; - } - return false; - } - } - return false; - } + function getCommentsSortedByDate() { + return DiscussionComment::getByThreadSortedByDate($this); + } } ?> -- libgit2 0.21.4