Commit 8d7f6afe authored by ErikHommel's avatar ErikHommel
Browse files

issue 7740 petition action updated for petition import

parent 36ad24ec
......@@ -32,8 +32,6 @@ class PetitionActivity extends AbstractAction {
$specs->addSpecification(new Specification('contact_id', 'Integer', E::ts('Contact ID'), TRUE, NULL));
$specs->addSpecification(new Specification('petition_flag', 'Integer', E::ts('Petition Flag'), FALSE, 0));
$specs->addSpecification(new Specification('more_information', 'Integer', E::ts('More Information'), FALSE, 0));
$specs->addSpecification(new Specification('phone', 'String', E::ts('Phone'), FALSE, NULL));
$specs->addSpecification(new Specification('mobile', 'String', E::ts('Mobile'), FALSE, NULL));
$specs->addSpecification(new Specification('signature_date', 'Date', E::ts('Signature Date'), FALSE, NULL));
return $specs;
}
......@@ -66,98 +64,31 @@ class PetitionActivity extends AbstractAction {
/**
* Do the actual action - process petition signature
*
* @param ParameterBagInterface $parameters
* @param ParameterBagInterface $parameterBag
* @param ParameterBagInterface $output
* @throws ExecutionException
*/
public function doAction(ParameterBagInterface $parameters, ParameterBagInterface $output) {
// update phones
$this->updatePhones($parameters);
if (!$this->signedPetitionEarlier($parameters)) {
$this->addPetitionActivity($parameters, $output);
public function doAction(ParameterBagInterface $parameterBag, ParameterBagInterface $output) {
if (!$this->signedPetitionEarlier($parameterBag)) {
$this->addPetitionActivity($parameterBag, $output);
// temporarily: add to Nieuwsbrief group if more information ticked
$this->addNieuwsbrief($parameters);
$this->addNieuwsbrief($parameterBag);
}
else {
throw new ExecutionException(E::ts('Petition was already signed by this contact'));
}
}
/**
* Method to update phones if required
* @param ParameterBagInterface $parameters
*/
private function updatePhones(ParameterBagInterface $parameters) {
$phone = $parameters->getParameter('phone');
$mobile = $parameters->getParameter('mobile');
if ($phone || $mobile) {
$contactId = $parameters->getParameter('contact_id');
try {
$phoneTypes = civicrm_api3('OptionValue', 'get', [
'sequential' => 1,
'return' => ["value", "name"],
'option_group_id' => "phone_type",
'name' => ['IN' => ["Phone", "Mobile"]],
]);
foreach ($phoneTypes['values'] as $phoneType) {
switch ($phoneType['name']) {
case "Phone":
if ($phone) {
$this->addPhone($contactId, $phoneType['value'], $phone);
}
break;
case "Mobile":
if ($mobile) {
$this->addPhone($contactId, $phoneType['value'], $mobile);
}
break;
}
}
}
catch (\CiviCRM_API3_Exception $ex) {
}
}
}
/**
* Method to add single phone if not exists for contact
*
* @param $contactId
* @param $phoneTypeId
* @param $phone
* @throws \CiviCRM_API3_Exception
*/
private function addPhone($contactId, $phoneTypeId, $phone) {
$phoneNumeric = \CRM_Aivlspecificactions_Utils::useOnlyNumericPhone($phone);
// first check if phone already exists
$query = "SELECT COUNT(*) FROM civicrm_phone WHERE contact_id = %1 AND phone_type_id = %2
AND phone_numeric = %3";
$count = \CRM_Core_DAO::singleValueQuery($query, [
1 => [(int) $contactId, "Integer"],
2 => [(int) $phoneTypeId, "Integer"],
3 => [(int) $phoneNumeric, "Integer"],
]);
if ($count == 0) {
civicrm_api3('Phone', 'create', [
'contact_id' => $contactId,
'location_type_id' => \CRM_Aivlspecificactions_Utils::getDefaultLocationTypeId(),
'phone_type_id' => $phoneTypeId,
'phone' => $phone,
'is_primary' => \CRM_Aivlspecificactions_Utils::setIsPrimary('phone', $contactId),
]);
}
}
/**
* Method to check if the contact already signed this petition
*
* @param $parameters
* @param $parameterBag
* @return bool
* @throws
*/
private function signedPetitionEarlier($parameters) {
$contactId = $parameters->getParameter('contact_id');
$campaignId = $parameters->getParameter('campaign_id');
private function signedPetitionEarlier(ParameterBagInterface $parameterBag) {
$contactId = $parameterBag->getParameter('contact_id');
$campaignId = $parameterBag->getParameter('campaign_id');
$activityTypeId = $this->configuration->getParameter('activity_type_id');
if ($contactId && $campaignId && $activityTypeId) {
$count = civicrm_api3('Activity', 'getcount', [
......@@ -176,13 +107,13 @@ class PetitionActivity extends AbstractAction {
* Method to add contact to newsletter group (temp until we fixed email preferences
*
* @link https://issues.civicoop.org/issues/7337)
* @param ParameterBagInterface $parameters
* @param ParameterBagInterface $parameterBag
* @throws \CiviCRM_API3_Exception
*/
private function addNieuwsbrief(ParameterBagInterface $parameters) {
private function addNieuwsbrief(ParameterBagInterface $parameterBag) {
$groupId = $this->configuration->getParameter('news_group_id');
$moreInfo = $parameters->getParameter('more_information');
$contactId = $parameters->getParameter('contact_id');
$moreInfo = $parameterBag->getParameter('more_information');
$contactId = $parameterBag->getParameter('contact_id');
if ($moreInfo && $groupId && $contactId) {
// first check if contact is already in group, if not add
$count = civicrm_api3('GroupContact', 'getcount', [
......@@ -204,24 +135,24 @@ class PetitionActivity extends AbstractAction {
}
/**
* @param ParameterBagInterface $parameters
* @param ParameterBagInterface $parameterBag
* @param ParameterBagInterface $output
* @throws ExecutionException
*/
private function addPetitionActivity(ParameterBagInterface $parameters, ParameterBagInterface $output) {
$campaignId = $parameters->getParameter('campaign_id');
$contactId = $parameters->getParameter('contact_id');
$petitionFlag = $parameters->getParameter('petition_flag');
private function addPetitionActivity(ParameterBagInterface $parameterBag, ParameterBagInterface $output) {
$campaignId = $parameterBag->getParameter('campaign_id');
$contactId = $parameterBag->getParameter('contact_id');
$petitionFlag = $parameterBag->getParameter('petition_flag');
if ($campaignId && $contactId && $petitionFlag) {
$activityParams = [
'source_contact_id' => 'user_contact_id',
'target_id' => $parameters->getParameter('contact_id'),
'target_id' => $parameterBag->getParameter('contact_id'),
'campaign_id' => $campaignId,
'activity_type_id' => $this->configuration->getParameter('activity_type_id'),
'status_id' => $this->configuration->getParameter('status_id'),
'subject' => $this->getCampaignTitle($campaignId),
];
$signatureDate = $parameters->getParameter('signature_date');
$signatureDate = $parameterBag->getParameter('signature_date');
if ($signatureDate) {
try {
$activityDate = new \DateTime($signatureDate);
......
......@@ -14,7 +14,7 @@
<url desc="Support">https://civicoop.org</url>
<url desc="Licensing">http://www.gnu.org/licenses/agpl-3.0.html</url>
</urls>
<releaseDate>2021-03-24</releaseDate>
<releaseDate>2021-05-03</releaseDate>
<version>1.14</version>
<develStage>stable</develStage>
<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