Commit 24cd3a4f7a697d581afd92990b74f3560bfdaafc

Authored by nbm
1 parent 92c89362

Use the KTZipImportStorage and BulkImportManager infrastructure to do

the ZIP uploads.


git-svn-id: https://kt-dms.svn.sourceforge.net/svnroot/kt-dms/trunk@3638 c91229c3-7414-0410-bfa2-8a42b809f60b
presentation/lookAndFeel/knowledgeTree/documentmanagement/bulkUploadBL.php
... ... @@ -39,7 +39,6 @@ require_once("$default->fileSystemRoot/lib/database/datetime.inc");
39 39 require_once("$default->fileSystemRoot/lib/documentmanagement/BulkUploadManager.inc");
40 40 require_once("$default->fileSystemRoot/lib/documentmanagement/Document.inc");
41 41 require_once("$default->fileSystemRoot/lib/documentmanagement/DocumentTransaction.inc");
42   -require_once("$default->fileSystemRoot/lib/foldermanagement/Folder.inc");
43 42 require_once("$default->fileSystemRoot/lib/security/Permission.inc");
44 43 require_once("$default->fileSystemRoot/lib/subscriptions/SubscriptionEngine.inc");
45 44 require_once("$default->fileSystemRoot/lib/visualpatterns/PatternCustom.inc");
... ... @@ -52,14 +51,16 @@ require_once("$default->fileSystemRoot/presentation/Html.inc");
52 51 require_once("$default->fileSystemRoot/presentation/webpageTemplate.inc");
53 52 require_once("bulkUploadUI.inc");
54 53  
55   -require_once(KT_LIB_DIR . '/storage/storagemanager.inc.php');
56   -require_once(KT_LIB_DIR . '/mime.inc.php');
  54 +require_once(KT_LIB_DIR . '/foldermanagement/Folder.inc');
  55 +require_once(KT_LIB_DIR . '/users/User.inc');
57 56  
58   -$oStorage =& KTStorageManagerUtil::getSingleton();
  57 +require_once(KT_LIB_DIR . '/import/bulkimport.inc.php');
  58 +require_once(KT_LIB_DIR . '/import/zipimportstorage.inc.php');
59 59  
60 60 $oPatternCustom = & new PatternCustom();
61 61  
62 62 /* CHECK: system has required features to handle bulk upload */
  63 +/*
63 64 if (!BulkUploadManager::isBulkUploadCapable()) {
64 65 // can't do bulk uploading
65 66 $sErrorMessage = _("This system is not capable of handling bulk uploads") . ". <br/>\n"
... ... @@ -70,6 +71,7 @@ if (!BulkUploadManager::isBulkUploadCapable()) {
70 71 $main->render();
71 72 exit(0);
72 73 }
  74 +*/
