Skip to content

Commit

Permalink
Fixed issue with add to group
Browse files Browse the repository at this point in the history
  • Loading branch information
jaapjansma committed Mar 2, 2023
1 parent 2d2e61e commit 56a56ed
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 61 deletions.
43 changes: 18 additions & 25 deletions CRM/Sepaterminatemandates/Form/Task/AddToGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@

class CRM_Sepaterminatemandates_Form_Task_AddToGroup extends CRM_Core_Form_Task {

protected $recordCount = 0;

protected $searchFormValues = [];

protected function setEntityShortName() {
self::$entityShortname = 'sepaterminatemandates';
}
Expand All @@ -30,23 +34,15 @@ public function preProcess() {
$url = $session->readUserContext();
$session->replaceUserContext($url);

$searchFormValues = $this->controller->exportValues($this->get('searchFormName'));
$this->_task = $searchFormValues['task'];
$this->searchFormValues = $this->controller->exportValues($this->get('searchFormName'));
$this->_task = $this->searchFormValues['task'];
$entityTasks = CRM_Sepaterminatemandates_Task::tasks();
$this->assign('taskName', $entityTasks[$this->_task]);

$entityIds = [];
if ($searchFormValues['radio_ts'] == 'ts_sel') {
foreach ($searchFormValues as $name => $value) {
if (substr($name, 0, CRM_Core_Form::CB_PREFIX_LEN) == CRM_Core_Form::CB_PREFIX) {
$entityIds[] = substr($name, CRM_Core_Form::CB_PREFIX_LEN);
}
}
} else {
$entityIds = $this->get('entityIds');
}
$this->recordCount = CRM_Sepaterminatemandates_Utils::getSelectedEntityIdCount($this->searchFormValues);
$this->_entityIds = $this->_componentIds = $entityIds;
$this->assign('status', E::ts("Number of selected records: %1", array(1=>count($this->_entityIds))));
$this->assign('status', E::ts("Number of selected records: %1", array(1=>$this->recordCount)));
}

public function buildQuickForm() {
Expand Down Expand Up @@ -164,21 +160,15 @@ public function postProcess() {
'reset' => TRUE, //do flush queue upon creation
));

$total = count($this->_entityIds);
$current = 0;
foreach($this->_entityIds as $entityId) {
$current++;
if ($current > $total) {
$current = $total;
}
$title = E::ts('Add to group %1', [1 => $current .'/'.$total]);
for($current=0; $current < $this->recordCount; $current++) {
$title = E::ts('Add to group %1', [1 => $current .'/'.$this->recordCount]);
//create a task without parameters
$task = new CRM_Queue_Task(
array(
'CRM_Sepaterminatemandates_Form_Task_AddToGroup',
'addToGroup'
), //call back method
array($entityId, $submittedValues), //parameters,
array($current, $submittedValues, $this->searchFormValues), //parameters,
$title
);
//now add this task to the queue
Expand All @@ -198,10 +188,13 @@ public function postProcess() {
$runner->runAllViaWeb(); // does not return
}

public static function addToGroup(CRM_Queue_TaskContext $ctx, int $mandateId, $formValues) {
$mandate = civicrm_api3('SepaMandate', 'getsingle', ['id' => $mandateId]);
$contactId = $mandate['contact_id'];
CRM_Contact_BAO_GroupContact::addContactsToGroup([$contactId], $formValues['group_id']);
public static function addToGroup(CRM_Queue_TaskContext $ctx, int $offset, $formValues, $searchFormValues) {
$entityIds = CRM_Sepaterminatemandates_Utils::getSelectedEntityIds($searchFormValues, $offset, 1);
foreach($entityIds as $entityId) {
$mandate = civicrm_api3('SepaMandate', 'getsingle', ['id' => $entityId]);
$contactId = $mandate['contact_id'];
CRM_Contact_BAO_GroupContact::addContactsToGroup([$contactId], $formValues['group_id']);
}
return TRUE;
}

Expand Down
22 changes: 9 additions & 13 deletions CRM/Sepaterminatemandates/Form/Task/SearchActionDesigner.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

class CRM_Sepaterminatemandates_Form_Task_SearchActionDesigner extends CRM_Searchactiondesigner_Form_Task_Task {

protected $recordCount = 0;

protected $searchFormValues = [];

protected function setEntityShortName() {
self::$entityShortname = 'sepaterminatemandates';
}
Expand All @@ -18,21 +22,13 @@ public function preProcess() {
$url = $session->readUserContext();
$session->replaceUserContext($url);

$searchFormValues = $this->controller->exportValues($this->get('searchFormName'));
$this->_task = $searchFormValues['task'];
$this->searchFormValues = $this->controller->exportValues($this->get('searchFormName'));;
$this->_task = $this->searchFormValues['task'];
$entityTasks = CRM_Sepaterminatemandates_Task::tasks();
$this->assign('taskName', $entityTasks[$this->_task]);

$entityIds = [];
if ($searchFormValues['radio_ts'] == 'ts_sel') {
foreach ($searchFormValues as $name => $value) {
if (substr($name, 0, CRM_Core_Form::CB_PREFIX_LEN) == CRM_Core_Form::CB_PREFIX) {
$entityIds[] = substr($name, CRM_Core_Form::CB_PREFIX_LEN);
}
}
} else {
$entityIds = $this->get('entityIds');
}
$this->recordCount = CRM_Sepaterminatemandates_Utils::getSelectedEntityIdCount($this->searchFormValues);
$entityIds = CRM_Sepaterminatemandates_Utils::getSelectedEntityIds($this->searchFormValues, 0, $this->recordCount);
$this->_entityIds = $this->_componentIds = $entityIds;

if (strpos($this->_task,'searchactiondesigner_') !== 0) {
Expand All @@ -42,7 +38,7 @@ public function preProcess() {

$this->searchTask = civicrm_api3('SearchTask', 'getsingle', array('id' => $this->searchTaskId));
$this->assign('searchTask', $this->searchTask);
$this->assign('status', E::ts("Number of selected records: %1", array(1=>count($this->_entityIds))));
$this->assign('status', E::ts("Number of selected records: %1", array(1=>$this->recordCount)));
}


Expand Down
39 changes: 16 additions & 23 deletions CRM/Sepaterminatemandates/Form/Task/TerminateMandate.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@

class CRM_Sepaterminatemandates_Form_Task_TerminateMandate extends CRM_Core_Form_Task {

protected $recordCount = 0;

protected $searchFormValues = [];

protected function setEntityShortName() {
self::$entityShortname = 'sepaterminatemandates';
}
Expand All @@ -30,23 +34,15 @@ public function preProcess() {
$url = $session->readUserContext();
$session->replaceUserContext($url);

$searchFormValues = $this->controller->exportValues($this->get('searchFormName'));
$this->_task = $searchFormValues['task'];
$this->searchFormValues = $this->controller->exportValues($this->get('searchFormName'));
$this->_task = $this->searchFormValues['task'];
$entityTasks = CRM_Sepaterminatemandates_Task::tasks();
$this->assign('taskName', $entityTasks[$this->_task]);

$entityIds = [];
if ($searchFormValues['radio_ts'] == 'ts_sel') {
foreach ($searchFormValues as $name => $value) {
if (substr($name, 0, CRM_Core_Form::CB_PREFIX_LEN) == CRM_Core_Form::CB_PREFIX) {
$entityIds[] = substr($name, CRM_Core_Form::CB_PREFIX_LEN);
}
}
} else {
$entityIds = $this->get('entityIds');
}
$this->recordCount = CRM_Sepaterminatemandates_Utils::getSelectedEntityIdCount($this->searchFormValues);
$this->_entityIds = $this->_componentIds = $entityIds;
$this->assign('status', E::ts("Number of selected records: %1", array(1=>count($this->_entityIds))));
$this->assign('status', E::ts("Number of selected records: %1", array(1=>$this->recordCount)));
}

public function buildQuickForm() {
Expand Down Expand Up @@ -91,21 +87,15 @@ public function postProcess() {
'reset' => TRUE, //do flush queue upon creation
));

$total = count($this->_entityIds);
$current = 0;
foreach($this->_entityIds as $entityId) {
$current++;
if ($current > $total) {
$current = $total;
}
$title = E::ts('Terminate mandate %1', [1 => $current .'/'.$total]);
for($current=0; $current < $this->recordCount; $current++) {
$title = E::ts('Terminate mandate %1', [1 => $current .'/'.$this->recordCount]);
//create a task without parameters
$task = new CRM_Queue_Task(
array(
'CRM_Sepaterminatemandates_Form_Task_TerminateMandate',
'terminateMandate'
), //call back method
array($entityId, $submittedValues), //parameters,
array($current, $submittedValues, $this->searchFormValues), //parameters,
$title
);
//now add this task to the queue
Expand All @@ -125,8 +115,11 @@ public function postProcess() {
$runner->runAllViaWeb(); // does not return
}

public static function terminateMandate(CRM_Queue_TaskContext $ctx, int $mandateId, $terminateConfiguration) {
CRM_Sepaterminatemandates_Utils::terminateMandate($mandateId, $terminateConfiguration);
public static function terminateMandate(CRM_Queue_TaskContext $ctx, int $offset, $terminateConfiguration, $searchFormValues) {
$entityIds = CRM_Sepaterminatemandates_Utils::getSelectedEntityIds($searchFormValues, $offset, 1);
foreach($entityIds as $entityId) {
CRM_Sepaterminatemandates_Utils::terminateMandate($entityId, $terminateConfiguration);
}
return TRUE;
}

Expand Down
37 changes: 37 additions & 0 deletions CRM/Sepaterminatemandates/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,41 @@ public static function terminateMandate(int $mandateId, $terminateConfiguration)
return TRUE;
}

public static function getSelectedEntityIds(array $searchFormValues, int $offset=0, int $limit=1): array {
$selectedEntityIds = [];
$entityIds = [];
if ($searchFormValues['radio_ts'] == 'ts_sel') {
foreach ($searchFormValues as $name => $value) {
if (substr($name, 0, CRM_Core_Form::CB_PREFIX_LEN) == CRM_Core_Form::CB_PREFIX) {
$entityIds[] = substr($name, CRM_Core_Form::CB_PREFIX_LEN);
}
}
for($i=$offset; $i < ($offset+$limit); $i++) {
$selectedEntityIds[] = $entityIds[$i];
}
} else {
$query = new CRM_Sepaterminatemandates_Query($searchFormValues, $offset, $limit);
foreach($query->rows() as $row) {
$selectedEntityIds[] = $row['id'];
}
}
return $selectedEntityIds;
}

public static function getSelectedEntityIdCount(array $searchFormValues): int {
$recordCount = 0;
if ($searchFormValues['radio_ts'] == 'ts_sel') {
foreach ($searchFormValues as $name => $value) {
if (substr($name, 0, CRM_Core_Form::CB_PREFIX_LEN) == CRM_Core_Form::CB_PREFIX) {
$entityIds[] = substr($name, CRM_Core_Form::CB_PREFIX_LEN);
}
}
$recordCount = count($entityIds);
} else {
$query = new CRM_Sepaterminatemandates_Query($searchFormValues, 0, 1);
$recordCount = $query->count();
}
return $recordCount;
}

}

0 comments on commit 56a56ed

Please sign in to comment.