Merge pull request #6170 from colemanw/CRM-16354
[civicrm-core.git] / CRM / Utils / JSON.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
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
e7112fa7 31 * @copyright CiviCRM LLC (c) 2004-2015
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
24df2136 51 /**
fe482240 52 * encode data for dataTable plugin.
42e397d3 53 * @deprecated
24df2136 54 *
77855840
TO
55 * @param array $params
56 * Associated array of row elements.
57 * @param int $sEcho
58 * Datatable needs this to make it more secure.
59 * @param int $iTotal
60 * Total records.
61 * @param int $iFilteredTotal
62 * Total records on a page.
63 * @param array $selectorElements
64 * Selector elements.
24df2136 65 * @return string
24df2136 66 */
00be9182 67 public static function encodeDataTableSelector($params, $sEcho, $iTotal, $iFilteredTotal, $selectorElements) {
6a488035
TO
68 $sOutput = '{';
69 $sOutput .= '"sEcho": ' . intval($sEcho) . ', ';
70 $sOutput .= '"iTotalRecords": ' . $iTotal . ', ';
71 $sOutput .= '"iTotalDisplayRecords": ' . $iFilteredTotal . ', ';
72 $sOutput .= '"aaData": [ ';
73 foreach ($params as $key => $value) {
74 $addcomma = FALSE;
75 $sOutput .= "[";
76 foreach ($selectorElements as $element) {
77 if ($addcomma) {
78 $sOutput .= ",";
79 }
6a488035
TO
80 //CRM-7130 --lets addslashes to only double quotes,
81 //since we are using it to quote the field value.
82 //str_replace helps to provide a break for new-line
83 $sOutput .= '"' . addcslashes(str_replace(array("\r\n", "\n", "\r"), '<br />', $value[$element]), '"\\') . '"';
84
24df2136 85 //remove extra spaces and tab character that breaks dataTable CRM-12551
353ffa53 86 $sOutput = preg_replace("/\s+/", " ", $sOutput);
6a488035
TO
87 $addcomma = TRUE;
88 }
89 $sOutput .= "],";
90 }
91 $sOutput = substr_replace($sOutput, "", -1);
92 $sOutput .= '] }';
93
94 return $sOutput;
95 }
96025800 96
6a488035 97}