Skip to content

Commit

Permalink
dev/core#2066 Extract getSelectedIDs
Browse files Browse the repository at this point in the history
This is an extension of #18768

I decided moving the IF into the function & making FALSE a return option was cleaner
  • Loading branch information
eileenmcnaughton committed Oct 14, 2020
1 parent 7ad12b2 commit 56752ec
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 73 deletions.
8 changes: 3 additions & 5 deletions CRM/Activity/Form/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,9 @@ public static function preProcessCommon(&$form) {

$form->_task = $values['task'];

$ids = [];
if ($values['radio_ts'] === 'ts_sel') {
$ids = $form->getSelectedIDs($values);
}
else {
$ids = $form->getSelectedIDs($values);

if (!$ids) {
$queryParams = $form->get('queryParams');
$query = new CRM_Contact_BAO_Query($queryParams, NULL, NULL, FALSE, FALSE,
CRM_Contact_BAO_Query::MODE_ACTIVITY
Expand Down
12 changes: 3 additions & 9 deletions CRM/Campaign/Form/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,9 @@ public function preProcess() {

$this->_task = $values['task'];

$ids = [];
if ($values['radio_ts'] == 'ts_sel') {
foreach ($values as $name => $value) {
if (substr($name, 0, CRM_Core_Form::CB_PREFIX_LEN) == CRM_Core_Form::CB_PREFIX) {
$ids[] = substr($name, CRM_Core_Form::CB_PREFIX_LEN);
}
}
}
else {
$ids = $form->getSelectedIDs($values);

if (!$ids) {
$qfKey = CRM_Utils_Request::retrieve('qfKey', 'String', $this);
$cacheKey = "civicrm search {$qfKey}";
$allCids = Civi::service('prevnext')->getSelection($cacheKey, "getall");
Expand Down
7 changes: 2 additions & 5 deletions CRM/Contribute/Form/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,8 @@ public static function preProcessCommon(&$form) {

$form->_task = $values['task'] ?? NULL;

$ids = [];
if (isset($values['radio_ts']) && $values['radio_ts'] === 'ts_sel') {
$ids = $form->getSelectedIDs($values);
}
else {
$ids = $form->getSelectedIDs($values);
if (!$ids) {
$queryParams = $form->get('queryParams');
$isTest = FALSE;
if (is_array($queryParams)) {
Expand Down
19 changes: 11 additions & 8 deletions CRM/Core/Form/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,20 +101,23 @@ public function setNextUrl(string $pathPart) {
}

/**
* Get the ids the user has selected.
* Get the ids the user has selected or FALSE if selection has not been used.
*
* @param array $values
*
* @return array
* @return array|bool
*/
public function getSelectedIDs(array $values): array {
$ids = [];
foreach ($values as $name => $value) {
if (substr($name, 0, CRM_Core_Form::CB_PREFIX_LEN) == CRM_Core_Form::CB_PREFIX) {
$ids[] = substr($name, CRM_Core_Form::CB_PREFIX_LEN);
public function getSelectedIDs(array $values) {
if ($values['radio_ts'] === 'ts_sel') {
$ids = [];
foreach ($values as $name => $value) {
if (substr($name, 0, CRM_Core_Form::CB_PREFIX_LEN) == CRM_Core_Form::CB_PREFIX) {
$ids[] = substr($name, CRM_Core_Form::CB_PREFIX_LEN);
}
}
return $ids;
}
return $ids;
return FALSE;
}

/**
Expand Down
12 changes: 3 additions & 9 deletions CRM/Event/Form/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,9 @@ public static function preProcessCommon(&$form) {
CRM_Core_Error::statusBounce(ts('You do not have permission to access this page.'));
}

$ids = [];
if ($values['radio_ts'] == 'ts_sel') {
foreach ($values as $name => $value) {
if (substr($name, 0, CRM_Core_Form::CB_PREFIX_LEN) == CRM_Core_Form::CB_PREFIX) {
$ids[] = substr($name, CRM_Core_Form::CB_PREFIX_LEN);
}
}
}
else {
$ids = $form->getSelectedIDs($values);

if (!$ids) {
$queryParams = $form->get('queryParams');
$sortOrder = NULL;
if ($form->get(CRM_Utils_Sort::SORT_ORDER)) {
Expand Down
12 changes: 3 additions & 9 deletions CRM/Grant/Form/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,9 @@ public static function preProcessCommon(&$form) {
CRM_Core_Error::statusBounce(ts('You do not have permission to access this page.'));
}

$ids = [];
if ($values['radio_ts'] == 'ts_sel') {
foreach ($values as $name => $value) {
if (substr($name, 0, CRM_Core_Form::CB_PREFIX_LEN) == CRM_Core_Form::CB_PREFIX) {
$ids[] = substr($name, CRM_Core_Form::CB_PREFIX_LEN);
}
}
}
else {
$ids = $form->getSelectedIDs($values);

if (!$ids) {
$queryParams = $form->get('queryParams');
$sortOrder = NULL;
if ($form->get(CRM_Utils_Sort::SORT_ORDER)) {
Expand Down
12 changes: 3 additions & 9 deletions CRM/Mailing/Form/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,9 @@ public static function preProcessCommon(&$form) {
$form->_task = $values['task'] ?? NULL;

// ids are mailing event queue ids
$ids = [];
if ($values['radio_ts'] == 'ts_sel') {
foreach ($values as $name => $value) {
if (substr($name, 0, CRM_Core_Form::CB_PREFIX_LEN) == CRM_Core_Form::CB_PREFIX) {
$ids[] = substr($name, CRM_Core_Form::CB_PREFIX_LEN);
}
}
}
else {
$ids = $form->getSelectedIDs($values);

if (!$ids) {
$queryParams = $form->get('queryParams');
$sortOrder = NULL;
if ($form->get(CRM_Utils_Sort::SORT_ORDER)) {
Expand Down
12 changes: 3 additions & 9 deletions CRM/Member/Form/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,9 @@ public static function preProcessCommon(&$form) {
CRM_Core_Error::statusBounce(ts('You do not have permission to access this page.'));
}

$ids = [];
if ($values['radio_ts'] === 'ts_sel') {
foreach ($values as $name => $value) {
if (substr($name, 0, CRM_Core_Form::CB_PREFIX_LEN) == CRM_Core_Form::CB_PREFIX) {
$ids[] = substr($name, CRM_Core_Form::CB_PREFIX_LEN);
}
}
}
else {
$ids = $form->getSelectedIDs($values);

if (!$ids) {
$queryParams = $form->get('queryParams');
$sortOrder = NULL;
if ($form->get(CRM_Utils_Sort::SORT_ORDER)) {
Expand Down
12 changes: 3 additions & 9 deletions CRM/Pledge/Form/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,9 @@ public static function preProcessCommon(&$form) {

$form->_task = $values['task'];

$ids = [];
if ($values['radio_ts'] == 'ts_sel') {
foreach ($values as $name => $value) {
if (substr($name, 0, CRM_Core_Form::CB_PREFIX_LEN) == CRM_Core_Form::CB_PREFIX) {
$ids[] = substr($name, CRM_Core_Form::CB_PREFIX_LEN);
}
}
}
else {
$ids = $form->getSelectedIDs($values);

if (!$ids) {
$queryParams = $form->get('queryParams');
$sortOrder = NULL;
if ($form->get(CRM_Utils_Sort::SORT_ORDER)) {
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/CRM/Contribute/Form/TaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function testPreProcessCommonAfterSorting() {
}

// Assert contribIds are returned in a sorted order.
$form = $this->getFormObject('CRM_Contribute_Form_Task', [], 'Search');
$form = $this->getFormObject('CRM_Contribute_Form_Task', ['radio_ts' => 'ts_all'], 'Search');
foreach ($fields as $val) {
$form->set(CRM_Utils_Sort::SORT_ORDER, "`{$val}` asc");
CRM_Contribute_Form_Task::preProcessCommon($form);
Expand Down

0 comments on commit 56752ec

Please sign in to comment.