3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2014
37 * Class handles functions for JSON format
39 class CRM_Utils_JSON
{
42 * Output json to the client.
45 public static function output($input) {
46 header('Content-Type: application/json');
47 echo json_encode($input);
48 CRM_Utils_System
::civiExit();
55 * @param array $params
56 * Associated array, that needs to be.
57 * converted to JSON array
58 * @param string $identifier
59 * Identifier for the JSON array.
64 public static function encode($params, $identifier = 'id') {
65 $buildObject = array();
66 foreach ($params as $value) {
67 $name = addslashes($value['name']);
68 $buildObject[] = "{ name: \"$name\", {$identifier}:\"{$value[$identifier]}\"}";
71 $jsonObject = '{ identifier: "' . $identifier . '", items: [' . implode(',', $buildObject) . ' ]}';
77 * Encode json format for flexigrid, NOTE: "id" should be present in $params for each row
79 * @param array $params
80 * Associated array of values rows.
82 * Page no for selector.
84 * @param array $selectorElements
90 public static function encodeSelector(&$params, $page, $total, $selectorElements) {
93 $json .= "page: $page,\n";
94 $json .= "total: $total,\n";
98 foreach ($params as $key => $value) {
103 $json .= "id:'" . $value['id'] . "',";
106 foreach ($selectorElements as $element) {
110 $json .= "'" . addslashes($value[$element]) . "'";
124 * encode data for dataTable plugin.
126 * @param array $params
127 * Associated array of row elements.
129 * Datatable needs this to make it more secure.
132 * @param int $iFilteredTotal
133 * Total records on a page.
134 * @param array $selectorElements
138 public static function encodeDataTableSelector($params, $sEcho, $iTotal, $iFilteredTotal, $selectorElements) {
140 $sOutput .= '"sEcho": ' . intval($sEcho) . ', ';
141 $sOutput .= '"iTotalRecords": ' . $iTotal . ', ';
142 $sOutput .= '"iTotalDisplayRecords": ' . $iFilteredTotal . ', ';
143 $sOutput .= '"aaData": [ ';
144 foreach ($params as $key => $value) {
147 foreach ($selectorElements as $element) {
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]), '"\\') . '"';
156 //remove extra spaces and tab character that breaks dataTable CRM-12551
157 $sOutput = preg_replace("/\s+/", " ", $sOutput);
162 $sOutput = substr_replace($sOutput, "", -1);