drupal-civicrm/sites/all/modules/civicrm/CRM/Utils/JSON.php

96 lines
3.5 KiB
PHP
Raw Normal View History

2018-01-14 15:10:16 +02:00
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.7 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2017 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
*
* @package CRM
* @copyright CiviCRM LLC (c) 2004-2017
*/
/**
* Class handles functions for JSON format
*/
class CRM_Utils_JSON {
/**
* Output json to the client.
* @param mixed $input
*/
public static function output($input) {
CRM_Utils_System::setHttpHeader('Content-Type', 'application/json');
echo json_encode($input);
CRM_Utils_System::civiExit();
}
/**
* Do not use this function. See CRM-16353.
* @deprecated
*
* @param array $params
* Associated array of row elements.
* @param int $sEcho
* Datatable needs this to make it more secure.
* @param int $iTotal
* Total records.
* @param int $iFilteredTotal
* Total records on a page.
* @param array $selectorElements
* Selector elements.
* @return string
*/
public static function encodeDataTableSelector($params, $sEcho, $iTotal, $iFilteredTotal, $selectorElements) {
$sOutput = '{';
$sOutput .= '"sEcho": ' . intval($sEcho) . ', ';
$sOutput .= '"iTotalRecords": ' . $iTotal . ', ';
$sOutput .= '"iTotalDisplayRecords": ' . $iFilteredTotal . ', ';
$sOutput .= '"aaData": [ ';
foreach ((array) $params as $key => $value) {
$addcomma = FALSE;
$sOutput .= "[";
foreach ($selectorElements as $element) {
if ($addcomma) {
$sOutput .= ",";
}
// CRM-7130 --lets addslashes to only double quotes,
// since we are using it to quote the field value.
// str_replace helps to provide a break for new-line
$sOutput .= '"' . addcslashes(str_replace(array("\r\n", "\n", "\r"), '<br />', $value[$element]), '"\\') . '"';
// remove extra spaces and tab character that breaks dataTable CRM-12551
$sOutput = preg_replace("/\s+/", " ", $sOutput);
$addcomma = TRUE;
}
$sOutput .= "],";
}
$sOutput = substr_replace($sOutput, "", -1);
$sOutput .= '] }';
return $sOutput;
}
}