CWIS Developer Documentation
Message.php
Go to the documentation of this file.
1 <?PHP
2 #
3 # FILE: Message.php
4 #
5 # FUNCTIONS PROVIDED:
6 # Message->Message($MessageId)
7 # - constructor
8 # Message->MessageId()
9 # Message->ParentId()
10 # Message->ParentyType()
11 # Message->DatePosted()
12 # Message->PosterId()
13 # Message->Subject()
14 # Message->Body()
15 # - methods to retrieve resource attributes
16 #
17 # Part of the Collection Workflow Integration System (CWIS)
18 # Copyright 2012 Internet Scout Project
19 # http://scout.wisc.edu/
20 #
21 
27 class Message {
28 
29  # ---- PUBLIC INTERFACE --------------------------------------------------
30 
31  const OK = 0;
32  const NONEXISTENT = 1;
33 
34  const PARENTTYPE_TOPIC = 1;
36 
39 
47  function Message($MessageId = NULL)
48  {
49  $this->ErrorStatus = self::NONEXISTENT;
50  $this->DB = new Database();
51 
52  # no ID supplied
53  if (is_null($MessageId))
54  {
55  # add record to database with that ID
56  $this->DB->Query("INSERT INTO Messages (MessageId) VALUES (NULL)");
57  $this->DB->Query("SELECT LAST_INSERT_ID() AS Id FROM Messages");
58 
59  if ($this->DB->NumRowsSelected())
60  {
61  $this->MessageId = intval($this->DB->FetchField("Id"));
62  $this->ErrorStatus = self::OK;
63  }
64  }
65 
66  # ID supplied
67  else
68  {
69  $this->DB->Query("
70  SELECT * FROM Messages
71  WHERE MessageId = '".intval($MessageId)."'");
72 
73  if ($this->DB->NumRowsSelected())
74  {
75  # set attributes to values returned by database
76  $this->DBFields = $this->DB->FetchRow();
77  $this->MessageId = intval($this->DBFields["MessageId"]);
78  $this->ErrorStatus = Message::OK;
79  }
80  }
81  }
82 
86  function Delete()
87  {
88  if ($this->ErrorStatus == Message::OK)
89  {
90  $this->DB->Query("DELETE FROM Messages WHERE MessageId = ".$this->MessageId);
91  }
92  }
93 
98 
103  function MessageId() { return $this->MessageId; }
104 
109  function PosterName()
110  {
111  $PosterName = new User($this->DB, (int)$this->PosterId());
112  return $PosterName->Get("UserName");
113  }
114 
119  function PosterEmail()
120  {
121  $PosterName = new User($this->DB, (int)$this->PosterId());
122  return $PosterName->Get("EMail");
123  }
124 
129  function EditorId($NewValue = DB_NOVALUE) { return $this->UpdateValue("EditorId", $NewValue); }
130 
138  function ParentId($NewValue = DB_NOVALUE) { return $this->UpdateValue("ParentId", $NewValue); }
139 
147  function ParentType($NewValue = DB_NOVALUE) { return $this->UpdateValue("ParentType", $NewValue); }
148 
154  function DatePosted($NewValue = DB_NOVALUE) { return $this->UpdateValue("DatePosted", $NewValue); }
155 
161  function DateEdited($NewValue = DB_NOVALUE) { return $this->UpdateValue("DateEdited", $NewValue); }
162 
168  function PosterId($NewValue = DB_NOVALUE) { return $this->UpdateValue("PosterId", $NewValue); }
169 
175  function Subject($NewValue = DB_NOVALUE) { return $this->UpdateValue("Subject", $NewValue); }
176 
182  function Body($NewValue = DB_NOVALUE) { return $this->UpdateValue("Body", $NewValue); }
183 
188  function GetErrorStatus() { return $this->ErrorStatus; }
189 
192  # ---- PRIVATE INTERFACE -------------------------------------------------
193 
194  private $MessageId;
195  private $DB;
196  private $DBFields;
197  private $ErrorStatus;
198 
199  # convenience function to supply parameters to Database->UpdateValue()
200  private function UpdateValue($FieldName, $NewValue)
201  {
202  if ($this->ErrorStatus == Message::OK)
203  {
204  return $this->DB->UpdateValue("Messages", $FieldName, $NewValue,
205  "MessageId = '".$this->MessageId."'", $this->DBFields, TRUE);
206  }
207  else
208  {
209  return NULL;
210  }
211  }
212 }