1: <?php
2: /**
3: * Description of Jstore
4: *
5: * @author SARTAJ
6: */
7:
8: class Jstore extends Control{
9: private $tables = 'tables: []';
10: public function oncreate($element){
11: $this->setHTMLName("");
12: $this->registerEventJS('ready');
13: }
14:
15:
16: public function onjsrender(){
17: addFileLink($this->myrespath . '/jsstore.js');
18: //addFileLink($this->myrespath . '/jsstore.worker.js');
19: if(is_dir("cache/sphpbin/resm/jslib") && !is_file("cache/sphpbin/resm/jslib/jsstore.worker.js")){
20: copy(realpath($this->mypath) . '/jsstore.worker.js',"cache/sphpbin/resm/jslib/jsstore.worker.js");
21: $workerpath = "cache/sphpbin/resm/jslib/jsstore.worker.js";
22: }else if(is_dir("cache/sphpbin/resm/jslib")){
23: $workerpath = "cache/sphpbin/resm/jslib/jsstore.worker.js";
24: }else{
25: $workerpath = $this->myrespath . '/jsstore.worker.js';
26: }
27: $strdatajs = "";
28: $blndatanotvalid = false;
29: if(trim($this->innerHTML) != ""){
30: $tbl1 = json_decode(trim($this->innerHTML),true);
31: if(isset($tbl1["tables"])){
32: $tbl2 = $tbl1["tables"];
33: $this->tables = 'tables: ' .json_encode($tbl2);
34: if(isset($tbl1["data"])){
35: $strdatajs = "";
36: foreach($tbl1["data"] as $key=>$val){
37: $strdatajs .= "sque1.addInQueue(new Promise(function(resolve2,reject2){
38: connection.insert({
39: into: '". $key ."',
40: values: ". json_encode($val) ."
41: }).then(function (rowsAdded) {
42: if (rowsAdded > 0) {
43: resolve2();
44: }else{reject2();}
45: }).catch(function(err){logMe(err);reject2();}); }));";
46: }
47: if($strdatajs != ""){
48: $strdatajs = "var sque1 = new StQueue();". $strdatajs . "sque1.wait(function(){dbEngine.ondbready();});";
49: }else{
50: $strdatajs = "dbEngine.ondbready();";
51: }
52: }else{
53: $strdatajs = "dbEngine.ondbready();";
54: }
55: $this->unsetrender();
56: }else{
57: $blndatanotvalid = true;
58: }}else{
59: $blndatanotvalid = true;
60: }
61: if($blndatanotvalid){
62: $this->innerHTML = '{
63: "tables": [{
64: "name": "tblfdb",
65: "columns": [{
66: "name": "id",
67: "primaryKey": true
68: },
69: {
70: "name": "recname",
71: "dataType": "string"
72: },
73: {
74: "name": "recpath",
75: "dataType": "object"
76: }]},
77: {
78: "name": "tbllic",
79: "columns": [{
80: "name": "id",
81: "primaryKey": true
82: },
83: {
84: "name": "licname",
85: "dataType": "string"
86: },
87: {
88: "name": "starttime",
89: "dataType": "number"
90: },
91: {
92: "name": "endtime",
93: "dataType": "number"
94: },
95: {
96: "name": "lastps",
97: "dataType": "string"
98: },
99: {
100: "name": "extradata",
101: "dataType": "string"
102: }]
103: }],
104: "data": {
105: "tblfdb": [{"id": 1, "recname": "pckg", "recpath": null}],
106: "tbllic": [{"id": 1, "licname": "pckg", "starttime": 0, "endtime": 0, "lastps": "", "extradata": ""}]
107: }
108: }
109: ';
110: }
111: addHeaderJSCode('jstore'. $this->name, ' var connection = new JsStore.Instance(new Worker("'. $workerpath .'"));
112: const dbEngine = {
113: dbname: "'. $this->name .'",
114: isopen: false,
115: ondbready: function(){comp_'. $this->name .'_ready();},
116: isRecordExist: function(selector, callback) {
117: connection.select(selector).then(function (tbldatalist) {
118: if (tbldatalist.length > 0) {
119: callback(true);
120: } else
121: {
122: callback(false);
123: }
124: });
125: },
126: select: function(selector,
127: callback) {
128: connection.select(selector).then(callback).catch(function (error) {
129: logMe(error);
130: })},
131: insert: function(tblName,
132: value,
133: callback) {
134: connection.insert({
135: into: tblName,
136: values: [value]
137: }).then(function (rowsAdded) {
138: if (rowsAdded > 0) {
139: callback(rowsAdded);
140: }
141: }).catch(function (err) {
142: logMe(err);
143: });
144: },
145: checkDB: function(){
146: connection.isDbExist(dbEngine.dbname).then(function (isExist) {
147: if (isExist) {
148: connection.openDb(dbEngine.dbname).then(function () {
149: dbEngine.ondbready();
150: });
151: } else {
152: var DataBase = {
153: name: dbEngine.dbname,'. $this->tables .'
154: };
155: connection.createDb(DataBase).then(function (tables) {
156: setTimeout(function() { '. $strdatajs .' }, 1000);
157: });
158: }
159: }).catch(function (err) {
160: console.log(err);
161: });
162:
163: }
164: };
165: ');
166: addHeaderJSFunction('comp_'. $this->name .'_ready', 'function comp_'. $this->name .'_ready(event) {', '}');
167: addHeaderJSFunctionCode("ready", $this->name, 'dbEngine.checkDB();');
168: }
169:
170: public function onrender() {
171:
172: }
173:
174: }
175: