Cupara
Well-known member
Ok lets do this from the beginning. I made points_id primary, user_id unique. Now I have my confirm page, what should I be changing in my data writer listed below.
Ignore the _postDelete portion as that is not going to be used at all.
Next thing, how should my data writer look and should I change anything on my actionConfirm part?
actionConfirm:
PHP:
class xShop_DataWriter_UserPoints extends XenForo_DataWriter
{
protected function _getFields()
{
return array(
'xshop_points' => array(
'points_id' => array('type' => self::TYPE_UINT, 'autoIncrement' => true),
'user_id' => array('type' => self::TYPE_UINT),
'points_total' => array('type' => self::TYPE_UINT),
'points_earned' => array('type' => self::TYPE_UINT)
)
);
}
protected function _getExistingData($data)
{
if (!$userid = $this->_getExistingPrimaryKey($data, 'user_id'))
{
return false;
}
return array('xshop_points' => $this->getModelFromCache('xShop_Model_Points')->getUserPoints($userid));
}
protected function _getUpdateCondition($tableName)
{
return 'points_id = ' . $this->_db->quote($this->getExisting('points_id'));
}
/* protected function _postDelete()
{
$model = $this->getModelFromCache('xShop_Model_Stock');
$deletedId = $this->_existingData['xshop_stock']['member_id'];
if ($model->hasData($deletedId))
{
$dw->delete();
}
}*/
}
Ignore the _postDelete portion as that is not going to be used at all.
Next thing, how should my data writer look and should I change anything on my actionConfirm part?
actionConfirm:
PHP:
public function actionConfirm()
{
$this->_assertPostOnly();
$dwInput = $this->_input->filter(array(
'item_id' => XenForo_Input::UINT,
'item_name' => XenForo_Input::STRING,
'item_cost' => XenForo_Input::UINT,
'points_id' => XenForo_Input::UINT,
'points_total' => XenForo_Input::UINT,
'points_earned' => XenForo_Input::UINT,
'stock_order' => XenForo_Input::UINT,
'member_id' => XenForo_Input::UINT,
'stock_id' => XenForo_Input::UINT
));
$userId = $dwInput['member_id'];
$pointsModel = $this->_getPointsModel();
$exists = $pointsModel->getUserPoints($userId); // whatever function you use for this
$dw = XenForo_DataWriter::create('xShop_DataWriter_UserPoints');
if ($exists) // update
{
$dw->setExistingData($userId);
}
$points_total = $dw->get('points_total');
$new_total = $points_total - $dwInput['item_cost'];
$vars = array('user_id' => $userId, 'points_total' => $new_total);
$dw->bulkSet($vars);
$dw->preSave();
if ($dw->hasErrors())
{
$errors = $dw->getErrors();
$errorKey = reset($errors);
return false;
}
$dw->save();
$redirectType = ($userId ?
XenForo_ControllerResponse_Redirect::RESOURCE_UPDATED :
XenForo_ControllerResponse_Redirect::RESOURCE_CREATED);
return $this->responseRedirect(
$redirectType,
XenForo_Link::buildPublicLink('shop')
);
}