CRM-11254 - Add base class for Import/dataSource
authorColeman Watts <coleman@civicrm.org>
Mon, 27 Jul 2015 01:14:26 +0000 (21:14 -0400)
committerColeman Watts <coleman@civicrm.org>
Mon, 27 Jul 2015 01:14:26 +0000 (21:14 -0400)
CRM/Activity/Import/Form/DataSource.php
CRM/Activity/Import/Form/Summary.php
CRM/Contact/Import/Form/Summary.php
CRM/Contribute/Import/Form/DataSource.php
CRM/Contribute/Import/Form/Summary.php
CRM/Custom/Import/Form/DataSource.php
CRM/Event/Import/Form/DataSource.php
CRM/Event/Import/Form/Summary.php
CRM/Import/Form/DataSource.php [new file with mode: 0644]
CRM/Member/Import/Form/DataSource.php
CRM/Member/Import/Form/Summary.php

index 9ba87a83f2c93efa7b09fd62b7270d0784234ad4..ce30f7bfb4cf64ff18b1869b4b67ed871fbb14b2 100644 (file)
 /**
  * This class gets the name of the file to upload
  */
-class CRM_Activity_Import_Form_DataSource extends CRM_Core_Form {
+class CRM_Activity_Import_Form_DataSource extends CRM_Import_Form_DataSource {
 
-  /**
-   * Set variables up before form is built.
-   *
-   * @return void
-   */
-  public function preProcess() {
-    $session = CRM_Core_Session::singleton();
-    $session->pushUserContext(CRM_Utils_System::url('civicrm/import/activity', 'reset=1'));
-    // check for post max size
-    CRM_Core_Config_Defaults::formatUnitSize(ini_get('post_max_size'), TRUE);
-  }
+  const PATH = 'civicrm/import/activity';
+
+  const IMPORT_ENTITY = 'Activity';
 
   /**
    * Build the form object.
@@ -56,24 +48,9 @@ class CRM_Activity_Import_Form_DataSource extends CRM_Core_Form {
    * @return void
    */
   public function buildQuickForm() {
-    //Setting Upload File Size
-    $config = CRM_Core_Config::singleton();
-
-    $uploadFileSize = CRM_Core_Config_Defaults::formatUnitSize($config->maxFileSize . 'm', TRUE);
-    $uploadSize = round(($uploadFileSize / (1024 * 1024)), 2);
-
-    $this->assign('uploadSize', $uploadSize);
-    $this->setMaxFileSize($uploadFileSize);
-    $this->add('File', 'uploadFile', ts('Import Data File'), 'size=30 maxlength=255', TRUE);
-    $this->addRule('uploadFile', ts('File size should be less than %1 MBytes (%2 bytes)', array(
-          1 => $uploadSize,
-          2 => $uploadFileSize,
-        )), 'maxfilesize', $uploadFileSize);
-    $this->addRule('uploadFile', ts('A valid file must be uploaded.'), 'uploadedfile');
-    $this->addRule('uploadFile', ts('Input file must be in CSV format'), 'utf8File');
-
-    $this->addElement('checkbox', 'skipColumnHeader', ts('First row contains column headers'));
+    parent::buildQuickForm();
 
+    // FIXME: This 'onDuplicate' form element is never used -- copy/paste error?
     $duplicateOptions = array();
     $duplicateOptions[] = $this->createElement('radio',
       NULL, NULL, ts('Skip'), CRM_Import_Parser::DUPLICATE_SKIP
@@ -88,40 +65,6 @@ class CRM_Activity_Import_Form_DataSource extends CRM_Core_Form {
     $this->addGroup($duplicateOptions, 'onDuplicate',
       ts('On duplicate entries')
     );
-
-    //get the saved mapping details
-    $mappingArray = CRM_Core_BAO_Mapping::getMappings(CRM_Core_OptionGroup::getValue('mapping_type',
-      'Import Activity',
-      'name'
-    ));
-    $this->assign('savedMapping', $mappingArray);
-    $this->add('select', 'savedMapping', ts('Mapping Option'), array('' => ts('- select -')) + $mappingArray);
-
-    if ($loadeMapping = $this->get('loadedMapping')) {
-      $this->assign('loadedMapping', $loadeMapping);
-      $this->setDefaults(array('savedMapping' => $loadeMapping));
-    }
-
-    $this->setDefaults(array(
-      'onDuplicate' => CRM_Import_Parser::DUPLICATE_SKIP,
-    ));
-
-    //build date formats
-    CRM_Core_Form_Date::buildAllowedDateFormats($this);
-
-    $this->addButtons(array(
-        array(
-          'type' => 'upload',
-          'name' => ts('Continue'),
-          'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
-          'isDefault' => TRUE,
-        ),
-        array(
-          'type' => 'cancel',
-          'name' => ts('Cancel'),
-        ),
-      )
-    );
   }
 
   /**
@@ -130,45 +73,13 @@ class CRM_Activity_Import_Form_DataSource extends CRM_Core_Form {
    * @return void
    */
   public function postProcess() {
-    $this->controller->resetPage('MapField');
-
-    $fileName = $this->controller->exportValue($this->_name, 'uploadFile');
-    $skipColumnHeader = $this->controller->exportValue($this->_name, 'skipColumnHeader');
-    $onDuplicate = $this->controller->exportValue($this->_name, 'onDuplicate');
-    $dateFormats = $this->controller->exportValue($this->_name, 'dateFormats');
-    $savedMapping = $this->controller->exportValue($this->_name, 'savedMapping');
-
-    $this->set('onDuplicate', $onDuplicate);
-    $this->set('dateFormats', $dateFormats);
-    $this->set('savedMapping', $savedMapping);
-
-    $session = CRM_Core_Session::singleton();
-    $session->set("dateTypes", $dateFormats);
-
-    $config = CRM_Core_Config::singleton();
-    $seperator = $config->fieldSeparator;
-
-    $mapper = array();
-
-    $parser = new CRM_Activity_Import_Parser_Activity($mapper);
-    $parser->setMaxLinesToProcess(100);
-    $parser->run($fileName, $seperator,
-      $mapper,
-      $skipColumnHeader,
-      CRM_Import_Parser::MODE_MAPFIELD
-    );
-
-    // add all the necessary variables to the form
-    $parser->set($this);
-  }
+    $this->storeFormValues(array(
+      'onDuplicate',
+      'dateFormats',
+      'savedMapping',
+    ));
 
-  /**
-   * Return a descriptive name for the page, used in wizard header
-   *
-   * @return string
-   */
-  public function getTitle() {
-    return ts('Upload Data');
+    $this->submitFileForMapping('CRM_Activity_Import_Parser_Activity');
   }
 
 }
index 919bfd1333060a499e75c5689f6889a08bd82eb6..6bdb5da67b3eabf8c92652a21cd44e447b193892 100644 (file)
@@ -44,9 +44,8 @@ class CRM_Activity_Import_Form_Summary extends CRM_Import_Form_Summary {
    * @return void
    */
   public function preProcess() {
-
     // set the error message path to display
-    $errorFile = $this->assign('errorFile', $this->get('errorFile'));
+    $this->assign('errorFile', $this->get('errorFile'));
 
     $totalRowCount = $this->get('totalRowCount');
     $relatedCount = $this->get('relatedCount');
index 478abf06439fd1d95a6435d12601b7d1ff651bee..8f31bb5826265c96eac150c7ba5e1ccf6452b58e 100644 (file)
@@ -45,7 +45,7 @@ class CRM_Contact_Import_Form_Summary extends CRM_Import_Form_Summary {
    */
   public function preProcess() {
     // set the error message path to display
-    $errorFile = $this->assign('errorFile', $this->get('errorFile'));
+    $this->assign('errorFile', $this->get('errorFile'));
 
     $totalRowCount = $this->get('totalRowCount');
     $relatedCount = $this->get('relatedCount');
index 6368355d8b8ef5bfc376e1bcaba01f969c0cbd74..14d1d8e30b19b5d48123c9fe5f4bcc884c5960a2 100644 (file)
 /**
  * This class gets the name of the file to upload
  */
-class CRM_Contribute_Import_Form_DataSource extends CRM_Core_Form {
+class CRM_Contribute_Import_Form_DataSource extends CRM_Import_Form_DataSource {
 
-  /**
-   * Set variables up before form is built.
-   *
-   * @return void
-   */
-  public function preProcess() {
-    $session = CRM_Core_Session::singleton();
-    $session->pushUserContext(CRM_Utils_System::url('civicrm/contribute/import', 'reset=1'));
-    // check for post max size
-    CRM_Core_Config_Defaults::formatUnitSize(ini_get('post_max_size'), TRUE);
-  }
+  const PATH = 'civicrm/contribute/import';
+
+  const IMPORT_ENTITY = 'Contribution';
 
   /**
    * Build the form object.
@@ -56,24 +48,7 @@ class CRM_Contribute_Import_Form_DataSource extends CRM_Core_Form {
    * @return void
    */
   public function buildQuickForm() {
-    //Setting Upload File Size
-    $config = CRM_Core_Config::singleton();
-
-    $uploadFileSize = CRM_Core_Config_Defaults::formatUnitSize($config->maxFileSize . 'm', TRUE);
-    $uploadSize = round(($uploadFileSize / (1024 * 1024)), 2);
-
-    $this->assign('uploadSize', $uploadSize);
-
-    $this->add('File', 'uploadFile', ts('Import Data File'), 'size=30 maxlength=255', TRUE);
-    $this->setMaxFileSize($uploadFileSize);
-    $this->addRule('uploadFile', ts('File size should be less than %1 MBytes (%2 bytes)', array(
-          1 => $uploadSize,
-          2 => $uploadFileSize,
-        )), 'maxfilesize', $uploadFileSize);
-    $this->addRule('uploadFile', ts('A valid file must be uploaded.'), 'uploadedfile');
-    $this->addRule('uploadFile', ts('Input file must be in CSV format'), 'utf8File');
-
-    $this->addElement('checkbox', 'skipColumnHeader', ts('First row contains column headers'));
+    parent::buildQuickForm();
 
     $duplicateOptions = array();
     $duplicateOptions[] = $this->createElement('radio',
@@ -86,68 +61,11 @@ class CRM_Contribute_Import_Form_DataSource extends CRM_Core_Form {
       ts('Import mode')
     );
 
-    //get the saved mapping details
-    $mappingArray = CRM_Core_BAO_Mapping::getMappings(CRM_Core_OptionGroup::getValue('mapping_type',
-      'Import Contribution',
-      'name'
-    ));
-    $this->assign('savedMapping', $mappingArray);
-    $this->add('select', 'savedMapping', ts('Mapping Option'), array('' => ts('- select -')) + $mappingArray);
-    $this->addElement('submit', 'loadMapping', ts('Load Mapping'), NULL, array('onclick' => 'checkSelect()'));
-
-    if ($loadeMapping = $this->get('loadedMapping')) {
-      $this->assign('loadedMapping', $loadeMapping);
-      $this->setDefaults(array('savedMapping' => $loadeMapping));
-    }
-
-    $this->setDefaults(array(
-      'onDuplicate' =>
-        CRM_Import_Parser::DUPLICATE_SKIP,
-    ));
-
-    //contact types option
-    $contactOptions = array();
-    if (CRM_Contact_BAO_ContactType::isActive('Individual')) {
-      $contactOptions[] = $this->createElement('radio',
-        NULL, NULL, ts('Individual'), CRM_Import_Parser::CONTACT_INDIVIDUAL
-      );
-    }
-    if (CRM_Contact_BAO_ContactType::isActive('Household')) {
-      $contactOptions[] = $this->createElement('radio',
-        NULL, NULL, ts('Household'), CRM_Import_Parser::CONTACT_HOUSEHOLD
-      );
-    }
-    if (CRM_Contact_BAO_ContactType::isActive('Organization')) {
-      $contactOptions[] = $this->createElement('radio',
-        NULL, NULL, ts('Organization'), CRM_Import_Parser::CONTACT_ORGANIZATION
-      );
-    }
-
-    $this->addGroup($contactOptions, 'contactType',
-      ts('Contact Type')
-    );
+    $this->setDefaults(array('onDuplicate' => CRM_Import_Parser::DUPLICATE_SKIP));
 
-    $this->setDefaults(array(
-      'contactType' =>
-        CRM_Import_Parser::CONTACT_INDIVIDUAL,
-    ));
-
-    //build date formats
-    CRM_Core_Form_Date::buildAllowedDateFormats($this);
+    $this->addElement('submit', 'loadMapping', ts('Load Mapping'), NULL, array('onclick' => 'checkSelect()'));
 
-    $this->addButtons(array(
-        array(
-          'type' => 'upload',
-          'name' => ts('Continue'),
-          'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
-          'isDefault' => TRUE,
-        ),
-        array(
-          'type' => 'cancel',
-          'name' => ts('Cancel'),
-        ),
-      )
-    );
+    $this->addContactTypeSelector();
   }
 
   /**
@@ -156,47 +74,14 @@ class CRM_Contribute_Import_Form_DataSource extends CRM_Core_Form {
    * @return void
    */
   public function postProcess() {
-    $this->controller->resetPage('MapField');
-
-    $fileName = $this->controller->exportValue($this->_name, 'uploadFile');
-    $skipColumnHeader = $this->controller->exportValue($this->_name, 'skipColumnHeader');
-    $onDuplicate = $this->controller->exportValue($this->_name, 'onDuplicate');
-    $contactType = $this->controller->exportValue($this->_name, 'contactType');
-    $dateFormats = $this->controller->exportValue($this->_name, 'dateFormats');
-    $savedMapping = $this->controller->exportValue($this->_name, 'savedMapping');
-
-    $this->set('onDuplicate', $onDuplicate);
-    $this->set('contactType', $contactType);
-    $this->set('dateFormats', $dateFormats);
-    $this->set('savedMapping', $savedMapping);
-
-    $session = CRM_Core_Session::singleton();
-    $session->set("dateTypes", $dateFormats);
-
-    $config = CRM_Core_Config::singleton();
-    $seperator = $config->fieldSeparator;
-
-    $mapper = array();
-
-    $parser = new CRM_Contribute_Import_Parser_Contribution($mapper);
-    $parser->setMaxLinesToProcess(100);
-    $parser->run($fileName, $seperator,
-      $mapper,
-      $skipColumnHeader,
-      CRM_Import_Parser::MODE_MAPFIELD, $contactType
-    );
-
-    // add all the necessary variables to the form
-    $parser->set($this);
-  }
+    $this->storeFormValues(array(
+      'onDuplicate',
+      'contactType',
+      'dateFormats',
+      'savedMapping',
+    ));
 
-  /**
-   * Return a descriptive name for the page, used in wizard header
-   *
-   * @return string
-   */
-  public function getTitle() {
-    return ts('Upload Data');
+    $this->submitFileForMapping('CRM_Contribute_Import_Parser_Contribution');
   }
 
 }
index b935b9daaa7b5fa1392f8ebe5ed8d2d220f49100..9d6d76f2c5c617caa58c831f7458e3474cff2d1e 100644 (file)
@@ -45,7 +45,7 @@ class CRM_Contribute_Import_Form_Summary extends CRM_Import_Form_Summary {
    */
   public function preProcess() {
     // set the error message path to display
-    $errorFile = $this->assign('errorFile', $this->get('errorFile'));
+    $this->assign('errorFile', $this->get('errorFile'));
 
     $totalRowCount = $this->get('totalRowCount');
     $relatedCount = $this->get('relatedCount');
index 3b3d88b2efeee2fa3d67954ba2c00cb63e9ae8a8..3458491a6b577c5ccceaac4a59fe2dcbb00e1ce7 100644 (file)
 /**
  * This class gets the name of the file to upload
  */
-class CRM_Custom_Import_Form_DataSource extends CRM_Core_Form {
+class CRM_Custom_Import_Form_DataSource extends CRM_Import_Form_DataSource {
 
-  /**
-   * Set variables up before form is built.
-   *
-   * @return void
-   */
-  public function preProcess() {
-    $session = CRM_Core_Session::singleton();
-    $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this, FALSE);
-    if ($this->_id) {
-      $params = "reset=1&id={$this->_id}";
-    }
-    else {
-      $params = "reset=1";
-    }
-    $session->pushUserContext(CRM_Utils_System::url('civicrm/import/custom', $params));
+  const PATH = 'civicrm/import/custom';
 
-    // check for post max size
-    CRM_Core_Config_Defaults::formatUnitSize(ini_get('post_max_size'), TRUE);
-  }
+  const IMPORT_ENTITY = 'Multi value custom data';
 
   /**
    * @return array
@@ -83,82 +67,12 @@ class CRM_Custom_Import_Form_DataSource extends CRM_Core_Form {
    * @return void
    */
   public function buildQuickForm() {
+    parent::buildQuickForm();
+
     $multipleCustomData = CRM_Core_BAO_CustomGroup::getMultipleFieldGroup();
     $this->add('select', 'multipleCustomData', ts('Multi-value Custom Data'), array('' => ts('- select -')) + $multipleCustomData, TRUE);
 
-    //Setting Upload File Size
-    $config = CRM_Core_Config::singleton();
-
-    $uploadFileSize = CRM_Core_Config_Defaults::formatUnitSize($config->maxFileSize . 'm', TRUE);
-    $uploadSize = round(($uploadFileSize / (1024 * 1024)), 2);
-
-    $this->assign('uploadSize', $uploadSize);
-    $this->add('File', 'uploadFile', ts('Import Data File'), 'size=30 maxlength=255', TRUE);
-    $this->setMaxFileSize($uploadFileSize);
-    $this->addRule('uploadFile', ts('File size should be less than %1 MBytes (%2 bytes)', array(
-          1 => $uploadSize,
-          2 => $uploadFileSize,
-        )), 'maxfilesize', $uploadFileSize);
-    $this->addRule('uploadFile', ts('A valid file must be uploaded.'), 'uploadedfile');
-    $this->addRule('uploadFile', ts('Input file must be in CSV format'), 'utf8File');
-
-    $this->addElement('checkbox', 'skipColumnHeader', ts('First row contains column headers'));
-
-    //get the saved mapping details
-    $mappingArray = CRM_Core_BAO_Mapping::getMappings(CRM_Core_OptionGroup::getValue('mapping_type',
-      'Import Multi value custom data',
-      'name'
-    ));
-    $this->assign('savedMapping', $mappingArray);
-    $this->add('select', 'savedMapping', ts('Mapping Option'), array('' => ts('- select -')) + $mappingArray);
-
-    if ($loadeMapping = $this->get('loadedMapping')) {
-      $this->assign('loadedMapping', $loadeMapping);
-      $this->setDefaults(array('savedMapping' => $loadeMapping));
-    }
-
-    //contact types option
-    $contactOptions = array();
-    if (CRM_Contact_BAO_ContactType::isActive('Individual')) {
-      $contactOptions[] = $this->createElement('radio',
-        NULL, NULL, ts('Individual'), CRM_Import_Parser::CONTACT_INDIVIDUAL
-      );
-    }
-    if (CRM_Contact_BAO_ContactType::isActive('Household')) {
-      $contactOptions[] = $this->createElement('radio',
-        NULL, NULL, ts('Household'), CRM_Import_Parser::CONTACT_HOUSEHOLD
-      );
-    }
-    if (CRM_Contact_BAO_ContactType::isActive('Organization')) {
-      $contactOptions[] = $this->createElement('radio',
-        NULL, NULL, ts('Organization'), CRM_Import_Parser::CONTACT_ORGANIZATION
-      );
-    }
-
-    $this->addGroup($contactOptions, 'contactType',
-      ts('Contact Type')
-    );
-
-    $this->setDefaults(array(
-      'contactType' => CRM_Import_Parser::CONTACT_INDIVIDUAL,
-    ));
-
-    //build date formats
-    CRM_Core_Form_Date::buildAllowedDateFormats($this);
-
-    $this->addButtons(array(
-        array(
-          'type' => 'upload',
-          'name' => ts('Continue'),
-          'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
-          'isDefault' => TRUE,
-        ),
-        array(
-          'type' => 'cancel',
-          'name' => ts('Cancel'),
-        ),
-      )
-    );
+    $this->addContactTypeSelector();
   }
 
   /**
@@ -167,49 +81,14 @@ class CRM_Custom_Import_Form_DataSource extends CRM_Core_Form {
    * @return void
    */
   public function postProcess() {
-    $this->controller->resetPage('MapField');
-
-    $fileName = $this->controller->exportValue($this->_name, 'uploadFile');
-    $skipColumnHeader = $this->controller->exportValue($this->_name, 'skipColumnHeader');
-    $contactType = $this->controller->exportValue($this->_name, 'contactType');
-    $dateFormats = $this->controller->exportValue($this->_name, 'dateFormats');
-    $savedMapping = $this->controller->exportValue($this->_name, 'savedMapping');
-    $multipleCustomData = $this->controller->exportValue($this->_name, 'multipleCustomData');
-
-    $this->set('contactType', $contactType);
-    $this->set('dateFormats', $dateFormats);
-    $this->set('savedMapping', $savedMapping);
-    $this->set('multipleCustomData', $multipleCustomData);
-
-    $session = CRM_Core_Session::singleton();
-    $session->set("dateTypes", $dateFormats);
-
-    $config = CRM_Core_Config::singleton();
-    $seperator = $config->fieldSeparator;
-
-    $mapper = array();
-
-    $parser = new CRM_Custom_Import_Parser_Api($mapper);
-    $parser->setEntity($multipleCustomData);
-
-    $parser->setMaxLinesToProcess(100);
-    $parser->run($fileName, $seperator,
-      $mapper,
-      $skipColumnHeader,
-      CRM_Import_Parser::MODE_MAPFIELD, $contactType
-    );
-
-    // add all the necessary variables to the form
-    $parser->set($this);
-  }
+    $this->storeFormValues(array(
+      'contactType',
+      'dateFormats',
+      'savedMapping',
+      'multipleCustomData',
+    ));
 
-  /**
-   * Return a descriptive name for the page, used in wizard header
-   *
-   * @return string
-   */
-  public function getTitle() {
-    return ts('Upload Data');
+    $this->submitFileForMapping('CRM_Custom_Import_Parser_Api');
   }
 
 }
index c387bddfb0c8d8e53285b411e38f2944da4bb069..5aa49d09f1cac391ceba171d763debb90e098d47 100644 (file)
 /**
  * This class gets the name of the file to upload
  */
-class CRM_Event_Import_Form_DataSource extends CRM_Core_Form {
+class CRM_Event_Import_Form_DataSource extends CRM_Import_Form_DataSource {
 
-  /**
-   * Set variables up before form is built.
-   *
-   * @return void
-   */
-  public function preProcess() {
-    $session = CRM_Core_Session::singleton();
-    $session->pushUserContext(CRM_Utils_System::url('civicrm/event/import', 'reset=1'));
-    // check for post max size
-    CRM_Core_Config_Defaults::formatUnitSize(ini_get('post_max_size'), TRUE);
-  }
+  const PATH = 'civicrm/event/import';
+
+  const IMPORT_ENTITY = 'Participant';
 
   /**
    * Build the form object.
@@ -56,24 +48,7 @@ class CRM_Event_Import_Form_DataSource extends CRM_Core_Form {
    * @return void
    */
   public function buildQuickForm() {
-    //Setting Upload File Size
-    $config = CRM_Core_Config::singleton();
-
-    $uploadFileSize = CRM_Core_Config_Defaults::formatUnitSize($config->maxFileSize . 'm', TRUE);
-    $uploadSize = round(($uploadFileSize / (1024 * 1024)), 2);
-
-    $this->assign('uploadSize', $uploadSize);
-
-    $this->add('File', 'uploadFile', ts('Import Data File'), 'size=30 maxlength=255', TRUE);
-    $this->setMaxFileSize($uploadFileSize);
-    $this->addRule('uploadFile', ts('File size should be less than %1 MBytes (%2 bytes)', array(
-          1 => $uploadSize,
-          2 => $uploadFileSize,
-        )), 'maxfilesize', $uploadFileSize);
-    $this->addRule('uploadFile', ts('A valid file must be uploaded.'), 'uploadedfile');
-    $this->addRule('uploadFile', ts('Input file must be in CSV format'), 'utf8File');
-
-    $this->addElement('checkbox', 'skipColumnHeader', ts('First row contains column headers'));
+    parent::buildQuickForm();
 
     $duplicateOptions = array();
     $duplicateOptions[] = $this->createElement('radio',
@@ -85,72 +60,13 @@ class CRM_Event_Import_Form_DataSource extends CRM_Core_Form {
     $duplicateOptions[] = $this->createElement('radio',
       NULL, NULL, ts('No Duplicate Checking'), CRM_Import_Parser::DUPLICATE_NOCHECK
     );
-    // for contributions NOCHECK == SKIP
-    //      $duplicateOptions[] = $this->createElement('radio',
-    //          null, null, ts('No Duplicate Checking'), CRM_Import_Parser::DUPLICATE_NOCHECK);
-
     $this->addGroup($duplicateOptions, 'onDuplicate',
       ts('On Duplicate Entries')
     );
 
-    //get the saved mapping details
-    $mappingArray = CRM_Core_BAO_Mapping::getMappings(CRM_Core_OptionGroup::getValue('mapping_type',
-      'Import Participant',
-      'name'
-    ));
-    $this->assign('savedMapping', $mappingArray);
-    $this->add('select', 'savedMapping', ts('Mapping Option'), array('' => ts('- select -')) + $mappingArray);
-
-    if ($loadeMapping = $this->get('loadedMapping')) {
-      $this->assign('loadedMapping', $loadeMapping);
-      $this->setDefaults(array('savedMapping' => $loadeMapping));
-    }
-
-    $this->setDefaults(array(
-      'onDuplicate' =>
-        CRM_Import_Parser::DUPLICATE_SKIP,
-    ));
-
-    //contact types option
-    $contactOptions = array();
-    if (CRM_Contact_BAO_ContactType::isActive('Individual')) {
-      $contactOptions[] = $this->createElement('radio',
-        NULL, NULL, ts('Individual'), CRM_Import_Parser::CONTACT_INDIVIDUAL
-      );
-    }
-    if (CRM_Contact_BAO_ContactType::isActive('Household')) {
-      $contactOptions[] = $this->createElement('radio',
-        NULL, NULL, ts('Household'), CRM_Import_Parser::CONTACT_HOUSEHOLD
-      );
-    }
-    if (CRM_Contact_BAO_ContactType::isActive('Organization')) {
-      $contactOptions[] = $this->createElement('radio',
-        NULL, NULL, ts('Organization'), CRM_Import_Parser::CONTACT_ORGANIZATION
-      );
-    }
-    $this->addGroup($contactOptions, 'contactType', ts('Contact Type'));
-
-    $this->setDefaults(array(
-        'contactType' => CRM_Import_Parser::CONTACT_INDIVIDUAL,
-      )
-    );
-
-    //build date formats
-    CRM_Core_Form_Date::buildAllowedDateFormats($this);
+    $this->setDefaults(array('onDuplicate' => CRM_Import_Parser::DUPLICATE_SKIP));
 
-    $this->addButtons(array(
-        array(
-          'type' => 'upload',
-          'name' => ts('Continue'),
-          'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
-          'isDefault' => TRUE,
-        ),
-        array(
-          'type' => 'cancel',
-          'name' => ts('Cancel'),
-        ),
-      )
-    );
+    $this->addContactTypeSelector();
   }
 
   /**
@@ -159,46 +75,14 @@ class CRM_Event_Import_Form_DataSource extends CRM_Core_Form {
    * @return void
    */
   public function postProcess() {
-    $this->controller->resetPage('MapField');
-
-    $fileName = $this->controller->exportValue($this->_name, 'uploadFile');
-    $skipColumnHeader = $this->controller->exportValue($this->_name, 'skipColumnHeader');
-    $onDuplicate = $this->controller->exportValue($this->_name, 'onDuplicate');
-    $contactType = $this->controller->exportValue($this->_name, 'contactType');
-    $dateFormats = $this->controller->exportValue($this->_name, 'dateFormats');
-    $savedMapping = $this->controller->exportValue($this->_name, 'savedMapping');
-
-    $this->set('onDuplicate', $onDuplicate);
-    $this->set('contactType', $contactType);
-    $this->set('dateFormats', $dateFormats);
-    $this->set('savedMapping', $savedMapping);
-
-    $session = CRM_Core_Session::singleton();
-    $session->set("dateTypes", $dateFormats);
-
-    $config = CRM_Core_Config::singleton();
-    $seperator = $config->fieldSeparator;
-
-    $mapper = array();
-    $parser = new CRM_Event_Import_Parser_Participant($mapper);
-    $parser->setMaxLinesToProcess(100);
-    $parser->run($fileName, $seperator,
-      $mapper,
-      $skipColumnHeader,
-      CRM_Import_Parser::MODE_MAPFIELD, $contactType
-    );
-
-    // add all the necessary variables to the form
-    $parser->set($this);
-  }
+    $this->storeFormValues(array(
+      'onDuplicate',
+      'contactType',
+      'dateFormats',
+      'savedMapping',
+    ));
 
-  /**
-   * Return a descriptive name for the page, used in wizard header
-   *
-   * @return string
-   */
-  public function getTitle() {
-    return ts('Upload Data');
+    $this->submitFileForMapping('CRM_Event_Import_Parser_Participant');
   }
 
 }
index 8c49d34a39d06688f3ec9a04af9a5a9db123ea6c..d077ff0be8c3a665291e07a9ddcd2d60332e30fb 100644 (file)
@@ -45,7 +45,7 @@ class CRM_Event_Import_Form_Summary extends CRM_Import_Form_Summary {
    */
   public function preProcess() {
     // set the error message path to display
-    $errorFile = $this->assign('errorFile', $this->get('errorFile'));
+    $this->assign('errorFile', $this->get('errorFile'));
 
     $totalRowCount = $this->get('totalRowCount');
     $relatedCount = $this->get('relatedCount');
diff --git a/CRM/Import/Form/DataSource.php b/CRM/Import/Form/DataSource.php
new file mode 100644 (file)
index 0000000..d5f6a81
--- /dev/null
@@ -0,0 +1,195 @@
+<?php
+/*
+ +--------------------------------------------------------------------+
+ | CiviCRM version 4.6                                                |
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC (c) 2004-2015                                |
+ +--------------------------------------------------------------------+
+ | This file is a part of CiviCRM.                                    |
+ |                                                                    |
+ | CiviCRM is free software; you can copy, modify, and distribute it  |
+ | under the terms of the GNU Affero General Public License           |
+ | Version 3, 19 November 2007 and the CiviCRM Licensing Exception.   |
+ |                                                                    |
+ | CiviCRM is distributed in the hope that it will be useful, but     |
+ | WITHOUT ANY WARRANTY; without even the implied warranty of         |
+ | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.               |
+ | See the GNU Affero General Public License for more details.        |
+ |                                                                    |
+ | You should have received a copy of the GNU Affero General Public   |
+ | License and the CiviCRM Licensing Exception along                  |
+ | with this program; if not, contact CiviCRM LLC                     |
+ | at info[AT]civicrm[DOT]org. If you have questions about the        |
+ | GNU Affero General Public License or the licensing of CiviCRM,     |
+ | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
+ +--------------------------------------------------------------------+
+ */
+
+/**
+ *
+ * @package CRM
+ * @copyright CiviCRM LLC (c) 2004-2015
+ * $Id$
+ *
+ */
+
+/**
+ * Base class for upload-only import forms (all but Contact import)
+ */
+abstract class CRM_Import_Form_DataSource extends CRM_Core_Form {
+
+  /**
+   * Set variables up before form is built.
+   *
+   * @return void
+   */
+  public function preProcess() {
+    $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this, FALSE);
+    $params = "reset=1";
+    if ($this->_id) {
+      $params .= "&id={$this->_id}";
+    }
+    CRM_Core_Session::singleton()->pushUserContext(CRM_Utils_System::url(static::PATH, $params));
+
+    // check for post max size
+    CRM_Core_Config_Defaults::formatUnitSize(ini_get('post_max_size'), TRUE);
+  }
+
+  /**
+   * Common form elements.
+   *
+   * @return void
+   */
+  public function buildQuickForm() {
+    $config = CRM_Core_Config::singleton();
+
+    $uploadFileSize = CRM_Core_Config_Defaults::formatUnitSize($config->maxFileSize . 'm', TRUE);
+    $uploadSize = round(($uploadFileSize / (1024 * 1024)), 2);
+
+    $this->assign('uploadSize', $uploadSize);
+
+    $this->add('File', 'uploadFile', ts('Import Data File'), 'size=30 maxlength=255', TRUE);
+    $this->setMaxFileSize($uploadFileSize);
+    $this->addRule('uploadFile', ts('File size should be less than %1 MBytes (%2 bytes)', array(
+      1 => $uploadSize,
+      2 => $uploadFileSize,
+    )), 'maxfilesize', $uploadFileSize);
+    $this->addRule('uploadFile', ts('A valid file must be uploaded.'), 'uploadedfile');
+    $this->addRule('uploadFile', ts('Input file must be in CSV format'), 'utf8File');
+
+    $this->addElement('checkbox', 'skipColumnHeader', ts('First row contains column headers'));
+
+    //get the saved mapping details
+    $mappingArray = CRM_Core_BAO_Mapping::getMappings(CRM_Core_OptionGroup::getValue('mapping_type',
+      'Import ' . static::IMPORT_ENTITY,
+      'name'
+    ));
+    $this->assign('savedMapping', $mappingArray);
+    $this->add('select', 'savedMapping', ts('Mapping Option'), array('' => ts('- select -')) + $mappingArray);
+
+    if ($loadedMapping = $this->get('loadedMapping')) {
+      $this->assign('loadedMapping', $loadedMapping);
+      $this->setDefaults(array('savedMapping' => $loadedMapping));
+    }
+
+    //build date formats
+    CRM_Core_Form_Date::buildAllowedDateFormats($this);
+
+    $this->addButtons(array(
+        array(
+          'type' => 'upload',
+          'name' => ts('Continue'),
+          'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
+          'isDefault' => TRUE,
+        ),
+        array(
+          'type' => 'cancel',
+          'name' => ts('Cancel'),
+        ),
+      )
+    );
+  }
+
+  /**
+   * A long-winded way to add one radio element to the form
+   */
+  protected function addContactTypeSelector() {
+    //contact types option
+    $contactOptions = array();
+    if (CRM_Contact_BAO_ContactType::isActive('Individual')) {
+      $contactOptions[] = $this->createElement('radio',
+        NULL, NULL, ts('Individual'), CRM_Import_Parser::CONTACT_INDIVIDUAL
+      );
+    }
+    if (CRM_Contact_BAO_ContactType::isActive('Household')) {
+      $contactOptions[] = $this->createElement('radio',
+        NULL, NULL, ts('Household'), CRM_Import_Parser::CONTACT_HOUSEHOLD
+      );
+    }
+    if (CRM_Contact_BAO_ContactType::isActive('Organization')) {
+      $contactOptions[] = $this->createElement('radio',
+        NULL, NULL, ts('Organization'), CRM_Import_Parser::CONTACT_ORGANIZATION
+      );
+    }
+
+    $this->addGroup($contactOptions, 'contactType',
+      ts('Contact Type')
+    );
+
+    $this->setDefaults(array(
+      'contactType' => CRM_Import_Parser::CONTACT_INDIVIDUAL,
+    ));
+  }
+
+  /**
+   * @param array $names
+   */
+  protected function storeFormValues($names) {
+    foreach ($names as $name) {
+      $this->set($name, $this->controller->exportValue($this->_name, $name));
+    }
+  }
+
+  /**
+   * Common form postProcess
+   *
+   * @param string $parserClassName
+   */
+  protected function submitFileForMapping($parserClassName) {
+    $this->controller->resetPage('MapField');
+
+    $fileName = $this->controller->exportValue($this->_name, 'uploadFile');
+    $skipColumnHeader = $this->controller->exportValue($this->_name, 'skipColumnHeader');
+
+    $session = CRM_Core_Session::singleton();
+    $session->set("dateTypes", $this->get('dateFormats'));
+
+    $config = CRM_Core_Config::singleton();
+    $seperator = $config->fieldSeparator;
+
+    $mapper = array();
+
+    $parser = new $parserClassName($mapper);
+    $parser->setMaxLinesToProcess(100);
+    $parser->run($fileName,
+      $seperator,
+      $mapper,
+      $skipColumnHeader,
+      CRM_Import_Parser::MODE_MAPFIELD,
+      $this->get('contactType')
+    );
+
+    // add all the necessary variables to the form
+    $parser->set($this);
+  }
+
+  /**
+   * Return a descriptive name for the page, used in wizard header
+   *
+   * @return string
+   */
+  public function getTitle() {
+    return ts('Upload Data');
+  }
+
+}
index 9acf84984505123bd7d68f198878f297e0dae7af..27cf004179b71e8603ccb0f42ee08883a13202a8 100644 (file)
 /**
  * This class gets the name of the file to upload
  */
-class CRM_Member_Import_Form_DataSource extends CRM_Core_Form {
+class CRM_Member_Import_Form_DataSource extends CRM_Import_Form_DataSource {
 
-  /**
-   * Set variables up before form is built.
-   *
-   * @return void
-   */
-  public function preProcess() {
-    $session = CRM_Core_Session::singleton();
-    $session->pushUserContext(CRM_Utils_System::url('civicrm/member/import', 'reset=1'));
-    // check for post max size
-    CRM_Core_Config_Defaults::formatUnitSize(ini_get('post_max_size'), TRUE);
-  }
+  const PATH = 'civicrm/member/import';
+
+  const IMPORT_ENTITY = 'Membership';
 
   /**
    * Build the form object.
@@ -56,24 +48,7 @@ class CRM_Member_Import_Form_DataSource extends CRM_Core_Form {
    * @return void
    */
   public function buildQuickForm() {
-    //Setting Upload File Size
-    $config = CRM_Core_Config::singleton();
-
-    $uploadFileSize = CRM_Core_Config_Defaults::formatUnitSize($config->maxFileSize . 'm', TRUE);
-    $uploadSize = round(($uploadFileSize / (1024 * 1024)), 2);
-
-    $this->assign('uploadSize', $uploadSize);
-
-    $this->add('File', 'uploadFile', ts('Import Data File'), 'size=30 maxlength=255', TRUE);
-    $this->setMaxFileSize($uploadFileSize);
-    $this->addRule('uploadFile', ts('File size should be less than %1 MBytes (%2 bytes)', array(
-          1 => $uploadSize,
-          2 => $uploadFileSize,
-        )), 'maxfilesize', $uploadFileSize);
-    $this->addRule('uploadFile', ts('A valid file must be uploaded.'), 'uploadedfile');
-    $this->addRule('uploadFile', ts('Input file must be in CSV format'), 'utf8File');
-
-    $this->addElement('checkbox', 'skipColumnHeader', ts('First row contains column headers'));
+    parent::buildQuickForm();
 
     $duplicateOptions = array();
     $duplicateOptions[] = $this->createElement('radio',
@@ -90,61 +65,7 @@ class CRM_Member_Import_Form_DataSource extends CRM_Core_Form {
       'onDuplicate' => CRM_Import_Parser::DUPLICATE_SKIP,
     ));
 
-    //get the saved mapping details
-    $mappingArray = CRM_Core_BAO_Mapping::getMappings(CRM_Core_OptionGroup::getValue('mapping_type',
-      'Import Membership',
-      'name'
-    ));
-    $this->assign('savedMapping', $mappingArray);
-    $this->add('select', 'savedMapping', ts('Mapping Option'), array('' => ts('- select -')) + $mappingArray);
-
-    if ($loadeMapping = $this->get('loadedMapping')) {
-      $this->assign('loadedMapping', $loadeMapping);
-      $this->setDefaults(array('savedMapping' => $loadeMapping));
-    }
-
-    //contact types option
-    $contactOptions = array();
-    if (CRM_Contact_BAO_ContactType::isActive('Individual')) {
-      $contactOptions[] = $this->createElement('radio',
-        NULL, NULL, ts('Individual'), CRM_Import_Parser::CONTACT_INDIVIDUAL
-      );
-    }
-    if (CRM_Contact_BAO_ContactType::isActive('Household')) {
-      $contactOptions[] = $this->createElement('radio',
-        NULL, NULL, ts('Household'), CRM_Import_Parser::CONTACT_HOUSEHOLD
-      );
-    }
-    if (CRM_Contact_BAO_ContactType::isActive('Organization')) {
-      $contactOptions[] = $this->createElement('radio',
-        NULL, NULL, ts('Organization'), CRM_Import_Parser::CONTACT_ORGANIZATION
-      );
-    }
-
-    $this->addGroup($contactOptions, 'contactType',
-      ts('Contact Type')
-    );
-
-    $this->setDefaults(array(
-      'contactType' => CRM_Import_Parser::CONTACT_INDIVIDUAL,
-    ));
-
-    //build date formats
-    CRM_Core_Form_Date::buildAllowedDateFormats($this);
-
-    $this->addButtons(array(
-        array(
-          'type' => 'upload',
-          'name' => ts('Continue'),
-          'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
-          'isDefault' => TRUE,
-        ),
-        array(
-          'type' => 'cancel',
-          'name' => ts('Cancel'),
-        ),
-      )
-    );
+    $this->addContactTypeSelector();
   }
 
   /**
@@ -153,47 +74,14 @@ class CRM_Member_Import_Form_DataSource extends CRM_Core_Form {
    * @return void
    */
   public function postProcess() {
-    $this->controller->resetPage('MapField');
-
-    $fileName = $this->controller->exportValue($this->_name, 'uploadFile');
-    $skipColumnHeader = $this->controller->exportValue($this->_name, 'skipColumnHeader');
-    $onDuplicate = $this->controller->exportValue($this->_name, 'onDuplicate');
-    $contactType = $this->controller->exportValue($this->_name, 'contactType');
-    $dateFormats = $this->controller->exportValue($this->_name, 'dateFormats');
-    $savedMapping = $this->controller->exportValue($this->_name, 'savedMapping');
-
-    $this->set('onDuplicate', $onDuplicate);
-    $this->set('contactType', $contactType);
-    $this->set('dateFormats', $dateFormats);
-    $this->set('savedMapping', $savedMapping);
-
-    $session = CRM_Core_Session::singleton();
-    $session->set("dateTypes", $dateFormats);
-
-    $config = CRM_Core_Config::singleton();
-    $seperator = $config->fieldSeparator;
-
-    $mapper = array();
-
-    $parser = new CRM_Member_Import_Parser_Membership($mapper);
-    $parser->setMaxLinesToProcess(100);
-    $parser->run($fileName, $seperator,
-      $mapper,
-      $skipColumnHeader,
-      CRM_Import_Parser::MODE_MAPFIELD, $contactType
-    );
-
-    // add all the necessary variables to the form
-    $parser->set($this);
-  }
+    $this->storeFormValues(array(
+      'onDuplicate',
+      'contactType',
+      'dateFormats',
+      'savedMapping',
+    ));
 
-  /**
-   * Return a descriptive name for the page, used in wizard header
-   *
-   * @return string
-   */
-  public function getTitle() {
-    return ts('Upload Data');
+    $this->submitFileForMapping('CRM_Member_Import_Parser_Membership');
   }
 
 }
index d68eff14e0e4fa8a7cd42ef50bea56d1de3c237c..a00b38b5a2c68c6483a155532b7dd1597e65bf27 100644 (file)
@@ -44,7 +44,6 @@ class CRM_Member_Import_Form_Summary extends CRM_Import_Form_Summary {
    * @return void
    */
   public function preProcess() {
-
     // set the error message path to display
     $this->assign('errorFile', $this->get('errorFile'));