Skip to content

Commit

Permalink
Ticket #3714 - Credits: Add possibility for common users to Send thei…
Browse files Browse the repository at this point in the history
…r credits.
  • Loading branch information
AntonLV committed Mar 18, 2022
1 parent ac4a115 commit 3cee082
Show file tree
Hide file tree
Showing 11 changed files with 154 additions and 49 deletions.
6 changes: 5 additions & 1 deletion modules/boonex/credits/classes/BxCreditsConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ function __construct($aModule)
// objects
'OBJECT_FORM_CREDIT' => 'bx_credits_credit',
'OBJECT_FORM_CREDIT_DISPLAY_GRANT' => 'bx_credits_credit_grant',
'OBJECT_FORM_CREDIT_DISPLAY_SEND' => 'bx_credits_credit_send',
'OBJECT_FORM_CREDIT_DISPLAY_WITHDRAW_REQUEST' => 'bx_credits_credit_withdraw_request',
'OBJECT_FORM_CREDIT_DISPLAY_WITHDRAW_CONFIRM' => 'bx_credits_credit_withdraw_confirm',
'OBJECT_FORM_BUNDLE' => 'bx_credits_bundle',
Expand All @@ -84,6 +85,7 @@ function __construct($aModule)

// email templates
'ETEMPLATE_GRANTED' => 'bx_credits_granted',
'ETEMPLATE_RECEIVED' => 'bx_credits_received',
'ETEMPLATE_PURCHASED' => 'bx_credits_purchased',
'ETEMPLATE_IN' => 'bx_credits_in',
'ETEMPLATE_OUT' => 'bx_credits_out',
Expand All @@ -94,6 +96,7 @@ function __construct($aModule)
'T' => array (
'txt_sample_single' => '_bx_credits_txt_sample_single',
'grant_popup' => '_bx_credits_grid_popup_title_htr_grant',
'send_popup' => '_bx_credits_grid_popup_title_htr_send',
'withdraw_request_popup' => '_bx_credits_grid_popup_title_htr_withdraw_request',
'withdraw_confirm_popup' => '_bx_credits_grid_popup_title_htr_withdraw_confirm',
),
Expand All @@ -118,6 +121,7 @@ function __construct($aModule)
'add_bundle_popup' => $sPrefix . '-add-bundle-popup',
'edit_bundle_popup' => $sPrefix . '-edit-bundle-popup',

'send_popup' => $sPrefix . '-send-popup',
'grant_popup' => $sPrefix . '-grant-popup',
'withdraw_request_popup' => $sPrefix . '-withdraw-request-popup',
'withdraw_confirm_popup' => $sPrefix . '-withdraw-confirm-popup',
Expand Down Expand Up @@ -224,7 +228,7 @@ public function setCheckoutData($aData)

