AjaxController.php in Webform CiviCRM Integration 8.5
File
src/Controller/AjaxController.php
View source
<?php
namespace Drupal\webform_civicrm\Controller;
use Drupal\civicrm\Civicrm;
use Symfony\Component\HttpFoundation\RequestStack;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
class AjaxController implements ContainerInjectionInterface {
protected $requestStack;
protected $civicrm;
public function __construct(Civicrm $civicrm, RequestStack $requestStack) {
$this->civicrm = $civicrm;
$this->requestStack = $requestStack;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('civicrm'), $container
->get('request_stack'));
}
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);
}
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);
}
return new JsonResponse($data);
}
}