2

I have created a custom search, and it works nicely. What I would really like though is to pass default values to the custom search and it doing the initial search with those values.

The user can then change the search criteria if he/she wants another filter.

Is that possible and how can I do that?

4
  • 1
    i suspect this isn't helpful but we did a PR to get more parameters accepted by Adv Search - github.com/civicrm/civicrm-core/pull/12455
    – petednz - fuzion
    Commented Jan 29, 2020 at 20:01
  • Thanks for the comment, but I really need something like a Custom Search :-) Commented Jan 30, 2020 at 9:19
  • What about creating a smart group from your custom search ? ... and then modifying your smart group criteria ? Commented Feb 4, 2020 at 23:13
  • Hmmm might give that a try...thanks for the suggestion! Commented Feb 5, 2020 at 7:20

1 Answer 1

3

Even i wanted an easy way of doing this as well but the current architecture of CiviCRM doesn't allow it automatically. I could achieve this by copying few functions(see below) from CRM_Core_Form_Search into my custom search file

  /**
   * @return array
   */
  public function getSearchFieldMetadata() {
    $searchFieldMetadata['contact'] = [
      'sort_name' => [
        'title' => ts('Sort Name'),
        'type' => CRM_Utils_Type::T_STRING,
      ]
    ];
    return $searchFieldMetadata;
  }

  /**
   * Get the validation rule to apply to a function.
   *
   * Alphanumeric is designed to always be safe & for now we just return
   * that but in future we can use tighter rules for types like int, bool etc.
   *
   * @param string $entity
   * @param string $fieldName
   *
   * @return string
   */
  protected function getValidationTypeForField($entity, $fieldName) {
    switch ($this->getSearchFieldMetadata()[$entity][$fieldName]['type']) {
      case CRM_Utils_Type::T_BOOLEAN:
        return 'Boolean';

      case CRM_Utils_Type::T_INT:
        return 'CommaSeparatedIntegers';

      case CRM_Utils_Type::T_DATE:
      case CRM_Utils_Type::T_DATE + CRM_Utils_Type::T_TIME:
        return 'Timestamp';

      default:
        return 'Alphanumeric';
    }
  }

  /**
   * Get the defaults for the entity for any fields described in metadata.
   *
   * @param string $entity
   *
   * @return array
   *
   * @throws \CRM_Core_Exception
   */
  protected function getEntityDefaults($entity) {
    $defaults = [];
    foreach (CRM_Utils_Array::value($entity, $this->getSearchFieldMetadata(), []) as $fieldName => $fieldSpec) {
      if (empty($_POST[$fieldName])) {
        $value = CRM_Utils_Request::retrieveValue($fieldName, $this->getValidationTypeForField($entity, $fieldName), NULL, NULL, 'GET');
        if ($value !== NULL) {
          $defaults[$fieldName] = $value;
        }
        if ($fieldSpec['type'] === CRM_Utils_Type::T_DATE || ($fieldSpec['type'] === CRM_Utils_Type::T_DATE + CRM_Utils_Type::T_TIME)) {
          $low = CRM_Utils_Request::retrieveValue($fieldName . '_low', 'Timestamp', NULL, NULL, 'GET');
          $high = CRM_Utils_Request::retrieveValue($fieldName . '_high', 'Timestamp', NULL, NULL, 'GET');
          if ($low !== NULL || $high !== NULL) {
            $defaults[$fieldName . '_relative'] = 0;
            $defaults[$fieldName . '_low'] = $low ? date('Y-m-d H:i:s', strtotime($low)) : NULL;
            $defaults[$fieldName . '_high'] = $high ? date('Y-m-d H:i:s', strtotime($high)) : NULL;
          }
          else {
            $relative = CRM_Utils_Request::retrieveValue($fieldName . '_relative', 'String', NULL, NULL, 'GET');
            if (!empty($relative) && isset(CRM_Core_OptionGroup::values('relative_date_filters')[$relative])) {
              $defaults[$fieldName . '_relative'] = $relative;
            }
          }
        }
      }
    }
    return $defaults;
  }

and added below snippet in my custom search file __construct() function(at the beginning)

    $this->_force = CRM_Utils_Request::retrieve('force', 'Boolean');
    if ($this->_force) {
      foreach (array_keys($this->getSearchFieldMetadata()) as $entity) {
        $formValues = array_merge($this->getEntityDefaults($entity), $formValues);
      }
    }

HTH

Pradeep

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.