/
SnippetController.php
254 lines (213 loc) · 7.09 KB
/
SnippetController.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
<?php namespace App\Http\Controllers;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Snippet;
use App\User;
use App\Comment;
use App\Repository\ElasticSearchSnippetRepository;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Auth;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Pagination\Paginator;
use Illuminate\Pagination\LengthAwarePaginator;
use App\Utillity\NotificationTrait;
class SnippetController extends Controller
{
use NotificationTrait;
private $snippet;
public function __construct(Snippet $snippet)
{
$this->snippet = $snippet;
$this->middleware('auth');
}
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
$snippets = $this->snippet->all();
return view('home')->with(compact('snippets'));
}
/**
* Show the form for creating a new resource.
*
* @return Response
*/
public function create()
{
$isWeeklyReport = \Session::get('isWeeklyReport');
$template = \Session::get('template');
// $oneBeforeSnippetRow = User::getSnippets(Auth::user()->id, 1);
// if (!empty($oneBeforeSnippetRow)) {
// $parser = new \cebe\markdown\GithubMarkdown();
// $oneBeforeSnippet = $parser->parse($oneBeforeSnippetRow[0]->body);
// }
if($isWeeklyReport) {
return view('snippet/create')->with(compact('isWeeklyReport', 'template'));
}
return view('snippet/create');
}
/**
* Store a newly created resource in storage.
*
* @return Response
*/
public function store(Request $request)
{
$input = $request->all();
if(array_key_exists('selected-tags', $input)) {
$input['user_id'] = Auth::user()->id;
$this->snippet->fill($input);
$this->snippet->save();
// スニペットにタグを紐付ける
$snippet = Snippet::find($this->snippet->id);
$snippet->tags()->attach($input['selected-tags']);
return redirect()->to("/snippet/{$this->snippet->id}");
} else {
\Session::flash('snippetFormError', '必ずタグを選択してください');
$request->flashOnly('title', 'body');
return redirect()->back()->withInput();
}
}
/**
* Display the specified resource.
*
* @param int $id
* @return Response
*/
public function show($id)
{
$parser = new \cebe\markdown\GithubMarkdown();
$query = \Request::query();
try {
if (array_key_exists('read', $query)) $this->isNtyChecked($query['nty']);
$snippet = $this->snippet->findOrFail($id);
$comments = Comment::commentList($id);
$stocksAndComments = $this->snippet->stocksAndCommentsCount($id);
$popularSnippets = User::getMyPopularSnippets($snippet['user_id']);
$parsedComment = array_map(function ($comment) use ($parser) {
$comment->parsedComment = $parser->parse($comment->comment);
return $comment;
}, $comments);
$markdown = $parser->parse($snippet->body);
$snippet['body'] = $markdown;
return view('snippet.show')->with(compact('snippet', 'parsedComment', 'stocksAndComments', 'popularSnippets'));
} catch(ModelNotFoundException $e){
return \Response::view('errors.404', [], '404');
}
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return Response
*/
public function edit($id)
{
$snippet = $this->snippet->find($id);
if ($snippet->user_id === Auth::user()->id) {
return view('snippet.edit')->with(compact('snippet'));
}
return redirect()->back();
}
/**
* Update the specified resource in storage.
*
* @param int $id
* @return Response
*/
public function update(Request $request, $id)
{
$input = $request->all();
if(array_key_exists('selected-tags', $input)) {
$this->snippet->where('id', $id)->update(['title' => $input['title'], 'body' => $input['body']]);
// スニペットにタグを紐付ける
$snippet = Snippet::find($id);
$snippet->tags()->detach();
$snippet->tags()->attach($input['selected-tags']);
return redirect()->to("/snippet/$id");
} else {
\Session::flash('snippetFormError', '必ずタグを選択してください');
$request->flashOnly('title', 'body');
return redirect()->back()->withInput();
}
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return Response
*/
public function destroy($id)
{
$data = $this->snippet->find($id);
$data->delete();
$data->tags()->detach();
return redirect()->to('/');
}
/**
* 「週報」リンクから遷移してきたときにパラメータを渡すためリダイレクトする
*
* @return \Illuminate\Http\RedirectResponse
*/
public function weeklyReportTemplate()
{
$isWeeklyReport = true;
$template = <<< EOS
#### 今週の学習時間
***
ここに書いて下さい
<br>
#### 学習している内容
***
<br>
#### 来週の学習予定時間
***
<br>
#### 来週の学習予定の内容
***
<br>
#### 今週の業務内容
***
<br>
#### 所感
***
<br>
EOS;
return redirect()->to('/snippet/create')->with(compact('isWeeklyReport', 'template'));
}
public function mypage()
{
return view('auth.mypage');
}
/**
* ElasticSearchで全文検索
* @return $this
*/
public function searchOnES()
{
$name = \Input::get('q');
$es = new ElasticSearchSnippetRepository();
$query = $es->search($name);
$count = $query['hits']['total'];
if($query['hits']['total'] > 0) {
$hitList = $query['hits']['hits'];
$perPage = 20;
// $paginatedSearchResults = new LengthAwarePaginator($hitList, $query['hits']['total'], $perPage);
$chunk_articles = array_chunk($hitList, $perPage);
$current_page = \Input::get('page', 1);
$paginatedSearchResults = new LengthAwarePaginator($chunk_articles [$current_page - 1], count($hitList), $perPage);
// $currentPage = LengthAwarePaginator::resolveCurrentPage();
// $collection = new Collection($hitList);
//
// $currentPageSearchResults = $collection->slice($currentPage * $perPage, $perPage)->all();
// $paginatedSearchResults = new LengthAwarePaginator($currentPageSearchResults, count($collection), $perPage);
$paginatedSearchResults->setPath("search");
return view('snippet.search')->with(compact('paginatedSearchResults', 'name', 'count'));
}
return view('snippet.search')->with(compact('name', 'count'));
}
}