Version 4.3.0

This commit is contained in:
Neo committed 2025-12-19 05:47:12 -08:00
1 parent 96997ddd8e
commit fe1acf984a
1349 files changed
+116159 -67711

No files matched your search

+150 -80
View File
@@ -25,15 +25,17 @@ class _ItemController extends \IPS\Api\Controller
/**
* List
*
* @param array $where Extra WHERE clause
* @param array $where Extra WHERE clause
* @param string $containerParam The parameter which includes the container values
* @param bool $byPassPerms If permissions should be ignored
* @return \IPS\Api\PaginatedResponse
*/
protected function _list( $where = array(), $containerParam = 'categories' )
protected function _list( $where = array(), $containerParam = 'categories', $byPassPerms=FALSE )
{
$class = $this->class;
/* Containers */
if ( isset( \IPS\Request::i()->$containerParam ) )
if ( $containerParam and isset( \IPS\Request::i()->$containerParam ) )
{
$where[] = array( \IPS\Db::i()->in( $class::$databasePrefix . $class::$databaseColumnMap['container'], array_map( 'intval', array_filter( explode( ',', \IPS\Request::i()->$containerParam ) ) ) ) );
}
@@ -134,13 +136,27 @@ class _ItemController extends \IPS\Api\Controller
}
$sortDir = ( isset( \IPS\Request::i()->sortDir ) and in_array( mb_strtolower( \IPS\Request::i()->sortDir ), array( 'asc', 'desc' ) ) ) ? \IPS\Request::i()->sortDir : 'asc';
/* Get results */
if ( $this->member and !$byPassPerms )
{
$query = $class::getItemsWithPermission( $where, "{$sortBy} {$sortDir}", NULL, 'view', \IPS\Content\Hideable::FILTER_AUTOMATIC, 0, $this->member )->getInnerIterator();
$count = $class::getItemsWithPermission( $where, "{$sortBy} {$sortDir}", NULL, 'view', \IPS\Content\Hideable::FILTER_AUTOMATIC, 0, $this->member, FALSE, FALSE, FALSE, TRUE );
}
else
{
$query = \IPS\Db::i()->select( '*', $class::$databaseTable, $where, "{$sortBy} {$sortDir}" );
$count = \IPS\Db::i()->select( 'COUNT(*)', $class::$databaseTable, $where )->first();
}
/* Return */
return new \IPS\Api\PaginatedResponse(
200,
\IPS\Db::i()->select( '*', $class::$databaseTable, $where, "{$sortBy} {$sortDir}" ),
$query,
isset( \IPS\Request::i()->page ) ? \IPS\Request::i()->page : 1,
$class,
\IPS\Db::i()->select( 'COUNT(*)', $class::$databaseTable, $where )->first()
$count,
$this->member,
isset( \IPS\Request::i()->perPage ) ? \IPS\Request::i()->perPage : NULL
);
}
@@ -153,16 +169,24 @@ class _ItemController extends \IPS\Api\Controller
protected function _view( $id )
{
$class = $this->class;
return new \IPS\Api\Response( 200, $class::load( $id )->apiOutput() );
$item = $class::load( $id );
if ( $this->member and !$item->can( 'read', $this->member ) )
{
throw new \OutOfRangeException;
}
return new \IPS\Api\Response( 200, $item->apiOutput( $this->member ) );
}
/**
* Create or update item
*
* @param \IPS\Content\Item $item The item
* @param \IPS\Content\Item $item The item
* @param string $type add or edit
* @return \IPS\Content\Item
*/
protected function _createOrUpdate( \IPS\Content\Item $item )
protected function _createOrUpdate( \IPS\Content\Item $item, $type='add' )
{
/* Title */
if ( isset( \IPS\Request::i()->title ) and isset( $item::$databaseColumnMap['title'] ) )
@@ -174,85 +198,98 @@ class _ItemController extends \IPS\Api\Controller
/* Tags */
if ( ( isset( \IPS\Request::i()->prefix ) or isset( \IPS\Request::i()->tags ) ) and in_array( 'IPS\Content\Tags', class_implements( get_class( $item ) ) ) )
{
$tags = isset( \IPS\Request::i()->tags ) ? array_filter( explode( ',', \IPS\Request::i()->tags ) ) : $item->tags();
if ( isset( \IPS\Request::i()->prefix ) )
{
if ( \IPS\Request::i()->prefix )
if ( !$this->member or $item::canTag( $this->member, $item->containerWrapper() ) )
{
$tags = isset( \IPS\Request::i()->tags ) ? array_filter( explode( ',', \IPS\Request::i()->tags ) ) : $item->tags();
if ( !$this->member or $item::canPrefix( $this->member, $item->containerWrapper() ) )
{
$tags['prefix'] = \IPS\Request::i()->prefix;
if ( isset( \IPS\Request::i()->prefix ) )
{
if ( \IPS\Request::i()->prefix )
{
$tags['prefix'] = \IPS\Request::i()->prefix;
}
}
elseif ( $existingPrefix = $item->prefix() )
{
$tags['prefix'] = $existingPrefix;
}
}
/* we need to save the item before we set the tags because setTags requires that the item exists */
$idColumn = $item::$databaseColumnId;
if ( !$item->$idColumn )
{
$item->save();
}
$item->setTags( $tags );
}
elseif ( $existingPrefix = $item->prefix() )
{
$tags['prefix'] = $existingPrefix;
}
/* we need to save the item before we set the tags because setTags requires that the item exists */
$idColumn = $item::$databaseColumnId;
if ( !$item->$idColumn )
{
$item->save();
}
$item->setTags( $tags );
}
/* Open/closed */
if ( isset( \IPS\Request::i()->locked ) and in_array( 'IPS\Content\Lockable', class_implements( get_class( $item ) ) ) )
{
if ( isset( $item::$databaseColumnMap['locked'] ) )
if ( !$this->member or ( \IPS\Request::i()->locked and $item->canLock( $this->member ) ) or ( !\IPS\Request::i()->locked and $item->canUnlock( $this->member ) ) )
{
$lockedColumn = $item::$databaseColumnMap['locked'];
$item->$lockedColumn = intval( \IPS\Request::i()->locked );
}
else
{
$stateColumn = $item::$databaseColumnMap['status'];
$item->$stateColumn = \IPS\Request::i()->locked ? 'closed' : 'open';
if ( isset( $item::$databaseColumnMap['locked'] ) )
{
$lockedColumn = $item::$databaseColumnMap['locked'];
$item->$lockedColumn = intval( \IPS\Request::i()->locked );
}
else
{
$stateColumn = $item::$databaseColumnMap['status'];
$item->$stateColumn = \IPS\Request::i()->locked ? 'closed' : 'open';
}
}
}
/* Hidden */
if ( isset( \IPS\Request::i()->hidden ) and in_array( 'IPS\Content\Hideable', class_implements( get_class( $item ) ) ) )
{
$idColumn = $item::$databaseColumnId;
if ( \IPS\Request::i()->hidden )
if ( !$this->member or ( \IPS\Request::i()->hidden and $item->canHide( $this->member ) ) or ( !\IPS\Request::i()->hidden and $item->canUnhide( $this->member ) ) )
{
if ( $item->$idColumn )
$idColumn = $item::$databaseColumnId;
if ( \IPS\Request::i()->hidden )
{
$item->hide( FALSE );
}
else
{
if ( isset( $item::$databaseColumnMap['hidden'] ) )
if ( $item->$idColumn )
{
$hiddenColumn = $item::$databaseColumnMap['hidden'];
$item->$hiddenColumn = \IPS\Request::i()->hidden;
$item->hide( FALSE );
}
else
{
$approvedColumn = $item::$databaseColumnMap['approved'];
$item->$approvedColumn = ( \IPS\Request::i()->hidden == -1 ) ? -1 : 0;
if ( isset( $item::$databaseColumnMap['hidden'] ) )
{
$hiddenColumn = $item::$databaseColumnMap['hidden'];
$item->$hiddenColumn = \IPS\Request::i()->hidden;
}
else
{
$approvedColumn = $item::$databaseColumnMap['approved'];
$item->$approvedColumn = ( \IPS\Request::i()->hidden == -1 ) ? -1 : 0;
}
}
}
}
else
{
if ( $item->$idColumn )
{
$item->unhide( FALSE );
}
else
{
if ( isset( $item::$databaseColumnMap['hidden'] ) )
if ( $item->$idColumn )
{
$hiddenColumn = $item::$databaseColumnMap['hidden'];
$item->$hiddenColumn = 0;
$item->unhide( FALSE );
}
else
{
$approvedColumn = $item::$databaseColumnMap['approved'];
$item->$approvedColumn = 1;
if ( isset( $item::$databaseColumnMap['hidden'] ) )
{
$hiddenColumn = $item::$databaseColumnMap['hidden'];
$item->$hiddenColumn = 0;
}
else
{
$approvedColumn = $item::$databaseColumnMap['approved'];
$item->$approvedColumn = 1;
}
}
}
}
@@ -261,33 +298,52 @@ class _ItemController extends \IPS\Api\Controller
/* Pinned */
if ( isset( \IPS\Request::i()->pinned ) and in_array( 'IPS\Content\Pinnable', class_implements( get_class( $item ) ) ) )
{
$pinnedColumn = $item::$databaseColumnMap['pinned'];
$item->$pinnedColumn = intval( \IPS\Request::i()->pinned );
if ( !$this->member or ( \IPS\Request::i()->pinned and $item->canPin( $this->member ) ) or ( !\IPS\Request::i()->pinned and $item->canUnpin( $this->member ) ) )
{
$pinnedColumn = $item::$databaseColumnMap['pinned'];
$item->$pinnedColumn = intval( \IPS\Request::i()->pinned );
}
}
/* Featured */
if ( isset( \IPS\Request::i()->featured ) and in_array( 'IPS\Content\Featurable', class_implements( get_class( $item ) ) ) )
{
$featuredColumn = $item::$databaseColumnMap['featured'];
$item->$featuredColumn = intval( \IPS\Request::i()->featured );
if ( !$this->member or ( \IPS\Request::i()->featured and $item->canFeature( $this->member ) ) or ( !\IPS\Request::i()->featured and $item->canUnfeature( $this->member ) ) )
{
$featuredColumn = $item::$databaseColumnMap['featured'];
$item->$featuredColumn = intval( \IPS\Request::i()->featured );
}
}
/* Update first comment if required */
$field = $item::$databaseColumnMap['first_comment_id'];
/* Update first comment if required, and it's not a new item */
$field = isset( $item::$databaseColumnMap['first_comment_id'] ) ? $item::$databaseColumnMap['first_comment_id'] : NULL;
$commentClass = $item::$commentClass;
$contentField = $commentClass::$databaseColumnMap['content'];
if ( $item::$firstCommentRequired AND isset( $item->$field ) AND isset( \IPS\Request::i()->$contentField ) )
if ( $item::$firstCommentRequired AND isset( $item->$field ) AND isset( \IPS\Request::i()->$contentField ) AND $type == 'edit' )
{
$comment = $commentClass::load( $item->$field );
$comment->$contentField = \IPS\Request::i()->$contentField;
$comment->save();
$content = \IPS\Request::i()->$contentField;
if ( $this->member )
{
$content = \IPS\Text\Parser::parseStatic( $content, TRUE, NULL, $this->member, $item::$application . '_' . mb_ucfirst( $item::$module ) );
}
/* Update Search Index of the first item */
if ( $item instanceof \IPS\Content\Searchable )
{
\IPS\Content\Search\Index::i()->index( $comment );
}
try
{
$comment = $commentClass::load( $item->$field );
}
catch ( \OutOfRangeException $e )
{
throw new \IPS\Api\Exception( 'NO_FIRST_POST', '1S377/1', 400 );
}
$comment->$contentField = $content;
$comment->save();
/* Update Search Index of the first item */
if ( $item instanceof \IPS\Content\Searchable )
{
\IPS\Content\Search\Index::i()->index( $comment );
}
}
/* Return */
@@ -308,18 +364,26 @@ class _ItemController extends \IPS\Api\Controller
$class = $this->class;
/* Work out the date */
$date = \IPS\Request::i()->date ? new \IPS\DateTime( \IPS\Request::i()->date ) : \IPS\DateTime::create();
$date = ( !$this->member and \IPS\Request::i()->date ) ? new \IPS\DateTime( \IPS\Request::i()->date ) : \IPS\DateTime::create();
/* Create item */
$item = $class::createItem( $author, \IPS\Request::i()->ip_address ?: \IPS\Request::i()->ipAddress(), $date, $container );
$this->_createOrUpdate( $item );
$item = $class::createItem( $author, ( !$this->member and \IPS\Request::i()->ip_address ) ? \IPS\Request::i()->ip_address : \IPS\Request::i()->ipAddress(), $date, $container );
$this->_createOrUpdate( $item, 'add' );
$item->save();
/* Create post */
if ( $class::$firstCommentRequired )
{
{
$postContents = \IPS\Request::i()->$firstPostParam;
if ( $this->member )
{
$postContents = \IPS\Text\Parser::parseStatic( $postContents, TRUE, NULL, $this->member, $class::$application . '_' . mb_ucfirst( $class::$module ) );
}
$commentClass = $item::$commentClass;
$post = $commentClass::create( $item, \IPS\Request::i()->$firstPostParam, TRUE, $author->member_id ? NULL : $author->real_name, NULL, $author, $date );
$post = $commentClass::create( $item, $postContents, TRUE, $author->member_id ? NULL : $author->real_name, NULL, $author, $date );
if ( isset( $class::$databaseColumnMap['first_comment_id'] ) )
{
$firstCommentColumn = $class::$databaseColumnMap['first_comment_id'];
@@ -362,6 +426,10 @@ class _ItemController extends \IPS\Api\Controller
/* Init */
$itemClass = $this->class;
$item = $itemClass::load( $id );
if ( $this->member and !$item->can( 'read', $this->member ) )
{
throw new \OutOfRangeException;
}
$itemIdColumn = $itemClass::$databaseColumnId;
$where [] = array( $commentClass::$databasePrefix . $commentClass::$databaseColumnMap['item'] . '=?', $item->$itemIdColumn );
@@ -406,7 +474,9 @@ class _ItemController extends \IPS\Api\Controller
\IPS\Db::i()->select( '*', $commentClass::$databaseTable, $where, "{$sortBy} {$sortDir}" ),
isset( \IPS\Request::i()->page ) ? \IPS\Request::i()->page : 1,
$commentClass,
\IPS\Db::i()->select( 'COUNT(*)', $commentClass::$databaseTable, $where )->first()
\IPS\Db::i()->select( 'COUNT(*)', $commentClass::$databaseTable, $where )->first(),
$this->member,
isset( \IPS\Request::i()->perPage ) ? \IPS\Request::i()->perPage : NULL
);
}
}