Commit 7aca1a70 authored by ErikHommel's avatar ErikHommel 🏘
Browse files

tested locally for petition

parent 61fea612
......@@ -4,6 +4,7 @@ namespace Civi\Aivlspecificactions\Actions;
use \Civi\ActionProvider\Action\AbstractAction;
use Civi\ActionProvider\Exception\ExecutionException;
use \Civi\ActionProvider\Exception\InvalidParameterException;
use Civi\ActionProvider\Parameter\OptionGroupByNameSpecification;
use \Civi\ActionProvider\Parameter\ParameterBagInterface;
use \Civi\ActionProvider\Parameter\SpecificationBag;
use \Civi\ActionProvider\Parameter\Specification;
......@@ -13,29 +14,22 @@ use Civi\FormProcessor\API\Exception;
use CRM_Aivlspecificactions_ExtensionUtil as E;
/**
* Class AivlPetition - process petition signed (on website)
* - get or create contact (using XCM)
* - add petition activity to contact
* - set email preferences for newsletter + action mail if new contact
* Class PetitionActivity - create activity for petition signed (on website)
*
* @package Civi\Aivlspecificactions\Actions
* @author Erik Hommel (CiviCooP) <erik.hommel@civicoop.org>
* @date 19 Maa 2020
* @date 22 Maa 2020
* @license AGPL-3.0
*/
class AivlPetition extends AbstractAction {
class PetitionActivity extends AbstractAction {
/**
* @return SpecificationBag
*/
public function getParameterSpecification() {
$specs = new SpecificationBag();
$specs->addSpecification(new Specification('first_name', 'String', E::ts('First Name'), TRUE, NULL));
$specs->addSpecification(new Specification('last_name', 'String', E::ts('First Name'), TRUE, NULL));
$specs->addSpecification(new Specification('email', 'Email', E::ts('Email'), TRUE, NULL));
$specs->addSpecification(new Specification('birth_date', 'String', E::ts('Birth Date'), FALSE, NULL));
$specs->addSpecification(new Specification('more_info', 'Boolean', E::ts('More Information'), FALSE, NULL));
$specs->addSpecification(new Specification('campaign_id', 'Integer', E::ts('Campaign ID'), TRUE, NULL));
$specs->addSpecification(new Specification('campaign_id', 'Integer', E::ts('Campaign ID'), FALSE, NULL));
$specs->addSpecification(new Specification('contact_id', 'Integer', E::ts('Contact ID'), TRUE, NULL));
return $specs;
}
......@@ -43,72 +37,84 @@ class AivlPetition extends AbstractAction {
* @return SpecificationBag
*/
public function getConfigurationSpecification() {
return new SpecificationBag();
$specs = new SpecificationBag();
$specs->addSpecification(new OptionGroupByNameSpecification('activity_type_id', 'activity_type', E::ts('Activity Type'), TRUE));
$specs->addSpecification(new OptionGroupByNameSpecification('status_id', 'activity_status', E::ts('Activity Status'), TRUE));
return $specs;
}
/**
* Do the actual action - process petition signature
*
*
* @param ParameterBagInterface $parameters
* @param ParameterBagInterface $output
* @throws ExecutionException
*/
public function doAction(ParameterBagInterface $parameters, ParameterBagInterface $output) {
$campaignId = trim($parameters->getParameter('campaign_id'));
$moreInfo = $parameters->getParameter('more_info');
// remember max contact ID to check after XCM if we have a new contact or not
$preMaxContactId = $this->getMaxContactId();
// get or create contact with XCM using params
$contactId = $this->getXCMContact($parameters);
// add petition activity
$this->addPetitionActivity($contactId, $campaignId);
// if this is a new contact, set email preferences if more information was ticked
if ($moreInfo && $contactId > $preMaxContactId) {
$this->setEmailPreferences($contactId);
$activityId = $this->addPetitionActivity($parameters, $output);
if ($activityId) {
$output->setParameter('activity_id', (int) $activityId);
}
}
/**
* Method to get or create the contact ID using XCM
*
* @param ParameterBagInterface $parameters
* @return int
* @param ParameterBagInterface $output
* @throws ExecutionException
*/
private function getXCMContact(ParameterBagInterface $parameters) {
$xcmParams = [
'first_name' => trim($parameters->getParameter('first_name')),
'last_name' => trim($parameters->getParameter('last_name')),
'email' => trim($parameters->getParameter('email')),
];
$birthDate = $parameters->getParameter('birth_date');
if ($birthDate && !empty($birthDate)) {
$xcmParams['birth_date'] = date('d-m-Y', strtotime($birthDate));
private function addPetitionActivity(ParameterBagInterface $parameters, ParameterBagInterface $output) {
$campaignId = $parameters->getParameter('campaign_id');
$contactId = $parameters->getParameter('contact_id');
if ($campaignId && $contactId) {
$activityParams = [
'source_contact_id' => 'user_contact_id',
'target_id' => $parameters->getParameter('contact_id'),
'campaign_id' => $campaignId,
'activity_type_id' => $this->configuration->getParameter('activity_type_id'),
'status_id' => $this->configuration->getParameter('status_id'),
'subject' => "Petition " . $this->getCampaignTitle($campaignId) . " signed on website.",
];
try {
$created = civicrm_api3('Activity', 'create', $activityParams);
$output->setParameter('activity_id', $created['id']);
}
catch (\CiviCRM_API3_Exception $ex) {
throw new ExecutionException(E::ts('Could not create petition activity, error message from API Activity create: ') . $ex->getMessage());
}
}
}
/**
* Method to get the campaign title
*
* @param $campaignId
* @return string|null
*/
private function getCampaignTitle($campaignId) {
$title = NULL;
try {
$result = civicrm_api3('Contact', 'getorcreate', $xcmParams);
return (int) $result['id'];
$title = (string) civicrm_api3('Campaign', 'getvalue', [
'return' => 'title',
'id' => $campaignId,
]);
}
catch (\CiviCRM_API3_Exception $ex) {
throw new ExecutionException(E::ts("Could not get or create a contact using XCM, error message from API Contact getorcreate: ") . $ex->getMessage());
}
return $title;
}
/**
* Method to get the maximum contact id
* Returns the specification of the output parameters of this action.
*
* @return bool|int
* This function could be overriden by child classes.
*
* @return SpecificationBag
*/
private function getMaxContactId() {
$query = "SELECT MAX(id) FROM civicrm_contact";
$maxContactId = \CRM_Core_DAO::singleValueQuery($query);
if ($maxContactId) {
return (int) $maxContactId;
}
else {
\Civi::log()->warning(E::ts('Error trying to get max contact ID with query ') . $query);
return 1; // there is always a contact with ID 1
}
public function getOutputSpecification() {
return new SpecificationBag([
new Specification('activity_id', 'Integer', E::ts('Petition Activity ID'), TRUE)
]);
}
}
<?php
namespace Civi\Aivlspecificactions\Actions;
use \Civi\ActionProvider\Action\AbstractAction;
use Civi\ActionProvider\Exception\ExecutionException;
use \Civi\ActionProvider\Exception\InvalidParameterException;
use \Civi\ActionProvider\Parameter\ParameterBagInterface;
use \Civi\ActionProvider\Parameter\SpecificationBag;
use \Civi\ActionProvider\Parameter\Specification;
use Civi\Core\Lock\NullLock;
use Civi\FormProcessor\API\Exception;
use CRM_Aivlspecificactions_ExtensionUtil as E;
/**
* Class PetitionEmailPrefs - set email preferences on petition signature
*
* @package Civi\Aivlspecificactions\Actions
* @author Erik Hommel (CiviCooP) <erik.hommel@civicoop.org>
* @date 22 Maa 2020
* @license AGPL-3.0
*/
class PetitionEmailPrefs extends AbstractAction {
/**
* @return SpecificationBag
*/
public function getParameterSpecification() {
$specs = new SpecificationBag();
$specs->addSpecification(new Specification('max_contact_id', 'Integer', E::ts('Max Contact ID pre-XCM'), TRUE, NULL));
$specs->addSpecification(new Specification('contact_id', 'Integer', E::ts('Contact ID'), TRUE, NULL));
$specs->addSpecification(new Specification('more_info', 'Boolean', E::ts('More Information'), TRUE, NULL));
return $specs;
}
/**
* @return SpecificationBag
*/
public function getConfigurationSpecification() {
$specs = new SpecificationBag();
$specs->addSpecification(new Specification('newsletter_group_id', 'Integer', E::ts('Newsletter Group ID'), TRUE, NULL, 'Group', NULL, FALSE));
$specs->addSpecification(new Specification('action_group_id', 'Integer', E::ts('Monthly Actions Group ID'), TRUE, NULL, 'Group', NULL, FALSE));
return $specs;
}
/**
* Do the actual action - process petition signature
*
* @param ParameterBagInterface $parameters
* @param ParameterBagInterface $output
* @throws ExecutionException
*/
public function doAction(ParameterBagInterface $parameters, ParameterBagInterface $output) {
$maxContactId = $parameters->getParameter('max_contact_id');
$contactId = $parameters->getParameter('contact_id');
$moreInfo = $parameters->getParameter('more_info');
// set email preferences if new contact and more info ticked
if ($moreInfo && $contactId > $maxContactId) {
$this->setEmailPreferences($contactId);
}
}
/**
* Method to set the email preferences
*
* @param $contactId
* @throws
*/
private function setEmailPreferences($contactId) {
//todo use FormProcessor, for now use API call from be.aivlapi extension
$newsGroup = $this->getGroupTitle(\Civi::settings()->get('aivl_monthly_newsletter_group_id'));
$actionGroup = $this->getGroupTitle(\Civi::settings()->get('aivl_monthly_actions_group_id'));
if ($newsGroup && $actionGroup) {
try {
civicrm_api3("AivlEmailPreferences", "set", [
'contact_id' => $contactId,
$newsGroup => "1",
$actionGroup => "1",
]);
}
catch (\CiviCRM_API3_Exception $ex) {
\Civi::log()->warning(E::ts('Could not add contact with ID to newsletter and monthly
actions groups when processing petition in ') . __METHOD__ . E::ts( ", error message
from API: ") . $ex->getMessage());
}
}
}
/**
* Method to get the processed group title
*
* @param $groupId
* @return bool|string|string[]
*/
private function getGroupTitle($groupId) {
if ($groupId) {
try {
$apiTitle = (string) civicrm_api3('Group', 'getvalue', [
'return' => 'title',
'id' => $groupId,
]);
$groupTitle = strtolower($apiTitle);
$groupTitle = str_replace(' ', '_', $groupTitle);
$groupTitle = str_replace(',', '_', $groupTitle);
$groupTitle = str_replace('.', '_', $groupTitle);
$groupTitle = str_replace(';', '_', $groupTitle);
$groupTitle = str_replace(':', '_', $groupTitle);
$groupTitle = str_replace('__', '_', $groupTitle);
return $groupTitle;
}
catch (\CiviCRM_API3_Exception $ex) {
}
}
else {
\Civi::log()->warning(E::ts('Could not find a group for newsletter or monthly action in ') . __METHOD__);
return FALSE;
}
}
}
......@@ -16,7 +16,9 @@ class CompilerPass implements CompilerPassInterface {
if ($container->hasDefinition('action_provider')) {
$actionProviderDefinition = $container->getDefinition('action_provider');
$actionProviderDefinition->addMethodCall('addAction',
['AivlPetition', 'Civi\Aivlspecificactions\Actions\AivlPetition', E::ts('Process AIVL Petition Signature'), []]);
['PetitionActivity', 'Civi\Aivlspecificactions\Actions\PetitionActivity', E::ts('Add AIVL Petition Activity'), []]);
$actionProviderDefinition->addMethodCall('addAction',
['PetitionEmailPrefs', 'Civi\Aivlspecificactions\Actions\PetitionEmailPrefs', E::ts('Set AIVL Petition Email Preferences for New Individual'), []]);
}
}
}
......@@ -2,6 +2,16 @@
require_once 'aivlspecificactions.civix.php';
use CRM_Aivlspecificactions_ExtensionUtil as E;
use Symfony\Component\DependencyInjection\ContainerBuilder;
/**
* Implements hook_civicrm_container().
*
* @param ContainerBuilder $container
*/
function aivlspecificactions_civicrm_container(ContainerBuilder $container) {
$container->addCompilerPass(new Civi\Aivlspecificactions\CompilerPass());
}
/**
* Implements hook_civicrm_config().
......
......@@ -14,9 +14,9 @@
<url desc="Support">https://civicoop.org</url>
<url desc="Licensing">http://www.gnu.org/licenses/agpl-3.0.html</url>
</urls>
<releaseDate>2020-03-19</releaseDate>
<releaseDate>2020-03-30</releaseDate>
<version>1.0</version>
<develStage>alpha</develStage>
<develStage>beta</develStage>
<compatibility>
<ver>4.7</ver>
</compatibility>
......
Supports Markdown
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