APIv4 - add descriptions to sql functions
authorColeman Watts <coleman@civicrm.org>
Fri, 17 Sep 2021 19:37:30 +0000 (15:37 -0400)
committerColeman Watts <coleman@civicrm.org>
Sat, 18 Sep 2021 18:13:27 +0000 (14:13 -0400)
28 files changed:
CRM/Api4/Page/Api4Explorer.php
Civi/Api4/Query/SqlFunction.php
Civi/Api4/Query/SqlFunctionABS.php
Civi/Api4/Query/SqlFunctionAVG.php
Civi/Api4/Query/SqlFunctionBINARY.php
Civi/Api4/Query/SqlFunctionCOALESCE.php
Civi/Api4/Query/SqlFunctionCONCAT.php
Civi/Api4/Query/SqlFunctionCOUNT.php
Civi/Api4/Query/SqlFunctionCURDATE.php
Civi/Api4/Query/SqlFunctionDATE.php
Civi/Api4/Query/SqlFunctionGREATEST.php
Civi/Api4/Query/SqlFunctionGROUP_CONCAT.php
Civi/Api4/Query/SqlFunctionIF.php
Civi/Api4/Query/SqlFunctionISNULL.php
Civi/Api4/Query/SqlFunctionLEAST.php
Civi/Api4/Query/SqlFunctionLOWER.php
Civi/Api4/Query/SqlFunctionMAX.php
Civi/Api4/Query/SqlFunctionMIN.php
Civi/Api4/Query/SqlFunctionNULLIF.php
Civi/Api4/Query/SqlFunctionRAND.php
Civi/Api4/Query/SqlFunctionREPLACE.php
Civi/Api4/Query/SqlFunctionROUND.php
Civi/Api4/Query/SqlFunctionSUM.php
Civi/Api4/Query/SqlFunctionTIME.php
Civi/Api4/Query/SqlFunctionUPPER.php
Civi/Api4/Query/SqlFunctionYEAR.php
ang/api4Explorer/Explorer.js
ext/search_kit/ang/crmSearchAdmin/crmSearchFunction.component.js

index 9cd4508fc56b106dc2b956e5819749c2e8f67c0d..a31e5feb62ece31f756e617173ecdefa92b49a33 100644 (file)
@@ -59,6 +59,7 @@ class CRM_Api4_Page_Api4Explorer extends CRM_Core_Page {
           $fns[] = [
             'name' => $className::getName(),
             'title' => $className::getTitle(),
+            'description' => $className::getDescription(),
             'params' => $className::getParams(),
             'category' => $className::getCategory(),
             'dataType' => $className::getDataType(),
index 915962aa821a49a45eaece69437de6694ade84bb..521a67b3bcc853292995105cb3b12780c5bf004e 100644 (file)
@@ -283,4 +283,9 @@ abstract class SqlFunction extends SqlExpression {
    */
   abstract public static function getTitle(): string;
 
+  /**
+   * @return string
+   */
+  abstract public static function getDescription(): string;
+
 }
index 015906c4def6c5ce1ce3ed622af4c310f5137ce0..7e8b6f1aab26e603e57603d07ecd3c9cadd0f157 100644 (file)
@@ -31,7 +31,14 @@ class SqlFunctionABS extends SqlFunction {
    * @return string
    */
   public static function getTitle(): string {
-    return ts('Absolute');
+    return ts('Absolute value');
+  }
+
+  /**
+   * @return string
+   */
+  public static function getDescription(): string {
+    return ts('The positive value of a number.');
   }
 
 }
index 59e6548a7309aa80e7b3b8f8160f2a99befa0084..44ff3b0720ca2c17d6f004a1f1c3b4ca68e0b719 100644 (file)
@@ -34,4 +34,11 @@ class SqlFunctionAVG extends SqlFunction {
     return ts('Average');
   }
 
+  /**
+   * @return string
+   */
+  public static function getDescription(): string {
+    return ts('The mean of all values in the grouping.');
+  }
+
 }
index e2500d83dbbf1bb6f4dcd28d04403f26192bfb44..cce4b8ea5059a7fdac14b064f9a093d367ddd2cc 100644 (file)
@@ -34,4 +34,11 @@ class SqlFunctionBINARY extends SqlFunction {
     return ts('Binary');
   }
 
+  /**
+   * @return string
+   */
+  public static function getDescription(): string {
+    return ts('Case-sensitive string treatment.');
+  }
+
 }
