3 trait Pman_Core_JsonOutputTrait {
9 if ($this->transObj ) {
10 $this->transObj->query( connection_aborted() ? 'ROLLBACK' : 'COMMIT');
13 $cli = HTML_FlexyFramework::get()->cli;
15 echo "OK: " .$str . "\n";
19 $retHTML = isset($_SERVER['CONTENT_TYPE']) &&
20 preg_match('#multipart/form-data#i', $_SERVER['CONTENT_TYPE']);
23 if (isset($_REQUEST['returnHTML']) && $_REQUEST['returnHTML'] == 'NO') {
27 $retHTML = isset($_REQUEST['returnHTML']) && $_REQUEST['returnHTML'] !='NO';
31 header('Content-type: text/html');
32 echo "<HTML><HEAD></HEAD><BODY>";
33 // encode html characters so they can be read..
34 echo str_replace(array('<','>'), array('\u003c','\u003e'),
35 $this->jsencode(array('success'=> true, 'data' => $str), false));
36 echo "</BODY></HTML>";
41 echo $this->jsencode(array('success'=> true, 'data' => $str),true);
49 * ---------------- Standard JSON outputers. - used everywhere
50 * JSON error - simple error with logging.
54 function jerr($str, $errors=array(), $content_type = false) // standard error reporting..
56 return $this->jerror('ERROR', $str,$errors,$content_type);
59 function jnotice($type, $str, $errors=array(), $content_type = false)
61 return $this->jerror('NOTICE-' . $type, $str, $errors, $content_type);
65 * jerrAuth: standard auth failure - with data that let's the UI know..
69 $au = $this->authUser;
71 // is it an authfailure?
72 $this->jerror("LOGIN-NOPERM", "Permission denied to view this resource", array('authFailure' => true));
74 $this->jerror("LOGIN-NOAUTH", "Not authenticated", array('authFailure' => true));
79 * Recomended JSON error indicator
82 * @param string $type - normally 'ERROR' - you can use this to track error types.
83 * @param string $message - error message displayed to user.
84 * @param array $errors - optioanl data to pass to front end.
85 * @param string $content_type - use text/plain to return plan text - ?? not sure why...
89 function jerror($type, $str, $errors=array(), $content_type = false) // standard error reporting..
91 if ($this->transObj) {
92 $this->transObj->query('ROLLBACK');
95 $cli = HTML_FlexyFramework::get()->cli;
97 echo "ERROR: " .$str . "\n"; // print the error first, as DB might fail..
99 $pman = HTML_FlexyFramework::get();
105 if ($type !== false && empty($pman->nodatabase)) {
108 DB_DataObject::factory('Events')->writeEventLogExtra($errors);
110 // various codes that are acceptable.
112 if (!preg_match('/^(ERROR|NOTICE|LOG)/', $type )) {
113 $type = 'ERROR-' . $type;
116 $this->addEvent($type, false, $str);
120 $cli = HTML_FlexyFramework::get()->cli;
122 exit(1); // cli --- exit code to stop shell execution if necessary.
126 if ($content_type == 'text/plain') {
127 header('Content-Disposition: attachment; filename="error.txt"');
128 header('Content-type: '. $content_type);
129 echo "ERROR: " .$str . "\n";
135 $retHTML = isset($_SERVER['CONTENT_TYPE']) &&
136 preg_match('#multipart/form-data#i', $_SERVER['CONTENT_TYPE']);
139 if (isset($_REQUEST['returnHTML']) && $_REQUEST['returnHTML'] == 'NO') {
143 $retHTML = isset($_REQUEST['returnHTML']) && $_REQUEST['returnHTML'] !='NO';
148 header('Content-type: text/html');
149 echo "<HTML><HEAD></HEAD><BODY>";
150 echo $this->jsencode(array(
153 'message' => $str, // compate with exeption / loadexception.
155 'errors' => $errors ? $errors : true, // used by forms to flag errors.
156 'authFailure' => !empty($errors['authFailure']),
158 echo "</BODY></HTML>";
162 if (isset($_REQUEST['_debug'])) {
163 echo '<PRE>'.htmlspecialchars(print_r(array(
167 'message' => $str, // compate with exeption / loadexception.
168 'errors' => $errors ? $errors : true, // used by forms to flag errors.
169 'authFailure' => !empty($errors['authFailure']),
175 echo $this->jsencode(array(
180 'message' => $str, // compate with exeption / loadexception.
181 'errors' => $errors ? $errors : true, // used by forms to flag errors.
182 'authFailure' => !empty($errors['authFailure']),
196 * output data for grids or tree
197 * @ar {Array} ar Array of data
198 * @total {Number|false} total number of records (or false to return count(ar)
199 * @extra {Array} extra key value list of data to pass as extra data.
202 function jdata($ar,$total=false, $extra=array(), $cachekey = false)
204 // should do mobile checking???
205 if ($total == false) {
208 $extra= $extra ? $extra : array();
211 $retHTML = isset($_SERVER['CONTENT_TYPE']) &&
212 preg_match('#multipart/form-data#i', $_SERVER['CONTENT_TYPE']);
215 if (isset($_REQUEST['returnHTML']) && $_REQUEST['returnHTML'] == 'NO') {
219 $retHTML = isset($_REQUEST['returnHTML']) && $_REQUEST['returnHTML'] !='NO';
224 header('Content-type: text/html');
225 echo "<HTML><HEAD></HEAD><BODY><![CDATA[";
226 // encode html characters so they can be read..
227 echo str_replace(array('<','>'), array('\u003c','\u003e'),
228 $this->jsencode(array('success' => true, 'total'=> $total, 'data' => $ar) + $extra, false));
229 echo "]]></BODY></HTML>";
234 // see if trimming will help...
235 if (!empty($_REQUEST['_pman_short'])) {
238 foreach($ar as $as) {
240 foreach($as as $k=>$v) {
241 if (is_string($v) && !strlen(trim($v))) {
253 $ret = $this->jsencode(array('success' => true, 'total'=> $total, 'data' => $ar) + $extra,true);
255 if (!empty($cachekey)) {
257 $fn = ini_get('session.save_path') . '/json-cache'.date('/Y/m/d').'.'. $cachekey . '.cache.json';
258 if (!file_exists(dirname($fn))) {
259 mkdir(dirname($fn), 0777,true);
261 file_put_contents($fn, $ret);
270 /** a daily cache **/
271 function jdataCache($cachekey)
273 $fn = ini_get('session.save_path') . '/json-cache'.date('/Y/m/d').'.'. $cachekey . '.cache.json';
274 if (file_exists($fn)) {
275 header('Content-type: application/json');
276 echo file_get_contents($fn);
283 function jsencode($v, $header = false)
286 header("Content-type: text/javascript");
288 if (function_exists("json_encode")) {
289 $ret= json_encode($v);
290 if ($ret !== false) {
294 require_once 'Services/JSON.php';
295 $js = new Services_JSON();
296 return $js->encodeUnsafe($v);