DocumentBrowser.inc
15.1 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
<?php
require_once("$default->fileSystemRoot/lib/security/permission.inc");
require_once("$default->fileSystemRoot/lib/users/User.inc");
require_once("$default->fileSystemRoot/lib/documentmanagement/Document.inc");
require_once("$default->fileSystemRoot/lib/foldermanagement/Folder.inc");
/**
*
* $Id$
*
* Contains document browsing business logic.
*
* 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.documentmanagement
*/
class DocumentBrowser {
/**
* Browse the documents by folder
*
* @param int the folder to browse from
* @return array folders and documents
*/
function browseByFolder($folderID = -1) {
global $default;
// instantiate and initialise
$results = array();
$sql = $default->db;
// no folder specified, so depending on the users groups, resolve to the right folderID
if ($folderID == -1) {
// no folder specified, so start at the root folder
// look up this users unit
$unitID = User::getUnitID($_SESSION["userID"]);
if ($unitID) {
// if the user is in a unit, start at the unit's root folder
// lookup the unit name
$unitName = lookupField($default->owl_units_table, "name", "id", $unitID);
$default->log->debug("DocumentBrowser::browseByFolder: unitID=$unitID; unitName=$unitName");
// the unit root folder has the same name as the unit
// FIXME: dodgy i know, but its easy
$unitRootFolderName = $unitName;
// now lookup the folderID
$folderID = lookupID($default->owl_folders_table, "name", $unitRootFolderName);
$default->log->info("DocumentBrowser::browseByFolder looked up unit root folderID=$folderID; unit root foldername=$unitRootFolderName");
} else {
// else just start at the root
$folderID = lookupID($default->owl_folders_table, "parent_id", 0);
$default->log->info("DocumentBrowser::browseByFolder: starting at docroot folderID=$folderID");
}
} else {
$default->log->info("DocumentBrowser::browseByFolder starting at passed in folderID=$folderID");
}
$default->log->debug("DocumentBrowser::browseByFolder: folderID=$folderID");
// get the folder
$results["folders"][] = & Folder::get($folderID);
if ($results["folders"][0]) {
// now find all the child folders relative to this one
// FIXME: in the same unit?
$folderQuery = "SELECT id from $default->owl_folders_table WHERE parent_id=" . $folderID . " ORDER BY name ASC";
$default->log->debug("DocumentBrowser::browseByFolder child folder query=$folderQuery");
if ($sql->query($folderQuery)) {
while ($sql->next_record()) {
// add the child folders to the array
$results["folders"][] = & Folder::get($sql->f("id"));
}
}
// create query to retrieve documents in this folder
$documentQuery = "SELECT id FROM $default->owl_documents_table WHERE folder_id=$folderID ORDER BY name ASC";
$default->log->debug("DocumentBrowser::browseByFolder about to execute $documentQuery");
if ($sql->query($documentQuery)) {
while ($sql->next_record()) {
// check permissions
if (Permission::userHasDocumentReadPermission($sql->f("id"))) {
// add documents to array
// set file attributes
$results["documents"][] = & Document::get($sql->f("id"));
} else {
$default->log->debug("DocumentBrowser::browseByFolder: read permission denied for document id=" . $sql->f("id"));
}
}
} else {
$_SESSION["errorMessage"] = "documents table select failed";
}
return $results;
} else {
return false;
}
}
/**
* Browse the documents by category
*
* @param string the category to browse
*/
function browseByCategory($category = "") {
global $default;
// TODO: add this to default inserts
$categoryField = "Category";
$results = array();
$sql = $default->db;
// lookup document_fields id for category
$categoryFieldID = lookupID($default->owl_fields_table, "name", "$categoryField");
$default->log->debug("DocumentBrowser::browseByCategory categoryFieldID=$categoryFieldID");
if ($category == "") {
$default->log->debug("DocumentBrowser::browseByCategory no category supplied, returning list");
// no category value supplied, so return a list of categories
// set the first value to "categories"
$results["categories"][] = "Categories";
// get a list of category values
$query = "select distinct value from $default->owl_document_fields_table where document_field_id=$categoryFieldID ORDER BY value ASC";
$default->log->debug("DocumentBrowser::browseByCategory category listing query=$query");
$sql->query($query);
// loop through resultset, build array and return
while ($sql->next_record()) {
$results["categories"][] = $sql->f("value");
}
// its ok if we return an empty array- the UI's responsibility to check and print an error
return $results;
} else {
$results["categories"][] = $category;
$default->log->debug("DocumentBrowser::browseByCategory get documents for category $category");
// we have a category to use, so find all the documents
// with this category value
$query = "select document_id from $default->owl_document_fields_table where document_field_id = $categoryFieldID " .
"and value='$category' ORDER BY value ASC";
$default->log->debug("DocumentBrowser::browseByCategory documents in category query=$query");
$sql->query($query);
// loop through resultset and add to array
while ($sql->next_record()) {
// check permissions
if (Permission::userHasDocumentReadPermission($sql->f("document_id"))) {
$results["documents"][] = & Document::get($sql->f("document_id"));
} else {
$default->log->debug("DocumentBrowser::browseByCategory permission denied for documentID=" . $sql->f("document_id"));
}
}
$default->log->debug("DocumentBrowser::browseByCategory results=" . arrayToString($results));
return $results;
}
}
/**
* Browse the documents by document type
*
* @param int the document type ID to browse
*/
function browseByDocumentType($documentTypeID = -1) {
global $default;
$results = array();
$sql = $default->db;
if ($documentTypeID == -1) {
// no category value supplied, so return a list of categories
// set the first value to "categories"
$results["documentTypes"][] = array("name" => "Document Types");
// return a list of document types
$query = "select * from $default->owl_document_types_table";
$sql->query($query);
while ($sql->next_record()) {
$results["documentTypes"][] = array ("id" => $sql->f("id"), "name" => $sql->f("name"));
}
return $results;
} else {
// lookup document type name from the passed in id
$documentTypeName = lookupField($default->owl_document_types_table, "name", "id", $documentTypeID);
$results["documentTypes"][] = array("id" => $documentTypeID, "name" => $documentTypeName);
// find all documents with this document type
$query = "SELECT DISTINCT id FROM $default->owl_documents_table WHERE document_type_id=$documentTypeID";
$default->log->debug("DocumentBrowser::browseByDocumentType: documents from doc type query=$query");
// loop through resultset and build array of documentIDs
$sql->query($query);
while ($sql->next_record()) {
// check permission
if (Permission::userHasDocumentReadPermission($sql->f("id"))) {
$results["documents"][] = & Document::get($sql->f("id"));
} else {
$default->log->debug("DocumentBrowser::browseByDocumentType: permission denied for documentID=" . $sql->f("document_id"));
}
}
$default->log->debug("DocumentBrowser::browseByDocumentType: results=" . arrayToString($results));
return $results;
}
}
/**
* Checks whether the required group is one of the
* groups in the $groupIDs array
*
* @param string the group name to check for
* @param array an array of groupIDs to check
* @return boolean true if $requiredGroupName is in the $groupIDs array, else false
*/
function checkGroup($requiredGroupName, $groupIDs) {
global $default;
$sql = $default->db;
// lookup the id of $requiredGroupName
$requiredGroupID = lookupID($default->owl_groups_table, "name", "$requiredGroupName");
$default->log->debug("DocumentBrowser::checkGroup reqGrpID=$requiredGroupID");
if ($requiredGroupID) {
// now loop through the array and check if we're in the group
$default->log->debug("DocumentBrowser::checkGroup req=$requiredGroupName; ids=" . arrayToString($groupIDs));
if (in_array($requiredGroupID, $groupIDs)) {
return true;
} else {
return false;
}
} else {
// this group doesn't exist
// TODO: add this to language
$_SESSION["errorMessage"] = "group $requiredGroupName doesn't exist";
return false;
}
}
/**
* Returns an array of folder details for the specified folder
*
* @param string the sql query to retrieve the correct folder
* @return array the folder details
*/
function retrieveFolderDetails($folderQuery) {
global $default;
$folders = array();
$folderSql = $default->db;
// perform folder details query and loop through results
if ($folderSql->query($folderQuery)) {
// should only be one result
if ($folderSql->next_record()) {
$default->log->debug("DocumentBrowser::retrieveFolderDetails got results for $folderQuery");
// add the folder details to the array
$folderName = $folderSql->f("name");
$folderID = $folderSql->f("id");
// set folder attributes
/*
$folders[$folderName] = array("id" => $folderID,
"description" => $folderSql->f("description"),
"parent_id" => $folderSql->f("parent_id"),
"creator_id" => $folderSql->f("creator_id"),
"document_type_id" => $folderSql->f("document_type_id"),
"unit_id" => $folderSql->f("unit_id"),
"is_public" => $folderSql->f("is_public"));*/
$folders[$folderName] = new Folder($folderName, $folderSql->f("description"), $folderSql->f("parent_id"),
$folderSql->f("creator_id"), $folderSql->f("document_type_id"),
$folderSql->f("unit_id"), $folderSql->f("is_public"));
} else {
$_SESSION["errorMessage"] = "could not retrieve folder details: $folderQuery";
$default->log->debug("DocumentBrowser::retrieveFolderDetails error=" . $_SESSION["errorMessage"]);
}
} else {
$_SESSION["errorMessage"] = "could not retrieve folder details: $folderQuery";
}
return $folders;
}
/**
* Lookup document details for all the document_ids in the input array
*
* @param array the documentIDs to retrieve details for
* @return array the details of all the documents
*/
function lookupDocumentDetails($documentIDs) {
global $default;
$documents = array();
$sql = $default->db;
// convert array to a comma separated string
$documentIDStr = arrayToCSS($documentIDs);
$default->log->debug("DocumentBrowser::lookupDocumentDetails documentIDStr=$documentIDStr; documentIDs=" . arrayToString($documentIDs));
// create query to retrieve the details of the specified documents
$documentQuery = "select * from $default->owl_documents_table where id in ($documentIDStr)";
if ($sql->query($documentQuery)) {
// TODO: check return status of query and die appropriately
while ($sql->next_record()) {
// set attributes
/*$documents[$sql->f("name")] = array("id" => $sql->f("id"),
"document_type_id" => $sql->f("id"),
"name" => $sql->f("name"),
"filename" => $sql->f("filename"),
"size" => $sql->f("size"),
"creator_id" => $sql->f("creator_id"),
"modified" => $sql->f("modified"),
"description" => $sql->f("description"),
"mime_id" => $sql->f("mime_id"),
"folder_id" => $sql->f("folder_id"),
"major_version" => $sql->f("major_version"),
"minor_version" => $sql->f("minor_version"),
"is_checked_out" => $sql->f("is_checked_out"));*/
$documents[$sql->f("name")] &= Document::get($sql->f("id"));
}
} else {
$_SESSION["errorMessage"] = "document query failed";
}
return $documents;
}
}