Version 5.0.0 beta 1

This commit is contained in:
Neo committed 2025-12-19 16:27:35 -08:00
1 parent 25ddeb65d6
commit 15c7beabc5
6736 files changed
+627902 -497943

No files matched your search

@@ -12,16 +12,42 @@
namespace IPS\nexus\modules\front\store;
/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
use DomainException;
use InvalidArgumentException;
use IPS\Dispatcher\Controller;
use IPS\Helpers\Form;
use IPS\Helpers\Form\Color;
use IPS\Helpers\Form\Email;
use IPS\Helpers\Form\Radio;
use IPS\Helpers\Form\Text;
use IPS\Helpers\Form\TextArea;
use IPS\Http\Url;
use IPS\Math\Number;
use IPS\Member;
use IPS\nexus\Customer;
use IPS\nexus\extensions\nexus\Item\GiftVoucher;
use IPS\nexus\Gateway;
use IPS\nexus\Invoice;
use IPS\nexus\Money;
use IPS\Output;
use IPS\Request;
use IPS\Settings;
use IPS\Theme;
use function defined;
use function in_array;
use function is_numeric;
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;
}
/**
* Gift Vouchers
*/
class _gifts extends \IPS\Dispatcher\Controller
class gifts extends Controller
{
/**
@@ -29,15 +55,10 @@ class _gifts extends \IPS\Dispatcher\Controller
*
* @return void
*/
public function execute()
public function execute() : void
{
\IPS\Output::i()->jsFiles = array_merge( \IPS\Output::i()->jsFiles, \IPS\Output::i()->js( 'front_store.js', 'nexus', 'front' ) );
\IPS\Output::i()->cssFiles = array_merge( \IPS\Output::i()->cssFiles, \IPS\Theme::i()->css( 'store.css', 'nexus' ) );
if ( \IPS\Theme::i()->settings['responsive'] )
{
\IPS\Output::i()->cssFiles = array_merge( \IPS\Output::i()->cssFiles, \IPS\Theme::i()->css( 'store_responsive.css', 'nexus', 'front' ) );
}
Output::i()->jsFiles = array_merge( Output::i()->jsFiles, Output::i()->js( 'front_store.js', 'nexus', 'front' ) );
Output::i()->cssFiles = array_merge( Output::i()->cssFiles, Theme::i()->css( 'store.css', 'nexus' ) );
parent::execute();
}
@@ -47,59 +68,60 @@ class _gifts extends \IPS\Dispatcher\Controller
*
* @return void
*/
protected function manage()
protected function manage() : void
{
/* Work out what our options are */
$memberCurrency = ( ( ( isset( \IPS\Request::i()->cookie['currency'] ) and \in_array( \IPS\Request::i()->cookie['currency'], \IPS\nexus\Money::currencies() ) ) and \in_array( \IPS\Request::i()->cookie['currency'], \IPS\nexus\Money::currencies() ) ) ? \IPS\Request::i()->cookie['currency'] : \IPS\nexus\Customer::loggedIn()->defaultCurrency() );
$memberCurrency = ( ( ( isset( Request::i()->cookie['currency'] ) and in_array( Request::i()->cookie['currency'], Money::currencies() ) ) and in_array( Request::i()->cookie['currency'], Money::currencies() ) ) ? Request::i()->cookie['currency'] : Customer::loggedIn()->defaultCurrency() );
$options = array();
foreach ( json_decode( \IPS\Settings::i()->nexus_gift_vouchers, TRUE ) as $voucher )
foreach ( json_decode( Settings::i()->nexus_gift_vouchers, TRUE ) as $voucher )
{
if ( isset( $voucher[ $memberCurrency ] ) and $voucher[ $memberCurrency ] )
{
$options[ $voucher[ $memberCurrency ] ] = new \IPS\nexus\Money( $voucher[ $memberCurrency ], $memberCurrency );
$options[ $voucher[ $memberCurrency ] ] = new Money( $voucher[ $memberCurrency ], $memberCurrency );
}
}
if ( \IPS\Settings::i()->nexus_gift_vouchers_free )
if ( Settings::i()->nexus_gift_vouchers_free )
{
$options['x'] = \IPS\Member::loggedIn()->language()->addToStack('other');
$options['x'] = Member::loggedIn()->language()->addToStack('other');
}
if ( empty( $options ) )
{
\IPS\Output::i()->error( 'no_module_permission', '4X213/2', 403, '' );
Output::i()->error( 'no_module_permission', '4X213/2', 403, '' );
}
$form = new \IPS\Helpers\Form( 'form', 'buy_gift_voucher' );
$form->class = 'ipsForm_vertical';
$form->add( new \IPS\Helpers\Form\Color( 'gift_voucher_color', '3b3b3b', FALSE ) );
$form->add( new \IPS\Helpers\Form\Radio( 'gift_voucher_amount', NULL, TRUE, array( 'options' => $options, 'parse' => 'normal', 'userSuppliedInput' => 'x' ) ) );
$form->add( new \IPS\Helpers\Form\Radio( 'gift_voucher_method', NULL, TRUE, array( 'options' => array( 'email' => 'email', 'print' => 'gift_voucher_print' ), 'toggles' => array( 'email' => array( 'gift_voucher_email' ) ) ) ) );
$form->add( new \IPS\Helpers\Form\Email( 'gift_voucher_email', NULL, NULL, array(), function( $val )
$form = new Form( 'form', 'buy_gift_voucher' );
$form->class = 'ipsForm--vertical ipsForm--buy-gift-voucher';
$form->add( new Color( 'gift_voucher_color', '3b3b3b', FALSE ) );
$form->add( new Radio( 'gift_voucher_amount', NULL, TRUE, array( 'options' => $options, 'parse' => 'normal', 'userSuppliedInput' => 'x' ) ) );
$form->add( new Radio( 'gift_voucher_method', NULL, TRUE, array( 'options' => array( 'email' => 'email', 'print' => 'gift_voucher_print' ), 'toggles' => array( 'email' => array( 'gift_voucher_email' ) ) ) ) );
$form->add( new Email( 'gift_voucher_email', NULL, NULL, array(), function( $val )
{
if ( !$val and \IPS\Request::i()->gift_voucher_method === 'email' )
if ( !$val and Request::i()->gift_voucher_method === 'email' )
{
throw new \DomainException('form_required');
throw new DomainException('form_required');
}
}, NULL, NULL, 'gift_voucher_email' ) );
$form->add( new \IPS\Helpers\Form\Text( 'gift_voucher_recipient' ) );
$form->add( new \IPS\Helpers\Form\Text( 'gift_voucher_sender', \IPS\Member::loggedIn()->name ) );
$form->add( new \IPS\Helpers\Form\TextArea( 'gift_voucher_message' ) );
$form->add( new Text( 'gift_voucher_recipient' ) );
$form->add( new Text( 'gift_voucher_sender', Member::loggedIn()->name ) );
$form->add( new TextArea( 'gift_voucher_message' ) );
/* Handle submissions */
if ( $values = $form->values() )
{
if( ! \is_numeric( $values['gift_voucher_amount'] ) )
if( ! is_numeric( $values['gift_voucher_amount'] ) )
{
$form->error = \IPS\Member::loggedIn()->language()->addToStack( 'gift_voucher_invalid_value' );
$form->error = Member::loggedIn()->language()->addToStack( 'gift_voucher_invalid_value' );
}
elseif( ! abs( $values['gift_voucher_amount'] ) )
{
$form->error = \IPS\Member::loggedIn()->language()->addToStack( 'gift_voucher_invalid_amount' );
$form->error = Member::loggedIn()->language()->addToStack( 'gift_voucher_invalid_amount' );
}
if( !$form->error )
{
$item = new \IPS\nexus\extensions\nexus\Item\GiftVoucher( \IPS\Member::loggedIn()->language()->get( 'gift_voucher' ), new \IPS\nexus\Money( abs( $values[ 'gift_voucher_amount' ] ), $memberCurrency ) );
$item->paymentMethodIds = array_keys( \IPS\nexus\Gateway::roots( NULL, NULL, array( 'm_active=1 AND m_gateway<>?', 'TwoCheckout' ) ) ); // It is against 2CO terms to use them for buying gift vouchers
$item = new GiftVoucher( Member::loggedIn()->language()->get( 'gift_voucher' ), new Money( abs( $values[ 'gift_voucher_amount' ] ), $memberCurrency ) );
$item->paymentMethodIds = array_keys( Gateway::roots( NULL, NULL, array( 'm_active=1' ) ) );
$item->extra[ 'method' ] = $values[ 'gift_voucher_method' ];
$item->extra[ 'recipient_email' ] = $values[ 'gift_voucher_email' ];
$item->extra[ 'recipient_name' ] = $values[ 'gift_voucher_recipient' ];
@@ -109,22 +131,22 @@ class _gifts extends \IPS\Dispatcher\Controller
$item->extra[ 'color' ] = $values[ 'gift_voucher_color' ];
$item->extra[ 'currency' ] = $memberCurrency;
$invoice = new \IPS\nexus\Invoice;
$invoice = new Invoice;
$invoice->currency = $memberCurrency;
$invoice->member = \IPS\Member::loggedIn();
$invoice->member = Customer::loggedIn();
$invoice->addItem( $item );
$invoice->save();
\IPS\Output::i()->redirect( $invoice->checkoutUrl() );
Output::i()->redirect( $invoice->checkoutUrl() );
}
}
/* Display */
$formTemplate = $form->customTemplate( array( \IPS\Theme::i()->getTemplate( 'store', 'nexus' ), 'giftCardForm' ) );
$formTemplate = $form->customTemplate( array( Theme::i()->getTemplate( 'store', 'nexus' ), 'giftCardForm' ) );
\IPS\Output::i()->output = \IPS\Theme::i()->getTemplate('store')->giftCard( $formTemplate );
\IPS\Output::i()->title = \IPS\Member::loggedIn()->language()->addToStack('buy_gift_voucher');
\IPS\Output::i()->breadcrumb[] = array( NULL, \IPS\Member::loggedIn()->language()->addToStack('buy_gift_voucher') );
Output::i()->output = Theme::i()->getTemplate('store')->giftCard( $formTemplate );
Output::i()->title = Member::loggedIn()->language()->addToStack('buy_gift_voucher');
Output::i()->breadcrumb[] = array( NULL, Member::loggedIn()->language()->addToStack('buy_gift_voucher') );
}
/**
@@ -132,9 +154,9 @@ class _gifts extends \IPS\Dispatcher\Controller
*
* @return void
*/
protected function formatCurrency()
protected function formatCurrency() : void
{
\IPS\Output::i()->json( (string) new \IPS\nexus\Money( \IPS\Request::i()->amount, ( ( isset( \IPS\Request::i()->cookie['currency'] ) and \in_array( \IPS\Request::i()->cookie['currency'], \IPS\nexus\Money::currencies() ) ) ? \IPS\Request::i()->cookie['currency'] : \IPS\nexus\Customer::loggedIn()->defaultCurrency() ) ) );
Output::i()->json( (string) new Money( Request::i()->amount, ( ( isset( Request::i()->cookie['currency'] ) and in_array( Request::i()->cookie['currency'], Money::currencies() ) ) ? Request::i()->cookie['currency'] : Customer::loggedIn()->defaultCurrency() ) ) );
}
/**
@@ -142,53 +164,53 @@ class _gifts extends \IPS\Dispatcher\Controller
*
* @return void
*/
protected function redeem()
protected function redeem() : void
{
if ( !\IPS\nexus\Customer::loggedIn()->member_id )
if ( !Customer::loggedIn()->member_id )
{
\IPS\Output::i()->error( 'redeem_must_be_logged_in', '1X213/1', 403, '' );
Output::i()->error( 'redeem_must_be_logged_in', '1X213/1', 403, '' );
}
$form = new \IPS\Helpers\Form( 'form', 'redeem_gift_voucher' );
$form->add( new \IPS\Helpers\Form\Text( 'redemption_code', isset( \IPS\Request::i()->code ) ? \IPS\Request::i()->code : NULL, TRUE, array(), function( $val )
$form = new Form( 'form', 'redeem_gift_voucher' );
$form->add( new Text( 'redemption_code', isset( Request::i()->code ) ? Request::i()->code : NULL, TRUE, array(), function( $val )
{
try
{
\IPS\nexus\extensions\nexus\Item\GiftVoucher::getPurchase( $val );
GiftVoucher::getPurchase( $val );
}
catch ( \InvalidArgumentException $e )
catch ( InvalidArgumentException )
{
throw new \DomainException('redeem_gift_voucher_error');
throw new DomainException('redeem_gift_voucher_error');
}
} ) );
if ( $values = $form->values() )
{
$purchase = \IPS\nexus\extensions\nexus\Item\GiftVoucher::getPurchase( $values['redemption_code'] );
$purchase = GiftVoucher::getPurchase( $values['redemption_code'] );
$extra = $purchase->extra;
$currency = isset( $extra['currency'] ) ? $extra['currency'] : \IPS\nexus\Customer::loggedIn()->defaultCurrency();
$currency = isset( $extra['currency'] ) ? $extra['currency'] : Customer::loggedIn()->defaultCurrency();
$credits = \IPS\nexus\Customer::loggedIn()->cm_credits;
$credits[ $currency ]->amount = $credits[ $currency ]->amount->add( new \IPS\Math\Number( number_format( $extra['amount'], \IPS\nexus\Money::numberOfDecimalsForCurrency( $currency ), '.', '' ) ) );
\IPS\nexus\Customer::loggedIn()->cm_credits = $credits;
\IPS\nexus\Customer::loggedIn()->save();
$credits = Customer::loggedIn()->cm_credits;
$credits[ $currency ]->amount = $credits[ $currency ]->amount->add( new Number( number_format( $extra['amount'], Money::numberOfDecimalsForCurrency( $currency ), '.', '' ) ) );
Customer::loggedIn()->cm_credits = $credits;
Customer::loggedIn()->save();
\IPS\nexus\Customer::loggedIn()->log( 'giftvoucher', array( 'type' => 'redeemed', 'code' => $values['redemption_code'], 'amount' => $extra['amount'], 'currency' => $extra['currency'], 'ps_member' => $purchase->member->member_id, 'newCreditAmount' => $credits[ $currency ]->amount ) );
$purchase->member->log( 'giftvoucher', array( 'type' => 'used', 'code' => $values['redemption_code'], 'amount' => $extra['amount'], 'currency' => $extra['currency'], 'by' => \IPS\nexus\Customer::loggedIn()->member_id ) );
Customer::loggedIn()->log( 'giftvoucher', array( 'type' => 'redeemed', 'code' => $values['redemption_code'], 'amount' => $extra['amount'], 'currency' => $extra['currency'], 'ps_member' => $purchase->member->member_id, 'newCreditAmount' => $credits[ $currency ]->amount ) );
$purchase->member->log( 'giftvoucher', array( 'type' => 'used', 'code' => $values['redemption_code'], 'amount' => $extra['amount'], 'currency' => $extra['currency'], 'by' => Customer::loggedIn()->member_id ) );
$purchase->delete();
\IPS\Output::i()->redirect( \IPS\Http\Url::internal( "app=nexus&module=store&controller=store&currency={$currency}", 'front', 'store' ) );
Output::i()->redirect( Url::internal( "app=nexus&module=store&controller=store&currency={$currency}", 'front', 'store' ) );
}
if( \IPS\Request::i()->isAjax() )
if( Request::i()->isAjax() )
{
$form->class = 'ipsForm_vertical';
\IPS\Output::i()->output = $form->customTemplate( array( \IPS\Theme::i()->getTemplate( 'forms', 'core', 'front' ), 'popupTemplate' ) );
$form->class = 'ipsForm--vertical ipsForm--redeem-voucher';
Output::i()->output = $form->customTemplate( array( Theme::i()->getTemplate( 'forms', 'core', 'front' ), 'popupTemplate' ) );
}
else
{
\IPS\Output::i()->title = \IPS\Member::loggedIn()->language()->addToStack( 'redemption_code' );
\IPS\Output::i()->output = $form;
Output::i()->title = Member::loggedIn()->language()->addToStack( 'redemption_code' );
Output::i()->output = $form;
}
}
}