-
Notifications
You must be signed in to change notification settings - Fork 0
/
ajax_response_class.php
75 lines (65 loc) · 2.16 KB
/
ajax_response_class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
/*********************************************************************
* AjaxResponse class *
* *
* Version: 1.0 *
* Date: 17-08-2020 *
* Author: Dan Machado *
* Require php 6 or above *
**********************************************************************/
class AjaxResponse
{
private static $response=array(
'textResponse'=>array(),
'Functions'=>array()
);
/**/public static function init($debug=false){
/* */if($debug){
/* */ob_start(function($buffer){
/* */AjaxResponse::functionCall('AjaxServerResponseHander.debug', array($buffer));
/* *///AjaxResponse::flushBuffer();
/* */header('Content-Type: application/json');
/* */echo json_encode(self::$response);
/* */});
/* */}
/* */else{
/* */ob_start();
/* */}
/**/}
/**/public static function pushBuffer(){
/* */ob_end_flush();
/* */ob_clean();
/* */ob_start();
/* */header('Content-Type: application/json');
/* */echo json_encode(self::$response);
/* */ob_end_flush();
/**/}
/**/public static function flushBuffer(){
/* */header('Content-Type: application/json');
/* */echo json_encode(self::$response);
/**/}
public static function innerHTML($id, $data=''){
if(is_array($id)){
foreach($id as $k=>$v){
self::$response['textResponse'][$k]=$v;
}
}
else{
self::$response['textResponse'][$id]=$data;
}
}
public static function functionCall($func, $params){
/*
README: the value for the function to be called
(self::$response['Functions']['my_javascript_func'])
needs to be an array because sometimes we need to apply the same
function multiple times with different parameters.
*/
if(!isset(self::$response['Functions'][$func])){
self::$response['Functions'][$func]=array();
}
self::$response['Functions'][$func][]=$params;
}
}
//####################################################################
?>