Version 5.0.0 beta 1
This commit is contained in:
1 parent
25ddeb65d6
commit
15c7beabc5
6736 files changed
+627902
-497943
No files matched your search
@@ -11,26 +11,36 @@
|
||||
namespace IPS\Helpers\Tree;
|
||||
|
||||
/* To prevent PHP errors (extending class does not exist) revealing path */
|
||||
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
|
||||
use Exception;
|
||||
use IPS\IPS;
|
||||
use IPS\Output;
|
||||
use IPS\Request;
|
||||
use IPS\Theme;
|
||||
use Throwable;
|
||||
use function defined;
|
||||
use function intval;
|
||||
|
||||
if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
{
|
||||
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
|
||||
header( ( $_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.0' ) . ' 403 Forbidden' );
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tree Table
|
||||
*/
|
||||
class _Tree
|
||||
class Tree
|
||||
{
|
||||
/**
|
||||
* @brief Title for tree table
|
||||
*/
|
||||
public $title = '';
|
||||
public ?string $title = '';
|
||||
|
||||
/**
|
||||
* @brief URL where the tree table is displayed
|
||||
*/
|
||||
public $url = '';
|
||||
public mixed $url = NULL;
|
||||
|
||||
/**
|
||||
* @brief Callback function to get the root rows
|
||||
@@ -46,6 +56,11 @@ class _Tree
|
||||
* @brief Callback function to get the parent ID for a row
|
||||
*/
|
||||
public $getRowParentId;
|
||||
|
||||
/**
|
||||
* @brief Callback function to get the root buttons
|
||||
*/
|
||||
public $getRootButtons;
|
||||
|
||||
/**
|
||||
* @brief Callback function to get the child rows for a row
|
||||
@@ -55,46 +70,45 @@ class _Tree
|
||||
/**
|
||||
* @brief Searchable?
|
||||
*/
|
||||
public $searchable = FALSE;
|
||||
public bool $searchable = FALSE;
|
||||
|
||||
/**
|
||||
* @brief If true, will prevent any item from being moved out of its current parent, only allowing them to be reordered within their current parent
|
||||
*/
|
||||
protected $lockParents = FALSE;
|
||||
protected bool $lockParents = FALSE;
|
||||
|
||||
/**
|
||||
* @brief If true, root cannot be turned into sub-items, and other items cannot be turned into roots
|
||||
*/
|
||||
protected $protectRoots = FALSE;
|
||||
protected bool $protectRoots = FALSE;
|
||||
|
||||
/**
|
||||
* @brief Number of roots to show per page (NULL for unlimited). In most cases this doesn't make sense, since it makes re-ordering impossible. But for trees which are not orderable and which may contain a lot of roots, you can set this value
|
||||
*/
|
||||
public $rootsPerPage = NULL;
|
||||
public ?int $rootsPerPage = NULL;
|
||||
|
||||
/**
|
||||
* @brief If using $rootsPerPage, a callback function that returns the total number of roots
|
||||
*/
|
||||
public $getTotalRoots = NULL;
|
||||
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param string $url URL where the tree table is displayed
|
||||
* @param string $title Tree Table title
|
||||
* @param callback $getRoots Callback function to get the root rows
|
||||
* @param callback $getRow Callback function to get a single row by ID
|
||||
* @param callback $getRowParentId Callback function to get the parent ID for a row
|
||||
* @param callback $getChildren Callback function to get the child rows for a row
|
||||
* @param callback|null $getRootButtons Callback function to get the root buttons
|
||||
* @param callback $searchable Show the search bar?
|
||||
* @param bool $lockParents If true, will prevent any item from being moved out of its current parent, only allowing them to be reordered within their current parent
|
||||
* @param bool $protectRoots If true, root cannot be turned into sub-items, and other items cannot be turned into roots
|
||||
* @param int|null $rootsPerPage Number of roots to show per page (NULL for unlimited). In most cases this doesn't make sense, since it makes re-ordering impossible. But for trees which are not orderable and which may contain a lot of roots, you can set this value
|
||||
* @param callback $getTotalRoots If using $rootsPerPage, a callback function that returns the total number of roots
|
||||
* @return void
|
||||
* @param mixed $url URL where the tree table is displayed
|
||||
* @param string|null $title Tree Table title
|
||||
* @param callable|null $getRoots Callback function to get the root rows
|
||||
* @param callable|null $getRow Callback function to get a single row by ID
|
||||
* @param callable|null $getRowParentId Callback function to get the parent ID for a row
|
||||
* @param callable|null $getChildren Callback function to get the child rows for a row
|
||||
* @param callback|null $getRootButtons Callback function to get the root buttons
|
||||
* @param callback|bool $searchable Show the search bar?
|
||||
* @param bool $lockParents If true, will prevent any item from being moved out of its current parent, only allowing them to be reordered within their current parent
|
||||
* @param bool $protectRoots If true, root cannot be turned into sub-items, and other items cannot be turned into roots
|
||||
* @param int|null $rootsPerPage Number of roots to show per page (NULL for unlimited). In most cases this doesn't make sense, since it makes re-ordering impossible. But for trees which are not orderable and which may contain a lot of roots, you can set this value
|
||||
* @param callback|null $getTotalRoots If using $rootsPerPage, a callback function that returns the total number of roots
|
||||
*/
|
||||
public function __construct( $url, $title, $getRoots, $getRow, $getRowParentId, $getChildren, $getRootButtons=NULL, $searchable=FALSE, $lockParents=FALSE, $protectRoots=FALSE, $rootsPerPage = NULL, $getTotalRoots = NULL )
|
||||
public function __construct( mixed $url, ?string $title, ?callable $getRoots, ?callable $getRow, ?callable $getRowParentId, ?callable $getChildren, ?callable $getRootButtons=NULL, callable|bool $searchable=FALSE, bool $lockParents=FALSE, bool $protectRoots=FALSE, int $rootsPerPage = NULL, callable $getTotalRoots = NULL )
|
||||
{
|
||||
$this->url = $url;
|
||||
$this->title = $title;
|
||||
@@ -120,11 +134,11 @@ class _Tree
|
||||
try
|
||||
{
|
||||
/* Get rows */
|
||||
$page = isset( \IPS\Request::i()->page ) ? \intval( \IPS\Request::i()->page ) : 1;
|
||||
$page = isset( Request::i()->page ) ? intval( Request::i()->page ) : 1;
|
||||
$root = NULL;
|
||||
$rootParent = NULL;
|
||||
|
||||
if( !\IPS\Request::i()->root )
|
||||
if( !Request::i()->root )
|
||||
{
|
||||
$getRootsFunction = $this->getRoots;
|
||||
$rows = $getRootsFunction( $this->rootsPerPage ? array( ( $page - 1 ) * $this->rootsPerPage, $this->rootsPerPage ) : NULL );
|
||||
@@ -132,17 +146,17 @@ class _Tree
|
||||
else
|
||||
{
|
||||
$getChildrenFunction = $this->getChildren;
|
||||
$rows = $getChildrenFunction( \IPS\Request::i()->root );
|
||||
$rows = $getChildrenFunction( Request::i()->root );
|
||||
|
||||
if ( \IPS\Request::i()->isAjax() )
|
||||
if ( Request::i()->isAjax() )
|
||||
{
|
||||
return \IPS\Theme::i()->getTemplate( 'trees', 'core' )->rows( $rows, mt_rand() );
|
||||
Output::i()->sendOutput( Theme::i()->getTemplate( 'trees', 'core' )->rows( $rows, mt_rand() ) );
|
||||
}
|
||||
|
||||
$getRowFunction = $this->getRow;
|
||||
$root = $getRowFunction( \IPS\Request::i()->root, TRUE );
|
||||
$root = $getRowFunction( Request::i()->root, TRUE );
|
||||
$getRowParentIdFunction = $this->getRowParentId;
|
||||
$rootParent = $getRowParentIdFunction( \IPS\Request::i()->root );
|
||||
$rootParent = $getRowParentIdFunction( Request::i()->root );
|
||||
}
|
||||
|
||||
/* Pagination? */
|
||||
@@ -153,21 +167,18 @@ class _Tree
|
||||
$totalNumber = $getTotalRootsFunction();
|
||||
if ( $totalNumber )
|
||||
{
|
||||
$pagination = \IPS\Theme::i()->getTemplate( 'global', 'core', 'global' )->pagination( $this->url, ceil( $totalNumber / $this->rootsPerPage ), $page, $this->rootsPerPage );
|
||||
$pagination = Theme::i()->getTemplate( 'global', 'core', 'global' )->pagination( $this->url, ceil( $totalNumber / $this->rootsPerPage ), $page, $this->rootsPerPage );
|
||||
}
|
||||
}
|
||||
|
||||
/* Display */
|
||||
$getRootButtonsFunction = $this->getRootButtons;
|
||||
return \IPS\Theme::i()->getTemplate( 'trees', 'core' )->template( $this->url, $this->title, $root, $rootParent, $rows, $getRootButtonsFunction(), $this->lockParents, $this->protectRoots, $this->searchable, $pagination );
|
||||
return Theme::i()->getTemplate( 'trees', 'core' )->template( $this->url, $this->title, $root, $rootParent, $rows, $getRootButtonsFunction(), $this->lockParents, $this->protectRoots, $this->searchable, $pagination );
|
||||
}
|
||||
catch ( \Exception $e )
|
||||
catch ( Exception | Throwable $e )
|
||||
{
|
||||
\IPS\IPS::exceptionHandler( $e );
|
||||
}
|
||||
catch ( \Throwable $e )
|
||||
{
|
||||
\IPS\IPS::exceptionHandler( $e );
|
||||
IPS::exceptionHandler( $e );
|
||||
}
|
||||
return '';
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user