DataWriter User

Cupara

Well-known member
I'm having issues and not sure why but I'm using template modification to add the new field to the registration form.

When the form is submitted everything is added to the user table but the code isn't being passed.

Template Mod:
HTML:
<fieldset>
     <dl class="ctrlUnit">
       <dt><label for="ctrl_xfpoints_code">{xen:phrase xfpoints_promotion_code}:</label></dt>
       <dd><input type="text" name="xfpoints_code" class="textCtrl OptOut" id="ctrl_xfpoints_code" /></dd>
     </dl>
   </fieldset>

xfPoints_DataWriter_User:
PHP:
<?php
class xfPoints_DataWriter_User extends XFCP_xfPoints_DataWriter_User
{
   protected function _getFields()
   {
       $fields = parent::_getFields();
  $fields['xf_user'] += array('xfpoints_code' => array('type' => self::TYPE_STRING, 'default' => '0'));
       return $fields;
   }
   /**
    * Post-save handling.
    */
   protected function _postSave()
   {
     $options = XenForo_Application::get('options');
     $visitor = XenForo_Visitor::getInstance();
     $promoModel = $this->_getPromotionModel();

     $promo = $promoModel->getCodeByCode($this->get('xfpoints_code'));
     if ($promo['code'])
     {
         parent::_postSave();
         $pointsModel = $this->getModelFromCache('xfPoints_Model_Currency');
         $addPoints = false;
         if ($this->get('user_state') == 'valid')
         {
           if ($this->isInsert())
           {
             $addPoints = true;
           }
           else if ($this->isChanged('user_state'))
           {
             $previousState = $this->getExisting('user_state');
             if ($previousState == 'moderated' || $previousState == 'email_confirm')
             {
               $addPoints = true;
             }
           }
         }

         if ($addPoints)
         {
           $pointsEarned = $promo['amount'];
           $pointsModel->updateUserPoints($this->get('user_id'), $pointsEarned);
         }
     } else {
       if ($options->xfpoints_currency_register > '0')
       {
         parent::_postSave();
         $pointsModel = $this->getModelFromCache('xfPoints_Model_Currency');
         $addPoints = false;
         if ($this->get('user_state') == 'valid')
         {
           if ($this->isInsert())
           {
             $addPoints = true;
           }
           else if ($this->isChanged('user_state'))
           {
             $previousState = $this->getExisting('user_state');
             if ($previousState == 'moderated' || $previousState == 'email_confirm')
             {
               $addPoints = true;
             }
           }
         }

         if ($addPoints)
         {
           $pointsEarned = $this->getModelFromCache('xfPoints_Model_Currency')->assignUserPoints($this->get('user_id'), 'register');
           $pointsModel->updateUserPoints($this->get('user_id'), $pointsEarned);
         }
       }
     }
   }
   protected function _getPromotionModel()
   {
     return $this->getModelFromCache('xfPoints_Model_Promotion');
   }
}

If I fill the field via phpMySQLAdmin then approve the account it will grab the right data and alter the xfpoints_currency field. So the if condition above is working fine, just having an issue inserting the field from the form to the database.
 
PHP:
 $fields['xf_user'] += array('xfpoints_code' => array('type' => self::TYPE_STRING, 'default' => '0'));
I think its should be
Code:
$fields['xf_user'] += array('xfpoints_code' => array('type' => self::TYPE_STRING, 'default' => ''));
And look at your code, I think problems you get because You call parent wrong position. Try to execute your action then call parent :)
 
The issue probably lies within your controller.

How are you getting the input from your HTML form saved to the DataWriter?

If you invoke the DataWriter and set your data there, the moment you return the parent action, that is all lost because the parent action invokes the DataWriter and sets a different set of data.

So it's probably in your controller action where it is going wrong.
 
The issue probably lies within your controller.

How are you getting the input from your HTML form saved to the DataWriter?

If you invoke the DataWriter and set your data there, the moment you return the parent action, that is all lost because the parent action invokes the DataWriter and sets a different set of data.

So it's probably in your controller action where it is going wrong.
I don't have a controller as I didn't think it was needed but looking back of Account controller I realize I need a controller for Register so that it will process the field.
 
Back
Top Bottom