Commit b0df6b4d959f31a102042d1ad4e473b5f2fb7b03
1 parent
212c09c8
Move all document addition logic (including incomplete documents that
will be completed at a later stage) into KTDocumentUtil, and use the abstracted storage system for existing code. git-svn-id: https://kt-dms.svn.sourceforge.net/svnroot/kt-dms/trunk@3596 c91229c3-7414-0410-bfa2-8a42b809f60b
Showing
1 changed file
with
223 additions
and
4 deletions
lib/documentmanagement/documentutil.inc.php
| @@ -28,6 +28,9 @@ | @@ -28,6 +28,9 @@ | ||
| 28 | */ | 28 | */ |
| 29 | 29 | ||
| 30 | require_once(KT_LIB_DIR . '/documentmanagement/DocumentFieldLink.inc'); | 30 | require_once(KT_LIB_DIR . '/documentmanagement/DocumentFieldLink.inc'); |
| 31 | +require_once(KT_LIB_DIR . '/storage/storagemanager.inc.php'); | ||
| 32 | +require_once(KT_LIB_DIR . '/documentmanagement/DocumentTransaction.inc'); | ||
| 33 | +require_once(KT_LIB_DIR . '/web/WebDocument.inc'); | ||
| 31 | 34 | ||
| 32 | class KTDocumentUtil { | 35 | class KTDocumentUtil { |
| 33 | function createMetadataVersion($oDocument) { | 36 | function createMetadataVersion($oDocument) { |
| @@ -99,17 +102,18 @@ class KTDocumentUtil { | @@ -99,17 +102,18 @@ class KTDocumentUtil { | ||
| 99 | return $oVersionedDocument; | 102 | return $oVersionedDocument; |
| 100 | } | 103 | } |
| 101 | 104 | ||
| 102 | - if (!PhysicalDocumentManager::uploadPhysicalDocument($oDocument, $oDocument->getFolderID(), "", $sFilename)) { | 105 | + $oStorage =& KTStorageManagerUtil::getSingleton(); |
| 106 | + | ||
| 107 | + if (!$oStorage->upload($oDocument, $sFilename)) { | ||
| 103 | // reinstate the backup | 108 | // reinstate the backup |
| 104 | copy($sBackupPath, $oDocument->getPath()); | 109 | copy($sBackupPath, $oDocument->getPath()); |
| 105 | // remove the backup | 110 | // remove the backup |
| 106 | unlink($sBackupPath); | 111 | unlink($sBackupPath); |
| 107 | - return PEAR::raiseError(_("An error occurred while storing the new file on the filesystem")); | 112 | + return PEAR::raiseError(_("An error occurred while storing the new file")); |
| 108 | } | 113 | } |
| 109 | 114 | ||
| 110 | $oDocument->setMetadataVersion($oDocument->getMetadataVersion()+1); | 115 | $oDocument->setMetadataVersion($oDocument->getMetadataVersion()+1); |
| 111 | 116 | ||
| 112 | - $oDocument->setFileSize($_FILES['fFile']['size']); | ||
| 113 | $oDocument->setLastModifiedDate(getCurrentDateTime()); | 117 | $oDocument->setLastModifiedDate(getCurrentDateTime()); |
| 114 | $oDocument->setIsCheckedOut(false); | 118 | $oDocument->setIsCheckedOut(false); |
| 115 | $oDocument->setCheckedOutUserID(-1); | 119 | $oDocument->setCheckedOutUserID(-1); |
| @@ -121,6 +125,7 @@ class KTDocumentUtil { | @@ -121,6 +125,7 @@ class KTDocumentUtil { | ||
| 121 | } else if ($sCheckInType == "minor") { | 125 | } else if ($sCheckInType == "minor") { |
| 122 | $oDocument->setMinorVersionNumber($oDocument->getMinorVersionNumber()+1); | 126 | $oDocument->setMinorVersionNumber($oDocument->getMinorVersionNumber()+1); |
| 123 | } | 127 | } |
| 128 | + $oDocument->setFileSize($_FILES['fFile']['size']); | ||
| 124 | 129 | ||
| 125 | $bSuccess = $oDocument->update(); | 130 | $bSuccess = $oDocument->update(); |
| 126 | if ($bSuccess !== true) { | 131 | if ($bSuccess !== true) { |
| @@ -132,7 +137,6 @@ class KTDocumentUtil { | @@ -132,7 +137,6 @@ class KTDocumentUtil { | ||
| 132 | 137 | ||
| 133 | // create the document transaction record | 138 | // create the document transaction record |
| 134 | $oDocumentTransaction = & new DocumentTransaction($oDocument->getID(), $sCheckInComment, CHECKIN); | 139 | $oDocumentTransaction = & new DocumentTransaction($oDocument->getID(), $sCheckInComment, CHECKIN); |
| 135 | - // TODO: check transaction creation status? | ||
| 136 | $oDocumentTransaction->create(); | 140 | $oDocumentTransaction->create(); |
| 137 | 141 | ||
| 138 | // fire subscription alerts for the checked in document | 142 | // fire subscription alerts for the checked in document |
| @@ -144,6 +148,221 @@ class KTDocumentUtil { | @@ -144,6 +148,221 @@ class KTDocumentUtil { | ||
| 144 | $default->log->info("checkInDocumentBL.php fired $count subscription alerts for checked out document " . $oDocument->getName()); | 148 | $default->log->info("checkInDocumentBL.php fired $count subscription alerts for checked out document " . $oDocument->getName()); |
| 145 | return true; | 149 | return true; |
| 146 | } | 150 | } |
| 151 | + | ||
| 152 | + function &_add($oFolder, $sFilename, $oUser, $aOptions) { | ||
| 153 | + $oContents = KTUtil::arrayGet($aOptions, 'contents'); | ||
| 154 | + $aMetadata = KTUtil::arrayGet($aOptions, 'metadata'); | ||
| 155 | + $oDocumentType = KTUtil::arrayGet($aOptions, 'documenttype'); | ||
| 156 | + $oDocument =& Document::createFromArray(array( | ||
| 157 | + 'name' => $sFilename, | ||
| 158 | + 'description' => $sFilename, | ||
| 159 | + 'filename' => $sFilename, | ||
| 160 | + 'folderid' => $oFolder->getID(), | ||
| 161 | + 'creatorid' => $oUser->getID(), | ||
| 162 | + )); | ||
| 163 | + if (PEAR::isError($oDocument)) { | ||
| 164 | + return $oDocument; | ||
| 165 | + } | ||
| 166 | + | ||
| 167 | + if (!is_null($oDocumentType)) { | ||
| 168 | + $oDocument->setDocumentTypeID($oDocumentType->getID()); | ||
| 169 | + } else { | ||
| 170 | + // XXX: Ug... | ||
| 171 | + $oDocument->setDocumentTypeID(1); | ||
| 172 | + } | ||
| 173 | + | ||
| 174 | + if (is_null($oContents)) { | ||
| 175 | + $res = KTDocumentUtil::setIncomplete($oDocument, "contents"); | ||
| 176 | + if (PEAR::isError($res)) { | ||
| 177 | + $oDocument->delete(); | ||
| 178 | + return $res; | ||
| 179 | + } | ||
| 180 | + } else { | ||
| 181 | + $res = KTDocumentUtil::storeContents($oDocument, $oContents, $aOptions); | ||
| 182 | + if (PEAR::isError($res)) { | ||
| 183 | + $oDocument->delete(); | ||
| 184 | + return $res; | ||
| 185 | + } | ||
| 186 | + } | ||
| 187 | + | ||
| 188 | + if (is_null($aMetadata)) { | ||
| 189 | + $res = KTDocumentUtil::setIncomplete($oDocument, "metadata"); | ||
| 190 | + if (PEAR::isError($res)) { | ||
| 191 | + $oDocument->delete(); | ||
| 192 | + return $res; | ||
| 193 | + } | ||
| 194 | + } else { | ||
| 195 | + $res = KTDocumentUtil::saveMetadata($oDocument, $aMetadata); | ||
| 196 | + if (PEAR::isError($res)) { | ||
| 197 | + $oDocument->delete(); | ||
| 198 | + return $res; | ||
| 199 | + } | ||
| 200 | + } | ||
| 201 | + | ||
| 202 | + // setIncomplete and storeContents may change the document's status or | ||
| 203 | + // storage_path, so now is the time to update | ||
| 204 | + $oDocument->update(); | ||
| 205 | + return $oDocument; | ||
| 206 | + } | ||
| 207 | + | ||
| 208 | + // {{{ validateMetadata | ||
| 209 | + function validateMetadata(&$oDocument, $aMetadata) { | ||
| 210 | + return $aMetadata; | ||
| 211 | + } | ||
| 212 | + // }}} | ||
| 213 | + | ||
| 214 | + // {{{ saveMetadata | ||
| 215 | + function saveMetadata(&$oDocument, $aMetadata) { | ||
| 216 | + $table = "document_fields_link"; | ||
| 217 | + $res = KTDocumentUtil::validateMetadata($oDocument, $aMetadata); | ||
| 218 | + if (PEAR::isError($res)) { | ||
| 219 | + return $res; | ||
| 220 | + } | ||
| 221 | + $aMetadata = $res; | ||
| 222 | + | ||
| 223 | + $res = DBUtil::runQuery(array("DELETE FROM $table WHERE document_id = ?", array($oDocument->getID()))); | ||
| 224 | + if (PEAR::isError($res)) { | ||
| 225 | + return $res; | ||
| 226 | + } | ||
| 227 | + // XXX: Metadata refactor | ||
| 228 | + foreach ($aMetadata as $oMetadata => $sValue) { | ||
| 229 | + $res = DBUtil::autoInsert($table, array( | ||
| 230 | + "document_id" => $oDocument->getID(), | ||
| 231 | + "document_field_id" => $oMetadata->getID(), | ||
| 232 | + "value" => $sValue, | ||
| 233 | + )); | ||
| 234 | + if (PEAR::isError($res)) { | ||
| 235 | + return $res; | ||
| 236 | + } | ||
| 237 | + } | ||
| 238 | + KTDocumentUtil::setComplete($oDocument, "metadata"); | ||
| 239 | + return true; | ||
| 240 | + } | ||
| 241 | + // }}} | ||
| 242 | + | ||
| 243 | + // {{{ setIncomplete | ||
| 244 | + function setIncomplete(&$oDocument, $reason) { | ||
| 245 | + $oDocument->setStatusID(STATUS_INCOMPLETE); | ||
| 246 | + $table = "document_incomplete"; | ||
| 247 | + $iId = $oDocument->getID(); | ||
| 248 | + $aIncomplete = DBUtil::getOneResult(array("SELECT * FROM $table WHERE id = ?", array($iId))); | ||
| 249 | + if (PEAR::isError($aIncomplete)) { | ||
| 250 | + return $aIncomplete; | ||
| 251 | + } | ||
| 252 | + if (is_null($aIncomplete)) { | ||
| 253 | + $aIncomplete = array("id" => $iId); | ||
| 254 | + } | ||
| 255 | + $aIncomplete[$reason] = true; | ||
| 256 | + $res = DBUtil::autoDelete($table, $iId); | ||
| 257 | + if (PEAR::isError($res)) { | ||
| 258 | + return $res; | ||
| 259 | + } | ||
| 260 | + $res = DBUtil::autoInsert($table, $aIncomplete); | ||
| 261 | + if (PEAR::isError($res)) { | ||
| 262 | + return $res; | ||
| 263 | + } | ||
| 264 | + return true; | ||
| 265 | + } | ||
| 266 | + // }}} | ||
| 267 | + | ||
| 268 | + // {{{ setComplete | ||
| 269 | + function setComplete(&$oDocument, $reason) { | ||
| 270 | + $table = "document_incomplete"; | ||
| 271 | + $iId = $oDocument->getID(); | ||
| 272 | + $aIncomplete = DBUtil::getOneResult(array("SELECT * FROM $table WHERE id = ?", array($iId))); | ||
| 273 | + if (PEAR::isError($aIncomplete)) { | ||
| 274 | + return $aIncomplete; | ||
| 275 | + } | ||
| 276 | + | ||
| 277 | + if (is_null($aIncomplete)) { | ||
| 278 | + $oDocument->setStatusID(LIVE); | ||
| 279 | + return true; | ||
| 280 | + } | ||
| 281 | + | ||
| 282 | + $aIncomplete[$reason] = false; | ||
| 283 | + | ||
| 284 | + $bIncomplete = false; | ||
| 285 | + | ||
| 286 | + foreach ($aIncomplete as $k => $v) { | ||
| 287 | + if ($k === "id") { continue; } | ||
| 288 | + | ||
| 289 | + if ($v) { | ||
| 290 | + $bIncomplete = true; | ||
| 291 | + } | ||
| 292 | + } | ||
| 293 | + | ||
| 294 | + if ($bIncomplete === false) { | ||
| 295 | + DBUtil::autoDelete($table, $iId); | ||
| 296 | + $oDocument->setStatusID(LIVE); | ||
| 297 | + return true; | ||
| 298 | + } | ||
| 299 | + | ||
| 300 | + $res = DBUtil::autoDelete($table, $iId); | ||
| 301 | + if (PEAR::isError($res)) { | ||
| 302 | + return $res; | ||
| 303 | + } | ||
| 304 | + $res = DBUtil::autoInsert($table, $aIncomplete); | ||
| 305 | + if (PEAR::isError($res)) { | ||
| 306 | + return $res; | ||
| 307 | + } | ||
| 308 | + } | ||
| 309 | + // }}} | ||
| 310 | + | ||
| 311 | + // {{{ add | ||
| 312 | + function &add($oFolder, $sFilename, $oUser, $aOptions) { | ||
| 313 | + if (KTDocumentUtil::exists($oFolder, $sFilename)) { | ||
| 314 | + return PEAR::raiseError("File already exists"); | ||
| 315 | + } | ||
| 316 | + $oDocument =& KTDocumentUtil::_add($oFolder, $sFilename, $oUser, $aOptions); | ||
| 317 | + | ||
| 318 | + //create the web document link | ||
| 319 | + $oWebDocument = & new WebDocument($oDocument->getID(), -1, 1, NOT_PUBLISHED, getCurrentDateTime()); | ||
| 320 | + $res = $oWebDocument->create(); | ||
| 321 | + if (PEAR::isError($res)) { | ||
| 322 | + $oDocument->delete(); | ||
| 323 | + return $res; | ||
| 324 | + } | ||
| 325 | + | ||
| 326 | + $aOptions = array('user' => $oUser); | ||
| 327 | + //create the document transaction record | ||
| 328 | + $oDocumentTransaction = & new DocumentTransaction($oDocument->getID(), "Document created", CREATE, $aOptions); | ||
| 329 | + $res = $oDocumentTransaction->create(); | ||
| 330 | + if (PEAR::isError($res)) { | ||
| 331 | + $oDocument->delete(); | ||
| 332 | + $oWebDocument->delete(); | ||
| 333 | + return $res; | ||
| 334 | + } | ||
| 335 | + | ||
| 336 | + return $oDocument; | ||
| 337 | + } | ||
| 338 | + // }}} | ||
| 339 | + | ||
| 340 | + // {{{ exists | ||
| 341 | + function exists($oFolder, $sFilename) { | ||
| 342 | + return Document::documentExists($sFilename, $oFolder->getID()); | ||
| 343 | + } | ||
| 344 | + // }}} | ||
| 345 | + | ||
| 346 | + // {{{ storeContents | ||
| 347 | + /** | ||
| 348 | + * Stores contents (filelike) from source into the document storage | ||
| 349 | + */ | ||
| 350 | + function storeContents(&$oDocument, $oContents, $aOptions = null) { | ||
| 351 | + if (is_null($aOptions)) { | ||
| 352 | + $aOptions = array(); | ||
| 353 | + } | ||
| 354 | + $bCanMove = KTUtil::arrayGet($aOptions, 'move'); | ||
| 355 | + $oStorage =& KTStorageManagerUtil::getSingleton(); | ||
| 356 | + $sFilename = tempnam('/tmp', 'kt_storecontents'); | ||
| 357 | + $oOutputFile = new KTFSFileLike($sFilename); | ||
| 358 | + $res = KTFileLikeUtil::copy_contents($oContents, $oOutputFile); | ||
| 359 | + if (!$oStorage->upload($oDocument, $sFilename)) { | ||
| 360 | + return PEAR::raiseError("Couldn't store contents"); | ||
| 361 | + } | ||
| 362 | + KTDocumentUtil::setComplete($oDocument, "contents"); | ||
| 363 | + return true; | ||
| 364 | + } | ||
| 365 | + // }}} | ||
| 147 | } | 366 | } |
| 148 | 367 | ||
| 149 | ?> | 368 | ?> |