Version: current

      1 <?php
      2 /**
      3  * Imports {@link Member} records by CSV upload, as defined in
      4  * {@link MemberCsvBulkLoader}.
      5  *
      6  * @package cms
      7  * @subpackage batchactions
      8  */
      9 class MemberImportForm extends Form {
     10
     11     /**
     12      * @var Group Optional group relation
     13      */
     14     protected $group;
     15
     16     function __construct($controller, $name, $fields = null, $actions = null, $validator = null) {
     17         if(!$fields) {
     18             $helpHtml = _t(
     19                 'MemberImportForm.Help1',
     20                 '<p>Import members in <em>CSV format</em> (comma-separated values). <small><a href="#" class="toggle-advanced">Show advanced usage</a></small></p>'
     21             );
     22             $helpHtml .= _t(
     23                 'MemberImportForm.Help2',
     24 '<div class="advanced">
     25     <h4>Advanced usage</h4>
     26     <ul>
     27     <li>Allowed columns: <em>%s</em></li>
     28     <li>Existing members are matched by their unique <em>Code</em> property, and updated with any new values from the imported file.</li>
     29     <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>
     30     </ul>
     31 </div>');
     32
     33             $importer = new MemberCsvBulkLoader();
     34             $importSpec = $importer->getImportSpec();
     35             $helpHtml = sprintf($helpHtml, implode(', ', array_keys($importSpec['fields'])));
     36
     37             $fields = new FieldSet(
     38                 new LiteralField('Help', $helpHtml),
     39                 $fileField = new FileField(
     40                     'CsvFile',
     41                     _t(
     42                         'SecurityAdmin_MemberImportForm.FileFieldLabel',
     43                         'CSV File <small>(Allowed extensions: *.csv)</small>'
     44                     )
     45                 )
     46             );
     47             $fileField->getValidator()->setAllowedExtensions(array('csv'));
     48         }
     49
     50         if(!$actions) $actions = new FieldSet(
     51             new FormAction('doImport', _t('SecurityAdmin_MemberImportForm.BtnImport', 'Import'))
     52         );
     53
     54         if(!$validator) $validator = new RequiredFields('CsvFile');
     55
     56
     57         parent::__construct($controller, $name, $fields, $actions, $validator);
     58
     59         Requirements::javascript(CMS_DIR . '/javascript/MemberImportForm.js');
     60         $this->addExtraClass('import-form');
     61     }
     62
     63     function doImport($data, $form) {
     64         $loader = new MemberCsvBulkLoader();
     65
     66         // optionally set group relation
     67         if($this->group) $loader->setGroups(array($this->group));
     68
     69         // load file
     70         $result = $loader->load($data['CsvFile']['tmp_name']);
     71
     72         // result message
     73         $msgArr = array();
     74         if($result->CreatedCount()) $msgArr[] = sprintf(
     75             _t('MemberImportForm.ResultCreated', 'Created %d members'),
     76             $result->CreatedCount()
     77         );
     78         if($result->UpdatedCount()) $msgArr[] = sprintf(
     79             _t('MemberImportForm.ResultUpdated', 'Updated %d members'),
     80             $result->UpdatedCount()
     81         );
     82         if($result->DeletedCount()) $msgArr[] = sprintf(
     83             _t('MemberImportForm.ResultDeleted', 'Deleted %d members'),
     84             $result->DeletedCount()
     85         );
     86         $msg = ($msgArr) ? implode(',', $msgArr) : _t('MemberImportForm.ResultNone', 'No changes');
     87
     88         $this->sessionMessage($msg, 'good');
     89
     90         Director::redirectBack();
     91     }
     92
     93     /**
     94      * @param $group Group
     95      */
     96     function setGroup($group) {
     97         $this->group = $group;
     98     }
     99
    100     /**
    101      * @return Group
    102      */
    103     function getGroup($group) {
    104         return $this->group;
    105     }
    106 }
    107 ?>

[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the Silverstripe Forums to ask development related questions. -