forked from picqer/exact-php-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
userscript.js
95 lines (81 loc) · 2.86 KB
/
userscript.js
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// ==UserScript==
// @name PHP SDK Entity generator
// @namespace php
// @description Generates php class that can be used in SDK
// @include https://start.exactonline.nl/docs/HlpRestAPIResourcesDetails.aspx?*
// @version 1
// @grant none
// ==/UserScript==
(function() {
var instant_download = false;
/** Add textarea to display php code **/
// Push current content to the left.
$('body > form').css('float', 'left');
$('body > form').css('width', '50%');
$('body > form').css('overflow', 'auto');
// Add textarea to body, css might need some work.
$('<a />', {
id : 'php_download',
text: 'Download Code',
style: 'margin: 10px; padding: 10px; color: white; background-color: red'
}).appendTo('body');
$('<br /><br />').appendTo('body');
$('<textarea/>', {
id : 'php',
style: 'min-height:800px; width: 47%; margin-left:10px;'
}).appendTo('body');
/** Initialize variables **/
var primarykey = 'ID';
var data = {};
// Fetch entity URL and strip '/api/v1/{division}/'
var url = $('#serviceUri').text().replace(/^\/api\/v[0-9]\/[^/]+\//, '');
var classname = url.replace(/.+\/(.+?)s?$/,'$1'); // Last part after slash should be the (plural) classname.
/** Fetch attribute information **/
$('#referencetable tr input').each(function() {
data[$(this).attr('name')] = {
'type' : $(this).attr('data-type').replace('Edm.', ''),
'description' : $(this).parent().siblings('td:last-child').text().trim()
};
// Set primarykey when found. Should be first itteration.
if ($(this).attr('data-key') == "True") {
primarykey = $(this).attr('name');
}
});
/** Build php code **/
phptxt = "<?php\n\nnamespace Picqer\\Financials\\Exact;\n\n/**";
// Build docblock
phptxt += "\n * Class " + classname;
phptxt += "\n *\n * @package Picqer\\Financials\\Exact";
phptxt += "\n * @see " + window.location.href;
phptxt += "\n *";
$.each(data,function(attribute, info){
phptxt += "\n * @property " + info.type + " $" + attribute + " " + info.description;
});
phptxt += "\n */";
// Build class
phptxt += "\nclass " + classname + " extends Model\n{\n\n use Query\\Findable;\n use Persistance\\Storable;";
if (primarykey != 'ID') {
phptxt += "\n\n protected $primaryKey = '" + primarykey + "';";
}
phptxt += "\n\n protected $fillable = [";
$.each(Object.keys(data), function(){
phptxt += "\n '" + this + "',";
});
phptxt += "\n ];";
phptxt += "\n\n protected $url = '" + url + "';";
phptxt += "\n\n}";
// Display php code
$('#php').text(phptxt);
// Present the php code as a download
$('#php_download').attr("href",
URL.createObjectURL(
new Blob([phptxt], {
type: "text/plain"
})
)
).attr('download', classname + '.php');
if (instant_download)
{
$('#php_download')[0].click();
}
})();