Vorgangsbezeichnung in Dialogbuchung: Speichern und Laden
[kivitendo-erp.git] / js / kivi.js
index c6245c8..31a56a4 100644 (file)
@@ -676,6 +676,49 @@ namespace("kivi", function(ns) {
     $input.prop('selectionStart', position);
     $input.prop('selectionEnd',   position);
   };
+
+  ns._shell_escape = function(str) {
+    if (str.match(/^[a-zA-Z0-9.,_=+/-]+$/))
+      return str;
+
+    return "'" + str.replace(/'/, "'\\''") + "'";
+  };
+
+  ns.call_as_curl = function(params) {
+    params      = params || {};
+    var uri     = document.documentURI.replace(/\?.*/, '');
+    var command = ['curl', '--user', kivi.myconfig.login + ':SECRET', '--request', params.method || 'POST']
+
+    $(params.data || []).each(function(idx, elt) {
+      command = command.concat([ '--form-string', elt.name + '=' + (elt.value || '') ]);
+    });
+
+    command.push(uri);
+
+    return $.map(command, function(elt, idx) {
+      return kivi._shell_escape(elt);
+    }).join(' ');
+  };
+
+  ns.serialize = function(source, target = [], prefix, in_array = false) {
+    let arr_prefix = first => in_array ? (first ? "[+]" : "[]") : "";
+
+    if (Array.isArray(source) ) {
+      source.forEach(( val, i ) => {
+        ns.serialize(val, target, prefix + arr_prefix(i == 0), true);
+      });
+    } else if (typeof source === "object") {
+      let first = true;
+      for (let key in source) {
+        ns.serialize(source[key], target, (prefix !== undefined ? prefix + arr_prefix(first) + "." : "") + key);
+        first = false;
+      }
+    } else {
+      target.push({ name: prefix + arr_prefix(false), value: source });
+    }
+
+    return target;
+  };
 });
 
 kivi = namespace('kivi');