Commit d584ab0b50199c5c87111fc159c175cf5d4a4711

Authored by nbm
1 parent 776ac2ae

Modernise the discussion entities using the usual KTEntity style.


git-svn-id: https://kt-dms.svn.sourceforge.net/svnroot/kt-dms/trunk@4113 c91229c3-7414-0410-bfa2-8a42b809f60b
lib/discussions/DiscussionComment.inc
@@ -4,7 +4,7 @@ @@ -4,7 +4,7 @@
4 * 4 *
5 * Represents a document discussion comment. 5 * Represents a document discussion comment.
6 * 6 *
7 - * Copyright (c) 2003 Jam Warehouse http://www.jamwarehouse.com 7 + * Copyright (c) 2005 Jam Warehouse http://www.jamwarehouse.com
8 * 8 *
9 * This program is free software; you can redistribute it and/or modify 9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by 10 * it under the terms of the GNU General Public License as published by
@@ -21,206 +21,92 @@ @@ -21,206 +21,92 @@
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * 22 *
23 * @version $Revision$ 23 * @version $Revision$
24 - * @author Omar Rahbeeni, CS Holdings, South Africa  
25 - * @package lib.discussions 24 + * @author Neil Blakey-Milner, Jam Warehouse, South Africa
  25 + * @package lib.discussions
26 */ 26 */
  27 +
