forked from vscarpenter/webSqlApp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webSqlSyncAdapterTest.php
69 lines (60 loc) · 4.33 KB
/
webSqlSyncAdapterTest.php
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
<?php
// Name : webSqlSyncAdapterTest.php - called by the webSqlApp (index.html) For test to debug server side
// Goal : To communicate data from a MySQL query using myJob to a webSqlSync json format. Result: It works server to client until now. All server table data to Client.
// Version: 2013-10-16
// It uses: webSqlSyncHandler.php and loginCheck.php that uses connections/connDbUP.php;
// To do : Debug the 2 way sync mecanism.
// To do : Take the userId and the password from userParam table from webSQL
// To test: use http://www.affairesup.com/webSqlApp/webSqlSyncAdapterTest.php //?username=exp&password=4success
//require_once('loginCheck.php');
//echo "<p> CLnum_rows in sqlSyncAdapter is ", $CLnum_rows, "</p>"; //R: GOOD
// if $CLnum_rows = 1, we have the OK to get the server data
$CLnum_rows = 1; //To bypass loginCheck (for debug use)
if ($CLnum_rows){
include("webSqlSyncHandlerTest.php");
$handler = new SqlSyncHandler(); // to initialize the json handler from 'php://input'. It put it in $clientData
$handler -> call('myJob',$handler); // call a custom function which will make a job with parsed data
}
else{
echo "<p> Your're not authorized to get the data from this server.";
}
function myJob($handler){
$currentDateTime = date("Y-m-d H:i:s");
$clientLastSyncDate = $handler -> clientData['info']['lastSyncDate']/1000; // It gives a 10 digits unix date time
//$clientLastSyncDate = $clientData['info']['lastSyncDate']/1000; // It gives a 10 digits unix date time
echo "webSqlSyncAdapterTest - clientLastSyncDate: ", $clientLastSyncDate, "<br>";
//print_r($handler -> clientData);
//$clientData = $handler -> clientData; // It gives a 10 digits unix date time
//X// print_r($clientData);
//X// $clientLastSyncDate = $clientData['info']['lastSyncDate']/1000; // It gives a 10 digits unix date time
//$clientLastSyncDate = $handler -> clientData['info']['lastSyncDate']/1000; // It gives a 10 digits unix date time
//X// echo "webSqlSyncAdapterTest - clientLastSyncDate: ", $clientLastSyncDate, "<br>";
require_once('setContactTest.php'); // With the JSON, it does many INSERTs or UPDATEs to MySQL following some conditions
// My job is to get all the table data from the server and send a json to client
$handler -> reply(true,"this is a positive reply", getServerData($clientLastSyncDate)); // with a dynamic array coming from a MySQL query //function reply($status,$message,$data)
// It return $serverAnswer from SqlSyncHandler.php: {"result":"OK","message":"this is a positive reply","syncDate":1327075596000,"data":{"Unites":[{"UniteID":"0","UniteSymbol":"h"},{"UniteID":"1","UniteSymbol":"km"},{"UniteID":"2","UniteSymbol":"$"},{"UniteID":"3","UniteSymbol":"U$"},{"UniteID":"4","UniteSymbol":"\u20ac"},{"UniteID":"5","UniteSymbol":"$P"}]}}
// a error reply example
//$handler -> reply(false,"this is a error reply",array('browser' => 'firefox'));
}
function getServerData($clientLastSyncDate){ //get the modified data from the server using an associative array
echo "clientLastSyncDate: ", $clientLastSyncDate, "<br>";
// Define here the tables to sync Server side param1 is the webSql table name and param2 is the MySQL table name
$tablesToSync = array(
array( "tableNameWebSql" => 'Contacts', "tableName_MySql" => 'Contacts', "getQueryFile" => 'getModifiedContact.php'),
// array( "tableNameWebSql" => 'Activites', "tableName_MySql" => 'RN_Activite', "getQueryFile" => 'getModifiedActivity.php'),
// array( "tableNameWebSql" => 'Projets', "tableName_MySql" => 'RN_Projet', "getQueryFile" => 'getModifiedProject.php'),
// array( "tableNameWebSql" => 'Ressources', "tableName_MySql" => 'RN_Ressource', "getQueryFile" => 'getModifiedRessource.php'),
// array( "tableNameWebSql" => 'Categories', "tableName_MySql" => 'RN_Categorie', "getQueryFile" => 'getModifiedCategory.php'),
array( "tableNameWebSql" => 'Unites', "tableName_MySql" => 'RN_Unite', "getQueryFile" => 'getModifiedUnit.php')
);
$getServerData = array();
//require_once('loginCheck.php'); // à mettre plus tard ??? et faire la requête en fonction de ClientID obtenu de loginCheck
require_once('connections/connDbUP.php');
foreach($tablesToSync as $value){
require_once($value['getQueryFile']);
$getServerData[$value['tableNameWebSql']] = $sql_result;
}
//unset($value); // Usefull ??? Supposé détruire la référence sur le dernier élément
return $getServerData;
}
?>