Skip to content
This repository was archived by the owner on Mar 30, 2024. It is now read-only.

Commit 54eda6d

Browse files
committed
Add category sharing
1 parent df2e451 commit 54eda6d

File tree

8 files changed

+205
-31
lines changed

8 files changed

+205
-31
lines changed

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
latest
2-
0.3.7
2+
0.3.8
33
0.3
44
0

php/core/Share.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ class Share {
1515

1616
private JSONReader $shares;
1717
private Login $login;
18+
private String $error = "";
1819

1920
public function __construct( Login $login ) {
20-
$this->shares = JSONReader('shares');
21+
$this->shares = new JSONReader('shares');
2122
$this->login = $login;
2223

2324
$this->checkInitGroup($this->login->getGroup());
@@ -53,15 +54,24 @@ public function addShare( string $category, string $group ) : void {
5354
'category' => $category,
5455
'group' => $group
5556
);
56-
if( $this->shares->searchValue([$this->login->getGroup(), 'byme'], $byme) !== false ){
57+
if( $this->shares->searchValue([$this->login->getGroup(), 'byme'], $byme) === false ){
5758
$this->shares->setValue([$this->login->getGroup(), 'byme', null], $byme);
5859
$this->shares->setValue([$group, 'withme', null], array(
5960
'category' => $category,
6061
'group' => $this->login->getGroup()
6162
));
6263
}
64+
else {
65+
$this->error = "Share already exists.";
66+
}
67+
}
68+
else {
69+
$this->error = "Unknown category to share";
6370
}
6471
}
72+
else {
73+
$this->error = "Invalid group to share with";
74+
}
6575
}
6676

6777
/**
@@ -91,6 +101,9 @@ public function removeShare( string $category, string $group ) : void {
91101
$this->shares->setValue([$group, 'withme', $pos], null);
92102
}
93103
}
104+
else {
105+
$this->error = "Invalid group to delete share for";
106+
}
94107
}
95108

96109
private function getAllCategories(){
@@ -106,6 +119,14 @@ public function getCategoriesAndGroups() : array {
106119
);
107120
}
108121

122+
public function getErrorMessage() : string {
123+
return $this->error;
124+
}
125+
126+
public function hasError() : bool {
127+
return !empty($this->error);
128+
}
129+
109130
/**
110131
* Get the list of shares shared with this user
111132
*/

php/core/Stats.php

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,26 @@ public function __construct( Template $temp, Login $login ) {
2727

2828
if( $_SERVER['REQUEST_METHOD'] === 'POST' ){
2929
$cmd = $this->paramsToCmd();
30-
if( !empty($cmd)){
30+
if( !empty($cmd) ){
3131
$data = new TTTStats($cmd, API::getStorageDir($this->login->getGroup()));
32-
$this->displayContent($data->getAllResults());
32+
$allData = $data->getAllResults();
33+
if( isset($_POST['shares']) && is_array($_POST['shares']) ){
34+
$shares = array();
35+
foreach($_POST['shares'] as $sh ){
36+
if(is_string($sh)){
37+
$sh = explode('::', $sh);
38+
$gr = preg_replace('/[^A-Za-z0-9]/', '', $sh[0]);
39+
if(InputParser::checkCategoryInput($sh[1]) && !empty($gr) ){
40+
if(!isset($shares[$gr])){
41+
$shares[$gr] = array();
42+
}
43+
$shares[$gr][] = $sh[1];
44+
}
45+
}
46+
}
47+
$this->addShares($allData, $cmd, $shares);
48+
}
49+
$this->displayContent($allData);
3350
}
3451
}
3552
}
@@ -47,6 +64,32 @@ private function displayContent(array $data) : void {
4764
}
4865
}
4966

