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