Merge pull request #16610 from mattwire/settingstrait_datepicker
[civicrm-core.git] / Civi / Api4 / Utils / SelectUtil.php
index 38b085ceefaa9f61ee403dc3a5bdd9d521c5721d..781428e2449659800602584adf4b5ee2972246c4 100644 (file)
@@ -14,8 +14,6 @@
  *
  * @package CRM
  * @copyright CiviCRM LLC https://civicrm.org/licensing
- * $Id$
- *
  */
 
 
@@ -43,17 +41,32 @@ class SelectUtil {
   }
 
   /**
+   * Filters a list of fieldnames by matching a pattern which may contain * wildcards.
+   *
+   * For fieldnames joined with a dot (e.g. email.contact_id), wildcards are only allowed after the last dot.
+   *
    * @param string $pattern
    * @param array $fieldNames
    * @return array
    */
   public static function getMatchingFields($pattern, $fieldNames) {
+    // If the pattern is "select all" then we return all base fields (excluding those with a dot)
     if ($pattern === '*') {
-      return $fieldNames;
+      return array_values(array_filter($fieldNames, function($field) {
+        return strpos($field, '.') === FALSE;
+      }));
     }
-    $pattern = '/^' . str_replace('\*', '.*', preg_quote($pattern, '/')) . '$/';
-    return array_values(array_filter($fieldNames, function($field) use ($pattern) {
-      return preg_match($pattern, $field);
+    $dot = strrpos($pattern, '.');
+    $prefix = $dot === FALSE ? '' : substr($pattern, 0, $dot + 1);
+    $search = $dot === FALSE ? $pattern : substr($pattern, $dot + 1);
+    $search = '/^' . str_replace('\*', '.*', preg_quote($search, '/')) . '$/';
+    return array_values(array_filter($fieldNames, function($field) use ($search, $prefix) {
+      // Exclude fields that don't have the same join prefix
+      if (($prefix !== '' && strpos($field, $prefix) !== 0) || substr_count($prefix, '.') !== substr_count($field, '.')) {
+        return FALSE;
+      }
+      // Now strip the prefix and compare field name to the pattern
+      return preg_match($search, substr($field, strlen($prefix)));
     }));
   }