Commit 622bebf6 authored by jaapjansma's avatar jaapjansma
Browse files

Made output specification for get action more generic and reusable.

parent 088a2e16
......@@ -2,6 +2,7 @@ Version 1.23 (not yet released)
------------
* Added action to retrieve the Download file link
* Made output specification for get action more generic and reusable.
Version 1.22.2
--------------
......
......@@ -12,6 +12,7 @@ use Civi\ActionProvider\Parameter\Specification;
use Civi\ActionProvider\Parameter\SpecificationBag;
use Civi\ActionProvider\Utils\CustomField;
use Civi\ActionProvider\Utils\Fields;
use CRM_ActionProvider_ExtensionUtil as E;
class GetActivity extends AbstractAction {
......@@ -47,26 +48,7 @@ class GetActivity extends AbstractAction {
*/
public function getOutputSpecification() {
$bag = new SpecificationBag();
$customGroups = civicrm_api3('CustomGroup', 'get', [
'extends' => 'Activity',
'is_active' => 1,
'options' => ['limit' => 0],
]);
foreach ($customGroups['values'] as $customGroup) {
$customFields = civicrm_api3('CustomField', 'get', [
'custom_group_id' => $customGroup['id'],
'is_active' => 1,
'options' => ['limit' => 0],
]);
foreach ($customFields['values'] as $customField) {
$spec = CustomField::getSpecFromCustomField($customField, $customGroup['title'] . ': ', FALSE);
if ($spec) {
$bag->addSpecification($spec);
}
}
}
Fields::getFieldsForEntity($bag, 'Activity', 'get', array());
return $bag;
}
......@@ -101,4 +83,4 @@ class GetActivity extends AbstractAction {
}
\ No newline at end of file
}
......@@ -12,6 +12,8 @@ use Civi\ActionProvider\Parameter\Specification;
use Civi\ActionProvider\Parameter\SpecificationBag;
use Civi\ActionProvider\Utils\CustomField;
use Civi\ActionProvider\Utils\Fields;
use Civi\ActionProvider\Utils\Type;
use CRM_ActionProvider_ExtensionUtil as E;
class GetCaseDataById extends AbstractAction {
......@@ -89,66 +91,7 @@ class GetCaseDataById extends AbstractAction {
*/
public function getOutputSpecification() {
$bag = new SpecificationBag();
$fields = civicrm_api3('Case', 'getfields', array('api_action' => 'get'));
foreach($fields['values'] as $field) {
if (in_array($field['name'], $this->skippedFields)) {
continue;
}
if (stripos($field['name'], 'custom_') !== 0) {
$options = null;
try {
$option_api = civicrm_api3('Case', 'getoptions', ['field' => $field['name']]);
if (isset($option_api['values']) && is_array($option_api['values'])) {
$options = $option_api['values'];
}
} catch (\Exception $e) {
// Do nothing
}
$type = \CRM_Utils_Type::typeToString($field['type']);
switch ($type) {
case 'Int':
case 'ContactReference':
$type = 'Integer';
break;
case 'File':
$type = 'Integer';
break;
case 'Memo':
$type = 'Text';
break;
case 'Link':
$type = 'String';
break;
case 'StateProvince':
$type = 'String';
break;
}
$spec = new Specification($field['name'], $type, $field['title'], false, null, null, $options, false);
$bag->addSpecification($spec);
}
}
$customGroups = civicrm_api3('CustomGroup', 'get', [
'extends' => 'Case',
'is_active' => 1,
'options' => ['limit' => 0],
]);
foreach ($customGroups['values'] as $customGroup) {
$customFields = civicrm_api3('CustomField', 'get', [
'custom_group_id' => $customGroup['id'],
'is_active' => 1,
'options' => ['limit' => 0],
]);
foreach ($customFields['values'] as $customField) {
$spec = CustomField::getSpecFromCustomField($customField, $customGroup['title'] . ': ', FALSE);
if ($spec) {
$bag->addSpecification($spec);
}
}
}
Fields::getFieldsForEntity($bag, 'Case', 'get', $this->skippedFields);
return $bag;
}
......
......@@ -8,25 +8,27 @@ use \Civi\ActionProvider\Parameter\SpecificationBag;
use \Civi\ActionProvider\Parameter\Specification;
use \Civi\ActionProvider\Utils\CustomField;
use Civi\ActionProvider\Utils\Fields;
use Civi\ActionProvider\Utils\Type;
use CRM_ActionProvider_ExtensionUtil as E;
class ContactDataById extends AbstractAction {
/**
* Run the action
*
*
* @param ParameterInterface $parameters
* The parameters to this action.
* @param ParameterBagInterface $output
* The parameters this action can send back
* The parameters this action can send back
* @return void
*/
protected function doAction(ParameterBagInterface $parameters, ParameterBagInterface $output) {
$contact = civicrm_api3('Contact', 'getsingle', array('id' => $parameters->getParameter('contact_id')));
foreach($contact as $field => $value) {
$output->setParameter($field, $value);
}
}
// Get custom data
$custom_data = civicrm_api3('CustomValue', 'get', array('entity_id' => $parameters->getParameter('contact_id'), 'entity_table' => 'civicrm_contact'));
foreach($custom_data['values'] as $custom) {
......@@ -34,19 +36,19 @@ class ContactDataById extends AbstractAction {
$output->setParameter($fieldName, $custom['latest']);
}
}
/**
* Returns the specification of the configuration options for the actual action.
*
*
* @return SpecificationBag
*/
public function getConfigurationSpecification() {
public function getConfigurationSpecification() {
return new SpecificationBag(array());
}
/**
* Returns the specification of the parameters of the actual action.
*
*
* @return SpecificationBag
*/
public function getParameterSpecification() {
......@@ -54,54 +56,18 @@ class ContactDataById extends AbstractAction {
new Specification('contact_id', 'Integer', E::ts('Contact ID'), true)
));
}
/**
* Returns the specification of the output parameters of this action.
*
*
* This function could be overriden by child classes.
*
*
* @return SpecificationBag
*/
public function getOutputSpecification() {
$bag = new SpecificationBag();
$contact_fields = civicrm_api3('contact', 'getfields', array('action' => 'get', 'options' => array('limit' => 0)));
foreach($contact_fields['values'] as $field) {
if (empty($field['type'])) {
continue;
}
$type = \CRM_Utils_Type::typeToString($field['type']);
if (empty($type)) {
continue;
}
switch ($type) {
case 'Int':
$type = 'Integer';
break;
}
if (stripos($field['name'], 'custom_') === 0) {
// It is a custom field
$customFieldId = str_replace("custom_", "", $field['name']);
$fieldName = CustomField::getCustomFieldName($customFieldId);
$fieldSpec = new Specification(
$fieldName,
$type,
$field['title'],
false
);
$fieldSpec->setApiFieldName($field['name']);
} else {
$fieldSpec = new Specification(
$field['name'],
$type,
$field['title'],
false
);
}
$bag->addSpecification($fieldSpec);
}
Fields::getFieldsForEntity($bag, 'contact', 'get', array());
return $bag;
}
}
......@@ -10,17 +10,18 @@ use \Civi\ActionProvider\Parameter\Specification;
use \Civi\ActionProvider\Parameter\OptionGroupSpecification;
use \Civi\ActionProvider\Utils\CustomField;
use Civi\ActionProvider\Utils\Fields;
use CRM_ActionProvider_ExtensionUtil as E;
class GetAddress extends AbstractAction {
/**
* Run the action
*
*
* @param ParameterInterface $parameters
* The parameters to this action.
* @param ParameterBagInterface $output
* The parameters this action can send back
* The parameters this action can send back
* @return void
*/
protected function doAction(ParameterBagInterface $parameters, ParameterBagInterface $output) {
......@@ -32,15 +33,15 @@ class GetAddress extends AbstractAction {
foreach($existingAddress as $field => $value) {
$output->setParameter($field, $value);
}
} catch (\Exception $e) {
// Do nothing
}
}
/**
* Returns the specification of the configuration options for the actual action.
*
*
* @return SpecificationBag
*/
public function getConfigurationSpecification() {
......@@ -51,10 +52,10 @@ class GetAddress extends AbstractAction {
new Specification('location_type_id', 'Integer', E::ts('Location type'), true, $defaultLocationType, null, $locationTypes, FALSE)
));
}
/**
* Returns the specification of the parameters of the actual action.
*
*
* @return SpecificationBag
*/
public function getParameterSpecification() {
......@@ -62,54 +63,18 @@ class GetAddress extends AbstractAction {
new Specification('contact_id', 'Integer', E::ts('Contact ID'), true),
));
}
/**
* Returns the specification of the output parameters of this action.
*
*
* This function could be overriden by child classes.
*
*
* @return SpecificationBag
*/
public function getOutputSpecification() {
$bag = new SpecificationBag();
$contact_fields = civicrm_api3('address', 'getfields', array('action' => 'get', 'options' => array('limit' => 0)));
foreach($contact_fields['values'] as $field) {
if (empty($field['type'])) {
continue;
}
$type = \CRM_Utils_Type::typeToString($field['type']);
if (empty($type)) {
continue;
}
switch ($type) {
case 'Int':
$type = 'Integer';
break;
}
if (stripos($field['name'], 'custom_') === 0) {
// It is a custom field
$customFieldId = str_replace("custom_", "", $field['name']);
$fieldName = CustomField::getCustomFieldName($customFieldId);
$fieldSpec = new Specification(
$fieldName,
$type,
$field['title'],
false
);
$fieldSpec->setApiFieldName($field['name']);
} else {
$fieldSpec = new Specification(
$field['name'],
$type,
$field['title'],
false
);
}
$bag->addSpecification($fieldSpec);
}
Fields::getFieldsForEntity($bag,'Address', 'get', array());
return $bag;
}
}
\ No newline at end of file
}
......@@ -10,17 +10,18 @@ use \Civi\ActionProvider\Parameter\Specification;
use \Civi\ActionProvider\Parameter\OptionGroupSpecification;
use \Civi\ActionProvider\Utils\CustomField;
use Civi\ActionProvider\Utils\Fields;
use CRM_ActionProvider_ExtensionUtil as E;
class GetAddressById extends AbstractAction {
/**
* Run the action
*
*
* @param ParameterInterface $parameters
* The parameters to this action.
* @param ParameterBagInterface $output
* The parameters this action can send back
* The parameters this action can send back
* @return void
*/
protected function doAction(ParameterBagInterface $parameters, ParameterBagInterface $output) {
......@@ -31,24 +32,24 @@ class GetAddressById extends AbstractAction {
foreach($existingAddress as $field => $value) {
$output->setParameter($field, $value);
}
} catch (\Exception $e) {
// Do nothing
}
}
/**
* Returns the specification of the configuration options for the actual action.
*
*
* @return SpecificationBag
*/
public function getConfigurationSpecification() {
return new SpecificationBag(array());
}
/**
* Returns the specification of the parameters of the actual action.
*
*
* @return SpecificationBag
*/
public function getParameterSpecification() {
......@@ -56,54 +57,18 @@ class GetAddressById extends AbstractAction {
new Specification('address_id', 'Integer', E::ts('Address ID'), true),
));
}
/**
* Returns the specification of the output parameters of this action.
*
*
* This function could be overriden by child classes.
*
*
* @return SpecificationBag
*/
public function getOutputSpecification() {
$bag = new SpecificationBag();
$contact_fields = civicrm_api3('address', 'getfields', array('action' => 'get', 'options' => array('limit' => 0)));
foreach($contact_fields['values'] as $field) {
if (empty($field['type'])) {
continue;
}
$type = \CRM_Utils_Type::typeToString($field['type']);
if (empty($type)) {
continue;
}
switch ($type) {
case 'Int':
$type = 'Integer';
break;
}
if (stripos($field['name'], 'custom_') === 0) {
// It is a custom field
$customFieldId = str_replace("custom_", "", $field['name']);
$fieldName = CustomField::getCustomFieldName($customFieldId);
$fieldSpec = new Specification(
$fieldName,
$type,
$field['title'],
false
);
$fieldSpec->setApiFieldName($field['name']);
} else {
$fieldSpec = new Specification(
$field['name'],
$type,
$field['title'],
false
);
}
$bag->addSpecification($fieldSpec);
}
Fields::getFieldsForEntity($bag,'Address', 'get', array());
return $bag;
}
}
\ No newline at end of file
}
......@@ -10,17 +10,18 @@ use \Civi\ActionProvider\Parameter\Specification;
use \Civi\ActionProvider\Parameter\OptionGroupSpecification;
use \Civi\ActionProvider\Utils\CustomField;
use Civi\ActionProvider\Utils\Fields;
use CRM_ActionProvider_ExtensionUtil as E;
class GetEmailAddress extends AbstractAction {
/**
* Run the action
*
*
* @param ParameterInterface $parameters
* The parameters to this action.
* @param ParameterBagInterface $output
* The parameters this action can send back
* The parameters this action can send back
* @return void
*/
protected function doAction(ParameterBagInterface $parameters, ParameterBagInterface $output) {
......@@ -32,15 +33,15 @@ class GetEmailAddress extends AbstractAction {
foreach($existingEmail as $field => $value) {
$output->setParameter($field, $value);
}
} catch (\Exception $e) {
// Do nothing
}
}
/**
* Returns the specification of the configuration options for the actual action.
*
*
* @return SpecificationBag
*/
public function getConfigurationSpecification() {
......@@ -51,10 +52,10 @@ class GetEmailAddress extends AbstractAction {
new Specification('location_type_id', 'Integer', E::ts('Location type'), true, $defaultLocationType, null, $locationTypes, FALSE)
));
}
/**
* Returns the specification of the parameters of the actual action.
*
*
* @return SpecificationBag
*/
public function getParameterSpecification() {
......@@ -62,54 +63,18 @@ class GetEmailAddress extends AbstractAction {
new Specification('contact_id', 'Integer', E::ts('Contact ID'), true),
));
}
/**
* Returns the specification of the output parameters of this action.
*
*
* This function could be overriden by child classes.
*
*
* @return SpecificationBag
*/
public function getOutputSpecification() {
$bag = new SpecificationBag();
$contact_fields = civicrm_api3('Email', 'getfields', array('action' => 'get', 'options' => array('limit' => 0)));
foreach($contact_fields['values'] as $field) {
if (empty($field['type'])) {
continue;
}
$type = \CRM_Utils_Type::typeToString($field['type']);
if (empty($type)) {
continue;
}
switch ($type) {
case 'Int':
$type = 'Integer';
break;
}
if (stripos($field['name'], 'custom_') === 0) {
// It is a custom field
$customFieldId = str_replace("custom_", "", $field['name']);
$fieldName = CustomField::getCustomFieldName($customFieldId);
$fieldSpec = new Specification(
$fieldName,
$type,
$field['title'],
false
);
$fieldSpec->setApiFieldName($field['name']);
} else {
$fieldSpec = new Specification(
$field['name'],
$type,
$field['title'],
false
);
}
$bag->addSpecification($fieldSpec);
}
Fields::getFieldsForEntity($bag,'Email', 'get', array());
return $bag;
}
}
\ No newline at end of file
}
......@@ -9,26 +9,27 @@ use \Civi\ActionProvider\Parameter\Specification;
use \Civi\ActionProvider\Parameter\OptionGroupSpecification;
use \Civi\ActionProvider\Utils\CustomField;
use Civi\ActionProvider\Utils\Fields;
use Civi\DataProcessor\DataSpecification\CustomFieldSpecification;
use CRM_ActionProvider_ExtensionUtil as E;
class GetEvent extends AbstractAction {
/**
* Returns the specification of the configuration options for the actual action.
*
*
* @return SpecificationBag
*/
public function getConfigurationSpecification() {
return new SpecificationBag(array());