Ejemplo n.º 1
0
 /**
  * Search all resources that current user has read access.
  *
  * NOTE: Even search results are asociated to an user they are persisted
  * using CACHE instead of COOKIE because too big search results may lead
  * to excesively big HTTP headers resulting in a web server error response.
  * When the user logs out an event handler will purge the search results
  * from cache.
  *
  * @return Response
  */
 public function search(Request $request)
 {
     $user = $request->user();
     $searchableModels = $this->getSearchableModels($user);
     // Validate form
     $this->validate($request, ['query' => 'required|min:1', 'sections' => 'required|array|in:' . implode(',', array_keys($searchableModels))]);
     // Remove unselected models
     $selectedModels = $request->input('sections', []);
     foreach ($searchableModels as $permission => $model) {
         if (!in_array($permission, $selectedModels)) {
             unset($searchableModels[$permission]);
         }
     }
     // Initializate results
     $query = addcslashes($request->input('query'), '%_');
     // Sanitize for 'LIKE' searches
     $session = $request->session();
     $currentRoute = $request->route()->getName();
     $cacheId = 'adminSearchResults' . $user->getKey();
     $results = collect();
     $totalResults = 0;
     // Perform search for all searchable models
     foreach ($searchableModels as $model) {
         $collection = $model->search($query);
         if (!($count = $collection->count())) {
             continue;
         }
         $totalResults += $count;
         $modelName = class_basename($model);
         // Build results object
         $result = new \stdClass();
         $result->label = $model->plural();
         $result->route = replace_last_segment($currentRoute, strtolower(str_plural($modelName)) . '.show');
         $result->collection = $collection;
         $results[$modelName] = $result;
     }
     // Results found
     if ($results->count()) {
         // Sort results showing first the models with smaller collections
         $results = $results->sort(function ($result1, $result2) {
             $count1 = $result1->collection->count();
             $count2 = $result2->collection->count();
             // Is same count order alphabetically
             if ($count1 === $count2) {
                 return strcasecmp($result1->label, $result2->label);
             }
             return $count1 < $count2 ? -1 : 1;
         });
         // Store results in the cache for 5 minutes
         Cache::put($cacheId, $results, 5);
         $session->flash('success', sprintf(_('%d results found'), $totalResults));
     } else {
         Cache::forget($cacheId);
         $session->flash('error', _('No results found'));
     }
     return redirect()->route('admin')->withInput();
 }
Ejemplo n.º 2
0
 /**
  * Display a listing of the resource.
  *
  * @return Response
  */
 public function index()
 {
     // Paginate resource resutls
     $results = $this->paginate();
     // If results found add asset to make tables responsive
     $results->total();
     // Create header links for sorting by column
     $links = (object) link_to_sort_by($this->resource->getVisibleLabels());
     // Set the route for the return button
     $returnRouteName = replace_last_segment($this->prefix);
     // Add data to the view
     $view = view('resource.index', compact(['results', 'links', 'returnRouteName']));
     // Add data to the layout
     $this->layout->title = $this->resource->plural();
     $this->layout->subtitle = $results->lastPage() > 1 ? sprintf(_('Page %d/%d'), $results->currentPage(), $results->lastPage()) : _('Index');
     // Return layout + view
     return $this->layout($view);
 }