Fix #8059 - being splitting of Roo.php into Traits
[Pman.Core] / JsonOutputTrait.php
similarity index 65%
rename from RooJsonOutputTrait.php
rename to JsonOutputTrait.php
index c307d27..0c1e7a4 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 
-trait Pman_Core_RooJsonOutputTrait {
+trait Pman_Core_JsonOutputTrait {
     
     function jok($str)
     {
@@ -9,13 +9,10 @@ trait Pman_Core_RooJsonOutputTrait {
         }
         
         $cli = HTML_FlexyFramework::get()->cli;
-        
         if ($cli) {
             echo "OK: " .$str . "\n";
             exit;
         }
-        require_once 'Services/JSON.php';
-        $json = new Services_JSON();
         
         $retHTML = isset($_SERVER['CONTENT_TYPE']) && 
                 preg_match('#multipart/form-data#i', $_SERVER['CONTENT_TYPE']);
@@ -33,37 +30,94 @@ trait Pman_Core_RooJsonOutputTrait {
             echo "<HTML><HEAD></HEAD><BODY>";
             // encode html characters so they can be read..
             echo  str_replace(array('<','>'), array('\u003c','\u003e'),
-                        $json->encodeUnsafe(array('success'=> true, 'data' => $str)));
+                        $this->jsencode(array('success'=> true, 'data' => $str), false));
             echo "</BODY></HTML>";
             exit;
         }
         
         
-        echo  $json->encode(array('success'=> true, 'data' => $str));
+        echo  $this->jsencode(array('success'=> true, 'data' => $str),true);
         
         exit;
+        
     }
     
     
-    function jerr($str, $errors=array(), $content_type = false)
+      /**
+     * ---------------- Standard JSON outputers. - used everywhere
+     * JSON error - simple error with logging.
+     * @see Pman::jerror
+     */
+    
+    function jerr($str, $errors=array(), $content_type = false) // standard error reporting..
     {
-        if ($this->transObj) {
-            $this->transObj->query('ROLLBACK');
-        }
-        
         return $this->jerror('ERROR', $str,$errors,$content_type);
     }
     
+    function jnotice($type, $str, $errors=array(), $content_type = false)
+    {
+        return $this->jerror('NOTICE-' . $type, $str, $errors, $content_type);
+    }
+    
+     /**
+     * jerrAuth: standard auth failure - with data that let's the UI know..
+     */
+    function jerrAuth()
+    {
+        $au = $this->authUser;
+        if ($au) {
+            // is it an authfailure?
+            $this->jerror("LOGIN-NOPERM", "Permission denied to view this resource", array('authFailure' => true));
+        }
+        $this->jerror("LOGIN-NOAUTH", "Not authenticated", array('authFailure' => true));
+    }
+     
+     
+    /**
+     * Recomended JSON error indicator
+     *
+     * 
+     * @param string $type  - normally 'ERROR' - you can use this to track error types.
+     * @param string $message - error message displayed to user.
+     * @param array $errors - optioanl data to pass to front end.
+     * @param string $content_type - use text/plain to return plan text - ?? not sure why...
+     *
+     */
+    
     function jerror($type, $str, $errors=array(), $content_type = false) // standard error reporting..
     {
-        if ($type !== false) {
+        if ($this->transObj) {
+            $this->transObj->query('ROLLBACK');
+        }
+        
+        $cli = HTML_FlexyFramework::get()->cli;
+        if ($cli) {
+            echo "ERROR: " .$str . "\n"; // print the error first, as DB might fail..
+        }
+        $pman = HTML_FlexyFramework::get();
+        
+       
+        
+
+        
+        if ($type !== false  &&  empty($pman->nodatabase)) {
+            
+            if(!empty($errors)){
+                DB_DataObject::factory('Events')->writeEventLogExtra($errors);
+            }
+            // various codes that are acceptable.
+            // 
+            if (!preg_match('/^(ERROR|NOTICE|LOG)/', $type )) {
+                $type = 'ERROR-' . $type;
+            }
+            
             $this->addEvent($type, false, $str);
+            
         }
          
         $cli = HTML_FlexyFramework::get()->cli;
         if ($cli) {
-            echo "ERROR: " .$str . "\n";
-            exit;
+            exit(1); // cli --- exit code to stop shell execution if necessary.
         }
         
         
@@ -74,8 +128,7 @@ trait Pman_Core_RooJsonOutputTrait {
             exit;
         } 
         
-        require_once 'Services/JSON.php';
-        $json = new Services_JSON();
+     // log all errors!!!
         
         $retHTML = isset($_SERVER['CONTENT_TYPE']) && 
                 preg_match('#multipart/form-data#i', $_SERVER['CONTENT_TYPE']);
@@ -88,17 +141,18 @@ trait Pman_Core_RooJsonOutputTrait {
             $retHTML = isset($_REQUEST['returnHTML']) && $_REQUEST['returnHTML'] !='NO';
         }
         
+        
         if ($retHTML) {
             header('Content-type: text/html');
             echo "<HTML><HEAD></HEAD><BODY>";
-            echo  $json->encodeUnsafe(array(
+            echo  $this->jsencode(array(
                     'success'=> false, 
                     'errorMsg' => $str,
                     'message' => $str, // compate with exeption / loadexception.
 
                     'errors' => $errors ? $errors : true, // used by forms to flag errors.
                     'authFailure' => !empty($errors['authFailure']),
-                ));
+                ), false);
             echo "</BODY></HTML>";
             exit;
         }
@@ -116,19 +170,33 @@ trait Pman_Core_RooJsonOutputTrait {
                 
         }
         
-        echo $json->encode(array(
+        echo $this->jsencode(array(
             'success'=> false, 
-            'data'=> array(), 
+            'data'=> array(),
+            'code' => $type,
             'errorMsg' => $str,
             'message' => $str, // compate with exeption / loadexception.
             'errors' => $errors ? $errors : true, // used by forms to flag errors.
             'authFailure' => !empty($errors['authFailure']),
-        ));
+        ),true);
+        
         
         exit;
         
     }
     
+     
+   
+    
+    
+    
+    /**
+     * output data for grids or tree
+     * @ar {Array} ar Array of data
+     * @total {Number|false} total number of records (or false to return count(ar)
+     * @extra {Array} extra key value list of data to pass as extra data.
+     * 
+     */
     function jdata($ar,$total=false, $extra=array(), $cachekey = false)
     {
         // should do mobile checking???
@@ -136,8 +204,7 @@ trait Pman_Core_RooJsonOutputTrait {
             $total = count($ar);
         }
         $extra=  $extra ? $extra : array();
-        require_once 'Services/JSON.php';
-        $json = new Services_JSON();
+        
         
         $retHTML = isset($_SERVER['CONTENT_TYPE']) && 
                 preg_match('#multipart/form-data#i', $_SERVER['CONTENT_TYPE']);
@@ -153,11 +220,11 @@ trait Pman_Core_RooJsonOutputTrait {
         if ($retHTML) {
             
             header('Content-type: text/html');
-            echo "<HTML><HEAD></HEAD><BODY>";
+            echo "<HTML><HEAD></HEAD><BODY><![CDATA[";
             // encode html characters so they can be read..
             echo  str_replace(array('<','>'), array('\u003c','\u003e'),
-                        $json->encodeUnsafe(array('success' =>  true, 'total'=> $total, 'data' => $ar) + $extra));
-            echo "</BODY></HTML>";
+                        $this->jsencode(array('success' =>  true, 'total'=> $total, 'data' => $ar) + $extra, false));
+            echo "]]></BODY></HTML>";
             exit;
         }
         
@@ -181,7 +248,7 @@ trait Pman_Core_RooJsonOutputTrait {
         }
         
       
-        $ret =  $json->encode(array('success' =>  true, 'total'=> $total, 'data' => $ar) + $extra);  
+        $ret =  $this->jsencode(array('success' =>  true, 'total'=> $total, 'data' => $ar) + $extra,true);  
         
         if (!empty($cachekey)) {
             
@@ -191,10 +258,13 @@ trait Pman_Core_RooJsonOutputTrait {
             }
             file_put_contents($fn, $ret);
         }
+        
         echo $ret;
         exit;
     }
     
+    
+   
     /** a daily cache **/
     function jdataCache($cachekey)
     {
@@ -207,4 +277,5 @@ trait Pman_Core_RooJsonOutputTrait {
         return false;
         
     }
+    
 }
\ No newline at end of file