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,16 +12,30 @@
|
||||
namespace IPS\nexus\api;
|
||||
|
||||
/* To prevent PHP errors (extending class does not exist) revealing path */
|
||||
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
|
||||
use IPS\Api\Controller;
|
||||
use IPS\Api\Exception;
|
||||
use IPS\Api\PaginatedResponse;
|
||||
use IPS\Api\Response;
|
||||
use IPS\Application;
|
||||
use IPS\Db;
|
||||
use IPS\nexus\Purchase;
|
||||
use IPS\Request;
|
||||
use OutOfRangeException;
|
||||
use function defined;
|
||||
use function in_array;
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Purchases API
|
||||
*/
|
||||
class _purchases extends \IPS\Api\Controller
|
||||
class purchases extends Controller
|
||||
{
|
||||
/**
|
||||
* GET /nexus/purchases
|
||||
@@ -40,82 +54,83 @@ class _purchases extends \IPS\Api\Controller
|
||||
* @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\nexus\Purchase>
|
||||
* @apireturn PaginatedResponse<IPS\nexus\Purchase>
|
||||
* @return PaginatedResponse<Purchase>
|
||||
*/
|
||||
public function GETindex()
|
||||
public function GETindex(): PaginatedResponse
|
||||
{
|
||||
/* Where clause */
|
||||
$where = array();
|
||||
|
||||
/* Get only the purchases from active applications */
|
||||
$where[] = array( "ps_app IN('" . implode( "','", array_keys( \IPS\Application::enabledApplications() ) ) . "')" );
|
||||
$where[] = array( "ps_app IN('" . implode( "','", array_keys( Application::enabledApplications() ) ) . "')" );
|
||||
|
||||
/* Customers */
|
||||
if ( $this->member )
|
||||
{
|
||||
$where[] = array( 'ps_member=?', $this->member->member_id );
|
||||
}
|
||||
elseif ( isset( \IPS\Request::i()->customers ) )
|
||||
elseif ( isset( Request::i()->customers ) )
|
||||
{
|
||||
$where[] = array( \IPS\Db::i()->in( 'ps_member', array_map( 'intval', array_filter( explode( ',', \IPS\Request::i()->customers ) ) ) ) );
|
||||
$where[] = array( Db::i()->in( 'ps_member', array_map( 'intval', array_filter( explode( ',', Request::i()->customers ) ) ) ) );
|
||||
}
|
||||
|
||||
/* Status */
|
||||
if ( isset( \IPS\Request::i()->active ) )
|
||||
if ( isset( Request::i()->active ) )
|
||||
{
|
||||
$where[] = array( 'ps_active=?', \intval( \IPS\Request::i()->active ) );
|
||||
$where[] = array( 'ps_active=?', intval( Request::i()->active ) );
|
||||
}
|
||||
if ( isset( \IPS\Request::i()->canceled ) )
|
||||
if ( isset( Request::i()->canceled ) )
|
||||
{
|
||||
$where[] = array( 'ps_cancelled=?', \intval( \IPS\Request::i()->canceled ) );
|
||||
$where[] = array( 'ps_cancelled=?', intval( Request::i()->canceled ) );
|
||||
}
|
||||
|
||||
/* Item */
|
||||
if ( isset( \IPS\Request::i()->itemApp ) )
|
||||
if ( isset( Request::i()->itemApp ) )
|
||||
{
|
||||
$where[] = array( 'ps_app=?', \IPS\Request::i()->itemApp );
|
||||
$where[] = array( 'ps_app=?', Request::i()->itemApp );
|
||||
}
|
||||
if ( isset( \IPS\Request::i()->itemType ) )
|
||||
if ( isset( Request::i()->itemType ) )
|
||||
{
|
||||
$where[] = array( 'ps_type=?', \IPS\Request::i()->itemType );
|
||||
$where[] = array( 'ps_type=?', Request::i()->itemType );
|
||||
}
|
||||
if ( isset( \IPS\Request::i()->itemId ) )
|
||||
if ( isset( Request::i()->itemId ) )
|
||||
{
|
||||
$where[] = array( 'ps_item_id=?', \IPS\Request::i()->itemId );
|
||||
$where[] = array( 'ps_item_id=?', Request::i()->itemId );
|
||||
}
|
||||
|
||||
/* Parent */
|
||||
if ( isset( \IPS\Request::i()->parent ) )
|
||||
if ( isset( Request::i()->parent ) )
|
||||
{
|
||||
$where[] = array( 'ps_parent=?', \intval( \IPS\Request::i()->parent ) );
|
||||
$where[] = array( 'ps_parent=?', intval( Request::i()->parent ) );
|
||||
}
|
||||
|
||||
/* Show */
|
||||
if ( isset( \IPS\Request::i()->show ) )
|
||||
if ( isset( Request::i()->show ) )
|
||||
{
|
||||
$where[] = array( 'ps_show=?', \intval( \IPS\Request::i()->show ) );
|
||||
$where[] = array( 'ps_show=?', intval( Request::i()->show ) );
|
||||
}
|
||||
|
||||
/* Sort */
|
||||
if ( isset( \IPS\Request::i()->sortBy ) and \in_array( \IPS\Request::i()->sortBy, array( 'start', 'expire' ) ) )
|
||||
if ( isset( Request::i()->sortBy ) and in_array( Request::i()->sortBy, array( 'start', 'expire' ) ) )
|
||||
{
|
||||
$sortBy = 'ps_' . \IPS\Request::i()->sortBy;
|
||||
$sortBy = 'ps_' . Request::i()->sortBy;
|
||||
}
|
||||
else
|
||||
{
|
||||
$sortBy = 'ps_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( '*', 'nexus_purchases', $where, "{$sortBy} {$sortDir}" ),
|
||||
isset( \IPS\Request::i()->page ) ? \IPS\Request::i()->page : 1,
|
||||
Db::i()->select( '*', 'nexus_purchases', $where, "{$sortBy} {$sortDir}" ),
|
||||
isset( Request::i()->page ) ? Request::i()->page : 1,
|
||||
'IPS\nexus\Purchase',
|
||||
\IPS\Db::i()->select( 'COUNT(*)', 'nexus_purchases', $where )->first(),
|
||||
Db::i()->select( 'COUNT(*)', 'nexus_purchases', $where )->first(),
|
||||
$this->member,
|
||||
isset( \IPS\Request::i()->perPage ) ? \IPS\Request::i()->perPage : NULL
|
||||
isset( Request::i()->perPage ) ? Request::i()->perPage : NULL
|
||||
);
|
||||
}
|
||||
|
||||
@@ -125,23 +140,24 @@ class _purchases extends \IPS\Api\Controller
|
||||
*
|
||||
* @param int $id ID Number
|
||||
* @throws 2X310/1 INVALID_ID The purchase ID does not exist or the authorized user does not have permission to view it
|
||||
* @return \IPS\nexus\Purchase
|
||||
* @apireturn \IPS\nexus\Purchase
|
||||
* @return Response
|
||||
*/
|
||||
public function GETitem( $id )
|
||||
public function GETitem( int $id ): Response
|
||||
{
|
||||
try
|
||||
{
|
||||
$object = \IPS\nexus\Purchase::load( $id );
|
||||
$object = Purchase::load( $id );
|
||||
if ( $this->member and !$object->canView( $this->member ) )
|
||||
{
|
||||
throw new \OutOfRangeException;
|
||||
throw new OutOfRangeException;
|
||||
}
|
||||
|
||||
return new \IPS\Api\Response( 200, $object->apiOutput( $this->member ) );
|
||||
return new Response( 200, $object->apiOutput( $this->member ) );
|
||||
}
|
||||
catch ( \OutOfRangeException $e )
|
||||
catch ( OutOfRangeException )
|
||||
{
|
||||
throw new \IPS\Api\Exception( 'INVALID_ID', '2X309/1', 404 );
|
||||
throw new Exception( 'INVALID_ID', '2X309/1', 404 );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -153,23 +169,24 @@ class _purchases extends \IPS\Api\Controller
|
||||
* @apiparam object customFields Values for custom fields
|
||||
* @param int $id ID Number
|
||||
* @throws 2X309/1 INVALID_ID The purchase ID does not exist
|
||||
* @return \IPS\nexus\Purchase
|
||||
* @apireturn \IPS\nexus\Purchase
|
||||
* @return Response
|
||||
*/
|
||||
public function POSTitem( $id )
|
||||
public function POSTitem( int $id ): Response
|
||||
{
|
||||
try
|
||||
{
|
||||
$purchase = \IPS\nexus\Purchase::load( $id );
|
||||
$purchase = Purchase::load( $id );
|
||||
}
|
||||
catch ( \OutOfRangeException $e )
|
||||
catch ( OutOfRangeException )
|
||||
{
|
||||
throw new \IPS\Api\Exception( 'INVALID_ID', '2X309/1', 404 );
|
||||
throw new Exception( 'INVALID_ID', '2X309/1', 404 );
|
||||
}
|
||||
|
||||
if ( isset( \IPS\Request::i()->customFields ) )
|
||||
if ( isset( Request::i()->customFields ) )
|
||||
{
|
||||
$customFields = $purchase->custom_fields;
|
||||
foreach ( \IPS\Request::i()->customFields as $k => $v )
|
||||
foreach ( Request::i()->customFields as $k => $v )
|
||||
{
|
||||
$customFields[ $k ] = $v;
|
||||
}
|
||||
@@ -178,6 +195,6 @@ class _purchases extends \IPS\Api\Controller
|
||||
|
||||
$purchase->save();
|
||||
|
||||
return new \IPS\Api\Response( 200, $purchase->apiOutput( $this->member ) );
|
||||
return new Response( 200, $purchase->apiOutput( $this->member ) );
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user