Merge pull request #12018 from civicrm/5.1
[civicrm-core.git] / CRM / Case / Form / Task.php
index a901b95090e6835e17d0f1414c059731fd4982ee..bba76dc1e6ac67c922a6718770b65efb6b8e867d 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.7                                                |
+ | CiviCRM version 5                                                  |
  +--------------------------------------------------------------------+
  | Copyright CiviCRM LLC (c) 2004-2018                                |
  +--------------------------------------------------------------------+
@@ -40,4 +40,87 @@ class CRM_Case_Form_Task extends CRM_Core_Form_Task {
   // Must be set to entity shortname (eg. event)
   static $entityShortname = 'case';
 
+  /**
+   * Deprecated copy of $_entityIds
+   *
+   * @var array
+   * @deprecated
+   */
+  public $_caseIds;
+
+  /**
+   * Build all the data structures needed to build the form.
+   */
+  public function preProcess() {
+    self::preProcessCommon($this);
+  }
+
+  /**
+   * @param CRM_Core_Form $form
+   */
+  public static function preProcessCommon(&$form) {
+    $form->_caseIds = array();
+
+    $values = $form->controller->exportValues($form->get('searchFormName'));
+
+    $form->_task = $values['task'];
+    $caseTasks = CRM_Case_Task::tasks();
+    $form->assign('taskName', $caseTasks[$form->_task]);
+
+    $ids = array();
+    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 {
+      $queryParams = $form->get('queryParams');
+      $query = new CRM_Contact_BAO_Query($queryParams, NULL, NULL, FALSE, FALSE,
+        CRM_Contact_BAO_Query::MODE_CASE
+      );
+      $query->_distinctComponentClause = " ( civicrm_case.id )";
+      $query->_groupByComponentClause = " GROUP BY civicrm_case.id ";
+      $result = $query->searchQuery(0, 0, NULL);
+      while ($result->fetch()) {
+        $ids[] = $result->case_id;
+      }
+    }
+
+    if (!empty($ids)) {
+      $form->_componentClause = ' civicrm_case.id IN ( ' . implode(',', $ids) . ' ) ';
+      $form->assign('totalSelectedCases', count($ids));
+    }
+
+    $form->_caseIds = $form->_entityIds = $form->_componentIds = $ids;
+
+    //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/case/search', $urlParams));
+    }
+    else {
+      $session->replaceUserContext(CRM_Utils_System::url("civicrm/contact/search/$searchFormName",
+        $urlParams
+      ));
+    }
+  }
+
+  /**
+   * @inheritDoc
+   */
+  public function setContactIDs() {
+    $this->_contactIds = CRM_Core_DAO::getContactIDsFromComponent($this->_entityIds,
+      'civicrm_case_contact', 'case_id'
+    );
+  }
+
 }