Commit 097bd1e19ef26461fb56552c6be930666e21844f

Authored by nbm
1 parent d2d4909d

Make document addition a single-page step, and make it aware of

fieldsets instead of dealing with fields directly.


git-svn-id: https://kt-dms.svn.sourceforge.net/svnroot/kt-dms/trunk@3686 c91229c3-7414-0410-bfa2-8a42b809f60b
presentation/lookAndFeel/knowledgeTree/documentmanagement/addDocument.php 0 → 100644
  1 +<?php
  2 +/**
  3 + * $Id$
  4 + *
  5 + * Web interface to adding a document to a folder
  6 + *
  7 + * Copyright (c) 2005 Jam Warehouse http://www.jamwarehouse.com
  8 + *
  9 + * This program is free software; you can redistribute it and/or modify
  10 + * it under the terms of the GNU General Public License as published by
  11 + * the Free Software Foundation; either version 2 of the License, or
  12 + * (at your option) any later version.
  13 + *
  14 + * This program is distributed in the hope that it will be useful,
  15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17 + * GNU General Public License for more details.
  18 + *
  19 + * You should have received a copy of the GNU General Public License
  20 + * along with this program; if not, write to the Free Software
  21 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22 + *
  23 + * @version $Revision$
  24 + * @author Neil Blakey-Milner, Jam Warehouse (Pty) Ltd, South Africa
  25 + */
  26 +
  27 +require_once("../../../../config/dmsDefaults.php");
  28 +require_once(KT_LIB_DIR . '/dispatcher.inc.php');
  29 +
  30 +// KTUtil::extractGPC('fFolderID', 'fStore', 'fDocumentTypeID', 'fName', 'fDependantDocumentID');
  31 +
  32 +require_once(KT_LIB_DIR . '/documentmanagement/Document.inc');
  33 +require_once(KT_LIB_DIR . '/foldermanagement/Folder.inc');
  34 +require_once(KT_LIB_DIR . '/documentmanagement/DependantDocumentInstance.inc');
  35 +require_once(KT_LIB_DIR . '/documentmanagement/DocumentLink.inc');
  36 +
  37 +require_once(KT_LIB_DIR . '/storage/storagemanager.inc.php');
  38 +require_once(KT_LIB_DIR . '/filelike/fsfilelike.inc.php');
  39 +require_once(KT_LIB_DIR . '/documentmanagement/DocumentType.inc');
  40 +require_once(KT_LIB_DIR . '/metadata/fieldset.inc.php');
  41 +require_once(KT_LIB_DIR . '/documentmanagement/documentutil.inc.php');
  42 +require_once(KT_LIB_DIR . '/visualpatterns/PatternMetaData.inc');
  43 +
  44 +require_once(KT_DIR . '/presentation/webpageTemplate.inc');
  45 +
  46 +$oStorage =& KTStorageManagerUtil::getSingleton();
  47 +
  48 +class KTAddDocumentDispatcher extends KTStandardDispatcher {
  49 + function check() {
  50 + $this->validateFolder($_REQUEST['fFolderId']);
  51 + $this->validatePermission('ktcore.permissions.write');
  52 + $this->validateFolderPermission();
  53 + $this->validatePost();
  54 + return true;
  55 + }
  56 +
  57 + function validateFolder($iFolderId) {
  58 + $this->oFolder =& Folder::get($iFolderId);
  59 + if (PEAR::isError($this->oFolder) || ($this->oFolder === false)) {
  60 + $this->errorPage(_("Invalid folder given"));
  61 + exit(0);
  62 + }
  63 + }
  64 +
  65 + function validatePermission($sPermission) {
  66 + $this->oPermission =& KTPermission::getByName($sPermission);
  67 + if (PEAR::isError($this->oPermission) || ($this->oPermission === false)) {
  68 + $this->errorPage(_("Permission not found"));
  69 + exit(0);
  70 + }
  71 + }
  72 +
  73 + function validateDocumentType($iId) {
  74 + $this->oDocumentType =& DocumentType::get($iId);
  75 + if (PEAR::isError($this->oDocumentType) || ($this->oDocumentType === false)) {
  76 + $this->errorPage(_("Invalid document type given"));
  77 + exit(0);
  78 + }
  79 + }
  80 +
  81 + function validateFolderPermission() {
  82 + $oUser =& User::get($_SESSION['userID']);
  83 + if (!KTPermissionUtil::userHasPermissionOnItem($oUser, $this->oPermission, $this->oFolder)) {
  84 + $this->errorPage(_("Permission denied"));
  85 + exit(0);
  86 + }
  87 + }
  88 +
  89 + function validatePost() {
  90 + $postExpected = KTUtil::arrayGet($_REQUEST, "postExpected");
  91 + $postReceived = KTUtil::arrayGet($_REQUEST, "postReceived");
  92 +
  93 + if (is_null($postExpected)) {
  94 + return;
  95 + }
  96 +
  97 + if (!is_null($postReceived)) {
  98 + return;
  99 + }
  100 +
  101 + $this->errorPage(_("You tried to upload a file that is larger than the PHP post_max_size setting."));
  102 + exit(0);
  103 + }
  104 +
  105 + function errorPage($errorMessage) {
  106 + $this->handleOutput($errorMessage);
  107 + exit(0);
  108 + }
  109 +
  110 + function do_main() {
  111 + $oTemplating =& KTTemplating::getSingleton();
  112 + $oTemplate =& $oTemplating->loadTemplate("ktcore/document/add");
  113 + $aTypes = $this->getDocumentTypes();
  114 + $iDefaultType = $aTypes[0]->getId();
  115 + $aTemplateData = array(
  116 + 'folder_id' => $this->oFolder->getID(),
  117 + 'folder_path_array' => $this->oFolder->getPathArray(),
  118 + 'document_type_choice' => $this->getDocumentTypeChoice($aTypes, 'getMetadataForType(this.value);'),
  119 + 'generic_metadata_fields' => $this->getGenericMetadataFieldsets(),
  120 + 'type_metadata_fields' => $this->getTypeMetadataFieldsets($iDefaultType),
  121 + );
  122 + $oTemplate->setData($aTemplateData);
  123 + return $oTemplate->render();
  124 + }
  125 +
  126 + function getDocumentTypeChoice($aTypes, $onchange = "") {
  127 + $oTemplating =& KTTemplating::getSingleton();
  128 + $oTemplate = $oTemplating->loadTemplate('ktcore/document/document_type_choice');
  129 + $aFields = array(
  130 + 'document_types' => $aTypes,
  131 + 'onchange' => $onchange,
  132 + );
  133 + return $oTemplate->render($aFields);
  134 + }
  135 +
  136 + function getGenericMetadataFieldsets() {
  137 + $oTemplating = KTTemplating::getSingleton();
  138 + $oTemplate = $oTemplating->loadTemplate("ktcore/metadata_fields/editable_metadata_fieldsets");
  139 + $aTemplateData = array(
  140 + 'caption' => _('Generic meta data'),
  141 + 'empty_message' => _("No Generic Meta Data"),
  142 + 'fieldsets' => KTFieldset::getGenericFieldsets(),
  143 + );
  144 + return $oTemplate->render($aTemplateData);
  145 + }
  146 +
  147 + function getTypeMetadataFieldsets($iDocumentTypeID) {
  148 + $aTemplateData = array(
  149 + 'caption' => _('Type specific meta data'),
  150 + 'empty_message' => _("No Type Specific Meta Data"),
  151 + 'fieldsets' => KTFieldset::getForDocumentType($iDocumentTypeID),
  152 + );
  153 + $oTemplating = KTTemplating::getSingleton();
  154 + $oTemplate = $oTemplating->loadTemplate("ktcore/metadata_fields/editable_metadata_fieldsets");
  155 + return $oTemplate->render($aTemplateData);
  156 + }
  157 +
  158 + function getDocumentTypes() {
  159 + if (!$this->oFolder->getRestrictDocumentTypes()) {
  160 + return DocumentType::getList();
  161 + }
  162 + $sTable = KTUtil::getTableName('folder_doctypes');
  163 + $aQuery = array(
  164 + "SELECT document_type_id FROM $sTable WHERE folder_id = ?",
  165 + array($this->oFolder->getId()),
  166 + );
  167 + $aIds = DBUtil::getResultArrayKey($aQuery, 'document_type_id');
  168 + $aRet = array();
  169 + foreach ($aIds as $iId) {
  170 + $aRet[] = DocumentType::get($iId);
  171 + }
  172 + return $aRet;
  173 + }
  174 +
  175 + function do_upload() {
  176 + // make sure the user actually selected a file first
  177 + // and that something was uploaded
  178 + if (!((strlen($_FILES['fFile']['name']) > 0) && $_FILES['fFile']['size'] > 0)) {
  179 + // no uploaded file
  180 + $message = _("You did not select a valid document to upload");
  181 +
  182 + $errors = array(
  183 + 1 => _("The uploaded file is larger than the PHP upload_max_filesize setting"),
  184 + 2 => _("The uploaded file is larger than the MAX_FILE_SIZE directive that was specified in the HTML form"),
  185 + 3 => _("The uploaded file was not fully uploaded to KnowledgeTree"),
  186 + 4 => _("No file was selected to be uploaded to KnowledgeTree"),
  187 + 6 => _("An internal error occurred receiving the uploaded document"),
  188 + );
  189 + $message = KTUtil::arrayGet($errors, $_FILES['fFile']['error'], $message);
  190 +
  191 + if (@ini_get("file_uploads") == false) {
  192 + $message = _("File uploads are disabled in your PHP configuration");
  193 + }
  194 +
  195 + $this->errorPage($message);
  196 + exit(0);
  197 + }
  198 +
  199 + DBUtil::startTransaction();
  200 +
  201 + $matches = array();
  202 + $aFields = array();
  203 + foreach ($_REQUEST as $k => $v) {
  204 + if (preg_match('/^emd(\d+)$/', $k, $matches)) {
  205 + $aFields[] = array(DocumentField::get($matches[1]), $v);
  206 + }
  207 + }
  208 +
  209 + $this->validateDocumentType($_REQUEST['fDocumentTypeID']);
  210 +
  211 + $aOptions = array(
  212 + 'contents' => new KTFSFileLike($_FILES['fFile']['tmp_name']),
  213 + 'documenttype' => $this->oDocumentType,
  214 + 'metadata' => $aFields,
  215 + 'description' => $_REQUEST['fName'],
  216 + );
  217 +
  218 + $oUser =& User::get($_SESSION["userID"]);
  219 + $oDocument =& KTDocumentUtil::add($this->oFolder, basename($_FILES['fFile']['name']), $oUser, $aOptions);
  220 + if (PEAR::isError($oDocument)) {
  221 + localRenderError($oDocument);
  222 + exit(0);
  223 + }
  224 +
  225 + //the document was created/uploaded due to a collaboration step in another
  226 + //document and must be linked to that document
  227 + if (isset($fDependantDocumentID)) {
  228 + $oDependantDocument = DependantDocumentInstance::get($fDependantDocumentID);
  229 + $oDocumentLink = & new DocumentLink($oDependantDocument->getParentDocumentID(), $oDocument->getID(), -1); // XXX: KT_LINK_DEPENDENT
  230 + if ($oDocumentLink->create()) {
  231 + //no longer a dependant document, but a linked document
  232 + $oDependantDocument->delete();
  233 + } else {
  234 + //an error occured whilst trying to link the two documents automatically. Email the parent document
  235 + //original to inform him/her that the two documents must be linked manually
  236 + $oParentDocument = Document::get($oDependantDocument->getParentDocumentID());
  237 + $oUserDocCreator = User::get($oParentDocument->getCreatorID());
  238 +
  239 + $sBody = $oUserDocCreator->getName() . ", an error occured whilst attempting to automatically link the document, '" .
  240 + $oDocument->getName() . "' to the document, '" . $oParentDocument->getName() . "'. These two documents " .
  241 + " are meant to be linked for collaboration purposes. As creator of the document, ' " . $oParentDocument->getName() . "', you are requested to " .
  242 + "please link them manually by browsing to the parent document, " .
  243 + generateControllerLink("viewDocument","fDocumentID=" . $oParentDocument->getID(), $oParentDocument->getName()) .
  244 + " and selecting the link button. " . $oDocument->getName() . " can be found at " . $oDocument->getDisplayPath();
  245 +
  246 + $oEmail = & new Email();
  247 + $oEmail->send($oUserDocCreator->getEmail(), "Automatic document linking failed", $sBody);
  248 +
  249 + //document no longer dependant document, but must be linked manually
  250 + $oDependantDocument->delete();
  251 + }
  252 + }
  253 +
  254 + DBUtil::commit();
  255 + //redirect to the document details page
  256 + controllerRedirect("viewDocument", "fDocumentID=" . $oDocument->getID());
  257 + }
  258 +}
  259 +$d =& new KTAddDocumentDispatcher;
  260 +$d->dispatch();
  261 +
  262 +
  263 +?>