public function getOrder($iLength = 16)
{
return genRndPwd($iLength, false);
return strtoupper(genRndPwd($iLength, false));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
*/

/**
* Withdraw form
* Credit form
*/
class BxCreditsFormWithdraw extends BxTemplFormView
class BxCreditsFormCredit extends BxTemplFormView
{
protected $_sModule;
protected $_oModule;
Expand All @@ -37,6 +37,10 @@ public function __construct($aInfo, $oTemplate = false)
$sInfo .= 'g';
break;

case $CNF['OBJECT_FORM_CREDIT_DISPLAY_SEND']:
$sInfo .= 's';
break;

case $CNF['OBJECT_FORM_CREDIT_DISPLAY_WITHDRAW_REQUEST']:
$sInfo .= 'wr';
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class BxCreditsGridHistoryAdministration extends BxTemplGrid
protected $_sModule;
protected $_oModule;

protected $_iUserId;
protected $_bWithdraw;

public function __construct ($aOptions, $oTemplate = false)
Expand All @@ -27,9 +28,11 @@ public function __construct ($aOptions, $oTemplate = false)

$this->_sDefaultSortingOrder = 'DESC';

$iProfileId = bx_get_logged_profile_id();
if($iProfileId !== false)
$this->_aQueryAppend['profile_id'] = (int)$iProfileId;
$iUserId = bx_get_logged_profile_id();
if($iUserId !== false) {
$this->_iUserId = (int)$iUserId;
$this->_aQueryAppend['user_id'] = $this->_iUserId;
}

$this->_bWithdraw = $this->_oModule->_oConfig->isWithdraw();
}
Expand All @@ -53,7 +56,7 @@ public function performActionGrant()
public function performActionWithdrawConfirm()
{
if(!$this->_bWithdraw)
return echoJson(array());
return echoJson([]);

$sAction = 'withdraw_confirm';

Expand All @@ -70,7 +73,7 @@ protected function _getActionWithdrawConfirm($sType, $sKey, $a, $isSmall = false

protected function _getCellDirection($mixedValue, $sKey, $aField, $aRow)
{
if(in_array($mixedValue, array(BX_CREDITS_DIRECTION_IN, BX_CREDITS_DIRECTION_OUT)))
if(in_array($mixedValue, [BX_CREDITS_DIRECTION_IN, BX_CREDITS_DIRECTION_OUT]))
$mixedValue = _t('_bx_credits_txt_direction_' . $mixedValue);

return parent::_getCellDefault($mixedValue, $sKey, $aField, $aRow);
Expand Down Expand Up @@ -110,7 +113,7 @@ protected function _getProfile($mixedValue)
if(!$oProfile)
return $mixedValue;

return $oProfile->getUnit(0, array('template' => array('name' => 'unit', 'size' => 'icon')));
return $oProfile->getUnit(0, ['template' => ['name' => 'unit', 'size' => 'icon']]);
}

protected function _performActionWithProfileAmount($sAction)
Expand All @@ -124,28 +127,28 @@ protected function _performActionWithProfileAmount($sAction)
$iProfile = (int)$oForm->getCleanValue('profile');
$fAmount = (float)$oForm->getCleanValue('amount');
$sMessage = $oForm->getCleanValue('message');
$aResult = $this->_oModule->{'process' . bx_gen_method_name($sAction)}($iProfile, $fAmount, $sMessage);
$aResult = $this->_oModule->{'process' . bx_gen_method_name($sAction)}($this->_iUserId, $iProfile, $fAmount, $sMessage);

if((int)$aResult['code'] == 0)
$aRes = array('msg' => _t(!empty($aResult['msg']) ? $aResult['msg'] : '_bx_credits_msg_action_performed'));
$aRes = ['msg' => _t(!empty($aResult['msg']) ? $aResult['msg'] : '_bx_credits_msg_action_performed')];
else
$aRes = array('msg' => _t(!empty($aResult['msg']) ? $aResult['msg'] : '_bx_credits_err_cannot_perform_action'));
$aRes = ['msg' => _t(!empty($aResult['msg']) ? $aResult['msg'] : '_bx_credits_err_cannot_perform_action')];

echoJson($aRes);
}
else {
$sContent = BxTemplFunctions::getInstance()->popupBox($this->_oModule->_oConfig->getHtmlIds($sAction . '_popup'), _t($CNF['T'][$sAction . '_popup']), $this->_oModule->_oTemplate->parseHtmlByName('credit_form.html', array(
$sContent = BxTemplFunctions::getInstance()->popupBox($this->_oModule->_oConfig->getHtmlIds($sAction . '_popup'), _t($CNF['T'][$sAction . '_popup']), $this->_oModule->_oTemplate->parseHtmlByName('credit_form.html', [
'form_id' => $oForm->aFormAttrs['id'],
'form' => $oForm->getCode(true),
'object' => $this->_sObject,
'action' => $sAction
)));
]));

echoJson(array('popup' => array('html' => $sContent, 'options' => array('closeOnOuterClick' => false))));
echoJson(['popup' => ['html' => $sContent, 'options' => ['closeOnOuterClick' => false]]]);
}
}

protected function _getFormObject($sAction, $aBundle = array())
protected function _getFormObject($sAction)
{
$CNF = &$this->_oModule->_oConfig->CNF;

Expand All @@ -158,17 +161,17 @@ protected function _getFormObject($sAction, $aBundle = array())
protected function _addJsCss()
{
parent::_addJsCss();
$this->_oModule->_oTemplate->addJs(array(
$this->_oModule->_oTemplate->addJs([
'jquery.form.min.js',
'withdraw.js'
));
]);

$this->_oModule->_oTemplate->addCss(array(
$this->_oModule->_oTemplate->addCss([
'main.css',
'withdraw.css'
));
]);

$oForm = new BxTemplFormView(array());
$oForm = new BxTemplFormView([]);
$oForm->addCssJs();
}
}
Expand Down
15 changes: 11 additions & 4 deletions modules/boonex/credits/classes/BxCreditsGridHistoryCommon.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,17 @@ public function __construct ($aOptions, $oTemplate = false)
parent::__construct ($aOptions, $oTemplate);
}