27 class DiscussionComment extends KTEntity { 28 class DiscussionComment extends KTEntity {
  29 + var $_bUsePearError = true;
  30 +
  31 + var $iThreadId;
  32 + var $iUserId;
  33 + var $sSubject;
  34 + var $sBody;
  35 + var $dDate;
  36 + var $iInReplyTo = -1;
28 37
29 - /**  
30 - * The underlying Discussion Comments class  
31 - */  
32 - var $iId;  
33 - var $iThreadID;  
34 - var $iUserID;  
35 - var $sSubject;  
36 - var $sBody;  
37 - var $dDate;  
38 - var $iInReplyTo;  
39 -  
40 - /**  
41 - * DiscussionComment Constructor  
42 - *  
43 - * @param string for body of text  
44 - * @param string for subject line  
45 - * @param integer for userID who is creating the Discussion Comment  
46 - * @param integer for the ThreadID which links all the comments for a document  
47 - */  
48 - function DiscussionComment($sNewBody, $sNewSubject, $iNewUserID, $iNewThreadID, $iNewInReplyTo) {  
49 - global $default; 38 + var $_aFieldToSelect = array(
  39 + 'iId' => 'id',
  40 + 'iThreadId' => 'thread_id',
  41 + 'iUserId' => 'user_id',
  42 + 'sSubject' => 'subject',
  43 + 'sBody' => 'body',
  44 + 'iInReplyTo' => 'in_reply_to',
  45 + 'dDate' => 'date',
  46 + );
50 47
51 - // start initializing variables.  
52 - $this->sBody = $sNewBody;  
53 - $this->sSubject = $sNewSubject;  
54 - $this->iUserID = $iNewUserID;  
55 - $this->iThreadID = $iNewThreadID;  
56 - $this->dDate = null; // This will get added in the SQL statement  
57 - $this->iInReplyTo = $iNewInReplyTo;  
58 - $this->iId = -1; // This will get created when the entry SQL statement 48 + function DiscussionComment($sBody = null, $sSubject = null, $iUserId = null, $iThreadId = null, $iInReplyTo = null) {
  49 + $this->sBody = $sBody;
  50 + $this->sSubject = $sSubject;
  51 + $this->iUserId = $iUserId;
  52 + $this->iThreadId = $iThreadId;
  53 + $this->iInReplyTo = $iInReplyTo;
59 } 54 }
60 55
61 -  
62 - /**  
63 - * Return the CommentID  
64 - */  
65 - function getID(){  
66 - return $this->iId;  
67 - }  
68 -  
69 - /**  
70 - * Return the ThreadID for the Comment  
71 - */  
72 - function getThreadID(){  
73 - return $this->iThreadID;  
74 - }  
75 -  
76 - /**  
77 - * Set a new Thread ID  
78 - */  
79 - function setThreadID($iNewThreadID){  
80 - $this->iThreadID = $iNewThreadID;  
81 - }  
82 -  
83 - /**  
84 - * Return the UserID  
85 - */  
86 - function getUserID(){  
87 - return $this->iUserID;  
88 - }  
89 -  
90 - /*  
91 - * Set a new User ID  
92 - */  
93 - function setUserID($iNewUserID){  
94 - $this->iUserID = $iNewUserID;  
95 - }  
96 -  
97 - /**  
98 - * Return the Subject text  
99 - */  
100 - function getSubject(){  
101 - return $this->sSubject;  
102 - }  
103 -  
104 - /**  
105 - * Set a new Subject text  
106 - */  
107 - function setSubject($sNewSubject){  
108 - $this->sSubject = $sNewSubject;  
109 -  
110 - }  
111 -  
112 - /**  
113 - * Return the Text Body  
114 - */  
115 - function getBody(){  
116 - return $this->sBody;  
117 - }  
118 -  
119 - /**  
120 - * Set a new TextBody  
121 - */  
122 - function setBody($sNewBody){  
123 - $this->sBody = $sNewBody;  
124 - }  
125 -  
126 - /**  
127 - * Get a Date Created  
128 - */  
129 - function getDate(){  
130 - return $this->dDate;  
131 - }  
132 -  
133 - /**  
134 - * Return the comment this is a reply to  
135 - */  
136 - function getInReplyTo(){  
137 - return $this->iInReplyTo;  
138 - }  
139 -  
140 - /**  
141 - * Sets the comment this is a reply to  
142 - */  
143 - function setInReplyTo($sNewCommentID){  
144 - $this->iInReplyTo = $sNewCommentID;  
145 - }  
146 -  
147 - /**  
148 - * Static function.  
149 - * Given a web_documents primary key it will create a  
150 - * discusson comment object and populate it with the  
151 - * corresponding database values  
152 - *  
153 - * @return Comment populated Comment object on successful query, false otherwise and set $_SESSION["errorMessage"]  
154 - */  
155 - function & get($iNewCommentID) {  
156 - global $default;  
157 - $sql = $default->db;  
158 - $result = $sql->query(array("SELECT * FROM $default->discussion_comments_table WHERE id = ?", $iNewCommentID));/*ok*/  
159 - if ($result) {  
160 - if ($sql->next_record()) {  
161 - $oDiscussionComment = & new DiscussionComment($sql->f("body"),$sql->f("subject"),$sql->f("user_id"),$sql->f("thread_id"),$sql->f("in_reply_to"));  
162 - $oDiscussionComment->iId = $iNewCommentID;  
163 - $oDiscussionComment->dDate = $sql->f("date");  
164 - return $oDiscussionComment;  
165 - }  
166 - return false;  
167 - }  
168 - return false; 56 + function getThreadId(){ return $this->iThreadId; }
  57 + function setThreadId($iThreadId){ $this->iThreadId = $iThreadId; }
  58 + function getUserId(){ return $this->iUserId; }
  59 + function setUserId($iNewUserId){ $this->iUserId = $iNewUserId; }
  60 + function getSubject(){ return $this->sSubject; }
  61 + function setSubject($sNewSubject){ $this->sSubject = $sNewSubject; }
  62 + function getBody(){ return $this->sBody; }
  63 + function setBody($sNewBody){ $this->sBody = $sNewBody; }
  64 + function getDate(){ return $this->dDate; }
  65 + function getInReplyTo(){ return $this->iInReplyTo; }
  66 + function setInReplyTo($sNewCommentId){ $this->iInReplyTo = $sNewCommentId; }
  67 +
  68 + function & get($iId) {
  69 + return KTEntityUtil::get('DiscussionComment', $iId);
169 } 70 }
170 -  
171 - /**  
172 - * Static function  
173 - * Get a list of DiscussionComments  
174 - *  
175 - * @param String Where clause (optional)  
176 - *  
177 - * @return Array array of DiscussionComments objects, false otherwise  
178 - */ 71 +
179 function getList($sWhereClause = null) { 72 function getList($sWhereClause = null) {
180 - return KTEntityUtil::getList(DiscussionComment::_table(), 'DiscussionComment', $sWhereClause); 73 + return KTEntityUtil::getList2('DiscussionComment', $sWhereClause);
181 } 74 }
182 75
183 - function _fieldValues () {  
184 - return array(  
185 - 'thread_id' => $this->iThreadID,  
186 - 'user_id' => $this->iUserID,  
187 - 'subject' => $this->sSubject,  
188 - 'body' => $this->sBody,  
189 - 'date' => getCurrentDateTime(),  
190 - 'in_reply_to' => $this->iInReplyTo,  
191 - ); 76 + function create() {
  77 + if (empty($this->dDate)) {
  78 + $this->dDate = getCurrentDateTime();
  79 + }
  80 + return parent::create();
192 } 81 }
193 82
194 function _table () { 83 function _table () {
195 global $default; 84 global $default;
196 return $default->discussion_comments_table; 85 return $default->discussion_comments_table;
197 } 86 }
198 -  
199 - function delete(){  
200 - global $default;  
201 -  
202 - // only delete the object if it exists in the database  
203 - if ($this->iId > 0) {  
204 - //check to see if group is linked to a unit  
205 - $sql = $default->db;  
206 - $query = array("SELECT * FROM ". $default->discussion_comments_table . " WHERE id = ?", $this->iId);/*ok*/  
207 - $sql->query($query);  
208 - $rows = $sql->num_rows($sql);  
209 -  
210 - if ($rows > 1) {  
211 - // duplicate Thread exists  
212 - return false;  
213 - } else {  
214 - $sql = $default->db;  
215 - $result = $sql->query("DELETE FROM $default->discussion_comments_table WHERE id = $this->iId");  
216 - if ($result) {  
217 - return true;  
218 - }  
219 - return false;  
220 - }  
221 - }  
222 - return false;  
223 - } 87 +
  88 + function &createFromArray($aArray) {
  89 + return KTEntityUtil::createFromArray('DiscussionComment', $aArray);
  90 + }
  91 +
  92 + function &getByThread($oThread) {
  93 + $iThreadId = KTUtil::getId($oThread);
  94 + return KTEntityUtil::getByDict('DiscussionComment', array(
  95 + 'thread_id' => $iThreadId,
  96 + ), array(
  97 + 'multi' => true,
  98 + ));
  99 + }
  100 +
  101 + function &getByThreadSortedByDate($oThread) {
  102 + $iThreadId = KTUtil::getId($oThread);
  103 + return KTEntityUtil::getByDict('DiscussionComment', array(
  104 + 'thread_id' => $iThreadId,
  105 + ), array(
  106 + 'multi' => true,
  107 + 'order' => 'date',
  108 + ));
  109 + }
224 } 110 }
225 111
226 ?> 112 ?>
lib/discussions/DiscussionThread.inc
@@ -25,143 +25,80 @@ @@ -25,143 +25,80 @@
25 * @package lib.discussions 25 * @package lib.discussions
26 */ 26 */
27 class DiscussionThread extends KTEntity{ 27 class DiscussionThread extends KTEntity{
  28 + var $_bUsePearError = true;
28 29
29 - /**  
30 - * The underlying Discussion Comments class  
31 - */  
32 - var $iId;  
33 - var $iDocumentID;  
34 - var $iFirstCommentID;  
35 - var $iLastCommentID;  
36 - var $iNumberOfViews;  
37 - var $iNumberOfReplies;  
38 - var $iCreatorID; 30 + var $iDocumentId;
  31 + var $iFirstCommentId = -1;
  32 + var $iLastCommentId = -1;
  33 + var $iNumberOfViews = 0;
  34 + var $iNumberOfReplies = 0;
  35 + var $iCreatorId;
39 36
  37 + var $_aFieldToSelect = array(
  38 + 'iId' => 'id',
  39 + 'iDocumentId' => 'document_id',
  40 + 'iFirstCommentId' => 'first_comment_id',
  41 + 'iLastCommentId' => 'last_comment_id',
  42 + 'iNumberOfViews' => 'views',
  43 + 'iNumberOfReplies' => 'replies',
  44 + 'iCreatorId' => 'creator_id',
  45 + );
40 46
41 /** 47 /**
42 * DiscussionThread Constructor 48 * DiscussionThread Constructor
43 */ 49 */
44 - function DiscussionThread($iNewFirstCommentID, $iNewDocumentID, $iNewCreatorID) {  
45 - global $default;  
46 - // create a new Discussion Thread object.  
47 - $this->iDocumentID = $iNewDocumentID;  
48 - $this->iCreatorID = $iNewCreatorID;  
49 - $this->iId = -1;  
50 - $this->iFirstCommentID = $iNewFirstCommentID;  
51 - $this->iLastCommentID = -1;  
52 - $this->iNumberOfViews = 0;  
53 - $this->iNumberOfReplies = 0; 50 + function DiscussionThread($iFirstCommentId = null, $iDocumentId = null, $iCreatorId = null) {
  51 + if (!empty($iDocumentId)) {
  52 + $this->iDocumentId = $iDocumentId;
  53 + }
  54 + if (!empty($iCreatorId)) {
  55 + $this->iCreatorId = $iCreatorId;
  56 + }
  57 + if (!empty($iFirstCommentId)) {
  58 + $this->iFirstCommentId = $iFirstCommentId;
  59 + }
54 } 60 }
55 61
56 - /**  
57 - * Get the iId for current thread  
58 - */  
59 - function getID(){  
60 - return $this->iId;  
61 - }  
62 -  
63 - /**  
64 - * Get document id  
65 - */  
66 - function getDocumentID(){  
67 - return $this->iDocumentID;  
68 - }  
69 -  
70 - /**  
71 - * Get the id of the first comment  
72 - */  
73 - function getFirstCommentID(){  
74 - return $this->iFirstCommentID;  
75 - }  
76 -  
77 - /**  
78 - * Set the First Comment ID  
79 - */  
80 - function setFirstCommentID($NewFirstCommentID){  
81 - $this->iFirstCommentID = $NewFirstCommentID;  
82 - }  
83 -  
84 - /**  
85 - * get the id of the last comment  
86 - */  
87 - function getLastCommentID(){  
88 - return $this->iLastCommentID;  
89 - }  
90 -  
91 - /**  
92 - * Set the id of the last comment  
93 - */  
94 - function setLastCommentID($iNewLastComment){  
95 - $this->iLastCommentID = $iNewLastComment;  
96 - }  
97 -  
98 - /**  
99 - * Get the total number of time the thread was viewed  
100 - */  
101 - function getNumberOfViews(){  
102 - return $this->iNumberOfViews;  
103 - }  
104 - /**  
105 - * Set the total number of times the thread was viewed  
106 - */  
107 - function setNumberOfViews($iValue) {  
108 - $this->iNumberOfViews = $iValue;  
109 - }  
110 -  
111 - /**  
112 - * Increment the total number of times the thread was viewed  
113 - */  
114 - function incrementNumberOfViews() {  
115 - $this->iNumberOfViews += 1;  
116 - }  
117 -  
118 - /**  
119 - * Get the total number of replies(comments) in a thread  
120 - */  
121 - function getNumberOfReplies(){  
122 - return $this->iNumberOfReplies;  
123 - }  
124 -  
125 - /**  
126 - * Increment the total number of replies (comments) in a thread  
127 - */  
128 - function incrementNumberOfReplies(){  
129 - $this->iNumberOfReplies += 1;  
130 - }  
131 - /**  
132 - * Set the total number of number of replies (comments) in a thread  
133 - */  
134 - function setNumberOfReplies($iValue){  
135 - $this->iNumberOfReplies = $iValue;  
136 - } 62 + function getDocumentId(){ return $this->iDocumentId; }
  63 + function getCreatorId(){ return $this->iCreatorId; }
  64 + function getFirstCommentId(){ return $this->iFirstCommentId; }
  65 + function setFirstCommentId($NewFirstCommentId){ $this->iFirstCommentId = $NewFirstCommentId; }
  66 + function getLastCommentId(){ return $this->iLastCommentId; }
  67 + function setLastCommentId($iNewLastComment){ $this->iLastCommentId = $iNewLastComment; }
  68 + function getNumberOfViews(){ return $this->iNumberOfViews; }
  69 + function setNumberOfViews($iValue) { $this->iNumberOfViews = $iValue; }
  70 + function incrementNumberOfViews() { $this->iNumberOfViews += 1; }
  71 + function getNumberOfReplies(){ return $this->iNumberOfReplies; }
  72 + function incrementNumberOfReplies(){ $this->iNumberOfReplies += 1; }
  73 + function setNumberOfReplies($iValue){ $this->iNumberOfReplies = $iValue; }
137 74
138 /** 75 /**
139 - * Get a All commentID's seperated by a comma "," 76 + * Get a All commentId's seperated by a comma ","
140 */ 77 */
141 - function getAllCommentID() { 78 + function getAllCommentId() {
142 global $default; 79 global $default;
143 80
144 $sql = $default->db; 81 $sql = $default->db;
145 $aQuery = array("SELECT id FROM $default->discussion_threads_table WHERE document_id = ? ORDER BY id",/*ok*/ 82 $aQuery = array("SELECT id FROM $default->discussion_threads_table WHERE document_id = ? ORDER BY id",/*ok*/
146 - $this->iDocumentID); 83 + $this->iDocumentId);
147 $result = $sql->query($aQuery); 84 $result = $sql->query($aQuery);
148 if ($result) { 85 if ($result) {
149 $sql->next_record(); 86 $sql->next_record();
150 - $iThreadID = $sql->f("id"); 87 + $iThreadId = $sql->f("id");
151 88
152 $aQuery = array("SELECT id FROM $default->discussion_comments_table WHERE thread_id = ? ORDER BY date DESC",/*ok*/ 89 $aQuery = array("SELECT id FROM $default->discussion_comments_table WHERE thread_id = ? ORDER BY date DESC",/*ok*/
153 - $iThreadID); 90 + $iThreadId);
154 $result = $sql->query($aQuery); 91 $result = $sql->query($aQuery);
155 92
156 if ($result) { 93 if ($result) {
157 while ($sql->next_record()) { 94 while ($sql->next_record()) {
158 if ($sql->f("id") > 0) { 95 if ($sql->f("id") > 0) {
159 - $sAllCommentID .= $sql->f("id") . ","; 96 + $sAllCommentId .= $sql->f("id") . ",";
160 } else { 97 } else {
161 - //ID not valid 98 + //Id not valid
162 } 99 }
163 } 100 }
164 - return $sAllCommentID ; 101 + return $sAllCommentId ;
165 } 102 }
166 return false; 103 return false;
167 } else { 104 } else {
@@ -182,10 +119,10 @@ class DiscussionThread extends KTEntity{ @@ -182,10 +119,10 @@ class DiscussionThread extends KTEntity{
182 return KTEntityUtil::getList(DiscussionThread::_table(), 'DiscussionThread', $sWhereClause); 119 return KTEntityUtil::getList(DiscussionThread::_table(), 'DiscussionThread', $sWhereClause);
183 } 120 }
184 121
185 - function getThreadIDforDoc($iDocumentID){ 122 + function getThreadIdforDoc($iDocumentId){
186 global $default; 123 global $default;
187 $sql = $default->db; 124 $sql = $default->db;
188 - $result = $sql->query(array("SELECT id FROM $default->discussion_threads_table WHERE document_id = ?", $iDocumentID));/*ok*/ 125 + $result = $sql->query(array("SELECT id FROM $default->discussion_threads_table WHERE document_id = ?", $iDocumentId));/*ok*/
189 if ($result) { 126 if ($result) {
190 if ($sql->next_record()) { 127 if ($sql->next_record()) {
191 if ($sql->f("id") > 0) { 128 if ($sql->f("id") > 0) {
@@ -200,45 +137,8 @@ class DiscussionThread extends KTEntity{ @@ -200,45 +137,8 @@ class DiscussionThread extends KTEntity{
200 } 137 }
201 138
202 139
203 - /**  
204 - * Static function.  
205 - * Given a web_documents primary key it will create a  
206 - * DiscussionThread object and populate it with the  
207 - * corresponding database values  
208 - *  
209 - * @return Unit populated Unit object on successful query, false otherwise and set $_SESSION["errorMessage"]  
210 - */  
211 - function & get($iNewThreadID) {  
212 - global $default;  
213 - $sql = $default->db;  
214 - $result = $sql->query(array("SELECT * FROM $default->discussion_threads_table WHERE id = ?", $iNewThreadID));/*ok*/  
215 - if ($result) {  
216 - if ($sql->next_record()) {  
217 -  
218 - $oDiscussionThread = & new DiscussionThread($sql->f("first_comment_id"),  
219 - $sql->f("document_id"),  
220 - $sql->f("creator_id"));  
221 -  
222 - $oDiscussionThread->iId = $iNewThreadID;  
223 - $oDiscussionThread->iLastCommentID = $sql->f("last_comment_id");  
224 - $oDiscussionThread->iNumberOfViews = $sql->f("views");  
225 - $oDiscussionThread->iNumberOfReplies = $sql->f("replies");  
226 - return $oDiscussionThread;  
227 - }  
228 - return false;  
229 - }  
230 - return false;  
231 - }  
232 -  
233 - function _fieldValues () {  
234 - return array(  
235 - 'document_id' => $this->iDocumentID,  
236 - 'first_comment_id' => $this->iFirstCommentID,  
237 - 'last_comment_id' => $this->iLastCommentID,  
238 - 'views' => $this->iNumberOfViews,  
239 - 'replies' => $this->iNumberOfReplies,  
240 - 'creator_id' => $this->iCreatorID,  
241 - ); 140 + function &get($iId) {
  141 + return KTEntityUtil::get('DiscussionThread', $iId);
242 } 142 }
243 143
244 function _table () { 144 function _table () {
@@ -246,36 +146,17 @@ class DiscussionThread extends KTEntity{ @@ -246,36 +146,17 @@ class DiscussionThread extends KTEntity{
246 return $default->discussion_threads_table; 146 return $default->discussion_threads_table;
247 } 147 }
248 148
249 - /**  
250 - * Delete a thread in the table  
251 - */  
252 - function delete(){  
253 - global $default;  
254 -  
255 - //only delete the object if it exists in the database  
256 - if ($this->iId > 0) {  
257 - //check to see if group is linked to a unit  
258 - $sql = $default->db;  
259 - $query = array("SELECT * FROM ". $default->discussion_threads_table ." WHERE id = ?", $this->iId);/*ok*/  
260 - $sql->query($query);  
261 - $rows = $sql->num_rows($sql); 149 + function &createFromArray($aArray) {
  150 + return KTEntityUTil::createFromArray('DiscussionThread', $aArray);
  151 + }
262 152
263 - if ($rows > 1) {  
264 - // duplicate Thread exists  
265 - $_SESSION["errorMessage"] = "Thread::The Thread id " . $this->iId . " has duplicates!";  
266 - return false; 153 + function getComments() {
  154 + return DiscussionComment::getByThread($this);
  155 + }
267 156
268 - } else {  
269 - $sql = $default->db;  
270 - $result = $sql->query("DELETE FROM $default->discussion_threads_table WHERE id = $this->iId");  
271 - if ($result) {  
272 - return true;  
273 - }  
274 - return false;  
275 - }  
276 - }  
277 - return false;  
278 - } 157 + function getCommentsSortedByDate() {
  158 + return DiscussionComment::getByThreadSortedByDate($this);
  159 + }
279 } 160 }
280 161
281 ?> 162 ?>