Introduce new AJAX framework with improved class use
authorJoey Schulze <joey@infodrom.org>
Sun, 9 Jul 2017 11:34:29 +0000 (13:34 +0200)
committerJoey Schulze <joey@infodrom.org>
Mon, 10 Jul 2017 20:48:29 +0000 (22:48 +0200)
src/ajax.php
src/future.php
src/infodrom.js

index 6c38fa6..c613aab 100644 (file)
@@ -2,38 +2,40 @@
 require_once('config.php');
 require_once('future.php');
 
 require_once('config.php');
 require_once('future.php');
 
-$data = array();
-
-if (strlen($_POST['func'])) {
-  $backend = new AJAXBackend();
-
-  if (method_exists($backend, $_POST['func'])) {
-    $func = $_POST['func'];
-    json_return($backend->$func());
+function route_request()
+{
+  if (empty($_SERVER['REMOTE_USER']))
+    return ajax_error('Die Sitzung ist abgelaufen, Sie werden zur Startseite weitergeleiet.', true);
+
+  list($class, $function) = explode('/', $_POST['route'], 2);
+  $method = 'ajax'.$function;
+
+  if (!class_exists($class))
+    return ajax_error('Klasse '.htmlspecialchars($class).' existiert nicht.');
+
+  try {
+    if (array_key_exists('id', $_POST))
+      $object = Factory::get($class, $_POST['id']);
+    else
+      $object = Factory::get($class);
+  } catch (Exception $e) {
+    return ajax_error('Klasse '.htmlspecialchars($class).'kann nicht instanziiert werden:<br>'.htmlspecialchars($e->getMessage()));
   }
 
   }
 
-  $path = substr($_SERVER['HTTP_REFERER'], strpos($_SERVER['HTTP_REFERER'], $_SERVER['SERVER_NAME'])+strlen($_SERVER['SERVER_NAME'])+1);
-  $slash = strpos($path, '/', strpos($path, '/')+1);
-
-  if ($slash !== false) {
-    $path = getcwd() . '/' . substr($path, 0, $slash) . '/' . 'moduleajaxbackend.class.php';
-
-    if (file_exists($path)) {
-      require_once($path);
-      $backend = new ModuleAJAXBackend();
+  if (!method_exists($object, $method))
+    return ajax_error('Klasse '.htmlspecialchars($class).':<br>AJAX-Backend '.htmlspecialchars($function).' existiert nicht.');
 
 
-      $method = 'ajax_' . $_POST['func'];
-      if (method_exists($backend, $method)) {
-       json_return($backend->$method());
-      }
-    }
-  }
-
-  error_log('Unknown function '.$_POST['func']);
-  json_return(array('status' => false,
-                   'error' => 'Unknown function '.urlencode($_POST['func']).'.'));
+  $return = $object->$method($_POST);
+  if (is_bool($return)) $return = array('status' => $return);
+  return $return;
 }
 
 }
 
-error_log('Unknown usage');
-json_return(array('status' => false, 'error' => 'Unknown usage'));
+if (strlen($_POST['route'])) {
+  $data = route_request();
+  if (!array_key_exists('status', $data))
+    $data['status'] = true;
+} else {
+  $data = array('status' => false, 'error' => 'Keine Route angegeben');
+}
 
 
+json_return($data);
index 779f700..f3f3c5e 100644 (file)
@@ -26,7 +26,7 @@ function json_return($data)
   exit();
 }
 
   exit();
 }
 
-function json_error($text)
+function ajax_error($text)
 {
   $data = array('status' => false, 'error' => $text);
   json_return($data);
 {
   $data = array('status' => false, 'error' => $text);
   json_return($data);
index 869c8c7..fa61440 100644 (file)
@@ -1,31 +1,3 @@
-function ajax_request(funcname, params, callback, error_callback)
-{
-    if (params === null)
-       params = 'func='+funcname;
-    else if (typeof params == 'object')
-       params['func'] = funcname
-    else if (typeof params == 'string')
-       params = 'func='+funcname+'&'+params
-    else
-       return false;
-
-    $.post('/ajax.php', params,
-          function(data){
-              if (!data.status) {
-                  if (typeof data.error == 'string')
-                      var error = data.error;
-                  else
-                      var error = 'An error occurred'
-                  alert(error);
-                  if (typeof error_callback == 'function')
-                      error_callback(data);
-                  return;
-              }
-              if (typeof callback == 'function')
-                  callback(data);
-          });
-}
-
 function show_message(text, timeout)
 {
     if (typeof timeout == 'undefined') timeout = 3;
 function show_message(text, timeout)
 {
     if (typeof timeout == 'undefined') timeout = 3;
@@ -75,3 +47,59 @@ function hide_error(text)
     $('#error_div').hide();
     return false;
 }
     $('#error_div').hide();
     return false;
 }
+
+(function($){
+    $.fn.ltag = function() {
+       return this.prop("tagName").toLowerCase();
+    };
+
+    $.fn.center = function () {
+       this.css("position","absolute");
+       this.css("top", Math.max(0, (($(window).height() - $(this).outerHeight()) / 2) +
+                                $(window).scrollTop()) + "px");
+       this.css("left", Math.max(0, (($(window).width() - $(this).outerWidth()) / 2) +
+                                 $(window).scrollLeft()) + "px");
+       return this;
+    };
+
+$.invoke = function(name, parms, callback) {
+    if (typeof(parms) == 'string' && parms.length)
+       parms = 'route='+name+'&'+parms;
+    else if (typeof(parms) == 'object')
+       parms['route'] = name;
+    else
+       parms = 'route='+name;
+
+    $.post('/ajax.php',
+               parms,
+               function(data){
+                   if (!data.status) {
+                       if (data.error) {
+                           if (data.redirect_login) {
+                               show_message(data.error, 5);
+                               setTimeout(function(){window.location.href = '/';}, 5000);
+                               return;
+                           } else
+                               return show_error(data.error);
+                       } else
+                           return show_error('Fehler im Backend zu "'+name+'" aufgetreten');
+                   }
+                   if (typeof(data.html) == 'object')
+                       for (id in data.html) {
+                           var elem = $('#'+id);
+                           if (elem.length) {
+                               elem.html(data.html[id]);
+                           }
+                       }
+                   if (typeof(data.values) == 'object')
+                       for (id in data.values) {
+                           var elem = $('#'+id);
+                           if (elem.length)
+                               elem.val(data.values[id]);
+                       }
+
+                   if (typeof(callback) == 'function')
+                       return callback(data);
+               });
+};
+})(jQuery);