67+
private function addShares( array &$allData, array $cmd, array $shares) : void {
68+
if(in_array('-devices', $cmd)){
69+
$did = array_search('-devices', $cmd);
70+
unset($cmd[$did], $cmd[$did+1]);
71+
}
72+
if(!in_array('-cats', $cmd)){
73+
$cmd[] = '-cats';
74+
$cmd[] = '';
75+
}
76+
$cid = array_search('-cats', $cmd) + 1;
77+
78+
foreach($shares as $group => $cats){
79+
$cmd[$cid] = implode(',', $cats);
80+
$sd = new TTTStats($cmd, API::getStorageDir($group));
81+
$data = $sd->getAllResults();
82+
array_walk_recursive( $data, function (&$value, $key) use (&$group) {
83+
if(in_array($key, ['name', 'category', 'Name', 'Category', 'Other devices', 'device'])){
84+
$value = $group . '::' . $value;
85+
}
86+
});
87+
foreach(['table','plain','combi','today'] as $key ){
88+
$allData[$key] = array_merge($allData[$key], $data[$key]);
89+
}
90+
}
91+
}
92+
5093
private function arrayToTable(array $data) : string {
5194
$table = "<table class=\"table table-striped table-responsive-sm statstable\">";
5295
$head = false;
@@ -162,6 +205,20 @@ private function setUpHtml(){
162205
$this->temp->setMultipleContent('Devices', $ds);
163206
}
164207

208+
$share = new Share($this->login);
209+
$withme = $share->getSharedWithMe();
210+
if(!empty($withme)){
211+
$this->temp->setContent('SHARESDIABLE', '');
212+
$d = array();
213+
foreach($withme as $w){
214+
$d[] = array(
215+
'%%VALUE%%' => $w['group'] . '::' . $w['category'],
216+
'%%NAME%%' => '"' . $w['category'] . '" from "' . $w['group'] . '"'
217+
);
218+
}
219+
$this->temp->setMultipleContent('Shares', $d);
220+
}
221+
165222
$this->temp->setContent('GRAPHES', json_encode(
166223
array_values(array_map(
167224
function ($s) {

php/core/WebGUI.php

Lines changed: 80 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,28 +49,95 @@ public function accountManage() : void {
4949
$account = new Template('account');
5050
$this->mainTemp->includeTemplate($account);
5151

52-
// handle pw change
53-
if( !empty($_POST['type']) && $_POST['type'] === 'change'){
54-
$newPw = $this->param->loginPost('password');
52+
$share = new Share($this->login);
53+
54+
// handle pw change, share add
55+
if( !empty($_POST['type']) ){
56+
if($_POST['type'] === 'change'){
57+
$newPw = $this->param->loginPost('password');
58+
$account->setContent('NOTEDISABLE','');
59+
if(!empty($newPw)){
60+
if(Login::createNewGroup(
61+
$this->login->getGroupList(),
62+
$this->login->getGroup(),
63+
$newPw,
64+
$this->login->isAdmin()
65+
)){
66+
$account->setContent('NOTEMSG','Changed password!');
67+
}
68+
else{
69+
$account->setContent('NOTEMSG','Error changing password!');
70+
}
71+
}
72+
else{
73+
$account->setContent('NOTEMSG','Invalid password given!');
74+
}
75+
}
76+
else if($_POST['type'] === 'share'){
77+
$account->setContent('NOTEDISABLE','');
78+
if( !empty($_POST['category']) && !empty($_POST['group']) ){
79+
$cat = $_POST['category'];
80+
$gr = $this->param->loginPost('group');
81+
if( InputParser::checkCategoryInput($cat) ){
82+
$share->addShare($cat, $gr);
83+
if($share->hasError()){
84+
$account->setContent('NOTEMSG', $share->getErrorMessage());
85+
}
86+
else{
87+
$account->setContent('NOTEMSG','Share was added.');
88+
}
89+
}
90+
else{
91+
$account->setContent('NOTEMSG','Invalid category given.');
92+
}
93+
}
94+
else {
95+
$account->setContent('NOTEMSG','Please give category and user to share with.');
96+
}
97+
}
98+
}
99+
else if(!empty($_GET['remove']) && is_string($_GET['remove'])
100+
&& !empty($_GET['group']) && is_string($_GET['group'])
101+
){
55102
$account->setContent('NOTEDISABLE','');
56-
if(!empty($newPw)){
57-
if(Login::createNewGroup(
58-
$this->login->getGroupList(),
59-
$this->login->getGroup(),
60-
$newPw,
61-
$this->login->isAdmin()
62-
)){
63-
$account->setContent('NOTEMSG','Changed password!');
103+
$gr = preg_replace('/[^A-Za-z0-9]/', '', $_GET['group']);
104+
$cat = $_GET['remove'];
105+
106+
if( InputParser::checkCategoryInput($cat) ){
107+
$share->removeShare($cat, $gr);
108+
if($share->hasError()){
109+
$account->setContent('NOTEMSG', $share->getErrorMessage());
64110
}
65111
else{
66-
$account->setContent('NOTEMSG','Error changing password!');
112+
$account->setContent('NOTEMSG','Share was deleted.');
67113
}
68114
}
69115
else{
70-
$account->setContent('NOTEMSG','Invalid password given!');
116+
$account->setContent('NOTEMSG','Invalid category given while deleting share.');
71117
}
72118
}
73119

120+
$d = array();
121+
foreach($share->getCategoriesAndGroups() as $n => $v){
122+
$d[$n] = array();
123+
foreach($v as $e){
124+
$d[$n][] = array(
125+
"NAME" => $e,
126+
"VALUE" => $e
127+
);
128+
}
129+
}
130+
$account->setMultipleContent('Category', $d['categories']);
131+
$account->setMultipleContent('Group', $d['groups']);
132+
$shares = array();
133+
foreach($share->getShares() as $s){
134+
$shares[] = array(
135+
'CATEGORY' => $s['category'],
136+
'GROUP' => $s['group']
137+
);
138+
}
139+
$account->setMultipleContent('Shares', $shares);
140+
74141
if($this->login->isAdmin()){
75142
$account->setContent('ISADMIN', '');
76143

php/core/templates/account.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,18 @@
88
"%%ANAME%%" : "Account",
99
"%%AADMIN%%" : "No",
1010
"%%AID%%" : "-1"
11+
},
12+
"Shares" : {
13+
"%%CATEGORY%%" : "",
14+
"%%GROUP%%" : ""
15+
},
16+
"Category" : {
17+
"%%VALUE%%" : "",
18+
"%%NAME%%" : ""
19+
},
20+
"Group" : {
21+
"%%VALUE%%" : "",
22+
"%%NAME%%" : ""
1123
}
1224
}
1325
}

php/core/templates/account_en.html

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ <h3>Change Password</h3>
2020
</form>
2121

2222
<h3>Share Stats</h3>
23-
<h4>Your Shares</h4>
23+
<h4>Your shares</h4>
2424
<table class="accounttable table table-striped table-responsive-sm">
2525
<tr>
2626
<thead class="thead-dark">
@@ -38,33 +38,33 @@ <h4>Your Shares</h4>
3838
<!--MULTIPLE-Shares-END-->
3939
</table>
4040

41-
<h4>Add Share</h4>
41+
<h4>Add share</h4>
4242
<form action="%%SERVERURL%%/?task=account" method="post">
4343
<input type="hidden" value="share" name="type">
4444
<div class="form-group row">
45-
<label for="devices" class="col-sm-2 col-form-label">Category to share</label>
45+
<label for="category" class="col-sm-2 col-form-label">Category to share</label>
4646
<div class="col-sm-10">
47-
<select name="category" id="devices" class="custom-select" size=4>
47+
<select name="category" id="category" class="custom-select">
4848
<!--MULTIPLE-Category-BEGIN-->
49-
<option value="%%VALUE%%" selected>%%NAME%%</option>
49+
<option value="%%VALUE%%">%%NAME%%</option>
5050
<!--MULTIPLE-Category-END-->
5151
</select>
5252
</div>
5353
</div>
5454
<div class="form-group row">
55-
<label for="devices" class="col-sm-2 col-form-label">User to share with</label>
55+
<label for="group" class="col-sm-2 col-form-label">User to share with</label>
5656
<div class="col-sm-10">
57-
<select name="group" id="devices" class="custom-select" size=4>
57+
<select name="group" id="group" class="custom-select">
5858
<!--MULTIPLE-Group-BEGIN-->
59-
<option value="%%VALUE%%" selected>%%NAME%%</option>
59+
<option value="%%VALUE%%">%%NAME%%</option>
6060
<!--MULTIPLE-Group-END-->
6161
</select>
6262
</div>
6363
</div>
6464
<div class="form-group row">
6565
<div class="col-sm-2">&nbsp;</div>
6666
<div class="col-sm-10">
67-
<input type="submit" value="Add share" class="btn btn-secondary">
67+
<input type="submit" value="Start sharing" class="btn btn-secondary">
6868
</div>
6969
</div>
7070
</form>

php/core/templates/stats.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,17 @@
22
"%%NOTEDISABLE%%" : "disable",
33
"%%CMDDISABLE%%" : "disable",
44
"%%LESSDATADISABLE%%" : "disable",
5+
"%%SHARESDIABLE%%" : "disable",
56
"%%NOTEMSG%%" : "",
67
"%%TODAY%%" : "",
78
"multiples" : {
89
"Devices" : {
910
"%%VALUE%%" : "",
1011
"%%NAME%%" : ""
12+
},
13+
"Shares" : {
14+
"%%VALUE%%" : "",
15+
"%%NAME%%" : ""
1116
}
1217
},
1318
"%%TABLEA%%" : "",

php/core/templates/stats_en.html

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,16 @@ <h3>Select data</h3>
6161
</select>
6262
</div>
6363
</div>
64+
<div class="form-group row %%SHARESDIABLE%%">
65+
<label for="shares" class="col-sm-2 col-form-label">Include shares</label>
66+
<div class="col-sm-10">
67+
<select name="shares[]" id="shares" class="custom-select" size=4 multiple>
68+
<!--MULTIPLE-Shares-BEGIN-->
69+
<option value="%%VALUE%%">%%NAME%%</option>
70+
<!--MULTIPLE-Shares-END-->
71+
</select>
72+
</div>
73+
</div>
6474
<div class="form-group row">
6575
<div class="col-sm-2">&nbsp;</div>
6676
<div class="col-sm-10">
@@ -83,10 +93,12 @@ <h3>Select data</h3>
8393
$("form").on("submit", () => {
8494
var values = {};
8595
$("input, select").each((k,v) => {
86-
values[$(v).attr("name")] = {
87-
value : $(v).val(),
88-
tag : $(v).prop("tagName")
89-
};
96+
if($(v).attr('id') !== 'shares' ){
97+
values[$(v).attr("name")] = {
98+
value : $(v).val(),
99+
tag : $(v).prop("tagName")
100+
};
101+
}
90102
});
91103
localStorage.setItem( 'lastServerStats', JSON.stringify(values));
92104
});

0 commit comments

Comments
 (0)