Commit 55610c56 authored by jaapjansma's avatar jaapjansma

Added action for create organization, added action for uploading files to an event

parent bd1f8b3a
......@@ -2,7 +2,9 @@ Version 1.4 (not yet released)
-----------
* Changed the implementation of the civicrm_container hook.
* Added action to upload files
* Added action to upload files for a contact
* Added action to upload files for an event
* Added action to create an organization
* Added action to retrieve the labels from option values
Version 1.3
......
<?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 CreateUpdateOrganization 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'] = "Organization";
if ($contact_sub_type) {
$params['contact_sub_type'] = $contact_sub_type;
}
$params['organization_name'] = $parameters->getParameter('organization_name');
$result = civicrm_api3('Contact', 'create', $params);
$contact_id = $result['id'];
$output->setParameter('contact_id', $contact_id);
// Create address
$address_id = ContactActionUtils::createAddressForContact($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' => 'Organization', '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 Organization'));
$spec = new SpecificationBag(array(
$contactIdSpec,
new Specification('organization_name', 'String', E::ts('Organization 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),
));
}
}
......@@ -54,6 +54,8 @@ class CreateOrUpdateEvent extends AbstractAction {
new Specification('description', 'String', E::ts('Description'), false, null, null, null, FALSE),
new Specification('start_date', 'Timestamp', E::ts('Start date'), true, null, null, null, FALSE),
new Specification('end_date', 'Timestamp', E::ts('End date'), false, null, null, null, FALSE),
new Specification('is_active', 'Boolean', E::ts('Is active'), false, 1, null, null, FALSE),
new Specification('is_public', 'Boolean', E::ts('Is public'), false, 0, null, null, FALSE),
));
$customGroups = civicrm_api3('CustomGroup', 'get', array('extends' => 'Event', 'is_active' => 1, 'options' => array('limit' => 0)));
......@@ -136,6 +138,12 @@ class CreateOrUpdateEvent extends AbstractAction {
$apiParams['loc_block_id'] = $locBlockId;
$apiParams['is_show_location'] = '1';
}
if ($parameters->doesParameterExists('is_active')) {
$apiParams['is_active'] = $parameters->getParameter('is_active');
}
if ($parameters->doesParameterExists('is_public')) {
$apiParams['is_public'] = $parameters->getParameter('is_public');
}
foreach($this->getParameterSpecification() as $spec) {
if (stripos($spec->getName(), 'custom_')!==0) {
......
<?php
namespace Civi\ActionProvider\Action\Event;
use \Civi\ActionProvider\Action\AbstractAction;
use Civi\ActionProvider\Parameter\FileSpecification;
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 UploadCustomFileField 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) {
$event_id = $parameters->getParameter('event_id');
$customFieldId = $this->configuration->getParameter('custom_field');
$file = $parameters->getParameter('file');
$deleteCurrentFile = false;
$uploadNewOne = true;
$updateCustomField = false;
if (empty($file)) {
$deleteCurrentFile = true;
$uploadNewOne = false;
} elseif (!isset($file['id'])) {
$deleteCurrentFile = true;
} elseif (isset($file['id'])) {
$uploadNewOne = false;
}
try {
$currentFile = civicrm_api3('Event', 'getvalue', [
'id' => $event_id,
'return' => 'custom_' . $customFieldId
]);
if (isset($file['id']) && $currentFile && $file['id'] != $currentFile) {
$deleteCurrentFile = true;
$updateCustomField = $file['id'];
}
if ($currentFile && $deleteCurrentFile) {
civicrm_api3('Attachment', 'delete', array('id' => $currentFile));
}
} catch (\Exception $e) {
// Do nothing
}
if ($uploadNewOne) {
$attachmentParams = [
'entity_table' => 'civicrm_event',
'entity_id' => $event_id,
'name' => $file['name'],
'content' => base64_decode($file['content']),
'mime_type' => $file['mime_type'],
'check_permissions' => FALSE,
];
$result = civicrm_api3('Attachment', 'create', $attachmentParams);
$updateCustomField = $result['id'];
}
if ($updateCustomField) {
civicrm_api3('Event', 'create', [
'id' => $event_id,
'custom_' . $customFieldId => $updateCustomField,
]);
}
}
/**
* Returns the specification of the configuration options for the actual action.
*
* @return SpecificationBag
*/
public function getConfigurationSpecification() {
$fileFields = array();
$customGroups = civicrm_api3('CustomGroup', 'get', array('is_active' => 1, 'is_multiple' => 0, 'extends' => 'Event', 'options' => array('limit' => 0)));
foreach($customGroups['values'] as $customGroup) {
if (!in_array($customGroup['extends'], [
'Event'
])) {
continue;
}
$customFields = civicrm_api3('CustomField', 'get', [
'custom_group_id' => $customGroup['id'],
'is_active' => 1,
'data_type' => 'File',
'options' => ['limit' => 0]
]);
foreach ($customFields['values'] as $customField) {
$fileFields[$customField['id']] = $customGroup['title']. ' :: '.$customField['label'];
}
}
return new SpecificationBag([
new Specification('custom_field', 'Integer', E::ts('File field'), true, null, null, $fileFields, false),
]);
}
/**
* Returns the specification of the parameters of the actual action.
*
* @return SpecificationBag
*/
public function getParameterSpecification() {
$specs = new SpecificationBag();
$specs->addSpecification(new Specification('event_id', 'Integer', E::ts('Event ID'), true));
$specs->addSpecification(new FileSpecification('file', E::ts('File'), false));
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();
}
}
\ No newline at end of file
......@@ -129,6 +129,10 @@ class Provider {
AbstractAction::SINGLE_CONTACT_ACTION_TAG,
AbstractAction::DATA_MANIPULATION_TAG,
));
$this->addActionWithoutFiltering('CreateUpdateOrganization', '\Civi\ActionProvider\Action\Contact\CreateUpdateOrganization', E::ts('Create or update Organization'), array(
AbstractAction::SINGLE_CONTACT_ACTION_TAG,
AbstractAction::DATA_MANIPULATION_TAG,
));
$this->addActionWithoutFiltering('UpdateCustomData', '\Civi\ActionProvider\Action\Contact\UpdateCustomData',E::ts('Update custom data for a contact') , array(
AbstractAction::SINGLE_CONTACT_ACTION_TAG,
AbstractAction::DATA_MANIPULATION_TAG,
......@@ -187,6 +191,10 @@ class Provider {
$this->addActionWithoutFiltering('GetEvent', '\Civi\ActionProvider\Action\Event\GetEvent',E::ts('Get event data') , array(
AbstractAction::DATA_RETRIEVAL_TAG,
));
$this->addActionWithoutFiltering('EventUploadCustomFileField', '\Civi\ActionProvider\Action\Event\UploadCustomFileField',E::ts('Upload file to a custom field for an event') , array(
AbstractAction::SINGLE_CONTACT_ACTION_TAG,
AbstractAction::DATA_MANIPULATION_TAG,
));
$this->addActionWithoutFiltering('DeleteEvent', '\Civi\ActionProvider\Action\Event\DeleteEvent',E::ts('Delete event') , array(
AbstractAction::DATA_MANIPULATION_TAG,
));
......
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