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

Commit df2e451

Browse files
committed
Share class and Template
1 parent adadea8 commit df2e451

File tree

2 files changed

+166
-0
lines changed

2 files changed

+166
-0
lines changed

php/core/Share.php

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
<?php
2+
/**
3+
* TaskTimeTerminate Sync-Server
4+
* https://github.com/KIMB-technologies/TaskTimeTerminate
5+
*
6+
* (c) 2020 KIMB-technologies
7+
* https://github.com/KIMB-technologies/
8+
*
9+
* released under the terms of GNU Public License Version 3
10+
* https://www.gnu.org/licenses/gpl-3.0.txt
11+
*/
12+
defined( 'TaskTimeTerminate' ) or die('Invalid Endpoint!');
13+
14+
class Share {
15+
16+
private JSONReader $shares;
17+
private Login $login;
18+
19+
public function __construct( Login $login ) {
20+
$this->shares = JSONReader('shares');
21+
$this->login = $login;
22+
23+
$this->checkInitGroup($this->login->getGroup());
24+
}
25+
26+
private function checkInitGroup(string $group) : void {
27+
if( !$this->shares->isValue([$group]) ){
28+
$this->shares->setValue([$group], array(
29+
'byme' => array(), // shared by me
30+
'withme' => array() // shared with me
31+
));
32+
}
33+
}
34+
35+
/**
36+
* Get the list of shares this user has created
37+
*/
38+
public function getShares() : array {
39+
return $this->shares->getValue([$this->login->getGroup(), 'byme']);
40+
}
41+
42+
/**
43+
* Add a share for this user (share a category with other group)
44+
*/
45+
public function addShare( string $category, string $group ) : void {
46+
if( $group !== $this->login->getGroup()
47+
&& $this->login->getGroupList()->isValue([$group])
48+
){
49+
if( in_array( $category, $this->getAllCategories() ) ){
50+
$this->checkInitGroup($group);
51+
52+
$byme = array(
53+
'category' => $category,
54+
'group' => $group
55+
);
56+
if( $this->shares->searchValue([$this->login->getGroup(), 'byme'], $byme) !== false ){
57+
$this->shares->setValue([$this->login->getGroup(), 'byme', null], $byme);
58+
$this->shares->setValue([$group, 'withme', null], array(
59+
'category' => $category,
60+
'group' => $this->login->getGroup()
61+
));
62+
}
63+
}
64+
}
65+
}
66+
67+
/**
68+
* Remove a share for this user (share a category with other group)
69+
*/
70+
public function removeShare( string $category, string $group ) : void {
71+
if( $group !== $this->login->getGroup()
72+
&& $this->login->getGroupList()->isValue([$group])
73+
){
74+
$this->checkInitGroup($group);
75+
76+
$byme = array(
77+
'category' => $category,
78+
'group' => $group
79+
);
80+
$pos = $this->shares->searchValue([$this->login->getGroup(), 'byme'], $byme);
81+
if( $pos !== false ){
82+
$this->shares->setValue([$this->login->getGroup(), 'byme', $pos], null);
83+
}
84+
85+
$withme = array(
86+
'category' => $category,
87+
'group' => $this->login->getGroup()
88+
);
89+
$pos = $this->shares->searchValue([$group, 'withme'], $withme);
90+
if( $pos !== false ){
91+
$this->shares->setValue([$group, 'withme', $pos], null);
92+
}
93+
}
94+
}
95+
96+
private function getAllCategories(){
97+
$stats = new TTTStats(['all'], API::getStorageDir($this->login->getGroup()));
98+
$combi = $stats->getAllResults()['combi'];
99+
return array_values(array_unique(array_column($combi, 'category')));
100+
}
101+
102+
public function getCategoriesAndGroups() : array {
103+
return array(
104+
'categories' => $this->getAllCategories(),
105+
'groups' => array_keys($this->login->getGroupList()->getArray())
106+
);
107+
}
108+
109+
/**
110+
* Get the list of shares shared with this user
111+
*/
112+
public function getSharedWithMe() : array {
113+
return $this->shares->getValue([$this->login->getGroup(), 'withme']);
114+
}
115+
}
116+
?>

php/core/templates/account_en.html

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,56 @@ <h3>Change Password</h3>
1919
</div>
2020
</form>
2121

22+
<h3>Share Stats</h3>
23+
<h4>Your Shares</h4>
24+
<table class="accounttable table table-striped table-responsive-sm">
25+
<tr>
26+
<thead class="thead-dark">
27+
<th>Shared Category</th>
28+
<th>Shared with</th>
29+
<th>Remove</th>
30+
</thead>
31+
</tr>
32+
<!--MULTIPLE-Shares-BEGIN-->
33+
<tr>
34+
<td>%%CATEGORY%%</td>
35+
<td>%%GROUP%%</td>
36+
<td><a href="%%SERVERURL%%/?task=account&remove=%%CATEGORY%%&group=%%GROUP%%" class="confirm confirmLink" title="Remove Share" content="Delete the share of category '%%CATEGORY%%' with user '%%GROUP%%'?"><button type="button" class="btn btn-danger">Remove</button></a></td>
37+
</tr>
38+
<!--MULTIPLE-Shares-END-->
39+
</table>
40+
41+
<h4>Add Share</h4>
42+
<form action="%%SERVERURL%%/?task=account" method="post">
43+
<input type="hidden" value="share" name="type">
44+
<div class="form-group row">
45+
<label for="devices" class="col-sm-2 col-form-label">Category to share</label>
46+
<div class="col-sm-10">
47+
<select name="category" id="devices" class="custom-select" size=4>
48+
<!--MULTIPLE-Category-BEGIN-->
49+
<option value="%%VALUE%%" selected>%%NAME%%</option>
50+
<!--MULTIPLE-Category-END-->
51+
</select>
52+
</div>
53+
</div>
54+
<div class="form-group row">
55+
<label for="devices" class="col-sm-2 col-form-label">User to share with</label>
56+
<div class="col-sm-10">
57+
<select name="group" id="devices" class="custom-select" size=4>
58+
<!--MULTIPLE-Group-BEGIN-->
59+
<option value="%%VALUE%%" selected>%%NAME%%</option>
60+
<!--MULTIPLE-Group-END-->
61+
</select>
62+
</div>
63+
</div>
64+
<div class="form-group row">
65+
<div class="col-sm-2">&nbsp;</div>
66+
<div class="col-sm-10">
67+
<input type="submit" value="Add share" class="btn btn-secondary">
68+
</div>
69+
</div>
70+
</form>
71+
2272
<h3>Administration</h3>
2373
<div class="%%NOTADMIN%%">
2474
This account is not an administration account.

0 commit comments

Comments
 (0)