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+ private String $ error = "" ;
19+
20+ public function __construct ( Login $ login ) {
21+ $ this ->shares = new JSONReader ('shares ' );
22+ $ this ->login = $ login ;
23+
24+ $ this ->checkInitGroup ($ this ->login ->getGroup ());
25+ }
26+
27+ private function checkInitGroup (string $ group ) : void {
28+ if ( !$ this ->shares ->isValue ([$ group ]) ){
29+ $ this ->shares ->setValue ([$ group ], array (
30+ 'byme ' => array (), // shared by me
31+ 'withme ' => array () // shared with me
32+ ));
33+ }
34+ }
35+
36+ /**
37+ * Get the list of shares this user has created
38+ */
39+ public function getShares () : array {
40+ return $ this ->shares ->getValue ([$ this ->login ->getGroup (), 'byme ' ]);
41+ }
42+
43+ /**
44+ * Add a share for this user (share a category with other group)
45+ */
46+ public function addShare ( string $ category , string $ group ) : void {
47+ if ( $ group !== $ this ->login ->getGroup ()
48+ && $ this ->login ->getGroupList ()->isValue ([$ group ])
49+ ){
50+ if ( in_array ( $ category , $ this ->getAllCategories () ) ){
51+ $ this ->checkInitGroup ($ group );
52+
53+ $ byme = array (
54+ 'category ' => $ category ,
55+ 'group ' => $ group
56+ );
57+ if ( $ this ->shares ->searchValue ([$ this ->login ->getGroup (), 'byme ' ], $ byme ) === false ){
58+ $ this ->shares ->setValue ([$ this ->login ->getGroup (), 'byme ' , null ], $ byme );
59+ $ this ->shares ->setValue ([$ group , 'withme ' , null ], array (
60+ 'category ' => $ category ,
61+ 'group ' => $ this ->login ->getGroup ()
62+ ));
63+ }
64+ else {
65+ $ this ->error = "Share already exists. " ;
66+ }
67+ }
68+ else {
69+ $ this ->error = "Unknown category to share " ;
70+ }
71+ }
72+ else {
73+ $ this ->error = "Invalid group to share with " ;
74+ }
75+ }
76+
77+ /**
78+ * Remove a share for this user (share a category with other group)
79+ */
80+ public function removeShare ( string $ category , string $ group ) : void {
81+ if ( $ group !== $ this ->login ->getGroup ()
82+ && $ this ->login ->getGroupList ()->isValue ([$ group ])
83+ ){
84+ $ this ->checkInitGroup ($ group );
85+
86+ $ byme = array (
87+ 'category ' => $ category ,
88+ 'group ' => $ group
89+ );
90+ $ pos = $ this ->shares ->searchValue ([$ this ->login ->getGroup (), 'byme ' ], $ byme );
91+ if ( $ pos !== false ){
92+ $ this ->shares ->setValue ([$ this ->login ->getGroup (), 'byme ' , $ pos ], null );
93+ }
94+
95+ $ withme = array (
96+ 'category ' => $ category ,
97+ 'group ' => $ this ->login ->getGroup ()
98+ );
99+ $ pos = $ this ->shares ->searchValue ([$ group , 'withme ' ], $ withme );
100+ if ( $ pos !== false ){
101+ $ this ->shares ->setValue ([$ group , 'withme ' , $ pos ], null );
102+ }
103+ }
104+ else {
105+ $ this ->error = "Invalid group to delete share for " ;
106+ }
107+ }
108+
109+ private function getAllCategories (){
110+ $ stats = new TTTStats (['all ' ], API ::getStorageDir ($ this ->login ->getGroup ()));
111+ $ combi = $ stats ->getAllResults ()['combi ' ];
112+ return array_values (array_unique (array_column ($ combi , 'category ' )));
113+ }
114+
115+ public function getCategoriesAndGroups () : array {
116+ return array (
117+ 'categories ' => $ this ->getAllCategories (),
118+ 'groups ' => array_keys ($ this ->login ->getGroupList ()->getArray ())
119+ );
120+ }
121+
122+ public function getErrorMessage () : string {
123+ return $ this ->error ;
124+ }
125+
126+ public function hasError () : bool {
127+ return !empty ($ this ->error );
128+ }
129+
130+ /**
131+ * Get the list of shares shared with this user
132+ */
133+ public function getSharedWithMe () : array {
134+ return $ this ->shares ->getValue ([$ this ->login ->getGroup (), 'withme ' ]);
135+ }
136+ }
137+ ?>
0 commit comments