0

I want to sort the Activity Types that appear in the dropdown menue in CaseView, when clicking "Add Activity". It should be sorted according to a determined order (based on frequency of use).

Therefore, I built a custom sort function that I included in sites/all/modules/civicrm/CRM/Case/Form/CaseView.php:

  public function compareToSample($aTypes, $sample_array) {
    $new_array = array();
    $size_sample = count($sample_array);
    // search for first element of $samplearray in $atypes
    for ($newarrayindex = 0; $newarrayindex < $size_sample; $newarrayindex++) {
      $key = array_search($sample_array[$newarrayindex], $aTypes, FALSE);
      if($key) {
        // found an element -> save at the end of $new_array
        array_push($new_array, $aTypes[$key]);
        unset($aTypes[$key]); // delete it from $aTypes
      }
    }
    // if after the whole process $aTypes is not empty, add the rest of elements to end of $new_array
    if(!(empty($aTypes))) {
      if(empty($new_array)) {
        // no element in common
        return $aTypes;
      }
      else {
        $new_array = array_merge($new_array, $aTypes);
      }
    }
    return $new_array;
  }

public function mySort($array_to_sort) {
    $sample_array = array(...some string elements representing the activity types in the right order...);
    if(empty($array_to_sort)) {
      return null; // EXCEPTION
    }
    $result = $this->compareToSample($array_to_sort, $sample_array);
    return $result;
  }

I am calling this function with $aTypes = $this->mySort($aTypes); inside the buildQuickForm() function in CaseView.php. (I am calling it just after the place where the original code is also sorting $aTypes, that is, just after:

if (!$xmlProcessor->getNaturalActivityTypeSort()) {
      asort($aTypes);
    }

The problem is now that the function only seems to sort the labels of the Activity Types, but not the Types themselves. So when clicking on one type, it will create an activity with another type of the list. How can I get the function to sort the real elements? Any hint is appreciated :)

1 Answer 1

2

I think my mistake was using the functions array_push and array_merge, as they assigned new keys to the array elements (activity types). Therefore, the single activity types couldn't be found in the following code anymore.

I changed these two expressions to:

$new_array[$key] = $aTypes[$key]; /// instead of: array_push($new_array, $aTypes[$key]);

and

$new_array = $new_array + $aTypes; /// instead of: $new_array = array_merge($new_array, $aTypes);

With that, it seems to work now.

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.