Version 4.5.0
This commit is contained in:
1 parent
1a0c7fd3c2
commit
f79dcf067a
3791 files changed
+248032
-76372
No files matched your search
+69
-45
@@ -11,7 +11,7 @@
|
||||
namespace IPS\Helpers\Table;
|
||||
|
||||
/* To prevent PHP errors (extending class does not exist) revealing path */
|
||||
if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
{
|
||||
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
|
||||
exit;
|
||||
@@ -62,6 +62,11 @@ class _Db extends Table
|
||||
*/
|
||||
public $keyField = NULL;
|
||||
|
||||
/**
|
||||
* @brief The database we will query against
|
||||
*/
|
||||
public $db;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
@@ -69,19 +74,19 @@ class _Db extends Table
|
||||
* @param \IPS\Http\Url $baseUrl Base URL
|
||||
* @param array|null $where WHERE clause
|
||||
* @param array|null $forceIndex Index to force
|
||||
* @param \IPS\Db|null $database An instance of \IPS\Db to run the queries against (defaults to current connection)
|
||||
* @return void
|
||||
*/
|
||||
public function __construct( $table, \IPS\Http\Url $baseUrl, $where=NULL, $forceIndex=NULL )
|
||||
public function __construct( $table, \IPS\Http\Url $baseUrl, $where=NULL, $forceIndex=NULL, $database=NULL )
|
||||
{
|
||||
$this->table = $table;
|
||||
$this->where = $where;
|
||||
$this->index = $forceIndex;
|
||||
$this->db = $database ?? \IPS\Db::i();
|
||||
|
||||
return parent::__construct( $baseUrl );
|
||||
}
|
||||
|
||||
protected $onlySelected = false;
|
||||
|
||||
|
||||
/**
|
||||
* Get rows
|
||||
*
|
||||
@@ -91,24 +96,25 @@ class _Db extends Table
|
||||
public function getRows( $advancedSearchValues )
|
||||
{
|
||||
/* Specify filter in where clause */
|
||||
$where = $this->where ? is_array( $this->where ) ? $this->where : array( $this->where ) : array();
|
||||
$where = $this->where ? \is_array( $this->where ) ? $this->where : array( $this->where ) : array();
|
||||
|
||||
if ( $this->filter and isset( $this->filters[ $this->filter ] ) )
|
||||
{
|
||||
$where[] = is_array( $this->filters[ $this->filter ] ) ? $this->filters[ $this->filter ] : array( $this->filters[ $this->filter ] );
|
||||
$where[] = \is_array( $this->filters[ $this->filter ] ) ? $this->filters[ $this->filter ] : array( $this->filters[ $this->filter ] );
|
||||
}
|
||||
|
||||
/* Add quick search term to where clause if necessary */
|
||||
if ( $this->quickSearch !== NULL and \IPS\Request::i()->quicksearch )
|
||||
{
|
||||
if ( is_callable( $this->quickSearch ) )
|
||||
if ( \is_callable( $this->quickSearch ) )
|
||||
{
|
||||
$where[] = call_user_func( $this->quickSearch, trim( \IPS\Request::i()->quicksearch ) );
|
||||
$quickSearchFunc = $this->quickSearch;
|
||||
$where[] = $quickSearchFunc( trim( \IPS\Request::i()->quicksearch ) );
|
||||
}
|
||||
else
|
||||
{
|
||||
$columns = is_array( $this->quickSearch ) ? $this->quickSearch[0] : $this->quickSearch;
|
||||
$columns = is_array( $columns ) ? $columns : array( $columns );
|
||||
$columns = \is_array( $this->quickSearch ) ? $this->quickSearch[0] : $this->quickSearch;
|
||||
$columns = \is_array( $columns ) ? $columns : array( $columns );
|
||||
|
||||
$_where = array();
|
||||
foreach ( $columns as $c )
|
||||
@@ -116,7 +122,7 @@ class _Db extends Table
|
||||
$_where[] = "LOWER(`{$c}`) LIKE CONCAT( '%', ?, '%' )";
|
||||
}
|
||||
|
||||
$where[] = array_merge( array( '(' . implode( ' OR ', $_where ) . ')' ), array_fill( 0, count( $_where ), mb_strtolower( trim( \IPS\Request::i()->quicksearch ) ) ) );
|
||||
$where[] = array_merge( array( '(' . implode( ' OR ', $_where ) . ')' ), array_fill( 0, \count( $_where ), mb_strtolower( trim( \IPS\Request::i()->quicksearch ) ) ) );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -125,11 +131,11 @@ class _Db extends Table
|
||||
{
|
||||
foreach ( $advancedSearchValues as $k => $v )
|
||||
{
|
||||
if ( isset( $this->advancedSearch[ $k ] ) AND $v !== '' )
|
||||
if ( isset( $this->advancedSearch[ $k ] ) AND $v !== '' AND ( !\is_array( $v ) OR !empty( $v ) ) )
|
||||
{
|
||||
$type = $this->advancedSearch[ $k ];
|
||||
|
||||
if ( is_array( $type ) )
|
||||
if ( \is_array( $type ) )
|
||||
{
|
||||
if ( isset( $type[2] ) )
|
||||
{
|
||||
@@ -146,7 +152,7 @@ class _Db extends Table
|
||||
switch ( $type )
|
||||
{
|
||||
case SEARCH_CUSTOM:
|
||||
if ( $clause = call_user_func( $lambda, $v ) )
|
||||
if ( $clause = $lambda( $v ) )
|
||||
{
|
||||
$where[] = $clause;
|
||||
}
|
||||
@@ -160,17 +166,24 @@ class _Db extends Table
|
||||
$where[] = array( "{$k} LIKE ?", '%' . $v . '%' );
|
||||
break;
|
||||
case SEARCH_QUERY_TEXT:
|
||||
switch ( $v[0] )
|
||||
if ( !empty( $v[1] ) )
|
||||
{
|
||||
case 'c':
|
||||
$where[] = array( "{$k} LIKE ?", '%' . $v[1] . '%' );
|
||||
break;
|
||||
case 'bw':
|
||||
$where[] = array( "{$k} LIKE ?", $v[1] . '%' );
|
||||
break;
|
||||
case 'eq':
|
||||
$where[] = array( "{$k}=?", $v[1] );
|
||||
break;
|
||||
switch ( $v[0] )
|
||||
{
|
||||
case 'c':
|
||||
$where[] = array( "{$k} LIKE ?", '%' . $v[1] . '%' );
|
||||
break;
|
||||
case 'bw':
|
||||
$where[] = array( "{$k} LIKE ?", $v[1] . '%' );
|
||||
break;
|
||||
case 'eq':
|
||||
$where[] = array( "{$k}=?", $v[1] );
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
unset( $advancedSearchValues[ $k ] );
|
||||
}
|
||||
break;
|
||||
case SEARCH_DATE_RANGE:
|
||||
@@ -204,7 +217,7 @@ class _Db extends Table
|
||||
case SEARCH_SELECT:
|
||||
if ( isset( $options['multiple'] ) AND $options['multiple'] === TRUE )
|
||||
{
|
||||
$where[] = array( \IPS\Db::i()->in( $k, $v ) );
|
||||
$where[] = array( $this->db->findInSet( $k, $v ) );
|
||||
break;
|
||||
}
|
||||
// No break so we fall through to radio
|
||||
@@ -227,7 +240,7 @@ class _Db extends Table
|
||||
case SEARCH_NODE:
|
||||
$nodeClass = $options['class'];
|
||||
$prop = isset( $options['searchProp'] ) ? $options['searchProp'] : '_id';
|
||||
if ( !is_array( $v ) )
|
||||
if ( !\is_array( $v ) )
|
||||
{
|
||||
$v = array( $v );
|
||||
}
|
||||
@@ -235,7 +248,7 @@ class _Db extends Table
|
||||
$values = array();
|
||||
foreach ( $v as $_v )
|
||||
{
|
||||
if ( !is_object( $_v ) )
|
||||
if ( !\is_object( $_v ) )
|
||||
{
|
||||
if ( mb_substr( $_v, 0, 2 ) === 's.' )
|
||||
{
|
||||
@@ -253,7 +266,7 @@ class _Db extends Table
|
||||
}
|
||||
$values[] = $_v->$prop;
|
||||
}
|
||||
$where[] = array( \IPS\Db::i()->in( $k, $values ) );
|
||||
$where[] = array( $this->db->in( $k, $values ) );
|
||||
break;
|
||||
|
||||
case SEARCH_NUMERIC:
|
||||
@@ -283,10 +296,10 @@ class _Db extends Table
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
$selects = $this->selects;
|
||||
|
||||
if ( count( $this->joins ) )
|
||||
if ( \count( $this->joins ) )
|
||||
{
|
||||
foreach( $this->joins as $join )
|
||||
{
|
||||
@@ -298,8 +311,8 @@ class _Db extends Table
|
||||
}
|
||||
|
||||
/* Count results (for pagination) */
|
||||
$count = \IPS\Db::i()->select( 'count(*)', $this->table, $where );
|
||||
if ( count( $this->joins ) )
|
||||
$count = $this->db->select( 'count(*)', $this->table, $where );
|
||||
if ( \count( $this->joins ) )
|
||||
{
|
||||
foreach( $this->joins as $join )
|
||||
{
|
||||
@@ -309,9 +322,9 @@ class _Db extends Table
|
||||
$count = $count->first();
|
||||
|
||||
/* Now get column headers */
|
||||
$query = \IPS\Db::i()->select( ( count( $selects ) ) ? $this->table . '.*, ' . implode( ', ', $selects ) : '*', $this->table, NULL, NULL, array( 0, 1 ) );
|
||||
$query = $this->db->select( ( \count( $selects ) ) ? $this->table . '.*, ' . implode( ', ', $selects ) : '*', $this->table, NULL, NULL, array( 0, 1 ) );
|
||||
|
||||
if ( count( $this->joins ) )
|
||||
if ( \count( $this->joins ) )
|
||||
{
|
||||
foreach( $this->joins as $join )
|
||||
{
|
||||
@@ -363,8 +376,8 @@ class _Db extends Table
|
||||
|
||||
/* Run query */
|
||||
$rows = array();
|
||||
$select = \IPS\Db::i()->select(
|
||||
( count( $selects ) ) ? $this->table . '.*, ' . implode( ', ', $selects ) : '*',
|
||||
$select = $this->db->select(
|
||||
( \count( $selects ) ) ? $this->table . '.*, ' . implode( ', ', $selects ) : '*',
|
||||
$this->table,
|
||||
$where,
|
||||
$orderBy,
|
||||
@@ -376,7 +389,7 @@ class _Db extends Table
|
||||
$select->forceIndex( $this->index );
|
||||
}
|
||||
|
||||
if ( count( $this->joins ) )
|
||||
if ( \count( $this->joins ) )
|
||||
{
|
||||
foreach( $this->joins as $join )
|
||||
{
|
||||
@@ -415,7 +428,8 @@ class _Db extends Table
|
||||
/* Parse if necessary (NB: deliberately do this before removing the row in case we need to do some processing, but don't want the column to actually show) */
|
||||
if( isset( $this->parsers[ $k ] ) )
|
||||
{
|
||||
$v = call_user_func( $this->parsers[ $k ], $v, $_row );
|
||||
$thisParser = $this->parsers[ $k ];
|
||||
$v = $thisParser( $v, $_row );
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -423,7 +437,7 @@ class _Db extends Table
|
||||
}
|
||||
|
||||
/* Are we including this one? */
|
||||
if( ( ( $this->include !== NULL and !in_array( $k, $this->include ) ) or ( $this->exclude !== NULL and in_array( $k, $this->exclude ) ) ) and !array_key_exists( $k, $advancedSearchValues ) )
|
||||
if( ( ( $this->include !== NULL and !\in_array( $k, $this->include ) ) or ( $this->exclude !== NULL and \in_array( $k, $this->exclude ) ) ) and !array_key_exists( $k, $advancedSearchValues ) )
|
||||
{
|
||||
unset( $row[ $k ] );
|
||||
continue;
|
||||
@@ -436,13 +450,18 @@ class _Db extends Table
|
||||
/* Add in some buttons if necessary */
|
||||
if( $this->rowButtons !== NULL )
|
||||
{
|
||||
$row['_buttons'] = call_user_func( $this->rowButtons, $_row );
|
||||
$rowButtons = $this->rowButtons;
|
||||
$row['_buttons'] = $rowButtons( $_row );
|
||||
}
|
||||
|
||||
/* Highlighting? */
|
||||
if ( isset( $this->parsers['_highlight'] ) and $class = call_user_func( $this->parsers['_highlight'], $_row ) )
|
||||
if ( isset( $this->parsers['_highlight'] ) )
|
||||
{
|
||||
$this->highlightRows[ $rowId ] = $class;
|
||||
$class = $this->parsers['_highlight'];
|
||||
if( $class = $class( $_row ) )
|
||||
{
|
||||
$this->highlightRows[ $rowId ] = $class;
|
||||
}
|
||||
}
|
||||
|
||||
$rows[ $rowId ] = $row;
|
||||
@@ -455,6 +474,11 @@ class _Db extends Table
|
||||
$sortDirection = $this->sortDirection;
|
||||
uasort( $rows, function( $a, $b ) use ( $sortBy, $sortDirection )
|
||||
{
|
||||
if( !isset( $a[ $sortBy ] ) )
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
if( $sortDirection === 'asc' )
|
||||
{
|
||||
return strnatcasecmp( mb_strtolower( $a[ $sortBy ] ), mb_strtolower( $b[ $sortBy ] ) );
|
||||
@@ -472,7 +496,7 @@ class _Db extends Table
|
||||
|
||||
/**
|
||||
* User set sortBy is suitable for an SQL sort operation
|
||||
* @param array $count Result of COUNT(*) query with field names included
|
||||
* @param array $count Result of count(*) query with field names included
|
||||
* @return boolean
|
||||
*/
|
||||
protected function _isSqlSort( $count )
|
||||
@@ -482,7 +506,7 @@ class _Db extends Table
|
||||
return false;
|
||||
}
|
||||
|
||||
if( !is_array( $count ) )
|
||||
if( !\is_array( $count ) )
|
||||
{
|
||||
$count = array( $count );
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user