Commit a98c1c6c authored by ErikHommel's avatar ErikHommel 🏘
Browse files

fix merge conflicts in info file

parents a3191cb6 9dd575d3
<?php
namespace Civi\Aivlspecificactions\Actions;
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\FormProcessor\API\Exception;
use CRM_Aivlspecificactions_ExtensionUtil as E;
/**
* Class GetEmailPrefs - get the email preferences of a contact
*
* @package Civi\Aivlspecificactions\Action
* @author Erik Hommel (CiviCooP) <erik.hommel@civicoop.org>
* @date 22 Apr 2020
* @license AGPL-3.0
*/
class GetEmailPrefs extends AbstractAction {
/**
* @return SpecificationBag
*/
public function getParameterSpecification() {
return new SpecificationBag([
new Specification('contact_id', 'Integer', E::ts('Contact ID'), TRUE)
]);
}
/**
* @return SpecificationBag|void
*/
public function getConfigurationSpecification() {
return new SpecificationBag([
new Specification("news_group_id", "Integer", E::ts("Maandelijkse Nieuwsbrief Groep"), TRUE, NULL, 'Group'),
new Specification("action_group_id", "Integer", E::ts("Maandelijkse Actieberichten Groep"), TRUE, NULL, 'Group'),
new Specification("yearly_group_id", "Integer", E::ts("Jaarlijks Rapport Groep"), TRUE, NULL, 'Group'),
new Specification("flex1_group_id", "Integer", E::ts("Flexibele Groep 1"), FALSE, NULL, 'Group'),
new Specification("flex2_group_id", "Integer", E::ts("Flexibele Groep 2"), FALSE, NULL, 'Group'),
new Specification("flex3_group_id", "Integer", E::ts("Flexibele Groep 3"), FALSE, NULL, 'Group'),
]);
}
/**
* @return SpecificationBag
*/
public function getOutputSpecification() {
// todo get AIVL settings and add elements for specific groups...how to deal with unpredictable
// todo inputs on form processor
return new SpecificationBag([
new Specification("contact_id", "Integer", E::ts("Contact ID"), TRUE),
new Specification("contact_type", "String", E::ts("Contact Type"), FALSE),
new Specification("display_name", "String", E::ts("Contact Display Name"), FALSE),
new Specification("sort_name", "String", E::ts("Contact Sort Name"), FALSE),
new Specification("contact_id", "Integer", E::ts("Contact ID"), TRUE),
new Specification("newsletter_and_actions", "Boolean", E::ts("Maandelijkse nieuwsbrief en actie berichten"), TRUE),
new Specification("newsletter_only", "Boolean", E::ts("Alleen maandelijkse nieuwsbrief"), TRUE),
new Specification("yearly_report", "Boolean", E::ts("Alleen jaar rapport"), TRUE),
new Specification("no_general_info", "Boolean", E::ts("Geen algemene berichten"), TRUE),
new Specification("flex1", "Boolean", E::ts("Flexibel 1"), TRUE),
new Specification("flex2", "Boolean", E::ts("Flexibel 2"), TRUE),
new Specification("flex3", "Boolean", E::ts("Flexibel 3"), TRUE),
]);
}
/**
* @param ParameterBagInterface $parameters
* @param ParameterBagInterface $output
* @throws InvalidParameterException
*/
public function doAction(ParameterBagInterface $parameters, ParameterBagInterface $output) {
$contactId = (int) $parameters->getParameter('contact_id');
if ($contactId) {
$groups = $this->retrieveContactGroups($contactId);
$outputValues = $this->calculateOutputValues($contactId, $groups);
foreach ($outputValues as $key => $value) {
$output->setParameter($key, $value);
}
}
else {
throw new InvalidParameterException(E::ts("Could not find mandatory parameter contact_id"));
}
}
/**
* Method to retrieve the contact data
*
* @param $contactId
* @return array
*/
private function retrieveContactData($contactId) {
$result = [];
if (!empty($contactId)) {
try {
$result = civicrm_api3('Contact', 'getsingle', [
'return' => ["contact_type", "sort_name", "display_name", "first_name", "last_name", "organization_name", "household_name", "addressee_display"],
'id' => $contactId,
'is_deceased' => 0,
'is_deleted' => 0,
]);
$removes = ['is_error', 'hash', 'contact_is_deleted', 'is_deceased', 'id'];
foreach ($removes as $remove) {
if (isset($result[$remove])) {
unset($result[$remove]);
}
}
} catch (\CiviCRM_API3_Exception $ex) {
}
}
return $result;
}
/**
* Method to generate the webform field name from the group title
*
* @param $groupTitle
* @return mixed|string
*/
private function generateWebformName($groupTitle) {
$webformName = strtolower($groupTitle);
$webformName = str_replace(' ', '_', $webformName);
$webformName = str_replace(',', '_', $webformName);
$webformName = str_replace('.', '_', $webformName);
$webformName = str_replace(';', '_', $webformName);
$webformName = str_replace(':', '_', $webformName);
$webformName = str_replace('__', '_', $webformName);
return $webformName;
}
/**
* Method to get the contact data
*
* @param $contactId
* @param $result
*/
private function getContactData($contactId, &$result) {
$result['contact_id'] = $contactId;
$result['display_name'] = "";
$result['sort_name'] = "";
$result['contact_type'] = "";
try {
$contactData = civicrm_api3("Contact", "getsingle", [
"return" => ["display_name", "sort_name", "contact_type"],
"id" => $contactId,
]);
if ($contactData) {
$result['display_name'] = $contactData['display_name'];
$result['sort_name'] = $contactData['sort_name'];
$result['contact_type'] = $contactData['contact_type'];
}
}
catch (\CiviCRM_API3_Exception $ex) {
}
}
/**
* Method to determine the output values
*
* @param $contactId
* @param $groups
* @return array
*/
private function calculateOutputValues($contactId, $groups) {
$result = $this->defaultPreferencesForGet();
$this->getContactData($contactId, $result);
$newsGroupId = $this->configuration->getParameter("news_group_id");
$actionGroupId = $this->configuration->getParameter("action_group_id");
$yearlyGroupId = $this->configuration->getParameter("yearly_group_id");
$flex1GroupId = $this->configuration->getParameter("flex1_group_id");
$flex2GroupId = $this->configuration->getParameter("flex2_group_id");
$flex3GroupId = $this->configuration->getParameter("flex3_group_id");
foreach ($groups as $groupId) {
switch ($groupId) {
// if monthly newsletter: monthly newsletter on and no general, yearly report and monthly actions off
case $newsGroupId:
if (!$result['newsletter_and_actions']) {
$result['newsletter_only'] = "1";
$result['newsletter_and_actions'] = "0";
$result['yearly_report'] = "0";
$result['no_general_info'] = "0";
}
break;
// if monthy actions: monthly newsletter and monthly actions on, general and yearly report off
case $actionGroupId:
$result['newsletter_only'] = "0";
$result['newsletter_and_actions'] = "1";
$result['yearly_report'] = "0";
$result['no_general_info'] = "0";
break;
// if yearly report: yearly report on and monthly newsletter, monthly actions and general off
case $yearlyGroupId:
$result['newsletter_only'] = "0";
$result['newsletter_and_actions'] = "0";
$result['yearly_report'] = "1";
$result['no_general_info'] = "0";
break;
// check if any of the specifics need setting
case $flex1GroupId:
$result['no_general_info'] = "0";
$result['flex1'] = "1";
break;
case $flex2GroupId:
$result['no_general_info'] = "0";
$result['flex2'] = "1";
break;
case $flex3GroupId:
$result['no_general_info'] = "0";
$result['flex3'] = "1";
break;
}
}
return $result;
}
/**
* Method to set the default preferences for get
*/
private function defaultPreferencesForGet() {
return [
"no_general_info" => "1",
"newsletter_only" => "0",
"newsletter_and_actions" => "0",
"yearly_report" => "0",
"flex1" => "0",
"flex2" => "0",
"flex3" => "0",
];
}
/**
* @param $contactId
* @return array
*/
private function retrieveContactGroups($contactId) {
$result = [];
if (!empty($contactId)) {
try {
$groupContacts = civicrm_api3('GroupContact', 'get', [
'sequential' => 1,
'return' => ["group_id"],
'contact_id' => $contactId,
'options' => ['limit' => 0],
]);
foreach ($groupContacts['values'] as $groupContact) {
$result[] = $groupContact['group_id'];
}
}
catch (\CiviCRM_API3_Exception $ex) {
}
}
return $result;
}
}
<?php
namespace Civi\Aivlspecificactions\Actions;
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\Core\Lock\NullLock;
use Civi\FormProcessor\API\Exception;
use CRM_Aivlspecificactions_ExtensionUtil as E;
/**
* Class SetEmailPrefs - set the email preferences of a contact from form data
*
* @package Civi\Aivlspecificactions\Action
* @author Erik Hommel (CiviCooP) <erik.hommel@civicoop.org>
* @date 22 Apr 2020
* @license AGPL-3.0
*/
class SetEmailPrefs extends AbstractAction {
/**
* @return SpecificationBag
*/
public function getParameterSpecification() {
$specs = new SpecificationBag();
$specs->addSpecification(new Specification("contact_id", "Integer", E::ts("Contact ID"), TRUE, NULL));
$specs->addSpecification(new Specification("newsletter_and_actions", "Boolean", E::ts("Maandelijkse nieuwsbrief en actie berichten"), TRUE, FALSE));
$specs->addSpecification(new Specification("newsletter_only", "Boolean", E::ts("Alleen maandelijkse nieuwsbrief"), TRUE, FALSE));
$specs->addSpecification(new Specification("yearly_report", "Boolean", E::ts("Alleen jaar rapport"), TRUE, FALSE));
$specs->addSpecification(new Specification("no_general_info", "Boolean", E::ts("Geen algemene berichten"), TRUE, FALSE));
$specs->addSpecification(new Specification("flex1", "Boolean", E::ts("Flexibel 1"), FALSE, FALSE));
$specs->addSpecification(new Specification("flex2", "Boolean", E::ts("Flexibel 2"), FALSE, FALSE));
$specs->addSpecification(new Specification("flex3", "Boolean", E::ts("Flexibel 3"), FALSE, FALSE));
return $specs;
}
/**
* @return SpecificationBag
*/
public function getConfigurationSpecification() {
return new SpecificationBag([
new Specification("news_group_id", "Integer", E::ts("Maandelijkse Nieuwsbrief Groep"), TRUE, NULL, 'Group'),
new Specification("action_group_id", "Integer", E::ts("Maandelijkse Actieberichten Groep"), TRUE, NULL, 'Group'),
new Specification("yearly_group_id", "Integer", E::ts("Jaarlijks Rapport Groep"), TRUE, NULL, 'Group'),
new Specification("flex1_group_id", "Integer", E::ts("Flexibele Groep 1"), FALSE, NULL, 'Group'),
new Specification("flex2_group_id", "Integer", E::ts("Flexibele Groep 2"), FALSE, NULL, 'Group'),
new Specification("flex3_group_id", "Integer", E::ts("Flexibele Groep 3"), FALSE, NULL, 'Group'),
]);
}
/**
* @param ParameterBagInterface $parameters
* @param ParameterBagInterface $output
* @throws InvalidParameterException
*/
public function doAction(ParameterBagInterface $parameters, ParameterBagInterface $output) {
$contactId = (int) $parameters->getParameter('contact_id');
if ($contactId) {
$this->setGroupMemberships($contactId, $parameters);
}
else {
throw new InvalidParameterException(E::ts("Could not find mandatory parameter contact_id"));
}
}
/**
* @param $contactId
* @param ParameterBagInterface $parameters
*/
private function setGroupMemberships($contactId, ParameterBagInterface $parameters) {
$adds = [];
$removes = [];
$action = $parameters->getParameter('newsletter_and_actions');
$news = $parameters->getParameter('newsletter_only');
$yearly = $parameters->getParameter('yearly_report');
$noInfo = $parameters->getParameter('no_general_info');
$flex1 = $parameters->getParameter('flex1');
$flex2 = $parameters->getParameter('flex2');
$flex3 = $parameters->getParameter('flex3');
if ($noInfo || (!$action && !$news && !$yearly && !$flex1 && !$flex2 && !$flex3)) {
$removes = [
$this->configuration->getParameter('yearly_group_id'),
$this->configuration->getParameter('action_group_id'),
$this->configuration->getParameter('news_group_id'),
$this->configuration->getParameter('flex1_group_id'),
$this->configuration->getParameter('flex2_group_id'),
$this->configuration->getParameter('flex3_group_id'),
];
}
if ($yearly && !$action && !$news && !$noInfo) {
$removes = [
$this->configuration->getParameter('action_group_id'),
$this->configuration->getParameter('news_group_id'),
];
$adds = [$this->configuration->getParameter('yearly_group_id')];
}
if ($action && !$news && !$yearly && !$noInfo) {
$adds = [
$this->configuration->getParameter('action_group_id'),
$this->configuration->getParameter('news_group_id'),
];
$removes = [$this->configuration->getParameter('yearly_group_id')];
}
if ($news && !$action && !$yearly && !$noInfo) {
$adds = [$this->configuration->getParameter('news_group_id')];
$removes = [
$this->configuration->getParameter('action_group_id'),
$this->configuration->getParameter('yearly_group_id'),
];
}
// now add or remove the flexible ones
if ($flex1) {
$adds[] = $this->configuration->getParameter('flex1_group_id');
}
else {
$removes[] = $this->configuration->getParameter('flex1_group_id');
}
if ($flex2) {
$adds[] = $this->configuration->getParameter('flex2_group_id');
}
else {
$removes[] = $this->configuration->getParameter('flex2_group_id');
}
if ($flex3) {
$adds[] = $this->configuration->getParameter('flex3_group_id');
}
else {
$removes[] = $this->configuration->getParameter('flex3_group_id');
}
// now add or remove the specific ones
$this->processAddsAndRemoves($contactId, $adds, $removes);
}
/**
* Method to process the group adds and removes to reflect the email preferences
*
* @param $contactId
* @param $adds
* @param $removes
*/
private function processAddsAndRemoves($contactId, $adds, $removes) {
$contactCurrentGroupIds = [];
foreach ($adds as $addGroupId) {
try {
civicrm_api3('GroupContact', 'create', [
'group_id' => $addGroupId,
'contact_id' => $contactId,
'status' => 'Added',
]);
}
catch (CiviCRM_API3_Exception $ex) {
}
}
try {
$apiGroupContact = civicrm_api3('GroupContact', 'get', [
'sequential' => 1,
'return' => ["group_id"],
'contact_id' => $contactId,
'options' => ['limit' => 0],
]);
foreach ($apiGroupContact['values'] as $apiGroupContact) {
$contactCurrentGroupIds[] = $apiGroupContact['group_id'];
}
}
catch (CiviCRM_API3_Exception $ex) {
}
foreach ($removes as $removeGroupId) {
if (in_array($removeGroupId, $contactCurrentGroupIds)) {
try {
civicrm_api3('GroupContact', 'create', [
'group_id' => $removeGroupId,
'contact_id' => $contactId,
'status' => 'Removed',
]);
}
catch (CiviCRM_API3_Exception $ex) {
}
}
}
}
}
......@@ -19,6 +19,10 @@ class CompilerPass implements CompilerPassInterface {
['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'), []]);
$actionProviderDefinition->addMethodCall('addAction',
['GetEmailPrefs', 'Civi\Aivlspecificactions\Actions\GetEmailPrefs', E::ts('Get AIVL Email Preferences'), []]);
$actionProviderDefinition->addMethodCall('addAction',
['SetEmailPrefs', 'Civi\Aivlspecificactions\Actions\SetEmailPrefs', E::ts('Set AIVL Email Preferences'), []]);
}
}
}
......@@ -15,11 +15,15 @@
<url desc="Licensing">http://www.gnu.org/licenses/agpl-3.0.html</url>
</urls>
<releaseDate>2020-05-07</releaseDate>
<version>1.1</version>
<version>1.2</version>
<develStage>beta</develStage>
<compatibility>
<ver>4.7</ver>
</compatibility>
<requires>
<ext>action-provider</ext>
<ext>form-processor</ext>
</requires>
<classloader>
<psr4 prefix="Civi\" path="Civi" />
</classloader>
......
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