Version 4.1.19

This commit is contained in:
Neo committed 2025-12-19 05:38:56 -08:00
1 parent 28bd025b35
commit 2bd5025018
2674 files changed
+233518 -77766

No files matched your search

@@ -2,9 +2,9 @@
/**
* @brief View product
* @author <a href='http://www.invisionpower.com'>Invision Power Services, Inc.</a>
* @copyright (c) 2001 - SVN_YYYY Invision Power Services, Inc.
* @copyright (c) 2001 - 2016 Invision Power Services, Inc.
* @license http://www.invisionpower.com/legal/standards/
* @package IPS Social Suite
* @package IPS Community Suite
* @subpackage Nexus
* @since 29 Apr 2014
* @version SVN_VERSION_NUMBER
@@ -59,7 +59,7 @@ class _product extends \IPS\Content\Controller
{
$_SESSION['cart'] = array();
}
$memberCurrency = ( isset( $_SESSION['currency'] ) ? $_SESSION['currency'] : \IPS\nexus\Customer::loggedIn()->defaultCurrency() );
$memberCurrency = ( ( isset( $_SESSION['currency'] ) and in_array( $_SESSION['currency'], \IPS\nexus\Money::currencies() ) ) ? $_SESSION['currency'] : \IPS\nexus\Customer::loggedIn()->defaultCurrency() );
/* Load Package */
$item = parent::manage();
@@ -68,21 +68,31 @@ class _product extends \IPS\Content\Controller
\IPS\Output::i()->error( 'node_error', '2X240/1', 404, '' );
}
$package = \IPS\nexus\Package::load( $item->id );
/* Do we have any in the cart already (this will affect stock level)? */
$inCart = 0;
$inCart = array();
foreach ( $_SESSION['cart'] as $itemInCart )
{
if ( $itemInCart->id === $package->id )
{
$inCart += $itemInCart->quantity;
$optionValues = array();
foreach( $package->optionIdKeys() as $id )
{
$optionValues[ $id ] = $itemInCart->details[$id];
}
$optionValues = json_encode( $optionValues );
if ( !isset( $inCart[ $optionValues ] ) )
{
$inCart[ $optionValues ] = 0;
}
$inCart[ $optionValues ] += $itemInCart->quantity;
}
}
/* Showing just the form to purchase, or full product page? */
if ( \IPS\Request::i()->purchase )
{
\IPS\Output::i()->output = \IPS\Theme::i()->getTemplate('store')->purchaseForm( $package, $item, $this->_getForm( $package, $inCart, TRUE ), $inCart );
\IPS\Output::i()->output = \IPS\Theme::i()->getTemplate('store')->purchaseForm( $package, $item, $this->_getForm( $package, $inCart, TRUE ) );
}
/* No - show the full page */
@@ -140,7 +150,7 @@ class _product extends \IPS\Content\Controller
}
/* Easypost */
if ( \IPS\Settings::i()->easypost_api_key and \IPS\Settings::i()->easypost_show_rates and ( $package->shipping == '*' or in_array( 'easypost', explode( ',', $package->shipping ) ) ) )
if ( $shipAddress and \IPS\Settings::i()->easypost_api_key and \IPS\Settings::i()->easypost_show_rates and ( $package->shipping == '*' or in_array( 'easypost', explode( ',', $package->shipping ) ) ) )
{
try
{
@@ -176,12 +186,20 @@ class _product extends \IPS\Content\Controller
if ( count( $renewOptions ) === 1 )
{
$renewalTerm = array_pop( $renewOptions );
$renewalTerm = new \IPS\nexus\Purchase\RenewalTerm( new \IPS\nexus\Money( $renewalTerm['cost'][ $memberCurrency ]['amount'], $memberCurrency ), new \DateInterval( 'P' . $renewalTerm['term'] . mb_strtoupper( $renewalTerm['unit'] ) ), isset( $this->tax ) ? \IPS\nexus\Tax::load( $this->tax ) : NULL, $renewalTerm['add'] );
$renewalTerm = new \IPS\nexus\Purchase\RenewalTerm( new \IPS\nexus\Money( $renewalTerm['cost'][ $memberCurrency ]['amount'], $memberCurrency ), new \DateInterval( 'P' . $renewalTerm['term'] . mb_strtoupper( $renewalTerm['unit'] ) ), $package->tax ? \IPS\nexus\Tax::load( $package->tax ) : NULL, $renewalTerm['add'] );
}
}
/* Display */
\IPS\Output::i()->output = \IPS\Theme::i()->getTemplate('store')->package( $package, $item, $this->_getForm( $package, $inCart, TRUE ), $inCart, $shippingMethods, $itemDataForShipping, $locationType, $renewalTerm );
$formKey = "package_{$package->id}_submitted";
if ( \IPS\Request::i()->isAjax() and isset( \IPS\Request::i()->$formKey ) )
{
\IPS\Output::i()->sendOutput( $this->_getForm( $package, $inCart, TRUE ), 500 );
}
else
{
\IPS\Output::i()->output = \IPS\Theme::i()->getTemplate('store')->package( $package, $item, $this->_getForm( $package, $inCart, TRUE ), array_sum( $inCart ), $shippingMethods, $itemDataForShipping, $locationType, $renewalTerm );
}
}
}
@@ -189,13 +207,13 @@ class _product extends \IPS\Content\Controller
* Get form
*
* @param \IPS\nexus\Package $package The package
* @param int $inCart The number in the cart already
* @param array $inCart The number in the cart already for each of the field combinations
* @return string
*/
protected function _getForm( \IPS\nexus\Package $package, $inCart, $verticalForm = FALSE )
{
/* Get member's currency */
$memberCurrency = ( isset( $_SESSION['currency'] ) ? $_SESSION['currency'] : \IPS\nexus\Customer::loggedIn()->defaultCurrency() );
$memberCurrency = ( ( isset( $_SESSION['currency'] ) and in_array( $_SESSION['currency'], \IPS\nexus\Money::currencies() ) ) ? $_SESSION['currency'] : \IPS\nexus\Customer::loggedIn()->defaultCurrency() );
/* Init form */
$form = new \IPS\Helpers\Form( "package_{$package->id}", 'add_to_cart' );
@@ -209,7 +227,7 @@ class _product extends \IPS\Content\Controller
$package->storeForm( $form, $memberCurrency );
/* Are we in stock? */
if ( $package->stock != -1 and $package->stock != -2 and ( $package->stock - $inCart ) <= 0 )
if ( $package->stock != -1 and $package->stock != -2 and ( $package->stock - array_sum( $inCart ) ) <= 0 )
{
$form->actionButtons = array();
$form->addButton( 'out_of_stock', 'submit', NULL, 'ipsButton ipsButton_primary', array( 'disabled' => 'disabled' ) );
@@ -245,9 +263,9 @@ class _product extends \IPS\Content\Controller
{
$customFields = \IPS\nexus\Package\CustomField::roots();
$stickyFields = array();
foreach ( $item->details as $k => $v )
foreach ( $item->details as $j => $v )
{
if ( $v and isset( $customFields[ $k ] ) and $customFields[ $k ]->sticky )
if ( $v and isset( $customFields[ $j ] ) and $customFields[ $j ]->sticky )
{
$stickyFields[] = $v;
}
@@ -317,24 +335,50 @@ class _product extends \IPS\Content\Controller
$customFields = \IPS\nexus\Package\CustomField::roots( NULL, NULL, array( array( 'cf_purchase=1' ), array( \IPS\Db::i()->findInSet( 'cf_packages', array( $package->id ) ) ) ) );
foreach ( $customFields as $field )
{
if( \IPS\Request::i()->isAjax() && isset( \IPS\Request::i()->stockCheck ) )
{
$field->required = FALSE; // Otherwise a required text field (for example) can block the price changing when a different radio (for example) value is selected
}
$form->add( $field->buildHelper() );
}
/* Is this the validation for the additional page? */
if ( \IPS\Request::i()->isAjax() and isset( \IPS\Request::i()->additionalPageCheck ) )
{
if ( $additionalPage = $package->storeAdditionalPage( $_POST ) )
{
\IPS\Output::i()->httpHeaders['X-IPS-FormNoSubmit'] = "true";
\IPS\Output::i()->json( $additionalPage );
}
}
/* Handle submissions */
if ( $values = $form->values() )
{
/* Custom fields */
$details = array();
foreach ( $values as $k => $v )
$editorUploadIds = array();
foreach ( $customFields as $field )
{
if ( mb_substr( $k, 0, 13 ) == 'nexus_pfield_' )
if ( isset( $values[ 'nexus_pfield_' . $field->id ] ) )
{
$class = \IPS\nexus\Package\CustomField::load( mb_substr( $k, 13 ) )->buildHelper();
$details[ mb_substr( $k, 13 ) ] = $class::stringValue( $v );
$class = $field->buildHelper();
$details[ $field->id ] = $class::stringValue( $values[ 'nexus_pfield_' . $field->id ] );
if ( !isset( \IPS\Request::i()->stockCheck ) and $field->type === 'Editor' )
{
$uploadId = \IPS\Db::i()->insert( 'nexus_cart_uploads', array(
'session_id' => \IPS\Session::i()->id,
'time' => time()
) );
$field->claimAttachments( $uploadId, 'cart' );
$editorUploadIds[] = $uploadId;
}
}
}
$optionValues = $package->optionValues( $details );
/* Stock check */
$quantity = isset( $values['quantity'] ) ? $values['quantity'] : 1;
try
@@ -345,7 +389,8 @@ class _product extends \IPS\Content\Controller
{
\IPS\Output::i()->error( 'product_options_price_error', '3X240/2', 500, 'product_options_price_error_admin' );
}
$inCartForThisFieldCombination = isset( $inCart[ json_encode( $optionValues ) ] ) ? $inCart[ json_encode( $optionValues ) ] : 0;
if ( \IPS\Request::i()->isAjax() && isset( \IPS\Request::i()->stockCheck ) )
{
/* Stock */
@@ -357,10 +402,10 @@ class _product extends \IPS\Content\Controller
);
}
else
{
{
$return = array(
'stock' => \IPS\Member::loggedIn()->language()->addToStack( 'x_in_stock', FALSE, array( 'pluralize' => array( $data['stock'] - $inCart ) ) ),
'okay' => true
'stock' => \IPS\Member::loggedIn()->language()->addToStack( 'x_in_stock', FALSE, array( 'pluralize' => array( $data['stock'] - $inCartForThisFieldCombination ) ) ),
'okay' => ( $data['stock'] - $inCartForThisFieldCombination > 0 ) ? true : false,
);
}
@@ -376,11 +421,11 @@ class _product extends \IPS\Content\Controller
if ( $term['add'] )
{
$data['price']->amount += $term['cost'][ $memberCurrency ]['amount'];
$normalPrice->amount += $term['cost'][ $memberCurrency ]['amount'];
$data['price']->amount = $data['price']->amount->add( new \IPS\Math\Number( number_format( $term['cost'][ $memberCurrency ]['amount'], \IPS\nexus\Money::numberOfDecimalsForCurrency( $memberCurrency ), '.', '' ) ) );
$normalPrice->amount = $normalPrice->amount->add( new \IPS\Math\Number( number_format( $term['cost'][ $memberCurrency ]['amount'], \IPS\nexus\Money::numberOfDecimalsForCurrency( $memberCurrency ), '.', '' ) ) );
}
$return['renewal'] = \IPS\Member::loggedIn()->language()->addToStack( 'and_renewal', FALSE, array( 'sprintf' => array( new \IPS\nexus\Purchase\RenewalTerm( new \IPS\nexus\Money( $term['cost'][ $memberCurrency ]['amount'] + $_data['renewalAdjustment'], $memberCurrency ), new \DateInterval( 'P' . $term['term'] . mb_strtoupper( $term['unit'] ) ), isset( $this->tax ) ? \IPS\nexus\Tax::load( $this->tax ) : NULL, $term['add'] ) ) ) );
$return['renewal'] = \IPS\Member::loggedIn()->language()->addToStack( 'and_renewal', FALSE, array( 'sprintf' => array( new \IPS\nexus\Purchase\RenewalTerm( new \IPS\nexus\Money( ( new \IPS\Math\Number( number_format( $term['cost'][ $memberCurrency ]['amount'], \IPS\nexus\Money::numberOfDecimalsForCurrency( $memberCurrency ), '.', '' ) ) )->add( ( new \IPS\Math\Number( number_format( $_data['renewalAdjustment'], \IPS\nexus\Money::numberOfDecimalsForCurrency( $memberCurrency ), '.', '' ) ) ) ), $memberCurrency ), new \DateInterval( 'P' . $term['term'] . mb_strtoupper( $term['unit'] ) ), $package->tax ? \IPS\nexus\Tax::load( $package->tax ) : NULL, $term['add'] ) ) ) );
}
else
{
@@ -392,16 +437,16 @@ class _product extends \IPS\Content\Controller
{
try
{
$taxRate = \IPS\nexus\Tax::load( $package->tax )->rate( \IPS\nexus\Customer::loggedIn()->estimatedLocation() );
$taxRate = new \IPS\Math\Number( \IPS\nexus\Tax::load( $package->tax )->rate( \IPS\nexus\Customer::loggedIn()->estimatedLocation() ) );
$data['price']->amount += ( $data['price']->amount * $taxRate );
$normalPrice->amount += ( $normalPrice->amount * $taxRate );
$data['price']->amount = $data['price']->amount->add( $data['price']->amount->multiply( $taxRate ) );
$normalPrice->amount = $normalPrice->amount->add( $normalPrice->amount->multiply( $taxRate ) );
}
catch ( \OutOfRangeException $e ) { }
}
/* Format and return */
if ( $data['price']->amount != $normalPrice->amount )
if ( $data['price']->amount->compare( $normalPrice->amount ) !== 0 )
{
$return['price'] = \IPS\Theme::i()->getTemplate( 'store', 'nexus' )->priceDiscounted( $normalPrice, $data['price'], FALSE );
}
@@ -411,11 +456,23 @@ class _product extends \IPS\Content\Controller
}
\IPS\Output::i()->json( $return );
}
elseif ( $data['stock'] != -1 and ( $data['stock'] - $inCart ) < $quantity )
elseif ( $data['stock'] != -1 and ( $data['stock'] - $inCartForThisFieldCombination ) < $quantity )
{
$form->error = \IPS\Member::loggedIn()->language()->addToStack( 'not_enough_in_stock', FALSE, array( 'sprintf' => array( $data['stock'] - $inCart ) ) );
$form->error = \IPS\Member::loggedIn()->language()->addToStack( 'not_enough_in_stock', FALSE, array( 'sprintf' => array( $data['stock'] - $inCartForThisFieldCombination ) ) );
return (string) $form;
}
if ( ( !isset( \IPS\Request::i()->additionalPageCheck ) or !\IPS\Request::i()->isAjax() ) and $additionalPage = $package->storeAdditionalPage( $_POST ) )
{
if ( \IPS\Request::i()->isAjax() )
{
\IPS\Output::i()->json( $additionalPage );
}
else
{
return $additionalPage;
}
}
/* Work out renewal term */
$renewalTerm = NULL;
@@ -430,7 +487,7 @@ class _product extends \IPS\Content\Controller
$chosenRenewOption = $renewOptions[ $values['renewal_term'] ];
}
$renewalTerm = new \IPS\nexus\Purchase\RenewalTerm( new \IPS\nexus\Money( $chosenRenewOption['cost'][ $memberCurrency ]['amount'] + $data['renewalAdjustment'], $memberCurrency ), new \DateInterval( 'P' . $chosenRenewOption['term'] . mb_strtoupper( $chosenRenewOption['unit'] ) ), isset( $this->tax ) ? \IPS\nexus\Tax::load( $this->tax ) : NULL, $chosenRenewOption['add'], $package->grace_period ? new \DateInterval( 'P' . $package->grace_period . 'D' ) : NULL );
$renewalTerm = new \IPS\nexus\Purchase\RenewalTerm( new \IPS\nexus\Money( ( new \IPS\Math\Number( number_format( $chosenRenewOption['cost'][ $memberCurrency ]['amount'], 2, '.', '' ) ) )->add( new \IPS\Math\Number( number_format( $data['renewalAdjustment'], 2, '.', '' ) ) ), $memberCurrency ), new \DateInterval( 'P' . $chosenRenewOption['term'] . mb_strtoupper( $chosenRenewOption['unit'] ) ), $package->tax ? \IPS\nexus\Tax::load( $package->tax ) : NULL, $chosenRenewOption['add'], $package->grace_period ? new \DateInterval( 'P' . $package->grace_period . 'D' ) : NULL );
}
/* Associations */
@@ -448,8 +505,9 @@ class _product extends \IPS\Content\Controller
}
}
/* Blah */
$package->addItemsToCartData( $details, $quantity, $renewalTerm, $parent, $values );
/* Actually add to cart */
$cartId = $package->addItemsToCartData( $details, $quantity, $renewalTerm, $parent, $values );
\IPS\Db::i()->update( 'nexus_cart_uploads', array( 'item_id' => $cartId ), \IPS\Db::i()->in( 'id', $editorUploadIds ) );
/* Redirect or AJAX */
if ( \IPS\Request::i()->isAjax() )
@@ -458,7 +516,7 @@ class _product extends \IPS\Content\Controller
$upsell = \IPS\nexus\Package\Item::getItemsWithPermission( array( array( 'p_upsell=1' ), array( \IPS\Db::i()->findInSet( 'p_associable', array( $package->id ) ) ) ), 'p_position' );
/* Send */
\IPS\Output::i()->sendOutput( \IPS\Theme::i()->getTemplate('store')->cartReview( $package, $quantity, $upsell ), 200, 'text/html' );
\IPS\Output::i()->json( array( 'dialog' => \IPS\Theme::i()->getTemplate('store')->cartReview( $package, $quantity, $upsell ), 'cart' => \IPS\Theme::i()->getTemplate('store')->cartHeader() ) );
}
else
{