SimpleExcel.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         // should do mobile checking???
135         if ($total == false) {
136             $total = count($ar);
137         }
138         $extra=  $extra ? $extra : array();
139         require_once 'Services/JSON.php';
140         $json = new Services_JSON();
141         
142         $retHTML = isset($_SERVER['CONTENT_TYPE']) && 
143                 preg_match('#multipart/form-data#i', $_SERVER['CONTENT_TYPE']);
144         
145         if ($retHTML){
146             if (isset($_REQUEST['returnHTML']) && $_REQUEST['returnHTML'] == 'NO') {
147                 $retHTML = false;
148             }
149         } else {
150             $retHTML = isset($_REQUEST['returnHTML']) && $_REQUEST['returnHTML'] !='NO';
151         }
152         
153         if ($retHTML) {
154             
155             header('Content-type: text/html');
156             echo "<HTML><HEAD></HEAD><BODY>";
157             // encode html characters so they can be read..
158             echo  str_replace(array('<','>'), array('\u003c','\u003e'),
159                         $json->encodeUnsafe(array('success' =>  true, 'total'=> $total, 'data' => $ar) + $extra));
160             echo "</BODY></HTML>";
161             exit;
162         }
163         
164         
165         // see if trimming will help...
166         if (!empty($_REQUEST['_pman_short'])) {
167             $nar = array();
168             
169             foreach($ar as $as) {
170                 $add = array();
171                 foreach($as as $k=>$v) {
172                     if (is_string($v) && !strlen(trim($v))) {
173                         continue;
174                     }
175                     $add[$k] = $v;
176                 }
177                 $nar[] = $add;
178             }
179             $ar = $nar;
180               
181         }
182         
183       
184         $ret =  $json->encode(array('success' =>  true, 'total'=> $total, 'data' => $ar) + $extra);  
185         
186         if (!empty($cachekey)) {
187             
188             $fn = ini_get('session.save_path') . '/json-cache'.date('/Y/m/d').'.'. $cachekey . '.cache.json';
189             if (!file_exists(dirname($fn))) {
190                 mkdir(dirname($fn), 0777,true);
191             }
192             file_put_contents($fn, $ret);
193         }
194         echo $ret;
195         exit;
196     }
197     
198     /** a daily cache **/
199     function jdataCache($cachekey)
200     {
201         $fn = ini_get('session.save_path') . '/json-cache'.date('/Y/m/d').'.'. $cachekey . '.cache.json';
202         if (file_exists($fn)) {
203             header('Content-type: application/json');
204             echo file_get_contents($fn);
205             exit;
206         }
207         return false;
208         
209     }
210 }