Advertisement
Guest User

Untitled

a guest
Nov 25th, 2015
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.05 KB | None | 0 0
  1.     // Refactor this plzzz
  2.     public function scopeIntelligentSearch($query, $input)
  3.     {
  4.         $deepQuery = explode(':', str_replace(' ', '', mb_strtolower($input)));
  5.  
  6.         // Special Search in this table
  7.         if(array_key_exists($deepQuery[0], $this->searchable)){
  8.             $length = count($deepQuery);
  9.  
  10.             // Simple Search
  11.             // There will be no higher, lesser nor between
  12.             if($length == 2){
  13.                 if($this->isBrazilianDate($deepQuery[1])){
  14.                     $date = explode('/', $deepQuery[1]);
  15.                     return $query->where($this->getTable().'.'.$this->searchable[$deepQuery[0]], 'like', $date[2].'-'.$date[1].'-'.$date[0].'%');
  16.                 }
  17.                 // If it is not a date, it will try to compare normally
  18.  
  19.                 if($deepQuery[0] == 'nota')
  20.                     $query = $query->where('clients_grade', $compareSign, $params[0]);
  21.  
  22.                 return $query->where($this->getTable().'.'.$this->searchable[$deepQuery[0]], '=', $deepQuery[1]);
  23.             }
  24.  
  25.             // Complex Search
  26.  
  27.             // Case not Between
  28.             if(count($params = explode('~', $deepQuery[2])) == 1){
  29.                
  30.                 if(in_array($deepQuery[1], ['maior', 'depois', 'apos']))
  31.                     $compareSign = ">=";
  32.                 else if(in_array($deepQuery[1], ['menor', 'antes']))
  33.                     $compareSign = "<=";
  34.                 else // Error Case
  35.                     return $query;
  36.  
  37.                 if($deepQuery[0] == 'nota')
  38.                     $query = $query->where('clients_grade', $compareSign, $params[0]);
  39.  
  40.                 if($this->isBrazilianDate($params[0])){
  41.                     $date = explode('/', $params[0]);
  42.                     return $query->where($this->getTable().'.'.$this->searchable[$deepQuery[0]], $compareSign, $date[2].'-'.$date[1].'-'.$date[0].' 00:00:00');
  43.                 }else
  44.                     // If it is not a date, it will try to compare normally
  45.                     return $query->where($this->getTable().'.'.$this->searchable[$deepQuery[0]], $compareSign, $params[0]);
  46.             }
  47.  
  48.             // Case Between
  49.             if(is_numeric($params[0])){
  50.                 // It will assume that it is a date
  51.                 $dates[0] = explode('/', $params[0]);
  52.                 $dates[1] = explode('/', $params[1]);
  53.  
  54.                 if(count($dates[0]) == 3 && count($dates[1]) == 3)
  55.                     return $query->whereBetween($this->getTable().'.'.$this->searchable[$deepQuery[0]], [$dates[0][2].'-'.$dates[0][1].'-'.$dates[0][0],
  56.                                                                                         $dates[1][2].'-'.$dates[1][1].'-'.$dates[1][0],]);
  57.             }
  58.  
  59.             if($deepQuery[0] == 'nota')
  60.                     $query = $query->whereBetween('clients_grade', [$params[0], $params[1]]);
  61.  
  62.             return $query->whereBetween($this->getTable().'.'.$this->searchable[$deepQuery[0]], [$params[0], $params[1]]);
  63.         }
  64.         // Error Case
  65.         return $query;
  66.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement