New column in posts xenforo table

ForestDrake

New member
Hello!
I have a problem.

How can I get post_id in addon with extended Thread controller in method actionAddReply()? I have in the post table a column is news (bool), also in the form I have an input when addReply is executed. If I use the datawriter I cant get the value from input so I cant get the post id inserted in the controller.

Please advice. Best regards.

My solution is bellow, but I have now the problem with controller, for all actions with Edit, add, inline-add, edit inline posts I have to create methods in extended controller. With datawrite can be better, look at the second example.
Code:
//controller extends
class HomePortal_ControllerPublic_Thread extends XFCP_HomePortal_ControllerPublic_Thread
{
    public $perms;

public function actionAddReply()
    {
        $response = parent::actionAddReply();

        $this->_assertPostOnly();

        if ($this->_input->inRequest('more_options'))
        {
            return $this->responseReroute(__CLASS__, 'reply');
        }

        $is_news = $this->_input->filterSingle('is_news', XenForo_Input::UINT);
        $threadId = $this->_input->filterSingle('thread_id', XenForo_Input::UINT);
        if ($this->perms['can_news'] && $is_news)
        {
            $db = XenForo_Application::get('db');

            $postId = $db->fetchRow("SELECT post_id FROM xf_post WHERE thread_id =" . $db->quote($threadId) . " order by post_id desc");

            $dw = XenForo_DataWriter::create('XenForo_DataWriter_DiscussionMessage_Post');
            $dw->setExistingData($postId);
            $dw->set('is_news', $is_news);

            $dw->save();
        }
        return $response;
    }

   protected function _preDispatch($action)
    {
        parent::_preDispatch($action);

        $this->perms = $this->getModelFromCache('HomePortal_Model_Perms')->getPermissions();
    }
}

//datawriter extends
class HomePortal_DataWriter_DiscussionMessage_Post extends XFCP_HomePortal_DataWriter_DiscussionMessage_Post
{
    //public $perms;

    protected function _getFields() {
        $fields = parent::_getFields();
        $fields['xf_post']['is_news'] = array('type' => self::TYPE_UINT, 'default' => '0');
        return $fields;
    }
}
Second example with datawriter only
Code:
class HomePortal_DataWriter_DiscussionMessage_Post extends XFCP_HomePortal_DataWriter_DiscussionMessage_Post
{
    public $perms;
    protected function _getFields() {
        $fields = parent::_getFields();
        $fields['xf_post']['is_news'] = array('type' => self::TYPE_UINT, 'default' => '0');
        return $fields;
    }


    protected function _messagePreSave()
    {
        $response = parent::_messagePreSave();

        //$_POST is not good here :(
        $is_news = $_POST['is_news'];
        if ($this->perms['can_news'])
        $this->set('is_news', $is_news);

        return $response;
    }
 
Last edited:
Back
Top Bottom