Merge pull request #5292 from jitendrapurohit/CRM-15933
[civicrm-core.git] / CRM / Utils / JSON.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
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
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
32 * $Id$
33 *
34 */
35
36/**
37 * Class handles functions for JSON format
38 */
39class CRM_Utils_JSON {
40
a63c07d9 41 /**
fe482240 42 * Output json to the client.
a63c07d9
CW
43 * @param mixed $input
44 */
00be9182 45 public static function output($input) {
a63c07d9 46 header('Content-Type: application/json');
fc7897b8
CW
47 echo json_encode($input);
48 CRM_Utils_System::civiExit();
a63c07d9
CW
49 }
50
6a488035 51 /**
fe482240 52 * Create JSON object.
a63c07d9 53 * @deprecated
6a488035 54 *
77855840
TO
55 * @param array $params
56 * Associated array, that needs to be.
6a488035 57 * converted to JSON array
77855840
TO
58 * @param string $identifier
59 * Identifier for the JSON array.
6a488035 60 *
a6c01b45
CW
61 * @return string
62 * JSON array
6a488035 63 */
00be9182 64 public static function encode($params, $identifier = 'id') {
6a488035
TO
65 $buildObject = array();
66 foreach ($params as $value) {
67 $name = addslashes($value['name']);
68 $buildObject[] = "{ name: \"$name\", {$identifier}:\"{$value[$identifier]}\"}";
69 }
70
71 $jsonObject = '{ identifier: "' . $identifier . '", items: [' . implode(',', $buildObject) . ' ]}';
72
73 return $jsonObject;
74 }
75
76 /**
100fef9d 77 * Encode json format for flexigrid, NOTE: "id" should be present in $params for each row
6a488035 78 *
77855840
TO
79 * @param array $params
80 * Associated array of values rows.
81 * @param int $page
82 * Page no for selector.
f4aaa82a 83 * @param $total
77855840
TO
84 * @param array $selectorElements
85 * Selector rows.
6a488035 86 *
72b3a70c
CW
87 * @return string
88 * json encoded string
6a488035 89 */
00be9182 90 public static function encodeSelector(&$params, $page, $total, $selectorElements) {
6a488035
TO
91 $json = "";
92 $json .= "{\n";
93 $json .= "page: $page,\n";
94 $json .= "total: $total,\n";
95 $json .= "rows: [";
96 $rc = FALSE;
97
98 foreach ($params as $key => $value) {
99 if ($rc) {
100 $json .= ",";
101 }
102 $json .= "\n{";
103 $json .= "id:'" . $value['id'] . "',";
104 $json .= "cell:[";
105 $addcomma = FALSE;
106 foreach ($selectorElements as $element) {
107 if ($addcomma) {
108 $json .= ",";
109 }
110 $json .= "'" . addslashes($value[$element]) . "'";
111 $addcomma = TRUE;
112 }
113 $json .= "]}";
114 $rc = TRUE;
115 }
116
117 $json .= "]\n";
118 $json .= "}";
119
120 return $json;
121 }
122
24df2136 123 /**
fe482240 124 * encode data for dataTable plugin.
24df2136 125 *
77855840
TO
126 * @param array $params
127 * Associated array of row elements.
128 * @param int $sEcho
129 * Datatable needs this to make it more secure.
130 * @param int $iTotal
131 * Total records.
132 * @param int $iFilteredTotal
133 * Total records on a page.
134 * @param array $selectorElements
135 * Selector elements.
24df2136 136 * @return string
24df2136 137 */
00be9182 138 public static function encodeDataTableSelector($params, $sEcho, $iTotal, $iFilteredTotal, $selectorElements) {
6a488035
TO
139 $sOutput = '{';
140 $sOutput .= '"sEcho": ' . intval($sEcho) . ', ';
141 $sOutput .= '"iTotalRecords": ' . $iTotal . ', ';
142 $sOutput .= '"iTotalDisplayRecords": ' . $iFilteredTotal . ', ';
143 $sOutput .= '"aaData": [ ';
144 foreach ($params as $key => $value) {
145 $addcomma = FALSE;
146 $sOutput .= "[";
147 foreach ($selectorElements as $element) {
148 if ($addcomma) {
149 $sOutput .= ",";
150 }
6a488035
TO
151 //CRM-7130 --lets addslashes to only double quotes,
152 //since we are using it to quote the field value.
153 //str_replace helps to provide a break for new-line
154 $sOutput .= '"' . addcslashes(str_replace(array("\r\n", "\n", "\r"), '<br />', $value[$element]), '"\\') . '"';
155
24df2136 156 //remove extra spaces and tab character that breaks dataTable CRM-12551
353ffa53 157 $sOutput = preg_replace("/\s+/", " ", $sOutput);
6a488035
TO
158 $addcomma = TRUE;
159 }
160 $sOutput .= "],";
161 }
162 $sOutput = substr_replace($sOutput, "", -1);
163 $sOutput .= '] }';
164
165 return $sOutput;
166 }
96025800 167
6a488035 168}