From 723ca1b4e6b2d70201521d732a80192f8161749b Mon Sep 17 00:00:00 2001 From: Walter A Jablonowski <31986246+walter-a-jablonowski@users.noreply.github.com> Date: Sun, 24 Mar 2024 21:46:35 +0100 Subject: [PATCH] Added ajax bridge, fixed all --- src/ajax/save_day_entries.php | 7 ++----- src/ajax/save_foods.php | 8 +++---- src/ajax_bridge.php | 22 ++++++++++++++++++++ src/controller.js | 8 +++---- src/controller.php | 4 ++-- src/index.php | 7 ++++--- src/lib/Controller_240323/ControllerBase.php | 8 +++---- src/lib/Controller_240323/README.md | 5 +++-- src/lib/send_230808.js | 4 ++-- 9 files changed, 47 insertions(+), 26 deletions(-) create mode 100644 src/ajax_bridge.php diff --git a/src/ajax/save_day_entries.php b/src/ajax/save_day_entries.php index 420bd3c8..bb8919dd 100644 --- a/src/ajax/save_day_entries.php +++ b/src/ajax/save_day_entries.php @@ -6,12 +6,9 @@ public function saveDayEntries( $request ) { // we use no backup here, just start from scratch if error - // $data = file_get_contents('php://input'); - $data = json_decode( file_get_contents('php://input'), true); + // $data = json_decode( file_get_contents('php://input'), true); - // file_put_contents( $fil, $data); - - if( ! file_put_contents('data/days/' . date('Y-m-d') . '.tsv', $data['data'])) + if( ! file_put_contents('data/days/' . date('Y-m-d') . '.tsv', $request['data'])) echo json_encode(['result' => 'error', 'message' => 'Error saving']); echo json_encode(['result' => 'success']); diff --git a/src/ajax/save_foods.php b/src/ajax/save_foods.php index b628160f..c0e1d1ff 100644 --- a/src/ajax/save_foods.php +++ b/src/ajax/save_foods.php @@ -1,17 +1,17 @@ 'error', 'message' => 'Error making backup']); // TASK: add in js - if( ! file_put_contents('data/foods.yml', $data['data'])) + if( ! file_put_contents('data/foods.yml', $request['data'])) echo json_encode(['result' => 'error', 'message' => 'Error saving']); echo json_encode(['result' => 'success']); diff --git a/src/ajax_bridge.php b/src/ajax_bridge.php new file mode 100644 index 00000000..fd3d666e --- /dev/null +++ b/src/ajax_bridge.php @@ -0,0 +1,22 @@ +config = new SimpleData( Yaml::parse( file_get_contents('config.yml'))); +config::setData( new SimpleData( Yaml::parse( file_get_contents('config.yml')))); + +$args = json_decode( file_get_contents('php://input'), true); + +$controller = new FoodsController(); +echo $controller->dispatch($args); + +?> diff --git a/src/controller.js b/src/controller.js index a09c0d52..a6fa7b62 100644 --- a/src/controller.js +++ b/src/controller.js @@ -69,8 +69,8 @@ class FoodsEventController saveFoodsBtnClick(event) { - send('ajax/save_foods.php', query('#foods').value, function( result, data) { - // send('index.php', { identifier: 'saveFoods', data: query('#foods').value ]}, function( result, data ) { + // send('ajax/save_foods.php', query('#foods').value, function( result, data) { + send('ajax_bridge.php', { identifier: 'saveFoods', data: query('#foods').value }, function( result, data ) { if( result === 'success') query('#foodsUIMsg').innerHTML = 'Saved' @@ -120,8 +120,8 @@ class FoodsEventController #saveDayEntries() { - send('ajax/save_day_entries.php', query('#dayEntries').value, function( result, data) { - // send('index.php', { identifier: 'saveDayEntries', data: query('#dayEntries').value ]}, function( result, data ) { + // send('ajax/save_day_entries.php', query('#dayEntries').value, function( result, data) { + send('ajax_bridge.php', { identifier: 'saveDayEntries', data: query('#dayEntries').value }, function( result, data ) { if( result === 'success') query('#foodsUIMsg').innerHTML = 'Saved' diff --git a/src/controller.php b/src/controller.php index 2ea0cad7..0d2de28b 100644 --- a/src/controller.php +++ b/src/controller.php @@ -18,7 +18,7 @@ class FoodsController extends ControllerBase public SimpleData $data; - public function __construct() + public function __construct(/*$model = null, $view = null*/) { parent::__construct(); @@ -116,7 +116,7 @@ public function __construct() } - public function view() { + public function render(/*$request*/) { // we currently use no Engine cause the app is tooooooooooo small diff --git a/src/index.php b/src/index.php index db3c9829..3cfc0500 100644 --- a/src/index.php +++ b/src/index.php @@ -1,7 +1,5 @@ run(); + +// Page wrapper for standalone use + ?> @@ -82,7 +83,7 @@ config::setData( new SimpleData( Yaml::parse( file_get_contents('config.yml')))); $controller = new FoodsController(); - echo $controller->view(); + echo $controller->render(); ?> diff --git a/src/lib/Controller_240323/ControllerBase.php b/src/lib/Controller_240323/ControllerBase.php index dae6f46b..9a42abce 100644 --- a/src/lib/Controller_240323/ControllerBase.php +++ b/src/lib/Controller_240323/ControllerBase.php @@ -31,12 +31,12 @@ public function dispatch( $request ) if( ! count($identifier) ) // ins of methods like index() as done in laravel { - return $this->render(); + return $this->render( $request ); } - elseif( count($identifier) == 1 && method_exists($this, $identifier[1])) + elseif( count($identifier) == 1 && method_exists($this, $identifier[0])) { - $method = $identifier[1]; - return $this->$method(); + $method = $identifier[0]; + return $this->$method( $request ); } elseif( count($identifier) > 1 ) { diff --git a/src/lib/Controller_240323/README.md b/src/lib/Controller_240323/README.md index 6b041fe7..906a073c 100644 --- a/src/lib/Controller_240323/README.md +++ b/src/lib/Controller_240323/README.md @@ -38,11 +38,11 @@ class SampleController extends ControllerBase { use SomeAjaxController; // trait (partial class if long) - // public function __constuct() public function __constuct( $model = null, $view = null ) + // public function __constuct() // or { - // parent::__construct(); // or parent::__construct( $model, $view ); + // parent::__construct(); // Register sub and ajax controllers @@ -58,6 +58,7 @@ class SampleController extends ControllerBase // dispatch() may be used by Routing just like any method here (which handles sub controllers) public function getData( $request ) + // public function getData() // or { // ajax ... } diff --git a/src/lib/send_230808.js b/src/lib/send_230808.js index 999a4fc0..f5ee4514 100644 --- a/src/lib/send_230808.js +++ b/src/lib/send_230808.js @@ -5,10 +5,10 @@ function send( url, data, callback ) headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify({ data: data }) + body: JSON.stringify( data ) }) .then( response => response.text()) -.then( result => { + .then( result => { result = JSON.parse(result) if( typeof callback === 'function' ) callback( result.result, result.data || null )