Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
6b83d5bd | 31 | * @copyright CiviCRM LLC (c) 2004-2019 |
6a488035 TO |
32 | */ |
33 | ||
34 | /** | |
35 | * Class handles functions for JSON format | |
36 | */ | |
37 | class CRM_Utils_JSON { | |
38 | ||
a63c07d9 | 39 | /** |
fe482240 | 40 | * Output json to the client. |
a63c07d9 CW |
41 | * @param mixed $input |
42 | */ | |
00be9182 | 43 | public static function output($input) { |
d42a224c | 44 | CRM_Utils_System::setHttpHeader('Content-Type', 'application/json'); |
fc7897b8 CW |
45 | echo json_encode($input); |
46 | CRM_Utils_System::civiExit(); | |
a63c07d9 CW |
47 | } |
48 | ||
d66880ec JG |
49 | /** |
50 | * Test whether the input string is valid JSON. | |
51 | * @param string $str | |
52 | * @return boolean | |
53 | */ | |
54 | public static function isValidJSON($str) { | |
55 | json_decode($str); | |
56 | return json_last_error() == JSON_ERROR_NONE; | |
57 | } | |
58 | ||
24df2136 | 59 | /** |
0dc4289e | 60 | * Do not use this function. See CRM-16353. |
42e397d3 | 61 | * @deprecated |
24df2136 | 62 | * |
77855840 TO |
63 | * @param array $params |
64 | * Associated array of row elements. | |
65 | * @param int $sEcho | |
66 | * Datatable needs this to make it more secure. | |
67 | * @param int $iTotal | |
68 | * Total records. | |
69 | * @param int $iFilteredTotal | |
70 | * Total records on a page. | |
71 | * @param array $selectorElements | |
72 | * Selector elements. | |
24df2136 | 73 | * @return string |
24df2136 | 74 | */ |
00be9182 | 75 | public static function encodeDataTableSelector($params, $sEcho, $iTotal, $iFilteredTotal, $selectorElements) { |
6a488035 TO |
76 | $sOutput = '{'; |
77 | $sOutput .= '"sEcho": ' . intval($sEcho) . ', '; | |
78 | $sOutput .= '"iTotalRecords": ' . $iTotal . ', '; | |
79 | $sOutput .= '"iTotalDisplayRecords": ' . $iFilteredTotal . ', '; | |
80 | $sOutput .= '"aaData": [ '; | |
697f11ef | 81 | foreach ((array) $params as $key => $value) { |
6a488035 TO |
82 | $addcomma = FALSE; |
83 | $sOutput .= "["; | |
84 | foreach ($selectorElements as $element) { | |
85 | if ($addcomma) { | |
86 | $sOutput .= ","; | |
87 | } | |
50bfb460 SB |
88 | // CRM-7130 --lets addslashes to only double quotes, |
89 | // since we are using it to quote the field value. | |
90 | // str_replace helps to provide a break for new-line | |
be2fb01f | 91 | $sOutput .= '"' . addcslashes(str_replace(["\r\n", "\n", "\r"], '<br />', $value[$element]), '"\\') . '"'; |
6a488035 | 92 | |
50bfb460 | 93 | // remove extra spaces and tab character that breaks dataTable CRM-12551 |
353ffa53 | 94 | $sOutput = preg_replace("/\s+/", " ", $sOutput); |
6a488035 TO |
95 | $addcomma = TRUE; |
96 | } | |
97 | $sOutput .= "],"; | |
98 | } | |
99 | $sOutput = substr_replace($sOutput, "", -1); | |
100 | $sOutput .= '] }'; | |
101 | ||
102 | return $sOutput; | |
103 | } | |
96025800 | 104 | |
6a488035 | 105 | } |