public function performActionSend()
{
$sAction = 'send';

$this->_performActionWithProfileAmount($sAction);
}

public function performActionWithdrawRequest()
{
if(!$this->_bWithdraw)
return echoJson(array());
return echoJson([]);

$sAction = 'withdraw_request';

Expand All @@ -38,10 +45,10 @@ protected function _getActionWithdrawRequest($sType, $sKey, $a, $isSmall = false

protected function _getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage)
{
if(empty($this->_aQueryAppend['profile_id']))
return array();
if(empty($this->_iUserId))
return [];

$this->_aOptions['source'] .= $this->_oModule->_oDb->prepareAsString(" AND `first_pid`=?", $this->_aQueryAppend['profile_id']);
$this->_aOptions['source'] .= $this->_oModule->_oDb->prepareAsString(" AND `first_pid`=?", $this->_iUserId);

return parent::_getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class BxCreditsGridOrdersAdministration extends BxTemplGrid
protected $_sModule;
protected $_oModule;

protected $_iUserId;

public function __construct ($aOptions, $oTemplate = false)
{
$this->_sModule = 'bx_credits';
Expand All @@ -26,9 +28,11 @@ public function __construct ($aOptions, $oTemplate = false)
$this->_sDefaultSortingOrder = 'DESC';
$this->_aQueryReset = array($this->_aOptions['order_get_field'], $this->_aOptions['order_get_dir'], $this->_aOptions['paginate_get_start'], $this->_aOptions['paginate_get_per_page']);

$iProfileId = bx_get_logged_profile_id();
if($iProfileId !== false)
$this->_aQueryAppend['profile_id'] = (int)$iProfileId;
$iUserId = bx_get_logged_profile_id();
if($iUserId !== false) {
$this->_iUserId = (int)$iUserId;
$this->_aQueryAppend['user_id'] = $this->_iUserId;
}
}

protected function _getCellProfileId($mixedValue, $sKey, $aField, $aRow)
Expand All @@ -38,11 +42,11 @@ protected function _getCellProfileId($mixedValue, $sKey, $aField, $aRow)

protected function _getCellBundle($mixedValue, $sKey, $aField, $aRow)
{
$mixedValue = $this->_oTemplate->parseHtmlByName('bundle_link.html', array(
'href' => $this->_oModule->_oConfig->getBundleUrl(array('id' => $aRow['bundle_id'])),
$mixedValue = $this->_oTemplate->parseHtmlByName('bundle_link.html', [
'href' => $this->_oModule->_oConfig->getBundleUrl(['id' => $aRow['bundle_id']]),
'title' => bx_html_attribute($mixedValue),
'content' => $mixedValue
));
]);

return parent::_getCellDefault($mixedValue, $sKey, $aField, $aRow);
}
Expand Down Expand Up @@ -70,7 +74,7 @@ protected function _getProfile($mixedValue)
if(!$oProfile)
return $mixedValue;

return $oProfile->getUnit(0, array('template' => array('name' => 'unit', 'size' => 'icon')));
return $oProfile->getUnit(0, ['template' => ['name' => 'unit', 'size' => 'icon']]);
}
}

Expand Down
6 changes: 3 additions & 3 deletions modules/boonex/credits/classes/BxCreditsGridOrdersCommon.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ public function __construct ($aOptions, $oTemplate = false)

protected function _getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage)
{
if(empty($this->_aQueryAppend['profile_id']))
return array();
if(empty($this->_iUserId))
return [];

$this->_aOptions['source'] .= $this->_oModule->_oDb->prepareAsString(" AND `to`.`profile_id`=?", $this->_aQueryAppend['profile_id']);
$this->_aOptions['source'] .= $this->_oModule->_oDb->prepareAsString(" AND `to`.`profile_id`=?", $this->_iUserId);

return parent::_getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage);
}
Expand Down
58 changes: 52 additions & 6 deletions modules/boonex/credits/classes/BxCreditsModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -764,17 +764,17 @@ public function updateProfileBalance($iFirstPid, $iSecondPid, $fAmount, $sOrder
return true;
}

