| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.5 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
| 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-2014 |
| 32 | * $Id$ |
| 33 | * |
| 34 | */ |
| 35 | |
| 36 | /** |
| 37 | * Class handles functions for JSON format |
| 38 | */ |
| 39 | class 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 $total |
| 69 | * @param array $selectorElements selector rows |
| 70 | * |
| 71 | * @return json encode string |
| 72 | */ |
| 73 | static function encodeSelector(&$params, $page, $total, $selectorElements) { |
| 74 | $json = ""; |
| 75 | $json .= "{\n"; |
| 76 | $json .= "page: $page,\n"; |
| 77 | $json .= "total: $total,\n"; |
| 78 | $json .= "rows: ["; |
| 79 | $rc = FALSE; |
| 80 | |
| 81 | foreach ($params as $key => $value) { |
| 82 | if ($rc) { |
| 83 | $json .= ","; |
| 84 | } |
| 85 | $json .= "\n{"; |
| 86 | $json .= "id:'" . $value['id'] . "',"; |
| 87 | $json .= "cell:["; |
| 88 | $addcomma = FALSE; |
| 89 | foreach ($selectorElements as $element) { |
| 90 | if ($addcomma) { |
| 91 | $json .= ","; |
| 92 | } |
| 93 | $json .= "'" . addslashes($value[$element]) . "'"; |
| 94 | $addcomma = TRUE; |
| 95 | } |
| 96 | $json .= "]}"; |
| 97 | $rc = TRUE; |
| 98 | } |
| 99 | |
| 100 | $json .= "]\n"; |
| 101 | $json .= "}"; |
| 102 | |
| 103 | return $json; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * This function is used to encode data for dataTable plugin |
| 108 | * |
| 109 | * @param array $params associated array of row elements |
| 110 | * @param int $sEcho datatable needs this to make it more secure |
| 111 | * @param int $iTotal total records |
| 112 | * @param int $iFilteredTotal total records on a page |
| 113 | * @param array $selectorElements selector elements |
| 114 | * @return string |
| 115 | * |
| 116 | */ |
| 117 | static function encodeDataTableSelector($params, $sEcho, $iTotal, $iFilteredTotal, $selectorElements) { |
| 118 | $sOutput = '{'; |
| 119 | $sOutput .= '"sEcho": ' . intval($sEcho) . ', '; |
| 120 | $sOutput .= '"iTotalRecords": ' . $iTotal . ', '; |
| 121 | $sOutput .= '"iTotalDisplayRecords": ' . $iFilteredTotal . ', '; |
| 122 | $sOutput .= '"aaData": [ '; |
| 123 | foreach ($params as $key => $value) { |
| 124 | $addcomma = FALSE; |
| 125 | $sOutput .= "["; |
| 126 | foreach ($selectorElements as $element) { |
| 127 | if ($addcomma) { |
| 128 | $sOutput .= ","; |
| 129 | } |
| 130 | //CRM-7130 --lets addslashes to only double quotes, |
| 131 | //since we are using it to quote the field value. |
| 132 | //str_replace helps to provide a break for new-line |
| 133 | $sOutput .= '"' . addcslashes(str_replace(array("\r\n", "\n", "\r"), '<br />', $value[$element]), '"\\') . '"'; |
| 134 | |
| 135 | //remove extra spaces and tab character that breaks dataTable CRM-12551 |
| 136 | $sOutput = preg_replace("/\s+/", " ", $sOutput); |
| 137 | $addcomma = TRUE; |
| 138 | } |
| 139 | $sOutput .= "],"; |
| 140 | } |
| 141 | $sOutput = substr_replace($sOutput, "", -1); |
| 142 | $sOutput .= '] }'; |
| 143 | |
| 144 | return $sOutput; |
| 145 | } |
| 146 | } |
| 147 | |