FolderBrowser.inc
7.44 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
<?php
require_once("$default->fileSystemRoot/lib/browse/Browser.inc");
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 folder view document browsing business logic.
*
* Copyright (c) 2003 Jam Warehouse http://www.jamwarehouse.com
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* @version $Revision$
* @author Michael Joseph <michael@jamwarehouse.com>, Jam Warehouse (Pty) Ltd, South Africa
* @package lib.browse
*/
class FolderBrowser extends Browser {
/**
* Construct a new FolderBrowser instance with the specified sort criteria
*
* @param string the field to sort the results by
* @param string the direction to sort the results
*/
function FolderBrowser($sSortField = "filename", $sSortDirection = "asc", $aNewSortCriteria = array()) {
Browser::Browser($sSortField, $sSortDirection, $aNewSortCriteria);
}
/**
* Browse the documents by folder
*
* @return array folders and documents
*/
function browse() {
global $default;
// XXX: Shouldn't be getting this from $_REQUEST
$iFolderID = $_REQUEST['fFolderID'];
// we're browsing folders
$this->setBrowseStart($iFolderID);
$iFolderID = $iFolderID;
// instantiate and initialise
$results = array();
$sql = $default->db;
// no folder specified, so depending on the users groups, resolve to the right folderID
if ($iFolderID == "") {
// no folder specified, so start at the root folder
//TO DO: Need to look up all the Units
// look up this users unit
$unitID = User::getUnitID($_SESSION["userID"]);
$default->log->debug("Users UNIT = " . $unitID );
if ($unitID) {
// if the user is in a unit, start at the unit's root folder
// lookup the unit name
$unitName = lookupField($default->units_table, "name", "id", $unitID);
// the unit root folder has the same name as the unit
// FIXME: dodgy i know, but its easy
$unitRootFolderName = $unitName;
// now lookup the folderID
$aFolders = Folder::getList("name='$unitRootFolderName' and parent_id=1");
$default->log->debug("Users UNIT Name = " . $unitRootFolderName );
if (!$aFolders) {
// no folder exists with this name, so start at the root
$default->log->debug("Users UNIT Name NO FOLDER EXISTS WITH THIS NAME" . $unitRootFolderName );
$iFolderID = lookupID($default->folders_table, "parent_id", 0);
} else {
$iFolderID = $aFolders[0]->getID();
$default->log->debug("FOLDER EXISTS WITH THIS NAME" . $unitRootFolderName );
}
} else {
// else just start at the root
// FIXME: start at the root folder for the default organisation
$iFolderID = lookupID($default->folders_table, "parent_id", 0);
}
} else{
$default->log->debug("Folder specified " . $iFolderID );
}
$default->log->debug("FolderBrowser::browseByFolder: folderID=" . $iFolderID);
// get the folder
$rootFolder = Folder::get($iFolderID);
// FIXME: check permissions for the current folder before getting children
$results["folders"][] = & $rootFolder;
if ($results["folders"][0]) {
// now find all the child folders relative to this one
// FIXME: in the same unit?
$oSortCriterion = $this->aSortCriteria[$this->sSortField];
$aLookupCriteria = $oSortCriterion->getLookup();
$aQuery = $oSortCriterion->folderQuery($iFolderID);
$oResult = DBUtil::runQuery($aQuery);
if (PEAR::isError($oResult)) {
var_dump($oResult);
exit(0);
}
if ($oResult->numRows()) {
while ($aRow = $oResult->fetchRow()) {
$default->log->debug("In folder iteration while, with folder_id " . $aRow["id"]);
// check whether to display folders which are not readable and display/hide these accordingly
$oFolder = Folder::get($aRow["id"]);
if ($default->folderHidingFlag) {
if (Permission::userHasFolderReadPermission($oFolder)) {
$default->log->debug("FOLDER PERMISSIONS: Does have permission for folder " . $oFolder->getID() . ":" . $aRow["id"]);
$results["folders"][] = $oFolder;
} else {
$default->log->debug("FOLDER PERMISSIONS: Does NOT have permission for folder " . $aRow["id"] );
}
} else{
$results["folders"][] = $oFolder;
}
}
}
$default->log->debug("Going on to document checking");
$aQuery = $oSortCriterion->documentQuery($iFolderID);
$oResult = DBUtil::runQuery($aQuery);
if (PEAR::isError($oResult)) {
var_dump($oResult);
exit(0);
}
// initialise access flag;
$results["accessDenied"] = false;
if ($oResult->numRows()) {
// do the check for whether this documents have folder read permission, if they do, it's all good.
$hasFolderRead = Permission::userHasFolderReadPermission($rootFolder);
while ($aRow = $oResult->fetchRow()) {
$oDocument = &Document::get($aRow["id"]);
// proceed if the document is live
if ($oDocument->isLive()) {
// check permissions
if ($hasFolderRead || Permission::userHasDocumentReadPermission($oDocument)) {
// add documents to array
// set file attributes
$results["documents"][] = $oDocument;
} else {
// set access denied message
$results["accessDenied"] = true;
}
}
}
} else {
$_SESSION["errorMessage"] = "documents table select failed";
}
return $results;
}
}
function getSectionName() {
return "Manage Documents";
}
}