73 75  
74 76 $postExpected = KTUtil::arrayGet($_REQUEST, "postExpected");
75 77 $postReceived = KTUtil::arrayGet($_REQUEST, "postReceived");
... ... @@ -164,6 +166,15 @@ if (!((strlen($_FILES[&#39;fFile&#39;][&#39;name&#39;]) &gt; 0) &amp;&amp; $_FILES[&#39;fFile&#39;][&#39;size&#39;] &gt; 0)) {
164 166 exit(0);
165 167 }
166 168  
  169 +// if changing this function, also change related error message
  170 +function isValidBulkUpload() {
  171 + return (strlen($_FILES['fFile']['name']) > 0)
  172 + && file_exists($_FILES['fFile']['tmp_name'])
  173 + && $_FILES['fFile']['size'] > 0
  174 + && (!$_FILES['fFile']['error'])
  175 + && preg_match('/\.zip/i', $_FILES['fFile']['name']);
  176 +}
  177 +
167 178 /* CHECK: bulk upload is valid */
168 179 if (!isValidBulkUpload()) {
169 180 $sErrorMessage = getInvalidBulkUploadErrorMsg() . getRetryUploadButton($fFolderID, $fDocumentTypeID);
... ... @@ -177,117 +188,30 @@ if (!isValidBulkUpload()) {
177 188 exit(0);
178 189 }
179 190  
180   -/* create temp dir, extract contents of .zip file to temp dir */
181   -// pass path of ZIP file to bulk upload manager to get names, paths of uploaded files
182   -// manager returns an array of ZipFile objects
183   -$aIndividualFiles = BulkUploadManager::unzipToTempDir($_FILES['fFile']['tmp_name'], $_FILES['fFile']['name']);
184   -
185   -/* CHECK: found good stuff in ZIP file */
186   -$aFileStatus = array();
187   -if (!$aIndividualFiles) {
188   - // error extracting from ZIP file
189   - $sErrorMessage = getInvalidBulkUploadErrorMsg() . getRetryUploadButton($fFolderID, $fDocumentTypeID);
190   -}
191   -
192   -while ($aIndividualFiles) {
193   - /* perform uploading logic as in addDocumentBL.php for each ZipFile */
194   - // DON'T die if uploading a particular file fails, keep errors for displaying later
195   - $oFile = array_pop($aIndividualFiles);
196   -
197   - $sBasename = basename($oFile->sFilename);
198   - $aFileFake = array(
199   - 'name' => $sBasename,
200   - 'type' => KTMime::getMimeTypeFromFile($oFile->sFilename),
201   - 'tmp_name' => $oFile->sFilename,
202   - 'error' => 0,
203   - 'size' => $oFile->iSize,
204   - );
205   -
206   - // create the document in the database
207   - $oDocument = & PhysicalDocumentManager::createDocumentFromUploadedFile($aFileFake, $fFolderID);
208   - // set the document title
209   - $oDocument->setName($sBasename);
210   - // set the document type
211   - $oDocument->setDocumentTypeID($fDocumentTypeID);
212   -
213   - if (Document::documentExists($oDocument->getFileName(), $oDocument->getFolderID())) {
214   - $aFileStatus[$sBasename] = _("A document with this file name already exists in this folder") . ".";
215   - continue;
216   - }
217   -
218   - $sFolderPath = Folder::getFolderPath($fFolderID);
219   -
220   - if (!$oDocument->create()) {
221   - $default->log->error("bulkUploadBL.php DB error storing document in folder $sFolderPath id=$fFolderID");
222   - $aFileStatus[$sBasename] = _("An error occured while storing the document in the database, please try again") . ".";
223   - continue;
224   - }
225   -
226   - // if the document was successfully created in the db, store it on the file system
227   - if (!$oStorage->upload($oDocument, $oFile->sFilename)) {
228   - $default->log->error("bulkUploadBL.php DB error storing document in folder $sFolderPath id=$fFolderID");
229   - $aFileStatus[$sBasename] = _("An error occured while storing the document in the database, please try again") . ".";
230   - continue;
  191 +$matches = array();
  192 +$aFields = array();
  193 +foreach ($_REQUEST as $k => $v) {
  194 + if (preg_match('/^emd(\d+)$/', $k, $matches)) {
  195 + $aFields[] = array(DocumentField::get($matches[1]), $v);
231 196 }
232   -
233   - $oDocument->update();
234   -
235   - // create the web document link
236   - $oWebDocument = & new WebDocument($oDocument->getID(), -1, 1, NOT_PUBLISHED, getCurrentDateTime());
237   - if ($oWebDocument->create()) {
238   - $default->log->info("bulkUploadBL.php created web document for document ID=" . $oDocument->getID());
239   - } else {
240   - $default->log->error("bulkUploadBL.php couldn't create web document for document ID=" . $oDocument->getID());
241   - }
242   -
243   - // create the document transaction record
244   - $oDocumentTransaction = & new DocumentTransaction($oDocument->getID(), "Document created", CREATE);
245   - if ($oDocumentTransaction->create()) {
246   - $default->log->debug("bulkUploadBL.php created create document transaction for document ID=" . $oDocument->getID());
247   - } else {
248   - $default->log->error("bulkUploadBL.php couldn't create create document transaction for document ID=" . $oDocument->getID());
249   - }
250   -
251   - // now handle meta data, pass new document id to queries
252   - $aQueries = constructQuery(array_keys($_POST), array("document_id" =>$oDocument->getID()));
253   - for ($i=0; $i<count($aQueries); $i++) {
254   - $sql = $default->db;
255   - if ($sql->query($aQueries[$i])) {
256   - $default->log->info("bulkUploadBL.php query succeeded=" . $aQueries[$i]);
257   - } else {
258   - $default->log->error("bulkUploadBL.php query failed=" . $aQueries[$i]);
259   - }
260   - }
261   -
262   - // fire subscription alerts for the new document
263   - $count = SubscriptionEngine::fireSubscription($fFolderID, SubscriptionConstants::subscriptionAlertType("AddDocument"),
264   - SubscriptionConstants::subscriptionType("FolderSubscription"),
265   - array( "newDocumentName" => $oDocument->getName(),
266   - "folderName" => Folder::getFolderName($fFolderID)));
267   - $default->log->info("bulkUploadBL.php fired $count subscription alerts for new document " . $oDocument->getName());
268   -
269   - /* display a status page with per-file results for bulk upload */
270   - $default->log->info("bulkUploadBL.php successfully added document " . $oDocument->getFileName() . " to folder $sFolderPath id=$fFolderID");
271   - /* store status for this document for later display */
272   - $aFileStatus[$oDocument->getName()] = _("Successfully added document");
273 197 }
274   -$oPatternCustom->setHtml(getStatusPage($fFolderID, $aFileStatus));
275   -
276   -if ($sErrorMessage) {
277   - $main->setErrorMessage($sErrorMessage);
  198 +$aOptions = array(
  199 + 'metadata' => $aFields,
  200 +);
  201 +
  202 +$fs =& new KTZipImportStorage($_FILES['fFile']['tmp_name']);
  203 +$oUser =& User::get($_SESSION['userID']);
  204 +$bm =& new KTBulkImportManager($oFolder, $fs, $oUser, $aOptions);
  205 +
  206 +DBUtil::startTransaction();
  207 +$res = $bm->import();
  208 +if (PEAR::isError($res)) {
  209 + DBUtil::rollback();
  210 + $_SESSION["KTErrorMessage"][] = _("Bulk import failed") . ": " . $res->getMessage();
  211 +} else {
  212 + DBUtil::commit();
278 213 }
279 214  
280   -// render main page
281   -$main->setCentralPayload($oPatternCustom);
282   -$main->render();
283   -
284   -// if changing this function, also change related error message
285   -function isValidBulkUpload() {
286   - return (strlen($_FILES['fFile']['name']) > 0)
287   - && file_exists($_FILES['fFile']['tmp_name'])
288   - && $_FILES['fFile']['size'] > 0
289   - && (!$_FILES['fFile']['error'])
290   - && preg_match('/\.zip/i', $_FILES['fFile']['name']);
291   -}
  215 +controllerRedirect("browse", 'fFolderID=' . $oFolder->getID());
292 216  
293 217 ?>
... ...
presentation/lookAndFeel/knowledgeTree/documentmanagement/bulkUploadUI.inc
... ... @@ -24,31 +24,20 @@
24 24 * @package documentmanagement
25 25 */
26 26  
  27 +require_once(KT_LIB_DIR . '/documentmanagement/DocumentField.inc');
  28 +
27 29 /**
28 30 * Displays the generic meta data fields form
29 31 */
30 32 function getGenericMetaDataForm() {
31   - global $default;
32   - /*ok*/ $sQuery = "SELECT DISTINCT -1 AS document_id, DF.id AS document_field_id, DF.name AS field_name, -1 AS id " .
33   - "FROM document_fields AS DF LEFT OUTER JOIN document_fields_link AS DFL ON DF.id = DFL.document_field_id " .
34   - "WHERE DF.is_generic = 1 ";
35   -
36   - $aStoreColumnNames = array("document_id", "document_field_id", "value");
37   - $aDisplayColumnNames = array("document_id", "field_name", "value");
38   - $aMetaDataColumnNames = array(2=>"field_name");
39   - $aColumnsEditable = array(0,0,1);
40   - $aColumnsVisible = array(0,1,1);
41   - $aColumnDisplayTypes = array(1,1,4);
42   - $aColumnDatabaseTypes = array(4,0,1);
43   -
44   - $oPatternTableSqlQuery = & new PatternEditableTableSqlQuery($sQuery, "document_fields_link", $aStoreColumnNames, $aDisplayColumnNames, $aColumnsEditable, $aColumnsVisible, $aColumnDisplayTypes, $aColumnDatabaseTypes);
45   - $oPatternTableSqlQuery->setTableCaption("Generic Meta Data");
46   - $oPatternTableSqlQuery->setUniqueName("gmd");
47   - $oPatternTableSqlQuery->setRequiredColumnNames(array("value"));
48   - $oPatternTableSqlQuery->setMetaDataFields($aMetaDataColumnNames);
49   - $oPatternTableSqlQuery->setEmptyTableMessage("No Generic Meta Data");
50   - $oPatternTableSqlQuery->setRenderJavascriptValidation(false);
51   - return $oPatternTableSqlQuery;
  33 + $oTemplating = KTTemplating::getSingleton();
  34 + $oTemplate = $oTemplating->loadTemplate("ktcore/metadata_fields/editable_metadata_fields");
  35 + $aTemplateData = array(
  36 + 'caption' => _('Generic meta data'),
  37 + 'empty_message' => _("No Generic Meta Data"),
  38 + 'fields' => DocumentField::getList(array('is_generic = ?', array(true))),
  39 + );
  40 + return $oTemplate->render($aTemplateData);
52 41 }
53 42  
54 43 /**
... ... @@ -56,48 +45,47 @@ function getGenericMetaDataForm() {
56 45 */
57 46 function getTypeSpecificMetaDataForm($iFolderID, $iDocumentTypeID) {
58 47 global $default;
59   - /*ok*/ $sQuery = array("SELECT DISTINCT -1 AS document_id, DF.id AS document_field_id, DF.name AS field_name, DTFL.is_mandatory AS is_mandatory, -1 AS id " .
60   - "FROM document_type_fields_link AS DTFL INNER JOIN document_fields AS DF ON DTFL.field_id = DF.id " .
61   - "LEFT OUTER JOIN document_fields_link AS DFL ON DFL.document_field_id = DTFL.field_id " .
62   - "WHERE DF.is_generic = 0 " .
63   - "AND DTFL.document_type_id = ?", $iDocumentTypeID);
64   -
65   - $aStoreColumnNames = array("document_id", "document_field_id", "value");
66   - $aDisplayColumnNames = array("document_id", "field_name", "value");
67   - $aColumnsEditable = array(0,0,1);
68   - $aColumnsVisible = array(0,1,1);
69   - $aColumnDisplayTypes = array(1,1,4);
70   - $aColumnDatabaseTypes = array(4,0,1);
71   - $aMetaDataColumnNames = array(2=>"field_name");
72   -
73   - $oPatternTableSqlQuery = & new PatternEditableTableSqlQuery($sQuery, "document_fields_link", $aStoreColumnNames, $aDisplayColumnNames, $aColumnsEditable, $aColumnsVisible, $aColumnDisplayTypes, $aColumnDatabaseTypes);
74   - $oPatternTableSqlQuery->setTableCaption("Type specific meta data");
75   - $oPatternTableSqlQuery->setEmptyTableMessage("No Type Specific Meta Data");
76   - $oPatternTableSqlQuery->setUniqueName("tsmd");
77   - $oPatternTableSqlQuery->setMetaDataFields($aMetaDataColumnNames);
78   - $oPatternTableSqlQuery->setRenderJavascriptValidation(false);
79   - return $oPatternTableSqlQuery;
  48 + /*ok*/ $sQuery = array("SELECT DF.id AS id " .
  49 + "FROM document_fields AS DF LEFT JOIN document_type_fields_link AS DTFL ON DTFL.field_id = DF.id " .
  50 + "WHERE DF.is_generic = ? " .
  51 + "AND DTFL.document_type_id = ?", array(false, $iDocumentTypeID));
  52 +
  53 + $aIDs = DBUtil::getResultArray($sQuery);
  54 +
  55 + $aFields = array();
  56 + foreach ($aIDs as $iID) {
  57 + $aFields[] =& call_user_func(array('DocumentField', 'get'), $iID);
  58 + }
  59 + $aTemplateData = array(
  60 + 'caption' => _('Type specific meta data'),
  61 + 'empty_message' => _("No Type Specific Meta Data"),
  62 + 'fields' => $aFields,
  63 + );
  64 + $oTemplating = KTTemplating::getSingleton();
  65 + $oTemplate = $oTemplating->loadTemplate("ktcore/metadata_fields/editable_metadata_fields");
  66 + return $oTemplate->render($aTemplateData);
80 67 }
81 68  
82 69 function getMetaDataForm($iFolderID, $iDocumentTypeID) {
83 70 $oGenericPattern = getGenericMetaDataForm();
84 71 $oTypeSpecificPattern = getTypeSpecificMetaDataForm($iFolderID, $iDocumentTypeID);
85 72 $sToRender .= "<table border=\"0\" width=\"100%\">\n";
86   - $sToRender .= "<tr><td><b>Document Meta Data<b></td></tr>\n";
87   - $sToRender .= "<tr><td valign=\"top\">" . $oGenericPattern->render() . "</td><td valign=\"top\">" . $oTypeSpecificPattern->render() . "</td></tr>";
  73 + $sToRender .= "<tr><td><b>" . _("Document Meta Data") . "<b></td></tr>\n";
  74 + $sToRender .= "<tr><td valign=\"top\">" . $oGenericPattern . "</td></tr>";
  75 + $sToRender .= "<tr><td valign=\"top\">" . $oTypeSpecificPattern . "</td></tr>";
88 76 $sToRender .= "</table>";
89 77  
90 78 // validation starts
91 79 $sToRender .= "\n\n<script language=\"javascript\">\n<!--\n";
92 80 $sToRender .= "function validateForm(theForm) {\n";
  81 + // document title
  82 + $sToRender .= "\tif (!(validRequired(theForm.fName, 'Document Title'))) {\n";
  83 + $sToRender .= "\t\treturn false;\n\t}\n";
93 84 // filename
94   - $sToRender .= "\tif (!(validRequired(theForm.fFile, 'Bulk Upload Zipfile'))) {\n";
  85 + $sToRender .= "\tif (!(validRequired(theForm.fFile, 'Document Filename'))) {\n";
95 86 $sToRender .= "\t\treturn false;\n\t}\n";
96   - // metadata
97   - $sToRender .= $oGenericPattern->getValidationJavascript();
98   - $sToRender .= $oTypeSpecificPattern->getValidationJavascript();
99 87 $sToRender .= "return true;\n}\n";
100   - $sToRender .= "//-->\n</script>\n\n";
  88 + $sToRender .= "//-->\n</script>\n\n";
101 89  
102 90 return $sToRender;
103 91 }
... ... @@ -182,33 +170,6 @@ function getPage($iFolderID, $iDocumentTypeID) {
182 170 return $sToRender;
183 171 }
184 172  
185   -function getStatusPage ($iFolderID, $aFileStatus) {
186   - global $default;
187   -
188   - $sToRender = sprintf(_("Bulk Upload completed for %s documents. <br/>"), count($aFileStatus));
189   - if ($aFileStatus) {
190   - $sToRender .= "<table>\n";
191   - $sToRender .= "<tr>\n";
192   - $sToRender .= "<th> " . _("Document") . " </th>\n";
193   - $sToRender .= "<th> &nbsp; </th>\n";
194   - $sToRender .= "<th> " . _("Status") . " </th>\n";
195   - $sToRender .= "</tr>\n";
196   - foreach ($aFileStatus as $sDocumentName => $sUploadStatus) {
197   - $sToRender .= "<tr>\n";
198   - $sToRender .= "<td> $sDocumentName </td>\n";
199   - $sToRender .= "<td> : </td>\n";
200   - $sToRender .= "<td> $sUploadStatus </td>\n";
201   - $sToRender .= "</tr>\n";
202   - }
203   - $sToRender .= "</table>\n";
204   - }
205   -
206   - // create "DONE" button (to go back to browsing)
207   - $sToRender .= getDoneButton($iFolderID);
208   -
209   - return $sToRender;
210   -}
211   -
212 173 function getInvalidBulkUploadErrorMsg() {
213 174 return _("You did not select a valid document to upload") . ". <br />\n" .
214 175 _("Bulk upload currently only supports .ZIP files without subdirectories") . ".<br />\n";
... ...