Search:

CWIS Developers Documentation

  • Main Page
  • Classes
  • Files
  • File List
  • File Members

Topic.php

Go to the documentation of this file.
00001 <?PHP
00002 #
00003 #   FILE:  Topic.php
00004 #
00005 #   FUNCTIONS PROVIDED:
00006 #       Topic->Topic($TopicId)
00007 #           - constructor
00008 #       Topic->TopicId()
00009 #       Topic->ForumId()
00010 #       Topic->CreatorId()
00011 #       Topic->DateCreated()
00012 #       Topic->TopicName()
00013 #       Topic->ViewCount()
00014 #       Topic->MessageCount()
00015 #       Topic->Delete()
00016 #           - methods to retrieve resource attributes
00017 #
00018 #   Copyright 2011 Internet Scout Project
00019 #   http://scout.wisc.edu/
00020 #
00021 
00027 class Topic {
00028 
00029     # ---- PUBLIC INTERFACE --------------------------------------------------
00030 
00031 
00032     # Error codes for the TOPIC
00033     const OK = 0;
00034     const NONEXISTENT = 1;
00035 
00038 
00046     function Topic($TopicId = NULL )
00047     {
00048         $this->ErrorStatus = Topic::OK;
00049         # locate class in database
00050         $this->DB = new SPTDatabase();
00051         $DB = $this->DB;
00052         # if ID supplied
00053         if ($TopicId !== NULL)
00054         {
00055             $this->TopicId = intval($TopicId);
00056             $DB->Query("SELECT * FROM Topics WHERE TopicId = ".$this->TopicId);
00057 
00058             # if row was loaded
00059             if ($DB->NumRowsSelected() > 0)
00060             {
00061                 # set attributes to values returned by database
00062                 $this->DBFields = $DB->FetchRow();
00063             }
00064             else
00065             {
00066                 $this->ErrorStatus = Topic::NONEXISTENT;
00067             }
00068         }
00069         elseif (func_num_args()==0)
00070         {
00071             # add record to database with that ID
00072             $DB->Query("INSERT INTO Topics (TopicId) VALUES (NULL)");
00073             $this->TopicId = $DB->Query("SELECT LAST_INSERT_ID()"
00074                     ." AS TopicId FROM Topics", "TopicId");
00075         }
00076         else
00077         {
00078             $this->ErrorStatus = Topic::NONEXISTENT;
00079         }
00080     }
00081 
00085     function Delete()
00086     {
00087         if ($this->ErrorStatus == Topic::OK)
00088         {
00089             # decrement the topic count for the parent forum
00090             $Forum = new Forum($this->ForumId());
00091             $Forum->TopicCount($Forum->TopicCount() - 1);
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 
00129         return $Messages;
00130     }
00131 
00136     function TopicId()        {  return $this->TopicId;  }
00137 
00142     function CreatorName()
00143     {
00144         $CreatorName = new User($this->DB, (int)$this->CreatorId());
00145         return $CreatorName->Get("UserName");
00146     }
00147 
00152     function CreatorEmail()
00153     {
00154         $CreatorName = new User($this->DB, (int)$this->CreatorId());
00155         return $CreatorName->Get("EMail");
00156     }
00157 
00163     function ForumId($NewValue = DB_NOVALUE) {  return $this->UpdateValue("ForumId", $NewValue);  }
00164 
00170     function CreatorId($NewValue = DB_NOVALUE) {  return $this->UpdateValue("CreatorId", $NewValue);  }
00171 
00177     function DateCreated($NewValue = DB_NOVALUE) {  return $this->UpdateValue("DateCreated", $NewValue);  }
00178 
00184     function TopicName($NewValue = DB_NOVALUE) {  return $this->UpdateValue("TopicName", $NewValue);  }
00185 
00191     function ViewCount($NewValue = DB_NOVALUE) {  return $this->UpdateValue("ViewCount", $NewValue);  }
00192 
00198     function MessageCount($NewValue = DB_NOVALUE) {  return $this->UpdateValue("MessageCount", $NewValue);  }
00199 
00204     function GetErrorStatus() { return $this->ErrorStatus; }
00205 
00208     # ---- PRIVATE INTERFACE -------------------------------------------------
00209 
00210     private $TopicId;
00211     private $DB;
00212     private $DBFields;
00213     private $ErrorStatus;
00214 
00215     # convenience function to supply parameters to Database->UpdateValue()
00216     private function UpdateValue($FieldName, $NewValue)
00217     {
00218         if ($this->ErrorStatus == Topic::OK)
00219         {
00220             return $this->DB->UpdateValue("Topics", $FieldName, $NewValue,
00221                                           "TopicId = '".$this->TopicId."'", $this->DBFields, TRUE);
00222         }
00223         else
00224         {
00225             return NULL;
00226         }
00227     }
00228 }
00229 
00230 ?>

CWIS logo doxygen
Copyright 2010 Internet Scout