CWIS Developer Documentation
Item.php
Go to the documentation of this file.
1 <?PHP
2 #
3 # FILE: Item.php
4 #
5 # Part of the ScoutLib application support library
6 # Copyright 2016 Edward Almasy and Internet Scout Research Group
7 # http://scout.wisc.edu
8 #
9 
13 abstract class Item
14 {
15 
16  # ---- PUBLIC INTERFACE --------------------------------------------------
17 
27  public function __construct($Id)
28  {
29  # set up database access values
30  $ClassName = get_class($this);
31  static::SetDatabaseAccessValues($ClassName);
32  $this->ItemIdColumnName = self::$ItemIdColumnNames[$ClassName];
33  $this->ItemNameColumnName = self::$ItemNameColumnNames[$ClassName];
34  $this->ItemTableName = self::$ItemTableNames[$ClassName];
35 
36  # normalize item ID
37  $this->Id = static::GetCanonicalId($Id);
38 
39  # load item info from database
40  $this->DB = new Database();
41  $this->DB->Query("SELECT * FROM `".$this->ItemTableName."`"
42  ." WHERE `".$this->ItemIdColumnName."` = "
43  .intval($this->Id));
44  $this->ValueCache = $this->DB->FetchRow();
45 
46  # error out if item not found in database
47  if ($this->ValueCache === FALSE)
48  {
49  throw new InvalidArgumentException("Attempt to load ".$ClassName
50  ." with unknown ID (".$Id.").");
51  }
52  }
53 
57  public function Destroy()
58  {
59  # delete item from database
60  $this->DB->Query("DELETE FROM `".$this->ItemTableName."`"
61  ." WHERE `".$this->ItemIdColumnName."` = ".intval($this->Id));
62  }
63 
69  public function Delete()
70  {
71  $this->Destroy();
72  }
73 
78  public function Id()
79  {
80  return $this->Id;
81  }
82 
88  public static function GetCanonicalId($Id)
89  {
90  return $Id;
91  }
92 
99  public function Name($NewValue = DB_NOVALUE)
100  {
101  return $this->UpdateValue("Name", $NewValue);
102  }
103 
110  public function DateCreated($NewValue = DB_NOVALUE)
111  {
112  return $this->UpdateDateValue("DateCreated", $NewValue);
113  }
114 
121  public function CreatedBy($NewValue = DB_NOVALUE)
122  {
123  return $this->UpdateValue("CreatedBy", $NewValue);
124  }
125 
132  public function DateLastModified($NewValue = DB_NOVALUE)
133  {
134  return $this->UpdateDateValue("DateLastModified", $NewValue);
135  }
136 
143  public function LastModifiedBy($NewValue = DB_NOVALUE)
144  {
145  return $this->UpdateValue("LastModifiedBy", $NewValue);
146  }
147 
155  public static function ItemExists($Id)
156  {
157  # check for NULL ID (usually used to indicate no value set)
158  if ($Id === NULL)
159  {
160  return FALSE;
161  }
162 
163  # set up database access values
164  $ClassName = get_called_class();
165  static::SetDatabaseAccessValues($ClassName);
166 
167  # build database query to check for item
168  $Query = "SELECT COUNT(*) AS ItemCount"
169  ." FROM ".self::$ItemTableNames[$ClassName]
170  ." WHERE ".self::$ItemIdColumnNames[$ClassName]." = ".intval($Id);
171 
172  # check for item and return result to caller
173  $DB = new Database();
174  $ItemCount = $DB->Query($Query, "ItemCount");
175  return ($ItemCount > 0) ? TRUE : FALSE;
176  }
177 
178 
179  # ---- PRIVATE INTERFACE -------------------------------------------------
180 
181  protected $DB;
182  protected $Id;
183  protected $ItemIdColumnName;
185  protected $ItemTableName;
186  protected $ValueCache = array();
187 
188  static protected $ItemIdColumnNames;
189  static protected $ItemNameColumnNames;
190  static protected $ItemTableNames;
191 
198  protected static function CreateWithValues($Values)
199  {
200  # set up database access values
201  $ClassName = get_called_class();
202  static::SetDatabaseAccessValues($ClassName);
203 
204  # set up query to add item to database
205  $Query = "INSERT INTO `".self::$ItemTableNames[$ClassName]."`";
206 
207  # add initial values to query if supplied
208  if (count($Values))
209  {
210  $Query .= " SET ";
211  foreach ($Values as $Column => $Value)
212  {
213  $Assignments[] = "`".$Column."` = '".addslashes($Value)."'";
214  }
215  $Query .= implode(", ", $Assignments);
216  }
217 
218  # add item to database
219  $DB = new Database();
220  $DB->Query($Query);
221 
222  # retrieve ID for newly-created item
223  $NewItemId = $DB->LastInsertId();
224 
225  # create item object
226  $NewItem = new $ClassName($NewItemId);
227 
228  # return new item object to caller
229  return $NewItem;
230  }
231 
238  static protected function SetDatabaseAccessValues($ClassName)
239  {
240  if (!isset(self::$ItemIdColumnNames[$ClassName]))
241  {
242  self::$ItemIdColumnNames[$ClassName] = $ClassName."Id";
243  self::$ItemNameColumnNames[$ClassName] = $ClassName."Name";
244  self::$ItemTableNames[$ClassName] = StdLib::Pluralize($ClassName);
245  }
246  }
247 
255  protected function UpdateValue($ColumnName, $NewValue = DB_NOVALUE)
256  {
257  return $this->DB->UpdateValue($this->ItemTableName, $ColumnName, $NewValue,
258  "`".$this->ItemIdColumnName."` = ".intval($this->Id),
259  $this->ValueCache);
260  }
261 
271  protected function UpdateDateValue($ColumnName, $NewValue = DB_NOVALUE)
272  {
273  if ($NewValue !== DB_NOVALUE)
274  {
275  $Date = new Date($NewValue);
276  $NewValue = $Date->FormattedForSql();
277  }
278  return $this->UpdateValue($ColumnName, $NewValue);
279  }
280 }
DateLastModified($NewValue=DB_NOVALUE)
Get/set when item was last modified.
Definition: Item.php:132
static $ItemIdColumnNames
Definition: Item.php:188
DateCreated($NewValue=DB_NOVALUE)
Get/set when item was created.
Definition: Item.php:110
Name($NewValue=DB_NOVALUE)
Get/set name of item.
Definition: Item.php:99
Id()
Get item ID.
Definition: Item.php:78
static CreateWithValues($Values)
Create a new item, using specified initial database values.
Definition: Item.php:198
UpdateValue($ColumnName, $NewValue=DB_NOVALUE)
Convenience function to supply parameters to Database::UpdateValue().
Definition: Item.php:255
SQL database abstraction object with smart query caching.
Definition: Database.php:22
CreatedBy($NewValue=DB_NOVALUE)
Get/set ID of user who created the item.
Definition: Item.php:121
static $ItemNameColumnNames
Definition: Item.php:189
$DB
Definition: Item.php:181
Delete()
Destroy item.
Definition: Item.php:69
LastModifiedBy($NewValue=DB_NOVALUE)
Get/set ID of user who last modified the item.
Definition: Item.php:143
static SetDatabaseAccessValues($ClassName)
Set the database access values (table name, ID column name, name column name) for specified class...
Definition: Item.php:238
static Pluralize($Word)
Pluralize an English word.
Definition: StdLib.php:105
Destroy()
Destroy item.
Definition: Item.php:57
Definition: Date.php:18
$ItemNameColumnName
Definition: Item.php:184
Common base class for persistent items store in database.
Definition: Item.php:13
const DB_NOVALUE
Definition: Database.php:1675
$ItemTableName
Definition: Item.php:185
$ValueCache
Definition: Item.php:186
__construct($Id)
Constructor, used to load existing items.
Definition: Item.php:27
$Id
Definition: Item.php:182
static ItemExists($Id)
Check whether an item exists with the specified ID.
Definition: Item.php:155
static $ItemTableNames
Definition: Item.php:190
$ItemIdColumnName
Definition: Item.php:183
UpdateDateValue($ColumnName, $NewValue=DB_NOVALUE)
Convenience function to supply parameters to Database::UpdateValue(), with preprocessing of new value...
Definition: Item.php:271
static GetCanonicalId($Id)
Normalize item ID to canonical form.
Definition: Item.php:88