Commit 48710956 authored by ErikHommel's avatar ErikHommel
Browse files

fix issue 7746 multi petition with primary campaign id

parent 13bb6fd4
<?php
/**
* Class for extension specific utility functions
*
* @author Erik Hommel (CiviCooP) <erik.hommel@civicoop.org>
* @date 24 Mar 2021
* @license AGPL-3.0
*/
class CRM_Aivlspecificactions_Utils {
/**
* Method to return only numeric digits of phone
*
* @param $phone
* @return string
*/
public static function useOnlyNumericPhone($phone) {
$length = strlen($phone);
$onlyNumerics = [];
for ($x = 0; $x <= $length; $x++) {
$digit = substr($phone, $x, 1);
if (is_numeric($digit)) {
$onlyNumerics[] = $digit;
}
}
return implode("", $onlyNumerics);
}
/**
* Method to get the default location type id
*
* @return array|false
*/
public static function getDefaultLocationTypeId() {
try {
$locationTypeId = civicrm_api3('LocationType', 'getvalue', [
'return' => "id",
'is_default' => 1,
]);
if ($locationTypeId) {
return $locationTypeId;
}
}
catch (CiviCRM_API3_Exception $ex) {
}
return 1;
}
/**
* Method to set the is primary for an entity (1 if no primary found yet for contact, else 0)
*
* @param $entity
* @param $contactId
* @return string
*/
public static function setIsPrimary($entity, $contactId) {
try {
$count = civicrm_api3(ucfirst(strtolower($entity)), 'getcoun', [
'contact_id' => $contactId,
'is_primary' => "1",
]);
if ($count == 0) {
return "1";
}
}
catch (CiviCRM_API3_Exception $ex) {
}
return "0";
}
}
......@@ -30,6 +30,7 @@ class MultiPetition extends AbstractAction {
$specs = new SpecificationBag();
$specs->addSpecification(new Specification('petition_ids', 'Integer', E::ts('Petition(s)'), FALSE, NULL, NULL, [], TRUE));
$specs->addSpecification(new Specification('contact_id', 'Integer', E::ts('Contact ID'), TRUE, NULL));
$specs->addSpecification(new Specification('campaign_id', 'Integer', E::ts('Primary Campaign ID'), FALSE, NULL));
$specs->addSpecification(new Specification('more_information', 'Integer', E::ts('More Information'), FALSE, 0));
return $specs;
......@@ -50,29 +51,51 @@ class MultiPetition extends AbstractAction {
* @throws ExecutionException
*/
public function doAction(ParameterBagInterface $parameterBag, ParameterBagInterface $output) {
$primaryCampaignId = (int) $parameterBag->getParameter('campaign_id');
$contactId = (int) $parameterBag->getParameter('contact_id');
$moreInformation = $parameterBag->getParameter('more_information');
if ($primaryCampaignId) {
$this->signPetition($primaryCampaignId, $contactId, $moreInformation, 0);
}
$petitionIds = $parameterBag->getParameter('petition_ids');
if ($petitionIds) {
$contactId = (int) $parameterBag->getParameter('contact_id');
$moreInformation = $parameterBag->getParameter('more_information');
foreach ($petitionIds as $petitionId) {
try {
$result = civicrm_api3('FormProcessor', 'petition_form', [
'petition_flag' => 1,
'campaign_id' => (int) $petitionId,
'contact_id' => $contactId,
'more_information' => $moreInformation,
]);
\Civi::log()->debug("Result is " . json_encode($result));
}
catch (\CiviCRM_API3_Exception $ex) {
\Civi::log()->error(E::ts("Could not process petition signature in ") . __METHOD__
. E::ts(" with petition ID: ") . $petitionId . E::ts(", contact ID: ") . $contactId
. E::ts(" and more information: ") . $moreInformation);
}
$this->signPetition($petitionId, $contactId, $moreInformation, $primaryCampaignId);
}
}
}
/**
* Method to sign the petition
*
* @param int $petitionId
* @param int $contactId
* @param bool $moreInformation
* @param int $primaryCampaignId
*/
private function signPetition(int $petitionId, int $contactId, bool $moreInformation, int $primaryCampaignId) {
try {
$petitionParams = [
'petition_flag' => 1,
'campaign_id' => (int) $petitionId,
'contact_id' => $contactId,
'more_information' => $moreInformation,
];
if ($primaryCampaignId) {
$petitionParams['primary_campaign_id'] = $primaryCampaignId;
}
\civicrm_api3('FormProcessor', 'petition_form', $petitionParams);
}
catch (\CiviCRM_API3_Exception $ex) {
\Civi::log()->error(E::ts("Could not process petition signature in ") . __METHOD__
. E::ts(" with petition ID: ") . $petitionId . E::ts(", contact ID: ") . $contactId
. E::ts(" and more information: ") . $moreInformation);
}
}
private function getPrimaryCampaignFieldName() {
}
/**
* Returns the specification of the output parameters of this action.
*
......
......@@ -36,6 +36,7 @@ class PetitionActivity extends AbstractAction {
$specs->addSpecification(new Specification('location', 'String', E::ts('Location'), FALSE, NULL));
$specs->addSpecification(new Specification('metadata_event', 'String', E::ts('Petition metadata->event'), FALSE, NULL));
$specs->addSpecification(new Specification('medium_id', 'Integer', E::ts('Activity Medium'), FALSE, NULL));
$specs->addSpecification(new Specification('primary_campaign_id', 'Integer', E::ts('Primary campaign'), FALSE, NULL));
return $specs;
}
......@@ -180,6 +181,14 @@ class PetitionActivity extends AbstractAction {
if ($mediumId) {
$activityParams['medium_id'] = $mediumId;
}
// if primary campaign has value, find custom field and add to params
$primaryCampaignId = $parameterBag->getParameter('primary_campaign_id');
if ($primaryCampaignId) {
$customFieldId = $this->getPrimaryCampaignIdCustomFieldId();
if ($customFieldId) {
$activityParams["custom_" . $customFieldId] = $primaryCampaignId;
}
}
try {
$created = civicrm_api3('Activity', 'create', $activityParams);
$output->setParameter('activity_id', $created['id']);
......@@ -190,6 +199,47 @@ class PetitionActivity extends AbstractAction {
}
}
/**
* Method to get the custom field id of the primary petition field
*
* @return array|mixed|void
*/
private function getPrimaryCampaignIdCustomFieldId() {
if (function_exists('civicrm_api4')) {
try {
$customFields = \Civi\Api4\CustomField::get()
->addSelect('id')
->addWhere('custom_group_id:name', '=', 'Petities_metadata')
->addWhere('name', '=', 'aivl_primary_petition')
->execute();
$customField = $customFields->first();
if (isset($customField['id'])) {
return $customField['id'];
}
}
catch (\API_Exception $ex) {
\Civi::log()->error(E::ts("Could not find custom field for primary petition in ") . __METHOD__
. E::ts(", error from API4 CustomField get: ") . $ex->getMessage());
}
}
else {
try {
$customFieldId = civicrm_api3('CustomField', 'getvalue', [
'return' => "id",
'custom_group_id' => "Petities_metadata",
'name' => "aivl_primary_petition",
]);
if ($customFieldId) {
return $customFieldId;
}
}
catch (\CiviCRM_API3_Exception $ex) {
\Civi::log()->error(E::ts("Could not find custom field for primary petition in ") . __METHOD__
. E::ts(", error from API3 CustomField getvalue: ") . $ex->getMessage());
}
}
}
/**
* Method to get custom field id for custom field Event in Petities Metadata
*
......
......@@ -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-10-21</releaseDate>
<version>1.17</version>
<releaseDate>2021-11-09</releaseDate>
<version>1.18</version>
<develStage>stable</develStage>
<compatibility>
<ver>4.7</ver>
......
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