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 * jerrAuth: standard auth failure - with data that let's the UI know..
67 $au = $this->authUser;
69 // is it an authfailure?
70 $this->jerror("LOGIN-NOPERM", "Permission denied to view this resource", array('authFailure' => true));
72 $this->jerror("LOGIN-NOAUTH", "Not authenticated", array('authFailure' => true));
77 * Recomended JSON error indicator
80 * @param string $type - normally 'ERROR' - you can use this to track error types.
81 * @param string $message - error message displayed to user.
82 * @param array $errors - optioanl data to pass to front end.
83 * @param string $content_type - use text/plain to return plan text - ?? not sure why...
87 function jerror($type, $str, $errors=array(), $content_type = false) // standard error reporting..
89 if ($this->transObj) {
90 $this->transObj->query('ROLLBACK');
93 $cli = HTML_FlexyFramework::get()->cli;
95 echo "ERROR: " .$str . "\n"; // print the error first, as DB might fail..
97 $pman = HTML_FlexyFramework::get();
103 if ($type !== false && empty($pman->nodatabase)) {
106 DB_DataObject::factory('Events')->writeEventLogExtra($errors);
108 // various codes that are acceptable.
110 if (!preg_match('/^(ERROR|NOTICE|LOG)/', $type )) {
111 $type = 'ERROR-' . $type;
114 $this->addEvent($type, false, $str);
118 $cli = HTML_FlexyFramework::get()->cli;
120 exit(1); // cli --- exit code to stop shell execution if necessary.
124 if ($content_type == 'text/plain') {
125 header('Content-Disposition: attachment; filename="error.txt"');
126 header('Content-type: '. $content_type);
127 echo "ERROR: " .$str . "\n";
133 $retHTML = isset($_SERVER['CONTENT_TYPE']) &&
134 preg_match('#multipart/form-data#i', $_SERVER['CONTENT_TYPE']);
137 if (isset($_REQUEST['returnHTML']) && $_REQUEST['returnHTML'] == 'NO') {
141 $retHTML = isset($_REQUEST['returnHTML']) && $_REQUEST['returnHTML'] !='NO';
146 header('Content-type: text/html');
147 echo "<HTML><HEAD></HEAD><BODY>";
148 echo $this->jsencode(array(
151 'message' => $str, // compate with exeption / loadexception.
153 'errors' => $errors ? $errors : true, // used by forms to flag errors.
154 'authFailure' => !empty($errors['authFailure']),
156 echo "</BODY></HTML>";
160 if (isset($_REQUEST['_debug'])) {
161 echo '<PRE>'.htmlspecialchars(print_r(array(
165 'message' => $str, // compate with exeption / loadexception.
166 'errors' => $errors ? $errors : true, // used by forms to flag errors.
167 'authFailure' => !empty($errors['authFailure']),
173 echo $this->jsencode(array(
178 'message' => $str, // compate with exeption / loadexception.
179 'errors' => $errors ? $errors : true, // used by forms to flag errors.
180 'authFailure' => !empty($errors['authFailure']),
194 * output data for grids or tree
195 * @ar {Array} ar Array of data
196 * @total {Number|false} total number of records (or false to return count(ar)
197 * @extra {Array} extra key value list of data to pass as extra data.
200 function jdata($ar,$total=false, $extra=array(), $cachekey = false)
202 // should do mobile checking???
203 if ($total == false) {
206 $extra= $extra ? $extra : array();
209 $retHTML = isset($_SERVER['CONTENT_TYPE']) &&
210 preg_match('#multipart/form-data#i', $_SERVER['CONTENT_TYPE']);
213 if (isset($_REQUEST['returnHTML']) && $_REQUEST['returnHTML'] == 'NO') {
217 $retHTML = isset($_REQUEST['returnHTML']) && $_REQUEST['returnHTML'] !='NO';
222 header('Content-type: text/html');
223 echo "<HTML><HEAD></HEAD><BODY><![CDATA[";
224 // encode html characters so they can be read..
225 echo str_replace(array('<','>'), array('\u003c','\u003e'),
226 $this->jsencode(array('success' => true, 'total'=> $total, 'data' => $ar) + $extra, false));
227 echo "]]></BODY></HTML>";
232 // see if trimming will help...
233 if (!empty($_REQUEST['_pman_short'])) {
236 foreach($ar as $as) {
238 foreach($as as $k=>$v) {
239 if (is_string($v) && !strlen(trim($v))) {
251 $ret = $this->jsencode(array('success' => true, 'total'=> $total, 'data' => $ar) + $extra,true);
253 if (!empty($cachekey)) {
255 $fn = ini_get('session.save_path') . '/json-cache'.date('/Y/m/d').'.'. $cachekey . '.cache.json';
256 if (!file_exists(dirname($fn))) {
257 mkdir(dirname($fn), 0777,true);
259 file_put_contents($fn, $ret);
268 /** a daily cache **/
269 function jdataCache($cachekey)
271 $fn = ini_get('session.save_path') . '/json-cache'.date('/Y/m/d').'.'. $cachekey . '.cache.json';
272 if (file_exists($fn)) {
273 header('Content-type: application/json');
274 echo file_get_contents($fn);