<commit_before><?php

require_once 'CRM/Core/Form.php';

/**
 * Form controller class
 *
 * @see http://wiki.civicrm.org/confluence/display/CRMDOC43/QuickForm+Reference
 */
class CRM_Currentmemberships_Form_GetMembership extends CRM_Core_Form {
  public function buildQuickForm() {

    // add form elements
    $this->add(
      'text', // field type
      'start_date', // field name
      'Start from date', // field label
       FALSE // is required
    );

    $this->add(
        'text',
        'end_date',
        'Start To Date',
         False
    );
    $this->addButtons(array(
      array(
        'type' => 'submit',
        'name' => ts('Submit'),
        'isDefault' => TRUE,
      ),
    ));

    // export form elements
    $this->assign('elementNames', $this->getRenderableElementNames());
    $this->assign('membership',$this->membership_get_example());
    parent::buildQuickForm();
  }

  public function postProcess() {
    $values = $this->exportValues();
    CRM_Core_Session::setStatus(ts('You picked start date from "%1 to %2"', array(
      1 => $values['start_date'],
      2 => $values['end_date']
    )));

    $params = array();

    if(!empty($values['start_date']) && !empty($values['end_date']))
    $params = array('start_date' => array(">="=>$values['start_date']), 'start_date' => array("<="=>$values['end_date']));

    $this->assign('membership',$this->membership_get_example($params));
    parent::postProcess();
  }


  /**
   * Get the fields/elements defined in this form.
   *
   * @return array (string)
   */
  public function getRenderableElementNames() {
    // The _elements list includes some items which should not be
    // auto-rendered in the loop -- such as "qfKey" and "buttons".  These
    // items don't have labels.  We'll identify renderable by filtering on
    // the 'label'.
    $elementNames = array();
    foreach ($this->_elements as $element) {
      /** @var HTML_QuickForm_Element $element */
      $label = $element->getLabel();
      if (!empty($label)) {
        $elementNames[] = $element->getName();
      }
    }
    return $elementNames;
  }


  function membership_get_example($params=array())
  {
    try {
      $result = civicrm_api3('membership', 'get', $params);
    } catch (CiviCRM_API3_Exception $e) {
      // handle error here
      $errorMessage = $e->getMessage();
      $errorCode = $e->getErrorCode();
      $errorData = $e->getExtraParams();
      return array('error' => $errorMessage, 'error_code' => $errorCode, 'error_data' => $errorData);
    }
    return $result;
  }
}<commit_msg>refactor the code<commit_after><?php

require_once 'CRM/Core/Form.php';

/**
 * Form controller class
 *
 * @see http://wiki.civicrm.org/confluence/display/CRMDOC43/QuickForm+Reference
 */
class CRM_Currentmemberships_Form_GetMembership extends CRM_Core_Form {
  public function buildQuickForm() {

    // add form elements
    $this->add(
      'text', // field type
      'start_date', // field name
      'Start from date', // field label
       FALSE // is required
    );

    $this->add(
        'text',
        'end_date',
        'Start To Date',
         False
    );
    $this->addButtons(array(
      array(
        'type' => 'submit',
        'name' => ts('Submit'),
        'isDefault' => TRUE,
      ),
    ));

    // export form elements
    $this->assign('elementNames', $this->getRenderableElementNames());
    $this->assign('membership',$this->getMembership());
    parent::buildQuickForm();
  }

  public function postProcess() {
    $values = $this->exportValues();
    CRM_Core_Session::setStatus(ts('You picked start date from "%1 to %2"', array(
      1 => $values['start_date'],
      2 => $values['end_date']
    )));

    $params = array();
    if(!empty($values['start_date']) && !empty($values['end_date']))
    $params = array('start_date' => array(">="=>$values['start_date']), 'start_date' => array("<="=>$values['end_date']));

    $this->assign('membership',$this->getMembership($params));
    parent::postProcess();
  }


  /**
   * Get the fields/elements defined in this form.
   *
   * @return array (string)
   */
  public function getRenderableElementNames() {
    // The _elements list includes some items which should not be
    // auto-rendered in the loop -- such as "qfKey" and "buttons".  These
    // items don't have labels.  We'll identify renderable by filtering on
    // the 'label'.
    $elementNames = array();
    foreach ($this->_elements as $element) {
      /** @var HTML_QuickForm_Element $element */
      $label = $element->getLabel();
      if (!empty($label)) {
        $elementNames[] = $element->getName();
      }
    }
    return $elementNames;
  }


  function getMembership($params=array())
  {
    try {
      $result = civicrm_api3('membership', 'get', $params);
    } catch (CiviCRM_API3_Exception $e) {
      // handle error here
      $errorMessage = $e->getMessage();
      $errorCode = $e->getErrorCode();
      $errorData = $e->getExtraParams();
      return array('error' => $errorMessage, 'error_code' => $errorCode, 'error_data' => $errorData);
    }
    return $result;
  }
}
{'fraction_non_alphanumeric': 0.16861370716510904, 'fraction_numerical': 0.003115264797507788, 'mean_word_length': 2.9273700305810397, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 61, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '7548704', 'n_tokens_mistral': 1613, 'n_tokens_neox': 1580, 'n_words': 350}