Skip to content

Commit

Permalink
Algolia pushToIndex is now triggered manually, because event is fired…
Browse files Browse the repository at this point in the history
… before pivot table sync.
  • Loading branch information
Ardakilic committed Dec 26, 2016
1 parent 0185b03 commit b40fe80
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 55 deletions.
14 changes: 12 additions & 2 deletions app/Http/Controllers/Admin/PhotoController.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ public function getGrid()
}


// Creation methods are exactly the same as the PhotoController@getNew and postNew, so no need to copy-paste here.

public function getEdit($id)
{
$photo = Photo::with('user', 'tags')->find($id);
Expand Down Expand Up @@ -93,7 +95,7 @@ public function postEdit($id, Request $request)
if ($isFileUploaded) {

//First, delete the old photos and all sizes using Croppa
Croppa::delete(config('whatthetag.uploads_folder'). '/' . $photo->image);
Croppa::delete(config('whatthetag.uploads_folder') . '/' . $photo->image);

//Then Upload the image and return the filename
$upload = Photo::upload($request->file('photo'));
Expand Down Expand Up @@ -132,6 +134,9 @@ public function postEdit($id, Request $request)
//Now, sync all the associated tags
$photo->tags()->sync($tagIds);

// Push to Algolia, auto-index disabled because event is fired before pivot table sync.
$photo->pushToIndex();

return back()
->withSuccess('Photo updated successfully!');
}
Expand All @@ -146,14 +151,19 @@ public function getDelete($id)
}

// First delete the photo
Croppa::delete(config('whatthetag.uploads_folder'). '/' . $photo->image);
Croppa::delete(config('whatthetag.uploads_folder') . '/' . $photo->image);

//If foreign keys were not added to pivot table as on delete cascade, we also needed to delete the tags beforehand
//I'm leaving this here just for reference
//$photo->tags()->detach();

$photo->delete();

// Remove From Algolia
// Commented-out, because event can handle this well enough,
// unlike upon creation and update which also has to mess with pivot table stuff
// $photo->removeFromIndex();

return back()
->withSuccess('Photo deleted successfully!');
}
Expand Down
103 changes: 54 additions & 49 deletions app/Http/Controllers/PhotoController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,17 @@
use Str;
use Auth;

class PhotoController extends Controller {

class PhotoController extends Controller
{

//Only users can access this route
public function __construct()
{
$this->middleware('auth', ['only' => ['getNew', 'postNew']]);
//I wish we could pass parameters to middlewares
$this->middleware('validSlugFirstParameter', ['only' => ['getTagged', 'getDetail', 'getUser']]);
}

/**
* Display a listing of the resource.
*
Expand All @@ -34,112 +35,113 @@ public function getIndex()
->orderByRandom()
->take(12)
->get();

return view('photos.list')
->withPhotos($photos);
}

public function getSearch()
{
return view('photos.search')
->withTitle('Search for a Photo!');

}

public function getRecents()
{
$photos = Photo::with('tags')
->take(12)
->orderBy('id', 'desc')
->paginate(config('whatthetag.pagination_count'));

return view('photos.list')
->withTitle('Recent Photos')
->withPhotos($photos);
}

public function getTagged($tagSlug)
{
$tag = Tag::with([
'photos' => function($q){
$q->orderBy('id', 'desc');
},
'photos.tags',
])
->whereSlug($tagSlug) //I didn't like findBySlug() provided with Sluggable package
'photos' => function ($q) {
$q->orderBy('id', 'desc');
},
'photos.tags',
])
->whereSlug($tagSlug)//I didn't like findBySlug() provided with Sluggable package
->first();

if(!$tag) {
if (!$tag) {
return redirect('/')
->withError('Tag '.$tagSlug.' not found');
->withError('Tag ' . $tagSlug . ' not found');
}

return view('photos.list')
->withTitle('Photos Tagged With: '.$tagSlug)
->withTitle('Photos Tagged With: ' . $tagSlug)
->withPhotos($tag->photos()->paginate(config('whatthetag.pagination_count')));
}

public function getUser($userSlug)
{
//Let's find the user first
//I don't like findBySlug() method
$user = User::with('photos', 'photos.tags', 'photos.user')
->whereSlug($userSlug)->first();
if(!$user) {

if (!$user) {
return redirect('/')
->withError('User not found');
}

return view('photos.list')
->withTitle('All Photos of: '.$user->name)
->withTitle('All Photos of: ' . $user->name)
->withPhotos($user->photos()->paginate(config('whatthetag.pagination_count')));

}

public function getDetail($photoSlug) {


public function getDetail($photoSlug)
{

$photo = Photo::with('tags', 'user')
->whereSlug($photoSlug)
->first();
if(!$photo) {

if (!$photo) {
return redirect('/')
->withError('Photo not found');
}

return view('photos.detail')
->withPhoto($photo);
}


public function getNew()
{
return view('photos.new');
}

public function postNew(Request $request)
{

$validation = Validator::make($request->all(), [
'title' => 'required|min:2',
'photo' => 'required|image',
'tags' => 'required'
'title' => 'required|min:2',
'photo' => 'required|image',
'tags' => 'required'
]);
if($validation->fails()) {

if ($validation->fails()) {
return back()
->withInput()
->withErrors($validation);
}

//Upload the image and return the filename and full path
$upload = Photo::upload($request->file('photo'));
$upload = Photo::upload($request->file('photo'));

//Tag Stuff
//First, create(if needed) and return IDs of tags
$tagIds = Tag::createAndReturnArrayOfTagIds($request->get('tags'));
$tagIds = Tag::createAndReturnArrayOfTagIds($request->get('tags'));

/*//If user wants to read the tags (keywords) from the file, then we need to fetch them from uploaded file.
if($request->has('read_tags_from_file')) {
$exif = exif_read_data($upload['fullpath'], 'ANY_TAG', true);
Expand All @@ -157,19 +159,22 @@ public function postNew(Request $request)
}
}*/
//Tag Stuff end
$photo = new Photo;
$photo->user_id = Auth::id();
$photo->title = $request->get('title');
$photo->image = $upload['filename'];

$photo = new Photo;
$photo->user_id = Auth::id();
$photo->title = $request->get('title');
$photo->image = $upload['filename'];
$photo->save();

//Now attach the tags, since this is creating method, attach() is okay
$photo->tags()->attach($tagIds);


// Push to Algolia, auto-index disabled because event is fired before pivot table sync.
$photo->pushToIndex();

return back()
->withSuccess('Photo Created Successfully!');

}

}
11 changes: 7 additions & 4 deletions app/Photo.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ public function sluggable()


public $indices;
public static $autoIndex = true;

// $autoIndex is set to false, because there is a pivot relationship and this doesn't fire the event
// Delete can still be done automatically
public static $autoIndex = false;
public static $autoDelete = true;

public function __construct($attributes = [])
Expand Down Expand Up @@ -127,9 +130,9 @@ public static function upload(UploadedFile $file)
// Set visibility to public
'ACL' => 'public-read',

//Let's add a year for cache headers:
'CacheControl' => 'max-age=31536000, public',
'Expires' => gmdate("D, d M Y H:i:s", time() + 31536000) . " GMT",
//Let's add a decade for cache headers:
'CacheControl' => 'max-age=315360000, public',
'Expires' => gmdate("D, d M Y H:i:s", time() + 315360000) . " GMT",
]
);

Expand Down

0 comments on commit b40fe80

Please sign in to comment.