public function __construct($controller, $name, $fields = null, $actions = null, $validator = null) { if (!$fields) { $helpHtml = _t('MemberImportForm.Help1', '<p>Import users in <em>CSV format</em> (comma-separated values).' . ' <small><a href="#" class="toggle-advanced">Show advanced usage</a></small></p>'); $helpHtml .= _t('MemberImportForm.Help2', '<div class="advanced">' . '<h4>Advanced usage</h4>' . '<ul>' . '<li>Allowed columns: <em>%s</em></li>' . '<li>Existing users are matched by their unique <em>Code</em> property, and updated with any new values from ' . 'the imported file.</li>' . '<li>Groups can be assigned by the <em>Groups</em> column. Groups are identified by their <em>Code</em> property, ' . 'multiple groups can be separated by comma. Existing group memberships are not cleared.</li>' . '</ul>' . '</div>'); $importer = new MemberCsvBulkLoader(); $importSpec = $importer->getImportSpec(); $helpHtml = sprintf($helpHtml, implode(', ', array_keys($importSpec['fields']))); $fields = new FieldList(new LiteralField('Help', $helpHtml), $fileField = new FileField('CsvFile', DBField::create_field('HTMLFragment', _t('SecurityAdmin_MemberImportForm.FileFieldLabel', 'CSV File <small>(Allowed extensions: *.csv)</small>')))); $fileField->getValidator()->setAllowedExtensions(array('csv')); } if (!$actions) { $action = new FormAction('doImport', _t('SecurityAdmin_MemberImportForm.BtnImport', 'Import from CSV')); $action->addExtraClass('btn btn-secondary-outline ss-ui-button'); $actions = new FieldList($action); } if (!$validator) { $validator = new RequiredFields('CsvFile'); } parent::__construct($controller, $name, $fields, $actions, $validator); Requirements::javascript(FRAMEWORK_ADMIN_DIR . '/client/dist/js/vendor.js'); Requirements::javascript(FRAMEWORK_ADMIN_DIR . '/client/dist/js/MemberImportForm.js'); Requirements::css(FRAMEWORK_ADMIN_DIR . '/client/dist/styles/bundle.css'); $this->addExtraClass('cms'); $this->addExtraClass('import-form'); }
/** * Test different scenarii for a failed upload : an error occured, no files where provided */ public function testUploadMissingRequiredFile() { /** @skipUpgrade */ $form = new Form(new Controller(), 'Form', new FieldList($fileField = new FileField('cv', 'Upload your CV')), new FieldList(), new RequiredFields('cv')); // All fields are filled but for some reason an error occured when uploading the file => fails $fileFieldValue = array('name' => 'aCV.txt', 'type' => 'application/octet-stream', 'tmp_name' => '/private/var/tmp/phpzTQbqP', 'error' => 1, 'size' => 3471); $fileField->setValue($fileFieldValue); $this->assertFalse($form->validate(), 'An error occured when uploading a file, but the validator returned true'); // We pass an empty set of parameters for the uploaded file => fails $fileFieldValue = array(); $fileField->setValue($fileFieldValue); $this->assertFalse($form->validate(), 'An empty array was passed as parameter for an uploaded file, but the validator returned true'); // We pass an null value for the uploaded file => fails $fileFieldValue = null; $fileField->setValue($fileFieldValue); $this->assertFalse($form->validate(), 'A null value was passed as parameter for an uploaded file, but the validator returned true'); }
public function __construct($controller, $name, $fields = null, $actions = null, $validator = null) { if (!$fields) { $helpHtml = _t('GroupImportForm.Help1', '<p>Import one or more groups in <em>CSV</em> format (comma-separated values).' . ' <small><a href="#" class="toggle-advanced">Show advanced usage</a></small></p>'); $helpHtml .= _t('GroupImportForm.Help2', '<div class="advanced">' . '<h4>Advanced usage</h4>' . '<ul>' . '<li>Allowed columns: <em>%s</em></li>' . '<li>Existing groups are matched by their unique <em>Code</em> value, and updated with any new values from the ' . 'imported file</li>' . '<li>Group hierarchies can be created by using a <em>ParentCode</em> column.</li>' . '<li>Permission codes can be assigned by the <em>PermissionCode</em> column. Existing permission codes are not ' . 'cleared.</li>' . '</ul>' . '</div>'); $importer = new GroupCsvBulkLoader(); $importSpec = $importer->getImportSpec(); $helpHtml = sprintf($helpHtml, implode(', ', array_keys($importSpec['fields']))); $fields = new FieldList(new LiteralField('Help', $helpHtml), $fileField = new FileField('CsvFile', DBField::create_field('HTMLFragment', _t('SecurityAdmin_MemberImportForm.FileFieldLabel', 'CSV File <small>(Allowed extensions: *.csv)</small>')))); $fileField->getValidator()->setAllowedExtensions(array('csv')); } if (!$actions) { $action = new FormAction('doImport', _t('SecurityAdmin_MemberImportForm.BtnImport', 'Import from CSV')); $action->addExtraClass('ss-ui-button'); $actions = new FieldList($action); } if (!$validator) { $validator = new RequiredFields('CsvFile'); } parent::__construct($controller, $name, $fields, $actions, $validator); $this->addExtraClass('cms'); $this->addExtraClass('import-form'); }
public function Field($properties = array()) { // Calculated config as per jquery.fileupload-ui.js $allowedMaxFileNumber = $this->getAllowedMaxFileNumber(); $config = array('url' => $this->Link('upload'), 'urlSelectDialog' => $this->Link('select'), 'urlAttach' => $this->Link('attach'), 'urlFileExists' => $this->Link('fileexists'), 'acceptFileTypes' => '.+$', 'maxNumberOfFiles' => $allowedMaxFileNumber ? $allowedMaxFileNumber - count($this->getItemIDs()) : null, 'replaceFile' => $this->getUpload()->getReplaceFile()); // Validation: File extensions if ($allowedExtensions = $this->getAllowedExtensions()) { $config['acceptFileTypes'] = '(\\.|\\/)(' . implode('|', $allowedExtensions) . ')$'; $config['errorMessages']['acceptFileTypes'] = _t('File.INVALIDEXTENSIONSHORT', 'Extension is not allowed'); } // Validation: File size if ($allowedMaxFileSize = $this->getValidator()->getAllowedMaxFileSize()) { $config['maxFileSize'] = $allowedMaxFileSize; $config['errorMessages']['maxFileSize'] = _t('File.TOOLARGESHORT', 'File size exceeds {size}', array('size' => File::format_size($config['maxFileSize']))); } // Validation: Number of files if ($allowedMaxFileNumber) { if ($allowedMaxFileNumber > 1) { $config['errorMessages']['maxNumberOfFiles'] = _t('UploadField.MAXNUMBEROFFILESSHORT', 'Can only upload {count} files', array('count' => $allowedMaxFileNumber)); } else { $config['errorMessages']['maxNumberOfFiles'] = _t('UploadField.MAXNUMBEROFFILESONE', 'Can only upload one file'); } } // add overwrite warning error message to the config object sent to Javascript if ($this->getOverwriteWarning()) { $config['errorMessages']['overwriteWarning'] = _t('UploadField.OVERWRITEWARNING', 'File with the same name already exists'); } $mergedConfig = array_merge($config, $this->ufConfig); return parent::Field(array('configString' => Convert::raw2json($mergedConfig), 'config' => new ArrayData($mergedConfig), 'multiple' => $allowedMaxFileNumber !== 1)); }
public function extraClass() { if ($this->isDisabled()) { $this->addExtraClass('disabled'); } if ($this->isReadonly()) { $this->addExtraClass('readonly'); } return parent::extraClass(); }