2011-03-23 10:51:00 +01:00
< ? php
/**
* Imports { @ link Group } records by CSV upload , as defined in
* { @ link GroupCsvBulkLoader } .
*
* @ package cms
* @ subpackage batchactions
*/
class GroupImportForm extends Form {
/**
* @ var Group Optional group relation
*/
protected $group ;
2012-09-19 12:07:39 +02:00
public function __construct ( $controller , $name , $fields = null , $actions = null , $validator = null ) {
2011-03-23 10:51:00 +01:00
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' ])));
2011-05-11 09:51:54 +02:00
$fields = new FieldList (
2011-03-23 10:51:00 +01:00
new LiteralField ( 'Help' , $helpHtml ),
$fileField = new FileField (
'CsvFile' ,
_t (
'SecurityAdmin_MemberImportForm.FileFieldLabel' ,
'CSV File <small>(Allowed extensions: *.csv)</small>'
)
)
);
$fileField -> getValidator () -> setAllowedExtensions ( array ( 'csv' ));
}
2011-05-11 09:51:54 +02:00
if ( ! $actions ) $actions = new FieldList (
2012-03-24 01:20:19 +01:00
$importAction = new FormAction ( 'doImport' , _t ( 'SecurityAdmin_MemberImportForm.BtnImport' , 'Import from CSV' ))
2011-03-23 10:51:00 +01:00
);
2012-03-24 01:20:19 +01:00
$importAction -> addExtraClass ( 'ss-ui-button' );
2011-03-23 10:51:00 +01:00
if ( ! $validator ) $validator = new RequiredFields ( 'CsvFile' );
parent :: __construct ( $controller , $name , $fields , $actions , $validator );
2012-03-24 01:20:19 +01:00
$this -> addExtraClass ( 'cms' );
2011-03-23 10:51:00 +01:00
$this -> addExtraClass ( 'import-form' );
}
2012-09-19 12:07:39 +02:00
public function doImport ( $data , $form ) {
2011-03-23 10:51:00 +01:00
$loader = new GroupCsvBulkLoader ();
// load file
$result = $loader -> load ( $data [ 'CsvFile' ][ 'tmp_name' ]);
// result message
$msgArr = array ();
2012-05-01 21:44:54 +02:00
if ( $result -> CreatedCount ()) $msgArr [] = _t (
'GroupImportForm.ResultCreated' , 'Created {count} groups' ,
array ( 'count' => $result -> CreatedCount ())
2011-03-23 10:51:00 +01:00
);
2012-05-01 21:44:54 +02:00
if ( $result -> UpdatedCount ()) $msgArr [] = _t (
'GroupImportForm.ResultUpdated' , 'Updated %d groups' ,
array ( 'count' => $result -> UpdatedCount ())
2011-03-23 10:51:00 +01:00
);
2012-05-01 21:44:54 +02:00
if ( $result -> DeletedCount ()) $msgArr [] = _t (
'GroupImportForm.ResultDeleted' , 'Deleted %d groups' ,
array ( 'count' => $result -> DeletedCount ())
2011-03-23 10:51:00 +01:00
);
$msg = ( $msgArr ) ? implode ( ',' , $msgArr ) : _t ( 'MemberImportForm.ResultNone' , 'No changes' );
$this -> sessionMessage ( $msg , 'good' );
2012-03-10 02:56:49 +01:00
$this -> controller -> redirectBack ();
2011-03-23 10:51:00 +01:00
}
}