Commit 887bcb1b authored by jaapjansma's avatar jaapjansma

Added actions for create update household and create update membership.

parent d243a9ba
<?php
namespace Civi\ActionProvider\Action\Contact;
use \Civi\ActionProvider\Action\AbstractAction;
use Civi\ActionProvider\Parameter\OptionGroupSpecification;
use \Civi\ActionProvider\Parameter\ParameterBagInterface;
use \Civi\ActionProvider\Parameter\SpecificationBag;
use \Civi\ActionProvider\Parameter\Specification;
use CRM_ActionProvider_ExtensionUtil as E;
class CreateUpdateHousehold 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) {
// Create contact
if ($parameters->getParameter('contact_id')) {
$params['id'] = $parameters->getParameter('contact_id');
}
$contact_sub_type = $this->configuration->getParameter('contact_sub_type');
$params['contact_type'] = "Household";
if ($contact_sub_type) {
$params['contact_sub_type'] = $contact_sub_type;
}
$params['household_name'] = $parameters->getParameter('household_name');
$result = civicrm_api3('Contact', 'create', $params);
$contact_id = $result['id'];
$output->setParameter('contact_id', $contact_id);
// Create address
$address_id = ContactActionUtils::createAddress($contact_id, $parameters, $this->configuration);
if ($address_id) {
$output->setParameter('address_id', $address_id);
}
// Create email
$email_id = ContactActionUtils::createEmail($contact_id, $parameters, $this->configuration);
if ($email_id) {
$output->setParameter('email_id', $email_id);
}
// Create phone
$phone_id = ContactActionUtils::createPhone($contact_id, $parameters, $this->configuration);
if ($phone_id) {
$output->setParameter('phone_id', $phone_id);
}
}
/**
* Returns the specification of the configuration options for the actual action.
*
* @return SpecificationBag
*/
public function getConfigurationSpecification() {
$contactSubTypes = array();
$contactSubTypesApi = civicrm_api3('ContactType', 'get', array('parent_id' => 'Household', 'options' => array('limit' => 0)));
$contactSubTypes[''] = E::ts(' - Select - ');
foreach($contactSubTypesApi['values'] as $contactSubType) {
$contactSubTypes[$contactSubType['name']] = $contactSubType['label'];
}
$spec = new SpecificationBag(array(
new Specification('contact_sub_type', 'String', E::ts('Contact sub type'), false, null, null, $contactSubTypes, FALSE),
));
ContactActionUtils::createAddressConfigurationSpecification($spec);
ContactActionUtils::createEmailConfigurationSpecification($spec);
ContactActionUtils::createPhoneConfigurationSpecification($spec);
return $spec;
}
/**
* Returns the specification of the parameters of the actual action.
*
* @return SpecificationBag
*/
public function getParameterSpecification() {
$contactIdSpec = new Specification('contact_id', 'Integer', E::ts('Contact ID'), false);
$contactIdSpec->setDescription(E::ts('Leave empty to create a new Individual'));
$spec = new SpecificationBag(array(
$contactIdSpec,
new Specification('household_name', 'String', E::ts('Household name'), false),
));
ContactActionUtils::createAddressParameterSpecification($spec);
ContactActionUtils::createEmailParameterSpecification($spec);
ContactActionUtils::createPhoneParameterSpecification($spec);
return $spec;
}
/**
* Returns the specification of the output parameters of this action.
*
* This function could be overriden by child classes.
*
* @return SpecificationBag
*/
public function getOutputSpecification() {
return new SpecificationBag(array(
new Specification('contact_id', 'Integer', E::ts('Contact ID'), false),
new Specification('address_id', 'Integer', E::ts('Address record ID'), false),
new Specification('email_id', 'Integer', E::ts('Email record ID'), false),
new Specification('phone_id', 'Integer', E::ts('Phone ID'), false),
));
}
/**
* Returns the human readable title of this action
*/
public function getTitle() {
return E::ts('Create or update Household');
}
/**
* Returns the tags for this action.
*/
public function getTags() {
return array(
AbstractAction::SINGLE_CONTACT_ACTION_TAG,
AbstractAction::DATA_MANIPULATION_TAG,
);
}
}
<?php
namespace Civi\ActionProvider\Action\Membership;
use \Civi\ActionProvider\Action\AbstractAction;
use \Civi\ActionProvider\Parameter\ParameterBagInterface;
use \Civi\ActionProvider\Parameter\SpecificationBag;
use \Civi\ActionProvider\Parameter\Specification;
use \Civi\ActionProvider\Action\Membership\Parameter\MembershipTypeSpecification;
use \Civi\ActionProvider\Utils\CustomField;
use CRM_ActionProvider_ExtensionUtil as E;
class CreateOrUpdateMembership extends AbstractAction {
/**
* Returns the human readable title of this action
*/
public function getTitle() {
return E::ts('Create or update an membership');
}
/**
* Returns the specification of the configuration options for the actual action.
*
* @return SpecificationBag
*/
public function getConfigurationSpecification() {
return new SpecificationBag(array(
new MembershipTypeSpecification('membership_type', E::ts('Membership Type'), TRUE),
));
}
/**
* Returns the specification of the configuration options for the actual action.
*
* @return SpecificationBag
*/
public function getParameterSpecification() {
$specs = new SpecificationBag(array(
/**
* The parameters given to the Specification object are:
* @param string $name
* @param string $dataType
* @param string $title
* @param bool $required
* @param mixed $defaultValue
* @param string|null $fkEntity
* @param array $options
* @param bool $multiple
*/
new Specification('membership_id', 'Integer', E::ts('Membership ID'), false, null, null, null, FALSE),
new Specification('contact_id', 'Integer', E::ts('Contact ID'), true, null, null, null, FALSE),
));
$customGroups = civicrm_api3('CustomGroup', 'get', array('extends' => 'Membership', 'is_active' => 1, 'options' => array('limit' => 0)));
foreach($customGroups['values'] as $customGroup) {
$customFields = civicrm_api3('CustomField', 'get', array('custom_group_id' => $customGroup['id'], 'is_active' => 1, 'options' => array('limit' => 0)));
foreach($customFields['values'] as $customField) {
$spec = CustomField::getSpecFromCustomField($customField, $customGroup['title'].': ', false);
if ($spec) {
$specs->addSpecification($spec);
}
}
}
return $specs;
}
/**
* Returns the specification of the output parameters of this action.
*
* This function could be overriden by child classes.
*
* @return SpecificationBag
*/
public function getOutputSpecification() {
return new SpecificationBag(array(
new Specification('id', 'Integer', E::ts('Membership ID')),
));
}
/**
* 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) {
// Get the contact and the event.
$apiParams = array();
if ($parameters->doesParameterExists('membership_id')) {
$apiParams['id'] = $parameters->getParameter('membership_id');
}
$apiParams['contact_id'] = $parameters->getParameter('contact_id');
$apiParams['membership_type_id'] = $this->configuration->getParameter('membership_type');
foreach($this->getParameterSpecification() as $spec) {
if (stripos($spec->getName(), 'custom_')!==0) {
continue;
}
if ($parameters->doesParameterExists($spec->getName())) {
$apiParams[$spec->getApiFieldName()] = $parameters->getParameter($spec->getName());
}
}
// Create or Update the event through an API call.
try {
$result = civicrm_api3('Membership', 'create', $apiParams);
$output->setParameter('id', $result['id']);
} catch (Exception $e) {
throw new \Civi\ActionProvider\Exception\ExecutionException(E::ts('Could not update or create an membership.'));
}
}
/**
* Returns the tags for this action.
*/
public function getTags() {
return array(
AbstractAction::DATA_MANIPULATION_TAG,
);
}
}
\ No newline at end of file
<?php
namespace Civi\ActionProvider\Action\Membership\Parameter;
use CRM_ActionProvider_ExtensionUtil as E;
use \Civi\ActionProvider\Parameter\Specification;
class MembershipTypeSpecification extends Specification {
/**
* @param string $name
* @param string $dataType
* @param string $title
* @param bool $required
* @param mixed $defaultValue
* @param string|null $fkEntity
* @param array $options
* @param bool $multiple
*/
public function __construct($name, $title='', $required = false, $defaultValue = null, $multiple = false) {
$this->setName($name);
$this->setDataType('String');
$this->setTitle($title);
$this->setRequired($required);
$this->setDefaultValue($defaultValue);
$this->setMultiple($multiple);
}
/**
* @return array
*/
public function getOptions() {
$options = array();
$optionsApi = civicrm_api3('MembershipType', 'get', array('is_active' => 1, 'options' => array('limit' => 0)));
foreach($optionsApi['values'] as $optionValue) {
$options[$optionValue['id']] = $optionValue['name'];
}
return $options;
}
/**
* @param array $options
*
* @return $this
*/
public function setOptions($options) {
// Do nothing
return $this;
}
/**
* @param $option
*/
public function addOption($option) {
// Do nothing.
}
}
......@@ -53,6 +53,7 @@ class Provider {
new \Civi\ActionProvider\Action\Contact\GetAddress(),
new \Civi\ActionProvider\Action\Contact\GetContactIdFromMasterAddress(),
new \Civi\ActionProvider\Action\Contact\CreateUpdateIndividual(),
new \Civi\ActionProvider\Action\Contact\CreateUpdateHousehold(),
new \Civi\ActionProvider\Action\Contact\UpdateCustomData(),
new \Civi\ActionProvider\Action\Contact\FindOrCreateContactByEmail(),
new \Civi\ActionProvider\Action\Activity\CreateActivity(),
......@@ -71,6 +72,7 @@ class Provider {
new \Civi\ActionProvider\Action\Website\GetWebsite(),
new \Civi\ActionProvider\Action\Phone\CreateUpdatePhone(),
new \Civi\ActionProvider\Action\Phone\GetPhone(),
new \Civi\ActionProvider\Action\Membership\CreateOrUpdateMembership(),
);
$conditions = array(
......
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