Version 4.5.0
This commit is contained in:
1 parent
1a0c7fd3c2
commit
f79dcf067a
3791 files changed
+248032
-76372
No files matched your search
@@ -12,7 +12,7 @@
|
||||
namespace IPS\nexus;
|
||||
|
||||
/* 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;
|
||||
@@ -67,7 +67,7 @@ class _Customer extends \IPS\Member
|
||||
$where = array( array( 'core_members.' . $idField . '=?', $id ) );
|
||||
if( $extraWhereClause !== NULL )
|
||||
{
|
||||
if ( !is_array( $extraWhereClause ) or !is_array( $extraWhereClause[0] ) )
|
||||
if ( !\is_array( $extraWhereClause ) or !\is_array( $extraWhereClause[0] ) )
|
||||
{
|
||||
$extraWhereClause = array( $extraWhereClause );
|
||||
}
|
||||
@@ -93,7 +93,7 @@ class _Customer extends \IPS\Member
|
||||
/* Guests */
|
||||
if( $id === NULL OR $id === 0 OR $id === '' )
|
||||
{
|
||||
$classname = get_called_class();
|
||||
$classname = \get_called_class();
|
||||
return new $classname;
|
||||
}
|
||||
|
||||
@@ -104,7 +104,7 @@ class _Customer extends \IPS\Member
|
||||
}
|
||||
|
||||
/* If we did, check it's valid */
|
||||
elseif( !in_array( $idField, static::$databaseIdFields ) )
|
||||
elseif( !\in_array( $idField, static::$databaseIdFields ) )
|
||||
{
|
||||
throw new \InvalidArgumentException;
|
||||
}
|
||||
@@ -256,7 +256,7 @@ class _Customer extends \IPS\Member
|
||||
{
|
||||
foreach ( $currencies as $k => $v )
|
||||
{
|
||||
if ( in_array( $this->language()->id, $v ) )
|
||||
if ( \in_array( $this->language()->id, $v ) )
|
||||
{
|
||||
return $k;
|
||||
}
|
||||
@@ -270,6 +270,33 @@ class _Customer extends \IPS\Member
|
||||
return \IPS\Settings::i()->nexus_currency;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Cache primary billing address in case the method is called multiple times
|
||||
*/
|
||||
protected $primaryBillingAddress = FALSE;
|
||||
|
||||
/**
|
||||
* Get primary billing address, if one exists
|
||||
*
|
||||
* @return \IPS\GeoLocation|NULL
|
||||
*/
|
||||
public function primaryBillingAddress()
|
||||
{
|
||||
if( $this->primaryBillingAddress === FALSE )
|
||||
{
|
||||
try
|
||||
{
|
||||
$this->primaryBillingAddress = \IPS\GeoLocation::buildFromJson( \IPS\Db::i()->select( 'address', 'nexus_customer_addresses', array( '`member`=? AND primary_billing=1', $this->member_id ) )->first() );
|
||||
}
|
||||
catch ( \UnderflowException $e )
|
||||
{
|
||||
$this->primaryBillingAddress = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
return $this->primaryBillingAddress;
|
||||
}
|
||||
|
||||
/**
|
||||
* Estimated location
|
||||
@@ -280,47 +307,48 @@ class _Customer extends \IPS\Member
|
||||
{
|
||||
if ( $this->member_id === \IPS\nexus\Customer::loggedIn()->member_id )
|
||||
{
|
||||
if ( isset( $_SESSION['location'] ) )
|
||||
if ( isset( \IPS\Request::i()->cookie['location'] ) AND ( !\IPS\Request::i()->cookie['location'] OR ( \IPS\Request::i()->cookie['location'] AND $data = json_decode( \IPS\Request::i()->cookie['location'], true ) AND isset( $data['member_id'] ) AND $data['member_id'] == $this->member_id ) ) )
|
||||
{
|
||||
$location = $_SESSION['location'] ? \IPS\GeoLocation::buildFromJson( $_SESSION['location'] ) : NULL;
|
||||
$location = NULL;
|
||||
|
||||
if( \IPS\Request::i()->cookie['location'] AND $data = json_decode( \IPS\Request::i()->cookie['location'], true ) AND $data['member_id'] == $this->member_id )
|
||||
{
|
||||
$location = \IPS\GeoLocation::buildFromJson( \IPS\Request::i()->cookie['location'] );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
try
|
||||
{
|
||||
$defaultAddress = \IPS\Db::i()->select( 'address', 'nexus_customer_addresses', array( 'member=? AND primary_billing=1', $this->member_id ) )->first();
|
||||
$_SESSION['location'] = $defaultAddress;
|
||||
$location = \IPS\GeoLocation::buildFromJson( $defaultAddress );
|
||||
}
|
||||
catch ( \UnderflowException $e )
|
||||
$location = $this->primaryBillingAddress();
|
||||
if ( !$location )
|
||||
{
|
||||
try
|
||||
{
|
||||
$location = \IPS\GeoLocation::getByIp( \IPS\Request::i()->ipAddress() );
|
||||
$_SESSION['location'] = json_encode( $location );
|
||||
\IPS\Request::i()->setCookie( 'location', $this->_addMemberToAddress( $location ) );
|
||||
}
|
||||
catch ( \Exception $e )
|
||||
{
|
||||
if( isset( $_SERVER['HTTP_ACCEPT_LANGUAGE'] ) )
|
||||
{
|
||||
$exploded = explode( '-', $_SERVER['HTTP_ACCEPT_LANGUAGE'] );
|
||||
|
||||
if ( in_array( mb_strtoupper( $exploded[1] ), \IPS\GeoLocation::$countries ) )
|
||||
$languages = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE'] );
|
||||
$exploded = explode( '-', array_shift( $languages ) );
|
||||
|
||||
if ( \in_array( mb_strtoupper( $exploded[1] ), \IPS\GeoLocation::$countries ) )
|
||||
{
|
||||
$location = new \IPS\GeoLocation;
|
||||
$location->country = mb_strtoupper( $exploded[1] );
|
||||
$_SESSION['location'] = json_encode( $location );
|
||||
\IPS\Request::i()->setCookie( 'location', $this->_addMemberToAddress( $location ) );
|
||||
}
|
||||
else
|
||||
{
|
||||
$location = NULL;
|
||||
$_SESSION['location'] = '';
|
||||
\IPS\Request::i()->setCookie( 'location', '' );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$location = NULL;
|
||||
$_SESSION['location'] = '';
|
||||
\IPS\Request::i()->setCookie( 'location', '' );
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -328,18 +356,22 @@ class _Customer extends \IPS\Member
|
||||
}
|
||||
else
|
||||
{
|
||||
try
|
||||
{
|
||||
$location = \IPS\GeoLocation::buildFromJson( \IPS\Db::i()->select( 'address', 'nexus_customer_addresses', array( 'member=? AND primary_billing=1', $this->member_id ) )->first() );
|
||||
}
|
||||
catch ( \UnderflowException $e )
|
||||
{
|
||||
$location = NULL;
|
||||
}
|
||||
$location = $this->primaryBillingAddress();
|
||||
}
|
||||
|
||||
return $location;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the member ID to the address object so we can validate it on subsequent requests
|
||||
*
|
||||
* @param \IPS\GeoLocation $location Location object
|
||||
* @return string
|
||||
*/
|
||||
protected function _addMemberToAddress( $location )
|
||||
{
|
||||
return json_encode( array_merge( json_decode( json_encode( $location ), true ), array( 'member_id' => $this->member_id ) ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Save Changed Columns
|
||||
@@ -353,7 +385,7 @@ class _Customer extends \IPS\Member
|
||||
$customerTable = array();
|
||||
foreach ( ( $this->_new ? $this->_data : $this->changed ) as $k => $v )
|
||||
{
|
||||
if ( ( mb_substr( $k, 0, 3 ) === 'cm_' and !in_array( $k, array( 'cm_credits', 'cm_no_sev', 'cm_return_group', 'cm_reg' ) ) ) or mb_substr( $k, 0, 6 ) === 'field_' )
|
||||
if ( ( mb_substr( $k, 0, 3 ) === 'cm_' and !\in_array( $k, array( 'cm_credits', 'cm_no_sev', 'cm_return_group', 'cm_reg' ) ) ) or mb_substr( $k, 0, 6 ) === 'field_' )
|
||||
{
|
||||
$customerTable[ $k ] = $v;
|
||||
unset( $this->_data[ $k ] );
|
||||
@@ -365,7 +397,7 @@ class _Customer extends \IPS\Member
|
||||
$data['member_id'] = $this->_data['member_id'];
|
||||
$this->_data = $data;
|
||||
|
||||
if ( count( $customerTable ) )
|
||||
if ( \count( $customerTable ) )
|
||||
{
|
||||
$customerTable['member_id'] = $this->member_id;
|
||||
\IPS\Db::i()->insert( 'nexus_customers', $customerTable, TRUE );
|
||||
@@ -397,7 +429,7 @@ class _Customer extends \IPS\Member
|
||||
{
|
||||
$return[] = (string) new \IPS\nexus\Money( $amount['amount'], $amount['t_currency'] );
|
||||
}
|
||||
return count( $return ) ? implode( ' + ', $return ) : new \IPS\nexus\Money( 0, $this->defaultCurrency() );
|
||||
return \count( $return ) ? implode( ' + ', $return ) : new \IPS\nexus\Money( 0, $this->defaultCurrency() );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -437,28 +469,28 @@ class _Customer extends \IPS\Member
|
||||
}
|
||||
|
||||
$return[] = 'addresses';
|
||||
if ( count( \IPS\nexus\Gateway::cardStorageGateways() ) )
|
||||
if ( \count( \IPS\nexus\Gateway::cardStorageGateways() ) or \count( \IPS\nexus\Gateway::otherStoredPaymentMethodGateways() ) )
|
||||
{
|
||||
$return[] = 'cards';
|
||||
}
|
||||
if ( count( \IPS\nexus\Customer\CustomField::roots() ) )
|
||||
if ( \count( \IPS\nexus\Customer\CustomField::roots() ) )
|
||||
{
|
||||
$return[] = 'info';
|
||||
}
|
||||
|
||||
if ( \IPS\Settings::i()->nexus_min_topup or count( json_decode( \IPS\Settings::i()->nexus_payout, TRUE ) ) )
|
||||
if ( \IPS\Settings::i()->nexus_min_topup or \count( json_decode( \IPS\Settings::i()->nexus_payout, TRUE ) ) )
|
||||
{
|
||||
$return[] = 'credit';
|
||||
}
|
||||
|
||||
$return[] = 'alternatives';
|
||||
|
||||
if ( count( \IPS\nexus\Donation\Goal::roots() ) )
|
||||
if ( \count( \IPS\nexus\Donation\Goal::roots() ) )
|
||||
{
|
||||
$return[] = 'donations';
|
||||
}
|
||||
|
||||
if ( \IPS\Settings::i()->cm_ref_on )
|
||||
if ( \IPS\Settings::i()->ref_on )
|
||||
{
|
||||
$return[] = 'referrals';
|
||||
}
|
||||
@@ -474,7 +506,7 @@ class _Customer extends \IPS\Member
|
||||
*/
|
||||
public function alternativeContacts( $where = array() )
|
||||
{
|
||||
$where = count( $where ) ? array( $where ) : $where;
|
||||
$where = \count( $where ) ? array( $where ) : $where;
|
||||
$where[] = array( 'main_id=?', $this->member_id );
|
||||
return new \IPS\Patterns\ActiveRecordIterator( \IPS\Db::i()->select( '*', 'nexus_alternate_contacts', $where )->setKeyField( 'alt_id' ), 'IPS\nexus\Customer\AlternativeContact' );
|
||||
}
|
||||
@@ -487,7 +519,7 @@ class _Customer extends \IPS\Member
|
||||
*/
|
||||
public function parentContacts( $where = array() )
|
||||
{
|
||||
$where = count( $where ) ? array( $where ) : $where;
|
||||
$where = \count( $where ) ? array( $where ) : $where;
|
||||
$where[] = array( 'alt_id=?', $this->member_id );
|
||||
return new \IPS\Patterns\ActiveRecordIterator( \IPS\Db::i()->select( '*', 'nexus_alternate_contacts', $where )->setKeyField( 'main_id' ), 'IPS\nexus\Customer\AlternativeContact' );
|
||||
}
|
||||
@@ -500,5 +532,50 @@ class _Customer extends \IPS\Member
|
||||
public function acpUrl()
|
||||
{
|
||||
return parent::acpUrl()->setQueryString( 'tab', 'nexus_Main' );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* ACP Customer Page URL
|
||||
*
|
||||
* @param \IPS\Math\Number $amount Adjustment amount
|
||||
* @param string $currency Currency code
|
||||
* @param bool $refund Should the spend be reduced?
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function updateSpend( \IPS\Math\Number $amount, $currency, $refund=false )
|
||||
{
|
||||
try
|
||||
{
|
||||
$currentSpend = \IPS\Db::i()->select( 'spend_amount', 'nexus_customer_spend', array( "spend_member_id=? AND spend_currency=?", $this->member_id, $currency ) )->first();
|
||||
$newSpend = new \IPS\Math\Number( number_format( $currentSpend, \IPS\nexus\Money::numberOfDecimalsForCurrency( $currency ), '.', '' ) );
|
||||
$newSpend = ( $refund ) ? $newSpend->subtract( $amount ) : $newSpend->add( $amount );
|
||||
\IPS\Db::i()->replace( 'nexus_customer_spend', array( 'spend_member_id' => $this->member_id, 'spend_amount' => $newSpend, 'spend_currency' => $currency ) );
|
||||
}
|
||||
catch ( \UnderflowException $e )
|
||||
{
|
||||
\IPS\Db::i()->insert( 'nexus_customer_spend', array( 'spend_member_id' => $this->member_id, 'spend_amount' => ( $refund ) ? $amount->multiply( new \IPS\Math\Number( "-1" ) ) : $amount, 'spend_currency' => $currency ) );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Recalculate total amount spent in all currencies
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function recountTotalSpend()
|
||||
{
|
||||
\IPS\Db::i()->delete( 'nexus_customer_spend', array( 'spend_member_id=?', $this->member_id ) );
|
||||
|
||||
$spend = array();
|
||||
foreach ( \IPS\Db::i()->select( 't_currency, ( SUM(t_amount)-SUM(t_partial_refund) ) AS amount', 'nexus_transactions', array( 't_member=? AND ( t_status=? OR t_status=? )', $this->member_id, \IPS\nexus\Transaction::STATUS_PAID, \IPS\nexus\Transaction::STATUS_PART_REFUNDED ), NULL, NULL, 't_currency' ) as $amount )
|
||||
{
|
||||
$spend[] = array( 'spend_member_id' => $this->member_id, 'spend_amount' => $amount['amount'], 'spend_currency' => $amount['t_currency'] );
|
||||
}
|
||||
|
||||
if( \count( $spend ) )
|
||||
{
|
||||
\IPS\Db::i()->insert( 'nexus_customer_spend', $spend );
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user