You are here

class AjaxController in Webform CiviCRM Integration 8.5

Hierarchy

Expanded class hierarchy of AjaxController

File

src/Controller/AjaxController.php, line 11

Namespace

Drupal\webform_civicrm\Controller
View source
class AjaxController implements ContainerInjectionInterface {
  protected $requestStack;

  /**
   * The CiviCRM service.
   *
   * @var \Drupal\civicrm\Civicrm
   */
  protected $civicrm;
  public function __construct(Civicrm $civicrm, RequestStack $requestStack) {
    $this->civicrm = $civicrm;
    $this->requestStack = $requestStack;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('civicrm'), $container
      ->get('request_stack'));
  }

  /**
   * Handles the ajax request.
   *
   * @param string $operation
   *   The operation to perform: stateProvince or county
   */
  public function handle($key, $input = '', $isBilling = FALSE) {
    $this->civicrm
      ->initialize();
    if ($key === 'stateProvince') {
      $isBilling = $isBilling === 'false' ? FALSE : $isBilling;
      return $this
        ->stateProvince($input, $isBilling);
    }
    elseif ($key === 'county') {
      return $this
        ->county($input);
    }
    else {
      $processor = \Drupal::service('webform_civicrm.webform_ajax');
      return new JsonResponse($processor
        ->contactAjax($key, $input));
    }
  }
  protected function stateProvince($input, $isBilling = FALSE) {
    if (!$input || (int) $input != $input && $input != 'default') {
      $data = [
        '' => t('- first choose a country'),
      ];
    }
    else {
      $data = \Drupal::service('webform_civicrm.utils')
        ->wf_crm_get_states($input, $isBilling);
    }

    // @todo use Drupal's cacheable response?
    return new JsonResponse($data);
  }
  protected function county($input) {
    $data = [];
    $utils = \Drupal::service('webform_civicrm.utils');
    if (strpos($input, '-') !== FALSE) {
      list($state, $country) = explode('-', $input);
      $params = [
        'field' => 'county_id',
        'state_province_id' => $utils
          ->wf_crm_state_abbr($state, 'id', $country),
      ];
      $data = $utils
        ->wf_crm_apivalues('address', 'getoptions', $params);
    }

    // @todo use Drupal's cacheable response?
    return new JsonResponse($data);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AjaxController::$civicrm protected property The CiviCRM service.
AjaxController::$requestStack protected property
AjaxController::county protected function
AjaxController::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
AjaxController::handle public function Handles the ajax request.
AjaxController::stateProvince protected function
AjaxController::__construct public function