From 2f4a9bd3742071224edf60806cd2fb757f5c8a85 Mon Sep 17 00:00:00 2001 From: bshuttle Date: Wed, 16 Aug 2006 11:03:54 +0000 Subject: [PATCH] - files should be validated when required - if there's no metadata, don't bother asking for it. --- plugins/ktcore/KTCorePlugin.php | 1 + plugins/ktcore/KTValidators.php | 20 ++++++++++++++++++++ plugins/ktcore/KTWidgets.php | 12 ++++++++++++ plugins/ktcore/folder/addDocument.php | 22 ++++++++++++++++++---- 4 files changed, 51 insertions(+), 4 deletions(-) diff --git a/plugins/ktcore/KTCorePlugin.php b/plugins/ktcore/KTCorePlugin.php index 277a168..21774bb 100644 --- a/plugins/ktcore/KTCorePlugin.php +++ b/plugins/ktcore/KTCorePlugin.php @@ -141,6 +141,7 @@ class KTCorePlugin extends KTPlugin { $this->registerValidator('KTMembershipValidator', 'ktcore.validators.membership', 'KTValidators.php'); $this->registerValidator('KTFieldsetValidator', 'ktcore.validators.fieldset', 'KTValidators.php'); $this->registerValidator('KTFileValidator', 'ktcore.validators.file', 'KTValidators.php'); + $this->registerValidator('KTRequiredFileValidator', 'ktcore.validators.requiredfile', 'KTValidators.php'); // criterion $this->registerCriterion('NameCriterion', 'ktcore.criteria.name', KT_LIB_DIR . '/browse/Criteria.inc'); diff --git a/plugins/ktcore/KTValidators.php b/plugins/ktcore/KTValidators.php index 586c8e3..b7cd38b 100644 --- a/plugins/ktcore/KTValidators.php +++ b/plugins/ktcore/KTValidators.php @@ -147,6 +147,26 @@ class KTRequiredValidator extends KTValidator { } } +// the required validator checks either single or multiple items +// in the data array. +class KTRequiredFileValidator extends KTValidator { + var $sNamespace = 'ktcore.validators.requiredfile'; + + function validate($data) { + $errors = array(); + + $val = KTUtil::arrayGet($_FILES, $this->sInputVariable); + if (empty($val) || empty($val['name'])) { + $errors[$this->sBasename] = _kt("You must select a file to upload."); + } + + return array( + 'errors' => $errors, + 'results' => array(), + ); + } +} + class KTEmailValidator extends KTValidator { var $sNamespace = 'ktcore.validators.emailaddress'; diff --git a/plugins/ktcore/KTWidgets.php b/plugins/ktcore/KTWidgets.php index 51f5d68..276bddd 100644 --- a/plugins/ktcore/KTWidgets.php +++ b/plugins/ktcore/KTWidgets.php @@ -22,6 +22,18 @@ class KTCoreFileWidget extends KTWidget { $tname = sprintf("_kt_attempt_unique_%s", $this->sName); return array($this->sBasename => $_FILES[$tname]); } + + function getValidators() { + if (!$this->bAutoValidate) { + return null; + } + + $oVF =& KTValidatorFactory::getSingleton(); + return $oVF->get('ktcore.validators.requiredfile', array( + 'test' => sprintf("_kt_attempt_unique_%s", $this->sName), + 'basename' => $this->sBasename, + )); + } } diff --git a/plugins/ktcore/folder/addDocument.php b/plugins/ktcore/folder/addDocument.php index eb56b04..1bbd03d 100644 --- a/plugins/ktcore/folder/addDocument.php +++ b/plugins/ktcore/folder/addDocument.php @@ -105,6 +105,7 @@ class KTFolderAddDocumentAction extends KTFolderAction { 'initial_string' => _kt('- Please select a document type -'), 'id_method' => 'getId', 'label_method' => 'getName', + 'simple_select' => false, )), )); @@ -127,6 +128,15 @@ class KTFolderAddDocumentAction extends KTFolderAction { return $oForm; } + + function getFieldsetsForType($iTypeId) { + $typeid = KTUtil::getId($iTypeId); + $aGenericFieldsetIds = KTFieldset::getGenericFieldsets(array('ids' => false)); + $aSpecificFieldsetIds = KTFieldset::getForDocumentType($typeid, array('ids' => false)); + + $fieldsets = kt_array_merge($aGenericFieldsetIds, $aSpecificFieldsetIds); + return $fieldsets; + } function do_main() { $this->oPage->setBreadcrumbDetails(_kt("Add a document")); @@ -147,6 +157,13 @@ class KTFolderAddDocumentAction extends KTFolderAction { $data = $res['results']; $key = KTUtil::randomString(32); $_SESSION['_add_data'] = array($key => $data); + + // if we don't need metadata + $fieldsets = $this->getFieldsetsForType($data['document_type']); + if (empty($fieldsets)) { + return $this->successRedirectTo('finalise', _kt("File uploaded successfully. Processing."), sprintf("fFileKey=%s", $key)); + } + // if we need metadata $this->successRedirectTo('metadata', _kt("File uploaded successfully. Please fill in the metadata below."), sprintf("fFileKey=%s", $key)); @@ -171,10 +188,7 @@ class KTFolderAddDocumentAction extends KTFolderAction { $widgets = array(); $validators = array(); - $aGenericFieldsetIds = KTFieldset::getGenericFieldsets(array('ids' => false)); - $aSpecificFieldsetIds = KTFieldset::getForDocumentType($doctypeid, array('ids' => false)); - - $fieldsets = kt_array_merge($aGenericFieldsetIds, $aSpecificFieldsetIds); + $fieldsets = $this->getFieldsetsForType($doctypeid); foreach ($fieldsets as $oFieldset) { $widgets = kt_array_merge($widgets, $oFReg->widgetsForFieldset($oFieldset, 'fieldset_' . $oFieldset->getId(), $this->oDocument)); -- libgit2 0.21.4