13
13
use Str ;
14
14
use Auth ;
15
15
16
- class PhotoController extends Controller {
17
-
16
+ class PhotoController extends Controller
17
+ {
18
+
18
19
//Only users can access this route
19
20
public function __construct ()
20
21
{
21
22
$ this ->middleware ('auth ' , ['only ' => ['getNew ' , 'postNew ' ]]);
22
23
//I wish we could pass parameters to middlewares
23
24
$ this ->middleware ('validSlugFirstParameter ' , ['only ' => ['getTagged ' , 'getDetail ' , 'getUser ' ]]);
24
25
}
25
-
26
+
26
27
/**
27
28
* Display a listing of the resource.
28
29
*
@@ -34,112 +35,113 @@ public function getIndex()
34
35
->orderByRandom ()
35
36
->take (12 )
36
37
->get ();
37
-
38
+
38
39
return view ('photos.list ' )
39
40
->withPhotos ($ photos );
40
41
}
41
-
42
+
42
43
public function getSearch ()
43
44
{
44
45
return view ('photos.search ' )
45
46
->withTitle ('Search for a Photo! ' );
46
-
47
+
47
48
}
48
-
49
+
49
50
public function getRecents ()
50
51
{
51
52
$ photos = Photo::with ('tags ' )
52
53
->take (12 )
53
54
->orderBy ('id ' , 'desc ' )
54
55
->paginate (config ('whatthetag.pagination_count ' ));
55
-
56
+
56
57
return view ('photos.list ' )
57
58
->withTitle ('Recent Photos ' )
58
59
->withPhotos ($ photos );
59
60
}
60
-
61
+
61
62
public function getTagged ($ tagSlug )
62
63
{
63
64
$ tag = Tag::with ([
64
- 'photos ' => function ($ q ){
65
- $ q ->orderBy ('id ' , 'desc ' );
66
- },
67
- 'photos.tags ' ,
68
- ])
69
- ->whereSlug ($ tagSlug ) //I didn't like findBySlug() provided with Sluggable package
65
+ 'photos ' => function ($ q ) {
66
+ $ q ->orderBy ('id ' , 'desc ' );
67
+ },
68
+ 'photos.tags ' ,
69
+ ])
70
+ ->whereSlug ($ tagSlug )//I didn't like findBySlug() provided with Sluggable package
70
71
->first ();
71
72
72
- if (!$ tag ) {
73
+ if (!$ tag ) {
73
74
return redirect ('/ ' )
74
- ->withError ('Tag ' . $ tagSlug. ' not found ' );
75
+ ->withError ('Tag ' . $ tagSlug . ' not found ' );
75
76
}
76
77
77
78
return view ('photos.list ' )
78
- ->withTitle ('Photos Tagged With: ' . $ tagSlug )
79
+ ->withTitle ('Photos Tagged With: ' . $ tagSlug )
79
80
->withPhotos ($ tag ->photos ()->paginate (config ('whatthetag.pagination_count ' )));
80
81
}
81
-
82
+
82
83
public function getUser ($ userSlug )
83
84
{
84
85
//Let's find the user first
85
86
//I don't like findBySlug() method
86
87
$ user = User::with ('photos ' , 'photos.tags ' , 'photos.user ' )
87
88
->whereSlug ($ userSlug )->first ();
88
-
89
- if (!$ user ) {
89
+
90
+ if (!$ user ) {
90
91
return redirect ('/ ' )
91
92
->withError ('User not found ' );
92
93
}
93
-
94
+
94
95
return view ('photos.list ' )
95
- ->withTitle ('All Photos of: ' . $ user ->name )
96
+ ->withTitle ('All Photos of: ' . $ user ->name )
96
97
->withPhotos ($ user ->photos ()->paginate (config ('whatthetag.pagination_count ' )));
97
98
98
99
}
99
-
100
- public function getDetail ($ photoSlug ) {
101
-
100
+
101
+ public function getDetail ($ photoSlug )
102
+ {
103
+
102
104
$ photo = Photo::with ('tags ' , 'user ' )
103
105
->whereSlug ($ photoSlug )
104
106
->first ();
105
-
106
- if (!$ photo ) {
107
+
108
+ if (!$ photo ) {
107
109
return redirect ('/ ' )
108
110
->withError ('Photo not found ' );
109
111
}
110
-
112
+
111
113
return view ('photos.detail ' )
112
114
->withPhoto ($ photo );
113
115
}
114
-
116
+
115
117
116
118
public function getNew ()
117
119
{
118
120
return view ('photos.new ' );
119
121
}
120
-
122
+
121
123
public function postNew (Request $ request )
122
124
{
123
-
125
+
124
126
$ validation = Validator::make ($ request ->all (), [
125
- 'title ' => 'required|min:2 ' ,
126
- 'photo ' => 'required|image ' ,
127
- 'tags ' => 'required '
127
+ 'title ' => 'required|min:2 ' ,
128
+ 'photo ' => 'required|image ' ,
129
+ 'tags ' => 'required '
128
130
]);
129
-
130
- if ($ validation ->fails ()) {
131
+
132
+ if ($ validation ->fails ()) {
131
133
return back ()
132
134
->withInput ()
133
135
->withErrors ($ validation );
134
136
}
135
-
137
+
136
138
//Upload the image and return the filename and full path
137
- $ upload = Photo::upload ($ request ->file ('photo ' ));
139
+ $ upload = Photo::upload ($ request ->file ('photo ' ));
138
140
139
141
//Tag Stuff
140
142
//First, create(if needed) and return IDs of tags
141
- $ tagIds = Tag::createAndReturnArrayOfTagIds ($ request ->get ('tags ' ));
142
-
143
+ $ tagIds = Tag::createAndReturnArrayOfTagIds ($ request ->get ('tags ' ));
144
+
143
145
/*//If user wants to read the tags (keywords) from the file, then we need to fetch them from uploaded file.
144
146
if($request->has('read_tags_from_file')) {
145
147
$exif = exif_read_data($upload['fullpath'], 'ANY_TAG', true);
@@ -157,19 +159,22 @@ public function postNew(Request $request)
157
159
}
158
160
}*/
159
161
//Tag Stuff end
160
-
161
- $ photo = new Photo ;
162
- $ photo ->user_id = Auth::id ();
163
- $ photo ->title = $ request ->get ('title ' );
164
- $ photo ->image = $ upload ['filename ' ];
162
+
163
+ $ photo = new Photo ;
164
+ $ photo ->user_id = Auth::id ();
165
+ $ photo ->title = $ request ->get ('title ' );
166
+ $ photo ->image = $ upload ['filename ' ];
165
167
$ photo ->save ();
166
-
168
+
167
169
//Now attach the tags, since this is creating method, attach() is okay
168
170
$ photo ->tags ()->attach ($ tagIds );
169
-
171
+
172
+ // Push to Algolia, auto-index disabled because event is fired before pivot table sync.
173
+ $ photo ->pushToIndex ();
174
+
170
175
return back ()
171
176
->withSuccess ('Photo Created Successfully! ' );
172
-
177
+
173
178
}
174
179
175
180
}
0 commit comments