Commit 11b0f04d authored by jaapjansma's avatar jaapjansma

refactor and adding action for creating website

parent 09f9fb4c
......@@ -240,7 +240,8 @@ abstract class AbstractAction implements \JsonSerializable {
* Returns the data structure to serialize it as a json
*/
public function jsonSerialize() {
$return = $this->toArray();
$return['name'] = $this->getName();
$return['title'] = $this->getTitle();
// An empty array goes wrong with the default confifuration.
if (empty($return['default_configuration'])) {
$return['default_configuration'] = new \stdClass();;
......
......@@ -63,7 +63,7 @@ class AddToGroup extends AbstractAction {
return array(
AbstractAction::SINGLE_CONTACT_ACTION_TAG,
AbstractAction::DATA_MANIPULATION_TAG,
'GroupContactAdd', // This how this action is called in CiviRules
'CiviRules.GroupContactAdd', // This how this action is called in CiviRules
);
}
......
......@@ -89,7 +89,7 @@ class CreateUpdateIndividual extends AbstractAction {
* @return SpecificationBag
*/
public function getParameterSpecification() {
$contactIdSpec = new Specification('contact_id', 'String', E::ts('Contact ID'), false);
$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,
......
<?php
namespace Civi\ActionProvider\Action\Website;
use \Civi\ActionProvider\Action\AbstractAction;
use \Civi\ActionProvider\Parameter\ParameterBagInterface;
use \Civi\ActionProvider\Parameter\SpecificationBag;
use \Civi\ActionProvider\Parameter\Specification;
use \Civi\ActionProvider\Parameter\OptionGroupSpecification;
use CRM_ActionProvider_ExtensionUtil as E;
class CreateUpdateWebsite 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) {
$existingWebsitelId = false;
$contact_id = $parameters->getParameter('contact_id');
$existingWebsitelParams['contact_id'] = $contact_id;
$existingWebsitelParams['website_type_id'] = $this->configuration->getParameter('website_type');
$existingWebsitelParams['return'] = 'id';
try {
$existingWebsitelId = civicrm_api3('Website', 'getvalue', $existingWebsitelParams);
} catch (\Exception $e) {
// Do nothing
}
if (!empty($parameters->getParameter('url'))) {
$params = array();
if ($existingWebsitelId) {
$params['id'] = $existingWebsitelId;
}
$params['website_type_id'] = $this->configuration->getParameter('website_type');
$params['contact_id'] = $contact_id;
$params['url'] = $parameters->getParameter('url');
civicrm_api3('Website', 'create', $params);
} elseif ($existingWebsitelId) {
$params = array();
$params['id'] = $existingWebsitelId;
civicrm_api3('Website', 'delete', $params);
}
}
/**
* Returns the specification of the configuration options for the actual action.
*
* @return SpecificationBag
*/
public function getConfigurationSpecification() {
return new SpecificationBag(array(
new OptionGroupSpecification('website_type', 'website_type', E::ts('Website Type'), true),
));
}
/**
* Returns the specification of the parameters of the actual action.
*
* @return SpecificationBag
*/
public function getParameterSpecification() {
return new SpecificationBag(array(
new Specification('contact_id', 'Integer', E::ts('Contact ID'), false),
new Specification('url', 'String', E::ts('Url'), false),
));
}
/**
* 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();
}
/**
* Returns the human readable title of this action
*/
public function getTitle() {
return E::ts('Create or update website of a contact');
}
/**
* Returns the tags for this action.
*/
public function getTags() {
return array(
AbstractAction::SINGLE_CONTACT_ACTION_TAG,
AbstractAction::DATA_MANIPULATION_TAG,
);
}
}
\ No newline at end of file
<?php
namespace Civi\ActionProvider\Action\Website;
use \Civi\ActionProvider\Action\AbstractAction;
use \Civi\ActionProvider\Parameter\ParameterBagInterface;
use \Civi\ActionProvider\Parameter\SpecificationBag;
use \Civi\ActionProvider\Parameter\Specification;
use \Civi\ActionProvider\Parameter\OptionGroupSpecification;
use CRM_ActionProvider_ExtensionUtil as E;
class GetWebsite 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_id = $parameters->getParameter('contact_id');
$existingWebsitelParams['contact_id'] = $contact_id;
$existingWebsitelParams['website_type_id'] = $this->configuration->getParameter('website_type');
try {
$existingWebsite = civicrm_api3('Website', 'getsingle', $existingWebsitelParams);
$output->setParameter('url', $existingWebsite['url']);
} catch (\Exception $e) {
// Do nothing
$output->setParameter('url', '');
}
}
/**
* Returns the specification of the configuration options for the actual action.
*
* @return SpecificationBag
*/
public function getConfigurationSpecification() {
return new SpecificationBag(array(
new OptionGroupSpecification('website_type', 'website_type', E::ts('Website Type'), true),
));
}
/**
* Returns the specification of the parameters of the actual action.
*
* @return SpecificationBag
*/
public function getParameterSpecification() {
return new SpecificationBag(array(
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() {
return new SpecificationBag(array(
new Specification('url', 'String', E::ts('Url'), false),
));
}
/**
* Returns the human readable title of this action
*/
public function getTitle() {
return E::ts('Get website url of a contact');
}
/**
* 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
......@@ -35,6 +35,8 @@ class Provider {
new \Civi\ActionProvider\Action\Event\UpdateParticipantStatus(),
new \Civi\ActionProvider\Action\Event\CreateOrUpdateParticipant(),
new \Civi\ActionProvider\Action\Event\GetParticipant(),
new \Civi\ActionProvider\Action\Website\CreateUpdateWebsite(),
new \Civi\ActionProvider\Action\Website\GetWebsite(),
);
foreach($actions as $action) {
......
(function(angular, $, _) {
// "crmApActionConfiguration" is a basic skeletal directive.
// Example usage: <crm-ap-action-configuration configuration="action.configuration" action="action.type"></crm-ap-action-configuration>
angular.module('action_provider').directive('crmApActionConfiguration', function() {
angular.module('action_provider').directive('crmApActionConfiguration', ["crmApi", function(crmApi) {
var actions = {};
return {
restrict: 'E',
templateUrl: '~/action_provider/crmApActionConfiguration.html',
scope: {
action: '=action',
configuration: '=configuration',
fields: '=?fields',
mapping: '=?mapping',
name: '=',
context: '@',
configuration: '=',
fields: '=?',
mapping: '=?',
},
link: function($scope, $el, $attr) {
$scope.ts = CRM.ts(null);
console.log($scope.action.parameter_spec);
$scope.action = {};
if (!($scope.context in actions)) {
actions[$scope.context] = {};
}
if ($scope.name in actions[$scope.context]) {
$scope.action = actions[$scope.context][$scope.name];
return;
}
crmApi('ActionProvider', 'getaction', {name: $scope.name, context: $scope.context}).
then(function (data) {
actions[$scope.context][$scope.name] = data;
$scope.action = data;
});
}
};
});
}]);
})(angular, CRM.$, CRM._);
<?php
use CRM_ActionProvider_ExtensionUtil as E;
/**
* ActionProvider.Getaction API
*
* @param array $params
* @return array API result descriptor
* @see civicrm_api3_create_success
* @see civicrm_api3_create_error
* @throws API_Exception
*/
function civicrm_api3_action_provider_getaction($params) {
$container = \Civi::service('action_provider');
if (isset($params['context'])) {
$provider = $container->getProviderByContext($params['context']);
} else {
$provider = $container->getDefaultProvider();
}
$action = $provider->getActionByName($params['name']);
return $action->toArray();
}
/**
* ActionProvider.Getaction API specification (optional)
* This is used for documentation and validation.
*
* @param array $spec description of fields supported by this API call
* @return void
* @see http://wiki.civicrm.org/confluence/display/CRM/API+Architecture+Standards
*/
function _civicrm_api3_action_provider_getaction_spec(&$spec) {
$spec['name'] = array(
'title' => E::ts('Name'),
'type' => CRM_Utils_Type::T_STRING,
'api.required' => true
);
$spec['context'] = array(
'title' => E::ts('Context'),
'type' => CRM_Utils_Type::T_STRING,
'api.required' => false
);
}
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