Commit 0e310f32f67db05445184d1f4e1febb1b5f659ae

Authored by nbm
1 parent 09e93793

DocumentUtil will hold utility functions to handle process and

life-cycle of document model objects.  Currently, it handles the
check-in process.


git-svn-id: https://kt-dms.svn.sourceforge.net/svnroot/kt-dms/trunk@3528 c91229c3-7414-0410-bfa2-8a42b809f60b
lib/documentmanagement/documentutil.inc.php 0 → 100644
  1 +<?php /* vim: set expandtab softtabstop=4 shiftwidth=4 foldmethod=marker: */
  2 +/**
  3 + * $Id$
  4 + *
  5 + * Document-handling utility functions
  6 + *
  7 + * Simplifies and canonicalises operations such as adding, updating, and
  8 + * deleting documents from the repository.
  9 + *
  10 + * Copyright (c) 2005 Jam Warehouse http://www.jamwarehouse.com
  11 + *
  12 + * This program is free software; you can redistribute it and/or modify
  13 + * it under the terms of the GNU General Public License as published by
  14 + * the Free Software Foundation; either version 2 of the License, or
  15 + * (at your option) any later version.
  16 + *
  17 + * This program is distributed in the hope that it will be useful,
  18 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20 + * GNU General Public License for more details.
  21 + *
  22 + * You should have received a copy of the GNU General Public License
  23 + * along with this program; if not, write to the Free Software
  24 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  25 + *
  26 + * @version $Revision$
  27 + * @author Neil Blakey-Milner, Jam Warehouse (Pty) Ltd, South Africa
  28 + */
  29 +
  30 +require_once(KT_LIB_DIR . '/documentmanagement/DocumentFieldLink.inc');
  31 +
  32 +class KTDocumentUtil {
  33 + function createMetadataVersion($oDocument) {
  34 + if (is_numeric($oDocument)) {
  35 + $oDocument =& Document::get($oDocument);
  36 + if (PEAR::isError($oDocument)) {
  37 + return $oDocument;
  38 + }
  39 + }
  40 + // XXX: PHP5 clone
  41 + $oVersionDocument = $oDocument;
  42 + $oVersionDocument->iId = -1;
  43 + $oVersionDocument->setStatusID(STATUS_VERSION);
  44 + $oVersionDocument->setLiveDocumentID($oDocument->getID());
  45 + $oVersionDocument->setIsCheckedOut(false);
  46 + $oVersionDocument->setCheckedOutUserID(null);
  47 + $res = $oVersionDocument->create();
  48 + if ($res !== true) {
  49 + if (PEAR::isError($res)) {
  50 + return $res;
  51 + }
  52 + // XXX: Remove when Document uses PEAR Errors
  53 + return PEAR::raiseError($_SESSION["errorMessage"]);
  54 + }
  55 +
  56 + $aFields =& DocumentFieldLink::getByDocument($oDocument);
  57 + $iVersionDocumentID = $oVersionDocument->getID();
  58 + foreach ($aFields as $oDFL) {
  59 + // XXX: PHP5 clone
  60 + $oVersionDFL = $oDFL;
  61 + $oVersionDFL->iId = -1;
  62 + $oVersionDFL->setDocumentID($iVersionDocumentID);
  63 + $res = $oVersionDFL->create();
  64 + }
  65 +
  66 + return $oVersionDocument;
  67 + }
  68 +
  69 + function bumpVersion($oDocument) {
  70 + if (is_numeric($oDocument)) {
  71 + $oDocument =& Document::get($oDocument);
  72 + if (PEAR::isError($oDocument)) {
  73 + return $oDocument;
  74 + }
  75 + }
  76 + $oDocument->setMetadataVersion($oDocument->getMetadataVersion()+1);
  77 + return $oDocument->update();
  78 + }
  79 +
  80 + function setModifiedDate($oDocument) {
  81 + if (is_numeric($oDocument)) {
  82 + $oDocument =& Document::get($oDocument);
  83 + if (PEAR::isError($oDocument)) {
  84 + return $oDocument;
  85 + }
  86 + }
  87 + $oDocument->setLastModifiedDate(getCurrentDateTime());
  88 + return $oDocument->update();
  89 + }
  90 +
  91 + function checkin($oDocument, $sFilename, $sCheckInComment, $sCheckInType = "minor") {
  92 + $sBackupPath = $oDocument->getPath() . "-" . $oDocument->getMajorVersionNumber() . "." . $oDocument->getMinorVersionNumber();
  93 + $bSuccess = @copy($oDocument->getPath(), $sBackupPath);
  94 + if ($bSuccess === false) {
  95 + return PEAR::raiseError(_("Unable to backup document prior to upload"));
  96 + }
  97 + $oVersionedDocument = KTDocumentUtil::createMetadataVersion($oDocument);
  98 + if (PEAR::isError($oVersionedDocument)) {
  99 + return $oVersionedDocument;
  100 + }
  101 +
  102 + if (!PhysicalDocumentManager::uploadPhysicalDocument($oDocument, $oDocument->getFolderID(), "", $sFilename)) {
  103 + // reinstate the backup
  104 + copy($sBackupPath, $oDocument->getPath());
  105 + // remove the backup
  106 + unlink($sBackupPath);
  107 + return PEAR::raiseError(_("An error occurred while storing the new file on the filesystem"));
  108 + }
  109 +
  110 + $oDocument->setMetadataVersion($oDocument->getMetadataVersion()+1);
  111 +
  112 + $oDocument->setFileSize($_FILES['fFile']['size']);
  113 + $oDocument->setLastModifiedDate(getCurrentDateTime());
  114 + $oDocument->setIsCheckedOut(false);
  115 + $oDocument->setCheckedOutUserID(-1);
  116 +
  117 + // bump the version numbers
  118 + if ($sCheckInType == "major") {
  119 + $oDocument->setMajorVersionNumber($oDocument->getMajorVersionNumber()+1);
  120 + $oDocument->setMinorVersionNumber(0);
  121 + } else if ($sCheckInType == "minor") {
  122 + $oDocument->setMinorVersionNumber($oDocument->getMinorVersionNumber()+1);
  123 + }
  124 +
  125 + $bSuccess = $oDocument->update();
  126 + if ($bSuccess !== true) {
  127 + if (PEAR::isError($bSuccess)) {
  128 + return $bSuccess;
  129 + }
  130 + return PEAR::raiseError(_("An error occurred while storing this document in the database"));
  131 + }
  132 +
  133 + // create the document transaction record
  134 + $oDocumentTransaction = & new DocumentTransaction($oDocument->getID(), $sCheckInComment, CHECKIN);
  135 + // TODO: check transaction creation status?
  136 + $oDocumentTransaction->create();
  137 +
  138 + // fire subscription alerts for the checked in document
  139 + $count = SubscriptionEngine::fireSubscription($oDocument->getID(), SubscriptionConstants::subscriptionAlertType("CheckInDocument"),
  140 + SubscriptionConstants::subscriptionType("DocumentSubscription"),
  141 + array( "folderID" => $oDocument->getFolderID(),
  142 + "modifiedDocumentName" => $oDocument->getName() ));
  143 + global $default;
  144 + $default->log->info("checkInDocumentBL.php fired $count subscription alerts for checked out document " . $oDocument->getName());
  145 + return true;
  146 + }
  147 +}
  148 +
  149 +?>
... ...