Version 4.5.0

This commit is contained in:
Neo committed 2025-12-19 05:51:58 -08:00
1 parent 1a0c7fd3c2
commit f79dcf067a
3791 files changed
+248032 -76372

No files matched your search

@@ -12,7 +12,7 @@
namespace IPS\nexus\modules\admin\subscriptions;
/* To prevent PHP errors (extending class does not exist) revealing path */
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' );
exit;
@@ -23,6 +23,11 @@ if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
*/
class _subscriptions extends \IPS\Node\Controller
{
/**
* @brief Has been CSRF-protected
*/
public static $csrfProtected = TRUE;
/**
* Node Class
*/
@@ -85,17 +90,23 @@ class _subscriptions extends \IPS\Node\Controller
*/
public function convertToSubscription()
{
\IPS\Session::i()->csrfCheck();
try
{
$package = \IPS\nexus\Package::load( \IPS\Request::i()->id );
if ( $package->deleteOrMoveQueued() )
{
throw new \OutOfRangeException;
}
}
catch( \OutOfRangeException $ex )
{
\IPS\Output::i()->error( 'node_error', '2X393/2', 404, '' );
}
$renewOptions = array();
if ( $package->renew_options and $_renewOptions = json_decode( $package->renew_options, TRUE ) and is_array( $_renewOptions ) )
if ( $package->renew_options and $_renewOptions = json_decode( $package->renew_options, TRUE ) and \is_array( $_renewOptions ) )
{
foreach ( $_renewOptions as $option )
{
@@ -104,11 +115,23 @@ class _subscriptions extends \IPS\Node\Controller
{
$costs[ $cost['currency'] ] = new \IPS\nexus\Money( $cost['amount'], $cost['currency'] );
}
/* Figure out tax */
$tax = NULL;
try
{
if( $package->tax )
{
$tax = \IPS\nexus\Tax::load( $package->tax );
}
}
catch( \OutOfRangeException $e ){}
/* Catch any invalid renewal terms, these can occasionally appear from legacy IP.Subscriptions */
try
{
$renewOptions[] = new \IPS\nexus\Purchase\RenewalTerm( $costs, new \DateInterval( "P{$option['term']}" . mb_strtoupper( $option['unit'] ) ), NULL, $option['add'] );
$renewOptions[] = new \IPS\nexus\Purchase\RenewalTerm( $costs, new \DateInterval( "P{$option['term']}" . mb_strtoupper( $option['unit'] ) ), $tax, $option['add'] );
}
catch( \Exception $ex) {}
}
@@ -129,8 +152,8 @@ class _subscriptions extends \IPS\Node\Controller
{
$sub = new \IPS\nexus\Subscription\Package;
$sub->enabled = 1;
$sub->tax = $values['sp_tax'] ? $values['p_tax']->id : 0;
$sub->gateways = ( isset( $values['sp_gateways'] ) and is_array( $values['sp_gateways'] ) ) ? implode( ',', array_keys( $values['sp_gateways'] ) ) : '*';
$sub->tax = $values['sp_tax'] ? $values['sp_tax']->id : 0;
$sub->gateways = ( isset( $values['sp_gateways'] ) and \is_array( $values['sp_gateways'] ) ) ? implode( ',', array_keys( $values['sp_gateways'] ) ) : '*';
$sub->price = json_encode( $values['sp_price'] );
foreach( array( 'primary_group', 'secondary_group') as $thingsWotAreTheSame )
@@ -201,6 +224,8 @@ class _subscriptions extends \IPS\Node\Controller
*/
public function enable()
{
\IPS\Session::i()->csrfCheck();
\IPS\Settings::i()->changeValues( array( 'nexus_subs_enabled' => true ) );
\IPS\Session::i()->log( 'acplog__subscription_settings' );
@@ -208,6 +233,70 @@ class _subscriptions extends \IPS\Node\Controller
\IPS\Output::i()->redirect( \IPS\Http\Url::internal('app=nexus&module=subscriptions&controller=subscriptions') );
}
/**
* Delete
*
* @return void
*/
public function delete()
{
/* Load package */
try
{
$package = \IPS\nexus\Subscription\Package::load( \IPS\Request::i()->id );
}
catch ( \OutOfRangeException $e )
{
return parent::delete();
}
/* Are there any purchases of this product? */
if ( !isset( \IPS\Request::i()->confirmImplications ) and \IPS\Db::i()->select( 'COUNT(*)', 'nexus_purchases', array( 'ps_app=? AND ps_type=? AND ps_item_id=?', 'nexus', 'subscription', $package->id ) )->first() )
{
$options = array(
'product_delete_confirm' => \IPS\Http\Url::internal( "app=nexus&module=subscriptions&controller=subscriptions&do=delete&wasConfirmed=1&id={$package->_id}&confirmImplications=1" )->csrf(),
);
if ( $package->enabled )
{
$options['product_delete_hide'] = \IPS\Http\Url::internal( "app=nexus&module=subscriptions&controller=subscriptions&do=hide&id={$package->_id}" )->csrf();
}
$options['cancel'] = \IPS\Http\Url::internal( "app=nexus&module=subscriptions&controller=subscriptions" );
\IPS\Output::i()->output = \IPS\Theme::i()->getTemplate( 'global', 'core' )->decision( 'subscription_delete_blurb', $options );
return;
}
/* If not, just handle the delete as normal */
return parent::delete();
}
/**
* Hide from store
*
* @return void
*/
public function hide()
{
\IPS\Session::i()->csrfCheck();
/* Load package */
try
{
$package = \IPS\nexus\Subscription\Package::load( \IPS\Request::i()->id );
}
catch ( \OutOfRangeException $e )
{
\IPS\Output::i()->error( 'node_error', '1X393/2', 404, '' );
}
/* Do it */
$package->enabled = FALSE;
$package->save();
/* Redirect */
\IPS\Output::i()->redirect( \IPS\Http\Url::internal( "app=nexus&module=subscriptions&controller=subscriptions" ) );
}
/**
* Add a member for free!
*
@@ -267,7 +356,7 @@ class _subscriptions extends \IPS\Node\Controller
$form->add( new \IPS\Helpers\Form\YesNo( 'nexus_subs_enabled', \IPS\Settings::i()->nexus_subs_enabled, FALSE, array(), NULL, NULL, NULL, 'nexus_subs_enabled' ) );
$form->add( new \IPS\Helpers\Form\YesNo( 'nexus_subs_register', \IPS\Settings::i()->nexus_subs_register, FALSE, array(), NULL, NULL, NULL, 'nexus_subs_register' ) );
$form->add( new \IPS\Helpers\Form\YesNo( 'nexus_subs_show_public', \IPS\Settings::i()->nexus_subs_show_public, FALSE, array(), NULL, NULL, NULL, 'nexus_subs_show_public' ) );
$form->add( new \IPS\Helpers\Form\Number( 'nexus_subs_invoice_grace', \IPS\Settings::i()->nexus_subs_invoice_grace, FALSE, array( ), NULL, NULL, \IPS\Member::loggedIn()->language()->addToStack('days'), 'nexus_subs_invoice_grace' ) );
$form->add( new \IPS\Helpers\Form\Interval( 'nexus_subs_invoice_grace', \IPS\Settings::i()->nexus_subs_invoice_grace, FALSE, array( 'valueAs' => \IPS\Helpers\Form\Interval::DAYS ), NULL, NULL, NULL, 'nexus_subs_invoice_grace' ) );
$form->add( new \IPS\Helpers\Form\Select( 'nexus_subs_exclude_groups', explode( ',', \IPS\Settings::i()->nexus_subs_exclude_groups ), FALSE, array( 'options' => $groups, 'multiple' => TRUE ) ) );
@@ -313,5 +402,235 @@ class _subscriptions extends \IPS\Node\Controller
\IPS\Output::i()->title = \IPS\Member::loggedIn()->language()->addToStack('settings');
\IPS\Output::i()->output = $form;
}
/**
* Redirect after save
*
* @param \IPS\Node\Model $old A clone of the node as it was before or NULL if this is a creation
* @param \IPS\Node\Model $new The node now
* @param string $lastUsedTab The tab last used in the form
* @return void
*/
protected function _afterSave( \IPS\Node\Model $old = NULL, \IPS\Node\Model $new, $lastUsedTab = FALSE )
{
$changes = array();
if ( $old )
{
foreach ( array( 'tax', 'renew_options', 'primary_group', 'secondary_group' ) as $k )
{
if ( $old->$k != $new->$k )
{
$changes[ $k ] = $old->$k;
}
}
}
/* If something has changed, see if anyone has purchased */
$purchases = 0;
if( \count( $changes ) )
{
$purchases = \IPS\Db::i()->select( 'COUNT(*)', 'nexus_member_subscriptions', array( 'sub_package_id=? AND sub_active=1 and sub_added_manually=0', $new->id ) )->first();
}
/* Only show this screen if the package has been purchased. Otherwise even just copying a package and saving asks if you want to update
existing purchases unnecessarily */
if ( !empty( $changes ) AND $purchases )
{
\IPS\Output::i()->output = \IPS\Theme::i()->getTemplate( 'global', 'core' )->decision( 'product_change_blurb', array(
'product_change_blurb_existing' => \IPS\Http\Url::internal( "app=nexus&module=subscriptions&controller=subscriptions&do=updateExisting&id={$new->_id}" )->setQueryString( 'changes', json_encode( $changes ) )->csrf(),
'product_change_blurb_new' => $this->url->setQueryString( array( 'root' => ( $new->parent() ? $new->parent()->_id : '' ) ) ),
) );
}
else
{
return parent::_afterSave( $old, $new, $lastUsedTab );
}
}
/**
* Update Existing Purchases
*
* @return void
*/
public function updateExisting()
{
\IPS\Session::i()->csrfCheck();
try
{
$package = \IPS\nexus\Subscription\Package::load( \IPS\Request::i()->id );
}
catch ( \OutOfRangeException $e )
{
\IPS\Output::i()->error( 'node_error', '1X393/1', 404, '' );
}
$changes = json_decode( \IPS\Request::i()->changes, TRUE );
if ( !isset( \IPS\Request::i()->processing ) )
{
if ( isset( $changes['renew_options'] ) )
{
$matrix = new \IPS\Helpers\Form\Matrix( 'matrix', 'continue' );
$matrix->manageable = FALSE;
$newOptions = array( '-' => \IPS\Member::loggedIn()->language()->addToStack('do_not_change') );
$newOption = json_decode( $package->renew_options, TRUE );
$costs = array();
foreach ( $newOption['cost'] as $data )
{
$costs[] = new \IPS\nexus\Money( $data['amount'], $data['currency'] );
}
switch ( $newOption['unit'] )
{
case 'd':
$term = \IPS\Member::loggedIn()->language()->addToStack('renew_days', FALSE, array( 'pluralize' => array( $newOption['term'] ) ) );
break;
case 'm':
$term = \IPS\Member::loggedIn()->language()->addToStack('renew_months', FALSE, array( 'pluralize' => array( $newOption['term'] ) ) );
break;
case 'y':
$term = \IPS\Member::loggedIn()->language()->addToStack('renew_years', FALSE, array( 'pluralize' => array( $newOption['term'] ) ) );
break;
}
$newOptions['o'] = \IPS\Member::loggedIn()->language()->addToStack( 'renew_option', FALSE, array( 'sprintf' => array( \IPS\Member::loggedIn()->language()->formatList( $costs, \IPS\Member::loggedIn()->language()->get('or_list_format') ), $term ) ) );
$newOptions['z'] = \IPS\Member::loggedIn()->language()->addToStack('remove_renewal_no_expire_leave');
$newOptions['y'] = \IPS\Member::loggedIn()->language()->addToStack('remove_renewal_no_expire_reactivate');
$newOptions['x'] = \IPS\Member::loggedIn()->language()->addToStack('remove_renewal_expire');
$matrix->columns = array(
'customers_currently_paying' => function( $key, $value, $data )
{
return $data[0];
},
'now_pay' => function( $key, $value, $data ) use ( $newOptions )
{
return new \IPS\Helpers\Form\Select( $key, $data[1], TRUE, array( 'options' => $newOptions, 'noDefault' => TRUE ) );
},
);
if ( $changes['renew_options'] )
{
$oldOption = json_decode( $changes['renew_options'], TRUE );
$costs = array();
foreach ( $oldOption['cost'] as $data )
{
$costs[] = new \IPS\nexus\Money( $data['amount'], $data['currency'] );
}
switch ( $oldOption['unit'] )
{
case 'd':
$term = \IPS\Member::loggedIn()->language()->addToStack('renew_days', FALSE, array( 'pluralize' => array( $oldOption['term'] ) ) );
break;
case 'm':
$term = \IPS\Member::loggedIn()->language()->addToStack('renew_months', FALSE, array( 'pluralize' => array( $oldOption['term'] ) ) );
break;
case 'y':
$term = \IPS\Member::loggedIn()->language()->addToStack('renew_years', FALSE, array( 'pluralize' => array( $oldOption['term'] ) ) );
break;
}
$matrix->rows[ 1 ] = array( \IPS\Member::loggedIn()->language()->addToStack( 'renew_option', FALSE, array( 'sprintf' => array( \IPS\Member::loggedIn()->language()->formatList( $costs, \IPS\Member::loggedIn()->language()->get('or_list_format') ), $term ) ) ), "o" );
}
if ( $values = $matrix->values() )
{
$data = json_decode( $changes['renew_options'], TRUE );
$changes['renew_options'] = array();
if( !empty( $data ) )
{
$changes['renew_options'] = array( 'old' => $data, 'new' => $values[1]['now_pay'] );
}
}
else
{
\IPS\Output::i()->output .= $matrix;
return;
}
}
}
if ( ( isset( $changes['renew_options'] ) or isset( $changes['tax'] ) ) and !isset( \IPS\Request::i()->ba ) )
{
$needBaPrompt = FALSE;
$canChangeOptions = FALSE;
if ( isset( $changes['renew_options'] ) and !\in_array( $changes['renew_options']['new'], array( '-', 'x', 'y', 'z' ) ) )
{
$needBaPrompt = TRUE;
$canChangeOptions = TRUE;
}
if ( isset( $changes['tax'] ) )
{
$needBaPrompt = TRUE;
}
if ( $needBaPrompt and $withBillingAgreement = \IPS\Db::i()->select( 'COUNT(*)', 'nexus_purchases', array( 'ps_app=? AND ps_type=? AND ps_item_id=? AND ps_billing_agreement>0 AND ba_canceled=0', 'nexus', 'subscription', $package->id ) )->join( 'nexus_billing_agreements', 'ba_id=ps_billing_agreement' )->first() )
{
$options = array(
'change_renew_ba_skip' => \IPS\Http\Url::internal( "app=nexus&module=subscriptions&controller=subscriptions&do=updateExisting" )->setQueryString( array(
'id' => \IPS\Request::i()->id,
'changes' => json_encode( $changes ),
'processing'=> 1,
'ba' => 0
) )->csrf(),
'change_renew_ba_cancel' => \IPS\Http\Url::internal( "app=nexus&module=subscriptions&controller=subscriptions&do=updateExisting" )->setQueryString( array(
'id' => \IPS\Request::i()->id,
'changes' => json_encode( $changes ),
'processing'=> 1,
'ba' => 1
) )->csrf()
);
if ( $canChangeOptions )
{
$options['change_renew_ba_go_back'] = \IPS\Http\Url::internal( "app=nexus&module=subscriptions&controller=subscriptions&do=updateExisting" )->setQueryString( array(
'id' => \IPS\Request::i()->id,
'changes' => \IPS\Request::i()->changes,
) )->csrf();
}
\IPS\Output::i()->output = \IPS\Theme::i()->getTemplate( 'global', 'core' )->decision( 'change_renew_ba_blurb', $options );
return;
}
}
\IPS\Output::i()->output = new \IPS\Helpers\MultipleRedirect(
\IPS\Http\Url::internal( "app=nexus&module=subscriptions&controller=subscriptions&do=updateExisting&id=1&changes=secondary_group" )->setQueryString( array(
'id' => \IPS\Request::i()->id,
'changes' => json_encode( $changes ),
'processing'=> 1,
'ba' => isset( \IPS\Request::i()->ba ) ? \IPS\Request::i()->ba : 0
) )->csrf(),
function( $offset ) use ( $package, $changes )
{
$offset = \intval( $offset );
$select = \IPS\Db::i()->select( '*', 'nexus_purchases', array( "ps_app=? and ps_type=? and ps_item_id=?", 'nexus', 'subscription', $package->id ), 'ps_id', array( $offset, 1 ) );
try
{
$purchase = \IPS\nexus\Purchase::constructFromData( $select->first() );
$total = \IPS\Db::i()->select( 'COUNT(*)', 'nexus_purchases', array( "ps_app=? and ps_type=? and ps_item_id=?", 'nexus', 'subscription', $package->id ) )->first();
$package->updatePurchase( $purchase, $changes, \IPS\Request::i()->ba );
return array( ++$offset, \IPS\Member::loggedIn()->language()->get('processing'), 100 / $total * $offset );
}
catch ( \UnderflowException $e )
{
return NULL;
}
},
function() use ( $package )
{
\IPS\Output::i()->redirect( \IPS\Http\Url::internal( "app=nexus&module=subscriptions&controller=subscriptions" )->setQueryString( array( 'root' => ( $package->parent() ? $package->parent()->_id : '' ) ) ) );
}
);
}
}