You are here

class CiviCrmApi in CiviCRM Entity 8.3

CiviCRM API implementation.

Hierarchy

Expanded class hierarchy of CiviCrmApi

2 files declare their use of CiviCrmApi
CivicrmFieldConfigTest.php in tests/src/Kernel/CivicrmFieldConfigTest.php
Query.php in src/Entity/Query/CiviCRM/Query.php
1 string reference to 'CiviCrmApi'
civicrm_entity.services.yml in ./civicrm_entity.services.yml
civicrm_entity.services.yml
1 service uses CiviCrmApi
civicrm_entity.api in ./civicrm_entity.services.yml
Drupal\civicrm_entity\CiviCrmApi

File

src/CiviCrmApi.php, line 10

Namespace

Drupal\civicrm_entity
View source
class CiviCrmApi implements CiviCrmApiInterface {

  /**
   * The CiviCRM service.
   *
   * @var \Drupal\civicrm\Civicrm
   */
  protected $civicrm;

  /**
   * Constructs a new CiviCrmApi object.
   *
   * @param \Drupal\civicrm\Civicrm $civicrm
   *   The CiviCRM service.
   */
  public function __construct(Civicrm $civicrm) {
    $this->civicrm = $civicrm;
  }

  /**
   * {@inheritdoc}
   */
  public function get($entity, array $params = []) {
    $this
      ->initialize();
    $result = civicrm_api3($entity, 'get', $params);
    return $result['values'];
  }

  /**
   * {@inheritdoc}
   */
  public function delete($entity, array $params) {
    $this
      ->initialize();
    $result = civicrm_api3($entity, 'delete', $params);
    return $result['values'];
  }
  public function validate($entity, $params) {
    $this
      ->initialize();
    if (!function_exists('_civicrm_api3_validate')) {
      require_once 'api/v3/utils.php';
    }
    return _civicrm_api3_validate($entity, 'create', $params);
  }

  /**
   * {@inheritdoc}
   */
  public function save($entity, array $params) {
    $this
      ->initialize();
    $result = civicrm_api3($entity, 'create', $params);
    return $result;
  }

  /**
   * {@inheritdoc}
   */
  public function getFields($entity, $action = '') {
    $this
      ->initialize();
    $result = civicrm_api3($entity, 'getfields', [
      'sequential' => 1,
      'action' => $action,
    ]);
    return $result['values'];
  }

  /**
   * {@inheritdoc}
   */
  public function getOptions($entity, $field_name) {
    $this
      ->initialize();
    $result = civicrm_api3($entity, 'getoptions', [
      'field' => $field_name,
    ]);
    return $result['values'];
  }

  /**
   * {@inheritdoc}
   */
  public function getCount($entity, array $params = []) {
    $this
      ->initialize();
    $result = civicrm_api3($entity, 'getcount', $params);
    return is_int($result) ? $result : $result['result'];
  }

  /**
   * {@inheritdoc}
   */
  public function getSingle($entity, array $params = []) {
    $this
      ->initialize();
    $result = civicrm_api3($entity, 'getsingle', $params);
    if (isset($result['is_error'])) {
      return [];
    }
    return $result;
  }

  /**
   * {@inheritdoc}
   */
  public function getValue($entity, array $params = []) {
    $this
      ->initialize();
    $result = civicrm_api3($entity, 'getvalue', $params);
    return $result;
  }

  /**
   * {@inheritdoc}
   */
  public function civicrmInitialize() {
    $this->civicrm
      ->initialize();
  }

  /**
   * {@inheritdoc}
   */
  public function getCustomFieldMetadata($field_name) {
    $field_name = explode('_', $field_name, 2);

    // There are field names that are just single names that can't be broken
    // down into two values.
    if (count($field_name) < 2) {
      return FALSE;
    }
    list(, $id) = $field_name;
    try {
      $values = $this
        ->get('CustomField', [
        'id' => $id,
        'is_active' => 1,
      ]);
      $values = reset($values);
      if (!empty($values)) {

        // Include information from group.
        if (isset($values['custom_group_id']) && ($custom_group_values = $this
          ->get('CustomGroup', [
          'sequential' => 1,
          'id' => $values['custom_group_id'],
        ]))) {
          $custom_group_values = reset($custom_group_values);
          $values += [
            'title' => $custom_group_values['title'],
            'extends' => $custom_group_values['extends'],
            'table_name' => $custom_group_values['table_name'],
            'is_multiple' => (bool) $custom_group_values['is_multiple'],
            'max_multiple' => $custom_group_values['max_multiple'] ?? -1,
          ];
        }
        return $values;
      }
      return FALSE;
    } catch (\CiviCRM_API3_Exception $e) {
      return FALSE;
    }
  }

  /**
   * Ensures that CiviCRM is loaded and API function available.
   */
  protected function initialize() {
    if (!function_exists('civicrm_api3')) {
      $this->civicrm
        ->initialize();
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CiviCrmApi::$civicrm protected property The CiviCRM service.
CiviCrmApi::civicrmInitialize public function Initialize the CiviCRM API. Overrides CiviCrmApiInterface::civicrmInitialize
CiviCrmApi::delete public function Delete an entity in CiviCRM. Overrides CiviCrmApiInterface::delete
CiviCrmApi::get public function Get an entity from CiviCRM. Overrides CiviCrmApiInterface::get
CiviCrmApi::getCount public function Get the count of entries for an entity. Overrides CiviCrmApiInterface::getCount
CiviCrmApi::getCustomFieldMetadata public function Retrieve custom field metadata for a field. Overrides CiviCrmApiInterface::getCustomFieldMetadata
CiviCrmApi::getFields public function Get fields from the CiviCRM entity. Overrides CiviCrmApiInterface::getFields
CiviCrmApi::getOptions public function Get options for the CiviCRM entity field. Overrides CiviCrmApiInterface::getOptions
CiviCrmApi::getSingle public function Get single from the CiviCRM entity. Overrides CiviCrmApiInterface::getSingle
CiviCrmApi::getValue public function Get values from the CiviCRM entity. Overrides CiviCrmApiInterface::getValue
CiviCrmApi::initialize protected function Ensures that CiviCRM is loaded and API function available.
CiviCrmApi::save public function Save and update an entity in CiviCRM. Overrides CiviCrmApiInterface::save
CiviCrmApi::validate public function
CiviCrmApi::__construct public function Constructs a new CiviCrmApi object.