index 62622be45f6fba4a5ab32dfc9edf75720a0e6b55..fc5765f6dfe17c1336a07e7d0c019c392cb4e2fd 100644 (file)
@@ -36,4 +36,11 @@ class SqlFunctionCOALESCE extends SqlFunction {
     return ts('Coalesce');
   }
 
+  /**
+   * @return string
+   */
+  public static function getDescription(): string {
+    return ts('The first value that is not null.');
+  }
+
 }
index a1c201fec288237e01d86f0f14f165f4968722a8..4d6437eb4a697788280fd2d22158da16386aa4aa 100644 (file)
@@ -34,7 +34,14 @@ class SqlFunctionCONCAT extends SqlFunction {
    * @return string
    */
   public static function getTitle(): string {
-    return ts('Combine');
+    return ts('Combine text');
+  }
+
+  /**
+   * @return string
+   */
+  public static function getDescription(): string {
+    return ts('Multiple values concatenated into a single string.');
   }
 
 }
index c51e024856ab1730f3ca109ed40617470b3b869c..e45c9ff67d8389bd9deeacaba3d5a78e1706a138 100644 (file)
@@ -38,4 +38,11 @@ class SqlFunctionCOUNT extends SqlFunction {
     return ts('Count');
   }
 
+  /**
+   * @return string
+   */
+  public static function getDescription(): string {
+    return ts('The number of items in the grouping.');
+  }
+
 }
index f81993b53662a407401602c881b30426283ee2cc..951e4498c533395eafa6b3dfd73df1f1b6762638 100644 (file)
@@ -29,4 +29,11 @@ class SqlFunctionCURDATE extends SqlFunction {
     return ts('Now');
   }
 
+  /**
+   * @return string
+   */
+  public static function getDescription(): string {
+    return ts('The current date.');
+  }
+
 }
index 05362a510349beeaebec0373b4fa4ae2dea99dfc..0fe0d136b683e40e484a934f392e59dced6aec76 100644 (file)
@@ -33,7 +33,14 @@ class SqlFunctionDATE extends SqlFunction {
    * @return string
    */
   public static function getTitle(): string {
-    return ts('Date Only');
+    return ts('Date only');
+  }
+
+  /**
+   * @return string
+   */
+  public static function getDescription(): string {
+    return ts('Only the date portion of a date/time.');
   }
 
 }
index 549e59a3de2fbf1fc6dca20df86785122c44ea83..3874ce4aecb3de34d812e12adaca41cb4cd34127 100644 (file)
@@ -36,4 +36,11 @@ class SqlFunctionGREATEST extends SqlFunction {
     return ts('Greatest');
   }
 
+  /**
+   * @return string
+   */
+  public static function getDescription(): string {
+    return ts('The largest of all provided values.');
+  }
+
 }
index 4745cbabea2413d61f5bf4e6bbc966e55c751bb6..1b67dfb9083549dce0c97dc249a7050bae9f3417 100644 (file)
@@ -80,4 +80,11 @@ class SqlFunctionGROUP_CONCAT extends SqlFunction {
     return ts('List');
   }
 
+  /**
+   * @return string
+   */
+  public static function getDescription(): string {
+    return ts('All values in the grouping.');
+  }
+
 }
index 1b120d4fed948881e34676cef9b11b97eb0354c3..66916a46f7483ed1aab89118d31b1a376ea34fb6 100644 (file)
@@ -34,7 +34,14 @@ class SqlFunctionIF extends SqlFunction {
    * @return string
    */
   public static function getTitle(): string {
-    return ts('If');
+    return ts('If/Else');
+  }
+
+  /**
+   * @return string
+   */
+  public static function getDescription(): string {
+    return ts('If the field is empty, the first value, otherwise the second.');
   }
 
 }
index beeaebc77d533c45ed6453bf5cf903b195d63891..17d729d430d675fbf879207006086c343ba1e2be 100644 (file)
@@ -35,4 +35,11 @@ class SqlFunctionISNULL extends SqlFunction {
     return ts('Is null');
   }
 
