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,43 +11,53 @@
|
||||
namespace IPS\nexus;
|
||||
|
||||
/* To prevent PHP errors (extending class does not exist) revealing path */
|
||||
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
|
||||
use Exception;
|
||||
use IPS\DateTime;
|
||||
use IPS\Db;
|
||||
use IPS\Member;
|
||||
use IPS\nexus\Subscription\Package;
|
||||
use IPS\Patterns\ActiveRecord;
|
||||
use OutOfRangeException;
|
||||
use function defined;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* User subscription model
|
||||
*/
|
||||
class _Subscription extends \IPS\Patterns\ActiveRecord
|
||||
class Subscription extends ActiveRecord
|
||||
{
|
||||
/**
|
||||
* @brief Database Table
|
||||
*/
|
||||
public static $databaseTable = 'nexus_member_subscriptions';
|
||||
public static ?string $databaseTable = 'nexus_member_subscriptions';
|
||||
|
||||
/**
|
||||
* @brief [ActiveRecord] Database Prefix
|
||||
*/
|
||||
public static $databasePrefix = 'sub_';
|
||||
public static string $databasePrefix = 'sub_';
|
||||
|
||||
/**
|
||||
* @brief Multiton Store
|
||||
*/
|
||||
protected static $multitons;
|
||||
protected static array $multitons;
|
||||
|
||||
/**
|
||||
* @brief [ActiveRecord] Database ID Fields
|
||||
*/
|
||||
protected static $databaseIdFields = array( 'sub_purchase_id' );
|
||||
protected static array $databaseIdFields = array( 'sub_purchase_id' );
|
||||
|
||||
/**
|
||||
* Get the DateTime object for when this little subscription doth run out
|
||||
*
|
||||
* @return \IPS\DateTime|NULL
|
||||
* @return DateTime|NULL
|
||||
*/
|
||||
public function get__expire()
|
||||
public function get__expire() : DateTime|null
|
||||
{
|
||||
if ( $this->expire )
|
||||
{
|
||||
@@ -56,17 +66,17 @@ class _Subscription extends \IPS\Patterns\ActiveRecord
|
||||
/* This has been invoiced, so there is a purchase row - fetch the expiration from that */
|
||||
try
|
||||
{
|
||||
$purchase = \IPS\nexus\Purchase::load( $this->purchase_id );
|
||||
$purchase = Purchase::load( $this->purchase_id );
|
||||
|
||||
if ( $purchase->expire )
|
||||
{
|
||||
return $purchase->expire;
|
||||
}
|
||||
}
|
||||
catch( \OutOfRangeException $e ) { }
|
||||
catch( OutOfRangeException ) { }
|
||||
}
|
||||
|
||||
return \IPS\DateTime::ts( $this->expire );
|
||||
return DateTime::ts( $this->expire );
|
||||
}
|
||||
|
||||
return NULL;
|
||||
@@ -75,15 +85,15 @@ class _Subscription extends \IPS\Patterns\ActiveRecord
|
||||
/**
|
||||
* Get the package (ooh, very mysterious, sounds like something from Narcos)
|
||||
*
|
||||
* @return \IPS\nexus\Subscription\Package or NULL, I don't really care at this point.
|
||||
* @return Package|null
|
||||
*/
|
||||
public function get_package()
|
||||
public function get_package() : Package|null
|
||||
{
|
||||
try
|
||||
{
|
||||
return \IPS\nexus\Subscription\Package::load( $this->package_id );
|
||||
return Package::load( $this->package_id );
|
||||
}
|
||||
catch( \OutOfRangeException $ex )
|
||||
catch( OutOfRangeException )
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
@@ -92,15 +102,15 @@ class _Subscription extends \IPS\Patterns\ActiveRecord
|
||||
/**
|
||||
* Get the purchase
|
||||
*
|
||||
* @return \IPS\nexus\Subscription\Package or NULL, I don't really care at this point.
|
||||
* @return Purchase|null
|
||||
*/
|
||||
public function get_purchase()
|
||||
public function get_purchase() : Purchase|null
|
||||
{
|
||||
try
|
||||
{
|
||||
return \IPS\nexus\Purchase::load( $this->purchase_id );
|
||||
return Purchase::load( $this->purchase_id );
|
||||
}
|
||||
catch( \OutOfRangeException $ex )
|
||||
catch( OutOfRangeException )
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
@@ -109,11 +119,11 @@ class _Subscription extends \IPS\Patterns\ActiveRecord
|
||||
/**
|
||||
* Change the subscription package
|
||||
*
|
||||
* @param \IPS\nexus\Subscription\Package $package The new package innit
|
||||
* @param \IPS\DateTime|NULL $expires The new expiration date
|
||||
* @param Package $package The new package innit
|
||||
* @param DateTime|NULL $expires The new expiration date
|
||||
* @return void
|
||||
*/
|
||||
public function changePackage( \IPS\nexus\Subscription\Package $package, $expires=NULL )
|
||||
public function changePackage( Package $package, ?DateTime $expires=NULL ) : void
|
||||
{
|
||||
$this->package_id = $package->id;
|
||||
$this->expire = ( $expires === NULL ) ? 0 : $expires->getTimeStamp();
|
||||
@@ -126,36 +136,36 @@ class _Subscription extends \IPS\Patterns\ActiveRecord
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function currentBlurb()
|
||||
public function currentBlurb() : string
|
||||
{
|
||||
if ( !$this->active AND !$this->purchase->cancelled )
|
||||
{
|
||||
return \IPS\Member::loggedIn()->language()->addToStack( 'nexus_subs_subscribed_expired' );
|
||||
return Member::loggedIn()->language()->addToStack( 'nexus_subs_subscribed_expired' );
|
||||
}
|
||||
elseif( $this->purchase->cancelled AND $this->purchase->can_reactivate )
|
||||
{
|
||||
return \IPS\Member::loggedIn()->language()->addToStack( 'nexus_subs_subscribed_cancelled' );
|
||||
return Member::loggedIn()->language()->addToStack( 'nexus_subs_subscribed_cancelled' );
|
||||
}
|
||||
elseif( $this->purchase->cancelled )
|
||||
{
|
||||
return \IPS\Member::loggedIn()->language()->addToStack( 'nexus_subs_subscribed_cancelled_no_reactivate' );
|
||||
return Member::loggedIn()->language()->addToStack( 'nexus_subs_subscribed_cancelled_no_reactivate' );
|
||||
}
|
||||
if ( $this->expire and $this->renews and !$this->manually_added )
|
||||
{
|
||||
return \IPS\Member::loggedIn()->language()->addToStack( 'nexus_subs_subscribed_with_expire' . ( ( $this->purchase and $this->purchase->renewals ) ? '' : '_no_renewal' ), NULL, array( 'sprintf' => array( $this->_expire->dayAndMonth() . ' ' . $this->_expire->format('Y') ) ) );
|
||||
return Member::loggedIn()->language()->addToStack( 'nexus_subs_subscribed_with_expire' . ( ( $this->purchase and $this->purchase->renewals ) ? '' : '_no_renewal' ), NULL, array( 'sprintf' => array( $this->_expire->dayAndMonth() . ' ' . $this->_expire->format('Y') ) ) );
|
||||
}
|
||||
|
||||
return \IPS\Member::loggedIn()->language()->addToStack( 'nexus_subs_subscribed' );
|
||||
return Member::loggedIn()->language()->addToStack( 'nexus_subs_subscribed' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Find and return the package this person is currently subscribed to, or NULL
|
||||
*
|
||||
* @param \IPS\Member $member The member
|
||||
* @param Member $member The member
|
||||
* @param bool $activeOnly If TRUE, returns only active subscription
|
||||
* @return ?\IPS\nexus\Subscription
|
||||
* @return static|ActiveRecord|null
|
||||
*/
|
||||
public static function loadByMember( \IPS\Member $member, bool $activeOnly )
|
||||
public static function loadByMember( Member $member, bool $activeOnly ) : static|ActiveRecord|null
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -166,9 +176,9 @@ class _Subscription extends \IPS\Patterns\ActiveRecord
|
||||
$where[] = [ 'sub_active=1' ];
|
||||
}
|
||||
|
||||
return static::constructFromData( \IPS\Db::i()->select( '*', 'nexus_member_subscriptions', $where, 'sub_active DESC, sub_start DESC' )->first() );
|
||||
return static::constructFromData( Db::i()->select( '*', 'nexus_member_subscriptions', $where, 'sub_active DESC, sub_start DESC' )->first() );
|
||||
}
|
||||
catch( \Exception $e )
|
||||
catch( Exception )
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
@@ -177,25 +187,25 @@ class _Subscription extends \IPS\Patterns\ActiveRecord
|
||||
/**
|
||||
* Find and return the package this person is currently subscribed to, or NULL
|
||||
*
|
||||
* @param \IPS\Member|NULL $member Pass a member in if you like, no pressure really up to you
|
||||
* @return \IPS\nexus\Subscription or NULL
|
||||
* @param Member|NULL $member Pass a member in if you like, no pressure really up to you
|
||||
* @return static|null
|
||||
* @deprecated Use loadByMember() instead
|
||||
*/
|
||||
public static function loadActiveByMember( $member=NULL )
|
||||
public static function loadActiveByMember( ?Member $member=NULL ) : static|null
|
||||
{
|
||||
return static::loadByMember( $member ? $member : \IPS\Member::loggedIn(), TRUE );
|
||||
return static::loadByMember( $member ?: Member::loggedIn(), TRUE );
|
||||
}
|
||||
|
||||
/**
|
||||
* Load a subscription by member and package
|
||||
*
|
||||
* @param \IPS\Member $member Take a guess
|
||||
* @param \IPS\nexus\Subscription\Package $package I mean it's really writing itself
|
||||
* @param Member $member Take a guess
|
||||
* @param Package $package I mean it's really writing itself
|
||||
* @param boolean $activeOnly Only get active packages
|
||||
* @return \IPS\nexus\Subscription
|
||||
* @throws \OutOfRangeException
|
||||
* @return static|ActiveRecord
|
||||
* @throws OutOfRangeException
|
||||
*/
|
||||
public static function loadByMemberAndPackage( \IPS\Member $member, \IPS\nexus\Subscription\Package $package, $activeOnly = TRUE )
|
||||
public static function loadByMemberAndPackage( Member $member, Package $package, bool $activeOnly = TRUE ) : static|ActiveRecord
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -206,22 +216,22 @@ class _Subscription extends \IPS\Patterns\ActiveRecord
|
||||
$where[] = array( 'sub_active=1' );
|
||||
}
|
||||
|
||||
return static::constructFromData( \IPS\Db::i()->select( '*', 'nexus_member_subscriptions', $where )->first() );
|
||||
return static::constructFromData( Db::i()->select( '*', 'nexus_member_subscriptions', $where )->first() );
|
||||
}
|
||||
catch( \Exception $ex )
|
||||
catch( Exception )
|
||||
{
|
||||
throw new \OutOfRangeException;
|
||||
throw new OutOfRangeException;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Mark all subscriptions by this member as inactive
|
||||
*
|
||||
* @param \IPS\Member $member I dunno, take a guess
|
||||
* @param Member $member I dunno, take a guess
|
||||
* @return void
|
||||
*/
|
||||
public static function markInactiveByUser( \IPS\Member $member )
|
||||
public static function markInactiveByUser( Member $member ) : void
|
||||
{
|
||||
\IPS\Db::i()->update( 'nexus_member_subscriptions', array( 'sub_active' => 0 ), array( 'sub_member_id=?', $member->member_id ) );
|
||||
Db::i()->update( 'nexus_member_subscriptions', array( 'sub_active' => 0 ), array( 'sub_member_id=?', $member->member_id ) );
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user