Merge pull request #22884 from civicrm/5.47
[civicrm-core.git] / CRM / Member / Form / Task.php
index 6718d6eaebf1562e55e69fca363a4ea8b58cf965..756355c1e949a5944a9779dd6e43a6555bd88cee 100644 (file)
@@ -15,6 +15,8 @@
  * @copyright CiviCRM LLC https://civicrm.org/licensing
  */
 
+use Civi\Api4\Membership;
+
 /**
  * Class for member form task actions.
  * FIXME: This needs refactoring to properly inherit from CRM_Core_Form_Task and share more functions.
@@ -56,15 +58,9 @@ class CRM_Member_Form_Task extends CRM_Core_Form_Task {
       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)) {
@@ -88,25 +84,7 @@ class CRM_Member_Form_Task extends CRM_Core_Form_Task {
     }
 
     $form->_memberIds = $form->_componentIds = $ids;
-
-    //set the context for redirection for any task actions
-    $session = CRM_Core_Session::singleton();
-
-    $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String', $form);
-    $urlParams = 'force=1';
-    if (CRM_Utils_Rule::qfKey($qfKey)) {
-      $urlParams .= "&qfKey=$qfKey";
-    }
-
-    $searchFormName = strtolower($form->get('searchFormName'));
-    if ($searchFormName === 'search') {
-      $session->replaceUserContext(CRM_Utils_System::url('civicrm/member/search', $urlParams));
-    }
-    else {
-      $session->replaceUserContext(CRM_Utils_System::url("civicrm/contact/search/$searchFormName",
-        $urlParams
-      ));
-    }
+    $form->setNextUrl('member');
   }
 
   /**
@@ -119,4 +97,45 @@ class CRM_Member_Form_Task extends CRM_Core_Form_Task {
     );
   }
 
+  /**
+   * @return array
+   */
+  protected function getIDS() {
+    return $this->_memberIds;
+  }
+
+  /**
+   * Get the rows form the search, keyed to make the token processor happy.
+   *
+   * @throws \API_Exception
+   */
+  protected function getRows(): array {
+    if (empty($this->rows)) {
+      // checkPermissions set to false - in case form is bypassing in some way.
+      $memberships = Membership::get(FALSE)
+        ->addWhere('id', 'IN', $this->getIDs())
+        ->setSelect(['id', 'contact_id'])->execute();
+      foreach ($memberships as $membership) {
+        $this->rows[] = [
+          'contact_id' => $membership['contact_id'],
+          'membership_id' => $membership['id'],
+          'schema' => [
+            'contactId' => $membership['contact_id'],
+            'membershipId' => $membership['id'],
+          ],
+        ];
+      }
+    }
+    return $this->rows;
+  }
+
+  /**
+   * Get the token processor schema required to list any tokens for this task.
+   *
+   * @return array
+   */
+  public function getTokenSchema(): array {
+    return ['membershipId', 'contactId'];
+  }
+
 }