+  /**
+   * @return string
+   */
+  public static function getDescription(): string {
+    return ts('TRUE if the value is NULL, otherwise FALSE.');
+  }
+
 }
index 6317647241de2eea969ca0d1ef45ab2bfe4e6947..bbf903819ef6897160585e0df7cf10983061fa32 100644 (file)
@@ -36,4 +36,11 @@ class SqlFunctionLEAST extends SqlFunction {
     return ts('Least');
   }
 
+  /**
+   * @return string
+   */
+  public static function getDescription(): string {
+    return ts('The smallest of all provided values.');
+  }
+
 }
index d1a77b6b5f58e1a79d94c78bf443d6caf1800ec1..23d535e352c4e359ae495fd96c011b6681a72553 100644 (file)
@@ -36,4 +36,11 @@ class SqlFunctionLOWER extends SqlFunction {
     return ts('Lowercase');
   }
 
+  /**
+   * @return string
+   */
+  public static function getDescription(): string {
+    return ts('Lowercase version of text.');
+  }
+
 }
index 6e3a880bf4c25ff01b5bbaace24e6938ee045cbe..24ad68af4a58e5e932d7a2d090e0573004e7b974 100644 (file)
@@ -36,4 +36,11 @@ class SqlFunctionMAX extends SqlFunction {
     return ts('Max');
   }
 
+  /**
+   * @return string
+   */
+  public static function getDescription(): string {
+    return ts('The largest value in the grouping.');
+  }
+
 }
index 3c6a79227cd4126355fff2812c810ea01254cacd..0883ee4c8ffdbae3505452558415b3326a496b7e 100644 (file)
@@ -36,4 +36,11 @@ class SqlFunctionMIN extends SqlFunction {
     return ts('Min');
   }
 
+  /**
+   * @return string
+   */
+  public static function getDescription(): string {
+    return ts('The smallest value in the grouping.');
+  }
+
 }
index 79e117a7fdb14e627a6b493c8ee6669da4e6b305..6cb6b2fb78155d457da1af75c0463edfb2b66c12 100644 (file)
@@ -34,7 +34,14 @@ class SqlFunctionNULLIF extends SqlFunction {
    * @return string
    */
   public static function getTitle(): string {
-    return ts('Null if');
+    return ts('Unequal');
+  }
+
+  /**
+   * @return string
+   */
+  public static function getDescription(): string {
+    return ts('The first value, only if it is not equal to the second.');
   }
 
 }
index 1cb0cfeeb91596438c967e3b64128634fe9baa6f..7e834ea55e43b44bd4297f361999fbc02f59e2ff 100644 (file)
@@ -26,7 +26,14 @@ class SqlFunctionRAND extends SqlFunction {
    * @return string
    */
   public static function getTitle(): string {
-    return ts('Random Number');
+    return ts('Random number');
+  }
+
+  /**
+   * @return string
+   */
+  public static function getDescription(): string {
+    return ts('Generates a random number between 0 and 1.');
   }
 
 }
index 27c7c1b51c989bc7691cd43bf3cfa8864545bf6d..916678db0fa6736859ca587666e359f0f59c51cd 100644 (file)
@@ -24,7 +24,7 @@ class SqlFunctionREPLACE extends SqlFunction {
         'min_expr' => 3,
         'max_expr' => 3,
         'optional' => FALSE,
-        'must_be' => ['SqlField', 'SqlString'],
+        'must_be' => ['SqlString', 'SqlField'],
       ],
     ];
   }
@@ -33,7 +33,14 @@ class SqlFunctionREPLACE extends SqlFunction {
    * @return string
    */
   public static function getTitle(): string {
-    return ts('Replace');
+    return ts('Replace text');
+  }
+
+  /**
+   * @return string
+   */
+  public static function getDescription(): string {
+    return ts('Substitutes one value for another in the text.');
   }
 
 }
index 5a57c584b1a140ec250a1c009c624cc5b516642f..285f43c621007348a96c8da66ed2ae636ed342df 100644 (file)
@@ -22,11 +22,9 @@ class SqlFunctionROUND extends SqlFunction {
     return [
       [
         'optional' => FALSE,
-        'must_be' => ['SqlField', 'SqlNumber'],
-      ],
-      [
-        'optional' => TRUE,
-        'must_be' => ['SqlNumber'],
+        'min_expr' => 1,
+        'max_expr' => 2,
+        'must_be' => ['SqlNumber', 'SqlField'],
       ],
     ];
   }
