Commit 82c4f1a113cdb38448d000f27a4be72d8548ee92
1 parent
4356fed6
KTS-2359
"Automatically fill document title from filename" Added more logic to removing the extension from the title. Committed by: Megan Watson Reviewed by: Conrad Vermeulen git-svn-id: https://kt-dms.svn.sourceforge.net/svnroot/kt-dms/trunk@7300 c91229c3-7414-0410-bfa2-8a42b809f60b
Showing
1 changed file
with
60 additions
and
58 deletions
plugins/ktcore/folder/addDocument.php
| @@ -6,7 +6,7 @@ | @@ -6,7 +6,7 @@ | ||
| 6 | * License Version 1.1.2 ("License"); You may not use this file except in | 6 | * License Version 1.1.2 ("License"); You may not use this file except in |
| 7 | * compliance with the License. You may obtain a copy of the License at | 7 | * compliance with the License. You may obtain a copy of the License at |
| 8 | * http://www.knowledgetree.com/KPL | 8 | * http://www.knowledgetree.com/KPL |
| 9 | - * | 9 | + * |
| 10 | * Software distributed under the License is distributed on an "AS IS" | 10 | * Software distributed under the License is distributed on an "AS IS" |
| 11 | * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. | 11 | * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing rights and | 12 | * See the License for the specific language governing rights and |
| @@ -17,9 +17,9 @@ | @@ -17,9 +17,9 @@ | ||
| 17 | * (ii) the KnowledgeTree copyright notice | 17 | * (ii) the KnowledgeTree copyright notice |
| 18 | * in the same form as they appear in the distribution. See the License for | 18 | * in the same form as they appear in the distribution. See the License for |
| 19 | * requirements. | 19 | * requirements. |
| 20 | - * | 20 | + * |
| 21 | * The Original Code is: KnowledgeTree Open Source | 21 | * The Original Code is: KnowledgeTree Open Source |
| 22 | - * | 22 | + * |
| 23 | * The Initial Developer of the Original Code is The Jam Warehouse Software | 23 | * The Initial Developer of the Original Code is The Jam Warehouse Software |
| 24 | * (Pty) Ltd, trading as KnowledgeTree. | 24 | * (Pty) Ltd, trading as KnowledgeTree. |
| 25 | * Portions created by The Jam Warehouse Software (Pty) Ltd are Copyright | 25 | * Portions created by The Jam Warehouse Software (Pty) Ltd are Copyright |
| @@ -52,7 +52,7 @@ class KTFolderAddDocumentAction extends KTFolderAction { | @@ -52,7 +52,7 @@ class KTFolderAddDocumentAction extends KTFolderAction { | ||
| 52 | if (empty($res)) { | 52 | if (empty($res)) { |
| 53 | return $res; | 53 | return $res; |
| 54 | } | 54 | } |
| 55 | - | 55 | + |
| 56 | $postExpected = KTUtil::arrayGet($_REQUEST, "postExpected"); | 56 | $postExpected = KTUtil::arrayGet($_REQUEST, "postExpected"); |
| 57 | $postReceived = KTUtil::arrayGet($_REQUEST, "postReceived"); | 57 | $postReceived = KTUtil::arrayGet($_REQUEST, "postReceived"); |
| 58 | if (!empty($postExpected)) { | 58 | if (!empty($postExpected)) { |
| @@ -67,7 +67,7 @@ class KTFolderAddDocumentAction extends KTFolderAction { | @@ -67,7 +67,7 @@ class KTFolderAddDocumentAction extends KTFolderAction { | ||
| 67 | 67 | ||
| 68 | function form_initialdata() { | 68 | function form_initialdata() { |
| 69 | $oForm = new KTForm; | 69 | $oForm = new KTForm; |
| 70 | - | 70 | + |
| 71 | $oForm->setOptions(array( | 71 | $oForm->setOptions(array( |
| 72 | 'label' => _kt("Add a document"), | 72 | 'label' => _kt("Add a document"), |
| 73 | 'action' => 'processInitialData', | 73 | 'action' => 'processInitialData', |
| @@ -79,24 +79,26 @@ class KTFolderAddDocumentAction extends KTFolderAction { | @@ -79,24 +79,26 @@ class KTFolderAddDocumentAction extends KTFolderAction { | ||
| 79 | 'submit_label' => _kt("Add"), | 79 | 'submit_label' => _kt("Add"), |
| 80 | 'file_upload' => true, | 80 | 'file_upload' => true, |
| 81 | )); | 81 | )); |
| 82 | - | 82 | + |
| 83 | $aTypes; | 83 | $aTypes; |
| 84 | foreach (DocumentType::getListForUserAndFolder($this->oUser, $this->oFolder) as $oDocumentType) { | 84 | foreach (DocumentType::getListForUserAndFolder($this->oUser, $this->oFolder) as $oDocumentType) { |
| 85 | if(!$oDocumentType->getDisabled()) { | 85 | if(!$oDocumentType->getDisabled()) { |
| 86 | $aTypes[] = $oDocumentType; | 86 | $aTypes[] = $oDocumentType; |
| 87 | } | 87 | } |
| 88 | - } | ||
| 89 | - | 88 | + } |
| 89 | + | ||
| 90 | // Onchange gets the name of the file and inserts it as the document title. | 90 | // Onchange gets the name of the file and inserts it as the document title. |
| 91 | - $sFileOnchange = "javascript: | ||
| 92 | - var arrPath=this.value.split('/'); | 91 | + $sFileOnchange = "javascript: |
| 92 | + var arrPath=this.value.split('/'); | ||
| 93 | if(arrPath.length == 1){ | 93 | if(arrPath.length == 1){ |
| 94 | var arrPath=this.value.split('\\\'); | 94 | var arrPath=this.value.split('\\\'); |
| 95 | } | 95 | } |
| 96 | - var name=arrPath[arrPath.length-1]; | ||
| 97 | - var title=name.split('.'); | ||
| 98 | - document.getElementById('document_name').value=title[0];"; | ||
| 99 | - | 96 | + var name=arrPath[arrPath.length-1]; |
| 97 | + var name=name.split('.'); | ||
| 98 | + name.pop(); | ||
| 99 | + var title=name.join('.'); | ||
| 100 | + document.getElementById('document_name').value=title;"; | ||
| 101 | + | ||
| 100 | $oForm->setWidgets(array( | 102 | $oForm->setWidgets(array( |
| 101 | array('ktcore.widgets.file',array( | 103 | array('ktcore.widgets.file',array( |
| 102 | 'label' => _kt('File'), | 104 | 'label' => _kt('File'), |
| @@ -122,9 +124,9 @@ class KTFolderAddDocumentAction extends KTFolderAction { | @@ -122,9 +124,9 @@ class KTFolderAddDocumentAction extends KTFolderAction { | ||
| 122 | 'id_method' => 'getId', | 124 | 'id_method' => 'getId', |
| 123 | 'label_method' => 'getName', | 125 | 'label_method' => 'getName', |
| 124 | 'simple_select' => false, | 126 | 'simple_select' => false, |
| 125 | - )), | 127 | + )), |
| 126 | )); | 128 | )); |
| 127 | - | 129 | + |
| 128 | $oForm->setValidators(array( | 130 | $oForm->setValidators(array( |
| 129 | array('ktcore.validators.file', array( | 131 | array('ktcore.validators.file', array( |
| 130 | 'test' => 'file', | 132 | 'test' => 'file', |
| @@ -139,65 +141,65 @@ class KTFolderAddDocumentAction extends KTFolderAction { | @@ -139,65 +141,65 @@ class KTFolderAddDocumentAction extends KTFolderAction { | ||
| 139 | 'output' => 'document_type', | 141 | 'output' => 'document_type', |
| 140 | 'class' => 'DocumentType', | 142 | 'class' => 'DocumentType', |
| 141 | 'ids' => true, | 143 | 'ids' => true, |
| 142 | - )), | 144 | + )), |
| 143 | )); | 145 | )); |
| 144 | - | 146 | + |
| 145 | return $oForm; | 147 | return $oForm; |
| 146 | } | 148 | } |
| 147 | - | 149 | + |
| 148 | function getFieldsetsForType($iTypeId) { | 150 | function getFieldsetsForType($iTypeId) { |
| 149 | $typeid = KTUtil::getId($iTypeId); | 151 | $typeid = KTUtil::getId($iTypeId); |
| 150 | $aGenericFieldsetIds = KTFieldset::getGenericFieldsets(array('ids' => false)); | 152 | $aGenericFieldsetIds = KTFieldset::getGenericFieldsets(array('ids' => false)); |
| 151 | $aSpecificFieldsetIds = KTFieldset::getForDocumentType($typeid, array('ids' => false)); | 153 | $aSpecificFieldsetIds = KTFieldset::getForDocumentType($typeid, array('ids' => false)); |
| 152 | - | 154 | + |
| 153 | $fieldsets = kt_array_merge($aGenericFieldsetIds, $aSpecificFieldsetIds); | 155 | $fieldsets = kt_array_merge($aGenericFieldsetIds, $aSpecificFieldsetIds); |
| 154 | - return $fieldsets; | 156 | + return $fieldsets; |
| 155 | } | 157 | } |
| 156 | 158 | ||
| 157 | function do_main() { | 159 | function do_main() { |
| 158 | $this->oPage->setBreadcrumbDetails(_kt("Add a document")); | 160 | $this->oPage->setBreadcrumbDetails(_kt("Add a document")); |
| 159 | $oForm = $this->form_initialdata(); | 161 | $oForm = $this->form_initialdata(); |
| 160 | return $oForm->renderPage(_kt('Add a document to: ') . $this->oFolder->getName()); | 162 | return $oForm->renderPage(_kt('Add a document to: ') . $this->oFolder->getName()); |
| 161 | - } | ||
| 162 | - | 163 | + } |
| 164 | + | ||
| 163 | function do_processInitialData() { | 165 | function do_processInitialData() { |
| 164 | $oForm = $this->form_initialdata(); | 166 | $oForm = $this->form_initialdata(); |
| 165 | $res = $oForm->validate(); | 167 | $res = $oForm->validate(); |
| 166 | if (!empty($res['errors'])) { | 168 | if (!empty($res['errors'])) { |
| 167 | return $oForm->handleError(); | 169 | return $oForm->handleError(); |
| 168 | - } | 170 | + } |
| 169 | $data = $res['results']; | 171 | $data = $res['results']; |
| 170 | $key = KTUtil::randomString(32); | 172 | $key = KTUtil::randomString(32); |
| 171 | 173 | ||
| 172 | - | ||
| 173 | - // joy joy, we need to store the file first, or PHP will (helpfully) | 174 | + |
| 175 | + // joy joy, we need to store the file first, or PHP will (helpfully) | ||
| 174 | // clean it up for us | 176 | // clean it up for us |
| 175 | 177 | ||
| 176 | $oKTConfig =& KTConfig::getSingleton(); | 178 | $oKTConfig =& KTConfig::getSingleton(); |
| 177 | - $sBasedir = $oKTConfig->get("urls/tmpDirectory"); | ||
| 178 | - | 179 | + $sBasedir = $oKTConfig->get("urls/tmpDirectory"); |
| 180 | + | ||
| 179 | $sFilename = tempnam($sBasedir, 'kt_storecontents'); | 181 | $sFilename = tempnam($sBasedir, 'kt_storecontents'); |
| 180 | $oContents = new KTFSFileLike($data['file']['tmp_name']); | 182 | $oContents = new KTFSFileLike($data['file']['tmp_name']); |
| 181 | $oOutputFile = new KTFSFileLike($sFilename); | 183 | $oOutputFile = new KTFSFileLike($sFilename); |
| 182 | - $res = KTFileLikeUtil::copy_contents($oContents, $oOutputFile); | ||
| 183 | - $data['file']['tmp_name'] = $sFilename; | 184 | + $res = KTFileLikeUtil::copy_contents($oContents, $oOutputFile); |
| 185 | + $data['file']['tmp_name'] = $sFilename; | ||
| 184 | 186 | ||
| 185 | if (PEAR::isError($res)) { | 187 | if (PEAR::isError($res)) { |
| 186 | $oForm->handleError(sprintf(_kt("Failed to store file: %s"), $res->getMessage())); | 188 | $oForm->handleError(sprintf(_kt("Failed to store file: %s"), $res->getMessage())); |
| 187 | } | 189 | } |
| 188 | - $_SESSION['_add_data'] = array($key => $data); | ||
| 189 | - | 190 | + $_SESSION['_add_data'] = array($key => $data); |
| 191 | + | ||
| 190 | // if we don't need metadata | 192 | // if we don't need metadata |
| 191 | $fieldsets = $this->getFieldsetsForType($data['document_type']); | 193 | $fieldsets = $this->getFieldsetsForType($data['document_type']); |
| 192 | if (empty($fieldsets)) { | 194 | if (empty($fieldsets)) { |
| 193 | - return $this->successRedirectTo('finalise', _kt("File uploaded successfully. Processing."), sprintf("fFileKey=%s", $key)); | 195 | + return $this->successRedirectTo('finalise', _kt("File uploaded successfully. Processing."), sprintf("fFileKey=%s", $key)); |
| 194 | } | 196 | } |
| 195 | - | 197 | + |
| 196 | // if we need metadata | 198 | // if we need metadata |
| 197 | - | 199 | + |
| 198 | $this->successRedirectTo('metadata', _kt("File uploaded successfully. Please fill in the metadata below."), sprintf("fFileKey=%s", $key)); | 200 | $this->successRedirectTo('metadata', _kt("File uploaded successfully. Please fill in the metadata below."), sprintf("fFileKey=%s", $key)); |
| 199 | } | 201 | } |
| 200 | - | 202 | + |
| 201 | function form_metadata($sess_key) { | 203 | function form_metadata($sess_key) { |
| 202 | $oForm = new KTForm; | 204 | $oForm = new KTForm; |
| 203 | $oForm->setOptions(array( | 205 | $oForm->setOptions(array( |
| @@ -210,49 +212,49 @@ class KTFolderAddDocumentAction extends KTFolderAction { | @@ -210,49 +212,49 @@ class KTFolderAddDocumentAction extends KTFolderAction { | ||
| 210 | 'context' => &$this, | 212 | 'context' => &$this, |
| 211 | 'extraargs' => $this->meldPersistQuery("","",true), | 213 | 'extraargs' => $this->meldPersistQuery("","",true), |
| 212 | )); | 214 | )); |
| 213 | - | 215 | + |
| 214 | $oFReg =& KTFieldsetRegistry::getSingleton(); | 216 | $oFReg =& KTFieldsetRegistry::getSingleton(); |
| 215 | - | 217 | + |
| 216 | $doctypeid = $_SESSION['_add_data'][$sess_key]['document_type']; | 218 | $doctypeid = $_SESSION['_add_data'][$sess_key]['document_type']; |
| 217 | - | 219 | + |
| 218 | $widgets = array(); | 220 | $widgets = array(); |
| 219 | $validators = array(); | 221 | $validators = array(); |
| 220 | $fieldsets = $this->getFieldsetsForType($doctypeid); | 222 | $fieldsets = $this->getFieldsetsForType($doctypeid); |
| 221 | - | 223 | + |
| 222 | foreach ($fieldsets as $oFieldset) { | 224 | foreach ($fieldsets as $oFieldset) { |
| 223 | $widgets = kt_array_merge($widgets, $oFReg->widgetsForFieldset($oFieldset, 'fieldset_' . $oFieldset->getId(), $this->oDocument)); | 225 | $widgets = kt_array_merge($widgets, $oFReg->widgetsForFieldset($oFieldset, 'fieldset_' . $oFieldset->getId(), $this->oDocument)); |
| 224 | - $validators = kt_array_merge($validators, $oFReg->validatorsForFieldset($oFieldset, 'fieldset_' . $oFieldset->getId(), $this->oDocument)); | 226 | + $validators = kt_array_merge($validators, $oFReg->validatorsForFieldset($oFieldset, 'fieldset_' . $oFieldset->getId(), $this->oDocument)); |
| 225 | } | 227 | } |
| 226 | - | 228 | + |
| 227 | $oForm->setWidgets($widgets); | 229 | $oForm->setWidgets($widgets); |
| 228 | - $oForm->setValidators($validators); | ||
| 229 | - | 230 | + $oForm->setValidators($validators); |
| 231 | + | ||
| 230 | return $oForm; | 232 | return $oForm; |
| 231 | } | 233 | } |
| 232 | - | 234 | + |
| 233 | function do_metadata() { | 235 | function do_metadata() { |
| 234 | $this->persistParams(array('fFileKey')); | 236 | $this->persistParams(array('fFileKey')); |
| 235 | - | 237 | + |
| 236 | $oForm = $this->form_metadata($_REQUEST['fFileKey']); | 238 | $oForm = $this->form_metadata($_REQUEST['fFileKey']); |
| 237 | return $oForm->render(); | 239 | return $oForm->render(); |
| 238 | } | 240 | } |
| 239 | - | 241 | + |
| 240 | function do_finalise() { | 242 | function do_finalise() { |
| 241 | - $this->persistParams(array('fFileKey')); | 243 | + $this->persistParams(array('fFileKey')); |
| 242 | $sess_key = $_REQUEST['fFileKey']; | 244 | $sess_key = $_REQUEST['fFileKey']; |
| 243 | $oForm = $this->form_metadata($sess_key); | 245 | $oForm = $this->form_metadata($sess_key); |
| 244 | $res = $oForm->validate(); | 246 | $res = $oForm->validate(); |
| 245 | if (!empty($res['errors'])) { | 247 | if (!empty($res['errors'])) { |
| 246 | - return $oForm->handleError(); | 248 | + return $oForm->handleError(); |
| 247 | } | 249 | } |
| 248 | $data = $res['results']; | 250 | $data = $res['results']; |
| 249 | - | 251 | + |
| 250 | $extra_d = $_SESSION['_add_data'][$sess_key]; | 252 | $extra_d = $_SESSION['_add_data'][$sess_key]; |
| 251 | $doctypeid = $extra_d['document_type']; | 253 | $doctypeid = $extra_d['document_type']; |
| 252 | $aGenericFieldsetIds = KTFieldset::getGenericFieldsets(array('ids' => false)); | 254 | $aGenericFieldsetIds = KTFieldset::getGenericFieldsets(array('ids' => false)); |
| 253 | $aSpecificFieldsetIds = KTFieldset::getForDocumentType($doctypeid, array('ids' => false)); | 255 | $aSpecificFieldsetIds = KTFieldset::getForDocumentType($doctypeid, array('ids' => false)); |
| 254 | $fieldsets = kt_array_merge($aGenericFieldsetIds, $aSpecificFieldsetIds); | 256 | $fieldsets = kt_array_merge($aGenericFieldsetIds, $aSpecificFieldsetIds); |
| 255 | - | 257 | + |
| 256 | 258 | ||
| 257 | $MDPack = array(); | 259 | $MDPack = array(); |
| 258 | foreach ($fieldsets as $oFieldset) { | 260 | foreach ($fieldsets as $oFieldset) { |
| @@ -260,9 +262,9 @@ class KTFolderAddDocumentAction extends KTFolderAction { | @@ -260,9 +262,9 @@ class KTFolderAddDocumentAction extends KTFolderAction { | ||
| 260 | $values = (array) KTUtil::arrayGet($data, 'fieldset_' . $oFieldset->getId()); | 262 | $values = (array) KTUtil::arrayGet($data, 'fieldset_' . $oFieldset->getId()); |
| 261 | 263 | ||
| 262 | foreach ($fields as $oField) { | 264 | foreach ($fields as $oField) { |
| 263 | - $val = KTUtil::arrayGet($values, 'metadata_' . $oField->getId()); | 265 | + $val = KTUtil::arrayGet($values, 'metadata_' . $oField->getId()); |
| 264 | // ALT.METADATA.LAYER.DIE.DIE.DIE | 266 | // ALT.METADATA.LAYER.DIE.DIE.DIE |
| 265 | - if (!is_null($val)) { | 267 | + if (!is_null($val)) { |
| 266 | $MDPack[] = array( | 268 | $MDPack[] = array( |
| 267 | $oField, | 269 | $oField, |
| 268 | $val | 270 | $val |
| @@ -271,12 +273,12 @@ class KTFolderAddDocumentAction extends KTFolderAction { | @@ -271,12 +273,12 @@ class KTFolderAddDocumentAction extends KTFolderAction { | ||
| 271 | 273 | ||
| 272 | } | 274 | } |
| 273 | } | 275 | } |
| 274 | - // older code | 276 | + // older code |
| 275 | 277 | ||
| 276 | $mpo =& new JavascriptObserver($this); | 278 | $mpo =& new JavascriptObserver($this); |
| 277 | $oUploadChannel =& KTUploadChannel::getSingleton(); | 279 | $oUploadChannel =& KTUploadChannel::getSingleton(); |
| 278 | $oUploadChannel->addObserver($mpo); | 280 | $oUploadChannel->addObserver($mpo); |
| 279 | - | 281 | + |
| 280 | require_once(KT_LIB_DIR . '/storage/storagemanager.inc.php'); | 282 | require_once(KT_LIB_DIR . '/storage/storagemanager.inc.php'); |
| 281 | require_once(KT_LIB_DIR . '/filelike/fsfilelike.inc.php'); | 283 | require_once(KT_LIB_DIR . '/filelike/fsfilelike.inc.php'); |
| 282 | require_once(KT_LIB_DIR . '/documentmanagement/DocumentType.inc'); | 284 | require_once(KT_LIB_DIR . '/documentmanagement/DocumentType.inc'); |
| @@ -287,10 +289,10 @@ class KTFolderAddDocumentAction extends KTFolderAction { | @@ -287,10 +289,10 @@ class KTFolderAddDocumentAction extends KTFolderAction { | ||
| 287 | 'redirect_to' => array('main', sprintf('fFolderId=%d', $this->oFolder->getId())), | 289 | 'redirect_to' => array('main', sprintf('fFolderId=%d', $this->oFolder->getId())), |
| 288 | 'max_str_len' => 200, | 290 | 'max_str_len' => 200, |
| 289 | ); | 291 | ); |
| 290 | - | 292 | + |
| 291 | $aFile = $this->oValidator->validateFile($extra_d['file'], $aErrorOptions); | 293 | $aFile = $this->oValidator->validateFile($extra_d['file'], $aErrorOptions); |
| 292 | $sTitle = sanitizeForSQL($extra_d['document_name']); | 294 | $sTitle = sanitizeForSQL($extra_d['document_name']); |
| 293 | - | 295 | + |
| 294 | $iFolderId = $this->oFolder->getId(); | 296 | $iFolderId = $this->oFolder->getId(); |
| 295 | $aOptions = array( | 297 | $aOptions = array( |
| 296 | 'contents' => new KTFSFileLike($aFile['tmp_name']), | 298 | 'contents' => new KTFSFileLike($aFile['tmp_name']), |
| @@ -315,7 +317,7 @@ class KTFolderAddDocumentAction extends KTFolderAction { | @@ -315,7 +317,7 @@ class KTFolderAddDocumentAction extends KTFolderAction { | ||
| 315 | exit(0); | 317 | exit(0); |
| 316 | 318 | ||
| 317 | } | 319 | } |
| 318 | - | 320 | + |
| 319 | } | 321 | } |
| 320 | 322 | ||
| 321 | ?> | 323 | ?> |