public function processGrant($iProfileId, $fAmount, $sMessage = '')
public function processGrant($iUserId, $iProfileId, $fAmount, $sMessage = '')
{
$CNF = &$this->_oConfig->CNF;

$oProfile = BxDolProfile::getInstance($iProfileId);
if(!$oProfile)
return array('code' => 1, 'msg' => '_bx_credits_err_profile_not_found');
return ['code' => 2, 'msg' => '_bx_credits_err_profile_not_found'];

$sInfo = '_bx_credits_txt_history_info_grant';
if(!$this->updateProfileBalance($iProfileId, 0, $fAmount, '', $sInfo))
return array('code' => 2, 'msg' => '_bx_credits_err_cannot_update_balance');
return ['code' => 3, 'msg' => '_bx_credits_err_cannot_update_balance'];

sendMailTemplate($CNF['ETEMPLATE_GRANTED'], 0, $iProfileId, array(
'amount' => $fAmount,
Expand All @@ -789,7 +789,52 @@ public function processGrant($iProfileId, $fAmount, $sMessage = '')
return array('code' => 0);
}

public function processWithdrawRequest($iProfileId, $fAmount, $sMessage = '')
public function processSend($iUserId, $iProfileId, $fAmount, $sMessage = '')
{
$CNF = &$this->_oConfig->CNF;

$fAmountAvail = $this->getProfileBalance($iUserId);
if($fAmount > $fAmountAvail)
return ['code' => 1, 'msg' => '_bx_credits_err_low_balance'];

$oProfile = BxDolProfile::getInstance($iProfileId);
if(!$oProfile)
return ['code' => 2, 'msg' => '_bx_credits_err_profile_not_found'];

$sOrder = $this->_oConfig->getOrder();
$sInfo = !empty($sMessage) ? $sMessage : '_bx_credits_txt_history_info_send';

if(!$this->updateProfileBalance($iUserId, $iProfileId, -$fAmount, $sOrder, $sInfo))
return ['code' => 3, 'msg' => '_bx_credits_err_cannot_update_balance'];

if(!$this->updateProfileBalance($iProfileId, $iUserId, $fAmount, $sOrder, $sInfo)) {
$sInfo = '_bx_credits_txt_history_info_cancellation';
$this->updateProfileBalance($iUserId, $iProfileId, $fAmount, $sOrder, $sInfo);

return ['code' => 3, 'msg' => '_bx_credits_err_cannot_update_balance'];
}

$oUser = BxDolProfile::getInstance($iUserId);
sendMailTemplate($CNF['ETEMPLATE_RECEIVED'], 0, $iProfileId, [
'performer_id' => $iUserId,
'performer_name' => $oUser->getDisplayName(),
'performer_link' => $oUser->getUrl(),
'amount' => $fAmount,
'order' => $sOrder,
'message' => $sMessage
]);

bx_alert($this->getName(), 'sent', 0, $iUserId, [
'performer' => $iUserId,
'profile' => $iProfileId,
'amount' => $fAmount,
'order' => $sOrder,
]);

return ['code' => 0];
}

public function processWithdrawRequest($iUserId, $iProfileId, $fAmount, $sMessage = '')
{
$CNF = &$this->_oConfig->CNF;

Expand Down Expand Up @@ -840,7 +885,7 @@ public function processWithdrawRequest($iProfileId, $fAmount, $sMessage = '')
return array('code' => 0);
}

public function processWithdrawConfirm($iProfileId, $fAmount, $sMessage = '')
public function processWithdrawConfirm($iUserId, $iProfileId, $fAmount, $sMessage = '')
{
$CNF = &$this->_oConfig->CNF;

Expand All @@ -861,7 +906,8 @@ public function processWithdrawConfirm($iProfileId, $fAmount, $sMessage = '')
'message' => $sMessage
));

bx_alert($this->getName(), 'withdraw_sent', 0, false, array(
bx_alert($this->getName(), 'withdraw_sent', 0, $iUserId, array(
'performer' => $iUserId,
'profile' => $iProfileId,
'amount' => $fAmount,
));
Expand Down
Loading

0 comments on commit 3cee082

Please sign in to comment.