Commit 5b80509c authored by jaapjansma's avatar jaapjansma
Browse files

update

parent 8bf8d209
Version 1.6
-----------
* Update related membership when create relationship or create/update relationship action is performed.
* Added action Get Contact By Custom Field.
Version 1.5
-----------
------------------------------
* Added action Get Relationship By Contact ID
* Added action Get State/Province ID by name
* Fixed issue with the Get Relationship action
Version 1.4
......
......@@ -14,16 +14,16 @@ use CRM_ActionProvider_ExtensionUtil as E;
* - create/update email
* - create/update address
* - create/update phone
*/
*/
class ContactActionUtils {
private static $locationTypes = false;
/**
* Create an address for a contact.
*/
public static function createAddressForContact($contact_id, ParameterBagInterface $parameters, ParameterBagInterface $configuration) {
$existingAddressId = false;
$existingAddressId = false;
if ($configuration->getParameter('address_update_existing')) {
$existingAddressId = self::findExistingAddress($contact_id, $configuration->getParameter('address_location_type'), $configuration->getParameter('address_is_primary'));
}
......@@ -127,8 +127,8 @@ class ContactActionUtils {
$addressParams['city'] = $parameters->getParameter('city');
$hasAddressParams = true;
}
if ($parameters->getParameter('state')) {
$addressParams['state_province_id'] = $parameters->getParameter('state');
if ($parameters->getParameter('state_province_id')) {
$addressParams['state_province_id'] = $parameters->getParameter('state_province_id');
$hasAddressParams = true;
}
if ($parameters->getParameter('country_id')) {
......@@ -142,7 +142,7 @@ class ContactActionUtils {
return false;
}
/**
* Update the configuration specification for create address.
*/
......@@ -155,7 +155,7 @@ class ContactActionUtils {
$spec->addSpecification(new Specification('address_update_existing', 'Boolean', E::ts('Address: update existing'), false, 0, null, null, FALSE));
}
/**
* Update the parameter specification for create address.
*/
......@@ -171,17 +171,17 @@ class ContactActionUtils {
$spec->addSpecification(new Specification('state_province_id', 'Integer', E::ts('State/Province ID'), false, null, 'StateProvince'));
$spec->addSpecification(new Specification('country_id', 'Integer', E::ts('Country ID'), false, null, 'Country'));
}
/**
* Create a phone for a contact.
*/
public static function createPhone($contact_id, ParameterBagInterface $parameters, ParameterBagInterface $configuration) {
$existingPhoneId = false;
$existingPhoneId = false;
if ($configuration->getParameter('phone_update_existing')) {
$existingPhoneId = self::findExistingPhone($contact_id, $configuration->getParameter('phone_location_type'), $configuration->getParameter('phone_is_primary'));
}
// Create phone
if ($parameters->getParameter('phone')) {
$phoneParams = array();
......@@ -235,7 +235,7 @@ class ContactActionUtils {
}
return false;
}
/**
* Update the configuration specification for create phone.
*/
......@@ -247,24 +247,24 @@ class ContactActionUtils {
$spec->addSpecification(new Specification('phone_location_type', 'Integer', E::ts('Phone: Location type'), true, $defaultLocationType, null, $locationTypes, FALSE));
$spec->addSpecification(new Specification('phone_update_existing', 'Boolean', E::ts('Phone: update existing'), false, 0, null, null, FALSE));
}
/**
* Update the parameter specification for create phone.
*/
public static function createPhoneParameterSpecification(SpecificationBag $spec) {
$spec->addSpecification(new Specification('phone', 'String', E::ts('Phonenumber'), false));
}
/**
* Create an e-mail address for a contact.
*/
public static function createEmail($contact_id, ParameterBagInterface $parameters, ParameterBagInterface $configuration) {
$existingEmailId = false;
$existingEmailId = false;
if ($configuration->getParameter('email_update_existing')) {
$existingEmailId = self::findExistingEmail($contact_id, $configuration->getParameter('email_location_type'), $configuration->getParameter('email_is_primary'));
}
// Create email
if ($parameters->getParameter('email')) {
$emailParams = array();
......@@ -318,7 +318,7 @@ class ContactActionUtils {
}
return false;
}
/**
* Update the configuration specification for create email.
*/
......@@ -330,14 +330,14 @@ class ContactActionUtils {
$spec->addSpecification(new Specification('email_location_type', 'Integer', E::ts('E-mail: Location type'), true, $defaultLocationType, null, $locationTypes, FALSE));
$spec->addSpecification(new Specification('email_update_existing', 'Boolean', E::ts('E-mail: update existing'), false, 0, null, null, FALSE));
}
/**
* Update the parameter specification for create email.
*/
public static function createEmailParameterSpecification(SpecificationBag $spec) {
$spec->addSpecification(new Specification('email', 'String', E::ts('E-mail'), false));
}
/**
* Returns the location types
*/
......@@ -351,5 +351,5 @@ class ContactActionUtils {
}
return self::$locationTypes;
}
}
<?php
namespace Civi\ActionProvider\Action\Contact;
use \Civi\ActionProvider\Action\AbstractAction;
use Civi\ActionProvider\Exception\InvalidParameterException;
use \Civi\ActionProvider\Parameter\ParameterBagInterface;
use \Civi\ActionProvider\Parameter\SpecificationBag;
use \Civi\ActionProvider\Parameter\Specification;
use Civi\ActionProvider\Utils\CustomField;
use CRM_ActionProvider_ExtensionUtil as E;
class FindByCustomField 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) {
$contact_type = civicrm_api3('ContactType', 'getsingle', array('id' => $this->configuration->getParameter('contact_type')));
$contact_sub_type = false;
if (isset($contact_type['parent_id']) && $contact_type['parent_id'] > 0) {
$contact_sub_type = $contact_type;
$contact_type = civicrm_api3('ContactType', 'getsingle', array('id' => $contact_sub_type['parent_id']));
}
$apiParams = array();
foreach($this->getParameterSpecification() as $spec) {
if ($parameters->doesParameterExists($spec->getName())) {
$apiParams[$spec->getApiFieldName()] = $parameters->getParameter($spec->getName());
}
}
if (!count($apiParams)) {
throw new InvalidParameterException(E::ts("No parameter given"));
}
$apiParams['contact_type'] = $contact_type['name'];
if ($contact_sub_type) {
$apiParams['contact_sub_type'] = $contact_sub_type['name'];
}
$apiParams['return'] = 'id';
$contact_id = civicrm_api3('Contact', 'getvalue', $apiParams);
$output->setParameter('contact_id', $contact_id);
}
/**
* Returns the specification of the configuration options for the actual action.
*
* @return SpecificationBag
*/
public function getConfigurationSpecification() {
return new SpecificationBag(array(
new Specification('contact_type', 'Integer', E::ts('Contact type'), true, null, 'ContactType', null, FALSE),
));
}
/**
* Returns the specification of the parameters of the actual action.
*
* @return SpecificationBag
*/
public function getParameterSpecification() {
$specs = new SpecificationBag();
$customGroups = civicrm_api3('CustomGroup', 'get', array('is_active' => 1, 'is_multiple' => 0, 'options' => array('limit' => 0)));
foreach($customGroups['values'] as $customGroup) {
if (!in_array($customGroup['extends'], array('Individual', 'Household', 'Organization', 'Contact'))) {
continue;
}
$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('contact_id', 'Integer', E::ts('Contact ID'), true)
));
}
}
<?php
namespace Civi\ActionProvider\Action\Contact;
use \Civi\ActionProvider\Action\AbstractAction;
use \Civi\ActionProvider\Parameter\ParameterBagInterface;
use \Civi\ActionProvider\Parameter\SpecificationBag;
use \Civi\ActionProvider\Parameter\Specification;
use \Civi\ActionProvider\Utils\CustomField;
use CRM_ActionProvider_ExtensionUtil as E;
class GetStateProvinceId extends AbstractAction {
/**
* Run the action
*
* @param ParameterBagInterface $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) {
if ($this->configuration->doesParameterExists('use_abbreviation') && $this->configuration->getParameter('use_abbreviation')) {
$sql = "SELECT id FROM `civicrm_state_province` WHERE `country_id` = %1 AND `abbreviation` = %2";
$params[1] = [$parameters->getParameter('country_id'), 'Integer'];
$params[2] = [$parameters->getParameter('state_province'), 'String'];
} else {
$sql = "SELECT id FROM `civicrm_state_province` WHERE `country_id` = %1 AND `name` = %2";
$params[1] = [$parameters->getParameter('country_id'), 'Integer'];
$params[2] = [$parameters->getParameter('state_province'), 'String'];
}
$state_id = \CRM_Core_DAO::singleValueQuery($sql, $params);
$output->setParameter('state_province_id', $state_id);
}
/**
* Returns the specification of the configuration options for the actual action.
*
* @return SpecificationBag
*/
public function getConfigurationSpecification() {
return new SpecificationBag(array(
new Specification('use_abbreviation', 'Boolean', E::ts('Use abbreviation'), true, false),
));
}
/**
* Returns the specification of the parameters of the actual action.
*
* @return SpecificationBag
*/
public function getParameterSpecification() {
return new SpecificationBag(array(
new Specification('country_id', 'Integer', E::ts('Country ID'), true),
new Specification('state_province', 'String', E::ts('State/Province name'), 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([
new Specification('state_province_id', 'Integer', E::ts('State/Province ID'), true),
]);
return $bag;
}
}
......@@ -60,6 +60,10 @@ class UpdateCustomData extends AbstractAction {
$customFields = civicrm_api3('CustomField', 'get', array('custom_group_id' => $customGroup['id'], 'is_active' => 1, 'options' => array('limit' => 0)));
foreach($customFields['values'] as $customField) {
if (isset($customField['is_view']) && $customField['is_view']) {
continue;
}
$spec = CustomField::getSpecFromCustomField($customField, $customGroup['title'].': ', false);
if ($spec) {
$specs->addSpecification($spec);
......
......@@ -116,7 +116,7 @@ class CreateOrUpdateRelationship extends CreateRelationship {
try {
// Do not use api as the api checks for an existing relationship.
$relationship = \CRM_Contact_BAO_Relationship::create($relationshipParams);
$relationship = \CRM_Contact_BAO_Relationship::add($relationshipParams);
$output->setParameter('id', $relationship->id);
} catch (\Exception $e) {
// Do nothing.
......@@ -124,4 +124,4 @@ class CreateOrUpdateRelationship extends CreateRelationship {
}
}
}
}
\ No newline at end of file
......@@ -129,11 +129,11 @@ class CreateRelationship extends AbstractAction {
}
try {
// Do not use api as the api checks for an existing relationship.
$relationship = \CRM_Contact_BAO_Relationship::create($relationshipParams);
$relationship = \CRM_Contact_BAO_Relationship::add($relationshipParams);
$output->setParameter('id', $relationship->id);
} catch (\Exception $e) {
// Do nothing.
}
}
}
}
\ No newline at end of file
......@@ -9,18 +9,18 @@ use \CRM_ActionProvider_ExtensionUtil as E;
/**
* Singleton and conatiner class with all the actions.
*
* This class could be overriden by child classes in an extension to provide a context aware container
* for the actions.
*
* This class could be overriden by child classes in an extension to provide a context aware container
* for the actions.
*/
class Provider {
/**
* @var array
* All the actions which are available for use in this context.
*/
protected $availableActions = array();
/**
* @var array
* All the actions including the inactive ones.
......@@ -54,7 +54,7 @@ class Provider {
* Contains all instanciated actions.
*/
protected $batchActions = array();
public function __construct() {
$this->addActionWithoutFiltering('OptionValueToLabel', '\Civi\ActionProvider\Action\Generic\OptionValueToLabel', E::ts('Show option value(s) as their Label(s)'), array(
AbstractAction::DATA_MANIPULATION_TAG));
......@@ -111,6 +111,10 @@ class Provider {
AbstractAction::SINGLE_CONTACT_ACTION_TAG,
AbstractAction::DATA_RETRIEVAL_TAG,
));
$this->addActionWithoutFiltering('GetStateProvinceId', '\Civi\ActionProvider\Action\Contact\GetStateProvinceId', E::ts('Get state/province ID by name'), array(
AbstractAction::WITHOUT_CONTACT_ACTION_TAG,
AbstractAction::DATA_RETRIEVAL_TAG,
));
$this->addActionWithoutFiltering('GetAddressById', '\Civi\ActionProvider\Action\Contact\GetAddressById', E::ts('Get address by ID'), array(
AbstractAction::SINGLE_CONTACT_ACTION_TAG,
AbstractAction::DATA_RETRIEVAL_TAG,
......@@ -123,10 +127,6 @@ class Provider {
AbstractAction::SINGLE_CONTACT_ACTION_TAG,
AbstractAction::DATA_RETRIEVAL_TAG,
));
$this->addActionWithoutFiltering('FindContactByCustomField', '\Civi\ActionProvider\Action\Contact\FindByCustomField', E::ts('Find contact by custom field'), array(
AbstractAction::SINGLE_CONTACT_ACTION_TAG,
AbstractAction::DATA_RETRIEVAL_TAG,
));
$this->addActionWithoutFiltering('CreateUpdateIndividual', '\Civi\ActionProvider\Action\Contact\CreateUpdateIndividual', E::ts('Create or update Individual'), array(
AbstractAction::SINGLE_CONTACT_ACTION_TAG,
AbstractAction::DATA_MANIPULATION_TAG,
......@@ -305,7 +305,7 @@ class Provider {
$this->availableActions = array_filter($this->allActions, array($this, 'filterActions'));
$this->availableConditions = array_filter($this->allConditions, array($this, 'filterConditions'));
}
/**
* Returns all available actions
*/
......@@ -322,12 +322,12 @@ class Provider {
}
return $titles;
}
/**
* Adds an action to the list of available actions.
*
*
* This function might be used by extensions to add their own actions to the system.
*
*
* @param String $name
* @param String $className
* @param String $title
......@@ -357,10 +357,10 @@ class Provider {
$this->acttionTags[$name] = $tags;
return $this;
}
/**
* Returns an action by its name.
*
*
* @return \Civi\ActionProvider\Action\AbstractAction|null when action is not found.
*/
public function getActionByName($name) {
......@@ -445,21 +445,21 @@ class Provider {
}
return null;
}
/**
* Returns a new ParameterBag
*
*
* This function exists so we can encapsulate the creation of a ParameterBag to the provider.
*
*
* @return ParameterBagInterface
*/
public function createParameterBag() {
return new ParameterBag();
}
/**
* Returns a new parameter bag based on the given mapping.
*
*
* @param ParameterBagInterface $parameterBag
* @param array $mapping
* @return ParameterBagInterface
......@@ -473,14 +473,14 @@ class Provider {
}
return $mappedParameterBag;
}
/**
* Filter the actions array and keep certain actions.
*
*
* This function might be override in a child class to filter out certain actions which do
* not make sense in that context. E.g. for example CiviRules has already a AddContactToGroup action
* not make sense in that context. E.g. for example CiviRules has already a AddContactToGroup action
* so it does not make sense to use the one provided by us.
*
*
* @param string
* The action to filter.
* @return bool
......@@ -504,5 +504,5 @@ class Provider {
protected function filterConditions(\Civi\ActionProvider\Condition\AbstractCondition $condition) {
return true;
}
}
......@@ -25,8 +25,6 @@ class CustomField {
switch ($type) {
case 'Int':
case 'ContactReference':
case 'StateProvince':
case 'Country':
$type = 'Integer';
break;
case 'File':
......
......@@ -13,8 +13,8 @@
<url desc="Documentation">https://lab.civicrm.org/jaapjansma/action-provider/wikis/home</url>
<url desc="Licensing">http://www.gnu.org/licenses/agpl-3.0.html</url>
</urls>
<releaseDate>2019-08-29</releaseDate>
<version>1.7-dev</version>
<releaseDate>2019-08-14</releaseDate>
<version>1.6-dev</version>
<develStage>stable</develStage>
<compatibility>
<ver>4.7</ver>
......
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