Extract setNextUrl
authoreileen <emcnaughton@wikimedia.org>
Tue, 13 Oct 2020 07:05:59 +0000 (20:05 +1300)
committereileen <emcnaughton@wikimedia.org>
Tue, 13 Oct 2020 07:05:59 +0000 (20:05 +1300)
This code is repeated all over the task sub system. This is a first attempt to
extract it on the parent class in order to start consolidating it

CRM/Core/Form/Task.php

index 2bc0033da6707fe65734b9516ccb6b6349148583..2c87a8ccbb92067970de5eeac75890750874cd5b 100644 (file)
@@ -73,6 +73,33 @@ abstract class CRM_Core_Form_Task extends CRM_Core_Form {
    */
   public static $entityShortname = NULL;
 
+  /**
+   * Set where the browser should be directed to next.
+   *
+   * @param string $pathPart
+   *
+   * @throws \CRM_Core_Exception
+   */
+  protected function setNextUrl(string $pathPart) {
+    //set the context for redirection for any task actions
+    $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String', $this);
+    $urlParams = 'force=1';
+    if (CRM_Utils_Rule::qfKey($qfKey)) {
+      $urlParams .= "&qfKey=$qfKey";
+    }
+
+    $session = CRM_Core_Session::singleton();
+    $searchFormName = strtolower($this->get('searchFormName'));
+    if ($searchFormName === 'search') {
+      $session->replaceUserContext(CRM_Utils_System::url('civicrm/' . $pathPart . '/search', $urlParams));
+    }
+    else {
+      $session->replaceUserContext(CRM_Utils_System::url("civicrm/contact/search/$searchFormName",
+        $urlParams
+      ));
+    }
+  }
+
   /**
    * Build all the data structures needed to build the form.
    *
@@ -133,24 +160,8 @@ abstract class CRM_Core_Form_Task extends CRM_Core_Form {
     // FIXME: This is really to handle legacy code that should probably be updated to use $form->_entityIds
     $entitySpecificIdsName = '_' . $form::$entityShortname . 'Ids';
     $form->$entitySpecificIdsName = $form->_entityIds;
+    $form->setNextUrl($form::$entityShortname);
 
-    //set the context for redirection for any task actions
-    $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String', $form);
-    $urlParams = 'force=1';
-    if (CRM_Utils_Rule::qfKey($qfKey)) {
-      $urlParams .= "&qfKey=$qfKey";
-    }
-
-    $session = CRM_Core_Session::singleton();
-    $searchFormName = strtolower($form->get('searchFormName'));
-    if ($searchFormName == 'search') {
-      $session->replaceUserContext(CRM_Utils_System::url('civicrm/' . $form::$entityShortname . '/search', $urlParams));
-    }
-    else {
-      $session->replaceUserContext(CRM_Utils_System::url("civicrm/contact/search/$searchFormName",
-        $urlParams
-      ));
-    }
   }
 
   /**