Merge pull request #15971 from aydun/report#23
[civicrm-core.git] / CRM / Utils / JSON.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
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 |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17
18/**
19 * Class handles functions for JSON format
20 */
21class CRM_Utils_JSON {
22
a63c07d9 23 /**
fe482240 24 * Output json to the client.
a63c07d9
CW
25 * @param mixed $input
26 */
00be9182 27 public static function output($input) {
d42a224c 28 CRM_Utils_System::setHttpHeader('Content-Type', 'application/json');
fc7897b8
CW
29 echo json_encode($input);
30 CRM_Utils_System::civiExit();
a63c07d9
CW
31 }
32
d66880ec
JG
33 /**
34 * Test whether the input string is valid JSON.
35 * @param string $str
36 * @return boolean
37 */
38 public static function isValidJSON($str) {
39 json_decode($str);
40 return json_last_error() == JSON_ERROR_NONE;
41 }
42
24df2136 43 /**
0dc4289e 44 * Do not use this function. See CRM-16353.
42e397d3 45 * @deprecated
24df2136 46 *
77855840
TO
47 * @param array $params
48 * Associated array of row elements.
49 * @param int $sEcho
50 * Datatable needs this to make it more secure.
51 * @param int $iTotal
52 * Total records.
53 * @param int $iFilteredTotal
54 * Total records on a page.
55 * @param array $selectorElements
56 * Selector elements.
24df2136 57 * @return string
24df2136 58 */
00be9182 59 public static function encodeDataTableSelector($params, $sEcho, $iTotal, $iFilteredTotal, $selectorElements) {
6a488035
TO
60 $sOutput = '{';
61 $sOutput .= '"sEcho": ' . intval($sEcho) . ', ';
62 $sOutput .= '"iTotalRecords": ' . $iTotal . ', ';
63 $sOutput .= '"iTotalDisplayRecords": ' . $iFilteredTotal . ', ';
64 $sOutput .= '"aaData": [ ';
697f11ef 65 foreach ((array) $params as $key => $value) {
6a488035
TO
66 $addcomma = FALSE;
67 $sOutput .= "[";
68 foreach ($selectorElements as $element) {
69 if ($addcomma) {
70 $sOutput .= ",";
71 }
50bfb460
SB
72 // CRM-7130 --lets addslashes to only double quotes,
73 // since we are using it to quote the field value.
74 // str_replace helps to provide a break for new-line
be2fb01f 75 $sOutput .= '"' . addcslashes(str_replace(["\r\n", "\n", "\r"], '<br />', $value[$element]), '"\\') . '"';
6a488035 76
50bfb460 77 // remove extra spaces and tab character that breaks dataTable CRM-12551
353ffa53 78 $sOutput = preg_replace("/\s+/", " ", $sOutput);
6a488035
TO
79 $addcomma = TRUE;
80 }
81 $sOutput .= "],";
82 }
83 $sOutput = substr_replace($sOutput, "", -1);
84 $sOutput .= '] }';
85
86 return $sOutput;
87 }
96025800 88
6a488035 89}