CWIS Developer Documentation

- Home
- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

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
26 public function __construct($Id)
27 {
28 # set up database access values
29 $ClassName = get_class($this);
30 static::SetDatabaseAccessValues($ClassName);
31 $this->ItemIdColumnName = self::$ItemIdColumnNames[$ClassName];
32 $this->ItemNameColumnName = self::$ItemNameColumnNames[$ClassName];
33 $this->ItemTableName = self::$ItemTableNames[$ClassName];
34
35 # normalize item ID
36 $this->Id = static::GetCanonicalId($Id);
37
38 # load item info from database
39 $this->DB = new Database();
40 $this->DB->Query("SELECT * FROM `".$this->ItemTableName."`"
41 ." WHERE `".$this->ItemIdColumnName."` = "
42 .intval($this->Id));
43 $this->ValueCache = $this->DB->FetchRow();
44
45 # error out if item not found in database
46 if ($this->ValueCache === FALSE)
47 {
48 throw new Exception("Attempt to load ".$ClassName
49 ." with unknown ID (".$Id.").");
50 }
51 }
52
56 public function Delete()
57 {
58 # delete item from database
59 $this->DB->Query("DELETE FROM `".$this->ItemTableName."`"
60 ." WHERE `".$this->ItemIdColumnName."` = ".intval($this->Id));
61 }
62
67 public function Id()
68 {
69 return $this->Id;
70 }
71
77 public static function GetCanonicalId($Id)
78 {
79 return $Id;
80 }
81
89 public static function ItemExists($Id)
90 {
91 # check for NULL ID
92 if ($Id === NULL)
93 {
94 return FALSE;
95 }
96
97 # set up database access values
98 $ClassName = get_called_class();
99 static::SetDatabaseAccessValues($ClassName);
100
101 # build database query to check for item
102 $Query = "SELECT COUNT(*) AS ItemCount"
103 ." FROM ".self::$ItemTableNames[$ClassName]
104 ." WHERE ".self::$ItemIdColumnNames[$ClassName]." = ".intval($Id);
105
106 # check for item and return result to caller
107 $DB = new Database();
108 $ItemCount = $DB->Query($Query, "ItemCount");
109 return ($ItemCount > 0) ? TRUE : FALSE;
110 }
111
112
113 # ---- PRIVATE INTERFACE -------------------------------------------------
114
115 protected $DB;
116 protected $Id;
117 protected $ItemIdColumnName;
118 protected $ItemNameColumnName;
119 protected $ItemTableName;
120 protected $ValueCache = array();
121
122 static protected $ItemIdColumnNames;
123 static protected $ItemNameColumnNames;
124 static protected $ItemTableNames;
125
132 protected static function CreateWithValues($Values)
133 {
134 # set up database access values
135 $ClassName = get_called_class();
136 static::SetDatabaseAccessValues($ClassName);
137
138 # set up query to add item to database
139 $Query = "INSERT INTO `".self::$ItemTableNames[$ClassName]."`";
140
141 # add initial values to query if supplied
142 if (count($Values))
143 {
144 $Query .= " SET ";
145 foreach ($Values as $Column => $Value)
146 {
147 $Assignments[] = "`".$Column."` = '".addslashes($Value)."'";
148 }
149 $Query .= implode(", ", $Assignments);
150 }
151
152 # add item to database
153 $DB = new Database();
154 $DB->Query($Query);
155
156 # retrieve ID for newly-created item
157 $NewItemId = $DB->LastInsertId();
158
159 # create item object
160 $NewItem = new $ClassName($NewItemId);
161
162 # return new item object to caller
163 return $NewItem;
164 }
165
172 static protected function SetDatabaseAccessValues($ClassName)
173 {
174 if (!isset(self::$ItemIdColumnNames[$ClassName]))
175 {
176 self::$ItemIdColumnNames[$ClassName] = $ClassName."Id";
177 self::$ItemNameColumnNames[$ClassName] = $ClassName."Name";
178 self::$ItemTableNames[$ClassName] = StdLib::Pluralize($ClassName);
179 }
180 }
181
189 protected function UpdateValue($ColumnName, $NewValue = DB_NOVALUE)
190 {
191 return $this->DB->UpdateValue($this->ItemTableName, $ColumnName, $NewValue,
192 "`".$this->ItemIdColumnName."` = ".intval($this->Id),
193 $this->ValueCache);
194 }
195 }
Item\$ItemIdColumnNames
static $ItemIdColumnNames
Definition: Item.php:122
Item\Id
Id()
Get item ID.
Definition: Item.php:67
Item\CreateWithValues
static CreateWithValues($Values)
Create a new item, using specified initial database values.
Definition: Item.php:132
Item\UpdateValue
UpdateValue($ColumnName, $NewValue=DB_NOVALUE)
Convenience function to supply parameters to Database->UpdateValue().
Definition: Item.php:189
Database
SQL database abstraction object with smart query caching.
Definition: Database.php:22
Item\$ItemNameColumnNames
static $ItemNameColumnNames
Definition: Item.php:123
Item\$DB
$DB
Definition: Item.php:115
Item\Delete
Delete()
Destroy item.
Definition: Item.php:56
Item\SetDatabaseAccessValues
static SetDatabaseAccessValues($ClassName)
Set the database access values (table name, ID column name, name column name) for specified class...
Definition: Item.php:172
StdLib\Pluralize
static Pluralize($Word)
Pluralize an English word.
Definition: StdLib.php:105
Item\$ItemNameColumnName
$ItemNameColumnName
Definition: Item.php:118
PHP
PHP
Definition: RSSClient--Test.php:12
Item
Common base class for persistent items store in database.
Definition: Item.php:13
DB_NOVALUE
const DB_NOVALUE
Definition: Database.php:1541
Item\$ItemTableName
$ItemTableName
Definition: Item.php:119
Item\$ValueCache
$ValueCache
Definition: Item.php:120
Item\__construct
__construct($Id)
Constructor, used to load existing items.
Definition: Item.php:26
Item\$Id
$Id
Definition: Item.php:116
Item\ItemExists
static ItemExists($Id)
Check whether an item exists with the specified ID.
Definition: Item.php:89
Item\$ItemTableNames
static $ItemTableNames
Definition: Item.php:124
Item\$ItemIdColumnName
$ItemIdColumnName
Definition: Item.php:117
Item\GetCanonicalId
static GetCanonicalId($Id)
Normalize item ID to canonical form.
Definition: Item.php:77