Version 5.0.14
This commit is contained in:
1 parent
5efe744c5f
commit
dbd774b30c
364 files changed
+7048
-1377
No files matched your search
@@ -13,12 +13,14 @@ namespace IPS\cms;
|
||||
|
||||
/* To prevent PHP errors (extending class does not exist) revealing path */
|
||||
|
||||
use DateInterval;
|
||||
use Exception;
|
||||
use IPS\Application;
|
||||
use IPS\cms\Pages\Page;
|
||||
use IPS\Content\Search\Index;
|
||||
use IPS\Content\ViewUpdates;
|
||||
use IPS\Data\Store;
|
||||
use IPS\DateTime;
|
||||
use IPS\Db;
|
||||
use IPS\Dispatcher;
|
||||
use IPS\File;
|
||||
@@ -32,6 +34,7 @@ use IPS\Node\Permissions;
|
||||
use IPS\Patterns\ActiveRecord;
|
||||
use IPS\Patterns\ActiveRecordIterator;
|
||||
use IPS\Platform\Bridge;
|
||||
use IPS\Request;
|
||||
use IPS\Settings;
|
||||
use IPS\Task;
|
||||
use IPS\Widget\Area;
|
||||
@@ -1490,6 +1493,179 @@ class Databases extends Model implements Permissions
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the filter cookie for this category/database
|
||||
*
|
||||
* @param Categories|null $category
|
||||
* @return array|null
|
||||
*/
|
||||
public function getFilterCookie( ?Categories $category=null ): ?array
|
||||
{
|
||||
$key = $category ? $category->id : 'd' . $this->id;
|
||||
if ( isset( Request::i()->cookie['cms_filters'] ) )
|
||||
{
|
||||
$saved = json_decode( Request::i()->cookie['cms_filters'], TRUE );
|
||||
|
||||
if ( array_key_exists( $key, $saved ) and count( $saved[ $key ] ) )
|
||||
{
|
||||
return $saved[ $key ];
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Save filter cookie for this category
|
||||
*
|
||||
* @param array|bool $values Filter values to save (array) or FALSE to remove cookie
|
||||
* @param Categories|null $category
|
||||
* @return void
|
||||
*/
|
||||
public function saveFilterCookie( array|bool $values, ?Categories $category=null ): void
|
||||
{
|
||||
$key = $category ? $category->id : 'd' . $this->id;
|
||||
$cookie = ( isset( Request::i()->cookie['cms_filters'] ) ) ? json_decode( Request::i()->cookie['cms_filters'], TRUE ) : array();
|
||||
|
||||
if ( $values === FALSE )
|
||||
{
|
||||
if ( array_key_exists( $key, $cookie ) )
|
||||
{
|
||||
unset( $cookie[ $key ] );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/* We only want to include ones where we have actually specified values to filter on */
|
||||
$toSave = array();
|
||||
foreach( $values AS $k => $v )
|
||||
{
|
||||
if ( is_numeric( $k ) or $k == 'cms_record_i_started' )
|
||||
{
|
||||
$toSave[ $k ] = $v;
|
||||
}
|
||||
}
|
||||
|
||||
$cookie[ $key ] = $toSave;
|
||||
}
|
||||
|
||||
Request::i()->setCookie( 'cms_filters', json_encode( $cookie ), DateTime::create()->add( new DateInterval( 'P7D' ) ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Use the cookie to build filters
|
||||
*
|
||||
* @param array $cookie
|
||||
* @param Categories|null $category
|
||||
* @return array
|
||||
*/
|
||||
public function buildWhereFromCookie( array $cookie, ?Categories $category=null ) : array
|
||||
{
|
||||
$where = [];
|
||||
|
||||
/** @var Fields $fieldClass */
|
||||
$fieldClass = 'IPS\cms\Fields' . $this->id;
|
||||
$customFields = $fieldClass::data( 'view', $category, $fieldClass::FIELD_SKIP_TITLE_CONTENT );
|
||||
|
||||
foreach( $cookie as $f => $v )
|
||||
{
|
||||
if ( $f == 'cms_record_i_started' and Member::loggedIn()->member_id )
|
||||
{
|
||||
$where[] = array( 'cms_custom_database_' . $this->id . '.member_id=' . Member::loggedIn()->member_id );
|
||||
continue;
|
||||
}
|
||||
|
||||
$k = 'content_field_' . $f;
|
||||
|
||||
if ( $customFields[ $f ]->type === 'Member' )
|
||||
{
|
||||
if ( ! empty( $v ) )
|
||||
{
|
||||
if ( is_array( $v ) )
|
||||
{
|
||||
foreach( $v as $m )
|
||||
{
|
||||
$member = Member::load( $m );
|
||||
$where[] = [ "FIND_IN_SET( " . $member->member_id . ", REPLACE(field_" . $f . ", '\n',','))" ];
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$member = Member::load( $v );
|
||||
$where[] = [ "FIND_IN_SET( " . $member->member_id . ", REPLACE(field_" . $f . ", '\n',','))" ];
|
||||
}
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if ( isset( $customFields[ $f ] ) and $v !== '___any___' )
|
||||
{
|
||||
if ( is_array( $v ) )
|
||||
{
|
||||
if ( array_key_exists( 'start', $v ) or array_key_exists( 'end', $v ) )
|
||||
{
|
||||
$start = ( $v['start'] instanceof DateTime ) ? $v['start']->getTimestamp() : intval( $v['start'] );
|
||||
$end = ( $v['end'] instanceof DateTime ) ? $v['end']->getTimestamp() : intval( $v['end'] );
|
||||
|
||||
if ( $start or $end )
|
||||
{
|
||||
$where[] = array( '( ' . mb_substr( $k, 8 ) . ' BETWEEN ' . $start . ' AND ' . $end . ' )' );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$like = array();
|
||||
foreach( $v as $val )
|
||||
{
|
||||
if ( $val === 0 or ! empty( $val ) )
|
||||
{
|
||||
$like[] = $val;
|
||||
}
|
||||
}
|
||||
|
||||
if( $customFields[ $f ]->default_value and in_array( $customFields[ $f ]->default_value, $v ) )
|
||||
{
|
||||
$where[] = array( "( " . mb_substr( $k, 8 ) . " IS NULL OR " . Db::i()->findInSet( mb_substr( $k, 8 ), $like ) . ")" );
|
||||
}
|
||||
else
|
||||
{
|
||||
$where[] = array( Db::i()->findInSet( mb_substr( $k, 8 ), $like ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if ( is_bool( $v ) )
|
||||
{
|
||||
/* YesNo fields are false or true */
|
||||
if ( $v === false )
|
||||
{
|
||||
$where[] = array( '(' . mb_substr( $k, 8 ) . ' IS NULL or ' . mb_substr( $k, 8 ) . '=0)' );
|
||||
}
|
||||
else
|
||||
{
|
||||
$where[] = array( mb_substr( $k, 8 ) . "=1" );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if ( $v !== 0 and ! $v )
|
||||
{
|
||||
$where[] = array( mb_substr( $k, 8 ) . " IS NULL" );
|
||||
}
|
||||
else
|
||||
{
|
||||
$where[] = array( mb_substr( $k, 8 ) . "=?", $v );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $where;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get output for API
|
||||
*
|
||||
|
||||
Reference in new issue
Block a user