00001 <?PHP 00002 00003 # 00004 # FILE: SPT--Topic.php 00005 # 00006 # FUNCTIONS PROVIDED: 00007 # Topic->Topic($TopicId) 00008 # - constructor 00009 # Topic->TopicId() 00010 # Topic->ForumId() 00011 # Topic->CreatorId() 00012 # Topic->DateCreated() 00013 # Topic->TopicName() 00014 # Topic->ViewCount() 00015 # Topic->MessageCount() 00016 # Topic->Delete() 00017 # - methods to retrieve resource attributes 00018 # 00019 # Part of the Scout Portal Toolkit 00020 # Copyright 2002 Internet Scout Project 00021 # http://scout.cs.wisc.edu 00022 # 00023 00029 class Topic { 00030 00031 # ---- PUBLIC INTERFACE -------------------------------------------------- 00032 00033 00034 # Error codes for the TOPIC 00035 const OK = 0; 00036 const NONEXISTENT = 1; 00037 00040 00048 function Topic($TopicId = NULL ) 00049 { 00050 $this->ErrorStatus = Topic::OK; 00051 # locate class in database 00052 $this->DB = new SPTDatabase(); 00053 $DB =& $this->DB; 00054 # if ID supplied 00055 if ($TopicId !== NULL) 00056 { 00057 $this->TopicId = intval($TopicId); 00058 $DB->Query("SELECT * FROM Topics WHERE TopicId = ".$this->TopicId); 00059 00060 # if row was loaded 00061 if ($DB->NumRowsSelected() > 0) 00062 { 00063 # set attributes to values returned by database 00064 $this->DBFields = $DB->FetchRow(); 00065 } 00066 else 00067 { 00068 $this->ErrorStatus = Topic::NONEXISTENT; 00069 } 00070 } 00071 else 00072 { 00073 # add record to database with that ID 00074 $DB->Query("INSERT INTO Topics (TopicId) VALUES (NULL)"); 00075 $this->TopicId = $DB->Query("SELECT LAST_INSERT_ID()" 00076 ." AS TopicId FROM Topics", "TopicId"); 00077 } 00078 00079 } 00080 00084 function Delete() 00085 { 00086 if ($this->ErrorStatus == Topic::OK) 00087 { 00088 $this->DB->Query("Select * from Messages where ParentId = ". 00089 $this->TopicId." AND ParentType = 1"); 00090 00091 # delete messages associated with this topic 00092 while ($Entry = $this->DB->FetchRow()) 00093 { 00094 $Message = & new Message($Entry["MessageId"]); 00095 $Message->Delete(); 00096 } 00097 $this->DB->Query("DELETE FROM Topics WHERE TopicId=".$this->TopicId); 00098 } 00099 } 00100 00105 00110 function GetMessageList() 00111 { 00112 $Messages = array(); 00113 00114 $this->DB->Query("Select * from Messages where ParentId = ". 00115 $this->TopicId. 00116 " AND ParentType = 1 ORDER BY DatePosted DESC"); 00117 00118 # delete messages associated with this topic 00119 while ($Entry = $this->DB->FetchRow()) 00120 { 00121 $Messages[$Entry["MessageId"]] = & new Message($Entry["MessageId"]); 00122 } 00123 return $Messages; 00124 } 00125 00130 function TopicId() { return $this->TopicId; } 00131 00136 function CreatorName() 00137 { 00138 $CreatorName = new User($this->DB, (int)$this->CreatorId()); 00139 return $CreatorName->Get("UserName"); 00140 } 00141 00146 function CreatorEmail() 00147 { 00148 $CreatorName = new User($this->DB, (int)$this->CreatorId()); 00149 return $CreatorName->Get("EMail"); 00150 } 00151 00157 function ForumId($NewValue = DB_NOVALUE) { return $this->UpdateValue("ForumId", $NewValue); } 00158 00164 function CreatorId($NewValue = DB_NOVALUE) { return $this->UpdateValue("CreatorId", $NewValue); } 00165 00171 function DateCreated($NewValue = DB_NOVALUE) { return $this->UpdateValue("DateCreated", $NewValue); } 00172 00178 function TopicName($NewValue = DB_NOVALUE) { return $this->UpdateValue("TopicName", $NewValue); } 00179 00185 function ViewCount($NewValue = DB_NOVALUE) { return $this->UpdateValue("ViewCount", $NewValue); } 00186 00192 function MessageCount($NewValue = DB_NOVALUE) { return $this->UpdateValue("MessageCount", $NewValue); } 00193 00198 function GetErrorStatus() { return $this->ErrorStatus; } 00199 00202 # ---- PRIVATE INTERFACE ------------------------------------------------- 00203 00204 private $TopicId; 00205 private $DB; 00206 private $DBFields; 00207 private $ErrorStatus; 00208 00209 # convenience function to supply parameters to Database->UpdateValue() 00210 private function UpdateValue($FieldName, $NewValue) 00211 { 00212 if ($this->ErrorStatus == Topic::OK) 00213 { 00214 return $this->DB->UpdateValue("Topics", $FieldName, $NewValue, 00215 "TopicId = '".$this->TopicId."'", $this->DBFields, TRUE); 00216 } 00217 else 00218 { 00219 return NULL; 00220 } 00221 } 00222 } 00223 00224 ?>