CWIS Developer Documentation
Topic.php
Go to the documentation of this file.
1 <?PHP
2 #
3 # FILE: Topic.php
4 #
5 # FUNCTIONS PROVIDED:
6 # Topic->Topic($TopicId)
7 # - constructor
8 # Topic->TopicId()
9 # Topic->ForumId()
10 # Topic->CreatorId()
11 # Topic->DateCreated()
12 # Topic->TopicName()
13 # Topic->ViewCount()
14 # Topic->MessageCount()
15 # Topic->Delete()
16 # - methods to retrieve resource attributes
17 #
18 # Copyright 2011 Internet Scout Project
19 # http://scout.wisc.edu/
20 #
21 
27 class Topic {
28 
29  # ---- PUBLIC INTERFACE --------------------------------------------------
30 
31 
32  # Error codes for the TOPIC
33  const OK = 0;
34  const NONEXISTENT = 1;
35 
38 
46  function Topic($TopicId = NULL )
47  {
48  $this->ErrorStatus = Topic::OK;
49  # locate class in database
50  $this->DB = new Database();
51  $DB = $this->DB;
52  # if ID supplied
53  if ($TopicId !== NULL)
54  {
55  $this->TopicId = intval($TopicId);
56  $DB->Query("SELECT * FROM Topics WHERE TopicId = ".$this->TopicId);
57 
58  # if row was loaded
59  if ($DB->NumRowsSelected() > 0)
60  {
61  # set attributes to values returned by database
62  $this->DBFields = $DB->FetchRow();
63  }
64  else
65  {
66  $this->ErrorStatus = Topic::NONEXISTENT;
67  }
68  }
69  elseif (func_num_args()==0)
70  {
71  # add record to database with that ID
72  $DB->Query("INSERT INTO Topics (TopicId) VALUES (NULL)");
73  $this->TopicId = $DB->Query("SELECT LAST_INSERT_ID()"
74  ." AS TopicId FROM Topics", "TopicId");
75  }
76  else
77  {
78  $this->ErrorStatus = Topic::NONEXISTENT;
79  }
80  }
81 
85  function Delete()
86  {
87  if ($this->ErrorStatus == Topic::OK)
88  {
89  # decrement the topic count for the parent forum
90  $Forum = new Forum($this->ForumId());
91  $Forum->TopicCount($Forum->TopicCount() - 1);
92 
93  $this->DB->Query("Select * from Messages where ParentId = ".
94  $this->TopicId." AND ParentType = 1");
95 
96  # delete messages associated with this topic
97  while ($Entry = $this->DB->FetchRow())
98  {
99  $Message = new Message($Entry["MessageId"]);
100  $Message->Delete();
101  }
102  $this->DB->Query("DELETE FROM Topics WHERE TopicId=".$this->TopicId);
103  }
104  }
105 
110 
115  function GetMessageList()
116  {
117  $Messages = array();
118 
119  # query for messages associated with this topic
120  $this->DB->Query("
121  SELECT * FROM Messages
122  WHERE ParentId = '".addslashes($this->TopicId)."'
123  AND ParentType = '1'
124  ORDER BY DatePosted ASC");
125 
126  # create Message objects from the results
127  while (FALSE !== ($Row = $this->DB->FetchRow()))
128  {
129  $Messages[$Row["MessageId"]] = new Message($Row["MessageId"]);
130  }
131 
132  return $Messages;
133  }
134 
139  function TopicId() { return $this->TopicId; }
140 
145  function CreatorName()
146  {
147  $CreatorName = new User($this->DB, (int)$this->CreatorId());
148  return $CreatorName->Get("UserName");
149  }
150 
155  function CreatorEmail()
156  {
157  $CreatorName = new User($this->DB, (int)$this->CreatorId());
158  return $CreatorName->Get("EMail");
159  }
160 
166  function ForumId($NewValue = DB_NOVALUE) { return $this->UpdateValue("ForumId", $NewValue); }
167 
173  function CreatorId($NewValue = DB_NOVALUE) { return $this->UpdateValue("CreatorId", $NewValue); }
174 
180  function DateCreated($NewValue = DB_NOVALUE) { return $this->UpdateValue("DateCreated", $NewValue); }
181 
187  function TopicName($NewValue = DB_NOVALUE) { return $this->UpdateValue("TopicName", $NewValue); }
188 
194  function ViewCount($NewValue = DB_NOVALUE) { return $this->UpdateValue("ViewCount", $NewValue); }
195 
201  function MessageCount($NewValue = DB_NOVALUE) { return $this->UpdateValue("MessageCount", $NewValue); }
202 
207  function GetErrorStatus() { return $this->ErrorStatus; }
208 
211  # ---- PRIVATE INTERFACE -------------------------------------------------
212 
213  private $TopicId;
214  private $DB;
215  private $DBFields;
216  private $ErrorStatus;
217 
218  # convenience function to supply parameters to Database->UpdateValue()
219  private function UpdateValue($FieldName, $NewValue)
220  {
221  if ($this->ErrorStatus == Topic::OK)
222  {
223  return $this->DB->UpdateValue("Topics", $FieldName, $NewValue,
224  "TopicId = '".$this->TopicId."'", $this->DBFields, TRUE);
225  }
226  else
227  {
228  return NULL;
229  }
230  }
231 }
232 
233 ?>