Merge pull request #20741 from civicrm/5.39
[civicrm-core.git] / Civi / Api4 / Query / SqlFunctionGROUP_CONCAT.php
CommitLineData
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
12namespace Civi\Api4\Query;
13
14/**
15 * Sql function
16 */
17class SqlFunctionGROUP_CONCAT extends SqlFunction {
18
7ce7b1cd
CW
19 public $supportsExpansion = TRUE;
20
e7f6def6
CW
21 protected static $category = self::CATEGORY_AGGREGATE;
22
23 protected static $params = [
24 [
25 'prefix' => ['', 'DISTINCT', 'ALL'],
26 'expr' => 1,
725a91cb 27 'must_be' => ['SqlField', 'sqlFunction'],
e7f6def6
CW
28 'optional' => FALSE,
29 ],
30 [
31 'prefix' => ['ORDER BY'],
32 'expr' => 1,
33 'suffix' => ['', 'ASC', 'DESC'],
34 'must_be' => ['SqlField'],
35 'optional' => TRUE,
36 ],
37 [
38 'prefix' => ['SEPARATOR'],
39 'expr' => 1,
40 'must_be' => ['SqlString'],
41 'optional' => TRUE,
7ce7b1cd
CW
42 // @see self::formatOutput()
43 'api_default' => [
44 'expr' => ['"' . \CRM_Core_DAO::VALUE_SEPARATOR . '"'],
45 ],
e7f6def6
CW
46 ],
47 ];
48
7ce7b1cd
CW
49 /**
50 * Reformat result as array if using default separator
51 *
52 * @see \Civi\Api4\Utils\FormattingUtil::formatOutputValues
53 * @param string $value
259207d0 54 * @param string $dataType
7ce7b1cd
CW
55 * @return string|array
56 */
259207d0 57 public function formatOutputValue($value, &$dataType) {
7ce7b1cd 58 $exprArgs = $this->getArgs();
259207d0 59 // By default, values are split into an array and formatted according to the field's dataType
7ce7b1cd
CW
60 if (!$exprArgs[2]['prefix']) {
61 $value = explode(\CRM_Core_DAO::VALUE_SEPARATOR, $value);
bc210cda
SL
62 // If the first expression is another sqlFunction then unset $dataType to preserve raw string
63 if ($exprArgs[0]['expr'][0] instanceof SqlFunction) {
64 $dataType = NULL;
65 }
7ce7b1cd 66 }
259207d0
CW
67 // If using custom separator, unset $dataType to preserve raw string
68 else {
69 $dataType = NULL;
70 }
7ce7b1cd
CW
71 return $value;
72 }
73
9cae8a07
CW
74 /**
75 * @return string
76 */
77 public static function getTitle(): string {
78 return ts('List');
79 }
80
e7f6def6 81}