Exemplo n.º 1
0
	function doImport($data, $form) {
		$loader = new GroupCsvBulkLoader();
		
		// load file
		$result = $loader->load($data['CsvFile']['tmp_name']);
		
		// result message
		$msgArr = array();
		if($result->CreatedCount()) $msgArr[] = sprintf(
			_t('GroupImportForm.ResultCreated', 'Created %d groups'),
			$result->CreatedCount()
		);
		if($result->UpdatedCount()) $msgArr[] = sprintf(
			_t('GroupImportForm.ResultUpdated', 'Updated %d groups'),
			$result->UpdatedCount()
		);
		if($result->DeletedCount()) $msgArr[] = sprintf(
			_t('GroupImportForm.ResultDeleted', 'Deleted %d groups'),
			$result->DeletedCount()
		);
		$msg = ($msgArr) ? implode(',', $msgArr) : _t('MemberImportForm.ResultNone', 'No changes');
	
		$this->sessionMessage($msg, 'good');
		
		$this->controller->redirectBack();
	}
 public function testImportPermissions()
 {
     $loader = new GroupCsvBulkLoader();
     $results = $loader->load($this->getCurrentRelativePath() . '/GroupCsvBulkLoaderTest_withExisting.csv');
     $created = $results->Created()->toArray();
     $this->assertEquals(count($created), 1);
     $this->assertEquals($created[0]->Code, 'newgroup1');
     $this->assertEquals($created[0]->Permissions()->column('Code'), array('CODE1'));
     $updated = $results->Updated()->toArray();
     $this->assertEquals(count($updated), 1);
     $this->assertEquals($updated[0]->Code, 'existinggroup');
     $array1 = $updated[0]->Permissions()->column('Code');
     $array2 = array('CODE1', 'CODE2');
     sort($array1);
     sort($array2);
     $this->assertEquals($array1, $array2);
 }
 public function __construct($controller, $name, $fields = null, $actions = null, $validator = null)
 {
     if (!$fields) {
         $helpHtml = _t('ExcelGroupImportForm.Help1', '<p><a href="{link}">Download sample file</a></p>', array('link' => $controller->Link('downloadsample/Group')));
         $helpHtml .= _t('ExcelGroupImportForm.Help2', '<ul>' . '<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>');
         $importer = new GroupCsvBulkLoader();
         $importSpec = $importer->getImportSpec();
         $helpHtml = sprintf($helpHtml, implode(', ', array_keys($importSpec['fields'])));
         $extensions = array('csv', 'xls', 'xlsx', 'ods', 'txt');
         $fields = new FieldList(new LiteralField('Help', $helpHtml), $fileField = new FileField('File', _t('ExcelGroupImportForm.FileFieldLabel', 'File <small><br/>(allowed extensions: {extensions})</small>', array('extensions' => implode(', ', $extensions)))));
         $fileField->getValidator()->setAllowedExtensions(array('csv'));
     }
     if (!$actions) {
         $action = new FormAction('doImport', _t('ExcelGroupImportForm.BtnImport', 'Import from file'));
         $action->addExtraClass('ss-ui-button');
         $actions = new FieldList($action);
     }
     if (!$validator) {
         $validator = new RequiredFields('File');
     }
     parent::__construct($controller, $name, $fields, $actions, $validator);
     $this->addExtraClass('cms');
     $this->addExtraClass('import-form');
 }