/
BookmarkletController.php
73 lines (63 loc) · 2.2 KB
/
BookmarkletController.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
<?php
namespace App\Http\Controllers\App;
use App\Http\Controllers\Controller;
use App\Models\Link;
use Illuminate\Contracts\View\Factory;
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
class BookmarkletController extends Controller
{
/**
* Show the link creation form based on the information provided by the Bookmarklet.
*
* @param Request $request
* @return Factory|RedirectResponse|View
*/
public function getLinkAddForm(Request $request)
{
$newUrl = $request->input('u');
$newTitle = $request->input('t');
$newDescription = $request->input('d');
// Redirect to the login if the user is not logged in
if (!auth()->check()) {
// Save details for the link in the session
session(['bookmarklet.new_url' => $newUrl]);
session(['bookmarklet.new_title' => $newTitle]);
session(['bookmarklet.new_description' => $newDescription]);
session(['bookmarklet.login_redirect' => true]);
return redirect()->route('bookmarklet-login');
}
if ($newUrl === null) {
// Receive the link details from the session after the user logged in
$newUrl = session()->pull('bookmarklet.new_url');
$newTitle = session()->pull('bookmarklet.new_title');
$newDescription = session()->pull('bookmarklet.new_description');
}
session(['bookmarklet.create' => true]);
return view('app.bookmarklet.create', [
'existing_link' => Link::withTrashed()->whereUrl($newUrl)->first() ?: false,
'bookmark_url' => $newUrl,
'bookmark_title' => $newTitle,
'bookmark_description' => $newDescription,
]);
}
/**
* Display the confirmation screen after adding a new link.
*
* @return View
*/
public function getCompleteView(): View
{
return view('app.bookmarklet.complete');
}
/**
* Return a special version of the login form made for the Bookmarklet.
*
* @return View
*/
public function getLoginForm(): View
{
return view('app.bookmarklet.login');
}
}