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  $this->DB->Query("Select * from Messages where ParentId = ".
120  $this->TopicId.
121  " AND ParentType = 1 ORDER BY DatePosted DESC");
122 
123  # delete messages associated with this topic
124  while ($Entry = $this->DB->FetchRow())
125  {
126  $Messages[$Entry["MessageId"]] = new Message($Entry["MessageId"]);
127  }
128 
129  return $Messages;
130  }
131 
136  function TopicId() { return $this->TopicId; }
137 
142  function CreatorName()
143  {
144  $CreatorName = new User($this->DB, (int)$this->CreatorId());
145  return $CreatorName->Get("UserName");
146  }
147 
152  function CreatorEmail()
153  {
154  $CreatorName = new User($this->DB, (int)$this->CreatorId());
155  return $CreatorName->Get("EMail");
156  }
157 
163  function ForumId($NewValue = DB_NOVALUE) { return $this->UpdateValue("ForumId", $NewValue); }
164 
170  function CreatorId($NewValue = DB_NOVALUE) { return $this->UpdateValue("CreatorId", $NewValue); }
171 
177  function DateCreated($NewValue = DB_NOVALUE) { return $this->UpdateValue("DateCreated", $NewValue); }
178 
184  function TopicName($NewValue = DB_NOVALUE) { return $this->UpdateValue("TopicName", $NewValue); }
185 
191  function ViewCount($NewValue = DB_NOVALUE) { return $this->UpdateValue("ViewCount", $NewValue); }
192 
198  function MessageCount($NewValue = DB_NOVALUE) { return $this->UpdateValue("MessageCount", $NewValue); }
199 
204  function GetErrorStatus() { return $this->ErrorStatus; }
205 
208  # ---- PRIVATE INTERFACE -------------------------------------------------
209 
210  private $TopicId;
211  private $DB;
212  private $DBFields;
213  private $ErrorStatus;
214 
215  # convenience function to supply parameters to Database->UpdateValue()
216  private function UpdateValue($FieldName, $NewValue)
217  {
218  if ($this->ErrorStatus == Topic::OK)
219  {
220  return $this->DB->UpdateValue("Topics", $FieldName, $NewValue,
221  "TopicId = '".$this->TopicId."'", $this->DBFields, TRUE);
222  }
223  else
224  {
225  return NULL;
226  }
227  }
228 }
229 
230 ?>