<commit_before><?php
/*-------------------------------------------------------+
| ICA Event Registration Module                          |
| Copyright (C) 2017 SYSTOPIA                            |
+--------------------------------------------------------+
| This program is released as free software under the    |
| Affero GPL license. You can redistribute it and/or     |
| modify it under the terms of this license which you    |
| can read by viewing the included agpl.txt or online    |
| at www.gnu.org/licenses/agpl.html. Removal of this     |
| copyright header is strictly prohibited without        |
| written permission from the original author(s).        |
+--------------------------------------------------------*/

define('MAX_LINE_COUNT', 20);

/**
 * TODO DOKU
 *
 * @see https://wiki.civicrm.org/confluence/display/CRMDOC/QuickForm+Reference
 */
class CRM_Registration_Form_RegistrationPaymentEdit extends CRM_Core_Form {


  protected $cid                      = NULL;
  protected $contribution             = NULL;
  protected $new_contribution         = NULL;
  protected $registration_id          = NULL;
  protected $line_items               = NULL;
  protected $role2label               = NULL;
  protected $role2amount              = NULL;
  protected $participants             = NULL;
  protected $participant2label        = NULL;
  protected $contribStatus2label      = NULL;
  protected $paymentInstrument2label  = NULL;

  /**
   * Check
   */
  public function preProcess() {
    // load contribution_id from URL
    $this->cid = CRM_Utils_Request::retrieve('cid', 'Integer');
    if (empty($this->cid)) {
      CRM_Core_Error::fatal("No contribution ID (cid) given.");
    } else {
      $this->add('hidden', 'cid', $this->cid);
    }

    // initialize variables/data
    $this->initializeInternalData();

    // load participants
    $this->populateParticipants();

    // load contribution stati
    $this->setContributionStati();
  }

  /**
   * Create form
   */
  public function buildQuickForm() {

    // generate lines
    $this->assign('line_numbers',   range(1, MAX_LINE_COUNT));
    $this->assign('max_line_count', MAX_LINE_COUNT);
    for ($i=1; $i <= MAX_LINE_COUNT; $i++) {
      $this->add('select',
        "participant_id_{$i}",
        'Participant',
        $this->participant2label,
        FALSE,
        array('class' => 'participant-id')
      );

      $this->add('select',
        "participant_role_{$i}",
        '(new) role',
        $this->role2label,
        FALSE,
        array('class' => 'participant-role')
      );

      $this->add('text',
        "participant_amount_{$i}",
        'amount',
        'readonly'
      );

    }

    $this->add('select',
      "contribution_status",
      'contribution_status',
      $this->contribStatus2label,
      FALSE,
      array('class' => 'contribution-status')
    );
    $this->add('static',
      "contribution_sum_description",
      'contribution_sum_description',
      "<b>Contribution</b>"
    );
    $this->add('text',
      "contribution_sum",
      'accumulated_amount',
      'readonly'
    );

    $this->assign('role2amount', json_encode($this->role2amount));
    $this->assign('line_count', count($this->participants));

    //
    $this->add('select',
      "payment_method",
      'Payment Method',
      $this->paymentInstrument2label,
      FALSE,
      array('class' => 'contribution-paymentMethod')
    );

    $this->addButtons(array(
      array(
        'type' => 'submit',
        'name' => ts('Submit'),
        'isDefault' => TRUE,
      ),
    ));

    // export form elements
    parent::buildQuickForm();
  }