templates/ktcore/document/add.smarty 0 → 100644
  1 +<script language="javascript" src="/thirdpartyjs/MochiKit/Base.js">
  2 +</script>
  3 +<script language="javascript" src="/thirdpartyjs/MochiKit/Async.js">
  4 +</script>
  5 +<script language="javascript" src="/thirdpartyjs/MochiKit/DOM.js">
  6 +</script>
  7 +{literal}
  8 +<script language="javascript">
  9 +
  10 +var fetching = null;
  11 +
  12 +function swapInItem(req) {
  13 + var cp = getElement(fetching);
  14 + cp.innerHTML = req.responseText;
  15 +}
  16 +
  17 +function xmlFailure(err) {
  18 + alert('failed');
  19 +}
  20 +
  21 +function swapElementFromRequest(elementId, url) {
  22 + var deff = doSimpleXMLHttpRequest(url);
  23 + deff.addCallback(swapInItem);
  24 + fetching = elementId;
  25 + var cp = getElement(fetching);
  26 + cp.innerHTML="loading...";
  27 +}
  28 +
  29 +function getMetadataForType(id) {
  30 + swapElementFromRequest('type_metadata_fields',
  31 + 'http://ktcvs.local/presentation/lookAndFeel/knowledgeTree/documentmanagement/getTypeMetadataFields.php?fDocumentTypeID='
  32 + + id);
  33 +}
  34 +
  35 +</script>
  36 +{/literal}
  37 +
  38 +<h1>Add a document</h1>
  39 +
  40 +<form method="POST" action="{$smarty.server.PHP_SELF}" enctype="multipart/form-data">
  41 +<input type="hidden" name="action" value="upload">
  42 +<input type="hidden" name="postReceived" value="1">
  43 +
  44 +<table class="prettysw" cellspacing="0" cellpadding="0">
  45 + <tr>
  46 + <th>Folder</th>
  47 + <td><input type="hidden" name="fFolderId" value="{$folder_id}">{" > "|join:$folder_path_array}</td>
  48 + </tr>
  49 + <tr>
  50 + <th>File</th>
  51 + <td><input type="file" name="fFile"></td>
  52 + </tr>
  53 + <tr>
  54 + <th>Title</th>
  55 + <td><input type="textbox" name="fName"></td>
  56 + </tr>
  57 + <tr>
  58 + <th>Document Type</th>
  59 + <td>{$document_type_choice}</td>
  60 + </tr>
  61 +</table>
  62 +
  63 +<div id="generic_metadata_fields">
  64 +{$generic_metadata_fields}
  65 +</div>
  66 +
  67 +<div id="type_metadata_fields">
  68 +{$type_metadata_fields}
  69 +</div>
  70 +
  71 +<input type="submit" name="submit" value="Import">
  72 +</form>