Merge pull request #74 from dlobo/MiscFixes
[civicrm-core.git] / CRM / Utils / JSON.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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
31 * @copyright CiviCRM LLC (c) 2004-2013
32 * $Id$
33 *
34 */
35
36/**
37 * Class handles functions for JSON format
38 */
39class CRM_Utils_JSON {
40
41 /**
42 * Function to create JSON object
43 *
44 * @param array $params associated array, that needs to be
45 * converted to JSON array
46 * @param string $identifier identifier for the JSON array
47 *
48 * @return string $jsonObject JSON array
49 * @static
50 */
51 static function encode($params, $identifier = 'id') {
52 $buildObject = array();
53 foreach ($params as $value) {
54 $name = addslashes($value['name']);
55 $buildObject[] = "{ name: \"$name\", {$identifier}:\"{$value[$identifier]}\"}";
56 }
57
58 $jsonObject = '{ identifier: "' . $identifier . '", items: [' . implode(',', $buildObject) . ' ]}';
59
60 return $jsonObject;
61 }
62
63 /**
64 * Function to encode json format for flexigrid, NOTE: "id" should be present in $params for each row
65 *
66 * @param array $params associated array of values rows
67 * @param int $page page no for selector
68 * @param array $selectorElements selector rows
69 *
70 * @return json encode string
71 */
72 static function encodeSelector(&$params, $page, $total, $selectorElements) {
73 $json = "";
74 $json .= "{\n";
75 $json .= "page: $page,\n";
76 $json .= "total: $total,\n";
77 $json .= "rows: [";
78 $rc = FALSE;
79
80 foreach ($params as $key => $value) {
81 if ($rc) {
82 $json .= ",";
83 }
84 $json .= "\n{";
85 $json .= "id:'" . $value['id'] . "',";
86 $json .= "cell:[";
87 $addcomma = FALSE;
88 foreach ($selectorElements as $element) {
89 if ($addcomma) {
90 $json .= ",";
91 }
92 $json .= "'" . addslashes($value[$element]) . "'";
93 $addcomma = TRUE;
94 }
95 $json .= "]}";
96 $rc = TRUE;
97 }
98
99 $json .= "]\n";
100 $json .= "}";
101
102 return $json;
103 }
104
105 static function encodeDataTableSelector($params, $sEcho, $iTotal, $iFilteredTotal, $selectorElements) {
106 $sOutput = '{';
107 $sOutput .= '"sEcho": ' . intval($sEcho) . ', ';
108 $sOutput .= '"iTotalRecords": ' . $iTotal . ', ';
109 $sOutput .= '"iTotalDisplayRecords": ' . $iFilteredTotal . ', ';
110 $sOutput .= '"aaData": [ ';
111 foreach ($params as $key => $value) {
112 $addcomma = FALSE;
113 $sOutput .= "[";
114 foreach ($selectorElements as $element) {
115 if ($addcomma) {
116 $sOutput .= ",";
117 }
118 //$sOutput .= '"'.addslashes($value[$element]).'"';
119
120 //CRM-7130 --lets addslashes to only double quotes,
121 //since we are using it to quote the field value.
122 //str_replace helps to provide a break for new-line
123 $sOutput .= '"' . addcslashes(str_replace(array("\r\n", "\n", "\r"), '<br />', $value[$element]), '"\\') . '"';
124
125 $addcomma = TRUE;
126 }
127 $sOutput .= "],";
128 }
129 $sOutput = substr_replace($sOutput, "", -1);
130 $sOutput .= '] }';
131
132 return $sOutput;
133 }
134}
135