RooJsonOutputTrait.php
[Pman.Core] / RooJsonOutputTrait.php
1 <?php
2
3 trait Pman_Core_RooJsonOutputTrait {
4     
5     function jok($str)
6     {
7         if ($this->transObj ) {
8             $this->transObj->query( connection_aborted() ? 'ROLLBACK' :  'COMMIT');
9         }
10         
11         $cli = HTML_FlexyFramework::get()->cli;
12         
13         if ($cli) {
14             echo "OK: " .$str . "\n";
15             exit;
16         }
17         require_once 'Services/JSON.php';
18         $json = new Services_JSON();
19         
20         $retHTML = isset($_SERVER['CONTENT_TYPE']) && 
21                 preg_match('#multipart/form-data#i', $_SERVER['CONTENT_TYPE']);
22         
23         if ($retHTML){
24             if (isset($_REQUEST['returnHTML']) && $_REQUEST['returnHTML'] == 'NO') {
25                 $retHTML = false;
26             }
27         } else {
28             $retHTML = isset($_REQUEST['returnHTML']) && $_REQUEST['returnHTML'] !='NO';
29         }
30         
31         if ($retHTML) {
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>";
38             exit;
39         }
40         
41         
42         echo  $json->encode(array('success'=> true, 'data' => $str));
43         
44         exit;
45     }
46     
47     
48     function jerr($str, $errors=array(), $content_type = false)
49     {
50         if ($this->transObj) {
51             $this->transObj->query('ROLLBACK');
52         }
53         
54         return $this->jerror('ERROR', $str,$errors,$content_type);
55     }
56     
57     function jerror($type, $str, $errors=array(), $content_type = false) // standard error reporting..
58     {
59         if ($type !== false) {
60             $this->addEvent($type, false, $str);
61         }
62          
63         $cli = HTML_FlexyFramework::get()->cli;
64         if ($cli) {
65             echo "ERROR: " .$str . "\n";
66             exit;
67         }
68         
69         
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";
74             exit;
75         } 
76         
77         require_once 'Services/JSON.php';
78         $json = new Services_JSON();
79         
80         $retHTML = isset($_SERVER['CONTENT_TYPE']) && 
81                 preg_match('#multipart/form-data#i', $_SERVER['CONTENT_TYPE']);
82         
83         if ($retHTML){
84             if (isset($_REQUEST['returnHTML']) && $_REQUEST['returnHTML'] == 'NO') {
85                 $retHTML = false;
86             }
87         } else {
88             $retHTML = isset($_REQUEST['returnHTML']) && $_REQUEST['returnHTML'] !='NO';
89         }
90         
91         if ($retHTML) {
92             header('Content-type: text/html');
93             echo "<HTML><HEAD></HEAD><BODY>";
94             echo  $json->encodeUnsafe(array(
95                     'success'=> false, 
96                     'errorMsg' => $str,
97                     'message' => $str, // compate with exeption / loadexception.
98
99                     'errors' => $errors ? $errors : true, // used by forms to flag errors.
100                     'authFailure' => !empty($errors['authFailure']),
101                 ));
102             echo "</BODY></HTML>";
103             exit;
104         }
105         
106         if (isset($_REQUEST['_debug'])) {
107             echo '<PRE>'.htmlspecialchars(print_r(array(
108                 'success'=> false, 
109                 'data'=> array(), 
110                 'errorMsg' => $str,
111                 'message' => $str, // compate with exeption / loadexception.
112                 'errors' => $errors ? $errors : true, // used by forms to flag errors.
113                 'authFailure' => !empty($errors['authFailure']),
114             ),true));
115             exit;
116                 
117         }
118         
119         echo $json->encode(array(
120             'success'=> false, 
121             'data'=> array(), 
122             'errorMsg' => $str,
123             'message' => $str, // compate with exeption / loadexception.
124             'errors' => $errors ? $errors : true, // used by forms to flag errors.
125             'authFailure' => !empty($errors['authFailure']),
126         ));
127         
128         exit;
129         
130     }
131     
132     function jdata($ar,$total=false, $extra=array(), $cachekey = false)
133     {
134         if ($this->transObj ) {
135             $this->transObj->query( connection_aborted() ? 'ROLLBACK' :  'COMMIT');
136         }
137         
138         // should do mobile checking???
139         if ($total == false) {
140             $total = count($ar);
141         }
142         $extra=  $extra ? $extra : array();
143         require_once 'Services/JSON.php';
144         $json = new Services_JSON();
145         
146         $retHTML = isset($_SERVER['CONTENT_TYPE']) && 
147                 preg_match('#multipart/form-data#i', $_SERVER['CONTENT_TYPE']);
148         
149         if ($retHTML){
150             if (isset($_REQUEST['returnHTML']) && $_REQUEST['returnHTML'] == 'NO') {
151                 $retHTML = false;
152             }
153         } else {
154             $retHTML = isset($_REQUEST['returnHTML']) && $_REQUEST['returnHTML'] !='NO';
155         }
156         
157         if ($retHTML) {
158             
159             header('Content-type: text/html');
160             echo "<HTML><HEAD></HEAD><BODY>";
161             // encode html characters so they can be read..
162             echo  str_replace(array('<','>'), array('\u003c','\u003e'),
163                         $json->encodeUnsafe(array('success' =>  true, 'total'=> $total, 'data' => $ar) + $extra));
164             echo "</BODY></HTML>";
165             exit;
166         }
167         
168         
169         // see if trimming will help...
170         if (!empty($_REQUEST['_pman_short'])) {
171             $nar = array();
172             
173             foreach($ar as $as) {
174                 $add = array();
175                 foreach($as as $k=>$v) {
176                     if (is_string($v) && !strlen(trim($v))) {
177                         continue;
178                     }
179                     $add[$k] = $v;
180                 }
181                 $nar[] = $add;
182             }
183             $ar = $nar;
184               
185         }
186         
187       
188         $ret =  $json->encode(array('success' =>  true, 'total'=> $total, 'data' => $ar) + $extra);  
189         
190         if (!empty($cachekey)) {
191             
192             $fn = ini_get('session.save_path') . '/json-cache'.date('/Y/m/d').'.'. $cachekey . '.cache.json';
193             if (!file_exists(dirname($fn))) {
194                 mkdir(dirname($fn), 0777,true);
195             }
196             file_put_contents($fn, $ret);
197         }
198         echo $ret;
199         exit;
200     }
201     
202     /** a daily cache **/
203     function jdataCache($cachekey)
204     {
205         $fn = ini_get('session.save_path') . '/json-cache'.date('/Y/m/d').'.'. $cachekey . '.cache.json';
206         if (file_exists($fn)) {
207             header('Content-type: application/json');
208             echo file_get_contents($fn);
209             exit;
210         }
211         return false;
212         
213     }
214 }