Browse Source

adding trashed flag

master
Jake Boyles 9 years ago
parent
commit
c6d58e0c17
  1. 20
      src/SearchBuilder.php
  2. 17
      src/SearchDrivers/BaseSearchDriver.php

20
src/SearchBuilder.php

@ -28,6 +28,11 @@ class SearchBuilder
/** /**
* @var * @var
*/ */
private $trashed = false;
/**
* @var
*/
private $config; private $config;
public function __construct(Repository $config) public function __construct(Repository $config)
@ -73,6 +78,19 @@ class SearchBuilder
return $this; return $this;
} }
/**
* @param $trashed
*
* @return $this
*/
public function trashed()
{
$this->trashed = true;
return $this;
}
/** /**
* @param $table * @param $table
* @param $searchFields * @param $searchFields
@ -104,7 +122,7 @@ class SearchBuilder
// Create a new instance of the selected drivers 'class' and pass // Create a new instance of the selected drivers 'class' and pass
// through table and fields to search // through table and fields to search
$driverInstance = new $driver($this->table, $this->searchFields, $relevanceFieldName);
$driverInstance = new $driver($this->table, $this->searchFields, $relevanceFieldName,['*'],$this->trashed);
return $driverInstance; return $driverInstance;
} }

17
src/SearchDrivers/BaseSearchDriver.php

@ -18,6 +18,8 @@ abstract class BaseSearchDriver implements SearchDriverInterface
protected $query; protected $query;
protected $trashed;
/** /**
* @param null $table * @param null $table
* @param array $searchFields * @param array $searchFields
@ -26,12 +28,13 @@ abstract class BaseSearchDriver implements SearchDriverInterface
* *
* @internal param $relevanceField * @internal param $relevanceField
*/ */
public function __construct($table = null, $searchFields = [], $relevanceFieldName, $columns = ['*'])
public function __construct($table = null, $searchFields = [], $relevanceFieldName, $columns = ['*'],$trashed)
{ {
$this->searchFields = $searchFields; $this->searchFields = $searchFields;
$this->table = $table; $this->table = $table;
$this->columns = $columns; $this->columns = $columns;
$this->relevanceFieldName = $relevanceFieldName; $this->relevanceFieldName = $relevanceFieldName;
$this->trashed = $trashed;
} }
/** /**
@ -97,12 +100,24 @@ abstract class BaseSearchDriver implements SearchDriverInterface
*/ */
protected function run() protected function run()
{ {
// If they included trashed flag then give them all records including soft deletes
if($this->trashed)
{
$this->query = \DB::table($this->table)
->select($this->columns)
->addSelect($this->buildSelectQuery($this->searchFields))
->orderBy($this->relevanceFieldName, 'desc')
->having($this->relevanceFieldName, '>', 0);
}
else
{
$this->query = \DB::table($this->table) $this->query = \DB::table($this->table)
->select($this->columns) ->select($this->columns)
->where('deleted_at',NULL) ->where('deleted_at',NULL)
->addSelect($this->buildSelectQuery($this->searchFields)) ->addSelect($this->buildSelectQuery($this->searchFields))
->orderBy($this->relevanceFieldName, 'desc') ->orderBy($this->relevanceFieldName, 'desc')
->having($this->relevanceFieldName, '>', 0); ->having($this->relevanceFieldName, '>', 0);
}
return $this->query; return $this->query;
} }

Loading…
Cancel
Save