Commit 3b09ad21 authored by ErikHommel's avatar ErikHommel 🏘
Browse files

fix issue 6544 for membership renew form with link

parent 8006a46d
<?php
namespace Civi\Aivlspecificactions\Actions;
use \Civi\ActionProvider\Action\AbstractAction;
use Civi\ActionProvider\Action\Membership\Parameter\MembershipTypeSpecification;
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;
use Civi\Core\Lock\NullLock;
use Civi\FormProcessor\API\Exception;
use CRM_Aivlspecificactions_ExtensionUtil as E;
/**
* Class ExtendMembership - find latest lid membership for contact and 1 year to end date
* @package Civi\Aivlspecificactions\Actions
* @author Erik Hommel (CiviCooP) <erik.hommel@civicoop.org>
* @date 25 Jun 2020
* @license AGPL-3.0
*/
class ExtendMembership extends AbstractAction {
/**
* @return SpecificationBag
*/
public function getParameterSpecification() {
$specs = new SpecificationBag();
$specs->addSpecification(new Specification('contact_id', 'Integer', E::ts('Contact ID'), TRUE, NULL));
return $specs;
}
/**
* @return SpecificationBag
*/
public function getConfigurationSpecification() {
$employees = \Civi::service('aivlgeneric')->getAivlEmployees();
return new SpecificationBag([
new MembershipTypeSpecification('membership_type_id', E::ts('Membership Type'), TRUE),
new Specification("error_contact_id", "Integer", E::ts("Employee for To Check Activity"), TRUE, NULL, NULL, $employees),
]);
}
/**
* Do the actual action - process petition signature
*
* @param ParameterBagInterface $parameters
* @param ParameterBagInterface $output
* @throws ExecutionException
*/
public function doAction(ParameterBagInterface $parameters, ParameterBagInterface $output) {
$membershipData = $this->getMembershipData($parameters, $output);
if ($membershipData) {
$this->extendMembership($membershipData, $parameters, $output);
}
}
/**
* @param ParameterBagInterface $parameters
* @param ParameterBagInterface $output
* @return array|bool
* @throws ExecutionException
*/
private function getMembershipData(ParameterBagInterface $parameters, ParameterBagInterface $output) {
$contactId = $parameters->getParameter('contact_id');
if ($contactId) {
try {
$membershipData = civicrm_api3('Membership', 'getsingle', [
'return' => ["id", "end_date"],
'contact_id' => (int) $contactId,
'membership_type_id' => $this->configuration->getParameter('membership_type_id'),
'options' => ['sort' => "start_date DESC", 'limit' => 1],
]);
if ($membershipData) {
return $membershipData;
}
}
catch (\CiviCRM_API3_Exception $ex) {
$errorActivityData = [
'assignee_id' => $this->configuration->getParameter('error_contact_id'),
'subject' => E::ts("Error extending membership: no membership found for contact ID ") . $contactId,
'details' => E::ts("Error message from API Membership getsingle: ") . $ex->getMessage(),
];
\Civi::service('aivlgeneric')->createToCheckActivity($errorActivityData);
return FALSE;
}
}
}
/**
* @param $membershipData
* @param ParameterBagInterface $parameters
* @param ParameterBagInterface $output
*
*/
private function extendMembership($membershipData, ParameterBagInterface $parameters, ParameterBagInterface $output) {
$contactId = $parameters->getParameter('contact_id');
// extend the membership by one year:
$newEndDate = strtotime('now');
$newEndDate = strtotime(date("Y-m-01"), $newEndDate);
$newEndDate = strtotime("+ 1 year", $newEndDate);
// update the membership or create
try {
civicrm_api3('Membership', 'create', [
'id' => $membershipData['id'],
'end_date' => date('Ymd', $newEndDate),
'membership_type_id' => $this->configuration->getParameter('membership_type_id'),
'contact_id' => $contactId,
]);
$output->setParameter('membership_id', $membershipData['id']);
$output->setParameter('new_end_date', date('d-m-Y', $newEndDate));
}
catch (\CiviCRM_API3_Exception $ex) {
$errorActivityData = [
'assignee_id' => $this->configuration->getParameter('error_contact_id'),
'subject' => E::ts("Error extending membership: error extending membership for contact ID ") . $contactId,
'details' => E::ts("Error message from API Membership create: ") . $ex->getMessage(),
];
\Civi::service('aivlgeneric')->createToCheckActivity($errorActivityData);
}
}
/**
* Returns the specification of the output parameters of this action.
*
* @return SpecificationBag
*/
public function getOutputSpecification() {
return new SpecificationBag([
new Specification('membership_id', 'Integer', E::ts('Membership ID'), FALSE),
new Specification('new_end_date', 'Date', E::ts('New Membership End Date'), FALSE),
]);
}
}
<?php
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;
use Civi\Core\Lock\NullLock;
use Civi\FormProcessor\API\Exception;
use CRM_Aivlspecificactions_ExtensionUtil as E;
/**
* Class LifeSignActivity - create activity for sign of life activity when renewing membership
* @package Civi\Aivlspecificactions\Actions
* @author Erik Hommel (CiviCooP) <erik.hommel@civicoop.org>
* @date 24 Jun 2020
* @license AGPL-3.0
*/
class LifeSignActivity extends AbstractAction {
/**
* @return SpecificationBag
*/
public function getParameterSpecification() {
$specs = new SpecificationBag();
$specs->addSpecification(new Specification('contact_id', 'Integer', E::ts('Contact ID'), TRUE, NULL));
return $specs;
}
/**
* @return SpecificationBag
*/
public function getConfigurationSpecification() {
$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) {
$activityId = $this->addLifeSignActivity($parameters, $output);
if ($activityId) {
$output->setParameter('activity_id', (int) $activityId);
}
}
/**
* @param ParameterBagInterface $parameters
* @param ParameterBagInterface $output
* @throws ExecutionException
*/
private function addLifeSignActivity(ParameterBagInterface $parameters, ParameterBagInterface $output) {
$contactId = $parameters->getParameter('contact_id');
if ($contactId) {
$activityParams = [
'source_contact_id' => 'user_contact_id',
'target_id' => $parameters->getParameter('contact_id'),
'activity_type_id' => $this->configuration->getParameter('activity_type_id'),
'status_id' => $this->configuration->getParameter('status_id'),
'subject' => "Ik blijf lid",
];
try {
$created = civicrm_api3('Activity', 'create', $activityParams);
return $created['id'];
}
catch (\CiviCRM_API3_Exception $ex) {
throw new ExecutionException(E::ts('Could not create sign of life activity, error message from API Activity create: ') . $ex->getMessage());
}
}
}
/**
* 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([
new Specification('activity_id', 'Integer', E::ts('Sign of Life Activity ID'), TRUE)
]);
}
}
......@@ -15,6 +15,10 @@ class CompilerPass implements CompilerPassInterface {
public function process(ContainerBuilder $container) {
if ($container->hasDefinition('action_provider')) {
$actionProviderDefinition = $container->getDefinition('action_provider');
$actionProviderDefinition->addMethodCall('addAction',
['ExtendMembership', 'Civi\Aivlspecificactions\Actions\ExtendMembership', E::ts('Extend AIVL Free Membership'), []]);
$actionProviderDefinition->addMethodCall('addAction',
['LifeSignActivity', 'Civi\Aivlspecificactions\Actions\LifeSignActivity', E::ts('Add AIVL Sign of Life Activity'), []]);
$actionProviderDefinition->addMethodCall('addAction',
['PetitionActivity', 'Civi\Aivlspecificactions\Actions\PetitionActivity', E::ts('Add AIVL Petition Activity'), []]);
$actionProviderDefinition->addMethodCall('addAction',
......
......@@ -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>2020-05-19</releaseDate>
<version>1.3</version>
<releaseDate>2020-06-25</releaseDate>
<version>1.4</version>
<develStage>beta</develStage>
<compatibility>
<ver>4.7</ver>
......@@ -23,6 +23,7 @@
<requires>
<ext>action-provider</ext>
<ext>form-processor</ext>
<ext>aivlgeneric</ext>
</requires>
<classloader>
<psr4 prefix="Civi\" path="Civi" />
......
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