/**
  * Returns a view with all the blueprints
  * available, irregardless of the user
  * who uploaded them
  *
  * @param Blueprint $blueprint
  * @param Guard     $guard
  *
  * @return $this
  */
 public function blueprints(Blueprint $blueprint, Guard $guard)
 {
     $blueprints = $blueprint->select('blueprint.author_name', 'blueprint.user_id', 'blueprint.id', 'blueprint.name', 'blueprint.bc_version', 'blueprint.screenshot', 'blueprint.archive_name', 'blueprint.description', 'blueprint.version')->with(['mods', 'votes' => function ($query) {
         $query->select('blueprint_user_vote.vote', 'blueprint_user_vote.user_id');
     }, 'user' => function ($query) {
         $query->select('users.id', 'users.name');
     }])->orderBy('uploaded_at', 'desc')->paginate(4);
     $blueprintsAsArray = $blueprints->toArray()['data'];
     foreach ($blueprintsAsArray as $key => $value) {
         if (!empty($value['votes'])) {
             $totalVotes = 0;
             foreach ($value['votes'] as $vote) {
                 // get total number of votes
                 $totalVotes += $vote['vote'];
                 // check if the current user has voted
                 if ($guard->check()) {
                     if ($vote['user_id'] == $guard->user()->getAuthIdentifier()) {
                         $blueprintsAsArray[$key]['user_has_voted'] = true;
                         $blueprintsAsArray[$key]['user_vote'] = $vote['vote'];
                     }
                 }
             }
             $blueprintsAsArray[$key]['total_votes'] = $totalVotes;
         } else {
             $blueprintsAsArray[$key]['user_has_voted'] = false;
             $blueprintsAsArray[$key]['total_votes'] = 0;
         }
     }
     return view('buildcraft.singles')->with(['blueprints' => $blueprints, 'blueprintsAsArray' => $blueprintsAsArray]);
 }
 public function postEditCollection($userId, $collectionId, Zipper $zipper, PathBuilder $pathBuilder, PostEditCollectionRequest $request, BlueprintCollection $blueprintCollection, Collection $collection, Filesystem $file, Repository $repository, Blueprint $blueprint)
 {
     $input = $request->get('collection');
     // Check if there's anything to remove
     if (isset($input['remove'])) {
         // Archive default storage path
         $buildcraftArchiveStoragePath = $pathBuilder->create()->fromPublicPath($repository->get('blueprint.paths.storage.buildcraft.archives'));
         // get an array of ids to remove
         foreach ($input['remove'] as $key => $value) {
             $remove[] = $key;
         }
         // Remove the ids
         $blueprintCollection->destroy($remove);
         //Rebuild the archive
         // Get the current archive and delete it
         $currentArchive = $collection->where(['id' => $collectionId])->select('archive_name')->first()->toArray();
         if ($file->exists($buildcraftArchiveStoragePath . $currentArchive['archive_name'])) {
             $file->delete($buildcraftArchiveStoragePath . $currentArchive['archive_name']);
         }
         // Get the ids that are still there
         $rebuild = $blueprintCollection->where(['collection_id' => $collectionId])->select('blueprint_id')->get()->toArray();
         $filesToRearchive = $blueprint->select('archive_name')->whereIn('id', array_flatten($rebuild))->get()->toArray();
         // Create a random name for the collection archive
         $collectionArchiveName = str_replace(' ', '', $input['name']) . '_' . getRandomId() . '_collection' . '.zip';
         // Start creating the archive
         $zipper->make($buildcraftArchiveStoragePath . $collectionArchiveName);
         foreach ($filesToRearchive as $key => $value) {
             // Add each archive to the the newly created archive
             $zipper->add($buildcraftArchiveStoragePath . $value['archive_name']);
         }
         //Clean up
         $zipper->close();
         $input['archive_name'] = $collectionArchiveName;
     }
     // Don't bother checking if anything has changed
     // just update the whole thing
     $collection->find($collectionId)->update($input);
     // Send the user back
     return redirect()->back()->with('collectionUpdateSuccess', true);
 }