function buildForm(&$form)
 {
     // check if we're in test mode
     if ($this->_mode == 'test') {
         $test = ' Test';
     } else {
         $test = '';
     }
     // we don't need the default stuff:
     $form->_paymentFields = array();
     $form->add('text', 'bank_account_number', ts('IBAN'), array('size' => 34, 'maxlength' => 34), TRUE);
     $form->add('text', 'bank_identification_number', ts('BIC'), array('size' => 11, 'maxlength' => 11), TRUE);
     $form->add('text', 'bank_name', ts('Bank Name'), array('size' => 20, 'maxlength' => 64), FALSE);
     $form->add('text', 'account_holder', ts('Account Holder'), array('size' => 20, 'maxlength' => 64), FALSE);
     $form->add('select', 'cycle_day', ts('Collection Day'), CRM_Sepa_Logic_Settings::getListSetting("cycledays", range(1, 28), $this->_creditorId), FALSE);
     $form->addDate('start_date', ts('start date'), TRUE, array());
     $form->registerRule('sepa_iban_valid', 'callback', 'rule_valid_IBAN', 'CRM_Sepa_Logic_Verification');
     $form->registerRule('sepa_bic_valid', 'callback', 'rule_valid_BIC', 'CRM_Sepa_Logic_Verification');
     $form->addRule('bank_account_number', ts('This is not a correct IBAN.'), 'sepa_iban_valid');
     $form->addRule('bank_identification_number', ts('This is not a correct BIC.'), 'sepa_bic_valid');
     $rcur_notice_days = (int) CRM_Sepa_Logic_Settings::getSetting("batching.RCUR.notice", $this->_creditorId);
     $ooff_notice_days = (int) CRM_Sepa_Logic_Settings::getSetting("batching.OOFF.notice", $this->_creditorId);
     $timestamp_rcur = strtotime("now + {$rcur_notice_days} days");
     $timestamp_ooff = strtotime("now + {$ooff_notice_days} days");
     $earliest_rcur_date = array(date('Y', $timestamp_rcur), date('m', $timestamp_rcur), date('d', $timestamp_rcur));
     $earliest_ooff_date = array(date('Y', $timestamp_ooff), date('m', $timestamp_ooff), date('d', $timestamp_ooff));
     $form->assign('earliest_rcur_date', $earliest_rcur_date);
     $form->assign('earliest_ooff_date', $earliest_ooff_date);
     CRM_Core_Region::instance('billing-block')->add(array('template' => 'CRM/Core/Payment/SEPA/SDD.tpl', 'weight' => -1));
 }
 /**
  * Will prepare the form and look up all necessary data
  */
 function prepareCreateForm($contact_id)
 {
     // load financial types
     $this->assign("financial_types", CRM_Contribute_PseudoConstant::financialType());
     $this->assign("date", date('Y-m-d'));
     $this->assign("start_date", date('Y-m-d'));
     // first, try to load contact
     $contact = civicrm_api('Contact', 'getsingle', array('version' => 3, 'id' => $contact_id));
     if (isset($contact['is_error']) && $contact['is_error']) {
         CRM_Core_Session::setStatus(sprintf(ts("Couldn't find contact #%s"), $contact_id), ts('Error'), 'error');
         $this->assign("display_name", "ERROR");
         return;
     }
     $this->assign("contact_id", $contact_id);
     $this->assign("display_name", $contact['display_name']);
     // look up campaigns
     $campaign_query = civicrm_api('Campaign', 'get', array('version' => 3, 'is_active' => 1, 'option.limit' => 9999, 'option.sort' => 'title'));
     $campaigns = array();
     $campaigns[''] = ts("No Campaign");
     if (isset($campaign_query['is_error']) && $campaign_query['is_error']) {
         CRM_Core_Session::setStatus(ts("Couldn't load campaign list."), ts('Error'), 'error');
     } else {
         foreach ($campaign_query['values'] as $campaign_id => $campaign) {
             $campaigns[$campaign_id] = $campaign['title'];
         }
     }
     $this->assign('campaigns', $campaigns);
     // look up account in other SEPA mandates
     $known_accounts = array();
     $query_sql = "SELECT DISTINCT iban, bic FROM civicrm_sdd_mandate WHERE contact_id={$contact_id} AND (creation_date >= (NOW() - INTERVAL 60 DAY));";
     $old_mandates = CRM_Core_DAO::executeQuery($query_sql);
     while ($old_mandates->fetch()) {
         $value = $old_mandates->iban . '/' . $old_mandates->bic;
         array_push($known_accounts, array("name" => $old_mandates->iban, "value" => $value));
     }
     // look up account in CiviBanking (if enabled...)
     $iban_reference_type = CRM_Core_OptionGroup::getValue('civicrm_banking.reference_types', 'IBAN', 'value', 'String', 'id');
     if ($iban_reference_type) {
         $accounts = civicrm_api('BankingAccount', 'get', array('version' => 3, 'contact_id' => $contact_id));
         if (isset($accounts['is_error']) && $accounts['is_error']) {
             // this probably means, that CiviBanking is not installed...
         } else {
             foreach ($accounts['values'] as $account_id => $account) {
                 $account_ref = civicrm_api('BankingAccountReference', 'getsingle', array('version' => 3, 'ba_id' => $account_id, 'reference_type_id' => $iban_reference_type));
                 if (isset($account_ref['is_error']) && $account_ref['is_error']) {
                     // this would also be an error, if no reference is set...
                 } else {
                     $account_data = json_decode($account['data_parsed']);
                     if (isset($account_data->BIC)) {
                         // we have IBAN and BIC -> add:
                         $value = $account_ref['reference'] . '/' . $account_data->BIC;
                         array_push($known_accounts, array("name" => $account_ref['reference'], "value" => $value));
                     }
                 }
             }
         }
     }
     // remove duplicate entries
     $known_account_names = array();
     foreach ($known_accounts as $index => $entry) {
         if (isset($known_account_names[$entry['name']])) {
             unset($known_accounts[$index]);
         } else {
             $known_account_names[$entry['name']] = $index;
         }
     }
     // add default entry
     array_push($known_accounts, array("name" => ts("enter new account"), "value" => "/"));
     $this->assign("known_accounts", $known_accounts);
     // look up creditors
     $creditor_query = civicrm_api('SepaCreditor', 'get', array('version' => 3));
     $creditors = array();
     if (isset($creditor_query['is_error']) && $creditor_query['is_error']) {
         CRM_Core_Session::setStatus(ts("Couldn't find any creditors."), ts('Error'), 'error');
     } else {
         foreach ($creditor_query['values'] as $creditor_id => $creditor) {
             $creditors[$creditor_id] = $creditor['name'];
         }
     }
     $this->assign('creditors', $creditors);
     // add cycle_days per creditor
     $creditor2cycledays = array();
     foreach ($creditors as $creditor_id => $creditor_name) {
         $creditor2cycledays[$creditor_id] = CRM_Sepa_Logic_Settings::getListSetting("cycledays", range(1, 28), $creditor_id);
     }
     $this->assign("creditor2cycledays", json_encode($creditor2cycledays));
     // all seems to be ok.
     $this->assign("submit_url", CRM_Utils_System::url('civicrm/sepa/cmandate'));
     // copy known parameters
     $copy_params = array('contact_id', 'creditor_id', 'total_amount', 'financial_type_id', 'campaign_id', 'source', 'note', 'iban', 'bic', 'date', 'mandate_type', 'start_date', 'cycle_day', 'interval', 'end_date', 'reference');
     foreach ($copy_params as $parameter) {
         if (isset($_REQUEST[$parameter])) {
             $this->assign($parameter, $_REQUEST[$parameter]);
         }
     }
     // set default creditor, if not provided
     if (empty($_REQUEST['creditor_id'])) {
         $default_creditor = CRM_Sepa_Logic_Settings::defaultCreditor();
         if ($default_creditor != NULL) {
             $this->assign('creditor_id', $default_creditor->id);
         }
     }
     // FIXME: Create "default" setting for mandate type (see SEPA-332)
     //$this->assign('mandate_type', isset($_REQUEST['mandate_type']) ? $_REQUEST['mandate_type'] : 'RCUR');
 }