Skip to content

Commit

Permalink
Added ajax bridge, fixed all
Browse files Browse the repository at this point in the history
  • Loading branch information
walter-a-jablonowski committed Mar 24, 2024
1 parent ca0032d commit 723ca1b
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 26 deletions.
7 changes: 2 additions & 5 deletions src/ajax/save_day_entries.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand Down
8 changes: 4 additions & 4 deletions src/ajax/save_foods.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<?php

// require_once 'lib/backup_230924.php';

trait SaveFoodsAjaxController
{
public function saveFoods( $request ) {

// require_once 'lib/backup_230924.php';

$data = json_decode( file_get_contents('php://input'), true);
// $data = json_decode( file_get_contents('php://input'), true);

// if( ! backup_fil( $groupsFil )) // prefer cause we have on backup form for all dools
// echo json_encode(['result' => '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']);
Expand Down
22 changes: 22 additions & 0 deletions src/ajax_bridge.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

use Symfony\Component\Yaml\Yaml;
use Symfony\Component\Yaml\Exception\ParseException;

require_once 'vendor/autoload.php';
require_once 'lib/SimpleData_240317/SimpleData.php';
require_once 'lib/ConfigStatic_240323/config.php';
require_once 'controller.php';


// Currently used as a bridge for dashbard integration

// $this->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);

?>
8 changes: 4 additions & 4 deletions src/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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'
Expand Down
4 changes: 2 additions & 2 deletions src/controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class FoodsController extends ControllerBase
public SimpleData $data;


public function __construct()
public function __construct(/*$model = null, $view = null*/)
{
parent::__construct();

Expand Down Expand Up @@ -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

Expand Down
7 changes: 4 additions & 3 deletions src/index.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php

// page wrapper for standalone use

use Symfony\Component\Yaml\Yaml;
use Symfony\Component\Yaml\Exception\ParseException;

Expand Down Expand Up @@ -33,6 +31,9 @@
//
// $router->run();


// Page wrapper for standalone use

?><!DOCTYPE html>
<html>
<head>
Expand Down Expand Up @@ -82,7 +83,7 @@
config::setData( new SimpleData( Yaml::parse( file_get_contents('config.yml'))));

$controller = new FoodsController();
echo $controller->view();
echo $controller->render();

?>

Expand Down
8 changes: 4 additions & 4 deletions src/lib/Controller_240323/ControllerBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
{
Expand Down
5 changes: 3 additions & 2 deletions src/lib/Controller_240323/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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 ...
}
Expand Down
4 changes: 2 additions & 2 deletions src/lib/send_230808.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand Down

0 comments on commit 723ca1b

Please sign in to comment.