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']),
180 * output data for grids or tree
181 * @ar {Array} ar Array of data
182 * @total {Number|false} total number of records (or false to return count(ar)
183 * @extra {Array} extra key value list of data to pass as extra data.
186 function jdata($ar,$total=false, $extra=array(), $cachekey = false)
188 // should do mobile checking???
189 if ($total == false) {
192 $extra= $extra ? $extra : array();
195 $retHTML = isset($_SERVER['CONTENT_TYPE']) &&
196 preg_match('#multipart/form-data#i', $_SERVER['CONTENT_TYPE']);
199 if (isset($_REQUEST['returnHTML']) && $_REQUEST['returnHTML'] == 'NO') {
203 $retHTML = isset($_REQUEST['returnHTML']) && $_REQUEST['returnHTML'] !='NO';
208 header('Content-type: text/html');
209 echo "<HTML><HEAD></HEAD><BODY><![CDATA[";
210 // encode html characters so they can be read..
211 echo str_replace(array('<','>'), array('\u003c','\u003e'),
212 $this->jsencode(array('success' => true, 'total'=> $total, 'data' => $ar) + $extra, false));
213 echo "]]></BODY></HTML>";
218 // see if trimming will help...
219 if (!empty($_REQUEST['_pman_short'])) {
222 foreach($ar as $as) {
224 foreach($as as $k=>$v) {
225 if (is_string($v) && !strlen(trim($v))) {
237 $ret = $this->jsencode(array('success' => true, 'total'=> $total, 'data' => $ar) + $extra,true);
239 if (!empty($cachekey)) {
241 $fn = ini_get('session.save_path') . '/json-cache'.date('/Y/m/d').'.'. $cachekey . '.cache.json';
242 if (!file_exists(dirname($fn))) {
243 mkdir(dirname($fn), 0777,true);
245 file_put_contents($fn, $ret);
254 /** a daily cache **/
255 function jdataCache($cachekey)
257 $fn = ini_get('session.save_path') . '/json-cache'.date('/Y/m/d').'.'. $cachekey . '.cache.json';
258 if (file_exists($fn)) {
259 header('Content-type: application/json');
260 echo file_get_contents($fn);