Commit | Line | Data |
---|---|---|
e7f6def6 CW |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
4 | | Copyright CiviCRM LLC. All rights reserved. | | |
5 | | | | |
6 | | This work is published under the GNU AGPLv3 license with some | | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
9 | +--------------------------------------------------------------------+ | |
10 | */ | |
11 | ||
12 | namespace Civi\Api4\Query; | |
13 | ||
14 | /** | |
15 | * Sql function | |
16 | */ | |
17 | class SqlFunctionGROUP_CONCAT extends SqlFunction { | |
18 | ||
7ce7b1cd CW |
19 | public $supportsExpansion = TRUE; |
20 | ||
e7f6def6 CW |
21 | protected static $category = self::CATEGORY_AGGREGATE; |
22 | ||
f19a0f00 CW |
23 | protected static function params(): array { |
24 | return [ | |
25 | [ | |
1b6a82ee | 26 | 'flag_before' => ['DISTINCT' => ts('Distinct')], |
f19a0f00 CW |
27 | 'max_expr' => 1, |
28 | 'must_be' => ['SqlField', 'SqlFunction'], | |
29 | 'optional' => FALSE, | |
7ce7b1cd | 30 | ], |
f19a0f00 | 31 | [ |
1b6a82ee | 32 | 'prefix' => 'ORDER BY', |
f19a0f00 | 33 | 'max_expr' => 1, |
1b6a82ee | 34 | 'flag_after' => ['ASC' => ts('Ascending'), 'DESC' => ts('Descending')], |
f19a0f00 CW |
35 | 'must_be' => ['SqlField'], |
36 | 'optional' => TRUE, | |
37 | ], | |
38 | [ | |
1b6a82ee | 39 | 'prefix' => 'SEPARATOR', |
f19a0f00 CW |
40 | 'max_expr' => 1, |
41 | 'must_be' => ['SqlString'], | |
42 | 'optional' => TRUE, | |
43 | // @see self::formatOutput() | |
44 | 'api_default' => [ | |
45 | 'expr' => ['"' . \CRM_Core_DAO::VALUE_SEPARATOR . '"'], | |
46 | ], | |
47 | ], | |
48 | ]; | |
49 | } | |
e7f6def6 | 50 | |
7ce7b1cd CW |
51 | /** |
52 | * Reformat result as array if using default separator | |
53 | * | |
54 | * @see \Civi\Api4\Utils\FormattingUtil::formatOutputValues | |
55 | * @param string $value | |
259207d0 | 56 | * @param string $dataType |
7ce7b1cd CW |
57 | * @return string|array |
58 | */ | |
259207d0 | 59 | public function formatOutputValue($value, &$dataType) { |
7ce7b1cd | 60 | $exprArgs = $this->getArgs(); |
259207d0 | 61 | // By default, values are split into an array and formatted according to the field's dataType |
1b6a82ee | 62 | if (isset($exprArgs[2]['expr'][0]->expr) && $exprArgs[2]['expr'][0]->expr === \CRM_Core_DAO::VALUE_SEPARATOR) { |
7ce7b1cd | 63 | $value = explode(\CRM_Core_DAO::VALUE_SEPARATOR, $value); |
984f0c4e | 64 | // If the first expression is another sqlFunction, allow it to control the dataType |
b0aa3463 | 65 | if ($exprArgs[0]['expr'][0] instanceof SqlFunction) { |
984f0c4e | 66 | $exprArgs[0]['expr'][0]->formatOutputValue(NULL, $dataType); |
bc210cda | 67 | } |
7ce7b1cd | 68 | } |
b0aa3463 | 69 | // If using custom separator, preserve raw string |
259207d0 | 70 | else { |
b0aa3463 | 71 | $dataType = 'String'; |
259207d0 | 72 | } |
7ce7b1cd CW |
73 | return $value; |
74 | } | |
75 | ||
9cae8a07 CW |
76 | /** |
77 | * @return string | |
78 | */ | |
79 | public static function getTitle(): string { | |
80 | return ts('List'); | |
81 | } | |
82 | ||
e7f6def6 | 83 | } |