@@ -38,4 +36,11 @@ class SqlFunctionROUND extends SqlFunction {
     return ts('Round');
   }
 
+  /**
+   * @return string
+   */
+  public static function getDescription(): string {
+    return ts('Number rounded to specified number of decimal places.');
+  }
+
 }
index 28522692aa013d2fc2c8e0cfe8c8241111215770..71c56611b88a0229bc02ef2603125be9b7655590 100644 (file)
@@ -34,4 +34,11 @@ class SqlFunctionSUM extends SqlFunction {
     return ts('Sum');
   }
 
+  /**
+   * @return string
+   */
+  public static function getDescription(): string {
+    return ts('The sum of all values in the grouping.');
+  }
+
 }
index e55b48b8671d958334eafcf8e397c86a51cc3bc8..03548773252e3c9c350c11c7350856cb2769e88e 100644 (file)
@@ -33,7 +33,14 @@ class SqlFunctionTIME extends SqlFunction {
    * @return string
    */
   public static function getTitle(): string {
-    return ts('Time Only');
+    return ts('Time only');
+  }
+
+  /**
+   * @return string
+   */
+  public static function getDescription(): string {
+    return ts('Only the time portaion of a date/time.');
   }
 
 }
index 57e3ae324365d485e83fda783972e31f3bd7a7dc..51c08af908bb4ba59003cecd1dc5979873fbf8c8 100644 (file)
@@ -36,4 +36,11 @@ class SqlFunctionUPPER extends SqlFunction {
     return ts('Uppercase');
   }
 
+  /**
+   * @return string
+   */
+  public static function getDescription(): string {
+    return ts('Uppercase version of text.');
+  }
+
 }
index 81b0ae52a57d0a308abfe0d35a90512e8e507dc2..f655000c437659aa195f85e6db8bb1ec9a393a30 100644 (file)
@@ -36,4 +36,11 @@ class SqlFunctionYEAR extends SqlFunction {
     return ts('Year Only');
   }
 
+  /**
+   * @return string
+   */
+  public static function getDescription(): string {
+    return ts('Only the year of a date.');
+  }
+
 }
index 2bdaa72b11c4f34fea2577e1a0c7a69cee390211..0d5404a8f4a189d17863de8968a8b1ce071a8f8f 100644 (file)
             children: _.transform(CRM.vars.api4.functions, function(result, fn) {
               result.push({
                 id: fn.name + '() AS ' + fn.name.toLowerCase(),
-                text: fn.name + '()',
-                description: fn.name + '(' + describeSqlFn(fn.params) + ')'
+                description: fn.description,
+                text: fn.name + '(' + describeSqlFn(fn.params) + ')'
               });
             })
           };
       var desc = ' ';
       _.each(params, function(param) {
         desc += ' ';
-        if (param.prefix) {
-          desc += _.filter(param.prefix).join('|') + ' ';
+        if (param.name) {
+          desc += param.name + ' ';
         }
-        if (param.expr === 1) {
+        if (!_.isEmpty(param.flag_before)) {
+          desc += '[' + _.filter(param.name ? [param.name] : _.keys(param.flag_before)).join('|') + '] ';
+        }
+        if (param.max_expr === 1) {
           desc += 'expr ';
-        } else if (param.expr > 1) {
+        } else if (param.max_expr > 1) {
           desc += 'expr, ... ';
         }
-        if (param.suffix) {
-          desc += ' ' + _.filter(param.suffix).join('|') + ' ';
+        if (!_.isEmpty(param.flag_after)) {
+          desc += ' [' + _.filter(param.flag_after).join('|') + '] ';
         }
       });
       return desc.replace(/[ ]+/g, ' ');
index b1d31d5675b5def54e45f2dcede7622a7646d5c2..0aee8488586c2434471be715bcee0c55845a957a 100644 (file)
@@ -69,7 +69,7 @@
             });
             functions.push({
               text: allTypes[type],
-              children: formatForSelect2(allowedFunctions, 'name', 'title')
+              children: formatForSelect2(allowedFunctions, 'name', 'title', ['description'])
             });
           });
         }