3 trait Pman_Core_JsonOutputTrait {
7 if ($this->transObj ) {
8 $this->transObj->query( connection_aborted() ? 'ROLLBACK' : 'COMMIT');
11 $cli = HTML_FlexyFramework::get()->cli;
13 echo "OK: " .$str . "\n";
17 $retHTML = isset($_SERVER['CONTENT_TYPE']) &&
18 preg_match('#multipart/form-data#i', $_SERVER['CONTENT_TYPE']);
21 if (isset($_REQUEST['returnHTML']) && $_REQUEST['returnHTML'] == 'NO') {
25 $retHTML = isset($_REQUEST['returnHTML']) && $_REQUEST['returnHTML'] !='NO';
29 header('Content-type: text/html');
30 echo "<HTML><HEAD></HEAD><BODY>";
31 // encode html characters so they can be read..
32 echo str_replace(array('<','>'), array('\u003c','\u003e'),
33 $this->jsencode(array('success'=> true, 'data' => $str), false));
34 echo "</BODY></HTML>";
39 echo $this->jsencode(array('success'=> true, 'data' => $str),true);
47 * ---------------- Standard JSON outputers. - used everywhere
48 * JSON error - simple error with logging.
52 function jerr($str, $errors=array(), $content_type = false) // standard error reporting..
54 return $this->jerror('ERROR', $str,$errors,$content_type);
57 function jnotice($type, $str, $errors=array(), $content_type = false)
59 return $this->jerror('NOTICE-' . $type, $str, $errors, $content_type);
63 * Recomended JSON error indicator
66 * @param string $type - normally 'ERROR' - you can use this to track error types.
67 * @param string $message - error message displayed to user.
68 * @param array $errors - optioanl data to pass to front end.
69 * @param string $content_type - use text/plain to return plan text - ?? not sure why...
73 function jerror($type, $str, $errors=array(), $content_type = false) // standard error reporting..
75 if ($this->transObj) {
76 $this->transObj->query('ROLLBACK');
79 $cli = HTML_FlexyFramework::get()->cli;
81 echo "ERROR: " .$str . "\n"; // print the error first, as DB might fail..
83 $pman = HTML_FlexyFramework::get();
89 if ($type !== false && empty($pman->nodatabase)) {
92 DB_DataObject::factory('Events')->writeEventLogExtra($errors);
94 // various codes that are acceptable.
96 if (!preg_match('/^(ERROR|NOTICE|LOG)/', $type )) {
97 $type = 'ERROR-' . $type;
100 $this->addEvent($type, false, $str);
104 $cli = HTML_FlexyFramework::get()->cli;
106 exit(1); // cli --- exit code to stop shell execution if necessary.
110 if ($content_type == 'text/plain') {
111 header('Content-Disposition: attachment; filename="error.txt"');
112 header('Content-type: '. $content_type);
113 echo "ERROR: " .$str . "\n";
119 $retHTML = isset($_SERVER['CONTENT_TYPE']) &&
120 preg_match('#multipart/form-data#i', $_SERVER['CONTENT_TYPE']);
123 if (isset($_REQUEST['returnHTML']) && $_REQUEST['returnHTML'] == 'NO') {
127 $retHTML = isset($_REQUEST['returnHTML']) && $_REQUEST['returnHTML'] !='NO';
132 header('Content-type: text/html');
133 echo "<HTML><HEAD></HEAD><BODY>";
134 echo $this->jsencode(array(
137 'message' => $str, // compate with exeption / loadexception.
139 'errors' => $errors ? $errors : true, // used by forms to flag errors.
140 'authFailure' => !empty($errors['authFailure']),
142 echo "</BODY></HTML>";
146 if (isset($_REQUEST['_debug'])) {
147 echo '<PRE>'.htmlspecialchars(print_r(array(
151 'message' => $str, // compate with exeption / loadexception.
152 'errors' => $errors ? $errors : true, // used by forms to flag errors.
153 'authFailure' => !empty($errors['authFailure']),
159 echo $this->jsencode(array(
164 'message' => $str, // compate with exeption / loadexception.
165 'errors' => $errors ? $errors : true, // used by forms to flag errors.
166 'authFailure' => !empty($errors['authFailure']),
178 function jdata($ar,$total=false, $extra=array(), $cachekey = false)
180 // should do mobile checking???
181 if ($total == false) {
184 $extra= $extra ? $extra : array();
185 require_once 'Services/JSON.php';
186 $json = new Services_JSON();
188 $retHTML = isset($_SERVER['CONTENT_TYPE']) &&
189 preg_match('#multipart/form-data#i', $_SERVER['CONTENT_TYPE']);
192 if (isset($_REQUEST['returnHTML']) && $_REQUEST['returnHTML'] == 'NO') {
196 $retHTML = isset($_REQUEST['returnHTML']) && $_REQUEST['returnHTML'] !='NO';
201 header('Content-type: text/html');
202 echo "<HTML><HEAD></HEAD><BODY>";
203 // encode html characters so they can be read..
204 echo str_replace(array('<','>'), array('\u003c','\u003e'),
205 $json->encodeUnsafe(array('success' => true, 'total'=> $total, 'data' => $ar) + $extra));
206 echo "</BODY></HTML>";
211 // see if trimming will help...
212 if (!empty($_REQUEST['_pman_short'])) {
215 foreach($ar as $as) {
217 foreach($as as $k=>$v) {
218 if (is_string($v) && !strlen(trim($v))) {
230 $ret = $json->encode(array('success' => true, 'total'=> $total, 'data' => $ar) + $extra);
232 if (!empty($cachekey)) {
234 $fn = ini_get('session.save_path') . '/json-cache'.date('/Y/m/d').'.'. $cachekey . '.cache.json';
235 if (!file_exists(dirname($fn))) {
236 mkdir(dirname($fn), 0777,true);
238 file_put_contents($fn, $ret);
244 /** a daily cache **/
245 function jdataCache($cachekey)
247 $fn = ini_get('session.save_path') . '/json-cache'.date('/Y/m/d').'.'. $cachekey . '.cache.json';
248 if (file_exists($fn)) {
249 header('Content-type: application/json');
250 echo file_get_contents($fn);