Ejemplo n.º 1
0
 /**
  * Return JSON data to the typehead search (used on the dashboard for now)
  * @param  text $search_term The text we're searching for
  * @return Response
  */
 public function get_typeahead($search_term = NULL)
 {
     $json = [];
     // OK, so. I don't think prefetch will work here, unless we use multiple lists and load the ten most recent items into each one...? Pointless. We'll always use $query.
     if ($search_term) {
         // Loop through all classes that we can edit
         $ctrl_classes = CtrlClass::whereRaw('(find_in_set(?, permissions))', ['edit'])->get();
         foreach ($ctrl_classes as $ctrl_class) {
             $class = $ctrl_class->get_class();
             // What are the searchable columns?
             $searchable_properties = $ctrl_class->ctrl_properties()->whereRaw('(find_in_set(?, flags))', ['search'])->whereNull('relationship_type')->get();
             // I have no idea how to include searchable related columns in the query builder below...
             if (!$searchable_properties->isEmpty()) {
                 $query = $class::query();
                 // From http://laravel.io/forum/04-13-2015-combine-foreach-loop-and-eloquent-to-perform-a-search
                 foreach ($searchable_properties as $searchable_property) {
                     /* not needed
                     			if ($loop++ == 1) {							
                     				$class::where($searchable_property->name,'LIKE',"%$query%");
                     			}
                     			else {
                     				$objects::orWhere($searchable_property->name,'LIKE',"%$query%");
                     			}
                     			*/
                     $query->orWhere($searchable_property->name, 'LIKE', "%{$search_term}%");
                 }
                 $objects = $query->get();
                 if (!$objects->isEmpty()) {
                     foreach ($objects as $object) {
                         $result = new \StdClass();
                         $result->class_name = $ctrl_class->get_singular();
                         $result->title = $this->get_object_title($object);
                         $result->edit_link = route('ctrl::edit_object', [$ctrl_class->id, $object->id]);
                         $result->icon = $ctrl_class->get_icon() ? $ctrl_class->get_icon() : 'fa fa-toggle-right';
                         $json[] = $result;
                     }
                 }
             }
         }
     }
     $status = 200;
     return \Response::json($json, $status);
 }