Subscription.inc
12 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
337
338
<?php
/*
* The contents of this file are subject to the KnowledgeTree Public
* License Version 1.1.2 ("License"); You may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.knowledgetree.com/KPL
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
* See the License for the specific language governing rights and
* limitations under the License.
*
* All copies of the Covered Code must include on each user interface screen:
* (i) the "Powered by KnowledgeTree" logo and
* (ii) the KnowledgeTree copyright notice
* in the same form as they appear in the distribution. See the License for
* requirements.
*
* The Original Code is: KnowledgeTree Open Source
*
* The Initial Developer of the Original Code is The Jam Warehouse Software
* (Pty) Ltd, trading as KnowledgeTree.
* Portions created by The Jam Warehouse Software (Pty) Ltd are Copyright
* (C) 2007 The Jam Warehouse Software (Pty) Ltd;
* All Rights Reserved.
* Contributor( s): ______________________________________
*
*/
require_once(KT_LIB_DIR . "/subscriptions/SubscriptionConstants.inc");
require_once(KT_LIB_DIR . "/foldermanagement/Folder.inc");
require_once(KT_LIB_DIR . "/documentmanagement/Document.inc");
require_once(KT_LIB_DIR . "/subscriptions/subscriptions.inc.php");
class Subscription extends KTEntity {
/**
* Primary key
*/
var $iId;
/**
* The ID of the user subscribed to the document
*/
var $iUserID;
/**
* The artefact type
*/
var $iSubscriptionType;
/**
* The ID of the artefact subscribed to
*/
var $iExternalID;
/**
* Whether this subscription is triggered
*/
var $bIsAlerted;
/**
* The subscription database table to use
*/
var $sTableName;
/**
* The subscription content id field name
*/
var $sIdFieldName;
/**
* Creates a new subscription object
*
* @param integer the user ID
* @param integer the external ID
* @param integer the subscription type
* @param bool whether alerted or not
*/
function Subscription($iUserID, $iExternalID, $iSubscriptionType, $bIsAlerted = false) {
global $default;
//id of -1 means that the object has not yet been stored in the database
$this->iId = -1;
$this->iUserID = $iUserID;
$this->iExternalID = $iExternalID;
$this->iSubscriptionType = $iSubscriptionType;
$this->bIsAlerted = $bIsAlerted;
$this->sTableName = Subscription::getTableName($iSubscriptionType);
$this->sIdFieldName = Subscription::getIdFieldName($iSubscriptionType);
}
/**
* Get the primary key of the current subscription object
*
* @return integer primary key of subscription
*/
function getID() {
return $this->iId;
}
/**
* Get the primary key of the subscription content.
*
* @return integer primary key of subscription content.
*/
function getExternalID() {
return $this->iExternalID;
}
/**
* Set the subscription content id
*
* @param integer new subscription content primary key
*/
function setExternalID($iNewValue) {
$this->iExternalID = $iNewValue;
}
/**
* Get the primary key of the user
*
* @return integer primary key of user
*/
function getUserID() {
return $this->iUserID;
}
/**
* Set the user id
*
* @param integer new user primary key
*/
function setUserID($iNewValue) {
$this->iUserID = $iNewValue;
}
/**
* Get the trigger status of this subscription
*
* @return boolean the trigger status of this subscription
*/
function getIsAlerted() {
return $this->bIsAlerted;
}
/**
* Set the trigger status of the subscription
*
* @param boolean new trigger status
*/
function setIsAlerted($iNewValue) {
$this->bIsAlerted = $iNewValue;
}
/**
* Returns the display path to the subscription content
*/
function getContentDisplayPath() {
if ($this->iSubscriptionType == SubscriptionConstants::subscriptionType("DocumentSubscription")) {
return Document::getDocumentDisplayPath($this->iExternalID);
} else if ($this->iSubscriptionType == SubscriptionConstants::subscriptionType("FolderSubscription")) {
return Folder::getFolderDisplayPath($this->iExternalID);
}
}
/**
* Returns the link to view the subscription content
*/
function getContentLink() {
// TODO: add subscription icon
if ($this->iSubscriptionType == SubscriptionConstants::subscriptionType("DocumentSubscription")) {
return generateControllerLink("viewDocument", "fDocumentId=$this->iExternalID", Document::getDocumentDisplayPath($this->iExternalID));
} else if ($this->iSubscriptionType == SubscriptionConstants::subscriptionType("FolderSubscription")) {
return generateControllerLink("browse", "fBrowseType=folder&fFolderId=$this->iExternalID", Folder::getFolderDisplayPath($this->iExternalID));
}
}
/**
* Returns the url to the subscription content
*/
function getContentUrl() {
if ($this->iSubscriptionType == SubscriptionConstants::subscriptionType("DocumentSubscription")) {
return generateControllerUrl("viewDocument", "fDocumentId=$this->iExternalID");
} else if ($this->iSubscriptionType == SubscriptionConstants::subscriptionType("FolderSubscription")) {
return generateControllerUrl("browse", "fBrowseType=folder&fFolderId=$this->iExternalID");
}
}
function isValid() {
if ($this->iSubscriptionType == SubscriptionConstants::subscriptionType("DocumentSubscription")) {
return !PEAR::isError(Document::get($this->iExternalID));
} else if ($this->iSubscriptionType == SubscriptionConstants::subscriptionType("FolderSubscription")) {
return !PEAR::isError(Folder::get($this->iExternalID));
}
}
function getAlertLink() {
global $default;
// TODO: add alerted icon
$sViewAlertParams = "fSubscriptionID=" . $this->iId . "&fSubscriptionType=" . $this->iSubscriptionType;
if ($this->iSubscriptionType == SubscriptionConstants::subscriptionType("DocumentSubscription")) {
return generateControllerLink("viewAlert", $sViewAlertParams, "<img src=\"" . imgSrc("widgets/subsc.gif") . "\" border=\"0\"/> " . Document::getDocumentDisplayPath($this->iExternalID));
} else if ($this->iSubscriptionType == SubscriptionConstants::subscriptionType("FolderSubscription")) {
return generateControllerLink("viewAlert", $sViewAlertParams, "<img src=\"" . imgSrc("widgets/subsc.gif") . "\" border=\"0\"/> " . Folder::getFolderDisplayPath($this->iExternalID));
}
}
function getSubscriptionTypeName() {
if ($this->iSubscriptionType == SubscriptionConstants::subscriptionType("DocumentSubscription")) {
return "document";
} else if ($this->iSubscriptionType == SubscriptionConstants::subscriptionType("FolderSubscription")) {
return "folder";
}
}
function _fieldValues () {
return array(
'user_id' => $this->iUserID,
$this->sIdFieldName => $this->iExternalID,
'is_alerted' => KTUtil::anyToBool($this->bIsAlerted),
);
}
function _table () {
return $this->sTableName;
}
/**
* Static function.
* Given a subscription primary key will create a subscription object and populate it with the corresponding
* database values
*
* @param integer primary key of subscription to get
* @param integer the type of subscription
* @return object subscription object on successful retrieval, false otherwise and set $_SESSION["errorMessage"]
*/
function & get($iSubscriptionID, $iSubscriptionType) {
// FIXME this is crack - need to start looking at multiple entities here.
$sQuery = "SELECT * FROM " . Subscription::getTableName($iSubscriptionType) . " WHERE id = ?";/*ok*/
$aParams = array($iSubscriptionID);
$res = DBUtil::getOneResult(array($sQuery, $aParams));
if (PEAR::isError($res)) {
return false; // return $res;
}
if (empty($res)) {
return false; // return PEAR::raiseError(_kt('No such Subscription ID'))
}
$oSubscription = new Subscription($res["user_id"],
$res[Subscription::getIdFieldName($iSubscriptionType)],
$iSubscriptionType,
$res["is_alerted"]);
$oSubscription->iId = $iSubscriptionID;
return $oSubscription;
}
/**
* Static function.
* Given a subscription's values will create a subscription object and populate it with the corresponding
* primary key
*
* @param integer the user ID
* @param integer the external ID
* @param integer the type of subscription
* @return object subscription object on successful retrieval, false otherwise and set $_SESSION["errorMessage"]
*/
function getByIDs($iUserID, $iExternalID, $iSubscriptionType) {
$sQuery = "SELECT * FROM " . Subscription::getTableName($iSubscriptionType) . " WHERE " . Subscription::getIdFieldName($iSubscriptionType) . " = ? AND user_id = ?";
$aParams = array($iExternalID, $iUserID);
$res = DBUtil::getOneResult(array($sQuery, $aParams));
if (PEAR::isError($res)) {
return false; // return $res;
}
if (empty($res)) {
return false; // return PEAR::raiseError(_kt('No such Subscription ID'))
}
$oSubscription = new Subscription($res["user_id"],
$res[Subscription::getIdFieldName($iSubscriptionType)],
$iSubscriptionType,
$res["is_alerted"]);
$oSubscription->iId = $res['id'];
return $oSubscription;
}
/**
* Checks if a given subscription already exists using the external and user ids
*
* @param integer the user ID
* @param integer the external ID
* @param integer the subscription type
* @return true if the document subscription exists, false otherwise
*/
function exists($iUserID, $iExternalID, $iSubscriptionType) {
$sQuery = "SELECT count(*) AS `subcount` FROM " . Subscription::getTableName($iSubscriptionType) . " WHERE " . Subscription::getIdFieldName($iSubscriptionType) . " = ? AND user_id = ?";
$aParams = array($iExternalID, $iUserID);
$res = DBUtil::getOneResultKey(array($sQuery, $aParams), 'subcount');
if (PEAR::isError($res)) {
return false;
} else {
return ($res == 1);
}
}
/**
* Returns the correct table name for the subscription type
*
* @param integer the subscription type
* @return string the subscription table name to use
*/
function getTableName($iSubscriptionType) {
global $default;
if ($iSubscriptionType == SubscriptionConstants::subscriptionType("DocumentSubscription")) {
return $default->document_subscriptions_table;
} else if($iSubscriptionType == SubscriptionConstants::subscriptionType("FolderSubscription")) {
return $default->folder_subscriptions_table;
}
}
/**
* Returns the correct id field name for the subscription type
*
* @param integer the subscription type
* @return string the subscription id field name to use
*/
function getIdFieldName($iSubscriptionType) {
if ($iSubscriptionType == SubscriptionEvent::subTypes("Document")) {
return "document_id";
} else if($iSubscriptionType == SubscriptionEvent::subTypes("Folder")) {
return "folder_id";
}
}
}
?>