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+ ?>
0 commit comments