Skip to content
Snippets Groups Projects
Commit b8f4b27c authored by jaapjansma's avatar jaapjansma
Browse files

Added action for opt out

parent 07f68d13
No related branches found
No related tags found
No related merge requests found
<?php
/**
* Copyright (C) 2022 Jaap Jansma (jaap.jansma@civicoop.org)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
namespace Civi\Aivlspecificactions\Actions;
use Civi\ActionProvider\Action\AbstractAction;
use Civi\ActionProvider\Parameter\ParameterBagInterface;
use Civi\ActionProvider\Parameter\Specification;
use Civi\ActionProvider\Parameter\SpecificationBag;
use CRM_Aivlspecificactions_ExtensionUtil as E;
class Optout extends AbstractAction {
/**
* Run the action
*
* @param ParameterBagInterface $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) {
$contacts = $this->getAllContacts($parameters->getParameter('email'));
foreach ($contacts as $contact) {
if (empty($contact['is_opt_out'])) {
// not set yet:
civicrm_api3('Contact', 'create', array(
'check_permissions' => 0,
'id' => $contact['id'],
'is_opt_out' => 1));
}
}
if ($parameters->getParameter('template_id') && count($contacts)) {
$this->sendOptoutMail($contacts, $parameters->getParameter('email'), $parameters->getParameter('template_id'), $this->configuration->getParameter('from_email'), $this->configuration->getParameter('reply_to'), $parameters->getParameter('bcc'));
}
if ($parameters->getParameter('hash') && $parameters->getParameter('job_id') && $parameters->getParameter('event_queue_id')) {
// Record unsubscribe event at the mailing.
$q = \CRM_Mailing_Event_BAO_Queue::verify($parameters->getParameter('job_id'), $parameters->getParameter('event_queue_id'), $parameters->getParameter('hash'));
if ($q) {
$ue = new \CRM_Mailing_Event_BAO_Unsubscribe();
$ue->event_queue_id = $q->id;
$ue->org_unsubscribe = 1;
$ue->time_stamp = date('YmdHis');
$ue->save();
}
}
}
/**
* Returns the specification of the configuration options for the actual
* action.
*
* @return SpecificationBag
*/
public function getConfigurationSpecification() {
return new SpecificationBag([
new Specification('from_email', 'String', E::ts('From E-mail'), TRUE),
new Specification('reply_to', 'String', E::ts('Reply To'), TRUE),
]);
}
/**
* Returns the specification of the parameters of the actual action.
*
* @return SpecificationBag
*/
public function getParameterSpecification() {
return new SpecificationBag([
new Specification('email', 'String', E::ts('E-mail'), TRUE),
new Specification('template_id', 'Integer', E::ts('OptOut Message Template Id'), FALSE),
new Specification('bcc', 'String', E::ts('BCC'), FALSE),
new Specification('hash', 'String', E::ts('Hash'), FALSE),
new Specification('job_id', 'Integer', E::ts('Job Id'), FALSE),
new Specification('event_queue_id', 'Integer', E::ts('Event Queue ID'), FALSE),
]);
}
private function getAllContacts($email) {
$contacts = [];
// first, get all email entities
$contact_ids = array();
$email_query = civicrm_api3('Email', 'get', array(
'check_permissions' => 0,
'email' => trim($email),
'option.limit' => 0,
'return' => 'contact_id'));
foreach ($email_query['values'] as $email) {
$contact_ids[] = $email['contact_id'];
}
if (empty($contact_ids)) {
return $contacts;
}
// now find all active contacts
$contact_query = civicrm_api3('Contact', 'get', array(
'check_permissions' => 0,
'id' => array('IN' => $contact_ids),
'option.limit' => 0,
'is_deleted' => 0,
'return' => 'id,display_name,is_opt_out'));
foreach ($contact_query['values'] as $contact) {
$contacts[] = $contact;
}
return $contacts;
}
private function sendOptoutMail($contacts, $email, $template_id, $from, $reply_to, $bcc=null) {
// compile send params
$optout_confirmation = array(
'check_permissions' => 0,
'id' => (int) $template_id,
'contact_id' => $contacts[0]['id'],
'to_name' => $contacts[0]['display_name'],
'to_email' => $email,
'from' => 'info@amnesty-international.be',
'reply_to' => "do-not-reply@amnesty-international.be",
);
// add bcc if any
if ($bcc) {
$optout_confirmation['bcc'] = $bcc;
}
// send the thing
return civicrm_api3('MessageTemplate', 'send', $optout_confirmation);
}
}
......@@ -32,6 +32,8 @@ class CompilerPass implements CompilerPassInterface {
['UpdateCallAssignment', 'Civi\Aivlspecificactions\Actions\UpdateCallAssignment', E::ts('Update AIVL TM Call Assignment'), []]);
$actionProviderDefinition->addMethodCall('addAction',
['MultiPetition', 'Civi\Aivlspecificactions\Actions\MultiPetition', E::ts('Proces multiple AIVL petitions'), []]);
$actionProviderDefinition->addMethodCall('addAction',
['AivlOptOut', 'Civi\Aivlspecificactions\Actions\OptOut', E::ts('Mailing Event: Opt out (AIVL)'), []]);
}
if ($container->hasDefinition('form_processor_type_factory')) {
$typeFactoryDefinition = $container->getDefinition('form_processor_type_factory');
......
......@@ -14,8 +14,8 @@
<url desc="Support">https://civicoop.org</url>
<url desc="Licensing">http://www.gnu.org/licenses/agpl-3.0.html</url>
</urls>
<releaseDate>2021-11-17</releaseDate>
<version>1.19</version>
<releaseDate>2022-05-25</releaseDate>
<version>1.20</version>
<develStage>stable</develStage>
<compatibility>
<ver>4.7</ver>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment