Commit 172e45eb authored by jaapjansma's avatar jaapjansma

Added actions for velt portal

parent 9f73dc65
......@@ -32,6 +32,8 @@ abstract class AbstractAction implements \JsonSerializable {
// Use this tag if the action works with multieple contacts.
const MULTIPLE_CONTACTS_ACTION_TAG = 'action-on-multiple-contacts';
const SEND_MESSAGES_TO_CONTACTS = 'send-messages';
/**
* @var ParameterBag
......
<?php
/**
* @author Jaap Jansma <jaap.jansma@civicoop.org>
* @license AGPL-3.0
*/
namespace Civi\ActionProvider\Action\BulkMail;
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 Send extends AbstractAction {
/**
* Returns the human readable title of this action
*/
public function getTitle() {
return E::ts('Send Bulk Mail');
}
/**
* 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 configuration options for the actual action.
*
* @return SpecificationBag
* @throws \Exception
*/
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('subject', 'String', E::ts('Subject'), true, null, null, null, False),
new Specification('body_html', 'Text', E::ts('HTML Body'), true, null, null, null, FALSE),
new Specification('group_id', 'Integer', E::ts('Select group'), true, null, 'Group', null, FALSE),
new Specification('sender_contact_id', 'Integer', E::ts('Sender Contact ID'), true)
));
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('Bulk mail record ID')),
));
}
/**
* Run the action
*
* @param ParameterBagInterface $parameters
* The parameters to this action.
* @param ParameterBagInterface $output
* The parameters this action can send back
* @return void
* @throws \Exception
*/
protected function doAction(ParameterBagInterface $parameters, ParameterBagInterface $output) {
$apiParams['name'] = $parameters->getParameter('subject');
$apiParams['subject'] = $parameters->getParameter('subject');
$apiParams['body_html'] = $parameters->getParameter('body_html');
$apiParams['created_id'] = $parameters->getParameter('sender_contact_id');
$mailing = civicrm_api3('Mailing', 'Create', $apiParams);
$apiGroupParams['group_type'] = 'Include';
$apiGroupParams['entity_table'] = 'civicrm_group';
$apiGroupParams['entity_id'] = $parameters->getParameter('group_id');
$apiGroupParams['mailing_id'] = $mailing['id'];
civicrm_api3('MailingGroup', 'create', $apiGroupParams);
// Now send the mailing
$now = new \DateTime();
$now->setTimezone(new \DateTimeZone('UTC'));
$mailingSendParams['id'] = $mailing['id'];
$mailingSendParams['scheduled_date'] = $now->format('Ymd His');
civicrm_api3('Mailing', 'submit', $mailingSendParams);
$output->setParameter('id', $mailing['id']);
}
/**
* Returns the tags for this action.
*/
public function getTags() {
return array(
AbstractAction::SEND_MESSAGES_TO_CONTACTS,
'bulk_mail',
);
}
}
\ No newline at end of file
<?php
namespace Civi\ActionProvider\Action\Event;
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 \Civi\ActionProvider\Utils\CustomField;
use CRM_ActionProvider_ExtensionUtil as E;
class CreateOrUpdateEvent extends AbstractAction {
/**
* Returns the human readable title of this action
*/
public function getTitle() {
return E::ts('Create or update an event');
}
/**
* 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 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('event_id', 'Integer', E::ts('Event ID'), false, null, null, null, FALSE),
new OptionGroupSpecification('event_type', 'event_type', E::ts('Event Type'), TRUE),
new Specification('title', 'String', E::ts('Title'), true, null, null, null, FALSE),
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),
));
$customGroups = civicrm_api3('CustomGroup', 'get', array('extends' => 'Event', '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('Event 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('event_id')) {
$apiParams['id'] = $parameters->getParameter('event_id');
}
$apiParams['title'] = $parameters->getParameter('title');
if ($parameters->doesParameterExists('description')) {
$apiParams['description'] = $parameters->getParameter('description');
}
$apiParams['start_date'] = $parameters->getParameter('start_date');
$apiParams['event_type_id'] = $parameters->getParameter('event_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('Event', 'create', $apiParams);
$output->setParameter('id', $result['id']);
} catch (Exception $e) {
throw new \Civi\ActionProvider\Exception\ExecutionException(E::ts('Could not update or create an event.'));
}
}
/**
* Returns the tags for this action.
*/
public function getTags() {
return array(
AbstractAction::DATA_MANIPULATION_TAG,
);
}
}
\ No newline at end of file
<?php
/**
* @author Jaap Jansma <jaap.jansma@civicoop.org>
* @license AGPL-3.0
*/
namespace Civi\ActionProvider\Action\Event;
use Civi\ActionProvider\Action\AbstractAction;
use Civi\ActionProvider\Parameter\ParameterBagInterface;
use Civi\ActionProvider\Parameter\Specification;
use Civi\ActionProvider\Parameter\SpecificationBag;
use CRM_ActionProvider_ExtensionUtil as E;
class DeleteEvent extends AbstractAction {
/**
* Returns the human readable title of this action
*/
public function getTitle() {
return E::ts('Delete event');
}
/**
* Returns the specification of the configuration options for the actual
* action.
*
* @return SpecificationBag
*/
public function getConfigurationSpecification() {
return new SpecificationBag();
}
/**
* Returns the specification of the parameters of the actual action.
*
* @return SpecificationBag
*/
public function getParameterSpecification() {
$bag = new SpecificationBag([
new Specification('id', 'Integer', E::ts('Event ID'), TRUE),
]);
return $bag;
}
/**
* 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();
}
/**
* Run the action
*
* @param ParameterBagInterface $parameters
* The parameters to this action.
* @param ParameterBagInterface $output
* The parameters this action can send back
* @return void
* @throws \Exception
*/
protected function doAction(ParameterBagInterface $parameters, ParameterBagInterface $output) {
if ($parameters->doesParameterExists('id')) {
$event_id = $parameters->getParameter('id');
try {
// Do not use api as the api checks for an existing relationship.
\CRM_Event_BAO_Event::del($event_id);
} catch (\Exception $e) {
// Do nothing.
}
}
}
/**
* 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\Event;
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 \Civi\ActionProvider\Utils\CustomField;
use CRM_ActionProvider_ExtensionUtil as E;
class GetEvent extends AbstractAction {
/**
* Returns the human readable title of this action
*/
public function getTitle() {
return E::ts('Get event data');
}
/**
* 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 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('event_id', 'Integer', E::ts('Event ID'), false, null, null, null, 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() {
$bag = new SpecificationBag();
$contact_fields = civicrm_api3('Event', '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;
}
$fieldSpec = new Specification(
$field['name'],
$type,
$field['title'],
false
);
$bag->addSpecification($fieldSpec);
}
return $bag;
}
/**
* 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.
$event_id = $parameters->getParameter('event_id');
if (!$event_id) {
return;
}
try {
$event = civicrm_api3('Event', 'getsingle', array('id' => $event_id));
foreach($this->getOutputSpecification() as $spec) {
$fieldName = $spec->getName();
if (stripos($fieldName, 'custom_') === 0 && isset($event[$fieldName.'_id'])) {
$fieldName = $fieldName . '_id';
}
if (isset($event[$fieldName])) {
$output->setParameter($spec->getName(), $event[$fieldName]);
}
}
} catch (\Exception $e) {
// Do nothing
}
}
/**
* Returns the tags for this action.
*/
public function getTags() {
return array(
AbstractAction::DATA_RETRIEVAL_TAG,
);
}
}
\ No newline at end of file
<?php
namespace Civi\ActionProvider\Action;
namespace Civi\ActionProvider\Action\Group;
use \Civi\ActionProvider\Action\AbstractAction;
use \Civi\ActionProvider\Parameter\ParameterBagInterface;
......
<?php
namespace Civi\ActionProvider\Action\Group;
use \Civi\ActionProvider\Action\AbstractAction;
use \Civi\ActionProvider\Parameter\ParameterBagInterface;
use \Civi\ActionProvider\Parameter\SpecificationBag;
use \Civi\ActionProvider\Parameter\Specification;
use CRM_ActionProvider_ExtensionUtil as E;
class AddToGroupParameter 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) {
civicrm_api3('GroupContact', 'create', array(
'contact_id' => $parameters->getParameter('contact_id'),
'group_id' => $parameters->getParameter('group_id'),
));
}
/**
* 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('contact_id', 'Integer', E::ts('Contact ID'), true),
new Specification('group_id', 'Integer', E::ts('Group ID'), true, null, 'Group', null, FALSE),
));
}
/**
* Returns the human readable title of this action
*/
public function getTitle() {
return E::ts('Add to group (with Group ID as parameter)');
}
/**
* Returns the tags for this action.
*/
public function getTags() {
return array(
AbstractAction::SINGLE_CONTACT_ACTION_TAG,
AbstractAction::DATA_MANIPULATION_TAG,
);
}
}
<?php
/**
* @author Jaap Jansma <jaap.jansma@civicoop.org>
* @license AGPL-3.0
*/
namespace Civi\ActionProvider\Action\Group;
use Civi\ActionProvider\Action\AbstractAction;
use Civi\ActionProvider\Parameter\ParameterBagInterface;
use Civi\ActionProvider\Parameter\Specification;
use Civi\ActionProvider\Parameter\OptionGroupSpecification;
use Civi\ActionProvider\Parameter\SpecificationBag;
use Civi\ActionProvider\Utils\CustomField;
use CRM_ActionProvider_ExtensionUtil as E;
class Create extends AbstractAction {
/**
* Returns the human readable title of this action
*/
public function getTitle() {
return E::ts('Create/Update a group');
}
/**
* Returns the specification of the configuration options for the actual
* action.
*
* @return SpecificationBag
*/
public function getConfigurationSpecification() {
return new SpecificationBag([
new OptionGroupSpecification('group_type','group_type', E::ts('Group type'), FALSE),
]);
}
/**
* Returns the specification of the parameters of the actual action.
*
* @return SpecificationBag
*/
public function getParameterSpecification() {
$bag = new SpecificationBag([
new Specification('id', 'Integer', E::ts('Group ID'), FALSE),
new Specification('title', 'String', E::ts('Group Title'), TRUE),
new Specification('description', 'Text', E::ts('Description'), FALSE),
]);
$customGroups = civicrm_api3('CustomGroup', 'get', [
'extends' => 'Group',
'is_active' => 1,
'options' => ['limit' => 0],
]);
foreach ($customGroups['values'] as $customGroup) {
$customFields = civicrm_api3('CustomField', 'get', [
'custom_group_id' => $customGroup['id'],
'is_active' => 1,
'options' => ['limit' => 0],
]);
foreach ($customFields['values'] as $customField) {
$spec = CustomField::getSpecFromCustomField($customField, $customGroup['title'] . ': ', FALSE);
if ($spec) {
$bag->addSpecification($spec);
}
}
}
return $bag;
}
/**
* 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('Group record ID')),
));
}
/**
* Run the action
*
* @param ParameterBagInterface $parameters
* The parameters to this action.
* @param ParameterBagInterface $output
* The parameters this action can send back
* @return void
* @throws \Exception
*/
protected function doAction(ParameterBagInterface $parameters, ParameterBagInterface $output) {
// Get the contact and the event.
if ($parameters->doesParameterExists('id')) {
$groupApiParams['id'] = $parameters->getParameter('id');
}
$groupApiParams['title'] = $parameters->getParameter('title');
$groupApiParams['description'] = $parameters->getParameter('description');
if ($this->configuration->doesParameterExists('group_type')) {
$groupApiParams['group_type'] = $this->configuration->getParameter('group_type');
}
foreach($this->getParameterSpecification() as $spec) {
if (stripos($spec->getName(), 'custom_')!==0) {
continue;
}
if ($parameters->doesParameterExists($spec->getName())) {
$groupApiParams[$spec->getApiFieldName()] = $parameters->getParameter($spec->getName());
}
}
try {
// Do not use api as the api checks for an existing relationship.
$result = civicrm_api3('Group', 'Create', $groupApiParams);
$output->setParameter('id', $result['id']);
} catch (\Exception $e) {
// Do nothing.
}
}
/**
* Returns the tags for this action.
*/
public function getTags() {
return array(
AbstractAction::DATA_MANIPULATION_TAG,
'group',
);
}
}
\ No newline at end of file
<?php
/**
* @author Jaap Jansma <jaap.jansma@civicoop.org>
* @license AGPL-3.0
*/
namespace Civi\ActionProvider\Action\Group;
use Civi\ActionProvider\Action\AbstractAction;
use Civi\ActionProvider\Parameter\ParameterBagInterface;
use Civi\ActionProvider\Parameter\Specification;
use Civi\ActionProvider\Parameter\SpecificationBag;
use CRM_ActionProvider_ExtensionUtil as E;
class DeleteGroup extends AbstractAction {
/**
* Returns the human readable title of this action
*/