Commit 82c4f1a113cdb38448d000f27a4be72d8548ee92

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