-
Notifications
You must be signed in to change notification settings - Fork 0
/
NewCC.php
executable file
·265 lines (227 loc) · 6.73 KB
/
NewCC.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
<?php
namespace App\Http\Controllers;
use App\Croak;
use App\Tag;
use App\File;
use Illuminate\Http\Request;
class CroakController extends Controller
{
function checkpid($e){
return isset($e['p_id']);
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(Request $req)
{
//global $result;
$result = array();
$croaks = Croak::all();
$i = 0;
foreach($croaks as $c){
$c['tags'] = $c->tags()->get();
if ($c->files()) $c['files'] = $c->files()->get();
$i++;
}
if ( isset($req->tags) ){
//tags query param is a string with each tag label separated by a space
$r_tags = explode(',', $req->tags);
$m = 0; //croaks associated with any (0) or all (1) of these tags
if (isset($req->mode)) $m = $req->mode;
foreach($croaks as $c){
$c_tags = $c->tags()->get();
if ($m==0){
foreach($c_tags as $t){
if (in_array($t->label, $r_tags)){ //if this croak tag matches any request tag
array_push($result, $c);
continue;
}
}
} else {
$include = true;
foreach($r_tags as $rt){
for($i = 0; $i < sizeof($c_tags); $i++){
if ($c_tags[$i]->label === $rt) break;
if ($i == sizeof($c_tags)-1){
$include = false; //none of the tags of this croak matched one of the request tags, therefore this croak cannot be included
break 2;
}
}
}
if ($include) array_push($result, $c);
}
}
} else {
$result = $croaks->toArray();
}
if (isset($req->radius) && isset($req->x) && isset($req->y)){
$s = sizeof($result);
$newresult = array();
for ($i = 0; $i < $s; $i++){
$c = $result[$i];
$latA = $req->y * pi()/180.0;
$lonA = $req->x * pi()/180.0;
$latB = $c['y'] * pi()/180.0;
$lonB = $c['x'] * pi()/180.0;
$dist = acos( sin($latA) * sin($latB) + cos($latA) * cos($latB) * cos($lonA - $lonB) ) * 6371; //km
//echo 'Crk ' . $c['id'] . ': ' . $dist . ' ';
//echo $dist . '<br>';
/* using unset() fucks up the array
if ( $dist > (int)$req->radius + 20){ //add to account for error
//echo 'beyond range';
unset($result[$i]);
} else {
$result[$i]['distance'] = $dist; //give dist to requester while we have it so they don't have to recalculate it
}
* */
if ($dist < (int) $req->radius + 20){
$result[$i]['distance'] = $dist;
array_push($newresult, $result[$i]);
}
}
$result = $newresult;
}
$actualresult = Array(); //i'm starting to see why people hate php now
if (isset($req->p_id)){
//$result = array_filter($result, "checkpid");
for ($j = 0; $j < sizeof($result); $j=$j+1){
if ($result[$j]['p_id'] != null && $result[$j]['p_id'] == $req->p_id){
array_push($actualresult, $result[$j]);
//continue;
}
//both unset and array_splice did not work properly
//unset($result[$j]);
//$result = array_splice($result, $j, 1);
}
return $actualresult;
}
//return $croaks;
return $result;
}
public function attachFilesTags($croak){
$result = array();
$result['tags'] = $croak->tags()->get();
if ($croak->files()) $result['files'] = $croak->files()->get();
return $result;
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$c = new Croak();
$c->type = $request->type;
if (!isset($request->x) || !isset($request->y)){
$c->x = $c->y = 0;
} else {
$c->x = $request->x;
$c->y = $request->y;
}
if (!isset($request->type)){
$c->type = 0;
}
if (isset($request->p_id)){
$c->p_id = $request->p_id;
$p = Croak::find($c->p_id);
$p->replies += 1;
$p->save();
}
$c->ip = \Request::getClientIp(true);
$c->content = $request->content;
$c->fade_rate = .6;
$c->score = 0;
/*
if (Auth::guest()){
//post anonymously
} else {
}
*/
$saved = null;
if ($saved = $c->save()){
$tags = explode(',', $request->tags);
foreach( $tags as $tag){
$tid = Tag::firstOrCreate(['label' => $tag])['id'];
$t = Tag::findOrFail($tid);
$t->refs += 1;
$t->save();
$c->tags()->attach($tid);
}
$files = $request->file('f');
$dst = 'f';
if (!is_null($files)){
foreach($files as $f){
$file;
if ( ($file = File::where('filename', '=', $f->getClientOriginalName())->first()) == null){
$file = File::create(['filename' => $f->getClientOriginalName(), 'path' => $dst . '/' . $f->getClientOriginalName(), 'filesize' => $f->getSize()]);
$upload_suc = $f->move($dst,$file->filename);
}
$c->files()->attach($file['id']);
}
}
//return var_dump($files);
return 0;
} else {
return $saved;
}
}
/**
* Display the specified resource.
*
* @param \App\Post $post
* @return \Illuminate\Http\Response
*/
public function show($id)
{
$croak = Croak::findOrFail($id);
$result = $croak->toArray();
$result['tags'] = $croak->tags()->get();
if ($croak->files()) $result['files'] = $croak->files()->get();
return $result;
//return CroakController::attachFilesTags($croak);
}
/**
* Show the form for editing the specified resource.
*
* @param \App\Post $post
* @return \Illuminate\Http\Response
*/
public function edit(Post $post)
{
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Post $post
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Post $post)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param \App\Post $post
* @return \Illuminate\Http\Response
*/
public function destroy(Post $post)
{
//
}
}