DashboardNews.inc
8.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
<?php
require_once("$default->fileSystemRoot/lib/documentmanagement/PhysicalDocumentManager.inc");
/**
* $Id$
*
* Represents a dahsboard news item.
*
* Licensed under the GNU GPL. For full terms see the file COPYING.
*
* @version $Revision$
* @author Michael Joseph <michael@jamwarehouse.com>, Jam Warehouse (Pty) Ltd, South Africa
* @package lib.authentication
*/
class DashboardNews {
/**
* The primary key of the news item
*/
var $iId;
/**
* A synopsis of the news item
*/
var $sSynopsis;
/**
* The new item content
*/
var $sBody;
/**
* The rank of the news item
*/
var $iRank;
/**
* An accompanying image
*/
var $sImage;
/**
* The size of the image
*/
var $iImageSize;
/**
* The mime type id of the image
*/
var $iImageMimeTypeID;
/**
* Constructs a news item
*
* @param string the synopsis
* @param string the body
* @param integer the rank
*/
function DashboardNews($sNewSynopsis, $sNewBody, $iNewRank, $mImage = "") {
// primary key not set as document is not stored yet
$this->iId = -1;
$this->setSynopsis($sNewSynopsis);
$this->setBody($sNewBody);
$this->setRank($iNewRank);
// if we've been passed an array
if (is_array($mImage)) {
// then the image details are in it
$this->setImage($mImage["image"]);
$this->setImageSize($mImage["filesize"]);
$this->setImageMimeTypeID($mImage["mimetype"]);
} else {
if (file_exists($mImage)) {
// we've been passed a file, so read it in
$this->setImageFile($mImage);
}
}
}
/**
* Gets the new item primary key
*/
function getID(){
return $this->iId;
}
/**
* Gets the synopsis
*/
function getSynopsis(){
return $this->sSynopsis;
}
/**
* Sets the synopsis
*
* @param string the new synopsis
*/
function setSynopsis($sNewSynopsis){
$this->sSynopsis = $sNewSynopsis;
}
/**
* Gets the body
*/
function getBody(){
return $this->sBody;
}
/**
* Sets the body
*
* @param string the new news body
*/
function setBody($sNewBody){
$this->sBody = $sNewBody;
}
/**
* Gets the rank
*/
function getRank(){
return $this->iRank;
}
/**
* Sets the rank
*
* @param integer the new news item rank
*/
function setRank($iNewRank){
$this->iRank = $iNewRank;
}
/**
* Retrieves the image text
*
*/
function getImage() {
return $this->sImage;
}
/**
* Sets the image text
*
* @param string the new image text
*/
function setImage($sNewImageText) {
$this->sImage = $sNewImageText;
}
/**
* Retrieve the image size
*/
function getImageSize() {
return $this->iImageSize;
}
/**
* Set the image size
*
* @param integer the image size
*/
function setImageSize($iNewImageSize) {
$this->iImageSize = $iNewImageSize;
}
/**
* Retrieve the image mime type
*/
function getImageMimeTypeID() {
return $this->iImageMimeTypeID;
}
/**
* Set the image mime type
*
* @param integer the image mime type
*/
function setImageMimeTypeID($iNewMimeTypeID) {
$this->iImageMimeTypeID = $iNewMimeTypeID;
}
/**
* Displays the news item image
*/
function displayImage(){
header("Content-Type: " . PhysicalDocumentManager::getMimeTypeName($this->iImageMimeTypeID));
header("Content-Length: " . $this->iImageSize);
echo $this->sImage;
}
/**
* Sets the image information from a file
*
* @param string path to the image on the filesystem
*/
function setImageFile($sPathToImage){
if (file_exists($sPathToImage)) {
$aImage = $this->imageToString($sPathToImage);
$this->sImage = $aImage["image"];
$this->iImageSize = $aImage["filesize"];
$this->iImageMimeTypeID = $aImage["mimetypeid"];
}
}
/**
* Inserts the current new item into the database
*
* @return boolean true on successful update, false otherwise and set $_SESSION["errorMessage"]
*/
function create(){
global $default;
//if the id >= 0, then the object has already been created
if ($this->iId < 0) {
$sql = $default->db;
$result = $sql->query("INSERT INTO $default->owl_news_table (synopsis, body, rank, image, image_size, image_mime_type_id) " .
"VALUES ('" . addslashes($this->sSynopsis) . "', '" . addslashes($this->sBody) . "', $this->iRank, " .
"'" . addslashes($this->sImage) . "', $this->iImageSize, $this->iImageMimeTypeID)");
if ($result) {
//set the current news item primary key
$this->iId = $sql->insert_id();
return true;
}
return false;
}
return false;
}
/**
* Update the documents current values in the database
*
* @return boolean true on successful update, false otherwise
*/
function update(){
global $default;
if ($this->iId >= 0) {
$sql = $default->db;
$sQuery = "UPDATE " . $default->owl_news_table . " SET " .
"synopsis = '" . addslashes($this->sSynopsis) . "', " .
"body = '" . addslashes($this->sBody) . "', " .
"rank = $this->iRank, " .
"image = '" . addslashes($this->sImage) . "', " .
"image_size = $this->iImageSize " .
($this->iImageMimeTypeID ? ", image_mime_type_id = $this->iImageMimeTypeID " : "") .
"WHERE id = $this->iId";
$result = $sql->query($sQuery);
if ($result) {
return true;
}
return false;
}
return false;
}
/**
* Delete the current news item from the database. Set the primary key to -1
* on successful deletion
*
* @return boolean true and reset id to -1 on successful deletion, false otherwise
*/
function delete() {
global $default;
if ($this->iId >= 0) {
$sql = $default->db;
$result = $sql->query("DELETE FROM " . $default->owl_news_table . " WHERE id = $this->iId");
if ($result) {
$this->iId = -1;
return true;
}
return false;
}
return false;
}
/**
* Static function. Given a news item primary key will create
* a DashboardNews object and populate it with the corresponding
* database values
*
* @return DashboardNews populated DashboardNews object on success, false otherwise
*/
function & get($iNewsID) {
global $default;
$sql = $default->db;
$sql->query("SELECT * FROM $default->owl_news_table WHERE id = $iNewsID");
if ($sql->next_record()) {
$aImage = array( "image" => $sql->f("image"),
"filesize" => $sql->f("image_size"),
"mimetypeid" => $sql->f("image_mime_type_id") );
$oDashboardNews = & new DashboardNews(stripslashes($sql->f("synopsis")), stripslashes($sql->f("body")), $sql->f("rank"), $aImage);
$oDashboardNews->iId = $iNewsID;
return $oDashboardNews;
}
return false;
}
/**
* Static function
* Get a list of DashboardNews objects
*
* @param String Where clause (optional)
* @return Array array of DashboardNews objects, false otherwise
*/
function getList($sWhereClause = null) {
global $default;
$aDashboardNewsArray = array();
$sql = $default->db;
$result = $sql->query("SELECT * FROM " . $default->owl_news_table . (isset($sWhereClause) ? " WHERE " . $sWhereClause : ""));
if ($result) {
$iCount = 0;
while ($sql->next_record()) {
$oDashboardNews = & DashboardNews::get($sql->f("id"));
$aDashboardNewsArray[$iCount++] = $oDashboardNews;
}
return $aDocumentArray;
}
return false;
}
/**
* Reads in an image file as a string and returns it
*
* @param string path to the image file
* @return string the image as a string
*/
function imageToString($sImagePath) {
// check if the image exists
if (file_exists($sImagePath)) {
// read in the file
$fd = fopen ($sImagePath, "rb");
$sImageString = fread($fd, filesize($sImagePath));
fclose($fd);
// return the string
return array("image" => $sImageString, "filesize" => filesize($sImagePath), "mimetypeid" => PhysicalDocumentManager::getMimeTypeID(null, $sImagePath));
} else {
return false;
}
}
}