Message.php

Go to the documentation of this file.
00001 <?PHP
00002 #
00003 #   FILE:  Message.php
00004 #
00005 #   FUNCTIONS PROVIDED:
00006 #       Message->Message($MessageId)
00007 #           - constructor
00008 #       Message->MessageId()
00009 #       Message->ParentId()
00010 #       Message->ParentyType()
00011 #       Message->DatePosted()
00012 #       Message->PosterId()
00013 #       Message->Subject()
00014 #       Message->Body()
00015 #           - methods to retrieve resource attributes
00016 #
00017 #   Part of the Collection Workflow Integration System (CWIS)
00018 #   Copyright 2012 Internet Scout Project
00019 #   http://scout.wisc.edu/
00020 #
00021 
00027 class Message {
00028 
00029     # ---- PUBLIC INTERFACE --------------------------------------------------
00030 
00031     const OK = 0;
00032     const NONEXISTENT = 1;
00033 
00034     const PARENTTYPE_TOPIC = 1;
00035     const PARENTTYPE_RESOURCE = 2;
00036 
00039 
00047     function Message($MessageId = NULL)
00048     {
00049         $this->ErrorStatus = self::NONEXISTENT;
00050         $this->DB = new Database();
00051 
00052         # no ID supplied
00053         if (is_null($MessageId))
00054         {
00055             # add record to database with that ID
00056             $this->DB->Query("INSERT INTO Messages (MessageId) VALUES (NULL)");
00057             $this->DB->Query("SELECT LAST_INSERT_ID() AS Id FROM Messages");
00058 
00059             if ($this->DB->NumRowsSelected())
00060             {
00061                 $this->MessageId = intval($this->DB->FetchField("Id"));
00062                 $this->ErrorStatus = self::OK;
00063             }
00064         }
00065 
00066         # ID supplied
00067         else
00068         {
00069             $this->DB->Query("
00070                 SELECT * FROM Messages
00071                 WHERE MessageId = '".intval($MessageId)."'");
00072 
00073             if ($this->DB->NumRowsSelected())
00074             {
00075                 # set attributes to values returned by database
00076                 $this->DBFields = $this->DB->FetchRow();
00077                 $this->MessageId = intval($this->DBFields["MessageId"]);
00078                 $this->ErrorStatus = Message::OK;
00079             }
00080         }
00081     }
00082 
00086     function Delete()
00087     {
00088         if ($this->ErrorStatus == Message::OK)
00089         {
00090             $this->DB->Query("DELETE FROM Messages WHERE MessageId = ".$this->MessageId);
00091         }
00092     }
00093 
00098 
00103     function MessageId()    {  return $this->MessageId;  }
00104 
00109     function PosterName()
00110     {
00111         $PosterName = new User($this->DB, (int)$this->PosterId());
00112         return $PosterName->Get("UserName");
00113     }
00114 
00119     function PosterEmail()
00120     {
00121         $PosterName = new User($this->DB, (int)$this->PosterId());
00122         return $PosterName->Get("EMail");
00123     }
00124 
00129     function EditorId($NewValue = DB_NOVALUE) {  return $this->UpdateValue("EditorId", $NewValue);  }
00130 
00138     function ParentId($NewValue = DB_NOVALUE) {  return $this->UpdateValue("ParentId", $NewValue);  }
00139 
00147     function ParentType($NewValue = DB_NOVALUE) {  return $this->UpdateValue("ParentType", $NewValue);  }
00148 
00154     function DatePosted($NewValue = DB_NOVALUE) {  return $this->UpdateValue("DatePosted", $NewValue);  }
00155 
00161     function DateEdited($NewValue = DB_NOVALUE) {  return $this->UpdateValue("DateEdited", $NewValue);  }
00162 
00168     function PosterId($NewValue = DB_NOVALUE) {  return $this->UpdateValue("PosterId", $NewValue);  }
00169 
00175     function Subject($NewValue = DB_NOVALUE) {  return $this->UpdateValue("Subject", $NewValue);  }
00176 
00182     function Body($NewValue = DB_NOVALUE) {  return $this->UpdateValue("Body", $NewValue);  }
00183 
00188     function GetErrorStatus() { return $this->ErrorStatus; }
00189 
00192     # ---- PRIVATE INTERFACE -------------------------------------------------
00193 
00194     private $MessageId;
00195     private $DB;
00196     private $DBFields;
00197     private $ErrorStatus;
00198 
00199     # convenience function to supply parameters to Database->UpdateValue()
00200     private function UpdateValue($FieldName, $NewValue)
00201     {
00202         if ($this->ErrorStatus == Message::OK)
00203         {
00204             return $this->DB->UpdateValue("Messages", $FieldName, $NewValue,
00205                                           "MessageId = '".$this->MessageId."'", $this->DBFields, TRUE);
00206         }
00207         else
00208         {
00209             return NULL;
00210         }
00211     }
00212 }