/**
 * will add the default creditor_id if no creditor_id is given, and the default creditor is valid
 */
function _civicrm_api3_sepa_mandate_adddefaultcreditor(&$params)
{
    if (empty($params['creditor_id'])) {
        $default_creditor = CRM_Sepa_Logic_Settings::defaultCreditor();
        if ($default_creditor != NULL) {
            $params['creditor_id'] = $default_creditor->id;
        }
    }
}
 /**
  * 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');
 }