folderutil.inc.php
8.52 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
<?php /* vim: set expandtab softtabstop=4 shiftwidth=4 foldmethod=marker: */
/**
* $Id$
*
* High-level folder operations
*
* Copyright (c) 2005 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 Neil Blakey-Milner, Jam Warehouse (Pty) Ltd, South Africa
*/
require_once(KT_LIB_DIR . '/storage/storagemanager.inc.php');
require_once(KT_LIB_DIR . "/subscriptions/subscriptions.inc.php");
require_once(KT_LIB_DIR . '/permissions/permission.inc.php');
require_once(KT_LIB_DIR . '/permissions/permissionutil.inc.php');
require_once(KT_LIB_DIR . '/users/User.inc');
require_once(KT_LIB_DIR . '/database/dbutil.inc');
class KTFolderUtil {
function _add ($oParentFolder, $sFolderName, $oUser) {
$oStorage =& KTStorageManagerUtil::getSingleton();
$oFolder =& Folder::createFromArray(array(
'name' => $sFolderName,
'description' => $sFolderName,
'parentid' => $oParentFolder->getID(),
'creatorid' => $oUser->getID(),
));
if (PEAR::isError($oFolder)) {
return $oFolder;
}
$res = $oStorage->createFolder($oFolder);
if (PEAR::isError($res)) {
$oFolder->delete();
return $res;
}
return $oFolder;
}
function add ($oParentFolder, $sFolderName, $oUser) {
$oFolder = KTFolderUtil::_add($oParentFolder, $sFolderName, $oUser);
if (PEAR::isError($oFolder)) {
return $oFolder;
}
// fire subscription alerts for the new folder
$oSubscriptionEvent = new SubscriptionEvent();
$oSubscriptionEvent->AddFolder($oFolder, $oParentFolder);
return $oFolder;
}
function move($oFolder, $oNewParentFolder, $oUser) {
if (KTFolderUtil::exists($oNewParentFolder, $oFolder->getName())) {
return PEAR::raiseError("Folder with the same name already exists in the new parent folder");
}
$oStorage =& KTStorageManagerUtil::getSingleton();
// First, deal with SQL, as it, at least, is guaranteed to be atomic
$table = "folders";
// Update the moved folder first...
$sQuery = "UPDATE $table SET full_path = ?, parent_folder_ids = ?, parent_id = ? WHERE id = ?";
$aParams = array(
sprintf("%s/%s", $oNewParentFolder->getFullPath(), $oNewParentFolder->getName()),
sprintf("%s,%s", $oNewParentFolder->getParentFolderIDs(), $oNewParentFolder->getID()),
$oNewParentFolder->getID(),
$oFolder->getID(),
);
$res = DBUtil::runQuery(array($sQuery, $aParams));
if (PEAR::isError($res)) {
return $res;
}
$sQuery = "UPDATE $table SET full_path = CONCAT(?, SUBSTRING(full_path FROM ?)), parent_folder_ids = CONCAT(?, SUBSTRING(parent_folder_ids FROM ?)) WHERE full_path LIKE ?";
$aParams = array(
sprintf("%s/%s", $oNewParentFolder->getFullPath(), $oNewParentFolder->getName()),
strlen($oFolder->getFullPath()) + 1,
sprintf("%s,%s", $oNewParentFolder->getParentFolderIDs(), $oNewParentFolder->getID()),
strlen($oFolder->getParentFolderIDs()) + 1,
sprintf("%s/%s%%", $oFolder->getFullPath(), $oFolder->getName()),
);
$res = DBUtil::runQuery(array($sQuery, $aParams));
if (PEAR::isError($res)) {
return $res;
}
$table = "documents";
$sQuery = "UPDATE $table SET full_path = CONCAT(?, SUBSTRING(full_path FROM ?)) WHERE full_path LIKE ?";
$aParams = array(
sprintf("%s/%s", $oNewParentFolder->getFullPath(), $oNewParentFolder->getName()),
strlen($oFolder->getFullPath()) + 1,
sprintf("%s/%s%%", $oFolder->getFullPath(), $oFolder->getName()),
);
$res = DBUtil::runQuery(array($sQuery, $aParams));
if (PEAR::isError($res)) {
return $res;
}
$res = $oStorage->moveFolder($oFolder, $oNewParentFolder);
if (PEAR::isError($res)) {
return $res;
}
return;
}
function exists($oParentFolder, $sName) {
return Folder::folderExistsName($sName, $oParentFolder->getID());
}
/* folderUtil::delete
*
* this function is _much_ more complex than it might seem.
* we need to:
* - recursively identify children
* - validate that permissions are allocated correctly.
* - step-by-step delete.
*/
function delete($oStartFolder, $oUser, $sReason) {
// FIXME: we need to work out if "write" is the right perm.
$oPerm = KTPermission::getByName('ktcore.permissions.write');
$aFolderIds = array(); // of oFolder
$aDocuments = array(); // of oDocument
$aFailedDocuments = array(); // of String
$aFailedFolders = array(); // of String
$aRemainingFolders = array($oStartFolder->getId());
DBUtil::startTransaction();
while (!empty($aRemainingFolders)) {
$iFolderId = array_pop($aRemainingFolders);
$oFolder = Folder::get($iFolderId);
if (PEAR::isError($oFolder) || ($oFolder == false)) {
DBUtil::rollback();
return PEAR::raiseError(sprintf('Failure resolving child folder with id = %d.', $iFolderId));
}
// don't just stop ... plough on.
if (KTPermissionUtil::userHasPermissionOnItem($oUser, $oPerm, $oFolder)) {
$aFolderIds[] = $iFolderId;
} else {
$aFailedFolders[] = $oFolder->getName();
}
// child documents
$aChildDocs = Document::getList(array('folder_id = ?',array($iFolderId)));
foreach ($aChildDocs as $oDoc) {
if (KTPermissionUtil::userHasPermissionOnItem($oUser, $oPerm, $oFolder)) {
$aDocuments[] = $oDoc;
} else {
$aFailedDocuments[] = $oDoc->getName();
}
}
// child folders.
$aCFIds = Folder::getList(array('parent_id = ?', array($iFolderId)), array('ids' => true));
$aRemainingFolders = array_merge($aRemainingFolders, $aCFIds);
}
// FIXME we could subdivide this to provide a per-item display (viz. bulk upload, etc.)
if ((!empty($aFailedDocuments) || (!empty($aFailedFolders)))) {
$sFD = '';
$sFF = '';
if (!empty($aFailedDocuments)) {
$sFD = _('Documents: ') . array_join(', ', $aFailedDocuments) . '. ';
}
if (!empty($aFailedFolders)) {
$sFF = _('Folders: ') . array_join(', ', $aFailedFolders) . '.';
}
return PEAR::raiseError(_('You do not have permission to delete these items. ') . $sFD . $sFF);
}
// now we can go ahead.
foreach ($aDocuments as $oDocument) {
$res = KTDocumentUtil::delete($oDocument, $sReason);
if (PEAR::isError($res)) {
DBUtil::rollback();
return PEAR::raiseError(_('Delete Aborted. Unexpected failure to delete document: ') . $oDocument->getName() . $res->getMessage());
}
}
$oStorage =& KTStorageManagerUtil::getSingleton();
$oStorage->removeFolderTree($oStartFolder);
// documents all cleared.
$sQuery = 'DELETE FROM ' . KTUtil::getTableName('folders') . ' WHERE id IN (' . DBUtil::paramArray($aFolderIds) . ')';
$aParams = $aFolderIds;
$res = DBUtil::runQuery(array($sQuery, $aParams));
if (PEAR::isError($res)) {
DBUtil::rollback();
return PEAR::raiseError(_('Failure deleting folders.'));
}
// and store
DBUtil::commit();
return true;
}
}
?>