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
@@ -12,18 +12,44 @@ namespace IPS\Content\Api;
|
||||
|
||||
/* To prevent PHP errors (extending class does not exist) revealing path */
|
||||
|
||||
use BadMethodCallException;
|
||||
use DateInterval;
|
||||
use IPS\Api\Controller;
|
||||
use IPS\Api\Exception;
|
||||
use IPS\Api\PaginatedResponse;
|
||||
use IPS\Api\Response;
|
||||
use IPS\Api\Webhook;
|
||||
use IPS\Content\Comment;
|
||||
use IPS\Content\Filter;
|
||||
use IPS\Content\Item;
|
||||
use IPS\Content\Search\Index;
|
||||
use IPS\Content\Search\SearchContent;
|
||||
use IPS\DateTime;
|
||||
use IPS\Db;
|
||||
use IPS\IPS;
|
||||
use IPS\Member;
|
||||
use IPS\Node\Model;
|
||||
use IPS\Poll;
|
||||
use IPS\Request;
|
||||
use IPS\Settings;
|
||||
use IPS\Text\Parser;
|
||||
use OutOfRangeException;
|
||||
use function defined;
|
||||
use function get_class;
|
||||
use function in_array;
|
||||
use function intval;
|
||||
use function substr;
|
||||
|
||||
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' );
|
||||
header( ( $_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.0' ) . ' 403 Forbidden' );
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Base API endpoint for Content Items
|
||||
*/
|
||||
class _ItemController extends \IPS\Api\Controller
|
||||
class ItemController extends Controller
|
||||
{
|
||||
/**
|
||||
* List
|
||||
@@ -31,36 +57,37 @@ class _ItemController extends \IPS\Api\Controller
|
||||
* @param array $where Extra WHERE clause
|
||||
* @param string $containerParam The parameter which includes the container values
|
||||
* @param bool $byPassPerms If permissions should be ignored
|
||||
* @param string $customSort Custom sort by parameter to use
|
||||
* @return \IPS\Api\PaginatedResponse
|
||||
* @param string|null $customSort Custom sort by parameter to use
|
||||
* @return PaginatedResponse
|
||||
*/
|
||||
protected function _list( $where = array(), $containerParam = 'categories', $byPassPerms = FALSE, $customSort = NULL )
|
||||
protected function _list( array $where = array(), string $containerParam = 'categories', bool $byPassPerms = FALSE, string|null $customSort = NULL ): PaginatedResponse
|
||||
{
|
||||
/* @var array $databaseColumnMap */
|
||||
$class = $this->class;
|
||||
|
||||
/* Containers */
|
||||
if ( $containerParam and isset( \IPS\Request::i()->$containerParam ) )
|
||||
if ( $containerParam and isset( Request::i()->$containerParam ) )
|
||||
{
|
||||
$where[] = array( \IPS\Db::i()->in( $class::$databaseTable . '.' . $class::$databasePrefix . $class::$databaseColumnMap['container'], array_map( 'intval', array_filter( explode( ',', \IPS\Request::i()->$containerParam ) ) ) ) );
|
||||
$where[] = array( Db::i()->in( $class::$databaseTable . '.' . $class::$databasePrefix . $class::$databaseColumnMap['container'], array_map( 'intval', array_filter( explode( ',', Request::i()->$containerParam ) ) ) ) );
|
||||
}
|
||||
|
||||
/* IDs */
|
||||
if ( isset( \IPS\Request::i()->ids ) )
|
||||
if ( isset( Request::i()->ids ) )
|
||||
{
|
||||
$idField = $class::$databaseTable . '.' . $class::$databasePrefix . $class::$databaseColumnId;
|
||||
$where[] = array(\IPS\Db::i()->in( $idField, array_map( 'intval', explode(',', \IPS\Request::i()->ids ) ) ) );
|
||||
$where[] = array(Db::i()->in( $idField, array_map( 'intval', explode(',', Request::i()->ids ) ) ) );
|
||||
}
|
||||
|
||||
/* Authors */
|
||||
if ( isset( \IPS\Request::i()->authors ) )
|
||||
if ( isset( Request::i()->authors ) )
|
||||
{
|
||||
$where[] = array( \IPS\Db::i()->in( $class::$databasePrefix . $class::$databaseColumnMap['author'], array_map( 'intval', array_filter( explode( ',', \IPS\Request::i()->authors ) ) ) ) );
|
||||
$where[] = array( Db::i()->in( $class::$databasePrefix . $class::$databaseColumnMap['author'], array_map( 'intval', array_filter( explode( ',', Request::i()->authors ) ) ) ) );
|
||||
}
|
||||
|
||||
/* Pinned? */
|
||||
if ( isset( \IPS\Request::i()->pinned ) AND \in_array( 'IPS\Content\Pinnable', class_implements( $class ) ) )
|
||||
if ( isset( Request::i()->pinned ) AND IPS::classUsesTrait( $class, 'IPS\Content\Pinnable' ) )
|
||||
{
|
||||
if ( \IPS\Request::i()->pinned )
|
||||
if ( Request::i()->pinned )
|
||||
{
|
||||
$where[] = array( $class::$databasePrefix . $class::$databaseColumnMap['pinned'] . "=1" );
|
||||
}
|
||||
@@ -71,9 +98,9 @@ class _ItemController extends \IPS\Api\Controller
|
||||
}
|
||||
|
||||
/* Featured? */
|
||||
if ( isset( \IPS\Request::i()->featured ) AND \in_array( 'IPS\Content\Featurable', class_implements( $class ) ) )
|
||||
if ( isset( Request::i()->featured ) AND IPS::classUsesTrait( $class, 'IPS\Content\Featurable' ) )
|
||||
{
|
||||
if ( \IPS\Request::i()->featured )
|
||||
if ( Request::i()->featured )
|
||||
{
|
||||
$where[] = array( $class::$databasePrefix . $class::$databaseColumnMap['featured'] . "=1" );
|
||||
}
|
||||
@@ -84,22 +111,22 @@ class _ItemController extends \IPS\Api\Controller
|
||||
}
|
||||
|
||||
/* Locked? */
|
||||
if ( isset( \IPS\Request::i()->locked ) AND \in_array( 'IPS\Content\Lockable', class_implements( $class ) ) )
|
||||
if ( isset( Request::i()->locked ) AND IPS::classUsesTrait( $class, 'IPS\Content\Lockable' ) )
|
||||
{
|
||||
if ( isset( $class::$databaseColumnMap['locked'] ) )
|
||||
{
|
||||
$where[] = array( $class::$databasePrefix . $class::$databaseColumnMap['locked'] . '=?', \intval( \IPS\Request::i()->locked ) );
|
||||
$where[] = array( $class::$databasePrefix . $class::$databaseColumnMap['locked'] . '=?', intval( Request::i()->locked ) );
|
||||
}
|
||||
else
|
||||
{
|
||||
$where[] = array( $class::$databasePrefix . $class::$databaseColumnMap['state'] . '=?', \IPS\Request::i()->locked ? 'closed' : 'open' );
|
||||
$where[] = array( $class::$databasePrefix . $class::$databaseColumnMap['state'] . '=?', Request::i()->locked ? 'closed' : 'open' );
|
||||
}
|
||||
}
|
||||
|
||||
/* Hidden */
|
||||
if ( isset( \IPS\Request::i()->hidden ) AND \in_array( 'IPS\Content\Hideable', class_implements( $class ) ) )
|
||||
if ( isset( Request::i()->hidden ) AND IPS::classUSesTrait( $class, 'IPS\Content\Hideable' ) )
|
||||
{
|
||||
if ( \IPS\Request::i()->hidden )
|
||||
if ( Request::i()->hidden )
|
||||
{
|
||||
if ( isset( $class::$databaseColumnMap['hidden'] ) )
|
||||
{
|
||||
@@ -124,9 +151,9 @@ class _ItemController extends \IPS\Api\Controller
|
||||
}
|
||||
|
||||
/* Has poll? */
|
||||
if ( isset( \IPS\Request::i()->hasPoll ) AND \in_array( 'IPS\Content\Polls', class_implements( $class ) ) )
|
||||
if ( isset( Request::i()->hasPoll ) AND IPS::classUsesTrait( $class, 'IPS\Content\Polls' ) )
|
||||
{
|
||||
if ( \IPS\Request::i()->hasPoll )
|
||||
if ( Request::i()->hasPoll )
|
||||
{
|
||||
$where[] = array( $class::$databasePrefix . $class::$databaseColumnMap['poll'] . ">0" );
|
||||
}
|
||||
@@ -147,21 +174,21 @@ class _ItemController extends \IPS\Api\Controller
|
||||
{
|
||||
$sortBy = $customSort;
|
||||
}
|
||||
elseif ( isset( \IPS\Request::i()->sortBy ) and \in_array( \IPS\Request::i()->sortBy, $supportedSortBy ) )
|
||||
elseif ( isset( Request::i()->sortBy ) and in_array( Request::i()->sortBy, $supportedSortBy ) )
|
||||
{
|
||||
$sortBy = $class::$databasePrefix . $class::$databaseColumnMap[ \IPS\Request::i()->sortBy ];
|
||||
$sortBy = $class::$databasePrefix . $class::$databaseColumnMap[ Request::i()->sortBy ];
|
||||
}
|
||||
else
|
||||
{
|
||||
$sortBy = $class::$databasePrefix . $class::$databaseColumnId;
|
||||
}
|
||||
$sortDir = ( isset( \IPS\Request::i()->sortDir ) and \in_array( mb_strtolower( \IPS\Request::i()->sortDir ), array( 'asc', 'desc' ) ) ) ? \IPS\Request::i()->sortDir : 'asc';
|
||||
$sortDir = ( isset( Request::i()->sortDir ) and in_array( mb_strtolower( Request::i()->sortDir ), array( 'asc', 'desc' ) ) ) ? 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 );
|
||||
$query = $class::getItemsWithPermission( $where, "{$sortBy} {$sortDir}", NULL, 'view', Filter::FILTER_AUTOMATIC, 0, $this->member )->getInnerIterator();
|
||||
$count = $class::getItemsWithPermission( $where, "{$sortBy} {$sortDir}", NULL, 'view', Filter::FILTER_AUTOMATIC, 0, $this->member, FALSE, FALSE, FALSE, TRUE );
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -177,19 +204,19 @@ class _ItemController extends \IPS\Api\Controller
|
||||
$where[] = array( "{$col}!=-2 AND {$col}!=-3" );
|
||||
}
|
||||
|
||||
$query = \IPS\Db::i()->select( '*', $class::$databaseTable, $where, "{$sortBy} {$sortDir}" );
|
||||
$count = \IPS\Db::i()->select( 'COUNT(*)', $class::$databaseTable, $where )->first();
|
||||
$query = Db::i()->select( '*', $class::$databaseTable, $where, "{$sortBy} {$sortDir}" );
|
||||
$count = Db::i()->select( 'COUNT(*)', $class::$databaseTable, $where )->first();
|
||||
}
|
||||
|
||||
/* Return */
|
||||
return new \IPS\Api\PaginatedResponse(
|
||||
return new PaginatedResponse(
|
||||
200,
|
||||
$query,
|
||||
isset( \IPS\Request::i()->page ) ? \IPS\Request::i()->page : 1,
|
||||
isset( Request::i()->page ) ? Request::i()->page : 1,
|
||||
$class,
|
||||
$count,
|
||||
$this->member,
|
||||
isset( \IPS\Request::i()->perPage ) ? \IPS\Request::i()->perPage : NULL
|
||||
isset( Request::i()->perPage ) ? Request::i()->perPage : NULL
|
||||
);
|
||||
}
|
||||
|
||||
@@ -197,58 +224,52 @@ class _ItemController extends \IPS\Api\Controller
|
||||
* View
|
||||
*
|
||||
* @param int $id ID Number
|
||||
* @return \IPS\Api\Response
|
||||
* @return Response
|
||||
*/
|
||||
protected function _view( $id )
|
||||
protected function _view( int $id ): Response
|
||||
{
|
||||
$class = $this->class;
|
||||
|
||||
$item = $class::load( $id );
|
||||
if( $this->member )
|
||||
if ( $this->member and !$item->can( 'read', $this->member ) )
|
||||
{
|
||||
if ( method_exists($item, 'canView') and !$item->canView( $this->member ) )
|
||||
{
|
||||
throw new OutOfRangeException;
|
||||
}
|
||||
else if ( !$item->can( 'read', $this->member ) )
|
||||
{
|
||||
throw new OutOfRangeException;
|
||||
}
|
||||
throw new OutOfRangeException;
|
||||
}
|
||||
|
||||
return new \IPS\Api\Response( 200, $item->apiOutput( $this->member ) );
|
||||
return new Response( 200, $item->apiOutput( $this->member ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Create or update item
|
||||
*
|
||||
* @param \IPS\Content\Item $item The item
|
||||
* @param Item $item The item
|
||||
* @param string $type add or edit
|
||||
* @return \IPS\Content\Item
|
||||
* @return Item
|
||||
*/
|
||||
protected function _createOrUpdate( \IPS\Content\Item $item, $type='add' )
|
||||
protected function _createOrUpdate( Item $item, string $type='add' ): Item
|
||||
{
|
||||
/* @var array $databaseColumnMap */
|
||||
/* Title */
|
||||
if ( isset( \IPS\Request::i()->title ) and isset( $item::$databaseColumnMap['title'] ) )
|
||||
if ( isset( Request::i()->title ) and isset( $item::$databaseColumnMap['title'] ) )
|
||||
{
|
||||
$titleColumn = $item::$databaseColumnMap['title'];
|
||||
$item->$titleColumn = \IPS\Request::i()->title;
|
||||
$item->$titleColumn = Request::i()->title;
|
||||
}
|
||||
|
||||
/* Tags */
|
||||
if ( ( isset( \IPS\Request::i()->prefix ) or isset( \IPS\Request::i()->tags ) ) and \in_array( 'IPS\Content\Tags', class_implements( \get_class( $item ) ) ) )
|
||||
if ( ( isset( Request::i()->prefix ) or isset( Request::i()->tags ) ) and IPS::classUsesTrait( $item, 'IPS\Content\Taggable' ) )
|
||||
{
|
||||
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();
|
||||
$tags = isset( Request::i()->tags ) ? array_filter( explode( ',', Request::i()->tags ) ) : $item->tags();
|
||||
|
||||
if ( !$this->member or $item::canPrefix( $this->member, $item->containerWrapper() ) )
|
||||
{
|
||||
if ( isset( \IPS\Request::i()->prefix ) )
|
||||
if ( isset( Request::i()->prefix ) )
|
||||
{
|
||||
if ( \IPS\Request::i()->prefix )
|
||||
if ( Request::i()->prefix )
|
||||
{
|
||||
$tags['prefix'] = \IPS\Request::i()->prefix;
|
||||
$tags['prefix'] = Request::i()->prefix;
|
||||
}
|
||||
}
|
||||
elseif ( $existingPrefix = $item->prefix() )
|
||||
@@ -269,30 +290,30 @@ class _ItemController extends \IPS\Api\Controller
|
||||
}
|
||||
|
||||
/* Open/closed */
|
||||
if ( isset( \IPS\Request::i()->locked ) and \in_array( 'IPS\Content\Lockable', class_implements( \get_class( $item ) ) ) )
|
||||
if ( isset( Request::i()->locked ) and IPS::classUsesTrait( $item, 'IPS\Content\Lockable' ) )
|
||||
{
|
||||
if ( !$this->member or ( \IPS\Request::i()->locked and $item->canLock( $this->member ) ) or ( !\IPS\Request::i()->locked and $item->canUnlock( $this->member ) ) )
|
||||
if ( !$this->member or ( Request::i()->locked and $item->canLock( $this->member ) ) or ( !Request::i()->locked and $item->canUnlock( $this->member ) ) )
|
||||
{
|
||||
if ( isset( $item::$databaseColumnMap['locked'] ) )
|
||||
{
|
||||
$lockedColumn = $item::$databaseColumnMap['locked'];
|
||||
$item->$lockedColumn = \intval( \IPS\Request::i()->locked );
|
||||
$item->$lockedColumn = intval( Request::i()->locked );
|
||||
}
|
||||
else
|
||||
{
|
||||
$stateColumn = $item::$databaseColumnMap['status'];
|
||||
$item->$stateColumn = \IPS\Request::i()->locked ? 'closed' : 'open';
|
||||
$item->$stateColumn = Request::i()->locked ? 'closed' : 'open';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Hidden */
|
||||
if ( isset( \IPS\Request::i()->hidden ) and \in_array( 'IPS\Content\Hideable', class_implements( \get_class( $item ) ) ) )
|
||||
if ( isset( Request::i()->hidden ) and IPS::classUsesTrait( $item, 'IPS\Content\Hideable' ) )
|
||||
{
|
||||
if ( !$this->member or ( \IPS\Request::i()->hidden and $item->canHide( $this->member ) ) or ( !\IPS\Request::i()->hidden and $item->canUnhide( $this->member ) ) )
|
||||
if ( !$this->member or ( Request::i()->hidden and $item->canHide( $this->member ) ) or ( !Request::i()->hidden and $item->canUnhide( $this->member ) ) )
|
||||
{
|
||||
$idColumn = $item::$databaseColumnId;
|
||||
if ( \IPS\Request::i()->hidden AND $item->hidden() != -1 )
|
||||
if ( Request::i()->hidden AND $item->hidden() != -1 )
|
||||
{
|
||||
if ( $item->$idColumn )
|
||||
{
|
||||
@@ -303,17 +324,17 @@ class _ItemController extends \IPS\Api\Controller
|
||||
if ( isset( $item::$databaseColumnMap['hidden'] ) )
|
||||
{
|
||||
$hiddenColumn = $item::$databaseColumnMap['hidden'];
|
||||
$item->$hiddenColumn = \IPS\Request::i()->hidden;
|
||||
$item->$hiddenColumn = Request::i()->hidden;
|
||||
}
|
||||
else
|
||||
{
|
||||
$approvedColumn = $item::$databaseColumnMap['approved'];
|
||||
$item->$approvedColumn = ( \IPS\Request::i()->hidden == -1 ) ? -1 : 0;
|
||||
$item->$approvedColumn = ( Request::i()->hidden == -1 ) ? -1 : 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ( !\IPS\Request::i()->hidden AND $item->hidden() == -1 )
|
||||
if ( !Request::i()->hidden AND $item->hidden() == -1 )
|
||||
{
|
||||
if ( $item->$idColumn )
|
||||
{
|
||||
@@ -337,27 +358,27 @@ class _ItemController extends \IPS\Api\Controller
|
||||
}
|
||||
|
||||
/* Pinned */
|
||||
if ( isset( \IPS\Request::i()->pinned ) and \in_array( 'IPS\Content\Pinnable', class_implements( \get_class( $item ) ) ) )
|
||||
if ( isset( Request::i()->pinned ) and IPS::classUsesTrait( $item, 'IPS\Content\Pinnable' ) )
|
||||
{
|
||||
if ( !$this->member or ( \IPS\Request::i()->pinned and $item->canPin( $this->member ) ) or ( !\IPS\Request::i()->pinned and $item->canUnpin( $this->member ) ) )
|
||||
if ( !$this->member or ( Request::i()->pinned and $item->canPin( $this->member ) ) or ( !Request::i()->pinned and $item->canUnpin( $this->member ) ) )
|
||||
{
|
||||
$pinnedColumn = $item::$databaseColumnMap['pinned'];
|
||||
$item->$pinnedColumn = \intval( \IPS\Request::i()->pinned );
|
||||
$item->$pinnedColumn = intval( Request::i()->pinned );
|
||||
}
|
||||
}
|
||||
|
||||
/* Featured */
|
||||
if ( isset( \IPS\Request::i()->featured ) and \in_array( 'IPS\Content\Featurable', class_implements( \get_class( $item ) ) ) )
|
||||
if ( isset( Request::i()->featured ) and IPS::classUsesTrait( $item, 'IPS\Content\Featurable' ) )
|
||||
{
|
||||
if ( !$this->member or ( \IPS\Request::i()->featured and $item->canFeature( $this->member ) ) or ( !\IPS\Request::i()->featured and $item->canUnfeature( $this->member ) ) )
|
||||
if ( !$this->member or $item->canFeature( $this->member ) )
|
||||
{
|
||||
$featuredColumn = $item::$databaseColumnMap['featured'];
|
||||
$item->$featuredColumn = \intval( \IPS\Request::i()->featured );
|
||||
$item->$featuredColumn = intval( Request::i()->featured );
|
||||
}
|
||||
}
|
||||
|
||||
/** We intentionally allow anonymous content in nodes where it's possible (but where it's disabled via the settings */
|
||||
if( isset( \IPS\Request::i()->anonymous ) AND \in_array( 'IPS\Content\Anonymous', class_implements( \get_class( $item ) ) ) )
|
||||
if( isset( Request::i()->anonymous ) AND IPS::classUsesTrait( $item, 'IPS\Content\Anonymous' ) )
|
||||
{
|
||||
/* we need to save the item before we set the anonymous data */
|
||||
$idColumn = $item::$databaseColumnId;
|
||||
@@ -367,40 +388,41 @@ class _ItemController extends \IPS\Api\Controller
|
||||
}
|
||||
try
|
||||
{
|
||||
$item->setAnonymous( (bool) \IPS\Request::i()->anonymous, $item->author() );
|
||||
$item->setAnonymous( (bool) Request::i()->anonymous, $item->author() );
|
||||
}
|
||||
catch ( \BadMethodCallException $e ){}
|
||||
catch ( BadMethodCallException $e ){}
|
||||
|
||||
}
|
||||
|
||||
/* 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;
|
||||
$field = $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 ) AND $type == 'edit' )
|
||||
if ( $item::$firstCommentRequired AND isset( $item->$field ) AND isset( Request::i()->$contentField ) AND $type == 'edit' )
|
||||
{
|
||||
$content = \IPS\Request::i()->$contentField;
|
||||
$content = Request::i()->$contentField;
|
||||
if ( $this->member )
|
||||
{
|
||||
$content = \IPS\Text\Parser::parseStatic( $content, TRUE, NULL, $this->member, $item::$application . '_' . mb_ucfirst( $item::$module ) );
|
||||
$content = Parser::parseStatic( $content, NULL, $this->member, $item::$application . '_' . IPS::mb_ucfirst( $item::$module ) );
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
/* @var Comment $commentClass */
|
||||
$comment = $commentClass::load( $item->$field );
|
||||
}
|
||||
catch ( \OutOfRangeException $e )
|
||||
catch ( OutOfRangeException $e )
|
||||
{
|
||||
throw new \IPS\Api\Exception( 'NO_FIRST_POST', '1S377/1', 400 );
|
||||
throw new 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 )
|
||||
if( SearchContent::isSearchable( $item ) )
|
||||
{
|
||||
\IPS\Content\Search\Index::i()->index( $comment );
|
||||
Index::i()->index( $comment );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -408,40 +430,40 @@ class _ItemController extends \IPS\Api\Controller
|
||||
return $item;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Create
|
||||
*
|
||||
* @param \IPS\Node\Model $container Container
|
||||
* @param \IPS\Member $author Author
|
||||
* @param string $firstPostParam The parameter which contains the body for the first comment
|
||||
* @return \IPS\Content\Item
|
||||
* @param Model|null $container Container
|
||||
* @param Member $author Author
|
||||
* @param string $firstPostParam The parameter which contains the body for the first comment
|
||||
* @return Item
|
||||
*/
|
||||
protected function _create( ?\IPS\Node\Model $container, \IPS\Member $author, $firstPostParam = 'post' )
|
||||
protected function _create( Model|null $container, Member $author, string $firstPostParam = 'post' ): Item
|
||||
{
|
||||
$class = $this->class;
|
||||
|
||||
/* Work out the date */
|
||||
$date = ( !$this->member and \IPS\Request::i()->date ) ? new \IPS\DateTime( \IPS\Request::i()->date ) : \IPS\DateTime::create();
|
||||
$date = ( !$this->member and Request::i()->date ) ? new DateTime( Request::i()->date ) : DateTime::create();
|
||||
|
||||
/* Create item */
|
||||
$item = $class::createItem( $author, ( !$this->member and \IPS\Request::i()->ip_address ) ? \IPS\Request::i()->ip_address : \IPS\Request::i()->ipAddress(), $date, $container );
|
||||
$item = $class::createItem( $author, ( !$this->member and Request::i()->ip_address ) ? Request::i()->ip_address : Request::i()->ipAddress(), $date, $container );
|
||||
|
||||
$this->_createOrUpdate( $item, 'add' );
|
||||
$this->_createOrUpdate( $item );
|
||||
$item->save();
|
||||
|
||||
/* Create post */
|
||||
if ( $class::$firstCommentRequired )
|
||||
{
|
||||
$postContents = \IPS\Request::i()->$firstPostParam;
|
||||
$postContents = Request::i()->$firstPostParam;
|
||||
|
||||
if ( $this->member )
|
||||
{
|
||||
$postContents = \IPS\Text\Parser::parseStatic( $postContents, TRUE, NULL, $this->member, $class::$application . '_' . mb_ucfirst( $class::$module ) );
|
||||
$postContents = Parser::parseStatic( $postContents, NULL, $this->member, $class::$application . '_' . IPS::mb_ucfirst( $class::$module ) );
|
||||
}
|
||||
|
||||
$commentClass = $item::$commentClass;
|
||||
$post = $commentClass::create( $item, $postContents, TRUE, $author->member_id ? NULL : $author->real_name, NULL, $author, $date, ( !$this->member and \IPS\Request::i()->ip_address ) ? \IPS\Request::i()->ip_address : \IPS\Request::i()->ipAddress(), NULL, ( isset( \IPS\Request::i()->anonymous ) ? (bool) \IPS\Request::i()->anonymous : NULL ) );
|
||||
$post = $commentClass::create( $item, $postContents, TRUE, $author->member_id ? NULL : $author->real_name, NULL, $author, $date, ( !$this->member and Request::i()->ip_address ) ? Request::i()->ip_address : Request::i()->ipAddress(), NULL, ( isset( Request::i()->anonymous ) ? (bool) Request::i()->anonymous : NULL ) );
|
||||
|
||||
if ( isset( $class::$databaseColumnMap['first_comment_id'] ) )
|
||||
{
|
||||
@@ -453,15 +475,15 @@ class _ItemController extends \IPS\Api\Controller
|
||||
}
|
||||
|
||||
/* Index */
|
||||
if ( $item instanceof \IPS\Content\Searchable )
|
||||
if( SearchContent::isSearchable( $item ) )
|
||||
{
|
||||
\IPS\Content\Search\Index::i()->index( $item );
|
||||
Index::i()->index( $item );
|
||||
}
|
||||
|
||||
/* Send webhooks */
|
||||
if ( \in_array( $item->hidden(), array( -1, 0, 1 ) ) ) // i.e. not post before register or pending deletion
|
||||
if ( in_array( $item->hidden(), array( -1, 0, 1 ) ) ) // i.e. not post before register or pending deletion
|
||||
{
|
||||
\IPS\Api\Webhook::fire( str_replace( '\\', '', \substr( \get_class( $item ), 3 ) ) . '_create', $item, $item->webhookFilters() );
|
||||
Webhook::fire( str_replace( '\\', '', substr( get_class( $item ), 3 ) ) . '_create', $item, $item->webhookFilters() );
|
||||
}
|
||||
|
||||
/* Send notifications and dish out points */
|
||||
@@ -470,7 +492,7 @@ class _ItemController extends \IPS\Api\Controller
|
||||
$item->sendNotifications();
|
||||
$author->achievementAction( 'core', 'NewContentItem', $item );
|
||||
}
|
||||
elseif( !\in_array( $item->hidden(), array( -1, -3 ) ) )
|
||||
elseif( !in_array( $item->hidden(), array( -1, -3 ) ) )
|
||||
{
|
||||
$item->sendUnapprovedNotification();
|
||||
}
|
||||
@@ -482,43 +504,43 @@ class _ItemController extends \IPS\Api\Controller
|
||||
/**
|
||||
* Create or update poll
|
||||
*
|
||||
* @param \IPS\Content\Item $item The content item to attach the poll to
|
||||
* @param Item $item The content item to attach the poll to
|
||||
* @param string $type Whether we are adding or editing
|
||||
* @return void
|
||||
*/
|
||||
protected function _createOrUpdatePoll( $item, $type )
|
||||
protected function _createOrUpdatePoll( Item $item, string $type ): void
|
||||
{
|
||||
/* Are we creating or updating a poll? */
|
||||
if( isset( \IPS\Request::i()->poll_title ) AND isset( \IPS\Request::i()->poll_options ) AND \IPS\Request::i()->poll_title AND \IPS\Request::i()->poll_options AND ( !$this->member OR $item::canCreatePoll( $this->member, $item->container() ) ) )
|
||||
if( isset( Request::i()->poll_title ) AND isset( Request::i()->poll_options ) AND Request::i()->poll_title AND Request::i()->poll_options AND ( !$this->member OR $item::canCreatePoll( $this->member, $item->container() ) ) )
|
||||
{
|
||||
$canCreatePoll = TRUE;
|
||||
|
||||
if( $type == 'edit' AND !$item->getPoll() )
|
||||
{
|
||||
$canCreatePoll = (bool) ( \IPS\Settings::i()->startpoll_cutoff == -1 or \IPS\DateTime::create()->sub( new \DateInterval( 'PT' . \IPS\Settings::i()->startpoll_cutoff . 'H' ) )->getTimestamp() < $item->mapped('date') );
|
||||
$canCreatePoll = ( Settings::i()->startpoll_cutoff == -1 or DateTime::create()->sub( new DateInterval( 'PT' . Settings::i()->startpoll_cutoff . 'H' ) )->getTimestamp() < $item->mapped('date') );
|
||||
}
|
||||
|
||||
if( $canCreatePoll === TRUE )
|
||||
{
|
||||
$poll = $item->getPoll() ?: new \IPS\Poll;
|
||||
$poll = $item->getPoll() ?: new Poll;
|
||||
|
||||
if( !$item->getPoll() )
|
||||
{
|
||||
$poll->starter_id = $item->author()->member_id;
|
||||
$poll->poll_item_class = \get_class( $item );
|
||||
$poll->poll_item_class = get_class( $item );
|
||||
}
|
||||
|
||||
$formatted = array(
|
||||
'title' => \IPS\Request::i()->poll_title,
|
||||
'questions' => \IPS\Request::i()->poll_options
|
||||
'title' => Request::i()->poll_title,
|
||||
'questions' => Request::i()->poll_options
|
||||
);
|
||||
|
||||
if( isset( \IPS\Request::i()->poll_only ) AND \IPS\Request::i()->poll_only )
|
||||
if( isset( Request::i()->poll_only ) AND Request::i()->poll_only )
|
||||
{
|
||||
$formatted['poll_only'] = 1;
|
||||
}
|
||||
|
||||
if( isset( \IPS\Request::i()->poll_public ) AND \IPS\Request::i()->poll_public )
|
||||
if( isset( Request::i()->poll_public ) AND Request::i()->poll_public )
|
||||
{
|
||||
$formatted['public'] = 1;
|
||||
}
|
||||
@@ -539,32 +561,28 @@ class _ItemController extends \IPS\Api\Controller
|
||||
* @param int $id ID Number
|
||||
* @param string $commentClass The class
|
||||
* @param array $where Base where clause
|
||||
* @return \IPS\Api\PaginatedResponse
|
||||
* @return PaginatedResponse
|
||||
*/
|
||||
protected function _comments( $id, $commentClass, $where = array() )
|
||||
protected function _comments( int $id, string $commentClass, array $where = array() ): PaginatedResponse
|
||||
{
|
||||
/* Init */
|
||||
/* @var array $databaseColumnMap
|
||||
* @var Item $itemClass
|
||||
* @var Comment $commentClass */
|
||||
$itemClass = $this->class;
|
||||
$item = $itemClass::load( $id );
|
||||
if( $this->member )
|
||||
if ( $this->member and !$item->can( 'read', $this->member ) )
|
||||
{
|
||||
if ( method_exists($item, 'canView') and !$item->canView( $this->member ) )
|
||||
{
|
||||
throw new OutOfRangeException;
|
||||
}
|
||||
else if ( !$item->can( 'read', $this->member ) )
|
||||
{
|
||||
throw new OutOfRangeException;
|
||||
}
|
||||
throw new OutOfRangeException;
|
||||
}
|
||||
$itemIdColumn = $itemClass::$databaseColumnId;
|
||||
$where [] = array( $commentClass::$databasePrefix . $commentClass::$databaseColumnMap['item'] . '=?', $item->$itemIdColumn );
|
||||
|
||||
/* Hideable? */
|
||||
if ( \in_array( 'IPS\Content\Hideable', class_implements( $commentClass ) ) )
|
||||
if ( IPS::classUsesTrait( $commentClass, 'IPS\Content\Hideable' ) )
|
||||
{
|
||||
/* If request for hidden comments, only show if request is via API key or by a moderator */
|
||||
if ( isset( \IPS\Request::i()->hidden ) AND \IPS\Request::i()->hidden
|
||||
if ( isset( Request::i()->hidden ) AND Request::i()->hidden
|
||||
AND ( $this->member === NULL OR $commentClass::modPermission( 'view_hidden', $this->member ) ) )
|
||||
{
|
||||
if ( isset( $commentClass::$databaseColumnMap['hidden'] ) )
|
||||
@@ -597,16 +615,16 @@ class _ItemController extends \IPS\Api\Controller
|
||||
|
||||
/* Sort */
|
||||
$sortBy = $commentClass::$databasePrefix . $commentClass::$databaseColumnMap['date'];
|
||||
$sortDir = ( isset( \IPS\Request::i()->sortDir ) and \in_array( mb_strtolower( \IPS\Request::i()->sortDir ), array( 'asc', 'desc' ) ) ) ? \IPS\Request::i()->sortDir : 'asc';
|
||||
$sortDir = ( isset( Request::i()->sortDir ) and in_array( mb_strtolower( Request::i()->sortDir ), array( 'asc', 'desc' ) ) ) ? Request::i()->sortDir : 'asc';
|
||||
|
||||
return new \IPS\Api\PaginatedResponse(
|
||||
return new PaginatedResponse(
|
||||
200,
|
||||
\IPS\Db::i()->select( '*', $commentClass::$databaseTable, $where, "{$sortBy} {$sortDir}" ),
|
||||
isset( \IPS\Request::i()->page ) ? \IPS\Request::i()->page : 1,
|
||||
Db::i()->select( '*', $commentClass::$databaseTable, $where, "{$sortBy} {$sortDir}" ),
|
||||
isset( Request::i()->page ) ? Request::i()->page : 1,
|
||||
$commentClass,
|
||||
\IPS\Db::i()->select( 'COUNT(*)', $commentClass::$databaseTable, $where )->first(),
|
||||
Db::i()->select( 'COUNT(*)', $commentClass::$databaseTable, $where )->first(),
|
||||
$this->member,
|
||||
isset( \IPS\Request::i()->perPage ) ? \IPS\Request::i()->perPage : NULL
|
||||
isset( Request::i()->perPage ) ? Request::i()->perPage : NULL
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user