3 trait Pman_Core_RooJsonOutputTrait {
7 if ($this->transObj ) {
8 $this->transObj->query( connection_aborted() ? 'ROLLBACK' : 'COMMIT');
11 $cli = HTML_FlexyFramework::get()->cli;
14 echo "OK: " .$str . "\n";
17 require_once 'Services/JSON.php';
18 $json = new Services_JSON();
20 $retHTML = isset($_SERVER['CONTENT_TYPE']) &&
21 preg_match('#multipart/form-data#i', $_SERVER['CONTENT_TYPE']);
24 if (isset($_REQUEST['returnHTML']) && $_REQUEST['returnHTML'] == 'NO') {
28 $retHTML = isset($_REQUEST['returnHTML']) && $_REQUEST['returnHTML'] !='NO';
32 header('Content-type: text/html');
33 echo "<HTML><HEAD></HEAD><BODY>";
34 // encode html characters so they can be read..
35 echo str_replace(array('<','>'), array('\u003c','\u003e'),
36 $json->encodeUnsafe(array('success'=> true, 'data' => $str)));
37 echo "</BODY></HTML>";
42 echo $json->encode(array('success'=> true, 'data' => $str));
48 function jerr($str, $errors=array(), $content_type = false)
50 if ($this->transObj) {
51 $this->transObj->query('ROLLBACK');
54 return $this->jerror('ERROR', $str,$errors,$content_type);
57 function jerror($type, $str, $errors=array(), $content_type = false) // standard error reporting..
59 if ($type !== false) {
60 $this->addEvent($type, false, $str);
63 $cli = HTML_FlexyFramework::get()->cli;
65 echo "ERROR: " .$str . "\n";
70 if ($content_type == 'text/plain') {
71 header('Content-Disposition: attachment; filename="error.txt"');
72 header('Content-type: '. $content_type);
73 echo "ERROR: " .$str . "\n";
77 require_once 'Services/JSON.php';
78 $json = new Services_JSON();
80 $retHTML = isset($_SERVER['CONTENT_TYPE']) &&
81 preg_match('#multipart/form-data#i', $_SERVER['CONTENT_TYPE']);
84 if (isset($_REQUEST['returnHTML']) && $_REQUEST['returnHTML'] == 'NO') {
88 $retHTML = isset($_REQUEST['returnHTML']) && $_REQUEST['returnHTML'] !='NO';
92 header('Content-type: text/html');
93 echo "<HTML><HEAD></HEAD><BODY>";
94 echo $json->encodeUnsafe(array(
97 'message' => $str, // compate with exeption / loadexception.
99 'errors' => $errors ? $errors : true, // used by forms to flag errors.
100 'authFailure' => !empty($errors['authFailure']),
102 echo "</BODY></HTML>";
106 if (isset($_REQUEST['_debug'])) {
107 echo '<PRE>'.htmlspecialchars(print_r(array(
111 'message' => $str, // compate with exeption / loadexception.
112 'errors' => $errors ? $errors : true, // used by forms to flag errors.
113 'authFailure' => !empty($errors['authFailure']),
119 echo $json->encode(array(
123 'message' => $str, // compate with exeption / loadexception.
124 'errors' => $errors ? $errors : true, // used by forms to flag errors.
125 'authFailure' => !empty($errors['authFailure']),
132 function jdata($ar,$total=false, $extra=array(), $cachekey = false)
134 // should do mobile checking???
135 if ($total == false) {
138 $extra= $extra ? $extra : array();
139 require_once 'Services/JSON.php';
140 $json = new Services_JSON();
142 $retHTML = isset($_SERVER['CONTENT_TYPE']) &&
143 preg_match('#multipart/form-data#i', $_SERVER['CONTENT_TYPE']);
146 if (isset($_REQUEST['returnHTML']) && $_REQUEST['returnHTML'] == 'NO') {
150 $retHTML = isset($_REQUEST['returnHTML']) && $_REQUEST['returnHTML'] !='NO';
155 header('Content-type: text/html');
156 echo "<HTML><HEAD></HEAD><BODY>";
157 // encode html characters so they can be read..
158 echo str_replace(array('<','>'), array('\u003c','\u003e'),
159 $json->encodeUnsafe(array('success' => true, 'total'=> $total, 'data' => $ar) + $extra));
160 echo "</BODY></HTML>";
165 // see if trimming will help...
166 if (!empty($_REQUEST['_pman_short'])) {
169 foreach($ar as $as) {
171 foreach($as as $k=>$v) {
172 if (is_string($v) && !strlen(trim($v))) {
184 $ret = $json->encode(array('success' => true, 'total'=> $total, 'data' => $ar) + $extra);
186 if (!empty($cachekey)) {
188 $fn = ini_get('session.save_path') . '/json-cache'.date('/Y/m/d').'.'. $cachekey . '.cache.json';
189 if (!file_exists(dirname($fn))) {
190 mkdir(dirname($fn), 0777,true);
192 file_put_contents($fn, $ret);
198 /** a daily cache **/
199 function jdataCache($cachekey)
201 $fn = ini_get('session.save_path') . '/json-cache'.date('/Y/m/d').'.'. $cachekey . '.cache.json';
202 if (file_exists($fn)) {
203 header('Content-type: application/json');
204 echo file_get_contents($fn);