diff --git a/CRM/Sepaterminatemandates/Form/Task/AddToGroup.php b/CRM/Sepaterminatemandates/Form/Task/AddToGroup.php index 8a5a4b1..7fd9dab 100644 --- a/CRM/Sepaterminatemandates/Form/Task/AddToGroup.php +++ b/CRM/Sepaterminatemandates/Form/Task/AddToGroup.php @@ -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'; } @@ -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() { @@ -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 @@ -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; } diff --git a/CRM/Sepaterminatemandates/Form/Task/SearchActionDesigner.php b/CRM/Sepaterminatemandates/Form/Task/SearchActionDesigner.php index 70b614a..b805169 100644 --- a/CRM/Sepaterminatemandates/Form/Task/SearchActionDesigner.php +++ b/CRM/Sepaterminatemandates/Form/Task/SearchActionDesigner.php @@ -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'; } @@ -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) { @@ -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))); } diff --git a/CRM/Sepaterminatemandates/Form/Task/TerminateMandate.php b/CRM/Sepaterminatemandates/Form/Task/TerminateMandate.php index 172b6f4..3a3cea0 100644 --- a/CRM/Sepaterminatemandates/Form/Task/TerminateMandate.php +++ b/CRM/Sepaterminatemandates/Form/Task/TerminateMandate.php @@ -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'; } @@ -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() { @@ -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 @@ -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; } diff --git a/CRM/Sepaterminatemandates/Utils.php b/CRM/Sepaterminatemandates/Utils.php index a0e4add..78697bc 100644 --- a/CRM/Sepaterminatemandates/Utils.php +++ b/CRM/Sepaterminatemandates/Utils.php @@ -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; + } + }