  /**
   * get the participants connected to the registration id
   * and set participant2label as well
   */
  protected function populateParticipants() {
    $this->participants = array();
    $this->participant2label = array();

    // participant API doesn't work here see CRM-16036
    // so we'll use SQL (and need to find the right table)

    // first: find the custom field / group involved
    $registration_id_field    = CRM_Registration_CustomData::getCustomField('GA_Registration', 'registration_id');
    $registration_id_column   = $registration_id_field['column_name'];
    $registration_group_table = CRM_Registration_CustomData::getGroupTable('GA_Registration');
    if (empty($registration_id_field) || empty($registration_group_table)) {
      // error: Field not found
      return;
    }

    $reg_id = $this->findTransactionIndex($this->registration_id);

    $participant_selector = CRM_Core_DAO::executeQuery("
      SELECT entity_id AS participant_id
      FROM {$registration_group_table} WHERE {$registration_id_column} = '{$reg_id['transaction_number']}'");
    $participant_ids = array();
    while ($participant_selector->fetch()) {
      $participant_ids[] = $participant_selector->participant_id;
    }

    if (!empty($participant_ids)) {
      $this->participants = civicrm_api3('Participant', 'get', array(
      'id'            => array('IN' => $participant_ids),
      'options.limit' => 0))['values'];
    }
    foreach ($this->participants as $particpant) {
      $this->participant2label[$particpant['id']] = "{$particpant['display_name']} ({$particpant['participant_fee_level']})";
    }
    $this->participant2label[0] = "";
  }

  public function setContributionStati() {
    $stati = civicrm_api3('OptionValue', 'get', array(
      'sequential' => 1,
      'options.limit'   => 0,
      'option_group_id' => "contribution_status",
    ))['values'];
    $this->contribStatus2label = array();
    // this should result in a valid contribStatus2label
    $this->contribStatus2label = CRM_Registration_Configuration::filterContributionStati($stati, $this->contribution['contribution_status']);
  }

  /**
   * set the default (=current) values in the form
   */
  public function setDefaultValues() {
    $values = array();
<commit_msg>add id to participant dropdown menu<commit_after><?php
/*-------------------------------------------------------+
| ICA Event Registration Module                          |
| Copyright (C) 2017 SYSTOPIA                            |
+--------------------------------------------------------+
| This program is released as free software under the    |
| Affero GPL license. You can redistribute it and/or     |
| modify it under the terms of this license which you    |
| can read by viewing the included agpl.txt or online    |
| at www.gnu.org/licenses/agpl.html. Removal of this     |
| copyright header is strictly prohibited without        |
| written permission from the original author(s).        |
+--------------------------------------------------------*/

define('MAX_LINE_COUNT', 20);

/**
 * TODO DOKU
 *
 * @see https://wiki.civicrm.org/confluence/display/CRMDOC/QuickForm+Reference
 */
class CRM_Registration_Form_RegistrationPaymentEdit extends CRM_Core_Form {


  protected $cid                      = NULL;
  protected $contribution             = NULL;
  protected $new_contribution         = NULL;
  protected $registration_id          = NULL;
  protected $line_items               = NULL;
  protected $role2label               = NULL;
  protected $role2amount              = NULL;
  protected $participants             = NULL;
  protected $participant2label        = NULL;
  protected $contribStatus2label      = NULL;
  protected $paymentInstrument2label  = NULL;

  /**
   * Check
   */
  public function preProcess() {
    // load contribution_id from URL
    $this->cid = CRM_Utils_Request::retrieve('cid', 'Integer');
    if (empty($this->cid)) {
      CRM_Core_Error::fatal("No contribution ID (cid) given.");
    } else {
      $this->add('hidden', 'cid', $this->cid);
    }

    // initialize variables/data
    $this->initializeInternalData();

    // load participants
    $this->populateParticipants();

    // load contribution stati
    $this->setContributionStati();
  }

  /**
   * Create form
   */
  public function buildQuickForm() {

    // generate lines
    $this->assign('line_numbers',   range(1, MAX_LINE_COUNT));
    $this->assign('max_line_count', MAX_LINE_COUNT);
    for ($i=1; $i <= MAX_LINE_COUNT; $i++) {
      $this->add('select',
        "participant_id_{$i}",
        'Participant',
        $this->participant2label,
        FALSE,
        array('class' => 'participant-id')
      );

      $this->add('select',
        "participant_role_{$i}",
        '(new) role',
        $this->role2label,
        FALSE,
        array('class' => 'participant-role')
      );

      $this->add('text',
        "participant_amount_{$i}",
        'amount',
        'readonly'
      );

    }

    $this->add('select',
      "contribution_status",
      'contribution_status',
      $this->contribStatus2label,
      FALSE,
      array('class' => 'contribution-status')
    );
    $this->add('static',
      "contribution_sum_description",
      'contribution_sum_description',
      "<b>Contribution</b>"
    );
    $this->add('text',
      "contribution_sum",
      'accumulated_amount',
      'readonly'
    );

    $this->assign('role2amount', json_encode($this->role2amount));
    $this->assign('line_count', count($this->participants));

    //
    $this->add('select',
      "payment_method",
      'Payment Method',
      $this->paymentInstrument2label,
      FALSE,
      array('class' => 'contribution-paymentMethod')
    );

    $this->addButtons(array(
      array(
        'type' => 'submit',
        'name' => ts('Submit'),
        'isDefault' => TRUE,
      ),
    ));

    // export form elements
    parent::buildQuickForm();
  }

  /**
   * get the participants connected to the registration id
   * and set participant2label as well
   */
  protected function populateParticipants() {
    $this->participants = array();
    $this->participant2label = array();

    // participant API doesn't work here see CRM-16036
    // so we'll use SQL (and need to find the right table)

    // first: find the custom field / group involved
    $registration_id_field    = CRM_Registration_CustomData::getCustomField('GA_Registration', 'registration_id');
    $registration_id_column   = $registration_id_field['column_name'];
    $registration_group_table = CRM_Registration_CustomData::getGroupTable('GA_Registration');
    if (empty($registration_id_field) || empty($registration_group_table)) {
      // error: Field not found
      return;
    }

    $reg_id = $this->findTransactionIndex($this->registration_id);

    $participant_selector = CRM_Core_DAO::executeQuery("
      SELECT entity_id AS participant_id
      FROM {$registration_group_table} WHERE {$registration_id_column} = '{$reg_id['transaction_number']}'");
    $participant_ids = array();
    while ($participant_selector->fetch()) {
      $participant_ids[] = $participant_selector->participant_id;
    }

    if (!empty($participant_ids)) {
      $this->participants = civicrm_api3('Participant', 'get', array(
      'id'            => array('IN' => $participant_ids),
      'options.limit' => 0))['values'];
    }
    foreach ($this->participants as $particpant) {
      $this->participant2label[$particpant['id']] = "{$particpant['display_name']} ({$particpant['participant_fee_level']}) [{$participant['id']}";
    }
    $this->participant2label[0] = "";
  }

  public function setContributionStati() {
    $stati = civicrm_api3('OptionValue', 'get', array(
      'sequential' => 1,
      'options.limit'   => 0,
      'option_group_id' => "contribution_status",
    ))['values'];
    $this->contribStatus2label = array();
    // this should result in a valid contribStatus2label
    $this->contribStatus2label = CRM_Registration_Configuration::filterContributionStati($stati, $this->contribution['contribution_status']);
  }

  /**
   * set the default (=current) values in the form
   */
  public function setDefaultValues() {
    $values = array();

{'fraction_non_alphanumeric': 0.16406118675407633, 'fraction_numerical': 0.006219532694570516, 'mean_word_length': 2.5829569406805177, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 109, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1363540', 'n_tokens_mistral': 3614, 'n_tokens_neox': 3395, 'n_words': 704}