1 | <?php |
||
11 | class WorkspaceController implements ControllerProviderInterface |
||
12 | { |
||
13 | |||
14 | public POINT_FOR_USING_A_CONQUERED_BADGE = 200; |
||
|
|||
15 | public POINT_FOR_USING_A_BADGE = 100; |
||
16 | public POINT_DEFAULT = 50; |
||
17 | |||
18 | private $app; |
||
19 | |||
20 | public function connect(Application $app) |
||
21 | { |
||
22 | $this->app = $app; |
||
23 | $factory = $app['controllers_factory']; |
||
24 | # il mount point e' precedente e non serve prima |
||
25 | $this->app['db']; |
||
26 | R::fancyDebug(TRUE); |
||
27 | $factory->get('/', array($this, 'getWorkspaceList')); |
||
28 | $factory->post('/', array($this, 'createWorkspace')); |
||
29 | $factory->get('/{id}', array($this, 'getWorkspace')); |
||
30 | $factory->get('/{id}/share', array($this, 'share')); |
||
31 | $factory->post('/{id}/part', array($this, 'postPart')); |
||
32 | $factory->get('/{id}/part/$id_part', array($this, 'getPart')); |
||
33 | $factory->patch('/{id}/part/$id_part/checkin', array($this, 'checkin')); |
||
34 | return $factory; |
||
35 | } |
||
36 | public function getSessionId() { |
||
37 | $user_id = $this->app['session']->get('user')['id']; |
||
38 | return $user_id; |
||
39 | } |
||
40 | public function getWorkspaceList(Request $request) |
||
41 | { |
||
42 | $user_id = $this->getSessionId(); |
||
43 | $workspaces = R::getAll("SELECT ws.id, |
||
44 | ws.title, |
||
45 | ws.description, |
||
46 | ws.environment, |
||
47 | ws.completed |
||
48 | FROM userworkspace AS uws |
||
49 | LEFT JOIN workspace AS ws |
||
50 | ON uws.workspace = ws.id |
||
51 | WHERE uws.user = ?",[$user_id]); |
||
52 | $list = []; |
||
53 | foreach ($workspaces as $ws) { |
||
54 | array_push($list, [ |
||
55 | "id"=>$ws['id'], |
||
56 | "title"=>$ws['title'], |
||
57 | "description"=>$ws['description'], |
||
58 | "environment"=>$ws['environment'], |
||
59 | "point"=>0, //TODO fare una view con i point già calcolati per il ws |
||
60 | "completed"=>$ws['completed'], |
||
61 | ]); |
||
62 | } |
||
63 | $headers = []; |
||
64 | return JsonResponse::create($list, 200, $headers)->setSharedMaxAge(300); |
||
65 | |||
66 | } |
||
67 | public function createWorkspace(Request $request) |
||
68 | { |
||
69 | $user_id = $this->getSessionId(); |
||
70 | $counter = 0; |
||
71 | $data = json_decode($request->getContent(), true); |
||
72 | //TODO validate json_decode |
||
73 | $title = $data['title']; |
||
74 | $description = $data['description']; |
||
75 | $environment = $data['environment']; |
||
76 | |||
77 | $patrol = $data['team']['patrol']; |
||
78 | $unit = $data['team']['unit']; |
||
79 | $group = $data['team']['group']; |
||
80 | |||
81 | //save the workspace get id |
||
82 | $ws = R::dispense("workspace"); |
||
83 | $ws->title = $title; |
||
84 | $ws->description = $description; |
||
85 | $ws->environment = $environment; |
||
86 | $ws->completed = false; |
||
87 | $ws->inserttime = date('Y-m-d H:i:s'); |
||
88 | $ws->lastupdatetime = date('Y-m-d H:i:s'); |
||
89 | $id = R::store($ws); |
||
90 | |||
91 | //save the team |
||
92 | $team = R::dispense("team"); |
||
93 | $team->workspace = $id; |
||
94 | $team->patrol = $patrol; |
||
95 | $team->unit = $unit; |
||
96 | $team->group = $group; |
||
97 | $team_id = R::store($team); |
||
98 | |||
99 | //create a phantom part to add badge |
||
100 | $part = R::dispense("part"); |
||
101 | $part->workspace = $id; |
||
102 | $part->user = $user_id; |
||
103 | $part->inserttime = date('Y-m-d H:i:s'); |
||
104 | $part->lastupdatetime = date('Y-m-d H:i:s'); |
||
105 | $part->totalpoint = 0; |
||
106 | $part_id = R::store($part); |
||
107 | |||
108 | //add the badge to the project |
||
109 | foreach ($data['badges'] as $badge_id) { |
||
110 | //TODO insert those badge as first hidden post |
||
111 | $pb = R::dispense("partbadge"); |
||
112 | $pb->badge = $badge_id; |
||
113 | $pb->part = $part_id; |
||
114 | $pb->inserttime = date('Y-m-d H:i:s'); |
||
115 | $tmp = R::store($pb); |
||
116 | } |
||
117 | |||
118 | //add the workspace created to the user as owner |
||
119 | $usw = R::dispense("userworkspace"); |
||
120 | $usw->user = $user_id; |
||
121 | $usw->workspace = $id; |
||
122 | $usw->inserttime = date('Y-m-d H:i:s'); |
||
123 | R::store($usw); |
||
124 | |||
125 | $res = ["id" => $id]; |
||
126 | $headers = []; |
||
127 | return JsonResponse::create($res, 201, $headers)->setSharedMaxAge(300); |
||
128 | } |
||
129 | |||
130 | public function getWorkspace($id, Request $request) { |
||
131 | $user_id = $this->getSessionId(); |
||
132 | //TODO controllare che l'utente abbia diritto a vedere questo workspace |
||
133 | |||
134 | $workspace = R::findOne("workspace", "id = ?", [$id]); |
||
135 | $part = R::findAll("part", "workspace = ?", [$id]); |
||
136 | |||
137 | $badges = R::findAll("workspacebadge", "workspace = ?", [$id]); |
||
138 | |||
139 | $l_part = []; |
||
140 | foreach ($part as $p) { |
||
141 | array_push($l_part, intval($p['id'])); |
||
142 | } |
||
143 | $l_badges = []; |
||
144 | foreach ($badges as $b) { |
||
145 | array_push($l_badges, intval($b['badge'])); |
||
146 | } |
||
147 | |||
148 | $res = [ |
||
149 | 'id'=> $workspace['id'], |
||
150 | 'title'=> $workspace['title'], |
||
151 | 'description'=> $workspace['description'], |
||
152 | 'environment'=> $workspace['environment'], |
||
153 | 'environment'=> $workspace['environment'], |
||
154 | 'completed'=> $workspace['completed'], |
||
155 | 'parts'=>$l_part, |
||
156 | 'badges'=>$l_badges |
||
157 | ]; |
||
158 | $headers = []; |
||
159 | return JsonResponse::create($res, 201, $headers)->setSharedMaxAge(300); |
||
160 | } |
||
161 | |||
162 | public function share($id, Request $request) { |
||
163 | $generatedKey = hash("sha256", (mt_rand(10000, 99999).time().$id)); |
||
164 | //TODO verificare documentazione realtiva sulla reale entropia generata da questo sistema |
||
165 | $user_id = $this->getSessionId(); |
||
166 | $share = R::dispense("share"); |
||
167 | $share->user = $user_id; |
||
168 | $share->workspace = $id; |
||
169 | $share->key = $generatedKey; |
||
170 | $share->inserttime = date('Y-m-d H:i:s'); |
||
171 | $share_id = R::store($share); |
||
172 | |||
173 | $date = new \DateTime(); |
||
174 | date_add($date, date_interval_create_from_date_string('15 minutes')); |
||
175 | |||
176 | $res = [ |
||
177 | "id"=>$share_id, |
||
178 | "key"=>$generatedKey, |
||
179 | "expire"=>$date->format('Y-m-d H:i:s') |
||
180 | ]; |
||
181 | |||
182 | $headers = []; |
||
183 | return JsonResponse::create($res, 200, $headers)->setSharedMaxAge(300); |
||
184 | } |
||
185 | |||
186 | public function postPart($id, Request $request) { |
||
187 | $user_id = $this->getSessionId(); |
||
188 | |||
306 |