DocumentBrowser.inc
11 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
<?php
/**
* $Id$
*
* Contains document browsing business logic.
*
* @version $Revision$
* @author <a href="mailto:michael@jamwarehouse.com>Michael Joseph</a>, Jam Warehouse (Pty) Ltd, South Africa
* @package dmslib
*/
class DocumentBrowser {
// TODO: need separation of logic from presentation
/**
* Browse the documents by folder
*
* @param $folderID the folder to browse from
*/
function browseByFolder($folderID = -1) {
global $default;
// instantiate and initialise
$folders = array();
$folderSql = new Owl_DB();
$documentSql = new Owl_DB();
// TODO: put this in defaults?
$rootFolderName = "Root Document Folder";
if ($folderID == -1) {
// no folder specified, so start at the root
// TODO: unitID should be on session
// if this is a system administrator, start her at the ROOT
// FIXME: implement checkGroup
if (checkGroup("System Administrators", $_SESSION["group_id"])) {
$folderQuery = "select * from $default->owl_folders_table where description='$rootFolderName'";
} else {
// otherwise start everyone relative to their unit
$folderQuery = "select * from $default->owl_folders_table " .
"where unit_id=" . $_SESSION["unit_id"] .
" and description='$rootFolderName'";
}
} else {
// start from the specified folder
// TODO: check that the user has access to this folder?
// should be done in checkPermissions routine.
$folderQuery = "select * from $default->owl_folders_table where id=$folderID";
}
// perform query and loop through results
$folderSql->query($query);
// TODO: check return status of query and die appropriately
//$numrows = $folderSql->num_rows($sql);
while($folderSql->next_record()) {
// for each folder, add it to the array
$folderName = $folderSql->f("name");
$folders[$folderName] = array();
// for convenience and reuse down below
$folderID = $folderSql->f("id");
// set folder attributes
// FIXME: should the ui do a lookup before displaying the id information
// or should the additional lookups happen here and pass back names instead of ids?
// or both?
$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"));
// create query to retrieve documents in this folder
$documentQuery = "select * from default->owl_documents_table where folder_id=$folderID";
$documentSql->query($documentQuery);
// TODO: check return status of query and die appropriately
while ($documentSql->next_record()) {
// add documents to array
$documentName = $documentSql->f("name");
// set file attributes
$folders[$folderName][$documentName] = array("id" => $documentSql->f("id"),
"document_type_id" => $documentSql->f("id"),
"name" => $documentName,
"filename" => $documentSql->f("filename"),
"size" => $documentSql->f("size"),
"creator_id" => $documentSql->f("creator_id"),
"modified" => $documentSql->f("modified"),
"description" => $documentSql->f("description"),
"mime_id" => $documentSql->f("mime_id"),
//"folder_id" => $documentSql->f("folder_id"),
"major_version" => $documentSql->f("major_version"),
"minor_version" => $documentSql->f("minor_version"),
"is_checked_out" => $documentSql->f("is_checked_out"));
}
}
return $folders;
}
/**
* Browse the documents by category
*
* @param $category the category to browse
*/
function browseByCategory($category = "") {
$global default;
// TODO: add this to default inserts
$categoryField = "Category";
$categories = new array();
$categorySql = new Owl_DB();
// lookup document_fields id for category
$query = "select id from $default->owl_fields_table where name='$categoryField'";
// TODO: if there are no categories then die
$categorySql->query($query);
// TODO: check return status of query and die appropriately
$numrows = $categorySql->num_rows($sql);
if ($numrows == 1) {
$categorySql->next_record();
$category_fieldID = $categorySql->f("id");
} else {
// error
die("no category field in the db. contact sysadmin.");
}
if ($category == "") {
// no category supplied, so return a list of categories
$categories = array();
// now get a list of category values
$query = "select value from $default->owl_document_fields_table where document_field_id=$category_fieldID";
$categorySql->query($query);
// loop through resultset, build array and return
while ($categorySql->next_record()) {
$categories[] = $categorySql->f("value");
}
// its ok if we return an empty array- the UI's responsibility to check and print an error
return $categories;
} else {
// we have a category to use, so find all the documents
// with this category value
// first lookup the document_field_id of this
$query = "select document_id from $default->owl_document_fields_table where document_field_id = $category_fieldID " .
"and value='$category'";
$categorySql->query($query);
// loop through resultset and build comma separated list of documentIDs
$documentIDs = "";
while ($categorySql->next_record()) {
$documentIDs = $categorySql->f("document_id") . ",";
//$categories[] = $categorySql->f("document_id");
}
// trim the last comma
$documentIDs = substr("$documentIDs", 0, -1);
// use lookup function to retrieve details
$documents = $this->lookupDocumentDetails($documentIDs);
// add to array and return
$categories[$category] = $documents;
return $categories;
}
}
/**
* Lookup document details for all the document_ids in the comma separated input string
*
* @param $documentIDs
* @return an array containing the details of all the documents
*/
function lookupDocumentDetails($documentIDs) {
global $default;
$documents = array();
$sql = new Owl_DB();
// create query to retrieve the details of the specified documents
$documentQuery = "select * from $default->owl_documents_table where document_id in ($documentIDs)";
$sql->query($documentQuery);
// TODO: check return status of query and die appropriately
while ($sql->next_record()) {
// set attributes
$documents[$sql->f("id")] = 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"));
}
return $folders;
}
/**
* Browse the documents by document type
*
* @param $documentTypeID the document type ID to browse
*/
function browseByDocumentType($documentTypeID = -1) {
global $default;
$documentTypes = array();
$sql = new Owl_DB();
if ($documentTypeID == -1) {
// return a list of document types
$query = "select * from $default->owl_document_types_table";
$sql->query($query);
while ($sql->next_record()) {
$documentTypes[$sql->f("id")] = $sql->f("name");
}
return $documentTypes;
} else {
// find all documents with this document type
$query = "select document_id from $default->owl_documents_table where document_type_id=$documentTypeID";
// loop through resultset and build comma separated list of documentIDs
$documentIDs = "";
while ($sql->next_record()) {
$documentIDs = $sql->f("document_id") . ",";
}
// trim the last comma
$documentIDs = substr("$documentIDs", 0, -1);
// use lookup function to retrieve details
$documents = $this->lookupDocumentDetails($documentIDs);
// add to array and return
$documentTypes[$documentTypeID] = $documents;
return $documentTypes;
}
}
}