@@ -6,7 +6,10 @@ class ServerStatsAccess extends StatsAccess {
66 private string $ groupId ;
77 private string $ token ;
88 private string $ thisClientName ;
9+
910 private ServerAccessCache $ cache ;
11+ private JSONReader $ offlineTasks ;
12+ private bool $ isOnline ;
1013
1114 private bool $ requestError = false ;
1215
@@ -18,10 +21,36 @@ public function __construct(){
1821 $ this ->token = $ c ->getValue (['sync ' , 'server ' , 'token ' ]);
1922 $ this ->thisClientName = $ c ->getValue (['sync ' , 'server ' , 'thisname ' ]);
2023
24+ $ this ->offlineTasks = Config::getStorageReader ('offlineTasks ' );
25+ $ this ->isOnline = Utilities::isOnline ($ this ->uri );
26+ $ this ->checkForOfflineTasks ();
27+
2128 $ this ->cache = new ServerAccessCache ( $ this ->uri , $ this ->groupId , $ this ->token , $ this ->thisClientName );
2229 }
2330
31+ private function checkForOfflineTasks () : void {
32+ // online and offline tasks to upload?
33+ if ($ this ->isOnline && !empty ($ this ->offlineTasks ->getArray ()) ){
34+ $ ok = true ;
35+ foreach ($ this ->offlineTasks ->getArray () as $ task ){
36+ $ this ->postToServer ('add ' , $ task );
37+ $ ok &= !$ this ->requestError ;
38+ }
39+ if (!$ ok ){
40+ CLIOutput::error (CLIOutput::ERROR_WARN , 'Unable to upload tasks done offline ' );
41+ }
42+ else {
43+ $ this ->offlineTasks ->setArray (array ());
44+ }
45+ }
46+ }
47+
2448 private function postToServer (string $ endpoint , array $ data = array () ) : array {
49+ if (!$ this ->isOnline ){
50+ CLIOutput::error (CLIOutput::ERROR_INFO , 'Client is offline, so no data from sync server will be shown. ' );
51+ return array ();
52+ }
53+
2554 $ context = array (
2655 'http ' => array (
2756 'method ' => 'POST ' ,
@@ -48,11 +77,11 @@ private function postToServer(string $endpoint, array $data = array() ) : array
4877 }
4978 else {
5079 $ msg = is_null ($ json ) ? $ ret : $ json ['error ' ];
51- echo " ERROR: Returned message from server: ' " . $ msg ."' " . PHP_EOL ;
80+ CLIOutput:: error (CLIOutput:: ERROR_WARN , " Returned message from server: ' " . $ msg ."' " ) ;
5281 }
5382 }
5483 }
55- echo " ERROR: Request failed! " . PHP_EOL ;
84+ CLIOutput:: error (CLIOutput:: ERROR_WARN , " Request failed! ") ;
5685 $ this ->requestError = true ;
5786 return array ();
5887 }
@@ -103,7 +132,13 @@ public function initialSync() : bool {
103132 }
104133
105134 public function setDayTasks (array $ tasks , int $ day ) : void {
106- $ this ->postToServer ('add ' , array ( 'day ' => $ day , 'tasks ' => $ tasks ) );
135+ $ data = array ( 'day ' => $ day , 'tasks ' => $ tasks );
136+ if ($ this ->isOnline ){
137+ $ this ->postToServer ('add ' , $ data );
138+ }
139+ else {
140+ $ this ->offlineTasks ->setValue ([null ], $ data );
141+ }
107142 }
108143
109144}
0 commit comments