Topic.php
Go to the documentation of this file.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 elseif (func_num_args()==0)
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 else
00079 {
00080 $this->ErrorStatus = Topic::NONEXISTENT;
00081 }
00082
00083
00084 }
00085
00089 function Delete()
00090 {
00091 if ($this->ErrorStatus == Topic::OK)
00092 {
00093 $this->DB->Query("Select * from Messages where ParentId = ".
00094 $this->TopicId." AND ParentType = 1");
00095
00096 # delete messages associated with this topic
00097 while ($Entry = $this->DB->FetchRow())
00098 {
00099 $Message = & new Message($Entry["MessageId"]);
00100 $Message->Delete();
00101 }
00102 $this->DB->Query("DELETE FROM Topics WHERE TopicId=".$this->TopicId);
00103 }
00104 }
00105
00110
00115 function GetMessageList()
00116 {
00117 $Messages = array();
00118
00119 $this->DB->Query("Select * from Messages where ParentId = ".
00120 $this->TopicId.
00121 " AND ParentType = 1 ORDER BY DatePosted DESC");
00122
00123 # delete messages associated with this topic
00124 while ($Entry = $this->DB->FetchRow())
00125 {
00126 $Messages[$Entry["MessageId"]] = & new Message($Entry["MessageId"]);
00127 }
00128 return $Messages;
00129 }
00130
00135 function TopicId() { return $this->TopicId; }
00136
00141 function CreatorName()
00142 {
00143 $CreatorName = new User($this->DB, (int)$this->CreatorId());
00144 return $CreatorName->Get("UserName");
00145 }
00146
00151 function CreatorEmail()
00152 {
00153 $CreatorName = new User($this->DB, (int)$this->CreatorId());
00154 return $CreatorName->Get("EMail");
00155 }
00156
00162 function ForumId($NewValue = DB_NOVALUE) { return $this->UpdateValue("ForumId", $NewValue); }
00163
00169 function CreatorId($NewValue = DB_NOVALUE) { return $this->UpdateValue("CreatorId", $NewValue); }
00170
00176 function DateCreated($NewValue = DB_NOVALUE) { return $this->UpdateValue("DateCreated", $NewValue); }
00177
00183 function TopicName($NewValue = DB_NOVALUE) { return $this->UpdateValue("TopicName", $NewValue); }
00184
00190 function ViewCount($NewValue = DB_NOVALUE) { return $this->UpdateValue("ViewCount", $NewValue); }
00191
00197 function MessageCount($NewValue = DB_NOVALUE) { return $this->UpdateValue("MessageCount", $NewValue); }
00198
00203 function GetErrorStatus() { return $this->ErrorStatus; }
00204
00207 # ---- PRIVATE INTERFACE -------------------------------------------------
00208
00209 private $TopicId;
00210 private $DB;
00211 private $DBFields;
00212 private $ErrorStatus;
00213
00214 # convenience function to supply parameters to Database->UpdateValue()
00215 private function UpdateValue($FieldName, $NewValue)
00216 {
00217 if ($this->ErrorStatus == Topic::OK)
00218 {
00219 return $this->DB->UpdateValue("Topics", $FieldName, $NewValue,
00220 "TopicId = '".$this->TopicId."'", $this->DBFields, TRUE);
00221 }
00222 else
00223 {
00224 return NULL;
00225 }
00226 }
00227 }
00228
00229 ?>