View source
<?php
namespace Drupal\civicrm_entity;
use Drupal\civicrm\Civicrm;
class CiviCrmApi implements CiviCrmApiInterface {
protected $civicrm;
public function __construct(Civicrm $civicrm) {
$this->civicrm = $civicrm;
}
public function get($entity, array $params = []) {
$this
->initialize();
$result = civicrm_api3($entity, 'get', $params);
return $result['values'];
}
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);
}
public function save($entity, array $params) {
$this
->initialize();
$result = civicrm_api3($entity, 'create', $params);
return $result;
}
public function getFields($entity, $action = '') {
$this
->initialize();
$result = civicrm_api3($entity, 'getfields', [
'sequential' => 1,
'action' => $action,
]);
return $result['values'];
}
public function getOptions($entity, $field_name) {
$this
->initialize();
$result = civicrm_api3($entity, 'getoptions', [
'field' => $field_name,
]);
return $result['values'];
}
public function getCount($entity, array $params = []) {
$this
->initialize();
$result = civicrm_api3($entity, 'getcount', $params);
return is_int($result) ? $result : $result['result'];
}
public function getSingle($entity, array $params = []) {
$this
->initialize();
$result = civicrm_api3($entity, 'getsingle', $params);
if (isset($result['is_error'])) {
return [];
}
return $result;
}
public function getValue($entity, array $params = []) {
$this
->initialize();
$result = civicrm_api3($entity, 'getvalue', $params);
return $result;
}
public function civicrmInitialize() {
$this->civicrm
->initialize();
}
public function getCustomFieldMetadata($field_name) {
$field_name = explode('_', $field_name, 2);
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)) {
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;
}
}
protected function initialize() {
if (!function_exists('civicrm_api3')) {
$this->civicrm
->initialize();
}
}
}