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,22 +12,37 @@
|
||||
namespace IPS\blog\api;
|
||||
|
||||
/* To prevent PHP errors (extending class does not exist) revealing path */
|
||||
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
|
||||
use IPS\Api\Exception;
|
||||
use IPS\Api\PaginatedResponse;
|
||||
use IPS\Api\Response;
|
||||
use IPS\blog\Category;
|
||||
use IPS\Db;
|
||||
use IPS\Http\Url\Friendly;
|
||||
use IPS\Lang;
|
||||
use IPS\Node\Api\NodeController;
|
||||
use IPS\Node\Model;
|
||||
use IPS\Request;
|
||||
use OutOfRangeException;
|
||||
use function defined;
|
||||
use function in_array;
|
||||
|
||||
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 Blog Blogs API
|
||||
*/
|
||||
class _categories extends \IPS\Node\Api\NodeController
|
||||
class categories extends NodeController
|
||||
{
|
||||
|
||||
/**
|
||||
* Class
|
||||
*/
|
||||
protected $class = 'IPS\blog\Category';
|
||||
protected string $class = 'IPS\blog\Category';
|
||||
|
||||
/**
|
||||
* GET /blog/categories
|
||||
@@ -38,33 +53,34 @@ class _categories extends \IPS\Node\Api\NodeController
|
||||
* @apiparam string sortDir Sort direction. Can be 'asc' or 'desc' - defaults to 'asc'
|
||||
* @apiparam int page Page number
|
||||
* @apiparam int perPage Number of results per page - defaults to 25
|
||||
* @return \IPS\Api\PaginatedResponse<IPS\blog\Entry>
|
||||
* @apireturn PaginatedResponse<IPS\blog\Category>
|
||||
* @return PaginatedResponse<Category>
|
||||
*/
|
||||
public function GETindex()
|
||||
public function GETindex(): PaginatedResponse
|
||||
{
|
||||
/* Where clause */
|
||||
$where = $this->_globalWhere();
|
||||
|
||||
/* Sort */
|
||||
if ( isset( \IPS\Request::i()->sortBy ) and \in_array( \IPS\Request::i()->sortBy, array( 'position' ) ) )
|
||||
if ( isset( Request::i()->sortBy ) and Request::i()->sortBy == 'position' )
|
||||
{
|
||||
$sortBy = 'category_' . \IPS\Request::i()->sortBy;
|
||||
$sortBy = 'category_' . Request::i()->sortBy;
|
||||
}
|
||||
else
|
||||
{
|
||||
$sortBy = 'category_id';
|
||||
}
|
||||
$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 */
|
||||
return new \IPS\Api\PaginatedResponse(
|
||||
return new PaginatedResponse(
|
||||
200,
|
||||
\IPS\Db::i()->select( '*', 'blog_categories', $where, "{$sortBy} {$sortDir}" ),
|
||||
isset( \IPS\Request::i()->page ) ? \IPS\Request::i()->page : 1,
|
||||
Db::i()->select( '*', 'blog_categories', $where, "{$sortBy} {$sortDir}" ),
|
||||
isset( Request::i()->page ) ? Request::i()->page : 1,
|
||||
'IPS\blog\Category',
|
||||
\IPS\Db::i()->select( 'COUNT(*)', 'blog_categories', $where )->first(),
|
||||
Db::i()->select( 'COUNT(*)', 'blog_categories', $where )->first(),
|
||||
$this->member,
|
||||
isset( \IPS\Request::i()->perPage ) ? \IPS\Request::i()->perPage : NULL
|
||||
isset( Request::i()->perPage ) ? Request::i()->perPage : NULL
|
||||
);
|
||||
}
|
||||
|
||||
@@ -73,9 +89,10 @@ class _categories extends \IPS\Node\Api\NodeController
|
||||
* Get information about a specific blog category
|
||||
*
|
||||
* @param int $id ID Number
|
||||
* @return \IPS\blog\Blog
|
||||
* @apireturn \IPS\blog\Category
|
||||
* @return Response
|
||||
*/
|
||||
public function GETitem( $id )
|
||||
public function GETitem( int $id ): Response
|
||||
{
|
||||
return $this->_view( $id );
|
||||
}
|
||||
@@ -88,17 +105,18 @@ class _categories extends \IPS\Node\Api\NodeController
|
||||
* @reqapiparam string name The category name
|
||||
* @apiparam int|null parent The ID number of the parent the category should be created in. NULL for root.
|
||||
* @apiparam int position The category position
|
||||
* @return \IPS\blog\Category
|
||||
* @apireturn \IPS\blog\Category
|
||||
* @throws 1B408/1 NO_TITLE A name for the category must be supplied
|
||||
* @return Response
|
||||
*/
|
||||
public function POSTindex()
|
||||
public function POSTindex(): Response
|
||||
{
|
||||
if ( !\IPS\Request::i()->name )
|
||||
if ( !Request::i()->name )
|
||||
{
|
||||
throw new \IPS\Api\Exception( 'NO_TITLE', '1B408/1', 400 );
|
||||
throw new Exception( 'NO_TITLE', '1B408/1', 400 );
|
||||
}
|
||||
|
||||
return new \IPS\Api\Response( 201, $this->_create()->apiOutput( $this->member ) );
|
||||
return new Response( 201, $this->_create()->apiOutput( $this->member ) );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -110,14 +128,16 @@ class _categories extends \IPS\Node\Api\NodeController
|
||||
* @apiparam int|null parent The ID number of the parent the category should be created in. NULL for root.
|
||||
* @apiparam int position The category position
|
||||
* @param int $id ID Number
|
||||
* @return \IPS\blog\Category
|
||||
* @apireturn \IPS\blog\Category
|
||||
* @return Response
|
||||
*/
|
||||
public function POSTitem( $id )
|
||||
public function POSTitem( int $id ): Response
|
||||
{
|
||||
/* @var Category $class */
|
||||
$class = $this->class;
|
||||
$category = $class::load( $id );
|
||||
|
||||
return new \IPS\Api\Response( 200, $this->_createOrUpdate( $category )->apiOutput( $this->member ) );
|
||||
return new Response( 200, $this->_createOrUpdate( $category )->apiOutput( $this->member ) );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -126,11 +146,13 @@ class _categories extends \IPS\Node\Api\NodeController
|
||||
*
|
||||
* @apiclientonly
|
||||
* @param int $id ID Number
|
||||
* @return void
|
||||
* @apireturn void
|
||||
* @return Response
|
||||
* @throws 2B408/3 INVALID_ID The blog category ID does not exist or the authorized user does not have permission to delete it
|
||||
*/
|
||||
public function DELETEitem( $id )
|
||||
public function DELETEitem( int $id ): Response
|
||||
{
|
||||
/* @var Category $class */
|
||||
$class = $this->class;
|
||||
|
||||
try
|
||||
@@ -138,31 +160,31 @@ class _categories extends \IPS\Node\Api\NodeController
|
||||
$category = $class::load( $id );
|
||||
$category->delete();
|
||||
|
||||
return new \IPS\Api\Response( 200, NULL );
|
||||
return new Response( 200, NULL );
|
||||
}
|
||||
catch ( \OutOfRangeException $e )
|
||||
catch ( OutOfRangeException $e )
|
||||
{
|
||||
throw new \IPS\Api\Exception( 'INVALID_ID', '2B408/3', 404 );
|
||||
throw new Exception( 'INVALID_ID', '2B408/3', 404 );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create or update node
|
||||
*
|
||||
* @param \IPS\Node\Model $category The node
|
||||
* @return \IPS\Node\Model
|
||||
* @param Model $node The node
|
||||
* @return Model
|
||||
*/
|
||||
protected function _createOrUpdate( \IPS\Node\Model $category )
|
||||
protected function _createOrUpdate( Model $node ): Model
|
||||
{
|
||||
if ( isset( \IPS\Request::i()->name ) )
|
||||
if ( isset( Request::i()->name ) )
|
||||
{
|
||||
\IPS\Lang::saveCustom( 'blog', "blog_category_{$category->id}", \IPS\Request::i()->name );
|
||||
Lang::saveCustom( 'blog', "blog_category_{$node->id}", Request::i()->name );
|
||||
|
||||
$category->seo_name = \IPS\Http\Url\Friendly::seoTitle( \IPS\Request::i()->name );
|
||||
$node->seo_name = Friendly::seoTitle( Request::i()->name );
|
||||
}
|
||||
|
||||
$category->parent = (int) \IPS\Request::i()->parent?: \IPS\blog\Category::$databaseColumnParentRootValue;
|
||||
$node->parent = (int) Request::i()->parent?: Category::$databaseColumnParentRootValue;
|
||||
|
||||
return parent::_createOrUpdate( $category );
|
||||
return parent::_createOrUpdate( $node );
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user