Commit afb92edc33040038b7515c89dcb6e3372a066146

Authored by Michael Joseph
1 parent d80e91e2

changed document browsing methods to use Document and Folder objects


git-svn-id: https://kt-dms.svn.sourceforge.net/svnroot/kt-dms/trunk@421 c91229c3-7414-0410-bfa2-8a42b809f60b
lib/documentmanagement/DocumentBrowser.inc
@@ -29,12 +29,15 @@ class DocumentBrowser { @@ -29,12 +29,15 @@ class DocumentBrowser {
29 global $default; 29 global $default;
30 30
31 // instantiate and initialise 31 // instantiate and initialise
32 - $folders = array(); 32 + $results = array();
33 $sql = new Owl_DB(); 33 $sql = new Owl_DB();
34 34
  35 + // no folder specified, so depending on the users groups, resolve to the right folderID
35 if ($folderID == -1) { 36 if ($folderID == -1) {
36 // no folder specified, so start at the root for this users organisation 37 // no folder specified, so start at the root for this users organisation
37 38
  39 + // TODO: check that all these lookup calls succeed?
  40 +
38 // lookup this users groups 41 // lookup this users groups
39 $groupIDs = lookupGroupIDs($_SESSION["userID"]); 42 $groupIDs = lookupGroupIDs($_SESSION["userID"]);
40 $default->log->debug("DocumentBrowser::browseByFolder: groupIDs=" . arrayToString($groupIDs)); 43 $default->log->debug("DocumentBrowser::browseByFolder: groupIDs=" . arrayToString($groupIDs));
@@ -55,13 +58,15 @@ class DocumentBrowser { @@ -55,13 +58,15 @@ class DocumentBrowser {
55 $rootFolderName = $organisationName . " Document Root"; 58 $rootFolderName = $organisationName . " Document Root";
56 59
57 // lookup the id of the root folder 60 // lookup the id of the root folder
58 - $folderID = lookupID($default->owl_folders_table, "name", "$rootFolderName");  
59 - $default->log->debug("DocumentBrowser::browseByFolder: folderID=$folderID"); 61 + $folderID = lookupID($default->owl_folders_table, "name", $rootFolderName);
  62 + $default->log->debug("DocumentBrowser::browseByFolder: root folderID=$folderID, root folder name=$rootFolderName");
60 63
61 - // if this is a system administrator, start her at the ROOT 64 + // if this is a system administrator, start her at the root folder
62 // TODO: add to default->sysadmin_group 65 // TODO: add to default->sysadmin_group
63 if ($this->checkGroup("System Administrators", $groupIDs)) { 66 if ($this->checkGroup("System Administrators", $groupIDs)) {
64 - $folderQuery = "SELECT * FROM $default->owl_folders_table WHERE name='$rootFolderName'"; 67 + //$folderQuery = "SELECT * FROM $default->owl_folders_table WHERE name='$rootFolderName'";
  68 + //$results["folders"][] = & Folder::get($folderID);
  69 + $default->log->info("DocumentBrowser::browseByFolder looked up org root folderID=$folderID; org root foldername=$rootFolderName");
65 } else { 70 } else {
66 // otherwise start everyone relative to their unit 71 // otherwise start everyone relative to their unit
67 72
@@ -70,42 +75,45 @@ class DocumentBrowser { @@ -70,42 +75,45 @@ class DocumentBrowser {
70 75
71 $default->log->debug("DocumentBrowser::browseByFolder: unitName=$unitName"); 76 $default->log->debug("DocumentBrowser::browseByFolder: unitName=$unitName");
72 77
73 - $unitRootFolder = $unitName . " Document Root"; 78 + $unitRootFolderName = $unitName . " Document Root";
74 79
75 // lookup descendant folders with the appropriate unit set 80 // lookup descendant folders with the appropriate unit set
76 - $folderQuery = "SELECT * from $default->owl_folders_table " .  
77 - "WHERE name='$unitRootFolder'"; 81 + //$folderQuery = "SELECT from $default->owl_folders_table " .
  82 + // "WHERE parent_id=$folderID and name='$unitRootFolder' and unit_id=$unitID";
  83 + $folderID = lookupID($default->owl_folders_table, "name", $unitRootFolderName);
  84 + //$results["folders"][] = & Folder::get($folderID);
  85 + $default->log->info("DocumentBrowser::browseByFolder looked up unit root folderID=$folderID; unit root foldername=$unitRootFolderName");
78 } 86 }
79 } else { 87 } else {
  88 + $default->log->info("DocumentBrowser::browseByFolder starting at passed in folderID=$folderID");
80 // start from the specified folder 89 // start from the specified folder
81 //$folderQuery = "SELECT * FROM $default->owl_folders_table WHERE id=$folderID"; 90 //$folderQuery = "SELECT * FROM $default->owl_folders_table WHERE id=$folderID";
82 - $folder = Folder::get($folderID);  
83 - // TODO: error check  
84 } 91 }
85 92
  93 + $default->log->debug("DocumentBrowser::browseByFolder: folderID=$folderID");
86 // retrieve folder details 94 // retrieve folder details
87 - $folders = $this->retrieveFolderDetails($folderQuery); 95 + //$folders = $this->retrieveFolderDetails($folderQuery);
88 96
89 // check if the user has access to this folder 97 // check if the user has access to this folder
90 if (Permission::userHasFolderReadPermission($folderID)) { 98 if (Permission::userHasFolderReadPermission($folderID)) {
  99 + // get the folder
  100 + $results["folders"][] = & Folder::get($folderID);
  101 + $default->log->debug("DocumentBrowser::browseByFolder: results=" . arrayToString($results));
91 102
92 - // lookup the name of the root folder  
93 - $folderName = lookupField($default->owl_folders_table, "name", "id", $folderID);  
94 -  
95 - $default->log->debug("DocumentBrowser::browseByFolder folderID=$folderID; folderName=$folderName");  
96 - $default->log->debug("DocumentBrowser::browseByFolder folders=" . arrayToString($folders));  
97 -  
98 - // now find all the child folders relative to this one  
99 - $folderQuery = "SELECT * from $default->owl_folders_table WHERE parent_id=" . $folderID; 103 + // now find all the child folders relative to this one
  104 + // FIXME: in the same unit?
  105 + $folderQuery = "SELECT id from $default->owl_folders_table WHERE parent_id=" . $folderID;
100 $default->log->debug("DocumentBrowser::browseByFolder child folder query=$folderQuery"); 106 $default->log->debug("DocumentBrowser::browseByFolder child folder query=$folderQuery");
101 - $childFolders = $this->retrieveFolderDetails($folderQuery);  
102 - $default->log->debug("DocumentBrowser::browseByFolder childFolders=" . arrayToString($childFolders));  
103 -  
104 - // add children to array  
105 - $folders[$folderName]["folders"] = $childFolders; 107 + if ($sql->query($folderQuery)) {
  108 + while ($sql->next_record()) {
  109 + // add the child folders to the array
  110 + $results["folders"][] = & Folder::get($sql->f("id"));
  111 + }
  112 + }
  113 + $default->log->debug("DocumentBrowser::browseByFolder: after child folders added; results=" . arrayToString($results));
106 114
107 // create query to retrieve documents in this folder 115 // create query to retrieve documents in this folder
108 - $documentQuery = "SELECT * FROM $default->owl_documents_table WHERE folder_id=$folderID"; 116 + $documentQuery = "SELECT id FROM $default->owl_documents_table WHERE folder_id=$folderID";
109 $default->log->debug("DocumentBrowser::browseByFolder about to execute $documentQuery"); 117 $default->log->debug("DocumentBrowser::browseByFolder about to execute $documentQuery");
110 if ($sql->query($documentQuery)) { 118 if ($sql->query($documentQuery)) {
111 while ($sql->next_record()) { 119 while ($sql->next_record()) {
@@ -113,27 +121,16 @@ class DocumentBrowser { @@ -113,27 +121,16 @@ class DocumentBrowser {
113 if (Permission::userHasDocumentReadPermission($sql->f("id"))) { 121 if (Permission::userHasDocumentReadPermission($sql->f("id"))) {
114 // add documents to array 122 // add documents to array
115 // set file attributes 123 // set file attributes
116 - $folders[$folderName]["documents"][$sql->f("name")] =  
117 - array("id" => $sql->f("id"),  
118 - "document_type_id" => $documentID,  
119 - "name" => $documentName,  
120 - "filename" => $sql->f("filename"),  
121 - "size" => $sql->f("size"),  
122 - "creator_id" => $sql->f("creator_id"),  
123 - "modified" => $sql->f("modified"),  
124 - "description" => $sql->f("description"),  
125 - "mime_id" => $sql->f("mime_id"),  
126 - "folder_id" => $sql->f("folder_id"),  
127 - "major_version" => $sql->f("major_version"),  
128 - "minor_version" => $sql->f("minor_version"),  
129 - "is_checked_out" => $sql->f("is_checked_out")); 124 + $results["documents"][] = & Document::get($sql->f("id"));
  125 + } else {
  126 + $default->log->debug("DocumentBrowser::browseByFolder: read permission denied for document id=" . $sql->f("id"));
130 } 127 }
131 } 128 }
132 } else { 129 } else {
133 $_SESSION["errorMessage"] = "documents table select failed"; 130 $_SESSION["errorMessage"] = "documents table select failed";
134 } 131 }
135 132
136 - return $folders; 133 + return $results;
137 134
138 } else { 135 } else {
139 // permission to view this folder denied 136 // permission to view this folder denied
@@ -152,7 +149,7 @@ class DocumentBrowser { @@ -152,7 +149,7 @@ class DocumentBrowser {
152 149
153 // TODO: add this to default inserts 150 // TODO: add this to default inserts
154 $categoryField = "Category"; 151 $categoryField = "Category";
155 - $categories = array(); 152 + $results = array();
156 $sql = new Owl_DB(); 153 $sql = new Owl_DB();
157 154
158 // lookup document_fields id for category 155 // lookup document_fields id for category
@@ -162,42 +159,38 @@ class DocumentBrowser { @@ -162,42 +159,38 @@ class DocumentBrowser {
162 if ($category == "") { 159 if ($category == "") {
163 $default->log->debug("DocumentBrowser::browseByCategory no category supplied, returning list"); 160 $default->log->debug("DocumentBrowser::browseByCategory no category supplied, returning list");
164 // no category value supplied, so return a list of categories 161 // no category value supplied, so return a list of categories
165 - $categories = array();  
166 -  
167 - // now get a list of category values 162 +
  163 + // get a list of category values
168 $query = "select value from $default->owl_document_fields_table where document_field_id=$categoryFieldID"; 164 $query = "select value from $default->owl_document_fields_table where document_field_id=$categoryFieldID";
169 $default->log->debug("DocumentBrowser::browseByCategory category listing query=$query"); 165 $default->log->debug("DocumentBrowser::browseByCategory category listing query=$query");
170 $sql->query($query); 166 $sql->query($query);
171 // loop through resultset, build array and return 167 // loop through resultset, build array and return
172 while ($sql->next_record()) { 168 while ($sql->next_record()) {
173 - $categories[] = $sql->f("value"); 169 + $results["categories"][] = $sql->f("value");
174 } 170 }
175 // its ok if we return an empty array- the UI's responsibility to check and print an error 171 // its ok if we return an empty array- the UI's responsibility to check and print an error
176 - return $categories; 172 + return $results;
177 } else { 173 } else {
178 $default->log->debug("DocumentBrowser::browseByCategory get documents for category $category"); 174 $default->log->debug("DocumentBrowser::browseByCategory get documents for category $category");
179 // we have a category to use, so find all the documents 175 // we have a category to use, so find all the documents
180 // with this category value 176 // with this category value
181 -  
182 - // first lookup the document_field_id of this 177 +
183 $query = "select document_id from $default->owl_document_fields_table where document_field_id = $categoryFieldID " . 178 $query = "select document_id from $default->owl_document_fields_table where document_field_id = $categoryFieldID " .
184 "and value='$category'"; 179 "and value='$category'";
185 $default->log->debug("DocumentBrowser::browseByCategory documents in category query=$query"); 180 $default->log->debug("DocumentBrowser::browseByCategory documents in category query=$query");
186 $sql->query($query); 181 $sql->query($query);
187 - // loop through resultset and build comma separated list of documentIDs  
188 - $documentIDs = array(); 182 + // loop through resultset and add to array
189 while ($sql->next_record()) { 183 while ($sql->next_record()) {
190 // check permissions 184 // check permissions
191 if (Permission::userHasDocumentReadPermission($sql->f("document_id"))) { 185 if (Permission::userHasDocumentReadPermission($sql->f("document_id"))) {
192 - $documentIDs[] = $sql->f("document_id"); 186 + $results["documents"][] = & Document::get($sql->f("document_id"));
  187 + } else {
  188 + $default->log->debug("DocumentBrowser::browseByCategory permission denied for documentID=" . $sql->f("document_id"));
193 } 189 }
194 - }  
195 - $default->log->debug("DocumentBrowser::browseByCategory documentIDs=" . arrayToString($documentIDs));  
196 - // use lookup function to retrieve details  
197 - $documents = $this->lookupDocumentDetails($documentIDs);  
198 - // add to array and return  
199 - $categories[$category]["documents"] = $documents;  
200 - return $categories; 190 + }
  191 + $default->log->debug("DocumentBrowser::browseByCategory results=" . arrayToString($results));
  192 +
  193 + return $results;
201 } 194 }
202 } 195 }
203 196
@@ -209,7 +202,7 @@ class DocumentBrowser { @@ -209,7 +202,7 @@ class DocumentBrowser {
209 function browseByDocumentType($documentTypeID = -1) { 202 function browseByDocumentType($documentTypeID = -1) {
210 global $default; 203 global $default;
211 204
212 - $documentTypes = array(); 205 + $results = array();
213 $sql = new Owl_DB(); 206 $sql = new Owl_DB();
214 207
215 if ($documentTypeID == -1) { 208 if ($documentTypeID == -1) {
@@ -217,31 +210,29 @@ class DocumentBrowser { @@ -217,31 +210,29 @@ class DocumentBrowser {
217 $query = "select * from $default->owl_document_types_table"; 210 $query = "select * from $default->owl_document_types_table";
218 $sql->query($query); 211 $sql->query($query);
219 while ($sql->next_record()) { 212 while ($sql->next_record()) {
220 - $documentTypes[$sql->f("id")] = $sql->f("name"); 213 + $results["documentTypes"][] = array ("id" => $sql->f("id"), "name" => $sql->f("name"));
221 } 214 }
222 - return $documentTypes; 215 + return $results;
223 } else { 216 } else {
224 - // lookup document type name 217 + // lookup document type name from the passed in id
225 $documentTypeName = lookupField($default->owl_document_types_table, "name", "id", $documentTypeID); 218 $documentTypeName = lookupField($default->owl_document_types_table, "name", "id", $documentTypeID);
226 219
227 // find all documents with this document type 220 // find all documents with this document type
228 $query = "select id from $default->owl_documents_table where document_type_id=$documentTypeID"; 221 $query = "select id from $default->owl_documents_table where document_type_id=$documentTypeID";
229 - $default->log->debug("DocumentBrowser::browseByCategory documents from doc type query=$query"); 222 + $default->log->debug("DocumentBrowser::browseByDocumentType: documents from doc type query=$query");
230 // loop through resultset and build array of documentIDs 223 // loop through resultset and build array of documentIDs
231 - $documentIDs = array();  
232 $sql->query($query); 224 $sql->query($query);
233 while ($sql->next_record()) { 225 while ($sql->next_record()) {
234 // check permission 226 // check permission
235 if (Permission::userHasDocumentReadPermission($sql->f("id"))) { 227 if (Permission::userHasDocumentReadPermission($sql->f("id"))) {
236 - $documentIDs[] = $sql->f("id"); 228 + $results["documents"][] = & Document::get($sql->f("id"));
  229 + } else {
  230 + $default->log->debug("DocumentBrowser::browseByDocumentType: permission denied for documentID=" . $sql->f("document_id"));
237 } 231 }
238 } 232 }
239 - $default->log->debug("DocumentBrowser::browseByCategory documentIDs=" . arrayToString($documentIDs));  
240 - // use lookup function to retrieve details  
241 - $documents = $this->lookupDocumentDetails($documentIDs);  
242 - // add to array and return  
243 - $documentTypes[$documentTypeName]["documents"]= $documents;  
244 - return $documentTypes; 233 + $default->log->debug("DocumentBrowser::browseByDocumentType: results=" . arrayToString($results));
  234 +
  235 + return $results;
245 } 236 }
246 } 237 }
247 238