Commit bd6cf679 authored by jaapjansma's avatar jaapjansma

updated action provider

parent 2e13e2a8
......@@ -22,7 +22,7 @@ class ContactActionUtils {
/**
* Create an address for a contact.
*/
public static function createAddress($contact_id, ParameterBagInterface $parameters, ParameterBagInterface $configuration) {
public static function createAddressForContact($contact_id, ParameterBagInterface $parameters, ParameterBagInterface $configuration) {
$existingAddressId = false;
if ($configuration->getParameter('address_update_existing')) {
// Try to find existing address
......@@ -35,16 +35,35 @@ class ContactActionUtils {
// Do nothing
}
}
return self::createAddress($existingAddressId, $contact_id, $parameters, $configuration);
}
/**
* Create an address
* @param $existingAddressId
* @param int|null $contact_id
* @param \Civi\ActionProvider\Parameter\ParameterBagInterface $parameters
* @param \Civi\ActionProvider\Parameter\ParameterBagInterface $configuration
*
* @return bool
* @throws \CiviCRM_API3_Exception
*/
public static function createAddress($existingAddressId, $contact_id, ParameterBagInterface $parameters, ParameterBagInterface $configuration) {
// Create address
$hasAddressParams = false;
$addressParams = array();
if ($existingAddressId) {
$addressParams['id'] = $existingAddressId;
}
$addressParams['contact_id'] = $contact_id;
if ($contact_id) {
$addressParams['contact_id'] = $contact_id;
} else {
$addressParams['contact_id'] = 'null';
}
$addressParams['master_id'] = 'null';
$addressParams['location_type_id'] = $configuration->getParameter('address_location_type');
if ($configuration->doesParameterExists('address_location_type')) {
$addressParams['location_type_id'] = $configuration->getParameter('address_location_type');
}
if ($parameters->getParameter('name')) {
$addressParams['name'] = $parameters->getParameter('name');
$hasAddressParams = true;
......@@ -72,8 +91,8 @@ class ContactActionUtils {
if ($hasAddressParams) {
$result = civicrm_api3('Address', 'create', $addressParams);
return $result['id'];
}
}
return false;
}
......
......@@ -25,7 +25,7 @@ class CreateUpdateAddress extends AbstractAction {
protected function doAction(ParameterBagInterface $parameters, ParameterBagInterface $output) {
$contact_id = $parameters->getParameter('contact_id');
$id = ContactActionUtils::createAddress($contact_id, $parameters, $this->configuration);
$id = ContactActionUtils::createAddressForContact($contact_id, $parameters, $this->configuration);
$output->setParameter('id', $id);
}
......
......@@ -37,7 +37,7 @@ class CreateUpdateHousehold extends AbstractAction {
$output->setParameter('contact_id', $contact_id);
// Create address
$address_id = ContactActionUtils::createAddress($contact_id, $parameters, $this->configuration);
$address_id = ContactActionUtils::createAddressForContact($contact_id, $parameters, $this->configuration);
if ($address_id) {
$output->setParameter('address_id', $address_id);
}
......
......@@ -47,7 +47,7 @@ class CreateUpdateIndividual extends AbstractAction {
$output->setParameter('contact_id', $contact_id);
// Create address
$address_id = ContactActionUtils::createAddress($contact_id, $parameters, $this->configuration);
$address_id = ContactActionUtils::createAddressForContact($contact_id, $parameters, $this->configuration);
if ($address_id) {
$output->setParameter('address_id', $address_id);
}
......
<?php
namespace Civi\ActionProvider\Action\Contact;
use \Civi\ActionProvider\Action\AbstractAction;
use \Civi\ActionProvider\Action\Contact\ContactActionUtils;
use \Civi\ActionProvider\Parameter\ParameterBagInterface;
use \Civi\ActionProvider\Parameter\SpecificationBag;
use \Civi\ActionProvider\Parameter\Specification;
use \Civi\ActionProvider\Parameter\OptionGroupSpecification;
use \Civi\ActionProvider\Utils\CustomField;
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
* @return void
*/
protected function doAction(ParameterBagInterface $parameters, ParameterBagInterface $output) {
$id = $parameters->getParameter('address_id');
$existingAddressParams['id'] = $id;
try {
$existingAddress = civicrm_api3('Address', 'getsingle', $existingAddressParams);
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() {
return new SpecificationBag(array(
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);
}
return $bag;
}
/**
* Returns the human readable title of this action
*/
public function getTitle() {
return E::ts('Get address by ID');
}
/**
* Returns the tags for this action.
*/
public function getTags() {
return array(
AbstractAction::SINGLE_CONTACT_ACTION_TAG,
AbstractAction::DATA_RETRIEVAL_TAG,
);
}
}
\ No newline at end of file
......@@ -3,6 +3,7 @@
namespace Civi\ActionProvider\Action\Event;
use \Civi\ActionProvider\Action\AbstractAction;
use Civi\ActionProvider\Action\Contact\ContactActionUtils;
use \Civi\ActionProvider\Parameter\ParameterBagInterface;
use \Civi\ActionProvider\Parameter\SpecificationBag;
use \Civi\ActionProvider\Parameter\Specification;
......@@ -10,6 +11,7 @@ use \Civi\ActionProvider\Parameter\OptionGroupSpecification;
use \Civi\ActionProvider\Utils\CustomField;
use CRM_ActionProvider_ExtensionUtil as E;
use Dompdf\Exception;
class CreateOrUpdateEvent extends AbstractAction {
......@@ -26,7 +28,13 @@ class CreateOrUpdateEvent extends AbstractAction {
* @return SpecificationBag
*/
public function getConfigurationSpecification() {
return new SpecificationBag(array());
$locationTypes = ContactActionUtils::getLocationTypes();
reset($locationTypes);
$defaultLocationType = key($locationTypes);
return new SpecificationBag(array(
new Specification('add_address', 'Boolean', E::ts('Add address'), true, false),
new Specification('address_location_type', 'Integer', E::ts('Address: Location type'), false, $defaultLocationType, null, $locationTypes, FALSE),
));
}
/**
......@@ -65,6 +73,8 @@ class CreateOrUpdateEvent extends AbstractAction {
}
}
}
ContactActionUtils::createAddressParameterSpecification($specs);
return $specs;
}
......@@ -92,6 +102,29 @@ class CreateOrUpdateEvent extends AbstractAction {
* @return void
*/
protected function doAction(ParameterBagInterface $parameters, ParameterBagInterface $output) {
$existingAddressId = false;
$locBlockId = false;
if ($this->configuration->getParameter('add_address')) {
if ($parameters->doesParameterExists('event_id')) {
try {
$event = civicrm_api3('Event', 'getsingle', ['id' => $parameters->getParameter('event_id')]);
$locationInUseByOtherEvents = civicrm_api3('Event', 'getcount', array('loc_block_id' => $event['loc_block_id']));
if ($locationInUseByOtherEvents == 1) {
$loc = civicrm_api3('LocBlock', 'getsingle', ['id' => $event['loc_block_id']]);
$locBlockId = $loc['id'];
$existingAddressId = $loc['address_id'];
}
} catch (\Exception $e) {
// Do nothing
}
}
$address_id = ContactActionUtils::createAddress($existingAddressId, null, $parameters, $this->configuration);
if (!$locBlockId) {
$result = civicrm_api3('LocBlock', 'create', array('address_id' => $address_id));
$locBlockId = $result['id'];
}
}
// Get the contact and the event.
$apiParams = array();
if ($parameters->doesParameterExists('event_id')) {
......@@ -106,6 +139,10 @@ class CreateOrUpdateEvent extends AbstractAction {
$apiParams['end_date'] = $parameters->getParameter('end_date');
}
$apiParams['event_type_id'] = $parameters->getParameter('event_type');
if ($locBlockId) {
$apiParams['loc_block_id'] = $locBlockId;
$apiParams['is_show_location'] = '1';
}
foreach($this->getParameterSpecification() as $spec) {
if (stripos($spec->getName(), 'custom_')!==0) {
......@@ -119,8 +156,6 @@ class CreateOrUpdateEvent extends AbstractAction {
try {
$result = civicrm_api3('Event', 'create', $apiParams);
$output->setParameter('id', $result['id']);
$output->setParameter('dump', var_export($apiParams, true));
$output->setParameter('dump2', var_export($parameters, true));
} catch (Exception $e) {
throw new \Civi\ActionProvider\Exception\ExecutionException(E::ts('Could not update or create an event.'));
}
......
......@@ -107,6 +107,8 @@ class GetEvent extends AbstractAction {
}
}
$bag->addSpecification(new Specification('address_id', 'Integer', E::ts('Address ID')));
return $bag;
}
......@@ -129,6 +131,7 @@ class GetEvent extends AbstractAction {
try {
$event = civicrm_api3('Event', 'getsingle', array('id' => $event_id));
foreach($this->getOutputSpecification() as $spec) {
$fieldName = $spec->getName();
if (stripos($fieldName, 'custom_') === 0) {
......@@ -141,6 +144,18 @@ class GetEvent extends AbstractAction {
$output->setParameter($spec->getName(), $event[$fieldName]);
}
}
// Get Address ID
try {
$event = civicrm_api3('Event', 'getsingle', ['id' => $parameters->getParameter('event_id')]);
$locationInUseByOtherEvents = civicrm_api3('Event', 'getcount', array('loc_block_id' => $event['loc_block_id']));
if ($locationInUseByOtherEvents == 1) {
$loc = civicrm_api3('LocBlock', 'getsingle', ['id' => $event['loc_block_id']]);
$output->setParameter('address_id', $loc['address_id']);
}
} catch (\Exception $e) {
// Do nothing
}
} catch (\Exception $e) {
// Do nothing
}
......
......@@ -51,6 +51,7 @@ class Provider {
new \Civi\ActionProvider\Action\Contact\CreateUpdateAddress(),
new \Civi\ActionProvider\Action\Contact\UsePrimaryAddressOfContact(),
new \Civi\ActionProvider\Action\Contact\GetAddress(),
new \Civi\ActionProvider\Action\Contact\GetAddressById(),
new \Civi\ActionProvider\Action\Contact\GetContactIdFromMasterAddress(),
new \Civi\ActionProvider\Action\Contact\CreateUpdateIndividual(),
new \Civi\ActionProvider\Action\Contact\CreateUpdateHousehold(),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment